167 Commits

Author SHA1 Message Date
Mikael Ågren
04d54c9396
Add QEMU_SYSCALL to enable Qemu syscalls
Enable Qemu syscall handling by defining QEMU_SYSCALL instead of
QEMU_DEBUG. That way we can select either or.
2025-04-11 09:37:46 +02:00
Michael Cardell Widerkrantz
7cd1f03933
Add start of test app for installing and verifying flash app
Currently needs LIBDIR set to tkey-libs with blake2s().
2025-04-11 09:37:46 +02:00
Mikael Ågren
099fe46e1f
Do note delete or corrupt preloaded app 0 when storing preloaded app 1 2025-04-11 09:37:46 +02:00
Mikael Ågren
90da452f80
Delete app digest and signature when preloaded app is deleted 2025-04-11 09:37:45 +02:00
Mikael Ågren
9018ae5b33
Store app digest and signature for each app slot 2025-04-11 09:37:45 +02:00
Michael Cardell Widerkrantz
93a49f50c2
Introduce syscalls to change preloaded app
Introduce syscalls:

- TK1_SYSCALL_PRELOAD_STORE
- TK1_SYSCALL_PRELOAD_STORE_FIN
- TK1_SYSCALL_PRELOAD_DELETE
- TK1_SYSCALL_REG_MGMT = 11

Change preload_store_finalize() not to take USS arg. Unused for
preloaded apps.
2025-04-11 09:37:45 +02:00
Mikael Ågren
ac5c2aef86
fw: Add second pre-loaded app slot in flash 2025-04-11 09:37:44 +02:00
Mikael Ågren
1c4d11f49e
fw: When requested, verify app digest before running 2025-04-11 09:37:44 +02:00
Mikael Ågren
e4028d9a86
Handle reset info in reset syscall
Disabling debug printouts to get firmware to fit in ROM
2025-04-11 09:37:44 +02:00
Jonas Thörnblad
ba211212f0
Add resetinfo testapp
Co-authored-by: Mikael Ågren <mikael@tillitis.se>
Co-authored-by: Michael Cardell Widerkrantz <mc@tillitis.se>
2025-04-11 09:37:43 +02:00
Michael Cardell Widerkrantz
ba9e08fcdb
Add resetinfo handling
Decide where to start from with data from resetinfo part of FW_RAM.

Co-authored-by: Jonas Thörnblad <jonas@tillitis.se>
Co-authored-by: Mikael Ågren <mikael@tillitis.se>
2025-04-11 08:43:41 +02:00
Michael Cardell Widerkrantz
76b25bc121
Document state machine
Golden path not updated
2025-04-11 08:43:41 +02:00
Michael Cardell Widerkrantz
ab12c201c4
Experiment with new state machine 2025-04-11 08:43:41 +02:00
Michael Cardell Widerkrantz
6ef1b1c659
Experiment with state machine when starting from flash
- Move around code to start an app from flash.
- Mark experimental stuff and debug stuff more clearly.
2025-04-11 08:43:40 +02:00
Michael Cardell Widerkrantz
9316886c88
Introduce symbolic names for present and present & authenticated
A preloaded app can be:

- present and not yet authenticated (0x01)
- present and authenticated (0x02)

Let's use symbolic names for these.
2025-04-11 08:43:40 +02:00
Michael Cardell Widerkrantz
a20bcdab29
Make run_flash() and run() both call jump_to_app() 2025-04-11 08:43:40 +02:00
Michael Cardell Widerkrantz
820c273e7f
Add hardcoded preloaded app size
- Enable TKEY_DEBUG
- Wait for something on CDC before continuing
2025-04-11 08:43:39 +02:00
Mikael Ågren
c273727c7b
fw: Replace custom picorv32 instructions in qemu target 2025-04-08 11:00:59 +02:00
Michael Cardell Widerkrantz
f2b78be4cb
Add start of pre-loaded app from flash
Based on earlier code by Daniel Jobson <jobson@tillitis.se> now
integrated into the new world order.

Co-authored-by: Mikael Ågren <mikael@tillitis.se>
Co-authored-by: Daniel Jobson <jobson@tillitis.se>
2025-04-08 11:00:59 +02:00
Michael Cardell Widerkrantz
20b008d20e
Add filesystem code and storage syscalls
Adds syscalls:

- ALLOCATE_AREA
- DEALLOCATE_AREA
- WRITE_DATA
- READ_DATA

and code to access the filesystem and the flash over SPI.

Based on original work by Daniel Jobson <jobson@tillitis.see> for
these files:

- auth_app.[ch]
- flash.[ch]
- spi.[ch]
- partition_table.[ch]
- rng.[ch]
- storage.[ch]

which are used with small changes to integrate with the new syscall
method.

