forked from AmpereComputing/edk2-ampere-tools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
common-functions
415 lines (383 loc) · 12.7 KB
/
common-functions
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
#!/bin/bash
#
# Copyright (c) 2014-2018, Linaro Ltd. All rights reserved.
# Copyright (c) 2020, Ampere Computing LLC.
#
# SPDX-License-Identifier: ISC
#
RESULT_BUF=`echo -e ------------------------------------------------------------`
RESULT_PASS_COUNT=0
RESULT_FAIL_COUNT=0
function result_log
{
if [ $1 -eq 0 ]; then
RESULT_BUF="`printf \"%s\n%55s\tpass\" \"$RESULT_BUF\" \"$2\"`"
RESULT_PASS_COUNT=$(($RESULT_PASS_COUNT + 1))
else
RESULT_BUF="`printf \"%s\n%55s\tfail\" \"$RESULT_BUF\" \"$2\"`"
RESULT_FAIL_COUNT=$(($RESULT_FAIL_COUNT + 1))
fi
}
function result_print
{
printf "%s" "$RESULT_BUF"
echo -e "\n------------------------------------------------------------"
printf "pass\t$RESULT_PASS_COUNT\n"
printf "fail\t$RESULT_FAIL_COUNT\n"
exit $RESULT_FAIL_COUNT
}
function get_build_arch
{
case `uname -m` in
arm*)
BUILD_ARCH=ARM;;
aarch64*)
BUILD_ARCH=AARCH64;;
i?86*)
BUILD_ARCH=IA32;;
x86_64*)
BUILD_ARCH=X64;;
*)
BUILD_ARCH=other;;
esac
}
function set_cross_compile
{
get_build_arch
echo "Target: $PLATFORM_ARCH"
echo "Build: $BUILD_ARCH"
if [ "$PLATFORM_ARCH" = "$BUILD_ARCH" ]; then
TEMP_CROSS_COMPILE=
else
case "$PLATFORM_ARCH" in
AARCH64)
TEMP_CROSS_COMPILE=${CROSS_COMPILE} ;;
ARM)
TEMP_CROSS_COMPILE=arm-linux-gnueabihf- ;;
IA32)
TEMP_CROSS_COMPILE=x86_64-linux-gnu-
for family in 6 5 4 3; do
if i$family86-linux-gnu-as -version >/dev/null 2>&1;then
TEMP_CROSS_COMPILE=i$family86-linux-gnu-
break
fi
done
;;
X64)
TEMP_CROSS_COMPILE=x86_64-linux-gnu- ;;
*)
echo "Unsupported target architecture '$PLATFORM_ARCH'!" >&2
;;
esac
fi
}
function get_gcc_version
{
$1 -v >/dev/null 2>&1 || return 1
gcc_version=$($1 -dumpversion)
MAJVER=`echo $gcc_version | cut -d. -f1`
case $gcc_version in
4*)
echo GCC$(echo ${gcc_version} | awk -F. '{print $1$2}')
;;
*)
if [ "$MAJVER" -ge 5 ]; then
# We only have a GCC5 build profile for now, so...
# echo GCC$MAJVER
echo GCC5
else
echo "Unknown toolchain version '$gcc_version'" >&2
echo "Attempting to build using GCC49 profile." >&2
echo GCC49
fi
;;
esac
return 0
}
function get_clang_version
{
clang_version=`$1 --version | head -1 | sed 's/^.*version\s*\([0-9]*\).\([0-9]*\).*/\1\2/g'`
case $clang_version in
38|35)
;;
*)
echo "Unknown toolchain version '$clang_version'" >&2
if [ $clang_version -gt 38 ]; then
clang_version=38
else
clang_version=35
fi
echo "Attempting to build using CLANG$clang_version profile." >&2
;;
esac
echo "CLANG$clang_version"
}
function download_patch_openssl
{
OPENSSL_VER=`ls EDKII_openssl-*.patch | sed 's/^.*-\([0-9.a-z]*\).patch/\1/'`
OPENSSL_TAR=openssl-${OPENSSL_VER}.tar.gz
OPENSSL_URL=http://www.openssl.org/source/${OPENSSL_TAR}
OPENSSL_DIR=openssl-${OPENSSL_VER}
OPENSSL_PATCH=EDKII_openssl-${OPENSSL_VER}.patch
if [ -e "$WORKSPACE"/CryptoPkg/Include/openssl/opensslconf.h ]; then
echo "OpenSSL already imported!"
return 0
fi
# Use cached copy if available
if [ -f "$WORKSPACE"/LinaroPkg/"$OPENSSL_TAR" ]; then
tar xzf "$WORKSPACE"/LinaroPkg/"$OPENSSL_TAR"
else
wget -O - -q ${OPENSSL_URL} | tar xzf -
fi
echo "Importing OpenSSL $OPENSSL_VER"
( cd ${OPENSSL_DIR}; patch -p1 -i ../${OPENSSL_PATCH} )
./Install.sh
if [ $? -eq 0 ]; then
OPENSSL_CONFIGURED=TRUE
else
echo "OpenSSL $OPENSSL_VER import failed!" >&2
rm -rf $OPENSSL_TAR $OPENSSL_DIR
return 1
fi
}
function clone_process_openssl
{
if [ -e openssl/include/openssl/opensslconf.h -o \
-e openssl/include/openssl/opensslconf.h.in ]; then
echo "OpenSSL already imported!"
return 0
fi
OPENSSL_VER=`git ls-remote --tags git://github.com/openssl/openssl.git | awk '{print $2;}' | sed 's-^refs/tags/--g' | grep -v '\^{}$' | grep '^OpenSSL' | grep -v '^OpenSSL_FIPS' | grep -v "\-pre" | tail -1 | sed -n 's/^OpenSSL_\([0-9]*\)_\([0-9]*\)_\([0-9.a-z]*\)$/openssl-\1.\2.\3\n/p'`
OPENSSL_TAR="$OPENSSL_VER.tar.gz"
if [ -z "$OPENSSL_TAR" ]; then
return 1
fi
OPENSSL_URL=http://www.openssl.org/source/${OPENSSL_TAR}
if [ ! -f "$OPENSSL_TAR" ]; then
wget -q ${OPENSSL_URL}
else
rm -rf openssl
fi
tar xzf "$OPENSSL_TAR"
if [ $? -ne 0 -o ! -d "$OPENSSL_VER" ]; then
echo "OpenSSL import failed!"
return 1
fi
if [ ! -d openssl ]; then
mkdir openssl
fi
mv "$OPENSSL_VER"/* openssl
rm -r "$OPENSSL_VER"/
# perl process_files.pl # not needed and not currently working
return $?
}
function import_openssl
{
# Don't re-import if already done
if [ "$OPENSSL_CONFIGURED" = "TRUE" ]; then
echo "Using existing OpenSSL $OPENSSL_VER"
return 0
fi
cd CryptoPkg/Library/OpensslLib/
if [ -f EDKII_openssl-*.patch ]; then
download_patch_openssl
else
clone_process_openssl
fi
RET=$?
cd - >/dev/null
return $RET
}
function search_packages_path
{
file="$1"
IFS=:
for dir in $PACKAGES_PATH; do
if [ -e "$dir/$file" ]; then
echo "$dir/$file"
unset IFS
return 0
fi
done
echo "$file not found in any directory on PACKAGES_PATH!" >&2
unset IFS
return 1
}
function get_atf_tools
{
ATF_TOOLS="$1"
if [ -e "${ATF_TOOLS}/tools/cert_create/cert_create" ]; then
return 0
fi
ATF_TOOL_TAG="v2.6"
rm -fr ${ATF_TOOLS}
mkdir -p ${ATF_TOOLS}
cd ${ATF_TOOLS}
git init
git remote add origin -f https://github.com/ARM-software/arm-trusted-firmware.git
git config core.sparseCheckout true
echo -ne "include/tools_share\nmake_helpers\ntools/cert_create\ntools/fiptool" > .git/info/sparse-checkout
git -C . checkout tags/${ATF_TOOL_TAG} -b ${ATF_TOOL_TAG}
make -C tools/cert_create -j $NUM_THREADS CRTTOOL=cert_create
make -C tools/fiptool -j $NUM_THREADS FIPTOOL=fiptool
return 0
}
function get_efitools
{
EFI_TOOLS="$1"
EFITOOLS_VER="$2"
rm -fr ${EFI_TOOLS}
mkdir -p ${EFI_TOOLS}
cd ${EFI_TOOLS}
git init
git remote add origin -f https://github.com/vathpela/efitools.git
git config core.sparseCheckout true
git -C . checkout v${EFITOOLS_VER}
make cert-to-efi-sig-list sign-efi-sig-list
return 0
}
function check_golang
{
GOLANG_VER=$1
GOLANG_ARCH=amd64
GOLANG_INSTALL_DIR=${TOOLS_DIR}/toolchain
echo "Checking golang ver ${GOLANG_VER}... build arch: ${BUILD_ARCH}"
if [ -e "${GOLANG_INSTALL_DIR}/go/bin/go" ]; then
return 0
fi
if [ "$BUILD_ARCH" = "AARCH64" ]; then
GOLANG_ARCH=arm64
fi
GOLANG_TAR=go${GOLANG_VER}.linux-${GOLANG_ARCH}.tar.gz
GOLANG_URL=https://golang.org/dl/${GOLANG_TAR}
mkdir -p ${GOLANG_INSTALL_DIR}
echo "Downloading ${GOLANG_URL}..."
wget -O - -q ${GOLANG_URL} | tar xzf - -C ${GOLANG_INSTALL_DIR}
}
function check_ampere_toolchain
{
TOOLS_DIR="$1"
AMP_GCC="$2"
if which ${AMP_GCC} >/dev/null 2>&1; then
AMP_VER="`${AMP_GCC} --version | grep Ampere | grep dynamic-nosysroot`"
if [ X"$AMP_VER" != X"" ]; then
echo "---> $AMP_VER"
return 0
fi
fi
TOOLCHAIN_TAR=ampere-8.3.0-20191025-dynamic-nosysroot-crosstools.tar.xz
TOOLCHAIN_URL=https://cdn.amperecomputing.com/tools/compilers/cross/8.3.0/${TOOLCHAIN_TAR}
get_build_arch
if [ "$PLATFORM_ARCH" = "$BUILD_ARCH" ]; then
TOOLCHAIN_TAR=ampere-8.3.0-20191025-dynamic-nosysroot-nativetools.tar.xz
TOOLCHAIN_URL=https://cdn.amperecomputing.com/tools/compilers/native/8.3.0/${TOOLCHAIN_TAR}
fi
echo "Downloading ${TOOLCHAIN_URL}..."
mkdir -p ${TOOLS_DIR}/toolchain/ampere
wget -O - -q ${TOOLCHAIN_URL} | tar --strip-components=1 -xJf - -C ${TOOLS_DIR}/toolchain/ampere
if ${AMP_GCC} -v >/dev/null 2>&1; then
return 0
fi
echo "ERROR: can't locate the Ampere Toolchain" >&2
echo " please download and install to edk2-ampere-tools/toolchain/ampere" >&2
echo " $ wget ${TOOLCHAIN_URL}" >&2
echo " $ mkdir -p edk2-ampere-tools/toolchain/ampere" >&2
echo " $ tar --strip-components=1 -xf ${TOOLCHAIN_TAR} -C edk2-ampere-tools/toolchain/ampere" >&2
return 1
}
function check_atf_tool
{
TOOLS_DIR="$1"
if which cert_create >/dev/null 2>&1; then
echo "---> cert_create, fiptool"
return 0
fi
get_atf_tools ${TOOLS_DIR}/AtfTools
mkdir -p ${TOOLS_DIR}/toolchain/atf-tools
cp -a ${ATF_TOOLS}/tools/cert_create/cert_create ${TOOLS_DIR}/toolchain/atf-tools/
cp -a ${ATF_TOOLS}/tools/fiptool/fiptool ${TOOLS_DIR}/toolchain/atf-tools/
if which cert_create >/dev/null 2>&1; then
rm -fr ${TOOLS_DIR}/AtfTools
return 0
fi
echo "ERROR: can't locate the cert_create, fiptool tools" >&2
echo " please download and install to edk2-ampere-tools/toolchain/atf-tools" >&2
echo " $ mkdir -p AtfTools" >&2
echo " $ cd AtfTools" >&2
echo " $ git init && git remote add origin -f https://github.com/ARM-software/arm-trusted-firmware.git" >&2
echo " $ echo -e \"include/tools_share\nmake_helpers\ntools/cert_create\ntools/fiptool\" > .git/info/sparse-checkout" >&2
echo " $ git -C . checkout --track origin/master" >&2
echo " $ make -C tools/cert_create CRTTOOL=cert_create" >&2
echo " $ make -C tools/fiptool FIPTOOL=fiptool" >&2
echo " $ cp -a tools/cert_create/cert_create edk2-ampere-tools/toolchain/atf-tools/" >&2
echo " $ cp -a tools/fiptool/fiptool edk2-ampere-tools/toolchain/atf-tools/" >&2
return 1
}
function check_iasl_tool
{
TOOLS_DIR="$1"
IASL_VER="$2"
if which iasl >/dev/null 2>&1; then
CURRENT_IASL_VER="`iasl -v | grep version | cut -d ' ' -f 5`"
if [ X"${CURRENT_IASL_VER}" = X"${IASL_VER}" ]; then
echo "---> iasl version ${CURRENT_IASL_VER}"
return 0
fi
fi
ACPICA_TAG="R${IASL_VER:4:2}_${IASL_VER:6:2}_${IASL_VER:2:2}"
IASL_NAME=acpica-${ACPICA_TAG}
IASL_URL=https://github.com/acpica/acpica/archive/refs/tags/${ACPICA_TAG}.tar.gz
echo "Downloading ${IASL_URL}..."
rm -fr ${TOOLS_DIR}/${IASL_NAME}
wget -O - -q ${IASL_URL} | tar xzf - -C ${TOOLS_DIR}
if [ $? -eq 0 ]; then
echo "Compiling ${IASL_NAME}..."
make -C ${TOOLS_DIR}/${IASL_NAME} -j $NUM_THREADS HOST=_CYGWIN
if [ $? -eq 0 ]; then
mkdir -p ${TOOLS_DIR}/toolchain/iasl
echo "Deploy to ${TOOLS_DIR}/iasl..."
cp ${TOOLS_DIR}/${IASL_NAME}/generate/unix/bin/iasl ${TOOLS_DIR}/toolchain/iasl/
if iasl -v >/dev/null 2>&1; then
rm -fr ${TOOLS_DIR}/${IASL_NAME}
return 0
fi
fi
fi
echo "ERROR: can't locate the iasl tool" >&2
echo " please download and install to edk2-ampere-tools/toolchain/iasl" >&2
echo " $ https://github.com/acpica/acpica/tags" >&2
echo " $ tar xzf ${IASL_VER}.tar.gz" >&2
echo " $ make -C ${IASL_VER} -j8 HOST=_CYGWIN" >&2
echo " $ cp generate/unix/bin/iasl edk2-ampere-tools/toolchain/iasl/" >&2
return 1
}
function check_lzma_tool
{
TOOLS_DIR="$1"
if which lzma >/dev/null 2>&1; then
return 0
fi
echo "ERROR: can't locate the lzma tool" >&2
echo " please install lzma tool" >&2
return 1
}
function check_efitools
{
TOOLS_DIR="$1"
EFITOOLS_VER="$2"
if which sign-efi-sig-list >/dev/null 2>&1 && which cert-to-efi-sig-list >/dev/null 2>&1 && sign-efi-sig-list --version 2>/dev/null | grep ${EFITOOLS_VER}; then
echo "---> sign-efi-sig-list, cert-to-efi-sig-list"
return 0
fi
get_efitools ${TOOLS_DIR}/efitools ${EFITOOLS_VER}
mkdir -p ${TOOLS_DIR}/toolchain/efitools
cp -a ${TOOLS_DIR}/efitools/sign-efi-sig-list ${TOOLS_DIR}/toolchain/efitools
cp -a ${TOOLS_DIR}/efitools/cert-to-efi-sig-list ${TOOLS_DIR}/toolchain/efitools
if sign-efi-sig-list --version >/dev/null 2>&1 && cert-to-efi-sig-list --version >/dev/null 2>&1; then
rm -fr ${TOOLS_DIR}/efitools
return 0
fi
echo "ERROR: can't locate the efitools" >&2
echo " please install efitools" >&2
return 1
}