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