From 3668de6ae2cbeaa6771906f8d1f372ffb1e693e0 Mon Sep 17 00:00:00 2001 From: asi345 Date: Thu, 4 Jan 2024 13:56:40 +0300 Subject: [PATCH] rust/src: add stdout print Due to the cyclic dependency issues, rust part of the firmware can not import std create and therefore it does not have io capabilities, including 'println!'. As a result, C 'printf' function is exposed to rust as an external function. It is a useful feature to have it because as developers we may need to debug many parts of the rust code when we are not using debuggers. Also, in projects such as simulator, firmware is mainly debugged with gdb. Hence there is limited flexibility of debugging when execution jumps to rust. In addition, simulator could print some of the ui elements of firmware to stdout in this way. Signed-off-by: asi345 --- src/CMakeLists.txt | 1 + src/rust/bitbox02/src/lib.rs | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 1adcc89496..bda608b112 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -395,6 +395,7 @@ add_custom_target(rust-bindgen --allowlist-function secp256k1_ecdsa_anti_exfil_host_commit --allowlist-function wally_get_secp_context --allowlist-function wally_hash160 + --allowlist-function printf ${CMAKE_CURRENT_SOURCE_DIR}/rust/bitbox02-sys/wrapper.h -- -DPB_NO_PACKED_STRUCTS=1 -DPB_FIELD_16BIT=1 -fshort-enums ${RUST_BINDGEN_FLAGS} ${RUST_INCLUDES} COMMAND diff --git a/src/rust/bitbox02/src/lib.rs b/src/rust/bitbox02/src/lib.rs index c059035494..638640bf86 100644 --- a/src/rust/bitbox02/src/lib.rs +++ b/src/rust/bitbox02/src/lib.rs @@ -176,6 +176,12 @@ pub fn reboot() -> ! { panic!("reboot called") } +pub fn print_stdout(msg: &str) { + unsafe { + bitbox02_sys::printf(crate::util::str_to_cstr_vec(msg).unwrap().as_ptr()); + } +} + #[cfg(test)] mod tests { use super::*;