diff --git a/src/main.rs b/src/main.rs index ae4bc5c..0fcabcc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -91,6 +91,14 @@ fn main() { .open(tbf_path.clone()) .unwrap(); + // Adding padding to the end of cortex-m apps. Check for a cortex-m app + // by inspecting the "machine" value in the elf header. 0x28 is ARM (see + // https://en.wikipedia.org/wiki/Executable_and_Linkable_Format#File_header + // for a list). + // + // RISC-V apps do not need to be sized to power of two. + let add_trailing_padding = elffile.ehdr.machine.0 == 0x28; + // Do the conversion to a tock binary. if opt.verbose { println!("Creating {:?}", tbf_path); @@ -106,6 +114,7 @@ fn main() { opt.protected_region_size, opt.permissions.to_vec(), minimum_tock_kernel_version, + add_trailing_padding, ) .unwrap(); if opt.verbose { @@ -142,6 +151,7 @@ fn elf_to_tbf( protected_region_size_arg: Option, permissions: Vec<(u32, u32)>, kernel_version: Option<(u16, u16)>, + trailing_padding: bool, ) -> io::Result<()> { let package_name = package_name.unwrap_or_default(); @@ -547,16 +557,25 @@ fn elf_to_tbf( // the relocation data length. binary_index += relocation_binary.len() + mem::size_of::(); - // That is everything that we are going to include in our app binary. Now - // we need to pad the binary to a power of 2 in size, and make sure it is - // at least 512 bytes in size. - let post_content_pad = if binary_index.count_ones() > 1 { - let power2len = cmp::max(1 << (32 - (binary_index as u32).leading_zeros()), 512); - power2len - binary_index + // That is everything that we are going to include in our app binary. + + let post_content_pad = if trailing_padding { + // If trailing padding is requested, we need to pad the binary to a + // power of 2 in size, and make sure it is at least 512 bytes in size. + let pad = if binary_index.count_ones() > 1 { + let power2len = cmp::max(1 << (32 - (binary_index as u32).leading_zeros()), 512); + power2len - binary_index + } else { + 0 + }; + // Increment to include the padding. + binary_index += pad; + pad } else { + // No padding. 0 }; - binary_index += post_content_pad; + let total_size = binary_index; // Now set the total size of the app in the header. @@ -574,7 +593,7 @@ fn elf_to_tbf( output.write_all(&rel_data_len)?; output.write_all(relocation_binary.as_ref())?; - // Pad to get a power of 2 sized flash app. + // Pad to get a power of 2 sized flash app, if requested. util::do_pad(output, post_content_pad as usize)?; Ok(())