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-12-09 11:34:17 -05:00
|
|
|
#include "../tk1/blake2s/blake2s.h"
|
2022-10-20 08:50:21 -04:00
|
|
|
#include "../tk1/lib.h"
|
|
|
|
#include "../tk1/proto.h"
|
|
|
|
#include "../tk1/types.h"
|
|
|
|
#include "../tk1_mem.h"
|
2022-09-19 02:51:11 -04:00
|
|
|
|
|
|
|
// clang-format off
|
2022-12-09 11:34:17 -05:00
|
|
|
volatile uint32_t *tk1name0 = (volatile uint32_t *)TK1_MMIO_TK1_NAME0;
|
|
|
|
volatile uint32_t *tk1name1 = (volatile uint32_t *)TK1_MMIO_TK1_NAME1;
|
|
|
|
volatile uint32_t *uds = (volatile uint32_t *)TK1_MMIO_UDS_FIRST;
|
|
|
|
volatile uint32_t *cdi = (volatile uint32_t *)TK1_MMIO_TK1_CDI_FIRST;
|
|
|
|
volatile uint32_t *udi = (volatile uint32_t *)TK1_MMIO_TK1_UDI_FIRST;
|
|
|
|
volatile uint32_t *switch_app = (volatile uint32_t *)TK1_MMIO_TK1_SWITCH_APP;
|
|
|
|
volatile uint8_t *fw_ram = (volatile uint8_t *)TK1_MMIO_FW_RAM_BASE;
|
|
|
|
volatile uint32_t *timer = (volatile uint32_t *)TK1_MMIO_TIMER_TIMER;
|
2022-10-20 08:50:21 -04:00
|
|
|
volatile uint32_t *timer_prescaler = (volatile uint32_t *)TK1_MMIO_TIMER_PRESCALER;
|
2022-12-09 11:34:17 -05:00
|
|
|
volatile uint32_t *timer_status = (volatile uint32_t *)TK1_MMIO_TIMER_STATUS;
|
|
|
|
volatile uint32_t *timer_ctrl = (volatile uint32_t *)TK1_MMIO_TIMER_CTRL;
|
|
|
|
volatile uint32_t *trng_status = (volatile uint32_t *)TK1_MMIO_TRNG_STATUS;
|
|
|
|
volatile uint32_t *trng_entropy = (volatile uint32_t *)TK1_MMIO_TRNG_ENTROPY;
|
|
|
|
volatile uint32_t *fw_blake2s_addr = (volatile uint32_t *)TK1_MMIO_TK1_BLAKE2S;
|
2022-09-19 02:51:11 -04:00
|
|
|
// clang-format on
|
|
|
|
|
2022-12-09 02:32:04 -05:00
|
|
|
#define UDS_WORDS 8
|
|
|
|
#define UDI_WORDS 2
|
|
|
|
#define CDI_WORDS 8
|
|
|
|
|
2023-03-01 08:00:27 -05:00
|
|
|
void *memcpy(void *dest, const void *src, size_t n)
|
|
|
|
{
|
|
|
|
uint8_t *src_byte = (uint8_t *)src;
|
|
|
|
uint8_t *dest_byte = (uint8_t *)dest;
|
|
|
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
dest_byte[i] = src_byte[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
2022-12-02 10:11:53 -05:00
|
|
|
void puts(char *reason)
|
2022-09-19 02:51:11 -04:00
|
|
|
{
|
|
|
|
for (char *c = reason; *c != '\0'; c++) {
|
|
|
|
writebyte(*c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-02 10:11:53 -05:00
|
|
|
void putsn(char *p, int n)
|
2022-09-19 02:51:11 -04:00
|
|
|
{
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
writebyte(p[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-02 10:11:53 -05:00
|
|
|
void puthex(uint8_t c)
|
2022-09-19 02:51:11 -04:00
|
|
|
{
|
|
|
|
unsigned int upper = (c >> 4) & 0xf;
|
|
|
|
unsigned int lower = c & 0xf;
|
|
|
|
writebyte(upper < 10 ? '0' + upper : 'a' - 10 + upper);
|
|
|
|
writebyte(lower < 10 ? '0' + lower : 'a' - 10 + lower);
|
|
|
|
}
|
|
|
|
|
2022-12-02 10:11:53 -05:00
|
|
|
void puthexn(uint8_t *p, int n)
|
2022-09-19 02:51:11 -04:00
|
|
|
{
|
|
|
|
for (int i = 0; i < n; i++) {
|
2022-12-02 10:11:53 -05:00
|
|
|
puthex(p[i]);
|
2022-09-19 02:51:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-09 11:34:17 -05:00
|
|
|
void hexdump(uint8_t *buf, int len)
|
|
|
|
{
|
|
|
|
uint8_t *row;
|
|
|
|
uint8_t *byte;
|
|
|
|
uint8_t *max;
|
|
|
|
|
|
|
|
row = buf;
|
|
|
|
max = &buf[len];
|
|
|
|
for (byte = 0; byte != max; row = byte) {
|
|
|
|
for (byte = row; byte != max && byte != (row + 16); byte++) {
|
|
|
|
puthex(*byte);
|
|
|
|
}
|
|
|
|
|
|
|
|
puts("\r\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-02 10:11:53 -05:00
|
|
|
void reverseword(uint32_t *wordp)
|
2022-09-19 02:51:11 -04:00
|
|
|
{
|
|
|
|
*wordp = ((*wordp & 0xff000000) >> 24) | ((*wordp & 0x00ff0000) >> 8) |
|
|
|
|
((*wordp & 0x0000ff00) << 8) | ((*wordp & 0x000000ff) << 24);
|
|
|
|
}
|
|
|
|
|
2022-10-19 12:03:21 -04:00
|
|
|
uint32_t wait_timer_tick(uint32_t last_timer)
|
|
|
|
{
|
|
|
|
uint32_t newtimer;
|
|
|
|
for (;;) {
|
|
|
|
newtimer = *timer;
|
|
|
|
if (newtimer != last_timer) {
|
|
|
|
return newtimer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-19 02:51:11 -04:00
|
|
|
int main()
|
|
|
|
{
|
2023-03-07 09:03:59 -05:00
|
|
|
// Function pointer to blake2s()
|
|
|
|
volatile int (*fw_blake2s)(void *, unsigned long, const void *,
|
|
|
|
unsigned long, const void *, unsigned long,
|
|
|
|
blake2s_ctx *);
|
|
|
|
|
2022-09-19 02:51:11 -04:00
|
|
|
uint8_t in;
|
|
|
|
|
|
|
|
// Wait for terminal program and a character to be typed
|
|
|
|
in = readbyte();
|
|
|
|
|
2022-12-02 10:11:53 -05:00
|
|
|
puts("I'm testfw on:");
|
2022-10-20 08:50:21 -04:00
|
|
|
// Output the TK1 core's NAME0 and NAME1
|
2022-09-19 02:51:11 -04:00
|
|
|
uint32_t name;
|
2023-03-01 08:00:27 -05:00
|
|
|
wordcpy_s(&name, 1, (void *)tk1name0, 1);
|
2022-12-02 10:11:53 -05:00
|
|
|
reverseword(&name);
|
|
|
|
putsn((char *)&name, 4);
|
|
|
|
puts(" ");
|
2023-03-01 08:00:27 -05:00
|
|
|
wordcpy_s(&name, 1, (void *)tk1name1, 1);
|
2022-12-02 10:11:53 -05:00
|
|
|
reverseword(&name);
|
|
|
|
putsn((char *)&name, 4);
|
|
|
|
puts("\r\n");
|
2022-09-19 02:51:11 -04:00
|
|
|
|
2022-12-09 02:32:04 -05:00
|
|
|
uint32_t zeros[8];
|
|
|
|
memset(zeros, 0, 8 * 4);
|
|
|
|
|
2022-09-19 02:51:11 -04:00
|
|
|
int anyfailed = 0;
|
|
|
|
|
2022-12-09 02:32:04 -05:00
|
|
|
uint32_t uds_local[UDS_WORDS];
|
2023-03-01 08:00:27 -05:00
|
|
|
|
2022-09-19 02:51:11 -04:00
|
|
|
// Should get non-empty UDS
|
2023-03-01 08:00:27 -05:00
|
|
|
wordcpy_s(uds_local, UDS_WORDS, (void *)uds, UDS_WORDS);
|
2022-12-09 02:32:04 -05:00
|
|
|
if (memeq(uds_local, zeros, UDS_WORDS * 4)) {
|
2022-12-02 10:11:53 -05:00
|
|
|
puts("FAIL: UDS empty\r\n");
|
2022-09-19 02:51:11 -04:00
|
|
|
anyfailed = 1;
|
|
|
|
}
|
2023-03-01 08:00:27 -05:00
|
|
|
|
2022-09-19 02:51:11 -04:00
|
|
|
// Should NOT be able to read from UDS again
|
2023-03-01 08:00:27 -05:00
|
|
|
wordcpy_s(uds_local, UDS_WORDS, (void *)uds, UDS_WORDS);
|
2022-12-09 02:32:04 -05:00
|
|
|
if (!memeq(uds_local, zeros, UDS_WORDS * 4)) {
|
2022-12-02 10:11:53 -05:00
|
|
|
puts("FAIL: Read UDS a second time\r\n");
|
2022-09-19 02:51:11 -04:00
|
|
|
anyfailed = 1;
|
|
|
|
}
|
|
|
|
|
2022-12-09 02:32:04 -05:00
|
|
|
uint32_t udi_local[UDI_WORDS];
|
2022-09-19 02:51:11 -04:00
|
|
|
// Should get non-empty UDI
|
2023-03-01 08:00:27 -05:00
|
|
|
wordcpy_s(udi_local, UDI_WORDS, (void *)udi, UDI_WORDS);
|
2022-12-09 02:32:04 -05:00
|
|
|
if (memeq(udi_local, zeros, UDI_WORDS * 4)) {
|
2022-12-02 10:11:53 -05:00
|
|
|
puts("FAIL: UDI empty\r\n");
|
2022-09-19 02:51:11 -04:00
|
|
|
anyfailed = 1;
|
|
|
|
}
|
|
|
|
|
2022-10-17 05:11:54 -04:00
|
|
|
// Should be able to write to CDI in fw (non-app) mode.
|
2022-12-09 02:32:04 -05:00
|
|
|
uint32_t cdi_writetest[CDI_WORDS] = {0xdeafbeef, 0xdeafbeef, 0xdeafbeef,
|
|
|
|
0xdeafbeef, 0xdeafbeef, 0xdeafbeef,
|
|
|
|
0xdeafbeef, 0xdeafbeef};
|
|
|
|
uint32_t cdi_readback[CDI_WORDS];
|
2023-03-01 08:00:27 -05:00
|
|
|
|
|
|
|
wordcpy_s((void *)cdi, CDI_WORDS, cdi_writetest, CDI_WORDS);
|
|
|
|
wordcpy_s(cdi_readback, CDI_WORDS, (void *)cdi, CDI_WORDS);
|
2022-12-09 02:32:04 -05:00
|
|
|
if (!memeq(cdi_writetest, cdi_readback, CDI_WORDS * 4)) {
|
2022-12-02 10:11:53 -05:00
|
|
|
puts("FAIL: Can't write CDI in fw mode\r\n");
|
2022-09-19 02:51:11 -04:00
|
|
|
anyfailed = 1;
|
|
|
|
}
|
|
|
|
|
2022-10-13 07:16:11 -04:00
|
|
|
// Test FW-RAM.
|
2022-10-17 05:11:54 -04:00
|
|
|
*fw_ram = 0x12;
|
|
|
|
if (*fw_ram != 0x12) {
|
2022-12-02 10:11:53 -05:00
|
|
|
puts("FAIL: Can't write and read FW RAM in fw mode\r\n");
|
2022-10-17 05:11:54 -04:00
|
|
|
anyfailed = 1;
|
2022-10-13 07:16:11 -04:00
|
|
|
}
|
|
|
|
|
2022-10-26 05:38:58 -04:00
|
|
|
uint32_t sw = *switch_app;
|
|
|
|
if (sw != 0) {
|
2022-12-02 10:11:53 -05:00
|
|
|
puts("FAIL: switch_app is not 0 in fw mode\r\n");
|
2022-10-26 05:38:58 -04:00
|
|
|
anyfailed = 1;
|
|
|
|
}
|
|
|
|
|
2022-12-09 11:34:17 -05:00
|
|
|
// Store function pointer to blake2s() so it's reachable from app
|
|
|
|
*fw_blake2s_addr = (uint32_t)blake2s;
|
|
|
|
|
2022-10-13 07:16:11 -04:00
|
|
|
// Turn on application mode.
|
|
|
|
// -------------------------
|
2023-03-02 12:20:28 -05:00
|
|
|
|
|
|
|
// Set up another stack because fw_ram is not available
|
|
|
|
// anymore in app_mode.
|
2023-03-07 09:03:59 -05:00
|
|
|
asm volatile("li sp, 0x40006ff0");
|
2023-03-02 12:20:28 -05:00
|
|
|
|
2022-09-19 02:51:11 -04:00
|
|
|
*switch_app = 1;
|
|
|
|
|
2022-10-26 05:38:58 -04:00
|
|
|
sw = *switch_app;
|
|
|
|
if (sw != 0xffffffff) {
|
2022-12-02 10:11:53 -05:00
|
|
|
puts("FAIL: switch_app is not 0xffffffff in app mode\r\n");
|
2022-10-26 05:38:58 -04:00
|
|
|
anyfailed = 1;
|
|
|
|
}
|
|
|
|
|
2022-09-19 02:51:11 -04:00
|
|
|
// Should NOT be able to read from UDS in app-mode.
|
2023-03-01 08:00:27 -05:00
|
|
|
wordcpy_s(uds_local, UDS_WORDS, (void *)uds, UDS_WORDS);
|
2022-12-09 02:32:04 -05:00
|
|
|
if (!memeq(uds_local, zeros, UDS_WORDS * 4)) {
|
2022-12-02 10:11:53 -05:00
|
|
|
puts("FAIL: Read from UDS in app-mode\r\n");
|
2022-09-19 02:51:11 -04:00
|
|
|
anyfailed = 1;
|
|
|
|
}
|
|
|
|
|
2022-12-09 02:32:17 -05:00
|
|
|
// Should NOT be able to read from UDI in app-mode.
|
2023-03-01 08:00:27 -05:00
|
|
|
wordcpy_s(udi_local, UDI_WORDS, (void *)udi, UDI_WORDS);
|
2022-12-09 02:32:17 -05:00
|
|
|
if (!memeq(udi_local, zeros, UDI_WORDS * 4)) {
|
|
|
|
puts("FAIL: Read from UDI in app-mode\r\n");
|
|
|
|
anyfailed = 1;
|
|
|
|
}
|
|
|
|
|
2022-12-09 02:32:04 -05:00
|
|
|
uint32_t cdi_local[CDI_WORDS];
|
|
|
|
uint32_t cdi_local2[CDI_WORDS];
|
2023-03-01 08:00:27 -05:00
|
|
|
wordcpy_s(cdi_local, CDI_WORDS, (void *)cdi, CDI_WORDS);
|
2022-12-09 02:32:04 -05:00
|
|
|
|
2022-09-19 02:51:11 -04:00
|
|
|
// Write to CDI should NOT have any effect in app mode.
|
2023-03-01 08:00:27 -05:00
|
|
|
wordcpy_s((void *)cdi, CDI_WORDS, zeros, CDI_WORDS);
|
|
|
|
wordcpy_s(cdi_local2, CDI_WORDS, (void *)cdi, CDI_WORDS);
|
2022-12-09 02:32:04 -05:00
|
|
|
if (!memeq(cdi_local, cdi_local2, CDI_WORDS * 4)) {
|
2022-12-02 10:11:53 -05:00
|
|
|
puts("FAIL: Write to CDI in app-mode\r\n");
|
2022-09-19 02:51:11 -04:00
|
|
|
anyfailed = 1;
|
|
|
|
}
|
|
|
|
|
2022-10-13 07:16:11 -04:00
|
|
|
// Test FW-RAM.
|
2022-10-17 05:11:54 -04:00
|
|
|
*fw_ram = 0x21;
|
|
|
|
if (*fw_ram == 0x21) {
|
2022-12-02 10:11:53 -05:00
|
|
|
puts("FAIL: Write and read FW RAM in app-mode\r\n");
|
2022-10-17 05:11:54 -04:00
|
|
|
anyfailed = 1;
|
2022-10-13 07:16:11 -04:00
|
|
|
}
|
|
|
|
|
2023-01-30 04:33:02 -05:00
|
|
|
puts("Testing timer... 3");
|
2022-10-19 12:03:21 -04:00
|
|
|
// Matching clock at 18 MHz, giving us timer in seconds
|
|
|
|
*timer_prescaler = 18 * 1000000;
|
|
|
|
|
|
|
|
// Test timer expiration after 1s
|
|
|
|
*timer = 1;
|
2023-01-30 04:33:02 -05:00
|
|
|
// Start the timer
|
|
|
|
*timer_ctrl = (1 << TK1_MMIO_TIMER_CTRL_START_BIT);
|
|
|
|
while (*timer_status & (1 << TK1_MMIO_TIMER_STATUS_RUNNING_BIT)) {
|
2022-10-19 12:03:21 -04:00
|
|
|
}
|
2023-01-30 04:33:02 -05:00
|
|
|
// Now timer has expired and is ready to run again
|
|
|
|
puts(" 2");
|
2022-10-19 12:03:21 -04:00
|
|
|
|
|
|
|
// Test to interrupt a timer - and reads from timer register
|
|
|
|
// Starting 10s timer and interrupting it in 3s...
|
|
|
|
*timer = 10;
|
2023-01-30 04:33:02 -05:00
|
|
|
*timer_ctrl = (1 << TK1_MMIO_TIMER_CTRL_START_BIT);
|
2022-10-19 12:03:21 -04:00
|
|
|
uint32_t last_timer = 10;
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
last_timer = wait_timer_tick(last_timer);
|
|
|
|
}
|
|
|
|
|
2023-01-30 04:33:02 -05:00
|
|
|
// Stop the timer
|
|
|
|
*timer_ctrl = (1 << TK1_MMIO_TIMER_CTRL_STOP_BIT);
|
|
|
|
puts(" 1. done.\r\n");
|
|
|
|
|
|
|
|
if (*timer_status & (1 << TK1_MMIO_TIMER_STATUS_RUNNING_BIT)) {
|
2022-12-02 10:11:53 -05:00
|
|
|
puts("FAIL: Timer didn't stop\r\n");
|
2022-10-19 12:03:21 -04:00
|
|
|
anyfailed = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*timer != 10) {
|
2022-12-02 10:11:53 -05:00
|
|
|
puts("FAIL: Timer didn't reset to 10\r\n");
|
2022-10-19 12:03:21 -04:00
|
|
|
anyfailed = 1;
|
|
|
|
}
|
|
|
|
|
2022-12-09 11:34:17 -05:00
|
|
|
// Testing the blake2s MMIO in app mode
|
|
|
|
|
|
|
|
fw_blake2s = (volatile int (*)(void *, unsigned long, const void *,
|
|
|
|
unsigned long, const void *,
|
|
|
|
unsigned long, blake2s_ctx *)) *
|
|
|
|
fw_blake2s_addr;
|
|
|
|
|
|
|
|
char msg[17] = "dldlkjsdkljdslsdj";
|
|
|
|
uint32_t digest0[8];
|
|
|
|
uint32_t digest1[8];
|
|
|
|
blake2s_ctx b2s_ctx;
|
|
|
|
|
|
|
|
blake2s(&digest0[0], 32, NULL, 0, &msg, 17, &b2s_ctx);
|
|
|
|
fw_blake2s(&digest1[0], 32, NULL, 0, &msg, 17, &b2s_ctx);
|
|
|
|
|
|
|
|
puts("digest #0: \r\n");
|
|
|
|
hexdump((uint8_t *)digest0, 32);
|
|
|
|
|
|
|
|
puts("digest #1: \r\n");
|
|
|
|
hexdump((uint8_t *)digest1, 32);
|
|
|
|
|
|
|
|
if (!memeq(digest0, digest1, 32)) {
|
|
|
|
puts("FAIL: Digests not the same\r\n");
|
|
|
|
anyfailed = 1;
|
|
|
|
}
|
|
|
|
|
2022-10-13 07:16:11 -04:00
|
|
|
// Check and display test results.
|
2022-09-19 02:51:11 -04:00
|
|
|
if (anyfailed) {
|
2022-12-02 10:11:53 -05:00
|
|
|
puts("Some test FAILED!\r\n");
|
2022-09-19 02:51:11 -04:00
|
|
|
} else {
|
2022-12-02 10:11:53 -05:00
|
|
|
puts("All tests passed.\r\n");
|
2022-09-19 02:51:11 -04:00
|
|
|
}
|
|
|
|
|
2022-12-02 10:11:53 -05:00
|
|
|
puts("\r\nHere are 256 bytes from the TRNG:\r\n");
|
2022-09-30 09:41:16 -04:00
|
|
|
for (int j = 0; j < 8; j++) {
|
|
|
|
for (int i = 0; i < 8; i++) {
|
|
|
|
while ((*trng_status &
|
2022-10-20 08:50:21 -04:00
|
|
|
(1 << TK1_MMIO_TRNG_STATUS_READY_BIT)) == 0) {
|
2022-09-30 09:41:16 -04:00
|
|
|
}
|
|
|
|
uint32_t rnd = *trng_entropy;
|
2022-12-02 10:11:53 -05:00
|
|
|
puthexn((uint8_t *)&rnd, 4);
|
|
|
|
puts(" ");
|
2022-09-30 09:41:16 -04:00
|
|
|
}
|
2022-12-02 10:11:53 -05:00
|
|
|
puts("\r\n");
|
2022-09-30 09:41:16 -04:00
|
|
|
}
|
2022-12-02 10:11:53 -05:00
|
|
|
puts("\r\n");
|
2022-09-30 09:41:16 -04:00
|
|
|
|
2022-12-02 10:11:53 -05:00
|
|
|
puts("Now echoing what you type...\r\n");
|
2022-09-19 02:51:11 -04:00
|
|
|
for (;;) {
|
|
|
|
in = readbyte(); // blocks
|
|
|
|
writebyte(in);
|
|
|
|
}
|
|
|
|
}
|