Skip to content

Commit

Permalink
Manage lib vs std boot
Browse files Browse the repository at this point in the history
  • Loading branch information
yogh333 committed Dec 12, 2024
1 parent ed340e8 commit e51c879
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 72 deletions.
37 changes: 30 additions & 7 deletions 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;
use crate::{seph, testing};
use core::convert::{Infallible, TryFrom};
use core::ops::{Index, IndexMut};

Expand Down Expand Up @@ -170,7 +170,7 @@ impl Comm {
/// Send the currently held APDU
// This is private. Users should call reply to set the satus word and
// transmit the response.
fn apdu_send(&mut self) {
fn apdu_send(&mut self, is_swap: bool) {
if !sys_seph::is_status_sent() {
sys_seph::send_general_status()
}
Expand Down Expand Up @@ -203,6 +203,13 @@ impl Comm {
}
_ => (),
}
if is_swap {
if !sys_seph::is_status_sent() {
sys_seph::send_general_status()
}
sys_seph::seph_recv(&mut spi_buffer, 0);
seph::handle_event(&mut self.apdu_buffer, &spi_buffer);
}
self.tx = 0;
self.rx = 0;
unsafe {
Expand Down Expand Up @@ -506,7 +513,17 @@ impl Comm {
self.apdu_buffer[self.tx + 1] = sw as u8;
self.tx += 2;
// Transmit the response
self.apdu_send();
self.apdu_send(false);
}

pub fn swap_reply<T: Into<Reply>>(&mut self, reply: T) {
let sw = reply.into().0;
// Append status word
self.apdu_buffer[self.tx] = (sw >> 8) as u8;
self.apdu_buffer[self.tx + 1] = sw as u8;
self.tx += 2;
// Transmit the response
self.apdu_send(true);
}

/// Set the Status Word of the response to `StatusWords::OK` (which is equal
Expand All @@ -515,6 +532,10 @@ impl Comm {
self.reply(StatusWords::Ok);
}

pub fn swap_reply_ok(&mut self) {
self.swap_reply(StatusWords::Ok);
}

/// Return APDU Metadata
pub fn get_apdu_metadata(&self) -> &ApduHeader {
assert!(self.apdu_buffer.len() >= 4);
Expand Down Expand Up @@ -552,10 +573,12 @@ impl Comm {
}

pub fn append(&mut self, m: &[u8]) {
for c in m.iter() {
self.apdu_buffer[self.tx] = *c;
self.tx += 1;
}
self.apdu_buffer[self.tx..self.tx + m.len()].copy_from_slice(m);
self.tx += m.len();
// for c in m.iter() {
// self.apdu_buffer[self.tx] = *c;
// self.tx += 1;
// }
}
}

Expand Down
22 changes: 12 additions & 10 deletions ledger_device_sdk/src/libcall/swap.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::nbgl::NbglSpinner;
use crate::testing::debug_print;
use ledger_secure_sdk_sys::{
check_address_parameters_t, create_transaction_parameters_t, get_printable_amount_parameters_t,
Expand Down Expand Up @@ -153,6 +154,11 @@ pub fn get_printable_amount_params(arg0: u32) -> PrintableAmountParams {
}
}

extern "C" {
fn c_reset_bss();
fn c_boot_std();
}

pub fn sign_tx_params(arg0: u32) -> CreateTxParams {
unsafe {
debug_print("SIGN_TX_PARAMS\n");
Expand All @@ -172,29 +178,19 @@ pub fn sign_tx_params(arg0: u32) -> CreateTxParams {

let mut create_tx_params: CreateTxParams = Default::default();

debug_print("GET_AMOUNT_LENGTH\n");
create_tx_params.amount_len = params.amount_length as usize;

let s = CustomString::<2>::from(create_tx_params.amount_len as u8);
debug_print("AMOUNT LENGTH: \n");
debug_print(s.as_str());
debug_print("\n");

debug_print("GET_AMOUNT\n");
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_AMOUNT_LENGTH\n");
create_tx_params.fee_amount_len = params.fee_amount_length as usize;

debug_print("GET_FEE_AMOUNT\n");
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_DEST_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 All @@ -207,6 +203,12 @@ pub fn sign_tx_params(arg0: u32) -> CreateTxParams {
as *mut create_transaction_parameters_t))
.result as *const u8 as *mut u8;

/* Reset BSS and complete application boot */
c_reset_bss();
c_boot_std();

NbglSpinner::new().text("Signing").show();

create_tx_params
}
}
1 change: 0 additions & 1 deletion ledger_secure_sdk_sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,6 @@ impl SDKBuilder {
.include(self.bolos_sdk.join("lib_cxng/include"))
.include(self.bolos_sdk.join("lib_stusb"))
.include(self.bolos_sdk.join("lib_stusb_impl"))
.include(self.bolos_sdk.join("lib_standard_app"))
.include(
self.bolos_sdk
.join("lib_stusb/STM32_USB_Device_Library/Core/Inc"),
Expand Down
111 changes: 57 additions & 54 deletions ledger_secure_sdk_sys/src/c/src.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "os_nvm.h"
#include "os_pic.h"
#include "checks.h"
#include "swap_lib_calls.h"
#ifdef HAVE_BLE
#include "ledger_ble.h"
bolos_ux_asynch_callback_t G_io_asynch_ux_callback;
Expand Down Expand Up @@ -259,6 +258,58 @@ void link_pass_nvram(
uint8_t G_io_apdu_buffer[260];
#endif

void c_reset_bss() {
size_t bss_len;
SYMBOL_ABSOLUTE_VALUE(bss_len, _bss_len);
struct SectionDst* bss;
SYMBOL_SBREL_ADDRESS(bss, _bss);
memset(bss, 0, bss_len);
}

void c_boot_std() {
// below is a 'manual' implementation of `io_seproxyhal_init`
#ifdef HAVE_MCU_PROTECT
unsigned char c[4];
c[0] = SEPROXYHAL_TAG_MCU;
c[1] = 0;
c[2] = 1;
c[3] = SEPROXYHAL_TAG_MCU_TYPE_PROTECT;
io_seproxyhal_spi_send(c, 4);
#endif

#ifdef HAVE_BLE
unsigned int plane = G_io_app.plane_mode;
#endif

memset(&G_io_app, 0, sizeof(G_io_app));

#ifdef HAVE_BLE
G_io_app.plane_mode = plane;
#endif
G_io_app.apdu_state = APDU_IDLE;
G_io_app.apdu_length = 0;
G_io_app.apdu_media = IO_APDU_MEDIA_NONE;

G_io_app.ms = 0;
io_usb_hid_init();

USB_power(0);
USB_power(1);
#ifdef HAVE_CCID
io_usb_ccid_set_card_inserted(1);
#endif

#ifdef HAVE_BLE
memset(&G_io_asynch_ux_callback, 0, sizeof(G_io_asynch_ux_callback));
BLE_power(1, NULL);
#endif

#if !defined(HAVE_BOLOS) && defined(HAVE_PENDING_REVIEW_SCREEN)
check_audited_app();
#endif // !defined(HAVE_BOLOS) && defined(HAVE_PENDING_REVIEW_SCREEN)
heap_init();
}

int c_main(int arg0) {
__asm volatile("cpsie i");

Expand All @@ -281,66 +332,18 @@ int c_main(int arg0) {
__asm volatile("mov %[result],r9" : [result] "=r" (data));

link_pass_ram(data_len, sidata_src, data);

libargs_t *args = (libargs_t *) arg0;

if (args == NULL || args->command == SIGN_TRANSACTION) {
size_t bss_len;
SYMBOL_ABSOLUTE_VALUE(bss_len, _bss_len);
struct SectionDst* bss;
SYMBOL_SBREL_ADDRESS(bss, _bss);
memset(bss, 0, bss_len);
}
if (arg0 == 0)
c_reset_bss();

// formerly known as 'os_boot()'
try_context_set(NULL);

for(;;) {
BEGIN_TRY {
TRY {
if (args == NULL || args->command == SIGN_TRANSACTION) {
// below is a 'manual' implementation of `io_seproxyhal_init`
#ifdef HAVE_MCU_PROTECT
unsigned char c[4];
c[0] = SEPROXYHAL_TAG_MCU;
c[1] = 0;
c[2] = 1;
c[3] = SEPROXYHAL_TAG_MCU_TYPE_PROTECT;
io_seproxyhal_spi_send(c, 4);
#endif

#ifdef HAVE_BLE
unsigned int plane = G_io_app.plane_mode;
#endif

memset(&G_io_app, 0, sizeof(G_io_app));

#ifdef HAVE_BLE
G_io_app.plane_mode = plane;
#endif
G_io_app.apdu_state = APDU_IDLE;
G_io_app.apdu_length = 0;
G_io_app.apdu_media = IO_APDU_MEDIA_NONE;

G_io_app.ms = 0;
io_usb_hid_init();

USB_power(0);
USB_power(1);
#ifdef HAVE_CCID
io_usb_ccid_set_card_inserted(1);
#endif

#ifdef HAVE_BLE
memset(&G_io_asynch_ux_callback, 0, sizeof(G_io_asynch_ux_callback));
BLE_power(1, NULL);
#endif

#if !defined(HAVE_BOLOS) && defined(HAVE_PENDING_REVIEW_SCREEN)
check_audited_app();
#endif // !defined(HAVE_BOLOS) && defined(HAVE_PENDING_REVIEW_SCREEN)
heap_init();
}
if (arg0 == 0)
c_boot_std();
sample_main(arg0);
}
CATCH(EXCEPTION_IO_RESET) {
Expand All @@ -355,4 +358,4 @@ int c_main(int arg0) {
END_TRY;
}
return 0;
}
}

0 comments on commit e51c879

Please sign in to comment.