Refactor. Add debug code. Use %u for unsigned. Use volatile pointer instead of memory storage.

This commit is contained in:
Szczepan Zalega
2019-08-23 17:51:22 +02:00
parent 9ddba5dfc3
commit 1100b159a9
2 changed files with 20 additions and 12 deletions

View File

@ -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, &current_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", &current_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, &current_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;