mirror of
https://github.com/spacemeowx2/blflash.git
synced 2024-11-17 05:09:43 +00:00
feat: support flash elf
This commit is contained in:
parent
e6ddfd0857
commit
0018374ab2
@ -1,5 +1,8 @@
|
||||
use std::borrow::Cow;
|
||||
use std::cmp::Ordering;
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
io::{Cursor, Write},
|
||||
};
|
||||
|
||||
use xmas_elf::program::{SegmentData, Type};
|
||||
use xmas_elf::ElfFile;
|
||||
@ -43,21 +46,18 @@ impl<'a> FirmwareImage<'a> {
|
||||
Some(CodeSegment { addr, data, size })
|
||||
})
|
||||
}
|
||||
pub fn to_flash_bin(&self, chip: &dyn Chip) -> Vec<u8> {
|
||||
let segs = self
|
||||
.segments()
|
||||
.filter_map(|segment| chip.get_flash_segment(segment))
|
||||
.collect::<Vec<_>>();
|
||||
let size = segs
|
||||
.iter()
|
||||
.fold(0, |len, i| len.max(i.addr + i.data.len() as u32));
|
||||
pub fn to_flash_bin(&self, _chip: &dyn Chip) -> Vec<u8> {
|
||||
// TODO: why it works???
|
||||
let segs = self.segments().collect::<Vec<_>>();
|
||||
|
||||
let bin = Vec::new();
|
||||
let mut writer = Cursor::new(bin);
|
||||
|
||||
let mut bin = Vec::new();
|
||||
bin.resize(size as usize, 0xFF);
|
||||
for s in segs {
|
||||
bin[s.addr as usize..s.addr as usize + s.data.len()].copy_from_slice(&s.data);
|
||||
writer.write_all(&s.data).unwrap();
|
||||
}
|
||||
bin
|
||||
|
||||
writer.into_inner()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -243,7 +243,7 @@ impl BootHeaderCfg {
|
||||
}
|
||||
pub fn make_image(&mut self, offset: usize, mut image: Vec<u8>) -> Result<Vec<u8>, Error> {
|
||||
let binlen = ((image.len() + 15) / 16) * 16;
|
||||
image.resize(binlen, 0);
|
||||
image.resize(binlen, 0xFF);
|
||||
let hash = Sha256::digest(&image);
|
||||
self.update_sha256(&hash[..])?;
|
||||
self.boot_cfg.img_len = image.len() as u32;
|
||||
|
Loading…
Reference in New Issue
Block a user