Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Y333/settings nvm management #192

Merged
merged 8 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ledger_device_sdk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ledger_device_sdk"
version = "1.17.0"
version = "1.17.1"
authors = ["yhql", "yogh333", "agrojean-ledger", "kingofpayne"]
edition = "2021"
license.workspace = true
Expand Down
20 changes: 17 additions & 3 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,11 +145,25 @@ 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(&self) -> &T {
unsafe {
let pic_addr = self.get_addr();
&*pic_addr.cast()
}
}
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion ledger_device_sdk/src/nbgl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ trait SyncNBGL: Sized {
}
}

fn ux_sync_wait(&mut self, exit_on_apdu: bool) -> SyncNbgl {
fn ux_sync_wait(&self, exit_on_apdu: bool) -> SyncNbgl {
unsafe {
if let Some(comm) = COMM_REF.as_mut() {
while !G_ENDED {
Expand Down
2 changes: 1 addition & 1 deletion ledger_device_sdk/src/nbgl/nbgl_address_review.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl<'a> NbglAddressReview<'a> {
}
}

pub fn show(&mut self, address: &str) -> bool {
pub fn show(&self, address: &str) -> bool {
unsafe {
let icon: nbgl_icon_details_t = match self.glyph {
Some(g) => g.into(),
Expand Down
2 changes: 1 addition & 1 deletion ledger_device_sdk/src/nbgl/nbgl_choice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl<'a> NbglChoice<'a> {
}

pub fn show(
&mut self,
&self,
message: &str,
sub_message: &str,
confirm_text: &str,
Expand Down
14 changes: 7 additions & 7 deletions ledger_device_sdk/src/nbgl/nbgl_generic_review.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ impl InfoButton {
/// using the NbglGenericReview struct.
pub struct TagValueList {
pairs: Vec<nbgl_contentTagValue_t>,
items: Vec<CString>,
values: Vec<CString>,
_items: Vec<CString>,
_values: Vec<CString>,
nb_max_lines_for_value: u8,
small_case_for_value: bool,
wrapping: bool,
Expand Down Expand Up @@ -142,8 +142,8 @@ impl TagValueList {
}
TagValueList {
pairs: c_field_strings,
items: c_field_names,
values: c_field_values,
_items: c_field_names,
_values: c_field_values,
nb_max_lines_for_value,
small_case_for_value,
wrapping,
Expand Down Expand Up @@ -199,7 +199,7 @@ impl TagValueConfirm {
/// when using the NbglGenericReview struct.
pub struct InfosList {
info_types_cstrings: Vec<CString>,
info_contents_cstrings: Vec<CString>,
_info_contents_cstrings: Vec<CString>,
info_types_ptr: Vec<*const c_char>,
info_contents_ptr: Vec<*const c_char>,
}
Expand All @@ -220,7 +220,7 @@ impl InfosList {
info_contents_cstrings.iter().map(|s| s.as_ptr()).collect();
InfosList {
info_types_cstrings: info_types_cstrings,
info_contents_cstrings: info_contents_cstrings,
_info_contents_cstrings: info_contents_cstrings,
info_types_ptr: info_types_ptr,
info_contents_ptr: info_contents_ptr,
}
Expand Down Expand Up @@ -413,7 +413,7 @@ impl NbglGenericReview {
.collect()
}

pub fn show(&mut self, reject_button_str: &str) -> bool {
pub fn show(&self, reject_button_str: &str) -> bool {
unsafe {
let c_content_list: Vec<nbgl_content_t> = self.to_c_content_list();

Expand Down
24 changes: 22 additions & 2 deletions ledger_device_sdk/src/nbgl/nbgl_home_and_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ const INFO_FIELDS: [*const c_char; 2] = [
"Developer\0".as_ptr() as *const c_char,
];

pub enum PageIndex {
Settings(u8),
Home,
}

/// Used to display the home screen of the application, with an optional glyph,
/// information fields, and settings switches.
pub struct NbglHomeAndSettings {
Expand All @@ -50,6 +55,7 @@ pub struct NbglHomeAndSettings {
generic_contents: nbgl_genericContents_t,
info_list: nbgl_contentInfoList_t,
icon: nbgl_icon_details_t,
start_page: PageIndex,
}

impl SyncNBGL for NbglHomeAndSettings {}
Expand All @@ -76,6 +82,7 @@ impl<'a> NbglHomeAndSettings {
generic_contents: nbgl_genericContents_t::default(),
info_list: nbgl_contentInfoList_t::default(),
icon: nbgl_icon_details_t::default(),
start_page: PageIndex::Home,
}
}

Expand Down Expand Up @@ -127,6 +134,13 @@ impl<'a> NbglHomeAndSettings {
}
}

pub fn set_start_page(self, page: PageIndex) -> NbglHomeAndSettings {
NbglHomeAndSettings {
start_page: page,
..self
}
}

/// Show the home screen and settings page.
/// This function will block until an APDU is received or the user quits the app.
/// DEPRECATED as it constraints to refresh screen for every received APDU.
Expand Down Expand Up @@ -186,7 +200,10 @@ impl<'a> NbglHomeAndSettings {
self.app_name.as_ptr() as *const c_char,
&self.icon as *const nbgl_icon_details_t,
core::ptr::null(),
INIT_HOME_PAGE as u8,
match self.start_page {
PageIndex::Home => INIT_HOME_PAGE as u8,
PageIndex::Settings(idx) => idx,
},
&self.generic_contents as *const nbgl_genericContents_t,
&self.info_list as *const nbgl_contentInfoList_t,
core::ptr::null(),
Expand Down Expand Up @@ -263,7 +280,10 @@ impl<'a> NbglHomeAndSettings {
self.app_name.as_ptr() as *const c_char,
&self.icon as *const nbgl_icon_details_t,
core::ptr::null(),
INIT_HOME_PAGE as u8,
match self.start_page {
PageIndex::Home => INIT_HOME_PAGE as u8,
PageIndex::Settings(idx) => idx,
},
&self.generic_contents as *const nbgl_genericContents_t,
&self.info_list as *const nbgl_contentInfoList_t,
core::ptr::null(),
Expand Down
2 changes: 1 addition & 1 deletion ledger_device_sdk/src/nbgl/nbgl_review.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl<'a> NbglReview<'a> {
}
}

pub fn show(&mut self, fields: &[Field]) -> bool {
pub fn show(&self, fields: &[Field]) -> bool {
unsafe {
let v: Vec<CField> = fields
.iter()
Expand Down
2 changes: 1 addition & 1 deletion ledger_device_sdk/src/nbgl/nbgl_review_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl<'a> NbglReviewStatus {
NbglReviewStatus { status_type }
}

pub fn show(&mut self, success: bool) {
pub fn show(&self, success: bool) {
unsafe {
self.ux_sync_init();
nbgl_useCaseReviewStatus(self.status_type.to_message(success), Some(quit_callback));
Expand Down
2 changes: 1 addition & 1 deletion ledger_device_sdk/src/nbgl/nbgl_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl NbglStatus {
}
}

pub fn show(&mut self, success: bool) {
pub fn show(&self, success: bool) {
unsafe {
self.ux_sync_init();
nbgl_useCaseStatus(
Expand Down
6 changes: 3 additions & 3 deletions ledger_device_sdk/src/nbgl/nbgl_streaming_review.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl NbglStreamingReview {
}
}

pub fn start(&mut self, title: &str, subtitle: &str) -> bool {
pub fn start(&self, title: &str, subtitle: &str) -> bool {
unsafe {
let title = CString::new(title).unwrap();
let subtitle = CString::new(subtitle).unwrap();
Expand Down Expand Up @@ -77,7 +77,7 @@ impl NbglStreamingReview {
}
}

pub fn continue_review(&mut self, fields: &[Field]) -> bool {
pub fn continue_review(&self, fields: &[Field]) -> bool {
unsafe {
let v: Vec<CField> = fields
.iter()
Expand Down Expand Up @@ -124,7 +124,7 @@ impl NbglStreamingReview {
}
}

pub fn finish(&mut self, finish_title: &str) -> bool {
pub fn finish(&self, finish_title: &str) -> bool {
unsafe {
let finish_title = CString::new(finish_title).unwrap();

Expand Down
6 changes: 3 additions & 3 deletions ledger_device_sdk/src/nvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
//! update:
//!
//! ```
//! use ledger_device_sdk::PIC;
//! use ledger_device_sdk::NVMData;
//! use ledger_device_sdk::nvm::AtomicStorage;
//!
//! // This is necessary to store the object in NVM and not in RAM
//! #[link_section=".nvm_data"]
//! static mut COUNTER: PIC<AtomicStorage<i32>> =
//! PIC::new(AtomicStorage::new(&3));
//! static mut COUNTER: NVMData<AtomicStorage<i32>> =
//! NVMData::new(AtomicStorage::new(&3));
//! ```
//!
//! In the program, `COUNTER` must not be used directly. It is a static variable
Expand Down