Skip to content

Commit 4b40291

Browse files
add binary size printing to monitor their evolution
1 parent d83d6b4 commit 4b40291

File tree

2 files changed

+111
-5
lines changed

2 files changed

+111
-5
lines changed

Justfile

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ build args="":
88
# Build production firmware package
99
build-minimal args="":
1010
cargo xtask {{args}} build-minimal-image
11-
@echo -n 'Commit '
11+
@echo '🚀​ Output Files Identification:'
12+
@echo -n ' Commit '
1213
@git rev-parse HEAD
13-
@sha256sum BtPackage/*
14+
@sha256sum BtPackage/* | sed 's/^/ /'
1415

1516
# Build debug firmware package with UART console without flash protection
1617
build-debug args="":
@@ -19,9 +20,10 @@ build-debug args="":
1920
# Build unsigned firmware package (without signing or packaging)
2021
build-unsigned args="":
2122
cargo xtask {{args}} build-unsigned
22-
@echo -n 'Commit '
23+
@echo '🚀​ Output Files Identification:'
24+
@echo -n ' Commit '
2325
@git rev-parse HEAD
24-
@sha256sum BtPackage/*
26+
@sha256sum BtPackage/* | sed 's/^/ /'
2527

2628
# Sign firmware with specified cosign2 config
2729
sign config="cosign2.toml":

xtask/src/main.rs

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,25 @@ fn build_bt_bootloader(verbose: bool) {
178178
exit(-1);
179179
}
180180

181+
// Create bootloader binary first to show actual size
182+
tracing::info!("Creating bootloader binary file");
183+
let mut cargo_cmd = Command::new(cargo());
184+
let cmd = cargo_cmd
185+
.current_dir(project_root().join("bootloader"))
186+
.args(["objcopy", "--release"]);
187+
let mut cmd = cmd.args(["--", "-j", ".text", "-O", "binary", "../BtPackage/bootloader.bin"]);
188+
if !verbose {
189+
cmd = cmd.stdout(Stdio::null()).stderr(Stdio::null());
190+
}
191+
let status = cmd.status().expect("Running Cargo objcopy failed");
192+
if !status.success() {
193+
tracing::error!("Bootloader binary generation failed");
194+
exit(-1);
195+
}
196+
197+
// Print bootloader binary size information
198+
print_bootloader_binary_size(&project_root().join("BtPackage/bootloader.bin"), "Bootloader Binary (actual size)");
199+
181200
tracing::info!("Generating bootloader hex file...");
182201
let mut cargo_cmd = Command::new(cargo());
183202
let cmd = cargo_cmd
@@ -209,6 +228,28 @@ fn build_bt_bootloader_debug(verbose: bool) {
209228
exit(-1);
210229
}
211230

231+
// Create debug bootloader binary first to show actual size
232+
tracing::info!("Creating debug bootloader binary file");
233+
let mut cargo_cmd = Command::new(cargo());
234+
let cmd = cargo_cmd
235+
.current_dir(project_root().join("bootloader"))
236+
.args(["objcopy", "--release"]);
237+
let mut cmd = cmd.args(["--", "-j", ".text", "-O", "binary", "../BtPackageDebug/bootloaderDebug.bin"]);
238+
if !verbose {
239+
cmd = cmd.stdout(Stdio::null()).stderr(Stdio::null());
240+
}
241+
let status = cmd.status().expect("Running Cargo objcopy failed");
242+
if !status.success() {
243+
tracing::error!("Debug bootloader binary generation failed");
244+
exit(-1);
245+
}
246+
247+
// Print debug bootloader binary size information
248+
print_bootloader_binary_size(
249+
&project_root().join("BtPackageDebug/bootloaderDebug.bin"),
250+
"Debug Bootloader Binary (actual size)",
251+
);
252+
212253
tracing::info!("Generating bootloader hex file...");
213254
let mut cargo_cmd = Command::new(cargo());
214255
let cmd = cargo_cmd
@@ -245,9 +286,30 @@ fn build_bt_firmware(verbose: bool) {
245286
exit(-1);
246287
}
247288

289+
// Create unpadded binary first to show actual size
290+
let mut cargo_cmd = Command::new(cargo());
291+
let cmd = cargo_cmd
292+
.current_dir(project_root().join("firmware"))
293+
.args(["objcopy", "--release"]);
294+
let mut cmd = cmd.args(["--", "-O", "binary", "../BtPackage/BT_application_unpadded.bin"]);
295+
if !verbose {
296+
cmd = cmd.stdout(Stdio::null()).stderr(Stdio::null());
297+
}
298+
let status = cmd.status().expect("Running Cargo objcopy failed");
299+
if !status.success() {
300+
tracing::error!("Firmware build failed");
301+
exit(-1);
302+
}
303+
248304
// Created a full populated flash image to avoid the signed fw is different from the slice to check.
249305
// We will always get the full slice of flash where app is flashed ( BASE_APP_ADDR up to BASE_BOOTLOADER_ADDR )
250306
tracing::info!("Creating BT application bin file");
307+
// Print actual binary size information
308+
print_binary_size(
309+
&project_root().join("BtPackage/BT_application_unpadded.bin"),
310+
"Firmware Binary (actual size)",
311+
);
312+
251313
let mut cargo_cmd = Command::new(cargo());
252314
let base_bootloader_addr = BASE_BOOTLOADER_ADDR.to_string();
253315
let cmd = cargo_cmd
@@ -256,7 +318,7 @@ fn build_bt_firmware(verbose: bool) {
256318
let mut cmd = cmd.args([
257319
"--",
258320
"--pad-to",
259-
base_bootloader_addr.as_str(),
321+
base_bootloader_addr.as_str(), // no need to reserve space for trailer because we don't use cosign2's extended signatures
260322
"-O",
261323
"binary",
262324
"../BtPackage/BT_application.bin",
@@ -556,6 +618,48 @@ fn patch_sd() {
556618
);
557619
}
558620

621+
fn print_binary_size(binary_path: &Path, description: &str) {
622+
if let Ok(metadata) = fs::metadata(binary_path) {
623+
let size_bytes = metadata.len();
624+
let size_kb = size_bytes as f64 / 1024.0;
625+
626+
// Calculate flash usage percentage
627+
// Available flash space for application: from BASE_APP_ADDR to BASE_BOOTLOADER_ADDR, minus signature header
628+
let app_flash_size = (BASE_BOOTLOADER_ADDR - BASE_APP_ADDR - SIGNATURE_HEADER_SIZE) as u64;
629+
let usage_percentage = (size_bytes as f64 / app_flash_size as f64) * 100.0;
630+
631+
println!("📊 {} Size:", description);
632+
println!(" Bytes: {} bytes", size_bytes);
633+
println!(" KiB: {:.2} KiB", size_kb);
634+
println!(" Flash Usage: {:.1}% of {} bytes available", usage_percentage, app_flash_size);
635+
} else {
636+
tracing::warn!("Could not read binary metadata for: {}", binary_path.display());
637+
}
638+
}
639+
640+
fn print_bootloader_binary_size(binary_path: &Path, description: &str) {
641+
if let Ok(metadata) = fs::metadata(binary_path) {
642+
let size_bytes = metadata.len();
643+
let size_kb = size_bytes as f64 / 1024.0;
644+
645+
// Calculate flash usage percentage for bootloader
646+
// Bootloader flash space: from BASE_BOOTLOADER_ADDR to end of flash (192K)
647+
let total_flash_size = 192 * 1024; // 192K in bytes
648+
let bootloader_flash_size = total_flash_size - BASE_BOOTLOADER_ADDR as u64;
649+
let usage_percentage = (size_bytes as f64 / bootloader_flash_size as f64) * 100.0;
650+
651+
println!("📊 {} Size:", description);
652+
println!(" Bytes: {} bytes", size_bytes);
653+
println!(" KiB: {:.2} KiB", size_kb);
654+
println!(
655+
" Flash Usage: {:.1}% of {} bytes available",
656+
usage_percentage, bootloader_flash_size
657+
);
658+
} else {
659+
tracing::warn!("Could not read binary metadata for: {}", binary_path.display());
660+
}
661+
}
662+
559663
fn main() {
560664
// Adding some info tracing just for logging activity
561665
env::set_var("RUST_LOG", "info");

0 commit comments

Comments
 (0)