Skip to content

Commit 91e34f5

Browse files
committed
Restore flash speedup via signature
1 parent 5704a06 commit 91e34f5

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

bootloaders/zero/sam_ba_monitor.c

+31-25
Original file line numberDiff line numberDiff line change
@@ -445,39 +445,45 @@ static void sam_ba_monitor_loop(void)
445445
if ((uint32_t)dst_addr == 0x40000) {
446446
jtagInit();
447447

448-
// TODO: TEMP
449-
jtagFlashEraseBlock(LAST_FLASH_PAGE);
450-
//
451-
452448
// content of the first flash page:
453-
// offset (32) : length(32) : sha256sum(256) : type (32) = 44 bytes
449+
// offset (32) : length(32) : sha256sum(256) : type (32) : force (32) = 48 bytes
454450
// for every section; check last sector of the flash to understand if reflash is needed
455-
externalFlashSignatures data[2];
456-
jtagFlashReadBlock(LAST_FLASH_PAGE, 88, (uint8_t*)data);
451+
externalFlashSignatures data[3];
452+
jtagFlashReadBlock(LAST_FLASH_PAGE, sizeof(data), (uint8_t*)data);
457453
externalFlashSignatures* newData = (externalFlashSignatures*)src_addr;
458-
for (int k=0; k<2; k++) {
459-
for (int j=0; j<2; j++) {
460-
if ((data[k].type == newData[j].type) || (data[k].type == 0xFFFFFFFF)) {
461-
if (newData[j].offset < offset) {
462-
offset = newData[j].offset;
463-
}
464-
for (int s=0; s<8; s++) {
465-
if (data[k].sha256sum[s] != newData[j].sha256sum[s]) {
466-
flashNeeded = true;
467-
break;
468-
}
469-
}
454+
for (int k=0; k<3; k++) {
455+
if (newData[k].force != 0) {
456+
offset = newData[k].offset;
457+
flashNeeded = true;
458+
break;
459+
}
460+
if ((data[k].type == newData[k].type) || (data[k].type == 0xFFFFFFFF)) {
461+
if (newData[k].offset < offset) {
462+
offset = newData[k].offset;
463+
}
464+
if (memcmp(data[k].sha256sum, newData[k].sha256sum, 32) != 0) {
465+
flashNeeded = true;
466+
break;
470467
}
471468
}
472469
}
473-
if (!flashNeeded) {
474-
goto end;
470+
471+
// merge old page and new page
472+
for (int k=0; k<3; k++) {
473+
if (newData[k].type != 0xFFFFFFFF) {
474+
memcpy(&data[k], &newData[k], sizeof(newData[k]));
475+
}
475476
}
476-
jtagFlashEraseBlock(LAST_FLASH_PAGE);
477+
478+
jtagFlashEraseBlock(SCRATCHPAD_FLASH_PAGE);
479+
// write first page to SCRATCHPAD_FLASH_PAGE (to allow correct verification)
477480
for (int j =0; j<size; ) {
478-
jtagFlashWriteBlock(LAST_FLASH_PAGE + j*4, 512, (uint32_t*)&src_addr[j]);
481+
jtagFlashWriteBlock(SCRATCHPAD_FLASH_PAGE + j*4, 512, (uint32_t*)&src_addr[j]);
479482
j += 128;
480483
}
484+
485+
// write real structure with checksums to LAST_FLASH_PAGE
486+
jtagFlashWriteBlock(LAST_FLASH_PAGE, sizeof(data), (uint32_t*)data);
481487
goto end;
482488
}
483489

@@ -551,14 +557,14 @@ static void sam_ba_monitor_loop(void)
551557
uint32_t i = 0;
552558

553559
#ifdef ENABLE_JTAG_LOAD
554-
uint8_t buf[4096];
560+
uint8_t buf[4096];
555561
#endif
556562

557563
#ifdef ENABLE_JTAG_LOAD
558564
if ((uint32_t)ptr_data == 0x40000) {
559565
data = (uint8_t*)buf;
560566
for (int j =0; j<size; ) {
561-
jtagFlashReadBlock(LAST_FLASH_PAGE + j, 512, &data[j]);
567+
jtagFlashReadBlock(SCRATCHPAD_FLASH_PAGE + j, 512, &data[j]);
562568
j += 512;
563569
}
564570
} else if ((uint32_t)ptr_data >= 0x41000) {

0 commit comments

Comments
 (0)