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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
#!/bin/bash
[ "$EUID" = 0 ] ||
{ echo "ERROR: Please run as root" && exit 1; }
. "/usr/share/grub/grub-mkconfig_lib"
# Android x86 supported file systems
# Note that by default, mostly Android initrds don't support btrfs and some other
# filesystems
# For higher versions, btrfs and some other filesystems are supported
: "${GRUB_ANDROID_SUPPORTED_FS:="ext2 ext3 ext4 vfat exfat"}"
# Excluded paths
# Start with a slash, separated by ',', ending slashes are not needed
: "${GRUB_ANDROID_EXCLUDE:="/bin,/boot/efi,/efi,/EFI,/dev,/etc,/lib,/lib64,/lib32,/proc,/run,/sbin,/sys,/usr,/tmp,/Program Files,/Program Files (x86),/ProgramData,/Windows,/Volumes,/System,/Library,/cores"}"
# Search image names
: "${GRUB_ANDROID_INCLUDE:="system system.img system.sfs system.efs system_a system_a.img system_a.sfs system_a.efs system_b system_b.img system_b.sfs system_b.efs"}"
# Search recursiveness depth
: "${GRUB_ANDROID_SEARCH_DEPTH:=3}"
# Default parameters
: "${GRUB_CMDLINE_ANDROID:=""}"
. /etc/default/grub
mapdevfs() { readlink -f "$1"; }
blkol() { blkid "$2" | grep -ioE "$1=\"[a-z0-9-]+\"" | awk -F'"' '{print $2}'; }
gfind() { grep "$2" <<<"${1}"; }
gprop() { gfind "${1}" "ro.$2=" | awk -F"=" '{print $2}'; }
gver() { gprop "${1}" "$2.version${3:+.$3}"; }
on_sataraid() {
type dmraid >/dev/null 2>&1 || return 1
local parent="${1%/*}"
local device="/dev/${parent##*/}"
if dmraid -r -c | grep -q "$device"; then
return 0
fi
return 1
}
parse_proc_mdstat() {
if type udevadm >/dev/null 2>&1; then
udevinfo() {
udevadm info "$@"
}
fi
# shellcheck disable=SC2162
while read line; do
for word in $line; do
dev="${word%%\[*}"
# TODO: factor this out to something in di-utils if
# it's needed elsewhere
if [ -d /sys/block ] && type udevinfo >/dev/null 2>&1; then
if ! udevinfo -q path -n "/dev/$dev" 2>/dev/null |
grep -q '/.*/.*/'; then
continue
fi
elif ! echo "$dev" | grep -q "/part"; then
continue
fi
raidpart="/dev/$dev"
echo "$(mapdevfs "$raidpart")"
done
done
}
parse_proc_swaps() {
while read line; do
set -f
set -- $line
set +f
echo "$(mapdevfs $1) swap"
done
}
partitions() {
# Exclude partitions that have whole_disk sysfs attribute set.
if [ -d /sys/block ]; then
# Exclude partitions on physical disks that are part of a
# Serial ATA RAID disk.
for part in /sys/block/*/*[0-9]; do
if [ -f "$part/start" ] &&
[ ! -f "$part/whole_disk" ] && ! on_sataraid $part; then
name="$(echo "${part##*/}" | sed 's,[!.],/,g')"
if [ -e "/dev/$name" ]; then
echo "/dev/$name"
fi
fi
done
# Add Serial ATA RAID devices
if type dmraid >/dev/null 2>&1 &&
dmraid -s -c >/dev/null 2>&1; then
for raidset in $(dmraid -sa -c); do
for part in /dev/mapper/"$raidset"*[0-9]; do
echo "$part"
done
done
fi
elif [ "$(uname -s)" = Linux ]; then
echo "Cannot find list of partitions! (Try mounting /sys.)" >&2
exit 1
else
# We don't know how to probe OSes on non-Linux kernels. For
# now, just don't get in the way.
exit 0
fi
# Also detect OSes on LVM volumes (assumes LVM is active)
if type lvs >/dev/null 2>&1; then
echo "$(
LVM_SUPPRESS_FD_WARNINGS=1 lvs --noheadings --separator : -o vg_name,lv_name 2>&1 |
sed "s|-|--|g;s|^[[:space:]]*\(.*\):\(.*\)$|/dev/mapper/\1-\2|" |
grep -v "No volume groups found"
)"
fi
}
parse_path() {
[ "${1}" ] || return 1
local path \
path_sysimg=$1 \
device=$2 \
buildprop \
abi \
buildver \
os \
vendor \
src \
microcode
path=$(dirname "$1")
shift 2
# Getting build.prop contents
buildprop="$(find_buildprop "$path_sysimg")" || return 1
# Get abi
abi=$(gprop "$buildprop" "product.cpu.abi")
case "$abi" in *64*) [ "$LM" ] || return 1 ;;
esac
case "$abi" in
x86*) : x86 ;;
arm*) : arm ;;
*) : ;;
esac
[ "$_" = "$GENKERNEL_ARCH" ] || return 1
# Parse version
buildver="$(grep -E "ro.([a-z]*).version" <<<"$(grep -v build <<<"${buildprop}")")"
for version in $buildver; do
case $version in
ro.bliss.version*)
os="Bliss OS $(gver "$buildver" "bliss")"
break
;;
ro.phoenix.version*)
os="Phoenix OS $(gver "$buildver" "phoenix" "code")"
break
;;
ro.primeos.version*)
os="Prime OS $(gver "$buildver" "primeos" "name")"
break
;;
ro.lineage.version*)
os="Lineage OS $(gver "$buildver" "lineage")"
;;
*)
os="AOSP $(gver "$buildver" "build" "release") $(gprop "$buildprop" "build.flavor")"
;;
esac
done
src=$(make_system_path_relative_to_its_root "$path")
# Checking for cpu id/manufacturer and microcode file
case "$(grep vendor_id /proc/cpuinfo | head -1 | awk '{print $3}')" in
"GenuineIntel") : "intel" ;;
"AuthenticAMD") : "amd" ;;
esac
vendor=$_
local kernels initrds ucodes system data
# shellcheck disable=SC2144
if [ -d "$path"/gearlock ] || [ -e "$path"/gearlock.??? ]; then
[ -d "$path"/gearlock ] &&
kernels=gearlock ||
kernels=$(ls -d "$path"/gearlock.??? || head -1) &&
kernels=$(basename "$kernels")
list=
for i in "$path"/boot/kernel-*; do
grub_file_is_not_garbage "$i" || continue
version="${i#*-}"
[ -e "$path/boot/initrd-${version}.img" ] && list="$version,$list"
done
initrds=${list%,}
system=$(basename "$path_sysimg")
data=$(ls -d "$path"/data* || head -1)
data=$(basename "$data")
for microcode in "$path"/boot/${vendor}-ucode.img "$path"/boot/${vendor}-microcode.img; do
[ -f "$microcode" ] &&
ucodes="$ucodes,$(basename "$microcode")"
done
elif [ -e "$path"/misc.img ]; then
kernels=ab
for microcode in "$path"/${vendor}-ucode.img "$path"/${vendor}-microcode.img; do
[ -f "$microcode" ] &&
ucodes="$ucodes,$(basename "$microcode")"
done
else
for kernel in "$path"/vmlinu[xz]* "$path"/kernel*; do
grub_file_is_not_garbage "$kernel" &&
kernels="$(basename "$kernel"),$kernels"
done
for initrd in "$path"/initrd*.img "$path"/initramfs*.img; do
grub_file_is_not_garbage "$initrd" &&
initrds="$(basename "$initrd"),$initrds"
done
for microcode in "$path"/${vendor}-ucode.img "$path"/${vendor}-microcode.img; do
[ -f "$microcode" ] &&
ucodes="$ucodes,$(basename "$microcode")"
done
fi
printf '%s\0%s\0%s\0%s\0%s\0%s\0%s\0%s\n' \
"${device}" \
"${src}" \
"${os}" \
"${kernels%,}" \
"${initrds%,}" \
"${ucodes#,}" \
"${system}" \
"${data}"
}
find_os() {
local mountpoint=$1 device=$2 foundpaths EXCLUDE=""
IFS=,
for dir in ${GRUB_ANDROID_EXCLUDE}; do
EXCLUDE="$EXCLUDE -not \( -path \"${mountpoint}${dir/ /\\ }/*\" -prune \)"
done
unset IFS
foundpaths="$(eval 'timeout 30 find "'"$mountpoint"'" -maxdepth '"$DEPTH"' \(' "$INCLUDE" '\)' "$EXCLUDE")" || return 1
while read -r path; do
parse_path "$path" "$device" || continue
done <<<"$foundpaths"
}
find_buildprop() {
if [ -d "$1" ]; then
local buildprop_file=$1/system/build.prop
[ -f "$buildprop_file" ] || buildprop_file=$1/build.prop
[ -f "$buildprop_file" ] || return 1
cat "$buildprop_file"
elif [ -f "$1" ]; then
case "$(basename "$1")" in
system*.img)
imgdir="$(tmpmount "$1" loop)" || return 1
find_buildprop "$imgdir"
umount -lf "$imgdir"
rm -rf "$imgdir"
;;
system*.?fs)
sfsdir="$(tmpmount "$1" loop)" || return 1
find_buildprop "$sfsdir/system.img"
umount -lf "$sfsdir"
rm -rf "$sfsdir"
;;
esac
fi
return $?
}
tmpmount() {
local disktmp opts=${2:+,$2} fs=${3:-auto}
disktmp="$(mktemp)"
rm -f "$disktmp"
mkdir -p "$disktmp" || true
if ! { type grub-mount && grub-mount "$1" "$disktmp"; } >/dev/null; then
[ "$(blkol TYPE "$1")" = "ntfs" ] && fs=$NTFS
# try with no `force` flag first
mount -o ro$opts -t "$fs" "$1" "$disktmp" 2>/dev/null ||
mount -o ro,force$opts -t "$fs" "$1" "$disktmp" 2>/dev/null
fi
echo "$disktmp"
}
mount_find() {
# check if mount-able, else search from highest top level
local part_mnt mnt_pt fs
fs=$(blkol TYPE "$1")
[ "$fs" ] && gfind "$GRUB_ANDROID_SUPPORTED_FS" "$fs" >/dev/null || return
part_mnt=$(tmpmount "$1")
[ "$part_mnt" ] &&
mnt_pt=$part_mnt ||
mnt_pt="$(grep "$1" /proc/mounts | head -1)" ||
return 1
find_os "$mnt_pt" "$1"
if [ "$part_mnt" ]; then
umount -lf "$part_mnt"
rm -rf "$part_mnt"
fi
}
DEPTH=$GRUB_ANDROID_SEARCH_DEPTH
po=
for f in ${GRUB_ANDROID_INCLUDE}; do
INCLUDE="$INCLUDE $po -name \"$f\""
po="-o"
done
# check cpu architecure
machine=$(uname -m)
case "$machine" in
i?86 | x86_*) : x86 ;;
arm*) : arm ;;
*)
echo "ERROR: Unsupported processor architecture!" >&2
exit 1
;;
esac
GENKERNEL_ARCH=$_
# check if cpu is 64 bit capable
LM=
grep -oqw 'lm' /proc/cpuinfo && LM=1
# check if kernel supports ntfs3
modinfo ntfs3 >/dev/null && NTFS=ntfs3 || NTFS=ntfs-3g
mkdir -p /tmp/aprob
: >"/tmp/aprob/swaps-map"
if [ -f /proc/swaps ]; then
grep "^/dev/" /proc/swaps | parse_proc_swaps >"/tmp/aprob/swaps-map" || true
fi
: >"/tmp/aprob/raided-map"
if [ -f /proc/mdstat ]; then
grep "^md" /proc/mdstat | parse_proc_mdstat >"/tmp/aprob/raided-map" || true
fi
for partition in $(partitions); do
if ! mapped="$(mapdevfs "$partition")"; then
continue
fi
# Skip partitions used in software RAID arrays
if grep -q "^$mapped" "/tmp/aprob//raided-map"; then
continue
fi
# Skip partitions used as active swap
if grep -q "^$mapped " "/tmp/aprob//swaps-map"; then
continue
fi
mount_find "$partition"
done |
sed 's/\xFF/\xFF\xFF/g; s/\x00/\xFF0/g'
|