diff --git a/README.md b/README.md index 5b026fd..ca16f5f 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,8 @@ What's in the image, and why: has it ask for the one arm64 guests use (245), and patches what the driver and its Metal plugin hardcode, so Metal offers the GPU families GPU-accelerated Chrome needs, and D32S8 depth/stencil textures. `--no-kexts` leaves them out, and the boot argument `-reimsvgpuoff` - turns the kext off. There are no others: QEMU's `isa-applesmc` is the SMC. + turns the kext off. The apv-arm64-port kexts load after these, on macOS 26.4 and later + ([below](#apv-arm64-port)). There are no others: QEMU's `isa-applesmc` is the SMC. - **The drivers a VM boots with**: OpenRuntime, OpenHfsPlus (recovery images and installers are HFS+), OpenPartitionDxe (for older recovery images) and ResetNvramEntry. - **The graphical picker**, OpenCanopy, with the GoldenGate icons. Pick with the arrow keys, Tab @@ -191,6 +192,12 @@ make LILU=path/to/Lilu.kext KERNEL_SDK=path/to/MacKernelSDK source builds the same bytes. `make` also writes `kext/sources.sha256`, what the kext was built from, and `cargo test` fails when the source no longer matches it. +## apv-arm64-port + +apv-arm64-port drives reims-vgpu's GPU on macOS 26.4 and later with kexts of its own, which every +image carries by default; older macOS 26 builds keep macOS's own driver with ReimsVgpu.kext. +`--no-apv-arm64-port` makes an image without them. + ## License reims-util is licensed under GPL-2.0-or-later OR LGPL-3.0-or-later; see [LICENSE](LICENSE). The diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..9cf0066 --- /dev/null +++ b/build.rs @@ -0,0 +1,105 @@ +//! Embeds the apv-arm64-port kexts, the seven bundles in kext/ that `SET` names, so the image +//! carries them as it carries ReimsVgpu.kext (src/opencore/apv_arm64_port.rs). Writes +//! `$OUT_DIR/apv_arm64_port.rs`: each file of each of them, by its path under kext/, with its +//! bytes, dotfiles left out. Nothing else in kext/ is read. With none of the seven there, nothing +//! is embedded; with only some, the build fails, as the seven load as one. +//! +//! The bundles are built without a path of the machine they were built on: the build fails when +//! an embedded file holds `/Users/`, as an unstripped kext's debug map does. + +use std::fmt::Write as _; +use std::path::{Path, PathBuf}; + +include!("src/opencore/apv_arm64_port_set.rs"); + +fn main() { + if let Err(e) = run() { + eprintln!("apv-arm64-port: {e}"); + std::process::exit(1); + } +} + +fn run() -> Result<(), String> { + let root = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").map_err(|e| e.to_string())?); + let dir = root.join("kext"); + println!("cargo:rerun-if-changed=build.rs"); + println!("cargo:rerun-if-changed=src/opencore/apv_arm64_port_set.rs"); + println!("cargo:rerun-if-changed=kext"); + + let present: Vec<&str> = SET + .iter() + .copied() + .filter(|b| dir.join(b).exists()) + .collect(); + if !present.is_empty() && present.len() != SET.len() { + let missing: Vec<&str> = SET + .iter() + .copied() + .filter(|b| !present.contains(b)) + .collect(); + return Err(format!( + "partial apv-arm64-port set in kext/, without {}: deploy all seven", + missing.join(", ") + )); + } + let mut files = Vec::new(); + for b in &present { + walk(&dir, Path::new(b), &mut files)?; + } + files.sort(); + + let mut code = String::from( + "/// Each file of the apv-arm64-port kexts, by its path under kext/ (build.rs).\n\ + pub static FILES: &[(&str, &[u8])] = &[\n", + ); + for f in &files { + let path = dir.join(f); + println!("cargo:rerun-if-changed={}", path.display()); + let bytes = std::fs::read(&path).map_err(|e| format!("read {}: {e}", path.display()))?; + if bytes.windows(7).any(|w| w == b"/Users/") { + return Err(format!( + "kext/{f} holds a /Users/ path, from the machine it was built on: deploy a \ + stripped build" + )); + } + let abs = path + .to_str() + .ok_or(format!("{} is not UTF-8", path.display()))?; + let _ = writeln!(code, " ({f:?}, include_bytes!({abs:?})),"); + } + code.push_str("];\n"); + let out = PathBuf::from(std::env::var("OUT_DIR").map_err(|e| e.to_string())?); + std::fs::write(out.join("apv_arm64_port.rs"), code) + .map_err(|e| format!("write apv_arm64_port.rs: {e}")) +} + +fn read_dir(dir: &Path) -> Result, String> { + let mut v = std::fs::read_dir(dir) + .and_then(|r| r.collect::, _>>()) + .map_err(|e| format!("read {}: {e}", dir.display()))?; + v.sort_by_key(|e| e.file_name()); + Ok(v) +} + +/// Adds the files under `dir/rel`, by their path under `dir`, skipping dotfiles. +fn walk(dir: &Path, rel: &Path, files: &mut Vec) -> Result<(), String> { + let path = dir.join(rel); + let t = std::fs::symlink_metadata(&path) + .map_err(|e| format!("read {}: {e}", path.display()))? + .file_type(); + if t.is_dir() { + for e in read_dir(&path)? { + if !e.file_name().to_string_lossy().starts_with('.') { + walk(dir, &rel.join(e.file_name()), files)?; + } + } + } else if t.is_file() { + let s = rel + .to_str() + .ok_or(format!("{} is not UTF-8", rel.display()))?; + files.push(s.to_string()); + } else { + return Err(format!("{} is neither a file nor a folder", rel.display())); + } + Ok(()) +} diff --git a/kext/APVDisplayPort.kext/Contents/Info.plist b/kext/APVDisplayPort.kext/Contents/Info.plist new file mode 100644 index 0000000..053771b --- /dev/null +++ b/kext/APVDisplayPort.kext/Contents/Info.plist @@ -0,0 +1,69 @@ + + + + + BuildMachineOSBuild + 23A344017 + CFBundleDevelopmentRegion + en + CFBundleExecutable + APVDisplayPort + CFBundleIdentifier + com.apple.driver.APVDisplayPort + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + com.apple.driver.APVDisplayPort + CFBundlePackageType + KEXT + CFBundleShortVersionString + 15.0.0 + CFBundleSupportedPlatforms + + MacOSX + + CFBundleVersion + 15.0.0 + DTCompiler + com.apple.compilers.llvm.clang.1_0 + DTPlatformBuild + + DTPlatformName + macosx + DTPlatformVersion + 26.7 + DTSDKBuild + 25G221 + DTSDKName + macosx26.7.internal + DTXcode + 2630 + DTXcodeBuild + 17E6107 + LSMinimumSystemVersion + 26.7 + NSHumanReadableCopyright + Copyright © 2018-2025 Apple Inc. All rights reserved. + OSBundleLibraries + + com.apple.driver.AppleParavirtGPUIOGPUFamily + 15.0.0 + com.apple.iokit.IOPCIFamily + 1.0.0b1 + com.apple.iokit.IOSurface + 87.0 + com.apple.kpi.iokit + 16.7 + com.apple.kpi.libkern + 16.7 + com.apple.kpi.mach + 9.0.0b1 + com.apple.kpi.private + 10.0.0d3 + com.steelbrain.paravirt.AppleGraphicsDeviceControlBridge + 1.0.0 + com.steelbrain.paravirt.IOGraphicsFamilyBridge + 1.0.0 + + + diff --git a/kext/APVDisplayPort.kext/Contents/MacOS/APVDisplayPort b/kext/APVDisplayPort.kext/Contents/MacOS/APVDisplayPort new file mode 100644 index 0000000..7a70380 Binary files /dev/null and b/kext/APVDisplayPort.kext/Contents/MacOS/APVDisplayPort differ diff --git a/kext/AppleGraphicsDeviceControlBridge.kext/Contents/Info.plist b/kext/AppleGraphicsDeviceControlBridge.kext/Contents/Info.plist new file mode 100644 index 0000000..11a0c67 --- /dev/null +++ b/kext/AppleGraphicsDeviceControlBridge.kext/Contents/Info.plist @@ -0,0 +1,33 @@ + + + + + CFBundleDevelopmentRegion + en + CFBundleExecutable + AppleGraphicsDeviceControlBridge + CFBundleIdentifier + com.steelbrain.paravirt.AppleGraphicsDeviceControlBridge + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + AppleGraphicsDeviceControlBridge + CFBundlePackageType + KEXT + CFBundleShortVersionString + 1.0.0 + CFBundleVersion + 1.0.0 + OSBundleCompatibleVersion + 1.0.0 + OSBundleLibraries + + com.apple.kpi.libkern + 8.0.0 + com.apple.kpi.mach + 8.0.0 + + OSBundleRequired + Root + + diff --git a/kext/AppleGraphicsDeviceControlBridge.kext/Contents/MacOS/AppleGraphicsDeviceControlBridge b/kext/AppleGraphicsDeviceControlBridge.kext/Contents/MacOS/AppleGraphicsDeviceControlBridge new file mode 100755 index 0000000..626e942 Binary files /dev/null and b/kext/AppleGraphicsDeviceControlBridge.kext/Contents/MacOS/AppleGraphicsDeviceControlBridge differ diff --git a/kext/AppleParavirtGPUIOGPUFamily.kext/Contents/Info.plist b/kext/AppleParavirtGPUIOGPUFamily.kext/Contents/Info.plist new file mode 100644 index 0000000..fec6b96 --- /dev/null +++ b/kext/AppleParavirtGPUIOGPUFamily.kext/Contents/Info.plist @@ -0,0 +1,69 @@ + + + + + BuildMachineOSBuild + 23A344017 + CFBundleDevelopmentRegion + en + CFBundleExecutable + AppleParavirtGPUIOGPUFamily + CFBundleIdentifier + com.apple.driver.AppleParavirtGPUIOGPUFamily + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + com.apple.driver.AppleParavirtGPUIOGPUFamily + CFBundlePackageType + KEXT + CFBundleShortVersionString + 15.0.0 + CFBundleSupportedPlatforms + + MacOSX + + CFBundleVersion + 15.0.0 + DTCompiler + com.apple.compilers.llvm.clang.1_0 + DTPlatformBuild + + DTPlatformName + macosx + DTPlatformVersion + 26.7 + DTSDKBuild + 25G221 + DTSDKName + macosx26.7.internal + DTXcode + 2630 + DTXcodeBuild + 17E6107 + LSMinimumSystemVersion + 26.7 + NSHumanReadableCopyright + Copyright © 2019-2025 Apple Inc. All rights reserved. + OSBundleCompatibleVersion + 15.0.0 + OSBundleLibraries + + com.apple.iokit.AppleParavirtIOSurface + 1.0 + com.apple.iokit.IOPCIFamily + 1.0.0b1 + com.apple.iokit.IOSurface + 87.0 + com.apple.kpi.iokit + 16.7 + com.apple.kpi.libkern + 16.7 + com.apple.kpi.mach + 9.0.0b1 + com.apple.kpi.private + 10.0.0d3 + com.steelbrain.paravirt.IOGPUFamilyBridge + 1.0.0 + + + diff --git a/kext/AppleParavirtGPUIOGPUFamily.kext/Contents/MacOS/AppleParavirtGPUIOGPUFamily b/kext/AppleParavirtGPUIOGPUFamily.kext/Contents/MacOS/AppleParavirtGPUIOGPUFamily new file mode 100644 index 0000000..9fb8d49 Binary files /dev/null and b/kext/AppleParavirtGPUIOGPUFamily.kext/Contents/MacOS/AppleParavirtGPUIOGPUFamily differ diff --git a/kext/AppleParavirtGPULoader.kext/Contents/Info.plist b/kext/AppleParavirtGPULoader.kext/Contents/Info.plist new file mode 100644 index 0000000..abe7583 --- /dev/null +++ b/kext/AppleParavirtGPULoader.kext/Contents/Info.plist @@ -0,0 +1,53 @@ + + + + + CFBundleDevelopmentRegion + en + CFBundleExecutable + AppleParavirtGPULoader + CFBundleIdentifier + com.steelbrain.paravirt.AppleParavirtGPULoader + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + AppleParavirtGPULoader + CFBundlePackageType + KEXT + CFBundleShortVersionString + 1.0.0 + CFBundleVersion + 1.0.0 + IOKitPersonalities + + AppleParavirtGPULoader + + CFBundleIdentifier + com.steelbrain.paravirt.AppleParavirtGPULoader + IOClass + AppleParavirtGPULoader + IOMatchCategory + AppleParavirtGPULoader + IOProviderClass + IOResources + IOResourceMatch + IOKit + + + OSBundleLibraries + + as.vit9696.Lilu + 1.7.2 + com.apple.kpi.bsd + 12.0.0 + com.apple.kpi.iokit + 12.0.0 + com.apple.kpi.libkern + 12.0.0 + com.apple.kpi.mach + 12.0.0 + + OSBundleRequired + Root + + diff --git a/kext/AppleParavirtGPULoader.kext/Contents/MacOS/AppleParavirtGPULoader b/kext/AppleParavirtGPULoader.kext/Contents/MacOS/AppleParavirtGPULoader new file mode 100755 index 0000000..6010376 Binary files /dev/null and b/kext/AppleParavirtGPULoader.kext/Contents/MacOS/AppleParavirtGPULoader differ diff --git a/kext/AppleParavirtIOSurface.kext/Contents/Info.plist b/kext/AppleParavirtIOSurface.kext/Contents/Info.plist new file mode 100644 index 0000000..65cfb83 --- /dev/null +++ b/kext/AppleParavirtIOSurface.kext/Contents/Info.plist @@ -0,0 +1,69 @@ + + + + + BuildMachineOSBuild + 23A344017 + CFBundleDevelopmentRegion + en + CFBundleExecutable + AppleParavirtIOSurface + CFBundleIdentifier + com.apple.iokit.AppleParavirtIOSurface + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + com.apple.iokit.AppleParavirtIOSurface + CFBundlePackageType + KEXT + CFBundleShortVersionString + 15.0.0 + CFBundleSupportedPlatforms + + MacOSX + + CFBundleVersion + 15.0.0 + DTCompiler + com.apple.compilers.llvm.clang.1_0 + DTPlatformBuild + + DTPlatformName + macosx + DTPlatformVersion + 26.7 + DTSDKBuild + 25G221 + DTSDKName + macosx26.7.internal + DTXcode + 2630 + DTXcodeBuild + 17E6107 + LSMinimumSystemVersion + 26.7 + NSHumanReadableCopyright + Copyright © 2019-2025 Apple Inc. All rights reserved. + OSBundleCompatibleVersion + 1.0 + OSBundleLibraries + + com.apple.iokit.IOPCIFamily + 1.0.0b1 + com.apple.iokit.IOSurface + 87.0 + com.apple.kpi.bsd + 9.0.0b1 + com.apple.kpi.iokit + 9.0.0b1 + com.apple.kpi.libkern + 9.0.0b1 + com.apple.kpi.mach + 9.0.0b1 + com.apple.kpi.private + 9.0.0b1 + com.apple.kpi.unsupported + 9.0.0b1 + + + diff --git a/kext/AppleParavirtIOSurface.kext/Contents/MacOS/AppleParavirtIOSurface b/kext/AppleParavirtIOSurface.kext/Contents/MacOS/AppleParavirtIOSurface new file mode 100644 index 0000000..ef9c1c4 Binary files /dev/null and b/kext/AppleParavirtIOSurface.kext/Contents/MacOS/AppleParavirtIOSurface differ diff --git a/kext/IOGPUFamilyBridge.kext/Contents/Info.plist b/kext/IOGPUFamilyBridge.kext/Contents/Info.plist new file mode 100644 index 0000000..dd4ee0d --- /dev/null +++ b/kext/IOGPUFamilyBridge.kext/Contents/Info.plist @@ -0,0 +1,33 @@ + + + + + CFBundleDevelopmentRegion + en + CFBundleExecutable + IOGPUFamilyBridge + CFBundleIdentifier + com.steelbrain.paravirt.IOGPUFamilyBridge + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + IOGPUFamilyBridge + CFBundlePackageType + KEXT + CFBundleShortVersionString + 1.0.0 + CFBundleVersion + 1.0.0 + OSBundleCompatibleVersion + 1.0.0 + OSBundleLibraries + + com.apple.kpi.libkern + 8.0.0 + com.apple.kpi.mach + 8.0.0 + + OSBundleRequired + Root + + diff --git a/kext/IOGPUFamilyBridge.kext/Contents/MacOS/IOGPUFamilyBridge b/kext/IOGPUFamilyBridge.kext/Contents/MacOS/IOGPUFamilyBridge new file mode 100755 index 0000000..e32fd80 Binary files /dev/null and b/kext/IOGPUFamilyBridge.kext/Contents/MacOS/IOGPUFamilyBridge differ diff --git a/kext/IOGraphicsFamilyBridge.kext/Contents/Info.plist b/kext/IOGraphicsFamilyBridge.kext/Contents/Info.plist new file mode 100644 index 0000000..e7152c2 --- /dev/null +++ b/kext/IOGraphicsFamilyBridge.kext/Contents/Info.plist @@ -0,0 +1,33 @@ + + + + + CFBundleDevelopmentRegion + en + CFBundleExecutable + IOGraphicsFamilyBridge + CFBundleIdentifier + com.steelbrain.paravirt.IOGraphicsFamilyBridge + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + IOGraphicsFamilyBridge + CFBundlePackageType + KEXT + CFBundleShortVersionString + 1.0.0 + CFBundleVersion + 1.0.0 + OSBundleCompatibleVersion + 1.0.0 + OSBundleLibraries + + com.apple.kpi.libkern + 8.0.0 + com.apple.kpi.mach + 8.0.0 + + OSBundleRequired + Root + + diff --git a/kext/IOGraphicsFamilyBridge.kext/Contents/MacOS/IOGraphicsFamilyBridge b/kext/IOGraphicsFamilyBridge.kext/Contents/MacOS/IOGraphicsFamilyBridge new file mode 100755 index 0000000..0bf3057 Binary files /dev/null and b/kext/IOGraphicsFamilyBridge.kext/Contents/MacOS/IOGraphicsFamilyBridge differ diff --git a/src/cli.rs b/src/cli.rs index 6d02763..fc1f296 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -104,10 +104,13 @@ pub struct OpencoreArgs { /// Use OpenCore's text picker instead of the graphical one #[arg(long)] pub text_picker: bool, - /// Leave out Lilu and ReimsVgpu.kext, so macOS 26 drives reims-vgpu's GPU with its driver as - /// it is + /// Leave out Lilu, ReimsVgpu.kext and the apv-arm64-port kexts, so macOS 26 drives + /// reims-vgpu's GPU with its driver as it is #[arg(long)] pub no_kexts: bool, + /// Leave out the apv-arm64-port kexts + #[arg(long)] + pub no_apv_arm64_port: bool, /// Write no OVMF files #[arg(long)] pub no_ovmf: bool, @@ -344,6 +347,7 @@ pub fn opencore_options(a: &OpencoreArgs) -> Result { graphical_picker: !a.text_picker, kexts: !a.no_kexts, }, + apv_arm64_port: !a.no_apv_arm64_port, ovmf_dir, }) } diff --git a/src/opencore/apv_arm64_port.rs b/src/opencore/apv_arm64_port.rs new file mode 100644 index 0000000..e4035ce --- /dev/null +++ b/src/opencore/apv_arm64_port.rs @@ -0,0 +1,301 @@ +//! The apv-arm64-port kexts: seven bundles, which load as one, after Lilu and ReimsVgpu, on macOS +//! 26.4 and later. kext/ holds them as built, under their own names: the apv-arm64-port project's +//! deploy.sh writes them there, and build.rs embeds them. Every image carries them unless kext/ +//! holds none of them; `--no-apv-arm64-port` makes one without them. + +use anyhow::{Context, Result, bail}; +use std::collections::{BTreeMap, BTreeSet}; + +include!(concat!(env!("OUT_DIR"), "/apv_arm64_port.rs")); + +include!("apv_arm64_port_set.rs"); + +/// A kext of the set. +#[derive(Debug)] +pub struct Kext<'a> { + pub bundle: &'a str, + /// Its executable in `Contents/MacOS`, or None for a kext with no code. + pub executable: Option, + identifier: String, + libraries: Vec, + /// Its files, by their path in the bundle. + pub files: Vec<(&'a str, &'a [u8])>, +} + +/// The set in `files` (each by its path under kext/), in the order OpenCore loads it: each kext +/// after those of the set its `OSBundleLibraries` names. Empty when `files` holds no kext; files +/// beside the bundles are left out, and anything short of the exact set refused. +pub fn set<'a>(files: &[(&'a str, &'a [u8])]) -> Result>> { + let mut bundles: BTreeMap<&str, Vec<(&str, &[u8])>> = BTreeMap::new(); + for &(path, data) in files { + let Some((bundle, rel)) = path.split_once('/') else { + continue; + }; + if !bundle.ends_with(".kext") || path.split('/').any(|c| c.starts_with('.')) { + continue; + } + if !SET.contains(&bundle) { + bail!( + "kext/{bundle} is not one of the apv-arm64-port kexts ({})", + SET.join(", ") + ); + } + bundles.entry(bundle).or_default().push((rel, data)); + } + if bundles.is_empty() { + return Ok(Vec::new()); + } + let missing: Vec<&str> = SET + .iter() + .copied() + .filter(|b| !bundles.contains_key(b)) + .collect(); + if !missing.is_empty() { + bail!( + "partial apv-arm64-port set in kext/, without {}: deploy all seven", + missing.join(", ") + ); + } + let mut kexts = Vec::new(); + for (bundle, files) in bundles { + kexts.push(kext(bundle, files)?); + } + order(kexts) +} + +fn kext<'a>(bundle: &'a str, files: Vec<(&'a str, &'a [u8])>) -> Result> { + let plist = files + .iter() + .find(|(p, _)| *p == "Contents/Info.plist") + .with_context(|| format!("kext/{bundle} has no Contents/Info.plist"))?; + let info: plist::Dictionary = plist::from_bytes(plist.1) + .with_context(|| format!("read kext/{bundle}/Contents/Info.plist"))?; + let string = |k: &str| info.get(k).and_then(plist::Value::as_string); + let identifier = string("CFBundleIdentifier") + .with_context(|| format!("kext/{bundle} has no CFBundleIdentifier"))? + .to_string(); + let executable = string("CFBundleExecutable").map(str::to_string); + if let Some(e) = &executable { + let path = format!("Contents/MacOS/{e}"); + if !files.iter().any(|(p, _)| *p == path) { + bail!("kext/{bundle} has no {path}, the executable its Info.plist names"); + } + } + let libraries = info + .get("OSBundleLibraries") + .and_then(plist::Value::as_dictionary) + .map(|d| d.keys().cloned().collect()) + .unwrap_or_default(); + Ok(Kext { + bundle, + executable, + identifier, + libraries, + files, + }) +} + +/// Orders the kexts so each comes after the ones it links against, and otherwise by name. +fn order(mut left: Vec>) -> Result>> { + let ids: BTreeSet = left.iter().map(|k| k.identifier.clone()).collect(); + let mut placed = BTreeSet::new(); + let mut out = Vec::new(); + while !left.is_empty() { + let ready = left.iter().position(|k| { + k.libraries + .iter() + .all(|l| !ids.contains(l) || placed.contains(l)) + }); + let Some(i) = ready else { + let names: Vec<&str> = left.iter().map(|k| k.bundle).collect(); + bail!( + "the apv-arm64-port kexts {} link against each other in a cycle", + names.join(", ") + ); + }; + let k = left.remove(i); + placed.insert(k.identifier.clone()); + out.push(k); + } + Ok(out) +} + +#[cfg(test)] +pub(crate) mod tests { + use super::*; + + /// A bundle's Info.plist: its identifier, executable and libraries. + fn info(id: &str, executable: Option<&str>, libraries: &[&str]) -> Vec { + let mut d = plist::Dictionary::new(); + d.insert("CFBundleIdentifier".into(), id.into()); + if let Some(e) = executable { + d.insert("CFBundleExecutable".into(), e.into()); + } + let mut l = plist::Dictionary::new(); + for x in libraries { + l.insert(x.to_string(), "1.0.0".into()); + } + d.insert("OSBundleLibraries".into(), l.into()); + let mut v = Vec::new(); + plist::Value::Dictionary(d).to_writer_xml(&mut v).unwrap(); + v + } + + /// The seven, linked as the set is: each kext on its bridges, the loader on Lilu. The + /// bridges here have no code, to cover a kext without an executable. + pub(crate) fn fixture() -> Vec<(String, Vec)> { + let lilu = "as.vit9696.Lilu"; + let kexts: [(&str, &str, bool, &[&str]); 7] = [ + ("AppleParavirtGPULoader", "loader", true, &[lilu]), + ("IOGPUFamilyBridge", "gpub", false, &["com.apple.kpi.iokit"]), + ("IOGraphicsFamilyBridge", "gfxb", false, &[]), + ("AppleGraphicsDeviceControlBridge", "agdcb", false, &[]), + ("AppleParavirtIOSurface", "iosurface", true, &[]), + ( + "AppleParavirtGPUIOGPUFamily", + "gpu", + true, + &["t.gpub", "t.iosurface"], + ), + ( + "APVDisplayPort", + "display", + true, + &["t.gfxb", "t.agdcb", "t.gpu"], + ), + ]; + let mut files = Vec::new(); + for (name, id, code, libraries) in kexts { + let exe = code.then_some(name); + files.push(( + format!("{name}.kext/Contents/Info.plist"), + info(&format!("t.{id}"), exe, libraries), + )); + if code { + files.push((format!("{name}.kext/Contents/MacOS/{name}"), vec![0xcf])); + } + } + files + } + + pub(crate) fn refs(files: &[(String, Vec)]) -> Vec<(&str, &[u8])> { + files + .iter() + .map(|(p, d)| (p.as_str(), d.as_slice())) + .collect() + } + + fn bundles(kexts: &[Kext<'_>]) -> Vec { + kexts + .iter() + .map(|k| k.bundle.replace(".kext", "")) + .collect() + } + + #[test] + fn ordered_by_libraries() { + let files = fixture(); + let kexts = set(&refs(&files)).unwrap(); + assert_eq!( + bundles(&kexts), + [ + "AppleGraphicsDeviceControlBridge", + "AppleParavirtGPULoader", + "AppleParavirtIOSurface", + "IOGPUFamilyBridge", + "AppleParavirtGPUIOGPUFamily", + "IOGraphicsFamilyBridge", + "APVDisplayPort", + ] + ); + } + + #[test] + fn executables() { + let files = fixture(); + let kexts = set(&refs(&files)).unwrap(); + for k in &kexts { + let code = !k.bundle.ends_with("Bridge.kext"); + assert_eq!( + k.executable.as_deref(), + code.then(|| k.bundle.trim_end_matches(".kext")), + "{}", + k.bundle + ); + assert_eq!(k.files.len(), if code { 2 } else { 1 }, "{}", k.bundle); + } + + let mut files = fixture(); + files.retain(|(p, _)| p != "APVDisplayPort.kext/Contents/MacOS/APVDisplayPort"); + let e = set(&refs(&files)).unwrap_err().to_string(); + assert!(e.contains("Contents/MacOS/APVDisplayPort"), "{e}"); + } + + #[test] + fn side_files_left_out() { + let mut files = fixture(); + for p in [ + "notes.txt", + "APVDisplayPort.plist", + "APVDisplayPort.kext/.DS_Store", + "APVDisplayPort.kext/Contents/._Info.plist", + ] { + files.push((p.to_string(), b"junk".to_vec())); + } + let kexts = set(&refs(&files)).unwrap(); + assert_eq!(kexts.len(), 7); + for k in &kexts { + for (p, _) in &k.files { + assert!(!p.contains("/.") && !p.starts_with('.'), "{}/{p}", k.bundle); + } + } + } + + #[test] + fn other_kexts_refused() { + for extra in ["Lilu.kext", "IOGPUFamily.kext"] { + let mut files = fixture(); + files.push((format!("{extra}/Contents/Info.plist"), info("x", None, &[]))); + let e = set(&refs(&files)).unwrap_err().to_string(); + assert!(e.contains(extra), "{e}"); + } + } + + #[test] + fn part_of_the_set_refused() { + let mut files = fixture(); + files.retain(|(p, _)| !p.starts_with("IOGPUFamilyBridge.kext/")); + let e = set(&refs(&files)).unwrap_err().to_string(); + assert!(e.contains("without IOGPUFamilyBridge.kext"), "{e}"); + } + + #[test] + fn cycle_refused() { + let mut files = fixture(); + for (p, d) in &mut files { + if p == "AppleParavirtIOSurface.kext/Contents/Info.plist" { + *d = info( + "t.iosurface", + Some("AppleParavirtIOSurface"), + &["t.display"], + ); + } + } + let e = set(&refs(&files)).unwrap_err().to_string(); + assert!(e.contains("cycle"), "{e}"); + } + + #[test] + fn nothing_without_kexts() { + assert!(set(&[]).unwrap().is_empty()); + let side: [(&str, &[u8]); 1] = [("notes.txt", b"")]; + assert!(set(&side).unwrap().is_empty()); + } + + /// What kext/ holds of the set is the whole of it, or nothing. + #[test] + fn embedded_set_whole() { + let kexts = set(FILES).unwrap(); + assert!(kexts.is_empty() || kexts.len() == SET.len()); + } +} diff --git a/src/opencore/apv_arm64_port_set.rs b/src/opencore/apv_arm64_port_set.rs new file mode 100644 index 0000000..daec187 --- /dev/null +++ b/src/opencore/apv_arm64_port_set.rs @@ -0,0 +1,12 @@ +/// The seven apv-arm64-port bundles, in kext/ under their own names, which load as one: +/// AppleParavirtGPULoader binds the others through the bridges, and refuses when one is missing +/// or differs. build.rs and apv_arm64_port.rs both include this list. +pub const SET: [&str; 7] = [ + "AppleParavirtGPULoader.kext", + "IOGPUFamilyBridge.kext", + "IOGraphicsFamilyBridge.kext", + "AppleGraphicsDeviceControlBridge.kext", + "AppleParavirtIOSurface.kext", + "AppleParavirtGPUIOGPUFamily.kext", + "APVDisplayPort.kext", +]; diff --git a/src/opencore/config.rs b/src/opencore/config.rs index 27f9b90..5354176 100644 --- a/src/opencore/config.rs +++ b/src/opencore/config.rs @@ -13,7 +13,9 @@ //! when the VM has one, unless macOS's "Play sound on startup" is off (`StartupMute`); //! - on macOS 26, two kexts: Lilu, and its plugin ReimsVgpu (kext/), which has macOS's own driver //! for reims-vgpu's GPU negotiate the interface version arm64 guests use, and patches what the -//! x86-64 driver and its Metal plugin hardcode; no others, as QEMU's `isa-applesmc` is the SMC; +//! x86-64 driver and its Metal plugin hardcode; after them, the seven apv-arm64-port kexts +//! (apv_arm64_port.rs), on macOS 26.4 and later alone; no others, as QEMU's `isa-applesmc` is +//! the SMC; //! - one ACPI table, the `EC` and `USBX` devices macOS looks for (acpi.rs); //! - CPUID leaf 1 reported as a Skylake-SP (family 6, model 85, stepping 4), a CPU every x86-64 //! macOS from 10.13 to 26 knows, whatever `-cpu` model the VM has; @@ -23,6 +25,7 @@ //! does not see it runs in a VM and lets iCloud and the App Store sign in; //! - the SMBIOS identity (smbios.rs), and the picker's timeout and boot arguments. +use super::apv_arm64_port::Kext as PortKext; use super::smbios::Smbios; use anyhow::{Context, Result, bail}; use plist::{Dictionary, Value}; @@ -65,6 +68,9 @@ pub const REIMS_VGPU: Kext = Kext { /// The kexts load on Darwin 25 alone, macOS 26, whose GPU driver ReimsVgpu patches. const KEXT_MIN_KERNEL: &str = "25.0.0"; const KEXT_MAX_KERNEL: &str = "25.99.99"; +/// The apv-arm64-port kexts load from macOS 26.4 on: on older builds, ReimsVgpu has macOS's own +/// driver take the GPU. Where the set refuses at boot, it leaves the GPU to that driver too. +const PORT_MIN_KERNEL: &str = "25.4.0"; /// Arguments for every boot: kernel symbols in panic logs, and no panics on a TLB shootdown or a /// timeout a vCPU took because the host descheduled it. @@ -86,11 +92,13 @@ pub struct Options { pub hide_vm: bool, /// OpenCanopy's graphical picker, or OpenCore's text one. pub graphical_picker: bool, - /// Lilu and ReimsVgpu, for macOS 26. + /// Lilu and ReimsVgpu (and the set `make` is given), for macOS 26. pub kexts: bool, } -pub fn make(sample: &[u8], o: &Options) -> Result> { +/// The config.plist for `o`, from the release's `sample`. With `o.kexts`, `port` +/// (apv_arm64_port.rs) loads after Lilu and ReimsVgpu. +pub fn make(sample: &[u8], o: &Options, port: &[PortKext<'_>]) -> Result> { let mut root: Value = plist::from_bytes(sample).context("read Sample.plist")?; let c = root .as_dictionary_mut() @@ -134,27 +142,11 @@ pub fn make(sample: &[u8], o: &Options) -> Result> { Value::Dictionary(Dictionary::new()), )?; - let mut kexts = Vec::new(); - if o.kexts { - for (k, comment) in [ - (LILU, "Patch engine"), - (REIMS_VGPU, "reims-vgpu's GPU features on macOS 26"), - ] { - let mut e = template(c, &["Kernel", "Add"])?; - e.insert("Arch".into(), "x86_64".into()); - e.insert("BundlePath".into(), k.bundle.into()); - e.insert("Comment".into(), comment.into()); - e.insert("Enabled".into(), true.into()); - e.insert( - "ExecutablePath".into(), - format!("Contents/MacOS/{}", k.executable).into(), - ); - e.insert("MaxKernel".into(), KEXT_MAX_KERNEL.into()); - e.insert("MinKernel".into(), KEXT_MIN_KERNEL.into()); - e.insert("PlistPath".into(), "Contents/Info.plist".into()); - kexts.push(Value::Dictionary(e)); - } - } + let kexts = if o.kexts { + kernel_add(&template(c, &["Kernel", "Add"])?, port) + } else { + Vec::new() + }; set(c, &["Kernel"], "Add", Value::Array(kexts))?; set(c, &["Kernel"], "Block", Value::Array(vec![]))?; set(c, &["Kernel"], "Force", Value::Array(vec![]))?; @@ -316,6 +308,49 @@ pub fn make(sample: &[u8], o: &Options) -> Result> { Ok(out) } +/// Kernel > Add's entries, made from the sample's first: Lilu and ReimsVgpu for macOS 26, then +/// `port` for macOS 26.4 and later. +fn kernel_add(template: &Dictionary, port: &[PortKext<'_>]) -> Vec { + let mut add = vec![ + ( + LILU.bundle, + Some(LILU.executable), + KEXT_MIN_KERNEL, + "Patch engine".to_string(), + ), + ( + REIMS_VGPU.bundle, + Some(REIMS_VGPU.executable), + KEXT_MIN_KERNEL, + "reims-vgpu's GPU features on macOS 26".to_string(), + ), + ]; + for k in port { + add.push(( + k.bundle, + k.executable.as_deref(), + PORT_MIN_KERNEL, + format!("apv-arm64-port's {}", k.bundle.trim_end_matches(".kext")), + )); + } + add.into_iter() + .map(|(bundle, executable, min_kernel, comment)| { + let mut e = template.clone(); + e.insert("Arch".into(), "x86_64".into()); + e.insert("BundlePath".into(), bundle.into()); + e.insert("Comment".into(), comment.into()); + e.insert("Enabled".into(), true.into()); + // A kext with no code has an empty path. + let path = executable.map(|x| format!("Contents/MacOS/{x}")); + e.insert("ExecutablePath".into(), path.unwrap_or_default().into()); + e.insert("MaxKernel".into(), KEXT_MAX_KERNEL.into()); + e.insert("MinKernel".into(), min_kernel.into()); + e.insert("PlistPath".into(), "Contents/Info.plist".into()); + Value::Dictionary(e) + }) + .collect() +} + fn dict<'a>(root: &'a mut Dictionary, path: &[&str]) -> Result<&'a mut Dictionary> { let mut d = root; for (i, k) in path.iter().enumerate() { @@ -359,3 +394,60 @@ fn unhex(s: &str) -> Result> { }) .collect() } + +#[cfg(test)] +mod tests { + use super::*; + use crate::opencore::apv_arm64_port as port; + + fn fields(v: &Value, keys: &[&str]) -> Vec { + let d = v.as_dictionary().unwrap(); + keys.iter() + .map(|k| d.get(k).and_then(Value::as_string).unwrap().to_string()) + .collect() + } + + #[test] + fn kernel_add_entries() { + let mut template = Dictionary::new(); + template.insert("BundlePath".into(), "Lilu.kext".into()); + template.insert("Enabled".into(), false.into()); + let keys = [ + "BundlePath", + "ExecutablePath", + "MinKernel", + "MaxKernel", + "Arch", + ]; + + // With none of the set in kext/, Lilu and ReimsVgpu alone. + let today = kernel_add(&template, &port::set(&[]).unwrap()); + let want = |b: &str, x: &str| { + [b, x, KEXT_MIN_KERNEL, KEXT_MAX_KERNEL, "x86_64"].map(str::to_string) + }; + assert_eq!( + today.iter().map(|e| fields(e, &keys)).collect::>(), + [ + want("Lilu.kext", "Contents/MacOS/Lilu"), + want("ReimsVgpu.kext", "Contents/MacOS/ReimsVgpu"), + ] + ); + + // With the set, it follows them in its order, from macOS 26.4; a kext with no code has + // no path. + let files = port::tests::fixture(); + let set = port::set(&port::tests::refs(&files)).unwrap(); + let all = kernel_add(&template, &set); + assert_eq!(all.len(), 9); + assert_eq!(all[..2], today[..]); + for (e, k) in all[2..].iter().zip(&set) { + let f = fields(e, &keys); + assert_eq!(f[0], k.bundle); + let x = k.executable.as_ref().map(|x| format!("Contents/MacOS/{x}")); + assert_eq!(f[1], x.unwrap_or_default()); + assert_eq!(f[2..], [PORT_MIN_KERNEL, KEXT_MAX_KERNEL, "x86_64"]); + assert!(fields(e, &["Comment"])[0].starts_with("apv-arm64-port's ")); + } + assert!(all.iter().any(|e| fields(e, &keys)[1].is_empty())); + } +} diff --git a/src/opencore/mod.rs b/src/opencore/mod.rs index c1b304b..49b4cd5 100644 --- a/src/opencore/mod.rs +++ b/src/opencore/mod.rs @@ -1,10 +1,12 @@ //! The OpenCore boot image: a 128 MiB GPT disk whose EFI system partition holds a pinned //! OpenCore release, the drivers and ACPI table a VM needs, the picker's icons and the boot chime //! from a pinned commit of OcBinaryData, a pinned Lilu release and its plugin ReimsVgpu.kext -//! (kext/), and a `config.plist` made for a q35 VM (config.rs) with an SMBIOS identity of its own -//! (smbios.rs). OVMF goes beside it (ovmf.rs), where reims-ctl looks for it. +//! (kext/), the apv-arm64-port kexts (apv_arm64_port.rs), and a `config.plist` made for a q35 VM +//! (config.rs) with an SMBIOS identity of its own (smbios.rs). OVMF goes beside it (ovmf.rs), +//! where reims-ctl looks for it. pub mod acpi; +pub mod apv_arm64_port; pub mod config; pub mod smbios; @@ -58,6 +60,8 @@ const DISK_SIZE: u64 = 128 << 20; pub struct Options { pub output: PathBuf, pub config: config::Options, + /// The apv-arm64-port kexts too, when kext/ holds them; with `config.kexts` alone. + pub apv_arm64_port: bool, /// Where OVMF's files go, or None to write none. pub ovmf_dir: Option, } @@ -68,6 +72,8 @@ pub struct Outputs { pub format: Format, pub opencore: &'static str, pub smbios: smbios::Smbios, + /// Whether the image carries the apv-arm64-port kexts. + pub apv_arm64_port: bool, pub ovmf: Option, } @@ -76,6 +82,11 @@ pub fn build(o: &Options, r: &dyn Reporter) -> Result { progress::check(r)?; let resources_path = cache::fetch(&RESOURCES, r)?; progress::check(r)?; + let port = if o.config.kexts && o.apv_arm64_port { + apv_arm64_port::set(apv_arm64_port::FILES)? + } else { + Vec::new() + }; let lilu_path = if o.config.kexts { let p = cache::fetch(&LILU_RELEASE, r)?; progress::check(r)?; @@ -84,11 +95,16 @@ pub fn build(o: &Options, r: &dyn Reporter) -> Result { None }; r.step(&format!("Building the OpenCore {VERSION} image")); - let mut files = files(&zip_path, &o.config)?; + let mut files = files(&zip_path, &o.config, &port)?; files.extend(resources(&resources_path, o.config.graphical_picker)?); if let Some(p) = &lilu_path { files.extend(kexts(p)?); } + for k in &port { + for (p, d) in &k.files { + files.push((format!("EFI/OC/Kexts/{}/{p}", k.bundle), d.to_vec())); + } + } let files: Vec<(String, Data)> = files .into_iter() .map(|(p, d)| (p, Data::Bytes(d))) @@ -115,12 +131,17 @@ pub fn build(o: &Options, r: &dyn Reporter) -> Result { format, opencore: VERSION, smbios: o.config.smbios.clone(), + apv_arm64_port: !port.is_empty(), ovmf, }) } /// The partition's files, from the release's X64 tree, and the made config.plist and ACPI table. -fn files(zip_path: &Path, c: &config::Options) -> Result)>> { +fn files( + zip_path: &Path, + c: &config::Options, + port: &[apv_arm64_port::Kext<'_>], +) -> Result)>> { let f = std::fs::File::open(zip_path).with_context(|| format!("open {}", zip_path.display()))?; let mut zip = zip::ZipArchive::new(f).context("read the OpenCore release")?; @@ -150,7 +171,10 @@ fn files(zip_path: &Path, c: &config::Options) -> Result)>> )); } let sample = read("Docs/Sample.plist")?; - files.push(("EFI/OC/config.plist".into(), config::make(&sample, c)?)); + files.push(( + "EFI/OC/config.plist".into(), + config::make(&sample, c, port)?, + )); files.push(( format!("EFI/OC/ACPI/{}", acpi::FILE_NAME), acpi::ssdt_ec_usbx(), diff --git a/src/tui.rs b/src/tui.rs index 16dde00..cb66ed4 100644 --- a/src/tui.rs +++ b/src/tui.rs @@ -272,6 +272,7 @@ impl App { show_vm: !form.toggle(5), text_picker: !form.toggle(7), no_kexts: false, + no_apv_arm64_port: false, no_ovmf: !form.toggle(3), ovmf_dir: None, };