summaryrefslogtreecommitdiff
path: root/atf-build.sh
blob: 351290cc433ab5bd4d531bcb7209c07723f10911 (plain)
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
#!/bin/bash
#
# Builds ARM Trusted Firmware, and generates FIPs with UEFI and optionally
# Trusted OS for the supported platforms.
# Not intended to be called directly, invoked from uefi-build.sh.
#
# Board configuration is extracted from
# parse-platforms.py and platforms.config.
#
# Copyright (c) 2014-2018, Linaro Ltd. All rights reserved.
# Copyright (c) 2017 ARM Ltd. All rights reserved.
#
# SPDX-License-Identifier: ISC
#

. "$TOOLS_DIR"/common-functions
OUTPUT_DIR="$PWD"/uefi-build

ATF_BUILDVER=1

function usage
{
	echo "usage:"
	echo "atf-build.sh -e <EDK2 source directory> -t <UEFI build profile/toolchain> <platform>"
	echo
}

function check_atf_buildver
{
	MAJOR=`grep "^VERSION_MAJOR" Makefile | sed 's/.*:= *\([0-9]*\).*/\1/'`
	[ $? -ne 0 ] && return 1
	MINOR=`grep "^VERSION_MINOR" Makefile | sed 's/.*:= *\([0-9]*\).*/\1/'`
	[ $? -ne 0 ] && return 1

	if [ "$MAJOR" -eq 1 ]; then
		if [ "$MINOR" -ge 2 ]; then
			ATF_BUILDVER=2
		fi
	else
		ATF_BUILDVER=2
	fi
}