Co-authored-by: Daniel Jobson <jobson@tillitis.se>
Co-authored-by: Mikael Ågren <mikael@tillitis.se>
2025-04-08 11:00:59 +02:00
Michael Cardell Widerkrantz
bd2df9bd3f
Update firmware state machine
Include flash apps and states in firmware state machine description
and diagram.
2025-04-08 11:00:58 +02:00
Michael Cardell Widerkrantz
3ef1dade37
Clarify golden path
- Clarify what the default behaviour is.
- Clarify when we should halt CPU.
- Move common things when booting from flash and UART to its own
  section.
2025-04-08 11:00:58 +02:00
Michael Cardell Widerkrantz
aa9335691f
doc: Add description on how firmware starts app from flash
A first attempt at describing how to start an app from flash and how
to handle information left in resetinfo from the previous app in the
chain.
2025-04-08 11:00:54 +02:00
Michael Cardell Widerkrantz
d0c049cdba
fw/ch552: Document new dynamic endpoint functionality
The CH552 firmware has an added functionality to control the USB
controller dynamically, turning on and off endpoints with a small
protocol.

Since most of the documentation for the already lives in the ordinary
firmware README, add this documentation there, too.
2025-04-07 11:00:32 +02:00
Jonas Thörnblad
d43585ee1a
ch552: Add functionality to dynamically control USB endpoints
- Make it possible to enable and disable endpoints on demand
- Add internal FPGA<->CH552 communication channel (IO_CH552)
- Reorder IO endpoint numbering
- Rename endpoint from TKEYCTRL to DEBUG and update related variables
- Rename endpoint from HID to FIDO and update related variables
2025-04-07 11:00:31 +02:00
Michael Cardell Widerkrantz
3dbc31f54c
fw: Move tk1_mem.h to tkey-libs
From now on the canonical home of the tk1_mem.h header file describing
the memory map of the TKey lives in tkey-libs:

https://github.com/tillitis/tkey-libs
2025-03-13 11:07:47 +01:00
Michael Cardell Widerkrantz
cd1a089763
fw: Build with tkey-libs
Build firmware, testfw and testapp using tkey-libs:

  https://github.com/tillitis/tkey-libs

In an effort not to have more or less identical code maintained in two
places, use tkey-libs when developing firmware, testfw and the
firmware testapp, too.

You can place the Git directory directly under hw/application_fpga
and then an ordinary make should work.

Or build with:

  make LIBDIR=/path/to/tkey-libs

Co-authored-by: Mikael Ågren <mikael@tillitis.se>
2025-03-13 11:07:36 +01:00
Mikael Ågren
c52442b54c
doc: Update documentation about syscalls
- Revise firmware implementation notes
- Document how to do fw syscalls
  - Document how to trigger a syscall function in the firmware, how to
    pass arguments, what the caller is responsible for and what is
    returned.
- Describe hardware syscall implementation
  - how the syscall interrupts are triggered,
  - the hardware privilege escalation,
  - the UDS protection.

Co-authored-by: Daniel Jobson <jobson@tillitis.se>
Co-authored-by: Michael Cardell Widerkrantz <mc@tillitis.se>
2025-02-27 14:35:22 +01:00
Mikael Ågren
77fc5cf578
fpga: Only allow system reset in firmware mode and syscalls 2025-02-27 14:29:07 +01:00
Mikael Ågren
9e317666d3
fpga/fw: Remove SYSTEM_MODE_CTRL register 2025-02-27 14:29:07 +01:00
Michael Cardell Widerkrantz
df04fd56dd
fpga/fw: Introduce syscall TK1_SYSCALL_GET_VIDPID
Introduce new syscall TK1_SYSCALL_GET_VIDPID to get Vendor ID and
Product ID from the protected Unique Device Identification number.

UDI is protected from device apps to protect the serial number, so
apps won't know the exact TKey they are running on other than the CDI.
It may, however, be important to know what *kind* of TKey they are
running on, so we want to expose the Vendor ID and Product ID.

- fpga: Allow UDI to be read when doing syscalls.
- Add the new syscall to firmware.
- Add test to testapp directly after negative test of reading UDI to
  read out VID/PID through a syscall.
2025-02-27 14:29:07 +01:00
Mikael Ågren
13f40561ab
testapp: Call reset syscall 2025-02-27 14:29:06 +01:00
Mikael Ågren
4ba164732d
testapp: Add syscalls 2025-02-27 14:29:06 +01:00
Mikael Ågren
fed9354fe9
testfw/testapp: Break out tests running in app mode into separate app
App mode can no longer be controlled from software. So the tests have to
run from firmware RAM.
2025-02-27 14:27:12 +01:00
Mikael Ågren
d82c3a706e
fw: Add syscalls
Adds:
- SYSCALL_RESET
- SYSCALL_SET_LED

