From a3a7bf9f3a22edcd0aa76098cba49608e1856152 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 21 Sep 2025 21:50:48 +0800 Subject: [PATCH] add a format for passing static initialized data to baremetal a small, 256-byte record is added to the top of baremetal records that is a "poke table" that incorporates what values to put where for baremetal targets, as well as a zeroization region. This is generally more efficient because most values are set to zero, and the only things that generally have a non-zero value are 'static' variables, which are used extremely sparingly. --- libs/bao1x-api/src/lib.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/libs/bao1x-api/src/lib.rs b/libs/bao1x-api/src/lib.rs index 2bb941f04..92d5f752e 100644 --- a/libs/bao1x-api/src/lib.rs +++ b/libs/bao1x-api/src/lib.rs @@ -52,3 +52,36 @@ pub mod camera { } } } + +/// Version number of the below structure +pub const STATICS_IN_ROM_VERSION: u16 = 1; +/// This encodes to jal x0, 256 - jumps 256 bytes ahead from the current PC location. +pub const JUMP_INSTRUCTION: u32 = 0x1000006f; +/// In-ROM representation of static initialization data +/// Placed by the image creation tool, and used for bootstrapping the Rust environment +/// `usize` is *not* allowed because this structure is packed on a 64-bit host. +#[repr(C, align(256))] +pub struct StaticsInRom { + // reserved for a jump-over instruction so that the structure can be located in-line in ROM. + #[allow(dead_code)] // this should never actually be used + pub jump_instruction: u32, + // version number of this structure + pub version: u16, + // total number of valid pokes in `poke_table` + pub valid_pokes: u16, + // Origin of the data segment + pub data_origin: u32, + // overall size in bytes. [origin:origin+size] will be zeroized. + pub data_size_bytes: u32, + // poke table of values to stick in the data segment. This is needed in particular + // to initialize `static` variables, such as Atomics and Mutexes, required by the + // loader environment. Presented as (address, data) tuples, up to 30 of them. + // Only entries from [0..valid_pokes] are processed. + pub poke_table: [(u32, u32); 30], +} + +impl StaticsInRom { + pub fn as_bytes(&self) -> &[u8] { + unsafe { core::slice::from_raw_parts(self as *const Self as *const u8, core::mem::size_of::()) } + } +}