File: 0003-debian-Add-initramfs-tools-implementation-for-cpio-f.patch

package info (click to toggle)
asahi-scripts 20250130-3%2Bdeb13u1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 336 kB
  • sloc: sh: 828; makefile: 97
file content (296 lines) | stat: -rw-r--r-- 8,397 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
From: Tobias Heider <me@tobhe.de>
Date: Sun, 12 Feb 2023 01:23:08 +0100
Subject: debian: Add initramfs-tools implementation for cpio firmware loading

- Add initramfs hooks and boot scripts to extract and mount firmware
- Add update-m1n1 kernel hook
- Add install-initramfs make target
- busybox mount doesn't support PARTUUID= so use /dev/disk/by-partuuid
---
 Makefile                            | 15 ++++++++-
 functions.sh                        |  4 +--
 initramfs/hooks/asahi               | 39 ++++++++++++++++++++++++
 initramfs/kernel/zz-update-m1n1     | 26 ++++++++++++++++
 initramfs/modules                   | 43 ++++++++++++++++++++++++++
 initramfs/scripts/init-bottom/asahi | 19 ++++++++++++
 initramfs/scripts/init-top/asahi    | 61 +++++++++++++++++++++++++++++++++++++
 7 files changed, 204 insertions(+), 3 deletions(-)
 create mode 100644 initramfs/hooks/asahi
 create mode 100644 initramfs/kernel/zz-update-m1n1
 create mode 100644 initramfs/modules
 create mode 100644 initramfs/scripts/init-bottom/asahi
 create mode 100644 initramfs/scripts/init-top/asahi

diff --git a/Makefile b/Makefile
index aca6667..3d9b04e 100644
--- a/Makefile
+++ b/Makefile
@@ -54,6 +54,19 @@ install-macsmc-battery: install
 	install -m0755 -t $(DESTDIR)$(SYSTEMD_UNIT_DIR) macsmc-battery/systemd/macsmc-battery-charge-control-end-threshold.service
 	install -m0644 -t $(DESTDIR)$(UDEV_RULES_DIR) macsmc-battery/udev/93-macsmc-battery-charge-control.rules
 
+install-initramfs: install
+	install -d $(DESTDIR)/etc/kernel/postinst.d/
+	install -m0755 -t $(DESTDIR)/etc/kernel/postinst.d/ initramfs/kernel/zz-update-m1n1
+	install -d $(DESTDIR)/etc/kernel/postrm.d/
+	install -m0755 -t $(DESTDIR)/etc/kernel/postrm.d/ initramfs/kernel/zz-update-m1n1
+	install -d $(DESTDIR)$(PREFIX)/share/initramfs-tools/hooks/
+	install -m0755 -t $(DESTDIR)$(PREFIX)/share/initramfs-tools/hooks/ initramfs/hooks/asahi
+	install -d $(DESTDIR)$(PREFIX)/share/initramfs-tools/scripts/init-top
+	install -m0755 -t $(DESTDIR)$(PREFIX)/share/initramfs-tools/scripts/init-top initramfs/scripts/init-top/asahi
+	install -d $(DESTDIR)$(PREFIX)/share/initramfs-tools/scripts/init-bottom
+	install -m0755 -t $(DESTDIR)$(PREFIX)/share/initramfs-tools/scripts/init-bottom initramfs/scripts/init-bottom/asahi
+	install -m0644 -t $(DESTDIR)$(PREFIX)/share/asahi-scripts initramfs/modules
+
 install-arch: install install-mkinitcpio install-macsmc-battery
 	install -m0755 -t $(DESTDIR)$(BIN_DIR)/ $(BUILD_ARCH_SCRIPTS)
 	install -dD $(DESTDIR)$(PREFIX)/lib/systemd/system
@@ -66,7 +79,7 @@ install-arch: install install-mkinitcpio install-macsmc-battery
 
 install-fedora: install install-dracut install-macsmc-battery
 
-install-debian: install install-macsmc-battery
+install-debian: install install-macsmc-battery install-initramfs
 
 uninstall:
 	rm -f $(addprefix $(DESTDIR)$(BIN_DIR)/,$(SCRIPTS))
