From 08349c14527289bd2d15e4e691a85082097504f6 Mon Sep 17 00:00:00 2001 From: steigr Date: Thu, 19 Nov 2026 23:47:59 +0100 Subject: [PATCH] mkvh: split large elf segments so the octane prom loads linux from disk The Octane's PROM reads each ELF segment of a volume-header file with one SCSI command. Its ISP1040 driver lists the transfer one entry per 16 KB page in a fixed array of its per-adapter state (about 73 entries) and queues it on an 8-entry request ring, so a segment over about 720 KB never fits the ring and times out, and one over about 1.1 MB also writes its page list over the next adapter's state and panics the PROM. An 11 MB Linux vmlinux did the latter. IRIX's own volume-header files stay small. mkvh build now splits every PT_LOAD of an ELF executable that carries more than 512 KB of file data into consecutive 512 KB segments at the same offsets and addresses, rewriting the program header table in place when the bytes after it are free. --no-split keeps files unchanged. Linux 7.3.4 on the Octane now boots from dksc(0,1,8)vmlinux to a shell. Tests: sgi_vh split_load_segments_* (ELF32 and ELF64, in place and appended table), build_image_splits_an_oversized_kernel_only_when_asked. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 7 + clanker/INDEX.md | 4 +- clanker/devices/ip30-linux-7.3-guest.md | 5 +- ...load-of-a-large-elf-overruns-a-dma-list.md | 67 ++-- docs/cpu-test-support.md | 6 + docs/octane-ip30.md | 11 + src/bin/mkvh.rs | 21 +- src/sgi_vh.rs | 315 +++++++++++++++++- 8 files changed, 402 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b2c1060f..fe6fd2888 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,13 @@ hashes are given where a change is easiest to understand by reading the commit. ## November 2026 +### Volume headers + +- `mkvh build` splits an ELF executable's segments larger than 512 KB, so the + Octane's PROM can load a Linux kernel from the disk's volume header; an + 11 MB `vmlinux` made it time out or panic. `--no-split` keeps files as + they are. + ### virtio - virtio-serial and virtio-9p on PCI (Octane, Tezro) offer diff --git a/clanker/INDEX.md b/clanker/INDEX.md index cb6e76d06..ca30fd375 100644 --- a/clanker/INDEX.md +++ b/clanker/INDEX.md @@ -149,8 +149,8 @@ Non-graphics devices: MC/MEMCFG and RAM banks, HPC3, VDMA, EEPROMs and NVRAM, PS | [ip30-module-name-must-match-active-cpus.md](devices/ip30-module-name-must-match-active-cpus.md) | rule | Octane: the module record must say PM10 for one CPU and PM20 for two, or the PROM fails its NIC diagnostic | | [ip30-low-memory-aliases-sdram.md](devices/ip30-low-memory-aliases-sdram.md) | rule | Octane: the 16 KiB at physical 0 is the first 16 KiB of SDRAM, which the processor reaches only there (0x2000_0000..+16 KiB bus-errors, as the stock IDE memory diagnostics expect); shares its JIT page generations; the guard holds on the table path only (the ppmem window maps whole units); DMA reach unknown | | [ip30-prom-stores-into-flash-stay-in-cache.md](devices/ip30-prom-stores-into-flash-stay-in-cache.md) | rule | Octane: the PROM's cached stores into its own flash never reach the Bridge; `CachedFlashLines` holds them (approximation: no write-back) | -| [ip30-prom-disk-load-of-a-large-elf-overruns-a-dma-list.md](devices/ip30-prom-disk-load-of-a-large-elf-overruns-a-dma-list.md) | open | Octane: the PROM loading an 11 MB ELF (Linux vmlinux) from the volume header writes its 16 KB-page DMA list over its adapter list and panics; TFTP boot works | -| [ip30-linux-7.3-guest.md](devices/ip30-linux-7.3-guest.md) | howto | Octane: mainline Linux 7.3 config, the PROM/Linux PCI window mismatch and the root-bus scan past device 7 (kernel patch), TFTP boot, no power-off | +| [ip30-prom-disk-load-of-a-large-elf-overruns-a-dma-list.md](devices/ip30-prom-disk-load-of-a-large-elf-overruns-a-dma-list.md) | rule | Octane PROM 4.17 reads a volume-header ELF segment in one SCSI command; over ~720 KB it times out (8-entry ring), over ~1.1 MB its page list overruns the adapter array and it panics. `mkvh` splits segments to 512 KB | +| [ip30-linux-7.3-guest.md](devices/ip30-linux-7.3-guest.md) | howto | Octane: mainline Linux 7.3 config, the PROM/Linux PCI window mismatch and the root-bus scan past device 7 (kernel patch), TFTP or volume-header boot, no power-off | | [ip30-device-timing-on-hptimer.md](devices/ip30-device-timing-on-hptimer.md) | design | Octane: IOC3, RAD1 and ISP1040 pace themselves on `hptimer` one-shots, with threads only where something blocks (the ISP1040 ring, serial receive, NVRAM writer) | | [ip30-openbsd-needs-64gb-window-and-slot-4-empty.md](devices/ip30-openbsd-needs-64gb-window-and-slot-4-empty.md) | rule | Octane: OpenBSD 6.9 reaches PCI through the Bridges' 64 GB windows (IOC3 at 0xF0_4050_0000) and takes the SuperIO interrupt from the first empty Bridge slot, so virtio goes in slots 6 and 7 (`bridge_slot`); what it attaches | | [ip30-ps2-keyboard-runs-set-3.md](devices/ip30-ps2-keyboard-runs-set-3.md) | rule | Octane: PROM 4.17 and IRIX's `ioc3_pckm` put the PS/2 keyboard in scan code set 3 (Linux/OpenBSD use 2); K_RD slot order, CLAMP_THREE, role by F2 answer; how the PROM's keyboard sequence was traced | diff --git a/clanker/devices/ip30-linux-7.3-guest.md b/clanker/devices/ip30-linux-7.3-guest.md index 6b53d0ea4..1d94ad99d 100644 --- a/clanker/devices/ip30-linux-7.3-guest.md +++ b/clanker/devices/ip30-linux-7.3-guest.md @@ -9,8 +9,9 @@ Bridge driver defers its probe for ever until it can read each board's part number from the one-wire NIC); RTC_DRV_DS1685_FAMILY; DEVTMPFS; `qlogic/1040.bin` built in for qla1280 (it calls request_firmware before any root exists); qla2xxx wants `ql2200_fw.bin`. The PROM loads the ELF64 -`vmlinux` (load address 0xa800000020004000) from `bootp()` but not from the -volume header (`ip30-prom-disk-load-of-a-large-elf-overruns-a-dma-list.md`). +`vmlinux` (load address 0xa800000020004000) from `bootp()`, and from the +volume header (`dksc(0,1,8)vmlinux`) once `mkvh build` has split its segment +into 512 KB pieces (`ip30-prom-disk-load-of-a-large-elf-overruns-a-dma-list.md`). **Kernel-side problems, not iris's** (fixed with a patch to `arch/mips/pci/pci-xtalk-bridge.c`): diff --git a/clanker/devices/ip30-prom-disk-load-of-a-large-elf-overruns-a-dma-list.md b/clanker/devices/ip30-prom-disk-load-of-a-large-elf-overruns-a-dma-list.md index 8f67d8f58..5fe937b16 100644 --- a/clanker/devices/ip30-prom-disk-load-of-a-large-elf-overruns-a-dma-list.md +++ b/clanker/devices/ip30-prom-disk-load-of-a-large-elf-overruns-a-dma-list.md @@ -1,29 +1,46 @@ -# Octane PROM: loading an 11 MB ELF from the volume header overruns a DMA list +# Octane PROM: a volume-header ELF segment over ~720 KB cannot be loaded -Status: open (PROM 4.17 as iris runs it, 2026-11-15). Not established whether -real hardware does the same; network boot is the workaround. +Status: rule (PROM 4.17, 2026-11-19). Not an iris bug: the limit is in the +PROM's code. `mkvh build` splits large segments (`PROM_SEGMENT_BYTES`, +512 KB) and the 11.5 MB Linux 7.3 kernel then boots from the volume header. +The first version of this note (2026-11-15) left the cause open. -**Symptom.** `boot -f dksc(0,1,8)vmlinux` with a Linux 7.3 kernel of 11.5 MB -(one PT_LOAD of 0xaf3e30 bytes at 0xa800000020004000) prints the size and -then `Read address error exception, bad address: 0x40001f400008` at PROM PC -0x9fc422a4, followed by `PANIC: driver_acquire: io_state.active deadlock` -for ever. The same file loads and runs from `bootp()vmlinux` (iris's NAT -TFTP). The SCSI trace ends with READ(10) of the ELF header (block 8, one -block): the crash comes while the PROM issues the segment's read. +**Symptom.** `boot -f dksc(0,1,8)vmlinux` with a Linux kernel of one 11 MB +PT_LOAD prints the size, then `Read address error exception, bad address: +0x40001f400008` at PROM PC 0x9fc422a4 and `PANIC: driver_acquire: +io_state.active deadlock` for ever. With the segment split into 1 MB +pieces it prints 1048576 and then `timeout on adapter 0 target 1`. With +512 KB pieces it loads and Linux runs. `bootp()vmlinux` always worked. -**What was found.** The PROM polls its SCSI adapters through a list whose -head is at 0xa800000020f17e40; the node for controller 1 at 0x20f0e9a0 had -next = 0x...20f0dfc0, base = 0x900000001f400000 before the boot and read -`0x00000000a0128000, 0x000040001f400000, 0x00000000a012c000, -0x0000400000050000` after it. Those are 16-byte records `(0, PCI address, -0x4000)` whose PCI addresses are the Bridge's direct window (0x8000_0000 + -physical 0x2012_8000, +0x4000, ...): a DMA page list for the 11 MB segment, -one record per 16 KB page, written past its buffer into the adapter node. -No CPU store to the node hit a physical write breakpoint, and iris's cache -model here is the shadow model (no dirty lines to lose), so the list is -written by the PROM (through the fast store path) or by a DMA. +**Cause.** The chain, PROM 4.17 addresses: -**Open.** What sizes that list on real hardware, i.e. whether the PROM -splits a large read by something iris answers (INQUIRY, MODE SENSE pages, -the ISP1040 firmware's limits) or real Octanes cannot load a kernel this -large from the volume header either. +- The volume-header file system (around 0x9fc77d00) reads all whole blocks + of a request straight into the destination in one call to the disk + driver; the ELF loader asks for a whole segment. dksc builds one READ(10) + for it (0x9fc733cc, 16-bit block count). Nothing splits the transfer. +- The ISP1040 driver (0x9fc42900) builds the DMA list with 0x9fc4400c: one + `{u64 PCI address, u32 length}` entry per 16 KB page, in an array at + +0x548 of its per-adapter state. Those states are a static array of 2528 + bytes each (0x9fc414d8: base 0x20f0dfc0, stride 0x9e0), so there is room + for about 73 entries; entry 74 on lands on controller 1's state (next + pointer, register base), and the adapter poll at 0x9fc422a4 dereferences + the garbage. The builder has no bound. +- Before that matters, the request ring has 8 entries (7 free when empty) + and the driver queues only if `(entries - 4) / 7 + 1 < free`: 4 data + segments in the command entry, 7 per continuation. More than 45 pages + (~720 KB) never fits, the driver waits for ring space that never comes, + and the command times out. + +IRIX's volume-header files stay under this: `sash` is a 267 KB ET_REL, +`IP30prom` 880 KB of raw flash image that the PROM does not ELF-load. + +**Rule.** A file the Octane PROM loads from a volume header needs every +PT_LOAD at or below ~720 KB of file data (512 KB plus a page of slack is what +`mkvh` uses). Splitting a segment into consecutive PT_LOADs at the same +offsets and addresses is transparent to the PROM's loader and to Linux. +`split_load_segments` in `src/sgi_vh.rs` does it; it rewrites the program +header table in place when the bytes after it are free (as in a vmlinux, +whose first segment starts at 0x1000), so the file keeps its size. + +**Debugging note.** A `lightning` build compiles out data breakpoints (`bp +add ... w`/`pw` never hit). Use a default build to catch a PROM store. diff --git a/docs/cpu-test-support.md b/docs/cpu-test-support.md index a8c0ed952..d204cc231 100644 --- a/docs/cpu-test-support.md +++ b/docs/cpu-test-support.md @@ -22,6 +22,12 @@ partition is type 5 (`PT_SYSV`)**, not 7 — see From the PROM: `boot -f dksc(0,,8)`. +An ELF executable with a segment of more than 512 KB is written with that +segment split into 512 KB segments, because the PROM reads a segment from the +volume header in one SCSI command and the Octane's PROM cannot read more than +about 720 KB in one (see [octane-ip30.md](octane-ip30.md#disks)). `--no-split` +copies every file unchanged. + ## B — `--load-elf` / monitor `loadelf` Load a static ELF32 MSB `ET_EXEC` for `EM_MIPS` straight into RAM and jump to diff --git a/docs/octane-ip30.md b/docs/octane-ip30.md index 7ffb07875..60f6a2258 100644 --- a/docs/octane-ip30.md +++ b/docs/octane-ip30.md @@ -110,6 +110,17 @@ stored and never run; SCSI commands go to iris's shared target model. The monitor's `scsi` and `cow` commands reach controller 0 (`scsi1`, `cow1` the external bus). +The PROM loads a file from a disk's volume header +(`boot -f dksc(0,1,8)vmlinux`) one ELF segment per SCSI read, and its +ISP1040 driver cannot issue a read of more than about 720 KB: a larger one +times out, and one over about 1.1 MB also corrupts the PROM's adapter state +and panics it. This is the PROM's own limit, not the emulator's. IRIX's +volume-header files are small enough; an 11 MB Linux `vmlinux` is not. +`mkvh build` splits each larger segment of an ELF executable into 512 KB +segments, which the PROM loads one after another, so a Linux kernel put in +the volume header with `mkvh` boots from disk (`--no-split` copies a file +unchanged). Loading over the network (`bootp()vmlinux`) has no such limit. + More storage goes on an option board in a spare XIO slot (`[[xio_board]]`, a PCI shoehorn or the PCI module; see [pci.md](pci.md#option-boards-in-xio-slots)): an [NVMe controller](nvme.md) with `[nvme]`, or a diff --git a/src/bin/mkvh.rs b/src/bin/mkvh.rs index e99bd6e1f..70114c31c 100644 --- a/src/bin/mkvh.rs +++ b/src/bin/mkvh.rs @@ -11,13 +11,17 @@ //! boot -f dksc(0,,8) //! ``` //! +//! An ELF executable whose segments are larger than the PROM can read in one +//! command (an 11 MB Linux vmlinux) is written with its segments split into +//! 512 KB pieces unless `--no-split` is given; see `PROM_SEGMENT_BYTES`. +//! //! Dump mode is the layout check: run it on an original SGI CD and the voldir //! should list sashARCS and friends with a valid checksum. use std::path::PathBuf; use std::process::exit; -use iris::sgi_vh::{build_image, ImageSpec, PartitionSpec, VolumeHeader}; +use iris::sgi_vh::{build_image_with_splits, ImageSpec, PartitionSpec, VolumeHeader, PROM_SEGMENT_BYTES}; const USAGE: &str = "\ usage: @@ -33,6 +37,10 @@ build options: e.g. --part 7:5:+:1283016=efs.img NB: SGI ships an EFS filesystem as type 5 (PT_SYSV), not type 7 — that is what the 6.5.22 install CD emits. + --no-split copy ELF files unchanged; by default an ELF executable + with a segment over 512 KB is written with that segment + split into 512 KB pieces, which the PROM can load (the + Octane's PROM panics or times out on a larger one) "; fn main() { @@ -50,7 +58,7 @@ fn main() { fn build(args: &[String]) -> Result<(), String> { let mut out: Option = None; - let mut spec = ImageSpec::default(); + let mut spec = ImageSpec { max_segment: PROM_SEGMENT_BYTES, ..ImageSpec::default() }; let mut i = 0; while i < args.len() { let a = &args[i]; @@ -58,6 +66,7 @@ fn build(args: &[String]) -> Result<(), String> { "--size" => { spec.total_bytes = parse_size(next(args, &mut i, "--size")?)?; } "--bootfile" => { spec.bootfile = Some(next(args, &mut i, "--bootfile")?.to_string()); } "--part" => { spec.partitions.push(parse_part(next(args, &mut i, "--part")?)?); } + "--no-split" => { spec.max_segment = 0; } _ if a.starts_with("--") => return Err(format!("unknown option '{}'\n{}", a, USAGE)), _ if out.is_none() => out = Some(PathBuf::from(a)), _ => { @@ -73,12 +82,18 @@ fn build(args: &[String]) -> Result<(), String> { return Err(format!("nothing to write\n{}", USAGE)); } - let placed = build_image(&out, &spec).map_err(|e| e.to_string())?; + let (placed, splits) = build_image_with_splits(&out, &spec).map_err(|e| e.to_string())?; let size = std::fs::metadata(&out).map(|m| m.len()).unwrap_or(0); println!("wrote {} ({} bytes)", out.display(), size); for f in &placed { println!(" {:<8} lbn {:>8} {:>10} bytes", f.name, f.lbn, f.nbytes); } + for (name, s) in &splits { + println!( + " {}: {} PT_LOAD segment(s), the largest {} bytes, written as {} of at most {} bytes so the PROM can load it (--no-split keeps it as it is)", + name, s.loads_before, s.largest, s.loads_after, spec.max_segment + ); + } Ok(()) } diff --git a/src/sgi_vh.rs b/src/sgi_vh.rs index 045ce91f7..1bdaed78e 100644 --- a/src/sgi_vh.rs +++ b/src/sgi_vh.rs @@ -340,6 +340,172 @@ pub struct ImageSpec { /// Voldir name -> host file to copy in. pub files: Vec<(String, std::path::PathBuf)>, pub partitions: Vec, + /// Split each ELF file's `PT_LOAD` segments so none carries more than + /// this many bytes of file data ([`split_load_segments`]); 0 copies the + /// files unchanged. [`PROM_SEGMENT_BYTES`] is what the PROMs can read. + pub max_segment: u64, +} + +/// The most file data one `PT_LOAD` segment of a volume-header file should +/// carry for the PROM to load it. +/// +/// The PROM reads each segment from the volume header with one SCSI command. +/// The Octane's PROM (4.17) describes the transfer to its ISP1040 as one +/// entry per 16 KB page, in a fixed array in its per-adapter state with room +/// for about 73, and queues it on an 8-entry request ring (one command entry +/// with 4 data segments, continuations with 7). A segment of more than 45 +/// pages, about 720 KB, never fits the ring and the command times out; one of +/// more than 73 pages also writes its page list over the next adapter's +/// state, and the PROM panics. 512 KB leaves room for a segment that does not +/// start on a page. IRIX's own volume-header files (`sash`, `IP30prom`) stay +/// under the limit; an 11 MB Linux `vmlinux` does not. +pub const PROM_SEGMENT_BYTES: u64 = 512 * 1024; + +/// A voldir file name and what [`split_load_segments`] did to it. +pub type SplitFile = (String, SegmentSplit); + +/// What [`split_load_segments`] did to one ELF file. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct SegmentSplit { + /// `PT_LOAD` segments before and after. + pub loads_before: usize, + pub loads_after: usize, + /// The largest segment's file data before the split. + pub largest: u64, +} + +/// Split every `PT_LOAD` segment of an MSB ELF executable (`ET_EXEC`, 32- or +/// 64-bit) that carries more than `max` bytes of file data into consecutive +/// segments of at most `max` bytes each, so a loader that reads a segment at +/// a time ([`PROM_SEGMENT_BYTES`]) reads it in pieces. The pieces cover the +/// same file bytes at the same addresses; the last one keeps the zero fill +/// (`p_memsz` beyond `p_filesz`). Other program headers are kept, in order. +/// +/// The new program header table goes where the old one was when the bytes +/// it grows into belong to no segment, section or section header table, and +/// after the end of the file otherwise. +/// +/// `Ok(None)` when the file is not such an ELF executable or needs no split. +pub fn split_load_segments(data: &[u8], max: u64) -> Result, SegmentSplit)>, String> { + if max == 0 || data.len() < 52 || &data[0..4] != b"\x7fELF" || data[5] != 2 { + return Ok(None); + } + let wide = match data[4] { + 1 => false, + 2 => true, + _ => return Ok(None), + }; + let rd = |off: usize, n: usize| -> Result { + let b = data.get(off..off + n).ok_or_else(|| format!("ELF header field at {off:#x} is past the end of the file"))?; + Ok(b.iter().fold(0u64, |v, &x| (v << 8) | u64::from(x))) + }; + if rd(16, 2)? != 2 { + return Ok(None); // not ET_EXEC: a relocatable sash, say + } + // (offset, size) of e_phoff, e_shoff; then e_phentsize, e_phnum, e_shentsize, e_shnum. + let (phoff_at, shoff_at, word, half_at) = if wide { (0x20, 0x28, 8, 0x36) } else { (0x1c, 0x20, 4, 0x2a) }; + let phoff = rd(phoff_at, word)? as usize; + let shoff = rd(shoff_at, word)? as usize; + let phentsize = rd(half_at, 2)? as usize; + let phnum = rd(half_at + 2, 2)? as usize; + let shentsize = rd(half_at + 4, 2)? as usize; + let shnum = rd(half_at + 6, 2)? as usize; + let want = if wide { 56 } else { 32 }; + if phnum == 0 { + return Ok(None); + } + if phentsize != want { + return Err(format!("program header entries are {phentsize} bytes, not {want}")); + } + + // p_type, p_flags, p_offset, p_vaddr, p_paddr, p_filesz, p_memsz, p_align. + type Ph = [u64; 8]; + let read_ph = |i: usize| -> Result { + let b = phoff + i * phentsize; + Ok(if wide { + [rd(b, 4)?, rd(b + 4, 4)?, rd(b + 8, 8)?, rd(b + 16, 8)?, rd(b + 24, 8)?, rd(b + 32, 8)?, rd(b + 40, 8)?, rd(b + 48, 8)?] + } else { + [rd(b, 4)?, rd(b + 24, 4)?, rd(b + 4, 4)?, rd(b + 8, 4)?, rd(b + 12, 4)?, rd(b + 16, 4)?, rd(b + 20, 4)?, rd(b + 28, 4)?] + }) + }; + let old: Vec = (0..phnum).map(read_ph).collect::>()?; + const PT_LOAD: u64 = 1; + let loads_before = old.iter().filter(|p| p[0] == PT_LOAD).count(); + let largest = old.iter().filter(|p| p[0] == PT_LOAD).map(|p| p[5]).max().unwrap_or(0); + if largest <= max { + return Ok(None); + } + + let mut new: Vec = Vec::new(); + for p in &old { + let [ty, flags, off, vaddr, paddr, filesz, memsz, align] = *p; + if ty != PT_LOAD || filesz <= max { + new.push(*p); + continue; + } + if off.checked_add(filesz).is_none_or(|end| end > data.len() as u64) || memsz < filesz { + return Err(format!("PT_LOAD at {vaddr:#x} lies outside the file or is shorter in memory than on disk")); + } + let mut done = 0; + while done < filesz { + let n = (filesz - done).min(max); + let last = done + n == filesz; + let m = if last { memsz - done } else { n }; + new.push([ty, flags, off + done, vaddr + done, paddr + done, n, m, align]); + done += n; + } + } + if new.len() > 0xfffe { + return Err(format!("splitting would need {} program headers", new.len())); + } + let loads_after = new.iter().filter(|p| p[0] == PT_LOAD).count(); + + // Byte ranges the new table must not overwrite if it stays in place. + let mut used: Vec<(u64, u64)> = new.iter().filter(|p| p[5] > 0).map(|p| (p[2], p[2] + p[5])).collect(); + if shoff != 0 && shnum != 0 { + used.push((shoff as u64, (shoff + shnum * shentsize) as u64)); + for i in 0..shnum { + let b = shoff + i * shentsize; + let (ty, off, size) = if wide { (rd(b + 4, 4)?, rd(b + 24, 8)?, rd(b + 32, 8)?) } else { (rd(b + 4, 4)?, rd(b + 16, 4)?, rd(b + 20, 4)?) }; + const SHT_NOBITS: u64 = 8; + if ty != SHT_NOBITS && size > 0 { + used.push((off, off + size)); + } + } + } + let table = (new.len() * phentsize) as u64; + let fits = |at: u64| at + table <= data.len() as u64 && !used.iter().any(|&(a, b)| a < at + table && at < b); + let mut out = data.to_vec(); + let at = if fits(phoff as u64) { + phoff as u64 + } else { + out.resize(out.len().next_multiple_of(8), 0); + let at = out.len() as u64; + out.resize(out.len() + table as usize, 0); + at + }; + + let put = |out: &mut Vec, off: usize, n: usize, v: u64| { + for i in 0..n { + out[off + i] = (v >> (8 * (n - 1 - i))) as u8; + } + }; + for (i, p) in new.iter().enumerate() { + let b = at as usize + i * phentsize; + let [ty, flags, off, vaddr, paddr, filesz, memsz, align] = *p; + if wide { + for (o, n, v) in [(0, 4, ty), (4, 4, flags), (8, 8, off), (16, 8, vaddr), (24, 8, paddr), (32, 8, filesz), (40, 8, memsz), (48, 8, align)] { + put(&mut out, b + o, n, v); + } + } else { + for (o, v) in [(0, ty), (4, off), (8, vaddr), (12, paddr), (16, filesz), (20, memsz), (24, flags), (28, align)] { + put(&mut out, b + o, 4, v); + } + } + } + put(&mut out, phoff_at, word, at); + put(&mut out, half_at + 2, 2, new.len() as u64); + Ok(Some((out, SegmentSplit { loads_before, loads_after, largest }))) } /// Build a complete raw image from `spec` and return the placed voldir entries. @@ -348,13 +514,28 @@ pub struct ImageSpec { /// volume-header partition (slot 8) is sized to cover them unless the caller /// supplies slot 8 itself. pub fn build_image(path: &Path, spec: &ImageSpec) -> io::Result> { + build_image_with_splits(path, spec).map(|(files, _)| files) +} + +/// [`build_image`], also returning each voldir file whose segments were split +/// ([`ImageSpec::max_segment`]) with what was done to it. +pub fn build_image_with_splits(path: &Path, spec: &ImageSpec) -> io::Result<(Vec, Vec)> { let mut vh = VolumeHeader::new(); let mut placed: Vec<(u32, Vec)> = Vec::new(); + let mut splits = Vec::new(); let mut lbn = VH_SECTORS as u32; for (name, src) in &spec.files { - let data = std::fs::read(src) + let mut data = std::fs::read(src) .map_err(|e| io::Error::new(e.kind(), format!("{}: {}", src.display(), e)))?; + match split_load_segments(&data, spec.max_segment) { + Ok(Some((split, what))) => { + data = split; + splits.push((name.clone(), what)); + } + Ok(None) => {} + Err(e) => return Err(io::Error::new(io::ErrorKind::InvalidData, format!("{}: {}", src.display(), e))), + } let nbytes = u32::try_from(data.len()) .map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, format!("{} is larger than 4 GB", src.display())))?; vh.add_file(name, lbn, nbytes)?; @@ -436,7 +617,7 @@ pub fn build_image(path: &Path, spec: &ImageSpec) -> io::Result f.write_all(&data)?; } f.sync_all()?; - Ok(vh.files()) + Ok((vh.files(), splits)) } fn sectors_for(bytes: u64) -> u64 { @@ -599,6 +780,7 @@ mod tests { bootfile: None, files: vec![("cputest".into(), payload.clone())], partitions: vec![PartitionSpec { slot: 7, first_block: Some(64), nblks: 128, ptype: PT_RAW, content: None }], + max_segment: 0, }; let placed = build_image(&p, &spec).expect("build"); assert_eq!(placed.len(), 1); @@ -653,6 +835,135 @@ mod tests { let _ = std::fs::remove_file(&big); } + /// An MSB ELF executable: one `PT_NOTE` and one `PT_LOAD` of `filesz` + /// bytes (each byte its offset mod 251) with `bss` bytes of zero fill, + /// the load at file offset 0x1000 and `pad` free bytes after the program + /// headers. A section header table with one `PROGBITS` section over the + /// segment follows the data. + fn test_elf(wide: bool, filesz: u64, bss: u64) -> Vec { + let (ehsize, phentsize, shentsize) = if wide { (64usize, 56usize, 64usize) } else { (52, 32, 40) }; + let load_off = 0x1000u64; + let vaddr = if wide { 0xa800_0000_2000_4000u64 } else { 0x8800_4000 }; + let shoff = (load_off + filesz).next_multiple_of(8); + let mut d = vec![0u8; shoff as usize + 2 * shentsize]; + let put = |d: &mut Vec, off: usize, n: usize, v: u64| { + for i in 0..n { d[off + i] = (v >> (8 * (n - 1 - i))) as u8; } + }; + d[0..4].copy_from_slice(b"\x7fELF"); + d[4] = if wide { 2 } else { 1 }; + d[5] = 2; + d[6] = 1; + put(&mut d, 16, 2, 2); // ET_EXEC + put(&mut d, 18, 2, 8); // EM_MIPS + let w = if wide { 8 } else { 4 }; + let (phoff_at, shoff_at, half_at) = if wide { (0x20, 0x28, 0x36) } else { (0x1c, 0x20, 0x2a) }; + put(&mut d, phoff_at, w, ehsize as u64); + put(&mut d, shoff_at, w, shoff); + put(&mut d, half_at, 2, phentsize as u64); + put(&mut d, half_at + 2, 2, 2); + put(&mut d, half_at + 4, 2, shentsize as u64); + put(&mut d, half_at + 6, 2, 2); + let ph = |d: &mut Vec, i: usize, f: [u64; 8]| { + let b = ehsize + i * phentsize; + let [ty, flags, off, va, pa, fsz, msz, align] = f; + if wide { + for (o, n, v) in [(0, 4, ty), (4, 4, flags), (8, 8, off), (16, 8, va), (24, 8, pa), (32, 8, fsz), (40, 8, msz), (48, 8, align)] { put(d, b + o, n, v); } + } else { + for (o, v) in [(0, ty), (4, off), (8, va), (12, pa), (16, fsz), (20, msz), (24, flags), (28, align)] { put(d, b + o, 4, v); } + } + }; + ph(&mut d, 0, [4, 4, load_off, vaddr, vaddr, 0x10, 0x10, 4]); + ph(&mut d, 1, [1, 7, load_off, vaddr, vaddr, filesz, filesz + bss, 0x4000]); + for i in 0..filesz as usize { d[load_off as usize + i] = (i % 251) as u8; } + // Section 1: PROGBITS over the segment. + let s1 = shoff as usize + shentsize; + put(&mut d, s1 + 4, 4, 1); + if wide { put(&mut d, s1 + 24, 8, load_off); put(&mut d, s1 + 32, 8, filesz); } + else { put(&mut d, s1 + 16, 4, load_off); put(&mut d, s1 + 20, 4, filesz); } + d + } + + /// The `PT_LOAD` headers of an MSB ELF file as (offset, vaddr, filesz, memsz). + fn loads(d: &[u8]) -> Vec<(u64, u64, u64, u64)> { + let wide = d[4] == 2; + let rd = |off: usize, n: usize| d[off..off + n].iter().fold(0u64, |v, &x| (v << 8) | x as u64); + let (phoff, es, n) = if wide { (rd(0x20, 8), rd(0x36, 2), rd(0x38, 2)) } else { (rd(0x1c, 4), rd(0x2a, 2), rd(0x2c, 2)) }; + (0..n as usize).filter_map(|i| { + let b = phoff as usize + i * es as usize; + if rd(b, 4) != 1 { return None; } + Some(if wide { (rd(b + 8, 8), rd(b + 16, 8), rd(b + 32, 8), rd(b + 40, 8)) } + else { (rd(b + 4, 4), rd(b + 8, 4), rd(b + 16, 4), rd(b + 20, 4)) }) + }).collect() + } + + #[test] + fn split_load_segments_cuts_a_large_segment_into_prom_sized_pieces() { + for wide in [true, false] { + let filesz = 2 * PROM_SEGMENT_BYTES + 0x1234; + let elf = test_elf(wide, filesz, 0x3000); + let (out, what) = split_load_segments(&elf, PROM_SEGMENT_BYTES).unwrap().expect("split"); + assert_eq!(what, SegmentSplit { loads_before: 1, loads_after: 3, largest: filesz }); + let got = loads(&out); + let (off0, va0) = (got[0].0, got[0].1); + let mut at = 0; + for (i, &(off, va, fsz, msz)) in got.iter().enumerate() { + assert!(fsz <= PROM_SEGMENT_BYTES, "piece {i} carries {fsz:#x} bytes"); + assert_eq!((off, va), (off0 + at, va0 + at), "piece {i} continues the last"); + let last = i + 1 == got.len(); + assert_eq!(msz, if last { fsz + 0x3000 } else { fsz }, "only the last piece keeps the zero fill"); + at += fsz; + } + assert_eq!(at, filesz, "the pieces cover the segment"); + // The table had room after the ELF header: the data is untouched. + assert_eq!(out.len(), elf.len()); + assert_eq!(&out[0x1000..], &elf[0x1000..]); + } + } + + #[test] + fn split_load_segments_moves_the_table_past_the_end_when_it_has_no_room() { + let mut elf = test_elf(true, 3 * PROM_SEGMENT_BYTES, 0); + // Put the program headers right before the segment: no room to grow. + let phoff = 0x1000 - 2 * 56; + elf.copy_within(64..64 + 2 * 56, phoff); + elf[0x20..0x28].copy_from_slice(&(phoff as u64).to_be_bytes()); + let (out, _) = split_load_segments(&elf, PROM_SEGMENT_BYTES).unwrap().expect("split"); + let new_phoff = u64::from_be_bytes(out[0x20..0x28].try_into().unwrap()); + assert!(new_phoff >= elf.len() as u64 && new_phoff % 8 == 0, "table at {new_phoff:#x}"); + assert_eq!(&out[..0x20], &elf[..0x20]); + assert_eq!(&out[0x40..elf.len()], &elf[0x40..], "everything after the header is kept"); + assert_eq!(loads(&out).len(), 3); + } + + #[test] + fn split_load_segments_leaves_small_and_foreign_files_alone() { + assert_eq!(split_load_segments(&test_elf(true, PROM_SEGMENT_BYTES, 0x100), PROM_SEGMENT_BYTES), Ok(None)); + let mut rel = test_elf(true, 4 * PROM_SEGMENT_BYTES, 0); + rel[17] = 1; // ET_REL, like sash + assert_eq!(split_load_segments(&rel, PROM_SEGMENT_BYTES), Ok(None)); + assert_eq!(split_load_segments(&vec![0xAAu8; 2 << 20], PROM_SEGMENT_BYTES), Ok(None)); + assert_eq!(split_load_segments(&test_elf(true, 4 * PROM_SEGMENT_BYTES, 0), 0), Ok(None)); + } + + #[test] + fn build_image_splits_an_oversized_kernel_only_when_asked() { + let kernel = unique_tmp_path("kernel"); + let elf = test_elf(true, PROM_SEGMENT_BYTES * 5 / 2, 0x100); + std::fs::write(&kernel, &elf).unwrap(); + for (max, want) in [(PROM_SEGMENT_BYTES, 3), (0, 1)] { + let p = unique_tmp_path("split"); + let spec = ImageSpec { files: vec![("vmlinux".into(), kernel.clone())], max_segment: max, ..Default::default() }; + let (placed, splits) = build_image_with_splits(&p, &spec).expect("build"); + assert_eq!(splits.len(), usize::from(max != 0)); + let bytes = std::fs::read(&p).unwrap(); + let off = placed[0].lbn as usize * SECTOR_SIZE as usize; + let file = &bytes[off..off + placed[0].nbytes as usize]; + assert_eq!(loads(file).len(), want); + let _ = std::fs::remove_file(&p); + } + let _ = std::fs::remove_file(&kernel); + } + #[test] fn build_image_rejects_an_image_too_small_for_its_contents() { let p = unique_tmp_path("toosmall");