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