-
Notifications
You must be signed in to change notification settings - Fork 10
/
recomod.sh
executable file
·473 lines (379 loc) · 13.7 KB
/
recomod.sh
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#!/usr/bin/env bash
SCRIPT_DIR=$(dirname "$0")
SCRIPT_DIR=${SCRIPT_DIR:-"."}
. "$SCRIPT_DIR/lib/common_minimal.sh"
FUS_SOURCES=https://raw.githubusercontent.com/MrChromebox/scripts/master/sources.sh
cleanup(){
umount "$ROOT" || :
losetup -d "$loopdev"
}
leave() {
trap - EXIT
exit "$1"
}
quit() {
trap - EXIT
echo -e "\x1b[31mExiting: $1\x1b[39;49m" >&2
exit "$2"
}
fbool(){
[ "$(eval echo $(echo \"\$FLAGS_$1\"))" = "$FLAGS_TRUE" ]
}
should_debug(){
fbool debug && ! fbool quiet
}
debug() {
if should_debug; then
echo -e "\x1B[33mDebug: $*\x1b[39;49m" >&2
fi
}
suppress() {
if should_debug; then
$@
else
$@ >/dev/null 2>&1
fi
}
suppress_err() {
if should_debug; then
$@
else
$@ 2>/dev/null
fi
}
info() {
if ! fbool quiet; then
echo -e "\x1B[32mInfo: $*\x1b[39;49m"
fi
}
traps() {
set -e
trap 'last_command=$current_command; current_command=$BASH_COMMAND' DEBUG
trap 'echo -e "\x1b[31m\"${last_command}\": "$BASH_COMMAND" command failed with exit code $?. THIS IS A BUG, REPORT IT HERE https://github.com/MercuryWorkshop/RecoMod\x1b[31m"' EXIT
}
configure_binaries() {
if ! suppress which curl; then
quit "curl binary not found! You must install curl. On debian/ubuntu, run sudo apt install curl" 1
fi
if ! suppress which cgpt; then
quit "cgpt binary not found! You must install cgpt. On debian/ubuntu, run sudo apt install cgpt" 1
fi
if ! suppress which futility; then
quit "futility binary not found! You must install vboot-utils. On debian/ubuntu, run sudo apt install vboot-kernel-utils and on arch the package is vboot-utils from the AUR" 1
fi
if [ -f /sbin/ssd_util.sh ]; then
SSD_UTIL=/sbin/ssd_util.sh
elif [ -f /usr/share/vboot/bin/ssd_util.sh ]; then
SSD_UTIL=/usr/share/vboot/bin/ssd_util.sh
elif [ -f "${SCRIPT_DIR}/lib/ssd_util.sh" ]; then
SSD_UTIL="${SCRIPT_DIR}/lib/ssd_util.sh"
else
quit "Cannot find the required ssd_util script. Please make sure you're executing this script inside the directory it resides in" 1
fi
if ! fbool minimal; then
info "Downloading clamide into lib/"
curl -sL https://github.com/CoolElectronics/clamide/releases/latest/download/clamide -o "${SCRIPT_DIR}/lib/clamide"
info "Downloading pv into lib/"
if ! [ -f lib/pv ]; then
curl -sL https://github.com/mosajjal/binary-tools/raw/master/x64/pv -o lib/pv
fi
fi
if fbool rw_legacy; then
info "Downloading latest rw_legacy payloads into lib/rwl"
mkdir lib/rwl || :
rwlegacy_source="$(. <(curl -Ls "$FUS_SOURCES"); echo $rwlegacy_source)"
files="$(. <(curl -Ls "$FUS_SOURCES"); env | grep -e rwl_altfw -e seabios)"
while read file; do
key=${file%%=*}
val=${file##*=}
debug "Downloading $key: $val"
curl -sL "${rwlegacy_source}${val}" -o "lib/rwl/$val"
done <<< "$files"
fi
if fbool fullrom; then
quit "Sorry! I haven't gotten around to adding in fullrom support yet. Check back later" 1
fi
if fbool strip; then
if suppress which sfdisk && [[ "$(sfdisk -v)" == "sfdisk from util-linux 2.38."* ]]; then
debug "using machine's sfdisk"
SFDISK=$(which sfdisk)
elif [ -f "${SCRIPT_DIR}/lib/sfdisk" ] && [ "$(uname -m)" = "x86_64" ] && [[ "$(uname)" == *Linux* ]]; then
debug "using static sfdisk"
SFDISK="${SCRIPT_DIR}/lib/sfdisk"
chmod +x "$SFDISK"
else
quit "Could not find a working version of sfdisk.
If you are using a 32 bit or ARM system (or god forbid a mac), please make sure you have exactly version 2.38.1 of sfdisk installed, as it's the only one that works."
fi
fi
}
getopts() {
load_shflags
FLAGS_HELP="USAGE: $0 -i /path/to/recovery_image.bin [flags]"
DEFINE_string image "" \
"path to the recovery image you want to patch" "i"
DEFINE_string kit "./example-kit" \
"specify the path to the toolkit folder that will be injected" "k"
DEFINE_boolean help "$FLAGS_FALSE" \
"print usage" "h"
DEFINE_boolean quiet "$FLAGS_FALSE" \
"do not print anything to stdout" "q"
DEFINE_boolean disable-verity "$FLAGS_TRUE" \
"disable dm-verity on the kernel that will be installed during recovery. enabling this will make the image faster to launch, but if the image is installed through recovery it will only be able to boot in developer mode" ""
DEFINE_boolean minimal "$FLAGS_FALSE" \
"build a non interactive version of the toolkit. IF YOU HAVE AN ARM-BASED CHROMEBOOK, YOU MUST ENABLE THIS" ""
DEFINE_boolean cleanup "$FLAGS_TRUE" \
"clean up gracefully after the script finishes. disable this for debugging purposes" ""
DEFINE_boolean rw_legacy "$FLAGS_TRUE" \
"download files from mrchromebox.tech for use in the rw_legacy firmware configuration menu" ""
DEFINE_boolean fullrom "$FLAGS_FALSE" \
"download files from mrchromebox.tech for use in the full uefi rom firmware configuration menu. This will take up a lot of space, only do this if you know you need it." ""
DEFINE_boolean strip "$FLAGS_FALSE" \
"reduce the size of the recovery image by deleting everything that isn't neccessary. The image will no longer be able to recover chrome os" ""
DEFINE_boolean halcyon "$FLAGS_FALSE" \
"enable E-HALCYON patches. if you don't know what that means, leave this option alone" ""
FLAGS "$@" || leave $?
eval set -- "$FLAGS_ARGV"
if fbool halcyon && (fbool strip || fbool minimal); then
quit "--halcyon and --strip/--minimal are incompatible" 1
fi
if [ -z "$FLAGS_image" ]; then
flags_help || :
leave 1
fi
}
# https://chromium.googlesource.com/chromiumos/docs/+/master/lsb-release.md
lsbval() {
local key="$1"
local lsbfile="${2:-/etc/lsb-release}"
if ! echo "${key}" | grep -Eq '^[a-zA-Z0-9_]+$'; then
return 1
fi
sed -E -n -e \
"/^[[:space:]]*${key}[[:space:]]*=/{
s:^[^=]+=[[:space:]]*::
s:[[:space:]]+$::
p
}" "${lsbfile}"
}
patch_root_complete() {
cp "utils/bootstrap-shell.sh" "$ROOT/usr/sbin/bootstrap-shell"
chmod +x "$ROOT/usr/sbin/bootstrap-shell"
cp lib/clamide "$ROOT/usr/sbin/clamide"
chmod +x "$ROOT/usr/sbin/clamide"
# we don't neeeeeed pv, but it looks cool
cp lib/pv "$ROOT/usr/sbin/pv"
chmod +x "$ROOT/usr/sbin/pv"
cp -r "$FLAGS_kit" "$ROOT/usr/recokit"
chmod +x "$ROOT/usr/recokit/"*
mv "$ROOT/usr/sbin/chromeos-recovery" "$ROOT/usr/sbin/chromeos-recovery.old"
cp "utils/chromeos-recovery.sh" "$ROOT/usr/sbin/chromeos-recovery"
chmod +x "$ROOT/usr/sbin/chromeos-recovery"
chmod +x "$ROOT/usr/sbin/chromeos-recovery.old"
if fbool halcyon; then
local milestone=$(lsbval CHROMEOS_RELEASE_CHROME_MILESTONE "$ROOT/etc/lsb-release")
if (( milestone > 107 )); then
cleanup
quit "You are trying to use halcyon on an image of version R${milestone}. Due to a change in chromeos_startup.sh, this is not supported. Please download an R107 image instead" 1
fi
if (( milestone != 107 )); then
info "WARNING: ${bin} is not an R107 image. Proceeding anyway, but remember that R${milestone} is untested"
fi
info "Installing halcyon patches onto an R${milestone} image"
cat <<EOF >"$ROOT/usr/sbin/wipe_disk"
#!/bin/bash
echo "E mode activated"
exec /usr/sbin/chromeos-recovery
EOF
chmod +x "$ROOT/usr/sbin/wipe_disk"
# snippet sourced from https://github.com/sebanc/brunch/blob/r107/brunch-patches/40-custom_encryption.sh, licensed under GPLv3
cat <<EOF >"$ROOT/usr/share/cros/startup_utils.sh"
mount_var_and_home_chronos() {
mkdir -p /mnt/stateful_partition/encrypted/var
mount -n --bind /mnt/stateful_partition/encrypted/var /var || return 1
mkdir -p /mnt/stateful_partition/encrypted/chronos
mount -n --bind /mnt/stateful_partition/encrypted/chronos /home/chronos || return 1
}
umount_var_and_home_chronos() {
umount /home/chronos
umount /var
}
EOF
sed -i "s/# Check if we enable ext4 features\./STATE_DEV=\$(\. \/usr\/sbin\/write_gpt.sh;\. \/usr\/share\/misc\/chromeos-common\.sh;load_base_vars;get_fixed_dst_drive)p1/" "$ROOT/sbin/chromeos_startup.sh"
sed -i "s/stable/dev/" "$ROOT/etc/lsb-release"
sed -i "s/end script/sleep 2;tpm_manager_client take_ownership;restart cryptohomed;sleep 1\nend script/" "$ROOT/etc/init/ui.conf"
>"$ROOT/usr/recokit/halcyon_enabled"
fi
if fbool rw_legacy; then
tar -czvf "$ROOT/usr/recokit/rwl.tar.gz" -C "lib/rwl" .
>"$ROOT/usr/recokit/rw_legacy_enabled"
fi
if fbool fullrom; then
>"$ROOT/usr/recokit/fullrom_enabled"
fi
}
patch_root_minimal() {
cp "$FLAGS_kit/main-minimal.sh" "$ROOT/usr/sbin/chromeos-recovery"
cp -r "$FLAGS_kit" "$ROOT/usr/recokit"
chmod +x "$ROOT/usr/sbin/chromeos-recovery"
}
strip_root() {
# we don't usually need to install chrome, stripping can get the file size down
rm -rf "$ROOT/opt"
rm -rf "$ROOT/usr/libexec"
rm -rf "$ROOT/usr/share/chromeos-assets"
rm -rf "$ROOT/usr/share/fonts"
rm -rf "$ROOT/usr/lib64/va"
rm -rf "$ROOT/usr/lib64/dri"
rm -rf "$ROOT/usr/lib64/samba"
rm -rf "$ROOT/usr/share/vim" # :D
rm -rf "$ROOT/usr/share/cros-camera"
rm -rf "$ROOT/usr/share/X11"
rm -rf "$ROOT/usr/lib64/libimedecoder.so"
rm -f "$ROOT/usr/sbin/chromeos-firmwareupdate"
>"$ROOT/usr/stripped"
}
shrink_table() {
local buffer=5000000 #5mb buffer. keeps things from breaking too much
suppress e2fsck -fy "${loopdev}p3"
suppress resize2fs -M "${loopdev}p3"
local block_size
block_size=$(tune2fs -l "${loopdev}p3" | grep -i "block size" | awk '{print $3}')
local sector_size
sector_size=$(fdisk -l "${loopdev}" | grep "Sector size" | awk '{print $4}')
local block_count
block_count=$(tune2fs -l "${loopdev}p3" | grep -i "block count" | awk '{print $3}')
block_count=${block_count%%[[:space:]]*}
debug "bs: $block_size, blocks: $block_count"
local raw_bytes=$((block_count * block_size))
local resized_size=$((raw_bytes + buffer))
local fdisk_ra_entry
fdisk_ra_entry=$(fdisk -u -l "${loopdev}" | grep "${loopdev}p3")
local start_sector
start_sector=$(awk '{print $2}' <<<"$fdisk_ra_entry")
local end_sector
end_sector=$(awk '{print $3}' <<<"$fdisk_ra_entry")
local start_bytes=$((start_sector * sector_size))
local end_bytes=$((end_sector * sector_size))
local resized_end=$((start_bytes + resized_size))
debug "real size: $raw_bytes bytes"
info "resizing ${loopdev}p3 to $resized_size bytes"
debug "start of ${loopdev}p3 is $start_bytes bytes. changing end from $end_bytes bytes to $resized_end bytes"
# script will die if i connect a stream to /dev/null for whatever reason, giving birth to this particular bit of jank
local jankfile
jankfile=$(mktemp)
# if you're wondering why i don't use --script, it causes changes to not apply
# if you're wondering why i don't use <<EOF, it causes changes not to apply
# stupid fucking gnu devs
script /dev/null -c "parted ${loopdev} resizepart 3 ${resized_end}B" <<<"Yes" >"$jankfile"
suppress cat "$jankfile"
rm -f "$jankfile"
local numparts=12
local numtries=6
i=0
while [ $i -le $numtries ]; do
j=1
while [ $j -le $numparts ]; do
suppress "$SFDISK" -N $j --move-data "${loopdev}" <<<"+,-" || :
j=$((j+1))
done
i=$((i+1))
done
}
truncate_image() {
local buffer=35 # magic number to ward off evil gpt corruption spirits
local img=$1
local fdisk_stateful_entry
fdisk_stateful_entry=$(fdisk -l "$img" | grep "${img}1[[:space:]]")
local sector_size
sector_size=$(fdisk -l "$img" | grep "Sector size" | awk '{print $4}')
local end_sector
end_sector=$(awk '{print $3}' <<<"$fdisk_stateful_entry")
local end_bytes=$(((end_sector + buffer) * sector_size))
info "truncating image to $end_bytes bytes"
truncate -s $end_bytes "$img"
suppress gdisk "$img" << EOF
w
y
EOF
}
main() {
if [ ! -f "$FLAGS_image" ]; then
quit "\"$FLAGS_image\" is not a real file!!! You need to pass the path to the recovery image" 1
fi
local bin=$FLAGS_image
debug "Supplied image: $bin"
info "Creating loopback device"
local loopdev
loopdev=$(losetup -f)
losetup -P "$loopdev" "$bin"
debug "Setup loopback at $loopdev"
if fbool disable-verity; then
suppress "$SSD_UTIL" --remove_rootfs_verification -i "$loopdev" --partitions 4
fi
suppress "$SSD_UTIL" --remove_rootfs_verification --no_resign_kernel -i "$loopdev" --partitions 2
# for good measure
sync
ROOT=$(mktemp -d)
mount "${loopdev}p3" "$ROOT"
debug "Mounted root at $ROOT"
if fbool strip; then
info "Stripping uneeded components"
strip_root
fi
if fbool minimal; then
info "Installing minimal toolkit"
patch_root_minimal
else
info "Installing toolkit"
patch_root_complete
fi
if ! fbool cleanup; then
quit "Patching successful. skipping cleanup, please do it yourself!" 0
fi
sleep 0.2
sync
umount "$ROOT"
if fbool strip; then
info "Shrinking GPT table"
shrink_table
fi
losetup -d "$loopdev"
sync
sleep 0.2
if fbool strip; then
truncate_image "$bin"
fi
sync
sleep 0.2
rm -rf "$ROOT"
info "Patching successful, happy hacking!"
}
# make this sourceable for testing
if [ "$0" = "$BASH_SOURCE" ]; then
stty sane
traps
getopts "$@"
if [ "$EUID" -ne 0 ]; then
quit "Please run as root" 1
fi
if [ "$FLAGS_debug" = "$FLAGS_TRUE" ] && [ "$FLAGS_quiet" = "$FLAGS_FALSE" ]; then
set -x
fi
if [[ $(uname -r) =~ Microsoft$ ]] || grep -qEi "(Microsoft|WSL)" /proc/version &> /dev/null || [[ -n "$IS_WSL" || -n "$WSL_DISTRO_NAME" ]]; then
info "WSL DETECTED!!!!!!!!!!!!!!!!!!!!
WSL IS NOT SUPPORTED, PLEASE DO NOT FILE AN ISSUE IF THE SCRIPT RUNS INTO AN ERROR
THE SCRIPT WILL CONTINUE TO RUN, BUT IT MAY NOT WORK"
fi
# breaks without this
if suppress which sysctl; then
orig_sysctl=$(sysctl --values fs.protected_regular || :)
suppress sysctl -w fs.protected_regular=0 || :
fi
configure_binaries
main
if suppress which sysctl; then
suppress sysctl -w "fs.protected_regular=$orig_sysctl" || :
fi
leave 0
fi