Implement preload_store

This commit is contained in:
Daniel Jobson 2024-09-19 08:52:42 +02:00
parent 632bd33b9b
commit a9436c092b
No known key found for this signature in database
GPG Key ID: 3707A9DBF4BB8F1A
3 changed files with 49 additions and 9 deletions

View File

@ -7,8 +7,10 @@
#include "lib.h" #include "lib.h"
#include "mgmt_app.h" #include "mgmt_app.h"
#include "partition_table.h" #include "partition_table.h"
#include "htif.h"
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h>
#include <stdint.h> #include <stdint.h>
/* Returns non-zero if the app is valid */ /* Returns non-zero if the app is valid */
@ -45,22 +47,58 @@ int preload_start(partition_table_t *part_table)
return ret; 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 /* Check if we are allowed to store */
// where to store.
// Most likely needs to aggregate some data, before it writes to flash.
/* Check if we are allowed to deleted */
if (!mgmt_app_authenticate(&part_table->mgmt_app_data)) { if (!mgmt_app_authenticate(&part_table->mgmt_app_data)) {
return -1; 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)) { if (preload_check_valid_app(part_table)) {
return -1; 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; return 0;
} }

View File

@ -7,10 +7,11 @@
#include "partition_table.h" #include "partition_table.h"
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stddef.h>
bool preload_check_valid_app(partition_table_t *part_table); bool preload_check_valid_app(partition_table_t *part_table);
int preload_start(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); int preload_delete(partition_table_t *part_table);
#endif #endif

View File

@ -35,7 +35,8 @@ int syscall(syscall_t *ctx)
break; break;
case PRELOAD_STORE: case PRELOAD_STORE:
return preload_store(&part_table); return preload_store(&part_table, ctx->offset, ctx->data,
ctx->size);
break; break;
case PRELOAD_DELETE: case PRELOAD_DELETE: