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