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 | |
| 24 | /* incoming call-back: Host has transfered 'len' bytes (stored at |
| 25 | * 'data'), which we shall write to 'offset' into the partition |
| 26 | * associated with 'altif'. Guaranted to be les than |
| 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; |
| 33 | |
Harald Welte | e8eea29 | 2017-03-03 00:35:51 +0100 | [diff] [blame] | 34 | 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] | 35 | |
| 36 | switch (altif) { |
| 37 | case ALTIF_RAM: |
| 38 | addr = RAM_ADDR(offset); |
| 39 | if (addr > IRAM_ADDR + IRAM_SIZE) { |
Harald Welte | adba0ce | 2017-02-28 01:02:24 +0100 | [diff] [blame] | 40 | g_dfu->state = DFU_STATE_dfuERROR; |
| 41 | g_dfu->status = DFU_STATUS_errADDRESS; |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 42 | return DFU_RET_STALL; |
| 43 | } |
| 44 | memcpy((void *)addr, data, len); |
| 45 | return DFU_RET_ZLP; |
| 46 | case ALTIF_FLASH: |
| 47 | addr = FLASH_ADDR(offset); |
| 48 | if (addr > IFLASH_ADDR + IFLASH_SIZE) { |
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 | rc = FLASHD_Write(addr, data, len); |
| 54 | if (rc != 0) { |
| 55 | /* FIXME: set error codes */ |
| 56 | return DFU_RET_STALL; |
| 57 | } |
| 58 | return DFU_RET_ZLP; |
| 59 | default: |
| 60 | /* FIXME: set error codes */ |
| 61 | TRACE_ERROR("DFU download for unknown AltIf %d\n\r", altif); |
| 62 | return DFU_RET_STALL; |
| 63 | } |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 64 | } |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 65 | |
| 66 | /* incoming call-back: Host has requested to read back 'req_len' bytes |
| 67 | * starting from 'offset' of the firmware * associated with partition |
| 68 | * 'altif' */ |
| 69 | int USBDFU_handle_upload(uint8_t altif, unsigned int offset, |
| 70 | uint8_t *data, unsigned int req_len) |
| 71 | { |
| 72 | uint32_t addr; |
| 73 | |
| 74 | printf("upload(altif=%u, offset=%u, len=%u)", altif, offset, req_len); |
| 75 | |
| 76 | switch (altif) { |
| 77 | case ALTIF_RAM: |
| 78 | addr = RAM_ADDR(offset); |
| 79 | if (addr > IRAM_ADDR + IRAM_SIZE) { |
Harald Welte | adba0ce | 2017-02-28 01:02:24 +0100 | [diff] [blame] | 80 | g_dfu->state = DFU_STATE_dfuERROR; |
| 81 | g_dfu->status = DFU_STATUS_errADDRESS; |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 82 | return -1; |
| 83 | } |
| 84 | if ((uint8_t *)addr + req_len > IRAM_END) |
| 85 | req_len = IRAM_END - (uint8_t *)addr; |
| 86 | memcpy(data, (void *)addr, req_len); |
| 87 | break; |
| 88 | case ALTIF_FLASH: |
| 89 | addr = FLASH_ADDR(offset); |
| 90 | if (addr > IFLASH_ADDR + IFLASH_SIZE) { |
Harald Welte | adba0ce | 2017-02-28 01:02:24 +0100 | [diff] [blame] | 91 | g_dfu->state = DFU_STATE_dfuERROR; |
| 92 | g_dfu->status = DFU_STATUS_errADDRESS; |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 93 | return -1; |
| 94 | } |
| 95 | if ((uint8_t *)addr + req_len > IFLASH_END) |
| 96 | req_len = IFLASH_END - (uint8_t *)addr; |
| 97 | memcpy(data, (void *)addr, req_len); |
| 98 | break; |
| 99 | default: |
| 100 | TRACE_ERROR("DFU upload for unknown AltIf %d\n\r", altif); |
| 101 | /* FIXME: set error codes */ |
| 102 | return -1; |
| 103 | } |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 104 | printf("=%u\n\r", req_len); |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 105 | return req_len; |
| 106 | } |
| 107 | |
Harald Welte | 1405100 | 2017-03-04 19:17:27 +0100 | [diff] [blame] | 108 | static int uart_has_loopback_jumper(void) |
| 109 | { |
| 110 | unsigned int i; |
| 111 | const Pin uart_loopback_pins[] = { |
| 112 | {PIO_PA9A_URXD0, PIOA, ID_PIOA, PIO_INPUT, PIO_DEFAULT}, |
| 113 | {PIO_PA10A_UTXD0, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT} |
| 114 | }; |
| 115 | |
| 116 | /* Configure UART pins as I/O */ |
| 117 | PIO_Configure(uart_loopback_pins, PIO_LISTSIZE(uart_loopback_pins)); |
| 118 | |
| 119 | for (i = 0; i < 10; i++) { |
| 120 | /* Set TxD high; abort if RxD doesn't go high either */ |
| 121 | PIO_Set(&uart_loopback_pins[1]); |
| 122 | if (!PIO_Get(&uart_loopback_pins[0])) |
| 123 | return 0; |
| 124 | /* Set TxD low, abort if RxD doesn't go low either */ |
| 125 | PIO_Clear(&uart_loopback_pins[1]); |
| 126 | if (PIO_Get(&uart_loopback_pins[0])) |
| 127 | return 0; |
| 128 | } |
| 129 | /* if we reached here, RxD always follows TxD and thus a |
| 130 | * loopback jumper has been placed on RxD/TxD, and we will boot |
| 131 | * into DFU unconditionally */ |
| 132 | return 1; |
| 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 */ |
| 142 | if (uart_has_loopback_jumper()) |
| 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 | |
| 193 | //req_ctx_init(); |
| 194 | |
| 195 | PIO_InitializeInterrupts(0); |
| 196 | |
| 197 | EEFC_ReadUniqueID(g_unique_id); |
| 198 | |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 199 | printf("\n\r\n\r" |
| 200 | "=============================================================================\n\r" |
| 201 | "DFU bootloader %s for board %s (C) 2010-2017 by Harald Welte\n\r" |
| 202 | "=============================================================================\n\r", |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 203 | manifest_revision, manifest_board); |
| 204 | |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 205 | TRACE_INFO("Chip ID: 0x%08x (Ext 0x%08x)\n\r", CHIPID->CHIPID_CIDR, CHIPID->CHIPID_EXID); |
| 206 | TRACE_INFO("Serial Nr. %08x-%08x-%08x-%08x\n\r", |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 207 | g_unique_id[0], g_unique_id[1], |
| 208 | g_unique_id[2], g_unique_id[3]); |
Harald Welte | 5b108d8 | 2017-03-06 21:51:55 +0100 | [diff] [blame] | 209 | TRACE_INFO("Reset Cause: 0x%x\n\r", reset_cause); |
| 210 | |
| 211 | /* clear g_dfu on power-up reset */ |
| 212 | if (reset_cause == 0) |
| 213 | memset(g_dfu, 0, sizeof(*g_dfu)); |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 214 | |
Harald Welte | 8adf0ac | 2017-03-03 01:52:34 +0100 | [diff] [blame] | 215 | board_main_top(); |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 216 | |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 217 | TRACE_INFO("USB init...\n\r"); |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 218 | USBDFU_Initialize(&dfu_descriptors); |
| 219 | |
| 220 | while (USBD_GetState() < USBD_STATE_CONFIGURED) { |
Harald Welte | 45ebe45 | 2017-03-03 19:01:53 +0100 | [diff] [blame] | 221 | WDT_Restart(WDT); |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 222 | check_exec_dbg_cmd(); |
Harald Welte | 32852bc | 2017-02-28 00:21:45 +0100 | [diff] [blame] | 223 | #if 1 |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 224 | if (i >= MAX_USB_ITER * 3) { |
| 225 | TRACE_ERROR("Resetting board (USB could " |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 226 | "not be configured)\n\r"); |
Harald Welte | f415d71 | 2017-03-03 01:51:43 +0100 | [diff] [blame] | 227 | USBD_Disconnect(); |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 228 | NVIC_SystemReset(); |
| 229 | } |
| 230 | #endif |
| 231 | i++; |
| 232 | } |
| 233 | |
Harald Welte | ec9b5ff | 2017-03-02 23:18:40 +0100 | [diff] [blame] | 234 | FLASHD_Initialize(BOARD_MCK, 1); |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 235 | TRACE_INFO("entering main loop...\n\r"); |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 236 | while (1) { |
Harald Welte | 45ebe45 | 2017-03-03 19:01:53 +0100 | [diff] [blame] | 237 | WDT_Restart(WDT); |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 238 | #if TRACE_LEVEL >= TRACE_LEVEL_DEBUG |
| 239 | const char rotor[] = { '-', '\\', '|', '/' }; |
| 240 | putchar('\b'); |
| 241 | putchar(rotor[i++ % ARRAY_SIZE(rotor)]); |
| 242 | #endif |
| 243 | check_exec_dbg_cmd(); |
Harald Welte | 3bbaba0 | 2017-03-06 22:47:06 +0100 | [diff] [blame^] | 244 | #if 0 |
| 245 | osmo_timers_prepare(); |
| 246 | osmo_timers_update(); |
| 247 | #endif |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 248 | |
| 249 | if (USBD_GetState() < USBD_STATE_CONFIGURED) { |
| 250 | |
| 251 | if (isUsbConnected) { |
| 252 | isUsbConnected = 0; |
| 253 | } |
| 254 | } else if (isUsbConnected == 0) { |
Harald Welte | ec0837c | 2017-03-03 01:33:24 +0100 | [diff] [blame] | 255 | TRACE_INFO("USB is now configured\n\r"); |
Harald Welte | d8a003d | 2017-02-27 20:31:09 +0100 | [diff] [blame] | 256 | |
| 257 | isUsbConnected = 1; |
| 258 | } |
| 259 | } |
| 260 | } |