Skip to content

Commit

Permalink
Add debug feature to activate/deactivate traces
Browse files Browse the repository at this point in the history
  • Loading branch information
yogh333 committed Dec 12, 2024
1 parent e51c879 commit dd96b2a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 20 deletions.
1 change: 1 addition & 0 deletions ledger_device_sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]
Expand Down
2 changes: 1 addition & 1 deletion ledger_device_sdk/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
30 changes: 12 additions & 18 deletions ledger_device_sdk/src/libcall/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();

Expand All @@ -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] =
Expand All @@ -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();

Expand All @@ -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;
Expand All @@ -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();

Expand All @@ -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] =
Expand Down
7 changes: 6 additions & 1 deletion ledger_device_sdk/src/testing.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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];
Expand Down
2 changes: 2 additions & 0 deletions ledger_secure_sdk_sys/src/c/src.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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);
Expand Down

0 comments on commit dd96b2a

Please sign in to comment.