diff --git a/loader/Cargo.toml b/loader/Cargo.toml index a55527b57..21c6ca8fc 100644 --- a/loader/Cargo.toml +++ b/loader/Cargo.toml @@ -40,7 +40,9 @@ xous-pl230 = { path = "../libs/xous-pl230", optional = true, features = [ aes-gcm-siv = { version = "0.11.1", default-features = false, features = [ "aes", ], optional = true } -aes = { path = "../services/aes", default-features = false, optional = true } +aes = { path = "../services/aes", default-features = false, optional = true, features = [ + "chaffing", +] } crc = { version = "1.8.1", default-features = false } rand_chacha = { version = "0.3.1", optional = true, default-features = false } pio-proc = { version = "0.2.2", optional = true } diff --git a/loader/src/env.rs b/loader/src/env.rs index 2f55d11a1..ac45005fa 100644 --- a/loader/src/env.rs +++ b/loader/src/env.rs @@ -18,7 +18,7 @@ pub struct EnvHeader { count: u16, } -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct EnvVariables { count: usize, flattened_vars: Vec, diff --git a/loader/src/main.rs b/loader/src/main.rs index da5f14dad..46a06b6ec 100644 --- a/loader/src/main.rs +++ b/loader/src/main.rs @@ -61,7 +61,6 @@ // -[ ] 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; @@ -433,14 +432,11 @@ fn boot_sequence( env_variables.add_var("UUID", &hex_uuid); } - let mut env_header = crate::env::EnvHeader::default(); - let env = env_header.to_bytes(&env_variables); - // cold boot path println!("No suspend marker found, doing a cold boot!"); clear_ram(&mut cfg); phase_1(&mut cfg, detached_app); - phase_2(&mut cfg, &env); + phase_2(&mut cfg, env_variables); #[cfg(any(feature = "debug-print", feature = "swap"))] if VDBG || SDBG { check_load(&mut cfg); diff --git a/loader/src/phase2.rs b/loader/src/phase2.rs index da3e792df..b71231ee5 100644 --- a/loader/src/phase2.rs +++ b/loader/src/phase2.rs @@ -3,13 +3,13 @@ use xous::arch::{SWAP_CFG_VADDR, SWAP_COUNT_VADDR, SWAP_PT_VADDR}; #[cfg(feature = "swap")] use crate::swap::*; -use crate::*; +use crate::{env::EnvVariables, *}; /// Phase 2 bootloader /// /// 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, env: &Vec) { +pub fn phase_2(cfg: &mut BootConfig, env_variables: EnvVariables) { let args = cfg.args; // This is the offset in RAM where programs are loaded from. @@ -36,18 +36,38 @@ pub fn phase_2(cfg: &mut BootConfig, env: &Vec) { #[cfg(feature = "atsama5d27")] let mut kernel_irq_sp = 0; + #[cfg(feature = "bao1x")] + let mut trng = bao1x_hal::sce::trng::Trng::new(utralib::generated::HW_TRNG_BASE as usize); + #[cfg(feature = "bao1x")] + trng.setup_raw_generation(32); // this is safe to call multiple times to affirm the TRNG state + for tag in args.iter() { + let mut env_header = crate::env::EnvHeader::default(); + let mut pid_env = env_variables.clone(); + + #[cfg(feature = "bao1x")] + { + let mut seed = [0u8; 32]; + for chunk in seed.chunks_mut(4) { + chunk.copy_from_slice(&trng.get_u32().unwrap().to_ne_bytes()); + } + let seed_hex = hex::encode(&seed); + pid_env.add_var("SEED", &seed_hex); + } + + let env = env_header.to_bytes(&pid_env); + 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; @@ -56,7 +76,7 @@ pub fn phase_2(cfg: &mut BootConfig, env: &Vec) { { 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;