Skip to content

Commit 25d709e

Browse files
committed
Extract common code from the flash subcommand action
1 parent b5b3f01 commit 25d709e

File tree

4 files changed

+100
-114
lines changed

4 files changed

+100
-114
lines changed

cargo-espflash/src/main.rs

Lines changed: 27 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ use espflash::{
1111
cli::{
1212
board_info,
1313
clap::{ConnectOpts, FlashOpts},
14-
connect,
14+
connect, flash_elf_image,
1515
monitor::monitor,
1616
save_elf_as_image,
1717
},
18-
Chip, Config, ImageFormatId, PartitionTable,
18+
Chip, Config, ImageFormatId,
1919
};
2020
use miette::{IntoDiagnostic, Result, WrapErr};
2121

@@ -122,79 +122,55 @@ fn flash(
122122
metadata: CargoEspFlashMeta,
123123
cargo_config: CargoConfig,
124124
) -> Result<()> {
125-
// Connect the Flasher to the target device and build the project, and store a
126-
// reference to the artifact path.
127125
let mut flasher = connect(&opts.connect_opts, &config)?;
128126

129-
let path = build(&opts.build_opts, &cargo_config, Some(flasher.chip()))
127+
let artifact_path = build(&opts.build_opts, &cargo_config, Some(flasher.chip()))
130128
.wrap_err("Failed to build project")?;
131129

132-
// If the '--bootloader' option is provided, load the binary file at the
133-
// specified path.
134-
let bootloader = if let Some(path) = opts
135-
.flash_opts
136-
.bootloader
137-
.as_deref()
138-
.or_else(|| metadata.bootloader.as_deref())
139-
{
140-
let path = fs::canonicalize(path).into_diagnostic()?;
141-
let data = fs::read(path).into_diagnostic()?;
142-
Some(data)
143-
} else {
144-
None
145-
};
146-
147-
// If the '--partition-table' option is provided, load the partition table from
148-
// the CSV at the specified path.
149-
let partition_table = if let Some(path) = opts
150-
.flash_opts
151-
.partition_table
152-
.as_deref()
153-
.or_else(|| metadata.partition_table.as_deref())
154-
{
155-
let path = fs::canonicalize(path).into_diagnostic()?;
156-
let data = fs::read_to_string(path)
157-
.into_diagnostic()
158-
.wrap_err("Failed to open partition table")?;
159-
let table =
160-
PartitionTable::try_from_str(data).wrap_err("Failed to parse partition table")?;
161-
Some(table)
162-
} else {
163-
None
164-
};
165-
166-
let image_format = opts
167-
.build_opts
168-
.format
169-
.as_deref()
170-
.map(ImageFormatId::from_str)
171-
.transpose()?
172-
.or(metadata.format);
173-
174130
// Print the board information once the project has successfully built. We do
175131
// here rather than upon connection to show the Cargo output prior to the board
176132
// information, rather than breaking up cargo-espflash's output.
177133
flasher.board_info()?;
178134

179135
// Read the ELF data from the build path and load it to the target.
180-
let elf_data = fs::read(path).into_diagnostic()?;
136+
let elf_data = fs::read(artifact_path).into_diagnostic()?;
137+
181138
if opts.flash_opts.ram {
182139
flasher.load_elf_to_ram(&elf_data)?;
183140
} else {
184-
flasher.load_elf_to_flash_with_format(
141+
let bootloader = opts
142+
.flash_opts
143+
.bootloader
144+
.as_deref()
145+
.or(metadata.bootloader.as_deref());
146+
147+
let partition_table = opts
148+
.flash_opts
149+
.partition_table
150+
.as_deref()
151+
.or(metadata.partition_table.as_deref());
152+
153+
let image_format = opts
154+
.build_opts
155+
.format
156+
.as_deref()
157+
.map(ImageFormatId::from_str)
158+
.transpose()?
159+
.or(metadata.format);
160+
161+
flash_elf_image(
162+
&mut flasher,
185163
&elf_data,
186164
bootloader,
187165
partition_table,
188166
image_format,
189167
)?;
190168
}
191-
println!("\nFlashing has completed!");
192169

193170
if opts.flash_opts.monitor {
194171
monitor(flasher.into_serial()).into_diagnostic()?;
195172
}
196173

197-
// We're all done!
198174
Ok(())
199175
}
200176

espflash/src/cli/clap.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,4 @@ pub struct FlashOpts {
2525
/// Open a serial monitor after flashing
2626
#[clap(long)]
2727
pub monitor: bool,
28-
/// Encrypt the flash contents
29-
#[clap(long, conflicts_with = "ram")]
30-
pub encrypt: bool,
31-
/// Encryption key to encrypt the flash contents with
32-
#[clap(long, requires = "encrypt")]
33-
pub encryption_key: Option<String>,
3428
}

espflash/src/cli/mod.rs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
//!
33
//! No stability guaranties apply
44
5-
use std::{fs, path::PathBuf};
5+
use std::{
6+
fs,
7+
path::{Path, PathBuf},
8+
};
69

710
use config::Config;
811
use miette::{IntoDiagnostic, Result, WrapErr};
@@ -11,6 +14,7 @@ use serialport::{FlowControl, SerialPortType};
1114
use self::clap::ConnectOpts;
1215
use crate::{
1316
cli::serial::get_serial_port_info, error::Error, Chip, FirmwareImage, Flasher, ImageFormatId,
17+
PartitionTable,
1418
};
1519

1620
pub mod clap;
@@ -72,3 +76,45 @@ pub fn save_elf_as_image(
7276

7377
Ok(())
7478
}
79+
80+
pub fn flash_elf_image(
81+
flasher: &mut Flasher,
82+
elf_data: &[u8],
83+
bootloader: Option<&Path>,
84+
partition_table: Option<&Path>,
85+
image_format: Option<ImageFormatId>,
86+
) -> Result<()> {
87+
// If the '--bootloader' option is provided, load the binary file at the
88+
// specified path.
89+
let bootloader = if let Some(path) = bootloader {
90+
let path = fs::canonicalize(path).into_diagnostic()?;
91+
let data = fs::read(path).into_diagnostic()?;
92+
93+
Some(data)
94+
} else {
95+
None
96+
};
97+
98+
// If the '--partition-table' option is provided, load the partition table from
99+
// the CSV at the specified path.
100+
let partition_table = if let Some(path) = partition_table {
101+
let path = fs::canonicalize(path).into_diagnostic()?;
102+
let data = fs::read_to_string(path)
103+
.into_diagnostic()
104+
.wrap_err("Failed to open partition table")?;
105+
106+
let table =
107+
PartitionTable::try_from_str(data).wrap_err("Failed to parse partition table")?;
108+
109+
Some(table)
110+
} else {
111+
None
112+
};
113+
114+
// Load the ELF data, optionally using the provider bootloader/partition
115+
// table/image format, to the device's flash memory.
116+
flasher.load_elf_to_flash_with_format(elf_data, bootloader, partition_table, image_format)?;
117+
println!("\nFlashing has completed!");
118+
119+
Ok(())
120+
}

espflash/src/main.rs

Lines changed: 26 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
1-
use std::{
2-
fs::{self, read, read_to_string},
3-
mem::swap,
4-
path::PathBuf,
5-
str::FromStr,
6-
};
1+
use std::{fs, mem::swap, path::PathBuf, str::FromStr};
72

83
use clap::{AppSettings, IntoApp, Parser};
94
use espflash::{
105
cli::{
116
board_info,
127
clap::{ConnectOpts, FlashOpts},
13-
connect,
8+
connect, flash_elf_image,
149
monitor::monitor,
1510
save_elf_as_image,
1611
},
17-
Chip, Config, Error, ImageFormatId, PartitionTable,
12+
Chip, Config, ImageFormatId,
1813
};
1914
use miette::{IntoDiagnostic, Result, WrapErr};
2015

@@ -72,68 +67,43 @@ fn main() -> Result<()> {
7267
use SubCommand::*;
7368

7469
match subcommand {
75-
BoardInfo(matches) => board_info(matches, config),
76-
SaveImage(matches) => save_image(matches),
70+
BoardInfo(opts) => board_info(opts, config),
71+
SaveImage(opts) => save_image(opts),
7772
}
7873
} else {
7974
flash(opts, config)
8075
}
8176
}
8277

