From d85d60db72f50e626df0fd64ffe8342e212822b5 Mon Sep 17 00:00:00 2001 From: Michael Cardell Widerkrantz Date: Fri, 4 Apr 2025 14:32:15 +0200 Subject: [PATCH] fw: Implement redundancy of partition table --- hw/application_fpga/fw/tk1/partition_table.c | 36 +++++++++++++------- hw/application_fpga/fw/tk1/partition_table.h | 6 ++-- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/hw/application_fpga/fw/tk1/partition_table.c b/hw/application_fpga/fw/tk1/partition_table.c index da2c9b8..594d1a7 100644 --- a/hw/application_fpga/fw/tk1/partition_table.c +++ b/hw/application_fpga/fw/tk1/partition_table.c @@ -26,30 +26,42 @@ void part_digest(struct partition_table *part_table, uint8_t *out_digest, size_t int part_table_read(struct partition_table_storage *storage) { + uint32_t offset[2] = { + ADDR_PARTITION_TABLE_0, + ADDR_PARTITION_TABLE_1, + }; + uint8_t check_digest[PART_DIGEST_SIZE]; + flash_release_powerdown(); memset(storage, 0x00, sizeof(*storage)); - flash_read_data(ADDR_PARTITION_TABLE, (uint8_t *)storage, + for (int i = 0; i < 2; i ++) { + flash_read_data(offset[i], (uint8_t *)storage, sizeof(*storage)); + part_digest(&storage->table, check_digest, sizeof(check_digest)); - // TODO: Implement redundancy - - uint8_t check_digest[PART_DIGEST_SIZE]; - part_digest(&storage->table, check_digest, sizeof(check_digest)); - - if (!memeq(check_digest, storage->check_digest, sizeof(check_digest))) { - return -1; + if (memeq(check_digest, storage->check_digest, sizeof(check_digest))) { + return 0; + } } - return 0; + return -1; } int part_table_write(struct partition_table_storage *storage) { + uint32_t offset[2] = { + ADDR_PARTITION_TABLE_0, + ADDR_PARTITION_TABLE_1, + }; + part_digest(&storage->table, storage->check_digest, sizeof(storage->check_digest)); - flash_sector_erase(ADDR_PARTITION_TABLE); - flash_write_data(ADDR_PARTITION_TABLE, (uint8_t *)storage, - sizeof(*storage)); + + for (int i = 0; i < 2; i ++) { + flash_sector_erase(offset[i]); + flash_write_data(offset[i], (uint8_t *)storage, + sizeof(*storage)); + } return 0; } diff --git a/hw/application_fpga/fw/tk1/partition_table.h b/hw/application_fpga/fw/tk1/partition_table.h index e570fac..2948a31 100644 --- a/hw/application_fpga/fw/tk1/partition_table.h +++ b/hw/application_fpga/fw/tk1/partition_table.h @@ -21,6 +21,7 @@ /* storage 3 128KiB 0xB0000 */ /* storage 4 128KiB 0xD0000 */ /* ---- ---- ---- */ +/* Partition2 64KiB 0xf0000 */ /* To simplify all blocks are aligned with the 64KiB blocks on the W25Q80DL * flash. */ @@ -30,13 +31,14 @@ #define ADDR_BITSTREAM 0UL #define SIZE_BITSTREAM 0x20000UL // 128KiB -#define ADDR_PARTITION_TABLE (ADDR_BITSTREAM + SIZE_BITSTREAM) +#define ADDR_PARTITION_TABLE_0 (ADDR_BITSTREAM + SIZE_BITSTREAM) +#define ADDR_PARTITION_TABLE_1 0xf0000 #define SIZE_PARTITION_TABLE \ 0x10000UL // 64KiB, 60 KiB reserved, 2 flash pages (2 x 4KiB) for the // partition table #define N_PRELOADED_APP 2 -#define ADDR_PRE_LOADED_APP_0 (ADDR_PARTITION_TABLE + SIZE_PARTITION_TABLE) +#define ADDR_PRE_LOADED_APP_0 (ADDR_PARTITION_TABLE_0 + SIZE_PARTITION_TABLE) #define SIZE_PRE_LOADED_APP 0x20000UL // 128KiB #define ADDR_STORAGE_AREA (ADDR_PRE_LOADED_APP_0 + (N_PRELOADED_APP * SIZE_PRE_LOADED_APP))