From dd96b2ab778d36e2ed69d3f7294e153e63b286ee Mon Sep 17 00:00:00 2001 From: GroM Date: Thu, 12 Dec 2024 17:07:07 +0100 Subject: [PATCH] Add debug feature to activate/deactivate traces --- ledger_device_sdk/Cargo.toml | 1 + ledger_device_sdk/src/io.rs | 2 +- ledger_device_sdk/src/libcall/swap.rs | 30 +++++++++++---------------- ledger_device_sdk/src/testing.rs | 7 ++++++- ledger_secure_sdk_sys/src/c/src.c | 2 ++ 5 files changed, 22 insertions(+), 20 deletions(-) diff --git a/ledger_device_sdk/Cargo.toml b/ledger_device_sdk/Cargo.toml index 40e04506..81b94c6b 100644 --- a/ledger_device_sdk/Cargo.toml +++ b/ledger_device_sdk/Cargo.toml @@ -24,6 +24,7 @@ const-zero = "0.1.1" ledger_secure_sdk_sys = { path = "../ledger_secure_sdk_sys", version = "1.5.3" } [features] +debug = [] speculos = [] ccid = [] heap = [ "ledger_secure_sdk_sys/heap" ] diff --git a/ledger_device_sdk/src/io.rs b/ledger_device_sdk/src/io.rs index 6a78e0bd..84ee9f5e 100644 --- a/ledger_device_sdk/src/io.rs +++ b/ledger_device_sdk/src/io.rs @@ -7,7 +7,7 @@ use ledger_secure_sdk_sys::*; #[cfg(feature = "ccid")] use crate::ccid; -use crate::{seph, testing}; +use crate::seph; use core::convert::{Infallible, TryFrom}; use core::ops::{Index, IndexMut}; diff --git a/ledger_device_sdk/src/libcall/swap.rs b/ledger_device_sdk/src/libcall/swap.rs index 36d3f8fd..05e1d522 100644 --- a/ledger_device_sdk/src/libcall/swap.rs +++ b/ledger_device_sdk/src/libcall/swap.rs @@ -5,8 +5,6 @@ use ledger_secure_sdk_sys::{ libargs_s__bindgen_ty_1, libargs_t, }; -use super::string::CustomString; - pub struct CheckAddressParams { pub dpath: [u8; 64], pub dpath_len: usize, @@ -69,7 +67,7 @@ impl Default for CreateTxParams { pub fn get_check_address_params(arg0: u32) -> CheckAddressParams { unsafe { - debug_print("GET_CHECK_ADDRESS_PARAMS\n"); + debug_print("=> get_check_address_params\n"); let mut libarg: libargs_t = libargs_t::default(); @@ -86,15 +84,15 @@ pub fn get_check_address_params(arg0: u32) -> CheckAddressParams { let mut check_address_params: CheckAddressParams = Default::default(); - debug_print("GET_DPATH_LENGTH\n"); + debug_print("==> GET_DPATH_LENGTH\n"); check_address_params.dpath_len = *(params.address_parameters as *const u8) as usize; - debug_print("GET_DPATH \n"); + debug_print("==> GET_DPATH \n"); for i in 1..1 + check_address_params.dpath_len * 4 { check_address_params.dpath[i - 1] = *(params.address_parameters.add(i)); } - debug_print("GET_REF_ADDRESS\n"); + debug_print("==> GET_REF_ADDRESS\n"); let mut address_length = 0usize; while *(params.address_to_check.wrapping_add(address_length)) != '\0' as i8 { check_address_params.ref_address[address_length] = @@ -113,7 +111,7 @@ pub fn get_check_address_params(arg0: u32) -> CheckAddressParams { pub fn get_printable_amount_params(arg0: u32) -> PrintableAmountParams { unsafe { - debug_print("GET_PRINTABLE_AMOUNT_PARAMS\n"); + debug_print("=> get_printable_amount_params\n"); let mut libarg: libargs_t = libargs_t::default(); @@ -131,21 +129,16 @@ pub fn get_printable_amount_params(arg0: u32) -> PrintableAmountParams { let mut printable_amount_params: PrintableAmountParams = Default::default(); - debug_print("GET_AMOUNT_LENGTH\n"); + debug_print("==> GET_AMOUNT_LENGTH\n"); printable_amount_params.amount_len = params.amount_length as usize; - let s = CustomString::<2>::from(printable_amount_params.amount_len as u8); - debug_print("AMOUNT LENGTH: \n"); - debug_print(s.as_str()); - debug_print("\n"); - - debug_print("GET_AMOUNT\n"); + debug_print("==> GET_AMOUNT\n"); for i in 0..printable_amount_params.amount_len { printable_amount_params.amount[16 - printable_amount_params.amount_len + i] = *(params.amount.add(i)); } - debug_print("GET_AMOUNT_STR\n"); + debug_print("==> GET_AMOUNT_STR\n"); printable_amount_params.amount_str = &(*(libarg.__bindgen_anon_1.get_printable_amount as *mut get_printable_amount_parameters_t)) .printable_amount as *const i8 as *mut i8; @@ -161,7 +154,7 @@ extern "C" { pub fn sign_tx_params(arg0: u32) -> CreateTxParams { unsafe { - debug_print("SIGN_TX_PARAMS\n"); + debug_print("=> sign_tx_params\n"); let mut libarg: libargs_t = libargs_t::default(); @@ -178,19 +171,20 @@ pub fn sign_tx_params(arg0: u32) -> CreateTxParams { let mut create_tx_params: CreateTxParams = Default::default(); + debug_print("==> GET_AMOUNT\n"); create_tx_params.amount_len = params.amount_length as usize; - for i in 0..create_tx_params.amount_len { create_tx_params.amount[16 - create_tx_params.amount_len + i] = *(params.amount.add(i)); } + debug_print("==> GET_FEE\n"); create_tx_params.fee_amount_len = params.fee_amount_length as usize; - for i in 0..create_tx_params.fee_amount_len { create_tx_params.fee_amount[16 - create_tx_params.fee_amount_len + i] = *(params.fee_amount.add(i)); } + debug_print("==> GET_DESTINATION_ADDRESS\n"); let mut dest_address_length = 0usize; while *(params.destination_address.wrapping_add(dest_address_length)) != '\0' as i8 { create_tx_params.dest_address[dest_address_length] = diff --git a/ledger_device_sdk/src/testing.rs b/ledger_device_sdk/src/testing.rs index 1cebb21c..e80e7f84 100644 --- a/ledger_device_sdk/src/testing.rs +++ b/ledger_device_sdk/src/testing.rs @@ -1,8 +1,11 @@ -use core::arch::asm; use core::panic::PanicInfo; +#[cfg(feature = "debug")] +use core::arch::asm; + /// Debug 'print' function that uses ARM semihosting /// Prints only strings with no formatting +#[cfg(feature = "debug")] pub fn debug_print(s: &str) { let p = s.as_bytes().as_ptr(); for i in 0..s.len() { @@ -16,6 +19,8 @@ pub fn debug_print(s: &str) { } } } +#[cfg(not(feature = "debug"))] +pub fn debug_print(_s: &str) {} pub fn to_hex(m: u32) -> [u8; 8] { let mut hex = [0u8; 8]; diff --git a/ledger_secure_sdk_sys/src/c/src.c b/ledger_secure_sdk_sys/src/c/src.c index 4716d0f5..0a751021 100644 --- a/ledger_secure_sdk_sys/src/c/src.c +++ b/ledger_secure_sdk_sys/src/c/src.c @@ -333,6 +333,7 @@ int c_main(int arg0) { link_pass_ram(data_len, sidata_src, data); + // if libcall, does not reset bss as it is shared with the calling app if (arg0 == 0) c_reset_bss(); @@ -342,6 +343,7 @@ int c_main(int arg0) { for(;;) { BEGIN_TRY { TRY { + // if libcall, does not start io and memory allocator if (arg0 == 0) c_boot_std(); sample_main(arg0);