On macOS's bash, 3.2, a script that stops at an unset variable under set -u exits 0 where an EXIT trap is set, and $? is 0 inside the trap too, so a check that aborted read as a pass (measured: the same script exits 1 with no trap). payload_test.sh showed it; seven more scripts pair set -u with an EXIT trap: check-boot.sh, check-imports.sh, walk_window_macos.sh, opencore-add-kexts.sh, ship-commit.sh, msi-boot.sh and msi-image.sh. Each now keeps an ended flag, set at each deliberate success end, and its trap does its cleanup as before, then turns an exit 0 short of that end into 1 and keeps any other status. payload_test.sh takes the same form in place of the trap its last fix removed, so it cleans its scratch on an abort again.
152 lines
5.9 KiB
Bash
Executable File
152 lines
5.9 KiB
Bash
Executable File
#!/bin/bash
|
|
# A copy of a VM's OpenCore image with kexts added: each bundle in
|
|
# EFI/OC/Kexts, with a Kernel > Add entry made as reims-util makes its own
|
|
# (from the image's first entry where it has one: x86_64, its executable and
|
|
# Info.plist), but with no Darwin bounds, where guest/prebuilt's set has them:
|
|
# the kexts load on whatever release the VM runs, as work on a release the set
|
|
# leaves out needs. For iterating on the guest kexts (guest/kext,
|
|
# guest/rgfxgate) and the Graft that binds them, without installing anything
|
|
# inside the guest.
|
|
#
|
|
# scripts/internal/opencore-add-kexts.sh SRC OUT KEXT...
|
|
#
|
|
# SRC is the VM's image, qcow2 or raw, and is only read. OUT is the copy, qcow2
|
|
# for a .qcow2 name and raw otherwise; it must not exist, so SRC is never
|
|
# written. The KEXTs go after the image's own entries, each after the given
|
|
# kexts its OSBundleLibraries names. A kext the image already carries (by
|
|
# bundle name) is replaced, files and entry, in its entry's place, or moves to
|
|
# the end where a given kext it links against comes later. Prints the
|
|
# Kernel > Add entries of OUT.
|
|
#
|
|
# Needs qemu-img, mtools and python3. It edits a raw copy in a temporary
|
|
# directory, so nothing is mounted.
|
|
set -euo pipefail
|
|
|
|
usage() { echo "usage: scripts/internal/opencore-add-kexts.sh SRC OUT KEXT..." >&2; exit 2; }
|
|
[[ $# -ge 3 ]] || usage
|
|
src=$1 out=$2
|
|
shift 2
|
|
for tool in qemu-img mcopy mdir mdeltree python3; do
|
|
command -v "$tool" > /dev/null || { echo "opencore-add-kexts: needs $tool (qemu-img; mtools)" >&2; exit 2; }
|
|
done
|
|
[[ -f $src ]] || { echo "opencore-add-kexts: no image $src" >&2; exit 2; }
|
|
[[ ! -e $out ]] || { echo "opencore-add-kexts: $out exists; give a new path for the copy" >&2; exit 2; }
|
|
for k in "$@"; do
|
|
[[ -f $k/Contents/Info.plist ]] || { echo "opencore-add-kexts: $k is not a kext bundle" >&2; exit 2; }
|
|
done
|
|
|
|
tmp=$(mktemp -d "${TMPDIR:-/tmp}/opencore-add-kexts.XXXXXX")
|
|
# bash 3.2, macOS's, exits 0 from a set -u error where an EXIT trap is set, with $? 0 in the
|
|
# trap too, so an aborted run would read as a pass (docs/lessons.md): the trap fails a run
|
|
# that exits 0 short of its end, which sets `ended`.
|
|
ended=0
|
|
trap 'rc=$?; rm -rf "$tmp"; [[ $rc != 0 || $ended == 1 ]] || rc=1; exit $rc' EXIT
|
|
qemu-img convert -O raw "$src" "$tmp/disk.raw"
|
|
|
|
# The ESP: the GPT's first partition, in 512-byte sectors as reims-util
|
|
# writes them.
|
|
offset=$(python3 -I - "$tmp/disk.raw" << 'EOF'
|
|
import struct, sys
|
|
with open(sys.argv[1], "rb") as f:
|
|
f.seek(512)
|
|
header = f.read(92)
|
|
if header[:8] != b"EFI PART":
|
|
sys.exit("opencore-add-kexts: the image has no GPT")
|
|
entries_lba, = struct.unpack_from("<Q", header, 72)
|
|
f.seek(entries_lba * 512)
|
|
first_lba, = struct.unpack_from("<Q", f.read(128), 32)
|
|
print(first_lba * 512)
|
|
EOF
|
|
)
|
|
export MTOOLS_SKIP_CHECK=1
|
|
img="$tmp/disk.raw@@$offset"
|
|
mcopy -i "$img" ::/EFI/OC/config.plist "$tmp/config.plist"
|
|
|
|
# The bundles, without Finder's files.
|
|
mkdir "$tmp/kexts"
|
|
for k in "$@"; do
|
|
name=$(basename "$k")
|
|
cp -R "$k" "$tmp/kexts/$name"
|
|
find "$tmp/kexts/$name" -name '.*' -exec rm -rf {} +
|
|
if mdir -i "$img" "::/EFI/OC/Kexts/$name" > /dev/null 2>&1; then
|
|
mdeltree -i "$img" "::/EFI/OC/Kexts/$name"
|
|
fi
|
|
mcopy -s -i "$img" "$tmp/kexts/$name" ::/EFI/OC/Kexts/
|
|
done
|
|
|
|
python3 -I - "$tmp/config.plist" "$tmp/kexts" "$@" << 'EOF'
|
|
import copy, os, plistlib, sys
|
|
|
|
config_path, staged = sys.argv[1], sys.argv[2]
|
|
with open(config_path, "rb") as f:
|
|
config = plistlib.load(f)
|
|
add = config["Kernel"]["Add"]
|
|
# Every key OpenCore reads is set below; the first entry, where there is one,
|
|
# carries any other key over.
|
|
template = add[0] if add else {}
|
|
|
|
kexts = []
|
|
for path in sys.argv[3:]:
|
|
name = os.path.basename(path.rstrip("/"))
|
|
with open(os.path.join(staged, name, "Contents/Info.plist"), "rb") as f:
|
|
info = plistlib.load(f)
|
|
kexts.append((name, info))
|
|
|
|
# Each after the given kexts it links against, otherwise in the order given.
|
|
ids = {info["CFBundleIdentifier"] for _, info in kexts}
|
|
placed, ordered, left = set(), [], list(kexts)
|
|
while left:
|
|
for i, (name, info) in enumerate(left):
|
|
libs = set(info.get("OSBundleLibraries", {})) & ids
|
|
if libs <= placed:
|
|
break
|
|
else:
|
|
sys.exit("opencore-add-kexts: the kexts link against each other in a cycle")
|
|
name, info = left.pop(i)
|
|
placed.add(info["CFBundleIdentifier"])
|
|
ordered.append((name, info))
|
|
|
|
for name, info in ordered:
|
|
entry = copy.deepcopy(template)
|
|
executable = info.get("CFBundleExecutable")
|
|
entry.update({
|
|
"Arch": "x86_64",
|
|
"BundlePath": name,
|
|
"Comment": f"{name[:-5] if name.endswith('.kext') else name}, added for development",
|
|
"Enabled": True,
|
|
"ExecutablePath": f"Contents/MacOS/{executable}" if executable else "",
|
|
"MaxKernel": "",
|
|
"MinKernel": "",
|
|
"PlistPath": "Contents/Info.plist",
|
|
})
|
|
at = next((i for i, e in enumerate(add) if e.get("BundlePath") == name), None)
|
|
# A kext replaced in its entry's place must still come after the given
|
|
# kexts it links against, which may be new and so appended.
|
|
libs = set(info.get("OSBundleLibraries", {})) & ids
|
|
after = [i for i, e in enumerate(add) if e.get("BundlePath") in
|
|
{n for n, i2 in ordered if i2["CFBundleIdentifier"] in libs}]
|
|
if at is not None and after and max(after) > at:
|
|
del add[at]
|
|
at = None
|
|
if at is None:
|
|
add.append(entry)
|
|
else:
|
|
add[at] = entry
|
|
|
|
with open(config_path, "wb") as f:
|
|
plistlib.dump(config, f)
|
|
for e in add:
|
|
print(f" {e['BundlePath']:<40} {e['ExecutablePath'] or '(no code)':<44} "
|
|
f"{e['MinKernel'] or '-'}..{e['MaxKernel'] or '-'}{'' if e['Enabled'] else ' (disabled)'}")
|
|
EOF
|
|
mcopy -o -i "$img" "$tmp/config.plist" ::/EFI/OC/config.plist
|
|
|
|
case $out in
|
|
*.qcow2) format=qcow2 ;;
|
|
*) format=raw ;;
|
|
esac
|
|
qemu-img convert -O "$format" "$tmp/disk.raw" "$tmp/out"
|
|
mv "$tmp/out" "$out"
|
|
ended=1
|
|
echo "opencore-add-kexts: wrote $out ($format)"
|