xref: /rk3399_rockchip-uboot/make.sh (revision 9c00c79ad8a2faf57d33727dd542b5bd372c7bd1)
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
9JOB=`sed -n "N;/processor/p" /proc/cpuinfo|wc -l`
10SUPPORT_LIST=`ls configs/*[r,p][x,v,k][0-9][0-9]*_defconfig`
11
12# @LOADER: map to $RKCHIP_LOADER for loader ini
13# @TRUST:  map to $RKCHIP_TRUST for trust ini
14# @LABEL:  map to $RKCHIP_LEBEL for verbose message
15# @-:      default state/value
16CHIP_TYPE_FIXUP_TABLE=(
17	# CONFIG_XXX                         RKCHIP         LOADER       TRUST         LABEL
18	"CONFIG_ROCKCHIP_RK3368              RK3368H         -            -             -"
19	"CONFIG_ROCKCHIP_RV1108              RV110X          -            -             -"
20	"CONFIG_ROCKCHIP_PX3SE               PX3SE           -            -             -"
21	"CONFIG_ROCKCHIP_RK3126              RK3126          -            -             -"
22	"CONFIG_ROCKCHIP_RK3326              RK3326          -            -             -"
23	"CONFIG_ROCKCHIP_RK3128X             RK3128X         -            -             -"
24	"CONFIG_ROCKCHIP_PX5                 PX5             -            -             -"
25	"CONFIG_ROCKCHIP_RK3399PRO           RK3399PRO       -            -             -"
26	"CONFIG_ROCKCHIP_RK1806              RK1806          -            -             -"
27	"CONFIG_TARGET_GVA_RK3229            RK322X          RK322XAT     -             -"
28	"CONFIG_COPROCESSOR_RK1808           RKNPU-LION      RKNPULION    RKNPULION     -"
29)
30
31# <*> Fixup rsa/sha pack mode for platforms
32#     RSA: RK3308/PX30/RK3326/RK1808 use RSA-PKCS1 V2.1, it's pack magic is "3", and others use default configure.
33#     SHA: RK3368 use rk big endian SHA256, it's pack magic is "2", and others use default configure.
34# <*> Fixup images size pack for platforms
35# <*> Fixup verbose message about AARCH32
36#
37# @RSA:     rsa mode
38# @SHA:     sha mode
39# @A64-KB:  arm64 platform image size: [uboot,trust]
40# @A64-NUM: arm64 platform image number of total: [uboot,trust]
41# @A32-KB:  arm32 platform image size: [uboot,trust]
42# @A32-NUM: arm32 platform image number of total: [uboot,trust]
43# @LOADER:  map to $RKCHIP_LOADER for loader ini
44# @TRUST:   map to $RKCHIP_TRUST for trust ini
45# @-:       default state/value
46CHIP_CFG_FIXUP_TABLE=(
47	# CONFIG_XXX              RSA     SHA     A64-KB      A64-NUM     A32-KB       A32-NUM      LOAER        TRUST
48	"CONFIG_ROCKCHIP_RK3368    -       2       -,-          -,-        -,-          -,-           -           -"
49	"CONFIG_ROCKCHIP_RK3036    -       -       512,512      1,1        -,-          -,-           -           -"
50	"CONFIG_ROCKCHIP_PX30      3       -       -,-          -,-        -,-          -,-           -           -"
51	"CONFIG_ROCKCHIP_RK3326    3       -       -,-          -,-        -,-          -,-           AARCH32     -"
52	"CONFIG_ROCKCHIP_RK3308    3       -       1024,1024    2,2        512,512      2,2           -           AARCH32"
53	"CONFIG_ROCKCHIP_RK1808    3       -       1024,1024    2,2        -,-          -,-           -           -"
54	"CONFIG_ROCKCHIP_RV1126    3       -       -,-          -,-        -,-          -,-           -           -"
55)
56
57########################################### User can modify #############################################
58# User's rkbin tool relative path
59RKBIN_TOOLS=../rkbin/tools
60
61# User's GCC toolchain and relative path
62ADDR2LINE_ARM32=arm-linux-gnueabihf-addr2line
63ADDR2LINE_ARM64=aarch64-linux-gnu-addr2line
64OBJ_ARM32=arm-linux-gnueabihf-objdump
65OBJ_ARM64=aarch64-linux-gnu-objdump
66GCC_ARM32=arm-linux-gnueabihf-
67GCC_ARM64=aarch64-linux-gnu-
68TOOLCHAIN_ARM32=../prebuilts/gcc/linux-x86/arm/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabihf/bin
69TOOLCHAIN_ARM64=../prebuilts/gcc/linux-x86/aarch64/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin
70
71########################################### User not touch #############################################
72RKTOOLS=./tools
73
74# Declare global INI file searching index name for every chip, update in select_chip_info()
75RKCHIP="-"
76RKCHIP_LABEL="-"
77RKCHIP_LOADER="-"
78RKCHIP_TRUST="-"
79
80INI_TRUST=
81INI_LOADER=
82
83# Declare rkbin repository path, updated in prepare()
84RKBIN=
85
86# Declare global toolchain path for CROSS_COMPILE, updated in select_toolchain()
87TOOLCHAIN_GCC=
88TOOLCHAIN_OBJDUMP=
89TOOLCHAIN_ADDR2LINE=
90
91# Declare global default output dir and cmd, update in prepare()
92OPTION=
93
94# Declare global plaform configure, updated in fixup_platform_configure()
95PLATFORM_RSA=
96PLATFORM_SHA=
97PLATFORM_UBOOT_IMG_SIZE=
98PLATFORM_TRUST_IMG_SIZE=
99
100IMAGE_FORMAT="RKFW"
101IMAGE_NOPACK="n"
102
103#########################################################################################################
104function help()
105{
106	echo
107	echo "Usage:"
108	echo "	./make.sh [board|sub-command]"
109	echo
110	echo "	 - board:        board name of defconfig"
111	echo "	 - sub-command:  elf*|loader*|spl*|itb|trust*|uboot|map|sym|<addr>|EXT_DTB=*"
112	echo "	 - ini:          assigned ini file to pack trust/loader"
113	echo
114	echo "Output:"
115	echo "	 When board built okay, there are uboot/trust/loader images in current directory"
116	echo
117	echo "Example:"
118	echo
119	echo "1. Build:"
120	echo "	./make.sh evb-rk3399               --- build for evb-rk3399_defconfig"
121	echo "	./make.sh firefly-rk3288           --- build for firefly-rk3288_defconfig"
122	echo "	./make.sh EXT_DTB=rk-kernel.dtb    --- build with exist .config and external dtb"
123	echo "	./make.sh                          --- build with exist .config"
124	echo "	./make.sh env                      --- build envtools"
125	echo
126	echo "2. Pack:"
127	echo "	./make.sh uboot                    --- pack uboot.img"
128	echo "	./make.sh trust                    --- pack trust.img"
129	echo "	./make.sh trust <ini>              --- pack trust img with assigned ini file"
130	echo "	./make.sh loader                   --- pack loader bin"
131	echo "	./make.sh loader <ini>             --- pack loader img with assigned ini file"
132	echo "	./make.sh spl                      --- pack loader with u-boot-spl.bin and u-boot-tpl.bin"
133	echo "	./make.sh spl-s                    --- pack loader only replace miniloader with u-boot-spl.bin"
134	echo "	./make.sh itb                      --- pack u-boot.itb(TODO: bl32 is not included for ARMv8)"
135	echo
136	echo "3. Debug:"
137	echo "	./make.sh elf                      --- dump elf file with -D(default)"
138	echo "	./make.sh elf-S                    --- dump elf file with -S"
139	echo "	./make.sh elf-d                    --- dump elf file with -d"
140	echo "	./make.sh elf-*                    --- dump elf file with -*"
141	echo "	./make.sh <no reloc_addr>          --- dump function symbol and code position of address(no relocated)"
142	echo "	./make.sh <reloc_addr-reloc_off>   --- dump function symbol and code position of address(relocated)"
143	echo "	./make.sh map                      --- cat u-boot.map"
144	echo "	./make.sh sym                      --- cat u-boot.sym"
145}
146
147function prepare()
148{
149	if [ -d ${RKBIN_TOOLS} ]; then
150		absolute_path=$(cd `dirname ${RKBIN_TOOLS}`; pwd)
151		RKBIN=${absolute_path}
152	else
153		echo "ERROR: No ../rkbin repository"
154		exit 1
155	fi
156
157	if grep -Eq ''^CONFIG_ARM64=y'|'^CONFIG_ARM64_BOOT_AARCH32=y'' .config ; then
158		ARM64_TRUSTZONE="y"
159	fi
160
161	if grep  -q '^CONFIG_ROCKCHIP_FIT_IMAGE_PACK=y' .config ; then
162		IMAGE_FORMAT="FIT"
163	fi
164}
165
166function process_args()
167{
168	while [ $# -gt 0 ]; do
169		case $1 in
170			--help|-help|help|--h|-h)
171				help
172				exit 0
173				;;
174
175			''|loader|trust|uboot|spl*|debug*|itb|env|EXT_DTB=*|nopack|fit*)
176				ARG_SUBCMD=$1
177				shift 1
178				;;
179
180			--rollback-index-uboot)
181				ARG_ROLLBACK_IDX_UBOOT="--rollback-index-uboot "$2
182				shift 2
183				;;
184
185			--rollback-index-boot)
186				ARG_ROLLBACK_IDX_BOOT="--rollback-index-boot "$2
187				shift 2
188				;;
189
190			--new-spl)
191				ARG_NEW_SPL=$1
192				shift 1
193				;;
194			map|sym|elf*)
195				ARG_SUBCMD=$1
196				if [ "$2" = "spl" -o "$2" = "tpl" ]; then
197					ARG_S_TPL=$2
198					shift 1
199				fi
200				shift 1
201				;;
202
203			*.ini|*.INI)
204				if [ ! -f $1 ]; then
205					echo "ERROR: No $1"
206				fi
207				if grep -q 'CODE471_OPTION' $1 ; then
208					ARG_INI_LOADER=$1
209				elif grep -Eq ''BL31_OPTION'|'TOS'' $1 ; then
210					ARG_INI_TRUST=$1
211				fi
212				shift 1
213				;;
214
215			*)
216				# FUNC address
217				if [ -z $(echo $1 | sed 's/[0-9,a-f,A-F,x,X,-]//g') ]; then
218					ARG_FUNCADDR=$1
219				else
220					ARG_BOARD=$1
221					if [ ! -f configs/${ARG_BOARD}_defconfig ]; then
222						echo -e "\n${SUPPORT_LIST}\n"
223						echo "ERROR: No configs/${ARG_BOARD}_defconfig"
224						exit 1
225					else
226						echo "make for ${ARG_BOARD}_defconfig by -j${JOB}"
227						make ${ARG_BOARD}_defconfig ${OPTION}
228					fi
229				fi
230				shift 1
231				;;
232		esac
233	done
234
235	if [ ! -f .config ]; then
236		echo
237		echo "ERROR: No .config"
238		help
239		exit 1
240	fi
241}
242
243function select_toolchain()
244{
245	if grep -q '^CONFIG_ARM64=y' .config ; then
246		if [ -d ${TOOLCHAIN_ARM64} ]; then
247			absolute_path=$(cd `dirname ${TOOLCHAIN_ARM64}`; pwd)
248			TOOLCHAIN_GCC=${absolute_path}/bin/${GCC_ARM64}
249			TOOLCHAIN_OBJDUMP=${absolute_path}/bin/${OBJ_ARM64}
250			TOOLCHAIN_ADDR2LINE=${absolute_path}/bin/${ADDR2LINE_ARM64}
251		else
252			echo "ERROR: No toolchain: ${TOOLCHAIN_ARM64}"
253			exit 1
254		fi
255	else
256		if [ -d ${TOOLCHAIN_ARM32} ]; then
257			absolute_path=$(cd `dirname ${TOOLCHAIN_ARM32}`; pwd)
258			TOOLCHAIN_GCC=${absolute_path}/bin/${GCC_ARM32}
259			TOOLCHAIN_OBJDUMP=${absolute_path}/bin/${OBJ_ARM32}
260			TOOLCHAIN_ADDR2LINE=${absolute_path}/bin/${ADDR2LINE_ARM32}
261		else
262			echo "ERROR: No toolchain: ${TOOLCHAIN_ARM32}"
263			exit 1
264		fi
265	fi
266}
267
268function sub_commands()
269{
270	# skip "--" parameter, such as "--rollback-index-..."
271	if [[ "$ARG_SUBCMD" != "--*" ]]; then
272		cmd=${ARG_SUBCMD%-*}
273		opt=${ARG_SUBCMD#*-}
274	else
275		cmd=$ARG_SUBCMD
276	fi
277
278	if [ "$ARG_S_TPL" == "tpl" -o "$ARG_S_TPL" == "spl" ]; then
279		elf=`find -name u-boot-${ARG_S_TPL}`
280		map=`find -name u-boot-${ARG_S_TPL}.map`
281		sym=`find -name u-boot-${ARG_S_TPL}.sym`
282	else
283		elf=u-boot
284		map=u-boot.map
285		sym=u-boot.sym
286	fi
287
288	case $cmd in
289		elf)
290			if [ ! -f ${elf} ]; then
291				echo "ERROR: No elf: ${elf}"
292				exit 1
293			else
294				# default 'cmd' without option, use '-D'
295				if [ "${cmd}" = 'elf' -a "${opt}" = 'elf' ]; then
296					opt=D
297				fi
298				${TOOLCHAIN_OBJDUMP} -${opt} ${elf} | less
299				exit 0
300			fi
301			;;
302
303		debug)
304			./scripts/rkpatch.sh ${opt}
305			exit 0
306			;;
307
308		fit)
309			if [ "$opt" = "ns" ]; then
310				./scripts/fit-vboot.sh --no-vboot --ini-trust $INI_TRUST --ini-loader $INI_LOADER $ARG_NEW_SPL
311			fi
312			exit 0
313			;;
314
315		map)
316			cat ${map} | less
317			exit 0
318			;;
319
320		sym)
321			cat ${sym} | less
322			exit 0
323			;;
324
325		trust)
326			pack_trust_image
327			exit 0
328			;;
329
330		loader)
331			pack_loader_image
332			exit 0
333			;;
334
335		spl)
336			pack_spl_loader_image ${opt}
337			exit 0
338			;;
339
340		itb)
341			pack_uboot_itb_image
342			exit 0
343			;;
344
345		uboot)
346			pack_uboot_image
347			exit 0
348			;;
349
350		env)
351			make CROSS_COMPILE=${TOOLCHAIN_GCC} envtools
352			exit 0
353			;;
354
355		--rollback-index*)
356			pack_fit_image $ARG_ROLLBACK_IDX_UBOOT $ARG_ROLLBACK_IDX_BOOT --ini-trust $INI_TRUST --ini-loader $INI_LOADER  $ARG_NEW_SPL
357			exit 0
358			;;
359
360		EXT_DTB=*)
361			OPTION=${ARG_SUBCMD}
362			;;
363
364		nopack)
365			IMAGE_NOPACK="y"
366			;;
367
368		*)
369			# Search function and code position of address
370			FUNCADDR=$ARG_FUNCADDR
371
372			RELOC_OFF=${FUNCADDR#*-}
373			FUNCADDR=${FUNCADDR%-*}
374			if [ -z $(echo ${FUNCADDR} | sed 's/[0-9,a-f,A-F,x,X,-]//g') ] && [ ${FUNCADDR} ]; then
375				# With prefix: '0x' or '0X'
376				if [ `echo ${FUNCADDR} | sed -n "/0[x,X]/p" | wc -l` -ne 0 ]; then
377					FUNCADDR=`echo $FUNCADDR | awk '{ print strtonum($0) }'`
378					FUNCADDR=`echo "obase=16;${FUNCADDR}"|bc |tr '[A-Z]' '[a-z]'`
379				fi
380				if [ `echo ${RELOC_OFF} | sed -n "/0[x,X]/p" | wc -l` -ne 0 ] && [ ${RELOC_OFF} ]; then
381					RELOC_OFF=`echo $RELOC_OFF | awk '{ print strtonum($0) }'`
382					RELOC_OFF=`echo "obase=16;${RELOC_OFF}"|bc |tr '[A-Z]' '[a-z]'`
383				fi
384
385				# If reloc address is assigned, do sub
386				if [ "${FUNCADDR}" != "${RELOC_OFF}" ]; then
387					# Hex -> Dec -> SUB -> Hex
388					FUNCADDR=`echo $((16#${FUNCADDR}))`
389					RELOC_OFF=`echo $((16#${RELOC_OFF}))`
390					FUNCADDR=$((FUNCADDR-RELOC_OFF))
391					FUNCADDR=$(echo "obase=16;${FUNCADDR}"|bc |tr '[A-Z]' '[a-z]')
392				fi
393
394				echo
395				sed -n "/${FUNCADDR}/p" ${sym}
396				${TOOLCHAIN_ADDR2LINE} -e ${elf} ${FUNCADDR}
397				exit 0
398			fi
399			;;
400	esac
401}
402
403# We select chip info to do:
404#	1. RKCHIP: fixup platform configure
405#	2. RKCHIP_LOADER: search ini file to pack loader
406#	3. RKCHIP_TRUST: search ini file to pack trust
407#	4. RKCHIP_LABEL: show build message
408#
409# We read chip info from .config and 'RKCHIP_INI_DESC'
410function select_chip_info()
411{
412	# Read RKCHIP firstly from .config
413	# The regular expression that matching:
414	#  - PX30, PX3SE
415	#  - RK????, RK????X
416	#  - RV????
417	chip_pattern='^CONFIG_ROCKCHIP_[R,P][X,V,K][0-9ESX]{1,5}'
418	RKCHIP=`egrep -o ${chip_pattern} .config`
419
420	# default
421	RKCHIP=${RKCHIP##*_}
422
423	# need fixup ?
424	for item in "${CHIP_TYPE_FIXUP_TABLE[@]}"
425	do
426		config_xxx=`echo $item | awk '{ print $1 }'`
427		if grep  -q "^${config_xxx}=y" .config ; then
428			RKCHIP=`echo $item | awk '{ print $2 }'`
429			RKCHIP_LOADER=`echo $item | awk '{ print $3 }'`
430			RKCHIP_TRUST=`echo  $item | awk '{ print $4 }'`
431			RKCHIP_LABEL=`echo  $item | awk '{ print $5 }'`
432		fi
433	done
434
435	if [ "$RKCHIP_LOADER" = "-" ]; then
436		RKCHIP_LOADER=${RKCHIP}
437	fi
438	if [ "$RKCHIP_TRUST" = "-" ]; then
439		RKCHIP_TRUST=${RKCHIP}
440	fi
441	if [ "$RKCHIP_LABEL" = "-" ]; then
442		RKCHIP_LABEL=${RKCHIP}
443	fi
444}
445
446function fixup_platform_configure()
447{
448	u_kb="-" u_num="-" t_kb="-" t_num="-"  sha="-" rsa="-"
449
450	for item in "${CHIP_CFG_FIXUP_TABLE[@]}"
451	do
452		config_xxx=`echo $item | awk '{ print $1 }'`
453		if grep  -q "^${config_xxx}=y" .config ; then
454			# <*> Fixup rsa/sha pack mode for platforms
455			rsa=`echo $item | awk '{ print $2 }'`
456			sha=`echo $item | awk '{ print $3 }'`
457
458			# <*> Fixup images size pack for platforms, and ini file
459			if grep -q '^CONFIG_ARM64_BOOT_AARCH32=y' .config ; then
460				u_kb=`echo  $item | awk '{ print $6 }' | awk -F "," '{ print $1 }'`
461				t_kb=`echo  $item | awk '{ print $6 }' | awk -F "," '{ print $2 }'`
462				u_num=`echo $item | awk '{ print $7 }' | awk -F "," '{ print $1 }'`
463				t_num=`echo $item | awk '{ print $7 }' | awk -F "," '{ print $2 }'`
464
465				PAD_LOADER=`echo $item | awk '{ print $8 }'`
466				PAD_TRUST=`echo  $item | awk '{ print $9 }'`
467				if [ "$PAD_LOADER" != "-" ]; then
468					RKCHIP_LOADER=${RKCHIP_LOADER}${PAD_LOADER}
469				fi
470				if [ "$PAD_TRUST" != "-" ]; then
471					RKCHIP_TRUST=${RKCHIP_TRUST}${PAD_TRUST}
472				fi
473				RKCHIP_LABEL=${RKCHIP_LABEL}"AARCH32"
474			else
475				u_kb=`echo  $item | awk '{ print $4 }' | awk -F "," '{ print $1 }'`
476				t_kb=`echo  $item | awk '{ print $4 }' | awk -F "," '{ print $2 }'`
477				u_num=`echo $item | awk '{ print $5 }' | awk -F "," '{ print $1 }'`
478				t_num=`echo $item | awk '{ print $5 }' | awk -F "," '{ print $2 }'`
479			fi
480		fi
481	done
482
483	if [ "$sha" != "-" ]; then
484		PLATFORM_SHA="--sha $sha"
485	fi
486	if [ "$rsa" != "-" ]; then
487		PLATFORM_RSA="--rsa $rsa"
488	fi
489	if [ "$u_kb" != "-" ]; then
490		PLATFORM_UBOOT_IMG_SIZE="--size $u_kb $u_num"
491	fi
492	if [ "$t_kb" != "-" ]; then
493		PLATFORM_TRUST_IMG_SIZE="--size $t_kb $t_num"
494	fi
495}
496
497function select_ini_file()
498{
499	# default
500	INI_LOADER=${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini
501	if [ "$ARM64_TRUSTZONE" = "y" ]; then
502		INI_TRUST=${RKBIN}/RKTRUST/${RKCHIP_TRUST}TRUST.ini
503	else
504		INI_TRUST=${RKBIN}/RKTRUST/${RKCHIP_TRUST}TOS.ini
505	fi
506
507	# args
508	if [ "$ARG_INI_TRUST" != "" ]; then
509		INI_TRUST=$ARG_INI_TRUST
510	fi
511	if [ "$ARG_INI_LOADER" != "" ]; then
512		INI_LOADER=$ARG_INI_LOADER
513	fi
514}
515
516function pack_uboot_image()
517{
518	if [ "$IMAGE_FORMAT" != "RKFW" ]; then
519		return
520	fi
521
522	# Check file size
523	head_kb=2
524	uboot_kb=`ls -l u-boot.bin | awk '{print $5}'`
525	if [ "$PLATFORM_UBOOT_IMG_SIZE" = "" ]; then
526		uboot_max_kb=1046528
527	else
528		uboot_max_kb=`echo $PLATFORM_UBOOT_IMG_SIZE | awk '{print strtonum($2)}'`
529		uboot_max_kb=$(((uboot_max_kb-head_kb)*1024))
530	fi
531
532	if [ $uboot_kb -gt $uboot_max_kb ]; then
533		echo
534		echo "ERROR: pack uboot failed! u-boot.bin actual: $uboot_kb bytes, max limit: $uboot_max_kb bytes"
535		exit 1
536	fi
537
538	# Pack
539	uboot_load_addr=`sed -n "/CONFIG_SYS_TEXT_BASE=/s/CONFIG_SYS_TEXT_BASE=//p" include/autoconf.mk|tr -d '\r'`
540	if [ -z $uboot_load_addr ]; then
541		echo "ERROR: No CONFIG_SYS_TEXT_BASE for u-boot";
542		exit 1
543	fi
544	${RKTOOLS}/loaderimage --pack --uboot u-boot.bin uboot.img ${uboot_load_addr} ${PLATFORM_UBOOT_IMG_SIZE}
545	rm u-boot.img u-boot-dtb.img -rf
546	echo "pack uboot okay! Input: u-boot.bin"
547}
548
549function pack_uboot_itb_image()
550{
551	ini=$INI_TRUST
552
553	if [ ! -f $INI_TRUST ]; then
554		echo "pack trust failed! Can't find: $INI_TRUST"
555		return
556	fi
557
558	if [ "$ARM64_TRUSTZONE" = "y" ]; then
559		bl31=`sed -n '/_bl31_/s/PATH=//p' $ini |tr -d '\r'`
560		cp ${RKBIN}/${bl31} bl31.elf
561		make CROSS_COMPILE=${TOOLCHAIN_GCC} u-boot.itb
562		echo "pack u-boot.itb okay! Input: $ini"
563	else
564		tos_image=`sed -n "/TOS=/s/TOS=//p" $ini |tr -d '\r'`
565		tosta_image=`sed -n "/TOSTA=/s/TOSTA=//p" $ini |tr -d '\r'`
566		if [ $tosta_image ]; then
567			cp ${RKBIN}/${tosta_image} tee.bin
568		elif [ $tos_image ]; then
569			cp ${RKBIN}/${tos_image}   tee.bin
570		else
571			echo "ERROR: No any tee bin"
572			exit 1
573		fi
574
575		tee_offset=`sed -n "/ADDR=/s/ADDR=//p" $ini |tr -d '\r'`
576		if [ "$tee_offset" = "" ]; then
577			tee_offset=0x8400000
578		fi
579
580		mcu_enabled=`awk -F"," '/MCU=/ { printf $3 }' $ini | tr -d ' '`
581		if [ "$mcu_enabled" = "enabled" ]; then
582			mcu_image=`awk -F"," '/MCU=/ { printf $1 }' $ini | tr -d ' ' | cut -c 5-`
583			mcu_offset=`awk -F"," '/MCU=/ { printf $2 }' $ini | tr -d ' '`
584			cp ${RKBIN}/${mcu_image} mcu.bin
585		fi
586
587		SPL_FIT_SOURCE=`sed -n "/CONFIG_SPL_FIT_SOURCE=/s/CONFIG_SPL_FIT_SOURCE=//p" .config | tr -d '""'`
588		if [ ! -z $SPL_FIT_SOURCE ]; then
589			cp $SPL_FIT_SOURCE u-boot.its
590		else
591			SPL_FIT_GENERATOR=`sed -n "/CONFIG_SPL_FIT_GENERATOR=/s/CONFIG_SPL_FIT_GENERATOR=//p" .config | tr -d '""'`
592			$SPL_FIT_GENERATOR $tee_offset $mcu_offset > u-boot.its
593		fi
594		./tools/mkimage -f u-boot.its -E u-boot.itb
595		echo "pack u-boot.itb okay! Input: $ini"
596	fi
597	echo
598}
599
600function pack_spl_loader_image()
601{
602	mode=$1
603	tmp_dir=${RKBIN}/tmp
604	tmp_ini=${tmp_dir}/${RKCHIP_LOADER}MINIALL.ini
605	ini=$INI_LOADER
606	if [ ! -f $INI_LOADER ]; then
607		echo "pack loader failed! Can't find: $INI_LOADER"
608		return
609	fi
610
611	rm ${tmp_dir} -rf
612	mkdir ${tmp_dir} -p
613	cp spl/u-boot-spl.bin ${tmp_dir}/
614	cp $ini $tmp_ini
615	if [ "$mode" = 'spl' ]; then	# pack tpl+spl
616		label="TPL+SPL"
617		cp tpl/u-boot-tpl.bin ${tmp_dir}/
618		header=`sed -n '/NAME=/s/NAME=//p' ${ini}`
619		dd if=${tmp_dir}/u-boot-tpl.bin of=${tmp_dir}/tpl.bin bs=1 skip=4
620		sed -i "1s/^/${header:0:4}/" ${tmp_dir}/tpl.bin
621		sed -i "s/FlashData=.*$/FlashData=.\/tmp\/tpl.bin/" $tmp_ini
622	else
623		label="SPL"
624	fi
625
626	sed -i "s/FlashBoot=.*$/FlashBoot=.\/tmp\/u-boot-spl.bin/" $tmp_ini
627	cd ${RKBIN}
628	${RKTOOLS}/boot_merger $tmp_ini
629	rm ${tmp_dir} -rf
630	cd -
631	rm *_loader_*.bin -rf
632	mv ${RKBIN}/*_loader_*.bin ./
633
634	filename=`basename *_loader_*.bin`
635	if [[ $filename != *spl* ]]; then
636		rename 's/loader_/spl_loader_/' *_loader_*.bin
637	fi
638	echo "pack loader(${label}) okay! Input: $ini"
639}
640
641function pack_loader_image()
642{
643	ini=$INI_LOADER
644	if [ ! -f $INI_LOADER ]; then
645		echo "pack loader failed! Can't find: $INI_LOADER"
646		return
647	fi
648
649	rm *_loader_*.bin -rf
650	numline=`cat $ini | wc -l`
651	if [ $numline -eq 1 ]; then
652		image=`sed -n "/PATH=/p" $ini | tr -d '\r' | cut -d '=' -f 2`
653		cp ${RKBIN}/${image} ./
654	else
655		cd ${RKBIN}
656		${RKTOOLS}/boot_merger $ini
657		cd - && mv ${RKBIN}/*_loader_*.bin ./
658	fi
659
660	file=`ls *loader*.bin`
661	echo "pack $file okay! Input: $ini"
662}
663
664function pack_arm32_trust_image()
665{
666	ini=$1
667	tos_image=`sed -n "/TOS=/s/TOS=//p" $ini |tr -d '\r'`
668	tosta_image=`sed -n "/TOSTA=/s/TOSTA=//p" $ini |tr -d '\r'`
669	tee_output=`sed -n "/OUTPUT=/s/OUTPUT=//p" $ini |tr -d '\r'`
670	if [ "$tee_output" = "" ]; then
671		tee_output="./trust.img"
672	fi
673	tee_offset=`sed -n "/ADDR=/s/ADDR=//p" $ini |tr -d '\r'`
674	if [ "$tee_offset" = "" ]; then
675		tee_offset=0x8400000
676	fi
677
678	# OP-TEE is 132M(0x8400000) offset from DRAM base.
679	dram_base=`sed -n "/CONFIG_SYS_SDRAM_BASE=/s/CONFIG_SYS_SDRAM_BASE=//p" include/autoconf.mk|tr -d '\r'`
680	tee_load_addr=$((dram_base+tee_offset))
681	tee_load_addr=$(echo "obase=16;${tee_load_addr}"|bc) # Convert Dec to Hex
682
683	if [ $tosta_image ]; then
684		${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${tosta_image} ${tee_output} ${tee_load_addr} ${PLATFORM_TRUST_IMG_SIZE}
685	elif [ $tos_image ]; then
686		${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${tos_image}   ${tee_output} ${tee_load_addr} ${PLATFORM_TRUST_IMG_SIZE}
687	else
688		echo "ERROR: No any tee bin"
689		exit 1
690	fi
691	echo "pack trust okay! Input: $ini"
692}
693
694function pack_arm64_trust_image()
695{
696	ini=$1
697	cd ${RKBIN}
698	${RKTOOLS}/trust_merger ${PLATFORM_SHA} ${PLATFORM_RSA} ${PLATFORM_TRUST_IMG_SIZE} $ini
699	cd - && mv ${RKBIN}/trust*.img ./
700	echo "pack trust okay! Input: $ini"
701}
702
703function pack_trust_image()
704{
705	if [ "$IMAGE_FORMAT" != "RKFW" ]; then
706		return
707	fi
708
709	rm trust*.img -rf
710	ini=$INI_TRUST
711	if [ ! -f $INI_TRUST ]; then
712		echo "pack trust failed! Can't find: $INI_TRUST"
713		return
714	fi
715
716	numline=`cat $ini | wc -l`
717	if [ $numline -eq 1 ]; then
718		image=`sed -n "/PATH=/p" $ini | tr -d '\r' | cut -d '=' -f 2`
719		cp ${RKBIN}/${image} ./trust.img
720		echo "pack trust okay! Input: $ini"
721		return;
722	else
723		if [ "$ARM64_TRUSTZONE" = "y" ]; then
724			pack_arm64_trust_image $ini
725		else
726			pack_arm32_trust_image $ini
727		fi
728	fi
729}
730
731function pack_fit_image()
732{
733	if grep -q '^CONFIG_FIT_SIGNATURE=y' .config ; then
734		./scripts/fit-vboot.sh $ARG_ROLLBACK_IDX_UBOOT $ARG_ROLLBACK_IDX_BOOT --ini-trust $INI_TRUST --ini-loader $INI_LOADER $ARG_NEW_SPL
735	else
736		rm uboot.img trust*.img -rf
737		./scripts/fit-vboot-uboot.sh --no-vboot --no-rebuild --ini-trust $INI_TRUST --ini-loader $INI_LOADER  $ARG_NEW_SPL
738		echo "pack uboot.img (with uboot trust) okay! Input: $INI_TRUST"
739	fi
740}
741
742function pack_images()
743{
744	if [ "$IMAGE_NOPACK" != "y" ]; then
745		if [ "$IMAGE_FORMAT" = "RKFW" ]; then
746			pack_uboot_image
747			pack_trust_image
748			pack_loader_image
749		elif [ "$IMAGE_FORMAT" = "FIT" ]; then
750			pack_fit_image $ARG_ROLLBACK_IDX_UBOOT $ARG_ROLLBACK_IDX_BOOT --ini-trust $INI_TRUST --ini-loader $INI_LOADER  $ARG_NEW_SPL
751		fi
752	fi
753}
754
755function clean_files()
756{
757	if [ -f spl/u-boot-spl.dtb ]; then
758		rm spl/u-boot-spl.dtb -rf
759	fi
760	if [ -f tpl/u-boot-tpl.dtb ]; then
761		rm tpl/u-boot-tpl.dtb -rf
762	fi
763	if [ -f u-boot.dtb ]; then
764		rm u-boot.dtb -rf
765	fi
766}
767
768function finish()
769{
770	echo
771	if [ ! -z "$OPTION" ]; then
772		echo "Platform ${RKCHIP_LABEL} is build OK, with exist .config ($OPTION)"
773	elif [ "$ARG_BOARD" = '' ]; then
774		echo "Platform ${RKCHIP_LABEL} is build OK, with exist .config"
775	else
776		echo "Platform ${RKCHIP_LABEL} is build OK, with new .config(make ${ARG_BOARD}_defconfig)"
777	fi
778}
779
780process_args $*
781prepare
782select_toolchain
783select_chip_info
784fixup_platform_configure
785select_ini_file
786sub_commands
787clean_files
788make CROSS_COMPILE=${TOOLCHAIN_GCC} ${OPTION} all --jobs=${JOB}
789pack_images
790finish
791