Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 44 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,33 @@ Arguments:
<elf[,architecture]>... application file(s) to package

Options:
-v, --verbose Be verbose
--deterministic Produce a deterministic TAB file
--disable Mark the app as disabled in the TBF flags
--app-version <APP_VERSION> Set the version number [default: 0]
--minimum-ram-size <min-ram-size> in bytes
-o, --output-file <filename> output file name [default: TockApp.tab]
-n, --package-name <pkg-name> package name
--stack <stack-size> in bytes
--app-heap <heap-size> in bytes [default: 1024]
--kernel-heap <kernel-heap-size> in bytes [default: 1024]
--protected-region-size <protected-region-size> Size of the protected region (including headers)
--permissions <permissions>... A list of driver numbers and allowed commands
--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
--minimum-footer-size <min-footer-size> Minimum number of bytes to reserve space for in the footer [default: 0]
--sha256 Add a SHA256 hash credential to each TBF
--sha384 Add a SHA384 hash credential to each TBF
--sha512 Add a SHA512 hash credential to each TBF
--rsa4096-private <rsa4096-private-key> Add an 4096-bit RSA signature credential using this private key
-h, --help Print help
-V, --version Print version
-v, --verbose Be verbose
--deterministic Produce a deterministic TAB file
--disable Mark the app as disabled in the TBF flags
--app-version <APP_VERSION> Set the version number [default: 0]
--minimum-ram-size <min-ram-size> in bytes - has no effect, kept for backwards compatibility
-o, --output-file <filename> output file name [default: TockApp.tab]
-n, --package-name <pkg-name> package name
--stack <stack-size> in bytes
--app-heap <heap-size> in bytes [default: 1024]
--kernel-heap <kernel-heap-size> in bytes [default: 1024]
--protected-region-size <protected-region-size> Size of the protected region (including headers)
--permissions <permissions>... A list of driver numbers and allowed commands
--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
--minimum-footer-size <min-footer-size> Minimum number of bytes to reserve space for in the footer [default: 0]
--sha256 Add a SHA256 hash credential to each TBF
--sha384 Add a SHA384 hash credential to each TBF
--sha512 Add a SHA512 hash credential to each TBF
--rsa4096-private <rsa4096-private-key> Add an 4096-bit RSA signature credential using this private key
--ecdsa-nist-p256-private <ecdsa-nist-p256-private-key> Add an ECDSA NIST P256 signature credential using this private key
-h, --help Print help
-V, --version Print version
```

For example, converting a "blink" app from a compiled .elf file (for a Cortex-M4
Expand Down Expand Up @@ -167,6 +168,23 @@ increase the size of the protected region to make the start of the TBF header at
an address aligned to 256 bytes when the application binary is at its correct
fixed address.

#### Trailing Padding (Total Size Alignment)

elf2tab adds trailing padding to each TBF to satisfy architecture-specific
alignment constraints:

- **ARM**: total size is padded to a power of 2 (for MPU region alignment).
- **RISC-V**: total size is padded to a multiple of 4 (TBF alignment
requirement).
- **x86**: total size is padded to a multiple of 4096 (page size).

An application can override the default padding by defining a
`tbf_total_size_padding` symbol in its ELF file. When present, the symbol's
value is interpreted as a byte multiple, and the total TBF size will be padded
to a multiple of that value instead of the architecture default. This is useful,
for example, when a platform requires a different alignment than the
architecture-level default.

#### Syscall Permissions

elf2tab allows explicitly specifying the syscalls that an app is allowed to
Expand Down
4 changes: 2 additions & 2 deletions src/cmdline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ pub struct Opt {
#[arg(
long = "minimum-ram-size",
id = "min-ram-size",
help = "in bytes",
help = "in bytes - has no effect, kept for backwards compatibility",
conflicts_with = "stack-size",
conflicts_with = "heap-size",
conflicts_with = "kernel-heap-size"
)]
pub minimum_stack_size: Option<u32>,
pub _minimum_stack_size: Option<u32>,

#[arg(
long = "output-file",
Expand Down
48 changes: 31 additions & 17 deletions src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,10 @@ pub fn elf_to_tbf(
// - RISC_V: make sure the entire TBF is a multiple of 4 to meet TBF
// alignment requirements.
// - x86: use 4k padding to match page size.
let trailing_padding = match elf_file.ehdr.e_machine {
//
// This default may be overridden by a `tbf_total_size_padding` symbol
// in the ELF file.
let arch_trailing_padding = match elf_file.ehdr.e_machine {
elf::abi::EM_ARM => Some(TrailingPadding::TotalSizePowerOfTwo),
elf::abi::EM_RISCV => Some(TrailingPadding::TotalSizeMultiple(4)),
elf::abi::EM_386 => Some(TrailingPadding::TotalSizeMultiple(4096)),
Expand Down Expand Up @@ -470,26 +473,37 @@ pub fn elf_to_tbf(
// Adjust the protected region size to make fixed address work
////////////////////////////////////////////////////////////////////////////

// Applications can hint a desired protected region size to elf2tab by
// defining a special `tbf_protected_region_size` symbol:
let protected_region_size_symbol =
// Applications can hint configuration to elf2tab by defining special
// symbols:
//
// - `tbf_protected_region_size`: desired protected region size.
// - `tbf_total_size_padding`: pad total size to a multiple of this value,
// overriding the architecture default.
let (protected_region_size_symbol, total_size_padding_symbol) =
if let Ok(Some((symtab, sym_strtab))) = elf_file.symbol_table() {
// We are looking for the `tbf_protected_region_size` symbol and its
// value. If it exists, we can use it as a hint for the protected
// region size.
symtab
.iter()
.find(|sym| {
let name = sym_strtab
.get(sym.st_name as usize)
.expect("Failed to parse symbol name");
name == "tbf_protected_region_size"
})
.map(|tbf_header_sym| tbf_header_sym.st_value as u32)
let mut prs = None;
let mut tsp = None;
for sym in symtab.iter() {
let name = sym_strtab
.get(sym.st_name as usize)
.expect("Failed to parse symbol name");
if name == "tbf_protected_region_size" {
prs = Some(sym.st_value as u32);
} else if name == "tbf_total_size_padding" {
tsp = Some(sym.st_value as usize);
}
}
(prs, tsp)
} else {
None
(None, None)
};

// Override trailing padding if specified in the ELF via the
// `tbf_total_size_padding` symbol.
let trailing_padding = total_size_padding_symbol
.map(TrailingPadding::TotalSizeMultiple)
.or(arch_trailing_padding);

// Determine the protected region size by checking the following sources in
// this order:
//
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ fn main() {
)
.unwrap();
if opt.verbose {
println!("");
println!();
}

match outfile.write_all(output_vector.as_ref()) {
Expand Down