xref: /rk3399_rockchip-uboot/make.sh (revision 7e0be77742a5903a97b5f26200cf9bf3a517d318)
1#!/bin/bash
2#
3# Copyright (c) 2019 Fuzhou Rockchip Electronics Co., Ltd
4#
5# SPDX-License-Identifier: GPL-2.0
6#
7
8set -e
9JOB=`sed -n "N;/processor/p" /proc/cpuinfo|wc -l`
10SUPPORT_LIST=`ls configs/*[r,p][x,v,k][0-9][0-9]*_defconfig`
11
12# @LOADER: map to $RKCHIP_LOADER for loader ini
13# @TRUST:  map to $RKCHIP_TRUST for trust ini
14# @LABEL:  map to $RKCHIP_LEBEL for verbose message
15# @-:      default state/value
16CHIP_TYPE_FIXUP_TABLE=(
17	# CONFIG_XXX                         RKCHIP         LOADER       TRUST         LABEL
18	"CONFIG_ROCKCHIP_RK3368              RK3368H         -            -             -"
19	"CONFIG_ROCKCHIP_RV1108              RV110X          -            -             -"
20	"CONFIG_ROCKCHIP_PX3SE               PX3SE           -            -             -"
21	"CONFIG_ROCKCHIP_RK3126              RK3126          -            -             -"
22	"CONFIG_ROCKCHIP_RK3326              RK3326          -            -             -"
23	"CONFIG_ROCKCHIP_RK3128X             RK3128X         -            -             -"
24	"CONFIG_ROCKCHIP_PX5                 PX5             -            -             -"
25	"CONFIG_ROCKCHIP_RK3399PRO           RK3399PRO       -            -             -"
26	"CONFIG_ROCKCHIP_RK1806              RK1806          -            -             -"
27	"CONFIG_TARGET_GVA_RK3229            RK322X          RK322XAT     -             -"
28	"CONFIG_COPROCESSOR_RK1808           RKNPU-LION      RKNPULION    RKNPULION     -"
29)
30
31# <*> Fixup rsa/sha pack mode for platforms
32#     RSA: RK3308/PX30/RK3326/RK1808 use RSA-PKCS1 V2.1, it's pack magic is "3", and others use default configure.
33#     SHA: RK3368 use rk big endian SHA256, it's pack magic is "2", and others use default configure.
34# <*> Fixup images size pack for platforms
35# <*> Fixup verbose message about AARCH32
36#
37# @RSA:     rsa mode
38# @SHA:     sha mode
39# @A64-KB:  arm64 platform image size: [uboot,trust]
40# @A64-NUM: arm64 platform image number of total: [uboot,trust]
41# @A32-KB:  arm32 platform image size: [uboot,trust]
42# @A32-NUM: arm32 platform image number of total: [uboot,trust]
43# @LOADER:  map to $RKCHIP_LOADER for loader ini
44# @TRUST:   map to $RKCHIP_TRUST for trust ini
45# @-:       default state/value
46CHIP_CFG_FIXUP_TABLE=(
47	# CONFIG_XXX              RSA     SHA     A64-KB      A64-NUM     A32-KB       A32-NUM      LOAER        TRUST
48	"CONFIG_ROCKCHIP_RK3368    -       2       -,-          -,-        -,-          -,-           -           -"
49	"CONFIG_ROCKCHIP_RK3036    -       -       512,512      1,1        -,-          -,-           -           -"
50	"CONFIG_ROCKCHIP_PX30      3       -       -,-          -,-        -,-          -,-           -           -"
51	"CONFIG_ROCKCHIP_RK3326    3       -       -,-          -,-        -,-          -,-           AARCH32     -"
52	"CONFIG_ROCKCHIP_RK3308    3       -       1024,1024    2,2        512,512      2,2           -           AARCH32"
53	"CONFIG_ROCKCHIP_RK1808    3       -       1024,1024    2,2        -,-          -,-           -           -"
54	"CONFIG_ROCKCHIP_RV1126    3       -       -,-          -,-        -,-          -,-           -           -"
55)
56
57########################################### User can modify #############################################
58# User's rkbin tool relative path
59RKBIN_TOOLS=../rkbin/tools
60
61# User's GCC toolchain and relative path
62ADDR2LINE_ARM32=arm-linux-gnueabihf-addr2line
63ADDR2LINE_ARM64=aarch64-linux-gnu-addr2line
64OBJ_ARM32=arm-linux-gnueabihf-objdump
65OBJ_ARM64=aarch64-linux-gnu-objdump
66GCC_ARM32=arm-linux-gnueabihf-
67GCC_ARM64=aarch64-linux-gnu-
68TOOLCHAIN_ARM32=../prebuilts/gcc/linux-x86/arm/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabihf/bin
69TOOLCHAIN_ARM64=../prebuilts/gcc/linux-x86/aarch64/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin
70
71########################################### User not touch #############################################
72RKTOOLS=./tools
73
74# Declare global INI file searching index name for every chip, update in select_chip_info()
75RKCHIP="-"
76RKCHIP_LABEL="-"
77RKCHIP_LOADER="-"
78RKCHIP_TRUST="-"
79
80INI_TRUST=
81INI_LOADER=
82
83# Declare rkbin repository path, updated in prepare()
84RKBIN=
85
86# Declare global toolchain path for CROSS_COMPILE, updated in select_toolchain()
87TOOLCHAIN_GCC=
88TOOLCHAIN_OBJDUMP=
89TOOLCHAIN_ADDR2LINE=
90
91# Declare global default output dir and cmd, update in prepare()
92OPTION=
93
94# Declare global plaform configure, updated in fixup_platform_configure()
95PLATFORM_RSA=
96PLATFORM_SHA=
97PLATFORM_UBOOT_IMG_SIZE=
98PLATFORM_TRUST_IMG_SIZE=
99
100IMAGE_FORMAT="RKFW"
101IMAGE_NOPACK="n"
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*|itb|trust*|uboot|map|sym|<addr>|EXT_DTB=*"
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		IMAGE_FORMAT="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*|debug*|itb|env|EXT_DTB=*|nopack|fit*)
176				ARG_SUBCMD=$1
177				shift 1
178				;;
179
180			--rollback-index-uboot)
181				ARG_ROLLBACK_IDX_UBOOT="--rollback-index-uboot "$2
182				shift 2
183				;;
184
185			--rollback-index-boot)
186				ARG_ROLLBACK_IDX_BOOT="--rollback-index-boot "$2
187				shift 2
188				;;
189
190			map|sym|elf*)
191				ARG_SUBCMD=$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|*.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				# FUNC address
213				if [ -z $(echo $1 | sed 's/[0-9,a-f,A-F,x,X,-]//g') ]; then
214					ARG_FUNCADDR=$1
215				else
216					ARG_BOARD=$1
217					if [ ! -f configs/${ARG_BOARD}_defconfig ]; then
218						echo -e "\n${SUPPORT_LIST}\n"
219						echo "ERROR: No configs/${ARG_BOARD}_defconfig"
220						exit 1
221					else
222						echo "make for ${ARG_BOARD}_defconfig by -j${JOB}"
223						make ${ARG_BOARD}_defconfig ${OPTION}
224					fi
225				fi
226				shift 1
227				;;
228		esac
229	done
230
231	if [ ! -f .config ]; then
232		echo
233		echo "ERROR: No .config"
234		help
235		exit 1
236	fi
237}
238
239function select_toolchain()
240{
241	if grep -q '^CONFIG_ARM64=y' .config ; then
242		if [ -d ${TOOLCHAIN_ARM64} ]; then
243			absolute_path=$(cd `dirname ${TOOLCHAIN_ARM64}`; pwd)
244			TOOLCHAIN_GCC=${absolute_path}/bin/${GCC_ARM64}
245			TOOLCHAIN_OBJDUMP=${absolute_path}/bin/${OBJ_ARM64}
246			TOOLCHAIN_ADDR2LINE=${absolute_path}/bin/${ADDR2LINE_ARM64}
247		else
248			echo "ERROR: No toolchain: ${TOOLCHAIN_ARM64}"
249			exit 1
250		fi
251	else
252		if [ -d ${TOOLCHAIN_ARM32} ]; then
253			absolute_path=$(cd `dirname ${TOOLCHAIN_ARM32}`; pwd)
254			TOOLCHAIN_GCC=${absolute_path}/bin/${GCC_ARM32}
255			TOOLCHAIN_OBJDUMP=${absolute_path}/bin/${OBJ_ARM32}
256			TOOLCHAIN_ADDR2LINE=${absolute_path}/bin/${ADDR2LINE_ARM32}
257		else
258			echo "ERROR: No toolchain: ${TOOLCHAIN_ARM32}"
259			exit 1
260		fi
261	fi
262}
263
264function sub_commands()
265{
266	# skip "--" parameter, such as "--rollback-index-..."
267	if [[ "$ARG_SUBCMD" != "--*" ]]; then
268		cmd=${ARG_SUBCMD%-*}
269		opt=${ARG_SUBCMD#*-}
270	else
271		cmd=$ARG_SUBCMD
272	fi
273
274	if [ "$ARG_S_TPL" == "tpl" -o "$ARG_S_TPL" == "spl" ]; then
275		elf=`find -name u-boot-${ARG_S_TPL}`
276		map=`find -name u-boot-${ARG_S_TPL}.map`
277		sym=`find -name u-boot-${ARG_S_TPL}.sym`
278	else
279		elf=u-boot
280		map=u-boot.map
281		sym=u-boot.sym
282	fi
283
284	case $cmd in
285		elf)
286			if [ ! -f ${elf} ]; then
287				echo "ERROR: No elf: ${elf}"
288				exit 1
289			else
290				# default 'cmd' without option, use '-D'
291				if [ "${cmd}" = 'elf' -a "${opt}" = 'elf' ]; then
292					opt=D
293				fi
294				${TOOLCHAIN_OBJDUMP} -${opt} ${elf} | less
295				exit 0
296			fi
297			;;
298
299		debug)
300			./scripts/rkpatch.sh ${opt}
301			exit 0
302			;;
303
304		fit)
305			if [ "$opt" = "ns" ]; then
306				./scripts/fit-vboot.sh --no-vboot --ini-trust $INI_TRUST --ini-loader $INI_LOADER
307			fi
308			exit 0
309			;;
310
311		map)
312			cat ${map} | less
313			exit 0
314			;;
315
316		sym)
317			cat ${sym} | less
318			exit 0
319			;;
320
321		trust)
322			pack_trust_image
323			exit 0
324			;;
325
326		loader)
327			pack_loader_image
328			exit 0
329			;;
330
331		spl)
332			pack_spl_loader_image ${opt}
333			exit 0
334			;;
335
336		itb)
337			pack_uboot_itb_image
338			exit 0
339			;;
340
341		uboot)
342			pack_uboot_image
343			exit 0
344			;;
345
346		env)
347			make CROSS_COMPILE=${TOOLCHAIN_GCC} envtools
348			exit 0
349			;;
350
351		--rollback-index*)
352			pack_fit_image $ARG_ROLLBACK_IDX_UBOOT $ARG_ROLLBACK_IDX_BOOT --ini-trust $INI_TRUST --ini-loader $INI_LOADER
353			exit 0
354			;;
355
356		EXT_DTB=*)
357			OPTION=${ARG_SUBCMD}
358			;;
359
360		nopack)
361			IMAGE_NOPACK="y"
362			;;
363
364		*)
365			# Search function and code position of address
366			FUNCADDR=$ARG_FUNCADDR
367
368			RELOC_OFF=${FUNCADDR#*-}
369			FUNCADDR=${FUNCADDR%-*}
370			if [ -z $(echo ${FUNCADDR} | sed 's/[0-9,a-f,A-F,x,X,-]//g') ] && [ ${FUNCADDR} ]; then
371				# With prefix: '0x' or '0X'
372				if [ `echo ${FUNCADDR} | sed -n "/0[x,X]/p" | wc -l` -ne 0 ]; then
373					FUNCADDR=`echo $FUNCADDR | awk '{ print strtonum($0) }'`
374					FUNCADDR=`echo "obase=16;${FUNCADDR}"|bc |tr '[A-Z]' '[a-z]'`
375				fi
376				if [ `echo ${RELOC_OFF} | sed -n "/0[x,X]/p" | wc -l` -ne 0 ] && [ ${RELOC_OFF} ]; then
377					RELOC_OFF=`echo $RELOC_OFF | awk '{ print strtonum($0) }'`
378					RELOC_OFF=`echo "obase=16;${RELOC_OFF}"|bc |tr '[A-Z]' '[a-z]'`
379				fi
380
381				# If reloc address is assigned, do sub
382				if [ "${FUNCADDR}" != "${RELOC_OFF}" ]; then
383					# Hex -> Dec -> SUB -> Hex
384					FUNCADDR=`echo $((16#${FUNCADDR}))`
385					RELOC_OFF=`echo $((16#${RELOC_OFF}))`
386					FUNCADDR=$((FUNCADDR-RELOC_OFF))
387					FUNCADDR=$(echo "obase=16;${FUNCADDR}"|bc |tr '[A-Z]' '[a-z]')
388				fi
389
390				echo
391				sed -n "/${FUNCADDR}/p" ${sym}
392				${TOOLCHAIN_ADDR2LINE} -e ${elf} ${FUNCADDR}
393				exit 0
394			fi
395			;;
396	esac
397}
398
399# We select chip info to do:
400#	1. RKCHIP: fixup platform configure
401#	2. RKCHIP_LOADER: search ini file to pack loader
402#	3. RKCHIP_TRUST: search ini file to pack trust
403#	4. RKCHIP_LABEL: show build message
404#
405# We read chip info from .config and 'RKCHIP_INI_DESC'
406function select_chip_info()
407{
408	# Read RKCHIP firstly from .config
409	# The regular expression that matching:
410	#  - PX30, PX3SE
411	#  - RK????, RK????X
412	#  - RV????
413	chip_pattern='^CONFIG_ROCKCHIP_[R,P][X,V,K][0-9ESX]{1,5}'
414	RKCHIP=`egrep -o ${chip_pattern} .config`
415
416	# default
417	RKCHIP=${RKCHIP##*_}
418
419	# need fixup ?
420	for item in "${CHIP_TYPE_FIXUP_TABLE[@]}"
421	do
422		config_xxx=`echo $item | awk '{ print $1 }'`
423		if grep  -q "^${config_xxx}=y" .config ; then
424			RKCHIP=`echo $item | awk '{ print $2 }'`
425			RKCHIP_LOADER=`echo $item | awk '{ print $3 }'`
426			RKCHIP_TRUST=`echo  $item | awk '{ print $4 }'`
427			RKCHIP_LABEL=`echo  $item | awk '{ print $5 }'`
428		fi
429	done
430
431	if [ "$RKCHIP_LOADER" = "-" ]; then
432		RKCHIP_LOADER=${RKCHIP}
433	fi
434	if [ "$RKCHIP_TRUST" = "-" ]; then
435		RKCHIP_TRUST=${RKCHIP}
436	fi
437	if [ "$RKCHIP_LABEL" = "-" ]; then
438		RKCHIP_LABEL=${RKCHIP}
439	fi
440}
441
442function fixup_platform_configure()
443{
444	u_kb="-" u_num="-" t_kb="-" t_num="-"  sha="-" rsa="-"
445
446	for item in "${CHIP_CFG_FIXUP_TABLE[@]}"
447	do
448		config_xxx=`echo $item | awk '{ print $1 }'`
449		if grep  -q "^${config_xxx}=y" .config ; then
450			# <*> Fixup rsa/sha pack mode for platforms
451			rsa=`echo $item | awk '{ print $2 }'`
452			sha=`echo $item | awk '{ print $3 }'`
453
454			# <*> Fixup images size pack for platforms, and ini file
455			if grep -q '^CONFIG_ARM64_BOOT_AARCH32=y' .config ; then
456				u_kb=`echo  $item | awk '{ print $6 }' | awk -F "," '{ print $1 }'`
457				t_kb=`echo  $item | awk '{ print $6 }' | awk -F "," '{ print $2 }'`
458				u_num=`echo $item | awk '{ print $7 }' | awk -F "," '{ print $1 }'`
459				t_num=`echo $item | awk '{ print $7 }' | awk -F "," '{ print $2 }'`
460
461				PAD_LOADER=`echo $item | awk '{ print $8 }'`
462				PAD_TRUST=`echo  $item | awk '{ print $9 }'`
463				if [ "$PAD_LOADER" != "-" ]; then
464					RKCHIP_LOADER=${RKCHIP_LOADER}${PAD_LOADER}
465				fi
466				if [ "$PAD_TRUST" != "-" ]; then
467					RKCHIP_TRUST=${RKCHIP_TRUST}${PAD_TRUST}
468				fi
469				RKCHIP_LABEL=${RKCHIP_LABEL}"AARCH32"
470			else
471				u_kb=`echo  $item | awk '{ print $4 }' | awk -F "," '{ print $1 }'`
472				t_kb=`echo  $item | awk '{ print $4 }' | awk -F "," '{ print $2 }'`
473				u_num=`echo $item | awk '{ print $5 }' | awk -F "," '{ print $1 }'`
474				t_num=`echo $item | awk '{ print $5 }' | awk -F "," '{ print $2 }'`
475			fi
476		fi
477	done
478
479	if [ "$sha" != "-" ]; then
480		PLATFORM_SHA="--sha $sha"
481	fi
482	if [ "$rsa" != "-" ]; then
483		PLATFORM_RSA="--rsa $rsa"
484	fi
485	if [ "$u_kb" != "-" ]; then
486		PLATFORM_UBOOT_IMG_SIZE="--size $u_kb $u_num"
487	fi
488	if [ "$t_kb" != "-" ]; then
489		PLATFORM_TRUST_IMG_SIZE="--size $t_kb $t_num"
490	fi
491}
492
493function select_ini_file()
494{
495	# default
496	INI_LOADER=${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini
497	if [ "$ARM64_TRUSTZONE" = "y" ]; then
498		INI_TRUST=${RKBIN}/RKTRUST/${RKCHIP_TRUST}TRUST.ini
499	else
500		INI_TRUST=${RKBIN}/RKTRUST/${RKCHIP_TRUST}TOS.ini
501	fi
502
503	# args
504	if [ "$ARG_INI_TRUST" != "" ]; then
505		INI_TRUST=$ARG_INI_TRUST
506	fi
507	if [ "$ARG_INI_LOADER" != "" ]; then
508		INI_LOADER=$ARG_INI_LOADER
509	fi
510}
511
512function pack_uboot_image()
513{
514	if [ "$IMAGE_FORMAT" != "RKFW" ]; then
515		return
516	fi
517
518	# Check file size
519	head_kb=2
520	uboot_kb=`ls -l u-boot.bin | awk '{print $5}'`
521	if [ "$PLATFORM_UBOOT_IMG_SIZE" = "" ]; then
522		uboot_max_kb=1046528
523	else
524		uboot_max_kb=`echo $PLATFORM_UBOOT_IMG_SIZE | awk '{print strtonum($2)}'`
525		uboot_max_kb=$(((uboot_max_kb-head_kb)*1024))
526	fi
527
528	if [ $uboot_kb -gt $uboot_max_kb ]; then
529		echo
530		echo "ERROR: pack uboot failed! u-boot.bin actual: $uboot_kb bytes, max limit: $uboot_max_kb bytes"
531		exit 1
532	fi
533
534	# Pack
535	uboot_load_addr=`sed -n "/CONFIG_SYS_TEXT_BASE=/s/CONFIG_SYS_TEXT_BASE=//p" include/autoconf.mk|tr -d '\r'`
536	if [ -z $uboot_load_addr ]; then
537		echo "ERROR: No CONFIG_SYS_TEXT_BASE for u-boot";
538		exit 1
539	fi
540	${RKTOOLS}/loaderimage --pack --uboot u-boot.bin uboot.img ${uboot_load_addr} ${PLATFORM_UBOOT_IMG_SIZE}
541	rm u-boot.img u-boot-dtb.img -rf
542	echo "pack uboot okay! Input: u-boot.bin"
543}
544
545function pack_uboot_itb_image()
546{
547	ini=$INI_TRUST
548
549	if [ ! -f $INI_TRUST ]; then
550		echo "pack trust failed! Can't find: $INI_TRUST"
551		return
552	fi
553
554	if [ "$ARM64_TRUSTZONE" = "y" ]; then
555		bl31=`sed -n '/_bl31_/s/PATH=//p' $ini |tr -d '\r'`
556		cp ${RKBIN}/${bl31} bl31.elf
557		make CROSS_COMPILE=${TOOLCHAIN_GCC} u-boot.itb
558		echo "pack u-boot.itb okay! Input: $ini"
559	else
560		tos_image=`sed -n "/TOS=/s/TOS=//p" $ini |tr -d '\r'`
561		tosta_image=`sed -n "/TOSTA=/s/TOSTA=//p" $ini |tr -d '\r'`
562		if [ $tosta_image ]; then
563			cp ${RKBIN}/${tosta_image} tee.bin
564		elif [ $tos_image ]; then
565			cp ${RKBIN}/${tos_image}   tee.bin
566		else
567			echo "ERROR: No any tee bin"
568			exit 1
569		fi
570
571		tee_offset=`sed -n "/ADDR=/s/ADDR=//p" $ini |tr -d '\r'`
572		if [ "$tee_offset" = "" ]; then
573			tee_offset=0x8400000
574		fi
575
576		mcu_enabled=`awk -F"," '/MCU=/ { printf $3 }' $ini | tr -d ' '`
577		if [ "$mcu_enabled" = "enabled" ]; then
578			mcu_image=`awk -F"," '/MCU=/ { printf $1 }' $ini | tr -d ' ' | cut -c 5-`
579			mcu_offset=`awk -F"," '/MCU=/ { printf $2 }' $ini | tr -d ' '`
580			cp ${RKBIN}/${mcu_image} mcu.bin
581		fi
582
583		SPL_FIT_SOURCE=`sed -n "/CONFIG_SPL_FIT_SOURCE=/s/CONFIG_SPL_FIT_SOURCE=//p" .config | tr -d '""'`
584		if [ ! -z $SPL_FIT_SOURCE ]; then
585			cp $SPL_FIT_SOURCE u-boot.its
586		else
587			SPL_FIT_GENERATOR=`sed -n "/CONFIG_SPL_FIT_GENERATOR=/s/CONFIG_SPL_FIT_GENERATOR=//p" .config | tr -d '""'`
588			$SPL_FIT_GENERATOR $tee_offset $mcu_offset > u-boot.its
589		fi
590		./tools/mkimage -f u-boot.its -E u-boot.itb
591		echo "pack u-boot.itb okay! Input: $ini"
592	fi
593	echo
594}
595
596function pack_spl_loader_image()
597{
598	mode=$1
599	tmp_dir=${RKBIN}/tmp
600	tmp_ini=${tmp_dir}/${RKCHIP_LOADER}MINIALL.ini
601	ini=$INI_LOADER
602	if [ ! -f $INI_LOADER ]; then
603		echo "pack loader failed! Can't find: $INI_LOADER"
604		return
605	fi
606
607	rm ${tmp_dir} -rf
608	mkdir ${tmp_dir} -p
609	cp spl/u-boot-spl.bin ${tmp_dir}/
610	cp $ini $tmp_ini
611	if [ "$mode" = 'spl' ]; then	# pack tpl+spl
612		label="TPL+SPL"
613		cp tpl/u-boot-tpl.bin ${tmp_dir}/
614		header=`sed -n '/NAME=/s/NAME=//p' ${ini}`
615		dd if=${tmp_dir}/u-boot-tpl.bin of=${tmp_dir}/tpl.bin bs=1 skip=4
616		sed -i "1s/^/${header:0:4}/" ${tmp_dir}/tpl.bin
617		sed -i "s/FlashData=.*$/FlashData=.\/tmp\/tpl.bin/" $tmp_ini
618	else
619		label="SPL"
620	fi
621
622	sed -i "s/FlashBoot=.*$/FlashBoot=.\/tmp\/u-boot-spl.bin/" $tmp_ini
623	cd ${RKBIN}
624	${RKTOOLS}/boot_merger $tmp_ini
625	rm ${tmp_dir} -rf
626	cd -
627	rm *_loader_*.bin -rf
628	mv ${RKBIN}/*_loader_*.bin ./
629
630	filename=`basename *_loader_*.bin`
631	if [[ $filename != *spl* ]]; then
632		rename 's/loader_/spl_loader_/' *_loader_*.bin
633	fi
634	echo "pack loader(${label}) okay! Input: $ini"
635}
636
637function pack_loader_image()
638{
639	ini=$INI_LOADER
640	if [ ! -f $INI_LOADER ]; then
641		echo "pack loader failed! Can't find: $INI_LOADER"
642		return
643	fi
644
645	rm *_loader_*.bin -rf
646	numline=`cat $ini | wc -l`
647	if [ $numline -eq 1 ]; then
648		image=`sed -n "/PATH=/p" $ini | tr -d '\r' | cut -d '=' -f 2`
649		cp ${RKBIN}/${image} ./
650	else
651		cd ${RKBIN}
652		${RKTOOLS}/boot_merger $ini
653		cd - && mv ${RKBIN}/*_loader_*.bin ./
654	fi
655
656	file=`ls *loader*.bin`
657	echo "pack $file okay! Input: $ini"
658}
659
660function pack_arm32_trust_image()
661{
662	ini=$1
663	tos_image=`sed -n "/TOS=/s/TOS=//p" $ini |tr -d '\r'`
664	tosta_image=`sed -n "/TOSTA=/s/TOSTA=//p" $ini |tr -d '\r'`
665	tee_output=`sed -n "/OUTPUT=/s/OUTPUT=//p" $ini |tr -d '\r'`
666	if [ "$tee_output" = "" ]; then
667		tee_output="./trust.img"
668	fi
669	tee_offset=`sed -n "/ADDR=/s/ADDR=//p" $ini |tr -d '\r'`
670	if [ "$tee_offset" = "" ]; then
671		tee_offset=0x8400000
672	fi
673
674	# OP-TEE is 132M(0x8400000) offset from DRAM base.
675	dram_base=`sed -n "/CONFIG_SYS_SDRAM_BASE=/s/CONFIG_SYS_SDRAM_BASE=//p" include/autoconf.mk|tr -d '\r'`
676	tee_load_addr=$((dram_base+tee_offset))
677	tee_load_addr=$(echo "obase=16;${tee_load_addr}"|bc) # Convert Dec to Hex
678
679	if [ $tosta_image ]; then
680		${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${tosta_image} ${tee_output} ${tee_load_addr} ${PLATFORM_TRUST_IMG_SIZE}
681	elif [ $tos_image ]; then
682		${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${tos_image}   ${tee_output} ${tee_load_addr} ${PLATFORM_TRUST_IMG_SIZE}
683	else
684		echo "ERROR: No any tee bin"
685		exit 1
686	fi
687	echo "pack trust okay! Input: $ini"
688}
689
690function pack_arm64_trust_image()
691{
692	ini=$1
693	cd ${RKBIN}
694	${RKTOOLS}/trust_merger ${PLATFORM_SHA} ${PLATFORM_RSA} ${PLATFORM_TRUST_IMG_SIZE} $ini
695	cd - && mv ${RKBIN}/trust*.img ./
696	echo "pack trust okay! Input: $ini"
697}
698
699function pack_trust_image()
700{
701	if [ "$IMAGE_FORMAT" != "RKFW" ]; then
702		return
703	fi
704
705	rm trust*.img -rf
706	ini=$INI_TRUST
707	if [ ! -f $INI_TRUST ]; then
708		echo "pack trust failed! Can't find: $INI_TRUST"
709		return
710	fi
711
712	numline=`cat $ini | wc -l`
713	if [ $numline -eq 1 ]; then
714		image=`sed -n "/PATH=/p" $ini | tr -d '\r' | cut -d '=' -f 2`
715		cp ${RKBIN}/${image} ./trust.img
716		echo "pack trust okay! Input: $ini"
717		return;
718	else
719		if [ "$ARM64_TRUSTZONE" = "y" ]; then
720			pack_arm64_trust_image $ini
721		else
722			pack_arm32_trust_image $ini
723		fi
724	fi
725}
726
727function pack_fit_image()
728{
729	if grep -q '^CONFIG_FIT_SIGNATURE=y' .config ; then
730		./scripts/fit-vboot.sh $ARG_ROLLBACK_IDX_UBOOT $ARG_ROLLBACK_IDX_BOOT --ini-trust $INI_TRUST --ini-loader $INI_LOADER
731	else
732		rm uboot.img trust*.img -rf
733		./scripts/fit-vboot-uboot.sh --no-vboot --no-rebuild --ini-trust $INI_TRUST --ini-loader $INI_LOADER
734		echo "pack uboot.img (with uboot trust) okay! Input: $INI_TRUST"
735	fi
736}
737
738function pack_images()
739{
740	if [ "$IMAGE_NOPACK" != "y" ]; then
741		if [ "$IMAGE_FORMAT" = "RKFW" ]; then
742			pack_uboot_image
743			pack_trust_image
744			pack_loader_image
745		elif [ "$IMAGE_FORMAT" = "FIT" ]; then
746			pack_fit_image $ARG_ROLLBACK_IDX_UBOOT $ARG_ROLLBACK_IDX_BOOT --ini-trust $INI_TRUST --ini-loader $INI_LOADER
747		fi
748	fi
749}
750
751function clean_files()
752{
753	if [ -f spl/u-boot-spl.dtb ]; then
754		rm spl/u-boot-spl.dtb -rf
755	fi
756	if [ -f tpl/u-boot-tpl.dtb ]; then
757		rm tpl/u-boot-tpl.dtb -rf
758	fi
759	if [ -f u-boot.dtb ]; then
760		rm u-boot.dtb -rf
761	fi
762}
763
764function finish()
765{
766	echo
767	if [ ! -z "$OPTION" ]; then
768		echo "Platform ${RKCHIP_LABEL} is build OK, with exist .config ($OPTION)"
769	elif [ "$ARG_BOARD" = '' ]; then
770		echo "Platform ${RKCHIP_LABEL} is build OK, with exist .config"
771	else
772		echo "Platform ${RKCHIP_LABEL} is build OK, with new .config(make ${ARG_BOARD}_defconfig)"
773	fi
774}
775
776process_args $*
777prepare
778select_toolchain
779select_chip_info
780fixup_platform_configure
781select_ini_file
782sub_commands
783clean_files
784make CROSS_COMPILE=${TOOLCHAIN_GCC} ${OPTION} all --jobs=${JOB}
785pack_images
786finish
787