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