Skip to content

Commit 17a549a

Browse files
authored
Don't leak object crate's ElfFile32 type in public APIs (esp-rs#825)
* Don't leak `object` crate's `ElfFile32` type in public APIs * Update `CHANGELOG.md`
1 parent 3177481 commit 17a549a

File tree

13 files changed

+32
-53
lines changed

13 files changed

+32
-53
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3434
- Update flash size when creating the app partition (#797)
3535
- `--non-interactive` may now react to key events (user input, Ctrl-C, Ctrl-R) if possible (#819)
3636
- Removed `get_` prefix from any functions which previously had it (#824)
37+
- Take elf data as bytes rather than `ElfFile` struct when creating an image format (#825)
3738

3839
### Fixed
3940

espflash/src/cli/mod.rs

+5-12
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use esp_idf_part::{DataType, Partition, PartitionTable};
2525
use indicatif::{style::ProgressStyle, HumanCount, ProgressBar};
2626
use log::{debug, info, warn};
2727
use miette::{IntoDiagnostic, Result, WrapErr};
28-
use object::read::elf::ElfFile32 as ElfFile;
2928
use serialport::{FlowControl, SerialPortInfo, SerialPortType, UsbPortInfo};
3029

3130
use self::{
@@ -606,15 +605,13 @@ pub fn save_elf_as_image(
606605
skip_padding: bool,
607606
xtal_freq: XtalFrequency,
608607
) -> Result<()> {
609-
let elf = ElfFile::parse(elf_data).into_diagnostic()?;
608+
// To get a chip revision, the connection is needed
609+
// For simplicity, the revision None is used
610+
let image = chip
611+
.into_target()
612+
.flash_image(elf_data, flash_data.clone(), None, xtal_freq)?;
610613

611614
if merge {
612-
// To get a chip revision, the connection is needed
613-
// For simplicity, the revision None is used
614-
let image = chip
615-
.into_target()
616-
.flash_image(elf, flash_data.clone(), None, xtal_freq)?;
617-
618615
display_image_size(image.app_size(), image.part_size());
619616

620617
let mut file = fs::OpenOptions::new()
@@ -645,10 +642,6 @@ pub fn save_elf_as_image(
645642
file.write_all(&padding_bytes).into_diagnostic()?;
646643
}
647644
} else {
648-
let image = chip
649-
.into_target()
650-
.flash_image(elf, flash_data, None, xtal_freq)?;
651-
652645
display_image_size(image.app_size(), image.part_size());
653646

654647
let parts = image.ota_segments().collect::<Vec<_>>();

espflash/src/flasher/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1041,8 +1041,6 @@ impl Flasher {
10411041
mut progress: Option<&mut dyn ProgressCallbacks>,
10421042
xtal_freq: XtalFrequency,
10431043
) -> Result<(), Error> {
1044-
let elf = ElfFile::parse(elf_data)?;
1045-
10461044
let mut target =
10471045
self.chip
10481046
.flash_target(self.spi_params, self.use_stub, self.verify, self.skip);
@@ -1057,7 +1055,7 @@ impl Flasher {
10571055
let image =
10581056
self.chip
10591057
.into_target()
1060-
.flash_image(elf, flash_data, chip_revision, xtal_freq)?;
1058+
.flash_image(elf_data, flash_data, chip_revision, xtal_freq)?;
10611059

10621060
// When the `cli` feature is enabled, display the image size information.
10631061
#[cfg(feature = "cli")]

espflash/src/image_format/esp_idf.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,17 @@ pub struct IdfBootloaderFormat<'a> {
119119

120120
impl<'a> IdfBootloaderFormat<'a> {
121121
pub fn new(
122-
elf: ElfFile<'a>,
122+
elf_data: &'a [u8],
123123
chip: Chip,
124124
flash_data: FlashData,
125125
params: Esp32Params,
126126
) -> Result<Self, Error> {
127+
let elf = ElfFile::parse(elf_data)?;
128+
127129
let partition_table = flash_data.partition_table.unwrap_or_else(|| {
128130
default_partition_table(&params, flash_data.flash_settings.size.map(|v| v.size()))
129131
});
132+
130133
let mut bootloader = if let Some(bytes) = flash_data.bootloader {
131134
Cow::Owned(bytes)
132135
} else {

espflash/src/targets/esp32.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use std::ops::Range;
22

3-
use object::read::elf::ElfFile32 as ElfFile;
4-
53
#[cfg(feature = "serialport")]
64
use crate::{connection::Connection, targets::bytes_to_mac_addr};
75
use crate::{
@@ -157,12 +155,12 @@ impl Target for Esp32 {
157155

158156
fn flash_image<'a>(
159157
&self,
160-
elf: ElfFile<'a>,
158+
elf_data: &'a [u8],
161159
flash_data: FlashData,
162160
_chip_revision: Option<(u32, u32)>,
163161
xtal_freq: XtalFrequency,
164162
) -> Result<IdfBootloaderFormat<'a>, Error> {
165-
let booloader: &'static [u8] = match xtal_freq {
163+
let bootloader: &'static [u8] = match xtal_freq {
166164
XtalFrequency::_40Mhz => {
167165
include_bytes!("../../resources/bootloaders/esp32-bootloader.bin")
168166
}
@@ -183,10 +181,10 @@ impl Target for Esp32 {
183181
0x3f_0000,
184182
CHIP_ID,
185183
FlashFrequency::_40Mhz,
186-
booloader,
184+
bootloader,
187185
);
188186

189-
IdfBootloaderFormat::new(elf, Chip::Esp32, flash_data, params)
187+
IdfBootloaderFormat::new(elf_data, Chip::Esp32, flash_data, params)
190188
}
191189

192190
#[cfg(feature = "serialport")]

espflash/src/targets/esp32c2.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::{collections::HashMap, ops::Range};
22

33
use log::debug;
4-
use object::read::elf::ElfFile32 as ElfFile;
54

65
#[cfg(feature = "serialport")]
76
use crate::{connection::Connection, targets::bytes_to_mac_addr};
@@ -92,12 +91,12 @@ impl Target for Esp32c2 {
9291

9392
fn flash_image<'a>(
9493
&self,
95-
elf: ElfFile<'a>,
94+
elf_data: &'a [u8],
9695
flash_data: FlashData,
9796
_chip_revision: Option<(u32, u32)>,
9897
xtal_freq: XtalFrequency,
9998
) -> Result<IdfBootloaderFormat<'a>, Error> {
100-
let booloader: &'static [u8] = match xtal_freq {
99+
let bootloader: &'static [u8] = match xtal_freq {
101100
XtalFrequency::_40Mhz => {
102101
debug!("Using 40MHz bootloader");
103102
include_bytes!("../../resources/bootloaders/esp32c2-bootloader.bin")
@@ -120,10 +119,10 @@ impl Target for Esp32c2 {
120119
0x1f_0000,
121120
CHIP_ID,
122121
FlashFrequency::_30Mhz,
123-
booloader,
122+
bootloader,
124123
);
125124

126-
IdfBootloaderFormat::new(elf, Chip::Esp32c2, flash_data, params)
125+
IdfBootloaderFormat::new(elf_data, Chip::Esp32c2, flash_data, params)
127126
}
128127

129128
#[cfg(feature = "serialport")]

espflash/src/targets/esp32c3.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use std::ops::Range;
22

3-
use object::read::elf::ElfFile32 as ElfFile;
4-
53
#[cfg(feature = "serialport")]
64
use crate::connection::Connection;
75
use crate::{
@@ -81,7 +79,7 @@ impl Target for Esp32c3 {
8179

8280
fn flash_image<'a>(
8381
&self,
84-
elf: ElfFile<'a>,
82+
elf_data: &'a [u8],
8583
flash_data: FlashData,
8684
_chip_revision: Option<(u32, u32)>,
8785
xtal_freq: XtalFrequency,
@@ -93,7 +91,7 @@ impl Target for Esp32c3 {
9391
});
9492
}
9593

96-
IdfBootloaderFormat::new(elf, Chip::Esp32c3, flash_data, PARAMS)
94+
IdfBootloaderFormat::new(elf_data, Chip::Esp32c3, flash_data, PARAMS)
9795
}
9896

9997
fn spi_registers(&self) -> SpiRegisters {

espflash/src/targets/esp32c6.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use std::ops::Range;
22

3-
use object::read::elf::ElfFile32 as ElfFile;
4-
53
#[cfg(feature = "serialport")]
64
use crate::connection::Connection;
75
use crate::{
@@ -76,7 +74,7 @@ impl Target for Esp32c6 {
7674

7775
fn flash_image<'a>(
7876
&self,
79-
elf: ElfFile<'a>,
77+
elf_data: &'a [u8],
8078
flash_data: FlashData,
8179
_chip_revision: Option<(u32, u32)>,
8280
xtal_freq: XtalFrequency,
@@ -88,7 +86,7 @@ impl Target for Esp32c6 {
8886
});
8987
}
9088

91-
IdfBootloaderFormat::new(elf, Chip::Esp32c6, flash_data, PARAMS)
89+
IdfBootloaderFormat::new(elf_data, Chip::Esp32c6, flash_data, PARAMS)
9290
}
9391

9492
fn spi_registers(&self) -> SpiRegisters {

espflash/src/targets/esp32h2.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use std::{collections::HashMap, ops::Range};
22

3-
use object::read::elf::ElfFile32 as ElfFile;
4-
53
#[cfg(feature = "serialport")]
64
use crate::connection::Connection;
75
use crate::{
@@ -83,7 +81,7 @@ impl Target for Esp32h2 {
8381

8482
fn flash_image<'a>(
8583
&self,
86-
elf: ElfFile<'a>,
84+
elf_data: &'a [u8],
8785
flash_data: FlashData,
8886
_chip_revision: Option<(u32, u32)>,
8987
xtal_freq: XtalFrequency,
@@ -95,7 +93,7 @@ impl Target for Esp32h2 {
9593
});
9694
}
9795

98-
IdfBootloaderFormat::new(elf, Chip::Esp32h2, flash_data, PARAMS)
96+
IdfBootloaderFormat::new(elf_data, Chip::Esp32h2, flash_data, PARAMS)
9997
}
10098

10199
fn spi_registers(&self) -> SpiRegisters {

espflash/src/targets/esp32p4.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use std::ops::Range;
22

3-
use object::read::elf::ElfFile32 as ElfFile;
4-
53
#[cfg(feature = "serialport")]
64
use crate::connection::Connection;
75
use crate::{
@@ -73,7 +71,7 @@ impl Target for Esp32p4 {
7371

7472
fn flash_image<'a>(
7573
&self,
76-
elf: ElfFile<'a>,
74+
elf_data: &'a [u8],
7775
flash_data: FlashData,
7876
_chip_revision: Option<(u32, u32)>,
7977
xtal_freq: XtalFrequency,
@@ -85,7 +83,7 @@ impl Target for Esp32p4 {
8583
});
8684
}
8785

88-
IdfBootloaderFormat::new(elf, Chip::Esp32p4, flash_data, PARAMS)
86+
IdfBootloaderFormat::new(elf_data, Chip::Esp32p4, flash_data, PARAMS)
8987
}
9088

9189
fn spi_registers(&self) -> SpiRegisters {

espflash/src/targets/esp32s2.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use std::ops::Range;
22

3-
use object::read::elf::ElfFile32 as ElfFile;
4-
53
#[cfg(feature = "serialport")]
64
use super::flash_target::MAX_RAM_BLOCK_SIZE;
75
#[cfg(feature = "serialport")]
@@ -145,7 +143,7 @@ impl Target for Esp32s2 {
145143

146144
fn flash_image<'a>(
147145
&self,
148-
elf: ElfFile<'a>,
146+
elf_data: &'a [u8],
149147
flash_data: FlashData,
150148
_chip_revision: Option<(u32, u32)>,
151149
xtal_freq: XtalFrequency,
@@ -157,7 +155,7 @@ impl Target for Esp32s2 {
157155
});
158156
}
159157

160-
IdfBootloaderFormat::new(elf, Chip::Esp32s2, flash_data, PARAMS)
158+
IdfBootloaderFormat::new(elf_data, Chip::Esp32s2, flash_data, PARAMS)
161159
}
162160

163161
#[cfg(feature = "serialport")]

espflash/src/targets/esp32s3.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use std::ops::Range;
22

3-
use object::read::elf::ElfFile32 as ElfFile;
4-
53
#[cfg(feature = "serialport")]
64
use crate::connection::Connection;
75
use crate::{
@@ -100,7 +98,7 @@ impl Target for Esp32s3 {
10098

10199
fn flash_image<'a>(
102100
&self,
103-
elf: ElfFile<'a>,
101+
elf_data: &'a [u8],
104102
flash_data: FlashData,
105103
_chip_revision: Option<(u32, u32)>,
106104
xtal_freq: XtalFrequency,
@@ -112,7 +110,7 @@ impl Target for Esp32s3 {
112110
});
113111
}
114112

115-
IdfBootloaderFormat::new(elf, Chip::Esp32s3, flash_data, PARAMS)
113+
IdfBootloaderFormat::new(elf_data, Chip::Esp32s3, flash_data, PARAMS)
116114
}
117115

118116
fn spi_registers(&self) -> SpiRegisters {

espflash/src/targets/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
77
use std::collections::HashMap;
88

9-
use object::read::elf::ElfFile32 as ElfFile;
109
use serde::{Deserialize, Serialize};
1110
use strum::{Display, EnumIter, EnumString, VariantNames};
1211

@@ -355,7 +354,7 @@ pub trait Target: ReadEFuse {
355354
/// Build an image from the provided data for flashing
356355
fn flash_image<'a>(
357356
&self,
358-
elf: ElfFile<'a>,
357+
elf_data: &'a [u8],
359358
flash_data: FlashData,
360359
chip_revision: Option<(u32, u32)>,
361360
xtal_freq: XtalFrequency,

0 commit comments

Comments
 (0)