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