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