mirror of
https://github.com/tillitis/tillitis-key1.git
synced 2025-04-20 07:16:00 -04:00
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:
parent
b837eec9e6
commit
930d204e9f
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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 {
|
||||
|
@ -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);
|
||||
|
@ -24,8 +24,6 @@ type PartTable struct {
|
||||
Nonce [16]uint8
|
||||
AuthDigest [16]uint8
|
||||
}
|
||||
AddrStart uint32
|
||||
Size uint32
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user