/
uki-script
executable file
·228 lines (221 loc) · 8.36 KB
/
uki-script
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
#!/bin/bash
# uki-script - Create unified kernel images in Arch
# v1.0.1
#
# Written in 2020 by Tamvana Makuluni <sw@makuluni.com>
#
# To the extent possible under law, the author has dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
#
# You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>
get_ctime() {
ctime=0
files=
for file in $1; do
if [ -e "$file" ]; then
files="$files ${file@Q}"
file_time=`stat --printf %Y "$file" 2>/dev/null || printf "0"`
[ $file_time -gt $ctime ] && ctime="$file_time"
fi
done
[ $# -gt 1 ] && eval $2=$ctime
[ $# -gt 2 ] && eval $3=\$files
}
backup() {
let backup=$backups
ext="${1##*.}"
base="${1%%.$ext}"
while [ 0 -lt $backup ]; do
let backup-=1
backup_file="$base-fallback${backup##0}.$ext"
if [ $backup -eq 0 ]; then
from_file="$1"
else
let from=$backup-1
from_file="$base-fallback${from##0}.$ext"
fi
if [ -e "$from_file" ]; then
cp "$from_file" "$backup_file" || exit 1
fi
done
}
process_conf() {
unified=
kernel_name="${1%%.conf}"
kernel_name="${kernel_name##*/}"
build_backups=0
install_backups=1
ucode=/boot/*-ucode.img
ucode_initramfs=
cmdline="$(cat /proc/cmdline)"
splash="/usr/share/systemd/bootctl/splash-arch.bmp"
install=
sbctl_sign=
sign_key=
sign_cert=
efistub="/usr/lib/systemd/boot/efi/linuxx64.efi.stub"
osrel="/usr/lib/os-release"
echo "Building unified kernel image for kernel $kernel_name."
if [ -e "$conf" ]; then
while read -r line; do
case "${line%%:*}" in
\#*)
;;
kernel)
kernel_name="$(printf '%s' "${line:7}" | sed 's/^\s\+\|\s\+$//')"
;;
ucode)
ucode="$(printf '%s' "${line:6}" | sed 's/^\s\+\|\s\+$//')"
;;
splash)
splash="$(printf '%s' "${line:7}" | sed 's/^\s\+\|\s\+$//')"
;;
efistub)
efistub="$(printf '%s' "${line:8}" | sed 's/^\s\+\|\s\+$//')"
;;
command_line)
cmdline="${line:13}"
;;
build)
unified="$(printf '%s' "${line:6}" | sed 's/^\s\+\|\s\+$//')"
;;
install)
install="$(printf '%s' "${line:8}" | sed 's/^\s\+\|\s\+$//')"
;;
build_backups)
let build_backups="${line##*:}" v=1 || { echo "invalid config line: $line" && exit 1; }
;;
install_backups)
let install_backups="${line##*:}" v=1 || { echo "invalid config line: $line" && exit 1; }
;;
sbctl_sign)
sbctl_sign="$(printf '%s' "${line:11}" | sed 's/^\s\+\|\s\+$//')"
;;
sign_key)
sign_key="$(printf '%s' "${line:9}" | sed 's/^\s\+\|\s\+$//')"
;;
sign_cert)
sign_cert="$(printf '%s' "${line:10}" | sed 's/^\s\+\|\s\+$//')"
;;
*)
echo " Unknown option ${line%%:*}!" >&2
exit 1
;;
esac
done <"$conf"
fi
initramfs="/boot/initramfs-$kernel_name.img"
kernel="/boot/vmlinuz-$kernel_name"
if [ "x$unified" == "x" ]; then
unified="/boot/unified-image-$kernel_name.efi"
elif [ -d "$unified" ]; then
unified="$unified/unified-image-$kernel_name.efi"
fi
if [ -e "$initramfs" ] && [ -e "$kernel" ]; then
get_ctime "$initramfs" initramfs_ctime
get_ctime "$ucode" ucode_ctime ucode
if [ $ucode_ctime -gt 0 ]; then
ucode_initramfs="${initramfs%%.img}-withucode.img"
get_ctime "$ucode_initramfs"
if [ $ctime -lt $initramfs_ctime ] || [ $ctime -lt $ucode_ctime ]; then
eval "cat $ucode ${initramfs@Q} > ${ucode_initramfs@Q}"
fi
initramfs="$ucode_initramfs"
fi
get_ctime "$osrel" osrel_ctime
get_ctime "$conf" conf_ctime
get_ctime "$splash" splash_ctime
get_ctime "$kernel" kernel_ctime
get_ctime "$initramfs" initramfs_ctime
get_ctime "$efistub" efistub_ctime
get_ctime "$unified"
if $FORCE \
|| [ $ctime -lt $osrel_ctime ] \
|| [ $ctime -lt $conf_ctime ] \
|| [ $ctime -lt $splash_ctime ] \
|| [ $ctime -lt $kernel_ctime ] \
|| [ $ctime -lt $initramfs_ctime ] \
|| [ $ctime -lt $efistub_ctime ] \
; then
tmpfile="$(mktemp /tmp/uki-script.XXXXXX)"
backups=$build_backups
echo "$cmdline" > $tmpfile
backup "$unified"
osrelease_offset=$(objdump -h "$efistub" | awk 'NF==7 {size=strtonum("0x"$3);offset=strtonum("0x"$4)} END {print size + offset}')
cmdline_offset=$((osrelease_offset+$(stat -Lc%s "$osrel")))
linux_offset=$((cmdline_offset+$(stat -Lc%s "$tmpfile")))
if [ -e "$splash" ]; then
splash_offset=${linux_offset}
linux_offset=$((splash_offset+$(stat -Lc%s "$splash")))
fi
initrd_offset=$((linux_offset+$(stat -Lc%s "$kernel")))
if [ -e "$splash" ]; then
objcopy \
--add-section .osrel="$osrel" --change-section-vma .osrel=$(printf 0x%x $osrelease_offset) \
--add-section .cmdline="$tmpfile" --change-section-vma .cmdline=$(printf 0x%x $cmdline_offset) \
--add-section .splash="$splash" --change-section-vma .splash=$(printf 0x%x $splash_offset) \
--add-section .linux="$kernel" --change-section-vma .linux=$(printf 0x%x $linux_offset) \
--add-section .initrd="$initramfs" --change-section-vma .initrd=$(printf 0x%x $initrd_offset) \
"$efistub" "$unified" || exit 1
else
objcopy \
--add-section .osrel="$osrel" --change-section-vma .osrel=$(printf 0x%x $osrelease_offset) \
--add-section .cmdline="$tmpfile" --change-section-vma .cmdline=$(printf 0x%x $cmdline_offset) \
--add-section .linux="$kernel" --change-section-vma .linux=$(printf 0x%x $linux_offset) \
--add-section .initrd="$initramfs" --change-section-vma .initrd=$(printf 0x%x $initrd_offset) \
"$efistub" "$unified" || exit 1
fi
rm $tmpfile
if [ "x$sign_key" != "x" ] && [ "x$sign_cert" != "x" ]; then
sbsign --key "$sign_key" --cert "$sign_cert" --output "$unified" "$unified" || exit 1
fi
if [ "x$sbctl_sign" != "x" ]; then
sbctl sign "$unified" || exit 1
fi
if [ -e "$unified" ] && [ -d "$install" ]; then
backups=$install_backups
backup "$install/$kernel_name.efi"
cp "$unified" "$install/$kernel_name.efi" || exit 1
fi
echo " Done."
else
echo " Image $kernel_name is up to date."
fi
fi
}
if ! args="$(getopt --options f --longoptions force -- "$@")"; then
exit 1
fi
eval "set -- $args"
FORCE=false
CONTINUE=true
while $CONTINUE; do
case $1 in
-f)
FORCE=true
;;
--force)
FORCE=true
;;
--)
CONTINUE=false
;;
esac
shift
done
if [ "$#" -gt 0 ]; then
for conf in "$@"; do
if [ -f "$conf" ]; then
process_conf "$conf"
elif [ -f "/etc/uki-script/$conf.conf" ]; then
process_conf "/etc/uki-script/$conf.conf"
else
echo "Could not find configuration ${conf@Q}" >&2
exit 1
fi
done
else
for conf in /etc/uki-script/*.conf; do
process_conf "$conf"
done
fi
exit 0