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