mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-15 08:30:02 +00:00
Merge pull request #739 from betrusted-io/sec-ci
initialize backup register region
This commit is contained in:
commit
428845777b
4 changed files with 206 additions and 0 deletions
|
|
@ -66,9 +66,64 @@ pub fn setup_dabao_se0_pin<T: IoSetup + IoGpio>(iox: &T) -> (IoxPort, u8) {
|
|||
(DABAO_SE0_PORT, DABAO_SE0_PIN)
|
||||
}
|
||||
|
||||
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
|
||||
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);
|
||||
|
||||
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) {
|
||||
if setup_backup_region() == 0 {
|
||||
crate::println!("backup region is clean!");
|
||||
}
|
||||
|
||||
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
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
146
libs/bao1x-hal/src/buram.rs
Normal file
146
libs/bao1x-hal/src/buram.rs
Normal file
|
|
@ -0,0 +1,146 @@
|
|||
use core::ops::Range;
|
||||
|
||||
const KEY_RANGE: Range<usize> = 2..8;
|
||||
const KEY_LEN: usize = range_len(KEY_RANGE) * size_of::<u32>();
|
||||
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>) -> 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::<u32>(),
|
||||
)
|
||||
.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::<u32>(),
|
||||
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
|
||||
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
|
||||
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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)
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Reference in a new issue