From 0692dddbae23b55b4d85a6040a283406d4a8a358 Mon Sep 17 00:00:00 2001 From: Michael Cardell Widerkrantz Date: Mon, 28 Apr 2025 16:38:41 +0200 Subject: [PATCH] fw: Simplify error return codes Since callees doesn't differentiate between different errors, we have no list of what different error codes mean, just return -1 on all errors. --- hw/application_fpga/fw/tk1/preload_app.c | 44 +++++++-------- hw/application_fpga/fw/tk1/storage.c | 68 +++++++++++++----------- 2 files changed, 60 insertions(+), 52 deletions(-) diff --git a/hw/application_fpga/fw/tk1/preload_app.c b/hw/application_fpga/fw/tk1/preload_app.c index 95020b2..4f40f99 100644 --- a/hw/application_fpga/fw/tk1/preload_app.c +++ b/hw/application_fpga/fw/tk1/preload_app.c @@ -22,11 +22,11 @@ static uint32_t slot_to_start_address(uint8_t slot) int preload_load(struct partition_table *part_table, uint8_t from_slot) { if (part_table == NULL) { - return -5; + return -1; } if (from_slot >= N_PRELOADED_APP) { - return -4; + return -1; } /* Check for a valid app in flash */ @@ -51,16 +51,16 @@ int preload_store(struct partition_table *part_table, uint32_t offset, uint8_t *data, size_t size, uint8_t to_slot) { if (part_table == NULL || data == NULL) { - return -5; + return -1; } if (to_slot >= N_PRELOADED_APP) { - return -4; + return -1; } /* Check if we are allowed to store */ if (!mgmt_app_authenticate()) { - return -3; + return -1; } /* Check for a valid app in flash, bale out if it already exists */ @@ -69,16 +69,16 @@ int preload_store(struct partition_table *part_table, uint32_t offset, } if (offset > SIZE_PRE_LOADED_APP) { - return -2; + return -1; } if (size > 4096) { - return -2; + return -1; } if ((offset + size) > SIZE_PRE_LOADED_APP) { /* Writing outside of area */ - return -2; + return -1; } uint32_t address = slot_to_start_address(to_slot) + offset; @@ -97,27 +97,27 @@ int preload_store_finalize(struct partition_table_storage *part_table_storage, struct partition_table *part_table = &part_table_storage->table; if (part_table == NULL) { - return -5; + return -1; } // Allow data to point only to app RAM if (app_digest < (uint8_t *)TK1_RAM_BASE || app_digest >= (uint8_t *)(TK1_RAM_BASE + TK1_RAM_SIZE)) { - return -5; + return -1; } if (app_signature < (uint8_t *)TK1_RAM_BASE || app_signature >= (uint8_t *)(TK1_RAM_BASE + TK1_RAM_SIZE)) { - return -5; + return -1; } if (to_slot >= N_PRELOADED_APP) { - return -4; + return -1; } /* Check if we are allowed to store */ if (!mgmt_app_authenticate()) { - return -3; + return -1; } /* Check for a valid app in flash, bale out if it already exists */ @@ -126,7 +126,7 @@ int preload_store_finalize(struct partition_table_storage *part_table_storage, } if (app_size == 0 || app_size > SIZE_PRE_LOADED_APP) { - return -2; + return -1; } part_table->pre_app_data[to_slot].size = app_size; @@ -141,7 +141,7 @@ int preload_store_finalize(struct partition_table_storage *part_table_storage, debug_lf(); if (part_table_write(part_table_storage) != 0) { - return -6; + return -1; } return 0; @@ -153,16 +153,16 @@ int preload_delete(struct partition_table_storage *part_table_storage, struct partition_table *part_table = &part_table_storage->table; if (part_table_storage == NULL) { - return -5; + return -1; } if (slot >= N_PRELOADED_APP) { - return -4; + return -1; } /* Check if we are allowed to deleted */ if (!mgmt_app_authenticate()) { - return -3; + return -1; } /*Check for a valid app in flash */ @@ -180,7 +180,7 @@ int preload_delete(struct partition_table_storage *part_table_storage, sizeof(part_table->pre_app_data[slot].signature)); if (part_table_write(part_table_storage) != 0) { - return -6; + return -1; } /* Assumes the area is 64 KiB block aligned */ @@ -197,16 +197,16 @@ int preload_get_digsig(struct partition_table *part_table, uint8_t slot) { if (part_table == NULL || app_digest == NULL || app_signature == NULL) { - return -5; + return -1; } if (slot >= N_PRELOADED_APP) { - return -4; + return -1; } /* Check if we are allowed to read */ if (!mgmt_app_authenticate()) { - return -3; + return -1; } memcpy_s(app_digest, 32, part_table->pre_app_data[slot].digest, diff --git a/hw/application_fpga/fw/tk1/storage.c b/hw/application_fpga/fw/tk1/storage.c index 2b83c5f..e12503a 100644 --- a/hw/application_fpga/fw/tk1/storage.c +++ b/hw/application_fpga/fw/tk1/storage.c @@ -13,12 +13,15 @@ #include "partition_table.h" #include "storage.h" -/* Returns the index of the first empty area. If there is no empty area -1 is - * returned. */ +/* + * Returns the index of the first empty area. + * + * Returns -1 on errors. + */ static int get_first_empty(struct partition_table *part_table) { if (part_table == NULL) { - return -4; + return -1; } for (uint8_t i = 0; i < N_STORAGE_AREA; i++) { @@ -26,13 +29,14 @@ static int get_first_empty(struct partition_table *part_table) return i; } } + return -1; } static int index_to_address(int index, uint32_t *address) { if (address == NULL) { - return -4; + return -1; } if ((index < 0) || (index >= N_STORAGE_AREA)) { @@ -44,12 +48,15 @@ static int index_to_address(int index, uint32_t *address) return 0; } -/* Returns the index of the area an app has allocated. If no area is - * authenticated -1 is returned. */ +/* + * Returns the index of the area an app has allocated. + * + * Returns -1 on errors. + */ static int storage_get_area(struct partition_table *part_table) { if (part_table == NULL) { - return -4; + return -1; } for (uint8_t i = 0; i < N_STORAGE_AREA; i++) { @@ -60,6 +67,7 @@ static int storage_get_area(struct partition_table *part_table) } } } + return -1; } @@ -79,7 +87,7 @@ int storage_allocate_area(struct partition_table_storage *part_table_storage) } int index = get_first_empty(part_table); - if (index == -1) { + if (index < 0) { /* No empty slot */ return -1; } @@ -114,13 +122,13 @@ int storage_allocate_area(struct partition_table_storage *part_table_storage) int storage_deallocate_area(struct partition_table_storage *part_table_storage) { if (part_table_storage == NULL) { - return -4; + return -1; } struct partition_table *part_table = &part_table_storage->table; int index = storage_get_area(part_table); - if (index == -1) { + if (index < 0) { /* No area to deallocate */ return -1; } @@ -128,7 +136,7 @@ int storage_deallocate_area(struct partition_table_storage *part_table_storage) uint32_t start_address = 0; int err = index_to_address(index, &start_address); if (err) { - return -3; + return -1; } /* Erase area first */ @@ -149,7 +157,7 @@ int storage_deallocate_area(struct partition_table_storage *part_table_storage) sizeof(part_table->app_storage[index].auth.authentication_digest)); if (part_table_write(part_table_storage) != 0) { - return -5; + return -1; } return 0; @@ -162,7 +170,7 @@ int storage_erase_sector(struct partition_table *part_table, uint32_t offset, size_t size) { if (part_table == NULL) { - return -4; + return -1; } int index = storage_get_area(part_table); @@ -174,25 +182,25 @@ int storage_erase_sector(struct partition_table *part_table, uint32_t offset, uint32_t start_address = 0; int err = index_to_address(index, &start_address); if (err) { - return -3; + return -1; } if (offset > SIZE_STORAGE_AREA) { - return -2; + return -1; } /* Cannot only erase entire sectors */ if (offset % 4096 != 0) { - return -2; + return -1; } /* Cannot erase less than one sector */ if (size < 4096 || size > SIZE_STORAGE_AREA || size % 4096 != 0) { - return -2; + return -1; } if ((offset + size) >= SIZE_STORAGE_AREA) { - return -2; + return -1; } uint32_t address = start_address + offset; @@ -217,13 +225,13 @@ int storage_write_data(struct partition_table *part_table, uint32_t offset, uint8_t *data, size_t size) { if (part_table == NULL) { - return -4; + return -1; } // Allow data to point only to app RAM if (data < (uint8_t *)TK1_RAM_BASE || data >= (uint8_t *)(TK1_RAM_BASE + TK1_RAM_SIZE)) { - return -4; + return -1; } int index = storage_get_area(part_table); @@ -235,20 +243,20 @@ int storage_write_data(struct partition_table *part_table, uint32_t offset, uint32_t start_address = 0; int err = index_to_address(index, &start_address); if (err) { - return -3; + return -1; } if (offset > SIZE_STORAGE_AREA) { - return -2; + return -1; } if (size > 4096) { - return -2; + return -1; } if ((offset + size) > SIZE_STORAGE_AREA) { /* Writing outside of area */ - return -2; + return -1; } uint32_t address = start_address + offset; @@ -267,13 +275,13 @@ int storage_read_data(struct partition_table *part_table, uint32_t offset, uint8_t *data, size_t size) { if (part_table == NULL) { - return -4; + return -1; } // Allow data to point only to app RAM if (data < (uint8_t *)TK1_RAM_BASE || data >= (uint8_t *)(TK1_RAM_BASE + TK1_RAM_SIZE)) { - return -4; + return -1; } int index = storage_get_area(part_table); @@ -285,20 +293,20 @@ int storage_read_data(struct partition_table *part_table, uint32_t offset, uint32_t start_address = 0; int err = index_to_address(index, &start_address); if (err) { - return -3; + return -1; } if (offset > SIZE_STORAGE_AREA) { - return -2; + return -1; } if (size > 4096) { - return -2; + return -1; } if ((offset + size) > SIZE_STORAGE_AREA) { /* Reading outside of area */ - return -2; + return -1; } uint32_t address = start_address + offset;