function build_platform
{
	if [ X"$EDK2_DIR" = X"" ];then
		echo "EDK2_DIR not set!" >&2
		return 1
	fi

	check_atf_buildver || return 1

	BUILD_ATF="`$TOOLS_DIR/parse-platforms.py $PLATFORM_CONFIG -p $1 get -o build_atf`"
	if [ X"$BUILD_ATF" = X"" ]; then
		echo "Platform '$1' is not configured to build ARM Trusted Firmware."
		return 0
	fi

	ATF_PLATFORM="`$TOOLS_DIR/parse-platforms.py $PLATFORM_CONFIG -p $1 get -o atf_platform`"
	if [ X"$ATF_PLATFORM" = X"" ]; then
		ATF_PLATFORM=$1
	fi

	#
	# Read platform configuration
	#
	PLATFORM_NAME="`$TOOLS_DIR/parse-platforms.py $PLATFORM_CONFIG -p $1 get -o longname`"
	PLATFORM_ARCH="`$TOOLS_DIR/parse-platforms.py $PLATFORM_CONFIG -p $1 get -o arch`"
	PLATFORM_IMAGE_DIR="`$TOOLS_DIR/parse-platforms.py $PLATFORM_CONFIG -p $1 get -o uefi_image_dir`"
	PLATFORM_BUILDFLAGS="`$TOOLS_DIR/parse-platforms.py $PLATFORM_CONFIG -p $1 get -o atf_buildflags`"

	if [ $VERBOSE -eq 1 ]; then
		echo "PLATFORM_NAME=$PLATFORM_NAME"
		echo "PLATFORM_ARCH=$PLATFORM_ARCH"
		echo "PLATFORM_IMAGE_DIR=$PLATFORM_IMAGE_DIR"
		echo "PLATFORM_BUILDFLAGS=$PLATFORM_BUILDFLAGS"
	fi

	unset BL30 BL31 BL32 BL32_EXTRA1 BL32_EXTRA2 BL33
	BL30="`$TOOLS_DIR/parse-platforms.py $PLATFORM_CONFIG -p $1 get -o scp_bin`"
	if [ $ATF_BUILDVER -gt 1 ]; then
		unset SCP_BL2
		SCP_BL2=`search_packages_path "$BL30"`
	fi
	BL31="`$TOOLS_DIR/parse-platforms.py $PLATFORM_CONFIG -p $1 get -o el3_bin`"
	BL33="$WORKSPACE/Build/$PLATFORM_IMAGE_DIR/$BUILD_PROFILE/FV/`$TOOLS_DIR/parse-platforms.py $PLATFORM_CONFIG -p $1 get -o uefi_bin`"

	#
	# Set up cross compilation variables (if applicable)
	#
	set_cross_compile
	CROSS_COMPILE="$TEMP_CROSS_COMPILE"
	echo "Building ARM Trusted Firmware for $PLATFORM_NAME - $BUILD_PROFILE"
	echo "CROSS_COMPILE=\"$TEMP_CROSS_COMPILE\""

	if [ X"$BL30" != X"" ]; then
		BL30=`search_packages_path "$BL30"`
	fi
	if [ X"$BL31" != X"" ]; then
		BL31=`search_packages_path "$BL31"`
	fi

	#
	# BL32 requires more attention
	# If TOS_DIR is not set, we assume user does not want a Trusted OS,
	# even if the source directory and/or binary for it exists
	#
	if [ X"$TOS_DIR" != X"" ]; then
		SPD="`$TOOLS_DIR/parse-platforms.py $PLATFORM_CONFIG -p $1 get -o atf_spd`"

		TOS_BIN="`$TOOLS_DIR/parse-platforms.py $PLATFORM_CONFIG -p $1 get -o tos_bin`"
		TOS_BIN_EXTRA1="`$TOOLS_DIR/parse-platforms.py $PLATFORM_CONFIG -p $1 get -o tos_bin_extra1`"
		TOS_BIN_EXTRA2="`$TOOLS_DIR/parse-platforms.py $PLATFORM_CONFIG -p $1 get -o tos_bin_extra2`"
		if [ X"$TOS_BIN" != X"" ]; then
			BL32=$WORKSPACE/Build/$PLATFORM_IMAGE_DIR/$BUILD_PROFILE/FV/$TOS_BIN
		fi
		if [ X"$TOS_BIN_EXTRA1" != X"" ]; then
			BL32_EXTRA1=$WORKSPACE/Build/$PLATFORM_IMAGE_DIR/$BUILD_PROFILE/FV/$TOS_BIN_EXTRA1
		fi
		if [ X"$TOS_BIN_EXTRA2" != X"" ]; then
			BL32_EXTRA2=$WORKSPACE/Build/$PLATFORM_IMAGE_DIR/$BUILD_PROFILE/FV/$TOS_BIN_EXTRA2
		fi

		if [ X"$SPD" != X"" ] && [ X"$BL32" != X"" ]; then
			#
			# Since SPD cannot be exported or undefined,
			# we parametrise it here
			#
			SPD_OPTION="SPD=$SPD"
		else
			echo "WARNING:	Proceeding without Trusted OS!"
			echo "		Please specify both ATF_SPD and TOS_BIN"
			echo "		if you wish to use a Trusted OS!"
		fi
	else
	#
	# Since TOS_DIR is not set, user does not want a Trusted OS
	# even if the source directory and/or binary for it exists.
	# Next, Check whether user wants secure partition image.
	# If SPM_BIN is set then include pre-built secure partition image as a
	# BL32 Image and implicitly set SPM=1.
	#
		SPM_BIN="`$TOOLS_DIR/parse-platforms.py $PLATFORM_CONFIG -p $1 get -o spm_bin`"

		if [ X"$SPM_BIN" != X"" ]; then
			BL32=$WORKSPACE/Build/StandaloneSmmPkg/$BUILD_PROFILE/FV/$SPM_BIN
			PLATFORM_BUILDFLAGS="$PLATFORM_BUILDFLAGS SPM=1"
		fi
	# We assume that user does not want secure partition either.
	# Todo: Revisit if either one of Trusted OS or Secure Partition Image is Mandatory.
	fi



	#
	# Debug extraction handling
	#
	case "$BUILD_ATF" in
	debug*)
		DEBUG=1
		BUILD_TYPE="debug"
		;;
	*)
		DEBUG=0
		BUILD_TYPE="release"
		;;
	esac

	export BL30 BL31 BL32 BL32_EXTRA1 BL32_EXTRA2 BL33

	echo "BL30=$BL30"
	if [ $ATF_BUILDVER -gt 1 ] && [ X"$BL30" != X"" ]; then
		export SCP_BL2
		echo "SCP_BL2=$BL30"
	fi
	echo "BL31=$BL31"
	echo "BL32=$BL32"
	echo "BL32_EXTRA1=$BL32_EXTRA1"
	echo "BL32_EXTRA2=$BL32_EXTRA2"
	echo "BL33=$BL33"
	echo "$SPD_OPTION"
	echo "BUILD_TYPE=$BUILD_TYPE"

	#
	# If a build was done with BL32, and followed by another without,
	# the BL32 component remains in fip.bin, so we delete the build dir
	# contents before calling make
	#
	rm -rf build/"$ATF_PLATFORM/$BUILD_TYPE"/*

	#
	# Build ARM Trusted Firmware and create FIP
	#
	if [ $VERBOSE -eq 1 ]; then
		echo "Calling ARM Trusted Firmware build:"
		echo "CROSS_COMPILE="$CROSS_COMPILE" make -j$NUM_THREADS PLAT="$ATF_PLATFORM" $SPD_OPTION DEBUG=$DEBUG ${PLATFORM_BUILDFLAGS} all fip"
	fi
	CROSS_COMPILE="$CROSS_COMPILE" make -j$NUM_THREADS PLAT="$ATF_PLATFORM" $SPD_OPTION DEBUG=$DEBUG ${PLATFORM_BUILDFLAGS} all fip
	if [ $? -eq 0 ]; then
		#
		# Copy resulting images to UEFI image dir
		#
		if [ $VERBOSE -eq 1 ]; then
			echo "Copying bl1.bin bl2.bin and fip.bin to "$WORKSPACE/Build/$PLATFORM_IMAGE_DIR/$BUILD_PROFILE/FV/""
			echo "Copying images to '$WORKSPACE/Build/$PLATFORM_IMAGE_DIR/$BUILD_PROFILE/FV/':"
			CPFLAGS="-v"
		else
			CPFLAGS=""
		fi
		for file in build/"$ATF_PLATFORM/$BUILD_TYPE"/{bl1,bl2,fip}.bin; do
			if [ -f "$file" ]; then
				cp -a $CPFLAGS "$file" "$WORKSPACE/Build/$PLATFORM_IMAGE_DIR/$BUILD_PROFILE/FV"
			fi
		done
	else
		return 1
	fi
}

# Check to see if we are in a trusted firmware directory
# refuse to continue if we aren't
if [ ! -d bl32 ]
then
	echo "ERROR: we aren't in the arm-trusted-firmware directory."
	usage
	exit 1
fi

build=

if [ $# = 0 ]
then
	usage
	exit 1
else
	while [ "$1" != "" ]; do
		case $1 in
			"-e" )
				shift
				EDK2_DIR="$1"
				;;
			"/h" | "/?" | "-?" | "-h" | "--help" )
				usage
				exit
				;;
			"-t" )
				shift
				BUILD_PROFILE="$1"
				;;
			* )
				build="$1"
				;;
		esac
		shift
	done
fi

if [ X"$build" = X"" ]; then
	echo "No platform specified!" >&2
	echo
	usage
	exit 1
fi

build_platform $build
exit $?