From e030561fc5b12d2c48dbcaf0ef4d1dfcb044c64d Mon Sep 17 00:00:00 2001 From: Sergio Gasquez Date: Wed, 26 Feb 2025 16:16:50 +0100 Subject: [PATCH 1/3] feat: Add write-bin subcommand --- cargo-espflash/README.md | 1 + cargo-espflash/src/main.rs | 3 +++ espflash/src/bin/espflash.rs | 45 +----------------------------------- espflash/src/cli/mod.rs | 43 ++++++++++++++++++++++++++++++++-- 4 files changed, 46 insertions(+), 46 deletions(-) diff --git a/cargo-espflash/README.md b/cargo-espflash/README.md index cd631308..016cbcdf 100644 --- a/cargo-espflash/README.md +++ b/cargo-espflash/README.md @@ -71,6 +71,7 @@ Commands: read-flash Read SPI flash content reset Reset the target device save-image Generate a binary application image and save it to a local disk + write-bin Write a binary file to a specific address in a target device's flash help Print this message or the help of the given subcommand(s) Options: diff --git a/cargo-espflash/src/main.rs b/cargo-espflash/src/main.rs index 157b741f..84a3ef2a 100644 --- a/cargo-espflash/src/main.rs +++ b/cargo-espflash/src/main.rs @@ -117,6 +117,8 @@ enum Commands { /// Otherwise, each segment will be saved as individual binaries, prefixed /// with their intended addresses in flash. SaveImage(SaveImageArgs), + /// Write a binary file to a specific address in a target device's flash + WriteBin(WriteBinArgs), } #[derive(Debug, Args)] @@ -240,6 +242,7 @@ fn main() -> Result<()> { Commands::ReadFlash(args) => read_flash(args, &config), Commands::Reset(args) => reset(args, &config), Commands::SaveImage(args) => save_image(args, &config), + Commands::WriteBin(args) => write_bin(args, &config), } } diff --git a/espflash/src/bin/espflash.rs b/espflash/src/bin/espflash.rs index e439579d..4538620c 100644 --- a/espflash/src/bin/espflash.rs +++ b/espflash/src/bin/espflash.rs @@ -1,8 +1,4 @@ -use std::{ - fs::{self, File}, - io::Read, - path::PathBuf, -}; +use std::{fs, path::PathBuf}; use clap::{Args, CommandFactory, Parser, Subcommand}; use espflash::{ @@ -140,20 +136,6 @@ struct SaveImageArgs { save_image_args: cli::SaveImageArgs, } -/// Writes a binary file to a specific address in the chip's flash -#[derive(Debug, Args)] -#[non_exhaustive] -struct WriteBinArgs { - /// Address at which to write the binary file - #[arg(value_parser = parse_u32)] - pub address: u32, - /// File containing the binary data to write - pub file: String, - /// Connection configuration - #[clap(flatten)] - connect_args: ConnectArgs, -} - fn main() -> Result<()> { miette::set_panic_hook(); initialize_logger(LevelFilter::Info); @@ -341,28 +323,3 @@ fn save_image(args: SaveImageArgs, config: &Config) -> Result<()> { Ok(()) } - -fn write_bin(args: WriteBinArgs, config: &Config) -> Result<()> { - let mut flasher = connect(&args.connect_args, config, false, false)?; - print_board_info(&mut flasher)?; - - let mut f = File::open(&args.file).into_diagnostic()?; - - // If the file size is not divisible by 4, we need to pad `FF` bytes to the end - let size = f.metadata().into_diagnostic()?.len(); - let mut padded_bytes = 0; - if size % 4 != 0 { - padded_bytes = 4 - (size % 4); - } - let mut buffer = Vec::with_capacity(size.try_into().into_diagnostic()?); - f.read_to_end(&mut buffer).into_diagnostic()?; - buffer.extend(std::iter::repeat(0xFF).take(padded_bytes as usize)); - - flasher.write_bin_to_flash( - args.address, - &buffer, - Some(&mut EspflashProgress::default()), - )?; - - Ok(()) -} diff --git a/espflash/src/cli/mod.rs b/espflash/src/cli/mod.rs index 652ccfbc..b9e586ec 100644 --- a/espflash/src/cli/mod.rs +++ b/espflash/src/cli/mod.rs @@ -12,8 +12,8 @@ use std::{ collections::HashMap, - fs, - io::Write, + fs::{self, File}, + io::{Read, Write}, num::ParseIntError, path::{Path, PathBuf}, }; @@ -315,6 +315,20 @@ pub struct ListPortsArgs { pub name_only: bool, } +/// Writes a binary file to a specific address in the chip's flash +#[derive(Debug, Args)] +#[non_exhaustive] +pub struct WriteBinArgs { + /// Address at which to write the binary file + #[arg(value_parser = parse_u32)] + pub address: u32, + /// File containing the binary data to write + pub file: String, + /// Connection configuration + #[clap(flatten)] + connect_args: ConnectArgs, +} + /// Parses an integer, in base-10 or hexadecimal format, into a [u32] pub fn parse_u32(input: &str) -> Result { let input: &str = &input.replace('_', ""); @@ -972,6 +986,31 @@ pub fn make_flash_data( ) } +pub fn write_bin(args: WriteBinArgs, config: &Config) -> Result<()> { + let mut flasher = connect(&args.connect_args, config, false, false)?; + print_board_info(&mut flasher)?; + + let mut f = File::open(&args.file).into_diagnostic()?; + + // If the file size is not divisible by 4, we need to pad `FF` bytes to the end + let size = f.metadata().into_diagnostic()?.len(); + let mut padded_bytes = 0; + if size % 4 != 0 { + padded_bytes = 4 - (size % 4); + } + let mut buffer = Vec::with_capacity(size.try_into().into_diagnostic()?); + f.read_to_end(&mut buffer).into_diagnostic()?; + buffer.extend(std::iter::repeat(0xFF).take(padded_bytes as usize)); + + flasher.write_bin_to_flash( + args.address, + &buffer, + Some(&mut EspflashProgress::default()), + )?; + + Ok(()) +} + mod test { use clap::Parser; From 8b50b4c1b5accd75b56abd79f86dad1355a05ebb Mon Sep 17 00:00:00 2001 From: Sergio Gasquez Date: Thu, 27 Feb 2025 09:54:35 +0100 Subject: [PATCH 2/3] docs: Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f6d9ab8f..9b8483b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Add `no-reset` flag to `monitor` subcommands (#737) - Add an environment variable to set monitoring baudrate (`MONITOR_BAUD`) (#737) - Add list-ports command to list available serial ports. (#761) +- [cargo-espflash]: Add `write-bin` subcommand (#789) ### Changed From 633bce1233bd06cfc53c3c755dfba87176bcc582 Mon Sep 17 00:00:00 2001 From: Sergio Gasquez Date: Thu, 27 Feb 2025 10:42:59 +0100 Subject: [PATCH 3/3] docs: Add docstring --- espflash/src/cli/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/espflash/src/cli/mod.rs b/espflash/src/cli/mod.rs index b9e586ec..c541bede 100644 --- a/espflash/src/cli/mod.rs +++ b/espflash/src/cli/mod.rs @@ -986,6 +986,7 @@ pub fn make_flash_data( ) } +/// Write a binary to the flash memory of a target device pub fn write_bin(args: WriteBinArgs, config: &Config) -> Result<()> { let mut flasher = connect(&args.connect_args, config, false, false)?; print_board_info(&mut flasher)?;