From df78f3ee7587c860d506323ad4641bd9c54cc941 Mon Sep 17 00:00:00 2001 From: Sergio Gasquez Date: Mon, 22 Jan 2024 12:04:20 +0100 Subject: [PATCH] feat: Add read_flash support to cargo-espflash --- cargo-espflash/src/main.rs | 9 ++++--- espflash/src/bin/espflash.rs | 49 +++--------------------------------- espflash/src/cli/mod.rs | 45 +++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 49 deletions(-) diff --git a/cargo-espflash/src/main.rs b/cargo-espflash/src/main.rs index 132b04b6..ab30c8c1 100644 --- a/cargo-espflash/src/main.rs +++ b/cargo-espflash/src/main.rs @@ -10,9 +10,9 @@ use espflash::{ cli::{ self, board_info, checksum_md5, completions, config::Config, connect, erase_flash, erase_partitions, erase_region, flash_elf_image, monitor::monitor, partition_table, - print_board_info, save_elf_as_image, serial_monitor, ChecksumMd5Args, CompletionsArgs, - ConnectArgs, EraseFlashArgs, EraseRegionArgs, EspflashProgress, FlashConfigArgs, - MonitorArgs, PartitionTableArgs, + print_board_info, read_flash, save_elf_as_image, serial_monitor, ChecksumMd5Args, + CompletionsArgs, ConnectArgs, EraseFlashArgs, EraseRegionArgs, EspflashProgress, + FlashConfigArgs, MonitorArgs, PartitionTableArgs, ReadFlashArgs, }, error::Error as EspflashError, flasher::{parse_partition_table, FlashData, FlashSettings}, @@ -100,6 +100,8 @@ enum Commands { /// '--to-binary' options, plus the ability to print a partition table /// in tabular format. PartitionTable(PartitionTableArgs), + /// Read SPI flash content + ReadFlash(ReadFlashArgs), /// Generate a binary application image and save it to a local disk /// /// If the '--merge' option is used, then the bootloader, partition table, @@ -219,6 +221,7 @@ fn main() -> Result<()> { Commands::Flash(args) => flash(args, &config), Commands::Monitor(args) => serial_monitor(args, &config), Commands::PartitionTable(args) => partition_table(args), + Commands::ReadFlash(args) => read_flash(args, &config), Commands::SaveImage(args) => save_image(args), Commands::ChecksumMd5(args) => checksum_md5(&args, &config), } diff --git a/espflash/src/bin/espflash.rs b/espflash/src/bin/espflash.rs index bff31d53..bf42833a 100644 --- a/espflash/src/bin/espflash.rs +++ b/espflash/src/bin/espflash.rs @@ -9,9 +9,9 @@ use espflash::{ cli::{ self, board_info, checksum_md5, completions, config::Config, connect, erase_flash, erase_partitions, erase_region, flash_elf_image, monitor::monitor, parse_uint32, - partition_table, print_board_info, save_elf_as_image, serial_monitor, ChecksumMd5Args, - CompletionsArgs, ConnectArgs, EraseFlashArgs, EraseRegionArgs, EspflashProgress, - FlashConfigArgs, MonitorArgs, PartitionTableArgs, + partition_table, print_board_info, read_flash, save_elf_as_image, serial_monitor, + ChecksumMd5Args, CompletionsArgs, ConnectArgs, EraseFlashArgs, EraseRegionArgs, + EspflashProgress, FlashConfigArgs, MonitorArgs, PartitionTableArgs, ReadFlashArgs, }, error::Error, flasher::{parse_partition_table, FlashData, FlashSettings}, @@ -121,31 +121,6 @@ struct FlashArgs { image: PathBuf, } -#[derive(Debug, Args)] -#[non_exhaustive] -pub struct ReadFlashArgs { - /// Offset to start reading from - #[arg(value_name = "OFFSET", value_parser = parse_uint32)] - pub addr: u32, - /// Size of each individual packet of data - /// - /// Defaults to 0x1000 (FLASH_SECTOR_SIZE) - #[arg(long, default_value = "0x1000", value_parser = parse_uint32)] - pub block_size: u32, - /// Connection configuration - #[clap(flatten)] - connect_args: ConnectArgs, - /// Size of the region to erase - #[arg(value_name = "SIZE", value_parser = parse_uint32)] - pub size: u32, - /// Name of binary dump - #[arg(value_name = "FILE")] - pub file: PathBuf, - /// Maximum number of un-acked packets - #[arg(long, default_value = "64", value_parser = parse_uint32)] - pub max_in_flight: u32, -} - #[derive(Debug, Args)] #[non_exhaustive] struct SaveImageArgs { @@ -322,24 +297,6 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> { } } -fn read_flash(args: ReadFlashArgs, config: &Config) -> Result<()> { - if args.connect_args.no_stub { - return Err(Error::StubRequired.into()); - } - - let mut flasher = connect(&args.connect_args, config, false, false)?; - print_board_info(&mut flasher)?; - flasher.read_flash( - args.addr, - args.size, - args.block_size, - args.max_in_flight, - args.file, - )?; - - Ok(()) -} - fn save_image(args: SaveImageArgs) -> Result<()> { let elf_data = fs::read(&args.image) .into_diagnostic() diff --git a/espflash/src/cli/mod.rs b/espflash/src/cli/mod.rs index 38cd0247..b650a19b 100644 --- a/espflash/src/cli/mod.rs +++ b/espflash/src/cli/mod.rs @@ -194,6 +194,32 @@ pub struct PartitionTableArgs { to_csv: bool, } +/// Reads the content of flash memory and saves it to a file +#[derive(Debug, Args)] +#[non_exhaustive] +pub struct ReadFlashArgs { + /// Offset to start reading from + #[arg(value_name = "OFFSET", value_parser = parse_uint32)] + pub addr: u32, + /// Size of each individual packet of data + /// + /// Defaults to 0x1000 (FLASH_SECTOR_SIZE) + #[arg(long, default_value = "0x1000", value_parser = parse_uint32)] + pub block_size: u32, + /// Connection configuration + #[clap(flatten)] + connect_args: ConnectArgs, + /// Size of the region to erase + #[arg(value_name = "SIZE", value_parser = parse_uint32)] + pub size: u32, + /// Name of binary dump + #[arg(value_name = "FILE")] + pub file: PathBuf, + /// Maximum number of un-acked packets + #[arg(long, default_value = "64", value_parser = parse_uint32)] + pub max_in_flight: u32, +} + /// Save the image to disk instead of flashing to device #[derive(Debug, Args)] #[non_exhaustive] @@ -677,6 +703,25 @@ fn erase_partition(flasher: &mut Flasher, part: &Partition) -> Result<()> { flasher.erase_region(offset, size).into_diagnostic() } +/// Read flash content and write it to a file +pub fn read_flash(args: ReadFlashArgs, config: &Config) -> Result<()> { + if args.connect_args.no_stub { + return Err(Error::StubRequired.into()); + } + + let mut flasher = connect(&args.connect_args, config, false, false)?; + print_board_info(&mut flasher)?; + flasher.read_flash( + args.addr, + args.size, + args.block_size, + args.max_in_flight, + args.file, + )?; + + Ok(()) +} + /// Convert and display CSV and binary partition tables pub fn partition_table(args: PartitionTableArgs) -> Result<()> { if args.to_binary {