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