fw: Prefix all HTIF console I/O functions with htif_

This commit is contained in:
Michael Cardell Widerkrantz 2022-12-02 15:09:10 +01:00
parent fbe71e1466
commit 65bc96a725
No known key found for this signature in database
GPG Key ID: D3DB3DDF57E704E5
5 changed files with 70 additions and 69 deletions

View File

@ -153,8 +153,9 @@ you can use with host software to talk to the firmware.
To quit QEMU you can use: `Ctrl-a x` (see `Ctrl-a ?` for other commands). To quit QEMU you can use: `Ctrl-a x` (see `Ctrl-a ?` for other commands).
Debugging? Use the HTIF console by removing `-DNOCONSOLE` from the Debugging? Use the HTIF console by removing `-DNOCONSOLE` from the
`CFLAGS` and using the helper functions in `lib.c` like `puts()` `CFLAGS` and using the helper functions in `lib.c` like `htif_puts()`
`putinthex()` `hexdump()` and friends for printf-like debugging. `htif_putinthex()` `htif_hexdump()` and friends for printf-like
debugging.
You can also use the qemu monitor for debugging, e.g. `info You can also use the qemu monitor for debugging, e.g. `info
registers`, or run qemu with `-d in_asm` or `-d trace:riscv_trap`. registers`, or run qemu with `-d in_asm` or `-d trace:riscv_trap`.

View File

@ -7,28 +7,28 @@
#include "types.h" #include "types.h"
#if NOCONSOLE #if NOCONSOLE
void putc(int ch) void htif_putc(int ch)
{ {
} }
void lf() void htif_lf()
{ {
} }
void puthex(uint8_t c) void htif_puthex(uint8_t c)
{ {
} }
void putinthex(const uint32_t n) void htif_putinthex(const uint32_t n)
{ {
} }
int puts(const char *s) int htif_puts(const char *s)
{ {
return 0; return 0;
} }
void hexdump(uint8_t *buf, int len) void htif_hexdump(uint8_t *buf, int len)
{ {
} }
@ -67,14 +67,14 @@ static int htif_putchar(int ch)
return ch & 0xff; return ch & 0xff;
} }
int puts(const char *s) int htif_puts(const char *s)
{ {
while (*s) while (*s)
htif_putchar(*s++); htif_putchar(*s++);
return 0; return 0;
} }
void hexdump(uint8_t *buf, int len) void htif_hexdump(uint8_t *buf, int len)
{ {
uint8_t *row; uint8_t *row;
uint8_t *byte; uint8_t *byte;
@ -84,24 +84,24 @@ void hexdump(uint8_t *buf, int len)
max = &buf[len]; max = &buf[len];
for (byte = 0; byte != max; row = byte) { for (byte = 0; byte != max; row = byte) {
for (byte = row; byte != max && byte != (row + 16); byte++) { for (byte = row; byte != max && byte != (row + 16); byte++) {
puthex(*byte); htif_puthex(*byte);
} }
lf(); htif_lf();
} }
} }
void putc(int ch) void htif_putc(int ch)
{ {
htif_putchar(ch); htif_putchar(ch);
} }
void lf() void htif_lf()
{ {
htif_putchar('\n'); htif_putchar('\n');
} }
void puthex(uint8_t c) void htif_puthex(uint8_t c)
{ {
unsigned int upper = (c >> 4) & 0xf; unsigned int upper = (c >> 4) & 0xf;
unsigned int lower = c & 0xf; unsigned int lower = c & 0xf;
@ -110,14 +110,14 @@ void puthex(uint8_t c)
htif_putchar(lower < 10 ? '0' + lower : 'a' - 10 + lower); htif_putchar(lower < 10 ? '0' + lower : 'a' - 10 + lower);
} }
void putinthex(const uint32_t n) void htif_putinthex(const uint32_t n)
{ {
uint8_t buf[4]; uint8_t buf[4];
memcpy(buf, &n, 4); memcpy(buf, &n, 4);
puts("0x"); htif_puts("0x");
for (int i = 3; i > -1; i--) { for (int i = 3; i > -1; i--) {
puthex(buf[i]); htif_puthex(buf[i]);
} }
} }
#endif #endif

