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.
This commit is contained in:
Michael Cardell Widerkrantz 2025-04-28 16:38:41 +02:00
parent 15a350da1e
commit 0692dddbae
No known key found for this signature in database
GPG key ID: D3DB3DDF57E704E5
2 changed files with 60 additions and 52 deletions

View file

@ -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) int preload_load(struct partition_table *part_table, uint8_t from_slot)
{ {
if (part_table == NULL) { if (part_table == NULL) {
return -5; return -1;
} }
if (from_slot >= N_PRELOADED_APP) { if (from_slot >= N_PRELOADED_APP) {
return -4; return -1;
} }
/* Check for a valid app in flash */ /* 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) uint8_t *data, size_t size, uint8_t to_slot)
{ {
if (part_table == NULL || data == NULL) { if (part_table == NULL || data == NULL) {
return -5; return -1;
} }
if (to_slot >= N_PRELOADED_APP) { if (to_slot >= N_PRELOADED_APP) {
return -4; return -1;
} }
/* Check if we are allowed to store */ /* Check if we are allowed to store */
if (!mgmt_app_authenticate()) { if (!mgmt_app_authenticate()) {
return -3; return -1;
} }
/* Check for a valid app in flash, bale out if it already exists */ /* 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) { if (offset > SIZE_PRE_LOADED_APP) {
return -2; return -1;
} }
if (size > 4096) { if (size > 4096) {
return -2; return -1;
} }
if ((offset + size) > SIZE_PRE_LOADED_APP) { if ((offset + size) > SIZE_PRE_LOADED_APP) {
/* Writing outside of area */ /* Writing outside of area */
return -2; return -1;
} }
uint32_t address = slot_to_start_address(to_slot) + offset; 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; struct partition_table *part_table = &part_table_storage->table;
if (part_table == NULL) { if (part_table == NULL) {
return -5; return -1;
} }
// Allow data to point only to app RAM // Allow data to point only to app RAM
if (app_digest < (uint8_t *)TK1_RAM_BASE || if (app_digest < (uint8_t *)TK1_RAM_BASE ||
app_digest >= (uint8_t *)(TK1_RAM_BASE + TK1_RAM_SIZE)) { app_digest >= (uint8_t *)(TK1_RAM_BASE + TK1_RAM_SIZE)) {
return -5; return -1;
} }
if (app_signature < (uint8_t *)TK1_RAM_BASE || if (app_signature < (uint8_t *)TK1_RAM_BASE ||
app_signature >= (uint8_t *)(TK1_RAM_BASE + TK1_RAM_SIZE)) { app_signature >= (uint8_t *)(TK1_RAM_BASE + TK1_RAM_SIZE)) {
return -5; return -1;
} }
if (to_slot >= N_PRELOADED_APP) { if (to_slot >= N_PRELOADED_APP) {
return -4; return -1;
} }
/* Check if we are allowed to store */ /* Check if we are allowed to store */
if (!mgmt_app_authenticate()) { if (!mgmt_app_authenticate()) {
return -3; return -1;
} }
/* Check for a valid app in flash, bale out if it already exists */ /* 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) { if (app_size == 0 || app_size > SIZE_PRE_LOADED_APP) {
return -2; return -1;
} }
part_table->pre_app_data[to_slot].size = app_size; 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(); debug_lf();
if (part_table_write(part_table_storage) != 0) { if (part_table_write(part_table_storage) != 0) {
return -6; return -1;
} }
return 0; 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; struct partition_table *part_table = &part_table_storage->table;
if (part_table_storage == NULL) { if (part_table_storage == NULL) {
return -5; return -1;
} }
if (slot >= N_PRELOADED_APP) { if (slot >= N_PRELOADED_APP) {
return -4; return -1;
} }
/* Check if we are allowed to deleted */ /* Check if we are allowed to deleted */
if (!mgmt_app_authenticate()) { if (!mgmt_app_authenticate()) {
return -3; return -1;
} }
/*Check for a valid app in flash */ /*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)); sizeof(part_table->pre_app_data[slot].signature));
if (part_table_write(part_table_storage) != 0) { if (part_table_write(part_table_storage) != 0) {
return -6; return -1;
} }
/* Assumes the area is 64 KiB block aligned */ /* Assumes the area is 64 KiB block aligned */
@ -197,16 +197,16 @@ int preload_get_digsig(struct partition_table *part_table,
uint8_t slot) uint8_t slot)
{ {
if (part_table == NULL || app_digest == NULL || app_signature == NULL) { if (part_table == NULL || app_digest == NULL || app_signature == NULL) {
return -5; return -1;
} }
if (slot >= N_PRELOADED_APP) { if (slot >= N_PRELOADED_APP) {
return -4; return -1;
} }
/* Check if we are allowed to read */ /* Check if we are allowed to read */
if (!mgmt_app_authenticate()) { if (!mgmt_app_authenticate()) {
return -3; return -1;
} }
memcpy_s(app_digest, 32, part_table->pre_app_data[slot].digest, memcpy_s(app_digest, 32, part_table->pre_app_data[slot].digest,

View file

@ -13,12 +13,15 @@
#include "partition_table.h" #include "partition_table.h"
#include "storage.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) static int get_first_empty(struct partition_table *part_table)
{ {
if (part_table == NULL) { if (part_table == NULL) {
return -4; return -1;
} }
for (uint8_t i = 0; i < N_STORAGE_AREA; i++) { 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 i;
} }
} }
return -1; return -1;
} }
static int index_to_address(int index, uint32_t *address) static int index_to_address(int index, uint32_t *address)
{ {
if (address == NULL) { if (address == NULL) {
return -4; return -1;
} }
if ((index < 0) || (index >= N_STORAGE_AREA)) { if ((index < 0) || (index >= N_STORAGE_AREA)) {
@ -44,12 +48,15 @@ static int index_to_address(int index, uint32_t *address)
return 0; 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) static int storage_get_area(struct partition_table *part_table)
{ {
if (part_table == NULL) { if (part_table == NULL) {
return -4; return -1;
} }
for (uint8_t i = 0; i < N_STORAGE_AREA; i++) { 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; return -1;
} }
@ -79,7 +87,7 @@ int storage_allocate_area(struct partition_table_storage *part_table_storage)
} }
int index = get_first_empty(part_table); int index = get_first_empty(part_table);
if (index == -1) { if (index < 0) {
/* No empty slot */ /* No empty slot */
return -1; 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) int storage_deallocate_area(struct partition_table_storage *part_table_storage)
{ {
if (part_table_storage == NULL) { if (part_table_storage == NULL) {
return -4; return -1;
} }
struct partition_table *part_table = &part_table_storage->table; struct partition_table *part_table = &part_table_storage->table;
int index = storage_get_area(part_table); int index = storage_get_area(part_table);
if (index == -1) { if (index < 0) {
/* No area to deallocate */ /* No area to deallocate */
return -1; return -1;
} }
@ -128,7 +136,7 @@ int storage_deallocate_area(struct partition_table_storage *part_table_storage)
uint32_t start_address = 0; uint32_t start_address = 0;
int err = index_to_address(index, &start_address); int err = index_to_address(index, &start_address);
if (err) { if (err) {
return -3; return -1;
} }
/* Erase area first */ /* 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)); sizeof(part_table->app_storage[index].auth.authentication_digest));
if (part_table_write(part_table_storage) != 0) { if (part_table_write(part_table_storage) != 0) {
return -5; return -1;
} }
return 0; return 0;
@ -162,7 +170,7 @@ int storage_erase_sector(struct partition_table *part_table, uint32_t offset,
size_t size) size_t size)
{ {
if (part_table == NULL) { if (part_table == NULL) {
return -4; return -1;
} }
int index = storage_get_area(part_table); 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; uint32_t start_address = 0;
int err = index_to_address(index, &start_address); int err = index_to_address(index, &start_address);
if (err) { if (err) {
return -3; return -1;
} }
if (offset > SIZE_STORAGE_AREA) { if (offset > SIZE_STORAGE_AREA) {
return -2; return -1;
} }
/* Cannot only erase entire sectors */ /* Cannot only erase entire sectors */
if (offset % 4096 != 0) { if (offset % 4096 != 0) {
return -2; return -1;
} }
/* Cannot erase less than one sector */ /* Cannot erase less than one sector */
if (size < 4096 || size > SIZE_STORAGE_AREA || size % 4096 != 0) { if (size < 4096 || size > SIZE_STORAGE_AREA || size % 4096 != 0) {
return -2; return -1;
} }
if ((offset + size) >= SIZE_STORAGE_AREA) { if ((offset + size) >= SIZE_STORAGE_AREA) {
return -2; return -1;
} }
uint32_t address = start_address + offset; 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) uint8_t *data, size_t size)
{ {
if (part_table == NULL) { if (part_table == NULL) {
return -4; return -1;
} }
// Allow data to point only to app RAM // Allow data to point only to app RAM
if (data < (uint8_t *)TK1_RAM_BASE || if (data < (uint8_t *)TK1_RAM_BASE ||
data >= (uint8_t *)(TK1_RAM_BASE + TK1_RAM_SIZE)) { data >= (uint8_t *)(TK1_RAM_BASE + TK1_RAM_SIZE)) {
return -4; return -1;
} }
int index = storage_get_area(part_table); 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; uint32_t start_address = 0;
int err = index_to_address(index, &start_address); int err = index_to_address(index, &start_address);
if (err) { if (err) {
return -3; return -1;
} }
if (offset > SIZE_STORAGE_AREA) { if (offset > SIZE_STORAGE_AREA) {
return -2; return -1;
} }
if (size > 4096) { if (size > 4096) {
return -2; return -1;
} }
if ((offset + size) > SIZE_STORAGE_AREA) { if ((offset + size) > SIZE_STORAGE_AREA) {
/* Writing outside of area */ /* Writing outside of area */
return -2; return -1;
} }
uint32_t address = start_address + offset; 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) uint8_t *data, size_t size)
{ {
if (part_table == NULL) { if (part_table == NULL) {
return -4; return -1;
} }
// Allow data to point only to app RAM // Allow data to point only to app RAM
if (data < (uint8_t *)TK1_RAM_BASE || if (data < (uint8_t *)TK1_RAM_BASE ||
data >= (uint8_t *)(TK1_RAM_BASE + TK1_RAM_SIZE)) { data >= (uint8_t *)(TK1_RAM_BASE + TK1_RAM_SIZE)) {
return -4; return -1;
} }
int index = storage_get_area(part_table); 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; uint32_t start_address = 0;
int err = index_to_address(index, &start_address); int err = index_to_address(index, &start_address);
if (err) { if (err) {
return -3; return -1;
} }
if (offset > SIZE_STORAGE_AREA) { if (offset > SIZE_STORAGE_AREA) {
return -2; return -1;
} }
if (size > 4096) { if (size > 4096) {
return -2; return -1;
} }
if ((offset + size) > SIZE_STORAGE_AREA) { if ((offset + size) > SIZE_STORAGE_AREA) {
/* Reading outside of area */ /* Reading outside of area */
return -2; return -1;
} }
uint32_t address = start_address + offset; uint32_t address = start_address + offset;