forked from askeing/B2G-flash-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shallow_flash.sh
executable file
·447 lines (390 loc) · 13 KB
/
shallow_flash.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#!/bin/bash
#==========================================================================
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#==========================================================================
# Description:
# This script was written for shallow flash the gaia and/or gecko.
#
# Author: Askeing [email protected]
# History:
# 2013/08/02 Askeing: v1.0 First release.
# 2014/08/09 Added Cygwin support by revelon.
#==========================================================================
####################
# Parameter Flags #
####################
VERY_SURE=false
KEEP_PROFILE=false
ADB_DEVICE="Device"
FLASH_GAIA=false
FLASH_GAIA_FILE=""
FLASH_GECKO=false
FLASH_GECKO_FILE=""
NO_FTU=${NO_FTU:-false}
# for other bash script tools call.
case `uname` in
"Linux"|"CYGWIN"*) SP="";;
"Darwin") SP=" ";;
esac
####################
# Functions #
####################
## helper function
function helper(){
echo -e "This script was written for shallow flash of gaia and/or gecko.\n"
echo -e "Usage: ./shallow_flash.sh [parameters]"
echo -e "-g|--gaia\tFlash the gaia (zip format) onto your device."
echo -e "-G|--gecko\tFlash the gecko (tar.gz format) onto your device."
echo -e "--keep_profile\tKeep the user profile on your device. (BETA)"
echo -e "-s <serial number>\tdirects command to device with the given serial number."
echo -e "-y\t\tflash the file without asking askeing (it's a joke...)"
echo -e "-h|--help\tDisplay help."
echo -e "Example:"
case `uname` in
"Linux"|"CYGWIN"*)
echo -e " Flash gaia.\t\t./shallow_flash.sh --gaia=gaia.zip"
echo -e " Flash gecko.\t\t./shallow_flash.sh --gecko=b2g-18.0.en-US.android-arm.tar.gz"
echo -e " Flash gaia and gecko.\t./shallow_flash.sh -ggaia.zip -Gb2g-18.0.en-US.android-arm.tar.gz";;
"Darwin")
echo -e " Flash gaia.\t\t./shallow_flash.sh --gaia gaia.zip"
echo -e " Flash gecko.\t\t./shallow_flash.sh --gecko b2g-18.0.en-US.android-arm.tar.gz"
echo -e " Flash gaia and gecko.\t./shallow_flash.sh -g gaia.zip -G b2g-18.0.en-US.android-arm.tar.gz";;
esac
exit 0
}
## adb with flags
function run_adb()
{
# TODO: Bug 875534 - Unable to direct ADB forward command to inari devices due to colon (:) in serial ID
# If there is colon in serial number, this script will have some warning message.
adb $ADB_FLAGS $@
}
## make sure user want to shallow flash
function make_sure() {
echo "Are you sure you want to flash "
if [[ $FLASH_GAIA == true ]]; then
echo -e "Gaia: $FLASH_GAIA_FILE "
fi
if [[ $FLASH_GECKO == true ]]; then
echo -e "Gecko: $FLASH_GECKO_FILE "
fi
read -p "to your $ADB_DEVICE? [y/N]" isFlash
test "$isFlash" != "y" && test "$isFlash" != "Y" && echo -e "bye bye." && exit 0
}
## check the return code, exit if return code is not zero.
function check_exit_code() {
RET=$1
ERROR_MSG=$2
if [[ ${RET} != 0 ]]; then
if [[ -z ${ERROR_MSG} ]]; then
echo "### Failed!"
else
echo "### Failed: ${ERROR_MSG}"
fi
exit 1
fi
}
function check_adb_result() {
RET=$1
if [[ $RET == *"error"* ]]; then
check_exit_code 1 "Please make sure adb is running as root."
elif [[ $RET == *"cannot run as root"* ]]; then
check_exit_code 1 "Please make sure adb is running as root."
elif [[ $RET == *"remount failed"* ]]; then
check_exit_code 1 "Please make sure adb is running as root."
elif [[ $RET == *"Operation not permitted"* ]]; then
check_exit_code 1 "Please make sure adb is running as root."
fi
}
## adb root, then remount and stop b2g
function adb_root_remount() {
echo "### Waiting for device... please ensure it is connected, switched on and remote debugging is enabled in Gaia"
run_adb wait-for-device #in: gedit display issue
echo "### Restarting adb with root permissions..."
RET=$(run_adb root)
echo "$RET"
check_adb_result "$RET"
run_adb wait-for-device #in: gedit display issue
echo "### Remounting the /system partition..."
RET=$(run_adb remount)
echo "$RET"
check_adb_result "$RET"
RET=$(run_adb shell mount -o remount,rw /system)
echo "$RET"
check_adb_result "$RET"
echo "### Stopping b2g process..."
run_adb shell stop b2g
}
## adb sync then reboot
function adb_reboot() {
run_adb shell sync
run_adb shell reboot
run_adb wait-for-device #in: gedit display issue
}
## clean cache, gaia (webapps) and profiles
function adb_clean_gaia() {
echo "### Cleaning Gaia and Profiles ..."
run_adb shell rm -r /cache/* &&
run_adb shell rm -r /data/b2g/* &&
run_adb shell rm -r /data/local/storage/persistent/* &&
run_adb shell rm -r /data/local/svoperapps &&
run_adb shell rm -r /data/local/webapps &&
run_adb shell rm -r /data/local/user.js &&
run_adb shell rm -r /data/local/permissions.sqlite* &&
run_adb shell rm -r /data/local/OfflineCache &&
run_adb shell rm -r /data/local/indexedDB &&
run_adb shell rm -r /data/local/debug_info_trigger &&
run_adb shell rm -r /system/b2g/webapps &&
echo "### Cleaning Done."
}
## push gaia into device
function adb_push_gaia() {
GAIA_DIR=$1
## Adjusting user.js
cat $GAIA_DIR/gaia/profile/user.js | sed -e "s/user_pref/pref/" > $GAIA_DIR/user.js &&
if [[ `uname` == "CYGWIN"* ]]; then
if [[ ! -d "/cygdrive/c/tmp" ]]; then
mkdir "/cygdrive/c/tmp"
fi
cp -r $GAIA_DIR /cygdrive/c/tmp/
fi &&
echo "### Pushing Gaia to device ..."
run_adb shell mkdir -p /system/b2g/defaults/pref &&
run_adb push $GAIA_DIR/gaia/profile/webapps /system/b2g/webapps &&
run_adb push $GAIA_DIR/user.js /system/b2g/defaults/pref &&
run_adb push $GAIA_DIR/gaia/profile/settings.json /system/b2g/defaults &&
echo "### Push Done."
}
## shallow flash gaia
function shallow_flash_gaia() {
GAIA_ZIP_FILE=$1
if ! [[ -f $GAIA_ZIP_FILE ]]; then
echo "### Cannot find $GAIA_ZIP_FILE file."
exit 2
fi
if ! which mktemp > /dev/null; then
echo "### Package \"mktemp\" not found!"
rm -rf ./shallowflashgaia_temp
mkdir shallowflashgaia_temp
cd shallowflashgaia_temp
TMP_DIR=`pwd`
cd ..
else
TMP_DIR=`mktemp -d -t shallowflashgaia.XXXXXXXXXXXX`
fi
unzip_file $GAIA_ZIP_FILE $TMP_DIR &&
adb_clean_gaia &&
adb_push_gaia $TMP_DIR
check_exit_code $? "Pushing Gaia to device failed."
rm -rf $TMP_DIR
}
## unzip zip file
function unzip_file() {
ZIP_FILE=$1
DEST_DIR=$2
if ! [[ -z $ZIP_FILE ]]; then
test ! -f $ZIP_FILE && echo -e "### The file $ZIP_FILE DOES NOT exist." && exit 2
else
echo "### No input zip file."
exit 2
fi
echo "### Unzip $ZIP_FILE to $DEST_DIR ..."
test -e $ZIP_FILE && unzip -q $ZIP_FILE -d $DEST_DIR
check_exit_code $? "Unzip $ZIP_FILE Failed."
#ls -LR $DEST_DIR
}
## shallow flash gecko
function shallow_flash_gecko() {
GECKO_TAR_FILE=$1
if ! [[ -f $GECKO_TAR_FILE ]]; then
echo "### Cannot find $GECKO_TAR_FILE file."
exit 2
fi
if ! which mktemp > /dev/null; then
echo "### Package \"mktemp\" not found!"
rm -rf ./shallowflashgecko_temp
mkdir shallowflashgecko_temp
cd shallowflashgecko_temp
TMP_DIR=`pwd`
cd ..
else
TMP_DIR=`mktemp -d -t shallowflashgecko.XXXXXXXXXXXX`
fi
## push gecko into device
untar_file $GECKO_TAR_FILE $TMP_DIR &&
if [[ `uname` == "CYGWIN"* ]]; then
cp -r $TMP_DIR /cygdrive/c/tmp/
fi &&
echo "### Pushing Gecko to device..." &&
run_adb push $TMP_DIR/b2g /system/b2g &&
echo "### Push Done."
check_exit_code $? "Pushing Gecko to device failed."
rm -rf $TMP_DIR
}
## untar tar.gz file
function untar_file() {
TAR_FILE=$1
DEST_DIR=$2
if ! [[ -z $TAR_FILE ]]; then
test ! -f $TAR_FILE && echo -e "### The file $TAR_FILE DOES NOT exist." && exit 2
else
echo "### No input tar file."
exit 2
fi
echo "### Untar $TAR_FILE to $DEST_DIR ..."
test -e $TAR_FILE && tar -xzf $TAR_FILE -C $DEST_DIR
check_exit_code $? "Untar $TAR_FILE Failed."
#ls -LR $DEST_DIR
}
## option $1 is temp_folder
function backup_profile() {
DEST_DIR=$1
echo "### Profile back up to ${DEST_DIR}"
bash ./backup_restore_profile.sh -p${SP}${DEST_DIR} --no-reboot -b
}
## option $1 is temp_folder
function restore_profile() {
DEST_DIR=$1
echo "### Restore Profile from ${DEST_DIR}"
bash ./backup_restore_profile.sh -p${SP}${DEST_DIR} --no-reboot -r
}
## option $1 is temp_folder
function remove_profile() {
DEST_DIR=$1
echo "### Removing Profile under ${DEST_DIR}"
rm -rf ${DEST_DIR}
echo "### Removing Profile complete ."
}
#########################
# Processing Parameters #
#########################
## show helper if nothing specified
if [[ $# = 0 ]]; then echo "Nothing specified"; helper; exit 0; fi
## distinguish platform
case `uname` in
"Linux"|"CYGWIN"*)
## add getopt argument parsing
TEMP=`getopt -o g::G::s::yh --long gaia::,gecko::,keep_profile,help \
-n 'invalid option' -- "$@"`
if [[ $? != 0 ]]; then echo "Try '--help' for more information." >&2; exit 1; fi
eval set -- "$TEMP";;
"Darwin");;
esac
while true
do
case "$1" in
-h|--help) helper; exit 0;;
-g|--gaia)
FLASH_GAIA=true;
case "$2" in
"") echo -e "Please specify the gaia path.\nTry '--help' for more information.";exit 1;;
*) FLASH_GAIA_FILE=$2; shift 2;;
esac ;;
-G|--gecko)
FLASH_GECKO=true;
case "$2" in
"") echo -e "Please specify the gecko path.\nTry '--help' for more information."; exit 1;;
*) FLASH_GECKO_FILE=$2; shift 2;;
esac ;;
--keep_profile) if [[ -e ./backup_restore_profile.sh ]]; then KEEP_PROFILE=true; else echo "### There is no backup_restore_profile.sh file."; fi; shift;;
-s)
case "$2" in
"") shift 2;;
*) ADB_DEVICE=$2; ADB_FLAGS+="-s $2"; shift 2;;
esac ;;
-y) VERY_SURE=true; shift;;
--) shift;break;;
"") shift;break;;
*) helper; echo error occured; exit 1;;
esac
done
####################
# Make Sure #
####################
if [[ $VERY_SURE == false ]] && ([[ $FLASH_GAIA == true ]] || [[ $FLASH_GECKO == true ]]); then
make_sure
fi
if ! [[ -f $FLASH_GAIA_FILE ]] && [[ $FLASH_GAIA == true ]]; then
echo "### Cannot find $FLASH_GAIA_FILE file."
exit 2
fi
if ! [[ -f $FLASH_GECKO_FILE ]] && [[ $FLASH_GECKO == true ]]; then
echo "### Cannot find $FLASH_GECKO_FILE file."
exit 2
fi
####################
# ADB Work #
####################
adb_root_remount
####################
# Backup Profile #
####################
if [[ $KEEP_PROFILE == true ]] && ([[ $FLASH_GAIA == true ]] || [[ $FLASH_GECKO == true ]]) ; then
if ! which mktemp > /dev/null; then
echo "### Package \"mktemp\" not found!"
rm -rf ./profile_temp
mkdir profile_temp
cd profile_temp
TMP_PROFILE_DIR=`pwd`
cd ..
else
TMP_PROFILE_DIR=`mktemp -d -t shallowflashprofile.XXXXXXXXXXXX`
fi
backup_profile ${TMP_PROFILE_DIR}
fi
####################
# Processing Gaia #
####################
if [[ $FLASH_GAIA == true ]]; then
echo "### Processing Gaia: $FLASH_GAIA_FILE"
shallow_flash_gaia $FLASH_GAIA_FILE
fi
####################
# Processing Gecko #
####################
if [[ $FLASH_GECKO == true ]]; then
echo "### Processing Gecko: $FLASH_GECKO_FILE"
shallow_flash_gecko $FLASH_GECKO_FILE
fi
####################
# NO FTU #
####################
if [[ ${NO_FTU} == true ]]; then
if [[ -f ./disable_ftu.py ]]; then
echo "### Processing NO FTU..."
./disable_ftu.py
echo "### NO FTU done."
fi
fi
####################
# Restore Profile #
####################
if [[ $KEEP_PROFILE == true ]] && ([[ $FLASH_GAIA == true ]] || [[ $FLASH_GECKO == true ]]) ; then
restore_profile ${TMP_PROFILE_DIR}
remove_profile ${TMP_PROFILE_DIR}
fi
## Hack for windows with cygwin for permission issue
## should use the correct permission instead of 777
## TODO: resolve in some better way in the future?
if [[ `uname` == "CYGWIN"* ]]; then
run_adb shell chmod 777 /system/b2g/b2g
run_adb shell chmod 777 /system/b2g/updater
run_adb shell chmod 777 /system/b2g/run-mozilla.sh
run_adb shell chmod 777 /system/b2g/plugin-container
fi &&
####################
# ADB Work #
####################
adb_reboot
####################
# Version #
####################
if [[ -e ./check_versions.sh ]]; then
bash ./check_versions.sh
fi
####################
# Done #
####################
echo -e "### Shallow Flash Successful!"