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 <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2022-05-15 18:44:09 -04:00 committed by Eric Callahan
parent e8374ce367
commit 8316d2f0af
7 changed files with 26 additions and 29 deletions

View File

@ -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);

View File

@ -7,7 +7,7 @@
#include <string.h> // 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));
}

View File

@ -4,6 +4,7 @@
//
// This file may be distributed under the terms of the GNU GPLv3 license.
#include <string.h> // 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);

View File

@ -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);

View File

@ -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);

View File

@ -4,7 +4,6 @@
//
// This file may be distributed under the terms of the GNU GPLv3 license.
#include <string.h> // 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);
}

View File

@ -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