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

Debug: Adds RTT support to bootloader #1362

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions bootloader.ld
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,17 @@ SECTIONS
. = ALIGN(4);
_etext = .;

/* Place RTT allocations first in RAM in debug builds, so that they are
* aligned between bootloader and firmware */
.rtt (NOLOAD) :
{
. = ALIGN(4);
_srtt = .;
*(.segger_rtt);
*(.segger_rtt_buf);
_ertt = .;
} > ram

.relocate :
{
. = ALIGN(4);
Expand Down
11 changes: 11 additions & 0 deletions firmware.ld
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ SECTIONS
. = ALIGN(4);
_etext = .;

/* Place RTT allocations first in RAM in debug builds, so that they are
* aligned between bootloader and firmware */
.rtt (NOLOAD) :
{
. = ALIGN(4);
_srtt = .;
*(.segger_rtt);
*(.segger_rtt_buf);
_ertt = .;
} > ram

.relocate :
{
. = ALIGN(4);
Expand Down
4 changes: 4 additions & 0 deletions src/bootloader/bootloader.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <memory/memory_shared.h>
#include <memory/nvmctrl.h>
#include <pukcc/curve_p256.h>
#include <rust/rust.h>
#include <screen.h>
#include <ui/components/ui_images.h>
#include <ui/fonts/arial_fonts.h>
Expand Down Expand Up @@ -206,6 +207,8 @@ void _binExec(void* l_code_addr)

static void _binary_exec(void)
{
util_log("Jumping to firmware");
rust_rtt_flush();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we maybe call this in util_log() by default?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flush will wait until the debugger reads out the buffer and updates the read pointer. It isn't necessary in general, but if we don't do it here messages from the bootloader might be overwritten by the firmware.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

calling it every time will basically make RTT sync, when its speed/efficiency comes from it being async.

_render_bootloader_finished_marker();

int i;
Expand Down Expand Up @@ -981,6 +984,7 @@ void bootloader_jump(void)
}

// App not entered. Start USB API to receive boot commands
util_log("Not jumping to firmware");
_compute_is_app_flash_empty();
_render_default_screen();
if (usb_start(_api_setup) != ERR_NONE) {
Expand Down
1 change: 1 addition & 0 deletions src/hardfault.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ void MemManage_Handler(void)

void Abort(const char* msg)
{
util_log("%s", msg);
screen_print_debug(msg, 0);
usb_stop();
#if !defined(TESTING)
Expand Down
10 changes: 8 additions & 2 deletions src/platform/platform_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,22 @@
#endif
#include "rust/rust.h"

#if defined(BOOTLOADER)
#define PREFIX "boot"
#else
#define PREFIX "app"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fw or firmware? to disambiguate from our other use of app (bitcoin/ethereum/u2f/...)

#endif

void platform_init(void)
{
oled_init();
#if !defined(BOOTLOADER)
// The factory setup image already has a c implementation of RTT.
#if FACTORYSETUP != 1
// these two functions are noops if "rtt" feature isn't enabled in rust
rust_rtt_init();
util_log("platform_init");
util_log(PREFIX ": platform_init");
#endif
#if !defined(BOOTLOADER)
sd_mmc_start();
#endif
}
5 changes: 5 additions & 0 deletions src/rust/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
[source.crates-io]
replace-with = "vendored-sources"

[source."git+https://github.com/BitBoxSwiss/rtt-target.git?branch=bitbox02"]
git = "https://github.com/BitBoxSwiss/rtt-target.git"
branch = "bitbox02"
replace-with = "vendored-sources"

[source."git+https://github.com/digitalbitbox/rust-bip32-ed25519?tag=v0.1.2"]
git = "https://github.com/BitBoxSwiss/rust-bip32-ed25519"
tag = "v0.2.0"
Expand Down
14 changes: 10 additions & 4 deletions src/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ sha2 = { version = "0.10.8", default-features = false }
sha3 = { version = "0.10.8", default-features = false }
zeroize = "1.7.0"

[patch.crates-io]
rtt-target = { git = "https://github.com/BitBoxSwiss/rtt-target.git", branch = "bitbox02" }

[profile.release]
# This only affects the .elf output. Debug info is stripped from the final .bin.
# Paths to source code can still appear in the final bin, as they are part of the panic!() output.
Expand Down
5 changes: 5 additions & 0 deletions src/rust/bitbox02-rust-c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ pub extern "C" fn rust_rtt_init() {
::util::log::rtt_init();
}

#[no_mangle]
pub extern "C" fn rust_rtt_flush() {
::util::log::rtt_flush();
}

/// # Safety
///
/// The pointer `ptr` must point to a null terminated string
Expand Down
2 changes: 1 addition & 1 deletion src/rust/util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ license = "Apache-2.0"

[dependencies]
num-bigint = { workspace = true, default-features = false }
rtt-target = { version = "0.5.0", optional = true }
rtt-target = { version = "0.6.1", optional = true }
cortex-m = { version = "0.7.7", features = ["critical-section-single-core"], optional = true }

[features]
Expand Down
25 changes: 23 additions & 2 deletions src/rust/util/src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,27 @@ pub use log;

pub fn rtt_init() {
#[cfg(feature = "rtt")]
rtt_target::rtt_init_print!();
log!("RTT Initialized");
{
let channels = rtt_target::rtt_init! {
up: {
0: {
size: 1024,
mode: rtt_target::ChannelMode::NoBlockSkip,
name: "Terminal",
section: ".segger_rtt_buf",
}
}
section_cb: ".segger_rtt"
};

rtt_target::set_print_channel(channels.up.0);

log!("RTT Initialized");
}
}

/// Wait until all messages have been read by host
pub fn rtt_flush() {
#[cfg(feature = "rtt")]
rtt_target::with_terminal_channel(|c| c.flush());
}
1 change: 1 addition & 0 deletions src/rust/vendor/portable-atomic/.cargo-checksum.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading