Skip to content

Commit

Permalink
Merge pull request #569 from Nitrokey/simpler-vfs
Browse files Browse the repository at this point in the history
Simplify VFS initialization
  • Loading branch information
sosthene-nitrokey authored Dec 16, 2024
2 parents 8bfc4fb + e8c0603 commit afd6a8e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 42 deletions.
93 changes: 52 additions & 41 deletions components/boards/src/store.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use core::{
marker::PhantomData,
mem::MaybeUninit,
ptr::addr_of_mut,
sync::atomic::{AtomicBool, Ordering},
};

Expand Down Expand Up @@ -37,7 +38,7 @@ const_ram_storage!(
#[allow(clippy::missing_safety_doc)]
#[cfg(feature = "provisioner")]
pub unsafe fn steal_internal_storage<S: StoragePointers>() -> &'static mut S::InternalStorage {
S::ifs_storage().as_mut().unwrap()
S::ifs_storage().assume_init_mut()
}

// FIXME: document safety
Expand All @@ -46,14 +47,14 @@ pub trait StoragePointers: 'static {
type InternalStorage: Storage;
type ExternalStorage: Storage;

unsafe fn ifs_storage() -> &'static mut Option<Self::InternalStorage>;
unsafe fn ifs_alloc() -> &'static mut Option<Allocation<Self::InternalStorage>>;
unsafe fn ifs() -> &'static mut Option<Filesystem<'static, Self::InternalStorage>>;
unsafe fn ifs_storage() -> &'static mut MaybeUninit<Self::InternalStorage>;
unsafe fn ifs_alloc() -> &'static mut MaybeUninit<Allocation<Self::InternalStorage>>;
unsafe fn ifs() -> &'static mut MaybeUninit<Filesystem<'static, Self::InternalStorage>>;
unsafe fn ifs_ptr() -> *mut Fs<Self::InternalStorage>;

unsafe fn efs_storage() -> &'static mut Option<Self::ExternalStorage>;
unsafe fn efs_alloc() -> &'static mut Option<Allocation<Self::ExternalStorage>>;
unsafe fn efs() -> &'static mut Option<Filesystem<'static, Self::ExternalStorage>>;
unsafe fn efs_storage() -> &'static mut MaybeUninit<Self::ExternalStorage>;
unsafe fn efs_alloc() -> &'static mut MaybeUninit<Allocation<Self::ExternalStorage>>;
unsafe fn efs() -> &'static mut MaybeUninit<Filesystem<'static, Self::ExternalStorage>>;
unsafe fn efs_ptr() -> *mut Fs<Self::ExternalStorage>;
}

