blob: 26cbb8c50c9cc19683f2828645253d8a9c2bb71f [file] [log] [blame]
Harald Welted8a003d2017-02-27 20:31:09 +01001#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 Welte3bbaba02017-03-06 22:47:06 +01007#include <osmocom/core/timer.h>
8
Harald Welte32852bc2017-02-28 00:21:45 +01009#define ALTIF_RAM 0
10#define ALTIF_FLASH 1
11
Harald Welted8a003d2017-02-27 20:31:09 +010012unsigned int g_unique_id[4];
Kévin Redon869dbfa2018-06-17 22:36:44 +020013/* remember if the watchdog has been configured in the main loop so we can kick it in the ISR */
14static bool watchdog_configured = false;
Harald Welted8a003d2017-02-27 20:31:09 +010015
Kévin Redon9def7632018-06-27 16:27:41 +020016/* There is not enough space in the 16 KiB DFU bootloader to include led.h functions */
17#ifdef PINS_LEDS
18/** LED pin configurations */
19static const Pin pinsLeds[] = { PINS_LEDS } ;
20#endif
21
Harald Welted8a003d2017-02-27 20:31:09 +010022/*----------------------------------------------------------------------------
23 * Callbacks
24 *----------------------------------------------------------------------------*/
25
Harald Welte32852bc2017-02-28 00:21:45 +010026#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 Redon318309f2018-06-01 11:02:49 +020032/* incoming call-back: Host has transferred 'len' bytes (stored at
Harald Welte32852bc2017-02-28 00:21:45 +010033 * 'data'), which we shall write to 'offset' into the partition
Kévin Redon318309f2018-06-01 11:02:49 +020034 * associated with 'altif'. Guaranted to be less than
Harald Welte32852bc2017-02-28 00:21:45 +010035 * BOARD_DFU_PAGE_SIZE */
36int USBDFU_handle_dnload(uint8_t altif, unsigned int offset,
37 uint8_t *data, unsigned int len)
Harald Welted8a003d2017-02-27 20:31:09 +010038{
Harald Welte32852bc2017-02-28 00:21:45 +010039 uint32_t addr;
Harald Welte1cfc2612018-06-29 21:07:41 +020040 unsigned int i;
Harald Welte32852bc2017-02-28 00:21:45 +010041 int rc;
Kévin Redon318309f2018-06-01 11:02:49 +020042 /* address of the last allocated variable on the stack */
43 uint32_t stack_addr = (uint32_t)&rc;
Kévin Redon869dbfa2018-06-17 22:36:44 +020044 /* kick the dog to have enough time to flash */
45 if (watchdog_configured) {
46 WDT_Restart(WDT);
47 }
Harald Welte32852bc2017-02-28 00:21:45 +010048
Harald Weltee8eea292017-03-03 00:35:51 +010049 printf("dnload(altif=%u, offset=%u, len=%u)\n\r", altif, offset, len);
Harald Welte32852bc2017-02-28 00:21:45 +010050
Kévin Redon9def7632018-06-27 16:27:41 +020051#ifdef PINS_LEDS
52 PIO_Clear(&pinsLeds[LED_NUM_RED]);
53#endif
54
Harald Welte32852bc2017-02-28 00:21:45 +010055 switch (altif) {
56 case ALTIF_RAM:
57 addr = RAM_ADDR(offset);
Kévin Redonb73f0a02018-06-17 22:33:29 +020058 if (addr < IRAM_ADDR || addr + len >= IRAM_ADDR + IRAM_SIZE || addr + len >= stack_addr) {
Harald Welteadba0ce2017-02-28 01:02:24 +010059 g_dfu->state = DFU_STATE_dfuERROR;
60 g_dfu->status = DFU_STATUS_errADDRESS;
Kévin Redon9def7632018-06-27 16:27:41 +020061 rc = DFU_RET_STALL;
62 break;
Harald Welte32852bc2017-02-28 00:21:45 +010063 }
64 memcpy((void *)addr, data, len);
Kévin Redon9def7632018-06-27 16:27:41 +020065 rc = DFU_RET_ZLP;
66 break;
Harald Welte32852bc2017-02-28 00:21:45 +010067 case ALTIF_FLASH:
68 addr = FLASH_ADDR(offset);
Kévin Redonb73f0a02018-06-17 22:33:29 +020069 if (addr < IFLASH_ADDR || addr + len >= IFLASH_ADDR + IFLASH_SIZE) {
Harald Welteadba0ce2017-02-28 01:02:24 +010070 g_dfu->state = DFU_STATE_dfuERROR;
71 g_dfu->status = DFU_STATUS_errADDRESS;
Kévin Redon9def7632018-06-27 16:27:41 +020072 rc = DFU_RET_STALL;
73 break;
Harald Welte32852bc2017-02-28 00:21:45 +010074 }
Kévin Redonb73f0a02018-06-17 22:33:29 +020075 rc = FLASHD_Unlock(addr, addr + len, 0, 0);
76 if (rc != 0) {
77 TRACE_ERROR("DFU download flash unlock failed\n\r");
Kévin Redon9def7632018-06-27 16:27:41 +020078 rc = DFU_RET_STALL;
79 break;
Kévin Redonb73f0a02018-06-17 22:33:29 +020080 }
Harald Welte32852bc2017-02-28 00:21:45 +010081 rc = FLASHD_Write(addr, data, len);
82 if (rc != 0) {
Kévin Redonb73f0a02018-06-17 22:33:29 +020083 TRACE_ERROR("DFU download flash erase failed\n\r");
Kévin Redon9def7632018-06-27 16:27:41 +020084 rc = DFU_RET_STALL;
85 break;
Kévin Redonb73f0a02018-06-17 22:33:29 +020086 }
Harald Welte1cfc2612018-06-29 21:07:41 +020087 for (i = 0; i < len; i++) {
Kévin Redonb73f0a02018-06-17 22:33:29 +020088 if (((uint8_t*)addr)[i]!=data[i]) {
89 TRACE_ERROR("DFU download flash data written not correct\n\r");
Kévin Redon9def7632018-06-27 16:27:41 +020090 rc = DFU_RET_STALL;
91 break;
Kévin Redonb73f0a02018-06-17 22:33:29 +020092 }
93 }
Kévin Redon9def7632018-06-27 16:27:41 +020094 rc = DFU_RET_ZLP;
95 break;
Harald Welte32852bc2017-02-28 00:21:45 +010096 default:
Harald Welte32852bc2017-02-28 00:21:45 +010097 TRACE_ERROR("DFU download for unknown AltIf %d\n\r", altif);
Kévin Redon9def7632018-06-27 16:27:41 +020098 rc = DFU_RET_STALL;
99 break;
Harald Welte32852bc2017-02-28 00:21:45 +0100100 }
Kévin Redon9def7632018-06-27 16:27:41 +0200101
102#ifdef PINS_LEDS
103 PIO_Set(&pinsLeds[LED_NUM_RED]);
104#endif
105
106 return rc;
Harald Welted8a003d2017-02-27 20:31:09 +0100107}
Harald Welte32852bc2017-02-28 00:21:45 +0100108
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' */
112int 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 Welteadba0ce2017-02-28 01:02:24 +0100123 g_dfu->state = DFU_STATE_dfuERROR;
124 g_dfu->status = DFU_STATUS_errADDRESS;
Harald Welte32852bc2017-02-28 00:21:45 +0100125 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 Welteadba0ce2017-02-28 01:02:24 +0100134 g_dfu->state = DFU_STATE_dfuERROR;
135 g_dfu->status = DFU_STATUS_errADDRESS;
Harald Welte32852bc2017-02-28 00:21:45 +0100136 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 Welteec0837c2017-03-03 01:33:24 +0100147 printf("=%u\n\r", req_len);
Harald Welte32852bc2017-02-28 00:21:45 +0100148 return req_len;
149}
150
Harald Welte27f5fc62017-11-28 22:15:56 +0100151/* can be overridden by board specific code, e.g. by pushbutton */
152WEAK int board_override_enter_dfu(void)
Harald Welte14051002017-03-04 19:17:27 +0100153{
Harald Welte27f5fc62017-11-28 22:15:56 +0100154 return 0;
Harald Welte14051002017-03-04 19:17:27 +0100155}
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 */
159int 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 Welte27f5fc62017-11-28 22:15:56 +0100164 if (board_override_enter_dfu())
Harald Welte14051002017-03-04 19:17:27 +0100165 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 Welted8a003d2017-02-27 20:31:09 +0100181
182/* returns '1' in case we should break any endless loop */
183static 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
199extern int main(void)
200{
201 uint8_t isUsbConnected = 0;
202 unsigned int i = 0;
Harald Welte5b108d82017-03-06 21:51:55 +0100203 uint32_t reset_cause = (RSTC->RSTC_SR & RSTC_SR_RSTTYP_Msk) >> RSTC_SR_RSTTYP_Pos;
Harald Welted8a003d2017-02-27 20:31:09 +0100204
Kévin Redon869dbfa2018-06-17 22:36:44 +0200205 /* Enable watchdog for 2000ms, with no window */
Harald Welte45ebe452017-03-03 19:01:53 +0100206 WDT_Enable(WDT, WDT_MR_WDRSTEN | WDT_MR_WDDBGHLT | WDT_MR_WDIDLEHLT |
Kévin Redon869dbfa2018-06-17 22:36:44 +0200207 (WDT_GetPeriod(2000) << 16) | WDT_GetPeriod(2000));
208 watchdog_configured = true;
Harald Welted8a003d2017-02-27 20:31:09 +0100209
Kévin Redon9def7632018-06-27 16:27:41 +0200210#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 Welted8a003d2017-02-27 20:31:09 +0100217 PIO_InitializeInterrupts(0);
218
219 EEFC_ReadUniqueID(g_unique_id);
220
Harald Welteec0837c2017-03-03 01:33:24 +0100221 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 Welted8a003d2017-02-27 20:31:09 +0100225 manifest_revision, manifest_board);
226
Harald Welteec0837c2017-03-03 01:33:24 +0100227 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 Welted8a003d2017-02-27 20:31:09 +0100229 g_unique_id[0], g_unique_id[1],
230 g_unique_id[2], g_unique_id[3]);
Harald Welte5b108d82017-03-06 21:51:55 +0100231 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 Welted8a003d2017-02-27 20:31:09 +0100236
Harald Welte8adf0ac2017-03-03 01:52:34 +0100237 board_main_top();
Harald Welted8a003d2017-02-27 20:31:09 +0100238
Harald Welteec0837c2017-03-03 01:33:24 +0100239 TRACE_INFO("USB init...\n\r");
Kévin Redonf5869d42018-06-17 22:31:21 +0200240 /* 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 Welted8a003d2017-02-27 20:31:09 +0100246 USBDFU_Initialize(&dfu_descriptors);
247
248 while (USBD_GetState() < USBD_STATE_CONFIGURED) {
Harald Welte45ebe452017-03-03 19:01:53 +0100249 WDT_Restart(WDT);
Harald Welted8a003d2017-02-27 20:31:09 +0100250 check_exec_dbg_cmd();
Harald Welte32852bc2017-02-28 00:21:45 +0100251#if 1
Harald Welted8a003d2017-02-27 20:31:09 +0100252 if (i >= MAX_USB_ITER * 3) {
253 TRACE_ERROR("Resetting board (USB could "
Harald Welteec0837c2017-03-03 01:33:24 +0100254 "not be configured)\n\r");
Harald Weltef415d712017-03-03 01:51:43 +0100255 USBD_Disconnect();
Harald Welted8a003d2017-02-27 20:31:09 +0100256 NVIC_SystemReset();
257 }
258#endif
259 i++;
260 }
261
Kévin Redonb73f0a02018-06-17 22:33:29 +0200262 /* Initialize the flash to be able to write it, using the IAP ROM code */
Harald Welteec9b5ff2017-03-02 23:18:40 +0100263 FLASHD_Initialize(BOARD_MCK, 1);
Kévin Redonb73f0a02018-06-17 22:33:29 +0200264
Harald Welteec0837c2017-03-03 01:33:24 +0100265 TRACE_INFO("entering main loop...\n\r");
Harald Welted8a003d2017-02-27 20:31:09 +0100266 while (1) {
Harald Welte45ebe452017-03-03 19:01:53 +0100267 WDT_Restart(WDT);
Harald Welted8a003d2017-02-27 20:31:09 +0100268#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 Welte3bbaba02017-03-06 22:47:06 +0100274#if 0
275 osmo_timers_prepare();
276 osmo_timers_update();
277#endif
Harald Welted8a003d2017-02-27 20:31:09 +0100278
279 if (USBD_GetState() < USBD_STATE_CONFIGURED) {
280
281 if (isUsbConnected) {
282 isUsbConnected = 0;
283 }
284 } else if (isUsbConnected == 0) {
Harald Welteec0837c2017-03-03 01:33:24 +0100285 TRACE_INFO("USB is now configured\n\r");
Harald Welted8a003d2017-02-27 20:31:09 +0100286
287 isUsbConnected = 1;
288 }
289 }
290}