mirror of
https://github.com/tillitis/tillitis-key1.git
synced 2025-06-26 07:00:29 -04:00
Experiment with state machine when starting from flash
- Move around code to start an app from flash. - Mark experimental stuff and debug stuff more clearly.
This commit is contained in:
parent
9316886c88
commit
6ef1b1c659
2 changed files with 41 additions and 20 deletions
|
@ -68,6 +68,8 @@ static void run(const struct context *ctx);
|
||||||
static uint32_t xorwow(uint32_t state, uint32_t acc);
|
static uint32_t xorwow(uint32_t state, uint32_t acc);
|
||||||
#endif
|
#endif
|
||||||
static void scramble_ram(void);
|
static void scramble_ram(void);
|
||||||
|
static int compute_app_digest(uint8_t *digest);
|
||||||
|
static int load_flash_app(struct partition_table *part_table, uint8_t digest[32]);
|
||||||
|
|
||||||
static void print_hw_version(void)
|
static void print_hw_version(void)
|
||||||
{
|
{
|
||||||
|
@ -367,6 +369,24 @@ static void jump_to_app(void)
|
||||||
__builtin_unreachable();
|
__builtin_unreachable();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int load_flash_app(struct partition_table *part_table, uint8_t digest[32])
|
||||||
|
{
|
||||||
|
if (preload_load(part_table) == -1) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
*app_size = part_table->pre_app_data.size;
|
||||||
|
if (*app_size > TK1_APP_MAX_SIZE) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int digest_err = compute_app_digest(digest);
|
||||||
|
assert(digest_err == 0);
|
||||||
|
print_digest(digest);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
static void run_flash(const struct context *ctx, struct partition_table *part_table)
|
static void run_flash(const struct context *ctx, struct partition_table *part_table)
|
||||||
{
|
{
|
||||||
/* At this point we expect an app to be loaded into RAM */
|
/* At this point we expect an app to be loaded into RAM */
|
||||||
|
@ -465,6 +485,7 @@ int main(void)
|
||||||
|
|
||||||
scramble_ram();
|
scramble_ram();
|
||||||
|
|
||||||
|
// TODO Remove
|
||||||
// Wait for terminal program and a character to be typed
|
// Wait for terminal program and a character to be typed
|
||||||
enum ioend endpoint = IO_NONE;
|
enum ioend endpoint = IO_NONE;
|
||||||
uint8_t available = 0;
|
uint8_t available = 0;
|
||||||
|
@ -480,6 +501,8 @@ int main(void)
|
||||||
assert(1 == 2);
|
assert(1 == 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO end of remove block
|
||||||
|
|
||||||
if (part_table_read(&part_table) != 0) {
|
if (part_table_read(&part_table) != 0) {
|
||||||
// Couldn't read or create partition table
|
// Couldn't read or create partition table
|
||||||
assert(1 == 2);
|
assert(1 == 2);
|
||||||
|
@ -490,33 +513,20 @@ int main(void)
|
||||||
run(&ctx);
|
run(&ctx);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Lie and tell filesystem we have a 128 kiB device app on
|
// TODO Lie and tell filesystem we have a 128 kiB device app
|
||||||
// flash.
|
// on flash.
|
||||||
part_table.pre_app_data.size = 0x20000;
|
part_table.pre_app_data.size = 0x20000;
|
||||||
|
|
||||||
// Just start the preloaded app. This should be a part of an
|
// TODO Just start something from flash without looking in
|
||||||
// initial state. The initial state should check resetinfo if
|
// FW_RAM.
|
||||||
// it should start from flash or not.
|
|
||||||
if (preload_load(&part_table) == -1) {
|
|
||||||
state = FW_STATE_FAIL;
|
|
||||||
}
|
|
||||||
|
|
||||||
*app_size = part_table.pre_app_data.size;
|
|
||||||
assert(*app_size <= TK1_APP_MAX_SIZE);
|
|
||||||
|
|
||||||
int digest_err = compute_app_digest(ctx.digest);
|
|
||||||
assert(digest_err == 0);
|
|
||||||
print_digest(ctx.digest);
|
|
||||||
|
|
||||||
part_table.pre_app_data.status = PRE_LOADED_STATUS_PRESENT;
|
|
||||||
|
|
||||||
state = FW_STATE_RUN_FLASH;
|
state = FW_STATE_RUN_FLASH;
|
||||||
|
|
||||||
// End of initial state.
|
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case FW_STATE_INITIAL:
|
case FW_STATE_INITIAL:
|
||||||
|
// Where do we start? Read resetinfo 'startfrom'
|
||||||
|
|
||||||
|
case FW_STATE_WAITCOMMAND:
|
||||||
if (readcommand(&hdr, cmd, state) == -1) {
|
if (readcommand(&hdr, cmd, state) == -1) {
|
||||||
state = FW_STATE_FAIL;
|
state = FW_STATE_FAIL;
|
||||||
break;
|
break;
|
||||||
|
@ -542,6 +552,16 @@ int main(void)
|
||||||
break; // This is never reached!
|
break; // This is never reached!
|
||||||
|
|
||||||
case FW_STATE_RUN_FLASH:
|
case FW_STATE_RUN_FLASH:
|
||||||
|
// TODO Just lie and say that an app is present but not yet
|
||||||
|
// authenticated.
|
||||||
|
part_table.pre_app_data.status = PRE_LOADED_STATUS_PRESENT;
|
||||||
|
|
||||||
|
if (load_flash_app(&part_table, ctx.digest) < 0) {
|
||||||
|
debug_puts("Couldn't load app from flash\n");
|
||||||
|
state = FW_STATE_FAIL;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
run_flash(&ctx, &part_table);
|
run_flash(&ctx, &part_table);
|
||||||
break; // This is never reached!
|
break; // This is never reached!
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
enum state {
|
enum state {
|
||||||
FW_STATE_INITIAL,
|
FW_STATE_INITIAL,
|
||||||
|
FW_STATE_WAITCOMMAND,
|
||||||
FW_STATE_LOADING,
|
FW_STATE_LOADING,
|
||||||
FW_STATE_RUN,
|
FW_STATE_RUN,
|
||||||
FW_STATE_RUN_FLASH,
|
FW_STATE_RUN_FLASH,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue