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

Feat/stax nbgl #109

Closed
wants to merge 2 commits into from
Closed
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
3 changes: 3 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ runner = "speculos -m nanox --display=headless"
[target.nanosplus]
runner = "speculos -m nanosp --display=headless"

[target.stax]
runner = "speculos -a 13 --model stax"

[unstable]
build-std = ["core"]
build-std-features = ["compiler-builtins-mem"]
1 change: 1 addition & 0 deletions ledger_device_sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ lto = true
speculos = []
pre1_54 = []
lib_bagl = []
nbgl = []
ccid = []
pending_review_screen = []
6 changes: 4 additions & 2 deletions ledger_device_sdk/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ fn main() -> Result<(), Box<dyn Error>> {
NanoS,
NanoSPlus,
NanoX,
Stax,
}
use Device::*;

Expand All @@ -14,8 +15,9 @@ fn main() -> Result<(), Box<dyn Error>> {
"nanos" => NanoS,
"nanosplus" => NanoSPlus,
"nanox" => NanoX,
"stax" => Stax,
target_name => panic!(
"invalid target `{target_name}`, expected one of `nanos`, `nanox`, `nanosplus`. Run with `-Z build-std=core --target=./<target name>.json`"
"invalid target `{target_name}`, expected one of `nanos`, `nanox`, `nanosplus` or `stax`. Run with `-Z build-std=core --target=./<target name>.json`"
),
};

Expand All @@ -30,7 +32,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let linkerscript = match device {
NanoS => "nanos_layout.ld",
NanoX => "nanox_layout.ld",
NanoSPlus => "nanosplus_layout.ld",
NanoSPlus | Stax => "nanosplus_layout.ld",
};
std::fs::copy(linkerscript, out_dir.join(linkerscript))?;
std::fs::copy("link.ld", out_dir.join("link.ld"))?;
Expand Down
189 changes: 189 additions & 0 deletions ledger_device_sdk/examples/stax.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
#![no_std]
#![no_main]

// Force boot section to be embedded in
use ledger_device_sdk as _;

use ledger_device_sdk::nbgl::{home, Home};
use ledger_device_sdk::uxapp::UxEvent;
use ledger_secure_sdk_sys::seph;
use ledger_secure_sdk_sys::*;

#[no_mangle]
pub static G_ux_params: bolos_ux_params_t = bolos_ux_params_t {
ux_id: 0,
len: 0,
u: bolos_ux_params_s__bindgen_ty_1 {
pairing_request: bolos_ux_params_s__bindgen_ty_1__bindgen_ty_1 {
type_: 0,
pairing_info_len: 0,
pairing_info: [0; 16],
},
},
};

#[panic_handler]
fn panic(_: &core::panic::PanicInfo) -> ! {
exit_app(1);
}

enum TuneIndex {
Reserved,
Boot,
Charging,
LedgerMoment,
Error,
Neutral,
Lock,
Success,
LookAtMe,
TapCasual,
TapNext,
}

impl TryFrom<u8> for TuneIndex {
type Error = ();
fn try_from(index: u8) -> Result<TuneIndex, ()> {
Ok(match index {
TUNE_RESERVED => TuneIndex::Reserved,
TUNE_BOOT => TuneIndex::Boot,
TUNE_CHARGING => TuneIndex::Charging,
TUNE_LEDGER_MOMENT => TuneIndex::LedgerMoment,
TUNE_ERROR => TuneIndex::Error,
TUNE_NEUTRAL => TuneIndex::Neutral,
TUNE_LOCK => TuneIndex::Lock,
TUNE_SUCCESS => TuneIndex::Success,
TUNE_LOOK_AT_ME => TuneIndex::LookAtMe,
TUNE_TAP_CASUAL => TuneIndex::TapCasual,
TUNE_TAP_NEXT => TuneIndex::TapNext,
_ => return Err(()),
})
}
}

// this is a mock that does nothing yet, but should become a direct translation
// of the C original. This was done to avoid compiling `os_io_seproxyhal.c` which
// includes many other things
#[no_mangle]
extern "C" fn io_seproxyhal_play_tune(tune_index: u8) {
let index = TuneIndex::try_from(tune_index);
if index.is_err() {
return;
}
}

