diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d639781..e725567d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Add `Serialize` and `Deserialize` to `FlashFrequency`, `FlashMode` and `FlashSize`. (#528) - Add `checksum-md5` command (#536) - Add verify and skipping of unchanged flash regions - add `--no-verify` and `--no-skip` (#538) +- Add --min-chip-rev argument to specify minimum chip revision (#252) ### Fixed @@ -27,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixed a missed `flush` call that may be causing communication errors (#521) ### Changed +- Created `FlashData` and `FlashSettings` structs to reduce number of input arguments in some functions (#512) - espflash will now exit with an error if `defmt` is selected but not usable (#524) diff --git a/cargo-espflash/src/main.rs b/cargo-espflash/src/main.rs index 92bb826a..517bb372 100644 --- a/cargo-espflash/src/main.rs +++ b/cargo-espflash/src/main.rs @@ -15,6 +15,7 @@ use espflash::{ FlashConfigArgs, MonitorArgs, PartitionTableArgs, }, error::Error as EspflashError, + flasher::{FlashData, FlashSettings}, image_format::ImageFormatKind, logging::initialize_logger, targets::Chip, @@ -313,33 +314,32 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> { println!("Partition table: {}", path.display()); } - let partition_table = match partition_table { - Some(path) => Some(parse_partition_table(path)?), - None => None, - }; + let flash_settings = FlashSettings::new( + args.build_args.flash_config_args.flash_mode, + args.build_args.flash_config_args.flash_size, + args.build_args.flash_config_args.flash_freq, + ); + + let flash_data = FlashData::new( + bootloader, + partition_table, + args.flash_args.partition_table_offset, + args.flash_args.format.or(metadata.format), + args.flash_args.target_app_partition, + flash_settings, + args.flash_args.min_chip_rev, + )?; if args.flash_args.erase_parts.is_some() || args.flash_args.erase_data_parts.is_some() { erase_partitions( &mut flasher, - partition_table.clone(), + flash_data.partition_table.clone(), args.flash_args.erase_parts, args.flash_args.erase_data_parts, )?; } - flash_elf_image( - &mut flasher, - &elf_data, - bootloader, - partition_table, - args.flash_args.target_app_partition, - args.flash_args.format.or(metadata.format), - args.build_args.flash_config_args.flash_mode, - args.build_args.flash_config_args.flash_size, - args.build_args.flash_config_args.flash_freq, - args.flash_args.partition_table_offset, - args.flash_args.min_chip_rev, - )?; + flash_elf_image(&mut flasher, &elf_data, flash_data)?; } if args.flash_args.monitor { @@ -564,20 +564,28 @@ fn save_image(args: SaveImageArgs) -> Result<()> { println!("Partition table: {}", path.display()); } - save_elf_as_image( - args.save_image_args.chip, - args.save_image_args.min_chip_rev, - &elf_data, - args.save_image_args.file, - args.format.or(metadata.format), + let flash_settings = FlashSettings::new( args.build_args.flash_config_args.flash_mode, args.build_args.flash_config_args.flash_size, args.build_args.flash_config_args.flash_freq, + ); + + let flash_data = FlashData::new( + bootloader.as_deref(), + partition_table.as_deref(), args.save_image_args.partition_table_offset, - args.save_image_args.merge, - bootloader, - partition_table, + args.format.or(metadata.format), args.save_image_args.target_app_partition, + flash_settings, + args.save_image_args.min_chip_rev, + )?; + + save_elf_as_image( + &elf_data, + args.save_image_args.chip, + args.save_image_args.file, + flash_data, + args.save_image_args.merge, args.save_image_args.skip_padding, )?; diff --git a/espflash/src/bin/espflash.rs b/espflash/src/bin/espflash.rs index fe4bb86a..4df6c83f 100644 --- a/espflash/src/bin/espflash.rs +++ b/espflash/src/bin/espflash.rs @@ -14,6 +14,7 @@ use espflash::{ EspflashProgress, FlashConfigArgs, MonitorArgs, PartitionTableArgs, }, error::Error, + flasher::{FlashData, FlashSettings}, image_format::ImageFormatKind, logging::initialize_logger, targets::Chip, @@ -237,33 +238,32 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> { println!("Partition table: {}", path.display()); } - let partition_table = match partition_table { - Some(path) => Some(parse_partition_table(path)?), - None => None, - }; + let flash_settings = FlashSettings::new( + args.flash_config_args.flash_mode, + args.flash_config_args.flash_size, + args.flash_config_args.flash_freq, + ); + + let flash_data = FlashData::new( + bootloader, + partition_table, + args.flash_args.partition_table_offset, + args.flash_args.format, + args.flash_args.target_app_partition, + flash_settings, + args.flash_args.min_chip_rev, + )?; if args.flash_args.erase_parts.is_some() || args.flash_args.erase_data_parts.is_some() { erase_partitions( &mut flasher, - partition_table.clone(), + flash_data.partition_table.clone(), args.flash_args.erase_parts, args.flash_args.erase_data_parts, )?; } - flash_elf_image( - &mut flasher, - &elf_data, - bootloader, - partition_table, - args.flash_args.target_app_partition, - args.flash_args.format, - args.flash_config_args.flash_mode, - args.flash_config_args.flash_size, - args.flash_config_args.flash_freq, - args.flash_args.partition_table_offset, - args.flash_args.min_chip_rev, - )?; + flash_elf_image(&mut flasher, &elf_data, flash_data)?; } if args.flash_args.monitor { @@ -309,20 +309,28 @@ fn save_image(args: SaveImageArgs) -> Result<()> { println!("Partition table: {}", path.display()); } - save_elf_as_image( - args.save_image_args.chip, - args.save_image_args.min_chip_rev, - &elf_data, - args.save_image_args.file, - args.format, + let flash_settings = FlashSettings::new( args.flash_config_args.flash_mode, args.flash_config_args.flash_size, args.flash_config_args.flash_freq, + ); + + let flash_data = FlashData::new( + args.save_image_args.bootloader.as_deref(), + args.save_image_args.partition_table.as_deref(), args.save_image_args.partition_table_offset, - args.save_image_args.merge, - args.save_image_args.bootloader, - args.save_image_args.partition_table, + args.format, args.save_image_args.target_app_partition, + flash_settings, + args.save_image_args.min_chip_rev, + )?; + + save_elf_as_image( + &elf_data, + args.save_image_args.chip, + args.save_image_args.file, + flash_data, + args.save_image_args.merge, args.save_image_args.skip_padding, )?; diff --git a/espflash/src/cli/mod.rs b/espflash/src/cli/mod.rs index f48138c3..7dd5ad9a 100644 --- a/espflash/src/cli/mod.rs +++ b/espflash/src/cli/mod.rs @@ -35,7 +35,7 @@ use self::{ use crate::{ elf::ElfFirmwareImage, error::{Error, MissingPartition, MissingPartitionTable}, - flasher::{FlashFrequency, FlashMode, FlashSize, Flasher, ProgressCallbacks}, + flasher::{FlashData, FlashFrequency, FlashMode, FlashSize, Flasher, ProgressCallbacks}, image_format::ImageFormatKind, interface::Interface, targets::Chip, @@ -418,69 +418,21 @@ pub fn serial_monitor(args: MonitorArgs, config: &Config) -> Result<()> { /// Convert the provided firmware image from ELF to binary pub fn save_elf_as_image( - chip: Chip, - min_rev_full: u16, elf_data: &[u8], + chip: Chip, image_path: PathBuf, - image_format: Option, - flash_mode: Option, - flash_size: Option, - flash_freq: Option, - partition_table_offset: Option, + flash_data: FlashData, merge: bool, - bootloader_path: Option, - partition_table_path: Option, - target_app_partition: Option, skip_padding: bool, ) -> Result<()> { let image = ElfFirmwareImage::try_from(elf_data)?; if merge { - // merge_bin is TRUE - // merge bootloader, partition table and app binaries - // basic functionality, only merge 3 binaries - - // If the '-B' option is provided, load the bootloader binary file at the - // specified path. - let bootloader = if let Some(bootloader_path) = bootloader_path { - let path = fs::canonicalize(bootloader_path).into_diagnostic()?; - let data = fs::read(path).into_diagnostic()?; - - Some(data) - } else { - None - }; - - // If the '-T' option is provided, load the partition table from - // the CSV or binary file at the specified path. - let partition_table = if let Some(partition_table_path) = partition_table_path { - let path = fs::canonicalize(partition_table_path).into_diagnostic()?; - let data = fs::read(path) - .into_diagnostic() - .wrap_err("Failed to open partition table")?; - - let table = PartitionTable::try_from(data).into_diagnostic()?; - - Some(table) - } else { - None - }; - // To get a chip revision, the connection is needed // For simplicity, the revision None is used - let image = chip.into_target().get_flash_image( - &image, - bootloader, - partition_table, - target_app_partition, - image_format, - None, - min_rev_full, - flash_mode, - flash_size, - flash_freq, - partition_table_offset, - )?; + let image = chip + .into_target() + .get_flash_image(&image, flash_data.clone(), None)?; display_image_size(image.app_size(), image.part_size()); @@ -505,25 +457,16 @@ pub fn save_elf_as_image( // Take flash_size as input parameter, if None, use default value of 4Mb let padding_bytes = vec![ 0xffu8; - flash_size.unwrap_or_default().size() as usize + flash_data.flash_settings.size.unwrap_or_default().size() + as usize - file.metadata().into_diagnostic()?.len() as usize ]; file.write_all(&padding_bytes).into_diagnostic()?; } } else { - let image = chip.into_target().get_flash_image( - &image, - None, - None, - None, - image_format, - None, - min_rev_full, - flash_mode, - flash_size, - flash_freq, - partition_table_offset, - )?; + let image = chip + .into_target() + .get_flash_image(&image, flash_data, None)?; display_image_size(image.app_size(), image.part_size()); @@ -629,42 +572,11 @@ pub fn erase_region(args: EraseRegionArgs, config: &Config) -> Result<()> { pub fn flash_elf_image( flasher: &mut Flasher, elf_data: &[u8], - bootloader: Option<&Path>, - partition_table: Option, - target_app_partition: Option, - image_format: Option, - flash_mode: Option, - flash_size: Option, - flash_freq: Option, - partition_table_offset: Option, - min_rev_full: u16, + flash_data: FlashData, ) -> Result<()> { - // If the '--bootloader' option is provided, load the binary file at the - // specified path. - let bootloader = if let Some(path) = bootloader { - let path = fs::canonicalize(path).into_diagnostic()?; - let data = fs::read(path).into_diagnostic()?; - - Some(data) - } else { - None - }; - // Load the ELF data, optionally using the provider bootloader/partition // table/image format, to the device's flash memory. - flasher.load_elf_to_flash_with_format( - elf_data, - bootloader, - partition_table, - target_app_partition, - image_format, - flash_mode, - flash_size, - flash_freq, - partition_table_offset, - min_rev_full, - Some(&mut EspflashProgress::default()), - )?; + flasher.load_elf_to_flash(elf_data, flash_data, Some(&mut EspflashProgress::default()))?; info!("Flashing has completed!"); Ok(()) diff --git a/espflash/src/flasher/mod.rs b/espflash/src/flasher/mod.rs index c6d1e77a..157a5b9a 100644 --- a/espflash/src/flasher/mod.rs +++ b/espflash/src/flasher/mod.rs @@ -4,17 +4,19 @@ //! application to a target device. It additionally provides some operations to //! read information from the target device. -use std::{borrow::Cow, str::FromStr, thread::sleep}; +use std::{borrow::Cow, fs, path::Path, str::FromStr, thread::sleep}; use bytemuck::{Pod, Zeroable, __core::time::Duration}; use esp_idf_part::PartitionTable; use log::{debug, info, warn}; +use miette::{IntoDiagnostic, Result}; use serde::{Deserialize, Serialize}; use serialport::UsbPortInfo; use strum::{Display, EnumIter, EnumVariantNames}; use self::stubs::FlashStub; use crate::{ + cli::parse_partition_table, command::{Command, CommandType}, connection::Connection, elf::{ElfFirmwareImage, FirmwareImage, RomSegment}, @@ -254,6 +256,85 @@ impl FromStr for FlashSize { } } +/// Flash settings to use when flashing a device +#[derive(Copy, Clone, Debug)] +#[non_exhaustive] +pub struct FlashSettings { + pub mode: Option, + pub size: Option, + pub freq: Option, +} + +impl FlashSettings { + pub const fn default() -> Self { + FlashSettings { + mode: None, + size: None, + freq: None, + } + } + pub fn new( + mode: Option, + size: Option, + freq: Option, + ) -> Self { + FlashSettings { mode, size, freq } + } +} + +/// Flash data and configuration +#[derive(Debug, Clone)] +#[non_exhaustive] +pub struct FlashData { + pub bootloader: Option>, + pub partition_table: Option, + pub partition_table_offset: Option, + pub image_format: Option, + pub target_app_partition: Option, + pub flash_settings: FlashSettings, + pub min_chip_rev: u16, +} + +impl FlashData { + pub fn new( + bootloader: Option<&Path>, + partition_table: Option<&Path>, + partition_table_offset: Option, + image_format: Option, + target_app_partition: Option, + flash_settings: FlashSettings, + min_chip_rev: u16, + ) -> Result { + // If the '--bootloader' option is provided, load the binary file at the + // specified path. + let bootloader = if let Some(path) = bootloader { + let path = fs::canonicalize(path).into_diagnostic()?; + let data = fs::read(path).into_diagnostic()?; + + Some(data) + } else { + None + }; + + // If the '-T' option is provided, load the partition table from + // the CSV or binary file at the specified path. + let partition_table = match partition_table { + Some(path) => Some(parse_partition_table(path)?), + None => None, + }; + + Ok(FlashData { + bootloader, + partition_table, + partition_table_offset, + image_format, + target_app_partition, + flash_settings, + min_chip_rev, + }) + } +} + /// Parameters of the attached SPI flash chip (sizes, etc). #[derive(Copy, Clone, Debug)] #[repr(C)] @@ -808,18 +889,10 @@ impl Flasher { } /// Load an ELF image to flash and execute it - pub fn load_elf_to_flash_with_format( + pub fn load_elf_to_flash( &mut self, elf_data: &[u8], - bootloader: Option>, - partition_table: Option, - target_app_partition: Option, - image_format: Option, - flash_mode: Option, - flash_size: Option, - flash_freq: Option, - partition_table_offset: Option, - min_rev_full: u16, + flash_data: FlashData, mut progress: Option<&mut dyn ProgressCallbacks>, ) -> Result<(), Error> { let image = ElfFirmwareImage::try_from(elf_data)?; @@ -841,19 +914,10 @@ impl Flasher { None }; - let image = self.chip.into_target().get_flash_image( - &image, - bootloader, - partition_table, - target_app_partition, - image_format, - chip_revision, - min_rev_full, - flash_mode, - flash_size.or(Some(self.flash_size)), - flash_freq, - partition_table_offset, - )?; + let image = self + .chip + .into_target() + .get_flash_image(&image, flash_data, chip_revision)?; // When the `cli` feature is enabled, display the image size information. #[cfg(feature = "cli")] @@ -915,35 +979,6 @@ impl Flasher { }) } - /// Load an ELF image to flash and execute it - pub fn load_elf_to_flash( - &mut self, - elf_data: &[u8], - bootloader: Option>, - partition_table: Option, - target_app_partition: Option, - flash_mode: Option, - flash_size: Option, - flash_freq: Option, - partition_table_offset: Option, - min_rev_full: u16, - progress: Option<&mut dyn ProgressCallbacks>, - ) -> Result<(), Error> { - self.load_elf_to_flash_with_format( - elf_data, - bootloader, - partition_table, - target_app_partition, - None, - flash_mode, - flash_size, - flash_freq, - partition_table_offset, - min_rev_full, - progress, - ) - } - pub fn change_baud(&mut self, speed: u32) -> Result<(), Error> { debug!("Change baud to: {}", speed); diff --git a/espflash/src/image_format/esp8266.rs b/espflash/src/image_format/esp8266.rs index 528d88d4..39d4f001 100644 --- a/espflash/src/image_format/esp8266.rs +++ b/espflash/src/image_format/esp8266.rs @@ -5,7 +5,7 @@ use bytemuck::bytes_of; use crate::{ elf::{CodeSegment, FirmwareImage, RomSegment}, error::Error, - flasher::{FlashFrequency, FlashMode, FlashSize}, + flasher::FlashSettings, image_format::{ update_checksum, ImageFormat, ImageHeader, SegmentHeader, ESP_CHECKSUM_MAGIC, ESP_MAGIC, }, @@ -22,9 +22,7 @@ pub struct Esp8266Format<'a> { impl<'a> Esp8266Format<'a> { pub fn new( image: &'a dyn FirmwareImage<'a>, - flash_mode: Option, - flash_size: Option, - flash_freq: Option, + flash_settings: FlashSettings, ) -> Result { // IROM goes into a separate plain binary let irom_data = merge_rom_segments(image.rom_segments(Chip::Esp8266)); @@ -37,7 +35,7 @@ impl<'a> Esp8266Format<'a> { ); // Common header - let flash_mode = flash_mode.unwrap_or_default() as u8; + let flash_mode = flash_settings.mode.unwrap_or_default() as u8; let segment_count = image.ram_segments(Chip::Esp8266).count() as u8; let mut header = ImageHeader { @@ -48,8 +46,8 @@ impl<'a> Esp8266Format<'a> { ..Default::default() }; header.write_flash_config( - flash_size.unwrap_or_default(), - flash_freq.unwrap_or_default(), + flash_settings.size.unwrap_or_default(), + flash_settings.freq.unwrap_or_default(), Chip::Esp8266, )?; @@ -178,7 +176,7 @@ mod tests { let expected_bin = fs::read("tests/resources/esp8266_hal_blinky.bin").unwrap(); let image = ElfFirmwareImage::try_from(input_bytes.as_slice()).unwrap(); - let flash_image = Esp8266Format::new(&image, None, None, None).unwrap(); + let flash_image = Esp8266Format::new(&image, FlashSettings::default()).unwrap(); let segments = flash_image.flash_segments().collect::>(); let buf = segments[0].data.as_ref(); diff --git a/espflash/src/image_format/idf_bootloader.rs b/espflash/src/image_format/idf_bootloader.rs index 9ff393ec..4d671844 100644 --- a/espflash/src/image_format/idf_bootloader.rs +++ b/espflash/src/image_format/idf_bootloader.rs @@ -7,7 +7,7 @@ use sha2::{Digest, Sha256}; use crate::{ elf::{CodeSegment, FirmwareImage, RomSegment}, error::Error, - flasher::{FlashFrequency, FlashMode, FlashSize}, + flasher::FlashSettings, image_format::{ update_checksum, ImageFormat, ImageHeader, SegmentHeader, ESP_CHECKSUM_MAGIC, ESP_MAGIC, WP_PIN_DISABLED, @@ -37,15 +37,14 @@ impl<'a> IdfBootloaderFormat<'a> { min_rev_full: u16, params: Esp32Params, partition_table: Option, + partition_table_offset: Option, target_app_partition: Option, bootloader: Option>, - flash_mode: Option, - flash_size: Option, - flash_freq: Option, - partition_table_offset: Option, + flash_settings: FlashSettings, ) -> Result { - let partition_table = partition_table - .unwrap_or_else(|| params.default_partition_table(flash_size.map(|v| v.size()))); + let partition_table = partition_table.unwrap_or_else(|| { + params.default_partition_table(flash_settings.size.map(|v| v.size())) + }); let mut bootloader = if let Some(bytes) = bootloader { Cow::Owned(bytes) } else { @@ -59,13 +58,13 @@ impl<'a> IdfBootloaderFormat<'a> { } // update the header if a user has specified any custom arguments - if let Some(mode) = flash_mode { + if let Some(mode) = flash_settings.mode { header.flash_mode = mode as u8; } header.write_flash_config( - flash_size.unwrap_or_default(), - flash_freq.unwrap_or(params.flash_freq), + flash_settings.size.unwrap_or_default(), + flash_settings.freq.unwrap_or(params.flash_freq), chip, )?; @@ -345,7 +344,7 @@ pub mod tests { use std::fs; use super::*; - use crate::elf::ElfFirmwareImage; + use crate::{elf::ElfFirmwareImage, image_format::FlashFrequency}; // Copied from: src/targets/esp32.rs const PARAMS: Esp32Params = Esp32Params::new( @@ -372,9 +371,7 @@ pub mod tests { None, None, None, - None, - None, - None, + FlashSettings::default(), ) .unwrap(); diff --git a/espflash/src/targets/esp32.rs b/espflash/src/targets/esp32.rs index bbcee321..03e9135d 100644 --- a/espflash/src/targets/esp32.rs +++ b/espflash/src/targets/esp32.rs @@ -1,12 +1,10 @@ use std::ops::Range; -use esp_idf_part::PartitionTable; - use crate::{ connection::Connection, elf::FirmwareImage, error::{Error, UnsupportedImageFormatError}, - flasher::{FlashFrequency, FlashMode, FlashSize}, + flasher::{FlashData, FlashFrequency}, image_format::{IdfBootloaderFormat, ImageFormat, ImageFormatKind}, targets::{bytes_to_mac_addr, Chip, Esp32Params, ReadEFuse, SpiRegisters, Target}, }; @@ -153,32 +151,24 @@ impl Target for Esp32 { fn get_flash_image<'a>( &self, image: &'a dyn FirmwareImage<'a>, - bootloader: Option>, - partition_table: Option, - target_app_partition: Option, - image_format: Option, + flash_data: FlashData, _chip_revision: Option<(u32, u32)>, - min_rev_full: u16, - flash_mode: Option, - flash_size: Option, - flash_freq: Option, - partition_table_offset: Option, ) -> Result + 'a>, Error> { - let image_format = image_format.unwrap_or(ImageFormatKind::EspBootloader); + let image_format = flash_data + .image_format + .unwrap_or(ImageFormatKind::EspBootloader); match image_format { ImageFormatKind::EspBootloader => Ok(Box::new(IdfBootloaderFormat::new( image, Chip::Esp32, - min_rev_full, + flash_data.min_chip_rev, PARAMS, - partition_table, - target_app_partition, - bootloader, - flash_mode, - flash_size, - flash_freq, - partition_table_offset, + flash_data.partition_table, + flash_data.partition_table_offset, + flash_data.target_app_partition, + flash_data.bootloader, + flash_data.flash_settings, )?)), _ => Err(UnsupportedImageFormatError::new(image_format, Chip::Esp32, None).into()), } diff --git a/espflash/src/targets/esp32c2.rs b/espflash/src/targets/esp32c2.rs index da12bf62..848ec164 100644 --- a/espflash/src/targets/esp32c2.rs +++ b/espflash/src/targets/esp32c2.rs @@ -1,12 +1,10 @@ use std::{collections::HashMap, ops::Range}; -use esp_idf_part::PartitionTable; - use crate::{ connection::Connection, elf::FirmwareImage, error::Error, - flasher::{FlashFrequency, FlashMode, FlashSize}, + flasher::{FlashData, FlashFrequency}, image_format::{DirectBootFormat, IdfBootloaderFormat, ImageFormat, ImageFormatKind}, targets::{bytes_to_mac_addr, Chip, Esp32Params, ReadEFuse, SpiRegisters, Target}, }; @@ -87,32 +85,24 @@ impl Target for Esp32c2 { fn get_flash_image<'a>( &self, image: &'a dyn FirmwareImage<'a>, - bootloader: Option>, - partition_table: Option, - target_app_partition: Option, - image_format: Option, + flash_data: FlashData, _chip_revision: Option<(u32, u32)>, - min_rev_full: u16, - flash_mode: Option, - flash_size: Option, - flash_freq: Option, - partition_table_offset: Option, ) -> Result + 'a>, Error> { - let image_format = image_format.unwrap_or(ImageFormatKind::EspBootloader); + let image_format = flash_data + .image_format + .unwrap_or(ImageFormatKind::EspBootloader); match image_format { ImageFormatKind::EspBootloader => Ok(Box::new(IdfBootloaderFormat::new( image, Chip::Esp32c2, - min_rev_full, + flash_data.min_chip_rev, PARAMS, - partition_table, - target_app_partition, - bootloader, - flash_mode, - flash_size, - flash_freq, - partition_table_offset, + flash_data.partition_table, + flash_data.partition_table_offset, + flash_data.target_app_partition, + flash_data.bootloader, + flash_data.flash_settings, )?)), ImageFormatKind::DirectBoot => Ok(Box::new(DirectBootFormat::new(image, 0)?)), } diff --git a/espflash/src/targets/esp32c3.rs b/espflash/src/targets/esp32c3.rs index 3ec97f04..2f2cd195 100644 --- a/espflash/src/targets/esp32c3.rs +++ b/espflash/src/targets/esp32c3.rs @@ -1,12 +1,10 @@ use std::ops::Range; -use esp_idf_part::PartitionTable; - use crate::{ connection::Connection, elf::FirmwareImage, error::{Error, UnsupportedImageFormatError}, - flasher::{FlashFrequency, FlashMode, FlashSize}, + flasher::{FlashData, FlashFrequency}, image_format::{DirectBootFormat, IdfBootloaderFormat, ImageFormat, ImageFormatKind}, targets::{Chip, Esp32Params, ReadEFuse, SpiRegisters, Target}, }; @@ -74,32 +72,24 @@ impl Target for Esp32c3 { fn get_flash_image<'a>( &self, image: &'a dyn FirmwareImage<'a>, - bootloader: Option>, - partition_table: Option, - target_app_partition: Option, - image_format: Option, + flash_data: FlashData, chip_revision: Option<(u32, u32)>, - min_rev_full: u16, - flash_mode: Option, - flash_size: Option, - flash_freq: Option, - partition_table_offset: Option, ) -> Result + 'a>, Error> { - let image_format = image_format.unwrap_or(ImageFormatKind::EspBootloader); + let image_format = flash_data + .image_format + .unwrap_or(ImageFormatKind::EspBootloader); match (image_format, chip_revision) { (ImageFormatKind::EspBootloader, _) => Ok(Box::new(IdfBootloaderFormat::new( image, Chip::Esp32c3, - min_rev_full, + flash_data.min_chip_rev, PARAMS, - partition_table, - target_app_partition, - bootloader, - flash_mode, - flash_size, - flash_freq, - partition_table_offset, + flash_data.partition_table, + flash_data.partition_table_offset, + flash_data.target_app_partition, + flash_data.bootloader, + flash_data.flash_settings, )?)), (ImageFormatKind::DirectBoot, None | Some((_, 3..))) => { Ok(Box::new(DirectBootFormat::new(image, 0)?)) diff --git a/espflash/src/targets/esp32c6.rs b/espflash/src/targets/esp32c6.rs index dd624bb0..035605cc 100644 --- a/espflash/src/targets/esp32c6.rs +++ b/espflash/src/targets/esp32c6.rs @@ -1,12 +1,10 @@ use std::ops::Range; -use esp_idf_part::PartitionTable; - use crate::{ connection::Connection, elf::FirmwareImage, error::Error, - flasher::{FlashFrequency, FlashMode, FlashSize}, + flasher::{FlashData, FlashFrequency}, image_format::{DirectBootFormat, IdfBootloaderFormat, ImageFormat, ImageFormatKind}, targets::{Chip, Esp32Params, ReadEFuse, SpiRegisters, Target}, }; @@ -71,32 +69,24 @@ impl Target for Esp32c6 { fn get_flash_image<'a>( &self, image: &'a dyn FirmwareImage<'a>, - bootloader: Option>, - partition_table: Option, - target_app_partition: Option, - image_format: Option, + flash_data: FlashData, _chip_revision: Option<(u32, u32)>, - min_rev_full: u16, - flash_mode: Option, - flash_size: Option, - flash_freq: Option, - partition_table_offset: Option, ) -> Result + 'a>, Error> { - let image_format = image_format.unwrap_or(ImageFormatKind::EspBootloader); + let image_format = flash_data + .image_format + .unwrap_or(ImageFormatKind::EspBootloader); match image_format { ImageFormatKind::EspBootloader => Ok(Box::new(IdfBootloaderFormat::new( image, Chip::Esp32c6, - min_rev_full, + flash_data.min_chip_rev, PARAMS, - partition_table, - target_app_partition, - bootloader, - flash_mode, - flash_size, - flash_freq, - partition_table_offset, + flash_data.partition_table, + flash_data.partition_table_offset, + flash_data.target_app_partition, + flash_data.bootloader, + flash_data.flash_settings, )?)), ImageFormatKind::DirectBoot => Ok(Box::new(DirectBootFormat::new(image, 0x0)?)), } diff --git a/espflash/src/targets/esp32h2.rs b/espflash/src/targets/esp32h2.rs index 81a7523b..943e6ba9 100644 --- a/espflash/src/targets/esp32h2.rs +++ b/espflash/src/targets/esp32h2.rs @@ -1,14 +1,12 @@ use std::collections::HashMap; use std::ops::Range; -use esp_idf_part::PartitionTable; - use super::{Chip, Esp32Params, ReadEFuse, SpiRegisters, Target}; use crate::{ connection::Connection, elf::FirmwareImage, error::Error, - flasher::{FlashFrequency, FlashMode, FlashSize}, + flasher::{FlashData, FlashFrequency}, image_format::{DirectBootFormat, IdfBootloaderFormat, ImageFormat, ImageFormatKind}, }; @@ -79,32 +77,24 @@ impl Target for Esp32h2 { fn get_flash_image<'a>( &self, image: &'a dyn FirmwareImage<'a>, - bootloader: Option>, - partition_table: Option, - target_app_partition: Option, - image_format: Option, + flash_data: FlashData, _chip_revision: Option<(u32, u32)>, - min_rev_full: u16, - flash_mode: Option, - flash_size: Option, - flash_freq: Option, - partition_table_offset: Option, ) -> Result + 'a>, Error> { - let image_format = image_format.unwrap_or(ImageFormatKind::EspBootloader); + let image_format = flash_data + .image_format + .unwrap_or(ImageFormatKind::EspBootloader); match image_format { ImageFormatKind::EspBootloader => Ok(Box::new(IdfBootloaderFormat::new( image, Chip::Esp32h2, - min_rev_full, + flash_data.min_chip_rev, PARAMS, - partition_table, - target_app_partition, - bootloader, - flash_mode, - flash_size, - flash_freq, - partition_table_offset, + flash_data.partition_table, + flash_data.partition_table_offset, + flash_data.target_app_partition, + flash_data.bootloader, + flash_data.flash_settings, )?)), ImageFormatKind::DirectBoot => Ok(Box::new(DirectBootFormat::new(image, 0x0)?)), } diff --git a/espflash/src/targets/esp32s2.rs b/espflash/src/targets/esp32s2.rs index 822bccc9..cabb2e56 100644 --- a/espflash/src/targets/esp32s2.rs +++ b/espflash/src/targets/esp32s2.rs @@ -1,12 +1,10 @@ use std::ops::Range; -use esp_idf_part::PartitionTable; - use crate::{ connection::Connection, elf::FirmwareImage, error::{Error, UnsupportedImageFormatError}, - flasher::{FlashFrequency, FlashMode, FlashSize, FLASH_WRITE_SIZE}, + flasher::{FlashData, FlashFrequency, FLASH_WRITE_SIZE}, image_format::{IdfBootloaderFormat, ImageFormat, ImageFormatKind}, targets::{Chip, Esp32Params, ReadEFuse, SpiRegisters, Target, MAX_RAM_BLOCK_SIZE}, }; @@ -139,32 +137,24 @@ impl Target for Esp32s2 { fn get_flash_image<'a>( &self, image: &'a dyn FirmwareImage<'a>, - bootloader: Option>, - partition_table: Option, - target_app_partition: Option, - image_format: Option, + flash_data: FlashData, _chip_revision: Option<(u32, u32)>, - min_rev_full: u16, - flash_mode: Option, - flash_size: Option, - flash_freq: Option, - partition_table_offset: Option, ) -> Result + 'a>, Error> { - let image_format = image_format.unwrap_or(ImageFormatKind::EspBootloader); + let image_format = flash_data + .image_format + .unwrap_or(ImageFormatKind::EspBootloader); match image_format { ImageFormatKind::EspBootloader => Ok(Box::new(IdfBootloaderFormat::new( image, Chip::Esp32s2, - min_rev_full, + flash_data.min_chip_rev, PARAMS, - partition_table, - target_app_partition, - bootloader, - flash_mode, - flash_size, - flash_freq, - partition_table_offset, + flash_data.partition_table, + flash_data.partition_table_offset, + flash_data.target_app_partition, + flash_data.bootloader, + flash_data.flash_settings, )?)), _ => Err(UnsupportedImageFormatError::new(image_format, Chip::Esp32s2, None).into()), } diff --git a/espflash/src/targets/esp32s3.rs b/espflash/src/targets/esp32s3.rs index 08d0b722..f4df473a 100644 --- a/espflash/src/targets/esp32s3.rs +++ b/espflash/src/targets/esp32s3.rs @@ -1,12 +1,10 @@ use std::ops::Range; -use esp_idf_part::PartitionTable; - use crate::{ connection::Connection, elf::FirmwareImage, error::Error, - flasher::{FlashFrequency, FlashMode, FlashSize}, + flasher::{FlashData, FlashFrequency}, image_format::{DirectBootFormat, IdfBootloaderFormat, ImageFormat, ImageFormatKind}, targets::{Chip, Esp32Params, ReadEFuse, SpiRegisters, Target}, }; @@ -93,32 +91,24 @@ impl Target for Esp32s3 { fn get_flash_image<'a>( &self, image: &'a dyn FirmwareImage<'a>, - bootloader: Option>, - partition_table: Option, - target_app_partition: Option, - image_format: Option, + flash_data: FlashData, _chip_revision: Option<(u32, u32)>, - min_rev_full: u16, - flash_mode: Option, - flash_size: Option, - flash_freq: Option, - partition_table_offset: Option, ) -> Result + 'a>, Error> { - let image_format = image_format.unwrap_or(ImageFormatKind::EspBootloader); + let image_format = flash_data + .image_format + .unwrap_or(ImageFormatKind::EspBootloader); match image_format { ImageFormatKind::EspBootloader => Ok(Box::new(IdfBootloaderFormat::new( image, Chip::Esp32s3, - min_rev_full, + flash_data.min_chip_rev, PARAMS, - partition_table, - target_app_partition, - bootloader, - flash_mode, - flash_size, - flash_freq, - partition_table_offset, + flash_data.partition_table, + flash_data.partition_table_offset, + flash_data.target_app_partition, + flash_data.bootloader, + flash_data.flash_settings, )?)), ImageFormatKind::DirectBoot => Ok(Box::new(DirectBootFormat::new(image, 0x400)?)), } diff --git a/espflash/src/targets/esp8266.rs b/espflash/src/targets/esp8266.rs index 1b03ddd0..53f73564 100644 --- a/espflash/src/targets/esp8266.rs +++ b/espflash/src/targets/esp8266.rs @@ -1,12 +1,10 @@ use std::ops::Range; -use esp_idf_part::PartitionTable; - use crate::{ connection::Connection, elf::FirmwareImage, error::{Error, UnsupportedImageFormatError}, - flasher::{FlashFrequency, FlashMode, FlashSize}, + flasher::FlashData, image_format::{Esp8266Format, ImageFormat, ImageFormatKind}, targets::{bytes_to_mac_addr, Chip, ReadEFuse, SpiRegisters, Target}, }; @@ -73,22 +71,17 @@ impl Target for Esp8266 { fn get_flash_image<'a>( &self, image: &'a dyn FirmwareImage<'a>, - _bootloader: Option>, - _partition_table: Option, - _target_app_partition: Option, - image_format: Option, + flash_data: FlashData, _chip_revision: Option<(u32, u32)>, - _min_rev_full: u16, - flash_mode: Option, - flash_size: Option, - flash_freq: Option, - _partition_table_offset: Option, ) -> Result + 'a>, Error> { - let image_format = image_format.unwrap_or(ImageFormatKind::EspBootloader); + let image_format = flash_data + .image_format + .unwrap_or(ImageFormatKind::EspBootloader); match image_format { ImageFormatKind::EspBootloader => Ok(Box::new(Esp8266Format::new( - image, flash_mode, flash_size, flash_freq, + image, + flash_data.flash_settings, )?)), _ => Err(UnsupportedImageFormatError::new(image_format, Chip::Esp8266, None).into()), } diff --git a/espflash/src/targets/mod.rs b/espflash/src/targets/mod.rs index 17492fa7..5ae72f51 100644 --- a/espflash/src/targets/mod.rs +++ b/espflash/src/targets/mod.rs @@ -27,7 +27,7 @@ use crate::{ connection::Connection, elf::FirmwareImage, error::Error, - flasher::{FlashFrequency, FlashMode, FlashSize, SpiAttachParams, FLASH_WRITE_SIZE}, + flasher::{FlashData, FlashFrequency, SpiAttachParams, FLASH_WRITE_SIZE}, image_format::{ImageFormat, ImageFormatKind}, }; @@ -297,16 +297,8 @@ pub trait Target: ReadEFuse { fn get_flash_image<'a>( &self, image: &'a dyn FirmwareImage<'a>, - bootloader: Option>, - partition_table: Option, - target_app_partition: Option, - image_format: Option, + flash_data: FlashData, chip_revision: Option<(u32, u32)>, - min_rev_full: u16, - flash_mode: Option, - flash_size: Option, - flash_freq: Option, - partition_table_offset: Option, ) -> Result + 'a>, Error>; /// What is the MAC address?