forked from ratcoded/fastboot-patcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
patcher
executable file
·127 lines (118 loc) · 5.57 KB
/
patcher
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
#!/bin/bash
#####=============== SMALLRAT TOOLBOX ===============#####
function message() {
case $1 in
"error") echo "${bold}${red}[patcher.sh](error) => {$2}${normal}";;
"warning") echo "${bold}${yellow}[patcher.sh](warning) => {$2}${normal}";;
"success") echo "${bold}${green}[patcher.sh](success) -> [$2]${normal}";;
"info") echo "[patcher.sh](info) -> [$2]";;
*) return 1;;
esac
}
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)
normal=$(tput sgr0)
bold=$(tput bold)
black=$(tput setaf 0)
red=$(tput setaf 1)
green=$(tput setaf 2)
yellow=$(tput setaf 3)
blue=$(tput setaf 4)
purple=$(tput setaf 5)
cyan=$(tput setaf 6)
white=$(tput setaf 7)
pass=":"
#####================================================#####
# unzip or rename if necessary
existance $csd/recovery.img.lz4 $pass $pass
if [ $found == true ]; then
message info "found lz4-zipped image! unzipping..." &&
lz4 -B6 --content-size -f $csd/recovery.img.lz4 && mv ./recovery.img ./raw.img &&
message success "unzipped to raw.img!"
else existance $csd/recovery.img $pass $pass
if [ $found == true ]; then
mv $csd/recovery.img $csd/raw.img &&
message success "found unzipped image!"
else message error "no image to patch found. please place recovery.img.lz4 or recovery.img in folder" && exit
fi
fi
# edit raw image
message info "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 &&
message success "made edit to image!"
# make key/signature
existance $csd/keys ":" "mkdir $csd/keys"
message info "making keyfile..."
existance $csd/keys/phh.pem ":" "openssl genrsa -f4 -out $csd/keys/phh.pem 4096 && message success 'made phh.pem'"
# fragment the edited image
existance $csd/fragments ":" "mkdir $csd/fragments"
cd fragments
message warning "fragmenting image for patching!"
$csd/magiskboot unpack $csd/header.img &&
$csd/magiskboot cpio ramdisk.cpio extract &&
existance $csd/fragments/system/bin/recovery "message success successfully fragmented image!" "message error fragmentation failed! && exit 1"
# patch the fragmented image
message warning "patching fragments (16)"
$csd/magiskboot hexpatch system/bin/recovery e10313aaf40300aa6ecc009420010034 e10313aaf40300aa6ecc0094 &&
message success 'finished (1)' # 20 01 00 35
$csd/magiskboot hexpatch system/bin/recovery eec3009420010034 eec3009420010035 &&
message success 'finished (2)'
$csd/magiskboot hexpatch system/bin/recovery 3ad3009420010034 3ad3009420010035 &&
message success 'finished (3)'
$csd/magiskboot hexpatch system/bin/recovery 50c0009420010034 50c0009420010035 &&
message success 'finished (4)'
$csd/magiskboot hexpatch system/bin/recovery 080109aae80000b4 080109aae80000b5 &&
message success 'finished (5)'
$csd/magiskboot hexpatch system/bin/recovery 20f0a6ef38b1681c 20f0a6ef38b9681c &&
message success 'finished (6)'
$csd/magiskboot hexpatch system/bin/recovery 23f03aed38b1681c 23f03aed38b9681c &&
message success 'finished (7)'
$csd/magiskboot hexpatch system/bin/recovery 20f09eef38b1681c 20f09eef38b9681c &&
message success 'finished (8)'
$csd/magiskboot hexpatch system/bin/recovery 26f0ceec30b1681c 26f0ceec30b9681c &&
message success 'finished (9)'
$csd/magiskboot hexpatch system/bin/recovery 24f0fcee30b1681c 24f0fcee30b9681c &&
message success 'finished (10)'
$csd/magiskboot hexpatch system/bin/recovery 27f02eeb30b1681c 27f02eeb30b9681c &&
message success 'finished (11)'
$csd/magiskboot hexpatch system/bin/recovery b4f082ee28b1701c b4f082ee28b970c1 &&
message success 'finished (12)'
$csd/magiskboot hexpatch system/bin/recovery 9ef0f4ec28b1701c 9ef0f4ec28b9701c &&
message success 'finished (13)'
$csd/magiskboot hexpatch system/bin/recovery 9ef00ced28b1701c 9ef00ced28b9701c &&
message success 'finished (14)'
$csd/magiskboot hexpatch system/bin/recovery 2001597ae0000054 2001597ae1000054 && # ccmp w9, w25, #0, eq ; b.e #0x20 ===> b.ne #0x20
message success 'finished (15)'
$csd/magiskboot cpio ramdisk.cpio 'add 0755 system/bin/recovery system/bin/recovery' &&
message success 'finished (16)'
message success "successfully finished patch to fragmented image!"
# reassemble fragmented image
message warning "attempting to defragment image! if your image is corrupted, this may not end well..."
$csd/magiskboot repack $csd/header.img $csd/output.img
existance $csd/output.img "message success 'assembled output.img!' && cd $csd" "message error 'failed to assemble image!' && exit 1"
# sign patched image with keyfile
message info "extracting public key to phh.pub.bin..."
python3 $csd/avbtool extract_public_key --key $csd/keys/phh.pem --output $csd/keys/phh.pub.bin &&
message info "signing patched image with key..."
python3 $csd/avbtool add_hash_footer --image $csd/output.img --partition_name recovery --partition_size $(wc -c $csd/raw.img | cut -f 1 -d ' ') --key $csd/keys/phh.pem --algorithm SHA256_RSA4096 &&
message success "signed image successfully!"
# package image for use in odin
message info "packaging image for odin..."
tar -cvf $csd/output.tar $csd/output.img &&
# hold off on this; what does it even do?
# md5sum -t $csd/output.tar >> $csd/output.tar &&
# mv $csd/output.tar $csd/output.tar.md5 &&
message success "successfully packaged output.img -> output.tar"
# dispose of any unnecessary files
message warning "cleaning up the directory..."
sudo rm -rf $csd/fragments &&
sudo rm -rf $csd/header.img &&
mv $csd/raw.img $csd/recovery.img &&
sudo rm -rf $csd/keys/phh.pem &&
message success "enjoy! hope what you're trying to do works."