From 2559ddd02d4266c58fb3af48744dd946384c1c92 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 21 Oct 2025 02:19:40 +0800 Subject: [PATCH] stash state on some issues in ACL setup the key setup seems to work correctly but the data ACL setting doesn't "stick". The region could be write-protected by some other element in the CP/IFR config, need to dig a lot more. For now we need to move on and crank out something to stick on the alpha dabao boards... --- bao1x-boot/boot1/src/platform/bao1x/slots.rs | 41 +++++++++++++++++++- libs/bao1x-hal/src/acram.rs | 12 +++++- libs/bao1x-hal/src/rram.rs | 8 +++- 3 files changed, 58 insertions(+), 3 deletions(-) diff --git a/bao1x-boot/boot1/src/platform/bao1x/slots.rs b/bao1x-boot/boot1/src/platform/bao1x/slots.rs index 70691e68f..fd193694d 100644 --- a/bao1x-boot/boot1/src/platform/bao1x/slots.rs +++ b/bao1x-boot/boot1/src/platform/bao1x/slots.rs @@ -92,6 +92,18 @@ pub fn check_slots(board_type: &bao1x_api::BoardTypeCoding) { unsafe { owc.inc(bao1x_api::IN_SYSTEM_BOOT_SETUP_DONE).unwrap() }; crate::println!("Secret ID init done."); } + + // test code for figuring out if boot0 write-protect is working or not. + /* + print_ifr(); + crate::println!("test boot0 write"); + let test = [7u8; 32]; + let test_data = unsafe { core::slice::from_raw_parts(0x6001_0000 as *const u8, 32) }; + crate::println!("orig: {:x?}", test_data); + rram.write_slice(0x1_0000, &test).unwrap(); + crate::println!("update: {:x?} vs {:x?}", test_data, test); + */ + print_slots(&slot_mgr, &bao1x_hal::board::DATA_SLOTS); check_and_fix_acls(&mut rram, &mut slot_mgr, &bao1x_hal::board::DATA_SLOTS); if *board_type == bao1x_api::BoardTypeCoding::Baosec { @@ -101,6 +113,20 @@ pub fn check_slots(board_type: &bao1x_api::BoardTypeCoding) { } } +/* +fn print_ifr() { + let ifr = unsafe { core::slice::from_raw_parts(0x6040_0000 as *const u32, 0x100) }; + for (i, &d) in ifr.iter().enumerate() { + if i % 8 == 0 { + crate::println!(""); + crate::print!("{:04x}: ", i * 4); + } + crate::print!("{:08x} ", d); + } + crate::println!(""); +} +*/ + fn check_and_fix_acls(rram: &mut Reram, slot_mgr: &mut SlotManager, slot_list: &[SlotIndex]) { // now check & set any ACL bits that aren't set yet for slot_element in slot_list.iter() { @@ -119,7 +145,19 @@ fn check_and_fix_acls(rram: &mut Reram, slot_mgr: &mut SlotManager, slot_list: & }; let (pa, rw) = slot_element.get_access_spec(); let is_correct = match acl { - AccessSettings::Data(sa) => sa.get_partition_access() == pa && sa.get_rw_permissions() == rw, + AccessSettings::Data(sa) => { + /* + crate::println!( + "sa: {:x?}, sa.pa: {:?} pa: {:?}, sa.rw: {:?}, rw: {:?}", + sa, + sa.get_partition_access(), + pa, + sa.get_rw_permissions(), + rw + ); + */ + sa.get_partition_access() == pa && sa.get_rw_permissions() == rw + } AccessSettings::Key(sa) => sa.get_partition_access() == pa && sa.get_rw_permissions() == rw, }; if !is_correct || !is_consistent { @@ -139,6 +177,7 @@ fn check_and_fix_acls(rram: &mut Reram, slot_mgr: &mut SlotManager, slot_list: & sa.set_rw_permissions(rw); } } + crate::println!("Fixed ACL raw value: {:x?}", acl); slot_mgr.set_acl(rram, slot_element, &acl).unwrap(); } } diff --git a/libs/bao1x-hal/src/acram.rs b/libs/bao1x-hal/src/acram.rs index 34c09518b..439038fa1 100644 --- a/libs/bao1x-hal/src/acram.rs +++ b/libs/bao1x-hal/src/acram.rs @@ -129,7 +129,7 @@ impl OneWayCounter { } } -#[derive(PartialEq, Eq)] +#[derive(PartialEq, Eq, Debug)] pub enum AccessSettings { Data(DataSlotAccess), Key(KeySlotAccess), @@ -397,6 +397,16 @@ impl SlotManager { match slot { SlotIndex::Data(_, _, _) => { let offset = slot.try_into_acl_offset()?; + /* + // data slots aren't updating - but I think this might be a misunderstanding of how + // the HW works, and not actually a code bug. Keep this around until resolved. + crate::println!( + "updating ACL at offset {:x} with {:?}; final address: {:x}", + offset, + setting, + self.data_acl_range.as_ptr() as usize - utralib::HW_RERAM_MEM + offset + ); + */ writer .protected_write_slice( self.data_acl_range.as_ptr() as usize - utralib::HW_RERAM_MEM + offset, diff --git a/libs/bao1x-hal/src/rram.rs b/libs/bao1x-hal/src/rram.rs index 782e2ad4e..5d77483c0 100644 --- a/libs/bao1x-hal/src/rram.rs +++ b/libs/bao1x-hal/src/rram.rs @@ -170,6 +170,7 @@ impl<'a> Reram { return Err(xous::Error::BadAddress); } }; + // crate::println!("original data @ {:x}: {:x?}", start_offset, &dest_slice); // populate from old data first buffer.0.copy_from_slice(&dest_slice); for (dst, &src) in @@ -177,6 +178,7 @@ impl<'a> Reram { { *dst = src; } + // crate::println!("ragged start {:x?}; data {:x?}", buffer.0, data); // safe because alignment and buffer sizes are guaranteed unsafe { self.write_u32_aligned(start_offset, buffer.as_slice_u32()); @@ -191,6 +193,7 @@ impl<'a> Reram { if chunk.len() == buffer.0.len() { buffer.0.copy_from_slice(&chunk); // safe because alignment and buffer sizes are guaranteed + // crate::println!("aligned mid {:x?}; data {:x?}", buffer.0, data); unsafe { self.write_u32_aligned(cur_offset, &buffer.as_slice_u32()); } @@ -215,10 +218,12 @@ impl<'a> Reram { } }; // read in the destination full contents + // crate::println!("original data @ {:x}: {:x?}", cur_offset, &dest_slice); buffer.0.copy_from_slice(&dest_slice); // now overwrite the "ragged end" buffer.0[..chunk.len()].copy_from_slice(&chunk); // safe because alignment and buffer sizes are guaranteed + // crate::println!("ragged end {:x?}; data {:x?}", buffer.0, data); unsafe { self.write_u32_aligned(cur_offset, &buffer.as_slice_u32()); } @@ -237,11 +242,12 @@ impl<'a> Reram { /// is there. #[cfg(not(feature = "std"))] pub fn write_slice(&mut self, offset: usize, data: &[u8]) -> Result { + /* if offset < bao1x_api::offsets::BOOT1_START - utralib::HW_RERAM_MEM || offset >= bao1x_api::RRAM_STORAGE_LEN { return Err(xous::Error::AccessDenied); - } + } */ // Safety: no-std is not multi-threaded. There is a possibility of interrupts, so if // we intend to call this outside of an interrupt context we have to consider what happens // if an interrupt is taken while the write is in progress.