-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathinstall.sh
executable file
·305 lines (231 loc) · 7.3 KB
/
install.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
#!/bin/bash
#
# Copyright 2015-2016 Preetam J. D'Souza
# Copyright 2016 The Maru OS Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
set -e
set -u
readonly SUCCESS=0
readonly SUCCESS_BASE=$(( 1 << 5 ))
readonly ERROR_BASE=$(( 1 << 6 ))
readonly SUCCESS_USER_ABORT=$(( SUCCESS_BASE + 1 ))
readonly SUCCESS_BOOTLOADER_UNLOCKED=$(( SUCCESS_BASE + 2 ))
readonly ERROR_INCOMPLETE_ZIP=$(( ERROR_BASE + 1 ))
readonly ERROR_ADB=$(( ERROR_BASE + 2 ))
readonly ERROR_FASTBOOT_PERMS=$(( ERROR_BASE + 3 ))
readonly ERROR_INCORRECT_INSTALLER=$(( ERROR_BASE + 4 ))
cat <<EOF
Welcome to the Maru installer!
In order to install Maru you will need to:
1. Connect your device to your computer over USB
2. Enable USB Debugging on your device:
1) Go to the Settings app and scroll down to
the System section
NOTE: If you already have "Developer options"
under System then go directly to #5
2) Tap on "About phone"
3) Tap "Build number" 7 times until you get a message
that says you are now a developer
4) Go back to the main Settings app
5) Tap on "Developer options"
6) Ensure that "USB debugging" is enabled
7) Tap "OK" if you see a dialog asking you to allow
USB Debugging for your computer's RSA key fingerprint
WARNING: Installing Maru will wipe all your personal data
so make sure you first back-up anything important!
EOF
mecho () {
# use /bin/echo for portability with the '-n' option on Macs
/bin/echo "$@"
}
mexit () {
cd - >/dev/null 2>&1
exit $1
}
fatal () {
cat <<EOF
Yikes, something went wrong with the installation. We're sorry.
Please contact us at [email protected] with the issue you are facing and we'll
personally walk you through the install process.
EOF
mexit 1
}
echo_incomplete_zip () {
cat <<EOF
Hmm, looks like your installer is a missing a few things.
Are you running this install script outside the directory you unzipped Maru in?
If that isn't it, please try downloading the installer again.
EOF
}
echo_device_not_found () {
cat <<EOF
Hmm, your device can't be found.
Please ensure that:
1. Your device is connected to your computer over USB
2. You have USB Debugging enabled (see above for instructions)
3. You unlock your device and tap "OK" if you see a dialog asking you
to allow USB Debugging for your computer's RSA key fingerprint
Go ahead and re-run the installer when you're ready.
EOF
}
echo_permissions_udev () {
cat <<EOF
On certain Linux distributions (Ubuntu 14.04 for example),
you will need to explicitly add permissions to access USB devices:
1. Disconnect your device from USB
2. Run this in a terminal (requires sudo):
$ wget -S -O - https://github.com/snowdream/51-android/blob/master/51-android.rules | sed "s/<username>/$USER/" | sudo tee >/dev/null /etc/udev/rules.d/51-android.rules; sudo udevadm control --reload-rules
3. Re-connect your device over USB and re-run this installer
EOF
}
echo_incorrect_installer() {
local readonly product="$1"
cat <<EOF
Woops, looks like you are using the wrong installer for your device!
Please download the correct installer, making sure that your device codename
($product) is listed in the zip file name.
EOF
}
echo_unlock_reboot () {
cat <<EOF
Successfully unlocked bootloader!
Your device will need to reboot before continuing. It will factory reset, so
this reboot can take a few minutes longer than usual.
Please re-run this script after your device completely boots up and you have
re-enabled USB Debugging.
EOF
}
echo_success () {
cat <<EOF
The first boot will take 2-3 mins as Maru sets up your device so please be
patient.
Rebooting into Maru...
EOF
}
check_fastboot () {
if [ "$(./fastboot devices | cut -f 1)" = "no permissions" ] ; then
return 1
fi
return 0
}
check_is_bootloader () {
if [ "$(./fastboot devices | wc -l)" -eq 0 ] ; then
return 1
fi
return 0
}
fastboot_get_product () {
echo "$(./fastboot getvar product 2>&1 | grep -i "product" | cut -f 2 -d " ")"
}
fastboot_get_lock_state_generic () {
local readonly unlocked="$(./fastboot oem device-info 2>&1 | grep -i "device unlocked" | cut -f 4 -d " ")"
if [ "$unlocked" = true ] ; then
echo "unlocked"
else
echo "locked"
fi
}
fastboot_get_lock_state () {
local readonly product="$1"
case "$product" in
# flo is weird and shows incorrect lock state in oem device-info, so use this workaround
flo) echo "$(./fastboot getvar lock_state 2>&1 | uniq | grep -i "lock_state" | cut -f 2 -d " ")" ;;
*) echo "$(fastboot_get_lock_state_generic)" ;;
esac
}
unlock_bootloader () {
mecho -n "Unlocking bootloader, you will need to confirm this on your device..."
if ! ./fastboot oem unlock >/dev/null 2>&1 ; then
echo "ERROR"
fatal
fi
echo "OK"
}
main () { # avoid global namespace for local variables
# enforce same directory as script
# this allows double-clicking the script to work on mac
cd "$(dirname "$0")"
mecho -n "Are you ready to install Maru? (yes/no): "
read response
mecho
if [ "$response" != "yes" ] ; then
mecho "Aborting installation."
mexit $SUCCESS_USER_ABORT
fi
mecho -n "Checking for a complete installation zip..."
if [ ! -f android-info.txt ] || [ ! -f boot.img ] || [ ! -f system.img ] ; then
echo "ERROR"
echo_incomplete_zip
mexit $ERROR_INCOMPLETE_ZIP
fi
echo "OK"
if ! check_is_bootloader ; then
mecho -n "Rebooting your device into bootloader..."
./adb reboot bootloader >/dev/null 2>&1 || {
echo "ERROR"
echo_device_not_found
mexit $ERROR_ADB
}
echo "OK"
# wait for the device to reboot into bootloader
sleep 7
fi
if ! check_is_bootloader ; then
fatal
fi
if ! check_fastboot ; then
echo "ERROR"
echo_permissions_udev
mexit $ERROR_FASTBOOT_PERMS
fi
mecho -n "Checking that this is the correct installer for your device..."
local readonly product="$(fastboot_get_product)"
if ! grep "$product" < android-info.txt &>/dev/null ; then
echo "ERROR"
echo_incorrect_installer "$product"
mexit $ERROR_INCORRECT_INSTALLER
fi
echo "OK"
mecho -n "Checking bootloader lock state..."
local readonly lock_state="$(fastboot_get_lock_state "$product")"
if [ "$lock_state" = locked ] ; then
echo "LOCKED"
unlock_bootloader
echo_unlock_reboot
./fastboot reboot >/dev/null 2>&1
mexit $SUCCESS_BOOTLOADER_UNLOCKED
else
echo "UNLOCKED"
fi
# echo "BAIL!"
# mexit 1
# point of no return!
mecho
mecho "Installing Maru, please keep your device connected..."
./fastboot format cache
./fastboot flash boot boot.img
./fastboot flash system system.img
if [ ! -f userdata.img ] ; then
./fastboot format userdata
else
./fastboot flash userdata userdata.img -w
fi
mecho
mecho "Installation complete!"
echo_success
./fastboot reboot >/dev/null 2>&1
mexit $SUCCESS
}
main