blob: a5b6f74d2e5234244aab3c296b56120af581359a [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];
13
14/*----------------------------------------------------------------------------
15 * Callbacks
16 *----------------------------------------------------------------------------*/
17
Harald Welte32852bc2017-02-28 00:21:45 +010018#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
Kévin Redon318309f2018-06-01 11:02:49 +020024/* incoming call-back: Host has transferred 'len' bytes (stored at
Harald Welte32852bc2017-02-28 00:21:45 +010025 * 'data'), which we shall write to 'offset' into the partition
Kévin Redon318309f2018-06-01 11:02:49 +020026 * associated with 'altif'. Guaranted to be less than
Harald Welte32852bc2017-02-28 00:21:45 +010027 * BOARD_DFU_PAGE_SIZE */
28int USBDFU_handle_dnload(uint8_t altif, unsigned int offset,
29 uint8_t *data, unsigned int len)
Harald Welted8a003d2017-02-27 20:31:09 +010030{
Harald Welte32852bc2017-02-28 00:21:45 +010031 uint32_t addr;
32 int rc;
Kévin Redon318309f2018-06-01 11:02:49 +020033 /* address of the last allocated variable on the stack */
34 uint32_t stack_addr = (uint32_t)&rc;
Harald Welte32852bc2017-02-28 00:21:45 +010035
Harald Weltee8eea292017-03-03 00:35:51 +010036 printf("dnload(altif=%u, offset=%u, len=%u)\n\r", altif, offset, len);
Harald Welte32852bc2017-02-28 00:21:45 +010037
38 switch (altif) {
39 case ALTIF_RAM:
40 addr = RAM_ADDR(offset);
Kévin Redonb73f0a02018-06-17 22:33:29 +020041 if (addr < IRAM_ADDR || addr + len >= IRAM_ADDR + IRAM_SIZE || addr + len >= stack_addr) {
Harald Welteadba0ce2017-02-28 01:02:24 +010042 g_dfu->state = DFU_STATE_dfuERROR;
43 g_dfu->status = DFU_STATUS_errADDRESS;
Harald Welte32852bc2017-02-28 00:21:45 +010044 return DFU_RET_STALL;
45 }
46 memcpy((void *)addr, data, len);
47 return DFU_RET_ZLP;
48 case ALTIF_FLASH:
49 addr = FLASH_ADDR(offset);
Kévin Redonb73f0a02018-06-17 22:33:29 +020050 if (addr < IFLASH_ADDR || addr + len >= IFLASH_ADDR + IFLASH_SIZE) {
Harald Welteadba0ce2017-02-28 01:02:24 +010051 g_dfu->state = DFU_STATE_dfuERROR;
52 g_dfu->status = DFU_STATUS_errADDRESS;
Harald Welte32852bc2017-02-28 00:21:45 +010053 return DFU_RET_STALL;
54 }
Kévin Redonb73f0a02018-06-17 22:33:29 +020055 rc = FLASHD_Unlock(addr, addr + len, 0, 0);
56 if (rc != 0) {
57 TRACE_ERROR("DFU download flash unlock failed\n\r");
58 /* FIXME: set error codes */
59 return DFU_RET_STALL;
60 }
Harald Welte32852bc2017-02-28 00:21:45 +010061 rc = FLASHD_Write(addr, data, len);
62 if (rc != 0) {
Kévin Redonb73f0a02018-06-17 22:33:29 +020063 TRACE_ERROR("DFU download flash erase failed\n\r");
64 /* FIXME: set error codes */
65 return DFU_RET_STALL;
66 }
67 for (unsigned int i=0; i<len; i++) {
68 if (((uint8_t*)addr)[i]!=data[i]) {
69 TRACE_ERROR("DFU download flash data written not correct\n\r");
70 return DFU_RET_STALL;
71 }
72 }
73 rc = FLASHD_Lock(addr, addr + len, 0, 0);
74 if (rc != 0) {
75 TRACE_ERROR("DFU download flash lock failed\n\r");
Harald Welte32852bc2017-02-28 00:21:45 +010076 /* FIXME: set error codes */
77 return DFU_RET_STALL;
78 }
79 return DFU_RET_ZLP;
80 default:
81 /* FIXME: set error codes */
82 TRACE_ERROR("DFU download for unknown AltIf %d\n\r", altif);
83 return DFU_RET_STALL;
84 }
Harald Welted8a003d2017-02-27 20:31:09 +010085}
Harald Welte32852bc2017-02-28 00:21:45 +010086
87/* incoming call-back: Host has requested to read back 'req_len' bytes
88 * starting from 'offset' of the firmware * associated with partition
89 * 'altif' */
90int USBDFU_handle_upload(uint8_t altif, unsigned int offset,
91 uint8_t *data, unsigned int req_len)
92{
93 uint32_t addr;
94
95 printf("upload(altif=%u, offset=%u, len=%u)", altif, offset, req_len);
96
97 switch (altif) {
98 case ALTIF_RAM:
99 addr = RAM_ADDR(offset);
100 if (addr > IRAM_ADDR + IRAM_SIZE) {
Harald Welteadba0ce2017-02-28 01:02:24 +0100101 g_dfu->state = DFU_STATE_dfuERROR;
102 g_dfu->status = DFU_STATUS_errADDRESS;
Harald Welte32852bc2017-02-28 00:21:45 +0100103 return -1;
104 }
105 if ((uint8_t *)addr + req_len > IRAM_END)
106 req_len = IRAM_END - (uint8_t *)addr;
107 memcpy(data, (void *)addr, req_len);
108 break;
109 case ALTIF_FLASH:
110 addr = FLASH_ADDR(offset);
111 if (addr > IFLASH_ADDR + IFLASH_SIZE) {
Harald Welteadba0ce2017-02-28 01:02:24 +0100112 g_dfu->state = DFU_STATE_dfuERROR;
113 g_dfu->status = DFU_STATUS_errADDRESS;
Harald Welte32852bc2017-02-28 00:21:45 +0100114 return -1;
115 }
116 if ((uint8_t *)addr + req_len > IFLASH_END)
117 req_len = IFLASH_END - (uint8_t *)addr;
118 memcpy(data, (void *)addr, req_len);
119 break;
120 default:
121 TRACE_ERROR("DFU upload for unknown AltIf %d\n\r", altif);
122 /* FIXME: set error codes */
123 return -1;
124 }
Harald Welteec0837c2017-03-03 01:33:24 +0100125 printf("=%u\n\r", req_len);
Harald Welte32852bc2017-02-28 00:21:45 +0100126 return req_len;
127}
128
Harald Welte27f5fc62017-11-28 22:15:56 +0100129/* can be overridden by board specific code, e.g. by pushbutton */
130WEAK int board_override_enter_dfu(void)
Harald Welte14051002017-03-04 19:17:27 +0100131{
Harald Welte27f5fc62017-11-28 22:15:56 +0100132 return 0;
Harald Welte14051002017-03-04 19:17:27 +0100133}
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 */
137int 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 */
Harald Welte27f5fc62017-11-28 22:15:56 +0100142 if (board_override_enter_dfu())
Harald Welte14051002017-03-04 19:17:27 +0100143 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 Welted8a003d2017-02-27 20:31:09 +0100159
160/* returns '1' in case we should break any endless loop */
161static 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
177extern int main(void)
178{
179 uint8_t isUsbConnected = 0;
180 unsigned int i = 0;
Harald Welte5b108d82017-03-06 21:51:55 +0100181 uint32_t reset_cause = (RSTC->RSTC_SR & RSTC_SR_RSTTYP_Msk) >> RSTC_SR_RSTTYP_Pos;
Harald Welted8a003d2017-02-27 20:31:09 +0100182
Harald Welte3bbaba02017-03-06 22:47:06 +0100183#if 0
Harald Welteabba8a82017-03-06 16:58:00 +0100184 led_init();
185 led_blink(LED_GREEN, BLINK_3O_30F);
186 led_blink(LED_RED, BLINK_3O_30F);
Harald Welte3bbaba02017-03-06 22:47:06 +0100187#endif
Harald Welte45ebe452017-03-03 19:01:53 +0100188
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 Welted8a003d2017-02-27 20:31:09 +0100192
Harald Welted8a003d2017-02-27 20:31:09 +0100193 PIO_InitializeInterrupts(0);
194
195 EEFC_ReadUniqueID(g_unique_id);
196
Harald Welteec0837c2017-03-03 01:33:24 +0100197 printf("\n\r\n\r"
198 "=============================================================================\n\r"
199 "DFU bootloader %s for board %s (C) 2010-2017 by Harald Welte\n\r"
200 "=============================================================================\n\r",
Harald Welted8a003d2017-02-27 20:31:09 +0100201 manifest_revision, manifest_board);
202
Harald Welteec0837c2017-03-03 01:33:24 +0100203 TRACE_INFO("Chip ID: 0x%08x (Ext 0x%08x)\n\r", CHIPID->CHIPID_CIDR, CHIPID->CHIPID_EXID);
204 TRACE_INFO("Serial Nr. %08x-%08x-%08x-%08x\n\r",
Harald Welted8a003d2017-02-27 20:31:09 +0100205 g_unique_id[0], g_unique_id[1],
206 g_unique_id[2], g_unique_id[3]);
Harald Welte5b108d82017-03-06 21:51:55 +0100207 TRACE_INFO("Reset Cause: 0x%x\n\r", reset_cause);
208
209 /* clear g_dfu on power-up reset */
210 if (reset_cause == 0)
211 memset(g_dfu, 0, sizeof(*g_dfu));
Harald Welted8a003d2017-02-27 20:31:09 +0100212
Harald Welte8adf0ac2017-03-03 01:52:34 +0100213 board_main_top();
Harald Welted8a003d2017-02-27 20:31:09 +0100214
Harald Welteec0837c2017-03-03 01:33:24 +0100215 TRACE_INFO("USB init...\n\r");
Kévin Redonf5869d42018-06-17 22:31:21 +0200216 /* Signal USB reset by disabling the pull-up on USB D+ for at least 10 ms */
217 const Pin usb_dp_pullup = PIN_USB_PULLUP;
218 PIO_Configure(&usb_dp_pullup, 1);
219 PIO_Set(&usb_dp_pullup);
220 mdelay(15);
221 PIO_Clear(&usb_dp_pullup);
Harald Welted8a003d2017-02-27 20:31:09 +0100222 USBDFU_Initialize(&dfu_descriptors);
223
224 while (USBD_GetState() < USBD_STATE_CONFIGURED) {
Harald Welte45ebe452017-03-03 19:01:53 +0100225 WDT_Restart(WDT);
Harald Welted8a003d2017-02-27 20:31:09 +0100226 check_exec_dbg_cmd();
Harald Welte32852bc2017-02-28 00:21:45 +0100227#if 1
Harald Welted8a003d2017-02-27 20:31:09 +0100228 if (i >= MAX_USB_ITER * 3) {
229 TRACE_ERROR("Resetting board (USB could "
Harald Welteec0837c2017-03-03 01:33:24 +0100230 "not be configured)\n\r");
Harald Weltef415d712017-03-03 01:51:43 +0100231 USBD_Disconnect();
Harald Welted8a003d2017-02-27 20:31:09 +0100232 NVIC_SystemReset();
233 }
234#endif
235 i++;
236 }
237
Kévin Redonb73f0a02018-06-17 22:33:29 +0200238 /* Initialize the flash to be able to write it, using the IAP ROM code */
Harald Welteec9b5ff2017-03-02 23:18:40 +0100239 FLASHD_Initialize(BOARD_MCK, 1);
Kévin Redonb73f0a02018-06-17 22:33:29 +0200240
Harald Welteec0837c2017-03-03 01:33:24 +0100241 TRACE_INFO("entering main loop...\n\r");
Harald Welted8a003d2017-02-27 20:31:09 +0100242 while (1) {
Harald Welte45ebe452017-03-03 19:01:53 +0100243 WDT_Restart(WDT);
Harald Welted8a003d2017-02-27 20:31:09 +0100244#if TRACE_LEVEL >= TRACE_LEVEL_DEBUG
245 const char rotor[] = { '-', '\\', '|', '/' };
246 putchar('\b');
247 putchar(rotor[i++ % ARRAY_SIZE(rotor)]);
248#endif
249 check_exec_dbg_cmd();
Harald Welte3bbaba02017-03-06 22:47:06 +0100250#if 0
251 osmo_timers_prepare();
252 osmo_timers_update();
253#endif
Harald Welted8a003d2017-02-27 20:31:09 +0100254
255 if (USBD_GetState() < USBD_STATE_CONFIGURED) {
256
257 if (isUsbConnected) {
258 isUsbConnected = 0;
259 }
260 } else if (isUsbConnected == 0) {
Harald Welteec0837c2017-03-03 01:33:24 +0100261 TRACE_INFO("USB is now configured\n\r");
Harald Welted8a003d2017-02-27 20:31:09 +0100262
263 isUsbConnected = 1;
264 }
265 }
266}