Skip to content

Commit f933d55

Browse files
Conan-Kudomarcan
authored andcommitted
dracut: Add firmware loading module
This implements the logic for loading Apple firmware on Asahi systems using Dracut as the initramfs system. Signed-off-by: Neal Gompa <[email protected]>
1 parent 594967c commit f933d55

5 files changed

Lines changed: 105 additions & 1 deletion

File tree

Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ ARCH_SCRIPTS=update-grub first-boot
66
UNITS=first-boot.service
77
MULTI_USER_WANTS=first-boot.service
88
DRACUT_CONF_DIR=$(PREFIX)/lib/dracut/dracut.conf.d
9+
DRACUT_MODULES_DIR=$(PREFIX)/lib/dracut/modules.d
910
BUILD_SCRIPTS=$(addprefix build/,$(SCRIPTS))
1011
BUILD_ARCH_SCRIPTS=$(addprefix build/,$(ARCH_SCRIPTS))
1112

@@ -26,6 +27,7 @@ install: all
2627
install -m0644 -t $(DESTDIR)/etc etc/m1n1.conf
2728
install -dD $(DESTDIR)$(PREFIX)/share/asahi-scripts
2829
install -m0644 -t $(DESTDIR)$(PREFIX)/share/asahi-scripts functions.sh
30+
install -dD $(DESTDIR)/lib/firmware/vendor
2931

3032
install-mkinitcpio: install
3133
install -dD $(DESTDIR)$(PREFIX)/lib/initcpio/install
@@ -35,7 +37,11 @@ install-mkinitcpio: install
3537

3638
install-dracut: install
3739
install -dD $(DESTDIR)$(DRACUT_CONF_DIR)
38-
install -m0644 -t $(DESTDIR)$(DRACUT_CONF_DIR) dracut/10-asahi.conf
40+
install -m0644 -t $(DESTDIR)$(DRACUT_CONF_DIR) dracut/dracut.conf.d/10-asahi.conf
41+
install -dD $(DESTDIR)$(DRACUT_MODULES_DIR)/99asahi-firmware
42+
install -m0755 -t $(DESTDIR)$(DRACUT_MODULES_DIR)/99asahi-firmware dracut/modules.d/99asahi-firmware/install-asahi-firmware.sh
43+
install -m0755 -t $(DESTDIR)$(DRACUT_MODULES_DIR)/99asahi-firmware dracut/modules.d/99asahi-firmware/load-asahi-firmware.sh
44+
install -m0755 -t $(DESTDIR)$(DRACUT_MODULES_DIR)/99asahi-firmware dracut/modules.d/99asahi-firmware/module-setup.sh
3945

4046
install-arch: install install-mkinitcpio
4147
install -m0755 -t $(DESTDIR)$(BIN_DIR)/ $(BUILD_ARCH_SCRIPTS)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@ add_drivers+=" rtc-macsmc simple-mfd-spmi spmi-apple-controller nvmem_spmi_mfd "
2323

2424
# For MTP HID
2525
add_drivers+=" apple-dockchannel dockchannel-hid apple-rtkit-helper "
26+
27+
# For Apple firmware
28+
add_dracutmodules+=" asahi-firmware "
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/sh
2+
# SPDX-License-Identifier: MIT
3+
4+
type getarg > /dev/null 2>&1 || . /lib/dracut-lib.sh
5+
6+
info ":: Asahi: Installing vendor firmware to root filesystem..."
7+
if [ ! -d /sysroot/lib/firmware/vendor ]; then
8+
warn ":: Asahi: Vendor firmware directory missing on the root filesystem!"
9+
return 1
10+
fi
11+
mount -t tmpfs vendorfw /sysroot/lib/firmware/vendor
12+
cp -pr /vendorfw/* /vendorfw/.vendorfw.manifest /sysroot/lib/firmware/vendor
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/bin/sh
2+
# SPDX-License-Identifier: MIT
3+
4+
type getarg > /dev/null 2>&1 || . /lib/dracut-lib.sh
5+
6+
if [ -e /vendorfw ]; then
7+
info ":: Asahi: Vendor firmware was loaded by the bootloader"
8+
return 0
9+
fi
10+
11+
if [ ! -e /proc/device-tree/chosen/asahi,efi-system-partition ]; then
12+
info ":: Asahi: Missing asahi,efi-system-partition variable, firmware will not be loaded!"
13+
return 0
14+
fi
15+
16+
info ":: Asahi: Triggering early load of NVMe modules..."
17+
modprobe apple-mailbox
18+
modprobe nvme-apple
19+
20+
for i in $(seq 0 50); do
21+
[ -e /sys/bus/platform/drivers/nvme-apple/*.nvme/nvme/nvme*/nvme*n1/ ] && break
22+
sleep 0.1
23+
done
24+
25+
if [ ! -e /sys/bus/platform/drivers/nvme-apple/*.nvme/nvme/nvme*/nvme*n1/ ]; then
26+
warn ":: Asahi: Timed out waiting for NVMe device"
27+
return 1
28+
fi
29+
30+
# If the above exists, hopefully the /dev device exists and this will work
31+
info ":: Asahi: Unpacking vendor firmware into initramfs..."
32+
33+
VENDORFW="/run/.system-efi/vendorfw/"
34+
35+
(
36+
. /usr/share/asahi-scripts/functions.sh
37+
mount_sys_esp /run/.system-efi
38+
)
39+
40+
if [ ! -e "$VENDORFW/firmware.cpio" ]; then
41+
warn ":: Asahi: Vendor firmware not found in ESP."
42+
umount /run/.system-efi
43+
return 1
44+
fi
45+
46+
( cd /; cpio -i < "$VENDORFW/firmware.cpio" )
47+
info ":: Asahi firmware unpacked successfully"
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/sh
2+
# SPDX-License-Identifier: MIT
3+
4+
# called by dracut
5+
check() {
6+
if [ -n "$hostonly" ] && [ ! -e /proc/device-tree/chosen/asahi,efi-system-partition ]; then
7+
return 0
8+
elif [ -z "$hostonly" ]; then
9+
return 0
10+
else
11+
return 255
12+
fi
13+
}
14+
15+
# called by dracut
16+
depends() {
17+
echo fs-lib
18+
return 0
19+
}
20+
21+
# called by dracut
22+
installkernel() {
23+
instmods apple-mailbox nvme-apple
24+
}
25+
26+
# called by dracut
27+
install() {
28+
inst_dir "/lib/firmware"
29+
ln_r "/lib/firmware/vendor" "/vendorfw"
30+
asahiscriptsdir="/usr/share/asahi-scripts"
31+
inst_dir $asahiscriptsdir
32+
$DRACUT_CP -R -L -t "${initdir}/${asahiscriptsdir}" "${dracutsysrootdir}${asahiscriptsdir}"/*
33+
inst_multiple cpio cut dirname modprobe mount seq sleep umount
34+
inst_hook pre-udev 10 "${moddir}/load-asahi-firmware.sh"
35+
inst_hook cleanup 99 "${moddir}/install-asahi-firmware.sh"
36+
}

0 commit comments

Comments
 (0)