aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/edison/flashall
blob: 2b9bc017eb85f006eb3d89a971f9fcef9f0dcd07 (plain)
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
#!/bin/bash

if ! [ $(id -u) = 0 ]; then
    echo "This script must be run as root"
    exit 1
fi

BACKUP_IFS=$IFS
IFS=$(echo -en "\n\b")

GETOPTS="$(which getopt)"
if [[ "$OSTYPE" == "darwin"* ]] ; then READLINK=greadlink; GETOPTS="$(brew list gnu-getopt | grep bin/getopt)"; else READLINK=readlink;fi;
BASE_DIR="$(dirname $($READLINK -f $0))"
ESC_BASE_DIR=${BASE_DIR/' '/'\ '}

USB_VID=8087
USB_PID=0a99
TIMEOUT_SEC=60

DO_RECOVERY=0
DO_DFU_FLASH=1
# Phone Flash tools configuration files
PFT_XML_FILE="${BASE_DIR}/pft-config-edison.xml"

# Handle Ifwi file for DFU update
IFWI_DFU_FILE=${ESC_BASE_DIR}/edison_ifwi-dbg

DO_LIST_VARIANTS=0
VAR_DIR="${BASE_DIR}/u-boot-envs"
VARIANT_NAME_DEFAULT="edison-default"
VARIANT_NAME_BLANK="edison-blank"
VARIANT_NAME=$VARIANT_NAME_BLANK

LOG_FILENAME="flash.log"
OUTPUT_LOG_CMD="2>&1 | tee -a ${LOG_FILENAME} | ( sed -n '19 q'; head -n 1; cat >/dev/null )"

function print-usage {
	cat << EOF
Usage: ${0##*/} [-hl][--help][--recovery] [--keep-data] [-m][-ipdb] [-t uboot-env]
Update all software and restore board to its initial state.
 -h,--help     display this help and exit.
 -v            verbose output
 --recovery    recover the board to DFU mode using a dedicated tool,
               available only on linux and window hosts.
 --keep-data   preserve user data when flashing.


 deprecated commands:
 -i            flash IFWI.
 -d            flash U-boot, U-boot Environment, Linux Kernel, Rootfs.
 -b            blank the device (eq -i -d -t ${VARIANT_NAME_BLANK}).
 -l            list availables U-boot target environments and exit.
 -t uboot-env  specify U-boot target environments to flash (default is ${VARIANT_NAME}).
EOF
	exit -5
}

function flash-command-try {
	eval dfu-util -v -d ${USB_VID}:${USB_PID} $@ $OUTPUT_LOG_CMD
}

function flash-dfu-ifwi {
	ifwi_hwid_found=`dfu-util -l -d ${USB_VID}:${USB_PID} | grep -c $1`
	if [ $ifwi_hwid_found -ne 0 ];
	then
		flash-command ${@:2}
	fi
}

function flash-command {
	flash-command-try $@
	if [ $? -ne 0 ] ;
	then
		echo "Flash failed on $@"
		exit -1
	fi
}

function flash-debug {
	echo "DEBUG: lsusb"
	lsusb
	echo "DEBUG: dfu-util -l"
	dfu-util -l
}

function flash-ifwi {
	if [ -x "$(which phoneflashtool)" ]; then
		flash-ifwi-pft
	elif [ -x "$(which xfstk-dldr-solo)" ]; then
		flash-ifwi-xfstk
	else
		echo "!!! You should install xfstk tools, please visit http://xfstk.sourceforge.net/"
		exit -1
	fi
}

function flash-ifwi-pft {
	eval phoneflashtool --cli -f "$PFT_XML_FILE"
	if [ $? -ne 0 ];
	then
		echo "Phoneflashtool error"
		flash-debug
		exit -1
	fi
}

function flash-ifwi-xfstk {
	XFSTK_PARAMS=" --gpflags 0x80000007 --osimage ${ESC_BASE_DIR}/u-boot-edison.img"
	XFSTK_PARAMS="${XFSTK_PARAMS} --fwdnx ${ESC_BASE_DIR}/edison_dnx_fwr.bin"
	XFSTK_PARAMS="${XFSTK_PARAMS} --fwimage ${ESC_BASE_DIR}/edison_ifwi-dbg-00.bin"
	XFSTK_PARAMS="${XFSTK_PARAMS} --osdnx ${ESC_BASE_DIR}/edison_dnx_osr.bin"

	eval xfstk-dldr-solo ${XFSTK_PARAMS}
	if [ $? -ne 0 ];
	then
		echo "Xfstk tool error"
		flash-debug
		exit -1
	fi
}

function dfu-wait {
	echo "Now waiting for dfu device ${USB_VID}:${USB_PID}"
	echo "Please plug and reboot the board"
	while [ `dfu-util -l -d ${USB_VID}:${USB_PID} | grep Found | grep -c ${USB_VID}` -eq 0 ] \
		&& [ $TIMEOUT_SEC -gt 0 ] && [ $(( TIMEOUT_SEC-- )) ];
	do
		sleep 1
	done

	if [ $TIMEOUT_SEC -eq 0 ];
	then
		echo "Timed out while waiting for dfu device ${USB_VID}:${USB_PID}"
		flash-debug
		echo "Did you plug and reboot your board?"
		echo "If yes, please try a recovery by calling this script with the --recovery option"
		exit -2
	fi
}

# Execute old getopt to have long options support
ARGS=$($GETOPTS -o hvlt:eidb -l "keep-data,recovery,help" -n "${0##*/}" -- "$@");
#Bad arguments
if [ $? -ne 0 ]; then print-usage ; fi;
eval set -- "$ARGS";

while true; do
	case "$1" in
		-l) shift; DO_LIST_VARIANTS=1;;
		-t) shift; if [ -n "$1" ]; then VARIANT_NAME=$1; shift; fi;;
		-h|--help) shift; print-usage;;
		-v) shift; OUTPUT_LOG_CMD=" 2>&1 | tee -a ${LOG_FILENAME}";;
		-i) shift; DO_RECOVERY=1;;
		-d) shift; DO_DFU_FLASH=1;;
		-b) shift; DO_RECOVERY=1; DO_DFU_FLASH=1; VARIANT_NAME=$VARIANT_NAME_BLANK;;
		--recovery) shift; DO_RECOVERY=1;DO_DFU_FLASH=0;;
		--keep-data) shift; DO_RECOVERY=0; VARIANT_NAME=$VARIANT_NAME_DEFAULT;;
		--) shift; break;;
	esac
