Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "write-bin-to-flash" subcommand #194

Merged
merged 1 commit into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions espflash/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
//! No stability guaranties apply

use std::{
fs,
io::Write,
fs::{self, File},
io::{Read, Write},
path::{Path, PathBuf},
};

Expand Down Expand Up @@ -83,6 +83,16 @@ pub struct PartitionTableOpts {
output: Option<PathBuf>,
}

#[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<Flasher> {
let port_info = get_serial_port_info(opts, config)?;

Expand Down Expand Up @@ -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(())
}
13 changes: 13 additions & 0 deletions espflash/src/flasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion espflash/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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)
Expand Down