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