8378
fn flash(opts: Opts, config: Config) -> Result<()> {
84-
let ram = opts.flash_opts.ram;
85-
let bootloader_path = opts.flash_opts.bootloader;
86-
let partition_table_path = opts.flash_opts.partition_table;
87-
let image_format_string = opts.format;
88-
89-
let elf = match opts.image {
90-
Some(elf) => elf,
91-
_ => {
92-
Opts::into_app().print_help().ok();
93-
return Ok(());
94-
}
95-
};
96-
9779
let mut flasher = connect(&opts.connect_opts, &config)?;
80+
flasher.board_info()?;
9881

99-
let input_bytes = read(&elf)
100-
.into_diagnostic()
101-
.wrap_err_with(|| format!("Failed to open elf image \"{}\"", elf))?;
82+
let elf = if let Some(elf) = opts.image {
83+
elf
84+
} else {
85+
Opts::into_app().print_help().ok();
86+
return Ok(());
87+
};
88+
89+
// Read the ELF data from the build path and load it to the target.
90+
let elf_data = fs::read(&elf).into_diagnostic()?;
10291

103-
if ram {
104-
flasher.load_elf_to_ram(&input_bytes)?;
92+
if opts.flash_opts.ram {
93+
flasher.load_elf_to_ram(&elf_data)?;
10594
} else {
106-
let bootloader = bootloader_path
107-
.as_deref()
108-
.map(read)
109-
.transpose()
110-
.into_diagnostic()
111-
.wrap_err_with(|| {
112-
format!(
113-
"Failed to open bootloader image \"{}\"",
114-
bootloader_path.unwrap().display()
115-
)
116-
})?;
117-
let image_format = image_format_string
95+
let bootloader = opts.flash_opts.bootloader.as_deref();
96+
let partition_table = opts.flash_opts.partition_table.as_deref();
97+
98+
let image_format = opts
99+
.format
118100
.as_deref()
119101
.map(ImageFormatId::from_str)
120102
.transpose()?;
121-
let partition_table = partition_table_path
122-
.as_deref()
123-
.map(|path| {
124-
let table = read_to_string(path)?;
125-
PartitionTable::try_from_str(&table).map_err(Error::from)
126-
})
127-
.transpose()
128-
.into_diagnostic()
129-
.wrap_err_with(|| {
130-
format!(
131-
"Failed to load partition table \"{}\"",
132-
partition_table_path.unwrap().display()
133-
)
134-
})?;
135-
flasher.load_elf_to_flash_with_format(
136-
&input_bytes,
103+
104+
flash_elf_image(
105+
&mut flasher,
106+
&elf_data,
137107
bootloader,
138108
partition_table,
139109
image_format,

0 commit comments

Comments
 (0)