Skip to content

Commit

Permalink
Add support for erase-region subcommand.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnross committed Sep 2, 2023
1 parent 8405686 commit df2dd6c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
20 changes: 13 additions & 7 deletions espflash/src/bin/espflash.rs
Original file line number Diff line number Diff line change
@@ -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,
},
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -125,11 +127,6 @@ struct WriteBinArgs {
connect_args: ConnectArgs,
}

/// Parses a string as a 32-bit unsigned integer.
fn parse_uint32(input: &str) -> Result<u32, ParseIntError> {
parse_int::parse(input)
}

fn main() -> Result<()> {
miette::set_panic_hook();
initialize_logger(LevelFilter::Info);
Expand All @@ -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),
Expand Down Expand Up @@ -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)?;

Expand Down
22 changes: 22 additions & 0 deletions espflash/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<u32, ParseIntError> {
parse_int::parse(input)
}

/// Select a serial port and establish a connection with a target device
pub fn connect(args: &ConnectArgs, config: &Config) -> Result<Flasher> {
let port_info = get_serial_port_info(args, config)?;
Expand Down

0 comments on commit df2dd6c

Please sign in to comment.