forked from flatfish-fox/flatfish-flash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flash-flatfish.sh
executable file
·221 lines (194 loc) · 7.08 KB
/
flash-flatfish.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
#!/bin/bash
#
# Description: Script to flash FxOS images for Flatfish
# Features:
# - Flash images from witin the flatfish B2G source directory
# - Flash images from any given directory
# - Allow the user to provide the path to the B2G source, in order to use the 'adb' in there.
# - Multiple checkings, including image file existence, adb status and running mode, device status, etc.
# - Convert Flatfish into fastboot mode, flash, and reboot it.
# - Allow the device to be running under the 'adb' or 'fastboot' mode.
#
# Author: William Liang, Copyright 2014
#
# This program file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# GPL Source: https://github.com/flatfish-fox/flatfish-flash
#
# Date: $Date: 2014/07/01 08:22:58 $
# Version: $Revision: 1.6 $
#
# History:
#
# $Log: flash-flatfish,v $
# Revision 1.6 2014/07/01 08:22:58 wyliang
# Released by GPL
#
# Revision 1.5 2014/06/21 05:19:40 wyliang
# Fix: 1. The bug of wrong B2G source path detection. 2. Solve the case when the udev rule is not well set which makes the fastboot fail to run.
#
# Revision 1.4 2014/06/19 05:40:49 wyliang
# Allow the device to already exist in fastboot mode; fix some bugs; add some new checkings
#
# Revision 1.3 2014/06/18 09:14:26 wyliang
# Support flashing images in any directory
#
# Revision 1.2 2014/03/16 14:14:53 wyliang
# Support extract version
#
# Revision 1.1 2013/11/20 09:04:14 wyliang
# First time to create the flash-flatfish script
#
#
OUT=out/target/product/flatfish
BOOT=boot.img
SYSTEM=system.img
DATA=userdata.img
B2G_DIR=""
FBT_SUDO=""
IN_B2G=1
usage() {
PROG=`basename $0`
echo -e "\n=== Usage ==="
echo "Usage: $PROG [-h] [<path_to_images> [<path_to_b2g_root>]]"
echo "Options:"
echo " -h: help"
echo " <path_to_images>: default to ./$OUT/"
echo " <path_to_b2g_root>: path to the root of the B2G source, for using 'adb' only. "\
"If 'adb' can be found in \$PATH, this option can be ignored."
echo "Example:"
echo " 1. Run in B2G root directory."
echo " $ $PROG"
echo " 2. flash images in the current directory"
echo " $ $PROG ."
echo " 3. flash images in ~/flatfish-images"
echo " $ $PROG ~/flatfish-images"
echo " 3. flash images in ~/flatfish-images, and specify the source path of B2G for using 'adb'"
echo " $ $PROG ~/flatfish-images ~/dev_fxos/a31-b2g"
exit 1
}
run() {
printf "\n[[ Execute: $* ]]\n"
if ! $*; then
echo "Problem occured. Process abort!"
usage
fi
}
# Apply B2G environment
apply_b2g_env() {
[ ! -r "$B2G_DIR/build/envsetup.sh" ] && return
printf "B2G_DIR found, setup the flatfish build environment to enable 'adb' and 'fastboot' ... "
(
pushd $B2G_DIR
run source $B2G_DIR/build/envsetup.sh
lunch 5
popd
) > /dev/null
echo "Done.."
}
# File checking
check_images() {
printf "\n--- Image files checking ---\n"
if [ "$IN_B2G" = 0 ]; then
[ ! -d "$OUT" ] && echo "Error: the specified directory '$OUT' is not a directory!" && usage
else
echo "No specified image directory. We are supposed to be in the root directory of B2G. Verify the path now."
if [ -d device/allwinners/flatfish ]; then # Internal source
[ ! -f "$OUT/$BOOT" ] && echo "Error: '$OUT/$BOOT' does not exist." && usage
elif [ ! -d device/allwinner/flatfish ]; then # open source
echo "Error: You need to go to the root directory of the B2G flatfish source, or specify the directory in which images exist."
usage
fi
echo "Yes, we are now in the root directory of B2G."
fi
DOFLASH=0
[ ! -f "$OUT/$BOOT" ] && echo "Warning: '$OUT/$BOOT' does not exist." && DOIT=1
[ ! -f "$OUT/$SYSTEM" ] && echo "Warning: '$OUT/$SYSTEM' does not exist!"
[ ! -f "$OUT/$DATA" ] && echo "Warning: '$OUT/$DATA' does not exist!"
echo "Good, image files are in position now."
}
# Enable adb
adb_to_fastboot() {
printf "\n--- Check to see if the adb works properly (for Android 4.2 and above) ---\n"
if ! which adb > /dev/null 2>&1; then
echo "'adb' is not found. Please add the path of adb in the PATH environment variable."
return 1
fi
if adb devices | grep 'no permissions' > /dev/null 2>&1; then
echo "Warning: It seems that the 'adb' command was not run correctly (in superuser mode)."
echo "--> Re-activate adb by 'sudo adb devices'. (Note: your password is required.)"
run adb kill-server
run sudo `which adb` devices
fi
echo "'adb' works fine."
# Check the readiness of the device
printf "\n--- Check to see if the flatfish device is ready ---\n"
if adb devices 2> /dev/null | grep FLATFISH > /dev/null 2>&1; then
echo "Good, it looks to be Okay."
else
echo "Oh, the flatfish device is not there, or is not turned on in a mode where 'adb' can be used."
return 1
fi
# Enable fastboot
printf "\n--- Change into the fastboot mode ---\n"
if ! run adb shell reboot boot-fastboot && ! run adb shell su -c reboot boot-fastboot; then
echo "'adb' failed to reboot to fastboot mode."
fi
# wait for the fastboot to take effect
printf "Wait for 10 seconds for the device to get ready in the fastboot mode ... "
i=9; while [ $i -gt 0 ]; do printf "\b$i"; i=$(($i-1)); sleep 1; done
printf "\bDone.\n"
}
# Try to use fastboot directly
check_fastboot() {
printf "\n--- Let's see if it's already in the fastboot mode ---\n"
FBT_CHK=`fastboot devices`
if echo "$FBT_CHK" | grep fastboot > /dev/null 2>&1; then
echo "Fastboot mode detected."
if echo "$FBT_CHK" | grep 'no permissions' > /dev/null 2>&1; then
echo "However, the udev rule for the device may not be well configured. Will request the user to run in sudo mode"
FBT_SUDO="sudo"
fi
else
echo "Error: Fastboot mode is not detected. Check to see if the device is not turned on."
echo "*Note: If it is powered but both 'adb' and 'fastboot' could not work, "
echo " please check to see if the device has become brick. (Bug 1026963)"
usage
fi
}
echo "Start the flash-flatfish.sh procedure."
# Check for help option
[ "$1" = "-h" ] && usage
if [ -z "$1" ]; then
IN_B2G=1
B2G_DIR="."
apply_b2g_env
else
IN_B2G=0
OUT="$1"
if [ -n "$2" ]; then
B2G_DIR="$2"
apply_b2g_env
fi
fi
# Check images, adb, and fastboot mode
check_images
adb_to_fastboot
check_fastboot
# Start the flashing process
printf "\n--- Start to flash boot, system, and data partitions ---\n"
FASTBOOT=`which fastboot`
[ ! -x "$FASTBOOT" ] && echo "Error: Fastboot not found! Abort." && usage
[ -f "$OUT/$BOOT" ] && run $FBT_SUDO $FASTBOOT flash boot $OUT/boot.img
run $FBT_SUDO $FASTBOOT flash system $OUT/system.img
run $FBT_SUDO $FASTBOOT flash data $OUT/userdata.img
# Reboot the system
printf "\n--- Reboot the device ---\n"
run $FBT_SUDO $FASTBOOT reboot