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