View File

@ -8,12 +8,12 @@
#include "types.h" #include "types.h"
void putc(int ch); void htif_putc(int ch);
void lf(); void htif_lf();
void puthex(uint8_t c); void htif_puthex(uint8_t c);
void putinthex(const uint32_t n); void htif_putinthex(const uint32_t n);
int puts(const char *s); int htif_puts(const char *s);
void hexdump(uint8_t *buf, int len); void htif_hexdump(uint8_t *buf, int len);
void *memset(void *dest, int c, unsigned n); void *memset(void *dest, int c, unsigned n);
void *memcpy(void *dest, const void *src, unsigned n); void *memcpy(void *dest, const void *src, unsigned n);
void *wordcpy(void *dest, const void *src, unsigned n); void *wordcpy(void *dest, const void *src, unsigned n);

View File

@ -36,20 +36,20 @@ struct namever {
static void print_hw_version(struct namever namever) static void print_hw_version(struct namever namever)
{ {
puts("Hello, I'm "); htif_puts("Hello, I'm ");
hexdump((uint8_t *)&namever.name0, 4); htif_hexdump((uint8_t *)&namever.name0, 4);
putc(namever.name0[0]); htif_putc(namever.name0[0]);
putc(namever.name0[1]); htif_putc(namever.name0[1]);
putc(namever.name0[2]); htif_putc(namever.name0[2]);
putc(namever.name0[3]); htif_putc(namever.name0[3]);
putc('-'); htif_putc('-');
putc(namever.name1[0]); htif_putc(namever.name1[0]);
putc(namever.name1[1]); htif_putc(namever.name1[1]);
putc(namever.name1[2]); htif_putc(namever.name1[2]);
putc(namever.name1[3]); htif_putc(namever.name1[3]);
putc(':'); htif_putc(':');
putinthex(namever.version); htif_putinthex(namever.version);
lf(); htif_lf();
} }
static struct namever get_hw_version(uint32_t name0, uint32_t name1, static struct namever get_hw_version(uint32_t name0, uint32_t name1,
@ -57,9 +57,9 @@ static struct namever get_hw_version(uint32_t name0, uint32_t name1,
{ {
struct namever namever; struct namever namever;
hexdump((uint8_t *)&name0, 4); htif_hexdump((uint8_t *)&name0, 4);
putinthex(name0); htif_putinthex(name0);
lf(); htif_lf();
namever.name0[0] = name0 >> 24; namever.name0[0] = name0 >> 24;
namever.name0[1] = name0 >> 16; namever.name0[1] = name0 >> 16;
@ -78,14 +78,14 @@ static struct namever get_hw_version(uint32_t name0, uint32_t name1,
static void print_digest(uint8_t *md) static void print_digest(uint8_t *md)
{ {
puts("The app digest:\n"); htif_puts("The app digest:\n");
for (int j = 0; j < 4; j++) { for (int j = 0; j < 4; j++) {
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
puthex(md[i + 8 * j]); htif_puthex(md[i + 8 * j]);
} }
lf(); htif_lf();
} }
lf(); htif_lf();
} }
// CDI = blake2s(uds, blake2s(app), uss) // CDI = blake2s(uds, blake2s(app), uss)
@ -179,9 +179,9 @@ int main()
// Jump to app - doesn't return // Jump to app - doesn't return
// First clears memory of firmware remains // First clears memory of firmware remains
puts("Jumping to "); htif_puts("Jumping to ");
putinthex(*app_addr); htif_putinthex(*app_addr);
lf(); htif_lf();
// clang-format off // clang-format off
asm volatile( asm volatile(
// Clear the stack // Clear the stack
@ -200,9 +200,9 @@ int main()
break; // This is never reached! break; // This is never reached!
default: default:
puts("Unknown firmware state 0x"); htif_puts("Unknown firmware state 0x");
puthex(state); htif_puthex(state);
lf(); htif_lf();
forever_redflash(); forever_redflash();
break; // Not reached break; // Not reached
} }
@ -216,7 +216,7 @@ int main()
} }
if (parseframe(in, &hdr) == -1) { if (parseframe(in, &hdr) == -1) {
puts("Couldn't parse header\n"); htif_puts("Couldn't parse header\n");
continue; continue;
} }
@ -226,7 +226,7 @@ int main()
// Is it for us? // Is it for us?
if (hdr.endpoint != DST_FW) { if (hdr.endpoint != DST_FW) {
puts("Message not meant for us\n"); htif_puts("Message not meant for us\n");
continue; continue;
} }
@ -236,7 +236,7 @@ int main()
// Min length is 1 byte so this should always be here // Min length is 1 byte so this should always be here
switch (cmd[0]) { switch (cmd[0]) {
case FW_CMD_NAME_VERSION: case FW_CMD_NAME_VERSION:
puts("cmd: name-version\n"); htif_puts("cmd: name-version\n");
if (hdr.len != 1) { if (hdr.len != 1) {
// Bad length - give them an empty response // Bad length - give them an empty response
fwreply(hdr, FW_RSP_NAME_VERSION, rsp); fwreply(hdr, FW_RSP_NAME_VERSION, rsp);
@ -251,7 +251,7 @@ int main()
break; break;
case FW_CMD_GET_UDI: case FW_CMD_GET_UDI:
puts("cmd: get-udi\n"); htif_puts("cmd: get-udi\n");
if (hdr.len != 1) { if (hdr.len != 1) {
// Bad cmd length // Bad cmd length
rsp[0] = STATUS_BAD; rsp[0] = STATUS_BAD;
@ -268,7 +268,7 @@ int main()
break; break;
case FW_CMD_LOAD_APP: case FW_CMD_LOAD_APP:
puts("cmd: load-app(size, uss)\n"); htif_puts("cmd: load-app(size, uss)\n");
if (hdr.len != 128) { if (hdr.len != 128) {
// Bad length // Bad length
rsp[0] = STATUS_BAD; rsp[0] = STATUS_BAD;
@ -281,9 +281,9 @@ int main()
(cmd[3] << 16) + (cmd[3] << 16) +
(cmd[4] << 24); (cmd[4] << 24);
puts("app size: "); htif_puts("app size: ");
putinthex(local_app_size); htif_putinthex(local_app_size);
lf(); htif_lf();
if (local_app_size == 0 || if (local_app_size == 0 ||
local_app_size > TK1_APP_MAX_SIZE) { local_app_size > TK1_APP_MAX_SIZE) {
@ -310,7 +310,7 @@ int main()
break; break;
case FW_CMD_LOAD_APP_DATA: case FW_CMD_LOAD_APP_DATA:
puts("cmd: load-app-data\n"); htif_puts("cmd: load-app-data\n");
if (hdr.len != 128 || (state != FW_STATE_INIT_LOADING && if (hdr.len != 128 || (state != FW_STATE_INIT_LOADING &&
state != FW_STATE_LOADING)) { state != FW_STATE_LOADING)) {
// Bad cmd length or state // Bad cmd length or state
@ -330,9 +330,9 @@ int main()
left -= nbytes; left -= nbytes;
if (left == 0) { if (left == 0) {
puts("Fully loaded "); htif_puts("Fully loaded ");
putinthex(*app_size); htif_putinthex(*app_size);
lf(); htif_lf();
// Compute Blake2S digest of the app, storing // Compute Blake2S digest of the app, storing
// it for FW_STATE_RUN // it for FW_STATE_RUN
@ -359,9 +359,9 @@ int main()
break; break;
default: default:
puts("Got unknown firmware cmd: 0x"); htif_puts("Got unknown firmware cmd: 0x");
puthex(cmd[0]); htif_puthex(cmd[0]);
lf(); htif_lf();
} }
} }

View File

@ -92,9 +92,9 @@ void fwreply(struct frame_header hdr, enum fwcmd rspcode, uint8_t *buf)
break; break;
default: default:
puts("fwreply(): Unknown response code: 0x"); htif_puts("fwreply(): Unknown response code: 0x");
puthex(rspcode); htif_puthex(rspcode);
lf(); htif_lf();
return; return;
} }