diff --git a/espflash/src/cli/mod.rs b/espflash/src/cli/mod.rs index cd4413a0..391e0991 100644 --- a/espflash/src/cli/mod.rs +++ b/espflash/src/cli/mod.rs @@ -3,8 +3,8 @@ //! No stability guaranties apply use std::{ - fs, - io::Write, + fs::{self, File}, + io::{Read, Write}, path::{Path, PathBuf}, }; @@ -83,6 +83,16 @@ pub struct PartitionTableOpts { output: Option, } +#[derive(Parser)] +pub struct WriteBinToFlashOpts { + /// Address at which to write the binary file + addr: u32, + /// File containing the binary data to write + bin_file: String, + #[clap(flatten)] + connect_opts: ConnectOpts, +} + pub fn connect(opts: &ConnectOpts, config: &Config) -> Result { let port_info = get_serial_port_info(opts, config)?; @@ -342,3 +352,17 @@ pub fn partition_table(opts: PartitionTableOpts) -> Result<()> { Ok(()) } + +pub fn write_bin_to_flash(opts: WriteBinToFlashOpts) -> Result<()> { + let config = Config::load()?; + let mut flasher = connect(&opts.connect_opts, &config)?; + flasher.board_info()?; + + let mut f = File::open(&opts.bin_file).into_diagnostic()?; + let metadata = fs::metadata(&opts.bin_file).into_diagnostic()?; + let mut buffer = vec![0; metadata.len() as usize]; + f.read(&mut buffer).into_diagnostic()?; + + flasher.write_bin_to_flash(opts.addr, &buffer)?; + Ok(()) +} diff --git a/espflash/src/flasher.rs b/espflash/src/flasher.rs index edba79d6..6fe19ee0 100644 --- a/espflash/src/flasher.rs +++ b/espflash/src/flasher.rs @@ -480,6 +480,19 @@ impl Flasher { Ok(()) } + /// Load an bin image to flash at a specific address + pub fn write_bin_to_flash(&mut self, addr: u32, data: &[u8]) -> Result<(), Error> { + let mut target = self.chip.flash_target(self.spi_params); + target.begin(&mut self.connection).flashing()?; + let segment = RomSegment { + addr, + data: Cow::from(data), + }; + target.write_segment(&mut self.connection, segment)?; + target.finish(&mut self.connection, true).flashing()?; + Ok(()) + } + /// Load an elf image to flash and execute it pub fn load_elf_to_flash( &mut self, diff --git a/espflash/src/main.rs b/espflash/src/main.rs index bea22e2d..cfe32ddb 100644 --- a/espflash/src/main.rs +++ b/espflash/src/main.rs @@ -4,7 +4,8 @@ use clap::{IntoApp, Parser}; use espflash::{ cli::{ board_info, connect, flash_elf_image, monitor::monitor, partition_table, save_elf_as_image, - ConnectOpts, FlashConfigOpts, FlashOpts, PartitionTableOpts, + write_bin_to_flash, ConnectOpts, FlashConfigOpts, FlashOpts, PartitionTableOpts, + WriteBinToFlashOpts, }, Chip, Config, ImageFormatId, }; @@ -36,6 +37,8 @@ pub enum SubCommand { SaveImage(SaveImageOpts), /// Operations for partitions tables PartitionTable(PartitionTableOpts), + /// Writes a binary file to a specific address in the chip's flash + WriteBinToFlash(WriteBinToFlashOpts), } #[derive(Parser)] @@ -92,6 +95,7 @@ fn main() -> Result<()> { BoardInfo(opts) => board_info(opts, config), SaveImage(opts) => save_image(opts), PartitionTable(opts) => partition_table(opts), + WriteBinToFlash(opts) => write_bin_to_flash(opts), } } else { flash(opts, config)