xref: /rk3399_rockchip-uboot/make.sh (revision 6037440f1237dd5604f31ae36a5eb2f742c31021)
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()
62OPTION=
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|EXT_DTB=<file>]"
78	echo
79	echo "	 - board:   board name of defconfig"
80	echo "	 - subcmd:  |elf*|loader*|spl*|itb|trust*|uboot|map|sym|<addr>|EXT_DTB=*"
81	echo "	 - ini:     assigned ini file to pack trust/loader"
82	echo
83	echo "Output:"
84	echo "	 When board built okay, there are uboot/trust/loader images in current directory"
85	echo
86	echo "Example:"
87	echo
88	echo "1. Build:"
89	echo "	./make.sh evb-rk3399               --- build for evb-rk3399_defconfig"
90	echo "	./make.sh firefly-rk3288           --- build for firefly-rk3288_defconfig"
91	echo "	./make.sh EXT_DTB=rk-kernel.dtb    --- build with exist .config and external dtb"
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|EXT_DTB=*)
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|EXT_DTB=*)
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 ${OPTION}
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		EXT_DTB=*)
280		OPTION=${SUBCMD}
281		;;
282
283		*)
284		# Search function and code position of address
285		RELOC_OFF=${FUNCADDR#*-}
286		FUNCADDR=${FUNCADDR%-*}
287		if [ -z $(echo ${FUNCADDR} | sed 's/[0-9,a-f,A-F,x,X,-]//g') ] && [ ${FUNCADDR} ]; then
288			# With prefix: '0x' or '0X'
289			if [ `echo ${FUNCADDR} | sed -n "/0[x,X]/p" | wc -l` -ne 0 ]; then
290				FUNCADDR=`echo $FUNCADDR | awk '{ print strtonum($0) }'`
291				FUNCADDR=`echo "obase=16;${FUNCADDR}"|bc |tr '[A-Z]' '[a-z]'`
292			fi
293			if [ `echo ${RELOC_OFF} | sed -n "/0[x,X]/p" | wc -l` -ne 0 ] && [ ${RELOC_OFF} ]; then
294				RELOC_OFF=`echo $RELOC_OFF | awk '{ print strtonum($0) }'`
295				RELOC_OFF=`echo "obase=16;${RELOC_OFF}"|bc |tr '[A-Z]' '[a-z]'`
296			fi
297
298			# If reloc address is assigned, do sub
299			if [ "${FUNCADDR}" != "${RELOC_OFF}" ]; then
300				# Hex -> Dec -> SUB -> Hex
301				FUNCADDR=`echo $((16#${FUNCADDR}))`
302				RELOC_OFF=`echo $((16#${RELOC_OFF}))`
303				FUNCADDR=$((FUNCADDR-RELOC_OFF))
304				FUNCADDR=$(echo "obase=16;${FUNCADDR}"|bc |tr '[A-Z]' '[a-z]')
305			fi
306
307			echo
308			sed -n "/${FUNCADDR}/p" ${sym}
309			${TOOLCHAIN_ADDR2LINE} -e ${elf} ${FUNCADDR}
310			exit 0
311		fi
312		;;
313	esac
314}
315
316# We select chip info to do:
317#	1. RKCHIP: fixup platform configure
318#	2. RKCHIP_LOADER: search ini file to pack loader
319#	3. RKCHIP_TRUST: search ini file to pack trust
320#	4. RKCHIP_LABEL: show build message
321#
322# We read chip info from .config and 'RKCHIP_INI_DESC'
323select_chip_info()
324{
325	local target_board item value
326
327	# Read RKCHIP firstly from .config
328	# The regular expression that matching:
329	#  - PX30, PX3SE
330	#  - RK????, RK????X
331	#  - RV????
332	local chip_reg='^CONFIG_ROCKCHIP_[R,P][X,V,K][0-9ESX]{1,5}'
333	count=`egrep -c ${chip_reg} .config`
334	# Obtain the matching only
335	RKCHIP=`egrep -o ${chip_reg} .config`
336
337	if [ $count -eq 1 ]; then
338		RKCHIP=${RKCHIP##*_}
339		grep '^CONFIG_ROCKCHIP_RK3368=y' .config >/dev/null \
340			&& RKCHIP=RK3368H
341		grep '^CONFIG_ROCKCHIP_RV1108=y' .config >/dev/null \
342			&& RKCHIP=RV110X
343		grep '^CONFIG_ROCKCHIP_RV1126=y' .config >/dev/null \
344			&& RKCHIP=RV1126
345	elif [ $count -gt 1 ]; then
346		# Grep the RK CHIP variant
347		grep '^CONFIG_ROCKCHIP_PX3SE=y' .config > /dev/null \
348			&& RKCHIP=PX3SE
349		grep '^CONFIG_ROCKCHIP_RK3126=y' .config >/dev/null \
350			&& RKCHIP=RK3126
351		grep '^CONFIG_ROCKCHIP_RK3326=y' .config >/dev/null \
352			&& RKCHIP=RK3326
353		grep '^CONFIG_ROCKCHIP_RK3128X=y' .config >/dev/null \
354			&& RKCHIP=RK3128X
355		grep '^CONFIG_ROCKCHIP_PX5=y' .config >/dev/null \
356			&& RKCHIP=PX5
357		grep '^CONFIG_ROCKCHIP_RK3399PRO=y' .config >/dev/null \
358			&& RKCHIP=RK3399PRO
359		grep '^CONFIG_ROCKCHIP_RK1806=y' .config >/dev/null \
360			&& RKCHIP=RK1806
361	else
362		echo "Can't get Rockchip SoC definition in .config"
363		exit 1
364	fi
365
366	# Default use RKCHIP
367	RKCHIP_LABEL=${RKCHIP}
368	RKCHIP_LOADER=${RKCHIP}
369	RKCHIP_TRUST=${RKCHIP}
370
371	# Read from RKCHIP_INI_DESC
372	for item in "${RKCHIP_INI_DESC[@]}"
373	do
374		target_board=`echo $item | awk '{ print $1 }'`
375		if grep  -q "^${target_board}=y" .config ; then
376			value=`echo $item | awk '{ print $2 }'`
377			if [ "$value" != "NA" ]; then
378				RKCHIP_LABEL=${value};
379			fi
380			value=`echo $item | awk '{ print $3 }'`
381			if [ "$value" != "NA" ]; then
382				RKCHIP_LOADER=${value};
383			fi
384			value=`echo $item | awk '{ print $4 }'`
385			if [ "$value" != "NA" ]; then
386				RKCHIP_TRUST=${value};
387			fi
388		fi
389	done
390}
391
392# Fixup platform special configure
393#	1. fixup pack mode;
394#	2. fixup image size
395#	3. fixup ARM64 cpu boot with AArch32
396fixup_platform_configure()
397{
398	local count plat
399
400# <*> Fixup rsa/sha pack mode for platforms
401	# RK3308/PX30/RK3326/RK1808 use RSA-PKCS1 V2.1, it's pack magic is "3"
402	if [ $RKCHIP = "PX30" -o $RKCHIP = "RK3326" -o $RKCHIP = "RK3308" -o $RKCHIP = "RK1808" ]; then
403		PLATFORM_RSA="--rsa 3"
404	# RK3368 use rk big endian SHA256, it's pack magic is "2"
405	elif [ $RKCHIP = "RK3368" -o $RKCHIP = "RK3368H" ]; then
406		PLATFORM_SHA="--sha 2"
407	# other platforms use default configure
408	fi
409
410# <*> Fixup images size pack for platforms
411	if [ $RKCHIP = "RK3308" ]; then
412		if grep -q '^CONFIG_ARM64_BOOT_AARCH32=y' .config ; then
413			PLATFORM_UBOOT_IMG_SIZE="--size 512 2"
414			PLATFORM_TRUST_IMG_SIZE="--size 512 2"
415		else
416			PLATFORM_UBOOT_IMG_SIZE="--size 1024 2"
417			PLATFORM_TRUST_IMG_SIZE="--size 1024 2"
418		fi
419	elif [ $RKCHIP = "RK1808" ]; then
420		PLATFORM_UBOOT_IMG_SIZE="--size 1024 2"
421		PLATFORM_TRUST_IMG_SIZE="--size 1024 2"
422	elif [ $RKCHIP = "RK3036" ]; then
423		PLATFORM_UBOOT_IMG_SIZE="--size 512 1"
424		PLATFORM_TRUST_IMG_SIZE="--size 512 1"
425	fi
426
427# <*> Fixup AARCH32 for ARM64 cpu platforms
428	if grep -q '^CONFIG_ARM64_BOOT_AARCH32=y' .config ; then
429		if [ $RKCHIP = "RK3308" ]; then
430			RKCHIP_LABEL=${RKCHIP_LABEL}"AARCH32"
431			RKCHIP_TRUST=${RKCHIP_TRUST}"AARCH32"
432		elif [ $RKCHIP = "RK3326" ]; then
433			RKCHIP_LABEL=${RKCHIP_LABEL}"AARCH32"
434			RKCHIP_LOADER=${RKCHIP_LOADER}"AARCH32"
435		fi
436	fi
437}
438
439pack_uboot_image()
440{
441	local UBOOT_LOAD_ADDR UBOOT_MAX_KB UBOOT_KB HEAD_KB=2
442
443	# Check file size
444	UBOOT_KB=`ls -l u-boot.bin | awk '{print $5}'`
445	if [ "$PLATFORM_UBOOT_IMG_SIZE" = "" ]; then
446		UBOOT_MAX_KB=1046528
447	else
448		UBOOT_MAX_KB=`echo $PLATFORM_UBOOT_IMG_SIZE | awk '{print strtonum($2)}'`
449		UBOOT_MAX_KB=$(((UBOOT_MAX_KB-HEAD_KB)*1024))
450	fi
451
452	if [ $UBOOT_KB -gt $UBOOT_MAX_KB ]; then
453		echo
454		echo "ERROR: pack uboot failed! u-boot.bin actual: $UBOOT_KB bytes, max limit: $UBOOT_MAX_KB bytes"
455		exit 1
456	fi
457
458	# Pack image
459	UBOOT_LOAD_ADDR=`sed -n "/CONFIG_SYS_TEXT_BASE=/s/CONFIG_SYS_TEXT_BASE=//p" include/autoconf.mk|tr -d '\r'`
460	if [ ! $UBOOT_LOAD_ADDR ]; then
461		UBOOT_LOAD_ADDR=`sed -n "/CONFIG_SYS_TEXT_BASE=/s/CONFIG_SYS_TEXT_BASE=//p" .config|tr -d '\r'`
462	fi
463
464	${RKTOOLS}/loaderimage --pack --uboot u-boot.bin uboot.img ${UBOOT_LOAD_ADDR} ${PLATFORM_UBOOT_IMG_SIZE}
465
466	# Delete u-boot.img and u-boot-dtb.img, which makes users not be confused with final uboot.img
467	ls u-boot.img >/dev/null 2>&1 && rm u-boot.img -rf
468	ls u-boot-dtb.img >/dev/null 2>&1 && rm u-boot-dtb.img -rf
469	echo "pack uboot okay! Input: u-boot.bin"
470}
471
472pack_uboot_itb_image()
473{
474	local ini
475
476	# ARM64
477	if grep -Eq ''^CONFIG_ARM64=y'|'^CONFIG_ARM64_BOOT_AARCH32=y'' .config ; then
478		ini=${RKBIN}/RKTRUST/${RKCHIP_TRUST}${PLATFORM_AARCH32}TRUST.ini
479		if [ ! -f ${ini} ]; then
480			echo "pack trust failed! Can't find: ${ini}"
481			return
482		fi
483
484		bl31=`sed -n '/_bl31_/s/PATH=//p' ${ini} |tr -d '\r'`
485
486		cp ${RKBIN}/${bl31} bl31.elf
487		make CROSS_COMPILE=${TOOLCHAIN_GCC} u-boot.itb
488		echo "pack u-boot.itb okay! Input: ${ini}"
489	else
490		ini=${RKBIN}/RKTRUST/${RKCHIP_TRUST}TOS.ini
491		if [ ! -f ${ini} ]; then
492			echo "pack trust failed! Can't find: ${ini}"
493			return
494		fi
495
496		TOS=`sed -n "/TOS=/s/TOS=//p" ${ini} |tr -d '\r'`
497		TOS_TA=`sed -n "/TOSTA=/s/TOSTA=//p" ${ini} |tr -d '\r'`
498
499		if [ $TOS_TA ]; then
500			cp ${RKBIN}/${TOS_TA} tee.bin
501		elif [ $TOS ]; then
502			cp ${RKBIN}/${TOS} tee.bin
503		else
504			echo "Can't find any tee bin"
505			exit 1
506		fi
507
508		make CROSS_COMPILE=${TOOLCHAIN_GCC} u-boot.itb
509		echo "pack u-boot.itb okay! Input: ${ini}"
510	fi
511}
512
513pack_spl_loader_image()
514{
515	local header label="SPL" mode=$1
516	local ini=${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini
517	local temp_ini=${RKBIN}/.temp/${RKCHIP_LOADER}MINIALL.ini
518
519	if [ "$FILE" != "" ]; then
520		ini=$FILE;
521	fi
522
523	if [ ! -f ${ini} ]; then
524		echo "pack TPL+SPL loader failed! Can't find: ${ini}"
525		return
526	fi
527
528	ls ${RKBIN}/.temp >/dev/null 2>&1 && rm ${RKBIN}/.temp -rf
529	mkdir ${RKBIN}/.temp
530
531	# Copy to .temp folder
532	cp spl/u-boot-spl.bin ${RKBIN}/.temp/
533	cp ${ini} ${RKBIN}/.temp/${RKCHIP_LOADER}MINIALL.ini -f
534
535	cd ${RKBIN}
536	if [ "$mode" = 'spl' ]; then	# pack tpl+spl
537		cp tpl/u-boot-tpl.bin ${RKBIN}/.temp/
538		# Update ini
539		label="TPL+SPL"
540		header=`sed -n '/NAME=/s/NAME=//p' ${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini`
541		dd if=${RKBIN}/.temp/u-boot-tpl.bin of=${RKBIN}/.temp/tpl.bin bs=1 skip=4
542		sed -i "1s/^/${header:0:4}/" ${RKBIN}/.temp/tpl.bin
543		sed -i "s/FlashData=.*$/FlashData=.\/.temp\/tpl.bin/"     ${temp_ini}
544	fi
545
546	sed -i "s/FlashBoot=.*$/FlashBoot=.\/.temp\/u-boot-spl.bin/"  ${temp_ini}
547
548	${RKTOOLS}/boot_merger ${BIN_PATH_FIXUP} ${temp_ini}
549	rm ${RKBIN}/.temp -rf
550	cd -
551	ls *_loader_*.bin >/dev/null 2>&1 && rm *_loader_*.bin
552
553	RKCHIP_LOWCASE=`echo ${RKCHIP} |tr '[A-Z]' '[a-z]'`
554	mv ${RKBIN}/*_loader_*.bin ./${RKCHIP_LOWCASE}_loader_spl.bin
555	echo "pack loader(${label}) okay! Input: ${ini}"
556	ls ./${RKCHIP_LOWCASE}_loader_spl.bin
557}
558
559pack_loader_image()
560{
561	local ini=${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini
562
563	if [ "$FILE" != "" ]; then
564		ini=$FILE;
565	fi
566
567	if [ ! -f $ini ]; then
568		echo "pack loader failed! Can't find: $ini"
569		return
570	fi
571
572	ls *_loader_*.bin >/dev/null 2>&1 && rm *_loader_*.bin
573
574	numline=`cat $ini | wc -l`
575	if [ $numline -eq 1 ]; then
576		image=`sed -n "/PATH=/p" $ini | tr -d '\r' | cut -d '=' -f 2`
577		cp ${RKBIN}/${image} ./
578		echo "pack trust okay! Input: ${ini}"
579		return;
580	fi
581
582	cd ${RKBIN}
583
584	${RKTOOLS}/boot_merger ${BIN_PATH_FIXUP} $ini
585	echo "pack loader okay! Input: $ini"
586
587	cd - && mv ${RKBIN}/*_loader_*.bin ./
588}
589
590pack_32bit_trust_image()
591{
592	local ini=$1 TOS TOS_TA DARM_BASE TEE_LOAD_ADDR TEE_OUTPUT TEE_OFFSET
593
594	if [ ! -f ${ini} ]; then
595		echo "pack trust failed! Can't find: ${ini}"
596		return
597	fi
598
599	# Parse orignal path
600	TOS=`sed -n "/TOS=/s/TOS=//p" ${ini} |tr -d '\r'`
601	TOS_TA=`sed -n "/TOSTA=/s/TOSTA=//p" ${ini} |tr -d '\r'`
602
603	# Parse address and output name
604	TEE_OUTPUT=`sed -n "/OUTPUT=/s/OUTPUT=//p" ${ini} |tr -d '\r'`
605	if [ "$TEE_OUTPUT" = "" ]; then
606		TEE_OUTPUT="./trust.img"
607	fi
608	TEE_OFFSET=`sed -n "/ADDR=/s/ADDR=//p" ${ini} |tr -d '\r'`
609	if [ "$TEE_OFFSET" = "" ]; then
610		TEE_OFFSET=0x8400000
611	fi
612
613	# OP-TEE is 132M(0x8400000) offset from DRAM base.
614	DARM_BASE=`sed -n "/CONFIG_SYS_SDRAM_BASE=/s/CONFIG_SYS_SDRAM_BASE=//p" include/autoconf.mk|tr -d '\r'`
615	TEE_LOAD_ADDR=$((DARM_BASE+TEE_OFFSET))
616
617	# Convert Dec to Hex
618	TEE_LOAD_ADDR=$(echo "obase=16;${TEE_LOAD_ADDR}"|bc)
619
620	# Replace "./tools/rk_tools/" with "./" to compatible legacy ini content of rkdevelop branch
621	TOS=$(echo ${TOS} | sed "s/tools\/rk_tools\//\.\//g")
622	TOS_TA=$(echo ${TOS_TA} | sed "s/tools\/rk_tools\//\.\//g")
623
624	if [ $TOS_TA ]; then
625		${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS_TA} ${TEE_OUTPUT} ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE}
626	elif [ $TOS ]; then
627		${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS}    ${TEE_OUTPUT} ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE}
628	else
629		echo "Can't find any tee bin"
630		exit 1
631	fi
632
633	echo "pack trust okay! Input: ${ini}"
634	echo
635}
636
637pack_64bit_trust_image()
638{
639	local ini=$1
640
641	if [ ! -f ${ini} ]; then
642		echo "pack trust failed! Can't find: ${ini}"
643		return
644	fi
645
646	cd ${RKBIN}
647	${RKTOOLS}/trust_merger ${PLATFORM_SHA} ${PLATFORM_RSA} ${PLATFORM_TRUST_IMG_SIZE} ${BIN_PATH_FIXUP} \
648				${PACK_IGNORE_BL32} ${ini}
649
650	cd - && mv ${RKBIN}/trust*.img ./
651	echo "pack trust okay! Input: ${ini}"
652	echo
653}
654
655pack_trust_image()
656{
657	local ini
658
659	ls trust*.img >/dev/null 2>&1 && rm trust*.img
660
661	# ARM64 uses trust_merger
662	if grep -Eq ''^CONFIG_ARM64=y'|'^CONFIG_ARM64_BOOT_AARCH32=y'' .config ; then
663		ini=${RKBIN}/RKTRUST/${RKCHIP_TRUST}TRUST.ini
664		if [ "$FILE" != "" ]; then
665			ini=$FILE;
666		fi
667
668		numline=`cat $ini | wc -l`
669		if [ $numline -eq 1 ]; then
670			image=`sed -n "/PATH=/p" $ini | tr -d '\r' | cut -d '=' -f 2`
671			cp ${RKBIN}/${image} ./trust.img
672			echo "pack trust okay! Input: ${ini}"
673			return;
674		fi
675		pack_64bit_trust_image ${ini}
676	# ARM uses loaderimage
677	else
678		ini=${RKBIN}/RKTRUST/${RKCHIP_TRUST}TOS.ini
679		if [ "$FILE" != "" ]; then
680			ini=$FILE;
681		fi
682		pack_32bit_trust_image ${ini}
683	fi
684}
685
686finish()
687{
688	echo
689	if [ ! -z "$OPTION" ]; then
690		echo "Platform ${RKCHIP_LABEL} is build OK, with exist .config ($OPTION)"
691	elif [ "$BOARD" = '' ]; then
692		echo "Platform ${RKCHIP_LABEL} is build OK, with exist .config"
693	else
694		echo "Platform ${RKCHIP_LABEL} is build OK, with new .config(make ${BOARD}_defconfig)"
695	fi
696}
697
698prepare
699select_toolchain
700select_chip_info
701fixup_platform_configure
702sub_commands
703make CROSS_COMPILE=${TOOLCHAIN_GCC} ${OPTION} all --jobs=${JOB}
704pack_uboot_image
705pack_loader_image
706pack_trust_image
707finish
708