Expand All @@ -67,57 +68,67 @@ macro_rules! impl_storage_pointers {
type InternalStorage = $I;
type ExternalStorage = $E;

unsafe fn ifs_storage() -> &'static mut Option<Self::InternalStorage> {
static mut IFS_STORAGE: Option<$I> = None;
unsafe fn ifs_storage() -> &'static mut ::core::mem::MaybeUninit<Self::InternalStorage>
{
static mut IFS_STORAGE: ::core::mem::MaybeUninit<$I> =
::core::mem::MaybeUninit::uninit();
#[allow(static_mut_refs)]
&mut IFS_STORAGE
}

unsafe fn ifs_alloc(
) -> &'static mut Option<::littlefs2::fs::Allocation<Self::InternalStorage>> {
static mut IFS_ALLOC: Option<::littlefs2::fs::Allocation<$I>> = None;
unsafe fn ifs_alloc() -> &'static mut ::core::mem::MaybeUninit<
::littlefs2::fs::Allocation<Self::InternalStorage>,
> {
static mut IFS_ALLOC: ::core::mem::MaybeUninit<::littlefs2::fs::Allocation<$I>> =
::core::mem::MaybeUninit::uninit();
#[allow(static_mut_refs)]
&mut IFS_ALLOC
}

unsafe fn ifs(
) -> &'static mut Option<::littlefs2::fs::Filesystem<'static, Self::InternalStorage>>
{
static mut IFS: Option<::littlefs2::fs::Filesystem<$I>> = None;
unsafe fn ifs() -> &'static mut ::core::mem::MaybeUninit<
::littlefs2::fs::Filesystem<'static, Self::InternalStorage>,
> {
static mut IFS: ::core::mem::MaybeUninit<::littlefs2::fs::Filesystem<$I>> =
::core::mem::MaybeUninit::uninit();
#[allow(static_mut_refs)]
&mut IFS
}

unsafe fn ifs_ptr() -> *mut ::trussed::store::Fs<Self::InternalStorage> {
use ::core::mem::MaybeUninit;
static mut IFS: MaybeUninit<::trussed::store::Fs<$I>> = MaybeUninit::uninit();
static mut IFS: ::core::mem::MaybeUninit<::trussed::store::Fs<$I>> =
::core::mem::MaybeUninit::uninit();
IFS.as_mut_ptr()
}

unsafe fn efs_storage() -> &'static mut Option<Self::ExternalStorage> {
static mut EFS_STORAGE: Option<$E> = None;
unsafe fn efs_storage() -> &'static mut ::core::mem::MaybeUninit<Self::ExternalStorage>
{
static mut EFS_STORAGE: ::core::mem::MaybeUninit<$E> =
::core::mem::MaybeUninit::uninit();
#[allow(static_mut_refs)]
&mut EFS_STORAGE
}

unsafe fn efs_alloc(
) -> &'static mut Option<::littlefs2::fs::Allocation<Self::ExternalStorage>> {
static mut EFS_ALLOC: Option<::littlefs2::fs::Allocation<$E>> = None;
unsafe fn efs_alloc() -> &'static mut ::core::mem::MaybeUninit<
::littlefs2::fs::Allocation<Self::ExternalStorage>,
> {
static mut EFS_ALLOC: ::core::mem::MaybeUninit<::littlefs2::fs::Allocation<$E>> =
::core::mem::MaybeUninit::uninit();
#[allow(static_mut_refs)]
&mut EFS_ALLOC
}

unsafe fn efs(
) -> &'static mut Option<::littlefs2::fs::Filesystem<'static, Self::ExternalStorage>>
{
static mut EFS: Option<::littlefs2::fs::Filesystem<$E>> = None;
unsafe fn efs() -> &'static mut ::core::mem::MaybeUninit<
::littlefs2::fs::Filesystem<'static, Self::ExternalStorage>,
> {
static mut EFS: ::core::mem::MaybeUninit<::littlefs2::fs::Filesystem<$E>> =
::core::mem::MaybeUninit::uninit();
#[allow(static_mut_refs)]
&mut EFS
}

unsafe fn efs_ptr() -> *mut ::trussed::store::Fs<Self::ExternalStorage> {
use ::core::mem::MaybeUninit;
static mut EFS: MaybeUninit<::trussed::store::Fs<$E>> = MaybeUninit::uninit();
static mut EFS: ::core::mem::MaybeUninit<::trussed::store::Fs<$E>> =
::core::mem::MaybeUninit::uninit();
EFS.as_mut_ptr()
}
}
Expand Down Expand Up @@ -194,36 +205,36 @@ pub fn init_store<B: Board>(
.compare_exchange_weak(false, true, Ordering::AcqRel, Ordering::Acquire)
.expect("multiple instances of RunnerStore are not allowed");

static mut VOLATILE_STORAGE: Option<VolatileStorage> = None;
static mut VOLATILE_FS_ALLOC: Option<Allocation<VolatileStorage>> = None;
static mut VOLATILE_FS: Option<Filesystem<VolatileStorage>> = None;
static mut VOLATILE_STORAGE: MaybeUninit<VolatileStorage> = MaybeUninit::uninit();
static mut VOLATILE_FS_ALLOC: MaybeUninit<Allocation<VolatileStorage>> = MaybeUninit::uninit();
static mut VOLATILE_FS: MaybeUninit<Filesystem<VolatileStorage>> = MaybeUninit::uninit();

unsafe {
let ifs_storage = B::ifs_storage().insert(int_flash);
let ifs_alloc = B::ifs_alloc().insert(Filesystem::allocate());
let efs_storage = B::efs_storage().insert(ext_flash);
let efs_alloc = B::efs_alloc().insert(Filesystem::allocate());
let vfs_storage = VOLATILE_STORAGE.insert(VolatileStorage::new());
let vfs_alloc = VOLATILE_FS_ALLOC.insert(Filesystem::allocate());
let ifs_storage = B::ifs_storage().write(int_flash);
let ifs_alloc = B::ifs_alloc().write(Filesystem::allocate());
let efs_storage = B::efs_storage().write(ext_flash);
let efs_alloc = B::efs_alloc().write(Filesystem::allocate());
let vfs_storage = (*addr_of_mut!(VOLATILE_STORAGE)).write(VolatileStorage::new());
let vfs_alloc = (*addr_of_mut!(VOLATILE_FS_ALLOC)).write(Filesystem::allocate());

let ifs = match init_ifs::<B>(ifs_storage, ifs_alloc, efs_storage, status) {
Ok(ifs) => B::ifs().insert(ifs),
Ok(ifs) => B::ifs().write(ifs),
Err(_e) => {
error!("IFS Mount Error {:?}", _e);
panic!("IFS");
}
};

let efs = match init_efs::<B>(efs_storage, efs_alloc, simulated_efs, status) {
Ok(efs) => B::efs().insert(efs),
Ok(efs) => B::efs().write(efs),
Err(_e) => {
error!("EFS Mount Error {:?}", _e);
panic!("EFS");
}
};

let vfs = match init_vfs(vfs_storage, vfs_alloc) {
Ok(vfs) => VOLATILE_FS.insert(vfs),
Ok(vfs) => (*addr_of_mut!(VOLATILE_FS)).write(vfs),
Err(_e) => {
error!("VFS Mount Error {:?}", _e);
panic!("VFS");
Expand Down
2 changes: 1 addition & 1 deletion utils/nrf-debugging/walk.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
read_size = args.read_size
prog_size = args.prog_size

block_count = img_size / block_size
block_count = img_size // block_size
if block_count * block_size != img_size:
print("image size should be a multiple of block size")
exit(1)
Expand Down

0 comments on commit afd6a8e

Please sign in to comment.