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