From df2dd6cabcbff6af31745ccebb6236c554cbb916 Mon Sep 17 00:00:00 2001 From: Joseph Ross Date: Sat, 2 Sep 2023 13:44:01 -0700 Subject: [PATCH] Add support for `erase-region` subcommand. --- espflash/src/bin/espflash.rs | 20 +++++++++++++------- espflash/src/cli/mod.rs | 22 ++++++++++++++++++++++ 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/espflash/src/bin/espflash.rs b/espflash/src/bin/espflash.rs index 0347cede..8cb984f3 100644 --- a/espflash/src/bin/espflash.rs +++ b/espflash/src/bin/espflash.rs @@ -1,15 +1,15 @@ use std::{ fs::{self, File}, io::Read, - num::ParseIntError, path::PathBuf, }; use clap::{Args, CommandFactory, Parser, Subcommand}; +use espflash::cli::EraseRegionArgs; use espflash::{ cli::{ self, board_info, completions, config::Config, connect, erase_partitions, flash_elf_image, - monitor::monitor, parse_partition_table, partition_table, print_board_info, + monitor::monitor, parse_partition_table, parse_uint32, partition_table, print_board_info, save_elf_as_image, serial_monitor, CompletionsArgs, ConnectArgs, EraseFlashArgs, ErasePartsArgs, EspflashProgress, FlashConfigArgs, MonitorArgs, PartitionTableArgs, }, @@ -46,6 +46,8 @@ enum Commands { EraseFlash(EraseFlashArgs), /// Erase specified partitions EraseParts(ErasePartsArgs), + /// Erase specified region + EraseRegion(EraseRegionArgs), /// Flash an application in ELF format to a connected target device /// /// Given a path to an ELF file, first convert it into the appropriate @@ -125,11 +127,6 @@ struct WriteBinArgs { connect_args: ConnectArgs, } -/// Parses a string as a 32-bit unsigned integer. -fn parse_uint32(input: &str) -> Result { - parse_int::parse(input) -} - fn main() -> Result<()> { miette::set_panic_hook(); initialize_logger(LevelFilter::Info); @@ -154,6 +151,7 @@ fn main() -> Result<()> { Commands::Completions(args) => completions(&args, &mut Cli::command(), "espflash"), Commands::EraseFlash(args) => erase_flash(args, &config), Commands::EraseParts(args) => erase_parts(args, &config), + Commands::EraseRegion(args) => erase_region(args, &config), Commands::Flash(args) => flash(args, &config), Commands::Monitor(args) => serial_monitor(args, &config), Commands::PartitionTable(args) => partition_table(args), @@ -184,6 +182,14 @@ fn erase_parts(args: ErasePartsArgs, config: &Config) -> Result<()> { Ok(()) } +fn erase_region(args: EraseRegionArgs, config: &Config) -> Result<()> { + let mut flash = connect(&args.connect_args, config)?; + flash.erase_region(args.addr, args.size)?; + flash.connection().reset()?; + + Ok(()) +} + fn flash(args: FlashArgs, config: &Config) -> Result<()> { let mut flasher = connect(&args.connect_args, config)?; diff --git a/espflash/src/cli/mod.rs b/espflash/src/cli/mod.rs index b08e593b..e755da2d 100644 --- a/espflash/src/cli/mod.rs +++ b/espflash/src/cli/mod.rs @@ -7,6 +7,7 @@ //! [cargo-espflash]: https://crates.io/crates/cargo-espflash //! [espflash]: https://crates.io/crates/espflash +use std::num::ParseIntError; use std::{ collections::HashMap, fs, @@ -185,6 +186,27 @@ pub struct ErasePartsArgs { pub partition_table: PathBuf, } +/// Erase specified region of flash +#[derive(Debug, Args)] +pub struct EraseRegionArgs { + /// Connection configuration + #[clap(flatten)] + pub connect_args: ConnectArgs, + + /// Offset to start erasing from + #[arg(value_name = "OFFSET", value_parser = parse_uint32)] + pub addr: u32, + + /// Size of the region to erase + #[arg(value_name = "SIZE", value_parser = parse_uint32)] + pub size: u32, +} + +/// Parses a string as a 32-bit unsigned integer. +pub fn parse_uint32(input: &str) -> Result { + parse_int::parse(input) +} + /// Select a serial port and establish a connection with a target device pub fn connect(args: &ConnectArgs, config: &Config) -> Result { let port_info = get_serial_port_info(args, config)?;