From 24c416cc66577a2cfbe8d6f01e1386340fc042f4 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 8 Jun 2018 15:29:52 +1000 Subject: [PATCH] stm32/mboot: Increase USB rx_buf and DFU buf sizes to full 2048 bytes. The DFU USB config descriptor returns 0x0800=2048 for the supported transfer size, and this applies to both TX (IN) and RX (OUT). So increase the rx_buf to support this size without having a buffer overflow on received data. With this patch mboot in USB DFU mode now works with dfu-util. --- ports/stm32/mboot/main.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/ports/stm32/mboot/main.c b/ports/stm32/mboot/main.c index b602f19964..a87507fd50 100644 --- a/ports/stm32/mboot/main.c +++ b/ports/stm32/mboot/main.c @@ -619,6 +619,8 @@ uint8_t i2c_slave_process_tx_byte(void) { /******************************************************************************/ // DFU +#define DFU_XFER_SIZE (2048) + enum { DFU_DNLOAD = 1, DFU_UPLOAD = 2, @@ -649,7 +651,7 @@ typedef struct _dfu_state_t { uint16_t wBlockNum; uint16_t wLength; uint32_t addr; - uint8_t buf[64] __attribute__((aligned(4))); + uint8_t buf[DFU_XFER_SIZE] __attribute__((aligned(4))); } dfu_state_t; static dfu_state_t dfu_state; @@ -762,7 +764,7 @@ static int dfu_handle_tx(int cmd, int arg, int len, uint8_t *buf, int max_len) { /******************************************************************************/ // USB -#define USB_TX_LEN (2048) +#define USB_XFER_SIZE (DFU_XFER_SIZE) enum { USB_PHY_FS_ID = 0, @@ -776,8 +778,8 @@ typedef struct _pyb_usbdd_obj_t { uint8_t bRequest; uint16_t wValue; uint16_t wLength; - uint8_t rx_buf[64]; - uint8_t tx_buf[USB_TX_LEN]; + uint8_t rx_buf[USB_XFER_SIZE]; + uint8_t tx_buf[USB_XFER_SIZE]; bool tx_pending; // RAM to hold the current descriptors, which we configure on the fly @@ -800,7 +802,7 @@ static const uint8_t dev_descr[0x12] = "\x12\x01\x00\x01\x00\x00\x00\x40\x83\x04 static uint8_t cfg_descr[9 + 9 + 9] = "\x09\x02\x1b\x00\x01\x01\x00\xc0\x32" "\x09\x04\x00\x00\x00\xfe\x01\x02\x04" - "\x09\x21\x0b\xff\x00\x00\x08\x1a\x01" // \x00\x08 goes with tx_buf[USB_TX_LEN] + "\x09\x21\x0b\xff\x00\x00\x08\x1a\x01" // \x00\x08 goes with USB_XFER_SIZE ; static uint8_t *pyb_usbdd_DeviceDescriptor(USBD_HandleTypeDef *pdev, uint16_t *length) { @@ -908,7 +910,7 @@ static uint8_t pyb_usbdd_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *r } } else if (req->bmRequest == 0xa1) { // device-to-host request - int len = dfu_handle_tx(self->bRequest, self->wValue, self->wLength, self->tx_buf, USB_TX_LEN); + int len = dfu_handle_tx(self->bRequest, self->wValue, self->wLength, self->tx_buf, USB_XFER_SIZE); if (len >= 0) { self->tx_pending = true; USBD_CtlSendData(&self->hUSBDDevice, self->tx_buf, len);