diff --git a/hw/application_fpga/apps/Makefile b/hw/application_fpga/apps/Makefile new file mode 100644 index 0000000..b8ce38c --- /dev/null +++ b/hw/application_fpga/apps/Makefile @@ -0,0 +1,123 @@ +P := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))) +LIBDIR ?= ../tkey-libs +OBJCOPY ?= llvm-objcopy +CC = clang +CFLAGS = \ + -target riscv32-unknown-none-elf \ + -march=rv32iczmmul \ + -mabi=ilp32 \ + -mcmodel=medany \ + -static \ + -std=gnu99 \ + -Os \ + -ffast-math \ + -fno-common \ + -fno-builtin-printf \ + -fno-builtin-putchar \ + -fno-builtin-memcpy \ + -nostdlib \ + -mno-relax \ + -Wall \ + -Wpedantic \ + -Wno-language-extension-token \ + -Werror \ + -flto \ + -g \ + -I $(LIBDIR)/include \ + -I $(LIBDIR) \ + -I include \ + -I ../ + +AS = clang + +ASFLAGS = \ + -target riscv32-unknown-none-elf \ + -march=rv32iczmmul \ + -mabi=ilp32 \ + -mno-relax + +LDFLAGS = \ + -T $(LIBDIR)/app.lds \ + -L $(LIBDIR) -lcrt0 -lcommon -lmonocypher -lblake2s + +.PHONY: all +all: defaultapp.bin reset_test.bin testapp.bin testloadapp.bin + +# Turn elf into bin for device +%.bin: %.elf + $(OBJCOPY) --input-target=elf32-littleriscv --output-target=binary $^ $@ + chmod a-x $@ + +.PHONY: tkey-libs +tkey-libs: + make -C $(LIBDIR) + +OBJS=syscall.o + +# syscall.o: syscall.S +# $(CC) $(CFLAGS) $(DEFAULTAPP_OBJS) $(LDFLAGS) -o $@ + +# defaultapp +DEFAULTAPP_FMTFILES = *.[ch] + +DEFAULTAPP_OBJS = \ + $(P)/defaultapp/main.o + +defaultapp.elf: tkey-libs $(OBJS) $(DEFAULTAPP_OBJS) + $(CC) $(CFLAGS) $(OBJS) $(DEFAULTAPP_OBJS) $(LDFLAGS) -o $@ + +# reset_test + +RESET_TEST_FMTFILES = *.[ch] + +RESET_TEST_OBJS = \ + $(P)/reset_test/main.o + +reset_test.elf: tkey-libs $(RESET_TEST_OBJS) + $(CC) $(CFLAGS) $(OBJS) $(RESET_TEST_OBJS) $(LDFLAGS) -o $@ + +# testapp + +TESTAPP_OBJS = \ + $(P)/testapp/main.o + +testapp.elf: tkey-libs $(TESTAPP_OBJS) + $(CC) $(CFLAGS) $(OBJS) $(TESTAPP_OBJS) $(LDFLAGS) -o $@ + +# testloadapp + +TESTLOADAPP_OBJS = \ + $(P)/testloadapp/main.o + +testloadapp.elf: tkey-libs $(TESTLOADAPP_OBJS) + $(CC) $(CFLAGS) $(OBJS) $(TESTLOADAPP_OBJS) $(LDFLAGS) -o $@ + +.PHONY: fmt +fmt: + clang-format --dry-run --ferror-limit=0 defaultapp/*.[ch] + clang-format --verbose -i defaultapp/*.[ch] + + clang-format --dry-run --ferror-limit=0 reset_test/*.[ch] + clang-format --verbose -i reset_test/*.[ch] + + clang-format --dry-run --ferror-limit=0 testapp/*.[ch] + clang-format --verbose -i testapp/*.[ch] + + clang-format --dry-run --ferror-limit=0 testloadapp/*.[ch] + clang-format --verbose -i testloadapp/*.[ch] + +.PHONY: checkfmt +checkfmt: + clang-format --dry-run --ferror-limit=0 defaultapp/*.[ch] + + clang-format --dry-run --ferror-limit=0 reset_test/*.[ch] + + clang-format --dry-run --ferror-limit=0 testapp/*.[ch] + + clang-format --dry-run --ferror-limit=0 testloadapp/*.[ch] + +.PHONY: clean +clean: + rm -f *.elf *.bin $(OBJS) $(DEFAULTAPP_OBJS) $(RESET_TEST_OBJS) \ + $(TESTAPP_OBJS) $(TESTLOADAPP_OBJS) + diff --git a/hw/application_fpga/apps/README.md b/hw/application_fpga/apps/README.md index 1204bbd..ca69f88 100644 --- a/hw/application_fpga/apps/README.md +++ b/hw/application_fpga/apps/README.md @@ -1,12 +1,5 @@ # Test applications -All of these test apps are controlled through the USB CDC, typically -by running picocom or similar terminal program, like: - -``` -$ picocom /dev/ttyACM1 -``` - - `defaultapp`: Immediately resets the TKey with the intention to start an app from the client, replicating the behaviour of earlier generations. @@ -16,3 +9,28 @@ $ picocom /dev/ttyACM1 - `testloadapp`: Interactively test management app things like installing an app (hardcoded for a small happy blinking app, see `blink.h` for the entire binary!) and to test verified boot. + +## Build + +``` +$ make +``` + +will build all the .elf and .bin files on the top level. + +## Use + +Use `tkey-runapp` from +[tkey-devtools](https://github.com/tillitis/tkey-devtools) to load the +apps: + +``` +$ tkey-runapp testapp.bin +``` + +All of these test apps are controlled through the USB CDC, typically +by running picocom or similar terminal program, like: + +``` +$ picocom /dev/ttyACM1 +``` diff --git a/hw/application_fpga/apps/defaultapp/Makefile b/hw/application_fpga/apps/defaultapp/Makefile deleted file mode 100644 index fd904d3..0000000 --- a/hw/application_fpga/apps/defaultapp/Makefile +++ /dev/null @@ -1,73 +0,0 @@ -P := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))) -LIBDIR ?= ../../tkey-libs -OBJCOPY ?= llvm-objcopy -CC = clang -CFLAGS = \ - -target riscv32-unknown-none-elf \ - -march=rv32iczmmul \ - -mabi=ilp32 \ - -mcmodel=medany \ - -static \ - -std=gnu99 \ - -Os \ - -ffast-math \ - -fno-common \ - -fno-builtin-printf \ - -fno-builtin-putchar \ - -fno-builtin-memcpy \ - -nostdlib \ - -mno-relax \ - -Wall \ - -Wpedantic \ - -Wno-language-extension-token \ - -Werror \ - -flto \ - -g \ - -I $(LIBDIR)/include \ - -I $(LIBDIR) - -AS = clang - -ASFLAGS = \ - -target riscv32-unknown-none-elf \ - -march=rv32iczmmul \ - -mabi=ilp32 \ - -mno-relax - -LDFLAGS = \ - -T $(LIBDIR)/app.lds \ - -L $(LIBDIR) -lcrt0 -lcommon -lmonocypher -lblake2s - -.PHONY: all -all: defaultapp.bin - -# Turn elf into bin for device -%.bin: %.elf - $(OBJCOPY) --input-target=elf32-littleriscv --output-target=binary $^ $@ - chmod a-x $@ - -.PHONY: tkey-libs -tkey-libs: - make -C $(LIBDIR) - -DEFAULTAPP_FMTFILES = *.[ch] - -DEFAULTAPP_OBJS = \ - $(P)/main.o \ - ../testapp/syscall.o \ - -defaultapp.elf: tkey-libs $(DEFAULTAPP_OBJS) - $(CC) $(CFLAGS) $(DEFAULTAPP_OBJS) $(LDFLAGS) -o $@ - -.PHONY: fmt -fmt: - clang-format --dry-run --ferror-limit=0 $(DEFAULTAPP_FMTFILES) - clang-format --verbose -i $(DEFAULT_FMTFILES) - -.PHONY: checkfmt -checkfmt: - clang-format --dry-run --ferror-limit=0 --Werror $(DEFAULT_FMTFILES) - -.PHONY: clean -clean: - rm -f defaultapp.* $(DEFAULTAPP_OBJS) diff --git a/hw/application_fpga/apps/defaultapp/main.c b/hw/application_fpga/apps/defaultapp/main.c index a6c7eec..93dd91a 100644 --- a/hw/application_fpga/apps/defaultapp/main.c +++ b/hw/application_fpga/apps/defaultapp/main.c @@ -1,13 +1,12 @@ // Copyright (C) 2025 - Tillitis AB // SPDX-License-Identifier: GPL-2.0-only +#include +#include +#include #include #include -#include "../testapp/syscall.h" -#include "../tk1/reset.h" -#include "../tk1/syscall_num.h" - int main(void) { struct reset rst = {0}; diff --git a/hw/application_fpga/apps/testapp/syscall.h b/hw/application_fpga/apps/include/syscall.h similarity index 100% rename from hw/application_fpga/apps/testapp/syscall.h rename to hw/application_fpga/apps/include/syscall.h diff --git a/hw/application_fpga/apps/reset_test/Makefile b/hw/application_fpga/apps/reset_test/Makefile deleted file mode 100644 index eb6c9b3..0000000 --- a/hw/application_fpga/apps/reset_test/Makefile +++ /dev/null @@ -1,74 +0,0 @@ -P := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))) -LIBDIR ?= ../../tkey-libs -OBJCOPY ?= llvm-objcopy -CC = clang -CFLAGS = \ - -target riscv32-unknown-none-elf \ - -march=rv32iczmmul \ - -mabi=ilp32 \ - -mcmodel=medany \ - -static \ - -std=gnu99 \ - -O2 \ - -ffast-math \ - -fno-common \ - -fno-builtin-printf \ - -fno-builtin-putchar \ - -fno-builtin-memcpy \ - -nostdlib \ - -mno-relax \ - -Wall \ - -Wpedantic \ - -Wno-language-extension-token \ - -Werror \ - -flto \ - -g \ - -I $(LIBDIR)/include \ - -I $(LIBDIR) \ - -DTKEY_DEBUG - -AS = clang - -ASFLAGS = \ - -target riscv32-unknown-none-elf \ - -march=rv32iczmmul \ - -mabi=ilp32 \ - -mno-relax - -LDFLAGS = \ - -T $(LIBDIR)/app.lds \ - -L $(LIBDIR) -lcrt0 -lcommon - -.PHONY: all -all: reset_test.bin - -# Turn elf into bin for device -%.bin: %.elf - $(OBJCOPY) --input-target=elf32-littleriscv --output-target=binary $^ $@ - chmod a-x $@ - -.PHONY: tkey-libs -tkey-libs: - make -C $(LIBDIR) - -RESET_TEST_FMTFILES = *.[ch] - -RESET_TEST_OBJS = \ - $(P)/main.o \ - $(P)/syscall.o - -reset_test.elf: tkey-libs $(RESET_TEST_OBJS) - $(CC) $(CFLAGS) $(RESET_TEST_OBJS) $(LDFLAGS) -o $@ - -.PHONY: fmt -fmt: - clang-format --dry-run --ferror-limit=0 $(RESET_TEST_FMTFILES) - clang-format --verbose -i $(RESET_TEST_FMTFILES) - -.PHONY: checkfmt -checkfmt: - clang-format --dry-run --ferror-limit=0 --Werror $(RESET_TEST_FMTFILES) - -.PHONY: clean -clean: - rm -f reset_test.bin reset_test.elf $(RESET_TEST_OBJS) diff --git a/hw/application_fpga/apps/reset_test/main.c b/hw/application_fpga/apps/reset_test/main.c index 076114d..c3b3998 100644 --- a/hw/application_fpga/apps/reset_test/main.c +++ b/hw/application_fpga/apps/reset_test/main.c @@ -1,7 +1,11 @@ // Copyright (C) 2022, 2023 - Tillitis AB // SPDX-License-Identifier: GPL-2.0-only +#include +#include +#include #include +#include #include #include #include @@ -9,11 +13,6 @@ #include #include -#include "../testapp/syscall.h" -#include "../tk1/proto.h" -#include "../tk1/reset.h" -#include "../tk1/syscall_num.h" - // Converts a single hex character to its integer value static uint8_t hex_char_to_byte(uint8_t c) { diff --git a/hw/application_fpga/apps/reset_test/syscall.S b/hw/application_fpga/apps/reset_test/syscall.S deleted file mode 100644 index 5fc9b3f..0000000 --- a/hw/application_fpga/apps/reset_test/syscall.S +++ /dev/null @@ -1,85 +0,0 @@ -// SPDX-FileCopyrightText: 2024 Tillitis AB -// SPDX-License-Identifier: BSD-2-Clause - -#include "../tk1/picorv32/custom_ops.S" - - .section ".text" - .globl syscall - - -syscall: - // Save registers to stack - addi sp, sp, -32*4 - sw x0, 0*4(sp) - sw x1, 1*4(sp) - // x2 (sp) is assumed to be preserved by the interrupt handler. - sw x3, 3*4(sp) - sw x4, 4*4(sp) - sw x5, 5*4(sp) - sw x6, 6*4(sp) - sw x7, 7*4(sp) - sw x8, 8*4(sp) - sw x9, 9*4(sp) - // x10 (a0) will contain syscall return value. And should not be saved. - sw x11, 11*4(sp) - sw x12, 12*4(sp) - sw x13, 13*4(sp) - sw x14, 14*4(sp) - sw x15, 15*4(sp) - sw x16, 16*4(sp) - sw x17, 17*4(sp) - sw x18, 18*4(sp) - sw x19, 19*4(sp) - sw x20, 20*4(sp) - sw x21, 21*4(sp) - sw x22, 22*4(sp) - sw x23, 23*4(sp) - sw x24, 24*4(sp) - sw x25, 25*4(sp) - sw x26, 26*4(sp) - sw x27, 27*4(sp) - sw x28, 28*4(sp) - sw x29, 29*4(sp) - sw x30, 30*4(sp) - sw x31, 31*4(sp) - - // Trigger syscall interrupt - li t1, 0xe1000000 // Syscall interrupt trigger address - sw zero, 0(t1) // Trigger interrupt - - // Restore registers from stack - lw x0, 0*4(sp) - lw x1, 1*4(sp) - // x2 (sp) is assumed to be preserved by the interrupt handler. - lw x3, 3*4(sp) - lw x4, 4*4(sp) - lw x5, 5*4(sp) - lw x6, 6*4(sp) - lw x7, 7*4(sp) - lw x8, 8*4(sp) - lw x9, 9*4(sp) - // x10 (a0) contains syscall return value. And should not be destroyed. - lw x11, 11*4(sp) - lw x12, 12*4(sp) - lw x13, 13*4(sp) - lw x14, 14*4(sp) - lw x15, 15*4(sp) - lw x16, 16*4(sp) - lw x17, 17*4(sp) - lw x18, 18*4(sp) - lw x19, 19*4(sp) - lw x20, 20*4(sp) - lw x21, 21*4(sp) - lw x22, 22*4(sp) - lw x23, 23*4(sp) - lw x24, 24*4(sp) - lw x25, 25*4(sp) - lw x26, 26*4(sp) - lw x27, 27*4(sp) - lw x28, 28*4(sp) - lw x29, 29*4(sp) - lw x30, 30*4(sp) - lw x31, 31*4(sp) - addi sp, sp, 32*4 - - ret diff --git a/hw/application_fpga/apps/testapp/syscall.S b/hw/application_fpga/apps/syscall.S similarity index 97% rename from hw/application_fpga/apps/testapp/syscall.S rename to hw/application_fpga/apps/syscall.S index 5fc9b3f..17712ab 100644 --- a/hw/application_fpga/apps/testapp/syscall.S +++ b/hw/application_fpga/apps/syscall.S @@ -1,7 +1,7 @@ // SPDX-FileCopyrightText: 2024 Tillitis AB // SPDX-License-Identifier: BSD-2-Clause -#include "../tk1/picorv32/custom_ops.S" +#include "../fw/tk1/picorv32/custom_ops.S" .section ".text" .globl syscall diff --git a/hw/application_fpga/apps/testapp/Makefile b/hw/application_fpga/apps/testapp/Makefile deleted file mode 100644 index a83469f..0000000 --- a/hw/application_fpga/apps/testapp/Makefile +++ /dev/null @@ -1,73 +0,0 @@ -P := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))) -LIBDIR ?= ../../tkey-libs -OBJCOPY ?= llvm-objcopy -CC = clang -CFLAGS = \ - -target riscv32-unknown-none-elf \ - -march=rv32iczmmul \ - -mabi=ilp32 \ - -mcmodel=medany \ - -static \ - -std=gnu99 \ - -O2 \ - -ffast-math \ - -fno-common \ - -fno-builtin-printf \ - -fno-builtin-putchar \ - -fno-builtin-memcpy \ - -nostdlib \ - -mno-relax \ - -Wall \ - -Wpedantic \ - -Wno-language-extension-token \ - -Werror \ - -flto \ - -g \ - -I $(LIBDIR)/include \ - -I $(LIBDIR) - -AS = clang - -ASFLAGS = \ - -target riscv32-unknown-none-elf \ - -march=rv32iczmmul \ - -mabi=ilp32 \ - -mno-relax - -LDFLAGS = \ - -T $(LIBDIR)/app.lds \ - -L $(LIBDIR) -lcrt0 -lcommon - -.PHONY: all -all: testapp.bin - -# Turn elf into bin for device -%.bin: %.elf - $(OBJCOPY) --input-target=elf32-littleriscv --output-target=binary $^ $@ - chmod a-x $@ - -.PHONY: tkey-libs -tkey-libs: - make -C $(LIBDIR) - -TESTAPP_FMTFILES = *.[ch] - -TESTAPP_OBJS = \ - $(P)/main.o \ - $(P)/syscall.o - -testapp.elf: tkey-libs $(TESTAPP_OBJS) - $(CC) $(CFLAGS) $(TESTAPP_OBJS) $(LDFLAGS) -o $@ - -.PHONY: fmt -fmt: - clang-format --dry-run --ferror-limit=0 $(TESTAPP_FMTFILES) - clang-format --verbose -i $(TESTAPP_FMTFILES) - -.PHONY: checkfmt -checkfmt: - clang-format --dry-run --ferror-limit=0 --Werror $(TESTAPP_FMTFILES) - -.PHONY: clean -clean: - rm -f testapp.bin testapp.elf $(TESTAPP_OBJS) diff --git a/hw/application_fpga/apps/testapp/main.c b/hw/application_fpga/apps/testapp/main.c index 4407a04..a1adeee 100644 --- a/hw/application_fpga/apps/testapp/main.c +++ b/hw/application_fpga/apps/testapp/main.c @@ -1,6 +1,9 @@ // Copyright (C) 2022, 2023 - Tillitis AB // SPDX-License-Identifier: GPL-2.0-only +#include +#include +#include #include #include #include @@ -8,9 +11,6 @@ #include #include -#include "../tk1/proto.h" -#include "../tk1/reset.h" -#include "../tk1/syscall_num.h" #include "syscall.h" #define USBMODE_PACKET_SIZE 64 diff --git a/hw/application_fpga/apps/testloadapp/Makefile b/hw/application_fpga/apps/testloadapp/Makefile deleted file mode 100644 index 785ab37..0000000 --- a/hw/application_fpga/apps/testloadapp/Makefile +++ /dev/null @@ -1,73 +0,0 @@ -P := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))) -LIBDIR ?= ../../tkey-libs -OBJCOPY ?= llvm-objcopy -CC = clang -CFLAGS = \ - -target riscv32-unknown-none-elf \ - -march=rv32iczmmul \ - -mabi=ilp32 \ - -mcmodel=medany \ - -static \ - -std=gnu99 \ - -Os \ - -ffast-math \ - -fno-common \ - -fno-builtin-printf \ - -fno-builtin-putchar \ - -fno-builtin-memcpy \ - -nostdlib \ - -mno-relax \ - -Wall \ - -Wpedantic \ - -Wno-language-extension-token \ - -Werror \ - -flto \ - -g \ - -I $(LIBDIR)/include \ - -I $(LIBDIR) - -AS = clang - -ASFLAGS = \ - -target riscv32-unknown-none-elf \ - -march=rv32iczmmul \ - -mabi=ilp32 \ - -mno-relax - -LDFLAGS = \ - -T $(LIBDIR)/app.lds \ - -L $(LIBDIR) -lcrt0 -lcommon -lmonocypher -lblake2s - -.PHONY: all -all: testloadapp.bin - -# Turn elf into bin for device -%.bin: %.elf - $(OBJCOPY) --input-target=elf32-littleriscv --output-target=binary $^ $@ - chmod a-x $@ - -.PHONY: tkey-libs -tkey-libs: - make -C $(LIBDIR) - -TESTLOADAPP_FMTFILES = *.[ch] - -TESTLOADAPP_OBJS = \ - $(P)/main.o \ - ../testapp/syscall.o \ - -testloadapp.elf: tkey-libs $(TESTLOADAPP_OBJS) - $(CC) $(CFLAGS) $(TESTLOADAPP_OBJS) $(LDFLAGS) -o $@ - -.PHONY: fmt -fmt: - clang-format --dry-run --ferror-limit=0 $(TESTLOADAPP_FMTFILES) - clang-format --verbose -i $(TESTLOADAPP_FMTFILES) - -.PHONY: checkfmt -checkfmt: - clang-format --dry-run --ferror-limit=0 --Werror $(TESTLOADAPP_FMTFILES) - -.PHONY: clean -clean: - rm -f testloadapp.bin testloadapp.elf $(TESTLOADAPP_OBJS) diff --git a/hw/application_fpga/apps/testloadapp/main.c b/hw/application_fpga/apps/testloadapp/main.c index 5d28e09..98300ac 100644 --- a/hw/application_fpga/apps/testloadapp/main.c +++ b/hw/application_fpga/apps/testloadapp/main.c @@ -2,18 +2,18 @@ // SPDX-License-Identifier: GPL-2.0-only #include +#include +#include #include #include +#include #include #include #include #include -#include "../testapp/syscall.h" -#include "../tk1/reset.h" -#include "../tk1/syscall_num.h" #include "blink.h" -#include "tkey/assert.h" +#include "syscall.h" // clang-format off static volatile uint32_t *cdi = (volatile uint32_t *) TK1_MMIO_TK1_CDI_FIRST; @@ -157,7 +157,8 @@ void reset_from_client(void) // Give the next in chain something to look at. memset(rst.next_app_data, 17, sizeof(rst.next_app_data)); - syscall(TK1_SYSCALL_RESET, (uint32_t)&rst, sizeof(rst.next_app_data), 0); + syscall(TK1_SYSCALL_RESET, (uint32_t)&rst, sizeof(rst.next_app_data), + 0); } int main(void)