/
fw_update.c
335 lines (278 loc) · 7.35 KB
/
fw_update.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
* fw_update.c
*
* Update bootloader for main firmware.
*
* Written & released by Keir Fraser <keir.xen@gmail.com>
*
* This is free and unencumbered software released into the public domain.
* See the file COPYING for more details, or visit <http://unlicense.org>.
*/
#if MCU == STM32F1
/* 8kB-64kB (56kB total) */
#define FIRMWARE_START 0x08002000
#define FIRMWARE_END 0x08010000
#elif MCU == STM32F7
/* 16kB-64KB (48kB total) */
#define FIRMWARE_START 0x08004000
#define FIRMWARE_END 0x08010000
#endif
int EXC_reset(void) __attribute__((alias("main")));
static struct {
time_t time;
bool_t state;
} blink;
static enum {
ST_inactive,
ST_command_wait,
ST_update,
} state = ST_inactive;
static uint8_t u_buf[2048] aligned(4);
static uint32_t u_prod;
static bool_t upd_strapped;
struct gw_info gw_info = {
.is_main_firmware = 0,
.max_cmd = CMD_MAX,
.hw_model = MCU
};
static void blink_init(void)
{
blink.time = time_now();
blink.state = FALSE;
act_led(FALSE);
}
static void update_reset(void)
{
blink_init();
state = ST_inactive;
}
static void update_configure(void)
{
blink_init();
state = ST_command_wait;
u_prod = 0;
}
const struct usb_class_ops usb_cdc_acm_ops = {
.reset = update_reset,
.configure = update_configure
};
static void end_command(void *ack, unsigned int ack_len)
{
if (state == ST_command_wait)
blink_init();
usb_write(EP_TX, ack, ack_len);
u_prod = 0;
}
static struct {
uint32_t len;
uint32_t cur;
} update;
static void erase_old_firmware(void)
{
uint32_t p;
for (p = FIRMWARE_START; p < FIRMWARE_END; p += FLASH_PAGE_SIZE)
fpec_page_erase(p);
}
static void update_prep(uint32_t len)
{
fpec_init();
erase_old_firmware();
state = ST_update;
update.cur = 0;
update.len = len;
printk("Update: %u bytes\n", len);
}
static void update_continue(void)
{
int len;
if ((len = ep_rx_ready(EP_RX)) >= 0) {
len = min_t(int, len, update.len - update.cur);
usb_read(EP_RX, &u_buf[u_prod], len);
u_prod += len;
}
if ((len = u_prod) >= 2) {
int nr = len & ~1;
fpec_write(u_buf, nr, FIRMWARE_START + update.cur);
update.cur += nr;
u_prod -= nr;
memcpy(u_buf, &u_buf[nr], u_prod);
}
if ((update.cur >= update.len) && ep_tx_ready(EP_TX)) {
uint16_t crc = crc16_ccitt((void *)FIRMWARE_START, update.len, 0xffff);
printk("Final CRC: %04x (%s)\n", crc, crc ? "FAIL" : "OK");
u_buf[0] = !!crc;
state = ST_command_wait;
end_command(u_buf, 1);
if (crc)
erase_old_firmware();
}
}
static void process_command(void)
{
uint8_t cmd = u_buf[0];
uint8_t len = u_buf[1];
uint8_t resp_sz = 2;
act_led(TRUE);
switch (cmd) {
case CMD_GET_INFO: {
uint8_t idx = u_buf[2];
if ((len != 3) || (idx != 0))
goto bad_command;
memset(&u_buf[2], 0, 32);
gw_info.fw_major = fw_major;
gw_info.fw_minor = fw_minor;
/* sample_freq is used as flags: bit 0 indicates if we entered
* the bootloader because the update jumper is strapped. */
gw_info.sample_freq = upd_strapped;
memcpy(&u_buf[2], &gw_info, sizeof(gw_info));
resp_sz += 32;
break;
}
case CMD_UPDATE: {
uint32_t u_len = *(uint32_t *)&u_buf[2];
if (len != 6) goto bad_command;
if (u_len & 3) goto bad_command;
update_prep(u_len);
break;
}
#if MCU == STM32F7
case CMD_SWITCH_FW_MODE: {
uint8_t mode = u_buf[2];
if ((len != 3) || (mode & ~1))
goto bad_command;
if (mode == FW_MODE_NORMAL) {
usb_deinit();
delay_ms(500);
system_reset();
}
break;
}
#endif
default:
goto bad_command;
}
u_buf[1] = ACK_OKAY;
out:
end_command(u_buf, resp_sz);
return;
bad_command:
u_buf[1] = ACK_BAD_COMMAND;
goto out;
}
static void update_process(void)
{
int len;
time_t now = time_now();
switch (state) {
case ST_command_wait:
if (time_diff(blink.time, now) > time_ms(200)) {
blink.time = now;
blink.state ^= 1;
act_led(blink.state);
}
len = ep_rx_ready(EP_RX);
if ((len >= 0) && (len < (sizeof(u_buf)-u_prod))) {
usb_read(EP_RX, &u_buf[u_prod], len);
u_prod += len;
}
if ((u_prod >= 2) && (u_prod >= u_buf[1]) && ep_tx_ready(EP_TX)) {
process_command();
}
break;
case ST_update:
update_continue();
break;
default:
break;
}
}
#if MCU == STM32F1
static bool_t enter_bootloader(void)
{
/* Turn on AFIO and GPIOA clocks. */
rcc->apb2enr = RCC_APB2ENR_IOPAEN | RCC_APB2ENR_AFIOEN;
/* Turn off serial-wire JTAG and reclaim the GPIOs. */
afio->mapr = AFIO_MAPR_SWJ_CFG_DISABLED;
/* Enable GPIOA, set all pins as floating, except PA14 = weak pull-up. */
gpioa->odr = 0xffffu;
gpioa->crh = 0x48444444u;
gpioa->crl = 0x44444444u;
/* Wait for PA14 to be pulled HIGH. */
cpu_relax();
cpu_relax();
/* Enter update mode only if PA14 (DCLK) is strapped to GND. */
upd_strapped = !gpio_read_pin(gpioa, 14);
return upd_strapped;
}
#elif MCU == STM32F7
static bool_t check_update_requested(void)
{
/* Check-and-clear a magic value poked into SRAM1 by the main firmware. */
bool_t match = (*(volatile uint32_t *)0x20010000 == 0xdeadbeef);
*(volatile uint32_t *)0x20010000 = 0;
return match;
}
static bool_t check_update_strapped(void)
{
int i, j;
/* Check whether the Serial TX/RX lines (PA9/PA10) are strapped. */
rcc->ahb1enr |= RCC_AHB1ENR_GPIOAEN;
(void)rcc->ahb1enr;
gpio_configure_pin(gpioa, 9, GPO_pushpull(IOSPD_LOW, HIGH));
gpio_configure_pin(gpioa, 10, GPI_pull_up);
for (i = 0; i < 10; i++) {
gpio_write_pin(gpioa, 9, i&1);
for (j = 0; j < 10000; j++)
cpu_relax();
if (gpio_read_pin(gpioa, 10) != (i&1))
return FALSE;
}
return TRUE;
}
static bool_t enter_bootloader(void)
{
bool_t upd_requested = check_update_requested();
upd_strapped = check_update_strapped();
return upd_requested || upd_strapped;
}
#endif
int main(void)
{
/* Relocate DATA. Initialise BSS. */
if (_sdat != _ldat)
memcpy(_sdat, _ldat, _edat-_sdat);
memset(_sbss, 0, _ebss-_sbss);
if (!enter_bootloader()) {
/* Nope, so jump straight at the main firmware. */
uint32_t sp = *(uint32_t *)FIRMWARE_START;
uint32_t pc = *(uint32_t *)(FIRMWARE_START + 4);
if (sp != ~0u) { /* only if firmware is apparently not erased */
asm volatile (
"mov sp,%0 ; blx %1"
:: "r" (sp), "r" (pc));
}
}
stm32_init();
time_init();
console_init();
console_crash_on_input();
board_init();
printk("\n** Greaseweazle Update Bootloader v%u.%u\n", fw_major, fw_minor);
printk("** Keir Fraser <keir.xen@gmail.com>\n");
printk("** https://github.com/keirf/Greaseweazle\n\n");
usb_init();
for (;;) {
usb_process();
update_process();
}
return 0;
}
/*
* Local variables:
* mode: C
* c-file-style: "Linux"
* c-basic-offset: 4
* tab-width: 4
* indent-tabs-mode: nil
* End:
*/