From 84324017f24289324d31627b773e910706fd532f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Mon, 9 Dec 2024 11:57:01 +0100 Subject: [PATCH 1/2] Simplify VFS initialization --- components/boards/src/store.rs | 13 +++++++------ utils/nrf-debugging/walk.py | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/components/boards/src/store.rs b/components/boards/src/store.rs index bc989236..fe337639 100644 --- a/components/boards/src/store.rs +++ b/components/boards/src/store.rs @@ -1,6 +1,7 @@ use core::{ marker::PhantomData, mem::MaybeUninit, + ptr::addr_of_mut, sync::atomic::{AtomicBool, Ordering}, }; @@ -194,17 +195,17 @@ pub fn init_store( .compare_exchange_weak(false, true, Ordering::AcqRel, Ordering::Acquire) .expect("multiple instances of RunnerStore are not allowed"); - static mut VOLATILE_STORAGE: Option = None; - static mut VOLATILE_FS_ALLOC: Option> = None; - static mut VOLATILE_FS: Option> = None; + static mut VOLATILE_STORAGE: MaybeUninit = MaybeUninit::uninit(); + static mut VOLATILE_FS_ALLOC: MaybeUninit> = MaybeUninit::uninit(); + static mut VOLATILE_FS: MaybeUninit> = 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 vfs_storage = (&mut *addr_of_mut!(VOLATILE_STORAGE)).write(VolatileStorage::new()); + let vfs_alloc = (&mut *addr_of_mut!(VOLATILE_FS_ALLOC)).write(Filesystem::allocate()); let ifs = match init_ifs::(ifs_storage, ifs_alloc, efs_storage, status) { Ok(ifs) => B::ifs().insert(ifs), @@ -223,7 +224,7 @@ pub fn init_store( }; let vfs = match init_vfs(vfs_storage, vfs_alloc) { - Ok(vfs) => VOLATILE_FS.insert(vfs), + Ok(vfs) => (&mut *addr_of_mut!(VOLATILE_FS)).write(vfs), Err(_e) => { error!("VFS Mount Error {:?}", _e); panic!("VFS"); diff --git a/utils/nrf-debugging/walk.py b/utils/nrf-debugging/walk.py index dccd32f5..875b6775 100644 --- a/utils/nrf-debugging/walk.py +++ b/utils/nrf-debugging/walk.py @@ -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) From e8c0603c0a2ea0f751383b5d535651eb2e1d2231 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Tue, 10 Dec 2024 14:06:17 +0100 Subject: [PATCH 2/2] Use MaybeUninit for all stores This simplifies slightly the implementation and saves firwmare bytes as the zero values do not need to be included in the binary --- components/boards/src/store.rs | 86 +++++++++++++++++++--------------- 1 file changed, 48 insertions(+), 38 deletions(-) diff --git a/components/boards/src/store.rs b/components/boards/src/store.rs index fe337639..53e6e782 100644 --- a/components/boards/src/store.rs +++ b/components/boards/src/store.rs @@ -38,7 +38,7 @@ const_ram_storage!( #[allow(clippy::missing_safety_doc)] #[cfg(feature = "provisioner")] pub unsafe fn steal_internal_storage() -> &'static mut S::InternalStorage { - S::ifs_storage().as_mut().unwrap() + S::ifs_storage().assume_init_mut() } // FIXME: document safety @@ -47,14 +47,14 @@ pub trait StoragePointers: 'static { type InternalStorage: Storage; type ExternalStorage: Storage; - unsafe fn ifs_storage() -> &'static mut Option; - unsafe fn ifs_alloc() -> &'static mut Option>; - unsafe fn ifs() -> &'static mut Option>; + unsafe fn ifs_storage() -> &'static mut MaybeUninit; + unsafe fn ifs_alloc() -> &'static mut MaybeUninit>; + unsafe fn ifs() -> &'static mut MaybeUninit>; unsafe fn ifs_ptr() -> *mut Fs; - unsafe fn efs_storage() -> &'static mut Option; - unsafe fn efs_alloc() -> &'static mut Option>; - unsafe fn efs() -> &'static mut Option>; + unsafe fn efs_storage() -> &'static mut MaybeUninit; + unsafe fn efs_alloc() -> &'static mut MaybeUninit>; + unsafe fn efs() -> &'static mut MaybeUninit>; unsafe fn efs_ptr() -> *mut Fs; } @@ -68,57 +68,67 @@ macro_rules! impl_storage_pointers { type InternalStorage = $I; type ExternalStorage = $E; - unsafe fn ifs_storage() -> &'static mut Option { - static mut IFS_STORAGE: Option<$I> = None; + unsafe fn ifs_storage() -> &'static mut ::core::mem::MaybeUninit + { + 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> { - static mut IFS_ALLOC: Option<::littlefs2::fs::Allocation<$I>> = None; + unsafe fn ifs_alloc() -> &'static mut ::core::mem::MaybeUninit< + ::littlefs2::fs::Allocation, + > { + 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 { - 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 { - static mut EFS_STORAGE: Option<$E> = None; + unsafe fn efs_storage() -> &'static mut ::core::mem::MaybeUninit + { + 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> { - static mut EFS_ALLOC: Option<::littlefs2::fs::Allocation<$E>> = None; + unsafe fn efs_alloc() -> &'static mut ::core::mem::MaybeUninit< + ::littlefs2::fs::Allocation, + > { + 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 { - 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() } } @@ -200,15 +210,15 @@ pub fn init_store( static mut VOLATILE_FS: MaybeUninit> = 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 = (&mut *addr_of_mut!(VOLATILE_STORAGE)).write(VolatileStorage::new()); - let vfs_alloc = (&mut *addr_of_mut!(VOLATILE_FS_ALLOC)).write(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::(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"); @@ -216,7 +226,7 @@ pub fn init_store( }; let efs = match init_efs::(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"); @@ -224,7 +234,7 @@ pub fn init_store( }; let vfs = match init_vfs(vfs_storage, vfs_alloc) { - Ok(vfs) => (&mut *addr_of_mut!(VOLATILE_FS)).write(vfs), + Ok(vfs) => (*addr_of_mut!(VOLATILE_FS)).write(vfs), Err(_e) => { error!("VFS Mount Error {:?}", _e); panic!("VFS");