fw: Remove storage area address and size from partition table

Instead we calculate start addresses from area index and use fixed size.
This commit is contained in:
Mikael Ågren 2025-03-28 11:37:30 +01:00
parent b837eec9e6
commit 930d204e9f
No known key found for this signature in database
GPG Key ID: E02DA3D397792C46
4 changed files with 51 additions and 27 deletions

View File

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

View File

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

View File

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

View File

@ -24,8 +24,6 @@ type PartTable struct {
Nonce [16]uint8
AuthDigest [16]uint8
}
AddrStart uint32
Size uint32
}
}