Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
agrojean-ledger committed Oct 6, 2023
1 parent 4d9bfc6 commit 50740ce
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 41 deletions.
80 changes: 50 additions & 30 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ fn finalize_nanos_configuration(command: &mut cc::Build, bolos_sdk: &String) ->
.target("thumbv6m-none-eabi")
.define("ST31", None)
.define("TARGET_NANOS", None)
.define("TARGET_ID", Some("0x31100004"))
.define("BAGL_HEIGHT", Some("32"))
.define("BAGL_WIDTH", Some("128"))
.file(format!("{bolos_sdk}/nanos/syscalls.c"))
Expand All @@ -27,7 +26,6 @@ fn finalize_nanox_configuration(command: &mut cc::Build, bolos_sdk: &String) ->
.target("thumbv6m-none-eabi")
.define("ST33", None)
.define("TARGET_NANOX", None)
.define("TARGET_ID", Some("0x33000004"))
.define("BAGL_HEIGHT", Some("64"))
.define("BAGL_WIDTH", Some("128"))
.define("HAVE_SEPROXYHAL_MCU", None)
Expand Down Expand Up @@ -81,7 +79,6 @@ fn finalize_nanosplus_configuration(command: &mut cc::Build, bolos_sdk: &String)
.target("thumbv8m.main-none-eabi")
.define("ST33K1M5", None)
.define("TARGET_NANOS2", None)
.define("TARGET_ID", Some("0x33100004"))
.define("BAGL_HEIGHT", Some("64"))
.define("BAGL_WIDTH", Some("128"))
.define("HAVE_SE_BUTTON", None)
Expand Down Expand Up @@ -128,6 +125,29 @@ fn configure_lib_bagl(command: &mut cc::Build, bolos_sdk: &String) {
}
}

fn extract_defines_from_cx_makefile(command: &mut cc::Build, makefile: &String) {
let mut makefile = File::open(makefile).unwrap();
let mut content = String::new();
makefile.read_to_string(&mut content).unwrap();
// Extract the defines from the Makefile.conf.cx.
// They all begin with `HAVE` and are ' ' and '\n' separated.
let mut defines = content
.split('\n')
.filter(|line| !line.starts_with('#')) // Remove lines that are commented
.flat_map(|line| line.split(' ').filter(|word| word.starts_with("HAVE")))
.collect::<Vec<&str>>();

// do not forget NATIVE_LITTLE_ENDIAN
let s = String::from("NATIVE_LITTLE_ENDIAN");
defines.push(s.as_str());

// Add the defines found in the Makefile.conf.cx to our build command.
for define in defines {
// scott could use for_each
command.define(define, None);
}
}

