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
34
35#include <stdint.h>
36#include <string.h>
37
38#include "mbedtls/memory_buffer_alloc.h"
39#include "ra8_board_ek_ra8d2.h"
40#include "ra8_boot_entry.h"
41#include "ra8_cgc.h"
42#include "ra8_dfu.h"
43#include "ra8_err.h"
44#include "ra8_isr.h"
45#include "ra8_psa_crypto.h"
46#include "ra8_time.h"
47#include "signed_payload.h"
48
50typedef enum : uint32_t {
51 k_sb_baud = 115200U,
52 k_sb_heap_bytes = 0x10000U,
54
56typedef enum : uint8_t {
58} sb_bit_t;
59
60static const uint8_t k_sb_msg_start[] = "secure-boot: start\r\n";
61static const uint8_t k_sb_msg_psa_fail[] = "secure-boot: psa_init FAIL\r\n";
62static const uint8_t k_sb_msg_rejected[] = "secure-boot: tampered REJECTED\r\n";
63static const uint8_t k_sb_msg_no_reject[] = "secure-boot: tampered ACCEPTED (bug!)\r\n";
64static const uint8_t k_sb_msg_enforcing[] = "secure-boot: ENFORCING OK -- launching authentic\r\n";
65static const uint8_t k_sb_msg_launchfail[] =
66 "secure-boot: LAUNCH FAILED (authentic rejected -- bug)\r\n";
67
69static uint8_t s_sb_heap[k_sb_heap_bytes];
70
72static uint8_t s_sb_tampered[sizeof(g_sb_signed_payload)];
73
74static void sb_write(const uint8_t* msg, size_t len)
75{
76 (void)ra8_board_uart_console_write(msg, len);
77}
78
79static void sb_panic_halt(void)
80{
81 while (1) {
82 __asm__ volatile("wfi");
83 }
84}
85
86static void sb_setup_or_halt(void)
87{
88 uint32_t cpuclk0_hz = 0U;
89 if (ra8_cgc_init() != k_ra8_ok) {
91 }
94 }
95 if (ra8_time_init(cpuclk0_hz) != k_ra8_ok) {
97 }
100 }
103 }
106 }
107 sb_write(k_sb_msg_start, (size_t)(sizeof(k_sb_msg_start) - 1U));
108 mbedtls_memory_buffer_alloc_init(s_sb_heap, sizeof(s_sb_heap));
109 if (ra8_psa_crypto_init() != k_ra8_ok) {
110 sb_write(k_sb_msg_psa_fail, (size_t)(sizeof(k_sb_msg_psa_fail) - 1U));
112 }
113}
114
119static bool sb_tampered_rejected(void)
120{
122 s_sb_tampered[0] ^= (uint8_t)k_sb_tamper_bit;
123 /* A rejected image makes ra8_dfu_launch return without copying or branching.
124 * If it instead launched the corrupted image, control would never come back
125 * here -- so reaching the next line at all proves the default-deny. */
126 ra8_dfu_launch((uintptr_t)s_sb_tampered,
127 (uint32_t)k_sb_payload_body_len,
128 (uint32_t)k_ra8_dfu_run_base);
129 return true;
130}
131
132void main(void)
133{
136
137 /* Step 1: the tampered image must be rejected (launch returns). */
138 if (sb_tampered_rejected()) {
139 sb_write(k_sb_msg_rejected, (size_t)(sizeof(k_sb_msg_rejected) - 1U));
141 } else {
142 sb_write(k_sb_msg_no_reject, (size_t)(sizeof(k_sb_msg_no_reject) - 1U));
145 }
146
147 /* Step 2: the genuine signed image must verify + pass anti-rollback, then be
148 * copied to the run base and branched to -- ra8_dfu_launch does not return. */
149 sb_write(k_sb_msg_enforcing, (size_t)(sizeof(k_sb_msg_enforcing) - 1U));
151 (uint32_t)k_sb_payload_body_len,
152 (uint32_t)k_ra8_dfu_run_base);
153
154 /* Only reached if the RoT rejected an authentic image -- a bug. */
155 sb_write(k_sb_msg_launchfail, (size_t)(sizeof(k_sb_msg_launchfail) - 1U));
158}
void main(void)
Secure fallback main entry point.
Definition main.c:37
static uint8_t s_sb_heap[k_sb_heap_bytes]
Static heap tf-psa's mbedtls_calloc draws from (no libc heap on target).
Definition main.c:69
static const uint8_t k_sb_msg_launchfail[]
Definition main.c:65
static void sb_setup_or_halt(void)
Definition main.c:86
static const uint8_t k_sb_msg_enforcing[]
Definition main.c:64
static void sb_write(const uint8_t *msg, size_t len)
Definition main.c:74
sb_const_t
Demo tunables.
Definition main.c:50
@ k_sb_baud
Sb baud.
Definition main.c:51
@ k_sb_heap_bytes
64 KiB static heap for tf-psa mbedtls_calloc.
Definition main.c:52
static uint8_t s_sb_tampered[sizeof(g_sb_signed_payload)]
Mutable copy of the signed image used to build the tampered case.
Definition main.c:72
static const uint8_t k_sb_msg_start[]
Definition main.c:60
static const uint8_t k_sb_msg_psa_fail[]
Definition main.c:61
static const uint8_t k_sb_msg_no_reject[]
Definition main.c:63
sb_bit_t
Byte flipped in the tampered copy (first body octet).
Definition main.c:56
@ k_sb_tamper_bit
Sb tamper bit.
Definition main.c:57
static bool sb_tampered_rejected(void)
Confirm a one-bit body corruption is rejected by the launch gate.
Definition main.c:119
static const uint8_t k_sb_msg_rejected[]
Definition main.c:62
static void sb_panic_halt(void)
Definition main.c:79
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
Controller-agnostic USB-DFU MRAM bootloader core for the RA8D2.
@ k_ra8_dfu_run_base
SRAM copy-to-run / payload link base.
Definition ra8_dfu.h:126
void ra8_dfu_launch(uintptr_t src, uint32_t img_len, uint32_t entry)
Copy an image to the SRAM run base and branch to it (copy-to-run).
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.
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
GENERATED: copy-to-run payload signed by scripts/secrets/rot_sign.py – do not edit.
static const uint8_t g_sb_signed_payload[148]
Signed copy-to-run image: 32-byte body + 116-byte RoT trailer.
@ k_sb_payload_body_len
Sb payload body length.