diff --git a/functions.sh b/functions.sh
index fd400ff..ccb3ed2 100644
--- a/functions.sh
+++ b/functions.sh
@@ -43,7 +43,7 @@ mount_sys_esp() {
         mount --bind "$bootmnt" "$mountpoint"
         warn "System ESP not identified in device tree, using $bootmnt"
     else
-        mount "PARTUUID=$esp_uuid" "$mountpoint"
+        mount -t vfat "/dev/disk/by-partuuid/$esp_uuid" "$mountpoint"
     fi
     dev="$(grep "$mountpoint" /proc/mounts | cut -d" " -f1)"
     info "Mounted System ESP $dev at $mountpoint"
@@ -72,7 +72,7 @@ mount_boot_esp() {
             return 1
         fi
 
-        mount "PARTUUID=$esp_uuid" "$mountpoint"
+        mount -t vfat "/dev/disk/by-partuuid/$esp_uuid" "$mountpoint"
     fi
     info "Mounted Boot ESP at $mountpoint"
 }
diff --git a/initramfs/hooks/asahi b/initramfs/hooks/asahi
new file mode 100644
index 0000000..dd44b59
--- /dev/null
+++ b/initramfs/hooks/asahi
@@ -0,0 +1,39 @@
+#!/bin/sh
+
+PREREQ=""
+
+prereqs()
+{
+        echo "$PREREQ"
+}
+
+case $1 in
+prereqs)
+        prereqs
+        exit 0
+        ;;
+esac
+
+# Hooks for loading asahi modules and firmware
+
+. /usr/share/initramfs-tools/hook-functions
+
+mkdir -p "${DESTDIR}/lib/firmware"
+mkdir -p "${DESTDIR}/usr/share/asahi-scripts"
+cp -r /usr/share/asahi-scripts/* "${DESTDIR}/usr/share/asahi-scripts"
+
+# Debian
+copy_exec /usr/bin/cpio
+copy_exec /usr/bin/cut
+copy_exec /usr/bin/dirname
+copy_exec /usr/bin/grep
+copy_exec /usr/bin/mkdir
+copy_exec /usr/bin/seq
+copy_exec /usr/bin/sleep
+copy_exec /usr/sbin/modprobe
+
+manual_add_modules vfat
+manual_add_modules nls_ascii
+manual_add_modules nls_cp437
+
+add_modules_from_file /usr/share/asahi-scripts/modules
diff --git a/initramfs/kernel/zz-update-m1n1 b/initramfs/kernel/zz-update-m1n1
new file mode 100644
index 0000000..f5853c3
--- /dev/null
+++ b/initramfs/kernel/zz-update-m1n1
@@ -0,0 +1,26 @@
+#! /bin/sh
+set -e
+
+which update-m1n1 >/dev/null 2>&1 || exit 0
+
+if [ ! -e /usr/lib/m1n1/m1n1.bin ] && [ -e /usr/lib/m1n1/boot.bin ]; then
+	# looks like glanzmanns m1n1 package, not debians.
+	exit 0
+fi
+
+set -- $DEB_MAINT_PARAMS
+mode="${1#\'}"
+mode="${mode%\'}"
+case $0:$mode in
+    # Only run on postinst configure and postrm remove, to avoid wasting
+    # time by calling update-grub multiple times on upgrade and removal.
+    # Also run if we have no DEB_MAINT_PARAMS, in order to work with old
+    # kernel packages.
+    */postinst.d/*:|*/postinst.d/*:configure|*/postrm.d/*:|*/postrm.d/*:remove)
+	if [ -e /boot/efi/m1n1/boot.bin ]; then
+		exec update-m1n1
+	fi
+	;;
+esac
+
+exit 0
diff --git a/initramfs/modules b/initramfs/modules
new file mode 100644
index 0000000..6cbe56c
--- /dev/null
+++ b/initramfs/modules
@@ -0,0 +1,43 @@
+# For NVMe & SMC
+apple-mailbox
+# For NVMe
+nvme_apple
+# For USB and HID
+pinctrl-apple-gpio
+# SMC core
+macsmc
+macsmc-rtkit
+# For USB
+i2c-apple
+i2c-pasemi-platform
+tps6598x
+apple-dart
+dwc3
+dwc3-of-simple
+nvmem-apple-efuses
+phy-apple-atc
+xhci-pci
+xhci-plat-hcd
+pcie-apple
+gpio_macsmc
+# For HID
+spi-apple
+spi-hid-apple
+spi-hid-apple-of
+# For RTC
+rtc-macsmc
+spmi-apple-controller
+apple_nvmem_spmi
+# For RTC, obsoleted by linux 6.15,
+# drop in future release
+simple-mfd-spmi
+nvmem_spmi_mfd
+# For MTP HID
+apple-dockchannel
+dockchannel-hid
+apple-rtkit-helper
+# For DP / HDMI audio
+apple-sio
+# For DPTX and HDMI displays
+mux-apple-display-crossbar
+phy-apple-dptx
diff --git a/initramfs/scripts/init-bottom/asahi b/initramfs/scripts/init-bottom/asahi
new file mode 100644
index 0000000..cac593b
--- /dev/null
+++ b/initramfs/scripts/init-bottom/asahi
@@ -0,0 +1,19 @@
+#!/bin/sh
+
+PREREQ=""
+prereqs()
+{
+        echo "$PREREQ"
+}
+case $1 in
+# get pre-requisites
+prereqs)
+        prereqs
+        exit 0
+        ;;
+esac
+
+. /scripts/functions
+
+mount -t tmpfs -o mode=0755 vendorfw "${rootmnt}"/lib/firmware/vendor
+cp -a /vendorfw/* /vendorfw/.vendorfw.manifest "${rootmnt}"/lib/firmware/vendor
diff --git a/initramfs/scripts/init-top/asahi b/initramfs/scripts/init-top/asahi
new file mode 100644
index 0000000..2044e32
--- /dev/null
+++ b/initramfs/scripts/init-top/asahi
@@ -0,0 +1,61 @@
+#!/bin/sh
+
+PREREQ="udev"
+prereqs()
+{
+        echo "$PREREQ"
+}
+case $1 in
+# get pre-requisites
+prereqs)
+        prereqs
+        exit 0
+        ;;
+esac
+
+. /scripts/functions
+
+if [ -e /lib/firmware/vendor ]; then
+	log_failure_msg "Vendor firmware was loaded by the bootloader"
+	return 1
+fi
+
+if [ ! -e /proc/device-tree/chosen/asahi,efi-system-partition ]; then
+	log_failure_msg "Missing asahi,efi-system-partition variable, firmware will not be loaded!"
+	return 1
+fi
+
+log_success_msg "Load NVME modules"
+modprobe apple-mailbox
+modprobe nvme-apple
+modprobe xhci-plat-hcd
+
+for i in $(seq 0 50); do
+[ -e /sys/bus/platform/drivers/nvme-apple/*.nvme/nvme/nvme*/nvme*n1/ ] && break
+	sleep 0.1
+done
+
+if [ ! -e /sys/bus/platform/drivers/nvme-apple/*.nvme/nvme/nvme*/nvme*n1/ ]; then
+err "Timed out waiting for NVMe device"
+	return 1
+fi
+
+# If the above exists, hopefully the /dev device exists and this will work
+VENDORFW="/run/.system-efi/vendorfw/"
+
+(
+	. /usr/share/asahi-scripts/functions.sh
+	mount_sys_esp /run/.system-efi
+)
+
+if [ ! -e "$VENDORFW/firmware.cpio" ]; then
+	log_failure_msg "Vendor firmware not available in ESP!"
+	umount /run/.system-efi
+	return 1
+fi
+
+
+( cd /; cpio -i < "$VENDORFW/firmware.cpio" )
+umount /run/.system-efi
+
+log_success_msg "Asahi: Vendor firmware unpacked successfully"