|
1 | 1 | use std::{borrow::Cow, io::Write, iter::once};
|
2 | 2 |
|
3 |
| -use bytemuck::{bytes_of, Pod, Zeroable, from_bytes}; |
| 3 | +use bytemuck::{bytes_of, from_bytes, Pod, Zeroable}; |
4 | 4 | use sha2::{Digest, Sha256};
|
5 | 5 |
|
6 | 6 | use crate::{
|
@@ -41,22 +41,41 @@ impl<'a> Esp32BootloaderFormat<'a> {
|
41 | 41 |
|
42 | 42 | let mut data = Vec::new();
|
43 | 43 |
|
44 |
| - let header = EspCommonHeader { |
45 |
| - magic: ESP_MAGIC, |
46 |
| - segment_count: 0, |
47 |
| - flash_mode: image.flash_mode as u8, |
48 |
| - flash_config: encode_flash_size(image.flash_size)? + image.flash_frequency as u8, |
49 |
| - entry: image.entry, |
50 |
| - }; |
51 |
| - |
52 |
| - // Update the bootloader header |
53 |
| - let current: EspCommonHeader = *from_bytes(&bootloader[0..8]); |
54 |
| - if current.magic != ESP_MAGIC { |
| 44 | + // fetch the generated header from the bootloader |
| 45 | + let mut header: EspCommonHeader = *from_bytes(&bootloader[0..8]); |
| 46 | + if header.magic != ESP_MAGIC { |
55 | 47 | return Err(Error::InvalidBootloader);
|
56 | 48 | }
|
57 |
| - bootloader.to_mut()[2..4].copy_from_slice(&bytes_of(&header)[2..4]); |
| 49 | + // update the header if a user has specified any custom arguments |
| 50 | + if image.flash_frequency.is_some() |
| 51 | + || image.flash_mode.is_some() |
| 52 | + || image.flash_size.is_some() |
| 53 | + { |
| 54 | + if let Some(mode) = image.flash_mode { |
| 55 | + header.flash_mode = mode as u8; |
| 56 | + bootloader.to_mut()[2] = bytes_of(&header)[2]; |
| 57 | + } |
| 58 | + match (image.flash_size, image.flash_frequency) { |
| 59 | + (Some(s), Some(f)) => { |
| 60 | + header.flash_config = encode_flash_size(s)? + f as u8; |
| 61 | + bootloader.to_mut()[3] = bytes_of(&header)[3]; |
| 62 | + } |
| 63 | + (Some(s), None) => { |
| 64 | + header.flash_config = encode_flash_size(s)? + (header.flash_config & 0x0F); |
| 65 | + bootloader.to_mut()[3] = bytes_of(&header)[3]; |
| 66 | + } |
| 67 | + (None, Some(f)) => { |
| 68 | + header.flash_config = (header.flash_config & 0xF0) + f as u8; |
| 69 | + bootloader.to_mut()[3] = bytes_of(&header)[3]; |
| 70 | + } |
| 71 | + (None, None) => {} // nothing to update |
| 72 | + } |
| 73 | + } |
58 | 74 |
|
59 | 75 | // write the header of the app
|
| 76 | + // use the same settings as the bootloader |
| 77 | + // just update the entry point |
| 78 | + header.entry = image.entry; |
60 | 79 | data.write_all(bytes_of(&header))?;
|
61 | 80 |
|
62 | 81 | let extended_header = ExtendedHeader {
|
|
0 commit comments