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