forked from ratcoded/fastboot-patcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
patcher-minimal
105 lines (96 loc) · 4.74 KB
/
patcher-minimal
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
#!/bin/bash
#####=============== SMALLRAT TOOLBOX ===============#####
function existance() {
if [ -e $1 ];
then found=true && eval $2
else found=false && eval $3
fi
}
csd=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd)
pass=":"
#####================================================#####
# exit or rename if necessary
existance $csd/recovery.img.lz4 $pass $pass
if [ $found == true ]; then
echo "found lz4-zipped image! please unzip it yourself, and re-run this script! " && exit
else existance $csd/recovery.img $pass $pass
if [ $found == true ]; then
mv $csd/recovery.img $csd/raw.img &&
echo "found unzipped image!"
else echo "no image to patch found. please place recovery.img in folder!" && exit 1
fi
fi
# edit raw image
echo "editing image..."
off=$(grep -ab -o SEANDROIDENFORCE raw.img | tail -n 1 | cut -d : -f 1)
dd if=raw.img of=header.img bs=4k count=$off iflag=count_bytes
echo "made edit to image!"
echo "running file check..."
existance $csd/header.img "echo finished!" "echo file check on header.img failed && exit 1"
# make key/signature
mkdir $csd/keys
echo "making keyfile..."
existance $csd/keys/phh.pem ":" "openssl genrsa -f4 -out $csd/keys/phh.pem 4096 && echo 'made phh.pem'"
# fragment the edited image
mkdir $csd/fragments
cd fragments
echo "fragmenting image for patching!"
$csd/magiskboot unpack $csd/header.img
$csd/magiskboot cpio ramdisk.cpio extract
echo "showing directory..."
ls $csd/fragments
existance $csd/fragments/system/bin/recovery "echo successfully fragmented image!" "echo fragmentation failed! && exit 1"
# patch the fragmented image
echo "patching fragments (16)"
$csd/magiskboot hexpatch $csd/fragments/system/bin/recovery e10313aaf40300aa6ecc009420010034 e10313aaf40300aa6ecc0094 &&
echo 'finished (1)' # 20 01 00 35
$csd/magiskboot hexpatch $csd/fragments/system/bin/recovery eec3009420010034 eec3009420010035 &&
echo 'finished (2)'
$csd/magiskboot hexpatch $csd/fragments/system/bin/recovery 3ad3009420010034 3ad3009420010035 &&
echo 'finished (3)'
$csd/magiskboot hexpatch $csd/fragments/system/bin/recovery 50c0009420010034 50c0009420010035 &&
echo 'finished (4)'
$csd/magiskboot hexpatch $csd/fragments/system/bin/recovery 080109aae80000b4 080109aae80000b5 &&
echo 'finished (5)'
$csd/magiskboot hexpatch $csd/fragments/system/bin/recovery 20f0a6ef38b1681c 20f0a6ef38b9681c &&
echo 'finished (6)'
$csd/magiskboot hexpatch $csd/fragments/system/bin/recovery 23f03aed38b1681c 23f03aed38b9681c &&
echo 'finished (7)'
$csd/magiskboot hexpatch $csd/fragments/system/bin/recovery 20f09eef38b1681c 20f09eef38b9681c &&
echo 'finished (8)'
$csd/magiskboot hexpatch $csd/fragments/system/bin/recovery 26f0ceec30b1681c 26f0ceec30b9681c &&
echo 'finished (9)'
$csd/magiskboot hexpatch $csd/fragments/system/bin/recovery 24f0fcee30b1681c 24f0fcee30b9681c &&
echo 'finished (10)'
$csd/magiskboot hexpatch $csd/fragments/system/bin/recovery 27f02eeb30b1681c 27f02eeb30b9681c &&
echo 'finished (11)'
$csd/magiskboot hexpatch $csd/fragments/system/bin/recovery b4f082ee28b1701c b4f082ee28b970c1 &&
echo 'finished (12)'
$csd/magiskboot hexpatch $csd/fragments/system/bin/recovery 9ef0f4ec28b1701c 9ef0f4ec28b9701c &&
echo 'finished (13)'
$csd/magiskboot hexpatch $csd/fragments/system/bin/recovery 9ef00ced28b1701c 9ef00ced28b9701c &&
echo 'finished (14)'
$csd/magiskboot hexpatch $csd/fragments/system/bin/recovery 2001597ae0000054 2001597ae1000054 && # ccmp w9, w25, #0, eq ; b.e #0x20 ===> b.ne #0x20
echo 'finished (15)'
$csd/magiskboot cpio ramdisk.cpio "add 0755 $csd/fragments/system/bin/recovery $csd/fragments/system/bin/recovery" &&
echo 'finished (16)'
echo "successfully finished patch to fragmented image!"
# reassemble fragmented image
mkdir $csd/output
echo "attempting to defragment image! if your image is corrupted, this may not end well..."
$csd/magiskboot repack $csd/header.img $csd/output/output.img
existance $csd/output/output.img "echo assembled output.img! && cd $csd" "echo output.img not found, exiting && exit 1"
# sign patched image with keyfile
echo "extracting public key to phh.pub.bin..."
python3 $csd/avbtool extract_public_key --key $csd/keys/phh.pem --output $csd/keys/phh.pub.bin &&
echo "signing patched image with key..."
python3 $csd/avbtool add_hash_footer --image $csd/output/output.img --partition_name recovery --partition_size $(wc -c $csd/raw.img | cut -f 1 -d ' ') --key $csd/keys/phh.pem --algorithm SHA256_RSA4096
echo "signed image successfully!"
# package image for use in odin
echo "packaging image for odin..."
tar -cvf $csd/output/output.tar $csd/output/output.img &&
md5sum -t $csd/output/output.tar >> $csd/output/output.tar &&
mv $csd/output/output.tar $csd/output/output.tar.md5 &&
echo "successfully packaged output.img -> output.tar.md5"
# hehe
echo "enjoy! hope what you're trying to do works."