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