xref: /rk3399_rockchip-uboot/make.sh (revision f3797579f0d06cd09733347bf361fd3fbcdb3d9b)
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="$1 "$2
182				shift 2
183				;;
184
185			--rollback-index-boot)
186				ARG_ROLLBACK_IDX_BOOT="$1 "$2
187				shift 2
188				;;
189			--boot_img)
190				ARG_EXT_BOOT_IMG="$1 "$2
191				shift 2
192				;;
193			--new-spl)
194				ARG_NEW_SPL=$1
195				shift 1
196				;;
197			map|sym|elf*)
198				ARG_SUBCMD=$1
199				if [ "$2" = "spl" -o "$2" = "tpl" ]; then
200					ARG_S_TPL=$2
201					shift 1
202				fi
203				shift 1
204				;;
205
206			*.ini|*.INI)
207				if [ ! -f $1 ]; then
208					echo "ERROR: No $1"
209				fi
210				if grep -q 'CODE471_OPTION' $1 ; then
211					ARG_INI_LOADER=$1
212				elif grep -Eq ''BL31_OPTION'|'TOS'' $1 ; then
213					ARG_INI_TRUST=$1
214				fi
215				shift 1
216				;;
217
218			*)
219				# FUNC address
220				if [ -z $(echo $1 | sed 's/[0-9,a-f,A-F,x,X,-]//g') ]; then
221					ARG_FUNCADDR=$1
222				else
223					ARG_BOARD=$1
224					if [ ! -f configs/${ARG_BOARD}_defconfig ]; then
225						echo -e "\n${SUPPORT_LIST}\n"
226						echo "ERROR: No configs/${ARG_BOARD}_defconfig"
227						exit 1
228					else
229						echo "make for ${ARG_BOARD}_defconfig by -j${JOB}"
230						make ${ARG_BOARD}_defconfig ${OPTION}
231					fi
232				fi
233				shift 1
234				;;
235		esac
236	done
237
238	if [ ! -f .config ]; then
239		echo
240		echo "ERROR: No .config"
241		help
242		exit 1
243	fi
244}
245
246function select_toolchain()
247{
248	if grep -q '^CONFIG_ARM64=y' .config ; then
249		if [ -d ${TOOLCHAIN_ARM64} ]; then
250			absolute_path=$(cd `dirname ${TOOLCHAIN_ARM64}`; pwd)
251			TOOLCHAIN_GCC=${absolute_path}/bin/${GCC_ARM64}
252			TOOLCHAIN_OBJDUMP=${absolute_path}/bin/${OBJ_ARM64}
253			TOOLCHAIN_ADDR2LINE=${absolute_path}/bin/${ADDR2LINE_ARM64}
254		else
255			echo "ERROR: No toolchain: ${TOOLCHAIN_ARM64}"
256			exit 1
257		fi
258	else
259		if [ -d ${TOOLCHAIN_ARM32} ]; then
260			absolute_path=$(cd `dirname ${TOOLCHAIN_ARM32}`; pwd)
261			TOOLCHAIN_GCC=${absolute_path}/bin/${GCC_ARM32}
262			TOOLCHAIN_OBJDUMP=${absolute_path}/bin/${OBJ_ARM32}
263			TOOLCHAIN_ADDR2LINE=${absolute_path}/bin/${ADDR2LINE_ARM32}
264		else
265			echo "ERROR: No toolchain: ${TOOLCHAIN_ARM32}"
266			exit 1
267		fi
268	fi
269}
270
271function sub_commands()
272{
273	# skip "--" parameter, such as "--rollback-index-..."
274	if [[ "$ARG_SUBCMD" != "--*" ]]; then
275		cmd=${ARG_SUBCMD%-*}
276		opt=${ARG_SUBCMD#*-}
277	else
278		cmd=$ARG_SUBCMD
279	fi
280
281	if [ "$ARG_S_TPL" == "tpl" -o "$ARG_S_TPL" == "spl" ]; then
282		elf=`find -name u-boot-${ARG_S_TPL}`
283		map=`find -name u-boot-${ARG_S_TPL}.map`
284		sym=`find -name u-boot-${ARG_S_TPL}.sym`
285	else
286		elf=u-boot
287		map=u-boot.map
288		sym=u-boot.sym
289	fi
290
291	case $cmd in
292		elf)
293			if [ ! -f ${elf} ]; then
294				echo "ERROR: No elf: ${elf}"
295				exit 1
296			else
297				# default 'cmd' without option, use '-D'
298				if [ "${cmd}" = 'elf' -a "${opt}" = 'elf' ]; then
299					opt=D
300				fi
301				${TOOLCHAIN_OBJDUMP} -${opt} ${elf} | less
302				exit 0
303			fi
304			;;
305
306		debug)
307			./scripts/rkpatch.sh ${opt}
308			exit 0
309			;;
310
311		fit)
312			if [ "$opt" = "ns" ]; then
313				./scripts/fit-vboot.sh --no-vboot --ini-trust $INI_TRUST --ini-loader $INI_LOADER $ARG_NEW_SPL $ARG_EXT_BOOT_IMG
314			fi
315			exit 0
316			;;
317
318		map)
319			cat ${map} | less
320			exit 0
321			;;
322
323		sym)
324			cat ${sym} | less
325			exit 0
326			;;
327
328		trust)
329			pack_trust_image
330			exit 0
331			;;
332
333		loader)
334			pack_loader_image
335			exit 0
336			;;
337
338		spl)
339			pack_spl_loader_image ${opt}
340			exit 0
341			;;
342
343		itb)
344			pack_uboot_itb_image
345			exit 0
346			;;
347
348		uboot)
349			pack_uboot_image
350			exit 0
351			;;
352
353		env)
354			make CROSS_COMPILE=${TOOLCHAIN_GCC} envtools
355			exit 0
356			;;
357
358		--rollback-index*)
359			pack_fit_image $ARG_ROLLBACK_IDX_UBOOT $ARG_ROLLBACK_IDX_BOOT --ini-trust $INI_TRUST --ini-loader $INI_LOADER  $ARG_NEW_SPL $ARG_EXT_BOOT_IMG
360			exit 0
361			;;
362
363		EXT_DTB=*)
364			OPTION=${ARG_SUBCMD}
365			;;
366
367		nopack)
368			IMAGE_NOPACK="y"
369			;;
370
371		*)
372			# Search function and code position of address
373			FUNCADDR=$ARG_FUNCADDR
374
375			RELOC_OFF=${FUNCADDR#*-}
376			FUNCADDR=${FUNCADDR%-*}
377			if [ -z $(echo ${FUNCADDR} | sed 's/[0-9,a-f,A-F,x,X,-]//g') ] && [ ${FUNCADDR} ]; then
378				# With prefix: '0x' or '0X'
379				if [ `echo ${FUNCADDR} | sed -n "/0[x,X]/p" | wc -l` -ne 0 ]; then
380					FUNCADDR=`echo $FUNCADDR | awk '{ print strtonum($0) }'`
381					FUNCADDR=`echo "obase=16;${FUNCADDR}"|bc |tr '[A-Z]' '[a-z]'`
382				fi
383				if [ `echo ${RELOC_OFF} | sed -n "/0[x,X]/p" | wc -l` -ne 0 ] && [ ${RELOC_OFF} ]; then
384					RELOC_OFF=`echo $RELOC_OFF | awk '{ print strtonum($0) }'`
385					RELOC_OFF=`echo "obase=16;${RELOC_OFF}"|bc |tr '[A-Z]' '[a-z]'`
386				fi
387
388				# If reloc address is assigned, do sub
389				if [ "${FUNCADDR}" != "${RELOC_OFF}" ]; then
390					# Hex -> Dec -> SUB -> Hex
391					FUNCADDR=`echo $((16#${FUNCADDR}))`
392					RELOC_OFF=`echo $((16#${RELOC_OFF}))`
393					FUNCADDR=$((FUNCADDR-RELOC_OFF))
394					FUNCADDR=$(echo "obase=16;${FUNCADDR}"|bc |tr '[A-Z]' '[a-z]')
395				fi
396
397				echo
398				sed -n "/${FUNCADDR}/p" ${sym}
399				${TOOLCHAIN_ADDR2LINE} -e ${elf} ${FUNCADDR}
400				exit 0
401			fi
402			;;
403	esac
404}
405
406# We select chip info to do:
407#	1. RKCHIP: fixup platform configure
408#	2. RKCHIP_LOADER: search ini file to pack loader
409#	3. RKCHIP_TRUST: search ini file to pack trust
410#	4. RKCHIP_LABEL: show build message
411#
412# We read chip info from .config and 'RKCHIP_INI_DESC'
413function select_chip_info()
414{
415	# Read RKCHIP firstly from .config
416	# The regular expression that matching:
417	#  - PX30, PX3SE
418	#  - RK????, RK????X
419	#  - RV????
420	chip_pattern='^CONFIG_ROCKCHIP_[R,P][X,V,K][0-9ESX]{1,5}'
421	RKCHIP=`egrep -o ${chip_pattern} .config`
422
423	# default
424	RKCHIP=${RKCHIP##*_}
425
426	# need fixup ?
427	for item in "${CHIP_TYPE_FIXUP_TABLE[@]}"
428	do
429		config_xxx=`echo $item | awk '{ print $1 }'`
430		if grep  -q "^${config_xxx}=y" .config ; then
431			RKCHIP=`echo $item | awk '{ print $2 }'`
432			RKCHIP_LOADER=`echo $item | awk '{ print $3 }'`
433			RKCHIP_TRUST=`echo  $item | awk '{ print $4 }'`
434			RKCHIP_LABEL=`echo  $item | awk '{ print $5 }'`
435		fi
436	done
437
438	if [ "$RKCHIP_LOADER" = "-" ]; then
439		RKCHIP_LOADER=${RKCHIP}
440	fi
441	if [ "$RKCHIP_TRUST" = "-" ]; then
442		RKCHIP_TRUST=${RKCHIP}
443	fi
444	if [ "$RKCHIP_LABEL" = "-" ]; then
445		RKCHIP_LABEL=${RKCHIP}
446	fi
447}
448
449function fixup_platform_configure()
450{
451	u_kb="-" u_num="-" t_kb="-" t_num="-"  sha="-" rsa="-"
452
453	for item in "${CHIP_CFG_FIXUP_TABLE[@]}"
454	do
455		config_xxx=`echo $item | awk '{ print $1 }'`
456		if grep  -q "^${config_xxx}=y" .config ; then
457			# <*> Fixup rsa/sha pack mode for platforms
458			rsa=`echo $item | awk '{ print $2 }'`
459			sha=`echo $item | awk '{ print $3 }'`
460
461			# <*> Fixup images size pack for platforms, and ini file
462			if grep -q '^CONFIG_ARM64_BOOT_AARCH32=y' .config ; then
463				u_kb=`echo  $item | awk '{ print $6 }' | awk -F "," '{ print $1 }'`
464				t_kb=`echo  $item | awk '{ print $6 }' | awk -F "," '{ print $2 }'`
465				u_num=`echo $item | awk '{ print $7 }' | awk -F "," '{ print $1 }'`
466				t_num=`echo $item | awk '{ print $7 }' | awk -F "," '{ print $2 }'`
467
468				PAD_LOADER=`echo $item | awk '{ print $8 }'`
469				PAD_TRUST=`echo  $item | awk '{ print $9 }'`
470				if [ "$PAD_LOADER" != "-" ]; then
471					RKCHIP_LOADER=${RKCHIP_LOADER}${PAD_LOADER}
472				fi
473				if [ "$PAD_TRUST" != "-" ]; then
474					RKCHIP_TRUST=${RKCHIP_TRUST}${PAD_TRUST}
475				fi
476				RKCHIP_LABEL=${RKCHIP_LABEL}"AARCH32"
477			else
478				u_kb=`echo  $item | awk '{ print $4 }' | awk -F "," '{ print $1 }'`
479				t_kb=`echo  $item | awk '{ print $4 }' | awk -F "," '{ print $2 }'`
480				u_num=`echo $item | awk '{ print $5 }' | awk -F "," '{ print $1 }'`
481				t_num=`echo $item | awk '{ print $5 }' | awk -F "," '{ print $2 }'`
482			fi
483		fi
484	done
485
486	if [ "$sha" != "-" ]; then
487		PLATFORM_SHA="--sha $sha"
488	fi
489	if [ "$rsa" != "-" ]; then
490		PLATFORM_RSA="--rsa $rsa"
491	fi
492	if [ "$u_kb" != "-" ]; then
493		PLATFORM_UBOOT_IMG_SIZE="--size $u_kb $u_num"
494	fi
495	if [ "$t_kb" != "-" ]; then
496		PLATFORM_TRUST_IMG_SIZE="--size $t_kb $t_num"
497	fi
498}
499
500function select_ini_file()
501{
502	# default
503	INI_LOADER=${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini
504	if [ "$ARM64_TRUSTZONE" = "y" ]; then
505		INI_TRUST=${RKBIN}/RKTRUST/${RKCHIP_TRUST}TRUST.ini
506	else
507		INI_TRUST=${RKBIN}/RKTRUST/${RKCHIP_TRUST}TOS.ini
508	fi
509
510	# args
511	if [ "$ARG_INI_TRUST" != "" ]; then
512		INI_TRUST=$ARG_INI_TRUST
513	fi
514	if [ "$ARG_INI_LOADER" != "" ]; then
515		INI_LOADER=$ARG_INI_LOADER
516	fi
517}
518
519function pack_uboot_image()
520{
521	if [ "$IMAGE_FORMAT" != "RKFW" ]; then
522		return
523	fi
524
525	# Check file size
526	head_kb=2
527	uboot_kb=`ls -l u-boot.bin | awk '{print $5}'`
528	if [ "$PLATFORM_UBOOT_IMG_SIZE" = "" ]; then
529		uboot_max_kb=1046528
530	else
531		uboot_max_kb=`echo $PLATFORM_UBOOT_IMG_SIZE | awk '{print strtonum($2)}'`
532		uboot_max_kb=$(((uboot_max_kb-head_kb)*1024))
533	fi
534
535	if [ $uboot_kb -gt $uboot_max_kb ]; then
536		echo
537		echo "ERROR: pack uboot failed! u-boot.bin actual: $uboot_kb bytes, max limit: $uboot_max_kb bytes"
538		exit 1
539	fi
540
541	# Pack
542	uboot_load_addr=`sed -n "/CONFIG_SYS_TEXT_BASE=/s/CONFIG_SYS_TEXT_BASE=//p" include/autoconf.mk|tr -d '\r'`
543	if [ -z $uboot_load_addr ]; then
544		echo "ERROR: No CONFIG_SYS_TEXT_BASE for u-boot";
545		exit 1
546	fi
547	${RKTOOLS}/loaderimage --pack --uboot u-boot.bin uboot.img ${uboot_load_addr} ${PLATFORM_UBOOT_IMG_SIZE}
548	rm u-boot.img u-boot-dtb.img -rf
549	echo "pack uboot okay! Input: u-boot.bin"
550}
551
552function pack_uboot_itb_image()
553{
554	ini=$INI_TRUST
555
556	if [ ! -f $INI_TRUST ]; then
557		echo "pack trust failed! Can't find: $INI_TRUST"
558		return
559	fi
560
561	if [ "$ARM64_TRUSTZONE" = "y" ]; then
562		bl31=`sed -n '/_bl31_/s/PATH=//p' $ini |tr -d '\r'`
563		cp ${RKBIN}/${bl31} bl31.elf
564		make CROSS_COMPILE=${TOOLCHAIN_GCC} u-boot.itb
565		echo "pack u-boot.itb okay! Input: $ini"
566	else
567		tos_image=`sed -n "/TOS=/s/TOS=//p" $ini |tr -d '\r'`
568		tosta_image=`sed -n "/TOSTA=/s/TOSTA=//p" $ini |tr -d '\r'`
569		if [ $tosta_image ]; then
570			cp ${RKBIN}/${tosta_image} tee.bin
571		elif [ $tos_image ]; then
572			cp ${RKBIN}/${tos_image}   tee.bin
573		else
574			echo "ERROR: No any tee bin"
575			exit 1
576		fi
577
578		tee_offset=`sed -n "/ADDR=/s/ADDR=//p" $ini |tr -d '\r'`
579		if [ "$tee_offset" = "" ]; then
580			tee_offset=0x8400000
581		fi
582
583		mcu_enabled=`awk -F"," '/MCU=/ { printf $3 }' $ini | tr -d ' '`
584		if [ "$mcu_enabled" = "enabled" ]; then
585			mcu_image=`awk -F"," '/MCU=/ { printf $1 }' $ini | tr -d ' ' | cut -c 5-`
586			mcu_offset=`awk -F"," '/MCU=/ { printf $2 }' $ini | tr -d ' '`
587			cp ${RKBIN}/${mcu_image} mcu.bin
588		fi
589
590		SPL_FIT_SOURCE=`sed -n "/CONFIG_SPL_FIT_SOURCE=/s/CONFIG_SPL_FIT_SOURCE=//p" .config | tr -d '""'`
591		if [ ! -z $SPL_FIT_SOURCE ]; then
592			cp $SPL_FIT_SOURCE u-boot.its
593		else
594			SPL_FIT_GENERATOR=`sed -n "/CONFIG_SPL_FIT_GENERATOR=/s/CONFIG_SPL_FIT_GENERATOR=//p" .config | tr -d '""'`
595			$SPL_FIT_GENERATOR $tee_offset $mcu_offset > u-boot.its
596		fi
597		./tools/mkimage -f u-boot.its -E u-boot.itb
598		echo "pack u-boot.itb okay! Input: $ini"
599	fi
600	echo
601}
602
603function pack_spl_loader_image()
604{
605	mode=$1
606	tmp_dir=${RKBIN}/tmp
607	tmp_ini=${tmp_dir}/${RKCHIP_LOADER}MINIALL.ini
608	ini=$INI_LOADER
609	if [ ! -f $INI_LOADER ]; then
610		echo "pack loader failed! Can't find: $INI_LOADER"
611		return
612	fi
613
614	rm ${tmp_dir} -rf
615	mkdir ${tmp_dir} -p
616	cp spl/u-boot-spl.bin ${tmp_dir}/
617	cp $ini $tmp_ini
618	if [ "$mode" = 'spl' ]; then	# pack tpl+spl
619		label="TPL+SPL"
620		cp tpl/u-boot-tpl.bin ${tmp_dir}/
621		header=`sed -n '/NAME=/s/NAME=//p' ${ini}`
622		dd if=${tmp_dir}/u-boot-tpl.bin of=${tmp_dir}/tpl.bin bs=1 skip=4
623		sed -i "1s/^/${header:0:4}/" ${tmp_dir}/tpl.bin
624		sed -i "s/FlashData=.*$/FlashData=.\/tmp\/tpl.bin/" $tmp_ini
625	else
626		label="SPL"
627	fi
628
629	sed -i "s/FlashBoot=.*$/FlashBoot=.\/tmp\/u-boot-spl.bin/" $tmp_ini
630	cd ${RKBIN}
631	${RKTOOLS}/boot_merger $tmp_ini
632	rm ${tmp_dir} -rf
633	cd -
634	rm *_loader_*.bin -rf
635	mv ${RKBIN}/*_loader_*.bin ./
636
637	filename=`basename *_loader_*.bin`
638	if [[ $filename != *spl* ]]; then
639		rename 's/loader_/spl_loader_/' *_loader_*.bin
640	fi
641	echo "pack loader(${label}) okay! Input: $ini"
642}
643
644function pack_loader_image()
645{
646	ini=$INI_LOADER
647	if [ ! -f $INI_LOADER ]; then
648		echo "pack loader failed! Can't find: $INI_LOADER"
649		return
650	fi
651
652	rm *_loader_*.bin -rf
653	numline=`cat $ini | wc -l`
654	if [ $numline -eq 1 ]; then
655		image=`sed -n "/PATH=/p" $ini | tr -d '\r' | cut -d '=' -f 2`
656		cp ${RKBIN}/${image} ./
657	else
658		cd ${RKBIN}
659		${RKTOOLS}/boot_merger $ini
660		cd - && mv ${RKBIN}/*_loader_*.bin ./
661	fi
662
663	file=`ls *loader*.bin`
664	echo "pack $file okay! Input: $ini"
665}
666
667function pack_arm32_trust_image()
668{
669	ini=$1
670	tos_image=`sed -n "/TOS=/s/TOS=//p" $ini |tr -d '\r'`
671	tosta_image=`sed -n "/TOSTA=/s/TOSTA=//p" $ini |tr -d '\r'`
672	tee_output=`sed -n "/OUTPUT=/s/OUTPUT=//p" $ini |tr -d '\r'`
673	if [ "$tee_output" = "" ]; then
674		tee_output="./trust.img"
675	fi
676	tee_offset=`sed -n "/ADDR=/s/ADDR=//p" $ini |tr -d '\r'`
677	if [ "$tee_offset" = "" ]; then
678		tee_offset=0x8400000
679	fi
680
681	# OP-TEE is 132M(0x8400000) offset from DRAM base.
682	dram_base=`sed -n "/CONFIG_SYS_SDRAM_BASE=/s/CONFIG_SYS_SDRAM_BASE=//p" include/autoconf.mk|tr -d '\r'`
683	tee_load_addr=$((dram_base+tee_offset))
684	tee_load_addr=$(echo "obase=16;${tee_load_addr}"|bc) # Convert Dec to Hex
685
686	if [ $tosta_image ]; then
687		${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${tosta_image} ${tee_output} ${tee_load_addr} ${PLATFORM_TRUST_IMG_SIZE}
688	elif [ $tos_image ]; then
689		${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${tos_image}   ${tee_output} ${tee_load_addr} ${PLATFORM_TRUST_IMG_SIZE}
690	else
691		echo "ERROR: No any tee bin"
692		exit 1
693	fi
694	echo "pack trust okay! Input: $ini"
695}
696
697function pack_arm64_trust_image()
698{
699	ini=$1
700	cd ${RKBIN}
701	${RKTOOLS}/trust_merger ${PLATFORM_SHA} ${PLATFORM_RSA} ${PLATFORM_TRUST_IMG_SIZE} $ini
702	cd - && mv ${RKBIN}/trust*.img ./
703	echo "pack trust okay! Input: $ini"
704}
705
706function pack_trust_image()
707{
708	if [ "$IMAGE_FORMAT" != "RKFW" ]; then
709		return
710	fi
711
712	rm trust*.img -rf
713	ini=$INI_TRUST
714	if [ ! -f $INI_TRUST ]; then
715		echo "pack trust failed! Can't find: $INI_TRUST"
716		return
717	fi
718
719	numline=`cat $ini | wc -l`
720	if [ $numline -eq 1 ]; then
721		image=`sed -n "/PATH=/p" $ini | tr -d '\r' | cut -d '=' -f 2`
722		cp ${RKBIN}/${image} ./trust.img
723		echo "pack trust okay! Input: $ini"
724		return;
725	else
726		if [ "$ARM64_TRUSTZONE" = "y" ]; then
727			pack_arm64_trust_image $ini
728		else
729			pack_arm32_trust_image $ini
730		fi
731	fi
732}
733
734function pack_fit_image()
735{
736	if grep -q '^CONFIG_FIT_SIGNATURE=y' .config ; then
737		./scripts/fit-vboot.sh $ARG_ROLLBACK_IDX_UBOOT $ARG_ROLLBACK_IDX_BOOT --ini-trust $INI_TRUST --ini-loader $INI_LOADER $ARG_NEW_SPL $ARG_EXT_BOOT_IMG
738	else
739		rm uboot.img trust*.img -rf
740		./scripts/fit-vboot-uboot.sh --no-vboot --no-rebuild --ini-trust $INI_TRUST --ini-loader $INI_LOADER  $ARG_NEW_SPL $ARG_EXT_BOOT_IMG
741		echo "pack uboot.img (with uboot trust) okay! Input: $INI_TRUST"
742	fi
743}
744
745function pack_images()
746{
747	if [ "$IMAGE_NOPACK" != "y" ]; then
748		if [ "$IMAGE_FORMAT" = "RKFW" ]; then
749			pack_uboot_image
750			pack_trust_image
751			pack_loader_image
752		elif [ "$IMAGE_FORMAT" = "FIT" ]; then
753			pack_fit_image $ARG_ROLLBACK_IDX_UBOOT $ARG_ROLLBACK_IDX_BOOT --ini-trust $INI_TRUST --ini-loader $INI_LOADER  $ARG_NEW_SPL $ARG_EXT_BOOT_IMG
754		fi
755	fi
756}
757
758function clean_files()
759{
760	if [ -f spl/u-boot-spl.dtb ]; then
761		rm spl/u-boot-spl.dtb -rf
762	fi
763	if [ -f tpl/u-boot-tpl.dtb ]; then
764		rm tpl/u-boot-tpl.dtb -rf
765	fi
766	if [ -f u-boot.dtb ]; then
767		rm u-boot.dtb -rf
768	fi
769}
770
771function finish()
772{
773	echo
774	if [ ! -z "$OPTION" ]; then
775		echo "Platform ${RKCHIP_LABEL} is build OK, with exist .config ($OPTION)"
776	elif [ "$ARG_BOARD" = '' ]; then
777		echo "Platform ${RKCHIP_LABEL} is build OK, with exist .config"
778	else
779		echo "Platform ${RKCHIP_LABEL} is build OK, with new .config(make ${ARG_BOARD}_defconfig)"
780	fi
781}
782
783process_args $*
784prepare
785select_toolchain
786select_chip_info
787fixup_platform_configure
788select_ini_file
789sub_commands
790clean_files
791make CROSS_COMPILE=${TOOLCHAIN_GCC} ${OPTION} all --jobs=${JOB}
792pack_images
793finish
794