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