feat: support flash elf

This commit is contained in:
spacemeowx2 2020-12-06 11:47:54 +08:00
parent e6ddfd0857
commit 0018374ab2
2 changed files with 14 additions and 14 deletions

View File

@ -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()
}
}

View File

@ -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;