From 1100b159a9c9f3ffb7fa302a87ecafd5f069b121 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Fri, 23 Aug 2019 17:51:22 +0200 Subject: [PATCH] Refactor. Add debug code. Use %u for unsigned. Use volatile pointer instead of memory storage. --- targets/stm32l432/bootloader/bootloader.c | 30 ++++++++++++++--------- targets/stm32l432/src/memory_layout.h | 2 +- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/targets/stm32l432/bootloader/bootloader.c b/targets/stm32l432/bootloader/bootloader.c index c5b29ef..8a06983 100644 --- a/targets/stm32l432/bootloader/bootloader.c +++ b/targets/stm32l432/bootloader/bootloader.c @@ -110,26 +110,34 @@ int is_bootloader_disabled() #include "version.h" bool is_firmware_version_newer_or_equal() { - printf1(TAG_BOOT,"Current firmware version: %d.%d.%d.%d\r\n", + printf1(TAG_BOOT,"Current firmware version: %u.%u.%u.%u\r\n", current_firmware_version.major, current_firmware_version.minor, current_firmware_version.patch, current_firmware_version.reserved); - volatile version_t new_version = *((volatile version_t *) VERSION_ADDR); - printf1(TAG_BOOT,"Uploaded firmware version: %d.%d.%d.%d\r\n", - new_version.major, new_version.minor, new_version.patch, new_version.reserved); - dump_hex1(TAG_BOOT, (uint32_t *) VERSION_ADDR, 20); + volatile version_t * new_version = ((volatile version_t *) NEW_FW_VERSION_ADDR); + printf1(TAG_BOOT,"Uploaded firmware version: %u.%u.%u.%u\r\n", + new_version->major, new_version->minor, new_version->patch, new_version->reserved); + dump_hex1(TAG_BOOT, (uint8_t *) NEW_FW_VERSION_ADDR, 8); + dump_hex1(TAG_BOOT, (uint8_t *) NEW_FW_VERSION_ADDR+8, 8); - printf1(TAG_BOOT,"AUTH_WORD_ADDR: %p\r\n", AUTH_WORD_ADDR); - printf1(TAG_BOOT,"VERSION_ADDR: %p\r\n", VERSION_ADDR); printf1(TAG_BOOT,"APPLICATION_END_ADDR: %p\r\n", APPLICATION_END_ADDR); printf1(TAG_BOOT,"BOOT_VERSION_ADDR: %p\r\n", BOOT_VERSION_ADDR); printf1(TAG_BOOT,"BOOT_VERSION_PAGE: %d\r\n", BOOT_VERSION_PAGE); - const bool allowed = is_newer(&new_version, ¤t_firmware_version) || current_firmware_version.raw == 0xFFFFFFFF; + flash_memory_st * ptr = 0x08000000; + printf1(TAG_BOOT,"AUTH_WORD_ADDR: %p\r\n", AUTH_WORD_ADDR); + printf1(TAG_BOOT,"NEW_FW_VERSION_ADDR: %p\r\n", NEW_FW_VERSION_ADDR); + printf1(TAG_BOOT,"NEW_FW_VERSION_ADDR: %p\r\n", ptr->app_version ); + printf1(TAG_BOOT,"NEW_FW_VERSION_ADDR: %p\r\n", new_version ); + printf1(TAG_BOOT,"current firm add: %p\r\n", ¤t_firmware_version ); + printf1(TAG_BOOT," ptr->bootloader_data "); + dump_hex1(TAG_BOOT, (uint8_t *) ptr->bootloader_data, 8); + + const bool allowed = is_newer(new_version, ¤t_firmware_version) || current_firmware_version.raw == 0xFFFFFFFF; if (allowed){ printf1(TAG_BOOT, "Update allowed, setting new firmware version as current.\r\n"); // current_firmware_version.raw = new_version.raw; uint8_t page[PAGE_SIZE]; memmove(page, (uint8_t*)BOOT_VERSION_ADDR, PAGE_SIZE); - memmove(page, &new_version, 4); + memmove(page, new_version, 4); printf1(TAG_BOOT, "Writing\r\n"); flash_erase_page(BOOT_VERSION_PAGE); flash_write(BOOT_VERSION_ADDR, page, PAGE_SIZE); @@ -182,7 +190,7 @@ int bootloader_bridge(int klen, uint8_t * keyh) || ((uint32_t)ptr+len) > APPLICATION_END_ADDR) { printf1(TAG_BOOT,"Bound exceeded [%08lx, %08lx]\r\n",APPLICATION_START_ADDR,APPLICATION_END_ADDR); - printf1(TAG_BOOT, "Expected version addrs: %p, %p\r\n", BOOT_VERSION_ADDR, VERSION_ADDR); + printf1(TAG_BOOT, "Expected version addrs: %p, %p\r\n", BOOT_VERSION_ADDR, NEW_FW_VERSION_ADDR); return CTAP2_ERR_NOT_ALLOWED; } @@ -230,7 +238,7 @@ int bootloader_bridge(int klen, uint8_t * keyh) #endif if (!is_firmware_version_newer_or_equal()){ printf1(TAG_BOOT, "Firmware older - update not allowed.\r\n"); - dump_hex1(TAG_BOOT, (uint32_t *) VERSION_ADDR, 20); + dump_hex1(TAG_BOOT, (uint32_t *) NEW_FW_VERSION_ADDR, 20); printf1(TAG_BOOT, "Rebooting...\r\n"); REBOOT_FLAG = 1; return CTAP2_ERR_OPERATION_DENIED; diff --git a/targets/stm32l432/src/memory_layout.h b/targets/stm32l432/src/memory_layout.h index 41ff02c..7700dab 100644 --- a/targets/stm32l432/src/memory_layout.h +++ b/targets/stm32l432/src/memory_layout.h @@ -44,7 +44,7 @@ #define AUTH_WORD_ADDR (APPLICATION_END_ADDR) #define LAST_ADDR (APPLICATION_END_ADDR-2048 + 8) -#define VERSION_ADDR (AUTH_WORD_ADDR-8) +#define NEW_FW_VERSION_ADDR (AUTH_WORD_ADDR-8) #define BOOT_VERSION_PAGE (APPLICATION_END_PAGE) #define BOOT_VERSION_ADDR (0x08000000 + BOOT_VERSION_PAGE*FLASH_PAGE_SIZE) #define LAST_PAGE (APPLICATION_END_PAGE-1)