Skip to content

Commit aee5a82

Browse files
alazartechjessebraham
authored andcommitted
Add "write-bin-to-flash" subcommand
1 parent e5de4d2 commit aee5a82

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

espflash/src/cli/mod.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
//! No stability guaranties apply
44
55
use std::{
6-
fs,
7-
io::Write,
6+
fs::{self, File},
7+
io::{Read, Write},
88
path::{Path, PathBuf},
99
};
1010

@@ -83,6 +83,16 @@ pub struct PartitionTableOpts {
8383
output: Option<PathBuf>,
8484
}
8585

86+
#[derive(Parser)]
87+
pub struct WriteBinToFlashOpts {
88+
/// Address at which to write the binary file
89+
addr: u32,
90+
/// File containing the binary data to write
91+
bin_file: String,
92+
#[clap(flatten)]
93+
connect_opts: ConnectOpts,
94+
}
95+
8696
pub fn connect(opts: &ConnectOpts, config: &Config) -> Result<Flasher> {
8797
let port_info = get_serial_port_info(opts, config)?;
8898

@@ -342,3 +352,17 @@ pub fn partition_table(opts: PartitionTableOpts) -> Result<()> {
342352

343353
Ok(())
344354
}
355+
356+
pub fn write_bin_to_flash(opts: WriteBinToFlashOpts) -> Result<()> {
357+
let config = Config::load()?;
358+
let mut flasher = connect(&opts.connect_opts, &config)?;
359+
flasher.board_info()?;
360+
361+
let mut f = File::open(&opts.bin_file).into_diagnostic()?;
362+
let metadata = fs::metadata(&opts.bin_file).into_diagnostic()?;
363+
let mut buffer = vec![0; metadata.len() as usize];
364+
f.read(&mut buffer).into_diagnostic()?;
365+
366+
flasher.write_bin_to_flash(opts.addr, &buffer)?;
367+
Ok(())
368+
}

espflash/src/flasher.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,19 @@ impl Flasher {
480480
Ok(())
481481
}
482482

483+
/// Load an bin image to flash at a specific address
484+
pub fn write_bin_to_flash(&mut self, addr: u32, data: &[u8]) -> Result<(), Error> {
485+
let mut target = self.chip.flash_target(self.spi_params);
486+
target.begin(&mut self.connection).flashing()?;
487+
let segment = RomSegment {
488+
addr,
489+
data: Cow::from(data),
490+
};
491+
target.write_segment(&mut self.connection, segment)?;
492+
target.finish(&mut self.connection, true).flashing()?;
493+
Ok(())
494+
}
495+
483496
/// Load an elf image to flash and execute it
484497
pub fn load_elf_to_flash(
485498
&mut self,

espflash/src/main.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use clap::{IntoApp, Parser};
44
use espflash::{
55
cli::{
66
board_info, connect, flash_elf_image, monitor::monitor, partition_table, save_elf_as_image,
7-
ConnectOpts, FlashConfigOpts, FlashOpts, PartitionTableOpts,
7+
write_bin_to_flash, ConnectOpts, FlashConfigOpts, FlashOpts, PartitionTableOpts,
8+
WriteBinToFlashOpts,
89
},
910
Chip, Config, ImageFormatId,
1011
};
@@ -36,6 +37,8 @@ pub enum SubCommand {
3637
SaveImage(SaveImageOpts),
3738
/// Operations for partitions tables
3839
PartitionTable(PartitionTableOpts),
40+
/// Writes a binary file to a specific address in the chip's flash
41+
WriteBinToFlash(WriteBinToFlashOpts),
3942
}
4043

4144
#[derive(Parser)]
@@ -92,6 +95,7 @@ fn main() -> Result<()> {
9295
BoardInfo(opts) => board_info(opts, config),
9396
SaveImage(opts) => save_image(opts),
9497
PartitionTable(opts) => partition_table(opts),
98+
WriteBinToFlash(opts) => write_bin_to_flash(opts),
9599
}
96100
} else {
97101
flash(opts, config)

0 commit comments

Comments
 (0)