forked from ARMmbed/mbed-os-example-bootloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
73 lines (56 loc) · 1.67 KB
/
main.cpp
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
#include "mbed.h"
#include "SDBlockDevice.h"
#include "FATFileSystem.h"
SDBlockDevice sd(D11, D12, D13, D10);
FATFileSystem fs("sd");
FlashIAP flash;
void apply_update(FILE *file, uint32_t address);
int main()
{
sd.init();
fs.mount(&sd);
FILE *file = fopen(MBED_CONF_APP_UPDATE_FILE, "rb");
if (file != NULL) {
printf("Firmware update found\r\n");
apply_update(file, POST_APPLICATION_ADDR);
fclose(file);
remove(MBED_CONF_APP_UPDATE_FILE);
} else {
printf("No update found to apply\r\n");
}
fs.unmount();
sd.deinit();
printf("Starting application\r\n");
mbed_start_application(POST_APPLICATION_ADDR);
}
void apply_update(FILE *file, uint32_t address)
{
flash.init();
const uint32_t page_size = flash.get_page_size();
char *page_buffer = new char[page_size];
uint32_t addr = address;
uint32_t next_sector = addr + flash.get_sector_size(addr);
bool sector_erased = false;
while (true) {
// Read data for this page
memset(page_buffer, 0, sizeof(page_buffer));
int size_read = fread(page_buffer, 1, page_size, file);
if (size_read <= 0) {
break;
}
// Erase this page if it hasn't been erased
if (!sector_erased) {
flash.erase(addr, flash.get_sector_size(addr));
sector_erased = true;
}
// Program page
flash.program(page_buffer, addr, page_size);
addr += page_size;
if (addr >= next_sector) {
next_sector = addr + flash.get_sector_size(addr);
sector_erased = false;
}
}
delete[] page_buffer;
flash.deinit();
}