done

if [ ${DO_LIST_VARIANTS} -eq 1 ];
then
	echo "Availables U-boot targets:"
	for variant in $(ls ${VAR_DIR}) ; do
		#echo just filname without extension
		echo "${variant%.*}"
	done
	exit -5
fi

if [[ ${DO_RECOVERY} -eq 1 || ${DO_DFU_FLASH} -eq 1 ]];
then
	echo "** Flashing Edison Board $(date) **" >> ${LOG_FILENAME}
fi


if [ ${DO_RECOVERY} -eq 1 ];
then
	if [[ "$OSTYPE" == "darwin"* ]] ; then
		echo "Recovery mode is only available on windows and linux";
		exit -3
	fi

	echo "Starting Recovery mode"
	echo "Please plug and reboot the board"
	if [ ! -f "${PFT_XML_FILE}" ];
	then
		echo "${PFT_XML_FILE} does not exist"
		exit -3
	fi
	echo "Flashing IFWI"
	flash-ifwi
	echo "Recovery Success..."
	echo "You can now try a regular flash"
fi

if [ ${DO_DFU_FLASH} -eq 1 ];
then
	echo "Using U-Boot target: ${VARIANT_NAME}"
	VARIANT_FILE="${VAR_DIR}/${VARIANT_NAME}.bin"
	if [ ! -f "${VARIANT_FILE}" ]; then
		echo "U-boot target ${VARIANT_NAME}: ${VARIANT_FILE} not found aborting"
		exit -5
	fi
	VARIANT_FILE=${VARIANT_FILE/' '/'\ '}

	dfu-wait

	echo "Flashing IFWI"

	flash-dfu-ifwi ifwi00 --alt ifwi00 -D "${IFWI_DFU_FILE}-00-dfu.bin"
	flash-dfu-ifwi ifwib00 --alt ifwib00 -D "${IFWI_DFU_FILE}-00-dfu.bin"

	flash-dfu-ifwi ifwi01 --alt ifwi01 -D "${IFWI_DFU_FILE}-01-dfu.bin"
	flash-dfu-ifwi ifwib01 --alt ifwib01 -D "${IFWI_DFU_FILE}-01-dfu.bin"

	flash-dfu-ifwi ifwi02 --alt ifwi02 -D "${IFWI_DFU_FILE}-02-dfu.bin"
	flash-dfu-ifwi ifwib02 --alt ifwib02 -D "${IFWI_DFU_FILE}-02-dfu.bin"

	flash-dfu-ifwi ifwi03 --alt ifwi03 -D "${IFWI_DFU_FILE}-03-dfu.bin"
	flash-dfu-ifwi ifwib03 --alt ifwib03 -D "${IFWI_DFU_FILE}-03-dfu.bin"

	flash-dfu-ifwi ifwi04 --alt ifwi04 -D "${IFWI_DFU_FILE}-04-dfu.bin"
	flash-dfu-ifwi ifwib04 --alt ifwib04 -D "${IFWI_DFU_FILE}-04-dfu.bin"

	flash-dfu-ifwi ifwi05 --alt ifwi05 -D "${IFWI_DFU_FILE}-05-dfu.bin"
	flash-dfu-ifwi ifwib05 --alt ifwib05 -D "${IFWI_DFU_FILE}-05-dfu.bin"

	flash-dfu-ifwi ifwi06 --alt ifwi06 -D "${IFWI_DFU_FILE}-06-dfu.bin"
	flash-dfu-ifwi ifwib06 --alt ifwib06 -D "${IFWI_DFU_FILE}-06-dfu.bin"

	echo "Flashing U-Boot"
	flash-command --alt u-boot0 -D "${ESC_BASE_DIR}/u-boot-edison.bin"

	echo "Flashing U-Boot Environment"
	flash-command --alt u-boot-env0 -D "${VARIANT_FILE}"

	echo "Flashing U-Boot Environment Backup"
	flash-command --alt u-boot-env1 -D "${VARIANT_FILE}" -R

	dfu-wait

	echo "Flashing boot partition (kernel)"
	flash-command --alt boot -D "${ESC_BASE_DIR}/eca-image-edison.hddimg"

	echo "Flashing rootfs, (it can take up to 5 minutes... Please be patient)"
	flash-command --alt rootfs -D "${ESC_BASE_DIR}/eca-image-edison.ext4" -R

	echo "Rebooting"
	echo "U-boot & Kernel System Flash Success..."
	if [ $VARIANT_NAME == $VARIANT_NAME_BLANK ] ; then
		echo "Your board needs to reboot twice to complete the flashing procedure, please do not unplug it for 2 minutes."
	fi
fi

if [[ ${DO_DFU_FLASH} -eq 0 &&  ${DO_RECOVERY} -eq 0 ]];
then
	echo "Nothing to do ..."
	print-usage
fi

IFS=${BACKUP_IFS}