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