-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbl_commands.c
565 lines (512 loc) · 13 KB
/
bl_commands.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/*
* This file is part of StormLoader, the Storm Bootloader
*
* StormLoader is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* StormLoader is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with StormLoader. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2014, Michael Andersen <[email protected]>
*/
#include "bootloader.h"
#include <stdlib.h>
#include <flashcalw.h>
#include <ioport.h>
#include <string.h>
#include "info.h"
// COMMAND HELPERS
// ===============
inline void _escape_cat(uint8_t *dat, uint16_t len);
inline void _escape_set(uint8_t *dat, uint16_t len, uint8_t cmd);
inline uint32_t _rx_u32(uint16_t offset);
inline uint16_t _rx_u16(uint16_t offset);
uint8_t _poll_xfbusy(void);
inline void _escape_cat(uint8_t *dat, uint16_t len)
{
while(len-- && (tx_left < (TXBUFSZ-1)))
{
if (*dat == ESCAPE_CHAR)
tx_stage_ram[tx_left++] = ESCAPE_CHAR;
tx_stage_ram[tx_left++] = *dat;
dat ++;
}
}
inline void _escape_set(uint8_t *dat, uint16_t len, uint8_t cmd)
{
tx_ptr = 0;
tx_left = 2;
tx_stage_ram[0] = ESCAPE_CHAR;
tx_stage_ram[1] = cmd;
if (len > 0)
{
_escape_cat(dat, len);
}
}
inline uint32_t _rx_u32(uint16_t offset)
{
uint32_t rv = ((uint32_t)rx_stage_ram[offset++]);
rv |= ((uint32_t)rx_stage_ram[offset++] << 8);
rv |= ((uint32_t)rx_stage_ram[offset++] << 16);
rv |= ((uint32_t)rx_stage_ram[offset++] << 24);
return rv;
}
inline uint16_t _rx_u16(uint16_t offset)
{
uint16_t rv = ((uint16_t)rx_stage_ram[offset++]);
rv |= ((uint16_t)rx_stage_ram[offset++] << 8);
return rv;
}
uint8_t _poll_xfbusy()
{
uint32_t timeout = 0;
for (timeout = 1000000; timeout > 0; timeout--)
{
uint8_t xf [] = {0xD7, 0x00, 0x00};
bl_spi_xfer(xf,xf,3);
if ((xf[2] >> 5) & 1) //Program/erase failure
return 2;
if (xf[2] >> 7)
return 0;
}
return 1;
}
// COMMANDS
// ========
void bl_c_ping()
{
_escape_set(NULL, 0, RES_PONG);
}
void bl_c_info()
{
char rv [193];
memset(rv, 0, 193);
uint8_t len = snprintf(&rv[1], 191, "StormLoader "BOOTLOADER_VERSION" ("BOOTLOADER_DATE")\n"
"Copyright 2014 Michael Andersen, UC Berkeley\n\n"
"This is free software, and you are welcome to redistribute it\n"
"See http://storm.pm/license for details\n");
rv[0] = len;
_escape_set((uint8_t*)&rv[0], 193, RES_INFO);
}
void bl_c_id()
{
//Read the program info and stuff
//Test holder for now
}
void bl_c_reset()
{
tx_left = 0;
rx_ptr = 0;
tx_left = 0;
}
void bl_c_epage()
{
if (rx_ptr != (4))
{
_escape_set(NULL, 0, RES_BADARGS);
return;
}
uint32_t addr = _rx_u32(0);
uint32_t pagenum = addr >> 9;
bool brv;
if (addr < ALLOWED_FLASH_FLOOR || addr >= ALLOWED_FLASH_CEILING ||
(addr & 511) != 0)
{
_escape_set(NULL, 0, RES_BADADDR);
return;
}
flashcalw_default_wait_until_ready();
brv = flashcalw_quick_page_read(pagenum);
if (brv)
{ //This means the page is already erased
_escape_set(NULL, 0, RES_OK);
return;
}
flashcalw_default_wait_until_ready();
brv = flashcalw_erase_page(pagenum, true);
flashcalw_picocache_invalid_all();
if (!brv)
{
_escape_set(NULL, 0, RES_INTERROR);
return;
}
flashcalw_default_wait_until_ready();
_escape_set(NULL, 0, RES_OK);
return;
}
void bl_c_wpage()
{
if (rx_ptr != (512+4))
{
_escape_set(NULL, 0, RES_BADARGS);
return;
}
uint32_t addr = _rx_u32(0);
uint32_t pagenum = addr >> 9;
bool brv;
if (addr < ALLOWED_FLASH_FLOOR || addr >= ALLOWED_FLASH_CEILING ||
(addr & 511) != 0)
{
_escape_set(NULL, 0, RES_BADADDR);
return;
}
flashcalw_default_wait_until_ready();
brv = flashcalw_erase_page(pagenum, true);
flashcalw_picocache_invalid_all();
if (!brv)
{
_escape_set(NULL, 0, RES_INTERROR);
return;
}
flashcalw_default_wait_until_ready();
flashcalw_clear_page_buffer();
flashcalw_default_wait_until_ready();
uint32_t *fp = (uint32_t*) addr;
uint16_t i;
for (i=4; i < 516; i+=4)
{
*fp = _rx_u32(i);
fp++;
}
flashcalw_default_wait_until_ready();
flashcalw_write_page(pagenum);
flashcalw_picocache_invalid_all();
flashcalw_default_wait_until_ready();
_escape_set(NULL, 0, RES_OK);
return;
}
void bl_c_xeblock(void)
{
if (rx_ptr != 4)
{
_escape_set(NULL, 0, RES_BADARGS);
return;
}
uint32_t baddr = _rx_u32(0);
if (baddr < ALLOWED_XFLASH_FLOOR || baddr >= ALLOWED_XFLASH_CEILING
|| (baddr & (XBLOCKSIZE-1)) != 0)
{
_escape_set(NULL, 0, RES_BADADDR);
return;
}
uint8_t eblock [] = {0x50, 0, 0, 0};
eblock[1] = (uint8_t) (baddr >> 16);
eblock[2] = (uint8_t) (baddr >> 8);
eblock[3] = (uint8_t) (baddr);
bl_spi_tx(eblock, 4, 1);
uint8_t iserr = _poll_xfbusy();
if (iserr == 1)
{
_escape_set(NULL, 0, RES_XFTIMEOUT);
return;
}
else if (iserr == 2)
{
_escape_set(NULL, 0, RES_XFEPE);
return;
}
else
{
_escape_set(NULL, 0, RES_OK);
return;
}
}
void bl_c_xwpage(void)
{
if (rx_ptr != 260)
{
_escape_set(NULL, 0, RES_BADARGS);
return;
}
uint32_t addr = _rx_u32(0);
if (addr < ALLOWED_XFLASH_FLOOR || addr >= ALLOWED_XFLASH_CEILING
|| (addr & (XPAGESIZE-1)) != 0)
{
_escape_set(NULL, 0, RES_BADADDR);
return;
}
uint8_t startprog [] = {0x82, 0x00, 0x00, 0x00};
startprog[1] = (uint8_t) (addr >> 16);
startprog[2] = (uint8_t) (addr >> 8);
startprog[3] = (uint8_t) (addr);
bl_spi_tx(startprog, 4, 0);
bl_spi_tx(&rx_stage_ram[4],256,1);
uint8_t iserr = _poll_xfbusy();
if (iserr == 1)
{
_escape_set(NULL, 0, RES_XFTIMEOUT);
return;
}
else if (iserr == 2)
{
_escape_set(NULL, 0, RES_XFEPE);
return;
}
else
{
_escape_set(NULL, 0, RES_OK);
return;
}
}
void bl_c_crcrx(void)
{
uint8_t rv [6];
rv[0] = (uint8_t) rx_ptr & 0xFF;
rv[1] = (uint8_t) (rx_ptr >> 8);
if (rx_ptr == 0)
{
rv[2] = rv[3] = rv[4] = rv[5] = 0xFF;
}
else
{
uint32_t crc = crc32(0, &rx_stage_ram[0],rx_ptr);
rv[2] = (uint8_t) (crc & 0xFF); crc >>=8;
rv[3] = (uint8_t) (crc & 0xFF); crc >>=8;
rv[4] = (uint8_t) (crc & 0xFF); crc >>=8;
rv[5] = (uint8_t) (crc & 0xFF);
}
_escape_set(rv, 6, RES_CRCRX);
}
void bl_c_rrange(void)
{
if (rx_ptr != 6)
{
_escape_set(NULL, 0, RES_BADARGS);
return;
}
uint16_t len = _rx_u16(4);
//We only go to half the buffer because of escape expanding
if (rx_ptr != 6 || len >= (TXBUFSZ>>1))
{
_escape_set(NULL, 0, RES_BADARGS);
return;
}
uint32_t addr = _rx_u32(0);
if ((addr+len) > ALLOWED_FLASH_CEILING+1)
{
_escape_set(NULL, 0, RES_BADADDR);
return;
}
flashcalw_picocache_invalid_all();
uint8_t* p = (uint8_t*) addr;
_escape_set(p, len, RES_RRANGE);
return;
}
void bl_c_xrrange(void)
{
if (rx_ptr != 6)
{
_escape_set(NULL, 0, RES_BADARGS);
return;
}
uint16_t len = _rx_u16(4);
//We only go to half the buffer because of escape expanding
if (rx_ptr != 6 || len >= (TXBUFSZ>>1))
{
_escape_set(NULL, 0, RES_BADARGS);
return;
}
uint32_t addr = _rx_u32(0);
if ((addr+len) > ALLOWED_XFLASH_CEILING+1)
{
_escape_set(NULL, 0, RES_BADADDR);
return;
}
uint8_t rd [] = {0x1B, 0x00, 0x00, 0x00, 0x00, 0x00};
rd[1] = (uint8_t) (addr >> 16);
rd[2] = (uint8_t) (addr >> 8);
rd[3] = (uint8_t) (addr);
bl_spi_tx(rd,6,0);
//We store the data in RX stage so we can escape it into tx
bl_spi_rx(rx_stage_ram, len);
_escape_set(rx_stage_ram, len, RES_XRRANGE);
return;
}
void bl_c_sattr(void)
{
uint8_t vlen = rx_stage_ram[9];
if (rx_ptr != 10+vlen)
{
_escape_set(NULL, 0, RES_BADARGS);
return;
}
//Attributes are stored in the first four pages of flash.
//Each attribute is 64 bytes long.
uint8_t idx = rx_stage_ram[0];
if (idx >= 16 || vlen >=56)
{
_escape_set(NULL, 0, RES_BADADDR);
return;
}
uint32_t addr = idx*64;
uint8_t fcmd [68];
fcmd[0] = 0x58;
fcmd[1] = (uint8_t) (addr >> 16);
fcmd[2] = (uint8_t) (addr >> 8);
fcmd[3] = (uint8_t) (addr);
uint8_t i;
for (i=0;i<8;i++) fcmd[4+i] = rx_stage_ram[1+i];
fcmd[12] = vlen;
for (i=0;i<vlen;i++) fcmd[13+i] = rx_stage_ram[10+i];
bl_spi_tx(fcmd,13+vlen,1);
uint8_t iserr = _poll_xfbusy();
if (iserr == 1)
{
_escape_set(NULL, 0, RES_XFTIMEOUT);
return;
}
else if (iserr == 2)
{
_escape_set(NULL, 0, RES_XFEPE);
return;
}
else
{
_escape_set(NULL, 0, RES_OK);
return;
}
}
void bl_c_gattr(void)
{
if (rx_ptr != 1)
{
_escape_set(NULL, 0, RES_BADARGS);
return;
}
if (rx_stage_ram[0] >= 16)
{
_escape_set(NULL, 0, RES_BADADDR);
return;
}
uint32_t addr = rx_stage_ram[0]*64;
uint8_t buf[64];
uint8_t rd [] = {0x1B, 0x00, 0x00, 0x00, 0x00, 0x00};
rd[1] = (uint8_t) (addr >> 16);
rd[2] = (uint8_t) (addr >> 8);
rd[3] = (uint8_t) (addr);
bl_spi_tx(rd,6,0);
//We store the data in RX stage so we can escape it into tx
bl_spi_rx(buf, 64);
_escape_set(buf, 64, RES_GATTR);
return;
}
void bl_c_crcif(void)
{
if (rx_ptr != 8)
{
_escape_set(NULL, 0, RES_BADARGS);
return;
}
uint32_t base = _rx_u32(0);
uint32_t len = _rx_u32(4);
if(base >= ALLOWED_FLASH_CEILING || (base+len) > ALLOWED_FLASH_CEILING+1
|| (len >= 512*1024))
{
_escape_set(NULL, 0, RES_BADADDR);
return;
}
flashcalw_picocache_invalid_all();
uint8_t* p = (uint8_t*) base;
uint32_t crc = crc32(0, p, len);
uint8_t rv [4];
rv[0] = (uint8_t) (crc & 0xFF); crc >>=8;
rv[1] = (uint8_t) (crc & 0xFF); crc >>=8;
rv[2] = (uint8_t) (crc & 0xFF); crc >>=8;
rv[3] = (uint8_t) (crc & 0xFF);
_escape_set(rv, 6, RES_CRCIF);
}
void bl_c_crcef(void)
{
if (rx_ptr != 8)
{
_escape_set(NULL, 0, RES_BADARGS);
return;
}
uint32_t len = _rx_u16(4);
uint32_t addr = _rx_u32(0);
if ((addr+len) > ALLOWED_XFLASH_CEILING+1)
{
_escape_set(NULL, 0, RES_BADADDR);
return;
}
uint32_t left = len;
uint32_t crc = 0;
while(left > 0)
{
uint16_t toread = 4096;
if (toread > left) toread = left;
uint8_t rd [] = {0x1B, 0x00, 0x00, 0x00, 0x00, 0x00};
rd[1] = (uint8_t) (addr >> 16);
rd[2] = (uint8_t) (addr >> 8);
rd[3] = (uint8_t) (addr);
bl_spi_tx(rd,6,0);
//We store the data in RX stage so we can escape it into tx
bl_spi_rx(&rx_stage_ram[0], toread);
uint16_t i;
crc = crc32(crc, &rx_stage_ram[0], toread);
left -= toread;
addr += toread;
}
uint8_t rv [4];
rv[0] = (uint8_t) (crc & 0xFF); crc >>=8;
rv[1] = (uint8_t) (crc & 0xFF); crc >>=8;
rv[2] = (uint8_t) (crc & 0xFF); crc >>=8;
rv[3] = (uint8_t) (crc & 0xFF);
_escape_set(rv, 4, RES_CRCXF);
return;
}
void bl_c_xepage(void)
{
if (rx_ptr != 4)
{
_escape_set(NULL, 0, RES_BADARGS);
return;
}
uint32_t addr = _rx_u32(0);
if (addr < ALLOWED_XFLASH_FLOOR || addr >= ALLOWED_XFLASH_CEILING
|| (addr & (XPAGESIZE-1)) != 0)
{
_escape_set(NULL, 0, RES_BADADDR);
return;
}
uint8_t epage [] = {0x81, 0, 0, 0};
epage[1] = (uint8_t) (addr >> 16);
epage[2] = (uint8_t) (addr >> 8);
epage[3] = (uint8_t) (addr);
bl_spi_tx(epage, 4, 1);
uint8_t iserr = _poll_xfbusy();
if (iserr == 1)
{
_escape_set(NULL, 0, RES_XFTIMEOUT);
return;
}
else if (iserr == 2)
{
_escape_set(NULL, 0, RES_XFEPE);
return;
}
else
{
_escape_set(NULL, 0, RES_OK);
return;
}
}
void bl_c_xfinit(void)
{
uint8_t switch_to_264b [] = {0x3D, 0x2A, 0x80, 0xA6};
bl_spi_tx(switch_to_264b, 4, 1);
_escape_set(NULL, 0, RES_OK);
return;
}
void bl_c_unknown()
{
_escape_set(NULL, 0, RES_UNKNOWN);
return;
}