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
10. "$TOOLS_DIR"/common-functions
11
12export CFG_TEE_CORE_LOG_LEVEL=2  # 0=none 1=err 2=info 3=debug 4=flow
13
14function usage
15{
16	echo "usage:"
17	echo "opteed-build.sh -e <EDK2 source directory> -t <UEFI build profile/toolchain> <platform>"
18	echo
19}
20
21function build_platform
22{
23	unset CFG_ARM64_core PLATFORM PLATFORM_FLAVOR DEBUG
24	TOS_PLATFORM="`$TOOLS_DIR/parse-platforms.py $PLATFORM_CONFIG -p $1 get -o tos_platform`"
25	if [ X"$TOS_PLATFORM" = X"" ]; then
26		TOS_PLATFORM="`$TOOLS_DIR/parse-platforms.py $PLATFORM_CONFIG -p $1 get -o atf_platform`"
27		if [ X"$TOS_PLATFORM" = X"" ]; then
28			TOS_PLATFORM=$1
29		fi
30	fi
31	TOS_PLATFORM_FLAVOR="`$TOOLS_DIR/parse-platforms.py $PLATFORM_CONFIG -p $1 get -o tos_platform_flavor`"
32
33	#
34	# Read platform configuration
35	#
36	PLATFORM_ARCH="`$TOOLS_DIR/parse-platforms.py $PLATFORM_CONFIG -p $1 get -o arch`"
37	PLATFORM_IMAGE_DIR="`$TOOLS_DIR/parse-platforms.py $PLATFORM_CONFIG -p $1 get -o uefi_image_dir`"
38	PLATFORM_BUILDFLAGS="`$TOOLS_DIR/parse-platforms.py $PLATFORM_CONFIG -p $1 get -o tos_buildflags`"
39
40	if [ $VERBOSE -eq 1 ]; then
41		echo "PLATFORM_ARCH=$PLATFORM_ARCH"
42		echo "PLATFORM_IMAGE_DIR=$PLATFORM_IMAGE_DIR"
43		echo "PLATFORM_BUILDFLAGS=$PLATFORM_BUILDFLAGS"
44	fi
45
46	#
47	# Set up cross compilation variables (if applicable)
48	#
49	# OP-TEE requires both 64- and 32-bit compilers for a 64-bit build
50	# For details, visit
51	# https://github.com/OP-TEE/optee_os/blob/master/documentation/build_system.md#cross_compile-cross-compiler-selection
52	#
53	set_cross_compile
54	if [ "$PLATFORM_ARCH" = "AARCH64" ]; then
55		export CFG_ARM64_core=y
56		export CROSS_COMPILE_core="$TEMP_CROSS_COMPILE"
57		export CROSS_COMPILE_ta_arm64="$TEMP_CROSS_COMPILE"
58		PLATFORM_ARCH="ARM"
59		set_cross_compile
60		PLATFORM_ARCH="AARCH64"
61		echo "CFG_ARM64_core=$CFG_ARM64_core"
62		echo "CROSS_COMPILE_ta_arm64=$CROSS_COMPILE_ta_arm64"
63	else
64		export CFG_ARM64_core=n
65	fi
66	export CROSS_COMPILE="$TEMP_CROSS_COMPILE"
67	echo "CROSS_COMPILE=$CROSS_COMPILE"
68	echo "CROSS_COMPILE_core=$CROSS_COMPILE_core"
69
70	#
71	# Set up build variables
72	#
73	BUILD_TOS="`$TOOLS_DIR/parse-platforms.py $PLATFORM_CONFIG -p $1 get -o build_tos`"
74	case "$BUILD_TOS" in
75	debug*)
76		export DEBUG=1
77		echo "PROFILE=DEBUG"
78		;;
79	*)
80		export DEBUG=0
81		echo "PROFILE=RELEASE"
82		;;
83	esac
84
85	export PLATFORM=$TOS_PLATFORM
86	export PLATFORM_FLAVOR=$TOS_PLATFORM_FLAVOR
87	echo "PLATFORM=$PLATFORM"
88	echo "PLATFORM_FLAVOR=$PLATFORM_FLAVOR"
89	echo "CFG_TEE_CORE_LOG_LEVEL=$CFG_TEE_CORE_LOG_LEVEL"
90
91	#
92	# Build OP-TEE
93	#
94	if [ $VERBOSE -eq 1 ]; then
95		echo "Calling OP-TEE build:"
96	fi
97	make -j$NUM_THREADS ${PLATFORM_BUILDFLAGS}
98	if [ $? -eq 0 ]; then
99		#
100		# Copy resulting images to UEFI image dir
101		#
102		TOS_BIN="`$TOOLS_DIR/parse-platforms.py $PLATFORM_CONFIG -p $1 get -o tos_bin`"
103		if [ $VERBOSE -eq 1 ]; then
104			echo "Copying '$TOS_BIN' to '$EDK2_DIR/Build/$PLATFORM_IMAGE_DIR/$BUILD_PROFILE/FV/'"
105		fi
106		cp -a out/arm-plat-"$TOS_PLATFORM"/core/"$TOS_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