From e5950ee1f094ef9107ff6fbb2bd7c2dda60d4eb0 Mon Sep 17 00:00:00 2001 From: HuJK Date: Wed, 9 Sep 2026 02:53:19 +0800 Subject: [PATCH] entries: report a Windows install as a boot entry of its own MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A Windows image has no vmlinuz, so `boot::scan` found nothing and every command could only answer "no boot artifacts" — indistinguishable from a Linux image whose /boot we failed to parse. A VM manager needs that difference: there is no kernel to direct-boot, and the image can only be started as a plain disk under UEFI firmware. Windows is not a separate question, so it doesn't get a separate command: it is another entry in `entries`/`boot-info`, carrying what starting it takes (firmware, architecture, boot manager) where a Linux entry carries kernel/initrd/cmdline. JSON entries gain `type` ("linux"/"windows") plus `firmware`/`arch`/`loader`, null on the kind they don't apply to; the array shape is unchanged, so existing consumers keep parsing. An entry comes either from a config's `chainloader` item (os-prober's dual-boot menu already declares Windows) or, when nothing declares it, from detection. Windows keeps its system files on NTFS, which we deliberately don't read, so detection works from the readable parts: the boot manager on the FAT ESP — whose PE machine field also gives the guest architecture — partition types only Windows creates, NTFS/ReFS magic, and, for BIOS installs, Windows' MBR bootstrap together with a VBR that chains to BOOTMGR/NTLDR. Keeping chainloader items also fixes a silent wrong answer: they have no kernel, so drop_stale_entries deleted them, and a `set default` pointing at Windows fell back to the first Linux entry with no hint. On a dual-boot image lbx now reports what GRUB would actually boot. Supporting changes: fsys detects ntfs/exfat/refs (and still never reads them), GPT/MBR learn the Microsoft partition types, and Partition gains a machine-readable `type_id` so callers match on the type instead of on `kind`'s wording. scripts/make-windows-test-image.sh builds the three layouts — uefi, bios, dual — this was verified against. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01Kh6FTGzsmY3wv9Kb8Deeah --- README.md | 79 +++++ scripts/make-windows-test-image.sh | 140 ++++++++ src/boot/grub.rs | 49 +++ src/boot/mod.rs | 143 +++++++- src/fsys/mod.rs | 22 +- src/guest.rs | 507 +++++++++++++++++++++++++++++ src/lib.rs | 1 + src/main.rs | 202 ++++++++---- src/part/gpt.rs | 18 +- src/part/mbr.rs | 10 + src/part/mod.rs | 15 + src/vdafix.rs | 1 + 12 files changed, 1114 insertions(+), 73 deletions(-) create mode 100755 scripts/make-windows-test-image.sh create mode 100644 src/guest.rs diff --git a/README.md b/README.md index 3805d8b..c6b0de3 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ source: grub $ lbx boot-info disk.qcow2 --json # same, machine-readable (for the VMM) $ lbx entries disk.qcow2 [--json] # ALL boot entries, '*' marks default + # (incl. Windows — see below) $ lbx info disk.qcow2 # image format + partition table + fs types $ lbx ls disk.qcow2 p2:/boot # list files @@ -75,6 +76,8 @@ kernel/initrd directly into guest memory, no temp files. | FAT12/16/32 (ESP) | [`fatfs`](https://crates.io/crates/fatfs) | done | | XFS | — | planned (RHEL 9 `/boot` default); detected with a clear error today | | btrfs / squashfs | — | detected with a clear error | +| NTFS / exFAT / ReFS | — | detected only, deliberately: nothing a direct boot needs is on them, but naming them identifies a [Windows guest](#windows-guests) | +| Windows entries | own code (`guest.rs`) | done: ESP boot manager (+ its PE arch), `chainloader` menu items, Microsoft partition types, NTFS/ReFS volumes, MBR/VBR boot code | | grub.cfg parser | `boot/grub.rs` | done: menuentry/submenu, linux*/initrd* variants, `set default` + grubenv `saved_entry` (index, `N>M`, id, title), `blscfg` redirect | | BLS (`/loader/entries`) | `boot/bls.rs` | done: version sort, `$kernelopts`/`$tuned_params` from grubenv, loader.conf `default` glob, boot-counting suffixes | | extlinux/syslinux | `boot/extlinux.rs` | done: `DEFAULT` label, `initrd=` lifted out of `APPEND` | @@ -92,6 +95,10 @@ We only need to read `/boot` and the ESP, not the root filesystem: - **btrfs** — openSUSE keeps `/boot` in a btrfs subvolume. Later. - **LVM/LUKS** — `/boot` is essentially never on LVM, and encrypted images can't be read without keys. Out of scope for now. +- **NTFS/exFAT/ReFS** — a Windows guest is direct-kernel-booted by nobody, + so there is nothing on them to extract. They are *detected* (never read) + because that is what tells a Windows image apart from a broken Linux one + — see below. ## Multi-kernel images @@ -112,6 +119,67 @@ the rewrite as `cmdline_fixed` (null when nothing applies); `extract --vdafix` writes the fixed cmdline. Whole-disk `root=/dev/sda` and multi-disk installs can't be mapped and are left untouched. +## Windows guests + +Not every image is Linux, and a VM manager needs to know before it goes +looking for a kernel: a Windows guest has none, and can only be started the +other way — as a plain disk under UEFI firmware (edk2/OVMF). So Windows is +not a separate question with a separate command; it is simply another +**boot entry**, listed by `entries`/`boot-info` alongside the Linux ones, +carrying what starting it takes in place of a kernel: + +```console +$ lbx entries win11.qcow2 +partition 1 (vfat): +* [1] Windows Boot Manager (windows) + firmware: uefi + arch: x86_64 + loader: p1:/EFI/Microsoft/Boot/bootmgfw.efi + +$ lbx entries dualboot.qcow2 --json +[{… "type":"linux", "kernel":"p5:/vmlinuz-6.6.9", "cmdline":"root=…", + "firmware":null, "arch":null, "loader":null}, + {… "type":"windows", "kernel":null, "cmdline":null, "default":true, + "firmware":"uefi","arch":"x86_64", + "loader":"p1:/EFI/Microsoft/Boot/bootmgfw.efi"}] +``` + +`type` is the field to switch on: a `linux` entry is direct-kernel booted +from `kernel`/`initrd`/`cmdline` as before, a `windows` entry needs +`firmware` (`uefi`/`bios`) and `arch` (which firmware build) instead, and +those fields are null on the kind they don't apply to. Everything else — +`partition`, `default`, `source`, `title`, `id` — means the same for both. + +A Windows entry comes from one of two places: + +- a **`chainloader` menu item** in an existing config. os-prober writes one + per foreign OS, so a dual-boot GRUB menu already declares Windows and + says whether it is the default; the target is resolved against the boot + manager actually on the ESP. (Such kernel-less items used to be dropped, + which silently moved the default onto a Linux entry GRUB would *not* + have booted.) +- **detection**, when nothing declares it — the usual Windows-only image. + Windows keeps its system files on NTFS, which we don't read, so this + works from what is readable anyway: + - the **ESP** is FAT: `/EFI/Microsoft/Boot/bootmgfw.efi` is the Windows + boot manager and settles it. Its PE header's machine field gives the + guest's architecture; + - **partition types** only Windows creates: Microsoft Reserved, Windows + recovery, LDM, Storage Spaces ("basic data" is deliberately not one of + them — it is the generic "holds a filesystem" type); + - **filesystem magic**: an NTFS or ReFS volume; + - **boot code**, for BIOS installs: Windows' own MBR bootstrap chaining + to an NTFS volume boot record that loads `BOOTMGR`/`NTLDR`. Either half + alone is too weak — the bootstrap outlives an OS replacement, and that + VBR code ships on *every* Windows-formatted volume — so both are + required. + +Windows volumes with **no** boot path at all are a data disk, not a guest: +they produce no entry, and the "nothing to boot" error says so, with the +evidence, rather than reporting a bootable Windows that isn't there. +`extract` refuses a Windows entry (there is no kernel to write) and points +at `--entry N` for the Linux ones. + ## Compressed kernels (direct-kernel boot) A direct-kernel-boot VMM (crosvm, `qemu -kernel`) loads the kernel itself, @@ -200,6 +268,17 @@ extract from the compressed image takes ~0.6 s. $ cargo run -- extract /tmp/lbxtest/disk.qcow2 -o /tmp/lbxtest/out ``` +`scripts/make-windows-test-image.sh [dir]` (needs `mkntfs` as well) builds +three Windows layouts for the entries above — GPT/UEFI with an ESP boot +manager, MBR/BIOS with Windows boot code, and a GRUB dual-boot menu whose +default is the Windows `chainloader` item: + +```console +$ cargo run -- entries /tmp/lbxwin/uefi.qcow2 +$ cargo run -- entries /tmp/lbxwin/bios.qcow2 +$ cargo run -- entries /tmp/lbxwin/dual.qcow2 +``` + ### Boot verification `scripts/verify-boot.sh ` is the end-to-end correctness check: it diff --git a/scripts/make-windows-test-image.sh b/scripts/make-windows-test-image.sh new file mode 100755 index 0000000..159567e --- /dev/null +++ b/scripts/make-windows-test-image.sh @@ -0,0 +1,140 @@ +#!/bin/sh +# Build test images that look like Windows guests, for `lbx entries`. +# Needs: sfdisk, mkfs.vfat, mmd/mcopy, mkntfs, qemu-img, python3. +# +# Three layouts, as a real installer would leave them: +# +# uefi.qcow2 GPT: ESP (with /EFI/Microsoft/Boot/bootmgfw.efi + BCD), +# Microsoft Reserved, an NTFS "Windows" volume, and an NTFS +# recovery partition. No config declares it, so `lbx entries` +# reports the entry from detection alone (source "windows"). +# bios.qcow2 MBR: one active NTFS partition (type 0x07), Windows' MBR +# bootstrap, and a volume boot record that chains to BOOTMGR. +# dual.qcow2 The same GPT layout plus an ext4 /boot with a GRUB menu +# whose os-prober `chainloader` item -- the one `set default` +# points at -- is the Windows install. +# +# The Windows-authored bytes we can't get from Linux tools -- the PE boot +# manager, the MBR bootstrap messages, the VBR's BOOTMGR reference -- are +# synthesized here; they are exactly the bytes `guest` keys on, so the +# images exercise the detector, not a real install's every detail. +set -eu + +dir="${1:-/tmp/lbxwin}" +rm -rf "$dir" +mkdir -p "$dir" +cd "$dir" + +# A boot manager is a PE/COFF executable; its COFF machine field is what +# tells the VMM which firmware architecture the guest needs (0x8664 = x86-64). +python3 -c " +import struct +v = bytearray(4096) +v[0:2] = b'MZ' +pe = 0x100 +v[0x3c:0x40] = struct.pack('/dev/null +esp="uefi.raw@@$((2048 * 512))" +mmd -i "$esp" ::/EFI ::/EFI/Microsoft ::/EFI/Microsoft/Boot ::/EFI/Boot +mcopy -i "$esp" bootmgfw.efi ::/EFI/Microsoft/Boot/bootmgfw.efi +mcopy -i "$esp" BCD ::/EFI/Microsoft/Boot/BCD +mcopy -i "$esp" bootmgfw.efi ::/EFI/Boot/bootx64.efi + +# mkntfs can't format at an offset; build the volumes, then place them. +truncate -s 64M windows.ntfs +mkntfs -F -Q -L Windows windows.ntfs >/dev/null 2>&1 +truncate -s 64M recovery.ntfs +mkntfs -F -Q -L Recovery recovery.ntfs >/dev/null 2>&1 +dd if=windows.ntfs of=uefi.raw bs=512 seek=296960 conv=notrunc status=none +dd if=recovery.ntfs of=uefi.raw bs=512 seek=428032 conv=notrunc status=none + +# --- BIOS / MBR --------------------------------------------------------- +truncate -s 128M bios.raw +sfdisk --quiet bios.raw <<'EOF' +label: dos +unit: sectors +start=2048, size=260096, type=7, bootable +EOF +dd if=windows.ntfs of=bios.raw bs=512 seek=2048 conv=notrunc status=none + +# Windows' own boot code: the MBR bootstrap's messages, and the file name +# the NTFS volume boot record chains to. mkntfs writes neither. +python3 -c " +disk = open('bios.raw', 'r+b') +mbr = bytearray(disk.read(512)) +for off, msg in ((0x8b, b'Invalid partition table'), + (0xa3, b'Error loading operating system'), + (0xc1, b'Missing operating system')): + mbr[off:off + len(msg)] = msg +disk.seek(0); disk.write(mbr) + +vbr_at = 2048 * 512 +disk.seek(vbr_at) +vbr = bytearray(disk.read(512)) +for off, msg in ((0x188, b'BOOTMGR'), + (0x1a0, b'BOOTMGR is missing')): # both inside the boot code + vbr[off:off + len(msg)] = msg +disk.seek(vbr_at); disk.write(vbr) +disk.close() +" + +# --- dual boot: the UEFI layout, plus a Linux /boot with a GRUB menu ---- +mkdir -p boot/grub +for v in 6.6.9-test; do + head -c 256K /dev/urandom > "boot/vmlinuz-$v" + head -c 512K /dev/urandom > "boot/initramfs-$v.img" +done +# `set default` names the os-prober entry by id, i.e. GRUB boots Windows. +cat > boot/grub/grub.cfg <<'CFG' +set default="osprober-efi-1234-ABCD" +menuentry 'Debian GNU/Linux' $menuentry_id_option 'debian' { + linux /vmlinuz-6.6.9-test root=/dev/vda5 ro quiet + initrd /initramfs-6.6.9-test.img +} +menuentry 'Debian GNU/Linux, recovery mode' $menuentry_id_option 'debian-recovery' { + linux /vmlinuz-6.6.9-test root=/dev/vda5 ro single + initrd /initramfs-6.6.9-test.img +} +menuentry 'Windows Boot Manager (on /dev/vda1)' --class windows $menuentry_id_option 'osprober-efi-1234-ABCD' { + insmod part_gpt + insmod fat + search --no-floppy --fs-uuid --set=root 1234-ABCD + chainloader /EFI/Microsoft/Boot/bootmgfw.efi +} +CFG + +truncate -s 512M dual.raw +dd if=uefi.raw of=dual.raw conv=notrunc status=none +sfdisk --quiet dual.raw <<'SFD' +label: gpt +unit: sectors +start=2048, size=262144, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B, name="EFI system partition" +start=264192, size=32768, type=E3C9E316-0B5C-4DB8-817D-F92DF00215AE, name="Microsoft reserved partition" +start=296960, size=131072, type=EBD0A0A2-B9E5-4433-87C0-68B6B72699C7, name="Basic data partition" +start=428032, size=131072, type=DE94BBA4-06D1-4D40-A16A-BFD50179D6AC, name="Basic data partition" +start=559104, size=262144, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, name="boot" +SFD +mke2fs -q -t ext4 -L bootfs -d boot -E offset=$((559104 * 512)) dual.raw 128M + +for img in uefi bios dual; do + qemu-img convert -f raw -O qcow2 "$img.raw" "$img.qcow2" +done +echo "created $dir/{uefi,bios,dual}.{raw,qcow2}" diff --git a/src/boot/grub.rs b/src/boot/grub.rs index b821c07..6854e1e 100644 --- a/src/boot/grub.rs +++ b/src/boot/grub.rs @@ -83,6 +83,19 @@ pub(crate) fn parse_str(text: &str, env: &BTreeMap) -> GrubScan "initrd" | "initrd16" | "initrdefi" => { cur.entry.initrd = words[1..].iter().map(|w| clean_path(w)).collect(); } + // os-prober writes a `chainloader` item per foreign OS; the + // argument is the boot manager to hand over to (options + // like `--force` come first). `chainloader +1` (a partition + // boot sector) names no file and resolves to nothing. + "chainloader" => { + cur.entry.chainloader = words[1..] + .iter() + .find(|w| !w.starts_with('-')) + // `+1` is a block list (a partition's boot sector), + // not a path: keep it verbatim so nothing takes it + // for a file. + .map(|w| if w.starts_with('+') { w.clone() } else { clean_path(w) }); + } _ => {} } } else { @@ -335,6 +348,42 @@ submenu 'Advanced options for Ubuntu' $menuentry_id_option 'gnulinux-advanced-uu } "#; + /// os-prober's dual-boot item: no kernel, a `chainloader` target, and + /// `set default` pointing at it by id. + const OSPROBER: &str = r#" +set default="osprober-efi-1234-ABCD" +menuentry 'Debian GNU/Linux' $menuentry_id_option 'debian' { + linux /vmlinuz-6.6.9 root=UUID=uuid1 ro + initrd /initramfs-6.6.9.img +} +menuentry 'Windows Boot Manager (on /dev/sda1)' --class windows $menuentry_id_option 'osprober-efi-1234-ABCD' { + insmod part_gpt + search --no-floppy --fs-uuid --set=root 1234-ABCD + chainloader /EFI/Microsoft/Boot/bootmgfw.efi +} +menuentry 'Older Windows (on /dev/sda3)' $menuentry_id_option 'osprober-chain-sda3' { + chainloader +1 +} +"#; + + #[test] + fn parses_chainloader_entries() { + let scan = parse_str(OSPROBER, &BTreeMap::new()); + assert_eq!(scan.entries.len(), 3); + // The Linux entry is untouched... + assert_eq!(scan.entries[0].kernel.as_deref(), Some("/vmlinuz-6.6.9")); + assert_eq!(scan.entries[0].chainloader, None); + // ...and the foreign ones carry their target instead of a kernel. + assert_eq!(scan.entries[1].kernel, None); + assert_eq!( + scan.entries[1].chainloader.as_deref(), + Some("/EFI/Microsoft/Boot/bootmgfw.efi") + ); + assert_eq!(scan.entries[2].chainloader.as_deref(), Some("+1")); + // `set default` names it by id: GRUB would boot Windows, not Linux. + assert_eq!(scan.default, Some(1)); + } + #[test] fn parses_entries_and_submenu() { let scan = parse_str(UBUNTU_STYLE, &BTreeMap::new()); diff --git a/src/boot/mod.rs b/src/boot/mod.rs index 5425771..df2f493 100644 --- a/src/boot/mod.rs +++ b/src/boot/mod.rs @@ -31,6 +31,9 @@ pub enum Source { Extlinux, #[default] Fallback, + /// Not declared by any config: [`crate::guest`] found the install + /// itself (a Windows boot manager on the ESP). + Windows, } impl std::fmt::Display for Source { @@ -40,10 +43,66 @@ impl std::fmt::Display for Source { Source::Bls => "bls", Source::Extlinux => "extlinux", Source::Fallback => "fallback", + Source::Windows => "windows", }) } } +/// What a VMM has to do to start an entry. `source` says where we learned +/// about the entry; this says what booting it means. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub enum EntryKind { + /// Kernel + initramfs: load them and jump (direct-kernel boot). + #[default] + Linux, + /// A Windows install: no kernel exists: run firmware and let its boot + /// manager take over, with the image attached as a plain disk. + Windows, +} + +impl std::fmt::Display for EntryKind { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(match self { + EntryKind::Linux => "linux", + EntryKind::Windows => "windows", + }) + } +} + +/// The firmware an entry starts from, when it isn't direct-kernel booted. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Firmware { + /// A boot manager on the ESP: run the image under UEFI firmware. + Uefi, + /// MBR/VBR boot code: needs a legacy BIOS (a VMM with no CSM can't). + Bios, + /// Both paths are present (a UEFI install that kept its MBR bootstrap). + Both, +} + +impl std::fmt::Display for Firmware { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(match self { + Firmware::Uefi => "uefi", + Firmware::Bios => "bios", + Firmware::Both => "uefi+bios", + }) + } +} + +/// What starting a Windows entry takes, in place of a kernel. +#[derive(Debug, Clone)] +pub struct WindowsBoot { + pub firmware: Firmware, + /// Architecture from the boot manager's PE header (`x86_64`, + /// `aarch64`, ...), i.e. which firmware build the VMM needs. `None` + /// for a BIOS install, whose loader sits on unreadable NTFS. + pub arch: Option<&'static str>, + /// The boot manager, as a `pN:/path` URI — unlike `kernel`/`initrd` + /// it may live on a different partition than the config that names it. + pub loader: Option, +} + /// One bootable configuration, resolved as far as we can. /// Paths are filesystem-absolute (see module note about /boot prefixes). #[derive(Debug, Clone, Default)] @@ -58,6 +117,22 @@ pub struct BootEntry { pub cmdline: Option, pub version: Option, pub source: Source, + /// Set when this entry boots Windows rather than a kernel; `kernel` + /// and `initrd` are then empty. [`BootEntry::kind`] reads it. + pub windows: Option, + /// A config's `chainloader` target, verbatim (an EFI binary path). + /// Such an entry hands control to another boot manager instead of + /// loading a kernel; [`crate::guest`] resolves what it points at. + pub chainloader: Option, +} + +impl BootEntry { + pub fn kind(&self) -> EntryKind { + match self.windows { + Some(_) => EntryKind::Windows, + None => EntryKind::Linux, + } + } } /// Everything we found on one filesystem. @@ -189,6 +264,26 @@ fn loader_conf_default(fs: &dyn FileSystem, configs: &mut Vec) -> Option None } +/// Keep the entries `keep` marks, moving `default` to wherever the entry +/// it pointed at ended up (or to the first survivor when that entry is +/// gone). The one place entry indices are renumbered. +pub(crate) fn retain( + entries: Vec, + default: Option, + keep: &[bool], +) -> (Vec, Option) { + let new_default = default + .map(|d| keep[..d].iter().filter(|&&k| k).count()) + .filter(|_| default.is_some_and(|d| keep[d])); + let kept: Vec = entries + .into_iter() + .zip(keep) + .filter_map(|(e, &k)| k.then_some(e)) + .collect(); + let new_default = if kept.is_empty() { None } else { new_default.or(Some(0)) }; + (kept, new_default) +} + /// Configs can reference kernels that were since removed. Drop entries /// whose kernel file is missing — unless that would drop everything, in /// which case keep the originals (better a stale answer than none). @@ -204,20 +299,19 @@ fn drop_stale_entries( // at the rootfs). Such a partition then no longer masquerades as bootable // and beat the one holding the real kernel. (Transient remote-read blips // are absorbed by the HTTP layer's retries.) + // + // A `chainloader` entry has no kernel by definition — it starts + // another boot manager (a dual-boot menu's Windows item). Keep it: + // `guest` resolves the target, and dropping it here would silently + // move the default off what the bootloader actually boots. let keep: Vec = entries .iter() - .map(|e| e.kernel.as_deref().is_some_and(|k| fs.exists(k))) + .map(|e| { + e.chainloader.is_some() + || e.kernel.as_deref().is_some_and(|k| fs.exists(k)) + }) .collect(); - let new_default = default - .map(|d| keep[..d].iter().filter(|&&k| k).count()) - .filter(|_| default.is_some_and(|d| keep[d])); - let kept: Vec = entries - .into_iter() - .zip(&keep) - .filter_map(|(e, &k)| k.then_some(e)) - .collect(); - let new_default = if kept.is_empty() { None } else { new_default.or(Some(0)) }; - (kept, new_default) + retain(entries, default, &keep) } /// Kernel releases this entry could map to, most-specific first: the @@ -380,7 +474,7 @@ fn fallback_scan(fs: &dyn FileSystem) -> Result<(Vec, Option)> } #[cfg(test)] -mod tests { +pub(crate) mod tests { use super::*; use crate::fsys::DirEntry; use std::collections::BTreeMap; @@ -500,6 +594,31 @@ mod tests { assert_eq!(scan.default, Some(0)); } + #[test] + fn chainloader_entries_survive_the_stale_sweep() { + // A chainloader item has no kernel by definition; dropping it would + // move the default onto an entry GRUB would not have booted. + let fs = MockFs::new([ + ( + "/boot/grub/grub.cfg", + "set default=\"1\"\nmenuentry 'Debian' {\n linux /boot/vmlinuz-6.6.9\n}\nmenuentry 'Windows Boot Manager' {\n chainloader /EFI/Microsoft/Boot/bootmgfw.efi\n}\nmenuentry 'UEFI Firmware Settings' {\n fwsetup\n}\n", + ), + ("/boot/vmlinuz-6.6.9", "k"), + ]); + let scan = scan(&fs).unwrap(); + // The kernel-less firmware-settings stub still goes. + assert_eq!(scan.entries.len(), 2); + assert_eq!(scan.default, Some(1)); + let win = &scan.entries[1]; + assert_eq!(win.kernel, None); + assert_eq!( + win.chainloader.as_deref(), + Some("/EFI/Microsoft/Boot/bootmgfw.efi") + ); + // `guest` decides what it points at; `boot` only records it. + assert_eq!(win.kind(), EntryKind::Linux); + } + #[test] fn fallback_sorts_by_version_and_honors_symlink() { let fs = MockFs::new([ diff --git a/src/fsys/mod.rs b/src/fsys/mod.rs index 2dcac0c..255f2d6 100644 --- a/src/fsys/mod.rs +++ b/src/fsys/mod.rs @@ -1,8 +1,11 @@ //! Filesystem access behind one read-only trait. //! //! Supported: ext2/3/4 (`ext4-view`), FAT12/16/32 (`fatfs`). -//! Detected-but-unsupported (XFS, btrfs, squashfs) get a precise error so -//! users know what they hit. XFS is the next milestone (RHEL 9 /boot). +//! Detected-but-unsupported (XFS, btrfs, squashfs, NTFS/exFAT/ReFS) get a +//! precise error so users know what they hit. XFS is the next milestone +//! (RHEL 9 /boot). The Windows filesystems are detected but deliberately +//! not read: nothing a direct-kernel boot needs lives on them, and naming +//! them is what lets [`crate::guest`] recognize a Windows image. mod ext4; mod vfat; @@ -89,11 +92,22 @@ pub fn detect(dev: &D) -> Result> { } } - // FAT: x86 jump at 0 plus an OEM/FS-type marker. fatfs validates the - // BPB properly; this is only a cheap pre-filter. + // The rest live in the boot sector: read it once. if dev.check_bounds(0, 512).is_ok() { let mut sector = [0u8; 512]; dev.read_at(0, &mut sector)?; + + // NTFS / exFAT / ReFS all put their name where FAT keeps the OEM + // string (offset 3). Checked before FAT, whose pre-filter is looser. + match §or[3..11] { + b"NTFS " => return Ok(Some("ntfs")), + b"EXFAT " => return Ok(Some("exfat")), + b"ReFS\0\0\0\0" => return Ok(Some("refs")), + _ => {} + } + + // FAT: x86 jump at 0 plus an OEM/FS-type marker. fatfs validates the + // BPB properly; this is only a cheap pre-filter. let jump_ok = matches!(sector[0], 0xeb | 0xe9); let fat32 = §or[82..90] == b"FAT32 "; let fat16 = sector[54..62].starts_with(b"FAT"); diff --git a/src/guest.rs b/src/guest.rs new file mode 100644 index 0000000..47ace63 --- /dev/null +++ b/src/guest.rs @@ -0,0 +1,507 @@ +//! Find the boot entries no bootloader config declares — today, a +//! **Windows** install. +//! +//! [`boot::scan`](crate::boot::scan) reads one filesystem and finds what +//! the configs on it describe. That misses a whole class of bootable +//! image: Windows keeps its system files on NTFS (which we deliberately +//! don't read) and, on a Windows-only image, no config file mentions it at +//! all. Such an image would look empty — indistinguishable from a Linux +//! one whose `/boot` we failed to parse. +//! +//! So this module walks the *disk* rather than a filesystem, and produces +//! the merged answer [`bootable`]: every partition that boots something, +//! Linux entries as the configs describe them, plus Windows folded in as +//! an entry of its own. A Windows entry carries what starting it takes — +//! firmware, architecture, boot manager — in place of a kernel. +//! +//! It comes from one of two places: +//! +//! * a **`chainloader` menu item** in an existing config: os-prober writes +//! one per foreign OS, so a dual-boot GRUB menu already declares Windows +//! and even says whether it is the default. The target is resolved here +//! against the boot manager actually on the ESP; +//! * **detection**, when nothing declares it — the usual Windows-only +//! image. What it keys on, all of it readable without touching NTFS: +//! - the **ESP** is FAT: `/EFI/Microsoft/Boot/bootmgfw.efi` is the +//! Windows boot manager and settles it, and its PE header gives the +//! architecture, i.e. which firmware build the VMM needs; +//! - **partition types** only Windows creates: Microsoft Reserved, +//! Windows recovery, LDM, Storage Spaces; +//! - **filesystem magic**: an NTFS or ReFS volume; +//! - **boot code**: Windows' own MBR bootstrap chaining to an NTFS +//! volume boot record that loads `BOOTMGR`/`NTLDR` — a BIOS install. +//! +//! [`Windows::evidence`] lists everything a verdict rests on, so it can be +//! judged rather than trusted. Windows volumes with no boot path at all +//! (`firmware: None`) are an attached data disk, not a guest: they produce +//! no entry, only an explanation for why the image boots nothing. + +use crate::blockdev::ReadAt; +use crate::boot::{BootEntry, BootScan, Firmware, Source, WindowsBoot}; +use crate::fsys::{self, FileSystem}; +use crate::part::{self, Partition, TableKind}; +use crate::Result; +use std::sync::Arc; + +/// A Windows install, as seen from outside its filesystems. +#[derive(Debug, Clone, Default)] +pub struct Windows { + /// `None` when Windows volumes were found but no boot path — an + /// attached data disk rather than a bootable guest. + pub firmware: Option, + /// URI of the UEFI boot manager, `pN:/EFI/Microsoft/Boot/bootmgfw.efi`. + pub boot_manager: Option, + /// URI of the UEFI boot configuration store (`BCD`), when present. + pub bcd: Option, + /// Architecture from the boot manager's PE header: `x86_64`, + /// `aarch64`, `i386` or `arm`. `None` when there is no boot manager to + /// read (a BIOS install's loader lives on unreadable NTFS). + pub arch: Option<&'static str>, + /// Everything the verdict rests on, one line per finding. + pub evidence: Vec, +} + +impl Windows { + /// One-line description for diagnostics: `"uefi, x86_64"`. + pub fn summary(&self) -> String { + let firmware = match self.firmware { + Some(f) => f.to_string(), + None => "no boot manager found".to_string(), + }; + match self.arch { + Some(arch) => format!("{firmware}, {arch}"), + None => firmware, + } + } +} + +/// One partition that boots something, with the handle needed to read the +/// files its entries name. +pub struct PartitionBoot { + pub partition: Partition, + /// Filesystem as detected: `"vfat"`, `"ext4"`, `"ntfs"`, ... + pub fs_type: &'static str, + /// Open handle, or `None` for a partition we could only sniff (NTFS): + /// a Windows entry has no per-entry files to probe anyway. + pub fs: Option>, + pub scan: BootScan, +} + +/// Every partition that boots something: the entries its configs declare, +/// plus the Windows install as an entry of its own. Partitions that boot +/// nothing are left out, so an empty result means the image boots nothing +/// we understand — ask [`windows`] why. +pub fn bootable(disk: &Arc) -> Result> { + let mut walk = walk(disk, true)?; + let win = walk.windows.take(); + let bm_path = win.as_ref().and_then(|w| w.boot_manager.as_deref()).map(path_of); + + // A `chainloader` item is kept only if we can say what it starts: the + // boot manager we found on the ESP. Anything else (another loader, + // `chainloader +1`) goes, as a kernel-less entry always did. + let mut declared = false; + for pb in &mut walk.parts { + let keep: Vec = pb + .scan + .entries + .iter_mut() + .map(|e| { + let Some(target) = e.chainloader.as_deref() else { return true }; + if !bm_path.is_some_and(|bm| same_file(target, bm)) { + return false; + } + e.windows = Some(WindowsBoot { + firmware: Firmware::Uefi, // it chainloads an EFI binary + arch: win.as_ref().and_then(|w| w.arch), + loader: win.as_ref().and_then(|w| w.boot_manager.clone()), + }); + declared = true; + true + }) + .collect(); + let (entries, default) = + crate::boot::retain(std::mem::take(&mut pb.scan.entries), pb.scan.default, &keep); + pb.scan.entries = entries; + pb.scan.default = default; + } + + // Nothing declared it, but it is there and bootable: add the entry. + if !declared + && let Some(win) = &win + && let Some(firmware) = win.firmware + { + let host = win + .boot_manager + .as_deref() + .and_then(uri_partition) + .or(walk.bios_part); + if let Some(index) = host + && let Some(pb) = walk.parts.iter_mut().find(|pb| pb.partition.index == index) + { + pb.scan.entries.push(BootEntry { + title: Some("Windows Boot Manager".to_string()), + id: Some("windows".to_string()), + source: Source::Windows, + windows: Some(WindowsBoot { + firmware, + arch: win.arch, + loader: win.boot_manager.clone(), + }), + ..Default::default() + }); + // Nothing else on that partition boots, so it is the default. + pb.scan.default.get_or_insert(pb.scan.entries.len() - 1); + } + } + + walk.parts.retain(|pb| !pb.scan.entries.is_empty()); + Ok(walk.parts) +} + +/// The Windows evidence on its own — for explaining an image that boots +/// nothing, where a full [`bootable`] scan has already come up empty. +pub fn windows(disk: &Arc) -> Result> { + Ok(walk(disk, false)?.windows) +} + +/// Partition types only Windows creates. "Microsoft basic data" is +/// deliberately absent: it is the generic "holds a filesystem" type and +/// says nothing on its own. +const WINDOWS_TYPES: &[&str] = &[ + part::gpt_type::MS_RESERVED, + part::gpt_type::WINDOWS_RECOVERY, + part::gpt_type::MS_LDM_METADATA, + part::gpt_type::MS_LDM_DATA, + part::gpt_type::MS_STORAGE_SPACES, + "0x27", // MBR hidden NTFS: the recovery partition +]; + +/// Path components of the UEFI Windows boot manager and its configuration +/// store, relative to the ESP root. +const BOOT_MANAGER: &[&str] = &["EFI", "Microsoft", "Boot", "bootmgfw.efi"]; +const BCD: &[&str] = &["EFI", "Microsoft", "Boot", "BCD"]; + +/// The NTFS volume boot record is 16 sectors; its boot code and messages +/// (`BOOTMGR is missing`, ...) fit well inside that. +const VBR_LEN: u64 = 16 * 512; + +struct Walk { + parts: Vec, + windows: Option, + /// Partition of a BIOS install's system volume, to hang its entry on. + bios_part: Option, +} + +/// One pass over the disk: what every partition is, and — with +/// `want_entries` — what the configs on it declare. +/// +/// `'static`: each partition is handed to `fsys` as a `Box`. +fn walk(disk: &Arc, want_entries: bool) -> Result { + let table = part::scan(disk.as_ref())?; + let mut parts: Vec = Vec::new(); + let mut evidence: Vec = Vec::new(); + let mut boot_manager = None; + let mut bcd = None; + let mut arch = None; + let mut ntfs_loader = None; // NTFS volume that chains to bootmgr/ntldr + let mut volumes = false; // an NTFS/ReFS volume exists at all + let mut win_type = false; // a Windows-only partition type + + // Disk level: the bootstrap code Windows writes into the MBR. + let mbr_bootstrap = matches!(table.kind, TableKind::Mbr) && { + let mut sector = [0u8; 512]; + disk.check_bounds(0, 512).is_ok() + && disk.read_at(0, &mut sector).is_ok() + && is_windows_bootstrap(§or) + }; + if mbr_bootstrap { + evidence.push("mbr: Windows bootstrap code".into()); + } + + for p in &table.partitions { + if WINDOWS_TYPES.contains(&p.type_id.as_str()) { + win_type = true; + evidence.push(format!("p{}: {} partition", p.index, p.kind)); + } + + let dev: Box = Box::new(p.open(Arc::clone(disk))); + let Some(fs_type) = fsys::detect(&dev).unwrap_or(None) else { continue }; + match fs_type { + "ntfs" | "refs" => { + volumes = true; + match vbr_loader(&dev) { + // Every Windows-formatted NTFS volume carries this boot + // code, data volumes included — it marks a BIOS boot + // path only together with a Windows MBR bootstrap. + Some(loader) => { + ntfs_loader.get_or_insert(p.index); + evidence.push(format!( + "p{}: {fs_type} volume, VBR chains to {loader}", + p.index + )); + } + None => evidence.push(format!("p{}: {fs_type} volume", p.index)), + } + parts.push(PartitionBoot { + partition: p.clone(), + fs_type, + fs: None, + scan: BootScan::default(), + }); + } + "ext4" | "vfat" => { + let Ok(fs) = fsys::open(dev) else { continue }; + if let Some(path) = find_path(fs.as_ref(), BOOT_MANAGER) { + evidence.push(format!("p{}: Windows boot manager {path}", p.index)); + if arch.is_none() { + arch = fs + .read_prefix(&path, 4096) + .ok() + .and_then(|head| pe_arch(&head)); + } + boot_manager.get_or_insert(uri(p.index, &path)); + } + if let Some(path) = find_path(fs.as_ref(), BCD) { + evidence.push(format!("p{}: boot configuration store {path}", p.index)); + bcd.get_or_insert(uri(p.index, &path)); + } + let scan = if want_entries { + crate::boot::scan(fs.as_ref()).unwrap_or_default() + } else { + BootScan::default() + }; + parts.push(PartitionBoot { + partition: p.clone(), + fs_type, + fs: Some(fs), + scan, + }); + } + _ => {} + } + } + + // A BIOS install is Windows' own MBR bootstrap chaining to an NTFS + // volume that loads bootmgr/ntldr. Either half alone is too weak: the + // bootstrap survives an OS replacement, and the VBR code ships with + // every Windows-formatted volume. + let bios_part = ntfs_loader.filter(|_| mbr_bootstrap); + let firmware = match (boot_manager.is_some(), bios_part.is_some()) { + (true, true) => Some(Firmware::Both), + (true, false) => Some(Firmware::Uefi), + (false, true) => Some(Firmware::Bios), + (false, false) => None, + }; + + // Enough to call it Windows: a boot path, a partition type only + // Windows creates, its boot configuration store, or an NTFS/ReFS + // volume. (Not exFAT — it is the interchange format of every camera + // and SD card, and says nothing about the guest.) + let is_windows = firmware.is_some() || win_type || bcd.is_some() || volumes; + let windows = is_windows.then_some(Windows { + firmware, + boot_manager, + bcd, + arch, + evidence, + }); + Ok(Walk { parts, windows, bios_part }) +} + +fn uri(part: usize, path: &str) -> String { + format!("p{part}:{path}") +} + +/// Partition index of a `pN:/path` URI. +fn uri_partition(uri: &str) -> Option { + uri.strip_prefix('p')?.split_once(':')?.0.parse().ok() +} + +/// Path half of a `pN:/path` URI. +fn path_of(uri: &str) -> &str { + uri.split_once(':').map(|(_, path)| path).unwrap_or(uri) +} + +/// Do two config paths name the same file? Compared case-insensitively +/// (the ESP is FAT), with `\` accepted for `/`, and by base name too: a +/// config may reach the boot manager by a path we spell differently. +fn same_file(a: &str, b: &str) -> bool { + let norm = |s: &str| s.replace('\\', "/").trim_start_matches('/').to_ascii_lowercase(); + let (a, b) = (norm(a), norm(b)); + let base = |s: &str| s.rsplit('/').next().unwrap_or(s).to_string(); + a == b || base(&a) == base(&b) +} + +/// Resolve `/a/b/c` one component at a time, ignoring case. FAT lookups +/// are case-insensitive already, but the walk also covers an ESP read +/// through another backend, and image builders vary the spelling (`EFI` / +/// `efi`, `Boot` / `boot`). Returns the path as actually spelled on disk. +fn find_path(fs: &dyn FileSystem, parts: &[&str]) -> Option { + let mut path = String::new(); + for part in parts { + let dir = if path.is_empty() { "/" } else { path.as_str() }; + let found = fs + .read_dir(dir) + .ok()? + .into_iter() + .find(|e| e.name.eq_ignore_ascii_case(part))?; + path = format!("{path}/{}", found.name); + } + Some(path) +} + +/// The loader an NTFS volume boot record chains to, from the file name +/// embedded in its boot code. +fn vbr_loader(dev: &D) -> Option<&'static str> { + let len = dev.size().min(VBR_LEN) as usize; + let mut buf = vec![0u8; len]; + dev.read_at(0, &mut buf).ok()?; + if contains(&buf, b"BOOTMGR") { + Some("bootmgr") // Vista and newer + } else if contains(&buf, b"NTLDR") { + Some("ntldr") // XP / 2003 + } else { + None + } +} + +/// Messages in the MBR bootstrap Windows (NT 5 through 10) writes. GRUB +/// and syslinux put their own code — and their own strings — there. +const MBR_STRINGS: &[&[u8]] = &[ + b"Invalid partition table", + b"Error loading operating system", + b"Missing operating system", +]; + +/// True if the MBR's 446-byte bootstrap area looks like Windows'. Two of +/// the three messages are enough: the set differs slightly by version. +fn is_windows_bootstrap(sector: &[u8]) -> bool { + let code = §or[..446.min(sector.len())]; + MBR_STRINGS.iter().filter(|s| contains(code, s)).count() >= 2 +} + +fn contains(haystack: &[u8], needle: &[u8]) -> bool { + haystack.len() >= needle.len() && haystack.windows(needle.len()).any(|w| w == needle) +} + +/// Architecture from a PE/COFF binary's machine field — for an EFI +/// executable this is the firmware architecture the guest needs. +fn pe_arch(data: &[u8]) -> Option<&'static str> { + if data.get(0..2)? != b"MZ" { + return None; + } + let pe = u32::from_le_bytes(data.get(0x3c..0x40)?.try_into().ok()?) as usize; + if data.get(pe..pe + 4)? != b"PE\0\0" { + return None; + } + let machine = u16::from_le_bytes(data.get(pe + 4..pe + 6)?.try_into().ok()?); + Some(match machine { + 0x014c => "i386", + 0x8664 => "x86_64", + 0xaa64 => "aarch64", + 0x01c4 => "arm", // ARMNT (Windows RT / 32-bit ARM) + _ => return None, + }) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::boot::tests::MockFs; + + /// Minimal PE: "MZ", the header offset at 0x3c, then "PE\0\0" + machine. + fn pe(machine: u16) -> Vec { + let pe_off = 0x80usize; + let mut v = vec![0u8; pe_off + 8]; + v[0..2].copy_from_slice(b"MZ"); + v[0x3c..0x40].copy_from_slice(&(pe_off as u32).to_le_bytes()); + v[pe_off..pe_off + 4].copy_from_slice(b"PE\0\0"); + v[pe_off + 4..pe_off + 6].copy_from_slice(&machine.to_le_bytes()); + v + } + + #[test] + fn pe_machine_types() { + assert_eq!(pe_arch(&pe(0x8664)), Some("x86_64")); + assert_eq!(pe_arch(&pe(0xaa64)), Some("aarch64")); + assert_eq!(pe_arch(&pe(0x014c)), Some("i386")); + assert_eq!(pe_arch(&pe(0x5678)), None); // unknown machine + assert_eq!(pe_arch(b"not a PE at all"), None); + assert_eq!(pe_arch(&pe(0x8664)[..0x20]), None); // truncated read + } + + #[test] + fn windows_mbr_bootstrap_needs_two_messages() { + let mut sector = [0u8; 512]; + sector[10..33].copy_from_slice(b"Invalid partition table"); + assert!(!is_windows_bootstrap(§or), "one message is not enough"); + sector[40..64].copy_from_slice(b"Missing operating system"); + assert!(is_windows_bootstrap(§or)); + + // A message living outside the bootstrap area (in the partition + // table or the 55aa marker region) must not count. + let mut tail = [0u8; 512]; + tail[446..469].copy_from_slice(b"Invalid partition table"); + tail[470..494].copy_from_slice(b"Missing operating system"); + assert!(!is_windows_bootstrap(&tail)); + } + + #[test] + fn finds_boot_manager_whatever_the_case() { + let fs = MockFs::new([ + ("/EFI/Microsoft/Boot/bootmgfw.efi", "MZ"), + ("/EFI/Microsoft/Boot/BCD", "hive"), + ]); + assert_eq!( + find_path(&fs, BOOT_MANAGER).as_deref(), + Some("/EFI/Microsoft/Boot/bootmgfw.efi") + ); + assert_eq!(find_path(&fs, BCD).as_deref(), Some("/EFI/Microsoft/Boot/BCD")); + + let lower = MockFs::new([("/efi/microsoft/boot/BOOTMGFW.EFI", "MZ")]); + assert_eq!( + find_path(&lower, BOOT_MANAGER).as_deref(), + Some("/efi/microsoft/boot/BOOTMGFW.EFI") + ); + } + + #[test] + fn linux_esp_has_no_boot_manager() { + let fs = MockFs::new([ + ("/EFI/BOOT/BOOTAA64.EFI", "MZ"), + ("/EFI/debian/grub.cfg", "menuentry 'Debian' {}"), + ]); + assert_eq!(find_path(&fs, BOOT_MANAGER), None); + assert_eq!(find_path(&fs, BCD), None); + } + + #[test] + fn chainloader_targets_match_the_boot_manager() { + let found = "/EFI/Microsoft/Boot/bootmgfw.efi"; + assert!(same_file("/EFI/Microsoft/Boot/bootmgfw.efi", found)); + assert!(same_file("/efi/microsoft/boot/bootmgfw.efi", found)); // FAT case + assert!(same_file("\\EFI\\Microsoft\\Boot\\bootmgfw.efi", found)); // BCD style + assert!(same_file("/EFI/MICROSOFT/BOOT/BOOTMGFW.EFI", found)); + assert!(!same_file("/EFI/ubuntu/grubx64.efi", found)); + assert!(!same_file("+1", found)); // chainload a boot sector + } + + #[test] + fn uri_parts() { + assert_eq!(uri(2, "/EFI/x.efi"), "p2:/EFI/x.efi"); + assert_eq!(uri_partition("p12:/EFI/x.efi"), Some(12)); + assert_eq!(uri_partition("/EFI/x.efi"), None); + assert_eq!(path_of("p1:/EFI/x.efi"), "/EFI/x.efi"); + } + + #[test] + fn summary_reports_what_is_known() { + let w = Windows { + firmware: Some(Firmware::Uefi), + arch: Some("x86_64"), + ..Default::default() + }; + assert_eq!(w.summary(), "uefi, x86_64"); + assert_eq!(Windows::default().summary(), "no boot manager found"); + } +} diff --git a/src/lib.rs b/src/lib.rs index be3aa84..7739e94 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,6 +20,7 @@ pub mod boot; pub mod disk; pub mod error; pub mod fsys; +pub mod guest; pub mod kernel; pub mod part; pub mod source; diff --git a/src/main.rs b/src/main.rs index c5b5248..f5b4609 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,12 @@ mod shell; -use anyhow::{bail, Context, Result}; +use anyhow::{anyhow, bail, Context, Result}; use clap::{Parser, Subcommand}; use lbx::blockdev::ReadAt; use lbx::boot::{BootEntry, BootScan, Source}; use lbx::disk::DiskImage; use lbx::fsys::{self, DirEntry, FileKind, FileSystem}; +use lbx::guest::PartitionBoot; use lbx::part::{self, Partition}; use std::collections::HashMap; use std::io::Write; @@ -166,7 +167,7 @@ fn main() -> Result<()> { } Command::Shell { image } => { let disk = open_disk(&image)?; - shell::run(open_filesystems(&disk, None)?) + shell::run(require_filesystems(&disk, None)?) } } } @@ -195,6 +196,9 @@ fn open_disk(image: &str) -> Result> { } /// (partition, filesystem) pairs for every partition we can actually read. +/// An image with nothing readable (a Windows guest: NTFS everywhere) +/// yields an empty list — the boot scanners turn that into a verdict of +/// their own; callers that must read a file use [`require_filesystems`]. fn open_filesystems( disk: &Arc, only_part: Option, @@ -219,6 +223,16 @@ fn open_filesystems( } } } + Ok(out) +} + +/// [`open_filesystems`] for the commands that exist to read a file: with +/// no readable filesystem there is nothing to say but so. +fn require_filesystems( + disk: &Arc, + only_part: Option, +) -> Result)>> { + let out = open_filesystems(disk, only_part)?; if out.is_empty() { bail!( "no readable filesystem found{}", @@ -228,30 +242,51 @@ fn open_filesystems( Ok(out) } -type PartScan = (Partition, Box, BootScan); - /// The partition whose boot entries we trust most: config-derived entries /// (they carry the cmdline) beat fallback directory scans. -fn best_boot_scan(disk: &Arc) -> Result> { +fn best_boot_scan(disk: &Arc) -> Result> { let has_config = |s: &BootScan| s.entries.iter().any(|e| e.source != Source::Fallback); - let mut best: Option = None; - for (p, fs) in open_filesystems(disk, None)? { - let scan = lbx::boot::scan(fs.as_ref())?; - if scan.entries.is_empty() { - continue; - } + let mut best: Option = None; + for pb in lbx::guest::bootable(disk)? { let better = match &best { None => true, - Some((_, _, b)) => has_config(&scan) && !has_config(b), + Some(b) => has_config(&pb.scan) && !has_config(&b.scan), }; if better { - best = Some((p, fs, scan)); + best = Some(pb); } } Ok(best) } +/// The error for an image that boots nothing we can use. Windows volumes +/// are the common reason and the caller can act on knowing that, so say +/// what was seen instead of leaving them with "not found". +fn no_boot_artifacts(disk: &Arc) -> anyhow::Error { + let Ok(Some(w)) = lbx::guest::windows(disk) else { + return anyhow!("no boot artifacts (kernel/initramfs) found in any partition"); + }; + let evidence = w.evidence.join("; "); + match w.firmware { + // Windows files but no boot path: an attached data disk. + None => anyhow!( + "nothing to boot: Windows volumes but no boot manager, so this \ + looks like a data disk rather than a bootable image ({evidence})" + ), + Some(f) => anyhow!( + "nothing to direct-kernel boot: this is a Windows guest ({}) — \ + run the image under {} firmware with the disk attached as-is \ + ({evidence})", + w.summary(), + match f { + lbx::boot::Firmware::Bios => "legacy BIOS", + _ => "UEFI", + } + ), + } +} + fn cmd_info(image: &str) -> Result<()> { let disk = open_disk(image)?; println!("format: {}", disk.format()); @@ -276,6 +311,16 @@ fn cmd_info(image: &str) -> Result<()> { } pub(crate) fn print_entry(part: usize, entry: &BootEntry, fixed: Option<&str>) { + // A Windows entry starts firmware, not a kernel: what it needs is the + // firmware kind, the architecture to run it as, and its boot manager. + if let Some(w) = &entry.windows { + println!(" firmware: {}", w.firmware); + println!(" arch: {}", w.arch.unwrap_or("-")); + if let Some(loader) = &w.loader { + println!(" loader: {loader}"); + } + return; + } if let Some(kernel) = &entry.kernel { println!(" kernel: {}", uri(part, kernel)); } @@ -318,27 +363,51 @@ fn kernel_compression(fs: &dyn FileSystem, entry: &BootEntry) -> Option Result<()> { let disk = open_disk(image)?; - let Some((p, fs, scan)) = best_boot_scan(&disk)? else { - bail!("no boot artifacts (kernel/initramfs) found in any partition"); + let Some(pb) = best_boot_scan(&disk)? else { + return Err(no_boot_artifacts(&disk)); }; - let index = scan.default.unwrap_or(0); - let entry = &scan.entries[index]; + let (part, fs) = (pb.partition.index, pb.fs.as_deref()); + let index = pb.scan.default.unwrap_or(0); + let entry = &pb.scan.entries[index]; let table = part::scan(disk.as_ref())?; let fixed = fixed_cmdline(entry, &table); if json { println!( "{}", - boot_info_json(p.index, entry, fixed.as_deref(), &scan.configs, fs.as_ref()) + boot_info_json(part, entry, fixed.as_deref(), &pb.scan.configs, fs) + ); + return Ok(()); + } + // What the bootloader would boot may be the Windows install, not a + // kernel — on a dual-boot image that is exactly what `set default` + // says. Report it as it is instead of failing to find a kernel. + if let Some(w) = &entry.windows { + println!("type: {}", entry.kind()); + println!("title: {}", entry_label(entry)); + println!("firmware: {}", w.firmware); + println!("arch: {}", w.arch.unwrap_or("-")); + if let Some(loader) = &w.loader { + println!("loader: {loader}"); + } + println!("source: {}", entry.source); + println!( + "note: nothing to direct-kernel boot here; run the image under \ + {} firmware with the disk attached as-is", + match w.firmware { + lbx::boot::Firmware::Bios => "legacy BIOS", + _ => "UEFI", + } ); return Ok(()); } let Some(kernel) = &entry.kernel else { bail!("default entry has no kernel path"); }; - println!("kernel: {}", uri(p.index, kernel)); + println!("type: {}", entry.kind()); + println!("kernel: {}", uri(part, kernel)); for initrd in &entry.initrd { - println!("initrd: {}", uri(p.index, initrd)); + println!("initrd: {}", uri(part, initrd)); } if let Some(cmdline) = &entry.cmdline { println!("cmdline: {cmdline}"); @@ -348,7 +417,7 @@ fn cmd_boot_info(image: &str, json: bool) -> Result<()> { } else { println!("cmdline: (unknown — entry came from {} scan)", entry.source); } - if let Some(c) = kernel_compression(fs.as_ref(), entry) { + if let Some(c) = fs.and_then(|fs| kernel_compression(fs, entry)) { println!("compression: {} (extract with --decompress for direct-kernel boot)", c.label()); } println!("source: {}", entry.source); @@ -375,38 +444,36 @@ fn cmd_entries(image: &str, json: bool, quick: bool) -> Result<()> { let mut found = false; let mut json_items: Vec = Vec::new(); - for (p, fs) in open_filesystems(&disk, None)? { - let scan = lbx::boot::scan(fs.as_ref())?; - if scan.entries.is_empty() { - continue; - } + for pb in lbx::guest::bootable(&disk)? { + let (index, scan, fs) = (pb.partition.index, &pb.scan, pb.fs.as_deref()); found = true; let mut dma_cache = HashMap::new(); if json { for (i, entry) in scan.entries.iter().enumerate() { let fixed = fixed_cmdline(entry, &table); - let dma = dma_cached(&mut dma_cache, fs.as_ref(), entry); + let dma = fs.and_then(|fs| dma_cached(&mut dma_cache, fs, entry)); json_items.push(entry_json( - p.index, + index, Some(i) == scan.default, entry, fixed.as_deref(), - fs.as_ref(), + fs, dma, quick, )); } continue; } - println!("partition {} ({}):", p.index, fs.fs_type()); + println!("partition {} ({}):", index, pb.fs_type); for (i, entry) in scan.entries.iter().enumerate() { let mark = if Some(i) == scan.default { "*" } else { " " }; println!("{mark} [{}] {} ({})", i + 1, entry_label(entry), entry.source); - print_entry(p.index, entry, fixed_cmdline(entry, &table).as_deref()); - if let Some(c) = kernel_compression(fs.as_ref(), entry) { + print_entry(index, entry, fixed_cmdline(entry, &table).as_deref()); + let Some(fs) = fs else { continue }; + if let Some(c) = kernel_compression(fs, entry) { println!(" note: compressed kernel ({}); extract with --decompress for direct-kernel boot", c.label()); } - if dma_cached(&mut dma_cache, fs.as_ref(), entry) == Some(false) { + if dma_cached(&mut dma_cache, fs, entry) == Some(false) { println!(" note: kernel lacks CONFIG_DMA_RESTRICTED_POOL; virtio fails under a protected VM"); } } @@ -416,7 +483,7 @@ fn cmd_entries(image: &str, json: bool, quick: bool) -> Result<()> { return Ok(()); } if !found { - bail!("no boot entries found in any partition"); + return Err(no_boot_artifacts(&disk)); } Ok(()) } @@ -448,7 +515,7 @@ fn cmd_ls(image: &str, path: &str, part: Option) -> Result<()> { let disk = open_disk(image)?; let (uri_part, path) = parse_uri(path); let part = uri_part.or(part); - for (p, fs) in open_filesystems(&disk, part)? { + for (p, fs) in require_filesystems(&disk, part)? { let Ok(entries) = fs.read_dir(&path) else { continue; }; @@ -483,7 +550,7 @@ fn read_by_uri( ) -> Result> { let (uri_part, path) = parse_uri(path); let part = uri_part.or(part); - for (_, fs) in open_filesystems(disk, part)? { + for (_, fs) in require_filesystems(disk, part)? { if let Ok(data) = fs.read_file(&path) { return Ok(data); } @@ -551,9 +618,10 @@ fn cmd_extract( decompress: bool, ) -> Result<()> { let disk = open_disk(image)?; - let Some((p, fs, scan)) = best_boot_scan(&disk)? else { - bail!("no boot artifacts (kernel/initramfs) found in any partition"); + let Some(pb) = best_boot_scan(&disk)? else { + return Err(no_boot_artifacts(&disk)); }; + let (p, scan) = (&pb.partition, &pb.scan); let index = match entry_arg { Some(n) if n >= 1 && n <= scan.entries.len() => n - 1, @@ -561,13 +629,25 @@ fn cmd_extract( None => scan.default.unwrap_or(0), }; let entry = &scan.entries[index]; + if let Some(w) = &entry.windows { + bail!( + "entry {} is a Windows install ({}), not a kernel — there is \ + nothing to extract; run the image under firmware, or pick a \ + Linux entry with --entry N (`lbx entries` lists them)", + index + 1, + w.firmware + ); + } + let Some(fs) = pb.fs.as_deref() else { + bail!("partition {} has no readable filesystem", p.index); + }; let Some(kernel) = &entry.kernel else { bail!("selected entry has no kernel path"); }; let table = part::scan(disk.as_ref())?; let fixed = fixed_cmdline(entry, &table); - println!("partition {} ({}), entry {} ({}):", p.index, fs.fs_type(), index + 1, entry.source); + println!("partition {} ({}), entry {} ({}):", p.index, pb.fs_type, index + 1, entry.source); print_entry(p.index, entry, fixed.as_deref()); for cfg in &scan.configs { println!(" config: {}", uri(p.index, cfg)); @@ -642,7 +722,9 @@ fn entry_json( is_default: bool, e: &BootEntry, fixed: Option<&str>, - fs: &dyn FileSystem, + // `None` for a partition we can only sniff (a Windows entry on NTFS): + // every per-entry file probe below is then skipped. + fs: Option<&dyn FileSystem>, dma_restricted_pool: Option, quick: bool, ) -> String { @@ -654,16 +736,14 @@ fn entry_json( // File sizes come from inode metadata only (no data read); a VMM // caching extracted files keys on URI + size to skip re-extraction. // `quick` (URL preview) skips them — a plain listing never uses them. - let kernel_size = if quick { - "null".into() - } else { - jsize(e.kernel.as_ref().and_then(|k| fs.file_size(k).ok())) + let kernel_size = match fs { + Some(fs) if !quick => jsize(e.kernel.as_ref().and_then(|k| fs.file_size(k).ok())), + _ => "null".into(), }; let initrd: Vec = e.initrd.iter().map(|i| jstr(&uri(part, i))).collect(); - let initrd_size: Vec = if quick { - e.initrd.iter().map(|_| "null".to_string()).collect() - } else { - e.initrd.iter().map(|i| jsize(fs.file_size(i).ok())).collect() + let initrd_size: Vec = match fs { + Some(fs) if !quick => e.initrd.iter().map(|i| jsize(fs.file_size(i).ok())).collect(), + _ => e.initrd.iter().map(|_| "null".to_string()).collect(), }; // Whether this kernel can do virtio in a gunyah protected VM (needs // CONFIG_DMA_RESTRICTED_POOL); null = could not determine. Resolved by @@ -674,15 +754,25 @@ fn entry_json( // Compression wrapping the kernel (gzip/zboot), so a direct-boot caller // knows to extract it with `--decompress`; null = already raw. `quick` // skips the prefix read (the URL preview doesn't direct-boot). - let kernel_compression = if quick { - "null".into() - } else { - kernel_compression(fs, e) + let kernel_compression = match fs { + Some(fs) if !quick => kernel_compression(fs, e) .map(|c| jstr(&c.label())) - .unwrap_or_else(|| "null".into()) + .unwrap_or_else(|| "null".into()), + _ => "null".into(), + }; + // Windows entries: what starting this one takes, in place of a kernel. + // Null on a Linux entry, as `kernel`/`cmdline` are null on a Windows one. + let (firmware, arch, loader) = match &e.windows { + Some(w) => ( + jstr(&w.firmware.to_string()), + w.arch.map(jstr).unwrap_or_else(|| "null".into()), + jopt(&w.loader), + ), + None => ("null".into(), "null".into(), "null".into()), }; format!( - "{{\"partition\":{part},\"default\":{is_default},\"source\":{},\"title\":{},\"version\":{},\"id\":{},\"kernel\":{kernel},\"kernel_size\":{kernel_size},\"initrd\":[{}],\"initrd_size\":[{}],\"cmdline\":{},\"cmdline_fixed\":{},\"dma_restricted_pool\":{dma_restricted_pool},\"kernel_compression\":{kernel_compression}}}", + "{{\"partition\":{part},\"default\":{is_default},\"type\":{},\"source\":{},\"title\":{},\"version\":{},\"id\":{},\"kernel\":{kernel},\"kernel_size\":{kernel_size},\"initrd\":[{}],\"initrd_size\":[{}],\"cmdline\":{},\"cmdline_fixed\":{},\"dma_restricted_pool\":{dma_restricted_pool},\"kernel_compression\":{kernel_compression},\"firmware\":{firmware},\"arch\":{arch},\"loader\":{loader}}}", + jstr(&e.kind().to_string()), jstr(&e.source.to_string()), jopt(&e.title), jopt(&e.version), @@ -699,9 +789,9 @@ fn boot_info_json( e: &BootEntry, fixed: Option<&str>, configs: &[String], - fs: &dyn FileSystem, + fs: Option<&dyn FileSystem>, ) -> String { - let dma = lbx::boot::dma_restricted_pool(fs, e); + let dma = fs.and_then(|fs| lbx::boot::dma_restricted_pool(fs, e)); let entry = entry_json(part, true, e, fixed, fs, dma, false); let configs: Vec = configs.iter().map(|c| jstr(&uri(part, c))).collect(); format!( diff --git a/src/part/gpt.rs b/src/part/gpt.rs index dadaa65..7891b3a 100644 --- a/src/part/gpt.rs +++ b/src/part/gpt.rs @@ -13,6 +13,15 @@ const LINUX_SWAP: &str = "0657fd6d-a4ab-43c4-84e5-0933c84b4f4f"; const LINUX_ROOT_X86_64: &str = "4f68bce3-e8cd-4db1-96e7-fbcaf984b709"; const LINUX_ROOT_ARM64: &str = "b921b045-1df0-41c3-af44-4c6f280d3fae"; const BIOS_BOOT: &str = "21686148-6449-6e6f-744e-656564454649"; +// Microsoft. Only Windows creates MSR/WinRE/LDM/Storage-Spaces partitions, +// so `guest` reads them as evidence of a Windows guest; "basic data" is +// weaker (it is also just the generic "some filesystem" type). +pub const MS_RESERVED: &str = "e3c9e316-0b5c-4db8-817d-f92df00215ae"; +pub const MS_BASIC_DATA: &str = "ebd0a0a2-b9e5-4433-87c0-68b6b72699c7"; +pub const WINDOWS_RECOVERY: &str = "de94bba4-06d1-4d40-a16a-bfd50179d6ac"; +pub const MS_LDM_METADATA: &str = "5808c8aa-7e8f-42e0-85d2-e1e90434cfb3"; +pub const MS_LDM_DATA: &str = "af9b60a0-1431-4f62-bc68-3311714a69ad"; +pub const MS_STORAGE_SPACES: &str = "e75caf8f-f680-4cee-afa3-b001e56efc2d"; fn type_name(guid: &str) -> (&'static str, bool) { match guid { @@ -21,6 +30,12 @@ fn type_name(guid: &str) -> (&'static str, bool) { LINUX_XBOOTLDR => ("Linux extended boot", true), LINUX_ROOT_X86_64 => ("Linux root (x86-64)", true), LINUX_ROOT_ARM64 => ("Linux root (ARM64)", true), + MS_BASIC_DATA => ("Microsoft basic data", true), // ESP-less Windows keeps nothing here we read, but a plain FAT data partition might hold configs + MS_RESERVED => ("Microsoft reserved", false), + WINDOWS_RECOVERY => ("Windows recovery", false), // NTFS + MS_LDM_METADATA => ("Microsoft LDM metadata", false), + MS_LDM_DATA => ("Microsoft LDM data", false), + MS_STORAGE_SPACES => ("Microsoft Storage Spaces", false), LINUX_LVM => ("Linux LVM", false), // probe once LVM support lands LINUX_SWAP => ("Linux swap", false), BIOS_BOOT => ("BIOS boot", false), @@ -91,7 +106,7 @@ pub fn scan(disk: &D) -> Result> { let guid = format_guid(type_guid); let (kind, probe_worthy) = type_name(&guid); - let kind = if kind == "unknown" { guid } else { kind.to_string() }; + let kind = if kind == "unknown" { guid.clone() } else { kind.to_string() }; let unique_guid = &e[16..32]; let part_uuid = if unique_guid.iter().all(|&b| b == 0) { @@ -105,6 +120,7 @@ pub fn scan(disk: &D) -> Result> { start_byte: first_lba * SECTOR, size_bytes: (last_lba - first_lba + 1) * SECTOR, kind, + type_id: guid, name, probe_worthy, part_uuid, diff --git a/src/part/mbr.rs b/src/part/mbr.rs index bbe2a77..fea60cd 100644 --- a/src/part/mbr.rs +++ b/src/part/mbr.rs @@ -7,10 +7,18 @@ const TYPE_EXTENDED_CHS: u8 = 0x05; const TYPE_EXTENDED_LBA: u8 = 0x0f; const TYPE_GPT_PROTECTIVE: u8 = 0xee; +/// MBR type byte in the same textual form `Partition::type_id` uses for a +/// GPT type GUID, so callers can match one field for either table. +pub(crate) fn type_id(t: u8) -> String { + format!("{t:#04x}") +} + fn type_name(t: u8) -> (String, bool) { match t { + 0x07 => ("NTFS/exFAT".into(), false), // also OS/2 IFS; the fs sniffer decides 0x0b | 0x0c => ("FAT32".into(), true), 0x0e => ("FAT16 LBA".into(), true), + 0x27 => ("Windows recovery".into(), false), // hidden NTFS (WinRE) 0x82 => ("Linux swap".into(), false), 0x83 => ("Linux".into(), true), 0x8e => ("Linux LVM".into(), false), // probe once LVM support lands @@ -86,6 +94,7 @@ pub fn scan(disk: &D) -> Result> { start_byte: e.start_lba as u64 * SECTOR, size_bytes: e.sectors as u64 * SECTOR, kind, + type_id: type_id(e.type_byte), name: None, probe_worthy, part_uuid: part_uuid(i + 1), @@ -113,6 +122,7 @@ pub fn scan(disk: &D) -> Result> { start_byte: ebr_offset + data.start_lba as u64 * SECTOR, size_bytes: data.sectors as u64 * SECTOR, kind, + type_id: type_id(data.type_byte), name: None, probe_worthy, part_uuid: part_uuid(index), diff --git a/src/part/mod.rs b/src/part/mod.rs index 579e980..148513b 100644 --- a/src/part/mod.rs +++ b/src/part/mod.rs @@ -7,6 +7,16 @@ mod gpt; mod mbr; +/// Well-known GPT partition type GUIDs, in the form [`Partition::type_id`] +/// carries — for callers that classify partitions by type (see +/// [`crate::guest`]). +pub mod gpt_type { + pub use super::gpt::{ + MS_BASIC_DATA, MS_LDM_DATA, MS_LDM_METADATA, MS_RESERVED, MS_STORAGE_SPACES, + WINDOWS_RECOVERY, + }; +} + use crate::blockdev::{ReadAt, Slice}; use crate::Result; use std::sync::Arc; @@ -29,6 +39,10 @@ pub struct Partition { pub size_bytes: u64, /// Human-readable type, e.g. "EFI System", "Linux filesystem", "0x83". pub kind: String, + /// Machine-readable type as the table stores it: the GPT type GUID, or + /// `0xNN` for an MBR type byte. Empty for the whole-disk pseudo-entry. + /// Callers match on this (see `guest`) rather than on `kind`'s wording. + pub type_id: String, /// GPT partition name, if any. pub name: Option, /// True for types worth probing for boot files (ESP, Linux, XBOOTLDR). @@ -68,6 +82,7 @@ pub fn scan(disk: &D) -> Result { start_byte: 0, size_bytes: disk.size(), kind: "whole disk".into(), + type_id: String::new(), name: None, probe_worthy: true, part_uuid: None, diff --git a/src/vdafix.rs b/src/vdafix.rs index 514496b..8be15a8 100644 --- a/src/vdafix.rs +++ b/src/vdafix.rs @@ -95,6 +95,7 @@ mod tests { start_byte: 0, size_bytes: 0, kind: "Linux filesystem".into(), + type_id: String::new(), name: None, probe_worthy: true, part_uuid: uuid.map(str::to_string),