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