From 1d8ebcd75f9191cdbb0f6ebde2cb80ff03f1d815 Mon Sep 17 00:00:00 2001 From: Uwe Bonnes Date: Fri, 3 Mar 2017 13:12:14 +0100 Subject: [PATCH] Handle SET_ADDRESS according to DFUSE specs. --- src/platforms/stm32/dfucore.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/platforms/stm32/dfucore.c b/src/platforms/stm32/dfucore.c index 7b733d7..38cb421 100644 --- a/src/platforms/stm32/dfucore.c +++ b/src/platforms/stm32/dfucore.c @@ -48,6 +48,7 @@ static struct { uint32_t addr; uint16_t blocknum; } prog; +static uint8_t current_error; const struct usb_device_descriptor dev = { .bLength = USB_DT_DEVICE_SIZE, @@ -149,7 +150,8 @@ static uint8_t usbdfu_getstatus(uint32_t *bwPollTimeout) /* Device will reset when read is complete */ usbdfu_state = STATE_DFU_MANIFEST; return DFU_STATUS_OK; - + case STATE_DFU_ERROR: + return current_error; default: return DFU_STATUS_OK; } @@ -159,23 +161,17 @@ static void usbdfu_getstatus_complete(usbd_device *dev, struct usb_setup_data *req) { (void)req; + (void)dev; switch(usbdfu_state) { case STATE_DFU_DNBUSY: flash_unlock(); if(prog.blocknum == 0) { - uint32_t addr = get_le32(prog.buf + 1); - if ((addr < app_address) || (addr >= max_address)) { - flash_lock(); - usbd_ep_stall_set(dev, 0, 1); - return; - } + int32_t addr = get_le32(prog.buf + 1); switch(prog.buf[0]) { case CMD_ERASE: dfu_check_and_do_sector_erase(addr); - case CMD_SETADDR: - prog.addr = addr; } } else { uint32_t baseaddr = prog.addr + @@ -218,6 +214,15 @@ static int usbdfu_control_request(usbd_device *dev, prog.blocknum = req->wValue; prog.len = *len; memcpy(prog.buf, *buf, *len); + if ((req->wValue == 0) && (prog.buf[0] == CMD_SETADDR)) { + uint32_t addr = get_le32(prog.buf + 1); + if ((addr < app_address) || (addr >= max_address)) { + current_error = DFU_STATUS_ERR_TARGET; + usbdfu_state = STATE_DFU_ERROR; + return 1; + } else + prog.addr = addr; + } usbdfu_state = STATE_DFU_DNLOAD_SYNC; return 1; }