Created
March 7, 2026 04:18
-
-
Save standarddeviant/4a9e717798cd0a51f3d2ae58467e32c4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use byteorder_pack::UnpackFrom; | |
| use std::io::Cursor; | |
| // const IMG_HDR_FMT: &str = "<IIHxxIIbbhIxxxx"; | |
| #[derive(Debug)] | |
| struct ImageHeader { | |
| magic: u32, | |
| load_addr: u32, | |
| hdr_size: u16, | |
| img_size: u32, | |
| flags: u32, | |
| ver_major: i8, | |
| ver_minor: i8, | |
| ver_revision: i16, | |
| ver_build_num: u32, | |
| } | |
| fn get_img_hdr(cursor: &mut Cursor<Vec<u8>>) -> ImageHeader { | |
| // A byte array containing a big-endian u32 (value 1) and a u8 (value 2) | |
| // let raw_data = vec![0x00, 0x00, 0x00, 0x01, 0x02]; | |
| // let mut cursor = Cursor::new(raw_data); | |
| // let raw_data: Vec<u8> = vec![0x00, 0x00, 0x00, 0x01, 0x02]; | |
| // let mut cursor = Cursor::new(raw_data); | |
| // Unpack the data into a tuple (u32, u8) | |
| let tup = | |
| <(u32, u32, u16, u16, u32, u32, i8, i8, i16, u32, u32)>::unpack_from_le(cursor).unwrap(); | |
| ImageHeader { | |
| magic: tup.0, | |
| load_addr: tup.1, | |
| hdr_size: tup.2, | |
| // padding: tup[3], | |
| img_size: tup.4, | |
| flags: tup.5, | |
| ver_major: tup.6, | |
| ver_minor: tup.7, | |
| ver_revision: tup.8, | |
| ver_build_num: tup.9, | |
| } | |
| } | |
| #[derive(Debug)] | |
| struct TLVInfo { | |
| magic: u16, | |
| tlv_size: u16, | |
| } | |
| fn get_tlv_info(cursor: &mut Cursor<Vec<u8>>) -> TLVInfo { | |
| let tup = <(u16, u16)>::unpack_from_le(cursor).unwrap(); | |
| TLVInfo { | |
| magic: tup.0, | |
| tlv_size: tup.1, | |
| } | |
| } | |
| #[derive(Debug)] | |
| struct TLVHeader { | |
| _type: u8, | |
| len: u16, | |
| } | |
| fn get_tlv_hdr(cursor: &mut Cursor<Vec<u8>>) -> TLVHeader { | |
| let tup = <(u8, u8, u16)>::unpack_from_le(cursor).unwrap(); | |
| TLVHeader { | |
| _type: tup.0, | |
| // pad: tup.1 | |
| len: tup.2, | |
| } | |
| } | |
| const TLV_HDR_FIELDS: [&str; 2] = ["type", "len"]; | |
| const TLV_HDR_FMT: &str = "<bxH"; | |
| const TLV_HDR_SIZE: u32 = 4; | |
| const IMG_HDR_MAGIC: u32 = 0x96F3B83D; | |
| const IMAGE_F_RAM_LOAD: u32 = 0x00000020; | |
| const TLV_INFO_FIELDS: [&str; 2] = ["magic", "tlv_size"]; | |
| const TLV_INFO_FMT: &str = "<HH"; | |
| const TLV_INFO_SIZE: u32 = 4; | |
| const TLV_INFO_MAGIC: u32 = 0x6907; | |
| pub fn get_tlv_hash_from_dfu_bytes(dfu_bytes: &Vec<u8>) -> Option<Vec<u8>> { | |
| let dfu_bytes = dfu_bytes.clone(); | |
| let the_dfu_bytes = dfu_bytes.clone(); | |
| let mut contents = Cursor::new(dfu_bytes); | |
| let img_header = get_img_hdr(&mut contents); | |
| info!("img_header = {img_header:?}"); | |
| // logging.debug(img_header) | |
| // # print("Initial image bytes:") | |
| let start: u32 = img_header.hdr_size.into(); | |
| let end: u32 = start + std::cmp::min(20_u32, img_header.img_size); | |
| // # print("\t" + " ".join("{:02x}".format(b) for b in contents[start:end])) | |
| let tlv_info_offset = img_header.hdr_size as u32 + img_header.img_size; | |
| contents.set_position(tlv_info_offset.into()); | |
| let tlv_info = get_tlv_info(&mut contents); | |
| info!("tlv_info = {tlv_info:?}"); | |
| let tlv_end: usize = tlv_info_offset as usize + tlv_info.tlv_size as usize; | |
| let mut tlv_off: usize = tlv_info_offset as usize + TLV_INFO_SIZE as usize; | |
| let mut tlv_num: usize = 0; | |
| while tlv_off < tlv_end { | |
| contents.set_position(tlv_off as u64); | |
| let tlv_hdr = get_tlv_hdr(&mut contents); | |
| if tlv_hdr._type == 0x12 { | |
| // "IMAGE_TLV_SHA512" in tlv_hdr: | |
| debug!("TLV {}: {:?}", tlv_num, tlv_hdr); | |
| let start: usize = tlv_off as usize + TLV_HDR_SIZE as usize; | |
| let end: usize = start as usize + tlv_hdr.len as usize; | |
| // logging.info( | |
| // " " + " ".join("{:02x}".format(b) for b in contents[start:end]) | |
| // ) | |
| // let hex_str = hex_st | |
| info!("sha512 value = {:?}", &the_dfu_bytes[start..end]); | |
| let hash_string = hex::encode(&the_dfu_bytes[start..end]); | |
| info!("sha512 value = {}", hash_string); | |
| return Some(the_dfu_bytes[start..end].to_vec()); | |
| } | |
| if tlv_hdr.len <= 128 { | |
| let start: usize = tlv_off as usize + TLV_HDR_SIZE as usize; | |
| let end: usize = start + tlv_hdr.len as usize; | |
| // print("\t" + " ".join("{:02x}".format(b) for b in contents[start:end])) | |
| } | |
| // loop book-keeping | |
| tlv_off += TLV_HDR_SIZE as usize + tlv_hdr.len as usize; | |
| tlv_num += 1; | |
| } | |
| None | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment