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