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