fn main() -> Result<(), Box<dyn Error>> {
let bolos_sdk = "./ledger-secure-sdk".to_string();

Expand Down Expand Up @@ -157,6 +177,7 @@ fn main() -> Result<(), Box<dyn Error>> {
command
.file("./src/c/src.c")
.file("./src/c/sjlj.s")
.file(format!("{bolos_sdk}/src/app_metadata.c"))
.file(format!("{bolos_sdk}/src/os_io_usb.c"))
.file(format!("{bolos_sdk}/src/pic.c"))
.file(format!("{bolos_sdk}/src/checks.c"))
Expand All @@ -177,6 +198,7 @@ fn main() -> Result<(), Box<dyn Error>> {
.file(format!(
"{bolos_sdk}/lib_stusb/STM32_USB_Device_Library/Class/HID/Src/usbd_hid.c"
))
.define("RUST_APP", None)
.define("HAVE_LOCAL_APDU_BUFFER", None)
.define("IO_HID_EP_LENGTH", Some("64"))
.define("USB_SEGMENT_SIZE", Some("64"))
Expand Down Expand Up @@ -270,40 +292,38 @@ fn main() -> Result<(), Box<dyn Error>> {
),
};

let cx_makefile = match device {
NanoS => finalize_nanos_configuration(&mut command, &bolos_sdk),
NanoX => finalize_nanox_configuration(&mut command, &bolos_sdk),
NanoSPlus => finalize_nanosplus_configuration(&mut command, &bolos_sdk),
};

if env::var_os("CARGO_FEATURE_PENDING_REVIEW_SCREEN").is_some() {
command.define("HAVE_PENDING_REVIEW_SCREEN", None);
}

// all 'finalize_...' functions also declare a new 'cfg' variable corresponding
// to the name of the target (as #[cfg(target = "nanox")] does not work, for example)
// this allows code to easily import things depending on the target
// Add app metadata defines to our build command.
let cx_makefile = match device {
NanoS => {
command.define("TARGET_NAME", Some("\"TARGET_NANOS\""));
command.define("TARGET_ID", Some("0x31100004"));
command.define("API_LEVEL", Some("10"));
finalize_nanos_configuration(&mut command, &bolos_sdk)
},

let mut makefile = File::open(cx_makefile).unwrap();
let mut content = String::new();
makefile.read_to_string(&mut content).unwrap();
// Extract the defines from the Makefile.conf.cx.
// They all begin with `HAVE` and are ' ' and '\n' separated.
let mut defines = content
.split('\n')
.filter(|line| !line.starts_with('#')) // Remove lines that are commented
.flat_map(|line| line.split(' ').filter(|word| word.starts_with("HAVE")))
.collect::<Vec<&str>>();
NanoX => {
command.define("TARGET_NAME", Some("\"TARGET_NANOX\""));
command.define("TARGET_ID", Some("0x33000004"));
command.define("API_LEVEL", Some("5"));
finalize_nanox_configuration(&mut command, &bolos_sdk)
},

// do not forget NATIVE_LITTLE_ENDIAN
let s = String::from("NATIVE_LITTLE_ENDIAN");
defines.push(s.as_str());
NanoSPlus => {
command.define("TARGET_NAME", Some("\"TARGET_NANOS2\""));
command.define("TARGET_ID", Some("0x33100004"));
command.define("API_LEVEL", Some("1"));
finalize_nanosplus_configuration(&mut command, &bolos_sdk)
},
};

// Add the defines found in the Makefile.conf.cx to our build command.
for define in defines {
// scott could use for_each
command.define(define, None);
}
// all 'finalize_...' functions also declare a new 'cfg' variable corresponding
// to the name of the target (as #[cfg(target = "nanox")] does not work, for example)
// this allows code to easily import things depending on the target
extract_defines_from_cx_makefile(&mut command, &cx_makefile);

command.compile("rust-app");

Expand Down
79 changes: 79 additions & 0 deletions ledger-secure-sdk/src/app_metadata.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*******************************************************************************
* Ledger - Secure firmware
* (c) 2023 Ledger
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
********************************************************************************/

#if !defined(HAVE_BOLOS)

#include <stdint.h>

#if !defined(RUST_APP)
#include "bolos_target.h"
#endif

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"

#define STR_IMPL_(x) #x
#define STRINGIFY(x) STR_IMPL_(x)

#define CREATE_METADATA_STRING_ITEM(ITEM_NAME, section_name) \
__attribute__((section(".ledger." #section_name))) const char section_name[] = ITEM_NAME;

#define CREATE_METADATA_STRING_ITEM_FROM_INT(ITEM_NAME, section_name) \
__attribute__((section(".ledger." #section_name))) const char section_name[] \
= STRINGIFY(ITEM_NAME);

#if defined(TARGET)
CREATE_METADATA_STRING_ITEM(TARGET, target)
#endif

#if defined(TARGET_NAME)
// __attribute__((section(".ledger.target_name"))) const char target_name[] = TARGET_NAME;
CREATE_METADATA_STRING_ITEM(TARGET_NAME, target_name)
#endif

#if defined(TARGET_ID)
CREATE_METADATA_STRING_ITEM_FROM_INT(TARGET_ID, target_id)
#endif

#if defined(APPNAME)
CREATE_METADATA_STRING_ITEM(APPNAME, app_name)
#endif

#if defined(APPVERSION)
CREATE_METADATA_STRING_ITEM(APPVERSION, app_version)
#endif

#if defined(API_LEVEL)
// __attribute__((section(".ledger.api_level"))) char api_level[] = STRINGIFY(API_LEVEL);
CREATE_METADATA_STRING_ITEM_FROM_INT(API_LEVEL, api_level)
#endif

#if defined(SDK_NAME)
CREATE_METADATA_STRING_ITEM(SDK_NAME, sdk_name)
#endif

#if defined(SDK_VERSION)
CREATE_METADATA_STRING_ITEM(SDK_VERSION, sdk_version)
#endif

#if defined(SDK_HASH)
CREATE_METADATA_STRING_ITEM(SDK_HASH, sdk_hash)
#endif

#pragma GCC diagnostic pop

#endif
8 changes: 8 additions & 0 deletions link.ld
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,15 @@ SECTIONS
_estack = ABSOLUTE(END_STACK);
} > SRAM

.ledger.target (INFO): { KEEP(*(.ledger.target)) }
.ledger.target_id (INFO): { KEEP(*(.ledger.target_id)) }
.ledger.target_name (INFO): { KEEP(*(.ledger.target_name)) }
.ledger.app_name (INFO): { KEEP(*(.ledger.app_name)) }
.ledger.app_version (INFO): { KEEP(*(.ledger.app_version)) }
.ledger.api_level (INFO): { KEEP(*(.ledger.api_level)) }
.ledger.sdk_version (INFO): { KEEP(*(.ledger.sdk_version)) }
.ledger.sdk_name (INFO): { KEEP(*(.ledger.sdk_name)) }
.ledger.sdk_hash (INFO): { KEEP(*(.ledger.sdk_hash)) }

.stack_sizes (INFO):
{
Expand Down
22 changes: 11 additions & 11 deletions src/infos.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/// Expose the API_LEVEL

Check failure on line 1 in src/infos.rs

View workflow job for this annotation

GitHub Actions / fmt

expected item after doc comment
#[link_section = ".ledger.api_level"]
#[used]
static API_LEVEL: u8 = if cfg!(target_os = "nanos") {
0
} else if cfg!(target_os = "nanox") {
5
} else if cfg!(target_os = "nanosplus") {
1
} else {
0xff
};
// #[link_section = ".ledger.api_level"]
// #[used]
// static API_LEVEL: u8 = if cfg!(target_os = "nanos") {
// 0
// } else if cfg!(target_os = "nanox") {
// 5
// } else if cfg!(target_os = "nanosplus") {
// 1
// } else {
// 0xff
// };

0 comments on commit 50740ce

Please sign in to comment.