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