-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootloader.c
388 lines (337 loc) · 9.78 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
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
/**
* @file bootloader.c
* @brief functions about bootloader implementation
*/
#include <stdbool.h>
#include <string.h>
#include "common.h"
#include "crc.h"
#include "flash.h"
#include "usart.h"
#define BL_VERSION "1.0"
#define BL_ACK 0
#define BL_NACK 1
#define BL_CRC_SIZE 4
#define BL_BUFFER_SIZE 256
/* bootloader commands list
* (command, code, reply length)
*/
#define BL_CMD_LIST \
_(get_cmd, 0x00, 10) \
_(get_version, 0x01, 3) \
_(get_id, 0x02, 4) \
_(get_protect_level, 0x03, 1) \
_(read_mem, 0x11, 1) \
_(jump_to_app, 0x21, 0) \
_(write_mem, 0x31, 1) \
_(erase_mem, 0x43, 1) \
_(write_protect, 0x63, 1) \
_(write_unprotect, 0x73, 1) \
_(read_protect, 0x82, 1) \
_(reload_opt_bytes, 0xA1, 0)
enum bl_cmd_code_list {
#define _(cmd, code, len) bl_##cmd##_cmd = code,
BL_CMD_LIST
#undef _
};
/* bootloader commands reply length */
enum bl_cmd_reply_list {
#define _(cmd, code, len) bl_##cmd##_len = len,
BL_CMD_LIST
#undef _
};
struct bl_command {
uint8_t code; /* command code */
uint8_t length; /* buffer length */
uint8_t buffer[BL_BUFFER_SIZE];
uint8_t crc[BL_CRC_SIZE]; /* CRC data */
};
/* write data to host */
static void bl_send_data(uint8_t *buffer, uint8_t len)
{
usart_send_data(buffer, len);
}
/* receive data from host */
static uint8_t bl_receive_data(void)
{
return usart_receive_data();
}
/* send ack to host */
static void bl_send_ack(uint8_t len)
{
uint8_t ack[2] = {BL_ACK, len};
bl_send_data(ack, 2);
}
/* send nack to host */
static void bl_send_nack(void)
{
uint8_t nack = BL_NACK;
bl_send_data(&nack, 1);
}
static bool bl_crc_verify(struct bl_command *command,
uint16_t len,
uint32_t crc)
{
uint32_t res;
uint8_t *ptr = (uint8_t *) command;
/* compute CRC value */
for (uint16_t i = 0; i < len; i++) {
res = crc_compute((uint32_t) ptr[i]);
}
/* reset CRC hardware */
crc_reset();
return res == crc;
}
/* handle BL_GET_CMD
* Get the version and the allowed commands supported by the current
* version of the protocol
*/
static void do_get_cmd(struct bl_command *command UNUSED)
{
/* get all command code */
uint8_t all_comand[] = {
#define _(cmd, code, len) bl_##cmd##_cmd,
BL_CMD_LIST
#undef _
};
bl_send_data((uint8_t *) all_comand, bl_get_cmd_len);
}
/* handle BL_GET_VERSION
* Get the protocol version
*/
static void do_get_version(struct bl_command *command UNUSED)
{
/* get bootloader version */
char *version = BL_VERSION;
bl_send_data((uint8_t *) version, bl_get_version_len);
}
/* handle BL_GET_ID
* Get the chip ID
*/
static void do_get_id(struct bl_command *command UNUSED)
{
/* send rev_id */
bl_send_data((uint8_t *) &DBGMCU, bl_get_id_len);
}
/* handle BL_GET_PROTECT_LEVEL
* Get the protection level status
*/
static void do_get_protect_level(struct bl_command *command UNUSED)
{
uint8_t protect_level = flash_get_protection_level();
bl_send_data(&protect_level, bl_get_protect_level_len);
}
/* handle BL_READ_MEM
* Read up to 256 bytes of memory starting from an address specified
* by the application
*/
static void do_read_mem(struct bl_command *command UNUSED)
{
/* decode buffer */
uint32_t base_addr = *(uint32_t *) command->buffer;
uint8_t len = command->buffer[4] + 1;
uint8_t buffer[len];
memcpy(buffer, (uint8_t *) base_addr, len);
/* check read data correct */
uint8_t res =
!strncmp((const char *) buffer, (const char *) base_addr, len);
bl_send_data(&res, bl_write_mem_len);
/* send data */
bl_send_data(buffer, len);
}
/* handle BL_JUMP_TO_APP
* Jump to user application code located in the internal flash memory or
* in the SRAM
*/
static void do_jump_to_app(struct bl_command *command UNUSED)
{
/* reset the peripherals that bootloader uses */
usart_reset();
crc_reset();
/* set MSP */
uint32_t msp = *(volatile uint32_t *) APP_BASE;
__asm volatile("MSR MSP, %0" ::"r"(msp));
/* set vector table offset */
VTOR = FLASH_BASE | VECT_TAB_OFF;
/* get application reset handler address */
uint32_t app_reset_addr = *(uint32_t *) (APP_BASE + 4);
/* jump to application */
void (*app_reset_handler)(void) = (void *) app_reset_addr;
app_reset_handler();
}
/* handle BL_WRITE_MEM
* Write up to 256 bytes to the RAM or flash memory starting from an
* address specified by the application
*/
static void do_write_mem(struct bl_command *command UNUSED)
{
/* decode buffer */
uint32_t base_addr = *(uint32_t *) command->buffer;
uint8_t bin_len = command->buffer[4];
uint8_t *bin_data = command->buffer + 5;
uint8_t res = 0;
/* check flash operation is on */
if (flash_is_on()) {
/* send write failed to host */
bl_send_data(&res, bl_erase_mem_len);
return;
}
/* unlock sequence */
flash_unlock_sequence();
/* write flash */
res = flash_write(base_addr, bin_data, bin_len);
/* enable lock */
flash_set_lock();
bl_send_data(&res, bl_write_mem_len);
}
/* handle BL_ERASE_MEM
* Erase from one to all the flash memory pages
*/
static void do_erase_mem(struct bl_command *command UNUSED)
{
/* page number */
uint8_t page = command->buffer[0];
/* the number of page to erase */
uint8_t page_num = command->buffer[1];
uint8_t res = 0;
/* check flash operation is on */
if (flash_is_on()) {
/* send erase failed to host */
bl_send_data(&res, bl_erase_mem_len);
return;
}
/* unlock sequence */
flash_unlock_sequence();
/* erase memory */
res = (page == 0xFF) ? flash_mass_erase() : flash_erase(page, page_num);
/* enable lock */
flash_set_lock();
bl_send_data(&res, bl_erase_mem_len);
}
/* handle BL_WRITE_PROTECT
* Enable the write protection for some sectors
*/
static void do_write_protect(struct bl_command *command UNUSED)
{
/* the number of page */
uint8_t page_num = command->buffer[0];
/* page number */
uint8_t *page = command->buffer + 1;
uint8_t res = 0;
/* check flash operation is on */
if (flash_is_on()) {
/* send erase failed to host */
bl_send_data(&res, bl_write_protect_len);
return;
}
/* unlock sequence */
flash_unlock_sequence();
/* option unlock sequence */
flash_opt_unlock_sequence();
/* write protection */
res = flash_write_protect(page, page_num);
/* enable lock */
flash_set_lock();
bl_send_data(&res, bl_write_protect_len);
}
/* handle BL_WRITE_UNPROTECT
* Disable the write protection for all flash memory sectors
*/
static void do_write_unprotect(struct bl_command *command UNUSED)
{
uint8_t res = 0;
/* check flash operation is on */
if (flash_is_on()) {
/* send erase failed to host */
bl_send_data(&res, bl_write_unprotect_len);
return;
}
/* unlock sequence */
flash_unlock_sequence();
/* option unlock sequence */
flash_opt_unlock_sequence();
/* write protection */
res = flash_write_unprotect();
/* enable lock */
flash_set_lock();
bl_send_data(&res, bl_write_unprotect_len);
}
/* handle BL_READ_PROTECT
* Enable the read protection
*/
static void do_read_protect(struct bl_command *command UNUSED)
{
/* read protection level */
uint8_t level = command->buffer[0];
uint8_t res = 0;
/* check flash operation is on */
if (flash_is_on()) {
/* send read protection failed to host */
bl_send_data(&res, bl_read_protect_len);
return;
}
/* unlock sequence */
flash_unlock_sequence();
/* option unlock sequence */
flash_opt_unlock_sequence();
/* write protection */
res = flash_read_protect(level);
/* enable lock */
flash_set_lock();
bl_send_data(&res, bl_write_unprotect_len);
}
/* handle BL_RELOAD_OPT_BYTES
* Reload option bytes
*/
static void do_reload_opt_bytes(struct bl_command *command UNUSED)
{
/* reload option bytes */
flash_reload_opt_bytes();
}
#define CMD_HANDLER(cmd, len) \
static void bl_##cmd(struct bl_command *command) \
{ \
uint32_t crc = *(uint32_t *) command->crc; \
/* CRC verify failed */ \
if (!bl_crc_verify(command, 2 + command->length, crc)) { \
/* send nack to host */ \
bl_send_nack(); \
return; \
} \
/* send ack */ \
bl_send_ack(len); \
/* execute command */ \
do_##cmd(command); \
}
/* command handler */
#define _(cmd, code, len) CMD_HANDLER(cmd, len)
BL_CMD_LIST
#undef _
/* read command from USART */
void bl_read_command(void)
{
struct bl_command command;
/* read command code */
command.code = bl_receive_data();
/* read command buffer length */
command.length = bl_receive_data();
/* read buffer data */
for (uint8_t i = 0; i < command.length; i++) {
command.buffer[i] = bl_receive_data();
}
/* read CRC data */
for (uint8_t i = 0; i < BL_CRC_SIZE; i++) {
command.crc[i] = bl_receive_data();
}
/* handle bootloader commands */
switch (command.code) {
#define _(cmd, code, len) \
case bl_##cmd##_cmd: \
bl_##cmd(&command); \
break;
BL_CMD_LIST
#undef _
default:
break;
}
}