From 95c80461116aa76de780ec0a36fafbeeceacc857 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 23 Nov 2025 08:47:43 +0800 Subject: [PATCH 1/4] add backup region abstractions these persist through a sleep cycle --- libs/bao1x-hal/src/buram.rs | 146 ++++++++++++++++++++++++++++++++++++ libs/bao1x-hal/src/lib.rs | 2 + 2 files changed, 148 insertions(+) create mode 100644 libs/bao1x-hal/src/buram.rs diff --git a/libs/bao1x-hal/src/buram.rs b/libs/bao1x-hal/src/buram.rs new file mode 100644 index 000000000..c5200d02a --- /dev/null +++ b/libs/bao1x-hal/src/buram.rs @@ -0,0 +1,146 @@ +use core::ops::Range; + +const KEY_RANGE: Range = 2..8; +const KEY_LEN: usize = range_len(KEY_RANGE) * size_of::(); +const HASH_LOC: usize = 0; +#[allow(dead_code)] +const RESERVED_LOC: usize = 1; + +use crate::buram::murmur3::murmur3_32; + +const fn range_len(r: Range) -> usize { r.end - r.start } + +pub struct BackupManager { + bu_reg: xous::MemoryRange, + bu_ram: xous::MemoryRange, +} + +impl BackupManager { + #[cfg(not(feature = "std"))] + pub fn new() -> Self { + let bu_reg = unsafe { + xous::MemoryRange::new( + utralib::utra::aobureg::HW_AOBUREG_BASE, + utralib::utra::aobureg::AOBUREG_NUMREGS * size_of::(), + ) + .unwrap() + }; + let bu_ram = unsafe { + xous::MemoryRange::new(bao1x_api::offsets::AO_BU_MEM, bao1x_api::offsets::AO_BU_MEM_LEN).unwrap() + }; + BackupManager { bu_reg, bu_ram } + } + + #[cfg(feature = "std")] + pub fn new() -> Self { + let bu_reg = xous::map_memory( + xous::MemoryAddress::new(utralib::utra::aobureg::HW_AOBUREG_BASE), + None, + utralib::utra::aobureg::AOBUREG_NUMREGS * size_of::(), + xous::MemoryFlags::R | xous::MemoryFlags::W, + ) + .expect("Couldn't map backup register range"); + + let bu_ram = xous::map_memory( + xous::MemoryAddress::new(bao1x_api::offsets::AO_BU_MEM), + None, + bao1x_api::offsets::AO_BU_MEM_LEN, + xous::MemoryFlags::R | xous::MemoryFlags::W, + ) + .expect("couldn't map backup RAM range"); + BackupManager { bu_reg, bu_ram } + } + + pub fn is_backup_valid(&self) -> bool { + let bu_ram_u32: &[u32] = unsafe { self.bu_ram.as_slice() }; + let hash = murmur3_32(bu_ram_u32, 0x0); + let bu_reg: &[u32] = unsafe { self.bu_reg.as_slice() }; + hash == bu_reg[HASH_LOC] + } + + pub fn is_zero(&self) -> bool { + let bu_reg: &[u32] = &unsafe { self.bu_reg.as_slice() }[..utralib::utra::aobureg::AOBUREG_NUMREGS]; + bu_reg.iter().all(|&x| x == 0) + } + + pub fn make_valid(&mut self) { + let bu_ram_u32: &[u32] = &unsafe { self.bu_ram.as_slice() }; + let hash = murmur3_32(bu_ram_u32, 0x0); + unsafe { + (self.bu_reg.as_mut_ptr() as *mut u32).add(HASH_LOC).write_volatile(hash); + } + } + + pub fn get_backup_key(&self) -> [u8; KEY_LEN] { + let mut dest = [0u8; KEY_LEN]; + let bu_reg: &[u32] = &unsafe { self.bu_reg.as_slice() }[KEY_RANGE]; + for (&src, dst) in bu_reg.iter().zip(dest.chunks_mut(4)) { + dst.copy_from_slice(&src.to_be_bytes()); + } + dest + } + + pub fn set_backup_key(&mut self, key: [u8; KEY_LEN]) { + let bu_reg: &mut [u32] = &mut unsafe { self.bu_reg.as_slice_mut() }[KEY_RANGE]; + for (src, dst) in key.chunks(4).zip(bu_reg.iter_mut()) { + *dst = u32::from_be_bytes(src.try_into().unwrap()) + } + } + + pub fn bu_ram_as_slice(&self) -> &[u8] { unsafe { self.bu_ram.as_slice() } } + + pub fn bu_ram_as_mut(&mut self) -> &mut [u8] { unsafe { self.bu_ram.as_slice_mut() } } +} + +pub mod murmur3 { + // Copyright (c) 2020 Stu Small + // + // Licensed under the Apache License, Version 2.0 + // or the MIT + // license , at your + // option. All files in the project carrying such notice may not be copied, + // modified, or distributed except according to those terms. + + // This murmur3 code was vendored in on April 28, 2021. We choose to vendor the code for this simple + // non-cryptographic hash in to reduce the number of crate dependencies in the build system. See + // https://github.com/betrusted-io/xous-core/issues/54 for more details on why. + // It also allowed us to adapt the code for our specific no_std needs. + + const C1: u32 = 0x85eb_ca6b; + const C2: u32 = 0xc2b2_ae35; + const R1: u32 = 16; + const R2: u32 = 13; + const M: u32 = 5; + const N: u32 = 0xe654_6b64; + + pub fn murmur3_32(source: &[u32], seed: u32) -> u32 { + let mut processed = 0; + let mut state = seed; + + for &k in source.iter() { + processed += 4; + state ^= calc_k(k); + state = state.rotate_left(R2); + state = (state.wrapping_mul(M)).wrapping_add(N); + } + finish(state, processed) + } + + fn finish(state: u32, processed: u32) -> u32 { + let mut hash = state; + hash ^= processed as u32; + hash ^= hash.wrapping_shr(R1); + hash = hash.wrapping_mul(C1); + hash ^= hash.wrapping_shr(R2); + hash = hash.wrapping_mul(C2); + hash ^= hash.wrapping_shr(R1); + hash + } + + fn calc_k(k: u32) -> u32 { + const C1: u32 = 0xcc9e_2d51; + const C2: u32 = 0x1b87_3593; + const R1: u32 = 15; + k.wrapping_mul(C1).rotate_left(R1).wrapping_mul(C2) + } +} diff --git a/libs/bao1x-hal/src/lib.rs b/libs/bao1x-hal/src/lib.rs index 70c386e76..6e9762aee 100644 --- a/libs/bao1x-hal/src/lib.rs +++ b/libs/bao1x-hal/src/lib.rs @@ -31,6 +31,8 @@ pub use shared_csr::*; #[cfg(not(feature = "hosted-baosec"))] pub mod acram; #[cfg(not(feature = "hosted-baosec"))] +pub mod buram; +#[cfg(not(feature = "hosted-baosec"))] pub mod coreuser; #[cfg(not(feature = "hosted-baosec"))] pub mod mbox; From 960ba350cb84f9c088edf640485698ba1b6191e4 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 23 Nov 2025 08:48:24 +0800 Subject: [PATCH 2/4] add bu_mem locations these failed to extract automatically from the RTL --- libs/bao1x-api/src/offsets.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/libs/bao1x-api/src/offsets.rs b/libs/bao1x-api/src/offsets.rs index 1e7fd3e0e..431bb186d 100644 --- a/libs/bao1x-api/src/offsets.rs +++ b/libs/bao1x-api/src/offsets.rs @@ -21,6 +21,9 @@ pub const IFR_LEN: usize = 0x400; /// It contains the lot ID, wafer ID, and wafer x/y position of the die. pub const IFR_CP_ID_BASE: usize = 0x6040_00A0; +pub const AO_BU_MEM: usize = 0x5030_0000; +pub const AO_BU_MEM_LEN: usize = 0x4000; + #[bitfield(u32)] #[derive(PartialEq, Eq, Debug)] pub struct DataSlotAccess { From 47428f40ff4eaa8f4cc44279426bd32af7fa3bbf Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 23 Nov 2025 08:49:50 +0800 Subject: [PATCH 3/4] add backup register initialization this will cause a "double boot" of boot0 on the very first power-up --- bao1x-boot/boot1/src/platform/bao1x/bao1x.rs | 35 ++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/bao1x-boot/boot1/src/platform/bao1x/bao1x.rs b/bao1x-boot/boot1/src/platform/bao1x/bao1x.rs index 1c4b82bb7..158e3a0ce 100644 --- a/bao1x-boot/boot1/src/platform/bao1x/bao1x.rs +++ b/bao1x-boot/boot1/src/platform/bao1x/bao1x.rs @@ -66,9 +66,44 @@ pub fn setup_dabao_se0_pin(iox: &T) -> (IoxPort, u8) { (DABAO_SE0_PORT, DABAO_SE0_PIN) } +pub fn setup_backup_region() { + let mut bu_mgr = bao1x_hal::buram::BackupManager::new(); + if !bu_mgr.is_backup_valid() { + // zeroize the backup RAM + bu_mgr.bu_ram_as_mut().fill(0); + // calculate the hash and mark as valid + bu_mgr.make_valid(); + + // setup the BIO, so the reset can also clear its registers and state for a clean BDMA pipeline + let mut bio_ss = xous_bio_bdma::BioSharedState::new(); + bio_ss.init(); + // must disable DMA filtering + bio_ss.bio.rmwf(utra::bio_bdma::SFR_CONFIG_DISABLE_FILTER_MEM, 1); + bio_ss.bio.rmwf(utra::bio_bdma::SFR_CONFIG_DISABLE_FILTER_PERI, 1); + + // stop all the machines, so that code can be loaded + bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0x0); + // reset all the fifos + bio_ss.bio.wo(utra::bio_bdma::SFR_FIFO_CLR, 0xF); + // setup clocking mode option + bio_ss.bio.rmwf(utralib::utra::bio_bdma::SFR_CONFIG_CLOCKING_MODE, 3 as u32); + + bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV0, 0x1_0000); + bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV1, 0x1_0000); + bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV2, 0x1_0000); + bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV3, 0x1_0000); + + // soft-reset the system + let mut rcurst = CSR::new(utra::sysctrl::HW_SYSCTRL_BASE as *mut u32); + rcurst.wo(utra::sysctrl::SFR_RCURST0, 0x55AA); + } +} + /// This can change the board type coding to a safer, simpler board type if the declared board type has /// problems booting. pub fn early_init(mut board_type: bao1x_api::BoardTypeCoding) -> (bao1x_api::BoardTypeCoding, u32) { + setup_backup_region(); + let iox = Iox::new(utra::iox::HW_IOX_BASE as *mut u32); // setup board-specific I/Os - early boot set. These are items that have to be From 31d0a6c691305c231d694fb87ff6dea0835e44a0 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 23 Nov 2025 09:05:03 +0800 Subject: [PATCH 4/4] use the opportunity to introduce a little jitter in the reboot timing --- bao1x-boot/boot1/src/platform/bao1x/bao1x.rs | 24 ++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/bao1x-boot/boot1/src/platform/bao1x/bao1x.rs b/bao1x-boot/boot1/src/platform/bao1x/bao1x.rs index 158e3a0ce..8077b27f4 100644 --- a/bao1x-boot/boot1/src/platform/bao1x/bao1x.rs +++ b/bao1x-boot/boot1/src/platform/bao1x/bao1x.rs @@ -66,7 +66,7 @@ pub fn setup_dabao_se0_pin(iox: &T) -> (IoxPort, u8) { (DABAO_SE0_PORT, DABAO_SE0_PIN) } -pub fn setup_backup_region() { +pub fn setup_backup_region() -> u32 { let mut bu_mgr = bao1x_hal::buram::BackupManager::new(); if !bu_mgr.is_backup_valid() { // zeroize the backup RAM @@ -93,16 +93,36 @@ pub fn setup_backup_region() { bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV2, 0x1_0000); bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV3, 0x1_0000); + let mut trng = bao1x_hal::sce::trng::Trng::new(utralib::utra::trng::HW_TRNG_BASE); + trng.setup_raw_generation(256); + trng.start(); + let mut delay: u16 = 0; + for _ in 0..8 { + delay ^= trng.get_u32().unwrap() as u16; + } + let mut acc = 1; + // one of ~1k slots for delay + for i in 0..delay & 0x3FF { + acc += i as u32; + } + // soft-reset the system let mut rcurst = CSR::new(utra::sysctrl::HW_SYSCTRL_BASE as *mut u32); rcurst.wo(utra::sysctrl::SFR_RCURST0, 0x55AA); + + // this is never returned, but guarantees the compiler does not optimize out the delay loop + acc + } else { + 0 } } /// This can change the board type coding to a safer, simpler board type if the declared board type has /// problems booting. pub fn early_init(mut board_type: bao1x_api::BoardTypeCoding) -> (bao1x_api::BoardTypeCoding, u32) { - setup_backup_region(); + if setup_backup_region() == 0 { + crate::println!("backup region is clean!"); + } let iox = Iox::new(utra::iox::HW_IOX_BASE as *mut u32);