blob: 5aafc7cbd8e1d700b15387f10b9a31e3455b784b [file] [log] [blame]
Kévin Redon9a12d682018-07-08 13:21:16 +02001/* SIMtrace 2 firmware USB DFU bootloader
2 *
3 * (C) 2015-2017 by Harald Welte <hwelte@hmw-consulting.de>
Kévin Redon9b367872019-02-07 17:52:08 +01004 * (C) 2018-2019 by sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>
Kévin Redon9a12d682018-07-08 13:21:16 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
Harald Welted8a003d2017-02-27 20:31:09 +010020#include "board.h"
21#include "utils.h"
22#include "usb/device/dfu/dfu.h"
23#include "usb/common/dfu/usb_dfu.h"
24#include "manifest.h"
Kévin Redonff3d8492018-08-06 17:57:20 +020025#include "USBD_HAL.h"
Harald Welted8a003d2017-02-27 20:31:09 +010026
Harald Welte3bbaba02017-03-06 22:47:06 +010027#include <osmocom/core/timer.h>
28
Harald Welte32852bc2017-02-28 00:21:45 +010029#define ALTIF_RAM 0
30#define ALTIF_FLASH 1
31
Harald Welted8a003d2017-02-27 20:31:09 +010032unsigned int g_unique_id[4];
Kévin Redon869dbfa2018-06-17 22:36:44 +020033/* remember if the watchdog has been configured in the main loop so we can kick it in the ISR */
34static bool watchdog_configured = false;
Harald Welted8a003d2017-02-27 20:31:09 +010035
Kévin Redon9def7632018-06-27 16:27:41 +020036/* There is not enough space in the 16 KiB DFU bootloader to include led.h functions */
37#ifdef PINS_LEDS
38/** LED pin configurations */
39static const Pin pinsLeds[] = { PINS_LEDS } ;
40#endif
41
Harald Welted8a003d2017-02-27 20:31:09 +010042/*----------------------------------------------------------------------------
43 * Callbacks
44 *----------------------------------------------------------------------------*/
45
Harald Welte32852bc2017-02-28 00:21:45 +010046#define RAM_ADDR(offset) (IRAM_ADDR + BOARD_DFU_RAM_SIZE + offset)
47#define FLASH_ADDR(offset) (IFLASH_ADDR + BOARD_DFU_BOOT_SIZE + offset)
48
49#define IFLASH_END ((uint8_t *)IFLASH_ADDR + IFLASH_SIZE)
50#define IRAM_END ((uint8_t *)IRAM_ADDR + IRAM_SIZE)
51
Kévin Redon318309f2018-06-01 11:02:49 +020052/* incoming call-back: Host has transferred 'len' bytes (stored at
Harald Welte32852bc2017-02-28 00:21:45 +010053 * 'data'), which we shall write to 'offset' into the partition
Kévin Redon318309f2018-06-01 11:02:49 +020054 * associated with 'altif'. Guaranted to be less than
Harald Welte32852bc2017-02-28 00:21:45 +010055 * BOARD_DFU_PAGE_SIZE */
56int USBDFU_handle_dnload(uint8_t altif, unsigned int offset,
57 uint8_t *data, unsigned int len)
Harald Welted8a003d2017-02-27 20:31:09 +010058{
Harald Welte32852bc2017-02-28 00:21:45 +010059 uint32_t addr;
Harald Welte1cfc2612018-06-29 21:07:41 +020060 unsigned int i;
Harald Welte32852bc2017-02-28 00:21:45 +010061 int rc;
Kévin Redon318309f2018-06-01 11:02:49 +020062 /* address of the last allocated variable on the stack */
63 uint32_t stack_addr = (uint32_t)&rc;
Kévin Redon869dbfa2018-06-17 22:36:44 +020064 /* kick the dog to have enough time to flash */
65 if (watchdog_configured) {
66 WDT_Restart(WDT);
67 }
Harald Welte32852bc2017-02-28 00:21:45 +010068
Kévin Redon98fbf232019-02-07 17:55:24 +010069#if TRACE_LEVEL >= TRACE_LEVEL_INFO
70 TRACE_INFO("dnload(altif=%u, offset=%u, len=%u)\n\r", altif, offset, len);
71#else
72 printf("DL off=%u\n\r", offset);
73#endif
Harald Welte32852bc2017-02-28 00:21:45 +010074
Kévin Redon9def7632018-06-27 16:27:41 +020075#ifdef PINS_LEDS
76 PIO_Clear(&pinsLeds[LED_NUM_RED]);
77#endif
78
Harald Welte32852bc2017-02-28 00:21:45 +010079 switch (altif) {
80 case ALTIF_RAM:
81 addr = RAM_ADDR(offset);
Kévin Redonb73f0a02018-06-17 22:33:29 +020082 if (addr < IRAM_ADDR || addr + len >= IRAM_ADDR + IRAM_SIZE || addr + len >= stack_addr) {
Harald Welteadba0ce2017-02-28 01:02:24 +010083 g_dfu->state = DFU_STATE_dfuERROR;
84 g_dfu->status = DFU_STATUS_errADDRESS;
Kévin Redon9def7632018-06-27 16:27:41 +020085 rc = DFU_RET_STALL;
86 break;
Harald Welte32852bc2017-02-28 00:21:45 +010087 }
88 memcpy((void *)addr, data, len);
Kévin Redon9def7632018-06-27 16:27:41 +020089 rc = DFU_RET_ZLP;
90 break;
Harald Welte32852bc2017-02-28 00:21:45 +010091 case ALTIF_FLASH:
92 addr = FLASH_ADDR(offset);
Kévin Redonb73f0a02018-06-17 22:33:29 +020093 if (addr < IFLASH_ADDR || addr + len >= IFLASH_ADDR + IFLASH_SIZE) {
Harald Welteadba0ce2017-02-28 01:02:24 +010094 g_dfu->state = DFU_STATE_dfuERROR;
95 g_dfu->status = DFU_STATUS_errADDRESS;
Kévin Redon9def7632018-06-27 16:27:41 +020096 rc = DFU_RET_STALL;
97 break;
Harald Welte32852bc2017-02-28 00:21:45 +010098 }
Kévin Redonb73f0a02018-06-17 22:33:29 +020099 rc = FLASHD_Unlock(addr, addr + len, 0, 0);
100 if (rc != 0) {
101 TRACE_ERROR("DFU download flash unlock failed\n\r");
Kévin Redon9def7632018-06-27 16:27:41 +0200102 rc = DFU_RET_STALL;
103 break;
Kévin Redonb73f0a02018-06-17 22:33:29 +0200104 }
Harald Welte32852bc2017-02-28 00:21:45 +0100105 rc = FLASHD_Write(addr, data, len);
106 if (rc != 0) {
Kévin Redonb73f0a02018-06-17 22:33:29 +0200107 TRACE_ERROR("DFU download flash erase failed\n\r");
Kévin Redon9def7632018-06-27 16:27:41 +0200108 rc = DFU_RET_STALL;
109 break;
Kévin Redonb73f0a02018-06-17 22:33:29 +0200110 }
Harald Welte1cfc2612018-06-29 21:07:41 +0200111 for (i = 0; i < len; i++) {
Kévin Redonb73f0a02018-06-17 22:33:29 +0200112 if (((uint8_t*)addr)[i]!=data[i]) {
113 TRACE_ERROR("DFU download flash data written not correct\n\r");
Kévin Redon9def7632018-06-27 16:27:41 +0200114 rc = DFU_RET_STALL;
115 break;
Kévin Redonb73f0a02018-06-17 22:33:29 +0200116 }
117 }
Kévin Redon9def7632018-06-27 16:27:41 +0200118 rc = DFU_RET_ZLP;
119 break;
Harald Welte32852bc2017-02-28 00:21:45 +0100120 default:
Harald Welte32852bc2017-02-28 00:21:45 +0100121 TRACE_ERROR("DFU download for unknown AltIf %d\n\r", altif);
Kévin Redon9def7632018-06-27 16:27:41 +0200122 rc = DFU_RET_STALL;
123 break;
Harald Welte32852bc2017-02-28 00:21:45 +0100124 }
Kévin Redon9def7632018-06-27 16:27:41 +0200125
126#ifdef PINS_LEDS
127 PIO_Set(&pinsLeds[LED_NUM_RED]);
128#endif
129
130 return rc;
Harald Welted8a003d2017-02-27 20:31:09 +0100131}
Harald Welte32852bc2017-02-28 00:21:45 +0100132
133/* incoming call-back: Host has requested to read back 'req_len' bytes
134 * starting from 'offset' of the firmware * associated with partition
135 * 'altif' */
136int USBDFU_handle_upload(uint8_t altif, unsigned int offset,
137 uint8_t *data, unsigned int req_len)
138{
139 uint32_t addr;
140
141 printf("upload(altif=%u, offset=%u, len=%u)", altif, offset, req_len);
142
143 switch (altif) {
144 case ALTIF_RAM:
145 addr = RAM_ADDR(offset);
146 if (addr > IRAM_ADDR + IRAM_SIZE) {
Harald Welteadba0ce2017-02-28 01:02:24 +0100147 g_dfu->state = DFU_STATE_dfuERROR;
148 g_dfu->status = DFU_STATUS_errADDRESS;
Harald Welte32852bc2017-02-28 00:21:45 +0100149 return -1;
150 }
151 if ((uint8_t *)addr + req_len > IRAM_END)
152 req_len = IRAM_END - (uint8_t *)addr;
153 memcpy(data, (void *)addr, req_len);
154 break;
155 case ALTIF_FLASH:
156 addr = FLASH_ADDR(offset);
157 if (addr > IFLASH_ADDR + IFLASH_SIZE) {
Harald Welteadba0ce2017-02-28 01:02:24 +0100158 g_dfu->state = DFU_STATE_dfuERROR;
159 g_dfu->status = DFU_STATUS_errADDRESS;
Harald Welte32852bc2017-02-28 00:21:45 +0100160 return -1;
161 }
162 if ((uint8_t *)addr + req_len > IFLASH_END)
163 req_len = IFLASH_END - (uint8_t *)addr;
164 memcpy(data, (void *)addr, req_len);
165 break;
166 default:
167 TRACE_ERROR("DFU upload for unknown AltIf %d\n\r", altif);
168 /* FIXME: set error codes */
169 return -1;
170 }
Harald Welteec0837c2017-03-03 01:33:24 +0100171 printf("=%u\n\r", req_len);
Harald Welte32852bc2017-02-28 00:21:45 +0100172 return req_len;
173}
174
Harald Welte27f5fc62017-11-28 22:15:56 +0100175/* can be overridden by board specific code, e.g. by pushbutton */
176WEAK int board_override_enter_dfu(void)
Harald Welte14051002017-03-04 19:17:27 +0100177{
Harald Welte27f5fc62017-11-28 22:15:56 +0100178 return 0;
Harald Welte14051002017-03-04 19:17:27 +0100179}
180
181/* using this function we can determine if we should enter DFU mode
182 * during boot, or if we should proceed towards the application/runtime */
183int USBDFU_OverrideEnterDFU(void)
184{
185 uint32_t *app_part = (uint32_t *)FLASH_ADDR(0);
Kévin Redon9918c282018-07-07 15:22:16 +0200186 /* at the first call we are before the text segment has been relocated,
187 * so g_dfu is not initialized yet */
188 g_dfu = &_g_dfu;
189 if (USB_DFU_MAGIC == g_dfu->magic) {
190 return 1;
191 }
Harald Welte14051002017-03-04 19:17:27 +0100192
193 /* If the loopback jumper is set, we enter DFU mode */
Kévin Redon9918c282018-07-07 15:22:16 +0200194 if (board_override_enter_dfu()) {
195 return 2;
196 }
Harald Welte14051002017-03-04 19:17:27 +0100197
198 /* if the first word of the application partition doesn't look
199 * like a stack pointer (i.e. point to RAM), enter DFU mode */
Kévin Redon9918c282018-07-07 15:22:16 +0200200 if ((app_part[0] < IRAM_ADDR) || ((uint8_t *)app_part[0] > IRAM_END)) {
201 return 3;
202 }
Harald Welte14051002017-03-04 19:17:27 +0100203
204 /* if the second word of the application partition doesn't look
205 * like a function from flash (reset vector), enter DFU mode */
206 if (((uint32_t *)app_part[1] < app_part) ||
Kévin Redon9918c282018-07-07 15:22:16 +0200207 ((uint8_t *)app_part[1] > IFLASH_END)) {
208 return 4;
209 }
Harald Welte14051002017-03-04 19:17:27 +0100210
211 return 0;
212}
Harald Welted8a003d2017-02-27 20:31:09 +0100213
214/* returns '1' in case we should break any endless loop */
215static void check_exec_dbg_cmd(void)
216{
217 int ch;
218
219 if (!UART_IsRxReady())
220 return;
221
222 ch = UART_GetChar();
223
224 //board_exec_dbg_cmd(ch);
225}
226
227/*------------------------------------------------------------------------------
228 * Main
229 *------------------------------------------------------------------------------*/
230#define MAX_USB_ITER BOARD_MCK/72 // This should be around a second
231extern int main(void)
232{
233 uint8_t isUsbConnected = 0;
234 unsigned int i = 0;
Harald Welte5b108d82017-03-06 21:51:55 +0100235 uint32_t reset_cause = (RSTC->RSTC_SR & RSTC_SR_RSTTYP_Msk) >> RSTC_SR_RSTTYP_Pos;
Harald Welted8a003d2017-02-27 20:31:09 +0100236
Kévin Redon869dbfa2018-06-17 22:36:44 +0200237 /* Enable watchdog for 2000ms, with no window */
Harald Welte45ebe452017-03-03 19:01:53 +0100238 WDT_Enable(WDT, WDT_MR_WDRSTEN | WDT_MR_WDDBGHLT | WDT_MR_WDIDLEHLT |
Kévin Redon869dbfa2018-06-17 22:36:44 +0200239 (WDT_GetPeriod(2000) << 16) | WDT_GetPeriod(2000));
240 watchdog_configured = true;
Harald Welted8a003d2017-02-27 20:31:09 +0100241
Kévin Redon9def7632018-06-27 16:27:41 +0200242#ifdef PINS_LEDS
243 /* Configure LED */
244 PIO_Configure(pinsLeds, sizeof(pinsLeds));
245 PIO_Set(&pinsLeds[LED_NUM_RED]);
246 PIO_Clear(&pinsLeds[LED_NUM_GREEN]);
247#endif
248
Harald Welted8a003d2017-02-27 20:31:09 +0100249 PIO_InitializeInterrupts(0);
250
251 EEFC_ReadUniqueID(g_unique_id);
252
Kévin Redon9918c282018-07-07 15:22:16 +0200253 printf("\n\r\n\r"
Harald Welteec0837c2017-03-03 01:33:24 +0100254 "=============================================================================\n\r"
Kévin Redon9b367872019-02-07 17:52:08 +0100255 "DFU bootloader %s for board %s\n\r"
256 "(C) 2010-2017 by Harald Welte, 2018-2019 by Kevin Redon\n\r"
Harald Welteec0837c2017-03-03 01:33:24 +0100257 "=============================================================================\n\r",
Harald Welted8a003d2017-02-27 20:31:09 +0100258 manifest_revision, manifest_board);
259
Kévin Redon75a5f222019-12-11 16:44:02 +0100260#if (TRACE_LEVEL >= TRACE_LEVEL_INFO)
261 TRACE_INFO("Chip ID: 0x%08lx (Ext 0x%08lx)\n\r", CHIPID->CHIPID_CIDR, CHIPID->CHIPID_EXID);
Harald Welteec0837c2017-03-03 01:33:24 +0100262 TRACE_INFO("Serial Nr. %08x-%08x-%08x-%08x\n\r",
Harald Welted8a003d2017-02-27 20:31:09 +0100263 g_unique_id[0], g_unique_id[1],
264 g_unique_id[2], g_unique_id[3]);
Kévin Redon75a5f222019-12-11 16:44:02 +0100265 static const char* reset_causes[] = {
266 "general reset (first power-up reset)",
267 "backup reset (return from backup mode)",
268 "watchdog reset (watchdog fault occurred)",
269 "software reset (processor reset required by the software)",
270 "user reset (NRST pin detected low)",
271 };
272 if (reset_cause < ARRAY_SIZE(reset_causes)) {
273 TRACE_INFO("Reset Cause: %s\n\r", reset_causes[reset_cause]);
274 } else {
275 TRACE_INFO("Reset Cause: 0x%lx\n\r", (RSTC->RSTC_SR & RSTC_SR_RSTTYP_Msk) >> RSTC_SR_RSTTYP_Pos);
276 }
277#endif
Harald Welte5b108d82017-03-06 21:51:55 +0100278
Kévin Redon9918c282018-07-07 15:22:16 +0200279#if (TRACE_LEVEL >= TRACE_LEVEL_INFO)
280 /* Find out why we are in the DFU bootloader, and not the main application */
281 TRACE_INFO("DFU bootloader start reason: ");
282 switch (USBDFU_OverrideEnterDFU()) {
283 case 0:
284 /* 0 normally means that there is no override, but we are in the bootloader,
285 * thus the first check in board_cstartup_gnu did return something else than 0.
286 * this can only be g_dfu->magic which is erased when the segment are
287 * relocated, which happens in board_cstartup_gnu just after USBDFU_OverrideEnterDFU.
288 * no static variable can be used to store this case since this will also be overwritten
289 */
290 case 1:
291 TRACE_INFO_WP("DFU switch requested by main application\n\r");
292 break;
293 case 2:
294 TRACE_INFO_WP("bootloader forced (button pressed or jumper set)\n\r");
295 break;
296 case 3:
297 TRACE_INFO_WP("stack pointer (first application word) does no point in RAM\n\r");
298 break;
299 case 4: // the is no reason
300 TRACE_INFO_WP("reset vector (second application word) does no point in flash\n\r");
301 break;
302 default:
303 TRACE_INFO_WP("unknown\n\r");
304 break;
305 }
306#endif
307
Harald Welte5b108d82017-03-06 21:51:55 +0100308 /* clear g_dfu on power-up reset */
309 if (reset_cause == 0)
310 memset(g_dfu, 0, sizeof(*g_dfu));
Harald Welted8a003d2017-02-27 20:31:09 +0100311
Harald Welte8adf0ac2017-03-03 01:52:34 +0100312 board_main_top();
Harald Welted8a003d2017-02-27 20:31:09 +0100313
Harald Welteec0837c2017-03-03 01:33:24 +0100314 TRACE_INFO("USB init...\n\r");
Kévin Redonf5869d42018-06-17 22:31:21 +0200315 /* Signal USB reset by disabling the pull-up on USB D+ for at least 10 ms */
Kévin Redoned3ceec2019-12-03 15:29:33 +0100316 USBD_Disconnect();
Kévin Redonff3d8492018-08-06 17:57:20 +0200317#ifdef PIN_USB_PULLUP
Kévin Redonf5869d42018-06-17 22:31:21 +0200318 const Pin usb_dp_pullup = PIN_USB_PULLUP;
319 PIO_Configure(&usb_dp_pullup, 1);
320 PIO_Set(&usb_dp_pullup);
Kévin Redonff3d8492018-08-06 17:57:20 +0200321#endif
Kévin Redoned3ceec2019-12-03 15:29:33 +0100322 mdelay(50);
Kévin Redonff3d8492018-08-06 17:57:20 +0200323#ifdef PIN_USB_PULLUP
Kévin Redonf5869d42018-06-17 22:31:21 +0200324 PIO_Clear(&usb_dp_pullup);
Kévin Redonff3d8492018-08-06 17:57:20 +0200325#endif
Kévin Redonff3d8492018-08-06 17:57:20 +0200326
Harald Welted8a003d2017-02-27 20:31:09 +0100327 USBDFU_Initialize(&dfu_descriptors);
328
329 while (USBD_GetState() < USBD_STATE_CONFIGURED) {
Harald Welte45ebe452017-03-03 19:01:53 +0100330 WDT_Restart(WDT);
Harald Welted8a003d2017-02-27 20:31:09 +0100331 check_exec_dbg_cmd();
Harald Welte32852bc2017-02-28 00:21:45 +0100332#if 1
Harald Welted8a003d2017-02-27 20:31:09 +0100333 if (i >= MAX_USB_ITER * 3) {
Kévin Redond70836f2019-12-03 15:36:58 +0100334 TRACE_ERROR("Resetting board (USB could not be configured)\n\r");
335 g_dfu->magic = USB_DFU_MAGIC; // start the bootloader after reboot
Harald Weltef415d712017-03-03 01:51:43 +0100336 USBD_Disconnect();
Harald Welted8a003d2017-02-27 20:31:09 +0100337 NVIC_SystemReset();
338 }
339#endif
340 i++;
341 }
342
Kévin Redonb73f0a02018-06-17 22:33:29 +0200343 /* Initialize the flash to be able to write it, using the IAP ROM code */
Harald Welteec9b5ff2017-03-02 23:18:40 +0100344 FLASHD_Initialize(BOARD_MCK, 1);
Kévin Redonb73f0a02018-06-17 22:33:29 +0200345
Harald Welteec0837c2017-03-03 01:33:24 +0100346 TRACE_INFO("entering main loop...\n\r");
Harald Welted8a003d2017-02-27 20:31:09 +0100347 while (1) {
Harald Welte45ebe452017-03-03 19:01:53 +0100348 WDT_Restart(WDT);
Harald Welted8a003d2017-02-27 20:31:09 +0100349#if TRACE_LEVEL >= TRACE_LEVEL_DEBUG
350 const char rotor[] = { '-', '\\', '|', '/' };
351 putchar('\b');
352 putchar(rotor[i++ % ARRAY_SIZE(rotor)]);
353#endif
354 check_exec_dbg_cmd();
Harald Welte3bbaba02017-03-06 22:47:06 +0100355#if 0
356 osmo_timers_prepare();
357 osmo_timers_update();
358#endif
Harald Welted8a003d2017-02-27 20:31:09 +0100359
360 if (USBD_GetState() < USBD_STATE_CONFIGURED) {
361
362 if (isUsbConnected) {
363 isUsbConnected = 0;
364 }
365 } else if (isUsbConnected == 0) {
Harald Welteec0837c2017-03-03 01:33:24 +0100366 TRACE_INFO("USB is now configured\n\r");
Harald Welted8a003d2017-02-27 20:31:09 +0100367
368 isUsbConnected = 1;
369 }
370 }
371}