Co-authored-by: Michael Cardell Widerkrantz <mc@tillitis.se>
2025-02-27 14:27:05 +01:00
Michael Cardell Widerkrantz
5eb020275b
fpga/fw/testfw: Remove Blake2s register
Since the introduction of the syscall mechanism we don't allow
execution in ROM anymore so it's impossible to call the firmware's
blake2s() function.

Co-authored-by: Mikael Ågren <mikael@tillitis.se>
2025-02-27 14:20:38 +01:00
Mikael Ågren
97de5e68fd
fpga/fw: Rename system_mode to app_mode
Rename `system_mode` to `app_mode` as to not confuse it with syscall or
firmware mode. When `app_mode` is `1`/`true` we are in app mode.
2025-02-27 14:20:37 +01:00
Jonas Thörnblad
8f2f312531
fpga/fw: Resize ROM and FW_RAM, add RESETINFO partition
In order to be able to leave data for firmware signalling the
intention with a reset or to leave data for the next app in a chain of
apps, we introduce a part of FW_RAM that can be used to store this
data. In order to do this, we:

- Change size of ROM from 6 KB to 8 KB.
- Change size of FW_RAM, from 2 KB to 4 KB.
- Add RESETINFO memory partition inside FW_RAM.
- Add generation of map file.
- Change CFLAGS from using -O2 to using -Os.
- Update address ranges for valid access to ROM and FW_RAM.
- Move stack to be located before data+bss and the RESETINFO data
  above them. This also means we introduce hardware stack overflow
  protection through the Security Monitor.
- Revise firmware README to the new use of FW_RAM.
2025-02-21 11:15:34 +01:00
Michael Cardell Widerkrantz
d2c7fb0ba9
doc: Update firmware README to include USB Mode Protocol
+ minor link and typo fixes.
2025-02-11 15:21:02 +01:00
Michael Cardell Widerkrantz
aedd6102ea
testfw: Add support for USB Mode Protocol 2025-02-11 14:10:57 +01:00
Jonas Thörnblad
04ec938200
ch552: Add new USB debug pipe (TKEYCTRL)
Make the CH552 present a new HID endpoint used for debug data.
2025-02-11 13:50:05 +01:00
Jonas Thörnblad
07dc20e4e1
fpga/testfw: Update clock frequency to 24 MHz
Reconfigure the baudrate to keep 500 kbaud.

Correct a forgotten test in testfw that wasn't updated the last time
frequency was raised in commit
75b028505f0d6dc685d37b84d73ddb9db5ee7ea2 in June 17, 2024.
2025-02-11 13:50:04 +01:00
Mikael Ågren
a0c031eb25
fw: Minimal CDC implementation of new framing protocol
Throwing away mode and length from incoming data. Adding mode and
length to outgoing data.

Splitting responses into frames small enough for the USB<->UART
transceiver to handle.
2025-02-11 13:50:03 +01:00
Jonas Thörnblad
0af82ee566
fpga/fw: Extend checks for invalid memory accesses
- Extend hardware checks for invalid memory accesses to include
  checking more address space.

- In fw include file: fix two typos for memory ranges that relates to
  above that fortunately have no impact on functionality.
2025-02-06 16:16:46 +01:00
Jonas Thörnblad
15ce2c438b
Add needed changes to firmware for simulation. 2024-11-28 16:10:00 +01:00
Daniel Jobson
3d7a97ecbc
fpga: remove the API for configuring the UART core
This removes the possibility to configure the bit rate, data bits and
stop bits at runtime from the API. This reduces the
usage of LCs with ~4%.

It is still possible to configure the core before building.

Update digest of application_fpga.bin.sha256
2024-11-26 15:24:12 +01:00
Jonas Thörnblad
aea2e319eb
Harmonize the naming of firmware and app mode.
- The API changes name from `_SWITCH_APP` to `_SYSTEM_MODE_CTRL`.
- The registers and wires changes name to `system_mode_*`, instead of a
  mix of `switch_app_*` and `fw_app_mode`.
2024-11-12 15:13:59 +01:00
Daniel Jobson
f13366538e
fw: Fix erroneous type in frame header 2024-10-09 15:52:00 +02:00
Daniel Jobson
81950ef7b2
fw: remove warning of missing prototypes when building with QEMU console
enabled.
2024-09-19 16:52:04 +02:00
Daniel Jobson
613316f53e
fw: simplify how to enable QEMU debug in firmware.
- Remove the define `NOCONSOLE`, add define `QEMU_CONSOLE`
- Inverse the use of it, add the define to have QEMU debug output in fw.
- Add a make target `qemu_firmware.elf` which builds the firmware with
  QEMU console enabled.

Co-authored-by: Mikael Ågren <mikael@tillitis.se>
2024-09-19 16:51:55 +02:00