xref: /rk3399_rockchip-uboot/make.sh (revision 37f507ff86ab23f03a5391f3b3b8462cf71cc072)
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 #############################################
75BIN_PATH_FIXUP="--replace tools/rk_tools/ ./"
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=
100
101PACK_FORMAT="rk"
102
103# Out env param
104PACK_IGNORE_BL32=$TRUST_PACK_IGNORE_BL32	# Value only: "--ignore-bl32"
105#########################################################################################################
106help()
107{
108	echo
109	echo "Usage:"
110	echo "	./make.sh [board|subcmd|EXT_DTB=<file>]"
111	echo
112	echo "	 - board:   board name of defconfig"
113	echo "	 - subcmd:  |elf*|loader*|spl*|itb|trust*|uboot|map|sym|<addr>|EXT_DTB=*"
114	echo "	 - ini:     assigned ini file to pack trust/loader"
115	echo
116	echo "Output:"
117	echo "	 When board built okay, there are uboot/trust/loader images in current directory"
118	echo
119	echo "Example:"
120	echo
121	echo "1. Build:"
122	echo "	./make.sh evb-rk3399               --- build for evb-rk3399_defconfig"
123	echo "	./make.sh firefly-rk3288           --- build for firefly-rk3288_defconfig"
124	echo "	./make.sh EXT_DTB=rk-kernel.dtb    --- build with exist .config and external dtb"
125	echo "	./make.sh                          --- build with exist .config"
126	echo "	./make.sh env                      --- build envtools"
127	echo
128	echo "2. Pack:"
129	echo "	./make.sh uboot                    --- pack uboot.img"
130	echo "	./make.sh trust                    --- pack trust.img"
131	echo "	./make.sh trust <ini>              --- pack trust img with assigned ini file"
132	echo "	./make.sh loader                   --- pack loader bin"
133	echo "	./make.sh loader <ini>             --- pack loader img with assigned ini file"
134	echo "	./make.sh spl                      --- pack loader with u-boot-spl.bin and u-boot-tpl.bin"
135	echo "	./make.sh spl-s                    --- pack loader only replace miniloader with u-boot-spl.bin"
136	echo "	./make.sh itb                      --- pack u-boot.itb(TODO: bl32 is not included for ARMv8)"
137	echo
138	echo "3. Debug:"
139	echo "	./make.sh elf                      --- dump elf file with -D(default)"
140	echo "	./make.sh elf-S                    --- dump elf file with -S"
141	echo "	./make.sh elf-d                    --- dump elf file with -d"
142	echo "	./make.sh elf-*                    --- dump elf file with -*"
143	echo "	./make.sh <no reloc_addr>          --- dump function symbol and code position of address(no relocated)"
144	echo "	./make.sh <reloc_addr-reloc_off>   --- dump function symbol and code position of address(relocated)"
145	echo "	./make.sh map                      --- cat u-boot.map"
146	echo "	./make.sh sym                      --- cat u-boot.sym"
147}
148
149prepare()
150{
151	local absolute_path cmd dir count
152
153	case $BOARD in
154		# Parse from exit .config
155		''|elf*|loader*|spl*|itb|debug*|trust|uboot|map|sym|env|EXT_DTB=*|fit*)
156		if [ ! -f .config ]; then
157			echo
158			echo "Build failed, Can't find .config"
159			help
160			exit 1
161		fi
162		;;
163	esac
164
165	# Parse help and make defconfig
166	case $BOARD in
167		#Help
168		--help|-help|help|--h|-h)
169		help
170		exit 0
171		;;
172
173		#Subcmd
174		''|elf*|loader*|spl*|itb|debug*|trust*|uboot|map|sym|env|EXT_DTB=*|fit*)
175		;;
176
177		*)
178		#Func address is valid ?
179		if [ -z $(echo ${FUNCADDR} | sed 's/[0-9,a-f,A-F,x,X,-]//g') ]; then
180			return
181		elif [ ! -f configs/${BOARD}_defconfig ]; then
182			echo
183			echo "Can't find: configs/${BOARD}_defconfig"
184			echo
185			echo "******** Rockchip Support List *************"
186			echo "${SUPPORT_LIST}"
187			echo "********************************************"
188			echo
189			exit 1
190		else
191			echo "make for ${BOARD}_defconfig by -j${JOB}"
192			make ${BOARD}_defconfig ${OPTION}
193		fi
194		;;
195	esac
196
197	# Initialize RKBIN
198	if [ -d ${RKBIN_TOOLS} ]; then
199		absolute_path=$(cd `dirname ${RKBIN_TOOLS}`; pwd)
200		RKBIN=${absolute_path}
201	else
202		echo
203		echo "Can't find '../rkbin/' repository, please download it before pack image!"
204		echo "How to obtain? 3 ways:"
205		echo "	1. Login your Rockchip gerrit account: \"Projects\" -> \"List\" -> search \"rk/rkbin\" repository"
206		echo "	2. Github repository: https://github.com/rockchip-linux/rkbin"
207		echo "	3. Download full release SDK repository"
208		exit 1
209	fi
210
211	if grep  -q '^CONFIG_ROCKCHIP_FIT_IMAGE_PACK=y' .config ; then
212		PACK_FORMAT="fit"
213	fi
214}
215
216select_toolchain()
217{
218	local absolute_path
219
220	if grep  -q '^CONFIG_ARM64=y' .config ; then
221		if [ -d ${TOOLCHAIN_ARM64} ]; then
222			absolute_path=$(cd `dirname ${TOOLCHAIN_ARM64}`; pwd)
223			TOOLCHAIN_GCC=${absolute_path}/bin/${GCC_ARM64}
224			TOOLCHAIN_OBJDUMP=${absolute_path}/bin/${OBJ_ARM64}
225			TOOLCHAIN_ADDR2LINE=${absolute_path}/bin/${ADDR2LINE_ARM64}
226		else
227			echo "Can't find toolchain: ${TOOLCHAIN_ARM64}"
228			exit 1
229		fi
230	else
231		if [ -d ${TOOLCHAIN_ARM32} ]; then
232			absolute_path=$(cd `dirname ${TOOLCHAIN_ARM32}`; pwd)
233			TOOLCHAIN_GCC=${absolute_path}/bin/${GCC_ARM32}
234			TOOLCHAIN_OBJDUMP=${absolute_path}/bin/${OBJ_ARM32}
235			TOOLCHAIN_ADDR2LINE=${absolute_path}/bin/${ADDR2LINE_ARM32}
236		else
237			echo "Can't find toolchain: ${TOOLCHAIN_ARM32}"
238			exit 1
239		fi
240	fi
241
242	# echo "toolchain: ${TOOLCHAIN_GCC}"
243}
244
245sub_commands()
246{
247	local cmd=${SUBCMD%-*} opt=${SUBCMD#*-}
248	local elf=u-boot map=u-boot.map sym=u-boot.sym
249
250	if [ "$FILE" == "tpl" -o "$FILE" == "spl" ]; then
251		elf=`find -name u-boot-${FILE}`
252		map=`find -name u-boot-${FILE}.map`
253		sym=`find -name u-boot-${FILE}.sym`
254	fi
255
256	case $cmd in
257		elf)
258		if [ -o ! -f ${elf} ]; then
259			echo "Can't find elf file: ${elf}"
260			exit 1
261		else
262			# default 'cmd' without option, use '-D'
263			if [ "${cmd}" = 'elf' -a "${opt}" = 'elf' ]; then
264				opt=D
265			fi
266			${TOOLCHAIN_OBJDUMP} -${opt} ${elf} | less
267			exit 0
268		fi
269		;;
270
271		debug)
272		./scripts/rkpatch.sh ${opt}
273		exit 0
274		;;
275
276		fit)
277		if [ "$opt" = "s" ]; then
278			./scripts/fit-vboot.sh
279		else
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		EXT_DTB=*)
326		OPTION=${SUBCMD}
327		;;
328
329		*)
330		# Search function and code position of address
331		RELOC_OFF=${FUNCADDR#*-}
332		FUNCADDR=${FUNCADDR%-*}
333		if [ -z $(echo ${FUNCADDR} | sed 's/[0-9,a-f,A-F,x,X,-]//g') ] && [ ${FUNCADDR} ]; then
334			# With prefix: '0x' or '0X'
335			if [ `echo ${FUNCADDR} | sed -n "/0[x,X]/p" | wc -l` -ne 0 ]; then
336				FUNCADDR=`echo $FUNCADDR | awk '{ print strtonum($0) }'`
337				FUNCADDR=`echo "obase=16;${FUNCADDR}"|bc |tr '[A-Z]' '[a-z]'`
338			fi
339			if [ `echo ${RELOC_OFF} | sed -n "/0[x,X]/p" | wc -l` -ne 0 ] && [ ${RELOC_OFF} ]; then
340				RELOC_OFF=`echo $RELOC_OFF | awk '{ print strtonum($0) }'`
341				RELOC_OFF=`echo "obase=16;${RELOC_OFF}"|bc |tr '[A-Z]' '[a-z]'`
342			fi
343
344			# If reloc address is assigned, do sub
345			if [ "${FUNCADDR}" != "${RELOC_OFF}" ]; then
346				# Hex -> Dec -> SUB -> Hex
347				FUNCADDR=`echo $((16#${FUNCADDR}))`
348				RELOC_OFF=`echo $((16#${RELOC_OFF}))`
349				FUNCADDR=$((FUNCADDR-RELOC_OFF))
350				FUNCADDR=$(echo "obase=16;${FUNCADDR}"|bc |tr '[A-Z]' '[a-z]')
351			fi
352
353			echo
354			sed -n "/${FUNCADDR}/p" ${sym}
355			${TOOLCHAIN_ADDR2LINE} -e ${elf} ${FUNCADDR}
356			exit 0
357		fi
358		;;
359	esac
360}
361
362# We select chip info to do:
363#	1. RKCHIP: fixup platform configure
364#	2. RKCHIP_LOADER: search ini file to pack loader
365#	3. RKCHIP_TRUST: search ini file to pack trust
366#	4. RKCHIP_LABEL: show build message
367#
368# We read chip info from .config and 'RKCHIP_INI_DESC'
369select_chip_info()
370{
371	# Read RKCHIP firstly from .config
372	# The regular expression that matching:
373	#  - PX30, PX3SE
374	#  - RK????, RK????X
375	#  - RV????
376	CHIP_PATTERN='^CONFIG_ROCKCHIP_[R,P][X,V,K][0-9ESX]{1,5}'
377	RKCHIP=`egrep -o ${CHIP_PATTERN} .config`
378
379	# default
380	RKCHIP=${RKCHIP##*_}
381
382	# need fixup ?
383	for ITEM in "${CHIP_TYPE_FIXUP_TABLE[@]}"
384	do
385		CONFIG_XXX=`echo $ITEM | awk '{ print $1 }'`
386		if grep  -q "^${CONFIG_XXX}=y" .config ; then
387			RKCHIP=`echo $ITEM | awk '{ print $2 }'`
388			RKCHIP_LOADER=`echo $ITEM | awk '{ print $3 }'`
389			RKCHIP_TRUST=`echo  $ITEM | awk '{ print $4 }'`
390			RKCHIP_LABEL=`echo  $ITEM | awk '{ print $5 }'`
391		fi
392	done
393
394	if [ "$RKCHIP_LOADER" = "-" ]; then
395		RKCHIP_LOADER=${RKCHIP}
396	fi
397	if [ "$RKCHIP_TRUST" = "-" ]; then
398		RKCHIP_TRUST=${RKCHIP}
399	fi
400	if [ "$RKCHIP_LABEL" = "-" ]; then
401		RKCHIP_LABEL=${RKCHIP}
402	fi
403
404	# echo "## $FUNCNAME: $RKCHIP, $RKCHIP_LOADER, $RKCHIP_TRUST, $RKCHIP_LABEL,"
405}
406
407function fixup_platform_configure()
408{
409	CFG_U_KB="-" CFG_U_NUM="-" CFG_T_KB="-" CFG_T_NUM="-"  CFG_SHA="-" CFG_RSA="-"
410
411	for ITEM in "${CHIP_CFG_FIXUP_TABLE[@]}"
412	do
413		CONFIG_XXX=`echo $ITEM | awk '{ print $1 }'`
414		if grep  -q "^${CONFIG_XXX}=y" .config ; then
415			# <*> Fixup rsa/sha pack mode for platforms
416			CFG_RSA=`echo $ITEM | awk '{ print $2 }'`
417			CFG_SHA=`echo $ITEM | awk '{ print $3 }'`
418
419			# <*> Fixup images size pack for platforms, and ini file
420			if grep -q '^CONFIG_ARM64_BOOT_AARCH32=y' .config ; then
421				CFG_U_KB=`echo  $ITEM | awk '{ print $6 }' | awk -F "," '{ print $1 }'`
422				CFG_U_NUM=`echo $ITEM | awk '{ print $7 }' | awk -F "," '{ print $1 }'`
423				CFG_T_KB=`echo  $ITEM | awk '{ print $6 }' | awk -F "," '{ print $2 }'`
424				CFG_T_NUM=`echo $ITEM | awk '{ print $7 }' | awk -F "," '{ print $2 }'`
425
426				PAD_LOADER=`echo $ITEM | awk '{ print $8 }'`
427				PAD_TRUST=`echo  $ITEM | awk '{ print $9 }'`
428				if [ "$PAD_LOADER" != "-" ]; then
429					RKCHIP_LOADER=${RKCHIP_LOADER}${PAD_LOADER}
430				fi
431				if [ "$PAD_TRUST" != "-" ]; then
432					RKCHIP_TRUST=${RKCHIP_TRUST}${PAD_TRUST}
433				fi
434				RKCHIP_LABEL=${RKCHIP_LABEL}"AARCH32"
435			else
436				CFG_U_KB=`echo  $ITEM | awk '{ print $4 }' | awk -F "," '{ print $1 }'`
437				CFG_U_NUM=`echo $ITEM | awk '{ print $5 }' | awk -F "," '{ print $1 }'`
438				CFG_T_KB=`echo  $ITEM | awk '{ print $4 }' | awk -F "," '{ print $2 }'`
439				CFG_T_NUM=`echo $ITEM | awk '{ print $5 }' | awk -F "," '{ print $2 }'`
440			fi
441		fi
442	done
443
444	if [ "$CFG_SHA" != "-" ]; then
445		PLATFORM_SHA="--sha $CFG_SHA"
446	fi
447	if [ "$CFG_RSA" != "-" ]; then
448		PLATFORM_RSA="--rsa $CFG_RSA"
449	fi
450	if [ "$CFG_U_KB" != "-" ]; then
451		PLATFORM_UBOOT_IMG_SIZE="--size $CFG_U_KB $CFG_U_NUM"
452	fi
453	if [ "$CFG_T_KB" != "-" ]; then
454		PLATFORM_TRUST_IMG_SIZE="--size $CFG_T_KB $CFG_T_NUM"
455	fi
456
457	# echo "## $FUNCNAME: $PLATFORM_RSA, $PLATFORM_SHA, $PLATFORM_TRUST_IMG_SIZE, $PLATFORM_UBOOT_IMG_SIZE"
458	# echo "## $FUNCNAME: $RKCHIP_LOADER, $RKCHIP_TRUST, $RKCHIP_LABEL"
459}
460
461pack_uboot_image()
462{
463	local UBOOT_LOAD_ADDR UBOOT_MAX_KB UBOOT_KB HEAD_KB=2
464
465	# Check file size
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	if [ "$PACK_FORMAT" = "rk" ]; then
481		# Pack image
482		UBOOT_LOAD_ADDR=`sed -n "/CONFIG_SYS_TEXT_BASE=/s/CONFIG_SYS_TEXT_BASE=//p" include/autoconf.mk|tr -d '\r'`
483		if [ ! $UBOOT_LOAD_ADDR ]; then
484			UBOOT_LOAD_ADDR=`sed -n "/CONFIG_SYS_TEXT_BASE=/s/CONFIG_SYS_TEXT_BASE=//p" .config|tr -d '\r'`
485		fi
486
487		${RKTOOLS}/loaderimage --pack --uboot u-boot.bin uboot.img ${UBOOT_LOAD_ADDR} ${PLATFORM_UBOOT_IMG_SIZE}
488		# Delete u-boot.img and u-boot-dtb.img, which makes users not be confused with final uboot.img
489		ls u-boot.img >/dev/null 2>&1 && rm u-boot.img -rf
490		ls u-boot-dtb.img >/dev/null 2>&1 && rm u-boot-dtb.img -rf
491		echo "pack uboot okay! Input: u-boot.bin"
492	fi
493}
494
495pack_uboot_itb_image()
496{
497	local ini TEE_OFFSET
498
499	# ARM64
500	if grep -Eq ''^CONFIG_ARM64=y'|'^CONFIG_ARM64_BOOT_AARCH32=y'' .config ; then
501		ini=${RKBIN}/RKTRUST/${RKCHIP_TRUST}${PLATFORM_AARCH32}TRUST.ini
502		if [ ! -f ${ini} ]; then
503			echo "pack trust failed! Can't find: ${ini}"
504			return
505		fi
506
507		bl31=`sed -n '/_bl31_/s/PATH=//p' ${ini} |tr -d '\r'`
508
509		cp ${RKBIN}/${bl31} bl31.elf
510		make CROSS_COMPILE=${TOOLCHAIN_GCC} u-boot.itb
511		echo "pack u-boot.itb okay! Input: ${ini}"
512	else
513		ini=${RKBIN}/RKTRUST/${RKCHIP_TRUST}TOS.ini
514		if [ ! -f ${ini} ]; then
515			echo "pack trust failed! Can't find: ${ini}"
516			return
517		fi
518
519		TOS=`sed -n "/TOS=/s/TOS=//p" ${ini} |tr -d '\r'`
520		TOS_TA=`sed -n "/TOSTA=/s/TOSTA=//p" ${ini} |tr -d '\r'`
521
522		if [ $TOS_TA ]; then
523			cp ${RKBIN}/${TOS_TA} tee.bin
524		elif [ $TOS ]; then
525			cp ${RKBIN}/${TOS} tee.bin
526		else
527			echo "Can't find any tee bin"
528			exit 1
529		fi
530
531		TEE_OFFSET=`sed -n "/ADDR=/s/ADDR=//p" ${ini} |tr -d '\r'`
532		if [ "$TEE_OFFSET" = "" ]; then
533			TEE_OFFSET=0x8400000
534		fi
535
536		SPL_FIT_SOURCE=`sed -n "/CONFIG_SPL_FIT_SOURCE=/s/CONFIG_SPL_FIT_SOURCE=//p" .config | tr -d '""'`
537		if [ ! -z $SPL_FIT_SOURCE ]; then
538			cp $SPL_FIT_SOURCE u-boot.its
539		else
540			SPL_FIT_GENERATOR=`sed -n "/CONFIG_SPL_FIT_GENERATOR=/s/CONFIG_SPL_FIT_GENERATOR=//p" .config | tr -d '""'`
541			$SPL_FIT_GENERATOR $TEE_OFFSET > u-boot.its
542		fi
543
544		./tools/mkimage -f u-boot.its -E u-boot.itb
545		echo "pack u-boot.itb okay! Input: ${ini}"
546	fi
547}
548
549pack_spl_loader_image()
550{
551	local header label="SPL" mode=$1
552	local ini=${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini
553	local temp_ini=${RKBIN}/.temp/${RKCHIP_LOADER}MINIALL.ini
554
555	if [ "$FILE" != "" ]; then
556		ini=$FILE;
557	fi
558
559	if [ ! -f ${ini} ]; then
560		echo "pack TPL+SPL loader failed! Can't find: ${ini}"
561		return
562	fi
563
564	ls ${RKBIN}/.temp >/dev/null 2>&1 && rm ${RKBIN}/.temp -rf
565	mkdir ${RKBIN}/.temp
566
567	# Copy to .temp folder
568	cp spl/u-boot-spl.bin ${RKBIN}/.temp/
569	cp ${ini} ${RKBIN}/.temp/${RKCHIP_LOADER}MINIALL.ini -f
570
571	cd ${RKBIN}
572	if [ "$mode" = 'spl' ]; then	# pack tpl+spl
573		cp tpl/u-boot-tpl.bin ${RKBIN}/.temp/
574		# Update ini
575		label="TPL+SPL"
576		header=`sed -n '/NAME=/s/NAME=//p' ${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini`
577		dd if=${RKBIN}/.temp/u-boot-tpl.bin of=${RKBIN}/.temp/tpl.bin bs=1 skip=4
578		sed -i "1s/^/${header:0:4}/" ${RKBIN}/.temp/tpl.bin
579		sed -i "s/FlashData=.*$/FlashData=.\/.temp\/tpl.bin/"     ${temp_ini}
580	fi
581
582	sed -i "s/FlashBoot=.*$/FlashBoot=.\/.temp\/u-boot-spl.bin/"  ${temp_ini}
583
584	${RKTOOLS}/boot_merger ${BIN_PATH_FIXUP} ${temp_ini}
585	rm ${RKBIN}/.temp -rf
586	cd -
587	ls *_loader_*.bin >/dev/null 2>&1 && rm *_loader_*.bin
588	mv ${RKBIN}/*_loader_*.bin ./
589	filename=`basename *_loader_*.bin`
590	if [[ $filename != *spl* ]]; then
591		rename 's/loader_/spl_loader_/' *_loader_*.bin
592	fi
593	echo "pack loader(${label}) okay! Input: ${ini}"
594	ls ./*_loader_*.bin
595}
596
597pack_loader_image()
598{
599	local ini=${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini
600
601	if [ "$FILE" != "" ]; then
602		ini=$FILE;
603	fi
604
605	if [ ! -f $ini ]; then
606		echo "pack loader failed! Can't find: $ini"
607		return
608	fi
609
610	ls *_loader_*.bin >/dev/null 2>&1 && rm *_loader_*.bin
611
612	numline=`cat $ini | wc -l`
613	if [ $numline -eq 1 ]; then
614		image=`sed -n "/PATH=/p" $ini | tr -d '\r' | cut -d '=' -f 2`
615		cp ${RKBIN}/${image} ./
616		echo "pack loader okay! Input: ${ini}"
617		return;
618	fi
619
620	if [ "$PACK_FORMAT" = "rk" ]; then
621		cd ${RKBIN}
622		${RKTOOLS}/boot_merger ${BIN_PATH_FIXUP} $ini
623		cd - && mv ${RKBIN}/*_loader_*.bin ./
624		echo "pack loader okay! Input: $ini"
625	else
626		./scripts/fit-vboot-uboot.sh --no-vboot --no-rebuild >/dev/null
627		file=`ls *loader*.bin`
628		echo "pack $file okay! Input: $ini"
629	fi
630}
631
632pack_32bit_trust_image()
633{
634	local ini=$1 TOS TOS_TA DARM_BASE TEE_LOAD_ADDR TEE_OUTPUT TEE_OFFSET FORMAT
635
636	if [ ! -f ${ini} ]; then
637		echo "pack trust failed! Can't find: ${ini}"
638		return
639	fi
640
641	# Parse orignal path
642	TOS=`sed -n "/TOS=/s/TOS=//p" ${ini} |tr -d '\r'`
643	TOS_TA=`sed -n "/TOSTA=/s/TOSTA=//p" ${ini} |tr -d '\r'`
644
645	# Parse address and output name
646	TEE_OUTPUT=`sed -n "/OUTPUT=/s/OUTPUT=//p" ${ini} |tr -d '\r'`
647	if [ "$TEE_OUTPUT" = "" ]; then
648		TEE_OUTPUT="./trust.img"
649	fi
650	TEE_OFFSET=`sed -n "/ADDR=/s/ADDR=//p" ${ini} |tr -d '\r'`
651	if [ "$TEE_OFFSET" = "" ]; then
652		TEE_OFFSET=0x8400000
653	fi
654
655	# OP-TEE is 132M(0x8400000) offset from DRAM base.
656	DARM_BASE=`sed -n "/CONFIG_SYS_SDRAM_BASE=/s/CONFIG_SYS_SDRAM_BASE=//p" include/autoconf.mk|tr -d '\r'`
657	TEE_LOAD_ADDR=$((DARM_BASE+TEE_OFFSET))
658
659	# Convert Dec to Hex
660	TEE_LOAD_ADDR=$(echo "obase=16;${TEE_LOAD_ADDR}"|bc)
661
662	# Replace "./tools/rk_tools/" with "./" to compatible legacy ini content of rkdevelop branch
663	TOS=$(echo ${TOS} | sed "s/tools\/rk_tools\//\.\//g")
664	TOS_TA=$(echo ${TOS_TA} | sed "s/tools\/rk_tools\//\.\//g")
665
666	if [ "$PACK_FORMAT" = "rk" ]; then
667		if [ $TOS_TA ]; then
668			${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS_TA} ${TEE_OUTPUT} ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE}
669		elif [ $TOS ]; then
670			${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS}    ${TEE_OUTPUT} ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE}
671		else
672			echo "Can't find any tee bin"
673			exit 1
674		fi
675
676		echo "pack trust okay! Input: ${ini}"
677	else
678		./scripts/fit-vboot-uboot.sh --no-vboot --no-rebuild >/dev/null
679		ls uboot.img >/dev/null 2>&1 && rm uboot.img -rf
680		ls trust.img >/dev/null 2>&1 && rm trust.img -rf
681		echo "pack uboot.fit (with uboot trust) okay! Input: ${ini}"
682	fi
683
684	echo
685}
686
687pack_64bit_trust_image()
688{
689	local ini=$1
690
691	if [ ! -f ${ini} ]; then
692		echo "pack trust failed! Can't find: ${ini}"
693		return
694	fi
695
696	cd ${RKBIN}
697	${RKTOOLS}/trust_merger ${PLATFORM_SHA} ${PLATFORM_RSA} ${PLATFORM_TRUST_IMG_SIZE} ${BIN_PATH_FIXUP} \
698				${PACK_IGNORE_BL32} ${ini}
699
700	cd - && mv ${RKBIN}/trust*.img ./
701	echo "pack trust okay! Input: ${ini}"
702	echo
703}
704
705pack_trust_image()
706{
707	local ini
708
709	ls trust*.img >/dev/null 2>&1 && rm trust*.img
710
711	# ARM64 uses trust_merger
712	if grep -Eq ''^CONFIG_ARM64=y'|'^CONFIG_ARM64_BOOT_AARCH32=y'' .config ; then
713		ini=${RKBIN}/RKTRUST/${RKCHIP_TRUST}TRUST.ini
714		if [ "$FILE" != "" ]; then
715			ini=$FILE;
716		fi
717
718		numline=`cat $ini | wc -l`
719		if [ $numline -eq 1 ]; then
720			image=`sed -n "/PATH=/p" $ini | tr -d '\r' | cut -d '=' -f 2`
721			cp ${RKBIN}/${image} ./trust.img
722			echo "pack trust okay! Input: ${ini}"
723			return;
724		fi
725		pack_64bit_trust_image ${ini}
726	# ARM uses loaderimage
727	else
728		ini=${RKBIN}/RKTRUST/${RKCHIP_TRUST}TOS.ini
729		if [ "$FILE" != "" ]; then
730			ini=$FILE;
731		fi
732		pack_32bit_trust_image ${ini}
733	fi
734}
735
736finish()
737{
738	echo
739	if [ ! -z "$OPTION" ]; then
740		echo "Platform ${RKCHIP_LABEL} is build OK, with exist .config ($OPTION)"
741	elif [ "$BOARD" = '' ]; then
742		echo "Platform ${RKCHIP_LABEL} is build OK, with exist .config"
743	else
744		echo "Platform ${RKCHIP_LABEL} is build OK, with new .config(make ${BOARD}_defconfig)"
745	fi
746}
747
748prepare
749select_toolchain
750select_chip_info
751fixup_platform_configure
752sub_commands
753make CROSS_COMPILE=${TOOLCHAIN_GCC} ${OPTION} all --jobs=${JOB}
754pack_uboot_image
755pack_loader_image
756pack_trust_image
757finish
758