use std::path::{Path, PathBuf};
use crate::backend::iso_reader::IsoReader;
const SECTOR: u64 = 512;
pub const FAT32_MAX_FILE: u64 = 4 * 1024 * 1024 * 1024 - 1;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ImageFormat {
Iso9660,
Udf,
IsoHybrid {
has_gpt: bool,
has_mbr: bool,
},
PartitionedDisk {
table: PartitionTableKind,
},
Raw,
Unknown,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PartitionTableKind {
Mbr,
Gpt,
None,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PartitionProbe {
pub index: u32,
pub table: PartitionTableKind,
pub fs_hint: String,
pub start_lba: u64,
pub size: u64,
pub boot_hints: Vec<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OsClass {
Windows {
is_installer: bool,
boot_wim: bool,
install_wim_size: Option<u64>,
},
Linux(LinuxFlavor),
PartitionedImage {
partitions: Vec<PartitionProbe>,
},
Unknown,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LinuxFlavor {
DebianInstaller,
UbuntuCasper,
DebianLive,
ArchLike,
NixOs,
GrubInternal,
Unknown,
}
impl LinuxFlavor {
pub fn label(self) -> &'static str {
match self {
LinuxFlavor::DebianInstaller => "Debian Installer",
LinuxFlavor::UbuntuCasper => "Ubuntu/Casper",
LinuxFlavor::DebianLive => "Debian Live",
LinuxFlavor::ArchLike => "Arch",
LinuxFlavor::NixOs => "NixOS",
LinuxFlavor::GrubInternal => "GRUB",
LinuxFlavor::Unknown => "Linux",
}
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct ProbeOpts {
pub allow_loop_mount: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ProvisioningPlan {
LoopbackGrub { stanza: String },
LoopbackPartition { stanza: String },
WindowsExtract { stanza: String, plan: WindowsPlan },
GenericFallback { stanza: String },
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WindowsPlan {
ExtractAllToFat32,
SplitWim { size: u64 },
ExtractBootOnly,
}
#[derive(Debug, Clone, PartialEq)]
pub struct InspectedImage {
pub path: PathBuf,
pub format: ImageFormat,
pub os: OsClass,
}
impl InspectedImage {
pub fn provisioning(&self) -> ProvisioningPlan {
crate::backend::grub_gen::plan_for(self)
}
}
fn read_sector(file: &mut std::fs::File, lba: u64, out: &mut [u8; 512]) -> std::io::Result<()> {
use std::io::{Read, Seek, SeekFrom};
file.seek(SeekFrom::Start(lba * SECTOR))?;
file.read_exact(out)
}
fn mbr_has_real_partitions(mbr: &[u8; 512]) -> bool {
for entry in 0..4 {
let base = 446 + entry * 16;
let ptype = mbr[base + 4];
if ptype != 0 && ptype != 0xEE {
return true;
}
}
false
}
pub fn probe_format(path: &Path) -> anyhow::Result<ImageFormat> {
use std::io::{Read, Seek, SeekFrom};
let mut f = std::fs::File::open(path)?;
let mut s0 = [0u8; 512];
let mut s1 = [0u8; 512];
let mut pvd = [0u8; 512];
read_sector(&mut f, 0, &mut s0)?;
read_sector(&mut f, 1, &mut s1)?;
f.seek(SeekFrom::Start(16 * 2048))?;
f.read_exact(&mut pvd)?;
let is_gpt = &s1[0..8] == b"EFI PART";
let protective_mbr = s0[450] == 0xEE;
let mbr_sig = s0[510] == 0x55 && s0[511] == 0xAA;
let has_mbr_partitions = mbr_sig && mbr_has_real_partitions(&s0);
let is_iso = &pvd[1..6] == b"CD001";
let is_udf = &pvd[0..5] == b"BEA01" || &pvd[1..6] == b"NSR02" || &pvd[1..6] == b"NSR03";
if is_iso || is_udf {
if protective_mbr || is_gpt || has_mbr_partitions {
return Ok(ImageFormat::IsoHybrid {
has_gpt: is_gpt,
has_mbr: has_mbr_partitions,
});
}
return Ok(if is_udf && !is_iso {
ImageFormat::Udf
} else {
ImageFormat::Iso9660
});
}
if is_gpt || protective_mbr {
return Ok(ImageFormat::PartitionedDisk {
table: PartitionTableKind::Gpt,
});
}
if has_mbr_partitions {
return Ok(ImageFormat::PartitionedDisk {
table: PartitionTableKind::Mbr,
});
}
Ok(ImageFormat::Raw)
}
pub fn inspect(path: &Path, _opts: &ProbeOpts) -> anyhow::Result<InspectedImage> {
let format = probe_format(path)?;
let os = match format {
ImageFormat::Iso9660 | ImageFormat::IsoHybrid { .. } | ImageFormat::Udf => {
classify_iso(path, format)?
}
ImageFormat::PartitionedDisk { table } => classify_partitioned(path, table)?,
_ => OsClass::Unknown,
};
Ok(InspectedImage {
path: path.to_path_buf(),
format,
os,
})
}
fn classify_iso(path: &Path, format: ImageFormat) -> anyhow::Result<OsClass> {
if format == ImageFormat::Udf {
return Ok(OsClass::Unknown);
}
let mut reader = IsoReader::open(path)?;
let paths = reader.list_paths()?;
if let Some(win) = detect_windows(&paths, &mut reader)? {
return Ok(OsClass::Windows {
is_installer: win.0,
boot_wim: win.1,
install_wim_size: win.2,
});
}
Ok(OsClass::Linux(detect_linux(&paths)))
}
fn detect_windows(
entries: &[String],
reader: &mut IsoReader,
) -> anyhow::Result<Option<(bool, bool, Option<u64>)>> {
let lower: Vec<String> = entries.iter().map(|e| e.to_ascii_lowercase()).collect();
let has = |needle: &str| lower.iter().any(|e| e.contains(needle));
let bootmgr = has("/bootmgr.efi");
let bootmgfw = has("/efi/microsoft/boot/bootmgfw.efi");
let bootx64 = has("/efi/boot/bootx64.efi");
let boot_wim = has("/sources/boot.wim");
let install_wim = has("/sources/install.wim");
let install_esd = has("/sources/install.esd");
let is_windows = bootmgr
|| bootmgfw
|| (bootx64 && boot_wim)
|| (boot_wim && install_wim)
|| (boot_wim && install_esd);
if !is_windows {
return Ok(None);
}
let is_installer = install_wim || install_esd;
let install_wim_size = if install_wim {
reader.find("/sources/install.wim")?.map(|e| e.len as u64)
} else if install_esd {
reader.find("/sources/install.esd")?.map(|e| e.len as u64)
} else {
None
};
Ok(Some((is_installer, boot_wim, install_wim_size)))
}
fn detect_linux(entries: &[String]) -> LinuxFlavor {
let lower: Vec<String> = entries.iter().map(|e| e.to_ascii_lowercase()).collect();
let has = |n: &str| lower.iter().any(|e| e.contains(n));
if has("/install.amd/vmlinuz") && has("/install.amd/initrd.gz") {
LinuxFlavor::DebianInstaller
} else if has("/casper/vmlinuz") && has("/casper/initrd") {
LinuxFlavor::UbuntuCasper
} else if has("/live/vmlinuz") && has("/live/initrd.img") {
LinuxFlavor::DebianLive
} else if has("/arch/boot/x86_64/vmlinuz-linux") {
LinuxFlavor::ArchLike
} else if has("/boot/bzimage") && has("/boot/initrd") {
LinuxFlavor::NixOs
} else if has("/boot/grub/grub.cfg") || has("/efi/boot/grub.cfg") {
LinuxFlavor::GrubInternal
} else {
LinuxFlavor::Unknown
}
}
fn classify_partitioned(path: &Path, table: PartitionTableKind) -> anyhow::Result<OsClass> {
let mut file = std::fs::OpenOptions::new()
.read(true)
.write(true)
.open(path)?;
let partitions = match table {
PartitionTableKind::Gpt => read_gpt_partitions(&mut file)?,
PartitionTableKind::Mbr => read_mbr_partitions(&mut file)?,
PartitionTableKind::None => Vec::new(),
};
let mut probes = Vec::new();
for (index, start_lba, size_sectors) in partitions {
let size = size_sectors * SECTOR;
let fs_hint = probe_partition_fs(&mut file, start_lba);
let mut boot_hints = Vec::new();
if fs_hint == "fat32" || fs_hint == "fat16" || fs_hint == "fat12" {
boot_hints = list_fat_boot_hints(&mut file, start_lba, size);
}
probes.push(PartitionProbe {
index,
table,
fs_hint,
start_lba,
size,
boot_hints,
});
}
if probes.is_empty() {
Ok(OsClass::Unknown)
} else {
Ok(OsClass::PartitionedImage { partitions: probes })
}
}
fn read_gpt_partitions(file: &mut std::fs::File) -> anyhow::Result<Vec<(u32, u64, u64)>> {
use std::io::{Read, Seek, SeekFrom};
let mut header = [0u8; 512];
file.seek(SeekFrom::Start(SECTOR))?;
file.read_exact(&mut header)?;
if &header[0..8] != b"EFI PART" {
return Ok(Vec::new());
}
let entries_lba = u64::from_le_bytes(header[72..80].try_into()?);
let num_entries = u32::from_le_bytes(header[80..84].try_into()?);
let entry_size = u32::from_le_bytes(header[84..88].try_into()?);
let mut out = Vec::new();
let mut index = 0u32;
for i in 0..num_entries {
let entry_offset = entries_lba * SECTOR + i as u64 * entry_size as u64;
let mut entry = vec![0u8; entry_size as usize];
file.seek(SeekFrom::Start(entry_offset))?;
file.read_exact(&mut entry)?;
let first_lba = u64::from_le_bytes(entry[32..40].try_into()?);
let last_lba = u64::from_le_bytes(entry[40..48].try_into()?);
if first_lba == 0 && last_lba == 0 {
continue;
}
index += 1;
let size = last_lba.saturating_sub(first_lba) + 1;
out.push((index, first_lba, size));
}
Ok(out)
}
fn read_mbr_partitions(file: &mut std::fs::File) -> anyhow::Result<Vec<(u32, u64, u64)>> {
use std::io::{Read, Seek, SeekFrom};
let mut mbr = [0u8; 512];
file.seek(SeekFrom::Start(0))?;
file.read_exact(&mut mbr)?;
if mbr[510] != 0x55 || mbr[511] != 0xAA {
return Ok(Vec::new());
}
let mut out = Vec::new();
for entry in 0..4 {
let base = 446 + entry * 16;
let ptype = mbr[base + 4];
if ptype == 0 || ptype == 0xEE {
continue;
}
let first_lba = u32::from_le_bytes(mbr[base + 8..base + 12].try_into()?) as u64;
let size = u32::from_le_bytes(mbr[base + 12..base + 16].try_into()?) as u64;
out.push((entry as u32 + 1, first_lba, size));
}
Ok(out)
}
fn probe_partition_fs(file: &mut std::fs::File, start_lba: u64) -> String {
use std::io::{Read, Seek, SeekFrom};
let mut boot = [0u8; 4096];
let n = file
.seek(SeekFrom::Start(start_lba * SECTOR))
.and_then(|_| file.read(&mut boot))
.unwrap_or(0);
if n < 512 {
return "?".to_string();
}
if boot[510] == 0x55 && boot[511] == 0xAA {
let type_str = &boot[82..90];
if type_str.starts_with(b"FAT32") {
return "fat32".to_string();
}
if type_str.starts_with(b"FAT16") {
return "fat16".to_string();
}
if type_str.starts_with(b"FAT12") {
return "fat12".to_string();
}
return "fat".to_string();
}
if &boot[3..11] == b"NTFS " {
return "ntfs".to_string();
}
if &boot[3..11] == b"EXFAT " {
return "exfat".to_string();
}
if n > 1082 && boot[1080] == 0x53 && boot[1081] == 0xEF {
return "ext".to_string();
}
"?".to_string()
}
fn list_fat_boot_hints(file: &mut std::fs::File, start_lba: u64, size: u64) -> Vec<String> {
let offset = start_lba * SECTOR;
let mut wrapper = OffsetReader::new(file, offset, size);
let Ok(fs) = fatfs::FileSystem::new(&mut wrapper, fatfs::FsOptions::new()) else {
return Vec::new();
};
let root = fs.root_dir();
let mut out = Vec::new();
collect_fat_paths(&root, "/", &mut out, 0);
out.into_iter()
.filter(|p| !p.ends_with('/'))
.filter(|p| {
let l = p.to_ascii_lowercase();
l.ends_with("grub.cfg")
|| l.contains("bootmgr")
|| l.contains("vmlinuz")
|| l.contains("initrd")
})
.collect()
}
fn collect_fat_paths(
dir: &fatfs::Dir<&mut OffsetReader<'_>>,
prefix: &str,
out: &mut Vec<String>,
depth: u32,
) {
if depth > 8 {
return;
}
for entry in dir.iter().flatten() {
let name = entry.file_name();
if name == "." || name == ".." {
continue;
}
let path = format!("{prefix}{name}");
if entry.is_dir() {
out.push(format!("{path}/"));
let sub = entry.to_dir();
collect_fat_paths(&sub, &format!("{path}/"), out, depth + 1);
} else {
out.push(path);
}
}
}
struct OffsetReader<'a> {
inner: &'a mut std::fs::File,
offset: u64,
size: u64,
pos: u64,
}
impl<'a> OffsetReader<'a> {
fn new(inner: &'a mut std::fs::File, offset: u64, size: u64) -> Self {
Self {
inner,
offset,
size,
pos: 0,
}
}
}
impl std::io::Read for OffsetReader<'_> {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
use std::io::{Seek, SeekFrom};
let remaining = self.size.saturating_sub(self.pos);
let n = (buf.len() as u64).min(remaining) as usize;
if n == 0 {
return Ok(0);
}
self.inner.seek(SeekFrom::Start(self.offset + self.pos))?;
let read = self.inner.read(&mut buf[..n])?;
self.pos += read as u64;
Ok(read)
}
}
impl std::io::Write for OffsetReader<'_> {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
use std::io::{Seek, SeekFrom};
let remaining = self.size.saturating_sub(self.pos);
let n = (buf.len() as u64).min(remaining) as usize;
self.inner.seek(SeekFrom::Start(self.offset + self.pos))?;
let written = self.inner.write(&buf[..n])?;
self.pos += written as u64;
Ok(written)
}
fn flush(&mut self) -> std::io::Result<()> {
self.inner.flush()
}
}
impl std::io::Seek for OffsetReader<'_> {
fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> {
use std::io::SeekFrom;
let new = match pos {
SeekFrom::Start(n) => n as i128,
SeekFrom::Current(n) => self.pos as i128 + n as i128,
SeekFrom::End(n) => self.size as i128 + n as i128,
};
if new < 0 || new > self.size as i128 {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"seek out of bounds",
));
}
self.pos = new as u64;
Ok(self.pos)
}
}
pub fn windows_provision_plan(os: &OsClass) -> WindowsPlan {
if let OsClass::Windows {
install_wim_size: Some(size),
..
} = os
{
if *size <= FAT32_MAX_FILE {
WindowsPlan::ExtractAllToFat32
} else {
WindowsPlan::SplitWim { size: *size }
}
} else {
WindowsPlan::ExtractBootOnly
}
}