Skip to content

Commit

Permalink
Factorize code
Browse files Browse the repository at this point in the history
  • Loading branch information
yogh333 committed Sep 27, 2024
1 parent 1e96d5d commit ee96b88
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions ledger_device_sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl<T> NVMData<T> {
/// because a static mutable will be assumed to be located in
/// RAM, and be accessed through the static base (r9)
#[cfg(not(target_os = "nanos"))]
pub fn get_mut(&mut self) -> &mut T {
fn get_addr(&self) -> *mut T {
use core::arch::asm;
unsafe {
// Compute offset in .nvm_data by taking the reference to
Expand All @@ -145,25 +145,22 @@ impl<T> NVMData<T> {
asm!( "mov {}, r9", out(reg) static_base);
let offset = (addr - static_base) as isize;
let data_addr = (_nvm_data_start as *const u8).offset(offset);
let pic_addr =
ledger_secure_sdk_sys::pic(data_addr as *mut core::ffi::c_void) as *mut T;
ledger_secure_sdk_sys::pic(data_addr as *mut core::ffi::c_void) as *mut T
}
}

#[cfg(not(target_os = "nanos"))]
pub fn get_mut(&mut self) -> &mut T {
unsafe {
let pic_addr = self.get_addr();
&mut *pic_addr.cast()
}
}

#[cfg(not(target_os = "nanos"))]
pub fn get_ref(&mut self) -> &T {
use core::arch::asm;
unsafe {
// Compute offset in .nvm_data by taking the reference to
// self.data and subtracting r9
let addr = &self.data as *const T as u32;
let static_base: u32;
asm!( "mov {}, r9", out(reg) static_base);
let offset = (addr - static_base) as isize;
let data_addr = (_nvm_data_start as *const u8).offset(offset);
let pic_addr =
ledger_secure_sdk_sys::pic(data_addr as *mut core::ffi::c_void) as *const T;
let pic_addr = self.get_addr();
&*pic_addr.cast()
}
}
Expand Down

0 comments on commit ee96b88

Please sign in to comment.