Skip to content

Commit 1d1a390

Browse files
committed
Write the common header to the bootloader binary too
1 parent 33cfa80 commit 1d1a390

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

espflash/src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ https://github.com/espressif/esp32c3-direct-boot-example"
109109
help("The accepted values are: {:?}", FlashSize::VARIANTS)
110110
)]
111111
InvalidFlashSize(String),
112+
#[error("The provided bootloader binary is not valid")]
113+
InvalidBootloader,
112114
}
113115

114116
#[derive(Error, Debug, Diagnostic)]

espflash/src/image_format/esp32bootloader.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{borrow::Cow, io::Write, iter::once};
22

3-
use bytemuck::{bytes_of, Pod, Zeroable};
3+
use bytemuck::{bytes_of, Pod, Zeroable, from_bytes};
44
use sha2::{Digest, Sha256};
55

66
use crate::{
@@ -33,7 +33,7 @@ impl<'a> Esp32BootloaderFormat<'a> {
3333
bootloader: Option<Vec<u8>>,
3434
) -> Result<Self, Error> {
3535
let partition_table = partition_table.unwrap_or_else(|| params.default_partition_table());
36-
let bootloader = if let Some(bytes) = bootloader {
36+
let mut bootloader = if let Some(bytes) = bootloader {
3737
Cow::Owned(bytes)
3838
} else {
3939
Cow::Borrowed(params.default_bootloader)
@@ -48,6 +48,15 @@ impl<'a> Esp32BootloaderFormat<'a> {
4848
flash_config: encode_flash_size(image.flash_size)? + image.flash_frequency as u8,
4949
entry: image.entry,
5050
};
51+
52+
// Update the bootloader header
53+
let current: EspCommonHeader = *from_bytes(&bootloader[0..8]);
54+
if current.magic != ESP_MAGIC {
55+
return Err(Error::InvalidBootloader);
56+
}
57+
bootloader.to_mut()[2..4].copy_from_slice(&bytes_of(&header)[2..4]);
58+
59+
// write the header of the app
5160
data.write_all(bytes_of(&header))?;
5261

5362
let extended_header = ExtendedHeader {

espflash/src/image_format/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const ESP_MAGIC: u8 = 0xE9;
1717
const WP_PIN_DISABLED: u8 = 0xEE;
1818

1919
#[derive(Copy, Clone, Zeroable, Pod, Debug)]
20-
#[repr(C)]
20+
#[repr(C, packed)]
2121
struct EspCommonHeader {
2222
magic: u8,
2323
segment_count: u8,
@@ -27,7 +27,7 @@ struct EspCommonHeader {
2727
}
2828

2929
#[derive(Copy, Clone, Zeroable, Pod, Debug)]
30-
#[repr(C)]
30+
#[repr(C, packed)]
3131
struct SegmentHeader {
3232
addr: u32,
3333
length: u32,

0 commit comments

Comments
 (0)