forked from lekron42/samloader_termux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
samcheck
217 lines (188 loc) · 5.09 KB
/
samcheck
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
#!/data/data/com.termux/files/usr/bin/bash
# -------------------------------------
# Helper script for samloader on Termux
# Github: itssct
# Version: v1.04
# Credits: githubuser lekron42
# -------------------------------------
show_usage() {
echo "This script automates firmware downloading using samloader"
echo -e "Usage: $(basename $(readlink -f $0)) [argument]\n"
echo "Arguments:"
echo " -f|--force : Force update even if no new version is found"
echo " -r|--repair : Update samloader to fix download failure"
echo " -p|--path : Path to save the decrypted Firmware"
echo " -h|--help : Show this help"
exit 0
}
update_samloader() {
{ proceed=$(pip3 uninstall samloader | tee /dev/fd/5); } 5>&1
if [ $(echo "$proceed" | grep -c "Successfully") -gt 0 ]; then
pip3 install git+https://github.com/martinetd/samloader.git
echo "Successfully reinstalled samloader."
else
echo "Uninstalling cancelled.."
fi
exit 0
}
if [ $(echo "$0" | grep -c "termux") -eq 0 ]; then
echo "Script not executed in Termux! Exiting.." 1>&2
exit 1
fi
if [ $# -eq 1 ]; then
case "$1" in
-h|--help)
show_usage
;;
-f|--force)
force_download=true
;;
-r|--repair)
update_samloader
;;
-p|--path)
shift
r_dest=$1
*)
echo "Incorrect input!" 1>&2
show_usage
;;
esac
elif [ $# -gt 1 ]; then
echo "Only 1 argument supported. $# given." 1>&2
exit 1
fi
i_python=$(pkg list-installed "python" 2> /dev/null | grep -c "installed")
i_git=$(pkg list-installed "git" 2> /dev/null | grep -c "installed")
if [ "$i_python" -eq 0 ] && [ "$i_git" -eq 0 ]; then
echo "Python and Git are not installed but required.."
read -p "Install now? (y/N) " r_pg
case "$r_pg" in
y|Y) ;;
*) exit 0;;
esac
apt-get update
pkg install python git
if [ $? -ne 0 ]; then
echo "Failed to install python and/or git!" 1>&2
exit 1
fi
elif [ "$i_python" -eq 0 ]; then
echo "Python is required but not installed.."
read -p "Install now? (y/N) " r_p
case "$r_p" in
y|Y) ;;
*) exit 0;;
esac
apt-get update
pkg install python
if [ $? -ne 0 ]; then
echo "Failed to install python!" 1>&2
exit 1
fi
elif [ "$i_git" -eq 0 ]; then
echo "Git is required but not installed.."
read -p "Install now? (y/N) " r_g
case "$r_g" in
y|Y) ;;
*) exit 0;;
esac
apt-get update
pkg install git
if [ $? -ne 0 ]; then
echo "Failed to install git!" 1>&2
exit 1
fi
fi
if [ $(pip3 list 2> /dev/null | grep -c "samloader") -eq 0 ]; then
echo "Samloader is required but not installed.."
read -p "Install now? (y/N) " r_install
case "$r_install" in
y|Y) ;;
*) exit 0;;
esac
pip3 install git+https://github.com/martinetd/samloader.git
fi
model="$(getprop 'ro.product.model')"
region="$(getprop 'ro.csc.sales_code')"
current_pda="$(getprop 'ro.system.build.version.incremental')"
current_csc="$(getprop 'ro.omc.build.version')"
echo -e "- Device:\t$model / $region"
latest_version="$(samloader -m $model -r $region checkupdate)"
latest_pda="$(echo $latest_version | cut -d/ -f1)"
latest_csc="$(echo $latest_version | cut -d/ -f2)"
echo -e "- Installed:\t$current_pda/$current_csc"
echo -e "- Latest:\t$latest_pda/$latest_csc"
echo "---------------"
if [ "$current_pda" = "$latest_pda" ] && [ "$current_csc" = "$latest_csc" ]; then
if [ ! "$force_download" = true ]; then
echo "No new version found! Use --force to download anyway.."
exit 0
fi
fi
read -p "Download? (y/N) " r_download
case "$r_download" in
y|Y) ;;
*) exit 0;;
esac
while true; do
read -p "Please enter an absolute path to save the file to after downloading (e.g. \"/sdcard/\"): " r_path
parse_f=$(echo "$r_path" | head -c 1)
parse_l=$(echo "$r_path" | tail -c 2)
if [ "$parse_f" = "\"" ] || [ "$parse_l" = "\"" ]; then
echo "Please enter path again without quotes!" 1>&2
else
if [ "$parse_l" != "/" ]; then
r_path="$r_path/"
fi
if [ -e "$r_path" ]; then
break
else
echo "Path not valid! Please try again.." 1>&2
fi
fi
done
touch "$r_path.samcheck" 2> /dev/null
if [ $? -ne 0 ]; then
echo "Permission denied! Running \"termux-setup-storage\".." 1>&2
echo "Waiting for storage permission.."
sleep 1
termux-setup-storage
count=0
while true; do
touch "$r_path.samcheck" 2> /dev/null
if [ $? -eq 0 ]; then
break
fi
let count++
if [ "$count" -gt 30 ]; then
echo "Operation timed out!" 1>&2
exit 1
fi
sleep 1
done
fi
if [ -e "$r_path.samcheck" ]; then
rm -f "$r_path.samcheck"
fi
echo "Downloading..."
filename="$(samloader -m $model -r $region download -v $latest_version -O $r_path/. | cut -d' ' -f2)"
size=$(wc -c "$r_path$filename" | cut -d' ' -f1)
if [ "$size" -lt 4500000000 ]; then
echo -e "\n\nDownload failed.." 1>&2
echo "Please use $(basename $(readlink -f $0)) --repair.\n" 1>&2
exit 1
fi
echo "Downloading finished!"
encr_protocol="$(echo $filename | tail -c 2)"
input="$r_path/$filename"
output="$r_path/$(echo $filename | cut -d. -f1-2)"
echo "Decrypting..."
samloader -m "$model" -r "$region" decrypt -v "$latest_version" -V "$encr_protocol" -i "$input" -o "$output"
echo "Decrypting finished!"
if [ -e "$input" ]; then
rm "$input"
fi
echo "Done! Output:"
echo "$r_path$(echo $filename | cut -d. -f1-2)"
exit 0