diff --git a/tools/Cargo.toml b/tools/Cargo.toml index 31ec6d331..8600cf2b3 100644 --- a/tools/Cargo.toml +++ b/tools/Cargo.toml @@ -42,3 +42,10 @@ name = "read-tags" [[bin]] name = "sign-image" + +[features] +precursor = [] +renode = [] +cramium-soc = [] +atsama5d2 = [] +default = [] diff --git a/tools/src/bin/create-image.rs b/tools/src/bin/create-image.rs index ce6db2f6f..69ca301f8 100644 --- a/tools/src/bin/create-image.rs +++ b/tools/src/bin/create-image.rs @@ -11,6 +11,7 @@ use tools::elf::{read_minielf, read_program}; use tools::tags::bflg::Bflg; use tools::tags::inie::IniE; use tools::tags::inif::IniF; +use tools::tags::inis::IniS; use tools::tags::memory::{MemoryRegion, MemoryRegions}; use tools::tags::pnam::ProcessNames; use tools::tags::xkrn::XousKernel; @@ -25,6 +26,13 @@ struct RamConfig { memory_required: u32, } +struct SwapConfig { + offset: u32, + size: u32, + regions: MemoryRegions, + memory_required: u32, +} + fn csr_to_config(hv: tools::utils::CsrConfig, ram_config: &mut RamConfig) { let mut found_ram_name = None; fn round_mem(src: u32) -> u32 { (src + 4095) & !4095 } @@ -111,6 +119,14 @@ fn main() { .number_of_values(1) .help("Initial program to load from FLASH"), ) + .arg( + Arg::with_name("inis") + .long("inis") + .takes_value(true) + .multiple(true) + .number_of_values(1) + .help("Program to be loaded into swap space"), + ) .arg( Arg::with_name("csv") .short("c") @@ -149,6 +165,13 @@ fn main() { .help("RAM offset and size, in the form of [offset]:[size]") .required_unless_one(&["ram", "svd", "csv"]), ) + .arg( + Arg::with_name("swap") + .long("swap") + .takes_value(true) + .value_name("OFFSET:SIZE") + .help("Swap offset and size, in the form of [offset]:[size]; note: offset and size have platform-dependent interpretations") + ) .arg( Arg::with_name("debug") .short("d") @@ -172,6 +195,13 @@ fn main() { memory_required: 0, }; + let mut swap_config = SwapConfig { + offset: Default::default(), + size: Default::default(), + regions: MemoryRegions::new(), + memory_required: 0, + }; + let mut process_names = ProcessNames::new(); if let Some(val) = matches.value_of("ram") { @@ -285,10 +315,71 @@ fn main() { csr_to_config(tools::utils::CsrConfig { regions: map }, &mut ram_config); } + // Swap has an architecture-dependent meaning. + // + // On Precursor, it's only used exclusively for testing, so we apply it as a + // "patch" on top of the RAM area -- it's a specifier for what part + // of RAM should be carved out to use as swap. + // + // On Cramium, the swap region may point to a section of memory-mapped RAM, *or* + // it can point to register-mapped SPI RAM. The distinction is based solely upon + // the starting address. If the starting address is `0`, we assume this is talking + // about register-mapped SPI RAM. If it is non-zero, then we assume this is referring + // to a window that is memory-mapped. + if let Some(val) = matches.value_of("swap") { + let swap_parts: Vec<&str> = val.split(':').collect(); + if swap_parts.len() != 2 { + eprintln!("Error: --swap argument should be of the form [offset]:[size]"); + return; + } + + swap_config.offset = match parse_u32(swap_parts[0]) { + Ok(o) => o, + Err(e) => { + eprintln!("Error: Unable to parse {}: {:?}", swap_parts[0], e); + return; + } + }; + + swap_config.size = match parse_u32(swap_parts[1]) { + Ok(o) => o, + Err(e) => { + eprintln!("Error: Unable to parse {}: {:?}", swap_parts[1], e); + return; + } + }; + + swap_config.memory_required += swap_config.size / 4096; + } + let mut args = XousArguments::new(ram_config.offset, ram_config.size, ram_config.name); if !ram_config.regions.is_empty() { - args.add(ram_config.regions); + if !swap_config.regions.is_empty() { + // now process the swap region. + #[cfg(any(feature = "precursor", feature = "renode"))] + { + // this is slightly janky, but we only use this configuration for testing swap + // so we can impose an artificial rule like "swap must be higher than RAM", instead + // of trying to handle the generic cases like "swap could be anywhere, maybe even + // a window inside RAM, or lower, or multiple fragments, or...". + assert!( + swap_config.offset > ram_config.offset, + "swap is assumed to be at a higher address than RAM" + ); + // split the RAM space, if necessary, to accommodate swap + if swap_config.offset < ram_config.offset + ram_config.size { + ram_config.size = swap_config.offset - ram_config.offset; + } + } + // Note that other configurations don't split RAM, since the swap is provisioned directly + // in hardware, and thus, no post-processing is required. + + args.add(swap_config.regions); + args.add(ram_config.regions); + } else { + args.add(ram_config.regions); + } } if matches.is_present("debug") { @@ -334,6 +425,23 @@ fn main() { } } + if let Some(init_paths) = matches.values_of("inis") { + for init_path in init_paths { + let program_name = std::path::Path::new(init_path); + process_names.set( + pid, + program_name + .file_stem() + .expect("program had no name") + .to_str() + .expect("program name is not valid utf-8"), + ); + pid += 1; + let init = read_minielf(init_path).expect("couldn't parse init file"); + args.add(IniS::new(init.entry_point, init.sections, init.program, init.alignment_offset)); + } + } + let xkrn = XousKernel::new( kernel.text_offset, kernel.text_size, diff --git a/tools/src/tags/inis.rs b/tools/src/tags/inis.rs new file mode 100644 index 000000000..52eb0a8a7 --- /dev/null +++ b/tools/src/tags/inis.rs @@ -0,0 +1,85 @@ +use std::fmt; +use std::io; + +use crate::elf::MiniElfSection; +use crate::xous_arguments::{XousArgument, XousArgumentCode, XousSize}; + +#[derive(Debug)] +pub struct IniS { + /// Address in flash + load_offset: u32, + + /// Virtual address entry point + entrypoint: u32, + + /// Array of minielf sections + sections: Vec, + + /// Actual program data + data: Vec, + + /// Alignment offset + alignment_offset: usize, +} + +impl fmt::Display for IniS { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + writeln!( + f, + " IniS: entrypoint @ {:08x}, loaded from {:08x}. Sections:", + self.entrypoint, self.load_offset + )?; + let mut load_offset = self.load_offset; + for section in &self.sections { + writeln!(f, " Loaded from {:08x} - {}", load_offset, section)?; + load_offset += section.size as u32; + } + Ok(()) + } +} + +impl IniS { + pub fn new( + entrypoint: u32, + sections: Vec, + mut data: Vec, + alignment_offset: usize, + ) -> IniS { + // pad the data to 4 bytes + while data.len() & 3 != 0 { + data.push(0); + } + IniS { load_offset: 0, entrypoint, sections, data, alignment_offset } + } +} + +impl XousArgument for IniS { + fn code(&self) -> XousArgumentCode { u32::from_le_bytes(*b"IniS") } + + fn length(&self) -> XousSize { 4 + 4 + (self.sections.len() * 8) as XousSize } + + fn finalize(&mut self, offset: usize) -> usize { + self.load_offset = offset as u32; + + assert!(offset % crate::tags::PAGE_SIZE == self.alignment_offset, "IniS load offset is not aligned"); + self.data = crate::tags::align_data_up(&self.data, 0); + self.data.len() + } + + fn last_data(&self) -> &[u8] { &self.data } + + fn alignment_offset(&self) -> usize { self.alignment_offset } + + fn serialize(&self, output: &mut dyn io::Write) -> io::Result { + let mut written = 0; + written += output.write(&self.load_offset.to_le_bytes())?; + written += output.write(&self.entrypoint.to_le_bytes())?; + for section in &self.sections { + written += output.write(§ion.virt.to_le_bytes())?; + let mut word2 = section.size.to_le_bytes(); + word2[3] = section.flags.bits(); + written += output.write(&word2)?; + } + Ok(written) + } +} diff --git a/tools/src/tags/mod.rs b/tools/src/tags/mod.rs index ceae9da67..3f3f3abf9 100644 --- a/tools/src/tags/mod.rs +++ b/tools/src/tags/mod.rs @@ -1,6 +1,7 @@ pub mod bflg; pub mod inie; pub mod inif; +pub mod inis; pub mod memory; pub mod pnam; pub mod xkrn;