forked from CyanogenMod/android_bootable_recovery
-
Notifications
You must be signed in to change notification settings - Fork 8
/
system-image-upgrader
executable file
·587 lines (499 loc) · 17 KB
/
system-image-upgrader
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
#!/sbin/sh
set -e
echo "Starting image Upgrade pre" > /cache/system-image-upgrader.log
if [ ! -e "$1" ]; then
echo "Command file doesn't exist: $1"
exit 1
fi
mv $1 $1.applying
COMMAND_FILE=$1.applying
REMOVE_LIST="$COMMAND_FILE"
# Used as a security check to see if we would change the password
DATA_FORMAT=0
# System Mountpoint
SYSTEM_MOUNTPOINT=/cache/system
echo "Starting image upgrader: $(date)" >> /cache/test.log
# Functions
check_filesystem() {
# $1 => image to check (partition or device img)
if [ ! -e $1 ]; then
echo "Partition/image not found: $1"
return 1
fi
# It's fine for e2fsck to return something different than 0
set +e
e2fsck -yf $1
ret=$?
# From e2fsck man page:
# 0 - No errors
# 1 - File system errors corrected
if [ $ret -ne 0 ] && [ $ret -ne 1 ]; then
echo "e2fsck is unable to fix partition/image $1, aborting (return code $ret)"
exit 1
fi
set -e
}
verify_signature() {
# $1 => validation keyring name
# $2 => path to validate
if [ -e /etc/system-image/skip-gpg-verification ]; then
echo "Skipping GPG verification"
return 0
fi
if [ ! -e $2 ]; then
echo "File doesn't exist: $2"
return 1
fi
# Check against the blacklist
if [ -e /tmp/system-image/blacklist/pubring.gpg ]; then
export GNUPGHOME=/tmp/system-image/blacklist/
if gpg --ignore-time-conflict --verify $2 >/dev/null 2>&1; then
echo "File signed by a blacklisted key: $2"
return 1
fi
fi
# Check against the keyring
export GNUPGHOME=/tmp/system-image/$1/
if [ ! -e "$GNUPGHOME" ]; then
echo "Keyring doesn't exist: $1"
return 1
fi
if gpg --ignore-time-conflict --verify $2 >/dev/null 2>&1; then
return 0
fi
return 1
}
install_keyring() {
# $1 => full path to tarball
# $2 => full path to signature
# Some basic checks
if [ ! -e "$1" ] || [ ! -e "$2" ]; then
echo "Missing keyring files: $1 => $2"
return 1
fi
# Unpacking
TMPDIR=$(mktemp -dt -p /tmp/system-image/ tmp.XXXXXXXXXX)
cd $TMPDIR
xzcat $1 | tar --numeric-owner -xf -
if [ ! -e keyring.json ] || [ ! -e keyring.gpg ]; then
rm -Rf $TMPDIR
echo "Invalid keyring: $1"
return 1
fi
# Extract the expiry
keyring_expiry=$(grep "^ \"expiry\": " keyring.json | cut -d: -f2 | sed -e "s/[ \",]//g")
if [ -n "$keyring_expiry" ] && [ "$keyring_expiry" -lt "$(date +%s)" ]; then
rm -Rf $TMPDIR
echo "Keyring expired: $1"
return 1
fi
# Extract the keyring type
keyring_type=$(grep "^ \"type\": " keyring.json | cut -d: -f2 | sed -e "s/[, \"]//g")
if [ -z "$keyring_type" ]; then
rm -Rf $TMPDIR
echo "Missing keyring type: $1"
return 1
fi
if [ -e /tmp/system-image/$keyring_type ]; then
rm -Rf $TMPDIR
echo "Keyring already loaded: $1"
return 1
fi
signer="unknown"
case "$keyring_type" in
archive-master)
signer=""
;;
image-master)
signer="archive-master"
;;
image-signing|blacklist)
signer="image-master"
;;
device-signing)
signer="image-signing"
;;
esac
if [ -n "$signer" ] && ! verify_signature $signer $2; then
rm -Rf $TMPDIR
echo "Invalid signature: $1"
return 1
fi
mkdir /tmp/system-image/$keyring_type
chmod 700 /tmp/system-image/$keyring_type
mv $TMPDIR/keyring.gpg /tmp/system-image/$keyring_type/pubring.gpg
chmod 600 /tmp/system-image/$keyring_type/pubring.gpg
chown 0:0 /tmp/system-image/$keyring_type/pubring.gpg
rm -Rf $TMPDIR
return 0
}
property_write() {
prop="$1"
prop_value="$2"
prop_dir="/data/android-data/property"
# everything is wiped after a format, let's get a skeleton going
mkdir -p "$prop_dir"
chown 0:0 "$prop_dir"
chmod 700 "$prop_dir"
echo -n "$prop_value" > "$prop_dir/$prop"
# properties won't be read if they aren't ro root
chown 0:0 "$prop_dir/$prop"
chmod 600 "$prop_dir/$prop"
}
adb_onlock() {
if [ "$DATA_FORMAT" -eq 0 ]; then
return 1
fi
flag="/data/.adb_onlock"
# if the param != "true" we just delete the flag
case $1 in
true)
touch "$flag"
;;
false)
rm -f "$flag"
;;
*)
echo "Unkown parameter $1, disabling"
rm -f "$flag"
;;
esac
}
set_password() {
if [ "$DATA_FORMAT" -eq 0 ]; then
return 1
fi
user="phablet"
password="$1"
if [ -z "$password" ]; then
return 1
fi
path=/bin:/usr/bin:/sbin:/usr/sbin
PATH=$path chroot "$SYSTEM_MOUNTPOINT" /bin/sh -c "echo -n "$user:$password" | chpasswd"
return 0
}
unset_password() {
if [ "$DATA_FORMAT" -eq 0 ]; then
return 1
fi
# Needs implementation
}
usb_enable() {
prop_dir="/data/android-data/property"
prop="persist.sys.usb.config"
prop_val="$1"
# Property value ordering is important here
grep -q -s mtp "$prop_dir/$prop" && [ "$prop_val" == "adb" ] && prop_val="mtp,adb"
grep -q -s adb "$prop_dir/$prop" && [ "$prop_val" == "mtp" ] && prop_val="mtp,adb"
property_write "$prop" "$prop_val"
}
usb_disable() {
prop_dir="/data/android-data/property"
prop="persist.sys.usb.config"
prop_val="$1"
remain_prop=""
# Property value ordering is important here
grep -q -s mtp "$prop_dir/$prop" && [ "$prop_val" == "adb" ] && remain_prop="mtp"
grep -q -s adb "$prop_dir/$prop" && [ "$prop_val" == "mtp" ] && remain_prop="adb"
# we should not allow empty properties for the usb config
[ "$remain_prop" == "" ] && remain_prop="adb"
property_write "$prop" "$remain_prop"
}
factory_wipe() {
# only set this flag if coming from a data wipe
if [ "$DATA_FORMAT" -eq 0 ]; then
return 1
fi
flag="/data/.factory_wipe"
# if the param != "true" we just delete the flag
case $1 in
true)
touch "$flag"
;;
false)
rm -f "$flag"
;;
*)
echo "Unkown parameter $1, disabling"
rm -f "$flag"
;;
esac
}
# Initialize GPG
rm -Rf /tmp/system-image
mkdir -p /tmp/system-image
if [ -e /etc/system-image/archive-master.tar.xz ]; then
echo "Loading keyring: archive-master.tar.xz"
install_keyring /etc/system-image/archive-master.tar.xz /etc/system-image/archive-master.tar.xz.asc
fi
# Initialize recovery SWAP
## Without a swap some systems will fail to install the ubuntu rootfs (due its size)
if [ ! -e /cache/recovery/SWAP.img ]; then
# Determine size based on device maintainer preference, set in the kernel
# cmdline. This falls back to using 32MB for the total swap file size.
if grep -q siu.swapsize= /proc/cmdline; then
swap_size=$(cat /proc/cmdline | sed "s/ /\n/g" | grep siu.swapsize= | awk -F "=" '{ print $2 }')
else
swap_size=32
fi
echo "Swap size: $swap_size MB"
dd if=/dev/zero of=/cache/recovery/SWAP.img bs=1M count=$swap_size
mkswap /cache/recovery/SWAP.img
fi
swapon /cache/recovery/SWAP.img
# Check the kernel command line to see whether Ubuntu should be installed to a partition
# or in a file that is loop mounted.
if grep -q systempart= /proc/cmdline; then
USE_SYSTEM_PARTITION=1
else
USE_SYSTEM_PARTITION=0
fi
# However, do not use the block device name in the kernel command line, as that is not consistently
# named in booting normal and recovery modes. Expect fstab to have a system mountpoint or use a fallback.
if [ "$USE_SYSTEM_PARTITION" -eq 1 ];then
SYSTEM_PARTITION=$(grep "^[^#]" /etc/recovery.fstab | grep "/system" | cut -f 1 -d\ )
#Fall back to emmc@android if there's no system in fstab
if [ "$SYSTEM_PARTITION" == "" ]; then
SYSTEM_PARTITION = "emmc@android"
fi
fi
# Process the command file
FULL_IMAGE=0
echo "Processing the command file" >> /cache/system-image-upgrader.log
while read line
do
set -- $line
case "$1" in
format)
echo "Formatting: $2"
case "$2" in
system)
FULL_IMAGE=1
# Cleanup files left from halium-install
rm -f /data/system.img
rm -f /data/android-rootfs.img
rm -f /data/rootfs.img
# Delete ubuntu.img if present. Either its a leftover, or we recreate it
rm -f /data/ubuntu.img
if [ "$USE_SYSTEM_PARTITION" -eq 1 ];then
echo "system partition: $SYSTEM_PARTITION"
umount /system || true
make_ext4fs $SYSTEM_PARTITION
else
# We need to use legacy image name here, halium-boot needs this to decide file layout
dd if=/dev/zero of=/data/ubuntu.img seek=500K bs=4096 count=0
mkfs.ext2 -F /data/ubuntu.img
fi
;;
data)
for entry in /data/* /data/.writable_image /data/.factory_wipe; do
if [ "$USE_SYSTEM_PARTITION" -eq 0 ];then
if [[ ( "$entry" == "/data/ubuntu.img" ) || ( "$entry" == "/data/rootfs.img" ) || ( "$entry" == "/data/system.img" ) || ( "$entry" == "/data/android-rootfs.img" ) ]]; then
continue
fi
fi
# Some devices use /data as /cache, so avoid removing
# files that are essential to flashing and upgrading
if [ "$entry" == "/data/cache" ]; then
continue
fi
rm -Rf $entry
done
# mtp is always enabled by default
usb_enable mtp
DATA_FORMAT=1
;;
*)
echo "Unknown format target: $2"
;;
esac
;;
enable)
echo "Enabling: $2"
case "$2" in
developer_mode)
usb_enable adb
;;
mtp)
usb_enable mtp
;;
default_password)
set_password $3
;;
adb_onlock)
adb_onlock true
;;
factory_wipe)
factory_wipe true
;;
*)
echo "Unknown enable target: $2"
;;
esac
;;
disable)
echo "Disabling: $2"
case "$2" in
developer_mode)
usb_disable adb
;;
mtp)
usb_disable mtp
;;
default_password)
unset_password
;;
adb_onlock)
adb_onlock false
;;
factory_wipe)
factory_wipe false
;;
*)
echo "Unknown disable target: $2"
;;
esac
;;
load_keyring)
if [ ! -e "/cache/recovery/$2" ] || [ ! -e "/cache/recovery/$3" ]; then
echo "Skipping missing file: $2"
continue
fi
REMOVE_LIST="$REMOVE_LIST /cache/recovery/$2 /cache/recovery/$3"
echo "Loading keyring: $2"
install_keyring /cache/recovery/$2 /cache/recovery/$3
if [ -e /tmp/system-image/image-master/pubring.gpg ] && \
[ ! -e /tmp/system-image/blacklist/pubring.gpg ] && \
[ -e /data/system-data/var/lib/system-image/blacklist.tar.xz ] && \
[ -e /data/system-data/var/lib/system-image/blacklist.tar.xz.asc ]; then
echo "Loading blacklist keyring"
install_keyring /data/system-data/var/lib/system-image/blacklist.tar.xz /data/system-data/var/lib/system-image/blacklist.tar.xz.asc
fi
;;
mount)
case "$2" in
system)
mkdir -p "$SYSTEM_MOUNTPOINT"
if [ "$USE_SYSTEM_PARTITION" -eq 1 ];then
umount $SYSTEM_PARTITION || true
umount $SYSTEM_MOUNTPOINT || true
umount /system || true
check_filesystem $SYSTEM_PARTITION
mount $SYSTEM_PARTITION "$SYSTEM_MOUNTPOINT"
else
check_filesystem /data/ubuntu.img
mount -o loop /data/ubuntu.img "$SYSTEM_MOUNTPOINT/"
fi
;;
*)
echo "Unknown mount target: $2"
;;
esac
;;
unmount)
case "$2" in
system)
umount "$SYSTEM_MOUNTPOINT"
rmdir "$SYSTEM_MOUNTPOINT"
;;
*)
echo "Unknown mount target: $2"
;;
esac
;;
update)
if [ ! -e "/cache/recovery/$2" ] || [ ! -e "/cache/recovery/$3" ]; then
echo "Skipping missing file: $2"
continue
fi
REMOVE_LIST="$REMOVE_LIST /cache/recovery/$3"
if ! verify_signature device-signing /cache/recovery/$3 && \
! verify_signature image-signing /cache/recovery/$3; then
echo "Invalid signature"
exit 1
fi
echo "Applying update: $2"
cd /cache
rm -Rf partitions
rm -Rf data || true
# Start by removing any file listed in "removed"
if [ "$FULL_IMAGE" != "1" ]; then
xzcat recovery/$2 | tar --numeric-owner -xf - removed >/dev/null 2>&1 || true
if [ -e removed ]; then
while read file; do
rm -Rf $file
done < removed
fi
rm -f removed
fi
# Unpack everything else on top of the system partition
xzcat recovery/$2 | tar --numeric-owner -xf -
rm -f removed
# Check for legacy channel flag and hardlink system.img again
if [ -e legacy_channel ]; then
ln /data/ubuntu.img /data/system.img || true
rm -f legacy_channel
fi
# Move things to data
mv data/* /data/ || true
rm -Rf data || true
# Process partition images
grep "^/dev" /etc/recovery.fstab | while read line; do
set -- $line
part=${2##/}
path=$1
if [ -e partitions/${part}.img ] && [ -e $path ]; then
echo "Flashing ${part} at ${path}"
cat partitions/${part}.img > ${path}
rm partitions/${part}.img
fi
done
# Remove tarball to free up space, since device tarballs
# extract partitions/blobs that might fill up cache,
# this way we ensure we got space for the partitions/blobs
rm -f recovery/$2
;;
*)
echo "Unknown command: $1"
;;
esac
done < $COMMAND_FILE
# Remove temporary SWAP
swapoff /cache/recovery/SWAP.img
rm -f /cache/recovery/SWAP.img
# Remove the update files
for file in $REMOVE_LIST; do
rm -f $file
done
# If a previous SWAP of 512MB is available, remove
if [ -e /data/SWAP.img ]; then
swap_size=`stat /data/SWAP.img | grep Size | awk -F' ' '{print $2}'`
if [ "$swap_size" = "536870912" ]; then
echo "Removing old 512MB swap file."
rm -f /data/SWAP.img
fi
fi
# Create the SWAP image if missing
# Here we only want a small SWAP to be created, as we found out
# that the kernel memory manager algorithm behaves better if swap
# is available, even if a minimal one (not to be used by the system)
if [ ! -e /data/SWAP.img ]; then
echo "Creating SWAP device (32MB)."
dd if=/dev/zero of=/data/SWAP.img bs=4096 count=8192
mkswap /data/SWAP.img
fi
# Ensure we have sane permissions
if [ "$USE_SYSTEM_PARTITION" -eq 0 ];then
chmod 600 /data/ubuntu.img
chown 0:0 /data/ubuntu.img
fi
chmod 600 /data/SWAP.img
chown 0:0 /data/SWAP.img
touch /data/.last_update || true
sync
echo "Done upgrading: $(date)" >> /cache/system-image-upgrader.log
# Make sure there are always 5% reserved in /data for root usage
# else filling $HOME can make the system unusable since writable
# files and dirs share the space with $HOME and android does not
# reserve root space in /data
tune2fs -m 5 $(grep "/data " /proc/mounts| sed -e 's/ .*$//') >> /cache/system-image-upgrader.log