xref: /rk3399_rockchip-uboot/make.sh (revision fcccd23a111deef8f2fec6540edc77dd50fbe392)
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" ]; 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	fi
411
412# <*> Fixup AARCH32 for ARM64 cpu platforms
413	if grep -q '^CONFIG_ARM64_BOOT_AARCH32=y' .config ; then
414		if [ $RKCHIP = "RK3308" ]; then
415			RKCHIP_LABEL=${RKCHIP_LABEL}"AARCH32"
416			RKCHIP_TRUST=${RKCHIP_TRUST}"AARCH32"
417		elif [ $RKCHIP = "RK3326" ]; then
418			RKCHIP_LABEL=${RKCHIP_LABEL}"AARCH32"
419			RKCHIP_LOADER=${RKCHIP_LOADER}"AARCH32"
420		fi
421	fi
422}
423
424pack_uboot_image()
425{
426	local UBOOT_LOAD_ADDR UBOOT_MAX_KB UBOOT_KB HEAD_KB=2
427
428	# Check file size
429	UBOOT_KB=`ls -l u-boot.bin | awk '{print $5}'`
430	if [ "$PLATFORM_UBOOT_IMG_SIZE" = "" ]; then
431		UBOOT_MAX_KB=1046528
432	else
433		UBOOT_MAX_KB=`echo $PLATFORM_UBOOT_IMG_SIZE | awk '{print strtonum($2)}'`
434		UBOOT_MAX_KB=$(((UBOOT_MAX_KB-HEAD_KB)*1024))
435	fi
436
437	if [ $UBOOT_KB -gt $UBOOT_MAX_KB ]; then
438		echo
439		echo "ERROR: pack uboot failed! u-boot.bin actual: $UBOOT_KB bytes, max limit: $UBOOT_MAX_KB bytes"
440		exit 1
441	fi
442
443	# Pack image
444	UBOOT_LOAD_ADDR=`sed -n "/CONFIG_SYS_TEXT_BASE=/s/CONFIG_SYS_TEXT_BASE=//p" include/autoconf.mk|tr -d '\r'`
445	if [ ! $UBOOT_LOAD_ADDR ]; then
446		UBOOT_LOAD_ADDR=`sed -n "/CONFIG_SYS_TEXT_BASE=/s/CONFIG_SYS_TEXT_BASE=//p" .config|tr -d '\r'`
447	fi
448
449	${RKTOOLS}/loaderimage --pack --uboot u-boot.bin uboot.img ${UBOOT_LOAD_ADDR} ${PLATFORM_UBOOT_IMG_SIZE}
450
451	# Delete u-boot.img and u-boot-dtb.img, which makes users not be confused with final uboot.img
452	ls u-boot.img >/dev/null 2>&1 && rm u-boot.img -rf
453	ls u-boot-dtb.img >/dev/null 2>&1 && rm u-boot-dtb.img -rf
454	echo "pack uboot okay! Input: u-boot.bin"
455}
456
457pack_uboot_itb_image()
458{
459	local ini
460
461	# ARM64
462	if grep -Eq ''^CONFIG_ARM64=y'|'^CONFIG_ARM64_BOOT_AARCH32=y'' .config ; then
463		ini=${RKBIN}/RKTRUST/${RKCHIP_TRUST}${PLATFORM_AARCH32}TRUST.ini
464		if [ ! -f ${ini} ]; then
465			echo "pack trust failed! Can't find: ${ini}"
466			return
467		fi
468
469		bl31=`sed -n '/_bl31_/s/PATH=//p' ${ini} |tr -d '\r'`
470
471		cp ${RKBIN}/${bl31} bl31.elf
472		make CROSS_COMPILE=${TOOLCHAIN_GCC} u-boot.itb
473		echo "pack u-boot.itb okay! Input: ${ini}"
474	else
475		ini=${RKBIN}/RKTRUST/${RKCHIP_TRUST}TOS.ini
476		if [ ! -f ${ini} ]; then
477			echo "pack trust failed! Can't find: ${ini}"
478			return
479		fi
480
481		TOS=`sed -n "/TOS=/s/TOS=//p" ${ini} |tr -d '\r'`
482		TOS_TA=`sed -n "/TOSTA=/s/TOSTA=//p" ${ini} |tr -d '\r'`
483
484		if [ $TOS_TA ]; then
485			cp ${RKBIN}/${TOS_TA} tee.bin
486		elif [ $TOS ]; then
487			cp ${RKBIN}/${TOS} tee.bin
488		else
489			echo "Can't find any tee bin"
490			exit 1
491		fi
492
493		make CROSS_COMPILE=${TOOLCHAIN_GCC} u-boot.itb
494		echo "pack u-boot.itb okay! Input: ${ini}"
495	fi
496}
497
498pack_spl_loader_image()
499{
500	local header label="SPL" mode=$1
501	local ini=${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini
502	local temp_ini=${RKBIN}/.temp/${RKCHIP_LOADER}MINIALL.ini
503
504	if [ "$FILE" != "" ]; then
505		ini=$FILE;
506	fi
507
508	if [ ! -f ${ini} ]; then
509		echo "pack TPL+SPL loader failed! Can't find: ${ini}"
510		return
511	fi
512
513	ls ${RKBIN}/.temp >/dev/null 2>&1 && rm ${RKBIN}/.temp -rf
514	mkdir ${RKBIN}/.temp
515
516	# Copy to .temp folder
517	cp spl/u-boot-spl.bin ${RKBIN}/.temp/
518	cp tpl/u-boot-tpl.bin ${RKBIN}/.temp/
519	cp ${ini} ${RKBIN}/.temp/${RKCHIP_LOADER}MINIALL.ini -f
520
521	cd ${RKBIN}
522	if [ "$mode" = 'spl' ]; then	# pack tpl+spl
523		# Update ini
524		label="TPL+SPL"
525		header=`sed -n '/NAME=/s/NAME=//p' ${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini`
526		dd if=${RKBIN}/.temp/u-boot-tpl.bin of=${RKBIN}/.temp/tpl.bin bs=1 skip=4
527		sed -i "1s/^/${header:0:4}/" ${RKBIN}/.temp/tpl.bin
528		sed -i "s/FlashData=.*$/FlashData=.\/.temp\/tpl.bin/"     ${temp_ini}
529	fi
530
531	sed -i "s/FlashBoot=.*$/FlashBoot=.\/.temp\/u-boot-spl.bin/"  ${temp_ini}
532
533	${RKTOOLS}/boot_merger ${BIN_PATH_FIXUP} ${temp_ini}
534	rm ${RKBIN}/.temp -rf
535	cd -
536	ls *_loader_*.bin >/dev/null 2>&1 && rm *_loader_*.bin
537	mv ${RKBIN}/*_loader_*.bin ./
538	echo "pack loader(${label}) okay! Input: ${ini}"
539	ls ./*_loader_*.bin
540}
541
542pack_loader_image()
543{
544	local ini=${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini
545
546	if [ "$FILE" != "" ]; then
547		ini=$FILE;
548	fi
549
550	if [ ! -f $ini ]; then
551		echo "pack loader failed! Can't find: $ini"
552		return
553	fi
554
555	ls *_loader_*.bin >/dev/null 2>&1 && rm *_loader_*.bin
556
557	numline=`cat $ini | wc -l`
558	if [ $numline -eq 1 ]; then
559		image=`sed -n "/PATH=/p" $ini | tr -d '\r' | cut -d '=' -f 2`
560		cp ${RKBIN}/${image} ./
561		echo "pack trust okay! Input: ${ini}"
562		return;
563	fi
564
565	cd ${RKBIN}
566
567	${RKTOOLS}/boot_merger ${BIN_PATH_FIXUP} $ini
568	echo "pack loader okay! Input: $ini"
569
570	cd - && mv ${RKBIN}/*_loader_*.bin ./
571}
572
573pack_32bit_trust_image()
574{
575	local ini=$1 TOS TOS_TA DARM_BASE TEE_LOAD_ADDR TEE_OUTPUT TEE_OFFSET
576
577	if [ ! -f ${ini} ]; then
578		echo "pack trust failed! Can't find: ${ini}"
579		return
580	fi
581
582	# Parse orignal path
583	TOS=`sed -n "/TOS=/s/TOS=//p" ${ini} |tr -d '\r'`
584	TOS_TA=`sed -n "/TOSTA=/s/TOSTA=//p" ${ini} |tr -d '\r'`
585
586	# Parse address and output name
587	TEE_OUTPUT=`sed -n "/OUTPUT=/s/OUTPUT=//p" ${ini} |tr -d '\r'`
588	if [ "$TEE_OUTPUT" = "" ]; then
589		TEE_OUTPUT="./trust.img"
590	fi
591	TEE_OFFSET=`sed -n "/ADDR=/s/ADDR=//p" ${ini} |tr -d '\r'`
592	if [ "$TEE_OFFSET" = "" ]; then
593		TEE_OFFSET=0x8400000
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" 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	# Replace "./tools/rk_tools/" with "./" to compatible legacy ini content of rkdevelop branch
604	TOS=$(echo ${TOS} | sed "s/tools\/rk_tools\//\.\//g")
605	TOS_TA=$(echo ${TOS_TA} | sed "s/tools\/rk_tools\//\.\//g")
606
607	if [ $TOS_TA ]; then
608		${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS_TA} ${TEE_OUTPUT} ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE}
609	elif [ $TOS ]; then
610		${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS}    ${TEE_OUTPUT} ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE}
611	else
612		echo "Can't find any tee bin"
613		exit 1
614	fi
615
616	echo "pack trust okay! Input: ${ini}"
617	echo
618}
619
620pack_64bit_trust_image()
621{
622	local ini=$1
623
624	if [ ! -f ${ini} ]; then
625		echo "pack trust failed! Can't find: ${ini}"
626		return
627	fi
628
629	cd ${RKBIN}
630	${RKTOOLS}/trust_merger ${PLATFORM_SHA} ${PLATFORM_RSA} ${PLATFORM_TRUST_IMG_SIZE} ${BIN_PATH_FIXUP} \
631				${PACK_IGNORE_BL32} ${ini}
632
633	cd - && mv ${RKBIN}/trust*.img ./
634	echo "pack trust okay! Input: ${ini}"
635	echo
636}
637
638pack_trust_image()
639{
640	local ini
641
642	ls trust*.img >/dev/null 2>&1 && rm trust*.img
643
644	# ARM64 uses trust_merger
645	if grep -Eq ''^CONFIG_ARM64=y'|'^CONFIG_ARM64_BOOT_AARCH32=y'' .config ; then
646		ini=${RKBIN}/RKTRUST/${RKCHIP_TRUST}TRUST.ini
647		if [ "$FILE" != "" ]; then
648			ini=$FILE;
649		fi
650
651		numline=`cat $ini | wc -l`
652		if [ $numline -eq 1 ]; then
653			image=`sed -n "/PATH=/p" $ini | tr -d '\r' | cut -d '=' -f 2`
654			cp ${RKBIN}/${image} ./trust.img
655			echo "pack trust okay! Input: ${ini}"
656			return;
657		fi
658		pack_64bit_trust_image ${ini}
659	# ARM uses loaderimage
660	else
661		ini=${RKBIN}/RKTRUST/${RKCHIP_TRUST}TOS.ini
662		if [ "$FILE" != "" ]; then
663			ini=$FILE;
664		fi
665		pack_32bit_trust_image ${ini}
666	fi
667}
668
669finish()
670{
671	echo
672	if [ "$BOARD" = '' ]; then
673		echo "Platform ${RKCHIP_LABEL} is build OK, with exist .config"
674	else
675		echo "Platform ${RKCHIP_LABEL} is build OK, with new .config(make ${BOARD}_defconfig)"
676	fi
677}
678
679prepare
680select_toolchain
681select_chip_info
682fixup_platform_configure
683sub_commands
684make CROSS_COMPILE=${TOOLCHAIN_GCC}  all --jobs=${JOB} ${OUTOPT}
685pack_uboot_image
686pack_loader_image
687pack_trust_image
688finish
689