xref: /rk3399_rockchip-uboot/make.sh (revision f089d907dee8c0ceb2394ba1cdca43cc3cd8d90d)
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  RK3399PRO-NPU  RK3399PRONPU  RK3399PRONPU"
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=
69PLATFORM_AARCH32=
70
71# Out env param
72PACK_IGNORE_BL32=$TRUST_PACK_IGNORE_BL32	# Value only: "--ignore-bl32"
73#########################################################################################################
74help()
75{
76	echo
77	echo "Usage:"
78	echo "	./make.sh [board|subcmd] [O=<dir>]"
79	echo
80	echo "	 - board: board name of defconfig"
81	echo "	 - subcmd: loader|loader-all|trust|uboot|elf|map|sym|<addr>|"
82	echo "	 - O=<dir>: assigned output directory"
83	echo
84	echo "Example:"
85	echo
86	echo "1. Build board:"
87	echo "	./make.sh evb-rk3399               --- build for evb-rk3399_defconfig"
88	echo "	./make.sh evb-rk3399 O=rockdev     --- build for evb-rk3399_defconfig with output dir "./rockdev""
89	echo "	./make.sh firefly-rk3288           --- build for firefly-rk3288_defconfig"
90	echo "	./make.sh                          --- build with exist .config"
91	echo
92	echo "	After build, Images of uboot, loader and trust are all generated."
93	echo
94	echo "2. Pack helper:"
95	echo "	./make.sh trust                    --- pack trust.img"
96	echo "	./make.sh uboot                    --- pack uboot.img"
97	echo "	./make.sh loader                   --- pack loader bin"
98	echo "	./make.sh loader-all	           --- pack loader bin (all supported loaders)"
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*|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*|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
265		exit 0
266		;;
267
268		loader)
269		pack_loader_image ${opt}
270		exit 0
271		;;
272
273		uboot)
274		pack_uboot_image
275		exit 0
276		;;
277
278		*)
279		# Search function and code position of address
280		RELOC_OFF=${FUNCADDR#*-}
281		FUNCADDR=${FUNCADDR%-*}
282		if [ -z $(echo ${FUNCADDR} | sed 's/[0-9,a-f,A-F,x,X,-]//g') ] && [ ${FUNCADDR} ]; then
283			# With prefix: '0x' or '0X'
284			if [ `echo ${FUNCADDR} | sed -n "/0[x,X]/p" | wc -l` -ne 0 ]; then
285				FUNCADDR=`echo $FUNCADDR | awk '{ print strtonum($0) }'`
286				FUNCADDR=`echo "obase=16;${FUNCADDR}"|bc |tr '[A-Z]' '[a-z]'`
287			fi
288			if [ `echo ${RELOC_OFF} | sed -n "/0[x,X]/p" | wc -l` -ne 0 ] && [ ${RELOC_OFF} ]; then
289				RELOC_OFF=`echo $RELOC_OFF | awk '{ print strtonum($0) }'`
290				RELOC_OFF=`echo "obase=16;${RELOC_OFF}"|bc |tr '[A-Z]' '[a-z]'`
291			fi
292
293			# If reloc address is assigned, do sub
294			if [ "${FUNCADDR}" != "${RELOC_OFF}" ]; then
295				# Hex -> Dec -> SUB -> Hex
296				FUNCADDR=`echo $((16#${FUNCADDR}))`
297				RELOC_OFF=`echo $((16#${RELOC_OFF}))`
298				FUNCADDR=$((FUNCADDR-RELOC_OFF))
299				FUNCADDR=$(echo "obase=16;${FUNCADDR}"|bc |tr '[A-Z]' '[a-z]')
300			fi
301
302			echo
303			sed -n "/${FUNCADDR}/p" ${OUTDIR}/u-boot.sym
304			${TOOLCHAIN_ADDR2LINE} -e ${OUTDIR}/u-boot ${FUNCADDR}
305			exit 0
306		fi
307		;;
308	esac
309}
310
311# We select chip info to do:
312#	1. RKCHIP: fixup platform configure
313#	2. RKCHIP_LOADER: search ini file to pack loader
314#	3. RKCHIP_TRUST: search ini file to pack trust
315#	4. RKCHIP_LABEL: show build message
316#
317# We read chip info from .config and 'RKCHIP_INI_DESC'
318select_chip_info()
319{
320	local target_board item value
321
322	# Read RKCHIP firstly from .config
323	# The regular expression that matching:
324	#  - PX30, PX3SE
325	#  - RK????, RK????X
326	#  - RV????
327	local chip_reg='^CONFIG_ROCKCHIP_[R,P][X,V,K][0-9ESX]{1,5}'
328	count=`egrep -c ${chip_reg} ${OUTDIR}/.config`
329	# Obtain the matching only
330	RKCHIP=`egrep -o ${chip_reg} ${OUTDIR}/.config`
331
332	if [ $count -eq 1 ]; then
333		RKCHIP=${RKCHIP##*_}
334		grep '^CONFIG_ROCKCHIP_RK3368=y' ${OUTDIR}/.config >/dev/null \
335			&& RKCHIP=RK3368H
336		grep '^CONFIG_ROCKCHIP_RV1108=y' ${OUTDIR}/.config >/dev/null \
337			&& RKCHIP=RV110X
338	elif [ $count -gt 1 ]; then
339		# Grep the RK CHIP variant
340		grep '^CONFIG_ROCKCHIP_PX3SE=y' ${OUTDIR}/.config > /dev/null \
341			&& RKCHIP=PX3SE
342		grep '^CONFIG_ROCKCHIP_RK3126=y' ${OUTDIR}/.config >/dev/null \
343			&& RKCHIP=RK3126
344		grep '^CONFIG_ROCKCHIP_RK3326=y' ${OUTDIR}/.config >/dev/null \
345			&& RKCHIP=RK3326
346		grep '^CONFIG_ROCKCHIP_RK3128X=y' ${OUTDIR}/.config >/dev/null \
347			&& RKCHIP=RK3128X
348		grep '^CONFIG_ROCKCHIP_PX5=y' ${OUTDIR}/.config >/dev/null \
349			&& RKCHIP=PX5
350		grep '^CONFIG_ROCKCHIP_RK3399PRO=y' ${OUTDIR}/.config >/dev/null \
351			&& RKCHIP=RK3399PRO
352	else
353		echo "Can't get Rockchip SoC definition in .config"
354		exit 1
355	fi
356
357	# Default use RKCHIP
358	RKCHIP_LABEL=${RKCHIP}
359	RKCHIP_LOADER=${RKCHIP}
360	RKCHIP_TRUST=${RKCHIP}
361
362	# Read from RKCHIP_INI_DESC
363	for item in "${RKCHIP_INI_DESC[@]}"
364	do
365		target_board=`echo $item | awk '{ print $1 }'`
366		if grep  -q "^${target_board}=y" ${OUTDIR}/.config ; then
367			value=`echo $item | awk '{ print $2 }'`
368			if [ "$value" != "NA" ]; then
369				RKCHIP_LABEL=${value};
370			fi
371			value=`echo $item | awk '{ print $3 }'`
372			if [ "$value" != "NA" ]; then
373				RKCHIP_LOADER=${value};
374			fi
375			value=`echo $item | awk '{ print $4 }'`
376			if [ "$value" != "NA" ]; then
377				RKCHIP_TRUST=${value};
378			fi
379		fi
380	done
381}
382
383# Fixup platform special configure
384#	1. fixup pack mode;
385#	2. fixup image size
386#	3. fixup ARM64 cpu boot with AArch32
387fixup_platform_configure()
388{
389	local count plat
390
391# <*> Fixup rsa/sha pack mode for platforms
392	# RK3308/PX30/RK3326/RK1808 use RSA-PKCS1 V2.1, it's pack magic is "3"
393	if [ $RKCHIP = "PX30" -o $RKCHIP = "RK3326" -o $RKCHIP = "RK3308" -o $RKCHIP = "RK1808" ]; then
394		PLATFORM_RSA="--rsa 3"
395	# RK3368 use rk big endian SHA256, it's pack magic is "2"
396	elif [ $RKCHIP = "RK3368" ]; then
397		PLATFORM_SHA="--sha 2"
398	# other platforms use default configure
399	fi
400
401# <*> Fixup images size pack for platforms
402	if [ $RKCHIP = "RK3308" ]; then
403		if grep -q '^CONFIG_ARM64_BOOT_AARCH32=y' ${OUTDIR}/.config ; then
404			PLATFORM_UBOOT_IMG_SIZE="--size 512 2"
405			PLATFORM_TRUST_IMG_SIZE="--size 512 2"
406		else
407			PLATFORM_UBOOT_IMG_SIZE="--size 1024 2"
408			PLATFORM_TRUST_IMG_SIZE="--size 1024 2"
409		fi
410	elif [ $RKCHIP = "RK1808" ]; then
411		PLATFORM_UBOOT_IMG_SIZE="--size 1024 2"
412		PLATFORM_TRUST_IMG_SIZE="--size 1024 2"
413	fi
414
415# <*> Fixup PLATFORM_AARCH32 for ARM64 cpu platforms
416	if [ $RKCHIP = "RK3308" ]; then
417		if grep -q '^CONFIG_ARM64_BOOT_AARCH32=y' ${OUTDIR}/.config ; then
418			PLATFORM_AARCH32="AARCH32"
419		fi
420	fi
421}
422
423debug_command()
424{
425		if [ "${cmd}" = 'debug' -a "${opt}" = 'debug' ]; then
426			echo
427			echo "The commands will modify .config and files, and can't auto restore changes!"
428			echo "debug-N, the N:"
429			echo "    1. lib/initcall.c debug() -> printf()"
430			echo "    2. common/board_r.c and common/board_f.c debug() -> printf()"
431			echo "    3. global #define DEBUG"
432			echo "    4. enable CONFIG_ROCKCHIP_DEBUGGER"
433			echo "    5. enable CONFIG_ROCKCHIP_CRC"
434			echo "    6. enable CONFIG_BOOTSTAGE_PRINTF_TIMESTAMP"
435			echo "    7. enable CONFIG_ROCKCHIP_CRASH_DUMP"
436			echo "    8. set CONFIG_BOOTDELAY=5"
437			echo "    9. armv7 start.S: print entry warning"
438			echo "   10. armv8 start.S: print entry warning"
439			echo "   11. firmware bootflow debug() -> printf()"
440			echo "   12. bootstage timing report"
441			echo
442			echo "Enabled: "
443			grep '^CONFIG_ROCKCHIP_DEBUGGER=y' ${OUTDIR}/.config > /dev/null \
444			&& echo "    CONFIG_ROCKCHIP_DEBUGGER"
445			grep '^CONFIG_ROCKCHIP_CRC=y' ${OUTDIR}/.config > /dev/null \
446			&& echo "    CONFIG_ROCKCHIP_CRC"
447			grep '^CONFIG_BOOTSTAGE_PRINTF_TIMESTAMP=y' ${OUTDIR}/.config > /dev/null \
448			&& echo "    CONFIG_BOOTSTAGE_PRINTF_TIMESTAMP"
449			grep '^CONFIG_ROCKCHIP_CRASH_DUMP=y' ${OUTDIR}/.config > /dev/null \
450			&& echo "    CONFIG_ROCKCHIP_CRASH_DUMP"
451
452		elif [ "${opt}" = '1' ]; then
453			sed -i 's/\<debug\>/printf/g' lib/initcall.c
454			sed -i 's/ifdef DEBUG/if 1/g' lib/initcall.c
455			echo "DEBUG [1]: lib/initcall.c debug() -> printf()"
456		elif [ "${opt}" = '2' ]; then
457			sed -i 's/\<debug\>/printf/g' ./common/board_f.c
458			sed -i 's/\<debug\>/printf/g' ./common/board_r.c
459			echo "DEBUG [2]: common/board_r.c and common/board_f.c debug() -> printf()"
460		elif [ "${opt}" = '3' ]; then
461			sed -i '$i \#define DEBUG\' include/configs/rockchip-common.h
462			echo "DEBUG [3]: global #define DEBUG"
463		elif [ "${opt}" = '4' ]; then
464			sed -i 's/\# CONFIG_ROCKCHIP_DEBUGGER is not set/CONFIG_ROCKCHIP_DEBUGGER=y/g' ${OUTDIR}/.config
465			echo "DEBUG [4]: CONFIG_ROCKCHIP_DEBUGGER is enabled"
466		elif [ "${opt}" = '5' ]; then
467			sed -i 's/\# CONFIG_ROCKCHIP_CRC is not set/CONFIG_ROCKCHIP_CRC=y/g' ${OUTDIR}/.config
468			echo "DEBUG [5]: CONFIG_ROCKCHIP_CRC is enabled"
469		elif [ "${opt}" = '6' ]; then
470			sed -i 's/\# CONFIG_BOOTSTAGE_PRINTF_TIMESTAMP is not set/CONFIG_BOOTSTAGE_PRINTF_TIMESTAMP=y/g' ${OUTDIR}/.config
471			echo "DEBUG [6]: CONFIG_BOOTSTAGE_PRINTF_TIMESTAMP is enabled"
472		elif [ "${opt}" = '7' ]; then
473			sed -i 's/\# CONFIG_ROCKCHIP_CRASH_DUMP is not set/CONFIG_ROCKCHIP_CRASH_DUMP=y/g' ${OUTDIR}/.config
474			echo "DEBUG [7]: CONFIG_ROCKCHIP_CRASH_DUMP is enabled"
475		elif [ "${opt}" = '8' ]; then
476			sed -i 's/^CONFIG_BOOTDELAY=0/CONFIG_BOOTDELAY=5/g' ${OUTDIR}/.config
477			echo "DEBUG [8]: CONFIG_BOOTDELAY is 5s"
478		elif [ "${opt}" = '9' ]; then
479			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' \
480			./arch/arm/cpu/armv7/start.S
481			echo "DEBUG [9]: armv7 start.S entry warning 'UUUU...'"
482		elif [ "${opt}" = '10' ]; then
483			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' \
484			./arch/arm/cpu/armv8/start.S
485			echo "DEBUG [10]: armv8 start.S entry warning 'UUUU...'"
486		elif [ "${opt}" = '11' ]; then
487			sed -i 's/\<debug\>/printf/g' common/fdt_support.c
488			sed -i 's/\<debug\>/printf/g' common/image-fdt.c
489			sed -i 's/\<debug\>/printf/g' common/image.c
490			sed -i 's/\<debug\>/printf/g' arch/arm/lib/bootm.c
491			sed -i 's/\<debug\>/printf/g' common/bootm.c
492			sed -i 's/\<debug\>/printf/g' common/image.c
493			sed -i 's/\<debug\>/printf/g' common/image-android.c
494			sed -i 's/\<debug\>/printf/g' common/android_bootloader.c
495			echo "DEBUG [11]: firmware bootflow debug() -> printf()"
496		elif [ "${opt}" = '12' ]; then
497			sed -i '$a\CONFIG_BOOTSTAGE=y\' ${OUTDIR}/.config
498			sed -i '$a\CONFIG_BOOTSTAGE_REPORT=y\' ${OUTDIR}/.config
499			sed -i '$a\CONFIG_CMD_BOOTSTAGE=y\' ${OUTDIR}/.config
500			echo "DEBUG [12]: bootstage timing report"
501		fi
502		echo
503}
504
505pack_uboot_image()
506{
507	local UBOOT_LOAD_ADDR UBOOT_MAX_KB UBOOT_KB HEAD_KB=2
508
509	# Check file size
510	UBOOT_KB=`ls -l u-boot.bin | awk '{print $5}'`
511	if [ "$PLATFORM_UBOOT_IMG_SIZE" = "" ]; then
512		UBOOT_MAX_KB=1046528
513	else
514		UBOOT_MAX_KB=`echo $PLATFORM_UBOOT_IMG_SIZE | awk '{print strtonum($2)}'`
515		UBOOT_MAX_KB=$(((UBOOT_MAX_KB-HEAD_KB)*1024))
516	fi
517
518	if [ $UBOOT_KB -gt $UBOOT_MAX_KB ]; then
519		echo
520		echo "ERROR: pack uboot failed! u-boot.bin actual: $UBOOT_KB bytes, max limit: $UBOOT_MAX_KB bytes"
521		exit 1
522	fi
523
524	# Pack image
525	UBOOT_LOAD_ADDR=`sed -n "/CONFIG_SYS_TEXT_BASE=/s/CONFIG_SYS_TEXT_BASE=//p" ${OUTDIR}/include/autoconf.mk|tr -d '\r'`
526	if [ ! $UBOOT_LOAD_ADDR ]; then
527		UBOOT_LOAD_ADDR=`sed -n "/CONFIG_SYS_TEXT_BASE=/s/CONFIG_SYS_TEXT_BASE=//p" ${OUTDIR}/.config|tr -d '\r'`
528	fi
529
530	${RKTOOLS}/loaderimage --pack --uboot ${OUTDIR}/u-boot.bin uboot.img ${UBOOT_LOAD_ADDR} ${PLATFORM_UBOOT_IMG_SIZE}
531
532	# Delete u-boot.img and u-boot-dtb.img, which makes users not be confused with final uboot.img
533	if [ -f ${OUTDIR}/u-boot.img ]; then
534		rm ${OUTDIR}/u-boot.img
535	fi
536
537	if [ -f ${OUTDIR}/u-boot-dtb.img ]; then
538		rm ${OUTDIR}/u-boot-dtb.img
539	fi
540
541	echo "pack uboot okay! Input: ${OUTDIR}/u-boot.bin"
542}
543
544pack_loader_image()
545{
546	local mode=$1 files ini
547
548	if [ ! -f ${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini ]; then
549		echo "pack loader failed! Can't find: ${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini"
550		return
551	fi
552
553	cd ${RKBIN}
554
555	if [ "${mode}" = 'all' ]; then
556		files=`ls ${RKBIN}/RKBOOT/${RKCHIP_LOADER}*MINIALL*.ini`
557		for ini in $files
558		do
559			if [ -f "$ini" ]; then
560				${RKTOOLS}/boot_merger ${BIN_PATH_FIXUP} $ini
561				echo "pack loader okay! Input: $ini"
562			fi
563		done
564	else
565		${RKTOOLS}/boot_merger ${BIN_PATH_FIXUP} ${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini
566		echo "pack loader okay! Input: ${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini"
567	fi
568
569	cd - && mv ${RKBIN}/*_loader_*.bin ./
570}
571
572pack_trust_image()
573{
574	local TOS TOS_TA DARM_BASE TEE_LOAD_ADDR TEE_OFFSET=0x8400000
575
576	# ARM64 uses trust_merger
577	if grep -Eq ''^CONFIG_ARM64=y'|'^CONFIG_ARM64_BOOT_AARCH32=y'' ${OUTDIR}/.config ; then
578		if [ ! -f ${RKBIN}/RKTRUST/${RKCHIP_TRUST}${PLATFORM_AARCH32}TRUST.ini ]; then
579			echo "pack trust failed! Can't find: ${RKBIN}/RKTRUST/${RKCHIP_TRUST}${PLATFORM_AARCH32}TRUST.ini"
580			return
581		fi
582
583		cd ${RKBIN}
584		${RKTOOLS}/trust_merger ${PLATFORM_SHA} ${PLATFORM_RSA} ${PLATFORM_TRUST_IMG_SIZE} ${BIN_PATH_FIXUP} \
585					${PACK_IGNORE_BL32} ${RKBIN}/RKTRUST/${RKCHIP_TRUST}${PLATFORM_AARCH32}TRUST.ini
586
587		cd - && mv ${RKBIN}/trust.img ./trust.img
588		echo "pack trust okay! Input: ${RKBIN}/RKTRUST/${RKCHIP_TRUST}${PLATFORM_AARCH32}TRUST.ini"
589	# ARM uses loaderimage
590	else
591		if [ ! -f ${RKBIN}/RKTRUST/${RKCHIP_TRUST}TOS.ini ]; then
592			echo "pack trust failed! Can't find: ${RKBIN}/RKTRUST/${RKCHIP_TRUST}TOS.ini"
593			return
594		fi
595
596		# OP-TEE is 132M(0x8400000) offset from DRAM base.
597		DARM_BASE=`sed -n "/CONFIG_SYS_SDRAM_BASE=/s/CONFIG_SYS_SDRAM_BASE=//p" ${OUTDIR}/include/autoconf.mk|tr -d '\r'`
598		TEE_LOAD_ADDR=$((DARM_BASE+TEE_OFFSET))
599
600		# Convert Dec to Hex
601		TEE_LOAD_ADDR=$(echo "obase=16;${TEE_LOAD_ADDR}"|bc)
602
603		# Parse orignal path
604		TOS=`sed -n "/TOS=/s/TOS=//p" ${RKBIN}/RKTRUST/${RKCHIP_TRUST}TOS.ini|tr -d '\r'`
605		TOS_TA=`sed -n "/TOSTA=/s/TOSTA=//p" ${RKBIN}/RKTRUST/${RKCHIP_TRUST}TOS.ini|tr -d '\r'`
606
607		# replace "./tools/rk_tools/" with "./" to compatible legacy ini content of rkdevelop branch
608		TOS=$(echo ${TOS} | sed "s/tools\/rk_tools\//\.\//g")
609		TOS_TA=$(echo ${TOS_TA} | sed "s/tools\/rk_tools\//\.\//g")
610
611		if [ $TOS_TA ]; then
612			${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS_TA} ./trust.img ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE}
613		elif [ $TOS ]; then
614			${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS}    ./trust.img ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE}
615		else
616			echo "Can't find any tee bin"
617			exit 1
618		fi
619
620		echo "pack trust okay! Input: ${RKBIN}/RKTRUST/${RKCHIP_TRUST}TOS.ini"
621	fi
622}
623
624finish()
625{
626	echo
627	if [ "$BOARD" = '' ]; then
628		echo "Platform ${RKCHIP_LABEL}${PLATFORM_AARCH32} is build OK, with exist .config"
629	else
630		echo "Platform ${RKCHIP_LABEL}${PLATFORM_AARCH32} is build OK, with new .config(make ${BOARD}_defconfig)"
631	fi
632}
633
634prepare
635select_toolchain
636select_chip_info
637fixup_platform_configure
638sub_commands
639make CROSS_COMPILE=${TOOLCHAIN_GCC}  all --jobs=${JOB} ${OUTOPT}
640pack_uboot_image
641pack_loader_image
642pack_trust_image
643finish
644