diff options
| -rw-r--r-- | CHANGELOG.md | 3 | ||||
| -rw-r--r-- | board/Cargo.toml | 1 | ||||
| -rw-r--r-- | board/build.rs | 8 | ||||
| -rw-r--r-- | src/host.rs | 26 | ||||
| -rw-r--r-- | tests/inspect_elf.rs | 167 |
5 files changed, 194 insertions, 11 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ae56f7..b4d6752 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,9 @@ the device linker script that's included in the runtime. By default, the name remains `device.x`. The name only matters if the "device" crate feature is enabled. +Use `RuntimeBuilder::boot_header` to force inclusion / exclusion of the NXP +boot ROM header in your image. + ### FlexRAM bank layouts The runtime builder lets users specify the _layout_, or assignment, of FlexRAM diff --git a/board/Cargo.toml b/board/Cargo.toml index bfebd0d..f9ef9aa 100644 --- a/board/Cargo.toml +++ b/board/Cargo.toml @@ -30,6 +30,7 @@ panic-rtt-target = { version = "0.1", optional = true, features = ["cortex-m"] } [features] nonboot = [] +secondary = [] rtic = [] # Begin board features. teensy4 = [ diff --git a/board/build.rs b/board/build.rs index e44a11d..d86d287 100644 --- a/board/build.rs +++ b/board/build.rs @@ -10,8 +10,10 @@ fn extract_features() -> HashSet<String> { /// Creates a runtime for a particular family, adjusting whether the image is expected to be boot /// based on provided features. fn create_runtime(family: imxrt_rt::Family, flash_size: usize) -> imxrt_rt::RuntimeBuilder { - if cfg!(feature = "nonboot") { - imxrt_rt::RuntimeBuilder::in_flash(family, flash_size, 16 * 1024) + if cfg!(feature = "nonboot") | cfg!(feature = "secondary") { + let mut bldr = imxrt_rt::RuntimeBuilder::in_flash(family, flash_size, 256 * 1024); + bldr.boot_header(cfg!(feature = "secondary")); + bldr } else { imxrt_rt::RuntimeBuilder::from_flexspi(family, flash_size) } @@ -54,7 +56,7 @@ fn main() { .heap_size_env_override("BOARD_HEAP") .build() .unwrap(), - "imxrt1170evk_cm7" => create_runtime(imxrt_rt::Family::Imxrt1170, 8 * 1024 * 1024) + "imxrt1170evk_cm7" => create_runtime(imxrt_rt::Family::Imxrt1170, 256 * 1024) .rodata(imxrt_rt::Memory::Dtcm) .stack_size_env_override("BOARD_STACK") .heap_size_env_override("BOARD_HEAP") diff --git a/src/host.rs b/src/host.rs index a4f572e..66e953b 100644 --- a/src/host.rs +++ b/src/host.rs @@ -129,6 +129,7 @@ struct FlashOpts { size: usize, offset: u32, flexspi: FlexSpi, + boot_header: bool, } impl FlashOpts { @@ -139,12 +140,6 @@ impl FlashOpts { .start_address(family) .map(|start_address| start_address + self.offset) } - - /// A bootable image (with the boot header) isn't offset - /// in flash. - fn is_boot_image(&self) -> bool { - self.offset == 0 - } } #[derive(Debug, Clone, PartialEq, Eq)] @@ -376,6 +371,7 @@ impl RuntimeBuilder { flash_opts: Some(FlashOpts { size: flash_size, offset: 0, + boot_header: true, flexspi: FlexSpi::family_default(family), }), linker_script_name: DEFAULT_LINKER_SCRIPT_NAME.into(), @@ -413,6 +409,7 @@ impl RuntimeBuilder { flash_opts: Some(FlashOpts { size: partition_size, offset: partition_offset, + boot_header: false, flexspi: FlexSpi::family_default(family), }), linker_script_name: DEFAULT_LINKER_SCRIPT_NAME.into(), @@ -541,6 +538,21 @@ impl RuntimeBuilder { self } + /// Override the boot header configuration. + /// + /// By default, a runtime constructed using [`from_flexspi`](Self::from_flexspi) includes a boot header + /// for compatibility with NXP's boot ROM. However, a runtime constructed using [`in_flash`](Self::in_flash) + /// lacks this boot header; you're expected to bring your own bootloader. + /// + /// This call lets you change that default behavior. If this builder is not configuring a flash-loaded + /// runtime, this is silently ignored. + pub fn boot_header(&mut self, boot_header: bool) -> &mut Self { + if let Some(flash_opts) = &mut self.flash_opts { + flash_opts.boot_header = boot_header; + } + self + } + /// Set the name of the linker script file. /// /// You can use this to customize the linker script name for your users. @@ -626,7 +638,7 @@ impl RuntimeBuilder { if let Some(flash_opts) = &self.flash_opts { write_flash_memory_map(writer, self.family, flash_opts, &self.flexram_layout)?; - if flash_opts.is_boot_image() { + if flash_opts.boot_header { let boot_header_x = match self.family { Family::Imxrt1010 | Family::Imxrt1015 diff --git a/tests/inspect_elf.rs b/tests/inspect_elf.rs index 50bc09a..928ddb3 100644 --- a/tests/inspect_elf.rs +++ b/tests/inspect_elf.rs @@ -60,6 +60,32 @@ fn cargo_build_nonboot(board: &str) -> Result<PathBuf> { Ok(path) } +fn cargo_build_secondary(board: &str) -> Result<PathBuf> { + let status = Command::new("cargo") + .arg("build") + .arg("--example=blink-rtic") + .arg(format!( + "--features=board/{board},board/rtic,board/secondary" + )) + .arg("--target=thumbv7em-none-eabihf") + .arg(format!("--target-dir=target/{board}-secondary")) + .arg("--quiet") + .spawn()? + .wait()?; + + // TODO(summivox): `ExitStatus::exit_ok()` stabilization (can be chained after the `.wait()?) + if !status.success() { + return Err( + format!("Building board '{board}' failed: process returned {status:?}",).into(), + ); + } + + let path = PathBuf::from(format!( + "target/{board}-secondary/thumbv7em-none-eabihf/debug/examples/blink-rtic" + )); + Ok(path) +} + /// Build an example, returning a path to the ELF. fn cargo_build(board: &str) -> Result<PathBuf> { cargo_build_with_envs(board, &[]) @@ -781,7 +807,7 @@ fn imxrt1170evk_cm7() { #[test] #[ignore = "building an example can take time"] fn imxrt1170evk_cm7_nonboot() { - const IMAGE_OFFSET: u64 = 16 * 1024; + const IMAGE_OFFSET: u64 = 256 * 1024; let path = cargo_build_nonboot("imxrt1170evk-cm7").expect("Unable to build example"); let contents = fs::read(path).expect("Could not read ELF file"); let elf = Elf::parse(&contents).expect("Could not parse ELF"); @@ -906,3 +932,142 @@ fn imxrt1170evk_cm7_nonboot() { assert_eq!(binary.section_lma(".heap"), heap.address, "Heap is NOLOAD"); assert!(binary.symbol("Reset").is_some()); } + +#[test] +#[ignore = "building an example can take time"] +fn imxrt1170evk_cm7_secondary() { + const IMAGE_OFFSET: u64 = 256 * 1024; + let path = cargo_build_secondary("imxrt1170evk-cm7").expect("Unable to build example"); + let contents = fs::read(path).expect("Could not read ELF file"); + let elf = Elf::parse(&contents).expect("Could not parse ELF"); + + let binary = ImxrtBinary::new(&elf, &contents); + assert_eq!( + binary.symbol_value("__dcd_start"), + binary.symbol_value("__dcd_end") + ); + assert_eq!(binary.symbol_value("__dcd"), Some(0)); + assert_eq!( + Fcb { + address: 0x3000_0400 + IMAGE_OFFSET, + size: 512 + }, + binary.fcb().unwrap() + ); + assert_eq!(binary.flexram_config().unwrap(), 0xFFAAFFAA); + + let ivt = binary.ivt().unwrap(); + assert_eq!(ivt.magic_header, 0x402000D1); + assert_eq!( + ivt.interrupt_vector_table, + 0x3000_2000 + IMAGE_OFFSET as u32 + ); + assert_eq!(ivt.device_configuration_data, 0); + assert_eq!( + ivt.boot_data as u64, + binary.symbol_value("__ivt").unwrap() + 32 + ); + assert!( + binary.section(".boot").is_ok(), + "Boot section missing in secondary non boot image" + ); + + let stack = binary.section(".stack").unwrap(); + assert_eq!( + Section { + address: DTCM, + size: 8 * 1024 + }, + stack, + "stack not at ORIGIN(DTCM), or not 8 KiB large" + ); + assert_eq!(binary.section_lma(".stack"), stack.address); + + let vector_table = binary.section(".vector_table").unwrap(); + assert_eq!( + Section { + address: stack.address + stack.size, + size: 16 * 4 + IMXRT1170_INTERRUPTS * 4 + }, + vector_table, + "vector table not at expected VMA behind the stack" + ); + assert!( + vector_table.address.is_multiple_of(1024), + "vector table is not 1024-byte aligned" + ); + assert_eq!( + binary.section_lma(".vector_table"), + 0x3000_2000 + IMAGE_OFFSET + ); + + let xip = binary.section(".xip").unwrap(); + let text = binary.section(".text").unwrap(); + assert_eq!(text.address, ITCM, "text"); + assert_eq!( + binary.section_lma(".text"), + aligned(0x3000_2000 + IMAGE_OFFSET + vector_table.size + xip.size, 4), + "text VMA expected behind vector table" + ); + + let rodata = binary.section(".rodata").unwrap(); + assert_eq!( + rodata.address, + vector_table.address + vector_table.size, + "rodata moved to DTCM behind vector table" + ); + assert!( + binary.section_lma(".rodata") + >= 0x3000_2000 + IMAGE_OFFSET + vector_table.size + aligned(text.size, 4), + ); + + let data = binary.section(".data").unwrap(); + assert_eq!(data.address, 0x2024_0000, "data VMA in OCRAM"); + assert_eq!( + data.size, 4, + "blink-rtic expected to have a single static mut u32" + ); + assert_eq!( + binary.section_lma(".data"), + binary.section_lma(".rodata") + aligned(rodata.size, 4), + "data LMA starts behind rodata" + ); + + let bss = binary.section(".bss").unwrap(); + assert_eq!( + bss.address, + data.address + aligned(data.size, 4), + "bss in OCRAM behind data" + ); + assert_eq!(binary.section_lma(".bss"), bss.address, "bss is NOLOAD"); + + let uninit = binary.section(".uninit").unwrap(); + assert_eq!( + uninit.address, + bss.address + aligned(bss.size, 4), + "uninit in OCRAM behind bss" + ); + assert_eq!( + binary.section_lma(".uninit"), + uninit.address, + "uninit is NOLOAD" + ); + + let heap = binary.section(".heap").unwrap(); + assert_eq!( + Section { + address: rodata.address + aligned(rodata.size, 4), + size: 0, + }, + heap, + "0 byte heap in DTCM behind rodata table" + ); + assert_eq!(binary.section_lma(".heap"), heap.address, "Heap is NOLOAD"); + + let increment_data_xip = binary.symbol_value("increment_data").unwrap(); + assert!( + 0x3000_0000 < increment_data_xip && increment_data_xip < 0x4000_0000, + "increment_data is not XiP" + ); + assert!(binary.symbol("Reset").is_some()); +} |
