xref: /rk3399_rockchip-uboot/make.sh (revision 1a2ec6b5b5dc2ccaffdddb21e07236a571ed2227)
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=y' .config ; then
452				u_kb=`echo  ${item} | awk '{ print $4 }' | awk -F "," '{ print $1 }'`
453				t_kb=`echo  ${item} | awk '{ print $4 }' | awk -F "," '{ print $2 }'`
454				u_num=`echo ${item} | awk '{ print $5 }' | awk -F "," '{ print $1 }'`
455				t_num=`echo ${item} | awk '{ print $5 }' | awk -F "," '{ print $2 }'`
456			else
457				u_kb=`echo  ${item} | awk '{ print $6 }' | awk -F "," '{ print $1 }'`
458				t_kb=`echo  ${item} | awk '{ print $6 }' | awk -F "," '{ print $2 }'`
459				u_num=`echo ${item} | awk '{ print $7 }' | awk -F "," '{ print $1 }'`
460				t_num=`echo ${item} | awk '{ print $7 }' | awk -F "," '{ print $2 }'`
461				# AArch32
462				if grep -q '^CONFIG_ARM64_BOOT_AARCH32=y' .config ; then
463					PADDING=`echo ${item} | awk '{ print $8 }'`
464					if [ "${PADDING}" != "-" ]; then
465						RKCHIP_LOADER=${RKCHIP_LOADER}${PADDING}
466					fi
467					PADDING=`echo  ${item} | awk '{ print $9 }'`
468					if [ "${PADDING}" != "-" ]; then
469						RKCHIP_TRUST=${RKCHIP_TRUST}${PADDING}
470					fi
471					RKCHIP_LABEL=${RKCHIP_LABEL}"AARCH32"
472				fi
473			fi
474		fi
475	done
476
477	if [ "${sha}" != "-" ]; then
478		PLATFORM_SHA="--sha ${sha}"
479	fi
480	if [ "${rsa}" != "-" ]; then
481		PLATFORM_RSA="--rsa ${rsa}"
482	fi
483	if [ "${u_kb}" != "-" ]; then
484		PLATFORM_UBOOT_SIZE="--size ${u_kb} ${u_num}"
485	fi
486	if [ "${t_kb}" != "-" ]; then
487		PLATFORM_TRUST_SIZE="--size ${t_kb} ${t_num}"
488	fi
489}
490
491function select_ini_file()
492{
493	# default
494	INI_LOADER=${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini
495	if [ "${ARM64_TRUSTZONE}" == "y" ]; then
496		INI_TRUST=${RKBIN}/RKTRUST/${RKCHIP_TRUST}TRUST.ini
497	else
498		INI_TRUST=${RKBIN}/RKTRUST/${RKCHIP_TRUST}TOS.ini
499	fi
500
501	# args
502	if [ "${ARG_INI_TRUST}" != "" ]; then
503		INI_TRUST=${ARG_INI_TRUST}
504	fi
505	if [ "${ARG_INI_LOADER}" != "" ]; then
506		INI_LOADER=${ARG_INI_LOADER}
507	fi
508}
509
510function handle_args_late()
511{
512	ARG_LIST_FIT="${ARG_LIST_FIT} --ini-trust ${INI_TRUST} --ini-loader ${INI_LOADER}"
513}
514
515function pack_uboot_image()
516{
517	if [ "${PLATFORM_TYPE}" != "RKFW" ]; then
518		return
519	fi
520
521	# Check file size
522	head_kb=2
523	uboot_kb=`ls -l u-boot.bin | awk '{ print $5 }'`
524	if [ "${PLATFORM_UBOOT_SIZE}" == "" ]; then
525		uboot_max_kb=1046528
526	else
527		uboot_max_kb=`echo ${PLATFORM_UBOOT_SIZE} | awk '{print strtonum($2)}'`
528		uboot_max_kb=$(((uboot_max_kb-head_kb)*1024))
529	fi
530
531	if [ ${uboot_kb} -gt ${uboot_max_kb} ]; then
532		echo
533		echo "ERROR: pack uboot failed! u-boot.bin actual: ${uboot_kb} bytes, max limit: ${uboot_max_kb} bytes"
534		exit 1
535	fi
536
537	# Pack
538	uboot_load_addr=`sed -n "/CONFIG_SYS_TEXT_BASE=/s/CONFIG_SYS_TEXT_BASE=//p" include/autoconf.mk|tr -d '\r'`
539	if [ -z ${uboot_load_addr} ]; then
540		echo "ERROR: No CONFIG_SYS_TEXT_BASE for u-boot";
541		exit 1
542	fi
543	${RKTOOLS}/loaderimage --pack --uboot u-boot.bin uboot.img ${uboot_load_addr} ${PLATFORM_UBOOT_SIZE}
544	rm u-boot.img u-boot-dtb.img -rf
545	echo "pack uboot okay! Input: u-boot.bin"
546}
547
548function pack_uboot_itb_image()
549{
550	ini=${INI_TRUST}
551	if [ ! -f ${INI_TRUST} ]; then
552		echo "pack trust failed! Can't find: ${INI_TRUST}"
553		return
554	fi
555
556	if [ "${ARM64_TRUSTZONE}" == "y" ]; then
557		bl31=`sed -n '/_bl31_/s/PATH=//p' ${ini} |tr -d '\r'`
558		cp ${RKBIN}/${bl31} bl31.elf
559		make CROSS_COMPILE=${TOOLCHAIN_GCC} u-boot.itb
560		echo "pack u-boot.itb okay! Input: ${ini}"
561	else
562		tos_image=`sed -n "/TOS=/s/TOS=//p" ${ini} |tr -d '\r'`
563		tosta_image=`sed -n "/TOSTA=/s/TOSTA=//p" ${ini} |tr -d '\r'`
564		if [ ${tosta_image} ]; then
565			cp ${RKBIN}/${tosta_image} tee.bin
566		elif [ ${tos_image} ]; then
567			cp ${RKBIN}/${tos_image}   tee.bin
568		else
569			echo "ERROR: No any tee bin"
570			exit 1
571		fi
572
573		tee_offset=`sed -n "/ADDR=/s/ADDR=//p" ${ini} |tr -d '\r'`
574		if [ "${tee_offset}" == "" ]; then
575			tee_offset=0x8400000
576		fi
577
578		mcu_enabled=`awk -F"," '/MCU=/ { printf $3 }' ${ini} | tr -d ' '`
579		if [ "${mcu_enabled}" == "enabled" ]; then
580			mcu_image=`awk -F"," '/MCU=/  { printf $1 }' ${ini} | tr -d ' ' | cut -c 5-`
581			cp ${RKBIN}/${mcu_image} mcu.bin
582			mcu_offset=`awk -F"," '/MCU=/ { printf $2 }' ${ini} | tr -d ' '`
583			optional_mcu="-m "${mcu_offset}
584		else
585			optional_mcu=
586		fi
587
588		compression=`awk -F"," '/COMPRESSION=/  { printf $1 }' ${ini} | tr -d ' ' | cut -c 13-`
589		if [ -z "${compression}" ]; then
590			compression="none"
591		fi
592
593		SPL_FIT_SOURCE=`sed -n "/CONFIG_SPL_FIT_SOURCE=/s/CONFIG_SPL_FIT_SOURCE=//p" .config | tr -d '""'`
594		if [ ! -z ${SPL_FIT_SOURCE} ]; then
595			cp ${SPL_FIT_SOURCE} u-boot.its
596		else
597			SPL_FIT_GENERATOR=`sed -n "/CONFIG_SPL_FIT_GENERATOR=/s/CONFIG_SPL_FIT_GENERATOR=//p" .config | tr -d '""'`
598			${SPL_FIT_GENERATOR} -u -t ${tee_offset} -c ${compression} ${optional_mcu} > u-boot.its
599		fi
600		./tools/mkimage -f u-boot.its -E u-boot.itb
601		echo "pack u-boot.itb okay! Input: ${ini}"
602	fi
603	echo
604}
605
606function pack_spl_loader_image()
607{
608	mode=$1
609	tmpdir=${RKBIN}/tmp
610	tmpini=${tmpdir}/${RKCHIP_LOADER}MINIALL.ini
611	ini=${INI_LOADER}
612	if [ ! -f ${INI_LOADER} ]; then
613		echo "pack loader failed! Can't find: ${INI_LOADER}"
614		return
615	fi
616
617	# Find magic for TPL
618	for item in "${CHIP_TPL_MAGIC_TABLE[@]}"
619	do
620		config_xxx=`echo ${item} | awk '{ print $1 }'`
621		if grep -q "^${config_xxx}=y" .config ; then
622			header=`echo ${item} | awk '{ print $2 }'`
623		fi
624	done
625
626	if [ -z ${header} ]; then
627		header=`sed -n '/NAME=/s/NAME=//p' ${ini}`
628	fi
629
630	# Prepare files
631	rm ${tmpdir} -rf && mkdir ${tmpdir} -p
632	cp spl/u-boot-spl.bin ${tmpdir}/ && cp ${ini} ${tmpini}
633
634	if [ "${mode}" == "tpl-spl" ]; then	# pack tpl+spl
635		label="TPL+SPL"
636		cp tpl/u-boot-tpl.bin ${tmpdir}/
637		dd if=${tmpdir}/u-boot-tpl.bin of=${tmpdir}/tpl.bin bs=1 skip=4
638		sed -i "1s/^/${header:0:4}/" ${tmpdir}/tpl.bin
639		sed -i "s/FlashData=.*$/FlashData=.\/tmp\/tpl.bin/" ${tmpini}
640		sed -i "0,/Path1=.*/s/Path1=.*$/Path1=.\/tmp\/tpl.bin/" ${tmpini}
641		sed -i "s/FlashBoot=.*$/FlashBoot=.\/tmp\/u-boot-spl.bin/" ${tmpini}
642	elif [ "${mode}" == "tpl" ]; then	# pack tpl
643		label="TPL"
644		cp tpl/u-boot-tpl.bin ${tmpdir}/
645		dd if=${tmpdir}/u-boot-tpl.bin of=${tmpdir}/tpl.bin bs=1 skip=4
646		sed -i "1s/^/${header:0:4}/" ${tmpdir}/tpl.bin
647		sed -i "s/FlashData=.*$/FlashData=.\/tmp\/tpl.bin/" ${tmpini}
648		sed -i "0,/Path1=.*/s/Path1=.*$/Path1=.\/tmp\/tpl.bin/" ${tmpini}
649	else
650		label="SPL"
651		sed -i "s/FlashBoot=.*$/FlashBoot=.\/tmp\/u-boot-spl.bin/" ${tmpini}
652	fi
653
654	# Pack
655	cd ${RKBIN}
656	${RKTOOLS}/boot_merger ${tmpini}
657
658	rm ${tmpdir} -rf && cd -
659	rm *_loader_*.bin -rf && mv ${RKBIN}/*_loader_*.bin ./
660	filename=`basename *_loader_*.bin`
661	if [[ ${filename} != *spl* ]]; then
662		rename 's/loader_/spl_loader_/' *_loader_*.bin
663	fi
664	echo "pack loader(${label}) okay! Input: ${ini}"
665}
666
667function pack_loader_image()
668{
669	ini=${INI_LOADER}
670	if [ ! -f ${INI_LOADER} ]; then
671		echo "pack loader failed! Can't find: ${INI_LOADER}"
672		return
673	fi
674
675	rm *_loader_*.bin -rf
676	numline=`cat ${ini} | wc -l`
677	if [ ${numline} -eq 1 ]; then
678		image=`sed -n "/PATH=/p" ${ini} | tr -d '\r' | cut -d '=' -f 2`
679		cp ${RKBIN}/${image} ./
680	else
681		cd ${RKBIN}
682		${RKTOOLS}/boot_merger ${ini}
683		cd - && mv ${RKBIN}/*_loader_*.bin ./
684	fi
685
686	file=`ls *loader*.bin`
687	echo "pack ${file} okay! Input: ${ini}"
688}
689
690function pack_arm32_trust_image()
691{
692	ini=$1
693	tos_image=`sed -n "/TOS=/s/TOS=//p" ${ini} |tr -d '\r'`
694	tosta_image=`sed -n "/TOSTA=/s/TOSTA=//p" ${ini} |tr -d '\r'`
695	tee_output=`sed -n "/OUTPUT=/s/OUTPUT=//p" ${ini} |tr -d '\r'`
696	if [ "${tee_output}" == "" ]; then
697		tee_output="./trust.img"
698	fi
699	tee_offset=`sed -n "/ADDR=/s/ADDR=//p" ${ini} |tr -d '\r'`
700	if [ "${tee_offset}" == "" ]; then
701		tee_offset=0x8400000
702	fi
703
704	# OP-TEE is 132M(0x8400000) offset from DRAM base.
705	dram_base=`sed -n "/CONFIG_SYS_SDRAM_BASE=/s/CONFIG_SYS_SDRAM_BASE=//p" include/autoconf.mk|tr -d '\r'`
706	tee_load_addr=$((dram_base+tee_offset))
707	tee_load_addr=$(echo "obase=16;${tee_load_addr}"|bc) # Convert Dec to Hex
708
709	if [ ${tosta_image} ]; then
710		${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${tosta_image} ${tee_output} ${tee_load_addr} ${PLATFORM_TRUST_SIZE}
711	elif [ ${tos_image} ]; then
712		${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${tos_image}   ${tee_output} ${tee_load_addr} ${PLATFORM_TRUST_SIZE}
713	else
714		echo "ERROR: No any tee bin"
715		exit 1
716	fi
717	echo "pack trust okay! Input: ${ini}"
718}
719
720function pack_arm64_trust_image()
721{
722	ini=$1
723	cd ${RKBIN}
724	${RKTOOLS}/trust_merger ${PLATFORM_SHA} ${PLATFORM_RSA} ${PLATFORM_TRUST_SIZE} ${ini}
725	cd - && mv ${RKBIN}/trust*.img ./
726	echo "pack trust okay! Input: ${ini}"
727}
728
729function pack_trust_image()
730{
731	if [ "${PLATFORM_TYPE}" != "RKFW" ]; then
732		return
733	fi
734
735	rm trust*.img -rf
736	ini=${INI_TRUST}
737	if [ ! -f ${INI_TRUST} ]; then
738		echo "pack trust failed! Can't find: ${INI_TRUST}"
739		return
740	fi
741
742	numline=`cat ${ini} | wc -l`
743	if [ ${numline} -eq 1 ]; then
744		image=`sed -n "/PATH=/p" ${ini} | tr -d '\r' | cut -d '=' -f 2`
745		cp ${RKBIN}/${image} ./trust.img
746		echo "pack trust okay! Input: ${ini}"
747		return;
748	else
749		if [ "${ARM64_TRUSTZONE}" == "y" ]; then
750			pack_arm64_trust_image ${ini}
751		else
752			pack_arm32_trust_image ${ini}
753		fi
754	fi
755}
756
757function pack_fit_image()
758{
759	if [ "${ARG_NO_UBOOT}" == "y" ]; then
760		rm u-boot-nodtb.bin u-boot.dtb -rf
761		touch u-boot-nodtb.bin u-boot.dtb
762	fi
763
764	if grep -q '^CONFIG_FIT_SIGNATURE=y' .config ; then
765		./scripts/fit-mkimg.sh --uboot-itb --boot-itb ${ARG_LIST_FIT}
766	else
767		rm uboot.img trust*.img -rf
768		./scripts/fit-mkimg.sh --uboot-itb --no-vboot --no-rebuild ${ARG_LIST_FIT}
769		echo "pack uboot.img okay! Input: ${INI_TRUST}"
770	fi
771}
772
773function pack_images()
774{
775	if [ "${ARG_NO_PACK}" != "y" ]; then
776		if [ "${PLATFORM_TYPE}" == "RKFW" ]; then
777			pack_uboot_image
778			pack_trust_image
779			pack_loader_image
780		elif [ "${PLATFORM_TYPE}" == "FIT" ]; then
781			pack_fit_image ${ARG_LIST_FIT}
782		fi
783	fi
784}
785
786function clean_files()
787{
788	rm spl/u-boot-spl.dtb tpl/u-boot-tpl.dtb u-boot.dtb -rf
789}
790
791function finish()
792{
793	echo
794	if [ "${ARG_BOARD}" == "" ]; then
795		echo "Platform ${RKCHIP_LABEL} is build OK, with exist .config"
796	else
797		echo "Platform ${RKCHIP_LABEL} is build OK, with new .config(make ${ARG_BOARD}_defconfig)"
798	fi
799}
800
801process_args $*
802prepare
803select_toolchain
804select_chip_info
805fixup_platform_configure
806select_ini_file
807handle_args_late
808sub_commands
809clean_files
810make CROSS_COMPILE=${TOOLCHAIN_GCC} all --jobs=${JOB}
811pack_images
812finish
813
814