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