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