tr4mpass/include/exploit/dfu_proto.h
tr4m0ryp 82bec20452 feat(exploit): Add DFU protocol state machine
Implemented DFU protocol operations (DNLOAD, UPLOAD, GETSTATUS,
CLRSTATUS, GETSTATE, ABORT) with proper byte ordering for status
parsing. Foundation for checkm8 exploit delivery.

Files created:
- include/exploit/dfu_proto.h
- src/exploit/dfu_proto.c

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-05 02:11:25 +02:00

113 lines
3.7 KiB
C

/* dfu_proto.h -- DFU protocol state machine for checkm8 exploit delivery */
#ifndef DFU_PROTO_H
#define DFU_PROTO_H
#include <stddef.h>
#include <stdint.h>
#include <libusb-1.0/libusb.h>
/* ------------------------------------------------------------------ */
/* DFU class-specific request codes (USB DFU 1.1 spec, Table 3.2) */
/* ------------------------------------------------------------------ */
#define DFU_REQ_DETACH 0x00
#define DFU_REQ_DNLOAD 0x01
#define DFU_REQ_UPLOAD 0x02
#define DFU_REQ_GETSTATUS 0x03
#define DFU_REQ_CLRSTATUS 0x04
#define DFU_REQ_GETSTATE 0x05
#define DFU_REQ_ABORT 0x06
/* ------------------------------------------------------------------ */
/* DFU state values (bState field in status response) */
/* ------------------------------------------------------------------ */
#define DFU_STATE_IDLE 0x02
#define DFU_STATE_DNLOAD_SYNC 0x03
#define DFU_STATE_DNLOAD_IDLE 0x05
#define DFU_STATE_MANIFEST_SYNC 0x06
#define DFU_STATE_UPLOAD_IDLE 0x09
#define DFU_STATE_ERROR 0x0A
/* ------------------------------------------------------------------ */
/* DFU status values (bStatus field in status response) */
/* ------------------------------------------------------------------ */
#define DFU_STATUS_OK 0x00
#define DFU_STATUS_ERR_TARGET 0x01
#define DFU_STATUS_ERR_FILE 0x02
#define DFU_STATUS_ERR_WRITE 0x03
#define DFU_STATUS_ERR_UNKNOWN 0x07
#define DFU_STATUS_ERR_STALLED 0x0F
/* ------------------------------------------------------------------ */
/* DFU status structure */
/* ------------------------------------------------------------------ */
typedef struct {
uint8_t bStatus;
uint32_t bwPollTimeout; /* 3 bytes on wire, stored as uint32 */
uint8_t bState;
uint8_t iString;
} dfu_status_t;
/* ------------------------------------------------------------------ */
/* Protocol API */
/* ------------------------------------------------------------------ */
/*
* Query device status. Sends DFU_GETSTATUS and parses the 6-byte
* response into the dfu_status_t structure.
* Returns 0 on success, -1 on failure.
*/
int dfu_get_status(libusb_device_handle *dev, dfu_status_t *status);
/*
* Clear error status. Sends DFU_CLRSTATUS with no data stage.
* Returns 0 on success, -1 on failure.
*/
int dfu_clr_status(libusb_device_handle *dev);
/*
* Query current DFU state byte.
* Returns 0 on success, -1 on failure.
*/
int dfu_get_state(libusb_device_handle *dev, uint8_t *state);
/*
* Send a DFU_DNLOAD request with the given block number and data.
* A zero-length transfer (len=0) signals end of download.
* Returns 0 on success, -1 on failure.
*/
int dfu_dnload(libusb_device_handle *dev, uint16_t block_num,
const void *data, size_t len);
/*
* Send a DFU_UPLOAD request to read data from the device.
* On success, *actual is set to the number of bytes received.
* Returns 0 on success, -1 on failure.
*/
int dfu_upload(libusb_device_handle *dev, uint16_t block_num,
void *buf, size_t len, size_t *actual);
/*
* Abort the current DFU operation.
* Returns 0 on success, -1 on failure.
*/
int dfu_abort(libusb_device_handle *dev);
/*
* Send a data buffer via DFU_DNLOAD followed by a GETSTATUS poll.
* Returns 0 on success, -1 on failure.
*/
int dfu_send_data(libusb_device_handle *dev, const void *data, size_t len);
/*
* Reset the DFU state machine to dfuIDLE by clearing errors and
* aborting any in-progress operation.
* Returns 0 on success, -1 on failure.
*/
int dfu_reset_to_idle(libusb_device_handle *dev);
#endif /* DFU_PROTO_H */