-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcm-fx6-bootloader-update.sh
executable file
·293 lines (233 loc) · 6.62 KB
/
cm-fx6-bootloader-update.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
#! /bin/bash
#
# CompuLab CM-FX6 module boot loader update utility
#
# Copyright (C) 2013-2015 CompuLab, Ltd.
# Author: Igor Grinberg <[email protected]>
#
# This program 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; either version 2 of the License, or
# (at your option) any later version.
#
# 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.
UPDATER_VERSION="2.4.0"
UPDATER_VERSION_DATE="Sep 25 2017"
UPDATER_BANNER="CompuLab CM-FX6 (Utilite) boot loader update utility ${UPDATER_VERSION} (${UPDATER_VERSION_DATE})"
NORMAL="\033[0m"
WARN="\033[33;1m"
BAD="\033[31;1m"
BOLD="\033[1m"
GOOD="\033[32;1m"
function good_msg() {
local msg_string=$1
msg_string="${msg_string:-...}"
echo -e "${GOOD}>>${NORMAL}${BOLD} ${msg_string} ${NORMAL}"
}
function bad_msg() {
local msg_string=$1
msg_string="${msg_string:-...}"
echo -e "${BAD}!!${NORMAL}${BOLD} ${msg_string} ${NORMAL}"
}
function warn_msg() {
local msg_string=$1
msg_string="${msg_string:-...}"
echo -e "${WARN}**${NORMAL}${BOLD} ${msg_string} ${NORMAL}"
}
function DD() {
dd $* &> /dev/null & pid=$!
while [ -e /proc/$pid ] ; do
echo -n "."
sleep 1
done
echo ""
wait $pid
return $?
}
function confirm() {
good_msg "$1"
select yn in "Yes" "No"; do
case $yn in
"Yes")
return 0;
;;
"No")
return 1;
;;
*)
case ${REPLY,,} in
"y"|"yes")
return 0;
;;
"n"|"no"|"abort")
return 1;
;;
esac
esac
done
return 1;
}
EEPROM_DEV="/sys/bus/i2c/devices/2-0050/eeprom"
MTD_DEV="mtd0"
MTD_DEV_FILE="/dev/${MTD_DEV}"
CPU_NAME=""
DRAM_NAME=""
BOOTLOADER_FILE="cm-fx6-firmware"
function find_bootloader_file() {
read -e -p "Please input firmware file path (or press ENTER to use \"cm-fx6-firmware\"): " filepath
if [[ -n $filepath ]]; then
BOOTLOADER_FILE=`eval "echo $filepath"`
fi
good_msg "Looking for boot loader image file: $BOOTLOADER_FILE"
if [ ! -s $BOOTLOADER_FILE ]; then
bad_msg "Can't find boot loader image file for the board"
return 1;
fi
good_msg "...Found"
return 0;
}
function check_spi_flash() {
good_msg "Looking for SPI flash: $MTD_DEV"
grep -qE "$MTD_DEV: [0-f]+ [0-f]+ \"uboot\"" /proc/mtd
if [ $? -ne 0 ]; then
bad_msg "Can't find $MTD_DEV device, is the SPI flash support enabled in kernel?"
return 1;
fi
if [ ! -c $MTD_DEV_FILE ]; then
bad_msg "Can't find $MTD_DEV device special file: $MTD_DEV_FILE"
return 1;
fi
good_msg "...Found"
return 0;
}
function get_uboot_version() {
local file="$1"
grep -oaE "U-Boot [0-9]+\.[0-9]+.* \(... +[0-9]+ [0-9]+ - [0-9]+:[0-9]+:[0-9]+.*\)" "$file"
}
function check_bootloader_versions() {
local flash_version=`echo \`get_uboot_version $MTD_DEV_FILE\``
local file_version=`echo \`get_uboot_version $BOOTLOADER_FILE\``
local file_size=`du -hL $BOOTLOADER_FILE | cut -f1`
good_msg "Current U-Boot version in SPI flash:\t$flash_version"
good_msg "New U-Boot version in file:\t\t$file_version ($file_size)"
confirm "Proceed with the update?" && return 0;
return 1;
}
function check_utility() {
local util_name="$1"
local utility=`which "$util_name"`
if [[ -z "$utility" || ! -x $utility ]]; then
bad_msg "Can't find $util_name utility! Please install $util_name before proceding!"
return 1;
fi
return 0;
}
function check_utilities() {
good_msg "Checking for utilities..."
check_utility "diff" || return 1;
check_utility "grep" || return 1;
check_utility "sed" || return 1;
check_utility "hexdump" || return 1;
check_utility "dd" || return 1;
check_utility "flash_erase" || return 1;
good_msg "...Done"
return 0;
}
function erase_spi_flash() {
good_msg "Erasing SPI flash..."
flash_erase $MTD_DEV_FILE 0 0
if [ $? -ne 0 ]; then
bad_msg "Failed erasing SPI flash!"
bad_msg "If you reboot, your system might not boot anymore!"
bad_msg "Please, try re-installing mtd-utils package and retry!"
return 1;
fi
good_msg "...Done"
return 0;
}
function write_bootloader() {
good_msg "Writing boot loader to the SPI flash..."
DD if=$BOOTLOADER_FILE of=$MTD_DEV_FILE
if [ $? -ne 0 ]; then
bad_msg "Failed writing boot loader to the SPI flash!"
bad_msg "If you reboot, your system might not boot anymore!"
bad_msg "Please, try re-installing mtd-utils package and retry!"
return 1;
fi
good_msg "...Done"
return 0;
}
function check_bootloader() {
good_msg "Checking boot loader in the SPI flash..."
local test_file="${BOOTLOADER_FILE}.test"
local size=$((`du -L $BOOTLOADER_FILE | cut -f1`*1024))
DD if=$MTD_DEV_FILE of=$test_file bs=$size count=1
diff $BOOTLOADER_FILE $test_file > /dev/null
if [ $? -ne 0 ]; then
bad_msg "Boot loader check failed! Please retry the update procedure!"
return 1;
fi
rm -f $test_file
good_msg "...Done"
return 0;
}
function check_board() {
good_msg "Checking that board is CM-FX6 (Utilite)..."
local module=`hexdump -C $EEPROM_DEV | grep 00000080 | sed 's/.*|\(CM-FX6\).*/\1/g'`
if [ "$module" == "CM-FX6" ]; then
good_msg "...Done"
return 0;
fi;
bad_msg "This board is not a CM-FX6 (Utilite)!"
return 1;
}
function error_exit() {
bad_msg "Boot loader update failed!"
exit $1;
}
function env_set() {
local var=$1
local value=$2
fw_setenv $var "$value"
local match=`fw_printenv $var | grep -e "^$var=$value\$" | wc -l`
[ $match -eq 1 ] && return 0;
return 1;
}
function reset_environment() {
warn_msg "Resetting U-Boot environment will override any changes made to the environment!"
confirm "Reset U-Boot environment (recommended)?"
if [ $? -eq 1 ]; then
good_msg "U-boot environment will not be reset."
return 0;
fi
check_utility "fw_setenv" && check_utility "fw_printenv"
if [[ $? -ne 0 ]]; then
bad_msg "Cannot reset environment."
return 1;
fi
local bootcmd_new="env default -a && saveenv; reset"
env_set bootcmd "$bootcmd_new" && env_set bootdelay 0
if [[ $? -eq 0 ]]; then
good_msg "U-boot environment will be reset on restart."
return 0;
fi
bad_msg "U-Boot environment reset failed!"
return 1;
}
#main()
echo -e "\n${UPDATER_BANNER}\n"
check_utilities || error_exit 4;
check_board || error_exit 3;
find_bootloader_file || error_exit 1;
check_spi_flash || error_exit 2;
check_bootloader_versions || exit 0;
warn_msg "Do not power off or reset your computer!!!"
erase_spi_flash || error_exit 5;
write_bootloader || error_exit 6;
check_bootloader || error_exit 7;
good_msg "Boot loader update succeeded!\n"
reset_environment || exit 0;
good_msg "Done!\n"