diff --git a/hw/application_fpga/Makefile b/hw/application_fpga/Makefile index 18cf61e..14fe805 100644 --- a/hw/application_fpga/Makefile +++ b/hw/application_fpga/Makefile @@ -90,7 +90,8 @@ FIRMWARE_DEPS = \ $(P)/fw/tk1/flash.h \ $(P)/fw/tk1/partition_table.h \ $(P)/fw/tk1/preload_app.h \ - $(P)/fw/tk1/auth_app.h + $(P)/fw/tk1/auth_app.h \ + $(P)/fw/tk1/htif.h FIRMWARE_OBJS = \ $(P)/fw/tk1/main.o \ @@ -104,7 +105,8 @@ FIRMWARE_OBJS = \ $(P)/fw/tk1/flash.o \ $(P)/fw/tk1/partition_table.o \ $(P)/fw/tk1/preload_app.o \ - $(P)/fw/tk1/auth_app.o + $(P)/fw/tk1/auth_app.o \ + $(P)/fw/tk1/htif.o FIRMWARE_SOURCES = \ $(P)/fw/tk1/main.c \ @@ -117,7 +119,8 @@ FIRMWARE_SOURCES = \ $(P)/fw/tk1/flash.c \ $(P)/fw/tk1/partition_table.c \ $(P)/fw/tk1/preload_app.c \ - $(P)/fw/tk1/auth_app.c + $(P)/fw/tk1/auth_app.c \ + $(P)/fw/tk1/htif.c TESTFW_OBJS = \ $(P)/fw/testfw/main.o \ diff --git a/hw/application_fpga/fw/tk1/assert.c b/hw/application_fpga/fw/tk1/assert.c index 3da2d59..379b6e6 100644 --- a/hw/application_fpga/fw/tk1/assert.c +++ b/hw/application_fpga/fw/tk1/assert.c @@ -4,6 +4,7 @@ */ #include "assert.h" +#include "htif.h" #include "lib.h" void assert_fail(const char *assertion, const char *file, unsigned int line, diff --git a/hw/application_fpga/fw/tk1/htif.c b/hw/application_fpga/fw/tk1/htif.c new file mode 100644 index 0000000..852bebc --- /dev/null +++ b/hw/application_fpga/fw/tk1/htif.c @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2022 - Tillitis AB + * SPDX-License-Identifier: GPL-2.0-only + */ + +#include "htif.h" + +#include + +#ifdef QEMU_CONSOLE +struct { + uint32_t arr[2]; +} static volatile tohost __attribute__((section(".htif"))); +struct { + uint32_t arr[2]; +} /*@unused@*/ static volatile fromhost __attribute__((section(".htif"))); + +static void htif_send(uint8_t dev, uint8_t cmd, int64_t data) +{ + /* endian neutral encoding with ordered 32-bit writes */ + union { + uint32_t arr[2]; + uint64_t val; + } encode = {.val = (uint64_t)dev << 56 | (uint64_t)cmd << 48 | data}; + tohost.arr[0] = encode.arr[0]; + tohost.arr[1] = encode.arr[1]; +} + +static void htif_set_tohost(uint8_t dev, uint8_t cmd, int64_t data) +{ + /* send data with specified device and command */ + while (tohost.arr[0]) { +#ifndef S_SPLINT_S + asm volatile("" : : "r"(fromhost.arr[0])); + asm volatile("" : : "r"(fromhost.arr[1])); +#endif + } + htif_send(dev, cmd, data); +} + +static void htif_putchar(char ch) +{ + htif_set_tohost((uint8_t)1, (uint8_t)1, (int64_t)ch & 0xff); +} + +void htif_puts(const char *s) +{ + while (*s != '\0') + htif_putchar(*s++); +} + +void htif_hexdump(void *buf, int len) +{ + uint8_t *byte_buf = (uint8_t *)buf; + + for (int i = 0; i < len; i++) { + htif_puthex(byte_buf[i]); + if (i % 2 == 1) { + (void)htif_putchar(' '); + } + + if (i != 1 && i % 16 == 1) { + htif_lf(); + } + } + + htif_lf(); +} + +void htif_putc(char ch) +{ + htif_putchar(ch); +} + +void htif_lf(void) +{ + htif_putchar('\n'); +} + +void htif_puthex(uint8_t c) +{ + unsigned int upper = (c >> 4) & 0xf; + unsigned int lower = c & 0xf; + + htif_putchar(upper < 10 ? '0' + upper : 'a' - 10 + upper); + htif_putchar(lower < 10 ? '0' + lower : 'a' - 10 + lower); +} + +void htif_putinthex(const uint32_t n) +{ + uint8_t *buf = (uint8_t *)&n; + + htif_puts("0x"); + for (int i = 3; i > -1; i--) { + htif_puthex(buf[i]); + } +} +#endif diff --git a/hw/application_fpga/fw/tk1/htif.h b/hw/application_fpga/fw/tk1/htif.h new file mode 100644 index 0000000..2295994 --- /dev/null +++ b/hw/application_fpga/fw/tk1/htif.h @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2022 - Tillitis AB + * SPDX-License-Identifier: GPL-2.0-only + */ + +#ifndef HTIF_H +#define HTIF_H + +#include + +#ifdef QEMU_CONSOLE +void htif_putc(char ch); +void htif_lf(void); +void htif_puthex(uint8_t c); +void htif_putinthex(const uint32_t n); +void htif_puts(const char *s); +void htif_hexdump(void *buf, int len); +#else +#define htif_putc(ch) +#define htif_lf(void) +#define htif_puthex(c) +#define htif_putinthex(n) +#define htif_puts(s) +#define htif_hexdump(buf, len) +#endif // #ifdef NOCONSOLE + +#endif diff --git a/hw/application_fpga/fw/tk1/lib.c b/hw/application_fpga/fw/tk1/lib.c index c992be7..cfe5a2f 100644 --- a/hw/application_fpga/fw/tk1/lib.c +++ b/hw/application_fpga/fw/tk1/lib.c @@ -8,96 +8,6 @@ #include #include -#ifdef QEMU_CONSOLE -struct { - uint32_t arr[2]; -} static volatile tohost __attribute__((section(".htif"))); -struct { - uint32_t arr[2]; -} /*@unused@*/ static volatile fromhost __attribute__((section(".htif"))); - -static void htif_send(uint8_t dev, uint8_t cmd, int64_t data) -{ - /* endian neutral encoding with ordered 32-bit writes */ - union { - uint32_t arr[2]; - uint64_t val; - } encode = {.val = (uint64_t)dev << 56 | (uint64_t)cmd << 48 | data}; - tohost.arr[0] = encode.arr[0]; - tohost.arr[1] = encode.arr[1]; -} - -static void htif_set_tohost(uint8_t dev, uint8_t cmd, int64_t data) -{ - /* send data with specified device and command */ - while (tohost.arr[0]) { -#ifndef S_SPLINT_S - asm volatile("" : : "r"(fromhost.arr[0])); - asm volatile("" : : "r"(fromhost.arr[1])); -#endif - } - htif_send(dev, cmd, data); -} - -static void htif_putchar(char ch) -{ - htif_set_tohost((uint8_t)1, (uint8_t)1, (int64_t)ch & 0xff); -} - -void htif_puts(const char *s) -{ - while (*s != '\0') - htif_putchar(*s++); -} - -void htif_hexdump(void *buf, int len) -{ - uint8_t *byte_buf = (uint8_t *)buf; - - for (int i = 0; i < len; i++) { - htif_puthex(byte_buf[i]); - if (i % 2 == 1) { - (void)htif_putchar(' '); - } - - if (i != 1 && i % 16 == 1) { - htif_lf(); - } - } - - htif_lf(); -} - -void htif_putc(char ch) -{ - htif_putchar(ch); -} - -void htif_lf(void) -{ - htif_putchar('\n'); -} - -void htif_puthex(uint8_t c) -{ - unsigned int upper = (c >> 4) & 0xf; - unsigned int lower = c & 0xf; - - htif_putchar(upper < 10 ? '0' + upper : 'a' - 10 + upper); - htif_putchar(lower < 10 ? '0' + lower : 'a' - 10 + lower); -} - -void htif_putinthex(const uint32_t n) -{ - uint8_t *buf = (uint8_t *)&n; - - htif_puts("0x"); - for (int i = 3; i > -1; i--) { - htif_puthex(buf[i]); - } -} -#endif - void *memset(void *dest, int c, unsigned n) { uint8_t *s = dest; diff --git a/hw/application_fpga/fw/tk1/lib.h b/hw/application_fpga/fw/tk1/lib.h index 18da2e2..2a1a608 100644 --- a/hw/application_fpga/fw/tk1/lib.h +++ b/hw/application_fpga/fw/tk1/lib.h @@ -9,25 +9,10 @@ #include #include -#ifdef QEMU_CONSOLE -void htif_putc(char ch); -void htif_lf(void); -void htif_puthex(uint8_t c); -void htif_putinthex(const uint32_t n); -void htif_puts(const char *s); -void htif_hexdump(void *buf, int len); -#else -#define htif_putc(ch) -#define htif_lf(void) -#define htif_puthex(c) -#define htif_putinthex(n) -#define htif_puts(s) -#define htif_hexdump(buf, len) -#endif /* QEMU_CONSOLE */ - void *memset(void *dest, int c, unsigned n); void memcpy_s(void *dest, size_t destsize, const void *src, size_t n); void wordcpy_s(void *dest, size_t destsize, const void *src, size_t n); int memeq(void *dest, const void *src, size_t n); void secure_wipe(void *v, size_t n); + #endif diff --git a/hw/application_fpga/fw/tk1/main.c b/hw/application_fpga/fw/tk1/main.c index bd7784e..41e1ead 100644 --- a/hw/application_fpga/fw/tk1/main.c +++ b/hw/application_fpga/fw/tk1/main.c @@ -6,6 +6,7 @@ #include "../tk1_mem.h" #include "assert.h" #include "blake2s/blake2s.h" +#include "htif.h" #include "lib.h" #include "partition_table.h" #include "preload_app.h" diff --git a/hw/application_fpga/fw/tk1/proto.c b/hw/application_fpga/fw/tk1/proto.c index de13fb2..97ae5ef 100644 --- a/hw/application_fpga/fw/tk1/proto.c +++ b/hw/application_fpga/fw/tk1/proto.c @@ -6,6 +6,7 @@ #include "proto.h" #include "../tk1_mem.h" #include "assert.h" +#include "htif.h" #include "led.h" #include "lib.h" #include "state.h"