xref: /rk3399_rockchip-uboot/make.sh (revision f8f8bbc62fc911ca724dd0a6184be5982d300b0e)
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		./scripts/fit-vboot.sh $*
279		exit 0
280		;;
281
282		map)
283		cat ${map} | less
284		exit 0
285		;;
286
287		sym)
288		cat ${sym} | less
289		exit 0
290		;;
291
292		trust)
293		pack_trust_image
294		exit 0
295		;;
296
297		loader)
298		pack_loader_image
299		exit 0
300		;;
301
302		spl)
303		pack_spl_loader_image ${opt}
304		exit 0
305		;;
306
307		itb)
308		pack_uboot_itb_image
309		exit 0
310		;;
311
312		uboot)
313		pack_uboot_image ${opt}
314		exit 0
315		;;
316
317		env)
318		make CROSS_COMPILE=${TOOLCHAIN_GCC} envtools
319		exit 0
320		;;
321
322		EXT_DTB=*)
323		OPTION=${SUBCMD}
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'
366select_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
458pack_uboot_image()
459{
460	local UBOOT_LOAD_ADDR UBOOT_MAX_KB UBOOT_KB HEAD_KB=2
461
462	# Check file size
463	UBOOT_KB=`ls -l u-boot.bin | awk '{print $5}'`
464	if [ "$PLATFORM_UBOOT_IMG_SIZE" = "" ]; then
465		UBOOT_MAX_KB=1046528
466	else
467		UBOOT_MAX_KB=`echo $PLATFORM_UBOOT_IMG_SIZE | awk '{print strtonum($2)}'`
468		UBOOT_MAX_KB=$(((UBOOT_MAX_KB-HEAD_KB)*1024))
469	fi
470
471	if [ $UBOOT_KB -gt $UBOOT_MAX_KB ]; then
472		echo
473		echo "ERROR: pack uboot failed! u-boot.bin actual: $UBOOT_KB bytes, max limit: $UBOOT_MAX_KB bytes"
474		exit 1
475	fi
476
477	if [ "$PACK_FORMAT" = "rk" ]; then
478		# Pack image
479		UBOOT_LOAD_ADDR=`sed -n "/CONFIG_SYS_TEXT_BASE=/s/CONFIG_SYS_TEXT_BASE=//p" include/autoconf.mk|tr -d '\r'`
480		if [ ! $UBOOT_LOAD_ADDR ]; then
481			UBOOT_LOAD_ADDR=`sed -n "/CONFIG_SYS_TEXT_BASE=/s/CONFIG_SYS_TEXT_BASE=//p" .config|tr -d '\r'`
482		fi
483
484		${RKTOOLS}/loaderimage --pack --uboot u-boot.bin uboot.img ${UBOOT_LOAD_ADDR} ${PLATFORM_UBOOT_IMG_SIZE}
485		# Delete u-boot.img and u-boot-dtb.img, which makes users not be confused with final uboot.img
486		ls u-boot.img >/dev/null 2>&1 && rm u-boot.img -rf
487		ls u-boot-dtb.img >/dev/null 2>&1 && rm u-boot-dtb.img -rf
488		echo "pack uboot okay! Input: u-boot.bin"
489	fi
490}
491
492pack_uboot_itb_image()
493{
494	local ini TEE_OFFSET
495
496	# ARM64
497	if grep -Eq ''^CONFIG_ARM64=y'|'^CONFIG_ARM64_BOOT_AARCH32=y'' .config ; then
498		ini=${RKBIN}/RKTRUST/${RKCHIP_TRUST}${PLATFORM_AARCH32}TRUST.ini
499		if [ ! -f ${ini} ]; then
500			echo "pack trust failed! Can't find: ${ini}"
501			return
502		fi
503
504		bl31=`sed -n '/_bl31_/s/PATH=//p' ${ini} |tr -d '\r'`
505
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		ini=${RKBIN}/RKTRUST/${RKCHIP_TRUST}TOS.ini
511		if [ ! -f ${ini} ]; then
512			echo "pack trust failed! Can't find: ${ini}"
513			return
514		fi
515
516		TOS=`sed -n "/TOS=/s/TOS=//p" ${ini} |tr -d '\r'`
517		TOS_TA=`sed -n "/TOSTA=/s/TOSTA=//p" ${ini} |tr -d '\r'`
518
519		if [ $TOS_TA ]; then
520			cp ${RKBIN}/${TOS_TA} tee.bin
521		elif [ $TOS ]; then
522			cp ${RKBIN}/${TOS} tee.bin
523		else
524			echo "Can't find any tee bin"
525			exit 1
526		fi
527
528		TEE_OFFSET=`sed -n "/ADDR=/s/ADDR=//p" ${ini} |tr -d '\r'`
529		if [ "$TEE_OFFSET" = "" ]; then
530			TEE_OFFSET=0x8400000
531		fi
532
533		SPL_FIT_SOURCE=`sed -n "/CONFIG_SPL_FIT_SOURCE=/s/CONFIG_SPL_FIT_SOURCE=//p" .config | tr -d '""'`
534		if [ ! -z $SPL_FIT_SOURCE ]; then
535			cp $SPL_FIT_SOURCE u-boot.its
536		else
537			SPL_FIT_GENERATOR=`sed -n "/CONFIG_SPL_FIT_GENERATOR=/s/CONFIG_SPL_FIT_GENERATOR=//p" .config | tr -d '""'`
538			$SPL_FIT_GENERATOR $TEE_OFFSET > u-boot.its
539		fi
540
541		./tools/mkimage -f u-boot.its -E u-boot.itb
542		echo "pack u-boot.itb okay! Input: ${ini}"
543	fi
544}
545
546pack_spl_loader_image()
547{
548	local header label="SPL" mode=$1
549	local ini=${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini
550	local temp_ini=${RKBIN}/.temp/${RKCHIP_LOADER}MINIALL.ini
551
552	if [ "$FILE" != "" ]; then
553		ini=$FILE;
554	fi
555
556	if [ ! -f ${ini} ]; then
557		echo "pack TPL+SPL loader failed! Can't find: ${ini}"
558		return
559	fi
560
561	ls ${RKBIN}/.temp >/dev/null 2>&1 && rm ${RKBIN}/.temp -rf
562	mkdir ${RKBIN}/.temp
563
564	# Copy to .temp folder
565	cp spl/u-boot-spl.bin ${RKBIN}/.temp/
566	cp ${ini} ${RKBIN}/.temp/${RKCHIP_LOADER}MINIALL.ini -f
567
568	cd ${RKBIN}
569	if [ "$mode" = 'spl' ]; then	# pack tpl+spl
570		cp tpl/u-boot-tpl.bin ${RKBIN}/.temp/
571		# Update ini
572		label="TPL+SPL"
573		header=`sed -n '/NAME=/s/NAME=//p' ${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini`
574		dd if=${RKBIN}/.temp/u-boot-tpl.bin of=${RKBIN}/.temp/tpl.bin bs=1 skip=4
575		sed -i "1s/^/${header:0:4}/" ${RKBIN}/.temp/tpl.bin
576		sed -i "s/FlashData=.*$/FlashData=.\/.temp\/tpl.bin/"     ${temp_ini}
577	fi
578
579	sed -i "s/FlashBoot=.*$/FlashBoot=.\/.temp\/u-boot-spl.bin/"  ${temp_ini}
580
581	${RKTOOLS}/boot_merger ${BIN_PATH_FIXUP} ${temp_ini}
582	rm ${RKBIN}/.temp -rf
583	cd -
584	ls *_loader_*.bin >/dev/null 2>&1 && rm *_loader_*.bin
585	mv ${RKBIN}/*_loader_*.bin ./
586	filename=`basename *_loader_*.bin`
587	if [[ $filename != *spl* ]]; then
588		rename 's/loader_/spl_loader_/' *_loader_*.bin
589	fi
590	echo "pack loader(${label}) okay! Input: ${ini}"
591	ls ./*_loader_*.bin
592}
593
594pack_loader_image()
595{
596	local ini=${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini
597
598	if [ "$FILE" != "" ]; then
599		ini=$FILE;
600	fi
601
602	if [ ! -f $ini ]; then
603		echo "pack loader failed! Can't find: $ini"
604		return
605	fi
606
607	ls *_loader_*.bin >/dev/null 2>&1 && rm *_loader_*.bin
608
609	numline=`cat $ini | wc -l`
610	if [ $numline -eq 1 ]; then
611		image=`sed -n "/PATH=/p" $ini | tr -d '\r' | cut -d '=' -f 2`
612		cp ${RKBIN}/${image} ./
613		echo "pack loader okay! Input: ${ini}"
614		return;
615	fi
616
617	if [ "$PACK_FORMAT" = "rk" ]; then
618		cd ${RKBIN}
619		${RKTOOLS}/boot_merger ${BIN_PATH_FIXUP} $ini
620		cd - && mv ${RKBIN}/*_loader_*.bin ./
621		echo "pack loader okay! Input: $ini"
622	else
623		./scripts/fit-vboot-uboot.sh --no-vboot --no-rebuild >/dev/null
624		file=`ls *loader*.bin`
625		echo "pack $file okay! Input: $ini"
626	fi
627}
628
629pack_32bit_trust_image()
630{
631	local ini=$1 TOS TOS_TA DARM_BASE TEE_LOAD_ADDR TEE_OUTPUT TEE_OFFSET FORMAT
632
633	if [ ! -f ${ini} ]; then
634		echo "pack trust failed! Can't find: ${ini}"
635		return
636	fi
637
638	# Parse orignal path
639	TOS=`sed -n "/TOS=/s/TOS=//p" ${ini} |tr -d '\r'`
640	TOS_TA=`sed -n "/TOSTA=/s/TOSTA=//p" ${ini} |tr -d '\r'`
641
642	# Parse address and output name
643	TEE_OUTPUT=`sed -n "/OUTPUT=/s/OUTPUT=//p" ${ini} |tr -d '\r'`
644	if [ "$TEE_OUTPUT" = "" ]; then
645		TEE_OUTPUT="./trust.img"
646	fi
647	TEE_OFFSET=`sed -n "/ADDR=/s/ADDR=//p" ${ini} |tr -d '\r'`
648	if [ "$TEE_OFFSET" = "" ]; then
649		TEE_OFFSET=0x8400000
650	fi
651
652	# OP-TEE is 132M(0x8400000) offset from DRAM base.
653	DARM_BASE=`sed -n "/CONFIG_SYS_SDRAM_BASE=/s/CONFIG_SYS_SDRAM_BASE=//p" include/autoconf.mk|tr -d '\r'`
654	TEE_LOAD_ADDR=$((DARM_BASE+TEE_OFFSET))
655
656	# Convert Dec to Hex
657	TEE_LOAD_ADDR=$(echo "obase=16;${TEE_LOAD_ADDR}"|bc)
658
659	# Replace "./tools/rk_tools/" with "./" to compatible legacy ini content of rkdevelop branch
660	TOS=$(echo ${TOS} | sed "s/tools\/rk_tools\//\.\//g")
661	TOS_TA=$(echo ${TOS_TA} | sed "s/tools\/rk_tools\//\.\//g")
662
663	if [ "$PACK_FORMAT" = "rk" ]; then
664		if [ $TOS_TA ]; then
665			${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS_TA} ${TEE_OUTPUT} ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE}
666		elif [ $TOS ]; then
667			${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS}    ${TEE_OUTPUT} ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE}
668		else
669			echo "Can't find any tee bin"
670			exit 1
671		fi
672
673		echo "pack trust okay! Input: ${ini}"
674	else
675		./scripts/fit-vboot-uboot.sh --no-vboot --no-rebuild >/dev/null
676		ls uboot.img >/dev/null 2>&1 && rm uboot.img -rf
677		ls trust.img >/dev/null 2>&1 && rm trust.img -rf
678		echo "pack uboot.fit (with uboot trust) okay! Input: ${ini}"
679	fi
680
681	echo
682}
683
684pack_64bit_trust_image()
685{
686	local ini=$1
687
688	if [ ! -f ${ini} ]; then
689		echo "pack trust failed! Can't find: ${ini}"
690		return
691	fi
692
693	cd ${RKBIN}
694	${RKTOOLS}/trust_merger ${PLATFORM_SHA} ${PLATFORM_RSA} ${PLATFORM_TRUST_IMG_SIZE} ${BIN_PATH_FIXUP} \
695				${PACK_IGNORE_BL32} ${ini}
696
697	cd - && mv ${RKBIN}/trust*.img ./
698	echo "pack trust okay! Input: ${ini}"
699	echo
700}
701
702pack_trust_image()
703{
704	local ini
705
706	ls trust*.img >/dev/null 2>&1 && rm trust*.img
707
708	# ARM64 uses trust_merger
709	if grep -Eq ''^CONFIG_ARM64=y'|'^CONFIG_ARM64_BOOT_AARCH32=y'' .config ; then
710		ini=${RKBIN}/RKTRUST/${RKCHIP_TRUST}TRUST.ini
711		if [ "$FILE" != "" ]; then
712			ini=$FILE;
713		fi
714
715		numline=`cat $ini | wc -l`
716		if [ $numline -eq 1 ]; then
717			image=`sed -n "/PATH=/p" $ini | tr -d '\r' | cut -d '=' -f 2`
718			cp ${RKBIN}/${image} ./trust.img
719			echo "pack trust okay! Input: ${ini}"
720			return;
721		fi
722		pack_64bit_trust_image ${ini}
723	# ARM uses loaderimage
724	else
725		ini=${RKBIN}/RKTRUST/${RKCHIP_TRUST}TOS.ini
726		if [ "$FILE" != "" ]; then
727			ini=$FILE;
728		fi
729		pack_32bit_trust_image ${ini}
730	fi
731}
732
733finish()
734{
735	echo
736	if [ ! -z "$OPTION" ]; then
737		echo "Platform ${RKCHIP_LABEL} is build OK, with exist .config ($OPTION)"
738	elif [ "$BOARD" = '' ]; then
739		echo "Platform ${RKCHIP_LABEL} is build OK, with exist .config"
740	else
741		echo "Platform ${RKCHIP_LABEL} is build OK, with new .config(make ${BOARD}_defconfig)"
742	fi
743}
744
745prepare
746select_toolchain
747select_chip_info
748fixup_platform_configure
749sub_commands
750make CROSS_COMPILE=${TOOLCHAIN_GCC} ${OPTION} all --jobs=${JOB}
751pack_uboot_image
752pack_loader_image
753pack_trust_image
754finish
755