Skip to content

Commit

Permalink
rust/src: add stdout print
Browse files Browse the repository at this point in the history
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 <inanata15@gmail.com>
  • Loading branch information
asi345 committed Jan 4, 2024
1 parent 460c7bd commit 3668de6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/rust/bitbox02/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down

0 comments on commit 3668de6

Please sign in to comment.