// Special handler for finger events, that are an 8 byte packet
pub fn get_touch_event() -> Option<()> {
if !seph::is_status_sent() {
seph::send_general_status();
}

let mut buf = [0u8; 8];
while seph::is_status_sent() {
seph::seph_recv(&mut buf, 0);

match buf[0] as u32 {
SEPROXYHAL_TAG_FINGER_EVENT => {
unsafe {
ux_process_finger_event(buf.as_mut_ptr());
}
// return Some(());
}
// SEPROXYHAL_TAG_TICKER_EVENT => unsafe {
// ledger_sdk_sys::ux_process_default_event();
// }
_ => (),
}
}
None
}

fn wait_any() {
loop {
if get_touch_event().is_some() {
return;
}
}
}

#[no_mangle]
extern "C" fn sample_main() {
unsafe {
nbgl_refreshReset();
}

// works
home(&BTC_BMP);
wait_any();

// does not work

// let icon = nbgl_icon_details_t {
// width: 64,
// height: 64,
// bpp: 2,
// isFile: true,
// bitmap: BTC_BMP.as_ptr(),
// };

// Home::new()
// .app_name("Bitcoin\0")
// .icon(&icon)
// .top_right_cb(|| {
// let icon2 = nbgl_icon_details_t {
// width: 64,
// height: 64,
// bpp: 2,
// isFile: true,
// bitmap: BTC_BMP.as_ptr(),
// };
// unsafe { nbgl_refreshReset(); }
// Home::new().icon(&icon2).app_name("Ethereum\0").show();
// wait_any();
// })
// .quit_cb(|| exit_app(12))
// .show();

// wait_any();

exit_app(0);
}

const BTC_BMP: [u8; 573] = [
0x40, 0x00, 0x40, 0x00, 0x21, 0x35, 0x02, 0x00, 0x33, 0x02, 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00,
0x00, 0x00, 0x02, 0xff, 0xbd, 0x55, 0xb9, 0x4e, 0x23, 0x41, 0x10, 0x6d, 0x7b, 0x3c, 0x5c, 0x46,
0x5e, 0x2f, 0x96, 0x20, 0xc3, 0x10, 0x5b, 0x82, 0xdd, 0x6c, 0x1d, 0x2d, 0x64, 0x10, 0x81, 0x88,
0x10, 0x01, 0x87, 0x03, 0x02, 0x84, 0x38, 0x02, 0x62, 0x90, 0x58, 0x44,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b, 0xd4, 0xc9, 0x94,
0xdc, 0x11, 0xe0, 0x4a, 0x0b, 0x59, 0x98, 0xe5, 0x34, 0xf8, 0x19, 0xff, 0x51, 0xb4, 0x8e, 0xd2,
0x42, 0x6c, 0x14, 0x65, 0x1b, 0x5f, 0xa2, 0x85, 0xcc, 0x6c, 0x2b, 0x49, 0x95, 0x22, 0xaa, 0x7f,
0x13, 0xcb, 0x14, 0xc3, 0x9f, 0x0c, 0xbc, 0x45, 0xf5, 0xf3, 0x0c, 0x1f, 0x6c, 0x06, 0x9e, 0x44,
0xc9, 0xc8, 0x9c, 0x0d, 0x19, 0x73, 0xc1, 0x13, 0x92, 0x70, 0xed, 0xed, 0xef, 0xee, 0xeb, 0xee,
0x9b, 0xf7, 0x37, 0xe6, 0x22, 0x62, 0xb1, 0x8f, 0xce, 0x97, 0x57, 0x3b, 0xbe, 0xf3, 0xd1, 0xf9,
0xd6, 0x74, 0x3e, 0x36, 0x9b, 0xaf, 0xf0, 0xd2, 0x64, 0x3e, 0x47, 0x43, 0x18, 0x30, 0x2e, 0x00,
0xc3, 0x43, 0x4f, 0xe4, 0x7e, 0x09, 0x4d, 0xb9, 0x84, 0xe5, 0x02, 0xd2, 0x36, 0x74, 0xdb, 0xae,
0x38, 0x7f, 0x4f, 0x50, 0xfb, 0xe1, 0xd9, 0x6f, 0xc0, 0xda, 0xaf, 0xef, 0x84, 0x74, 0x4d, 0x1e,
0x7f, 0xe2, 0x95, 0x0c, 0xef, 0xe7, 0x7f, 0x81, 0xae, 0x00, 0x08, 0x00, 0x00,
];
4 changes: 4 additions & 0 deletions ledger_device_sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ pub mod seph;

pub mod testing;

#[cfg(target_os = "stax")]
pub mod nbgl;
#[cfg(not(target_os = "stax"))]
pub mod ui;

