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