Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 1 | #include "board.h" |
| 2 | #include "utils.h" |
| 3 | #include "usb/device/dfu/dfu.h" |
| 4 | #include "usb/common/dfu/usb_dfu.h" |
| 5 | #include "manifest.h" |
| 6 | |
Harald Welte | 3bbaba0 | 2017-03-06 22:47:06 +0100 | [diff] [blame] | 7 | #include <osmocom/core/timer.h> |
| 8 | |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 9 | #define ALTIF_RAM 0 |
| 10 | #define ALTIF_FLASH 1 |
| 11 | |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 12 | unsigned int g_unique_id[4]; |
Kévin Redon | 869dbfa | 2018-06-17 22:36:44 +0200 | [diff] [blame] | 13 | /* remember if the watchdog has been configured in the main loop so we can kick it in the ISR */ |
| 14 | static bool watchdog_configured = false; |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 15 | |
Kévin Redon | 9def763 | 2018-06-27 16:27:41 +0200 | [diff] [blame] | 16 | /* There is not enough space in the 16 KiB DFU bootloader to include led.h functions */ |
| 17 | #ifdef PINS_LEDS |
| 18 | /** LED pin configurations */ |
| 19 | static const Pin pinsLeds[] = { PINS_LEDS } ; |
| 20 | #endif |
| 21 | |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 22 | /*---------------------------------------------------------------------------- |
| 23 | * Callbacks |
| 24 | *----------------------------------------------------------------------------*/ |
| 25 | |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 26 | #define RAM_ADDR(offset) (IRAM_ADDR + BOARD_DFU_RAM_SIZE + offset) |
| 27 | #define FLASH_ADDR(offset) (IFLASH_ADDR + BOARD_DFU_BOOT_SIZE + offset) |
| 28 | |
| 29 | #define IFLASH_END ((uint8_t *)IFLASH_ADDR + IFLASH_SIZE) |
| 30 | #define IRAM_END ((uint8_t *)IRAM_ADDR + IRAM_SIZE) |
| 31 | |
Kévin Redon | 318309f | 2018-06-01 11:02:49 +0200 | [diff] [blame] | 32 | /* incoming call-back: Host has transferred 'len' bytes (stored at |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 33 | * 'data'), which we shall write to 'offset' into the partition |
Kévin Redon | 318309f | 2018-06-01 11:02:49 +0200 | [diff] [blame] | 34 | * associated with 'altif'. Guaranted to be less than |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 35 | * BOARD_DFU_PAGE_SIZE */ |
| 36 | int USBDFU_handle_dnload(uint8_t altif, unsigned int offset, |
| 37 | uint8_t *data, unsigned int len) |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 38 | { |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 39 | uint32_t addr; |
Harald Welte | 1cfc261 | 2018-06-29 21:07:41 +0200 | [diff] [blame] | 40 | unsigned int i; |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 41 | int rc; |
Kévin Redon | 318309f | 2018-06-01 11:02:49 +0200 | [diff] [blame] | 42 | /* address of the last allocated variable on the stack */ |
| 43 | uint32_t stack_addr = (uint32_t)&rc; |
Kévin Redon | 869dbfa | 2018-06-17 22:36:44 +0200 | [diff] [blame] | 44 | /* kick the dog to have enough time to flash */ |
| 45 | if (watchdog_configured) { |
| 46 | WDT_Restart(WDT); |
| 47 | } |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 48 | |
Harald Welte | e8eea29 | 2017-03-03 00:35:51 +0100 | [diff] [blame] | 49 | printf("dnload(altif=%u, offset=%u, len=%u)\n\r", altif, offset, len); |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 50 | |
Kévin Redon | 9def763 | 2018-06-27 16:27:41 +0200 | [diff] [blame] | 51 | #ifdef PINS_LEDS |
| 52 | PIO_Clear(&pinsLeds[LED_NUM_RED]); |
| 53 | #endif |
| 54 | |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 55 | switch (altif) { |
| 56 | case ALTIF_RAM: |
| 57 | addr = RAM_ADDR(offset); |
Kévin Redon | b73f0a0 | 2018-06-17 22:33:29 +0200 | [diff] [blame] | 58 | if (addr < IRAM_ADDR || addr + len >= IRAM_ADDR + IRAM_SIZE || addr + len >= stack_addr) { |
Harald Welte | adba0ce | 2017-02-28 01:02:24 +0100 | [diff] [blame] | 59 | g_dfu->state = DFU_STATE_dfuERROR; |
| 60 | g_dfu->status = DFU_STATUS_errADDRESS; |
Kévin Redon | 9def763 | 2018-06-27 16:27:41 +0200 | [diff] [blame] | 61 | rc = DFU_RET_STALL; |
| 62 | break; |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 63 | } |
| 64 | memcpy((void *)addr, data, len); |
Kévin Redon | 9def763 | 2018-06-27 16:27:41 +0200 | [diff] [blame] | 65 | rc = DFU_RET_ZLP; |
| 66 | break; |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 67 | case ALTIF_FLASH: |
| 68 | addr = FLASH_ADDR(offset); |
Kévin Redon | b73f0a0 | 2018-06-17 22:33:29 +0200 | [diff] [blame] | 69 | if (addr < IFLASH_ADDR || addr + len >= IFLASH_ADDR + IFLASH_SIZE) { |
Harald Welte | adba0ce | 2017-02-28 01:02:24 +0100 | [diff] [blame] | 70 | g_dfu->state = DFU_STATE_dfuERROR; |
| 71 | g_dfu->status = DFU_STATUS_errADDRESS; |
Kévin Redon | 9def763 | 2018-06-27 16:27:41 +0200 | [diff] [blame] | 72 | rc = DFU_RET_STALL; |
| 73 | break; |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 74 | } |
Kévin Redon | b73f0a0 | 2018-06-17 22:33:29 +0200 | [diff] [blame] | 75 | rc = FLASHD_Unlock(addr, addr + len, 0, 0); |
| 76 | if (rc != 0) { |
| 77 | TRACE_ERROR("DFU download flash unlock failed\n\r"); |
Kévin Redon | 9def763 | 2018-06-27 16:27:41 +0200 | [diff] [blame] | 78 | rc = DFU_RET_STALL; |
| 79 | break; |
Kévin Redon | b73f0a0 | 2018-06-17 22:33:29 +0200 | [diff] [blame] | 80 | } |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 81 | rc = FLASHD_Write(addr, data, len); |
| 82 | if (rc != 0) { |
Kévin Redon | b73f0a0 | 2018-06-17 22:33:29 +0200 | [diff] [blame] | 83 | TRACE_ERROR("DFU download flash erase failed\n\r"); |
Kévin Redon | 9def763 | 2018-06-27 16:27:41 +0200 | [diff] [blame] | 84 | rc = DFU_RET_STALL; |
| 85 | break; |
Kévin Redon | b73f0a0 | 2018-06-17 22:33:29 +0200 | [diff] [blame] | 86 | } |
Harald Welte | 1cfc261 | 2018-06-29 21:07:41 +0200 | [diff] [blame] | 87 | for (i = 0; i < len; i++) { |
Kévin Redon | b73f0a0 | 2018-06-17 22:33:29 +0200 | [diff] [blame] | 88 | if (((uint8_t*)addr)[i]!=data[i]) { |
| 89 | TRACE_ERROR("DFU download flash data written not correct\n\r"); |
Kévin Redon | 9def763 | 2018-06-27 16:27:41 +0200 | [diff] [blame] | 90 | rc = DFU_RET_STALL; |
| 91 | break; |
Kévin Redon | b73f0a0 | 2018-06-17 22:33:29 +0200 | [diff] [blame] | 92 | } |
| 93 | } |
Kévin Redon | 9def763 | 2018-06-27 16:27:41 +0200 | [diff] [blame] | 94 | rc = DFU_RET_ZLP; |
| 95 | break; |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 96 | default: |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 97 | TRACE_ERROR("DFU download for unknown AltIf %d\n\r", altif); |
Kévin Redon | 9def763 | 2018-06-27 16:27:41 +0200 | [diff] [blame] | 98 | rc = DFU_RET_STALL; |
| 99 | break; |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 100 | } |
Kévin Redon | 9def763 | 2018-06-27 16:27:41 +0200 | [diff] [blame] | 101 | |
| 102 | #ifdef PINS_LEDS |
| 103 | PIO_Set(&pinsLeds[LED_NUM_RED]); |
| 104 | #endif |
| 105 | |
| 106 | return rc; |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 107 | } |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 108 | |
| 109 | /* incoming call-back: Host has requested to read back 'req_len' bytes |
| 110 | * starting from 'offset' of the firmware * associated with partition |
| 111 | * 'altif' */ |
| 112 | int USBDFU_handle_upload(uint8_t altif, unsigned int offset, |
| 113 | uint8_t *data, unsigned int req_len) |
| 114 | { |
| 115 | uint32_t addr; |
| 116 | |
| 117 | printf("upload(altif=%u, offset=%u, len=%u)", altif, offset, req_len); |
| 118 | |
| 119 | switch (altif) { |
| 120 | case ALTIF_RAM: |
| 121 | addr = RAM_ADDR(offset); |
| 122 | if (addr > IRAM_ADDR + IRAM_SIZE) { |
Harald Welte | adba0ce | 2017-02-28 01:02:24 +0100 | [diff] [blame] | 123 | g_dfu->state = DFU_STATE_dfuERROR; |
| 124 | g_dfu->status = DFU_STATUS_errADDRESS; |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 125 | return -1; |
| 126 | } |
| 127 | if ((uint8_t *)addr + req_len > IRAM_END) |
| 128 | req_len = IRAM_END - (uint8_t *)addr; |
| 129 | memcpy(data, (void *)addr, req_len); |
| 130 | break; |
| 131 | case ALTIF_FLASH: |
| 132 | addr = FLASH_ADDR(offset); |
| 133 | if (addr > IFLASH_ADDR + IFLASH_SIZE) { |
Harald Welte | adba0ce | 2017-02-28 01:02:24 +0100 | [diff] [blame] | 134 | g_dfu->state = DFU_STATE_dfuERROR; |
| 135 | g_dfu->status = DFU_STATUS_errADDRESS; |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 136 | return -1; |
| 137 | } |
| 138 | if ((uint8_t *)addr + req_len > IFLASH_END) |
| 139 | req_len = IFLASH_END - (uint8_t *)addr; |
| 140 | memcpy(data, (void *)addr, req_len); |
| 141 | break; |
| 142 | default: |
| 143 | TRACE_ERROR("DFU upload for unknown AltIf %d\n\r", altif); |
| 144 | /* FIXME: set error codes */ |
| 145 | return -1; |
| 146 | } |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 147 | printf("=%u\n\r", req_len); |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 148 | return req_len; |
| 149 | } |
| 150 | |
Harald Welte | 27f5fc6 | 2017-11-28 22:15:56 +0100 | [diff] [blame] | 151 | /* can be overridden by board specific code, e.g. by pushbutton */ |
| 152 | WEAK int board_override_enter_dfu(void) |
Harald Welte | 1405100 | 2017-03-04 19:17:27 +0100 | [diff] [blame] | 153 | { |
Harald Welte | 27f5fc6 | 2017-11-28 22:15:56 +0100 | [diff] [blame] | 154 | return 0; |
Harald Welte | 1405100 | 2017-03-04 19:17:27 +0100 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /* using this function we can determine if we should enter DFU mode |
| 158 | * during boot, or if we should proceed towards the application/runtime */ |
| 159 | int USBDFU_OverrideEnterDFU(void) |
| 160 | { |
| 161 | uint32_t *app_part = (uint32_t *)FLASH_ADDR(0); |
| 162 | |
| 163 | /* If the loopback jumper is set, we enter DFU mode */ |
Harald Welte | 27f5fc6 | 2017-11-28 22:15:56 +0100 | [diff] [blame] | 164 | if (board_override_enter_dfu()) |
Harald Welte | 1405100 | 2017-03-04 19:17:27 +0100 | [diff] [blame] | 165 | return 1; |
| 166 | |
| 167 | /* if the first word of the application partition doesn't look |
| 168 | * like a stack pointer (i.e. point to RAM), enter DFU mode */ |
| 169 | if ((app_part[0] < IRAM_ADDR) || |
| 170 | ((uint8_t *)app_part[0] > IRAM_END)) |
| 171 | return 1; |
| 172 | |
| 173 | /* if the second word of the application partition doesn't look |
| 174 | * like a function from flash (reset vector), enter DFU mode */ |
| 175 | if (((uint32_t *)app_part[1] < app_part) || |
| 176 | ((uint8_t *)app_part[1] > IFLASH_END)) |
| 177 | return 1; |
| 178 | |
| 179 | return 0; |
| 180 | } |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 181 | |
| 182 | /* returns '1' in case we should break any endless loop */ |
| 183 | static void check_exec_dbg_cmd(void) |
| 184 | { |
| 185 | int ch; |
| 186 | |
| 187 | if (!UART_IsRxReady()) |
| 188 | return; |
| 189 | |
| 190 | ch = UART_GetChar(); |
| 191 | |
| 192 | //board_exec_dbg_cmd(ch); |
| 193 | } |
| 194 | |
| 195 | /*------------------------------------------------------------------------------ |
| 196 | * Main |
| 197 | *------------------------------------------------------------------------------*/ |
| 198 | #define MAX_USB_ITER BOARD_MCK/72 // This should be around a second |
| 199 | extern int main(void) |
| 200 | { |
| 201 | uint8_t isUsbConnected = 0; |
| 202 | unsigned int i = 0; |
Harald Welte | 5b108d8 | 2017-03-06 21:51:55 +0100 | [diff] [blame] | 203 | uint32_t reset_cause = (RSTC->RSTC_SR & RSTC_SR_RSTTYP_Msk) >> RSTC_SR_RSTTYP_Pos; |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 204 | |
Kévin Redon | 869dbfa | 2018-06-17 22:36:44 +0200 | [diff] [blame] | 205 | /* Enable watchdog for 2000ms, with no window */ |
Harald Welte | 45ebe45 | 2017-03-03 19:01:53 +0100 | [diff] [blame] | 206 | WDT_Enable(WDT, WDT_MR_WDRSTEN | WDT_MR_WDDBGHLT | WDT_MR_WDIDLEHLT | |
Kévin Redon | 869dbfa | 2018-06-17 22:36:44 +0200 | [diff] [blame] | 207 | (WDT_GetPeriod(2000) << 16) | WDT_GetPeriod(2000)); |
| 208 | watchdog_configured = true; |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 209 | |
Kévin Redon | 9def763 | 2018-06-27 16:27:41 +0200 | [diff] [blame] | 210 | #ifdef PINS_LEDS |
| 211 | /* Configure LED */ |
| 212 | PIO_Configure(pinsLeds, sizeof(pinsLeds)); |
| 213 | PIO_Set(&pinsLeds[LED_NUM_RED]); |
| 214 | PIO_Clear(&pinsLeds[LED_NUM_GREEN]); |
| 215 | #endif |
| 216 | |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 217 | PIO_InitializeInterrupts(0); |
| 218 | |
| 219 | EEFC_ReadUniqueID(g_unique_id); |
| 220 | |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 221 | printf("\n\r\n\r" |
| 222 | "=============================================================================\n\r" |
| 223 | "DFU bootloader %s for board %s (C) 2010-2017 by Harald Welte\n\r" |
| 224 | "=============================================================================\n\r", |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 225 | manifest_revision, manifest_board); |
| 226 | |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 227 | TRACE_INFO("Chip ID: 0x%08x (Ext 0x%08x)\n\r", CHIPID->CHIPID_CIDR, CHIPID->CHIPID_EXID); |
| 228 | TRACE_INFO("Serial Nr. %08x-%08x-%08x-%08x\n\r", |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 229 | g_unique_id[0], g_unique_id[1], |
| 230 | g_unique_id[2], g_unique_id[3]); |
Harald Welte | 5b108d8 | 2017-03-06 21:51:55 +0100 | [diff] [blame] | 231 | TRACE_INFO("Reset Cause: 0x%x\n\r", reset_cause); |
| 232 | |
| 233 | /* clear g_dfu on power-up reset */ |
| 234 | if (reset_cause == 0) |
| 235 | memset(g_dfu, 0, sizeof(*g_dfu)); |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 236 | |
Harald Welte | 8adf0ac | 2017-03-03 01:52:34 +0100 | [diff] [blame] | 237 | board_main_top(); |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 238 | |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 239 | TRACE_INFO("USB init...\n\r"); |
Kévin Redon | f5869d4 | 2018-06-17 22:31:21 +0200 | [diff] [blame] | 240 | /* Signal USB reset by disabling the pull-up on USB D+ for at least 10 ms */ |
| 241 | const Pin usb_dp_pullup = PIN_USB_PULLUP; |
| 242 | PIO_Configure(&usb_dp_pullup, 1); |
| 243 | PIO_Set(&usb_dp_pullup); |
| 244 | mdelay(15); |
| 245 | PIO_Clear(&usb_dp_pullup); |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 246 | USBDFU_Initialize(&dfu_descriptors); |
| 247 | |
| 248 | while (USBD_GetState() < USBD_STATE_CONFIGURED) { |
Harald Welte | 45ebe45 | 2017-03-03 19:01:53 +0100 | [diff] [blame] | 249 | WDT_Restart(WDT); |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 250 | check_exec_dbg_cmd(); |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 251 | #if 1 |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 252 | if (i >= MAX_USB_ITER * 3) { |
| 253 | TRACE_ERROR("Resetting board (USB could " |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 254 | "not be configured)\n\r"); |
Harald Welte | f415d71 | 2017-03-03 01:51:43 +0100 | [diff] [blame] | 255 | USBD_Disconnect(); |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 256 | NVIC_SystemReset(); |
| 257 | } |
| 258 | #endif |
| 259 | i++; |
| 260 | } |
| 261 | |
Kévin Redon | b73f0a0 | 2018-06-17 22:33:29 +0200 | [diff] [blame] | 262 | /* Initialize the flash to be able to write it, using the IAP ROM code */ |
Harald Welte | ec9b5ff | 2017-03-02 23:18:40 +0100 | [diff] [blame] | 263 | FLASHD_Initialize(BOARD_MCK, 1); |
Kévin Redon | b73f0a0 | 2018-06-17 22:33:29 +0200 | [diff] [blame] | 264 | |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 265 | TRACE_INFO("entering main loop...\n\r"); |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 266 | while (1) { |
Harald Welte | 45ebe45 | 2017-03-03 19:01:53 +0100 | [diff] [blame] | 267 | WDT_Restart(WDT); |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 268 | #if TRACE_LEVEL >= TRACE_LEVEL_DEBUG |
| 269 | const char rotor[] = { '-', '\\', '|', '/' }; |
| 270 | putchar('\b'); |
| 271 | putchar(rotor[i++ % ARRAY_SIZE(rotor)]); |
| 272 | #endif |
| 273 | check_exec_dbg_cmd(); |
Harald Welte | 3bbaba0 | 2017-03-06 22:47:06 +0100 | [diff] [blame] | 274 | #if 0 |
| 275 | osmo_timers_prepare(); |
| 276 | osmo_timers_update(); |
| 277 | #endif |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 278 | |
| 279 | if (USBD_GetState() < USBD_STATE_CONFIGURED) { |
| 280 | |
| 281 | if (isUsbConnected) { |
| 282 | isUsbConnected = 0; |
| 283 | } |
| 284 | } else if (isUsbConnected == 0) { |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 285 | TRACE_INFO("USB is now configured\n\r"); |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 286 | |
| 287 | isUsbConnected = 1; |
| 288 | } |
| 289 | } |
| 290 | } |