xref: /rk3399_rockchip-uboot/make.sh (revision daa3bef505a406fa121901b85faddda2a2020cd8)
1#!/bin/bash
2#
3# Copyright (c) 2019 Fuzhou Rockchip Electronics Co., Ltd
4#
5# SPDX-License-Identifier: GPL-2.0
6#
7
8set -e
9BOARD=$1
10SUBCMD=$1
11FUNCADDR=$1
12JOB=`sed -n "N;/processor/p" /proc/cpuinfo|wc -l`
13SUPPORT_LIST=`ls configs/*[r,p][x,v,k][0-9][0-9]*_defconfig`
14
15# @target board: defined in arch/arm/mach-rockchip/<soc>/Kconfig
16# @label: show build message
17# @loader: search for ini file to pack loader
18# @trust: search for ini file to pack trust
19#
20# "NA" means use default name reading from .config
21#
22# Format:           target board               label         loader      trust
23RKCHIP_INI_DESC=("CONFIG_TARGET_GVA_RK3229       NA          RK322XAT     NA"
24                 "CONFIG_COPROCESSOR_RK1808  RKNPU-LION      RKNPULION    RKNPULION"
25# to be add...
26                )
27
28########################################### User can modify #############################################
29# User's rkbin tool relative path
30RKBIN_TOOLS=../rkbin/tools
31
32# User's GCC toolchain and relative path
33ADDR2LINE_ARM32=arm-linux-gnueabihf-addr2line
34ADDR2LINE_ARM64=aarch64-linux-gnu-addr2line
35OBJ_ARM32=arm-linux-gnueabihf-objdump
36OBJ_ARM64=aarch64-linux-gnu-objdump
37GCC_ARM32=arm-linux-gnueabihf-
38GCC_ARM64=aarch64-linux-gnu-
39TOOLCHAIN_ARM32=../prebuilts/gcc/linux-x86/arm/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabihf/bin
40TOOLCHAIN_ARM64=../prebuilts/gcc/linux-x86/aarch64/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin
41
42########################################### User not touch #############################################
43BIN_PATH_FIXUP="--replace tools/rk_tools/ ./"
44RKTOOLS=./tools
45
46# Declare global INI file searching index name for every chip, update in select_chip_info()
47RKCHIP=
48RKCHIP_LABEL=
49RKCHIP_LOADER=
50RKCHIP_TRUST=
51
52# Declare rkbin repository path, updated in prepare()
53RKBIN=
54
55# Declare global toolchain path for CROSS_COMPILE, updated in select_toolchain()
56TOOLCHAIN_GCC=
57TOOLCHAIN_OBJDUMP=
58TOOLCHAIN_ADDR2LINE=
59
60# Declare global default output dir and cmd, update in prepare()
61OUTDIR=$2
62OUTOPT=
63
64# Declare global plaform configure, updated in fixup_platform_configure()
65PLATFORM_RSA=
66PLATFORM_SHA=
67PLATFORM_UBOOT_IMG_SIZE=
68PLATFORM_TRUST_IMG_SIZE=
69
70# Out env param
71PACK_IGNORE_BL32=$TRUST_PACK_IGNORE_BL32	# Value only: "--ignore-bl32"
72#########################################################################################################
73help()
74{
75	echo
76	echo "Usage:"
77	echo "	./make.sh [board|subcmd] [O=<dir>]"
78	echo
79	echo "	 - board: board name of defconfig"
80	echo "	 - subcmd: loader|loader-all|trust|trust-all|uboot|elf|map|sym|<addr>|"
81	echo "	 - O=<dir>: assigned output directory"
82	echo
83	echo "Example:"
84	echo
85	echo "1. Build board:"
86	echo "	./make.sh evb-rk3399               --- build for evb-rk3399_defconfig"
87	echo "	./make.sh evb-rk3399 O=rockdev     --- build for evb-rk3399_defconfig with output dir "./rockdev""
88	echo "	./make.sh firefly-rk3288           --- build for firefly-rk3288_defconfig"
89	echo "	./make.sh                          --- build with exist .config"
90	echo
91	echo "	After build, Images of uboot, loader and trust are all generated."
92	echo
93	echo "2. Pack helper:"
94	echo "	./make.sh uboot                    --- pack uboot.img"
95	echo "	./make.sh trust                    --- pack trust.img"
96	echo "	./make.sh trust-all                --- pack trust img (all supported)"
97	echo "	./make.sh loader                   --- pack loader bin"
98	echo "	./make.sh loader-all	           --- pack loader bin (all supported)"
99	echo
100	echo "3. Debug helper:"
101	echo "	./make.sh elf                      --- dump elf file with -D(default)"
102	echo "	./make.sh elf-S                    --- dump elf file with -S"
103	echo "	./make.sh elf-d                    --- dump elf file with -d"
104	echo "	./make.sh <no reloc_addr>          --- dump function symbol and code position of address(no relocated)"
105	echo "	./make.sh <reloc_addr-reloc_off>   --- dump function symbol and code position of address(relocated)"
106	echo "	./make.sh map                      --- cat u-boot.map"
107	echo "	./make.sh sym                      --- cat u-boot.sym"
108}
109
110prepare()
111{
112	local absolute_path cmd dir count
113
114	# Parse output directory 'O=<dir>'
115	cmd=${OUTDIR%=*}
116	if [ "${cmd}" = 'O' ]; then
117		OUTDIR=${OUTDIR#*=}
118		OUTOPT=O=${OUTDIR}
119	else
120		case $BOARD in
121			# Parse from exit .config
122			''|elf*|loader*|spl*|itb|debug*|trust|uboot|map|sym)
123			count=`find -name .config | wc -l`
124			dir=`find -name .config`
125			# Good, find only one .config
126			if [ $count -eq 1 ]; then
127				dir=${dir%/*}
128				OUTDIR=${dir#*/}
129				# Set OUTOPT if not current directory
130				if [ $OUTDIR != '.' ]; then
131					OUTOPT=O=${OUTDIR}
132				fi
133			elif [ $count -eq 0 ]; then
134				echo
135				echo "Build failed, Can't find .config"
136				help
137				exit 1
138			else
139				echo
140				echo "Build failed, find $count '.config': "
141				echo "$dir"
142				echo "Please leave only one of them"
143				exit 1
144			fi
145			;;
146
147			*)
148			OUTDIR=.
149			;;
150		esac
151	fi
152
153	# Parse help and make defconfig
154	case $BOARD in
155		#Help
156		--help|-help|help|--h|-h)
157		help
158		exit 0
159		;;
160
161		#Subcmd
162		''|elf*|loader*|spl*|itb|debug*|trust*|uboot|map|sym)
163		;;
164
165		*)
166		#Func address is valid ?
167		if [ -z $(echo ${FUNCADDR} | sed 's/[0-9,a-f,A-F,x,X,-]//g') ]; then
168			return
169		elif [ ! -f configs/${BOARD}_defconfig ]; then
170			echo
171			echo "Can't find: configs/${BOARD}_defconfig"
172			echo
173			echo "******** Rockchip Support List *************"
174			echo "${SUPPORT_LIST}"
175			echo "********************************************"
176			echo
177			exit 1
178		else
179			echo "make for ${BOARD}_defconfig by -j${JOB}"
180			make ${BOARD}_defconfig ${OUTOPT}
181		fi
182		;;
183	esac
184
185	# Initialize RKBIN
186	if [ -d ${RKBIN_TOOLS} ]; then
187		absolute_path=$(cd `dirname ${RKBIN_TOOLS}`; pwd)
188		RKBIN=${absolute_path}
189	else
190		echo
191		echo "Can't find '../rkbin/' repository, please download it before pack image!"
192		echo "How to obtain? 3 ways:"
193		echo "	1. Login your Rockchip gerrit account: \"Projects\" -> \"List\" -> search \"rk/rkbin\" repository"
194		echo "	2. Github repository: https://github.com/rockchip-linux/rkbin"
195		echo "	3. Download full release SDK repository"
196		exit 1
197	fi
198}
199
200select_toolchain()
201{
202	local absolute_path
203
204	if grep  -q '^CONFIG_ARM64=y' ${OUTDIR}/.config ; then
205		if [ -d ${TOOLCHAIN_ARM64} ]; then
206			absolute_path=$(cd `dirname ${TOOLCHAIN_ARM64}`; pwd)
207			TOOLCHAIN_GCC=${absolute_path}/bin/${GCC_ARM64}
208			TOOLCHAIN_OBJDUMP=${absolute_path}/bin/${OBJ_ARM64}
209			TOOLCHAIN_ADDR2LINE=${absolute_path}/bin/${ADDR2LINE_ARM64}
210		else
211			echo "Can't find toolchain: ${TOOLCHAIN_ARM64}"
212			exit 1
213		fi
214	else
215		if [ -d ${TOOLCHAIN_ARM32} ]; then
216			absolute_path=$(cd `dirname ${TOOLCHAIN_ARM32}`; pwd)
217			TOOLCHAIN_GCC=${absolute_path}/bin/${GCC_ARM32}
218			TOOLCHAIN_OBJDUMP=${absolute_path}/bin/${OBJ_ARM32}
219			TOOLCHAIN_ADDR2LINE=${absolute_path}/bin/${ADDR2LINE_ARM32}
220		else
221			echo "Can't find toolchain: ${TOOLCHAIN_ARM32}"
222			exit 1
223		fi
224	fi
225
226	# echo "toolchain: ${TOOLCHAIN_GCC}"
227}
228
229sub_commands()
230{
231	local cmd=${SUBCMD%-*} opt=${SUBCMD#*-}
232
233	case $cmd in
234		elf)
235		if [ ! -f ${OUTDIR}/u-boot ]; then
236			echo "Can't find elf file: ${OUTDIR}/u-boot"
237			exit 1
238		else
239			# default 'cmd' without option, use '-D'
240			if [ "${cmd}" = 'elf' -a "${opt}" = 'elf' ]; then
241				opt=D
242			fi
243			${TOOLCHAIN_OBJDUMP} -${opt} ${OUTDIR}/u-boot | less
244			exit 0
245		fi
246		;;
247
248		debug)
249		debug_command
250		exit 0
251		;;
252
253		map)
254		cat ${OUTDIR}/u-boot.map | less
255		exit 0
256		;;
257
258		sym)
259		cat ${OUTDIR}/u-boot.sym | less
260		exit 0
261		;;
262
263		trust)
264		pack_trust_image ${opt}
265		exit 0
266		;;
267
268		loader)
269		pack_loader_image ${opt}
270		exit 0
271		;;
272
273		spl)
274		pack_spl_loader_image ${opt}
275		exit 0
276		;;
277
278		itb)
279		pack_uboot_itb_image
280		exit 0
281		;;
282
283		uboot)
284		pack_uboot_image ${opt}
285		exit 0
286		;;
287
288		*)
289		# Search function and code position of address
290		RELOC_OFF=${FUNCADDR#*-}
291		FUNCADDR=${FUNCADDR%-*}
292		if [ -z $(echo ${FUNCADDR} | sed 's/[0-9,a-f,A-F,x,X,-]//g') ] && [ ${FUNCADDR} ]; then
293			# With prefix: '0x' or '0X'
294			if [ `echo ${FUNCADDR} | sed -n "/0[x,X]/p" | wc -l` -ne 0 ]; then
295				FUNCADDR=`echo $FUNCADDR | awk '{ print strtonum($0) }'`
296				FUNCADDR=`echo "obase=16;${FUNCADDR}"|bc |tr '[A-Z]' '[a-z]'`
297			fi
298			if [ `echo ${RELOC_OFF} | sed -n "/0[x,X]/p" | wc -l` -ne 0 ] && [ ${RELOC_OFF} ]; then
299				RELOC_OFF=`echo $RELOC_OFF | awk '{ print strtonum($0) }'`
300				RELOC_OFF=`echo "obase=16;${RELOC_OFF}"|bc |tr '[A-Z]' '[a-z]'`
301			fi
302
303			# If reloc address is assigned, do sub
304			if [ "${FUNCADDR}" != "${RELOC_OFF}" ]; then
305				# Hex -> Dec -> SUB -> Hex
306				FUNCADDR=`echo $((16#${FUNCADDR}))`
307				RELOC_OFF=`echo $((16#${RELOC_OFF}))`
308				FUNCADDR=$((FUNCADDR-RELOC_OFF))
309				FUNCADDR=$(echo "obase=16;${FUNCADDR}"|bc |tr '[A-Z]' '[a-z]')
310			fi
311
312			echo
313			sed -n "/${FUNCADDR}/p" ${OUTDIR}/u-boot.sym
314			${TOOLCHAIN_ADDR2LINE} -e ${OUTDIR}/u-boot ${FUNCADDR}
315			exit 0
316		fi
317		;;
318	esac
319}
320
321# We select chip info to do:
322#	1. RKCHIP: fixup platform configure
323#	2. RKCHIP_LOADER: search ini file to pack loader
324#	3. RKCHIP_TRUST: search ini file to pack trust
325#	4. RKCHIP_LABEL: show build message
326#
327# We read chip info from .config and 'RKCHIP_INI_DESC'
328select_chip_info()
329{
330	local target_board item value
331
332	# Read RKCHIP firstly from .config
333	# The regular expression that matching:
334	#  - PX30, PX3SE
335	#  - RK????, RK????X
336	#  - RV????
337	local chip_reg='^CONFIG_ROCKCHIP_[R,P][X,V,K][0-9ESX]{1,5}'
338	count=`egrep -c ${chip_reg} ${OUTDIR}/.config`
339	# Obtain the matching only
340	RKCHIP=`egrep -o ${chip_reg} ${OUTDIR}/.config`
341
342	if [ $count -eq 1 ]; then
343		RKCHIP=${RKCHIP##*_}
344		grep '^CONFIG_ROCKCHIP_RK3368=y' ${OUTDIR}/.config >/dev/null \
345			&& RKCHIP=RK3368H
346		grep '^CONFIG_ROCKCHIP_RV1108=y' ${OUTDIR}/.config >/dev/null \
347			&& RKCHIP=RV110X
348	elif [ $count -gt 1 ]; then
349		# Grep the RK CHIP variant
350		grep '^CONFIG_ROCKCHIP_PX3SE=y' ${OUTDIR}/.config > /dev/null \
351			&& RKCHIP=PX3SE
352		grep '^CONFIG_ROCKCHIP_RK3126=y' ${OUTDIR}/.config >/dev/null \
353			&& RKCHIP=RK3126
354		grep '^CONFIG_ROCKCHIP_RK3326=y' ${OUTDIR}/.config >/dev/null \
355			&& RKCHIP=RK3326
356		grep '^CONFIG_ROCKCHIP_RK3128X=y' ${OUTDIR}/.config >/dev/null \
357			&& RKCHIP=RK3128X
358		grep '^CONFIG_ROCKCHIP_PX5=y' ${OUTDIR}/.config >/dev/null \
359			&& RKCHIP=PX5
360		grep '^CONFIG_ROCKCHIP_RK3399PRO=y' ${OUTDIR}/.config >/dev/null \
361			&& RKCHIP=RK3399PRO
362	else
363		echo "Can't get Rockchip SoC definition in .config"
364		exit 1
365	fi
366
367	# Default use RKCHIP
368	RKCHIP_LABEL=${RKCHIP}
369	RKCHIP_LOADER=${RKCHIP}
370	RKCHIP_TRUST=${RKCHIP}
371
372	# Read from RKCHIP_INI_DESC
373	for item in "${RKCHIP_INI_DESC[@]}"
374	do
375		target_board=`echo $item | awk '{ print $1 }'`
376		if grep  -q "^${target_board}=y" ${OUTDIR}/.config ; then
377			value=`echo $item | awk '{ print $2 }'`
378			if [ "$value" != "NA" ]; then
379				RKCHIP_LABEL=${value};
380			fi
381			value=`echo $item | awk '{ print $3 }'`
382			if [ "$value" != "NA" ]; then
383				RKCHIP_LOADER=${value};
384			fi
385			value=`echo $item | awk '{ print $4 }'`
386			if [ "$value" != "NA" ]; then
387				RKCHIP_TRUST=${value};
388			fi
389		fi
390	done
391}
392
393# Fixup platform special configure
394#	1. fixup pack mode;
395#	2. fixup image size
396#	3. fixup ARM64 cpu boot with AArch32
397fixup_platform_configure()
398{
399	local count plat
400
401# <*> Fixup rsa/sha pack mode for platforms
402	# RK3308/PX30/RK3326/RK1808 use RSA-PKCS1 V2.1, it's pack magic is "3"
403	if [ $RKCHIP = "PX30" -o $RKCHIP = "RK3326" -o $RKCHIP = "RK3308" -o $RKCHIP = "RK1808" ]; then
404		PLATFORM_RSA="--rsa 3"
405	# RK3368 use rk big endian SHA256, it's pack magic is "2"
406	elif [ $RKCHIP = "RK3368" ]; then
407		PLATFORM_SHA="--sha 2"
408	# other platforms use default configure
409	fi
410
411# <*> Fixup images size pack for platforms
412	if [ $RKCHIP = "RK3308" ]; then
413		if grep -q '^CONFIG_ARM64_BOOT_AARCH32=y' ${OUTDIR}/.config ; then
414			PLATFORM_UBOOT_IMG_SIZE="--size 512 2"
415			PLATFORM_TRUST_IMG_SIZE="--size 512 2"
416		else
417			PLATFORM_UBOOT_IMG_SIZE="--size 1024 2"
418			PLATFORM_TRUST_IMG_SIZE="--size 1024 2"
419		fi
420	elif [ $RKCHIP = "RK1808" ]; then
421		PLATFORM_UBOOT_IMG_SIZE="--size 1024 2"
422		PLATFORM_TRUST_IMG_SIZE="--size 1024 2"
423	fi
424
425# <*> Fixup AARCH32 for ARM64 cpu platforms
426	if grep -q '^CONFIG_ARM64_BOOT_AARCH32=y' ${OUTDIR}/.config ; then
427		if [ $RKCHIP = "RK3308" ]; then
428			RKCHIP_LABEL=${RKCHIP_LABEL}"AARCH32"
429			RKCHIP_TRUST=${RKCHIP_TRUST}"AARCH32"
430		elif [ $RKCHIP = "RK3326" ]; then
431			RKCHIP_LABEL=${RKCHIP_LABEL}"AARCH32"
432			RKCHIP_LOADER=${RKCHIP_LOADER}"AARCH32"
433		fi
434	fi
435}
436
437debug_command()
438{
439		if [ "${cmd}" = 'debug' -a "${opt}" = 'debug' ]; then
440			echo
441			echo "The commands will modify .config and files, and can't auto restore changes!"
442			echo "debug-N, the N:"
443			echo "    1. lib/initcall.c debug() -> printf()"
444			echo "    2. common/board_r.c and common/board_f.c debug() -> printf()"
445			echo "    3. global #define DEBUG"
446			echo "    4. enable CONFIG_ROCKCHIP_DEBUGGER"
447			echo "    5. enable CONFIG_ROCKCHIP_CRC"
448			echo "    6. enable CONFIG_BOOTSTAGE_PRINTF_TIMESTAMP"
449			echo "    7. enable CONFIG_ROCKCHIP_CRASH_DUMP"
450			echo "    8. set CONFIG_BOOTDELAY=5"
451			echo "    9. armv7 start.S: print entry warning"
452			echo "   10. armv8 start.S: print entry warning"
453			echo "   11. firmware bootflow debug() -> printf()"
454			echo "   12. bootstage timing report"
455			echo
456			echo "Enabled: "
457			grep '^CONFIG_ROCKCHIP_DEBUGGER=y' ${OUTDIR}/.config > /dev/null \
458			&& echo "    CONFIG_ROCKCHIP_DEBUGGER"
459			grep '^CONFIG_ROCKCHIP_CRC=y' ${OUTDIR}/.config > /dev/null \
460			&& echo "    CONFIG_ROCKCHIP_CRC"
461			grep '^CONFIG_BOOTSTAGE_PRINTF_TIMESTAMP=y' ${OUTDIR}/.config > /dev/null \
462			&& echo "    CONFIG_BOOTSTAGE_PRINTF_TIMESTAMP"
463			grep '^CONFIG_ROCKCHIP_CRASH_DUMP=y' ${OUTDIR}/.config > /dev/null \
464			&& echo "    CONFIG_ROCKCHIP_CRASH_DUMP"
465
466		elif [ "${opt}" = '1' ]; then
467			sed -i 's/\<debug\>/printf/g' lib/initcall.c
468			sed -i 's/ifdef DEBUG/if 1/g' lib/initcall.c
469			echo "DEBUG [1]: lib/initcall.c debug() -> printf()"
470		elif [ "${opt}" = '2' ]; then
471			sed -i 's/\<debug\>/printf/g' ./common/board_f.c
472			sed -i 's/\<debug\>/printf/g' ./common/board_r.c
473			echo "DEBUG [2]: common/board_r.c and common/board_f.c debug() -> printf()"
474		elif [ "${opt}" = '3' ]; then
475			sed -i '$i \#define DEBUG\' include/configs/rockchip-common.h
476			echo "DEBUG [3]: global #define DEBUG"
477		elif [ "${opt}" = '4' ]; then
478			sed -i 's/\# CONFIG_ROCKCHIP_DEBUGGER is not set/CONFIG_ROCKCHIP_DEBUGGER=y/g' ${OUTDIR}/.config
479			echo "DEBUG [4]: CONFIG_ROCKCHIP_DEBUGGER is enabled"
480		elif [ "${opt}" = '5' ]; then
481			sed -i 's/\# CONFIG_ROCKCHIP_CRC is not set/CONFIG_ROCKCHIP_CRC=y/g' ${OUTDIR}/.config
482			echo "DEBUG [5]: CONFIG_ROCKCHIP_CRC is enabled"
483		elif [ "${opt}" = '6' ]; then
484			sed -i 's/\# CONFIG_BOOTSTAGE_PRINTF_TIMESTAMP is not set/CONFIG_BOOTSTAGE_PRINTF_TIMESTAMP=y/g' ${OUTDIR}/.config
485			echo "DEBUG [6]: CONFIG_BOOTSTAGE_PRINTF_TIMESTAMP is enabled"
486		elif [ "${opt}" = '7' ]; then
487			sed -i 's/\# CONFIG_ROCKCHIP_CRASH_DUMP is not set/CONFIG_ROCKCHIP_CRASH_DUMP=y/g' ${OUTDIR}/.config
488			echo "DEBUG [7]: CONFIG_ROCKCHIP_CRASH_DUMP is enabled"
489		elif [ "${opt}" = '8' ]; then
490			sed -i 's/^CONFIG_BOOTDELAY=0/CONFIG_BOOTDELAY=5/g' ${OUTDIR}/.config
491			echo "DEBUG [8]: CONFIG_BOOTDELAY is 5s"
492		elif [ "${opt}" = '9' ]; then
493			sed -i '/save_boot_params_ret:/a\ldr r0, =CONFIG_DEBUG_UART_BASE\nmov r1, #100\nloop:\nmov r2, #0x55\nstr r2, [r0]\nsub r1, r1, #1\ncmp r1, #0\nbne loop\ndsb' \
494			./arch/arm/cpu/armv7/start.S
495			echo "DEBUG [9]: armv7 start.S entry warning 'UUUU...'"
496		elif [ "${opt}" = '10' ]; then
497			sed -i '/save_boot_params_ret:/a\ldr x0, =CONFIG_DEBUG_UART_BASE\nmov x1, #100\nloop:\nmov x2, #0x55\nstr x2, [x0]\nsub x1, x1, #1\ncmp x1, #0\nb.ne loop\ndsb sy' \
498			./arch/arm/cpu/armv8/start.S
499			echo "DEBUG [10]: armv8 start.S entry warning 'UUUU...'"
500		elif [ "${opt}" = '11' ]; then
501			sed -i 's/\<debug\>/printf/g' common/fdt_support.c
502			sed -i 's/\<debug\>/printf/g' common/image-fdt.c
503			sed -i 's/\<debug\>/printf/g' common/image.c
504			sed -i 's/\<debug\>/printf/g' arch/arm/lib/bootm.c
505			sed -i 's/\<debug\>/printf/g' common/bootm.c
506			sed -i 's/\<debug\>/printf/g' common/image.c
507			sed -i 's/\<debug\>/printf/g' common/image-android.c
508			sed -i 's/\<debug\>/printf/g' common/android_bootloader.c
509			echo "DEBUG [11]: firmware bootflow debug() -> printf()"
510		elif [ "${opt}" = '12' ]; then
511			sed -i '$a\CONFIG_BOOTSTAGE=y\' ${OUTDIR}/.config
512			sed -i '$a\CONFIG_BOOTSTAGE_REPORT=y\' ${OUTDIR}/.config
513			sed -i '$a\CONFIG_CMD_BOOTSTAGE=y\' ${OUTDIR}/.config
514			echo "DEBUG [12]: bootstage timing report"
515		fi
516		echo
517}
518
519pack_uboot_image()
520{
521	local UBOOT_LOAD_ADDR UBOOT_MAX_KB UBOOT_KB HEAD_KB=2
522
523	# Check file size
524	UBOOT_KB=`ls -l u-boot.bin | awk '{print $5}'`
525	if [ "$PLATFORM_UBOOT_IMG_SIZE" = "" ]; then
526		UBOOT_MAX_KB=1046528
527	else
528		UBOOT_MAX_KB=`echo $PLATFORM_UBOOT_IMG_SIZE | awk '{print strtonum($2)}'`
529		UBOOT_MAX_KB=$(((UBOOT_MAX_KB-HEAD_KB)*1024))
530	fi
531
532	if [ $UBOOT_KB -gt $UBOOT_MAX_KB ]; then
533		echo
534		echo "ERROR: pack uboot failed! u-boot.bin actual: $UBOOT_KB bytes, max limit: $UBOOT_MAX_KB bytes"
535		exit 1
536	fi
537
538	# Pack image
539	UBOOT_LOAD_ADDR=`sed -n "/CONFIG_SYS_TEXT_BASE=/s/CONFIG_SYS_TEXT_BASE=//p" ${OUTDIR}/include/autoconf.mk|tr -d '\r'`
540	if [ ! $UBOOT_LOAD_ADDR ]; then
541		UBOOT_LOAD_ADDR=`sed -n "/CONFIG_SYS_TEXT_BASE=/s/CONFIG_SYS_TEXT_BASE=//p" ${OUTDIR}/.config|tr -d '\r'`
542	fi
543
544	${RKTOOLS}/loaderimage --pack --uboot ${OUTDIR}/u-boot.bin uboot.img ${UBOOT_LOAD_ADDR} ${PLATFORM_UBOOT_IMG_SIZE}
545
546	# Delete u-boot.img and u-boot-dtb.img, which makes users not be confused with final uboot.img
547	if [ -f ${OUTDIR}/u-boot.img ]; then
548		rm ${OUTDIR}/u-boot.img
549	fi
550
551	if [ -f ${OUTDIR}/u-boot-dtb.img ]; then
552		rm ${OUTDIR}/u-boot-dtb.img
553	fi
554
555	echo "pack uboot okay! Input: ${OUTDIR}/u-boot.bin"
556}
557
558pack_uboot_itb_image()
559{
560	local ini=${RKBIN}/RKTRUST/${RKCHIP_TRUST}${PLATFORM_AARCH32}TRUST.ini
561
562	if [ ! -f ${ini} ]; then
563		echo "pack trust failed! Can't find: ${ini}"
564		return
565	fi
566
567	bl31=`sed -n '/_bl31_/s/PATH=//p' ${ini} |tr -d '\r'`
568
569	cp ${RKBIN}/${bl31} bl31.elf
570	make CROSS_COMPILE=${TOOLCHAIN_GCC} u-boot.itb
571
572	echo "pack u-boot.itb okay! Input: ${ini}"
573}
574
575pack_spl_loader_image()
576{
577	local header label="SPL" mode=$1
578	local ini=${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini
579	local temp_ini=${RKBIN}/.temp/${RKCHIP_LOADER}MINIALL.ini
580
581	if [ ! -f ${ini} ]; then
582		echo "pack TPL+SPL loader failed! Can't find: ${ini}"
583		return
584	fi
585
586	# Copy to .temp folder
587	if [ -d ${RKBIN}/.temp ]; then
588		rm ${RKBIN}/.temp -rf
589	else
590		mkdir ${RKBIN}/.temp
591	fi
592	cp ${OUTDIR}/spl/u-boot-spl.bin ${RKBIN}/.temp/
593	cp ${OUTDIR}/tpl/u-boot-tpl.bin ${RKBIN}/.temp/
594	cp ${ini} ${RKBIN}/.temp/
595
596	cd ${RKBIN}
597	if [ "$mode" = 'spl' ]; then	# pack tpl+spl
598		# Update ini
599		label="TPL+SPL"
600		header=`sed -n '/NAME=/s/NAME=//p' ${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini`
601		dd if=${RKBIN}/.temp/u-boot-tpl.bin of=${RKBIN}/.temp/tpl.bin bs=1 skip=4
602		sed -i "1s/^/${header:0:4}/" ${RKBIN}/.temp/tpl.bin
603		sed -i "s/FlashData=.*$/FlashData=.\/.temp\/tpl.bin/"     ${temp_ini}
604	fi
605
606	sed -i "s/FlashBoot=.*$/FlashBoot=.\/.temp\/u-boot-spl.bin/"  ${temp_ini}
607
608	${RKTOOLS}/boot_merger ${BIN_PATH_FIXUP} ${temp_ini}
609	rm ${RKBIN}/.temp -rf
610	cd -
611	ls *_loader_*.bin >/dev/null 2>&1 && rm *_loader_*.bin
612	mv ${RKBIN}/*_loader_*.bin ./
613	echo "pack loader(${label}) okay! Input: ${ini}"
614	ls ./*_loader_*.bin
615}
616
617pack_loader_image()
618{
619	local mode=$1 files ini=${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini
620
621	if [ ! -f $ini ]; then
622		echo "pack loader failed! Can't find: $ini"
623		return
624	fi
625
626	ls *_loader_*.bin >/dev/null && rm *_loader_*.bin
627	cd ${RKBIN}
628
629	if [ "${mode}" = 'all' ]; then
630		files=`ls ${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL*.ini`
631		for ini in $files
632		do
633			if [ -f "$ini" ]; then
634				${RKTOOLS}/boot_merger ${BIN_PATH_FIXUP} $ini
635				echo "pack loader okay! Input: $ini"
636			fi
637		done
638	else
639		${RKTOOLS}/boot_merger ${BIN_PATH_FIXUP} $ini
640		echo "pack loader okay! Input: $ini"
641	fi
642
643	cd - && mv ${RKBIN}/*_loader_*.bin ./
644}
645
646__pack_32bit_trust_image()
647{
648	local ini=$1 TOS TOS_TA DARM_BASE TEE_LOAD_ADDR TEE_OUTPUT TEE_OFFSET
649
650	if [ ! -f ${ini} ]; then
651		echo "pack trust failed! Can't find: ${ini}"
652		return
653	fi
654
655	# Parse orignal path
656	TOS=`sed -n "/TOS=/s/TOS=//p" ${ini} |tr -d '\r'`
657	TOS_TA=`sed -n "/TOSTA=/s/TOSTA=//p" ${ini} |tr -d '\r'`
658
659	# Parse address and output name
660	TEE_OUTPUT=`sed -n "/OUTPUT=/s/OUTPUT=//p" ${ini} |tr -d '\r'`
661	if [ "$TEE_OUTPUT" = "" ]; then
662		TEE_OUTPUT="./trust.img"
663	fi
664	TEE_OFFSET=`sed -n "/ADDR=/s/ADDR=//p" ${ini} |tr -d '\r'`
665	if [ "$TEE_OFFSET" = "" ]; then
666		TEE_OFFSET=0x8400000
667	fi
668
669	# OP-TEE is 132M(0x8400000) offset from DRAM base.
670	DARM_BASE=`sed -n "/CONFIG_SYS_SDRAM_BASE=/s/CONFIG_SYS_SDRAM_BASE=//p" ${OUTDIR}/include/autoconf.mk|tr -d '\r'`
671	TEE_LOAD_ADDR=$((DARM_BASE+TEE_OFFSET))
672
673	# Convert Dec to Hex
674	TEE_LOAD_ADDR=$(echo "obase=16;${TEE_LOAD_ADDR}"|bc)
675
676	# Replace "./tools/rk_tools/" with "./" to compatible legacy ini content of rkdevelop branch
677	TOS=$(echo ${TOS} | sed "s/tools\/rk_tools\//\.\//g")
678	TOS_TA=$(echo ${TOS_TA} | sed "s/tools\/rk_tools\//\.\//g")
679
680	if [ $TOS_TA ]; then
681		${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS_TA} ${TEE_OUTPUT} ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE}
682	elif [ $TOS ]; then
683		${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS}    ${TEE_OUTPUT} ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE}
684	else
685		echo "Can't find any tee bin"
686		exit 1
687	fi
688
689	echo "pack trust okay! Input: ${ini}"
690	echo
691}
692
693__pack_64bit_trust_image()
694{
695	local ini=$1
696
697	if [ ! -f ${ini} ]; then
698		echo "pack trust failed! Can't find: ${ini}"
699		return
700	fi
701
702	cd ${RKBIN}
703	${RKTOOLS}/trust_merger ${PLATFORM_SHA} ${PLATFORM_RSA} ${PLATFORM_TRUST_IMG_SIZE} ${BIN_PATH_FIXUP} \
704				${PACK_IGNORE_BL32} ${ini}
705
706	cd - && mv ${RKBIN}/trust*.img ./
707	echo "pack trust okay! Input: ${ini}"
708	echo
709}
710
711pack_trust_image()
712{
713	local mode=$1 files ini
714
715	ls trust*.img >/dev/null && rm trust*.img
716	# ARM64 uses trust_merger
717	if grep -Eq ''^CONFIG_ARM64=y'|'^CONFIG_ARM64_BOOT_AARCH32=y'' ${OUTDIR}/.config ; then
718		ini=${RKBIN}/RKTRUST/${RKCHIP_TRUST}TRUST.ini
719		if [ "${mode}" = 'all' ]; then
720			files=`ls ${RKBIN}/RKTRUST/${RKCHIP_TRUST}TRUST*.ini`
721			for ini in $files
722			do
723				__pack_64bit_trust_image ${ini}
724			done
725		else
726			__pack_64bit_trust_image ${ini}
727		fi
728	# ARM uses loaderimage
729	else
730		ini=${RKBIN}/RKTRUST/${RKCHIP_TRUST}TOS.ini
731		if [ "${mode}" = 'all' ]; then
732			files=`ls ${RKBIN}/RKTRUST/${RKCHIP_TRUST}TOS*.ini`
733			for ini in $files
734			do
735				__pack_32bit_trust_image ${ini}
736			done
737		else
738			__pack_32bit_trust_image ${ini}
739		fi
740	fi
741}
742
743finish()
744{
745	echo
746	if [ "$BOARD" = '' ]; then
747		echo "Platform ${RKCHIP_LABEL} is build OK, with exist .config"
748	else
749		echo "Platform ${RKCHIP_LABEL} is build OK, with new .config(make ${BOARD}_defconfig)"
750	fi
751}
752
753prepare
754select_toolchain
755select_chip_info
756fixup_platform_configure
757sub_commands
758make CROSS_COMPILE=${TOOLCHAIN_GCC}  all --jobs=${JOB} ${OUTOPT}
759pack_uboot_image
760pack_loader_image
761pack_trust_image
762finish
763