blob: 3d55fdc67c12a8afb9050a6c322b0aa547606029 [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
16/*----------------------------------------------------------------------------
17 * Callbacks
18 *----------------------------------------------------------------------------*/
19
Harald Welte32852bc2017-02-28 00:21:45 +010020#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 Redon318309f2018-06-01 11:02:49 +020026/* incoming call-back: Host has transferred 'len' bytes (stored at
Harald Welte32852bc2017-02-28 00:21:45 +010027 * 'data'), which we shall write to 'offset' into the partition
Kévin Redon318309f2018-06-01 11:02:49 +020028 * associated with 'altif'. Guaranted to be less than
Harald Welte32852bc2017-02-28 00:21:45 +010029 * BOARD_DFU_PAGE_SIZE */
30int USBDFU_handle_dnload(uint8_t altif, unsigned int offset,
31 uint8_t *data, unsigned int len)
Harald Welted8a003d2017-02-27 20:31:09 +010032{
Harald Welte32852bc2017-02-28 00:21:45 +010033 uint32_t addr;
Harald Welte1cfc2612018-06-29 21:07:41 +020034 unsigned int i;
Harald Welte32852bc2017-02-28 00:21:45 +010035 int rc;
Kévin Redon318309f2018-06-01 11:02:49 +020036 /* address of the last allocated variable on the stack */
37 uint32_t stack_addr = (uint32_t)&rc;
Kévin Redon869dbfa2018-06-17 22:36:44 +020038 /* kick the dog to have enough time to flash */
39 if (watchdog_configured) {
40 WDT_Restart(WDT);
41 }
Harald Welte32852bc2017-02-28 00:21:45 +010042
Harald Weltee8eea292017-03-03 00:35:51 +010043 printf("dnload(altif=%u, offset=%u, len=%u)\n\r", altif, offset, len);
Harald Welte32852bc2017-02-28 00:21:45 +010044
45 switch (altif) {
46 case ALTIF_RAM:
47 addr = RAM_ADDR(offset);
Kévin Redonb73f0a02018-06-17 22:33:29 +020048 if (addr < IRAM_ADDR || addr + len >= IRAM_ADDR + IRAM_SIZE || addr + len >= stack_addr) {
Harald Welteadba0ce2017-02-28 01:02:24 +010049 g_dfu->state = DFU_STATE_dfuERROR;
50 g_dfu->status = DFU_STATUS_errADDRESS;
Harald Welte32852bc2017-02-28 00:21:45 +010051 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 Redonb73f0a02018-06-17 22:33:29 +020057 if (addr < IFLASH_ADDR || addr + len >= IFLASH_ADDR + IFLASH_SIZE) {
Harald Welteadba0ce2017-02-28 01:02:24 +010058 g_dfu->state = DFU_STATE_dfuERROR;
59 g_dfu->status = DFU_STATUS_errADDRESS;
Harald Welte32852bc2017-02-28 00:21:45 +010060 return DFU_RET_STALL;
61 }
Kévin Redonb73f0a02018-06-17 22:33:29 +020062 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 Welte32852bc2017-02-28 00:21:45 +010068 rc = FLASHD_Write(addr, data, len);
69 if (rc != 0) {
Kévin Redonb73f0a02018-06-17 22:33:29 +020070 TRACE_ERROR("DFU download flash erase failed\n\r");
71 /* FIXME: set error codes */
72 return DFU_RET_STALL;
73 }
Harald Welte1cfc2612018-06-29 21:07:41 +020074 for (i = 0; i < len; i++) {
Kévin Redonb73f0a02018-06-17 22:33:29 +020075 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 Welte32852bc2017-02-28 00:21:45 +010083 /* 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 Welted8a003d2017-02-27 20:31:09 +010092}
Harald Welte32852bc2017-02-28 00:21:45 +010093
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' */
97int 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 Welteadba0ce2017-02-28 01:02:24 +0100108 g_dfu->state = DFU_STATE_dfuERROR;
109 g_dfu->status = DFU_STATUS_errADDRESS;
Harald Welte32852bc2017-02-28 00:21:45 +0100110 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 Welteadba0ce2017-02-28 01:02:24 +0100119 g_dfu->state = DFU_STATE_dfuERROR;
120 g_dfu->status = DFU_STATUS_errADDRESS;
Harald Welte32852bc2017-02-28 00:21:45 +0100121 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 Welteec0837c2017-03-03 01:33:24 +0100132 printf("=%u\n\r", req_len);
Harald Welte32852bc2017-02-28 00:21:45 +0100133 return req_len;
134}
135
Harald Welte27f5fc62017-11-28 22:15:56 +0100136/* can be overridden by board specific code, e.g. by pushbutton */
137WEAK int board_override_enter_dfu(void)
Harald Welte14051002017-03-04 19:17:27 +0100138{
Harald Welte27f5fc62017-11-28 22:15:56 +0100139 return 0;
Harald Welte14051002017-03-04 19:17:27 +0100140}
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 */
144int 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 Welte27f5fc62017-11-28 22:15:56 +0100149 if (board_override_enter_dfu())
Harald Welte14051002017-03-04 19:17:27 +0100150 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 Welted8a003d2017-02-27 20:31:09 +0100166
167/* returns '1' in case we should break any endless loop */
168static 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
184extern int main(void)
185{
186 uint8_t isUsbConnected = 0;
187 unsigned int i = 0;
Harald Welte5b108d82017-03-06 21:51:55 +0100188 uint32_t reset_cause = (RSTC->RSTC_SR & RSTC_SR_RSTTYP_Msk) >> RSTC_SR_RSTTYP_Pos;
Harald Welted8a003d2017-02-27 20:31:09 +0100189
Harald Welte3bbaba02017-03-06 22:47:06 +0100190#if 0
Harald Welteabba8a82017-03-06 16:58:00 +0100191 led_init();
192 led_blink(LED_GREEN, BLINK_3O_30F);
193 led_blink(LED_RED, BLINK_3O_30F);
Harald Welte3bbaba02017-03-06 22:47:06 +0100194#endif
Harald Welte45ebe452017-03-03 19:01:53 +0100195
Kévin Redon869dbfa2018-06-17 22:36:44 +0200196 /* Enable watchdog for 2000ms, with no window */
Harald Welte45ebe452017-03-03 19:01:53 +0100197 WDT_Enable(WDT, WDT_MR_WDRSTEN | WDT_MR_WDDBGHLT | WDT_MR_WDIDLEHLT |
Kévin Redon869dbfa2018-06-17 22:36:44 +0200198 (WDT_GetPeriod(2000) << 16) | WDT_GetPeriod(2000));
199 watchdog_configured = true;
Harald Welted8a003d2017-02-27 20:31:09 +0100200
Harald Welted8a003d2017-02-27 20:31:09 +0100201 PIO_InitializeInterrupts(0);
202
203 EEFC_ReadUniqueID(g_unique_id);
204
Harald Welteec0837c2017-03-03 01:33:24 +0100205 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 Welted8a003d2017-02-27 20:31:09 +0100209 manifest_revision, manifest_board);
210
Harald Welteec0837c2017-03-03 01:33:24 +0100211 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 Welted8a003d2017-02-27 20:31:09 +0100213 g_unique_id[0], g_unique_id[1],
214 g_unique_id[2], g_unique_id[3]);
Harald Welte5b108d82017-03-06 21:51:55 +0100215 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 Welted8a003d2017-02-27 20:31:09 +0100220
Harald Welte8adf0ac2017-03-03 01:52:34 +0100221 board_main_top();
Harald Welted8a003d2017-02-27 20:31:09 +0100222
Harald Welteec0837c2017-03-03 01:33:24 +0100223 TRACE_INFO("USB init...\n\r");
Kévin Redonf5869d42018-06-17 22:31:21 +0200224 /* 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 Welted8a003d2017-02-27 20:31:09 +0100230 USBDFU_Initialize(&dfu_descriptors);
231
232 while (USBD_GetState() < USBD_STATE_CONFIGURED) {
Harald Welte45ebe452017-03-03 19:01:53 +0100233 WDT_Restart(WDT);
Harald Welted8a003d2017-02-27 20:31:09 +0100234 check_exec_dbg_cmd();
Harald Welte32852bc2017-02-28 00:21:45 +0100235#if 1
Harald Welted8a003d2017-02-27 20:31:09 +0100236 if (i >= MAX_USB_ITER * 3) {
237 TRACE_ERROR("Resetting board (USB could "
Harald Welteec0837c2017-03-03 01:33:24 +0100238 "not be configured)\n\r");
Harald Weltef415d712017-03-03 01:51:43 +0100239 USBD_Disconnect();
Harald Welted8a003d2017-02-27 20:31:09 +0100240 NVIC_SystemReset();
241 }
242#endif
243 i++;
244 }
245
Kévin Redonb73f0a02018-06-17 22:33:29 +0200246 /* Initialize the flash to be able to write it, using the IAP ROM code */
Harald Welteec9b5ff2017-03-02 23:18:40 +0100247 FLASHD_Initialize(BOARD_MCK, 1);
Kévin Redonb73f0a02018-06-17 22:33:29 +0200248
Harald Welteec0837c2017-03-03 01:33:24 +0100249 TRACE_INFO("entering main loop...\n\r");
Harald Welted8a003d2017-02-27 20:31:09 +0100250 while (1) {
Harald Welte45ebe452017-03-03 19:01:53 +0100251 WDT_Restart(WDT);
Harald Welted8a003d2017-02-27 20:31:09 +0100252#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 Welte3bbaba02017-03-06 22:47:06 +0100258#if 0
259 osmo_timers_prepare();
260 osmo_timers_update();
261#endif
Harald Welted8a003d2017-02-27 20:31:09 +0100262
263 if (USBD_GetState() < USBD_STATE_CONFIGURED) {
264
265 if (isUsbConnected) {
266 isUsbConnected = 0;
267 }
268 } else if (isUsbConnected == 0) {
Harald Welteec0837c2017-03-03 01:33:24 +0100269 TRACE_INFO("USB is now configured\n\r");
Harald Welted8a003d2017-02-27 20:31:09 +0100270
271 isUsbConnected = 1;
272 }
273 }
274}