-
Notifications
You must be signed in to change notification settings - Fork 26
/
repit.sh
729 lines (560 loc) · 18 KB
/
repit.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
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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
#!/sbin/sh
#####################################################
# Lanchon REPIT #
# Copyright 2016, Lanchon #
#####################################################
#####################################################
# Lanchon REPIT is free software licensed under #
# the GNU General Public License (GPL) version 3 #
# and any later version. #
#####################################################
set -e
version="2017-01-15"
### logging
fatal() {
echo
>&2 echo "FATAL:" "$@"
exit 1
}
warning() {
>&2 echo "WARNING:" "$@"
}
info() {
echo "info:" "$@"
}
### helpers
printSizeMiB() {
local size="$1"
echo "$(( (size) / $MiB )) MiB"
}
checkTool() {
#info "checking tool: $1"
if [ -z "$(which "$1")" ]; then
fatal "required tool '$1' missing (please use a recent version of TWRP to run this package)"
fi
}
chooseTool() {
#info "choosing tool: $*"
local tool
for tool in "$@"; do
if [ -n "$(which "$tool")" ]; then
echo "$tool"
return
fi
done
fatal "all tool alternatives missing: $* (please use a recent version of TWRP to run this package)"
}
runParted() {
parted -s $ddev unit s "$@"
}
rereadParTable() {
local hint="$1"
info "rereading partition table"
sync
blockdev --flushbufs $ddev
if ! blockdev --rereadpt $ddev; then
fatal "unable to reread the partition table${hint}"
fi
sleep 1
}
detectBlockDeviceHeapRange() {
info "determining usable sector range of block device"
checkTool sgdisk
local out="$(sgdisk "$ddev" --set-alignment 1 --print)"
deviceHeapStart=$(echo "$out" | sed -n "s/^First usable sector is[ ]*\([0-9]*\)[, ]*last usable sector is[ ]*\([0-9]*\)[ ]*$/\1/p")
deviceHeapEnd=$(echo "$out" | sed -n "s/^First usable sector is[ ]*\([0-9]*\)[, ]*last usable sector is[ ]*\([0-9]*\)[ ]*$/\2/p")
if [ -z "$deviceHeapStart" ] || [ -z "$deviceHeapEnd" ]; then
warning "unable to parse sgdisk output (dump of output follows)"
>&2 echo
>&2 echo "$out"
>&2 echo
fatal "unable to determine usable sector range of block device"
fi
deviceHeapEnd=$(( deviceHeapEnd + 1 ))
}
alignDown() {
echo $(( ($1) / heapAlignment * heapAlignment ))
}
alignUp() {
echo $(( (($1) + (heapAlignment - 1)) / heapAlignment * heapAlignment ))
}
### data store
parOldStart() {
cat ${spar}$1/start
}
parNewStart() {
cat ${tpar}$1/start
}
parOldSize() {
cat ${spar}$1/size
}
parNewSize() {
cat ${tpar}$1/size
}
parFooterSize() {
cat ${tpar}$1/footerSize
}
parNewSizeMinusFooter() {
echo $(( $(parNewSize $1) - $(parFooterSize $1) ))
}
parNewSizeAligned() {
cat ${tpar}$1/sizeAligned
}
parOldEnd() {
echo $(( $(parOldStart $1) + $(parOldSize $1) ))
}
parNewEnd() {
echo $(( $(parNewStart $1) + $(parNewSize $1) ))
}
parNewEndAligned() {
echo $(( $(parNewStart $1) + $(parNewSizeAligned $1) ))
}
parGet() {
cat ${tpar}$1/$2
}
parSet() {
echo -n "$3" >${tpar}$1/$2
}
parName() {
echo -n "partition #$1 '$(parGet $1 fname)' ($(parGet $1 pname))"
}
### initialization
detectSideload() {
local packageName="$1"
# /tmp/update.zip: old sideload protocol
# /sideload/package.zip: current sideload-host protocol
if [ "$1" == "/tmp/update.zip" ] || [ "$1" == "/sideload/package.zip" ]; then
fatal "adb sideload is not directly supported: it hides the package filename and thus filename-based configuration does not work "\
"(please adb push the package to '/tmp' and run it from there; or adb sideload it after adding a 'flashize/settings' file to the package containing the desired package filename override)"
fi
}
checkTools() {
checkTool parted
checkTool sort
checkTool blockdev
checkTool sed
checkTool awk
checkTool readlink
checkTool basename
checkTool dirname
checkTool dd
checkTools_fs_ext4
checkTools_fs_vfat
checkTools_fs_f2fs
checkTools_fs_swap
checkTools_fs_raw
}
initPartitionConf() {
local n=$1
local size=$2
local content=$3
local fs=$4
$(parSet $n parsedSize $size)
$(parSet $n parsedContent $content)
$(parSet $n parsedFs $fs)
}
initPartition() {
local n=$1
local pname=$2
local fname=$3
local conf=$4
local footerSize=$5
mkdir -p ${tpar}$n
$(parSet $n pname $pname)
$(parSet $n fname $fname)
initPartitionConf $n $conf
$(parSet $n footerSize $footerSize)
initedPartitions="$initedPartitions $n"
}
initPartitions() {
rm -rf $tdir
initedPartitions=""
device_initPartitions
}
parsePartitionConfiguration() {
local n=$1
local packageName="$2"
local parName=$(parGet $n fname)
local conf="$(echo -n "$packageName" | sed -n "s/.*-${parName}=\([^-=]*\)\(-.*\|\)\$/\1/p")"
if [ -n "$conf" ]; then
local regex="^\(\|[0-9]\+[GgMm]\|[0-9]\+[.][0-9]*[GgMm]\|[.][0-9]\+[GgMm]\|same\|min\|max\)\(+\(\|keep\|wipe\)\(+\(\|ext4\|vfat\|f2fs\|swap\|raw\)\)\?\)\?$"
if [ -n "$(echo -n "$conf" | sed "s/$regex//")" ]; then
fatal "invalid partition configuration for '$parName': $parName=$conf"
fi
local val
val="$(echo -n "$conf" | sed -n "s/$regex/\1/p")"
if [ -n "$val" ]; then $(parSet $n parsedSize "$val"); fi
val="$(echo -n "$conf" | sed -n "s/$regex/\3/p")"
if [ -n "$val" ]; then $(parSet $n parsedContent "$val"); fi
val="$(echo -n "$conf" | sed -n "s/$regex/\5/p")"
if [ -n "$val" ]; then $(parSet $n parsedFs "$val"); fi
fi
}
parsePackageName() {
local packageName="$1"
local parNames=""
local n
for n in $configurablePartitions; do
if [ -n "$parNames" ]; then
parNames="$parNames|"
fi
parNames="$parNames$(parGet $n fname)"
done
info "valid package names: <prefix>[-($parNames)=<conf>]...<suffix>"
info "valid partition <conf> values: [<size>(G|M)|same|min|max][+[keep|wipe][+[ext4|vfat|f2fs|swap|raw]]]"
echo
echo "----- DEFAULTS -----"
for n in $configurablePartitions; do
#echo "-$(parGet $n fname)=$(parGet $n parsedSize)+$(parGet $n parsedContent)+$(parGet $n parsedFs)"
echo "$(parGet $n fname) = size:$(parGet $n parsedSize) + content:$(parGet $n parsedContent) + fs:$(parGet $n parsedFs)"
done
echo
info "parsing package name"
if [ -z "$packageName" ]; then
fatal "unable to retrieve package name"
fi
packageName="$(basename "$packageName" .zip)"
for n in $configurablePartitions; do
parsePartitionConfiguration $n $packageName
done
echo
echo "----- CONFIGURATION -----"
for n in $initedPartitions; do
#echo "-$(parGet $n fname)=$(parGet $n parsedSize)+$(parGet $n parsedContent)+$(parGet $n parsedFs)"
echo "$(parGet $n fname) = size:$(parGet $n parsedSize) + content:$(parGet $n parsedContent) + fs:$(parGet $n parsedFs)"
done
echo
}
disableSwap() {
#info "checking tool: swapoff"
if [ -n "$(which swapoff)" ]; then
info "disabling swap"
swapoff -a
fi
}
checkUnmount() {
local packageName="$1"
if [ ! -e $ddev ] || [ ! -e $sdev ]; then
fatal "block device not found"
fi
local hint
if [ -f "$packageName" ]; then
local resolvedPackageName
resolvedPackageName="$(readlink -f "$packageName")"
if [ -z "$resolvedPackageName" ]; then
resolvedPackageName="$packageName"
fi
if [ "$(dirname "$resolvedPackageName")" != "/tmp" ]; then
info "copying package to '/tmp'"
cp -f "$packageName" "/tmp/"
hint="this package copied itself to '/tmp'; please run it again from there"
else
hint=\
"please disable MTP in TWRP's 'Mount' menu (or disconnect from PC), reboot TWRP and run this package again; "\
"run it immediately after boot up, do not enable USB mass storage; "\
"note that you might be told to run it yet again from '/tmp'; "\
"make sure your phone is not encrypted: encrypted phones are not supported"
fi
else
hint="please use TWRP's file manager to copy this package to '/tmp' and run it again from there"
fi
info "unmounting all partitions"
local dev
for dev in $(grep -ow "^$unmountPattern" /proc/mounts | sort -u); do
if ! umount $dev; then
fatal "unable to unmount all partitions ($hint)"
fi
done
# rereadParTable requires everything unmounted
rereadParTable " ($hint)"
}
init() {
local packageName="$1"
detectSideload "$packageName"
tdir=/tmp/lanchon-repit
tchunk=$tdir/chunk.tmp
tpar=$tdir/partition-info/p
device_init
MiB=$(( 1024 * 1024 / sectorSize )) # 1 MiB in sectors
GiB=$(( 1024 * 1024 * 1024 / sectorSize )) # 1 GiB in sectors
checkTools
initPartitions
parsePackageName "$packageName"
disableSwap
checkUnmount "$packageName"
}
### setup
setup() {
checkTool parted
checkTool sort
checkTool blockdev
checkTool awk
#info "unmounting all partitions"
#...
#rereadParTable
heapMinSize=$(( 8 * MiB ))
device_setup
info "checking existing partitions"
local n
for n in $(seq 1 $partitionCount); do
if [ ! -e ${spar}$n ]; then
fatal "partition #$n: not found"
fi
if [ $(( $(parOldStart $n) < 0 )) -ne 0 ]; then
fatal "partition #$n: invalid start"
fi
if [ $(( $(parOldSize $n) <= 0 )) -ne 0 ]; then
fatal "partition #$n: invalid size"
fi
done
for n in $(seq $(( partitionCount + 1 )) $(( partitionCount + 100 )) ); do
if [ -e ${spar}$n ]; then
fatal "partition #$n: unexpected"
fi
done
}
setupHeapExisting() {
info "checking existing partition layout"
# TODO: verify that no partitions that are not members of the heap step into the heap area.
# TODO: maybe verify that all partitions that are members of the heap are fully contained in the heap area.
local nPrev=""
local gap
local n
for n in $heapPartitions; do
info "current size: $(parName $n): $(printSizeMiB $(parOldSize $n))"
if [ -z "$nPrev" ]; then
gap=$(( $(parOldStart $n) - heapStart ))
if [ $(( gap < 0 )) -ne 0 ]; then
warning "$(parName $n) starts $(printSizeMiB $(( -(gap) )) ) before the start of heap '$heapName'"
fi
gap=$(( $(parOldStart $n) - heapStartAligned ))
if [ $(( gap > 0 )) -ne 0 ]; then
warning "$(parName $n) starts $(printSizeMiB $gap) after the aligned start of heap '$heapName'"
fi
else
gap=$(( $(parOldStart $n) - $(parOldEnd $nPrev) ))
if [ $(( gap < 0 )) -ne 0 ]; then
fatal "layout reversal between $(parName $nPrev) and $(parName $n) in heap '$heapName'"
fi
gap=$(( $(parOldStart $n) - $(alignUp $(parOldEnd $nPrev)) ))
if [ $(( gap > 0 )) -ne 0 ]; then
warning "$(parName $n) starts $(printSizeMiB $gap) after the aligned end of $(parName $nPrev)"
fi
fi
nPrev=$n
done
gap=$(( heapEnd - $(parOldEnd $nPrev) ))
if [ $(( gap < 0 )) -ne 0 ]; then
warning "$(parName $nPrev) ends $(printSizeMiB $(( -(gap) )) ) after the end of heap '$heapName'"
fi
if [ $(( gap >= heapAlignment )) -ne 0 ]; then
warning "$(parName $nPrev) ends $(printSizeMiB $gap) before the end of heap '$heapName'"
fi
}
setupHeapPartition() {
local n=$1
local size="$(parGet $n parsedSize)"
local content="$(parGet $n parsedContent)"
local fs="$(parGet $n parsedFs)"
if [ -z "$size" ]; then
fatal "$(parName $n): undefined new size"
fi
if [ -z "$content" ]; then
fatal "$(parName $n): undefined content policy"
fi
if [ -z "$fs" ]; then
fatal "$(parName $n): undefined file system type"
fi
case "$size" in
same)
size=$(parOldSize $n)
;;
min)
size=$heapMinSize
;;
max)
size=0
;;
*)
local regex="^\([0-9.]*\)[ ]*\([GgMm]\)\?$"
local sizeUnit="$(echo -n "$size" | sed -n "s/$regex/\2/p")"
local size="$(echo -n "$size" | sed -n "s/$regex/\1/p")"
case "$sizeUnit" in
M|m)
sizeUnit=$MiB
;;
G|g|*)
sizeUnit=$GiB
;;
esac
local sizeGranularity=$(( heapSizeGranularity ))
size=$(awk "BEGIN { print int(($size) * $sizeUnit / $sizeGranularity + 0.5) * $sizeGranularity }")
if [ $(( size < heapSizeGranularity )) -ne 0 ]; then
fatal "$(parName $n): invalid new size"
fi
;;
esac
case "$content" in
keep)
;;
wipe)
;;
*)
fatal "$(parName $n): invalid content policy"
;;
esac
case "$fs" in
ext4)
;;
vfat)
;;
f2fs)
;;
swap)
;;
raw)
;;
*)
fatal "$(parName $n): invalid file system type"
;;
esac
$(parSet $n size $size)
$(parSet $n content $content)
$(parSet $n fs $fs)
$(parSet $n sizeAligned $(alignUp $size))
}
setupHeapNew() {
info "checking new partition layout"
local n
for n in $heapPartitions; do
setupHeapPartition $n
done
local sizeAligned=$(( heapEndAligned - heapStartAligned ))
# correct $sizeAligned in case the size of last partition and the end of heap are both unaligned,
# and their combination would lead to an unused area at heap end the size of $heapAlignment or larger.
local lastPar=$n
local lastParEnd=$(( heapEndAligned - $(parNewSizeAligned $lastPar) + $(parNewSize $lastPar) ))
if [ $(( heapEnd - lastParEnd >= heapAlignment )) -ne 0 ]; then
sizeAligned=$(( sizeAligned + heapAlignment ))
fi
for n in $heapPartitions; do
sizeAligned=$(( sizeAligned - $(parNewSizeAligned $n) ))
done
local maxPar
for n in $heapPartitions; do
if [ $(( $(parNewSize $n) == 0 )) -ne 0 ]; then
if [ -n "$maxPar" ]; then
fatal "more than one partition in heap '$heapName' has its size set to 'max'"
fi
maxPar=$n
local size=$(( sizeAligned / heapSizeGranularity * heapSizeGranularity ))
if [ $(( size < heapSizeGranularity )) -ne 0 ]; then
fatal "the new partition layout of heap '$heapName' requires more space than available"
fi
$(parSet $n size $size)
$(parSet $n sizeAligned $sizeAligned)
fi
info "new size: $(parName $n): $(printSizeMiB $(parNewSize $n))"
done
local nextStart=$heapStartAligned
for n in $heapPartitions; do
$(parSet $n start $nextStart)
nextStart=$(parNewEndAligned $n)
done
gap=$(( heapEnd - $(parNewEnd $lastPar) ))
if [ $(( gap < 0 )) -ne 0 ]; then
fatal "the new partition layout of heap '$heapName' requires more space than available"
fi
if [ $(( gap >= heapAlignment )) -ne 0 ]; then
warning "there will be $(printSizeMiB $gap) of unused space at the end of heap '$heapName'"
fi
}
setupHeap() {
setupHeapExisting
setupHeapNew
}
forEachHeap() {
for heapName in $allHeaps; do
echo "##### processing heap '$heapName'"
device_setupHeap_$heapName
heapStartAligned=$(alignUp $heapStart)
heapEndAligned=$(alignDown $heapEnd)
if [ -z "$heapSizeGranularity" ]; then
heapSizeGranularity=$heapAlignment
"$@"
heapSizeGranularity=""
else
"$@"
fi
done
}
### execution
processPar() {
local n=$1
echo "***** processing $(parName $n)"
eval processPar_$(parGet $n fs)_$(parGet $n content)_$processMode $n ${dpar}$n $(parOldStart $n) $(parOldSize $n) $(parNewStart $n) $(parNewSize $n)
}
processParList() {
local first=$1
local rest=${@:2}
echo "----- analyzing $(parName $first)"
if [ -z "$rest" ]; then
processPar $first
else
local second=$2
if [ $(( $(parNewEnd $first) > $(parOldStart $second) )) -ne 0 ]; then
info "$(parName $first) will expand into storage area currently used by $(parName $second)"
info "deferring processing of $(parName $first) until required space is freed"
processParList $rest
processPar $first
else
processPar $first
processParList $rest
fi
fi
}
processHeap() {
processMode=$1
processParList $heapPartitions
}
flushBuffers() {
info "flushing buffers"
sync
blockdev --flushbufs $ddev
sleep 3
}
### main
main() {
local packageName="$1"
echo " ####################################"
echo " Lanchon REPIT"
echo " A Data-Sparing Repartitioning Tool"
echo " Version: $version"
echo " Device: $deviceName"
echo " Copyright 2016, Lanchon (GPLv3)"
echo " ####################################"
echo
echo "===== PRELIMINARY CHECKS ====="
if [ -f "/tmp/repit-settings" ]; then
packageName="$(cat "/tmp/repit-settings")"
info "overriding configuration via '/tmp/repit-settings' to '$packageName'"
elif [ -n "$FLASHIZE_ENV_VERSION" ] && [ -f "/tmp/flashize/repit-settings" ]; then
packageName="$(cat "/tmp/flashize/repit-settings")"
info "overriding configuration via 'flashize/repit-settings' to '$packageName'"
fi
init "$packageName"
echo
echo "===== PREPARATION ====="
setup
forEachHeap setupHeap
echo
echo "===== DRY-RUN ====="
forEachHeap processHeap dry
echo
echo "===== EXECUTION ====="
forEachHeap processHeap wet
flushBuffers
echo
echo "===== SUCCESS ====="
#echo
}