testfw: Add support for USB Mode Protocol

This commit is contained in:
Michael Cardell Widerkrantz 2025-02-10 17:20:01 +01:00
parent f68414c4aa
commit aedd6102ea
No known key found for this signature in database
GPG Key ID: D3DB3DDF57E704E5
2 changed files with 69 additions and 16 deletions

View File

@ -142,6 +142,7 @@ TESTFW_OBJS = \
$(P)/fw/testfw/start.o \ $(P)/fw/testfw/start.o \
$(P)/fw/tk1/proto.o \ $(P)/fw/tk1/proto.o \
$(P)/fw/tk1/lib.o \ $(P)/fw/tk1/lib.o \
$(P)/fw/tk1/assert.o \
$(P)/fw/tk1/blake2s/blake2s.o $(P)/fw/tk1/blake2s/blake2s.o
#------------------------------------------------------------------- #-------------------------------------------------------------------

View File

@ -3,12 +3,15 @@
* SPDX-License-Identifier: GPL-2.0-only * SPDX-License-Identifier: GPL-2.0-only
*/ */
#include "../tk1/assert.h"
#include "../tk1/blake2s/blake2s.h" #include "../tk1/blake2s/blake2s.h"
#include "../tk1/lib.h" #include "../tk1/lib.h"
#include "../tk1/proto.h" #include "../tk1/proto.h"
#include "../tk1/types.h" #include "../tk1/types.h"
#include "../tk1_mem.h" #include "../tk1_mem.h"
#define USBMODE_PACKET_SIZE 64
// clang-format off // clang-format off
volatile uint32_t *tk1name0 = (volatile uint32_t *)TK1_MMIO_TK1_NAME0; 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 *tk1name1 = (volatile uint32_t *)TK1_MMIO_TK1_NAME1;
@ -42,26 +45,68 @@ void *memcpy(void *dest, const void *src, size_t n)
return dest; return dest;
} }
void puts(char *reason) static void write_with_header(const uint8_t *buf, size_t nbytes, enum mode mode)
{ {
for (char *c = reason; *c != '\0'; c++) { // Append USB Mode Protocol header:
writebyte(*c); // 1 byte mode
// 1 byte length
writebyte(mode);
writebyte(nbytes);
for (int i = 0; i < nbytes; i++) {
writebyte(buf[i]);
} }
} }
void putsn(char *p, int n) static void write(const uint8_t *buf, size_t nbytes)
{ {
for (int i = 0; i < n; i++) { uint8_t len;
writebyte(p[i]);
while (nbytes > 0) {
// We split the data into chunks that will fit in the
// USB Mode Protocol with some spare change.
len =
nbytes < USBMODE_PACKET_SIZE ? nbytes : USBMODE_PACKET_SIZE;
write_with_header((const uint8_t *)buf, len, MODE_CDC);
buf += len;
nbytes -= len;
} }
} }
unsigned strlen(const char *str)
{
const char *s;
for (s = str; *s; ++s)
;
return (s - str);
}
void puts(char *buf)
{
size_t nbytes = strlen(buf);
write((const uint8_t *)buf, nbytes);
}
void hex(uint8_t buf[2], const uint8_t c)
{
unsigned int upper = (c >> 4) & 0xf;
unsigned int lower = c & 0xf;
buf[0] = upper < 10 ? '0' + upper : 'a' - 10 + upper;
buf[1] = lower < 10 ? '0' + lower : 'a' - 10 + lower;
}
void puthex(uint8_t c) void puthex(uint8_t c)
{ {
unsigned int upper = (c >> 4) & 0xf; uint8_t buf[2];
unsigned int lower = c & 0xf;
writebyte(upper < 10 ? '0' + upper : 'a' - 10 + upper); hex(buf, c);
writebyte(lower < 10 ? '0' + lower : 'a' - 10 + lower); write(buf, 2);
} }
void puthexn(uint8_t *p, int n) void puthexn(uint8_t *p, int n)
@ -78,7 +123,7 @@ void hexdump(void *buf, int len)
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
puthex(byte_buf[i]); puthex(byte_buf[i]);
if (i % 2 == 1) { if (i % 2 == 1) {
writebyte(' '); puts(" ");
} }
if (i != 1 && i % 16 == 1) { if (i != 1 && i % 16 == 1) {
@ -156,7 +201,10 @@ int main(void)
unsigned long, const void *, unsigned long, unsigned long, const void *, unsigned long,
blake2s_ctx *); blake2s_ctx *);
uint8_t in; uint8_t in = 0;
uint8_t mode = 0;
uint8_t mode_bytes_left = 0;
// Hard coded test UDS in ../../data/uds.hex // Hard coded test UDS in ../../data/uds.hex
// clang-format off // clang-format off
uint32_t uds_test[8] = { uint32_t uds_test[8] = {
@ -172,18 +220,18 @@ int main(void)
// clang-format on // clang-format on
// Wait for terminal program and a character to be typed // Wait for terminal program and a character to be typed
in = readbyte(); in = readbyte(&mode, &mode_bytes_left);
puts("\r\nI'm testfw on:"); puts("\r\nI'm testfw on:");
// Output the TK1 core's NAME0 and NAME1 // Output the TK1 core's NAME0 and NAME1
uint32_t name; uint32_t name;
wordcpy_s(&name, 1, (void *)tk1name0, 1); wordcpy_s(&name, 1, (void *)tk1name0, 1);
reverseword(&name); reverseword(&name);
putsn((char *)&name, 4); write((const uint8_t *)&name, 4);
puts(" "); puts(" ");
wordcpy_s(&name, 1, (void *)tk1name1, 1); wordcpy_s(&name, 1, (void *)tk1name1, 1);
reverseword(&name); reverseword(&name);
putsn((char *)&name, 4); write((const uint8_t *)&name, 4);
puts("\r\n"); puts("\r\n");
uint32_t zeros[8]; uint32_t zeros[8];
@ -381,6 +429,7 @@ int main(void)
} }
puts("\r\nHere are 256 bytes from the TRNG:\r\n"); puts("\r\nHere are 256 bytes from the TRNG:\r\n");
for (int j = 0; j < 8; j++) { for (int j = 0; j < 8; j++) {
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
while ((*trng_status & while ((*trng_status &
@ -396,7 +445,10 @@ int main(void)
puts("Now echoing what you type...\r\n"); puts("Now echoing what you type...\r\n");
for (;;) { for (;;) {
in = readbyte(); // blocks in = readbyte(&mode, &mode_bytes_left);
writebyte(MODE_CDC);
writebyte(1);
writebyte(in); writebyte(in);
} }
} }