Skip to content

Commit 8cc300b

Browse files
committed
support upgrade boardloader.
1 parent a736e52 commit 8cc300b

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

core/embed/bootloader/main.c

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,83 @@ static void check_bootloader_version(void) {
637637

638638
#endif
639639

640+
#if !PRODUCTION
641+
642+
void check_board_update(void) {
643+
char boardloader_path[] = "0:updates/boardloader.bin";
644+
char boardloader_path_legacy[] = "0:boot/boardloader.bin";
645+
char* boardloader_path_p = NULL;
646+
647+
uint8_t* boardloader_buf = (uint8_t*)0x24000000;
648+
649+
// check file exists
650+
if (emmc_fs_path_exist(boardloader_path)) {
651+
boardloader_path_p = boardloader_path;
652+
} else if (emmc_fs_path_exist(boardloader_path_legacy)) {
653+
boardloader_path_p = boardloader_path_legacy;
654+
}
655+
if (boardloader_path_p == NULL) return;
656+
657+
// check file size
658+
EMMC_PATH_INFO file_info;
659+
if (!emmc_fs_path_info(boardloader_path_p, &file_info)) return;
660+
if (file_info.size != BOARDLOADER_SIZE) return;
661+
662+
// read file to buffer
663+
uint32_t num_of_read = 0;
664+
if (!emmc_fs_file_read(boardloader_path_p, 0, boardloader_buf, file_info.size,
665+
&num_of_read))
666+
return;
667+
668+
display_printf("update boardloader\n");
669+
display_printf("\rErasing: ");
670+
if (flash_erase(FLASH_SECTOR_BOARDLOADER) != sectrue) {
671+
display_printf(" fail\n");
672+
return;
673+
}
674+
display_printf(" done\n");
675+
676+
display_printf("\rPreparing Write: ");
677+
if (flash_unlock_write() != sectrue) {
678+
display_printf(" fail\n");
679+
return;
680+
}
681+
682+
display_printf("\rWriting: ");
683+
size_t processed_bytes = 0;
684+
uint16_t last_progress = 0;
685+
uint16_t current_progress = 0;
686+
for (size_t sector_offset = 0; sector_offset < BOARDLOADER_SIZE;
687+
sector_offset += 32) {
688+
if (flash_write_words(FLASH_SECTOR_BOARDLOADER, sector_offset,
689+
(uint32_t*)(boardloader_buf + processed_bytes)) !=
690+
sectrue) {
691+
display_printf(" fail\n");
692+
return;
693+
}
694+
processed_bytes += 32;
695+
current_progress = (uint16_t)(processed_bytes * 100 / BOARDLOADER_SIZE);
696+
if (last_progress != current_progress) {
697+
display_printf("\rWriting: %u%%", current_progress);
698+
last_progress = current_progress;
699+
hal_delay(100);
700+
}
701+
}
702+
display_printf(" done\n");
703+
704+
display_printf("\rFinishing Write: ");
705+
if (flash_lock_write() != sectrue) {
706+
display_printf(" fail\n");
707+
return;
708+
}
709+
display_printf(" done\n");
710+
emmc_fs_file_delete(boardloader_path_p);
711+
hal_delay(1000);
712+
restart();
713+
}
714+
715+
#endif
716+
640717
static bool enter_boot_forced(void) {
641718
return *BOOT_TARGET_FLAG_ADDR == BOOT_TARGET_BOOTLOADER;
642719
}
@@ -797,6 +874,12 @@ int main(void) {
797874
// check bootloader downgrade
798875
check_bootloader_version();
799876

877+
#endif
878+
879+
#if !PRODUCTION
880+
881+
check_board_update();
882+
800883
#endif
801884

802885
if (!enter_boot_forced()) {

0 commit comments

Comments
 (0)