blob: f9b6aeda5f24fed7f7851a332478962effbcda41 [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>
4 * (C) 2018 by sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>
5 *
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
Harald Weltee8eea292017-03-03 00:35:51 +010069 printf("dnload(altif=%u, offset=%u, len=%u)\n\r", altif, offset, len);
Harald Welte32852bc2017-02-28 00:21:45 +010070
Kévin Redon9def7632018-06-27 16:27:41 +020071#ifdef PINS_LEDS
72 PIO_Clear(&pinsLeds[LED_NUM_RED]);
73#endif
74
Harald Welte32852bc2017-02-28 00:21:45 +010075 switch (altif) {
76 case ALTIF_RAM:
77 addr = RAM_ADDR(offset);
Kévin Redonb73f0a02018-06-17 22:33:29 +020078 if (addr < IRAM_ADDR || addr + len >= IRAM_ADDR + IRAM_SIZE || addr + len >= stack_addr) {
Harald Welteadba0ce2017-02-28 01:02:24 +010079 g_dfu->state = DFU_STATE_dfuERROR;
80 g_dfu->status = DFU_STATUS_errADDRESS;
Kévin Redon9def7632018-06-27 16:27:41 +020081 rc = DFU_RET_STALL;
82 break;
Harald Welte32852bc2017-02-28 00:21:45 +010083 }
84 memcpy((void *)addr, data, len);
Kévin Redon9def7632018-06-27 16:27:41 +020085 rc = DFU_RET_ZLP;
86 break;
Harald Welte32852bc2017-02-28 00:21:45 +010087 case ALTIF_FLASH:
88 addr = FLASH_ADDR(offset);
Kévin Redonb73f0a02018-06-17 22:33:29 +020089 if (addr < IFLASH_ADDR || addr + len >= IFLASH_ADDR + IFLASH_SIZE) {
Harald Welteadba0ce2017-02-28 01:02:24 +010090 g_dfu->state = DFU_STATE_dfuERROR;
91 g_dfu->status = DFU_STATUS_errADDRESS;
Kévin Redon9def7632018-06-27 16:27:41 +020092 rc = DFU_RET_STALL;
93 break;
Harald Welte32852bc2017-02-28 00:21:45 +010094 }
Kévin Redonb73f0a02018-06-17 22:33:29 +020095 rc = FLASHD_Unlock(addr, addr + len, 0, 0);
96 if (rc != 0) {
97 TRACE_ERROR("DFU download flash unlock failed\n\r");
Kévin Redon9def7632018-06-27 16:27:41 +020098 rc = DFU_RET_STALL;
99 break;
Kévin Redonb73f0a02018-06-17 22:33:29 +0200100 }
Harald Welte32852bc2017-02-28 00:21:45 +0100101 rc = FLASHD_Write(addr, data, len);
102 if (rc != 0) {
Kévin Redonb73f0a02018-06-17 22:33:29 +0200103 TRACE_ERROR("DFU download flash erase failed\n\r");
Kévin Redon9def7632018-06-27 16:27:41 +0200104 rc = DFU_RET_STALL;
105 break;
Kévin Redonb73f0a02018-06-17 22:33:29 +0200106 }
Harald Welte1cfc2612018-06-29 21:07:41 +0200107 for (i = 0; i < len; i++) {
Kévin Redonb73f0a02018-06-17 22:33:29 +0200108 if (((uint8_t*)addr)[i]!=data[i]) {
109 TRACE_ERROR("DFU download flash data written not correct\n\r");
Kévin Redon9def7632018-06-27 16:27:41 +0200110 rc = DFU_RET_STALL;
111 break;
Kévin Redonb73f0a02018-06-17 22:33:29 +0200112 }
113 }
Kévin Redon9def7632018-06-27 16:27:41 +0200114 rc = DFU_RET_ZLP;
115 break;
Harald Welte32852bc2017-02-28 00:21:45 +0100116 default:
Harald Welte32852bc2017-02-28 00:21:45 +0100117 TRACE_ERROR("DFU download for unknown AltIf %d\n\r", altif);
Kévin Redon9def7632018-06-27 16:27:41 +0200118 rc = DFU_RET_STALL;
119 break;
Harald Welte32852bc2017-02-28 00:21:45 +0100120 }
Kévin Redon9def7632018-06-27 16:27:41 +0200121
122#ifdef PINS_LEDS
123 PIO_Set(&pinsLeds[LED_NUM_RED]);
124#endif
125
126 return rc;
Harald Welted8a003d2017-02-27 20:31:09 +0100127}
Harald Welte32852bc2017-02-28 00:21:45 +0100128
129/* incoming call-back: Host has requested to read back 'req_len' bytes
130 * starting from 'offset' of the firmware * associated with partition
131 * 'altif' */
132int USBDFU_handle_upload(uint8_t altif, unsigned int offset,
133 uint8_t *data, unsigned int req_len)
134{
135 uint32_t addr;
136
137 printf("upload(altif=%u, offset=%u, len=%u)", altif, offset, req_len);
138
139 switch (altif) {
140 case ALTIF_RAM:
141 addr = RAM_ADDR(offset);
142 if (addr > IRAM_ADDR + IRAM_SIZE) {
Harald Welteadba0ce2017-02-28 01:02:24 +0100143 g_dfu->state = DFU_STATE_dfuERROR;
144 g_dfu->status = DFU_STATUS_errADDRESS;
Harald Welte32852bc2017-02-28 00:21:45 +0100145 return -1;
146 }
147 if ((uint8_t *)addr + req_len > IRAM_END)
148 req_len = IRAM_END - (uint8_t *)addr;
149 memcpy(data, (void *)addr, req_len);
150 break;
151 case ALTIF_FLASH:
152 addr = FLASH_ADDR(offset);
153 if (addr > IFLASH_ADDR + IFLASH_SIZE) {
Harald Welteadba0ce2017-02-28 01:02:24 +0100154 g_dfu->state = DFU_STATE_dfuERROR;
155 g_dfu->status = DFU_STATUS_errADDRESS;
Harald Welte32852bc2017-02-28 00:21:45 +0100156 return -1;
157 }
158 if ((uint8_t *)addr + req_len > IFLASH_END)
159 req_len = IFLASH_END - (uint8_t *)addr;
160 memcpy(data, (void *)addr, req_len);
161 break;
162 default:
163 TRACE_ERROR("DFU upload for unknown AltIf %d\n\r", altif);
164 /* FIXME: set error codes */
165 return -1;
166 }
Harald Welteec0837c2017-03-03 01:33:24 +0100167 printf("=%u\n\r", req_len);
Harald Welte32852bc2017-02-28 00:21:45 +0100168 return req_len;
169}
170
Harald Welte27f5fc62017-11-28 22:15:56 +0100171/* can be overridden by board specific code, e.g. by pushbutton */
172WEAK int board_override_enter_dfu(void)
Harald Welte14051002017-03-04 19:17:27 +0100173{
Harald Welte27f5fc62017-11-28 22:15:56 +0100174 return 0;
Harald Welte14051002017-03-04 19:17:27 +0100175}
176
177/* using this function we can determine if we should enter DFU mode
178 * during boot, or if we should proceed towards the application/runtime */
179int USBDFU_OverrideEnterDFU(void)
180{
181 uint32_t *app_part = (uint32_t *)FLASH_ADDR(0);
Kévin Redon9918c282018-07-07 15:22:16 +0200182 /* at the first call we are before the text segment has been relocated,
183 * so g_dfu is not initialized yet */
184 g_dfu = &_g_dfu;
185 if (USB_DFU_MAGIC == g_dfu->magic) {
186 return 1;
187 }
Harald Welte14051002017-03-04 19:17:27 +0100188
189 /* If the loopback jumper is set, we enter DFU mode */
Kévin Redon9918c282018-07-07 15:22:16 +0200190 if (board_override_enter_dfu()) {
191 return 2;
192 }
Harald Welte14051002017-03-04 19:17:27 +0100193
194 /* if the first word of the application partition doesn't look
195 * like a stack pointer (i.e. point to RAM), enter DFU mode */
Kévin Redon9918c282018-07-07 15:22:16 +0200196 if ((app_part[0] < IRAM_ADDR) || ((uint8_t *)app_part[0] > IRAM_END)) {
197 return 3;
198 }
Harald Welte14051002017-03-04 19:17:27 +0100199
200 /* if the second word of the application partition doesn't look
201 * like a function from flash (reset vector), enter DFU mode */
202 if (((uint32_t *)app_part[1] < app_part) ||
Kévin Redon9918c282018-07-07 15:22:16 +0200203 ((uint8_t *)app_part[1] > IFLASH_END)) {
204 return 4;
205 }
Harald Welte14051002017-03-04 19:17:27 +0100206
207 return 0;
208}
Harald Welted8a003d2017-02-27 20:31:09 +0100209
210/* returns '1' in case we should break any endless loop */
211static void check_exec_dbg_cmd(void)
212{
213 int ch;
214
215 if (!UART_IsRxReady())
216 return;
217
218 ch = UART_GetChar();
219
220 //board_exec_dbg_cmd(ch);
221}
222
223/*------------------------------------------------------------------------------
224 * Main
225 *------------------------------------------------------------------------------*/
226#define MAX_USB_ITER BOARD_MCK/72 // This should be around a second
227extern int main(void)
228{
229 uint8_t isUsbConnected = 0;
230 unsigned int i = 0;
Harald Welte5b108d82017-03-06 21:51:55 +0100231 uint32_t reset_cause = (RSTC->RSTC_SR & RSTC_SR_RSTTYP_Msk) >> RSTC_SR_RSTTYP_Pos;
Harald Welted8a003d2017-02-27 20:31:09 +0100232
Kévin Redon869dbfa2018-06-17 22:36:44 +0200233 /* Enable watchdog for 2000ms, with no window */
Harald Welte45ebe452017-03-03 19:01:53 +0100234 WDT_Enable(WDT, WDT_MR_WDRSTEN | WDT_MR_WDDBGHLT | WDT_MR_WDIDLEHLT |
Kévin Redon869dbfa2018-06-17 22:36:44 +0200235 (WDT_GetPeriod(2000) << 16) | WDT_GetPeriod(2000));
236 watchdog_configured = true;
Harald Welted8a003d2017-02-27 20:31:09 +0100237
Kévin Redon9def7632018-06-27 16:27:41 +0200238#ifdef PINS_LEDS
239 /* Configure LED */
240 PIO_Configure(pinsLeds, sizeof(pinsLeds));
241 PIO_Set(&pinsLeds[LED_NUM_RED]);
242 PIO_Clear(&pinsLeds[LED_NUM_GREEN]);
243#endif
244
Harald Welted8a003d2017-02-27 20:31:09 +0100245 PIO_InitializeInterrupts(0);
246
247 EEFC_ReadUniqueID(g_unique_id);
248
Kévin Redon9918c282018-07-07 15:22:16 +0200249 printf("\n\r\n\r"
Harald Welteec0837c2017-03-03 01:33:24 +0100250 "=============================================================================\n\r"
251 "DFU bootloader %s for board %s (C) 2010-2017 by Harald Welte\n\r"
252 "=============================================================================\n\r",
Harald Welted8a003d2017-02-27 20:31:09 +0100253 manifest_revision, manifest_board);
254
Harald Welteec0837c2017-03-03 01:33:24 +0100255 TRACE_INFO("Chip ID: 0x%08x (Ext 0x%08x)\n\r", CHIPID->CHIPID_CIDR, CHIPID->CHIPID_EXID);
256 TRACE_INFO("Serial Nr. %08x-%08x-%08x-%08x\n\r",
Harald Welted8a003d2017-02-27 20:31:09 +0100257 g_unique_id[0], g_unique_id[1],
258 g_unique_id[2], g_unique_id[3]);
Kévin Redonba153872018-08-28 19:26:11 +0200259 TRACE_INFO("Reset Cause: 0x%lx\n\r", reset_cause);
Harald Welte5b108d82017-03-06 21:51:55 +0100260
Kévin Redon9918c282018-07-07 15:22:16 +0200261#if (TRACE_LEVEL >= TRACE_LEVEL_INFO)
262 /* Find out why we are in the DFU bootloader, and not the main application */
263 TRACE_INFO("DFU bootloader start reason: ");
264 switch (USBDFU_OverrideEnterDFU()) {
265 case 0:
266 /* 0 normally means that there is no override, but we are in the bootloader,
267 * thus the first check in board_cstartup_gnu did return something else than 0.
268 * this can only be g_dfu->magic which is erased when the segment are
269 * relocated, which happens in board_cstartup_gnu just after USBDFU_OverrideEnterDFU.
270 * no static variable can be used to store this case since this will also be overwritten
271 */
272 case 1:
273 TRACE_INFO_WP("DFU switch requested by main application\n\r");
274 break;
275 case 2:
276 TRACE_INFO_WP("bootloader forced (button pressed or jumper set)\n\r");
277 break;
278 case 3:
279 TRACE_INFO_WP("stack pointer (first application word) does no point in RAM\n\r");
280 break;
281 case 4: // the is no reason
282 TRACE_INFO_WP("reset vector (second application word) does no point in flash\n\r");
283 break;
284 default:
285 TRACE_INFO_WP("unknown\n\r");
286 break;
287 }
288#endif
289
Harald Welte5b108d82017-03-06 21:51:55 +0100290 /* clear g_dfu on power-up reset */
291 if (reset_cause == 0)
292 memset(g_dfu, 0, sizeof(*g_dfu));
Harald Welted8a003d2017-02-27 20:31:09 +0100293
Harald Welte8adf0ac2017-03-03 01:52:34 +0100294 board_main_top();
Harald Welted8a003d2017-02-27 20:31:09 +0100295
Harald Welteec0837c2017-03-03 01:33:24 +0100296 TRACE_INFO("USB init...\n\r");
Kévin Redonf5869d42018-06-17 22:31:21 +0200297 /* Signal USB reset by disabling the pull-up on USB D+ for at least 10 ms */
Kévin Redonff3d8492018-08-06 17:57:20 +0200298#ifdef PIN_USB_PULLUP
Kévin Redonf5869d42018-06-17 22:31:21 +0200299 const Pin usb_dp_pullup = PIN_USB_PULLUP;
300 PIO_Configure(&usb_dp_pullup, 1);
301 PIO_Set(&usb_dp_pullup);
Kévin Redonff3d8492018-08-06 17:57:20 +0200302#endif
303 USBD_HAL_Suspend();
304 mdelay(20);
305#ifdef PIN_USB_PULLUP
Kévin Redonf5869d42018-06-17 22:31:21 +0200306 PIO_Clear(&usb_dp_pullup);
Kévin Redonff3d8492018-08-06 17:57:20 +0200307#endif
308 USBD_HAL_Activate();
309
Harald Welted8a003d2017-02-27 20:31:09 +0100310 USBDFU_Initialize(&dfu_descriptors);
311
312 while (USBD_GetState() < USBD_STATE_CONFIGURED) {
Harald Welte45ebe452017-03-03 19:01:53 +0100313 WDT_Restart(WDT);
Harald Welted8a003d2017-02-27 20:31:09 +0100314 check_exec_dbg_cmd();
Harald Welte32852bc2017-02-28 00:21:45 +0100315#if 1
Harald Welted8a003d2017-02-27 20:31:09 +0100316 if (i >= MAX_USB_ITER * 3) {
317 TRACE_ERROR("Resetting board (USB could "
Harald Welteec0837c2017-03-03 01:33:24 +0100318 "not be configured)\n\r");
Harald Weltef415d712017-03-03 01:51:43 +0100319 USBD_Disconnect();
Harald Welted8a003d2017-02-27 20:31:09 +0100320 NVIC_SystemReset();
321 }
322#endif
323 i++;
324 }
325
Kévin Redonb73f0a02018-06-17 22:33:29 +0200326 /* Initialize the flash to be able to write it, using the IAP ROM code */
Harald Welteec9b5ff2017-03-02 23:18:40 +0100327 FLASHD_Initialize(BOARD_MCK, 1);
Kévin Redonb73f0a02018-06-17 22:33:29 +0200328
Harald Welteec0837c2017-03-03 01:33:24 +0100329 TRACE_INFO("entering main loop...\n\r");
Harald Welted8a003d2017-02-27 20:31:09 +0100330 while (1) {
Harald Welte45ebe452017-03-03 19:01:53 +0100331 WDT_Restart(WDT);
Harald Welted8a003d2017-02-27 20:31:09 +0100332#if TRACE_LEVEL >= TRACE_LEVEL_DEBUG
333 const char rotor[] = { '-', '\\', '|', '/' };
334 putchar('\b');
335 putchar(rotor[i++ % ARRAY_SIZE(rotor)]);
336#endif
337 check_exec_dbg_cmd();
Harald Welte3bbaba02017-03-06 22:47:06 +0100338#if 0
339 osmo_timers_prepare();
340 osmo_timers_update();
341#endif
Harald Welted8a003d2017-02-27 20:31:09 +0100342
343 if (USBD_GetState() < USBD_STATE_CONFIGURED) {
344
345 if (isUsbConnected) {
346 isUsbConnected = 0;
347 }
348 } else if (isUsbConnected == 0) {
Harald Welteec0837c2017-03-03 01:33:24 +0100349 TRACE_INFO("USB is now configured\n\r");
Harald Welted8a003d2017-02-27 20:31:09 +0100350
351 isUsbConnected = 1;
352 }
353 }
354}