1#!/bin/bash
2#
3# Builds OP-TEE Trusted OS.
4# Not intended to be called directly, invoked from tos-build.sh.
5#
6# Board configuration is extracted from
7# parse-platforms.py and platforms.config.
8#
9
10TOOLS_DIR="`dirname $0`"
11. "$TOOLS_DIR"/common-functions
12
13export CFG_TEE_CORE_LOG_LEVEL=2  # 0=none 1=err 2=info 3=debug 4=flow
14
15function usage
16{
17	echo "usage:"
18	echo "opteed-build.sh -e <EDK2 source directory> -t <UEFI build profile/toolchain> <platform>"
19	echo
20}
21
22function build_platform
23{
24	unset CFG_ARM64_core PLATFORM PLATFORM_FLAVOR DEBUG
25	TOS_PLATFORM="`$TOOLS_DIR/parse-platforms.py $PLATFORM_CONFIG -p $1 get -o tos_platform`"
26	if [ X"$TOS_PLATFORM" = X"" ]; then
27		TOS_PLATFORM="`$TOOLS_DIR/parse-platforms.py $PLATFORM_CONFIG -p $1 get -o atf_platform`"
28		if [ X"$TOS_PLATFORM" = X"" ]; then
29			TOS_PLATFORM=$1
30		fi
31	fi
32	TOS_PLATFORM_FLAVOR="`$TOOLS_DIR/parse-platforms.py $PLATFORM_CONFIG -p $1 get -o tos_platform_flavor`"
33
34	#
35	# Read platform configuration
36	#
37	PLATFORM_ARCH="`$TOOLS_DIR/parse-platforms.py $PLATFORM_CONFIG -p $1 get -o arch`"
38	PLATFORM_IMAGE_DIR="`$TOOLS_DIR/parse-platforms.py $PLATFORM_CONFIG -p $1 get -o uefi_image_dir`"
39	PLATFORM_BUILDFLAGS="`$TOOLS_DIR/parse-platforms.py $PLATFORM_CONFIG -p $1 get -o tos_buildflags`"
40
41	if [ $VERBOSE -eq 1 ]; then
42		echo "PLATFORM_ARCH=$PLATFORM_ARCH"
43		echo "PLATFORM_IMAGE_DIR=$PLATFORM_IMAGE_DIR"
44		echo "PLATFORM_BUILDFLAGS=$PLATFORM_BUILDFLAGS"
45	fi
46
47	#
48	# Set up cross compilation variables (if applicable)
49	#
50	# OP-TEE requires both 64- and 32-bit compilers for a 64-bit build
51	# For details, visit
52	# https://github.com/OP-TEE/optee_os/blob/master/documentation/build_system.md#cross_compile-cross-compiler-selection
53	#
54	set_cross_compile
55	if [ "$PLATFORM_ARCH" = "AARCH64" ]; then
56		export CFG_ARM64_core=y
57		export CROSS_COMPILE_core="$TEMP_CROSS_COMPILE"
58		export CROSS_COMPILE_ta_arm64="$TEMP_CROSS_COMPILE"
59		PLATFORM_ARCH="ARM"
60		set_cross_compile
61		PLATFORM_ARCH="AARCH64"
62		echo "CFG_ARM64_core=$CFG_ARM64_core"
63		echo "CROSS_COMPILE_ta_arm64=$CROSS_COMPILE_ta_arm64"
64	else
65		export CFG_ARM64_core=n
66	fi
67	export CROSS_COMPILE="$TEMP_CROSS_COMPILE"
68	echo "CROSS_COMPILE=$CROSS_COMPILE"
69	echo "CROSS_COMPILE_core=$CROSS_COMPILE_core"
70
71	#
72	# Set up build variables
73	#
74	BUILD_TOS="`$TOOLS_DIR/parse-platforms.py $PLATFORM_CONFIG -p $1 get -o build_tos`"
75	case "$BUILD_TOS" in
76	debug*)
77		export DEBUG=1
78		echo "PROFILE=DEBUG"
79		;;
80	*)
81		export DEBUG=0
82		echo "PROFILE=RELEASE"
83		;;
84	esac
85
86	export PLATFORM=$TOS_PLATFORM
87	export PLATFORM_FLAVOR=$TOS_PLATFORM_FLAVOR
88	echo "PLATFORM=$PLATFORM"
89	echo "PLATFORM_FLAVOR=$PLATFORM_FLAVOR"
90	echo "CFG_TEE_CORE_LOG_LEVEL=$CFG_TEE_CORE_LOG_LEVEL"
91
92	#
93	# Build OP-TEE
94	#
95	if [ $VERBOSE -eq 1 ]; then
96		echo "Calling OP-TEE build:"
97	fi
98	make -j$NUM_THREADS ${PLATFORM_BUILDFLAGS}
99	if [ $? -eq 0 ]; then
100		#
101		# Copy resulting images to UEFI image dir
102		#
103		if [ $VERBOSE -eq 1 ]; then
104			echo "Copying tee.bin to "$EDK2_DIR/Build/$PLATFORM_IMAGE_DIR/$BUILD_PROFILE/FV/""
105		fi
106		cp -a out/arm-plat-"$TOS_PLATFORM"/core/tee.bin "$EDK2_DIR/Build/$PLATFORM_IMAGE_DIR/$BUILD_PROFILE/FV/"
107	else
108		return 1
109	fi
110}
111
112# Check to see if we are in a trusted OS directory
113# refuse to continue if we aren't
114if [ ! -f documentation/optee_design.md ]
115then
116	echo "ERROR: we aren't in the optee_os directory."
117	usage
118	exit 1
119fi
120
121build=
122
123if [ $# = 0 ]
124then
125	usage
126	exit 1
127else
128	while [ "$1" != "" ]; do
129		case $1 in
130			"-e" )
131				shift
132				EDK2_DIR="$1"
133				;;
134			"/h" | "/?" | "-?" | "-h" | "--help" )
135				usage
136				exit
137				;;
138			"-t" )
139				shift
140				BUILD_PROFILE="$1"
141				;;
142			* )
143				build="$1"
144				;;
145		esac
146		shift
147	done
148fi
149
150if [ X"$build" = X"" ]; then
151	echo "No platform specified!" >&2
152	echo
153	usage
154	exit 1
155fi
156
157build_platform $build
158exit $?
159