From 8316d2f0af8f944c4841ce825e261d24890fa9bb Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Sun, 15 May 2022 18:44:09 -0400 Subject: [PATCH] armcm_boot: Introduce application_check_valid() Move application_check_valid() in bootentry.c to armcm_boot.c and make the check specific to the ARM cortex-m. On these ARM machines it is easier to validate the application stack address. Also rename jump_to_application() to application_jump(). Also rename flash_read_block() to application_read_flash() and move to armcm_boot.c . Signed-off-by: Kevin O'Connor --- src/bootentry.c | 16 +--------------- src/flashcmd.c | 6 +++--- src/generic/armcm_boot.c | 19 ++++++++++++++++++- src/generic/misc.h | 4 +++- src/sched.c | 2 +- src/stm32/flash.c | 7 ------- src/stm32/flash.h | 1 - 7 files changed, 26 insertions(+), 29 deletions(-) diff --git a/src/bootentry.c b/src/bootentry.c index a3ef379..b715852 100644 --- a/src/bootentry.c +++ b/src/bootentry.c @@ -44,20 +44,6 @@ check_double_reset(void) // No reset, read the key back out to clear it } -static uint8_t -check_application_code(void) -{ - // Read the first block of memory, if it - // is all 0xFF then no application has been flashed - uint8_t buf[CONFIG_BLOCK_SIZE]; - flash_read_block(CONFIG_APPLICATION_START, (uint32_t*)buf); - for (uint8_t i = 0; i < CONFIG_BLOCK_SIZE; i++) { - if (buf[i] != 0xFF) - return 1; - } - return 0; -} - // Check if bootloader or application should be started int bootentry_check(void) @@ -66,7 +52,7 @@ bootentry_check(void) // - The request signature is set in memory (request from app) // - No application code is present uint64_t bootup_code = get_bootup_code(); - if (bootup_code == REQUEST_SIG || !check_application_code() + if (bootup_code == REQUEST_SIG || !application_check_valid() || check_button_pressed()) { // Start bootloader main loop set_bootup_code(0); diff --git a/src/flashcmd.c b/src/flashcmd.c index 11e85e0..fe9416e 100644 --- a/src/flashcmd.c +++ b/src/flashcmd.c @@ -7,7 +7,7 @@ #include // memmove #include "autoconf.h" // CONFIG_BLOCK_SIZE #include "board/flash.h" // flash_write_page -#include "board/misc.h" // jump_to_application +#include "board/misc.h" // application_jump #include "byteorder.h" // cpu_to_le32 #include "command.h" // command_respond_ack #include "flashcmd.h" // flashcmd_is_in_transfer @@ -48,7 +48,7 @@ void complete_task(void) { if (complete && timer_is_before(complete_endtime, timer_read_time())) - jump_to_application(); + application_jump(); } DECL_TASK(complete_task); @@ -85,7 +85,7 @@ command_read_block(uint32_t *data) uint32_t block_address = le32_to_cpu(data[1]); uint32_t out[CONFIG_BLOCK_SIZE / 4 + 2 + 2]; out[2] = cpu_to_le32(block_address); - flash_read_block(block_address, &out[3]); + application_read_flash(block_address, &out[3]); command_respond_ack(CMD_REQ_BLOCK, out, ARRAY_SIZE(out)); } diff --git a/src/generic/armcm_boot.c b/src/generic/armcm_boot.c index 954e1b0..06537db 100644 --- a/src/generic/armcm_boot.c +++ b/src/generic/armcm_boot.c @@ -4,6 +4,7 @@ // // This file may be distributed under the terms of the GNU GPLv3 license. +#include // memcpy #include "armcm_boot.h" // DECL_ARMCM_IRQ #include "autoconf.h" // CONFIG_MCU #include "board/internal.h" // SysTick @@ -33,10 +34,26 @@ set_bootup_code(uint64_t code) barrier(); } +// Helper function to read area of flash +void +application_read_flash(uint32_t address, uint32_t *dest) +{ + memcpy(dest, (void*)address, CONFIG_BLOCK_SIZE); +} + +// Check if the application flash area looks valid +int +application_check_valid(void) +{ + uint32_t *app = (void*)CONFIG_APPLICATION_START; + return *app != 0 && *app != 0xffffffff; +} + #define REQUEST_START_APP 0x7b06ec45a9a8243d +// Jump to the main application (exiting the bootloader) void -jump_to_application(void) +application_jump(void) { irq_disable(); set_bootup_code(REQUEST_START_APP); diff --git a/src/generic/misc.h b/src/generic/misc.h index a7aa628..2cedb56 100644 --- a/src/generic/misc.h +++ b/src/generic/misc.h @@ -10,7 +10,9 @@ void *console_receive_buffer(void); uint64_t get_bootup_code(void); void set_bootup_code(uint64_t code); -void jump_to_application(void); +void application_read_flash(uint32_t address, uint32_t *dest); +int application_check_valid(void); +void application_jump(void); void timer_setup(void); diff --git a/src/sched.c b/src/sched.c index 403532f..f853b35 100644 --- a/src/sched.c +++ b/src/sched.c @@ -47,7 +47,7 @@ sched_main(void) { timer_setup(); if (!bootentry_check()) - jump_to_application(); + application_jump(); // Run all init functions marked with DECL_INIT() extern void ctr_run_initfuncs(void); diff --git a/src/stm32/flash.c b/src/stm32/flash.c index 12bd269..91dbe8f 100644 --- a/src/stm32/flash.c +++ b/src/stm32/flash.c @@ -4,7 +4,6 @@ // // This file may be distributed under the terms of the GNU GPLv3 license. -#include // memcpy #include "autoconf.h" // CONFIG_MACH_STM32F103 #include "flash.h" // flash_write_page #include "internal.h" // FLASH @@ -141,9 +140,3 @@ flash_write_page(uint32_t page_address, void *data) flash_write_stm32f1xx(page_address, (uint16_t *)data); } } - -void -flash_read_block(uint32_t block_address, uint32_t *buffer) -{ - memcpy(buffer, (void*)block_address, CONFIG_BLOCK_SIZE); -} diff --git a/src/stm32/flash.h b/src/stm32/flash.h index d2b3882..e7c8f5f 100644 --- a/src/stm32/flash.h +++ b/src/stm32/flash.h @@ -6,6 +6,5 @@ uint32_t flash_get_page_size(void); void flash_complete(void); void flash_write_page(uint32_t page_address, void *data); -void flash_read_block(uint32_t block_address, uint32_t *buffer); #endif