Skip to content

Commit

Permalink
Made footer parser work with non-static-lifetime byte array
Browse files Browse the repository at this point in the history
  • Loading branch information
george-cosma committed Aug 7, 2023
1 parent d096ba8 commit e989e86
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 8 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ ci-runner-github: ci-job-format ci-job-clippy
@cargo check
@echo "Running tests..."
@cargo test
@cargo test --workspace
2 changes: 1 addition & 1 deletion tbf-parser/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ pub fn parse_tbf_header(
}

pub fn parse_tbf_footer(
footers: &'static [u8],
footers: &[u8],
) -> Result<(types::TbfFooterV2Credentials, u32), types::TbfParseError> {
let mut remaining = footers;
let tlv_header: types::TbfTlv = remaining.try_into()?;
Expand Down
12 changes: 6 additions & 6 deletions tbf-parser/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,17 +256,17 @@ pub enum TbfFooterV2CredentialsType {
}

#[derive(Clone, Copy, Debug)]
pub struct TbfFooterV2Credentials {
pub struct TbfFooterV2Credentials<'a> {
format: TbfFooterV2CredentialsType,
data: &'static [u8],
data: &'a [u8],
}

impl TbfFooterV2Credentials {
impl TbfFooterV2Credentials<'_> {
pub fn format(&self) -> TbfFooterV2CredentialsType {
self.format
}

pub fn data(&self) -> &'static [u8] {
pub fn data(&self) -> &[u8] {
self.data
}
}
Expand Down Expand Up @@ -604,10 +604,10 @@ impl core::convert::TryFrom<&[u8]> for TbfHeaderV2KernelVersion {
}
}

impl core::convert::TryFrom<&'static [u8]> for TbfFooterV2Credentials {
impl<'b, 'a: 'b> core::convert::TryFrom<&'a [u8]> for TbfFooterV2Credentials<'b> {
type Error = TbfParseError;

fn try_from(b: &'static [u8]) -> Result<TbfFooterV2Credentials, Self::Error> {
fn try_from(b: &'a [u8]) -> Result<TbfFooterV2Credentials<'b>, Self::Error> {
let format = u32::from_le_bytes(
b.get(0..4)
.ok_or(TbfParseError::InternalError)?
Expand Down
Binary file added tbf-parser/tests/flashes/footerSHA256.dat
Binary file not shown.
34 changes: 33 additions & 1 deletion tbf-parser/tests/parse.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use tbf_parser::parse::*;

#[test]
fn check_sum() {
fn simple_tbf() {
let buffer: Vec<u8> = include_bytes!("./flashes/simple.dat").to_vec();

let (ver, header_len, whole_len) = parse_tbf_header_lengths(&buffer[0..8].try_into().unwrap())
Expand All @@ -20,3 +20,35 @@ fn check_sum() {
assert_eq!(header.get_package_name().unwrap(), "_heart");
assert_eq!(header.get_kernel_version().unwrap(), (2, 0));
}

#[test]
fn footer_sha256() {
let buffer: Vec<u8> = include_bytes!("./flashes/footerSHA256.dat").to_vec();

let (ver, header_len, whole_len) = parse_tbf_header_lengths(&buffer[0..8].try_into().unwrap())
.ok()
.unwrap();
assert_eq!(ver, 2);
assert_eq!(header_len, 76);
assert_eq!(whole_len, 8192);

let header = parse_tbf_header(&buffer[0..header_len as usize], 2).unwrap();
dbg!(&header);
assert_eq!(header.enabled(), true);
assert_eq!(header.get_minimum_app_ram_size(), 4848);
assert_eq!(header.get_init_function_offset(), 41 + header_len as u32);
assert_eq!(header.get_protected_size(), header_len as u32);
assert_eq!(header.get_package_name().unwrap(), "_heart");
assert_eq!(header.get_kernel_version().unwrap(), (2, 0));
let binary_offset = header.get_binary_end() as usize;
assert_eq!(binary_offset, 5836);

let (footer, footer_size) = parse_tbf_footer(&buffer[binary_offset..]).unwrap();
dbg!(footer);
assert_eq!(footer_size, 36);
let correct_sha256 = [
214u8, 17, 81, 32, 51, 178, 249, 35, 161, 33, 109, 184, 195, 46, 238, 158, 141, 54, 63, 94,
60, 245, 50, 228, 239, 107, 231, 127, 220, 158, 77, 160,
];
assert_eq!(footer.data(), correct_sha256);
}

0 comments on commit e989e86

Please sign in to comment.