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