Skip to content

Commit

Permalink
fix: blupdater shouldn't set UpdateAfter until success
Browse files Browse the repository at this point in the history
  • Loading branch information
mrnerdhair committed Apr 19, 2022
1 parent da8b101 commit ee6b75d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion include/keepkey/board/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ extern uint8_t *emulator_flash_base;

#define SIG_FLAG (*(uint8_t const *)FLASH_SIG_FLAG)

#define META_FLAGS (*(uint8_t const *)FLASH_META_FLAGS)
#define META_FLAGS (*(uint32_t const *)FLASH_META_FLAGS)


/* Misc Info. */
Expand Down
2 changes: 1 addition & 1 deletion tools/blupdater/header.s
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ g_header:
.byte 0 // sigindex2
.byte 0 // sigindex3
.byte 1 // sig_flag: Preserve
.word 1 // meta_flags: UpdateAfter
.word 0 // meta_flags: UpdateAfter
. = . + 48 // reserved
. = . + 64 // sig1
. = . + 64 // sig2
Expand Down
33 changes: 30 additions & 3 deletions tools/blupdater/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,17 @@ static bool write_bootloader(void) {
flash_unlock();

// erase the bootloader sectors, do not use flash_erase_word()
layoutProgress("Updating. DO NOT UNPLUG", 0);
layoutProgress("Updating bootloader. DO NOT UNPLUG", 0);
flash_erase_sector(5, FLASH_CR_PROGRAM_X32);
flash_wait_for_last_operation();
layoutProgress("Updating. DO NOT UNPLUG", 100);
layoutProgress("Updating bootloader. DO NOT UNPLUG", 100);
flash_erase_sector(6, FLASH_CR_PROGRAM_X32);
flash_wait_for_last_operation();

// Write into the sector.
for (int chunkstart = 0; chunkstart < _binary_payload_bin_size;
chunkstart += CHUNK_SIZE) {
layoutProgress("Updating. DO NOT UNPLUG",
layoutProgress("Updating bootloader. DO NOT UNPLUG",
200 + chunkstart * 800 / _binary_payload_bin_size);

size_t chunksize;
Expand Down Expand Up @@ -162,6 +162,33 @@ static bool unknown_bootloader(void) {
/// \brief Success: everything went smoothly as expected, and the device has a
/// new bootloader installed.
static void success(void) {
for (int i = 0; i < NUM_RETRIES; ++i) {
// Enable writing to the read-only sectors
memory_unlock();
flash_unlock();

flash_program_word(FLASH_META_FLAGS, META_FLAGS | 1);
flash_wait_for_last_operation();

if ((META_FLAGS & 1) == 1) {
break;
}
}

// Disallow writing to flash.
flash_lock();

// Ignore any reported errors, we only care about the end result.
flash_clear_status_flags();

if ((META_FLAGS & 1) != 1) {
layout_standard_notification("Bootloader Update Complete",
"Please unplug your device.",
NOTIFICATION_CONFIRMED);
display_refresh();
shutdown();
}

layout_standard_notification("Bootloader Update Complete",
"Your device will now restart",
NOTIFICATION_CONFIRMED);
Expand Down

0 comments on commit ee6b75d

Please sign in to comment.