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