migrate pddb size to board:: and fix boot init

the policy is to format the PDDB automatically on boot if
it can't be mounted.

Need to think about whether this is the right thing to do
or not for the product: if there is any sort of error, it will
just wipe the PDDB entirely, making it unrecoverable.

That being said, why would there be an error? if that happens,
something bad probably happened to the hardware. I feel like
if the SPI interface is reliable this shouldn't be a problem.
So the solution is to make sure the hardware is reliable...
on the other hand, fundamentally, SPI flash can wear out, so
this could aggravate problems at EOL for the device (i.e.
it will take small problems and "amplify them up" into a huge
problem).

The trade-off is...the auto-format makes the token "just work"
on boot. Maybe the right thing to do is to add some code that
checks to see if we're in a true blank state, and if so,
autoformat; and if it looks like something was there before,
prompt for a format?

tbd.
This commit is contained in:
bunnie 2025-05-08 16:06:42 +08:00
parent f92839602e
commit c7225d227d
4 changed files with 29 additions and 39 deletions

View file

@ -138,7 +138,7 @@ impl HostedSpinor {
pub fn bulk_erase(&self, start: u32, len: u32) -> Result<(), xous::Error> { pub fn bulk_erase(&self, start: u32, len: u32) -> Result<(), xous::Error> {
for b in flashmem().memory.as_mut_slice() for b in flashmem().memory.as_mut_slice()
[(start - xous::PDDB_LOC) as usize..(start - xous::PDDB_LOC + len) as usize] [(start - PDDB_A_LOC) as usize..(start - PDDB_A_LOC + len) as usize]
.iter_mut() .iter_mut()
{ {
*b = 0xFF; *b = 0xFF;

View file

@ -15,9 +15,7 @@ use aes::{Aes256, BLOCK_SIZE, Block};
use aes_gcm_siv::aead::{Aead, Payload}; use aes_gcm_siv::aead::{Aead, Payload};
use aes_gcm_siv::{Aes256GcmSiv, Nonce, Tag}; use aes_gcm_siv::{Aes256GcmSiv, Nonce, Tag};
use backend::bcrypt::*; use backend::bcrypt::*;
#[cfg(all(feature = "gen2", feature = "cramium-emu"))] #[cfg(all(feature = "gen2"))]
use cramium_emu::{SPINOR_BULK_ERASE_SIZE, SPINOR_ERASE_SIZE};
#[cfg(all(feature = "gen2", not(feature = "cramium-emu")))]
use cramium_hal::board::{SPINOR_BULK_ERASE_SIZE, SPINOR_ERASE_SIZE}; use cramium_hal::board::{SPINOR_BULK_ERASE_SIZE, SPINOR_ERASE_SIZE};
#[cfg(feature = "gen1")] #[cfg(feature = "gen1")]
use keystore_api::AesRootkeyType; use keystore_api::AesRootkeyType;
@ -28,6 +26,7 @@ use sha2::{Digest, Sha512_256Hw, Sha512_256Sw};
use spinor::{SPINOR_BULK_ERASE_SIZE, SPINOR_ERASE_SIZE}; use spinor::{SPINOR_BULK_ERASE_SIZE, SPINOR_ERASE_SIZE};
use subtle::ConstantTimeEq; use subtle::ConstantTimeEq;
use crate::api::PDDB_A_LOC;
use crate::*; use crate::*;
#[cfg(not(feature = "deterministic"))] #[cfg(not(feature = "deterministic"))]
@ -208,7 +207,7 @@ impl PddbOs {
let xns = xous_names::XousNames::new().unwrap(); let xns = xous_names::XousNames::new().unwrap();
#[cfg(any(feature = "precursor", feature = "renode"))] #[cfg(any(feature = "precursor", feature = "renode"))]
let pddb = xous::syscall::map_memory( let pddb = xous::syscall::map_memory(
xous::MemoryAddress::new(xous::PDDB_LOC as usize + xous::FLASH_PHYS_BASE as usize), xous::MemoryAddress::new(PDDB_A_LOC as usize + xous::FLASH_PHYS_BASE as usize),
None, None,
PDDB_A_LEN as usize, PDDB_A_LEN as usize,
xous::MemoryFlags::R | xous::MemoryFlags::RESERVE, xous::MemoryFlags::R | xous::MemoryFlags::RESERVE,
@ -428,7 +427,7 @@ impl PddbOs {
self.spinor self.spinor
.patch( .patch(
unsafe { self.pddb_mr.as_slice() }, unsafe { self.pddb_mr.as_slice() },
xous::PDDB_LOC, PDDB_A_LOC,
&data, &data,
offset + self.data_phys_base.as_u32(), offset + self.data_phys_base.as_u32(),
) )
@ -452,7 +451,7 @@ impl PddbOs {
self.spinor self.spinor
.patch( .patch(
unsafe { self.pddb_mr.as_slice() }, unsafe { self.pddb_mr.as_slice() },
xous::PDDB_LOC, PDDB_A_LOC,
&mbbb, &mbbb,
self.pt_phys_base.as_u32() + erased_offset, self.pt_phys_base.as_u32() + erased_offset,
) )
@ -490,7 +489,7 @@ impl PddbOs {
self.spinor self.spinor
.patch( .patch(
unsafe { self.pddb_mr.as_slice() }, unsafe { self.pddb_mr.as_slice() },
xous::PDDB_LOC, PDDB_A_LOC,
&blank, &blank,
self.pt_phys_base.as_u32() + base_page as u32, self.pt_phys_base.as_u32() + base_page as u32,
) )
@ -507,12 +506,7 @@ impl PddbOs {
"attempt to patch past page table end" "attempt to patch past page table end"
); );
self.spinor self.spinor
.patch( .patch(unsafe { self.pddb_mr.as_slice() }, PDDB_A_LOC, &data, self.pt_phys_base.as_u32() + offset)
unsafe { self.pddb_mr.as_slice() },
xous::PDDB_LOC,
&data,
self.pt_phys_base.as_u32() + offset,
)
.expect("couldn't write to page table"); .expect("couldn't write to page table");
} }
@ -523,12 +517,7 @@ impl PddbOs {
); );
log::info!("patching keys area with {} bytes", data.len()); log::info!("patching keys area with {} bytes", data.len());
self.spinor self.spinor
.patch( .patch(unsafe { self.pddb_mr.as_slice() }, PDDB_A_LOC, data, self.key_phys_base.as_u32() + offset)
unsafe { self.pddb_mr.as_slice() },
xous::PDDB_LOC,
data,
self.key_phys_base.as_u32() + offset,
)
.expect("couldn't burn keys"); .expect("couldn't burn keys");
} }
@ -537,7 +526,7 @@ impl PddbOs {
self.spinor self.spinor
.patch( .patch(
unsafe { self.pddb_mr.as_slice() }, unsafe { self.pddb_mr.as_slice() },
xous::PDDB_LOC, PDDB_A_LOC,
data, data,
self.mbbb_phys_base.as_u32() + offset, self.mbbb_phys_base.as_u32() + offset,
) )
@ -551,7 +540,7 @@ impl PddbOs {
self.spinor self.spinor
.patch( .patch(
unsafe { self.pddb_mr.as_slice() }, unsafe { self.pddb_mr.as_slice() },
xous::PDDB_LOC, PDDB_A_LOC,
data, data,
self.fscb_phys_base.as_u32() + offset, self.fscb_phys_base.as_u32() + offset,
) )
@ -807,7 +796,10 @@ impl PddbOs {
} }
#[cfg(feature = "gen2")] #[cfg(feature = "gen2")]
pub(crate) fn ensure_password(&self) -> PasswordState { self.rootkeys.ensure_password() } pub(crate) fn ensure_password(&mut self) -> PasswordState {
self.rootkeys.ensure_password();
self.try_login()
}
pub(crate) fn try_login(&mut self) -> PasswordState { pub(crate) fn try_login(&mut self) -> PasswordState {
use aes::cipher::KeyInit; use aes::cipher::KeyInit;
@ -2098,27 +2090,27 @@ impl PddbOs {
modals modals
.start_progress( .start_progress(
t!("pddb.erase", locales::LANG), t!("pddb.erase", locales::LANG),
xous::PDDB_LOC, PDDB_A_LOC,
xous::PDDB_LOC + PDDB_A_LEN as u32, PDDB_A_LOC + PDDB_A_LEN as u32,
xous::PDDB_LOC, PDDB_A_LOC,
) )
.expect("couldn't raise progress bar"); .expect("couldn't raise progress bar");
// retain this delay, because the next section is so compute-intensive, it may take a // retain this delay, because the next section is so compute-intensive, it may take a
// while for the GAM to catch up. // while for the GAM to catch up.
self.tt.sleep_ms(100).unwrap(); self.tt.sleep_ms(100).unwrap();
} }
for offset in (xous::PDDB_LOC..(xous::PDDB_LOC + PDDB_A_LEN as u32)) for offset in
.step_by(SPINOR_BULK_ERASE_SIZE as usize) (PDDB_A_LOC..(PDDB_A_LOC + PDDB_A_LEN as u32)).step_by(SPINOR_BULK_ERASE_SIZE as usize)
{ {
if (offset / SPINOR_BULK_ERASE_SIZE) % 4 == 0 { if (offset / SPINOR_BULK_ERASE_SIZE) % 4 == 0 {
log::info!("Initial erase: {}/{}", offset - xous::PDDB_LOC, PDDB_A_LEN as u32); log::info!("Initial erase: {}/{}", offset - PDDB_A_LOC, PDDB_A_LEN as u32);
if let Some(modals) = progress { if let Some(modals) = progress {
modals.update_progress(offset as u32).expect("couldn't update progress bar"); modals.update_progress(offset as u32).expect("couldn't update progress bar");
} }
} }
// do a blank check first to see if the sector really needs erasing // do a blank check first to see if the sector really needs erasing
let mut blank = true; let mut blank = true;
let slice_start = (offset - xous::PDDB_LOC) as usize / size_of::<u32>(); let slice_start = (offset - PDDB_A_LOC) as usize / size_of::<u32>();
for word in unsafe { for word in unsafe {
self.pddb_mr.as_slice::<u32>() self.pddb_mr.as_slice::<u32>()
[slice_start..slice_start + SPINOR_BULK_ERASE_SIZE as usize / size_of::<u32>()] [slice_start..slice_start + SPINOR_BULK_ERASE_SIZE as usize / size_of::<u32>()]
@ -2134,9 +2126,7 @@ impl PddbOs {
} }
} }
if let Some(modals) = progress { if let Some(modals) = progress {
modals modals.update_progress(PDDB_A_LOC + PDDB_A_LEN as u32).expect("couldn't update progress bar");
.update_progress(xous::PDDB_LOC + PDDB_A_LEN as u32)
.expect("couldn't update progress bar");
modals.finish_progress().expect("couldn't dismiss progress bar"); modals.finish_progress().expect("couldn't dismiss progress bar");
#[cfg(feature = "ux-swap-delay")] #[cfg(feature = "ux-swap-delay")]
self.tt.sleep_ms(100).unwrap(); self.tt.sleep_ms(100).unwrap();
@ -2332,7 +2322,7 @@ impl PddbOs {
} }
} }
self.spinor self.spinor
.patch(unsafe { self.pddb_mr.as_slice() }, xous::PDDB_LOC, &temp, offset as u32) .patch(unsafe { self.pddb_mr.as_slice() }, PDDB_A_LOC, &temp, offset as u32)
.expect("couldn't fill in disk with random datax"); .expect("couldn't fill in disk with random datax");
} }
if let Some(modals) = progress { if let Some(modals) = progress {
@ -2503,7 +2493,7 @@ impl PddbOs {
self.spinor self.spinor
.patch( .patch(
unsafe { self.pddb_mr.as_slice() }, unsafe { self.pddb_mr.as_slice() },
xous::PDDB_LOC, PDDB_A_LOC,
&mbbb, &mbbb,
self.pt_phys_base.as_u32() + erased_offset, self.pt_phys_base.as_u32() + erased_offset,
) )
@ -3087,7 +3077,7 @@ impl PddbOs {
pub(crate) fn pddb_get_all_keys<'a>( pub(crate) fn pddb_get_all_keys<'a>(
&'a self, &'a self,
cache: &'a Vec<BasisCacheEntry>, cache: &'a Vec<BasisCacheEntry>,
_op: GetKeyOp, // _op: GetKeyOp,
) -> Option<Vec<(BasisKeys, String)>> { ) -> Option<Vec<(BasisKeys, String)>> {
// populate the "known" entries // populate the "known" entries
let mut ret = Vec::<(BasisKeys, String)>::new(); let mut ret = Vec::<(BasisKeys, String)>::new();
@ -3240,7 +3230,7 @@ impl PddbOs {
self.spinor self.spinor
.patch( .patch(
self.pddb_mr.as_slice(), self.pddb_mr.as_slice(),
xous::PDDB_LOC, PDDB_A_LOC,
&page, &page,
self.pt_phys_base.as_u32() + (page_index * PAGE_SIZE) as u32, self.pt_phys_base.as_u32() + (page_index * PAGE_SIZE) as u32,
) )

View file

@ -150,7 +150,7 @@ impl DerefMut for Pte {
pub const PDDB_SIZE_PAGES: usize = crate::PDDB_A_LEN as usize / PAGE_SIZE; pub const PDDB_SIZE_PAGES: usize = crate::PDDB_A_LEN as usize / PAGE_SIZE;
/// This structure is mapped into the top of FLASH memory, starting at /// This structure is mapped into the top of FLASH memory, starting at
/// xous::PDDB_LOC. This actually slightly over-sizes the page table, /// PDDB_A_LOC. This actually slightly over-sizes the page table,
/// because the page table does not map the locations for the page table /// because the page table does not map the locations for the page table
/// itself, the MBBB, or the FSCB. However, the 0th entry of the page table /// itself, the MBBB, or the FSCB. However, the 0th entry of the page table
/// always corresponds to the base of data in FLASH, which means the excess /// always corresponds to the base of data in FLASH, which means the excess

View file

@ -3,7 +3,7 @@ use core::mem::size_of;
use std::convert::TryInto; use std::convert::TryInto;
use aes_gcm_siv::Nonce; use aes_gcm_siv::Nonce;
#[cfg(feature = "cramium-hal")] #[cfg(all(feature = "cramium-hal", not(feature = "hosted-baosec")))]
use cram_hal_service::trng::Trng; use cram_hal_service::trng::Trng;
#[cfg(feature = "hosted-baosec")] #[cfg(feature = "hosted-baosec")]
use cramium_emu::trng::Trng; use cramium_emu::trng::Trng;