From a9436c092b6a4a452011e63872c5b2043b2f22ed Mon Sep 17 00:00:00 2001 From: Daniel Jobson Date: Thu, 19 Sep 2024 08:52:42 +0200 Subject: [PATCH] Implement preload_store --- hw/application_fpga/fw/tk1/preload_app.c | 52 ++++++++++++++++++++---- hw/application_fpga/fw/tk1/preload_app.h | 3 +- hw/application_fpga/fw/tk1/syscall.c | 3 +- 3 files changed, 49 insertions(+), 9 deletions(-) diff --git a/hw/application_fpga/fw/tk1/preload_app.c b/hw/application_fpga/fw/tk1/preload_app.c index db5ca20..2379fd9 100644 --- a/hw/application_fpga/fw/tk1/preload_app.c +++ b/hw/application_fpga/fw/tk1/preload_app.c @@ -7,8 +7,10 @@ #include "lib.h" #include "mgmt_app.h" #include "partition_table.h" +#include "htif.h" #include +#include #include /* Returns non-zero if the app is valid */ @@ -45,22 +47,58 @@ int preload_start(partition_table_t *part_table) return ret; } -int preload_store(partition_table_t *part_table) +/* Expects to receive chunks of data up to 4096 bytes to store into the + * preloaded area. The offset needs to be kept and updated between each call. + * Once done, call preload_store_finalize() with the last parameters. + * */ +int preload_store(partition_table_t *part_table, uint32_t offset, uint8_t *data, + size_t size) { - // TODO: Can reuse the app loading context in main, to keep track of - // where to store. - // Most likely needs to aggregate some data, before it writes to flash. - - /* Check if we are allowed to deleted */ + /* Check if we are allowed to store */ if (!mgmt_app_authenticate(&part_table->mgmt_app_data)) { 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 */ if (preload_check_valid_app(part_table)) { return -1; } + if ((offset + size) > SIZE_PRE_LOADED_APP || + size > 4096) { + /* Writing outside of area */ + return -2; + } + + uint32_t address = ADDR_PRE_LOADED_APP + offset; + + htif_puts("preload_store: write to addr: "); + htif_putinthex(address); + htif_lf(); + + return flash_write_data(address, data, size); +} + +int preload_store_finalize(partition_table_t *part_table, bool use_uss, + uint32_t *uss, size_t app_size) +{ + /* Check if we are allowed to store */ + if (!mgmt_app_authenticate(&part_table->mgmt_app_data)) { + return -1; + } + + /* Check for a valid app in flash, bale out if it already exists */ + if (preload_check_valid_app(part_table)) { + return -1; + } + + part_table->pre_app_data.size = app_size; + part_table->pre_app_data.status = 0x02; /* Stored but not yet authenticated */ + + part_table_write(part_table); + + /* Force a restart to authenticate the stored app */ + return 0; } diff --git a/hw/application_fpga/fw/tk1/preload_app.h b/hw/application_fpga/fw/tk1/preload_app.h index ffff4ba..8cc7f0c 100644 --- a/hw/application_fpga/fw/tk1/preload_app.h +++ b/hw/application_fpga/fw/tk1/preload_app.h @@ -7,10 +7,11 @@ #include "partition_table.h" #include #include +#include bool preload_check_valid_app(partition_table_t *part_table); int preload_start(partition_table_t *part_table); -int preload_store(partition_table_t *part_table); +int preload_store(partition_table_t *part_table, uint32_t offset, uint8_t *data, size_t size); int preload_delete(partition_table_t *part_table); #endif diff --git a/hw/application_fpga/fw/tk1/syscall.c b/hw/application_fpga/fw/tk1/syscall.c index 46521ca..0a90fd3 100644 --- a/hw/application_fpga/fw/tk1/syscall.c +++ b/hw/application_fpga/fw/tk1/syscall.c @@ -35,7 +35,8 @@ int syscall(syscall_t *ctx) break; case PRELOAD_STORE: - return preload_store(&part_table); + return preload_store(&part_table, ctx->offset, ctx->data, + ctx->size); break; case PRELOAD_DELETE: