xref: /rk3399_rockchip-uboot/make.sh (revision 01b8c4d110abb0dcbe36dc5b6b10d93b2b8e2667)
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			''|loader|trust|uboot|spl*|debug*|itb|env|EXT_DTB=*|nopack|fit*)
175				ARG_SUBCMD=$1
176				shift 1
177				;;
178			map|sym|elf*)
179				ARG_SUBCMD=$1
180				if [ "$2" = "spl" -o "$2" = "tpl" ]; then
181					ARG_S_TPL=$2
182					shift 1
183				fi
184				shift 1
185				;;
186			*.ini)
187				if [ ! -f $1 ]; then
188					echo "ERROR: No $1"
189				fi
190				if grep -q 'CODE471_OPTION' $1 ; then
191					ARG_INI_LOADER=$1
192				elif grep -Eq ''BL31_OPTION'|'TOS'' $1 ; then
193					ARG_INI_TRUST=$1
194				fi
195				shift 1
196				;;
197			*)
198				# out scripts args
199				NUM=$(./scripts/fit-mkimg.sh --p-check $1)
200				if [ $NUM -ne 0 ]; then
201					[ $NUM -eq 1 ] && ARG_FIT="${ARG_FIT} $1"
202					[ $NUM -eq 2 ] && ARG_FIT="${ARG_FIT} $1 $2"
203					shift ${NUM}
204					continue
205				# FUNC address
206				elif [ -z $(echo $1 | sed 's/[0-9,a-f,A-F,x,X,-]//g') ]; then
207					ARG_FUNCADDR=$1
208				# xxx_defconfig
209				else
210					ARG_BOARD=$1
211					if [ ! -f configs/${ARG_BOARD}_defconfig ]; then
212						echo -e "\n${SUPPORT_LIST}\n"
213						echo "ERROR: No configs/${ARG_BOARD}_defconfig"
214						exit 1
215					else
216						echo "make for ${ARG_BOARD}_defconfig by -j${JOB}"
217						make ${ARG_BOARD}_defconfig ${OPTION}
218					fi
219				fi
220				shift 1
221				;;
222		esac
223	done
224
225	if [ ! -f .config ]; then
226		echo
227		echo "ERROR: No .config"
228		help
229		exit 1
230	fi
231}
232
233function select_toolchain()
234{
235	if grep -q '^CONFIG_ARM64=y' .config ; then
236		if [ -d ${TOOLCHAIN_ARM64} ]; then
237			absolute_path=$(cd `dirname ${TOOLCHAIN_ARM64}`; pwd)
238			TOOLCHAIN_GCC=${absolute_path}/bin/${GCC_ARM64}
239			TOOLCHAIN_OBJDUMP=${absolute_path}/bin/${OBJ_ARM64}
240			TOOLCHAIN_ADDR2LINE=${absolute_path}/bin/${ADDR2LINE_ARM64}
241		else
242			echo "ERROR: No toolchain: ${TOOLCHAIN_ARM64}"
243			exit 1
244		fi
245	else
246		if [ -d ${TOOLCHAIN_ARM32} ]; then
247			absolute_path=$(cd `dirname ${TOOLCHAIN_ARM32}`; pwd)
248			TOOLCHAIN_GCC=${absolute_path}/bin/${GCC_ARM32}
249			TOOLCHAIN_OBJDUMP=${absolute_path}/bin/${OBJ_ARM32}
250			TOOLCHAIN_ADDR2LINE=${absolute_path}/bin/${ADDR2LINE_ARM32}
251		else
252			echo "ERROR: No toolchain: ${TOOLCHAIN_ARM32}"
253			exit 1
254		fi
255	fi
256}
257
258function sub_commands()
259{
260	# skip "--" parameter, such as "--rollback-index-..."
261	if [[ "$ARG_SUBCMD" != "--*" ]]; then
262		cmd=${ARG_SUBCMD%-*}
263		opt=${ARG_SUBCMD#*-}
264	else
265		cmd=$ARG_SUBCMD
266	fi
267
268	if [ "$ARG_S_TPL" == "tpl" -o "$ARG_S_TPL" == "spl" ]; then
269		elf=`find -name u-boot-${ARG_S_TPL}`
270		map=`find -name u-boot-${ARG_S_TPL}.map`
271		sym=`find -name u-boot-${ARG_S_TPL}.sym`
272	else
273		elf=u-boot
274		map=u-boot.map
275		sym=u-boot.sym
276	fi
277
278	case $cmd in
279		elf)
280			if [ ! -f ${elf} ]; then
281				echo "ERROR: No elf: ${elf}"
282				exit 1
283			else
284				# default 'cmd' without option, use '-D'
285				if [ "${cmd}" = 'elf' -a "${opt}" = 'elf' ]; then
286					opt=D
287				fi
288				${TOOLCHAIN_OBJDUMP} -${opt} ${elf} | less
289				exit 0
290			fi
291			;;
292
293		debug)
294			./scripts/rkpatch.sh ${opt}
295			exit 0
296			;;
297
298		fit)
299			if [ "$opt" = "ns" ]; then
300				./scripts/fit-mkimg.sh --uboot --boot --no-vboot ${ARG_FIT}
301			fi
302			exit 0
303			;;
304
305		map)
306			cat ${map} | less
307			exit 0
308			;;
309
310		sym)
311			cat ${sym} | less
312			exit 0
313			;;
314
315		trust)
316			pack_trust_image
317			exit 0
318			;;
319
320		loader)
321			pack_loader_image
322			exit 0
323			;;
324
325		spl)
326			pack_spl_loader_image ${opt}
327			exit 0
328			;;
329
330		itb)
331			pack_uboot_itb_image
332			exit 0
333			;;
334
335		uboot)
336			pack_uboot_image
337			exit 0
338			;;
339
340		env)
341			make CROSS_COMPILE=${TOOLCHAIN_GCC} envtools
342			exit 0
343			;;
344
345		--rollback-index*)
346			pack_fit_image ${ARG_FIT}
347			exit 0
348			;;
349
350		EXT_DTB=*)
351			OPTION=${ARG_SUBCMD}
352			;;
353
354		nopack)
355			IMAGE_NOPACK="y"
356			;;
357
358		*)
359			# Search function and code position of address
360			FUNCADDR=$ARG_FUNCADDR
361
362			RELOC_OFF=${FUNCADDR#*-}
363			FUNCADDR=${FUNCADDR%-*}
364			if [ -z $(echo ${FUNCADDR} | sed 's/[0-9,a-f,A-F,x,X,-]//g') ] && [ ${FUNCADDR} ]; then
365				# With prefix: '0x' or '0X'
366				if [ `echo ${FUNCADDR} | sed -n "/0[x,X]/p" | wc -l` -ne 0 ]; then
367					FUNCADDR=`echo $FUNCADDR | awk '{ print strtonum($0) }'`
368					FUNCADDR=`echo "obase=16;${FUNCADDR}"|bc |tr '[A-Z]' '[a-z]'`
369				fi
370				if [ `echo ${RELOC_OFF} | sed -n "/0[x,X]/p" | wc -l` -ne 0 ] && [ ${RELOC_OFF} ]; then
371					RELOC_OFF=`echo $RELOC_OFF | awk '{ print strtonum($0) }'`
372					RELOC_OFF=`echo "obase=16;${RELOC_OFF}"|bc |tr '[A-Z]' '[a-z]'`
373				fi
374
375				# If reloc address is assigned, do sub
376				if [ "${FUNCADDR}" != "${RELOC_OFF}" ]; then
377					# Hex -> Dec -> SUB -> Hex
378					FUNCADDR=`echo $((16#${FUNCADDR}))`
379					RELOC_OFF=`echo $((16#${RELOC_OFF}))`
380					FUNCADDR=$((FUNCADDR-RELOC_OFF))
381					FUNCADDR=$(echo "obase=16;${FUNCADDR}"|bc |tr '[A-Z]' '[a-z]')
382				fi
383
384				echo
385				sed -n "/${FUNCADDR}/p" ${sym}
386				${TOOLCHAIN_ADDR2LINE} -e ${elf} ${FUNCADDR}
387				exit 0
388			fi
389			;;
390	esac
391}
392
393# We select chip info to do:
394#	1. RKCHIP: fixup platform configure
395#	2. RKCHIP_LOADER: search ini file to pack loader
396#	3. RKCHIP_TRUST: search ini file to pack trust
397#	4. RKCHIP_LABEL: show build message
398#
399# We read chip info from .config and 'RKCHIP_INI_DESC'
400function select_chip_info()
401{
402	# Read RKCHIP firstly from .config
403	# The regular expression that matching:
404	#  - PX30, PX3SE
405	#  - RK????, RK????X
406	#  - RV????
407	chip_pattern='^CONFIG_ROCKCHIP_[R,P][X,V,K][0-9ESX]{1,5}'
408	RKCHIP=`egrep -o ${chip_pattern} .config`
409
410	# default
411	RKCHIP=${RKCHIP##*_}
412
413	# need fixup ?
414	for item in "${CHIP_TYPE_FIXUP_TABLE[@]}"
415	do
416		config_xxx=`echo $item | awk '{ print $1 }'`
417		if grep  -q "^${config_xxx}=y" .config ; then
418			RKCHIP=`echo $item | awk '{ print $2 }'`
419			RKCHIP_LOADER=`echo $item | awk '{ print $3 }'`
420			RKCHIP_TRUST=`echo  $item | awk '{ print $4 }'`
421			RKCHIP_LABEL=`echo  $item | awk '{ print $5 }'`
422		fi
423	done
424
425	if [ "$RKCHIP_LOADER" = "-" ]; then
426		RKCHIP_LOADER=${RKCHIP}
427	fi
428	if [ "$RKCHIP_TRUST" = "-" ]; then
429		RKCHIP_TRUST=${RKCHIP}
430	fi
431	if [ "$RKCHIP_LABEL" = "-" ]; then
432		RKCHIP_LABEL=${RKCHIP}
433	fi
434}
435
436function fixup_platform_configure()
437{
438	u_kb="-" u_num="-" t_kb="-" t_num="-"  sha="-" rsa="-"
439
440	for item in "${CHIP_CFG_FIXUP_TABLE[@]}"
441	do
442		config_xxx=`echo $item | awk '{ print $1 }'`
443		if grep  -q "^${config_xxx}=y" .config ; then
444			# <*> Fixup rsa/sha pack mode for platforms
445			rsa=`echo $item | awk '{ print $2 }'`
446			sha=`echo $item | awk '{ print $3 }'`
447
448			# <*> Fixup images size pack for platforms, and ini file
449			if grep -q '^CONFIG_ARM64_BOOT_AARCH32=y' .config ; then
450				u_kb=`echo  $item | awk '{ print $6 }' | awk -F "," '{ print $1 }'`
451				t_kb=`echo  $item | awk '{ print $6 }' | awk -F "," '{ print $2 }'`
452				u_num=`echo $item | awk '{ print $7 }' | awk -F "," '{ print $1 }'`
453				t_num=`echo $item | awk '{ print $7 }' | awk -F "," '{ print $2 }'`
454
455				PAD_LOADER=`echo $item | awk '{ print $8 }'`
456				PAD_TRUST=`echo  $item | awk '{ print $9 }'`
457				if [ "$PAD_LOADER" != "-" ]; then
458					RKCHIP_LOADER=${RKCHIP_LOADER}${PAD_LOADER}
459				fi
460				if [ "$PAD_TRUST" != "-" ]; then
461					RKCHIP_TRUST=${RKCHIP_TRUST}${PAD_TRUST}
462				fi
463				RKCHIP_LABEL=${RKCHIP_LABEL}"AARCH32"
464			else
465				u_kb=`echo  $item | awk '{ print $4 }' | awk -F "," '{ print $1 }'`
466				t_kb=`echo  $item | awk '{ print $4 }' | awk -F "," '{ print $2 }'`
467				u_num=`echo $item | awk '{ print $5 }' | awk -F "," '{ print $1 }'`
468				t_num=`echo $item | awk '{ print $5 }' | awk -F "," '{ print $2 }'`
469			fi
470		fi
471	done
472
473	if [ "$sha" != "-" ]; then
474		PLATFORM_SHA="--sha $sha"
475	fi
476	if [ "$rsa" != "-" ]; then
477		PLATFORM_RSA="--rsa $rsa"
478	fi
479	if [ "$u_kb" != "-" ]; then
480		PLATFORM_UBOOT_IMG_SIZE="--size $u_kb $u_num"
481	fi
482	if [ "$t_kb" != "-" ]; then
483		PLATFORM_TRUST_IMG_SIZE="--size $t_kb $t_num"
484	fi
485}
486
487function select_ini_file()
488{
489	# default
490	INI_LOADER=${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini
491	if [ "$ARM64_TRUSTZONE" = "y" ]; then
492		INI_TRUST=${RKBIN}/RKTRUST/${RKCHIP_TRUST}TRUST.ini
493	else
494		INI_TRUST=${RKBIN}/RKTRUST/${RKCHIP_TRUST}TOS.ini
495	fi
496
497	# args
498	if [ "$ARG_INI_TRUST" != "" ]; then
499		INI_TRUST=$ARG_INI_TRUST
500	fi
501	if [ "$ARG_INI_LOADER" != "" ]; then
502		INI_LOADER=$ARG_INI_LOADER
503	fi
504}
505
506function handle_args_late()
507{
508	ARG_FIT="${ARG_FIT} --ini-trust $INI_TRUST --ini-loader $INI_LOADER"
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-mkimg.sh --uboot --boot ${ARG_FIT}
730	else
731		rm uboot.img trust*.img -rf
732		./scripts/fit-mkimg.sh --uboot --no-vboot --no-rebuild ${ARG_FIT}
733		echo "pack uboot.img 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_FIT}
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
781handle_args_late
782sub_commands
783clean_files
784make CROSS_COMPILE=${TOOLCHAIN_GCC} ${OPTION} all --jobs=${JOB}
785pack_images
786finish
787
788