ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
main.c
Go to the documentation of this file.
1
25
26#include <stdint.h>
27#include <string.h>
28
29#include "mbedtls/memory_buffer_alloc.h"
30#include "ra8_board_ek_ra8d2.h"
31#include "ra8_boot_entry.h"
32#include "ra8_cgc.h"
33#include "ra8_err.h"
34#include "ra8_isr.h"
35#include "ra8_psa_crypto.h"
36#include "ra8_rot.h"
37#include "ra8_time.h"
38#include "rot_fixture.h"
39
41typedef enum : uint32_t {
42 k_rot_baud = 115200U,
44 k_rot_heap_bytes = 0x10000U,
46
48typedef enum : uint8_t {
50} rot_bit_t;
51
52static const uint8_t k_rot_msg_pass[] = "rot verify: PASS\r\n";
53static const uint8_t k_rot_msg_fail[] = "rot verify: FAIL\r\n";
54static const uint8_t k_rot_diag_boot[] = "rot: boot (console up)\r\n";
55static const uint8_t k_rot_diag_psa_fail[] = "rot: psa_init FAIL\r\n";
56static const uint8_t k_rot_diag_psa_ok[] = "rot: psa_init ok\r\n";
57static const uint8_t k_rot_diag_genuine[] = "rot: genuine image ACCEPTED\r\n";
58static const uint8_t k_rot_diag_no_gen[] = "rot: genuine image REJECTED (bug)\r\n";
59static const uint8_t k_rot_diag_tamper[] = "rot: tampered image REJECTED\r\n";
60static const uint8_t k_rot_diag_no_tamp[] = "rot: tampered image ACCEPTED (bug)\r\n";
61
64
66static uint8_t s_rot_tamper[sizeof(k_rot_fixture_image)];
67
68static void rot_write(const uint8_t* msg, size_t len)
69{
70 (void)ra8_board_uart_console_write(msg, len);
71}
72
74static bool rot_genuine_ok(void)
75{
76 const ra8_rot_trailer_t* trailer =
78 if (trailer == nullptr) {
79 return false;
80 }
82}
83
85static bool rot_tamper_rejected(void)
86{
88 s_rot_tamper[0] ^= (uint8_t)k_rot_tamper_bit;
90 if (trailer == nullptr) {
91 return false;
92 }
94}
95
96static void rot_panic_halt(void)
97{
98 while (1) {
99 __asm__ volatile("wfi");
100 }
101}
102
103static void rot_setup_or_halt(void)
104{
105 uint32_t cpuclk0_hz = 0U;
106 if (ra8_cgc_init() != k_ra8_ok) {
108 }
111 }
112 if (ra8_time_init(cpuclk0_hz) != k_ra8_ok) {
114 }
117 }
120 }
123 }
124 rot_write(k_rot_diag_boot, (size_t)(sizeof(k_rot_diag_boot) - 1U));
125 mbedtls_memory_buffer_alloc_init(s_rot_heap, sizeof(s_rot_heap));
126 if (ra8_psa_crypto_init() != k_ra8_ok) {
127 rot_write(k_rot_diag_psa_fail, (size_t)(sizeof(k_rot_diag_psa_fail) - 1U));
129 }
130 rot_write(k_rot_diag_psa_ok, (size_t)(sizeof(k_rot_diag_psa_ok) - 1U));
131}
132
133void main(void)
134{
137
138 while (1) {
139 bool ok = true;
140
141 if (rot_genuine_ok()) {
142 rot_write(k_rot_diag_genuine, (size_t)(sizeof(k_rot_diag_genuine) - 1U));
143 } else {
144 rot_write(k_rot_diag_no_gen, (size_t)(sizeof(k_rot_diag_no_gen) - 1U));
145 ok = false;
146 }
147
148 if (rot_tamper_rejected()) {
149 rot_write(k_rot_diag_tamper, (size_t)(sizeof(k_rot_diag_tamper) - 1U));
150 } else {
151 rot_write(k_rot_diag_no_tamp, (size_t)(sizeof(k_rot_diag_no_tamp) - 1U));
152 ok = false;
153 }
154
155 if (ok) {
156 rot_write(k_rot_msg_pass, (size_t)(sizeof(k_rot_msg_pass) - 1U));
158 } else {
159 rot_write(k_rot_msg_fail, (size_t)(sizeof(k_rot_msg_fail) - 1U));
161 }
163 }
165}
void main(void)
Secure fallback main entry point.
Definition main.c:37
static const uint8_t k_rot_diag_boot[]
Definition main.c:54
static const uint8_t k_rot_diag_tamper[]
Definition main.c:59
static const uint8_t k_rot_diag_psa_ok[]
Definition main.c:56
static bool rot_genuine_ok(void)
The genuine signed fixture must verify (k_ra8_ok).
Definition main.c:74
static const uint8_t k_rot_diag_psa_fail[]
Definition main.c:55
rot_const_t
Demo tunables.
Definition main.c:41
@ k_rot_heap_bytes
64 KiB static heap for tf-psa mbedtls_calloc.
Definition main.c:44
@ k_rot_period_ms
Rot period ms.
Definition main.c:43
@ k_rot_baud
Rot baud.
Definition main.c:42
static bool rot_tamper_rejected(void)
A one-bit body corruption must be rejected (not k_ra8_ok).
Definition main.c:85
static void rot_panic_halt(void)
Definition main.c:96
rot_bit_t
Byte flipped in the tamper copy (first body octet).
Definition main.c:48
@ k_rot_tamper_bit
Rot tamper bit.
Definition main.c:49
static const uint8_t k_rot_diag_no_tamp[]
Definition main.c:60
static const uint8_t k_rot_msg_fail[]
Definition main.c:53
static uint8_t s_rot_tamper[sizeof(k_rot_fixture_image)]
Mutable copy of the fixture used for the tamper case.
Definition main.c:66
static const uint8_t k_rot_diag_genuine[]
Definition main.c:57
static void rot_write(const uint8_t *msg, size_t len)
Definition main.c:68
static const uint8_t k_rot_msg_pass[]
Definition main.c:52
static void rot_setup_or_halt(void)
Definition main.c:103
static uint8_t s_rot_heap[k_rot_heap_bytes]
Static heap tf-psa's mbedtls_calloc draws from (no libc heap on target).
Definition main.c:63
static const uint8_t k_rot_diag_no_gen[]
Definition main.c:58
Board-support layer for the Renesas EK-RA8D2 v1 evaluation kit.
ra8_err_t ra8_board_led_toggle(ra8_board_led_id_t led)
Toggle led's output state.
ra8_err_t ra8_board_led_init(ra8_board_led_id_t led)
Configure led as a digital output, initial level low (off).
@ k_ra8_board_led2
LED2, GREEN, P303 (jumper E26).
@ k_ra8_board_led1
LED1, BLUE, P600 (jumper E27).
ra8_err_t ra8_board_uart_console_write(const uint8_t *data, size_t len)
Polled blocking write to the J-Link OB VCOM console.
ra8_err_t ra8_board_uart_console_init(uint32_t baud)
Configure SCI8 + PD02/PD03 as the debug-console UART.
Boot entry points shared between a vector table and its startup code.
High-level Clock Generation Circuit driver.
ra8_err_t ra8_cgc_get_clock_hz(ra8_clock_id_t id, uint32_t *out_hz)
Query the current frequency of a clock-tree domain.
Definition ra8_cgc.c:132
@ k_ra8_clock_id_cpuclk0
Cortex-M85 CPUCLK0.
Definition ra8_cgc.h:70
ra8_err_t ra8_cgc_init(void)
Configure the clock tree to a safe default.
Definition ra8_cgc.c:727
Error Code Definitions for ra8-firmware.
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
Definition ra8_err.h:119
void * memcpy(void *dst, const void *src, size_t n)
Copy memory area between non-overlapping regions.
NVIC + ICU IELSR allocator.
void ra8_isr_globals_enable(void)
Globally enable maskable interrupts (PRIMASK = 0).
Definition ra8_isr.c:439
Application-level PSA Crypto facade over tf-psa-crypto.
ra8_err_t ra8_psa_crypto_init(void)
One-shot facade initialisation.
Root-of-trust signed-image verifier (SHA-256 + ECDSA-P256, default-deny).
ra8_err_t ra8_rot_verify_image(const uint8_t *body, uint32_t body_len, const ra8_rot_trailer_t *trailer)
Authenticate a signed image: SHA-256 + ECDSA-P256, default-deny.
const ra8_rot_trailer_t * ra8_rot_trailer_after(const void *image_base, uint32_t body_len)
Locate the trailer that immediately follows a signed image body.
SysTick-based tick counter, delay and timestamp helpers.
ra8_err_t ra8_time_init(uint32_t cpu_hz)
Initialise SysTick for a 1 kHz tick interrupt.
Definition ra8_time.c:59
void ra8_delay_ms(uint32_t ms)
Busy-wait for at least ms milliseconds.
Definition ra8_time.c:129
Baked RoT-signed image fixture (body + ra8_rot_trailer_t).
static const uint8_t k_rot_fixture_image[182]
Signed image: 66-byte body followed by the 116-byte trailer.
Definition rot_fixture.h:23
@ k_rot_fixture_body_len
Rot fixture body length.
Definition rot_fixture.h:20
Authenticity trailer appended after a signed image body.
Definition ra8_rot.h:150