Skip to content

Commit e72779d

Browse files
sandeepmistryfacchinm
authored andcommitted
Store new boot loader in flash (const), only update user row if needed, increase flash chunk size
1 parent 7eb793a commit e72779d

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed
Binary file not shown.

libraries/SAMD_BootloaderUpdater/src/SAMD_BootloaderUpdater.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#define ROW_SIZE (PAGE_SIZE * 4)
2626

2727
__attribute__((aligned (4)))
28-
uint8_t booloaderData[8192] = {
28+
const uint8_t booloaderData[8192] = {
2929
#if defined(ARDUINO_SAMD_MKRVIDOR4000)
3030
#include "bootloaders/mkrvidor4000.h"
3131
#else
@@ -81,26 +81,32 @@ int SAMD_BootloaderUpdaterClass::update(void(*progressCallback)(float))
8181
// enable auto page writes
8282
NVMCTRL->CTRLB.bit.MANW = 0;
8383

84-
// copy the user row, and set the BOOTPROT size to 0
84+
// read the user row
8585
uint32_t userRow[PAGE_SIZE / sizeof(uint32_t)];
8686
readFlash(USER_ROW_START, userRow, sizeof(userRow));
87-
userRow[0] |= 0x00000007;
8887

89-
// erase the user row and flash the update value
90-
eraseFlash(USER_ROW_START, sizeof(userRow));
91-
writeFlash(USER_ROW_START, userRow, sizeof(userRow));
88+
if ((userRow[0] & 0x00000007) != 0x00000007) {
89+
// bootloader is protected, unprotect it
90+
userRow[0] |= 0x00000007;
91+
92+
// erase the user row and flash the updated value
93+
eraseFlash(USER_ROW_START, sizeof(userRow));
94+
writeFlash(USER_ROW_START, userRow, sizeof(userRow));
95+
}
9296

9397
if (progressCallback) {
9498
progressCallback(0.0);
9599
}
96100

101+
#define CHUNK_SIZE (ROW_SIZE * 2)
102+
97103
// erase and copy the flash row by row
98-
for (size_t i = 0; i < sizeof(booloaderData); i += ROW_SIZE) {
99-
eraseFlash(BOOTLOADER_START + i, ROW_SIZE);
100-
writeFlash(BOOTLOADER_START + i, &booloaderData[i], ROW_SIZE);
104+
for (size_t i = 0; i < sizeof(booloaderData); i += CHUNK_SIZE) {
105+
eraseFlash(BOOTLOADER_START + i, CHUNK_SIZE);
106+
writeFlash(BOOTLOADER_START + i, &booloaderData[i], CHUNK_SIZE);
101107

102108
if (progressCallback) {
103-
progressCallback((i + ROW_SIZE) * 100.0 / sizeof(booloaderData));
109+
progressCallback((i + CHUNK_SIZE) * 100.0 / sizeof(booloaderData));
104110
}
105111
}
106112

0 commit comments

Comments
 (0)