-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_ready.sh
executable file
·334 lines (277 loc) · 7.96 KB
/
get_ready.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
#!/bin/bash
##########################################################################
# ODROID C2 Bare Metal
# Copyright (C) 2016 Federico "MrModd" Cosentino
#
# 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 3 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.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
##########################################################################
CURR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd )" # Current directory
#########################################################
# Variables
#########################################################
CC_NAME="gcc-linaro-aarch64-none-elf-4.9-2014.09_linux"
CC_URLS=("https://releases.linaro.org/archive/14.09/components/toolchain/binaries/gcc-linaro-aarch64-none-elf-4.9-2014.09_linux.tar.xz" \
"http://dn.odroid.com/toolchains/gcc-linaro-aarch64-none-elf-4.9-2014.09_linux.tar.xz")
UBOOT_GIT="https://github.com/hardkernel/u-boot.git"
# Point to the most recent commit of the U-Boot repository
UBOOT_COMMIT="odroidc2-v2015.01"
# In case of problems use the following tested commit
#UBOOT_COMMIT="1471870" #Commit date: Tue Nov 8 08:41:53 2016 +0900
BOOT_FILES=("https://github.com/hardkernel/u-boot/raw/odroidc2-v2015.01/sd_fuse/bl1.bin.hardkernel" \
"https://github.com/hardkernel/u-boot/raw/odroidc2-v2015.01/sd_fuse/sd_fusing.sh")
UBOOT_DIR="$CURR/uboot"
UBOOT_BIN="$UBOOT_DIR/sd_fuse/u-boot.bin"
CC_DIR="$CURR"
CROSS_COMPILE="$CC_DIR/$CC_NAME/bin/aarch64-none-elf-"
BOOT_DIR="$CURR/sdcard"
#########################################################
# Get branch that contains a commit:
# git branch --contains <commit>
#
# Get the commit linked to the given tag:
# git rev-parse <tag>~0
# Colors
TXT_COLOR="\e[1;34m" # Normal text
CON_COLOR="\e[1;32m" # Confirm messages
ERR_COLOR="\e[1;31m" # Error messages
RST_COLOR="\e[0m" # Reset terminal color
#########################################################
unset LD_LIBRARY_PATH
# Functions
download_crosscompiler() {
echo -e "${TXT_COLOR}Downloading crosscompiler...${RST_COLOR}"
if [ ! -d "$CC_DIR/$CC_NAME" ] ; then
found="false"
file=""
for url in "${CC_URLS[@]}" ; do
echo -e "${TXT_COLOR}Trying${RST_COLOR} \"$url\""
wget $url -P "/tmp"
if [ $? == 0 ] ; then
echo -e "${TXT_COLOR}File found!${RST_COLOR}"
file=$(echo $url | sed 's/.*\///')
found="true"
break
fi
done
if [ "$found" != "true" ] ; then
echo -e "${ERR_COLOR}Cannot download crosscompiler.${RST_COLOR}" >&2
return 1
fi
tar -xf "/tmp/$file" -C "$CC_DIR"
if [ $? != 0 ] ; then
echo -e "${ERR_COLOR}Cannot extract crosscompiler.${RST_COLOR}" >&2
rm -rf "$CC_DIR/$CC_NAME"
return 1
fi
rm -f "/tmp/$file"
echo -e "${CON_COLOR}Done.${RST_COLOR}"
else
echo -e "${CON_COLOR}Already present.${RST_COLOR}"
fi
return 0
}
clone_uboot() {
echo -e "${TXT_COLOR}Cloning U-boot...${RST_COLOR}"
if [ ! -d "$UBOOT_DIR" ] ; then
git clone $UBOOT_GIT -b master "$UBOOT_DIR"
if [ $? != 0 ] ; then
echo -e "${ERR_COLOR}Cannot clone repository.${RST_COLOR}" >&2
return 1
fi
PWD=$(pwd)
cd "$UBOOT_DIR"
git checkout $UBOOT_COMMIT
if [ $? != 0 ] ; then
echo -e "${ERR_COLOR}Cannot checkout to commit $UBOOT_COMMIT.${RST_COLOR}" >&2
cd "$PWD"
return 1
fi
cd "$PWD"
else
echo -e "${CON_COLOR}Already present.${RST_COLOR}"
echo -e "${TXT_COLOR}Pulling last changes...${RST_COLOR}"
PWD=$(pwd)
cd "$UBOOT_DIR"
git checkout master
git pull
if [ $? != 0 ] ; then
echo -e "${ERR_COLOR}Cannot pull from origin.${RST_COLOR}" >&2
cd "$PWD"
return 1
fi
git checkout $UBOOT_COMMIT
if [ $? != 0 ] ; then
echo -e "${ERR_COLOR}Cannot checkout to commit $UBOOT_COMMIT.${RST_COLOR}" >&2
cd "$PWD"
return 1
fi
cd "$PWD"
fi
echo -e "${CON_COLOR}Done.${RST_COLOR}"
return 0
}
compile_uboot() {
echo -e "${TXT_COLOR}Compiling U-boot...${RST_COLOR}"
export ARCH="arm"
export CROSS_COMPILE="$CROSS_COMPILE"
export PATH="$CC_DIR/$CC_NAME/bin/:$PATH"
make -C "$UBOOT_DIR" odroidc2_config
if [ $? != 0 ] ; then
echo -e "${ERR_COLOR}Error setting U-boot configuration.${RST_COLOR}" >&2
return 1
fi
make -C "$UBOOT_DIR" -j$(grep -c ^processor /proc/cpuinfo)
if [ $? != 0 ] ; then
echo -e "${ERR_COLOR}Error compiling U-boot.${RST_COLOR}" >&2
return 1
fi
echo -e "${CON_COLOR}Done.${RST_COLOR}"
return 0
}
copy_files() {
echo -e "${TXT_COLOR}Copying files for booting...${RST_COLOR}"
mkdir -p "$BOOT_DIR"
echo -e "${TXT_COLOR}U-boot binary...${RST_COLOR}"
if [ -e "$UBOOT_BIN" ] ; then
file=$(echo $UBOOT_BIN | sed 's/.*\///')
if [ -e "$BOOT_DIR/$file" ] ; then
echo -e "${CON_COLOR}Already exists.${RST_COLOR}"
else
cp "$UBOOT_BIN" "$BOOT_DIR"
if [ $? != 0 ] ; then
echo -e "${ERR_COLOR}Error copying U-boot binary.${RST_COLOR}" >&2
return 1
fi
echo -e "${CON_COLOR}Done.${RST_COLOR}"
fi
else
echo -e "${ERR_COLOR}U-boot binary not found, you should compile it first. Continuing anyway...${RST_COLOR}" >&2
fi
for url in "${BOOT_FILES[@]}" ; do
file=$(echo $url | sed 's/.*\///')
echo -e "${TXT_COLOR}$file...${RST_COLOR}"
if [ -e "$BOOT_DIR/$file" ] ; then
echo -e "${CON_COLOR}Already exists.${RST_COLOR}"
else
wget $url -P "$BOOT_DIR"
if [ $? != 0 ] ; then
echo -e "${ERR_COLOR}Error downloading $file. Continuing anyway...${RST_COLOR}" >&2
fi
if [ "${file##*.}" == "sh" ] ; then
chmod +x "$BOOT_DIR/$file"
fi
echo -e "${CON_COLOR}Done.${RST_COLOR}"
fi
done
echo -e "${TXT_COLOR}README...${RST_COLOR}"
cp "$CURR/configs/final_readme" "$BOOT_DIR/README"
if [ $? != 0 ] ; then
echo -e "${ERR_COLOR}Error copying README file.${RST_COLOR}" >&2
fi
echo -e "${CON_COLOR}Done.${RST_COLOR}"
echo -e "${TXT_COLOR}boot.ini...${RST_COLOR}"
cp "$CURR/configs/boot.ini" "$BOOT_DIR/boot.ini"
if [ $? != 0 ] ; then
echo -e "${ERR_COLOR}Error copying boot.ini file.${RST_COLOR}" >&2
fi
echo -e "${CON_COLOR}Done.${RST_COLOR}"
}
clean_all() {
echo -e "${TXT_COLOR}Cleaning all...${RST_COLOR}"
rm -rf "$BOOT_DIR"
rm -rf "$UBOOT_DIR"
rm -rf "$CC_DIR/$CC_NAME"
echo -e "${CON_COLOR}Done.${RST_COLOR}"
}
SCRIPT_NAME=$0
print_help() {
echo -e "Usage $SCRIPT_NAME [OPTIONS]" >&2
echo -e "\nOPTIONS:" >&2
echo -e "\t-h, -?" >&2
echo -e "\t\tshow this help" >&2
echo -e "\t-c" >&2
echo -e "\t\tdownload the crosscompiler" >&2
echo -e "\t-u" >&2
echo -e "\t\tclone and compile U-boot" >&2
echo -e "\t-p" >&2
echo -e "\t\tcopy boot files" >&2
echo -e "\t-z" >&2
echo -e "\t\tdelete all downloaded and compiled files" >&2
}
# Parse command line arguments
ARG_CC=""
ARG_UBOOT=""
ARG_COPY=""
ARG_CLEAN=""
while getopts "h?cupz" opt; do
case "$opt" in
h|\?)
print_help
exit 0
;;
c)
ARG_CC="true"
;;
u)
ARG_UBOOT="true"
;;
p)
ARG_COPY="true"
;;
z)
ARG_CLEAN="true"
;;
esac
done
if [ $ARG_CLEAN ] && ( [ $ARG_CC ] || [ $ARG_UBOOT ] || [ $ARG_COPY ] ) ; then
echo -e "${ERR_COLOR}Clean must be done alone.${RST_COLOR}" >&2
exit 1
fi
if [ ! $ARG_CLEAN ] && [ ! $ARG_CC ] && [ ! $ARG_UBOOT ] && [ ! $ARG_COPY ] ; then
echo -e "${ERR_COLOR}Nothing to do.${RST_COLOR}" >&2
print_help
exit 1
fi
shift $((OPTIND-1))
#[ "$1" = "--" ] && shift
# Do stuff
if [ $ARG_CC ] ; then
download_crosscompiler
if [ $? != 0 ] ; then
exit 1
fi
fi
if [ $ARG_UBOOT ] ; then
clone_uboot
if [ $? != 0 ] ; then
exit 1
fi
compile_uboot
if [ $? != 0 ] ; then
exit 1
fi
fi
if [ $ARG_COPY ] ; then
copy_files
if [ $? != 0 ] ; then
exit 1
fi
fi
if [ $ARG_CLEAN ] ; then
clean_all
if [ $? != 0 ] ; then
exit 1
fi
fi
exit 0