2022-09-19 02:51:11 -04:00
|
|
|
/*
|
2023-01-30 04:33:02 -05:00
|
|
|
* Copyright (C) 2022, 2023 - Tillitis AB
|
2022-09-19 02:51:11 -04:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
*/
|
|
|
|
|
2022-10-20 08:50:21 -04:00
|
|
|
#include "../tk1_mem.h"
|
2023-02-27 11:11:03 -05:00
|
|
|
#include "assert.h"
|
2022-09-19 02:51:11 -04:00
|
|
|
#include "blake2s/blake2s.h"
|
|
|
|
#include "lib.h"
|
|
|
|
#include "proto.h"
|
2023-03-09 08:11:43 -05:00
|
|
|
#include "state.h"
|
2022-09-19 02:51:11 -04:00
|
|
|
#include "types.h"
|
|
|
|
|
|
|
|
// clang-format off
|
2022-12-09 11:34:17 -05:00
|
|
|
static volatile uint32_t *uds = (volatile uint32_t *)TK1_MMIO_UDS_FIRST;
|
|
|
|
static volatile uint32_t *switch_app = (volatile uint32_t *)TK1_MMIO_TK1_SWITCH_APP;
|
|
|
|
static volatile uint32_t *name0 = (volatile uint32_t *)TK1_MMIO_TK1_NAME0;
|
|
|
|
static volatile uint32_t *name1 = (volatile uint32_t *)TK1_MMIO_TK1_NAME1;
|
|
|
|
static volatile uint32_t *ver = (volatile uint32_t *)TK1_MMIO_TK1_VERSION;
|
|
|
|
static volatile uint32_t *udi = (volatile uint32_t *)TK1_MMIO_TK1_UDI_FIRST;
|
|
|
|
static volatile uint32_t *cdi = (volatile uint32_t *)TK1_MMIO_TK1_CDI_FIRST;
|
|
|
|
static volatile uint32_t *app_addr = (volatile uint32_t *)TK1_MMIO_TK1_APP_ADDR;
|
|
|
|
static volatile uint32_t *app_size = (volatile uint32_t *)TK1_MMIO_TK1_APP_SIZE;
|
|
|
|
static volatile uint32_t *fw_blake2s_addr = (volatile uint32_t *)TK1_MMIO_TK1_BLAKE2S;
|
2022-12-12 09:58:46 -05:00
|
|
|
static volatile uint32_t *trng_status = (volatile uint32_t *)TK1_MMIO_TRNG_STATUS;
|
|
|
|
static volatile uint32_t *trng_entropy = (volatile uint32_t *)TK1_MMIO_TRNG_ENTROPY;
|
|
|
|
static volatile uint32_t *timer = (volatile uint32_t *)TK1_MMIO_TIMER_TIMER;
|
|
|
|
static volatile uint32_t *timer_prescaler = (volatile uint32_t *)TK1_MMIO_TIMER_PRESCALER;
|
|
|
|
static volatile uint32_t *timer_status = (volatile uint32_t *)TK1_MMIO_TIMER_STATUS;
|
|
|
|
static volatile uint32_t *timer_ctrl = (volatile uint32_t *)TK1_MMIO_TIMER_CTRL;
|
2024-03-18 10:57:55 -04:00
|
|
|
static volatile uint32_t *ram_rand = (volatile uint32_t *)TK1_MMIO_TK1_RAM_ADDR_RAND;
|
2023-03-09 02:47:53 -05:00
|
|
|
static volatile uint32_t *ram_scramble = (volatile uint32_t *)TK1_MMIO_TK1_RAM_SCRAMBLE;
|
2022-10-06 10:00:33 -04:00
|
|
|
// clang-format on
|
2022-09-19 02:51:11 -04:00
|
|
|
|
2023-03-09 09:00:16 -05:00
|
|
|
// Context for the loading of a TKey program
|
|
|
|
struct context {
|
2023-03-21 09:11:08 -04:00
|
|
|
uint32_t left; // Bytes left to receive
|
2023-03-09 09:00:16 -05:00
|
|
|
uint8_t digest[32]; // Program digest
|
|
|
|
uint8_t *loadaddr; // Where we are currently loading a TKey program
|
|
|
|
uint8_t use_uss; // Use USS?
|
|
|
|
uint8_t uss[32]; // User Supplied Secret, if any
|
|
|
|
};
|
|
|
|
|
2024-03-04 09:53:12 -05:00
|
|
|
static void print_hw_version(void);
|
2023-03-10 06:14:25 -05:00
|
|
|
static void print_digest(uint8_t *md);
|
2024-03-04 09:53:12 -05:00
|
|
|
static uint32_t rnd_word(void);
|
2023-03-21 09:11:08 -04:00
|
|
|
static void compute_cdi(const uint8_t *digest, const uint8_t use_uss,
|
|
|
|
const uint8_t *uss);
|
2023-03-10 06:14:25 -05:00
|
|
|
static void copy_name(uint8_t *buf, const size_t bufsiz, const uint32_t word);
|
|
|
|
static enum state initial_commands(const struct frame_header *hdr,
|
|
|
|
const uint8_t *cmd, enum state state,
|
|
|
|
struct context *ctx);
|
|
|
|
static enum state loading_commands(const struct frame_header *hdr,
|
|
|
|
const uint8_t *cmd, enum state state,
|
|
|
|
struct context *ctx);
|
|
|
|
static void run(const struct context *ctx);
|
2024-04-04 09:27:31 -04:00
|
|
|
static uint32_t xorwow(uint32_t state, uint32_t acc);
|
2024-03-04 09:53:12 -05:00
|
|
|
static void scramble_ram(void);
|
2023-03-10 06:14:25 -05:00
|
|
|
|
2024-03-04 09:53:12 -05:00
|
|
|
static void print_hw_version(void)
|
2022-09-19 02:51:11 -04:00
|
|
|
{
|
2023-03-09 11:09:23 -05:00
|
|
|
htif_puts("Hello, I'm firmware with");
|
|
|
|
htif_puts(" tk1_name0:");
|
|
|
|
htif_putinthex(*name0);
|
|
|
|
htif_puts(" tk1_name1:");
|
|
|
|
htif_putinthex(*name1);
|
|
|
|
htif_puts(" tk1_version:");
|
|
|
|
htif_putinthex(*ver);
|
2022-12-02 09:09:10 -05:00
|
|
|
htif_lf();
|
2022-11-25 08:00:53 -05:00
|
|
|
}
|
2022-09-19 02:51:11 -04:00
|
|
|
|
|
|
|
static void print_digest(uint8_t *md)
|
|
|
|
{
|
2022-12-02 09:09:10 -05:00
|
|
|
htif_puts("The app digest:\n");
|
2022-09-19 02:51:11 -04:00
|
|
|
for (int j = 0; j < 4; j++) {
|
|
|
|
for (int i = 0; i < 8; i++) {
|
2022-12-02 09:09:10 -05:00
|
|
|
htif_puthex(md[i + 8 * j]);
|
2022-09-19 02:51:11 -04:00
|
|
|
}
|
2022-12-02 09:09:10 -05:00
|
|
|
htif_lf();
|
2022-09-19 02:51:11 -04:00
|
|
|
}
|
2022-12-02 09:09:10 -05:00
|
|
|
htif_lf();
|
2022-09-19 02:51:11 -04:00
|
|
|
}
|
|
|
|
|
2024-03-04 09:53:12 -05:00
|
|
|
static uint32_t rnd_word(void)
|
2023-03-09 02:47:53 -05:00
|
|
|
{
|
|
|
|
while ((*trng_status & (1 << TK1_MMIO_TRNG_STATUS_READY_BIT)) == 0) {
|
|
|
|
}
|
|
|
|
return *trng_entropy;
|
|
|
|
}
|
|
|
|
|
2022-10-13 09:18:09 -04:00
|
|
|
// CDI = blake2s(uds, blake2s(app), uss)
|
2023-03-21 09:11:08 -04:00
|
|
|
static void compute_cdi(const uint8_t *digest, const uint8_t use_uss,
|
|
|
|
const uint8_t *uss)
|
2022-10-13 09:18:09 -04:00
|
|
|
{
|
2024-03-21 07:32:08 -04:00
|
|
|
uint32_t local_uds[8] = {0};
|
|
|
|
uint32_t local_cdi[8] = {0};
|
2023-03-16 08:51:24 -04:00
|
|
|
blake2s_ctx secure_ctx = {0};
|
|
|
|
uint32_t rnd_sleep = 0;
|
|
|
|
int blake2err = 0;
|
2022-10-13 09:18:09 -04:00
|
|
|
|
2023-03-01 09:10:27 -05:00
|
|
|
// Prepare to sleep a random number of cycles before reading out UDS
|
2022-12-12 09:58:46 -05:00
|
|
|
*timer_prescaler = 1;
|
2023-03-16 08:17:42 -04:00
|
|
|
rnd_sleep = rnd_word();
|
2022-12-12 09:58:46 -05:00
|
|
|
// Up to 65536 cycles
|
2023-03-16 08:17:42 -04:00
|
|
|
rnd_sleep &= 0xffff;
|
2023-03-21 09:11:08 -04:00
|
|
|
*timer = (uint32_t)(rnd_sleep == 0 ? 1 : rnd_sleep);
|
2023-01-30 04:33:02 -05:00
|
|
|
*timer_ctrl = (1 << TK1_MMIO_TIMER_CTRL_START_BIT);
|
|
|
|
while (*timer_status & (1 << TK1_MMIO_TIMER_STATUS_RUNNING_BIT)) {
|
2022-12-12 09:58:46 -05:00
|
|
|
}
|
|
|
|
|
2023-03-09 10:08:51 -05:00
|
|
|
blake2err = blake2s_init(&secure_ctx, 32, NULL, 0);
|
2023-03-01 09:10:27 -05:00
|
|
|
assert(blake2err == 0);
|
|
|
|
|
|
|
|
// Update hash with UDS. This means UDS will live for a short
|
2023-03-02 12:20:28 -05:00
|
|
|
// while on the firmware stack which is in the special fw_ram.
|
2023-03-27 08:57:58 -04:00
|
|
|
wordcpy_s(local_uds, 8, (void *)uds, 8);
|
|
|
|
blake2s_update(&secure_ctx, (const void *)local_uds, 32);
|
2024-02-02 08:20:21 -05:00
|
|
|
(void)secure_wipe(local_uds, sizeof(local_uds));
|
2023-03-01 09:10:27 -05:00
|
|
|
|
|
|
|
// Update with TKey program digest
|
2023-03-02 12:20:28 -05:00
|
|
|
blake2s_update(&secure_ctx, digest, 32);
|
2023-03-01 09:10:27 -05:00
|
|
|
|
|
|
|
// Possibly hash in the USS as well
|
2022-11-03 06:30:00 -04:00
|
|
|
if (use_uss != 0) {
|
2023-03-02 12:20:28 -05:00
|
|
|
blake2s_update(&secure_ctx, uss, 32);
|
2022-11-03 06:30:00 -04:00
|
|
|
}
|
2022-10-13 09:18:09 -04:00
|
|
|
|
2023-03-01 09:10:27 -05:00
|
|
|
// Write hashed result to Compound Device Identity (CDI)
|
2023-03-21 09:11:08 -04:00
|
|
|
blake2s_final(&secure_ctx, &local_cdi);
|
2022-10-13 09:18:09 -04:00
|
|
|
|
2023-03-16 08:30:56 -04:00
|
|
|
// Clear secure_ctx of any residue of UDS. Don't want to keep
|
|
|
|
// that for long even though fw_ram is cleared later.
|
2024-02-02 08:20:21 -05:00
|
|
|
(void)secure_wipe(&secure_ctx, sizeof(secure_ctx));
|
2022-10-13 09:18:09 -04:00
|
|
|
|
2023-03-01 09:10:27 -05:00
|
|
|
// CDI only word writable
|
2023-03-21 09:11:08 -04:00
|
|
|
wordcpy_s((void *)cdi, 8, &local_cdi, 8);
|
2022-10-13 09:18:09 -04:00
|
|
|
}
|
|
|
|
|
2023-03-09 11:09:23 -05:00
|
|
|
static void copy_name(uint8_t *buf, const size_t bufsiz, const uint32_t word)
|
|
|
|
{
|
|
|
|
assert(bufsiz >= 4);
|
|
|
|
|
|
|
|
buf[0] = word >> 24;
|
|
|
|
buf[1] = word >> 16;
|
|
|
|
buf[2] = word >> 8;
|
|
|
|
buf[3] = word;
|
|
|
|
}
|
|
|
|
|
2023-03-10 01:41:28 -05:00
|
|
|
static enum state initial_commands(const struct frame_header *hdr,
|
|
|
|
const uint8_t *cmd, enum state state,
|
|
|
|
struct context *ctx)
|
2023-03-09 09:00:16 -05:00
|
|
|
{
|
|
|
|
uint8_t rsp[CMDLEN_MAXBYTES] = {0};
|
|
|
|
|
|
|
|
switch (cmd[0]) {
|
2023-03-09 11:09:23 -05:00
|
|
|
case FW_CMD_NAME_VERSION:
|
2023-03-09 09:00:16 -05:00
|
|
|
htif_puts("cmd: name-version\n");
|
|
|
|
if (hdr->len != 1) {
|
2023-03-09 10:33:48 -05:00
|
|
|
// Bad length
|
2023-03-10 01:41:28 -05:00
|
|
|
state = FW_STATE_FAIL;
|
|
|
|
break;
|
2023-03-09 09:00:16 -05:00
|
|
|
}
|
|
|
|
|
2023-03-09 11:09:23 -05:00
|
|
|
copy_name(rsp, CMDLEN_MAXBYTES, *name0);
|
|
|
|
copy_name(&rsp[4], CMDLEN_MAXBYTES - 4, *name1);
|
|
|
|
wordcpy_s(&rsp[8], CMDLEN_MAXBYTES / 4 - 2, (void *)ver, 1);
|
|
|
|
|
2023-03-09 09:00:16 -05:00
|
|
|
fwreply(*hdr, FW_RSP_NAME_VERSION, rsp);
|
2023-03-10 01:41:28 -05:00
|
|
|
// still initial state
|
2023-03-09 11:09:23 -05:00
|
|
|
break;
|
2023-03-09 09:00:16 -05:00
|
|
|
|
2023-03-09 10:08:51 -05:00
|
|
|
case FW_CMD_GET_UDI: {
|
|
|
|
uint32_t udi_words[2];
|
|
|
|
|
2023-03-09 09:00:16 -05:00
|
|
|
htif_puts("cmd: get-udi\n");
|
|
|
|
if (hdr->len != 1) {
|
2023-03-09 10:33:48 -05:00
|
|
|
// Bad length
|
2023-03-10 01:41:28 -05:00
|
|
|
state = FW_STATE_FAIL;
|
|
|
|
break;
|
2023-03-09 09:00:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
rsp[0] = STATUS_OK;
|
2023-03-21 09:11:08 -04:00
|
|
|
wordcpy_s(&udi_words, 2, (void *)udi, 2);
|
|
|
|
memcpy_s(&rsp[1], CMDLEN_MAXBYTES - 1, &udi_words, 2 * 4);
|
2023-03-09 09:00:16 -05:00
|
|
|
fwreply(*hdr, FW_RSP_GET_UDI, rsp);
|
2023-03-10 01:41:28 -05:00
|
|
|
// still initial state
|
|
|
|
break;
|
|
|
|
}
|
2023-03-09 10:08:51 -05:00
|
|
|
|
|
|
|
case FW_CMD_LOAD_APP: {
|
|
|
|
uint32_t local_app_size;
|
2023-03-09 09:00:16 -05:00
|
|
|
|
|
|
|
htif_puts("cmd: load-app(size, uss)\n");
|
2023-03-24 06:47:30 -04:00
|
|
|
if (hdr->len != 128) {
|
2023-03-09 09:00:16 -05:00
|
|
|
// Bad length
|
2023-03-10 01:41:28 -05:00
|
|
|
state = FW_STATE_FAIL;
|
|
|
|
break;
|
2023-03-09 09:00:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// cmd[1..4] contains the size.
|
2023-03-09 10:08:51 -05:00
|
|
|
local_app_size =
|
2023-03-09 09:00:16 -05:00
|
|
|
cmd[1] + (cmd[2] << 8) + (cmd[3] << 16) + (cmd[4] << 24);
|
|
|
|
|
|
|
|
htif_puts("app size: ");
|
|
|
|
htif_putinthex(local_app_size);
|
|
|
|
htif_lf();
|
|
|
|
|
|
|
|
if (local_app_size == 0 || local_app_size > TK1_APP_MAX_SIZE) {
|
|
|
|
rsp[0] = STATUS_BAD;
|
|
|
|
fwreply(*hdr, FW_RSP_LOAD_APP, rsp);
|
2023-03-10 01:41:28 -05:00
|
|
|
// still initial state
|
|
|
|
break;
|
2023-03-09 09:00:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
*app_size = local_app_size;
|
|
|
|
|
|
|
|
// Do we have a USS at all?
|
|
|
|
if (cmd[5] != 0) {
|
|
|
|
// Yes
|
|
|
|
ctx->use_uss = TRUE;
|
|
|
|
memcpy_s(ctx->uss, 32, &cmd[6], 32);
|
|
|
|
} else {
|
|
|
|
ctx->use_uss = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
rsp[0] = STATUS_OK;
|
|
|
|
fwreply(*hdr, FW_RSP_LOAD_APP, rsp);
|
|
|
|
|
|
|
|
assert(*app_size != 0);
|
|
|
|
assert(*app_size <= TK1_APP_MAX_SIZE);
|
|
|
|
|
|
|
|
ctx->left = *app_size;
|
|
|
|
|
2023-03-10 01:41:28 -05:00
|
|
|
state = FW_STATE_LOADING;
|
|
|
|
break;
|
|
|
|
}
|
2023-03-09 09:00:16 -05:00
|
|
|
|
|
|
|
default:
|
|
|
|
htif_puts("Got unknown firmware cmd: 0x");
|
|
|
|
htif_puthex(cmd[0]);
|
|
|
|
htif_lf();
|
2023-03-10 01:41:28 -05:00
|
|
|
state = FW_STATE_FAIL;
|
|
|
|
break;
|
2023-03-09 09:00:16 -05:00
|
|
|
}
|
|
|
|
|
2023-03-10 01:41:28 -05:00
|
|
|
return state;
|
2023-03-09 09:00:16 -05:00
|
|
|
}
|
|
|
|
|
2023-03-10 01:41:28 -05:00
|
|
|
static enum state loading_commands(const struct frame_header *hdr,
|
|
|
|
const uint8_t *cmd, enum state state,
|
|
|
|
struct context *ctx)
|
2023-03-09 09:00:16 -05:00
|
|
|
{
|
|
|
|
uint8_t rsp[CMDLEN_MAXBYTES] = {0};
|
2023-03-21 09:11:08 -04:00
|
|
|
uint32_t nbytes = 0;
|
2023-03-09 09:00:16 -05:00
|
|
|
|
|
|
|
switch (cmd[0]) {
|
|
|
|
case FW_CMD_LOAD_APP_DATA:
|
|
|
|
htif_puts("cmd: load-app-data\n");
|
2023-03-24 06:47:30 -04:00
|
|
|
if (hdr->len != 128) {
|
2023-03-09 10:33:48 -05:00
|
|
|
// Bad length
|
2023-03-10 01:41:28 -05:00
|
|
|
state = FW_STATE_FAIL;
|
|
|
|
break;
|
2023-03-09 09:00:16 -05:00
|
|
|
}
|
|
|
|
|
2023-03-24 06:47:30 -04:00
|
|
|
if (ctx->left > (128 - 1)) {
|
|
|
|
nbytes = 128 - 1;
|
2023-03-09 09:00:16 -05:00
|
|
|
} else {
|
|
|
|
nbytes = ctx->left;
|
|
|
|
}
|
|
|
|
memcpy_s(ctx->loadaddr, ctx->left, cmd + 1, nbytes);
|
2024-03-21 07:32:08 -04:00
|
|
|
/*@-mustfreeonly@*/
|
2023-03-09 09:00:16 -05:00
|
|
|
ctx->loadaddr += nbytes;
|
2024-03-21 07:32:08 -04:00
|
|
|
/*@+mustfreeonly@*/
|
2023-03-09 09:00:16 -05:00
|
|
|
ctx->left -= nbytes;
|
|
|
|
|
|
|
|
if (ctx->left == 0) {
|
2023-03-21 09:11:08 -04:00
|
|
|
blake2s_ctx b2s_ctx = {0};
|
|
|
|
int blake2err = 0;
|
|
|
|
|
2023-03-09 09:00:16 -05:00
|
|
|
htif_puts("Fully loaded ");
|
|
|
|
htif_putinthex(*app_size);
|
|
|
|
htif_lf();
|
|
|
|
|
|
|
|
// Compute Blake2S digest of the app,
|
|
|
|
// storing it for FW_STATE_RUN
|
2023-03-21 09:11:08 -04:00
|
|
|
blake2err = blake2s(&ctx->digest, 32, NULL, 0,
|
|
|
|
(const void *)TK1_RAM_BASE,
|
|
|
|
*app_size, &b2s_ctx);
|
|
|
|
assert(blake2err == 0);
|
2023-03-09 09:00:16 -05:00
|
|
|
print_digest(ctx->digest);
|
|
|
|
|
|
|
|
// And return the digest in final
|
|
|
|
// response
|
|
|
|
rsp[0] = STATUS_OK;
|
|
|
|
memcpy_s(&rsp[1], CMDLEN_MAXBYTES - 1, &ctx->digest,
|
|
|
|
32);
|
|
|
|
fwreply(*hdr, FW_RSP_LOAD_APP_DATA_READY, rsp);
|
|
|
|
|
2023-03-10 01:41:28 -05:00
|
|
|
state = FW_STATE_RUN;
|
|
|
|
break;
|
2023-03-09 09:00:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
rsp[0] = STATUS_OK;
|
|
|
|
fwreply(*hdr, FW_RSP_LOAD_APP_DATA, rsp);
|
2023-03-10 01:41:28 -05:00
|
|
|
// still loading state
|
2023-03-09 09:00:16 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
htif_puts("Got unknown firmware cmd: 0x");
|
|
|
|
htif_puthex(cmd[0]);
|
|
|
|
htif_lf();
|
2023-03-10 01:41:28 -05:00
|
|
|
state = FW_STATE_FAIL;
|
|
|
|
break;
|
2023-03-09 09:00:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void run(const struct context *ctx)
|
|
|
|
{
|
|
|
|
*app_addr = TK1_RAM_BASE;
|
|
|
|
|
|
|
|
// CDI = hash(uds, hash(app), uss)
|
|
|
|
compute_cdi(ctx->digest, ctx->use_uss, ctx->uss);
|
|
|
|
|
|
|
|
htif_puts("Flipping to app mode!\n");
|
|
|
|
htif_puts("Jumping to ");
|
|
|
|
htif_putinthex(*app_addr);
|
|
|
|
htif_lf();
|
|
|
|
|
|
|
|
// Clear the firmware stack
|
|
|
|
// clang-format off
|
2023-03-21 09:11:08 -04:00
|
|
|
#ifndef S_SPLINT_S
|
2023-03-09 09:00:16 -05:00
|
|
|
asm volatile(
|
|
|
|
"li a0, 0xd0000000;" // FW_RAM
|
|
|
|
"li a1, 0xd0000800;" // End of 2 KB FW_RAM (just past the end)
|
|
|
|
"loop:;"
|
|
|
|
"sw zero, 0(a0);"
|
|
|
|
"addi a0, a0, 4;"
|
|
|
|
"blt a0, a1, loop;"
|
|
|
|
::: "memory");
|
2023-03-21 09:11:08 -04:00
|
|
|
#endif
|
2023-03-09 09:00:16 -05:00
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
// Flip over to application mode
|
|
|
|
*switch_app = 1;
|
|
|
|
|
|
|
|
// XXX Firmware stack now no longer available
|
|
|
|
// Don't use any function calls!
|
|
|
|
|
|
|
|
// Jump to app - doesn't return
|
|
|
|
// clang-format off
|
2023-03-21 09:11:08 -04:00
|
|
|
#ifndef S_SPLINT_S
|
2023-03-09 09:00:16 -05:00
|
|
|
asm volatile(
|
|
|
|
// Get value at TK1_MMIO_TK1_APP_ADDR
|
|
|
|
"lui a0,0xff000;"
|
|
|
|
"lw a0,0x030(a0);"
|
|
|
|
// Jump to it
|
|
|
|
"jalr x0,0(a0);"
|
|
|
|
::: "memory");
|
2023-03-21 09:11:08 -04:00
|
|
|
#endif
|
2023-03-09 09:00:16 -05:00
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
__builtin_unreachable();
|
|
|
|
}
|
|
|
|
|
2024-04-04 09:27:31 -04:00
|
|
|
static uint32_t xorwow(uint32_t state, uint32_t acc)
|
|
|
|
{
|
|
|
|
state ^= state << 13;
|
|
|
|
state ^= state >> 17;
|
|
|
|
state ^= state << 5;
|
|
|
|
state += acc;
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2024-03-04 09:53:12 -05:00
|
|
|
static void scramble_ram(void)
|
2022-09-19 02:51:11 -04:00
|
|
|
{
|
2023-03-09 09:50:55 -05:00
|
|
|
uint32_t *ram = (uint32_t *)(TK1_RAM_BASE);
|
|
|
|
|
2024-04-04 09:27:31 -04:00
|
|
|
// Fill RAM with random data
|
|
|
|
// Get random state and accumulator seeds.
|
|
|
|
uint32_t data_state = rnd_word();
|
|
|
|
uint32_t data_acc = rnd_word();
|
|
|
|
|
2023-03-09 02:47:53 -05:00
|
|
|
for (uint32_t w = 0; w < TK1_RAM_SIZE / 4; w++) {
|
2024-04-04 09:27:31 -04:00
|
|
|
data_state = xorwow(data_state, data_acc);
|
|
|
|
ram[w] = data_state;
|
2023-03-09 02:47:53 -05:00
|
|
|
}
|
|
|
|
|
2024-06-12 09:12:09 -04:00
|
|
|
// Set RAM address and data scrambling parameters
|
2024-03-18 10:57:55 -04:00
|
|
|
*ram_rand = rnd_word();
|
2023-03-09 02:47:53 -05:00
|
|
|
*ram_scramble = rnd_word();
|
2023-03-09 09:50:55 -05:00
|
|
|
}
|
2023-03-09 02:47:53 -05:00
|
|
|
|
2024-03-04 09:53:12 -05:00
|
|
|
int main(void)
|
2023-03-09 09:50:55 -05:00
|
|
|
{
|
2023-03-09 09:00:16 -05:00
|
|
|
struct context ctx = {0};
|
2023-03-16 08:51:24 -04:00
|
|
|
struct frame_header hdr = {0};
|
|
|
|
uint8_t cmd[CMDLEN_MAXBYTES] = {0};
|
2022-11-03 06:30:00 -04:00
|
|
|
enum state state = FW_STATE_INITIAL;
|
2023-03-09 09:50:55 -05:00
|
|
|
|
2023-03-09 11:09:23 -05:00
|
|
|
print_hw_version();
|
|
|
|
|
2022-12-09 11:34:17 -05:00
|
|
|
// Let the app know the function adddress for blake2s()
|
|
|
|
*fw_blake2s_addr = (uint32_t)blake2s;
|
2023-03-09 09:00:16 -05:00
|
|
|
|
2024-03-21 07:32:08 -04:00
|
|
|
/*@-mustfreeonly@*/
|
|
|
|
/* Yes, splint, this points directly to RAM and we don't care
|
|
|
|
* about freeing anything was pointing to 0x0 before.
|
|
|
|
*/
|
2023-03-09 09:00:16 -05:00
|
|
|
ctx.loadaddr = (uint8_t *)TK1_RAM_BASE;
|
2024-03-21 07:32:08 -04:00
|
|
|
/*@+mustfreeonly@*/
|
2023-03-09 09:00:16 -05:00
|
|
|
ctx.use_uss = FALSE;
|
2022-09-19 02:51:11 -04:00
|
|
|
|
2023-03-09 09:50:55 -05:00
|
|
|
scramble_ram();
|
|
|
|
|
2022-09-19 02:51:11 -04:00
|
|
|
for (;;) {
|
2022-11-03 06:30:00 -04:00
|
|
|
switch (state) {
|
|
|
|
case FW_STATE_INITIAL:
|
2023-03-09 08:11:43 -05:00
|
|
|
if (readcommand(&hdr, cmd, state) == -1) {
|
|
|
|
state = FW_STATE_FAIL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-03-09 09:00:16 -05:00
|
|
|
state = initial_commands(&hdr, cmd, state, &ctx);
|
2022-11-03 06:30:00 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FW_STATE_LOADING:
|
2023-03-09 08:11:43 -05:00
|
|
|
if (readcommand(&hdr, cmd, state) == -1) {
|
|
|
|
state = FW_STATE_FAIL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-03-09 09:00:16 -05:00
|
|
|
state = loading_commands(&hdr, cmd, state, &ctx);
|
2022-11-03 06:30:00 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FW_STATE_RUN:
|
2023-03-09 09:00:16 -05:00
|
|
|
run(&ctx);
|
2022-11-03 06:30:00 -04:00
|
|
|
break; // This is never reached!
|
|
|
|
|
2023-03-07 09:08:24 -05:00
|
|
|
case FW_STATE_FAIL:
|
|
|
|
// fallthrough
|
2022-11-03 06:30:00 -04:00
|
|
|
default:
|
2023-03-07 09:08:24 -05:00
|
|
|
htif_puts("firmware state 0x");
|
2022-12-02 09:09:10 -05:00
|
|
|
htif_puthex(state);
|
|
|
|
htif_lf();
|
2024-03-14 10:07:42 -04:00
|
|
|
assert(1 == 2);
|
2022-11-03 06:30:00 -04:00
|
|
|
break; // Not reached
|
|
|
|
}
|
2022-09-19 02:51:11 -04:00
|
|
|
}
|
|
|
|
|
2024-03-21 07:32:08 -04:00
|
|
|
/*@ -compdestroy @*/
|
|
|
|
/* We don't care about memory leaks here. */
|
2022-09-19 02:51:11 -04:00
|
|
|
return (int)0xcafebabe;
|
|
|
|
}
|