diff --git a/loader/Cargo.toml b/loader/Cargo.toml index b30693fcd..a55527b57 100644 --- a/loader/Cargo.toml +++ b/loader/Cargo.toml @@ -56,6 +56,7 @@ sha2 = { version = "0.10.8", optional = true, default-features = false } digest = { version = "0.10.7", optional = true } critical-section = { version = "1.2.0", optional = true } riscv = { version = "0.14.0", features = ["critical-section-single-hart"] } +hex = { version = "0.4.3", default-features = false, features = ["alloc"] } [dependencies.ed25519-dalek] version = "=2.1.0" diff --git a/loader/src/main.rs b/loader/src/main.rs index 7cc0d20ec..eeca134a1 100644 --- a/loader/src/main.rs +++ b/loader/src/main.rs @@ -60,9 +60,13 @@ // ** Testing features ** // -[ ] libs/precursor/hal/src/board/precursors.rs - PDDB_LEN is shortened for vexii-test target +extern crate alloc; +use alloc::vec::Vec; + #[macro_use] mod args; use args::{KernelArgument, KernelArguments}; +use bao1x_api::UUID; #[cfg_attr(feature = "atsama5d27", path = "platform/atsama5d27/debug.rs")] mod debug; @@ -75,6 +79,7 @@ mod asm; mod bootconfig; #[cfg_attr(feature = "atsama5d27", path = "platform/atsama5d27/consts.rs")] mod consts; +mod env; mod minielf; #[cfg(feature = "resume")] mod murmur3; @@ -121,10 +126,10 @@ static ALLOCATOR: linked_list_allocator::LockedHeap = linked_list_allocator::Loc // heap start is selected by looking at the total reserved .data + .bss region in the compiled loader. // it hovers at 0x5000 unless I start adding lots of statics to the loader (which there are not). #[cfg(feature = "bao1x")] -const HEAP_OFFSET: usize = 0x5000; +pub const HEAP_OFFSET: usize = 0x5000; // just a small heap, big enough for us to use alloc to simplify argument processing #[cfg(feature = "bao1x")] -const HEAP_LEN: usize = 0x1000; +pub const HEAP_LEN: usize = 0x1000; #[repr(C)] pub struct MemoryRegionExtra { @@ -337,19 +342,36 @@ pub unsafe extern "C" fn rust_entry(signed_buffer: *const usize, signature: u32) let arg_buffer = (signed_buffer as u32 + signature_size as u32) as *const usize; println!("arg_buffer: {:x}", arg_buffer as usize); + // build the environment variables + let mut env_variables = crate::env::EnvVariables::new(); + env_variables.add_var("ROOT_FILESYSTEM_HASH", &crate::env::to_hex_ascii(&fs_prehash)); + + #[cfg(feature = "bao1x")] + { + let owc = bao1x_hal::acram::OneWayCounter::new(); + let slot_mgr = bao1x_hal::acram::SlotManager::new(); + let sn = bao1x_hal::usb::derive_usb_serial_number(&owc, &slot_mgr); + env_variables.add_var("PUBLIC_SERIAL", &sn); + let hex_uuid = hex::encode(slot_mgr.read(&UUID).unwrap()); + env_variables.add_var("UUID", &hex_uuid); + } + + let mut env_header = crate::env::EnvHeader::default(); + let env = env_header.to_bytes(&env_variables); + // perhaps later on in these sequences, individual sub-images may be validated // against sub-signatures; or the images may need to be re-validated after loading // into RAM, if we have concerns about RAM glitching as an attack surface (I don't think we do...). // But for now, the basic "validate everything as a blob" is perhaps good enough to // armor code-at-rest against front-line patching attacks. let kab = KernelArguments::new(arg_buffer); - boot_sequence(kab, signature, fs_prehash, perclk_freq, detached_app); + boot_sequence(kab, signature, env, perclk_freq, detached_app); } fn boot_sequence( args: KernelArguments, _signature: u32, - fs_prehash: [u8; 64], + env: Vec, _perclk_freq: u32, detached_app: bool, ) -> ! { @@ -381,7 +403,7 @@ fn boot_sequence( println!("Configured for simulation. Skipping RAM clear!"); clear_ram(&mut cfg); phase_1(&mut cfg, detached_app); - phase_2(&mut cfg, &fs_prehash); + phase_2(&mut cfg, &env); #[cfg(any(feature = "debug-print", feature = "swap"))] if VDBG || SDBG { check_load(&mut cfg); @@ -395,7 +417,7 @@ fn boot_sequence( println!("No suspend marker found, doing a cold boot!"); clear_ram(&mut cfg); phase_1(&mut cfg, detached_app); - phase_2(&mut cfg, &fs_prehash); + phase_2(&mut cfg, &env); #[cfg(any(feature = "debug-print", feature = "swap"))] if VDBG || SDBG { check_load(&mut cfg); @@ -765,8 +787,12 @@ fn clear_ram(cfg: &mut BootConfig) { if VDBG { println!("Stack clearing limit: {:x}", clear_limit); } + #[cfg(not(feature = "bao1x"))] + let clear_start = 0; + #[cfg(feature = "bao1x")] + let clear_start = HEAP_OFFSET + HEAP_LEN; unsafe { - for addr in 0..(cfg.sram_size - clear_limit) / 4 { + for addr in clear_start..(cfg.sram_size - clear_limit) / 4 { // 8k is reserved for our own stack ram.add(addr).write_volatile(0); } diff --git a/loader/src/phase2.rs b/loader/src/phase2.rs index 16a6c1c2e..da3e792df 100644 --- a/loader/src/phase2.rs +++ b/loader/src/phase2.rs @@ -9,59 +9,13 @@ use crate::*; /// /// Set up all the page tables, allocating new root page tables for SATPs and corresponding /// sub-pages starting from the base of previously copied process data. -pub fn phase_2(cfg: &mut BootConfig, fs_prehash: &[u8; 64]) { +pub fn phase_2(cfg: &mut BootConfig, env: &Vec) { let args = cfg.args; // This is the offset in RAM where programs are loaded from. let mut process_offset = cfg.sram_start as usize + cfg.sram_size - cfg.init_size; println!("\n\nPhase2: Processess start out @ {:08x}", process_offset); - // Construct an environment block. This will be used by processes - // to access environment variables and system parameters. This - // is hardcoded for now, and can be expanded later. - #[rustfmt::skip] - let mut env = [ - 0x41, 0x70, 0x70, 0x50, // 'AppP' indicating application parameters - 0x08, 0x00, 0x00, 0x00, // Size of AppP tag contents - 0xb2, 0x00, 0x00, 0x00, // Size of entire AppP block including all tags - 0x02, 0x00, 0x00, 0x00, // Number of tags present - 0x45, 0x6e, 0x76, 0x42, // 'EnvB' indicating an environment block - 0x9a, 0x00, 0x00, 0x00, // Number of bytes that follows for the environment block - 0x01, 0x00, // Number of environment variables - 0x14, 0x00, // Length of name of first variable - // Name of first variable 'ROOT_FILESYSTEM_HASH': - 0x52, 0x4f, 0x4f, 0x54, 0x5f, 0x46, 0x49, 0x4c, 0x45, 0x53, 0x59, 0x53, 0x54, 0x45, 0x4d, - 0x5f, 0x48, 0x41, 0x53, 0x48, - // Length of the contents of the first variable - 0x80, 0x00, - // Root filesystem hash contents begin here: - 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, - 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, - 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, - 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, - 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, - 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, - 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, - 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, - 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, - ]; - // Convert the fs_prehash into a hex ascii string, suitable for environment variables - // The initial loader environment is hard-coded, so we use a hard-coded offset for the string - // destination. It was a deliberate decision to not include a generic environment variable - // handler in the loader because we want to keep the loader compact and with a small attack - // surface; a generic environment handling routine would add significant size without much - // benefit. - // - // Note that the main purpose for environment variables is for test & debug tooling, where - // single programs are run in hosted or emulated environments with arguments passed for fast - // debugging. The sole purpose for the environment variable in the loader's context is to - // pass this computed hash onto the userspace environments. - const HEX_DIGITS: [u8; 16] = *b"0123456789abcdef"; - for (i, &byte) in fs_prehash.iter().enumerate() { - env[env.len() - 128 + i * 2] = HEX_DIGITS[(byte >> 4) as usize]; - env[env.len() - 128 + i * 2 + 1] = HEX_DIGITS[(byte & 0xF) as usize]; - } - // Go through all Init processes and the kernel, setting up their // page tables and mapping memory to them. let mut pid = 2; @@ -86,14 +40,14 @@ pub fn phase_2(cfg: &mut BootConfig, fs_prehash: &[u8; 64]) { if tag.name == u32::from_le_bytes(*b"IniE") { let inie = MiniElf::new(&tag); println!("\n\nCopying IniE program into memory"); - let allocated = inie.load(cfg, process_offset, pid, &env, IniType::IniE); + let allocated = inie.load(cfg, process_offset, pid, env, IniType::IniE); println!("IniE Allocated {:x}", allocated); process_offset -= allocated; pid += 1; } else if tag.name == u32::from_le_bytes(*b"IniF") { let inif = MiniElf::new(&tag); println!("\n\nMapping IniF program into memory"); - let allocated = inif.load(cfg, process_offset, pid, &env, IniType::IniF); + let allocated = inif.load(cfg, process_offset, pid, env, IniType::IniF); println!("IniF Allocated {:x}", allocated); process_offset -= allocated; pid += 1; @@ -102,7 +56,7 @@ pub fn phase_2(cfg: &mut BootConfig, fs_prehash: &[u8; 64]) { { let inis = MiniElf::new(&tag); println!("\n\nMapping IniS program into memory"); - let allocated = inis.load(cfg, process_offset, pid, &env, IniType::IniS); + let allocated = inis.load(cfg, process_offset, pid, env, IniType::IniS); println!("IniS Allocated {:x}", allocated); process_offset -= allocated; pid += 1;