Skip to content

Commit

Permalink
add ShortId TBF header
Browse files Browse the repository at this point in the history
  • Loading branch information
bradjc committed Jun 20, 2024
1 parent 48de07c commit 9dfc4f5
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Options:
--write_id <write_id> A storage ID used for writing data
--read_ids <read_ids>... Storage IDs that this app is allowed to read
--access_ids <access_ids>... Storage IDs that this app is allowed to write
--short-id <short-id> ShortId to request in the app's header
--kernel-major <kernel-major-version> The kernel version that the app requires
--kernel-minor <kernel-minor-version> The minimum kernel minor version that the app requires
--supported-boards <supported-boards> comma separated list of boards this app is compatible with
Expand Down
8 changes: 8 additions & 0 deletions src/cmdline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,14 @@ pub struct Opt {
)]
pub access_ids: Option<Vec<u32>>,

#[arg(
long = "short-id",
id = "short-id",
help = "ShortId to request in the app's header",
value_parser=clap_num::maybe_hex::<u32>,
)]
pub short_id: Option<u32>,

#[arg(
long = "kernel-major",
id = "kernel-major-version",
Expand Down
2 changes: 2 additions & 0 deletions src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ pub fn elf_to_tbf(
permissions: Vec<(u32, u32)>,
storage_ids: (Option<u32>, Option<Vec<u32>>, Option<Vec<u32>>),
kernel_version: Option<(u16, u16)>,
short_id: Option<u32>,
disabled: bool,
minimum_footer_size: u32,
app_version: u32,
Expand Down Expand Up @@ -460,6 +461,7 @@ pub fn elf_to_tbf(
permissions,
storage_ids,
kernel_version,
short_id,
disabled,
);

Expand Down
47 changes: 46 additions & 1 deletion src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ pub enum TbfHeaderTypes {
Permissions = 6,
Persistent = 7,
KernelVersion = 8,

Program = 9,
ShortId = 10,

Credentials = 128,
}
Expand Down Expand Up @@ -124,6 +124,13 @@ struct TbfHeaderKernelVersion {
minor: u16,
}

#[repr(C)]
#[derive(Clone, Copy, Debug)]
struct TbfHeaderShortId {
base: TbfHeaderTlv,
short_id: u32,
}

#[repr(C)]
#[derive(Debug)]
pub struct TbfFooterCredentials {
Expand Down Expand Up @@ -275,6 +282,18 @@ impl fmt::Display for TbfHeaderKernelVersion {
}
}

impl fmt::Display for TbfHeaderShortId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// ^x.y means >= x.y, < (x+1).0
writeln!(
f,
"
ShortId: {0:>#10X}",
self.short_id
)
}
}

const FLAGS_ENABLE: u32 = 0x0000_0001;

pub struct TbfHeader {
Expand All @@ -287,6 +306,7 @@ pub struct TbfHeader {
hdr_permissions: Option<TbfHeaderPermissions>,
hdr_persistent: Option<TbfHeaderPersistentAcl>,
hdr_kernel_version: Option<TbfHeaderKernelVersion>,
hdr_short_id: Option<TbfHeaderShortId>,
package_name: String,
package_name_pad: usize,
}
Expand Down Expand Up @@ -318,6 +338,7 @@ impl TbfHeader {
hdr_permissions: None,
hdr_persistent: None,
hdr_kernel_version: None,
hdr_short_id: None,
package_name: String::new(),
package_name_pad: 0,
}
Expand All @@ -340,6 +361,7 @@ impl TbfHeader {
permissions: Vec<(u32, u32)>,
storage_ids: (Option<u32>, Option<Vec<u32>>, Option<Vec<u32>>),
kernel_version: Option<(u16, u16)>,
short_id: Option<u32>,
disabled: bool,
) -> usize {
// Need to calculate lengths ahead of time. Need the base and the
Expand Down Expand Up @@ -435,6 +457,11 @@ impl TbfHeader {
header_length += mem::size_of::<TbfHeaderKernelVersion>();
}

// Check if we have to include a kernel version header.
if short_id.is_some() {
header_length += mem::size_of::<TbfHeaderShortId>();
}

let mut flags = 0x0000_0000;

if !disabled {
Expand Down Expand Up @@ -534,6 +561,17 @@ impl TbfHeader {
});
}

// If short_id is set, we have to include the header.
if let Some(short_id_num) = short_id {
self.hdr_short_id = Some(TbfHeaderShortId {
base: TbfHeaderTlv {
tipe: TbfHeaderTypes::ShortId,
length: 4,
},
short_id: short_id_num,
});
}

// Return the length by generating the header and seeing how long it is.
self.generate()
.expect("No header was generated")
Expand Down Expand Up @@ -678,6 +716,11 @@ impl TbfHeader {
header_buf.write_all(unsafe { util::as_byte_slice(&self.hdr_kernel_version) })?;
}

// If the short id is set, include that TLV
if self.hdr_short_id.is_some() {
header_buf.write_all(unsafe { util::as_byte_slice(&self.hdr_short_id) })?;
}

let current_length = header_buf.get_ref().len();
util::do_pad(
&mut header_buf,
Expand Down Expand Up @@ -746,6 +789,8 @@ impl fmt::Display for TbfHeader {
.map_or(Ok(()), |hdr| write!(f, "{}", hdr))?;
self.hdr_kernel_version
.map_or(Ok(()), |hdr| write!(f, "{}", hdr))?;
self.hdr_short_id
.map_or(Ok(()), |hdr| write!(f, "{}", hdr))?;
Ok(())
}
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ fn main() {
opt.permissions.to_vec(),
(opt.write_id, opt.read_ids.clone(), opt.access_ids.clone()),
minimum_tock_kernel_version,
opt.short_id,
opt.disabled,
opt.minimum_footer_size,
opt.app_version,
Expand Down

0 comments on commit 9dfc4f5

Please sign in to comment.