Skip to content

Commit 8e03ab8

Browse files
committed
Skip app jump when firmware image is empty
Add a check for an empty user image by reading the reset vector word at `FLASH_BASE + OFFSET_UIMAGE + 4` and requiring it to be valid before branching to the application. This prevents jumping into unprogrammed flash. The startup log now also reports the empty-image state (`Empty=Y/N`) alongside remote/key status.
1 parent c75d558 commit 8e03ab8

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

bootloader-tftp/firmware/main.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141

4242
namespace board {
4343
void RebootHandler() {}
44-
} // namespace board
44+
} // namespace hal
4545

4646
int main() {
4747
rcu_periph_clock_enable(KEY_BOOTLOADER_TFTP_RCU_GPIOx);
@@ -66,8 +66,10 @@ int main() {
6666

6767
const auto kIsNotRemote = (bkp_data_read(BKP_DATA_1) != 0xA5A5);
6868
const auto kIsNotKey = (gpio_input_bit_get(KEY_BOOTLOADER_TFTP_GPIOx, KEY_BOOTLOADER_TFTP_GPIO_PINx));
69+
const uint32_t* reset_p = reinterpret_cast<uint32_t*>(FLASH_BASE + OFFSET_UIMAGE + 4);
70+
const auto kIsEmpty = (*reset_p == UINT32_MAX);
6971

70-
if (kIsNotRemote && kIsNotKey) {
72+
if (kIsNotRemote && kIsNotKey && !kIsEmpty) {
7173
// https://developer.arm.com/documentation/ka001423/1-0
7274
// 1. Disable interrupt response.
7375
__disable_irq();
@@ -97,7 +99,6 @@ int main() {
9799
// 7. Enable interrupts.
98100
__enable_irq();
99101
// 8. Call the reset handler
100-
const uint32_t* reset_p = reinterpret_cast<uint32_t*>(FLASH_BASE + OFFSET_UIMAGE + 4);
101102
asm volatile("bx %0;" : : "r"(*reset_p));
102103
}
103104

@@ -108,7 +109,7 @@ int main() {
108109
FirmwareVersion fw(kSoftwareVersion, __DATE__, __TIME__);
109110
FlashCodeInstall flashcode_install;
110111

111-
printf("Remote=%c, Key=%c\n", kIsNotRemote ? 'N' : 'Y', kIsNotKey ? 'N' : 'Y');
112+
printf("Remote=%c, Key=%c, Empty=%c\n", kIsNotRemote ? 'N' : 'Y', kIsNotKey ? 'N' : 'Y', kIsEmpty ? 'Y' : 'N');
112113
fw.Print("Bootloader TFTP Server");
113114

114115
RemoteConfig remote_config(remoteconfig::Output::CONFIG);

0 commit comments

Comments
 (0)