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