forked from slightlynybbled/bootypic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootloader.c
363 lines (288 loc) · 10.2 KB
/
bootloader.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
#include "xc.h"
#include "config.h"
#include "bootloader.h"
#if MAX_PROG_SIZE % _FLASH_ROW != 0
#error "MAX_PROG_SIZE must be a multiple of _FLASH_ROW"
#endif
/* bootloader starting address (cannot write to addresses between
* BOOTLOADER_START_ADDRESS and APPLICATION_START_ADDRESS) */
#if _FLASH_PAGE == 128
#define BOOTLOADER_START_ADDRESS 0x200
#elif _FLASH_PAGE == 512
#define BOOTLOADER_START_ADDRESS 0x400
#elif _FLASH_PAGE == 1024
#define BOOTLOADER_START_ADDRESS 0x800
#endif
static uint8_t message[RX_BUF_LEN] = {0};
static uint8_t f16_sum1 = 0, f16_sum2 = 0;
int main(void) {
if (pre_boot()) {
/* initialize the peripherals from the user-supplied initialization functions */
initPins();
initOsc();
initUart();
initTimers();
/* wait until something is received on the serial port */
while(!should_abort_boot()){
ClrWdt();
receiveBytes();
}
}
startApp(APPLICATION_START_ADDRESS);
return 0;
}
typedef enum ParseState {
STATE_WAIT_FOR_START, ///< Have not yet received a start byte.
STATE_READ_ESCAPED, ///< Have received an escape byte, next data byte must be decoded
STATE_READ_VERBATIM, ///< Have not received an escape byte, next data byte should be read as-is
STATE_END_OF_MESSAGE,
} ParseState;
void receiveBytes(void){
static uint16_t messageIndex = 0;
static ParseState state = STATE_WAIT_FOR_START;
// keep reading until one of the following:
// 1. we don't have any data available in the uart buffer to read in which case we return
// and wait for this function to be called again
// 2. we find an END OF MESSAGE, in which case we call processCommand to deal with the data
// 3. we overflow the receive buffer, in which case we throw out the data
while(true)
{
uint8_t a_byte;
if (!tryRxByte(&a_byte))
return;
switch (state){
case STATE_WAIT_FOR_START:
if (a_byte == START_OF_FRAME){
state = STATE_READ_VERBATIM;
}
// otherwise, ignore data until we see a start byte
break;
case STATE_READ_VERBATIM:
if (a_byte == ESC){
state = STATE_READ_ESCAPED;
} else if (a_byte == END_OF_FRAME) {
state = STATE_END_OF_MESSAGE;
} else {
message[messageIndex++] = a_byte;
}
break;
case STATE_READ_ESCAPED:
message[messageIndex++] = a_byte ^ ESC_XOR;
state = STATE_READ_VERBATIM;
break;
default:
break;
}
if (state == STATE_END_OF_MESSAGE) {
uint16_t fletcher = (uint16_t)message[messageIndex - 2]
+ ((uint16_t)message[messageIndex - 1] << 8);
if(fletcher == fletcher16(message, messageIndex - 2)){
processCommand(message);
// we got a valid message! reset the stall timer
TMR3HLD = 0;
TMR2 = 0;
}
}
if (messageIndex >= RX_BUF_LEN || state == STATE_END_OF_MESSAGE) {
uint16_t i;
for(i=0; i<RX_BUF_LEN; i++) message[i] = 0;
messageIndex = 0;
state = STATE_WAIT_FOR_START;
return;
}
}
}
/// Construct a uint32 from the little endian bytes starting at data
uint32_t from_lendian_uint32(const void * data){
const unsigned char * bytes = data;
return ((uint32_t)bytes[0] << 0)
| ((uint32_t)bytes[1] << 8)
| ((uint32_t)bytes[2] << 16)
| ((uint32_t)bytes[3] << 24);
}
void processCommand(uint8_t* data){
uint16_t i;
/* length is the length of the data block only, not including the command */
uint8_t cmd = data[2];
uint32_t address;
uint16_t word;
uint32_t longWord;
uint32_t progData[MAX_PROG_SIZE + 1] = {0};
char strVersion[16] = VERSION_STRING;
char strPlatform[20] = PLATFORM_STRING;
switch(cmd){
case CMD_READ_PLATFORM:
txString(cmd, strPlatform);
break;
case CMD_READ_VERSION:
txString(cmd, strVersion);
break;
case CMD_READ_ROW_LEN:
word = _FLASH_ROW;
txArray16bit(cmd, &word, 1);
break;
case CMD_READ_PAGE_LEN:
word = _FLASH_PAGE;
txArray16bit(cmd, &word, 1);
break;
case CMD_READ_PROG_LEN:
longWord = __PROGRAM_LENGTH;
txArray32bit(cmd, &longWord, 1);
break;
case CMD_READ_MAX_PROG_SIZE:
word = MAX_PROG_SIZE;
txArray16bit(cmd, &word, 1);
break;
case CMD_READ_APP_START_ADDR:
word = APPLICATION_START_ADDRESS;
txArray16bit(cmd, &word, 1);
break;
case CMD_READ_BOOT_START_ADDR:
word = BOOTLOADER_START_ADDRESS;
txArray16bit(cmd, &word, 1);
break;
case CMD_ERASE_PAGE:
/* should correspond to a border */
address = from_lendian_uint32(data + 3);
/* do not allow the bootloader to be erased */
if((address >= BOOTLOADER_START_ADDRESS) && (address < APPLICATION_START_ADDRESS))
break;
eraseByAddress(address);
/* re-initialize the bootloader start address */
if(address == 0){
/* this is the GOTO BOOTLOADER instruction */
progData[0] = 0x040000 | BOOTLOADER_START_ADDRESS;
progData[1] = 0x000000;
/* write the data */
doubleWordWrite(address, progData);
}
break;
case CMD_READ_ADDR:
address = from_lendian_uint32(data + 3);
progData[0] = address;
progData[1] = readAddress(address);
txArray32bit(cmd, progData, 2);
break;
case CMD_READ_MAX:
address = from_lendian_uint32(data + 3);
progData[0] = address;
for(i=0; i<MAX_PROG_SIZE; i++){
progData[i+1] = readAddress(address + 2*i);
}
txArray32bit(cmd, progData, MAX_PROG_SIZE + 1);
break;
case CMD_WRITE_ROW:
address = from_lendian_uint32(data + 3);
/* do not allow the bootloader to be overwritten */
if((address >= BOOTLOADER_START_ADDRESS) && (address < APPLICATION_START_ADDRESS))
break;
for(i=0; i<_FLASH_ROW; i++){
progData[i] = from_lendian_uint32(data + 7 + i * 4);
}
/* do not allow the reset vector to be changed by the application */
if(address < __IVT_BASE)
break;
writeRow(address, progData);
break;
case CMD_WRITE_MAX_PROG_SIZE:
address = from_lendian_uint32(data + 3);
/* do not allow the bootloader to be overwritten */
if((address >= BOOTLOADER_START_ADDRESS) && (address < APPLICATION_START_ADDRESS))
break;
/* fill the progData array */
for(i=0; i<MAX_PROG_SIZE; i++){
progData[i] = from_lendian_uint32(data + 7 + i * 4);
}
/* the zero address should always go to the bootloader */
if(address == 0){
progData[0] = 0x040000 | BOOTLOADER_START_ADDRESS;
progData[1] = 0x000000;
}
/* write to flash memory, one row at a time */
for (i=0; i*_FLASH_ROW<MAX_PROG_SIZE; i++){
writeRow(address + i * _FLASH_ROW * 2, progData + i*_FLASH_ROW);
}
break;
case CMD_START_APP:
startApp(APPLICATION_START_ADDRESS);
break;
default:
break;
}
}
void txStart(void){
f16_sum1 = f16_sum2 = 0;
while(U1STAbits.UTXBF); /* wait for tx buffer to empty */
U1TXREG = START_OF_FRAME;
}
void txByte(uint8_t byte){
if((byte == START_OF_FRAME) || (byte == END_OF_FRAME) || (byte == ESC)){
while(U1STAbits.UTXBF); /* wait for tx buffer to empty */
U1TXREG = ESC; /* send escape character */
while(U1STAbits.UTXBF); /* wait */
U1TXREG = ESC_XOR ^ byte;
}else{
while(U1STAbits.UTXBF); /* wait */
U1TXREG = byte;
}
fletcher16Accum(byte);
}
void txEnd(void){
/* append checksum */
uint8_t sum1 = f16_sum1;
uint8_t sum2 = f16_sum2;
txByte(sum1);
txByte(sum2);
while(U1STAbits.UTXBF); /* wait for tx buffer to empty */
U1TXREG = END_OF_FRAME;
}
void txBytes(uint8_t cmd, uint8_t* bytes, uint16_t len){
uint16_t i;
txStart();
txByte((uint8_t)(len & 0x00ff));
txByte((uint8_t)((len & 0xff00) >> 8));
txByte(cmd);
for(i=0; i<len; i++){
txByte(bytes[i]);
}
txEnd();
}
void txArray16bit(uint8_t cmd, uint16_t* words, uint16_t len){
uint16_t length = len << 1;
txBytes(cmd, (uint8_t*) words, length);
}
void txArray32bit(uint8_t cmd, uint32_t* words, uint16_t len){
uint16_t length = len << 2;
txBytes(cmd, (uint8_t*) words, length);
}
void txString(uint8_t cmd, char* str){
uint16_t i, length = 0;
/* find the length of the version string */
while(str[length] != 0) length++;
length++; /* be sure to get the string terminator */
txStart();
/* begin transmitting */
txByte((uint8_t)(length & 0xff));
txByte((uint8_t)((length & 0xff00) >> 8));
txByte(cmd);
for(i=0; i<length; i++){
txByte((uint8_t)str[i]);
}
txEnd();
}
uint16_t fletcher16Accum(uint8_t byte){
f16_sum1 = (f16_sum1 + (uint16_t)byte) & 0xff;
f16_sum2 = (f16_sum2 + f16_sum1) & 0xff;
return (f16_sum2 << 8) | f16_sum1;
}
uint16_t fletcher16(uint8_t* data, uint16_t length){
uint16_t sum1 = 0, sum2 = 0, checksum;
uint16_t i = 0;
while(i < length){
sum1 = (sum1 + (uint16_t)data[i]) & 0xff;
sum2 = (sum2 + sum1) & 0xff;
i++;
}
checksum = (sum2 << 8) | sum1;
return checksum;
}