-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
unlock.sh
193 lines (147 loc) · 5.77 KB
/
unlock.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
#!/usr/bin/env bash
if ! type java &> /dev/null; then
echo "Java not detected, install java and try again"
fi
if type mi-fastboot &> /dev/null; then
runFastboot=mi-fastboot
else
runFastboot=fastboot
fi
# On system with zenity use graphical mode
if type zenity &> /dev/null; then
# Show interface progress
zenity --progress --pulsate --no-cancel --text "Waiting for any device in fastboot mode" &
ZENITY_PID=$!
# Detect Product
product=$(timeout --foreground 60 $runFastboot getvar product 2>&1 | grep 'product:' | cut -d ' ' -f2)
if [[ -n $product ]]; then
# Detect Token if have product
token=$($runFastboot getvar token 2>&1 | grep token:| sed 's|.*token: ||g' | sed ':a;N;$!ba;s/\n//g')
if [[ -z $token ]]; then
#Another way to detect Token
token=$($runFastboot oem get_token 2>&1 | grep token:| sed 's|.*token: ||g' | sed ':a;N;$!ba;s/\n//g')
fi
else
zenity --error --text="Timeout, device not detected."
fi
# Close interface progress
kill "$ZENITY_PID"
if [[ -n $token ]]; then
region=$(zenity --height=360 --list --text="
Product: $product
Token: $token
" \
--title="Detected device" \
--radiolist \
--column="Select" --column="Region" \
TRUE "global" \
FALSE "india" \
FALSE "china" \
FALSE "russia" \
FALSE "europe")
# Check if a choice was made
if [ -n "$region" ]; then
data=$(zenity --entry \
--title="Enter DATA" \
--text="Enter the giant code obtained from the apk used on your Xiaomi device:")
if [[ -z $data ]]; then
zenity --error --text="Canceled, see you next time my friend."
exit
fi
else
zenity --error --text="Canceled, see you next time my friend."
exit
fi
zenity --progress --pulsate --no-cancel --text "Running java part, wait" &
ZENITY_PID=$!
javaOutput=$(bash get_token.sh --region=$region --product="$product" --token="$token" "$data" 2>&1 | sed 's/"/\\"/g')
unlockToken=$(echo "$javaOutput" | grep 'Unlock device token:' | sed 's|.*Unlock device token: ||g')
kill "$ZENITY_PID"
if [[ -z $unlockToken ]]; then
zenity --info \
--text="$javaOutput" \
--title="Done!"
exit
else
zenity --progress --pulsate --no-cancel --text "Unlocking, wait" &
ZENITY_PID=$!
# Convert using perl to replace xxd
mkdir -p ~/.cache
perl -e "print pack 'H*', '$unlockToken'" > ~/.cache/token.bin
# Unlock
stageUnlock=$($runFastboot stage ~/.cache/token.bin 2>&1 | sed 's/"/\\"/g')
oemUnlock=$($runFastboot oem unlock 2>&1 | sed 's/"/\\"/g')
# oemUnlockAgain=$($runFastboot oem-unlock "$unlockToken" 2>&1 | sed 's/"/\\"/g')
kill "$ZENITY_PID"
zenity --info \
--text="$stageUnlock
$oemUnlock" \
--title="Done!"
exit
fi
fi
# On system with dialog, like Termux
elif type dialog &> /dev/null; then
(dialog --infobox "Waiting for any device in fastboot mode" 10 50) &
DIALOG_PID=$!
# fix mi-fastboot device detection on termux
$runFastboot devices &>/dev/null
product=$(timeout --foreground 60 $runFastboot getvar product 2>&1 | grep 'product:' | cut -d ' ' -f2)
if [[ -n $product ]]; then
token=$($runFastboot getvar token 2>&1 | grep token: | sed 's|.*token: ||g' | sed ':a;N;$!ba;s/\n//g')
if [[ -z $token ]]; then
token=$($runFastboot oem get_token 2>&1 | grep token: | sed 's|.*token: ||g' | sed ':a;N;$!ba;s/\n//g')
fi
else
dialog --msgbox "Timeout, device not detected." 10 50
kill "$DIALOG_PID"
exit 1
fi
kill "$DIALOG_PID"
if [[ -n $token ]]; then
exec 3>&1
region=$(dialog --radiolist "Detected device\n\nProduct: $product\n\nToken: $token" 15 50 5 \
"global" "Global" on \
"india" "India" off \
"china" "China" off \
"russia" "Russia" off \
"europe" "Europe" off \
2>&1 1>&3)
exec 3>&-
if [ -z "$region" ]; then
dialog --msgbox "Canceled, see you next time my friend." 10 50
exit
fi
exec 3>&1
data=$(dialog --inputbox "Enter the giant code obtained from the apk used on your Xiaomi device:" 10 50 2>&1 1>&3)
exec 3>&-
if [[ -z $data ]]; then
dialog --msgbox "Canceled, see you next time my friend." 10 50
exit
fi
(dialog --infobox "Running java part, wait" 10 50) &
DIALOG_PID=$!
javaOutput=$(bash get_token.sh --region=$region --product="$product" --token="$token" "$data" 2>&1 | sed 's/"/\\"/g')
unlockToken=$(echo "$javaOutput" | grep "Unlock device token:" | sed 's|.*Unlock device token: ||g')
kill "$DIALOG_PID"
if [[ -z $unlockToken ]]; then
dialog --msgbox "$javaOutput" 20 60
exit
else
(dialog --infobox "Unlocking, wait" 10 50) &
DIALOG_PID=$!
mkdir -p ~/.cache
perl -e "print pack 'H*', '$unlockToken'" > ~/.cache/token.bin
stageUnlock=$($runFastboot stage ~/.cache/token.bin 2>&1 | sed 's/"/\\"/g')
oemUnlock=$($runFastboot oem unlock 2>&1 | sed 's/"/\\"/g')
# Unlock Again commented
# oemUnlockAgain=$($runFastboot oem-unlock "$unlockToken" 2>&1 | sed 's/"/\\"/g')
kill "$DIALOG_PID"
dialog --msgbox "$stageUnlock\n$oemUnlock" 20 60
exit
fi
fi
# if don't have zenity or dialog
else
exec bash get_token.sh "$@"
fi