diff --git a/hw/application_fpga/fw/tk1/partition_table.c b/hw/application_fpga/fw/tk1/partition_table.c index a75b2d9..0f35d62 100644 --- a/hw/application_fpga/fw/tk1/partition_table.c +++ b/hw/application_fpga/fw/tk1/partition_table.c @@ -17,11 +17,13 @@ enum part_status part_get_status(void) return part_status; } -static void part_digest(struct partition_table *part_table, uint8_t *out_digest, - size_t out_len); +static void part_checksum(struct partition_table *part_table, + uint8_t *out_digest, size_t out_len); -static void part_digest(struct partition_table *part_table, uint8_t *out_digest, - size_t out_len) +// part_digest computes a checksum over the partition table to detect +// flash problems +static void part_checksum(struct partition_table *part_table, + uint8_t *out_digest, size_t out_len) { int blake2err = 0; @@ -50,7 +52,7 @@ int part_table_read(struct partition_table_storage *storage) ADDR_PARTITION_TABLE_0, ADDR_PARTITION_TABLE_1, }; - uint8_t check_digest[PART_DIGEST_SIZE] = {0}; + uint8_t check_digest[PART_CHECKSUM_SIZE] = {0}; if (storage == NULL) { return -1; @@ -64,10 +66,10 @@ int part_table_read(struct partition_table_storage *storage) sizeof(*storage)) != 0) { return -1; } - part_digest(&storage->table, check_digest, - sizeof(check_digest)); + part_checksum(&storage->table, check_digest, + sizeof(check_digest)); - if (memeq(check_digest, storage->check_digest, + if (memeq(check_digest, storage->checksum, sizeof(check_digest))) { if (i == 1) { part_status = PART_SLOT0_INVALID; @@ -91,8 +93,8 @@ int part_table_write(struct partition_table_storage *storage) return -1; } - part_digest(&storage->table, storage->check_digest, - sizeof(storage->check_digest)); + part_checksum(&storage->table, storage->checksum, + sizeof(storage->checksum)); for (int i = 0; i < 2; i++) { flash_sector_erase(offset[i]); diff --git a/hw/application_fpga/fw/tk1/partition_table.h b/hw/application_fpga/fw/tk1/partition_table.h index dd4fa84..04ca116 100644 --- a/hw/application_fpga/fw/tk1/partition_table.h +++ b/hw/application_fpga/fw/tk1/partition_table.h @@ -46,7 +46,7 @@ #define SIZE_STORAGE_AREA 0x20000UL // 128KiB #define N_STORAGE_AREA 4 -#define PART_DIGEST_SIZE 16 +#define PART_CHECKSUM_SIZE 16 enum part_status { PART_SLOT0_INVALID = 1, @@ -70,6 +70,8 @@ enum part_status { /* - 1 byte status. */ /* - 16 bytes random nonce. */ /* - 16 bytes authentication tag. */ +/**/ +/*- Checksum over the above */ struct auth_metadata { uint8_t nonce[16]; @@ -99,7 +101,7 @@ struct partition_table { struct partition_table_storage { struct partition_table table; - uint8_t check_digest[PART_DIGEST_SIZE]; + uint8_t checksum[PART_CHECKSUM_SIZE]; // Helps detect flash problems } __attribute__((packed)); enum part_status part_get_status(void);