xref: /rk3399_rockchip-uboot/make.sh (revision 69b6e59094804837e7b9aeddd37415350cf34096)
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		./scripts/fit-vboot.sh $*
278		exit 0
279		;;
280
281		map)
282		cat ${map} | less
283		exit 0
284		;;
285
286		sym)
287		cat ${sym} | less
288		exit 0
289		;;
290
291		trust)
292		pack_trust_image
293		exit 0
294		;;
295
296		loader)
297		pack_loader_image
298		exit 0
299		;;
300
301		spl)
302		pack_spl_loader_image ${opt}
303		exit 0
304		;;
305
306		itb)
307		pack_uboot_itb_image
308		exit 0
309		;;
310
311		uboot)
312		pack_uboot_image ${opt}
313		exit 0
314		;;
315
316		env)
317		make CROSS_COMPILE=${TOOLCHAIN_GCC} envtools
318		exit 0
319		;;
320
321		EXT_DTB=*)
322		OPTION=${SUBCMD}
323		;;
324
325		*)
326		# Search function and code position of address
327		RELOC_OFF=${FUNCADDR#*-}
328		FUNCADDR=${FUNCADDR%-*}
329		if [ -z $(echo ${FUNCADDR} | sed 's/[0-9,a-f,A-F,x,X,-]//g') ] && [ ${FUNCADDR} ]; then
330			# With prefix: '0x' or '0X'
331			if [ `echo ${FUNCADDR} | sed -n "/0[x,X]/p" | wc -l` -ne 0 ]; then
332				FUNCADDR=`echo $FUNCADDR | awk '{ print strtonum($0) }'`
333				FUNCADDR=`echo "obase=16;${FUNCADDR}"|bc |tr '[A-Z]' '[a-z]'`
334			fi
335			if [ `echo ${RELOC_OFF} | sed -n "/0[x,X]/p" | wc -l` -ne 0 ] && [ ${RELOC_OFF} ]; then
336				RELOC_OFF=`echo $RELOC_OFF | awk '{ print strtonum($0) }'`
337				RELOC_OFF=`echo "obase=16;${RELOC_OFF}"|bc |tr '[A-Z]' '[a-z]'`
338			fi
339
340			# If reloc address is assigned, do sub
341			if [ "${FUNCADDR}" != "${RELOC_OFF}" ]; then
342				# Hex -> Dec -> SUB -> Hex
343				FUNCADDR=`echo $((16#${FUNCADDR}))`
344				RELOC_OFF=`echo $((16#${RELOC_OFF}))`
345				FUNCADDR=$((FUNCADDR-RELOC_OFF))
346				FUNCADDR=$(echo "obase=16;${FUNCADDR}"|bc |tr '[A-Z]' '[a-z]')
347			fi
348
349			echo
350			sed -n "/${FUNCADDR}/p" ${sym}
351			${TOOLCHAIN_ADDR2LINE} -e ${elf} ${FUNCADDR}
352			exit 0
353		fi
354		;;
355	esac
356}
357
358# We select chip info to do:
359#	1. RKCHIP: fixup platform configure
360#	2. RKCHIP_LOADER: search ini file to pack loader
361#	3. RKCHIP_TRUST: search ini file to pack trust
362#	4. RKCHIP_LABEL: show build message
363#
364# We read chip info from .config and 'RKCHIP_INI_DESC'
365select_chip_info()
366{
367	# Read RKCHIP firstly from .config
368	# The regular expression that matching:
369	#  - PX30, PX3SE
370	#  - RK????, RK????X
371	#  - RV????
372	CHIP_PATTERN='^CONFIG_ROCKCHIP_[R,P][X,V,K][0-9ESX]{1,5}'
373	RKCHIP=`egrep -o ${CHIP_PATTERN} .config`
374
375	# default
376	RKCHIP=${RKCHIP##*_}
377
378	# need fixup ?
379	for ITEM in "${CHIP_TYPE_FIXUP_TABLE[@]}"
380	do
381		CONFIG_XXX=`echo $ITEM | awk '{ print $1 }'`
382		if grep  -q "^${CONFIG_XXX}=y" .config ; then
383			RKCHIP=`echo $ITEM | awk '{ print $2 }'`
384			RKCHIP_LOADER=`echo $ITEM | awk '{ print $3 }'`
385			RKCHIP_TRUST=`echo  $ITEM | awk '{ print $4 }'`
386			RKCHIP_LABEL=`echo  $ITEM | awk '{ print $5 }'`
387		fi
388	done
389
390	if [ "$RKCHIP_LOADER" = "-" ]; then
391		RKCHIP_LOADER=${RKCHIP}
392	fi
393	if [ "$RKCHIP_TRUST" = "-" ]; then
394		RKCHIP_TRUST=${RKCHIP}
395	fi
396	if [ "$RKCHIP_LABEL" = "-" ]; then
397		RKCHIP_LABEL=${RKCHIP}
398	fi
399
400	# echo "## $FUNCNAME: $RKCHIP, $RKCHIP_LOADER, $RKCHIP_TRUST, $RKCHIP_LABEL,"
401}
402
403function fixup_platform_configure()
404{
405	CFG_U_KB="-" CFG_U_NUM="-" CFG_T_KB="-" CFG_T_NUM="-"  CFG_SHA="-" CFG_RSA="-"
406
407	for ITEM in "${CHIP_CFG_FIXUP_TABLE[@]}"
408	do
409		CONFIG_XXX=`echo $ITEM | awk '{ print $1 }'`
410		if grep  -q "^${CONFIG_XXX}=y" .config ; then
411			# <*> Fixup rsa/sha pack mode for platforms
412			CFG_RSA=`echo $ITEM | awk '{ print $2 }'`
413			CFG_SHA=`echo $ITEM | awk '{ print $3 }'`
414
415			# <*> Fixup images size pack for platforms, and ini file
416			if grep -q '^CONFIG_ARM64_BOOT_AARCH32=y' .config ; then
417				CFG_U_KB=`echo  $ITEM | awk '{ print $6 }' | awk -F "," '{ print $1 }'`
418				CFG_U_NUM=`echo $ITEM | awk '{ print $7 }' | awk -F "," '{ print $1 }'`
419				CFG_T_KB=`echo  $ITEM | awk '{ print $6 }' | awk -F "," '{ print $2 }'`
420				CFG_T_NUM=`echo $ITEM | awk '{ print $7 }' | awk -F "," '{ print $2 }'`
421
422				PAD_LOADER=`echo $ITEM | awk '{ print $8 }'`
423				PAD_TRUST=`echo  $ITEM | awk '{ print $9 }'`
424				if [ "$PAD_LOADER" != "-" ]; then
425					RKCHIP_LOADER=${RKCHIP_LOADER}${PAD_LOADER}
426				fi
427				if [ "$PAD_TRUST" != "-" ]; then
428					RKCHIP_TRUST=${RKCHIP_TRUST}${PAD_TRUST}
429				fi
430				RKCHIP_LABEL=${RKCHIP_LABEL}"AARCH32"
431			else
432				CFG_U_KB=`echo  $ITEM | awk '{ print $4 }' | awk -F "," '{ print $1 }'`
433				CFG_U_NUM=`echo $ITEM | awk '{ print $5 }' | awk -F "," '{ print $1 }'`
434				CFG_T_KB=`echo  $ITEM | awk '{ print $4 }' | awk -F "," '{ print $2 }'`
435				CFG_T_NUM=`echo $ITEM | awk '{ print $5 }' | awk -F "," '{ print $2 }'`
436			fi
437		fi
438	done
439
440	if [ "$CFG_SHA" != "-" ]; then
441		PLATFORM_SHA="--sha $CFG_SHA"
442	fi
443	if [ "$CFG_RSA" != "-" ]; then
444		PLATFORM_RSA="--rsa $CFG_RSA"
445	fi
446	if [ "$CFG_U_KB" != "-" ]; then
447		PLATFORM_UBOOT_IMG_SIZE="--size $CFG_U_KB $CFG_U_NUM"
448	fi
449	if [ "$CFG_T_KB" != "-" ]; then
450		PLATFORM_TRUST_IMG_SIZE="--size $CFG_T_KB $CFG_T_NUM"
451	fi
452
453	# echo "## $FUNCNAME: $PLATFORM_RSA, $PLATFORM_SHA, $PLATFORM_TRUST_IMG_SIZE, $PLATFORM_UBOOT_IMG_SIZE"
454	# echo "## $FUNCNAME: $RKCHIP_LOADER, $RKCHIP_TRUST, $RKCHIP_LABEL"
455}
456
457pack_uboot_image()
458{
459	local UBOOT_LOAD_ADDR UBOOT_MAX_KB UBOOT_KB HEAD_KB=2
460
461	# Check file size
462	UBOOT_KB=`ls -l u-boot.bin | awk '{print $5}'`
463	if [ "$PLATFORM_UBOOT_IMG_SIZE" = "" ]; then
464		UBOOT_MAX_KB=1046528
465	else
466		UBOOT_MAX_KB=`echo $PLATFORM_UBOOT_IMG_SIZE | awk '{print strtonum($2)}'`
467		UBOOT_MAX_KB=$(((UBOOT_MAX_KB-HEAD_KB)*1024))
468	fi
469
470	if [ $UBOOT_KB -gt $UBOOT_MAX_KB ]; then
471		echo
472		echo "ERROR: pack uboot failed! u-boot.bin actual: $UBOOT_KB bytes, max limit: $UBOOT_MAX_KB bytes"
473		exit 1
474	fi
475
476	if [ "$PACK_FORMAT" = "rk" ]; then
477		# Pack image
478		UBOOT_LOAD_ADDR=`sed -n "/CONFIG_SYS_TEXT_BASE=/s/CONFIG_SYS_TEXT_BASE=//p" include/autoconf.mk|tr -d '\r'`
479		if [ ! $UBOOT_LOAD_ADDR ]; then
480			UBOOT_LOAD_ADDR=`sed -n "/CONFIG_SYS_TEXT_BASE=/s/CONFIG_SYS_TEXT_BASE=//p" .config|tr -d '\r'`
481		fi
482
483		${RKTOOLS}/loaderimage --pack --uboot u-boot.bin uboot.img ${UBOOT_LOAD_ADDR} ${PLATFORM_UBOOT_IMG_SIZE}
484		# Delete u-boot.img and u-boot-dtb.img, which makes users not be confused with final uboot.img
485		ls u-boot.img >/dev/null 2>&1 && rm u-boot.img -rf
486		ls u-boot-dtb.img >/dev/null 2>&1 && rm u-boot-dtb.img -rf
487		echo "pack uboot okay! Input: u-boot.bin"
488	fi
489}
490
491pack_uboot_itb_image()
492{
493	local ini TEE_OFFSET
494
495	# ARM64
496	if grep -Eq ''^CONFIG_ARM64=y'|'^CONFIG_ARM64_BOOT_AARCH32=y'' .config ; then
497		ini=${RKBIN}/RKTRUST/${RKCHIP_TRUST}${PLATFORM_AARCH32}TRUST.ini
498		if [ ! -f ${ini} ]; then
499			echo "pack trust failed! Can't find: ${ini}"
500			return
501		fi
502
503		bl31=`sed -n '/_bl31_/s/PATH=//p' ${ini} |tr -d '\r'`
504
505		cp ${RKBIN}/${bl31} bl31.elf
506		make CROSS_COMPILE=${TOOLCHAIN_GCC} u-boot.itb
507		echo "pack u-boot.itb okay! Input: ${ini}"
508	else
509		ini=${RKBIN}/RKTRUST/${RKCHIP_TRUST}TOS.ini
510		if [ ! -f ${ini} ]; then
511			echo "pack trust failed! Can't find: ${ini}"
512			return
513		fi
514
515		TOS=`sed -n "/TOS=/s/TOS=//p" ${ini} |tr -d '\r'`
516		TOS_TA=`sed -n "/TOSTA=/s/TOSTA=//p" ${ini} |tr -d '\r'`
517
518		if [ $TOS_TA ]; then
519			cp ${RKBIN}/${TOS_TA} tee.bin
520		elif [ $TOS ]; then
521			cp ${RKBIN}/${TOS} tee.bin
522		else
523			echo "Can't find any tee bin"
524			exit 1
525		fi
526
527		TEE_OFFSET=`sed -n "/ADDR=/s/ADDR=//p" ${ini} |tr -d '\r'`
528		if [ "$TEE_OFFSET" = "" ]; then
529			TEE_OFFSET=0x8400000
530		fi
531
532		SPL_FIT_SOURCE=`sed -n "/CONFIG_SPL_FIT_SOURCE=/s/CONFIG_SPL_FIT_SOURCE=//p" .config | tr -d '""'`
533		if [ ! -z $SPL_FIT_SOURCE ]; then
534			cp $SPL_FIT_SOURCE u-boot.its
535		else
536			SPL_FIT_GENERATOR=`sed -n "/CONFIG_SPL_FIT_GENERATOR=/s/CONFIG_SPL_FIT_GENERATOR=//p" .config | tr -d '""'`
537			$SPL_FIT_GENERATOR $TEE_OFFSET > u-boot.its
538		fi
539
540		./tools/mkimage -f u-boot.its -E u-boot.itb
541		echo "pack u-boot.itb okay! Input: ${ini}"
542	fi
543}
544
545pack_spl_loader_image()
546{
547	local header label="SPL" mode=$1
548	local ini=${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini
549	local temp_ini=${RKBIN}/.temp/${RKCHIP_LOADER}MINIALL.ini
550
551	if [ "$FILE" != "" ]; then
552		ini=$FILE;
553	fi
554
555	if [ ! -f ${ini} ]; then
556		echo "pack TPL+SPL loader failed! Can't find: ${ini}"
557		return
558	fi
559
560	ls ${RKBIN}/.temp >/dev/null 2>&1 && rm ${RKBIN}/.temp -rf
561	mkdir ${RKBIN}/.temp
562
563	# Copy to .temp folder
564	cp spl/u-boot-spl.bin ${RKBIN}/.temp/
565	cp ${ini} ${RKBIN}/.temp/${RKCHIP_LOADER}MINIALL.ini -f
566
567	cd ${RKBIN}
568	if [ "$mode" = 'spl' ]; then	# pack tpl+spl
569		cp tpl/u-boot-tpl.bin ${RKBIN}/.temp/
570		# Update ini
571		label="TPL+SPL"
572		header=`sed -n '/NAME=/s/NAME=//p' ${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini`
573		dd if=${RKBIN}/.temp/u-boot-tpl.bin of=${RKBIN}/.temp/tpl.bin bs=1 skip=4
574		sed -i "1s/^/${header:0:4}/" ${RKBIN}/.temp/tpl.bin
575		sed -i "s/FlashData=.*$/FlashData=.\/.temp\/tpl.bin/"     ${temp_ini}
576	fi
577
578	sed -i "s/FlashBoot=.*$/FlashBoot=.\/.temp\/u-boot-spl.bin/"  ${temp_ini}
579
580	${RKTOOLS}/boot_merger ${BIN_PATH_FIXUP} ${temp_ini}
581	rm ${RKBIN}/.temp -rf
582	cd -
583	ls *_loader_*.bin >/dev/null 2>&1 && rm *_loader_*.bin
584	mv ${RKBIN}/*_loader_*.bin ./
585	filename=`basename *_loader_*.bin`
586	if [[ $filename != *spl* ]]; then
587		rename 's/loader_/spl_loader_/' *_loader_*.bin
588	fi
589	echo "pack loader(${label}) okay! Input: ${ini}"
590	ls ./*_loader_*.bin
591}
592
593pack_loader_image()
594{
595	local ini=${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini
596
597	if [ "$FILE" != "" ]; then
598		ini=$FILE;
599	fi
600
601	if [ ! -f $ini ]; then
602		echo "pack loader failed! Can't find: $ini"
603		return
604	fi
605
606	ls *_loader_*.bin >/dev/null 2>&1 && rm *_loader_*.bin
607
608	numline=`cat $ini | wc -l`
609	if [ $numline -eq 1 ]; then
610		image=`sed -n "/PATH=/p" $ini | tr -d '\r' | cut -d '=' -f 2`
611		cp ${RKBIN}/${image} ./
612		echo "pack loader okay! Input: ${ini}"
613		return;
614	fi
615
616	if [ "$PACK_FORMAT" = "rk" ]; then
617		cd ${RKBIN}
618		${RKTOOLS}/boot_merger ${BIN_PATH_FIXUP} $ini
619		cd - && mv ${RKBIN}/*_loader_*.bin ./
620		echo "pack loader okay! Input: $ini"
621	else
622		./scripts/fit-vboot-uboot.sh --no-vboot --no-rebuild >/dev/null
623		file=`ls *loader*.bin`
624		echo "pack $file okay! Input: $ini"
625	fi
626}
627
628pack_32bit_trust_image()
629{
630	local ini=$1 TOS TOS_TA DARM_BASE TEE_LOAD_ADDR TEE_OUTPUT TEE_OFFSET FORMAT
631
632	if [ ! -f ${ini} ]; then
633		echo "pack trust failed! Can't find: ${ini}"
634		return
635	fi
636
637	# Parse orignal path
638	TOS=`sed -n "/TOS=/s/TOS=//p" ${ini} |tr -d '\r'`
639	TOS_TA=`sed -n "/TOSTA=/s/TOSTA=//p" ${ini} |tr -d '\r'`
640
641	# Parse address and output name
642	TEE_OUTPUT=`sed -n "/OUTPUT=/s/OUTPUT=//p" ${ini} |tr -d '\r'`
643	if [ "$TEE_OUTPUT" = "" ]; then
644		TEE_OUTPUT="./trust.img"
645	fi
646	TEE_OFFSET=`sed -n "/ADDR=/s/ADDR=//p" ${ini} |tr -d '\r'`
647	if [ "$TEE_OFFSET" = "" ]; then
648		TEE_OFFSET=0x8400000
649	fi
650
651	# OP-TEE is 132M(0x8400000) offset from DRAM base.
652	DARM_BASE=`sed -n "/CONFIG_SYS_SDRAM_BASE=/s/CONFIG_SYS_SDRAM_BASE=//p" include/autoconf.mk|tr -d '\r'`
653	TEE_LOAD_ADDR=$((DARM_BASE+TEE_OFFSET))
654
655	# Convert Dec to Hex
656	TEE_LOAD_ADDR=$(echo "obase=16;${TEE_LOAD_ADDR}"|bc)
657
658	# Replace "./tools/rk_tools/" with "./" to compatible legacy ini content of rkdevelop branch
659	TOS=$(echo ${TOS} | sed "s/tools\/rk_tools\//\.\//g")
660	TOS_TA=$(echo ${TOS_TA} | sed "s/tools\/rk_tools\//\.\//g")
661
662	if [ "$PACK_FORMAT" = "rk" ]; then
663		if [ $TOS_TA ]; then
664			${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS_TA} ${TEE_OUTPUT} ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE}
665		elif [ $TOS ]; then
666			${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS}    ${TEE_OUTPUT} ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE}
667		else
668			echo "Can't find any tee bin"
669			exit 1
670		fi
671
672		echo "pack trust okay! Input: ${ini}"
673	else
674		./scripts/fit-vboot-uboot.sh --no-vboot --no-rebuild >/dev/null
675		ls uboot.img >/dev/null 2>&1 && rm uboot.img -rf
676		ls trust.img >/dev/null 2>&1 && rm trust.img -rf
677		echo "pack uboot.fit (with uboot trust) okay! Input: ${ini}"
678	fi
679
680	echo
681}
682
683pack_64bit_trust_image()
684{
685	local ini=$1
686
687	if [ ! -f ${ini} ]; then
688		echo "pack trust failed! Can't find: ${ini}"
689		return
690	fi
691
692	cd ${RKBIN}
693	${RKTOOLS}/trust_merger ${PLATFORM_SHA} ${PLATFORM_RSA} ${PLATFORM_TRUST_IMG_SIZE} ${BIN_PATH_FIXUP} \
694				${PACK_IGNORE_BL32} ${ini}
695
696	cd - && mv ${RKBIN}/trust*.img ./
697	echo "pack trust okay! Input: ${ini}"
698	echo
699}
700
701pack_trust_image()
702{
703	local ini
704
705	ls trust*.img >/dev/null 2>&1 && rm trust*.img
706
707	# ARM64 uses trust_merger
708	if grep -Eq ''^CONFIG_ARM64=y'|'^CONFIG_ARM64_BOOT_AARCH32=y'' .config ; then
709		ini=${RKBIN}/RKTRUST/${RKCHIP_TRUST}TRUST.ini
710		if [ "$FILE" != "" ]; then
711			ini=$FILE;
712		fi
713
714		numline=`cat $ini | wc -l`
715		if [ $numline -eq 1 ]; then
716			image=`sed -n "/PATH=/p" $ini | tr -d '\r' | cut -d '=' -f 2`
717			cp ${RKBIN}/${image} ./trust.img
718			echo "pack trust okay! Input: ${ini}"
719			return;
720		fi
721		pack_64bit_trust_image ${ini}
722	# ARM uses loaderimage
723	else
724		ini=${RKBIN}/RKTRUST/${RKCHIP_TRUST}TOS.ini
725		if [ "$FILE" != "" ]; then
726			ini=$FILE;
727		fi
728		pack_32bit_trust_image ${ini}
729	fi
730}
731
732finish()
733{
734	echo
735	if [ ! -z "$OPTION" ]; then
736		echo "Platform ${RKCHIP_LABEL} is build OK, with exist .config ($OPTION)"
737	elif [ "$BOARD" = '' ]; then
738		echo "Platform ${RKCHIP_LABEL} is build OK, with exist .config"
739	else
740		echo "Platform ${RKCHIP_LABEL} is build OK, with new .config(make ${BOARD}_defconfig)"
741	fi
742}
743
744prepare
745select_toolchain
746select_chip_info
747fixup_platform_configure
748sub_commands
749make CROSS_COMPILE=${TOOLCHAIN_GCC} ${OPTION} all --jobs=${JOB}
750pack_uboot_image
751pack_loader_image
752pack_trust_image
753finish
754