|
25 | 25 | #define ROW_SIZE (PAGE_SIZE * 4)
|
26 | 26 |
|
27 | 27 | __attribute__((aligned (4)))
|
28 |
| -uint8_t booloaderData[8192] = { |
| 28 | +const uint8_t booloaderData[8192] = { |
29 | 29 | #if defined(ARDUINO_SAMD_MKRVIDOR4000)
|
30 | 30 | #include "bootloaders/mkrvidor4000.h"
|
31 | 31 | #else
|
@@ -81,26 +81,32 @@ int SAMD_BootloaderUpdaterClass::update(void(*progressCallback)(float))
|
81 | 81 | // enable auto page writes
|
82 | 82 | NVMCTRL->CTRLB.bit.MANW = 0;
|
83 | 83 |
|
84 |
| - // copy the user row, and set the BOOTPROT size to 0 |
| 84 | + // read the user row |
85 | 85 | uint32_t userRow[PAGE_SIZE / sizeof(uint32_t)];
|
86 | 86 | readFlash(USER_ROW_START, userRow, sizeof(userRow));
|
87 |
| - userRow[0] |= 0x00000007; |
88 | 87 |
|
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 | + } |
92 | 96 |
|
93 | 97 | if (progressCallback) {
|
94 | 98 | progressCallback(0.0);
|
95 | 99 | }
|
96 | 100 |
|
| 101 | + #define CHUNK_SIZE (ROW_SIZE * 2) |
| 102 | + |
97 | 103 | // 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); |
101 | 107 |
|
102 | 108 | if (progressCallback) {
|
103 |
| - progressCallback((i + ROW_SIZE) * 100.0 / sizeof(booloaderData)); |
| 109 | + progressCallback((i + CHUNK_SIZE) * 100.0 / sizeof(booloaderData)); |
104 | 110 | }
|
105 | 111 | }
|
106 | 112 |
|
|
0 commit comments