pub mod uxapp;

use core::panic::PanicInfo;
Expand Down
98 changes: 98 additions & 0 deletions ledger_device_sdk/src/nbgl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use core::mem::transmute;
use ledger_secure_sdk_sys::nbgl_icon_details_t;

pub struct Home<'a> {
app_name: &'static str,
icon: Option<&'a nbgl_icon_details_t>,
tagline: Option<&'static str>,
with_settings: bool,
top_right_cb: Option<fn()>,
quit_cb: Option<fn()>,
}

impl<'a> Home<'a> {
pub fn new() -> Home<'a> {
Home {
app_name: "AppName\0",
icon: None,
tagline: None,
with_settings: false,
top_right_cb: None,
quit_cb: None,
}
}

pub fn app_name(self, app_name: &'static str) -> Home<'a> {
Home { app_name, ..self }
}

pub fn icon(self, icon: &'a nbgl_icon_details_t) -> Home<'a> {
Home {
icon: Some(icon),
..self
}
}

pub fn top_right_cb(self, top_right_cb: fn()) -> Home<'a> {
Home {
top_right_cb: Some(top_right_cb),
..self
}
}

pub fn quit_cb(self, quit_cb: fn()) -> Home<'a> {
Home {
quit_cb: Some(quit_cb),
..self
}
}

pub fn show(&self) {
// let bop: unsafe extern "C" fn() = self.top_right_cb.unwrap() as unsafe extern "C" fn();
unsafe {
ledger_secure_sdk_sys::nbgl_useCaseHome(
self.app_name.as_ptr() as *const core::ffi::c_char,
self.icon.unwrap() as *const nbgl_icon_details_t,
core::ptr::null(),
self.with_settings,
// match self.top_right_cb {
// None => None,
// Some(f) => Some(f),
// },
transmute(self.top_right_cb),
// Some(bop),
transmute(self.quit_cb),
)
}
}
}

extern "C" fn do_nothing() {}
extern "C" fn exit() {
ledger_secure_sdk_sys::exit_app(0);
}
pub fn home(image: &[u8]) {
let icon = ledger_secure_sdk_sys::nbgl_icon_details_t {
width: 64,
height: 64,
bpp: 2,
isFile: true,
bitmap: image.as_ptr(),
};
unsafe {
// appName: *const ::core::ffi::c_char,
// appIcon: *const nbgl_icon_details_t,
// tagline: *const ::core::ffi::c_char,
// withSettings: bool,
// topRightCallback: nbgl_callback_t,
// quitCallback: nbgl_callback_t
ledger_secure_sdk_sys::nbgl_useCaseHome(
"AppName\0".as_ptr() as *const core::ffi::c_char,
&icon,
core::ptr::null(),
false,
Some(do_nothing),
Some(exit),
);
}
}
2 changes: 1 addition & 1 deletion ledger_device_sdk/src/nvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ macro_rules! atomic_storage {
atomic_storage!(64);
#[cfg(target_os = "nanox")]
atomic_storage!(256);
#[cfg(target_os = "nanosplus")]
#[cfg(any(target_os = "nanosplus", target_os = "stax"))]
atomic_storage!(512);

pub enum AtomicStorageElem {
Expand Down
29 changes: 29 additions & 0 deletions ledger_device_sdk/stax.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"abi": "eabi",
"arch": "arm",
"c-enum-min-bits": 8,
"data-layout": "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64",
"emit-debug-gdb-scripts": false,
"executables": true,
"frame-pointer": "always",
"linker": "rust-lld",
"linker-flavor": "ld.lld",
"llvm-target": "thumbv8m.main-none-eabi",
"max-atomic-width": 32,
"panic-strategy": "abort",
"pre-link-args": {
"ld.lld": [
"-Tnanosplus_layout.ld",
"-Tlink.ld"
],
"ld": [
"-Tnanosplus_layout.ld",
"-Tlink.ld"
]
},
"relocation-model": "ropi-rwpi",
"singlethread": true,
"target-pointer-width": "32",
"os": "stax",
"target-family": [ "bolos" ]
}
5 changes: 1 addition & 4 deletions ledger_secure_sdk_sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,4 @@ description = "Bindings to Ledger C SDK"
[build-dependencies]
bindgen = "0.65.1"
cc = "1.0.73"

[features]
lib_bagl = []
ccid = []
glob = "0.3.1"
Loading
Loading