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
55
56#include <stddef.h>
57#include <stdint.h>
58
59#include "mbedtls/memory_buffer_alloc.h"
60#include "ra8_board_ek_ra8d2.h"
61#include "ra8_boot_entry.h"
62#include "ra8_err.h"
63#include "ra8_mstp.h"
64#include "ra8_psa_crypto.h"
65#include "ra8_tz_secure_boot.h"
66#include "trustzone_init.h"
67
69typedef enum : uint32_t {
70 k_sbns_heap_bytes = 0x10000U,
72
80typedef enum : uint32_t {
81 k_sbns_uart_baud = 115200U,
83
96
102typedef enum : uint32_t {
105
119
130volatile uint32_t g_sbns_step = k_sbns_step_idle;
131
141volatile uint32_t g_sbns_denied;
142
153volatile uint32_t g_sbns_jump_err;
154
166
177static bool s_sbns_console_up = false;
178
180static const uint8_t k_sbns_msg_crypto_ready[] = "sbns: crypto ready\r\n";
181
183static const uint8_t k_sbns_msg_verify_enter[] = "sbns: verify+enter NS\r\n";
184
186static const uint8_t k_sbns_msg_reject[] = "sbns: NS REJECTED err=0x";
187
189static const uint8_t k_sbns_msg_crlf[] = "\r\n";
190
203[[noreturn]] static void sbns_park(void)
204{
205 while (1) {
206 __asm__ volatile("wfi");
207 }
208}
209
230static void sbns_console_bringup(void)
231{
232 if (ra8_mstp_init() != k_ra8_ok) {
233 return;
234 }
236 return;
237 }
238 s_sbns_console_up = true;
239}
240
259static void sbns_console_say(const uint8_t* msg, uint32_t len)
260{
261 if (!s_sbns_console_up) {
262 return;
263 }
264 if (msg == nullptr) {
265 return;
266 }
267 (void)ra8_board_uart_console_write(msg, (size_t)len);
268}
269
287static void sbns_console_print_hex32(uint32_t value)
288{
289 if (!s_sbns_console_up) {
290 return;
291 }
292 uint8_t buf[k_sbns_hex_nibbles];
293 for (uint8_t i = 0U; i < (uint8_t)k_sbns_hex_nibbles; i++) {
294 const uint32_t shift =
295 (uint32_t)((uint8_t)k_sbns_hex_nibbles - 1U - i) * (uint32_t)k_sbns_hex_bits;
296 const uint8_t nib = (uint8_t)((value >> shift) & (uint32_t)k_sbns_hex_nibble_mask);
297 buf[i] = (nib <= (uint8_t)k_sbns_hex_dec_max)
298 ? (uint8_t)('0' + nib)
299 : (uint8_t)('a' + (nib - (uint8_t)k_sbns_hex_alpha_base));
300 }
301 uint8_t first = 0U;
302 while (first < (uint8_t)((uint8_t)k_sbns_hex_nibbles - 1U)) {
303 if (buf[first] != (uint8_t)'0') {
304 break;
305 }
306 first++;
307 }
308 (void)ra8_board_uart_console_write(&buf[first], (size_t)((uint8_t)k_sbns_hex_nibbles - first));
309}
310
327{
328 sbns_console_say(k_sbns_msg_reject, (uint32_t)(sizeof(k_sbns_msg_reject) - 1U));
329 sbns_console_print_hex32((uint32_t)err);
330 sbns_console_say(k_sbns_msg_crlf, (uint32_t)(sizeof(k_sbns_msg_crlf) - 1U));
331}
332
333void main(void)
334{
335 /* Bring up the crypto heap + PSA facade the root-of-trust verify needs. The
336 * SAU + NS-image copy already ran in ra8_trustzone_init (SystemInit). */
337 mbedtls_memory_buffer_alloc_init(s_sbns_heap, sizeof(s_sbns_heap));
338 const ra8_err_t psa_err = ra8_psa_crypto_init();
339 if ((psa_err != k_ra8_ok) && (psa_err != k_ra8_err_exists)) {
341 sbns_park();
342 }
344
345 /* Bring up the Secure UART console and announce the crypto milestone so the
346 * boot verdict is scrape-able on the bench. Best-effort: a dead console must
347 * never gate the security decision, so the prints no-op if it failed. */
350
351 /* Authenticate + jump. The RA8_ENABLE_ROOT_OF_TRUST gate inside jump_ns reads
352 * the NS RoT header for the body length, verifies SHA-256 + ECDSA-P256, and
353 * BLXNS-es ONLY on success. A genuine image never returns here. */
356 const ra8_err_t jump_err =
357 ra8_tz_secure_boot_jump_ns((const uint32_t*)(uintptr_t)k_sbns_ns_run_base);
358
359 /* Reached only when the gate DENIED the NS image (tampered / unsigned): the
360 * default-deny path. Latch + print the outcome for the bench and halt -- the
361 * NS world never ran, so g_sbns_ns_alive stays 0. */
362 g_sbns_jump_err = (uint32_t)jump_err;
363 g_sbns_denied = 1U;
365 sbns_console_say_reject(jump_err);
366 sbns_park();
367}
void main(void)
Secure fallback main entry point.
Definition main.c:37
@ k_sbns_ns_run_base
NS image VMA (SRAM2 NS alias).
static void sbns_console_print_hex32(uint32_t value)
Print a uint32 as hexadecimal on the console, no leading zeros.
Definition main.c:287
volatile uint32_t g_sbns_step
Secure boot progress breadcrumb (sbns_step_t).
Definition main.c:130
static bool s_sbns_console_up
True once the Secure J-Link OB VCOM console is initialised.
Definition main.c:177
static void sbns_park(void)
Park the Secure core in WFI forever.
Definition main.c:203
sbns_hex_t
Nibble-decode constants for the denial-code hexadecimal print.
Definition main.c:90
@ k_sbns_hex_dec_max
Highest nibble emitted as an ASCII '0'-'9'.
Definition main.c:93
@ k_sbns_hex_bits
Bits per hex nibble.
Definition main.c:92
@ k_sbns_hex_nibbles
Hex nibbles in a uint32 (32 / 4).
Definition main.c:91
@ k_sbns_hex_alpha_base
First nibble emitted as ASCII 'a'.
Definition main.c:94
volatile uint32_t g_sbns_denied
Set to 1 when the root-of-trust gate DENIED the NS image (tampered).
Definition main.c:141
static void sbns_console_bringup(void)
Bring up the Secure J-Link OB VCOM console (best-effort).
Definition main.c:230
sbns_uart_t
Secure-side J-Link OB VCOM (SCI8) console parameters.
Definition main.c:80
@ k_sbns_uart_baud
SCI8 J-Link OB VCOM console line rate (bps).
Definition main.c:81
static const uint8_t k_sbns_msg_verify_enter[]
UART breadcrumb: about to verify + BLXNS into the NS image.
Definition main.c:183
volatile uint32_t g_sbns_jump_err
The ra8_err_t ra8_tz_secure_boot_jump_ns returned on a denial.
Definition main.c:153
sbns_hex_mask_t
Bit mask isolating one hexadecimal nibble from a shifted word.
Definition main.c:102
@ k_sbns_hex_nibble_mask
Low-nibble mask for a uint32 shift result.
Definition main.c:103
static void sbns_console_say_reject(ra8_err_t err)
Print the sbns: NS REJECTED err=0x... verdict line for a denial.
Definition main.c:326
sbns_step_t
Boot-progress breadcrumbs latched into g_sbns_step.
Definition main.c:112
@ k_sbns_step_denied
jump_ns returned -> NS image denied.
Definition main.c:116
@ k_sbns_step_idle
Pre-main sentinel.
Definition main.c:113
@ k_sbns_step_crypto
Heap + PSA facade initialised.
Definition main.c:114
@ k_sbns_step_armed
About to call jump_ns (verify+BLXNS).
Definition main.c:115
@ k_sbns_step_psa_fail
PSA init failed (crypto unavailable).
Definition main.c:117
sbns_const_t
Static-heap sizing for tf-psa-crypto's mbedtls_calloc (no libc heap).
Definition main.c:69
@ k_sbns_heap_bytes
64 KiB static heap for the ECDSA verify.
Definition main.c:70
static const uint8_t k_sbns_msg_reject[]
UART verdict prefix: RoT denied the NS image (followed by the hex code).
Definition main.c:186
static void sbns_console_say(const uint8_t *msg, uint32_t len)
Emit a byte run on the Secure console (no-op until it is up).
Definition main.c:259
static uint8_t s_sbns_heap[k_sbns_heap_bytes]
Static heap tf-psa-crypto's mbedtls_calloc draws from.
Definition main.c:165
static const uint8_t k_sbns_msg_crlf[]
UART line terminator appended after the denial error code.
Definition main.c:189
static const uint8_t k_sbns_msg_crypto_ready[]
UART breadcrumb: crypto/PSA facade is up (printed pre-verify).
Definition main.c:180
Board-support layer for the Renesas EK-RA8D2 v1 evaluation kit.
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.
Error Code Definitions for ra8-firmware.
@ k_ra8_err_exists
Item already exists – cannot create again.
Definition ra8_err.h:216
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
Definition ra8_err.h:119
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
Definition ra8_err.h:546
Ref-counted Module Stop Control wrapper for the RA8D2.
ra8_err_t ra8_mstp_init(void)
Re-establish the ra8_mstp ref-count table from current hardware state.
Definition ra8_mstp.c:291
Application-level PSA Crypto facade over tf-psa-crypto.
ra8_err_t ra8_psa_crypto_init(void)
One-shot facade initialisation.
FSP-style TrustZone secure-boot for the Cortex-M85 (CPU0).
ra8_err_t ra8_tz_secure_boot_jump_ns(const uint32_t *ns_vector_table)
Switch to NS state and jump to the NS image's reset vector.