From 930d204e9fd85a614d690f7e694a7fd8f0c06aa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikael=20=C3=85gren?= Date: Fri, 28 Mar 2025 11:37:30 +0100 Subject: [PATCH] fw: Remove storage area address and size from partition table Instead we calculate start addresses from area index and use fixed size. --- hw/application_fpga/fw/tk1/partition_table.c | 6 -- hw/application_fpga/fw/tk1/partition_table.h | 4 -- hw/application_fpga/fw/tk1/storage.c | 66 ++++++++++++++----- .../tools/partition_table/partition_table.go | 2 - 4 files changed, 51 insertions(+), 27 deletions(-) diff --git a/hw/application_fpga/fw/tk1/partition_table.c b/hw/application_fpga/fw/tk1/partition_table.c index 1825cb1..afcc54d 100644 --- a/hw/application_fpga/fw/tk1/partition_table.c +++ b/hw/application_fpga/fw/tk1/partition_table.c @@ -26,12 +26,6 @@ int part_table_read(struct partition_table *part_table) part_table->header.version = PART_TABLE_VERSION; - for (int i = 0; i < 4; i++) { - part_table->app_storage[i].addr_start = - (ADDR_STORAGE_AREA + i * SIZE_STORAGE_AREA); - part_table->app_storage[i].size = SIZE_STORAGE_AREA; - } - part_table_write(part_table); } diff --git a/hw/application_fpga/fw/tk1/partition_table.h b/hw/application_fpga/fw/tk1/partition_table.h index 0399d92..1709d33 100644 --- a/hw/application_fpga/fw/tk1/partition_table.h +++ b/hw/application_fpga/fw/tk1/partition_table.h @@ -61,8 +61,6 @@ /* - 1 byte status. */ /* - 16 bytes random nonce. */ /* - 16 bytes authentication tag. */ -/* - 4 bytes physical start address. */ -/* - 4 bytes physical end address. */ struct auth_metadata { uint8_t nonce[16]; @@ -78,8 +76,6 @@ struct pre_loaded_app_metadata { struct app_storage_area { uint8_t status; struct auth_metadata auth; - uint32_t addr_start; - uint32_t size; } __attribute__((packed)); struct table_header { diff --git a/hw/application_fpga/fw/tk1/storage.c b/hw/application_fpga/fw/tk1/storage.c index 86f32dc..88c3ba8 100644 --- a/hw/application_fpga/fw/tk1/storage.c +++ b/hw/application_fpga/fw/tk1/storage.c @@ -25,6 +25,16 @@ static int get_first_empty(struct partition_table *part_table) return -1; } +static int index_to_address(int index, uint32_t *address) { + if ((index < 0) || (index >= N_STORAGE_AREA)) { + return -1; + } + + *address = ADDR_STORAGE_AREA + index * SIZE_STORAGE_AREA; + + return 0; +} + /* Returns the index of the area an app has allocated. If no area is * authenticated -1 is returned. */ static int storage_get_area(struct partition_table *part_table) @@ -55,14 +65,18 @@ int storage_allocate_area(struct partition_table *part_table) return -1; } + uint32_t start_address = 0; + int err = index_to_address(index, &start_address); + if (err) { + return -3; + } + /* Allocate the empty index found */ /* Erase area first */ /* Assumes the area is 64 KiB block aligned */ - flash_block_64_erase(part_table->app_storage[index] - .addr_start); // Erase first 64 KB block - flash_block_64_erase(part_table->app_storage[index].addr_start + - 0x10000); // Erase second 64 KB block + flash_block_64_erase(start_address); // Erase first 64 KB block + flash_block_64_erase(start_address + 0x10000); // Erase second 64 KB block /* Write partition table lastly */ part_table->app_storage[index].status = 0x01; @@ -83,13 +97,17 @@ int storage_deallocate_area(struct partition_table *part_table) return -1; } + uint32_t start_address = 0; + int err = index_to_address(index, &start_address); + if (err) { + return -3; + } + /* Erase area first */ /* Assumes the area is 64 KiB block aligned */ - flash_block_64_erase(part_table->app_storage[index] - .addr_start); // Erase first 64 KB block - flash_block_64_erase(part_table->app_storage[index].addr_start + - 0x10000); // Erase second 64 KB block + flash_block_64_erase(start_address); // Erase first 64 KB block + flash_block_64_erase(start_address + 0x10000); // Erase second 64 KB block /* Clear partition table lastly */ part_table->app_storage[index].status = 0; @@ -118,17 +136,23 @@ int storage_erase_sector(struct partition_table *part_table, uint32_t offset, return -1; } + uint32_t start_address = 0; + int err = index_to_address(index, &start_address); + if (err) { + return -3; + } + /* Cannot erase less than one sector */ - if (size < 4096 || size > part_table->app_storage[index].size || + if (size < 4096 || size > SIZE_STORAGE_AREA || size % 4096 != 0) { return -2; } - if ((offset) >= part_table->app_storage[index].size) { + if ((offset) >= SIZE_STORAGE_AREA) { return -2; } - uint32_t address = part_table->app_storage[index].addr_start + offset; + uint32_t address = start_address + offset; debug_puts("storage: erase addr: "); debug_putinthex(address); @@ -155,13 +179,19 @@ int storage_write_data(struct partition_table *part_table, uint32_t offset, return -1; } - if ((offset + size) > part_table->app_storage[index].size || + uint32_t start_address = 0; + int err = index_to_address(index, &start_address); + if (err) { + return -3; + } + + if ((offset + size) > SIZE_STORAGE_AREA || size > 4096) { /* Writing outside of area */ return -2; } - uint32_t address = part_table->app_storage[index].addr_start + offset; + uint32_t address = start_address + offset; debug_puts("storage: write to addr: "); debug_putinthex(address); @@ -182,12 +212,18 @@ int storage_read_data(struct partition_table *part_table, uint32_t offset, return -1; } - if ((offset + size) > part_table->app_storage[index].size) { + uint32_t start_address = 0; + int err = index_to_address(index, &start_address); + if (err) { + return -3; + } + + if ((offset + size) > SIZE_STORAGE_AREA) { /* Reading outside of area */ return -2; } - uint32_t address = part_table->app_storage[index].addr_start + offset; + uint32_t address = start_address + offset; debug_puts("storage: read from addr: "); debug_putinthex(address); diff --git a/hw/application_fpga/tools/partition_table/partition_table.go b/hw/application_fpga/tools/partition_table/partition_table.go index 93ea241..a34320e 100644 --- a/hw/application_fpga/tools/partition_table/partition_table.go +++ b/hw/application_fpga/tools/partition_table/partition_table.go @@ -24,8 +24,6 @@ type PartTable struct { Nonce [16]uint8 AuthDigest [16]uint8 } - AddrStart uint32 - Size uint32 } }