xref: /rk3399_rockchip-uboot/make.sh (revision 75eb6fceb584d246c2b7cfac79b4fe43d0ec0ecd)
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	else
379		echo "Can't get Rockchip SoC definition in .config"
380		exit 1
381	fi
382
383	# Default use RKCHIP
384	RKCHIP_LABEL=${RKCHIP}
385	RKCHIP_LOADER=${RKCHIP}
386	RKCHIP_TRUST=${RKCHIP}
387
388	# Read from RKCHIP_INI_DESC
389	for item in "${RKCHIP_INI_DESC[@]}"
390	do
391		target_board=`echo $item | awk '{ print $1 }'`
392		if grep  -q "^${target_board}=y" ${OUTDIR}/.config ; then
393			value=`echo $item | awk '{ print $2 }'`
394			if [ "$value" != "NA" ]; then
395				RKCHIP_LABEL=${value};
396			fi
397			value=`echo $item | awk '{ print $3 }'`
398			if [ "$value" != "NA" ]; then
399				RKCHIP_LOADER=${value};
400			fi
401			value=`echo $item | awk '{ print $4 }'`
402			if [ "$value" != "NA" ]; then
403				RKCHIP_TRUST=${value};
404			fi
405		fi
406	done
407}
408
409# Fixup platform special configure
410#	1. fixup pack mode;
411#	2. fixup image size
412#	3. fixup ARM64 cpu boot with AArch32
413fixup_platform_configure()
414{
415	local count plat
416
417# <*> Fixup rsa/sha pack mode for platforms
418	# RK3308/PX30/RK3326/RK1808 use RSA-PKCS1 V2.1, it's pack magic is "3"
419	if [ $RKCHIP = "PX30" -o $RKCHIP = "RK3326" -o $RKCHIP = "RK3308" -o $RKCHIP = "RK1808" ]; then
420		PLATFORM_RSA="--rsa 3"
421	# RK3368 use rk big endian SHA256, it's pack magic is "2"
422	elif [ $RKCHIP = "RK3368" ]; then
423		PLATFORM_SHA="--sha 2"
424	# other platforms use default configure
425	fi
426
427# <*> Fixup images size pack for platforms
428	if [ $RKCHIP = "RK3308" ]; then
429		if grep -q '^CONFIG_ARM64_BOOT_AARCH32=y' ${OUTDIR}/.config ; then
430			PLATFORM_UBOOT_IMG_SIZE="--size 512 2"
431			PLATFORM_TRUST_IMG_SIZE="--size 512 2"
432		else
433			PLATFORM_UBOOT_IMG_SIZE="--size 1024 2"
434			PLATFORM_TRUST_IMG_SIZE="--size 1024 2"
435		fi
436	elif [ $RKCHIP = "RK1808" ]; then
437		PLATFORM_UBOOT_IMG_SIZE="--size 1024 2"
438		PLATFORM_TRUST_IMG_SIZE="--size 1024 2"
439	fi
440
441# <*> Fixup AARCH32 for ARM64 cpu platforms
442	if grep -q '^CONFIG_ARM64_BOOT_AARCH32=y' ${OUTDIR}/.config ; then
443		if [ $RKCHIP = "RK3308" ]; then
444			RKCHIP_LABEL=${RKCHIP_LABEL}"AARCH32"
445			RKCHIP_TRUST=${RKCHIP_TRUST}"AARCH32"
446		elif [ $RKCHIP = "RK3326" ]; then
447			RKCHIP_LABEL=${RKCHIP_LABEL}"AARCH32"
448			RKCHIP_LOADER=${RKCHIP_LOADER}"AARCH32"
449		fi
450	fi
451}
452
453debug_command()
454{
455		if [ "${cmd}" = 'debug' -a "${opt}" = 'debug' ]; then
456			echo
457			echo "The commands will modify .config and files, and can't auto restore changes!"
458			echo "debug-N, the N:"
459			echo "    1. lib/initcall.c debug() -> printf()"
460			echo "    2. common/board_r.c and common/board_f.c debug() -> printf()"
461			echo "    3. global #define DEBUG"
462			echo "    4. enable CONFIG_ROCKCHIP_DEBUGGER"
463			echo "    5. enable CONFIG_ROCKCHIP_CRC"
464			echo "    6. enable CONFIG_BOOTSTAGE_PRINTF_TIMESTAMP"
465			echo "    7. enable CONFIG_ROCKCHIP_CRASH_DUMP"
466			echo "    8. set CONFIG_BOOTDELAY=5"
467			echo "    9. armv7 start.S: print entry warning"
468			echo "   10. armv8 start.S: print entry warning"
469			echo "   11. firmware bootflow debug() -> printf()"
470			echo "   12. bootstage timing report"
471			echo
472			echo "Enabled: "
473			grep '^CONFIG_ROCKCHIP_DEBUGGER=y' ${OUTDIR}/.config > /dev/null \
474			&& echo "    CONFIG_ROCKCHIP_DEBUGGER"
475			grep '^CONFIG_ROCKCHIP_CRC=y' ${OUTDIR}/.config > /dev/null \
476			&& echo "    CONFIG_ROCKCHIP_CRC"
477			grep '^CONFIG_BOOTSTAGE_PRINTF_TIMESTAMP=y' ${OUTDIR}/.config > /dev/null \
478			&& echo "    CONFIG_BOOTSTAGE_PRINTF_TIMESTAMP"
479			grep '^CONFIG_ROCKCHIP_CRASH_DUMP=y' ${OUTDIR}/.config > /dev/null \
480			&& echo "    CONFIG_ROCKCHIP_CRASH_DUMP"
481
482		elif [ "${opt}" = '1' ]; then
483			sed -i 's/\<debug\>/printf/g' lib/initcall.c
484			sed -i 's/ifdef DEBUG/if 1/g' lib/initcall.c
485			echo "DEBUG [1]: lib/initcall.c debug() -> printf()"
486		elif [ "${opt}" = '2' ]; then
487			sed -i 's/\<debug\>/printf/g' ./common/board_f.c
488			sed -i 's/\<debug\>/printf/g' ./common/board_r.c
489			echo "DEBUG [2]: common/board_r.c and common/board_f.c debug() -> printf()"
490		elif [ "${opt}" = '3' ]; then
491			sed -i '$i \#define DEBUG\' include/configs/rockchip-common.h
492			echo "DEBUG [3]: global #define DEBUG"
493		elif [ "${opt}" = '4' ]; then
494			sed -i 's/\# CONFIG_ROCKCHIP_DEBUGGER is not set/CONFIG_ROCKCHIP_DEBUGGER=y/g' ${OUTDIR}/.config
495			echo "DEBUG [4]: CONFIG_ROCKCHIP_DEBUGGER is enabled"
496		elif [ "${opt}" = '5' ]; then
497			sed -i 's/\# CONFIG_ROCKCHIP_CRC is not set/CONFIG_ROCKCHIP_CRC=y/g' ${OUTDIR}/.config
498			echo "DEBUG [5]: CONFIG_ROCKCHIP_CRC is enabled"
499		elif [ "${opt}" = '6' ]; then
500			sed -i 's/\# CONFIG_BOOTSTAGE_PRINTF_TIMESTAMP is not set/CONFIG_BOOTSTAGE_PRINTF_TIMESTAMP=y/g' ${OUTDIR}/.config
501			echo "DEBUG [6]: CONFIG_BOOTSTAGE_PRINTF_TIMESTAMP is enabled"
502		elif [ "${opt}" = '7' ]; then
503			sed -i 's/\# CONFIG_ROCKCHIP_CRASH_DUMP is not set/CONFIG_ROCKCHIP_CRASH_DUMP=y/g' ${OUTDIR}/.config
504			echo "DEBUG [7]: CONFIG_ROCKCHIP_CRASH_DUMP is enabled"
505		elif [ "${opt}" = '8' ]; then
506			sed -i 's/^CONFIG_BOOTDELAY=0/CONFIG_BOOTDELAY=5/g' ${OUTDIR}/.config
507			echo "DEBUG [8]: CONFIG_BOOTDELAY is 5s"
508		elif [ "${opt}" = '9' ]; then
509			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' \
510			./arch/arm/cpu/armv7/start.S
511			echo "DEBUG [9]: armv7 start.S entry warning 'UUUU...'"
512		elif [ "${opt}" = '10' ]; then
513			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' \
514			./arch/arm/cpu/armv8/start.S
515			echo "DEBUG [10]: armv8 start.S entry warning 'UUUU...'"
516		elif [ "${opt}" = '11' ]; then
517			sed -i 's/\<debug\>/printf/g' common/fdt_support.c
518			sed -i 's/\<debug\>/printf/g' common/image-fdt.c
519			sed -i 's/\<debug\>/printf/g' common/image.c
520			sed -i 's/\<debug\>/printf/g' arch/arm/lib/bootm.c
521			sed -i 's/\<debug\>/printf/g' common/bootm.c
522			sed -i 's/\<debug\>/printf/g' common/image.c
523			sed -i 's/\<debug\>/printf/g' common/image-android.c
524			sed -i 's/\<debug\>/printf/g' common/android_bootloader.c
525			echo "DEBUG [11]: firmware bootflow debug() -> printf()"
526		elif [ "${opt}" = '12' ]; then
527			sed -i '$a\CONFIG_BOOTSTAGE=y\' ${OUTDIR}/.config
528			sed -i '$a\CONFIG_BOOTSTAGE_REPORT=y\' ${OUTDIR}/.config
529			sed -i '$a\CONFIG_CMD_BOOTSTAGE=y\' ${OUTDIR}/.config
530			echo "DEBUG [12]: bootstage timing report"
531		fi
532		echo
533}
534
535pack_uboot_image()
536{
537	local UBOOT_LOAD_ADDR UBOOT_MAX_KB UBOOT_KB HEAD_KB=2
538
539	# Check file size
540	UBOOT_KB=`ls -l u-boot.bin | awk '{print $5}'`
541	if [ "$PLATFORM_UBOOT_IMG_SIZE" = "" ]; then
542		UBOOT_MAX_KB=1046528
543	else
544		UBOOT_MAX_KB=`echo $PLATFORM_UBOOT_IMG_SIZE | awk '{print strtonum($2)}'`
545		UBOOT_MAX_KB=$(((UBOOT_MAX_KB-HEAD_KB)*1024))
546	fi
547
548	if [ $UBOOT_KB -gt $UBOOT_MAX_KB ]; then
549		echo
550		echo "ERROR: pack uboot failed! u-boot.bin actual: $UBOOT_KB bytes, max limit: $UBOOT_MAX_KB bytes"
551		exit 1
552	fi
553
554	# Pack image
555	UBOOT_LOAD_ADDR=`sed -n "/CONFIG_SYS_TEXT_BASE=/s/CONFIG_SYS_TEXT_BASE=//p" ${OUTDIR}/include/autoconf.mk|tr -d '\r'`
556	if [ ! $UBOOT_LOAD_ADDR ]; then
557		UBOOT_LOAD_ADDR=`sed -n "/CONFIG_SYS_TEXT_BASE=/s/CONFIG_SYS_TEXT_BASE=//p" ${OUTDIR}/.config|tr -d '\r'`
558	fi
559
560	${RKTOOLS}/loaderimage --pack --uboot ${OUTDIR}/u-boot.bin uboot.img ${UBOOT_LOAD_ADDR} ${PLATFORM_UBOOT_IMG_SIZE}
561
562	# Delete u-boot.img and u-boot-dtb.img, which makes users not be confused with final uboot.img
563	if [ -f ${OUTDIR}/u-boot.img ]; then
564		rm ${OUTDIR}/u-boot.img
565	fi
566
567	if [ -f ${OUTDIR}/u-boot-dtb.img ]; then
568		rm ${OUTDIR}/u-boot-dtb.img
569	fi
570
571	echo "pack uboot okay! Input: ${OUTDIR}/u-boot.bin"
572}
573
574pack_uboot_itb_image()
575{
576	local ini
577
578	# ARM64
579	if grep -Eq ''^CONFIG_ARM64=y'|'^CONFIG_ARM64_BOOT_AARCH32=y'' ${OUTDIR}/.config ; then
580		ini=${RKBIN}/RKTRUST/${RKCHIP_TRUST}${PLATFORM_AARCH32}TRUST.ini
581		if [ ! -f ${ini} ]; then
582			echo "pack trust failed! Can't find: ${ini}"
583			return
584		fi
585
586		bl31=`sed -n '/_bl31_/s/PATH=//p' ${ini} |tr -d '\r'`
587
588		cp ${RKBIN}/${bl31} bl31.elf
589		make CROSS_COMPILE=${TOOLCHAIN_GCC} u-boot.itb
590		echo "pack u-boot.itb okay! Input: ${ini}"
591	else
592		ini=${RKBIN}/RKTRUST/${RKCHIP_TRUST}TOS.ini
593		if [ ! -f ${ini} ]; then
594			echo "pack trust failed! Can't find: ${ini}"
595			return
596		fi
597
598		TOS=`sed -n "/TOS=/s/TOS=//p" ${ini} |tr -d '\r'`
599		TOS_TA=`sed -n "/TOSTA=/s/TOSTA=//p" ${ini} |tr -d '\r'`
600
601		if [ $TOS_TA ]; then
602			cp ${RKBIN}/${TOS_TA} tee.bin
603		elif [ $TOS ]; then
604			cp ${RKBIN}/${TOS} tee.bin
605		else
606			echo "Can't find any tee bin"
607			exit 1
608		fi
609
610		make CROSS_COMPILE=${TOOLCHAIN_GCC} u-boot.itb
611		echo "pack u-boot.itb okay! Input: ${ini}"
612	fi
613}
614
615pack_spl_loader_image()
616{
617	local header label="SPL" mode=$1
618	local ini=${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini
619	local temp_ini=${RKBIN}/.temp/${RKCHIP_LOADER}MINIALL.ini
620
621	if [ "$FILE" != "" ]; then
622		ini=$FILE;
623	fi
624
625	if [ ! -f ${ini} ]; then
626		echo "pack TPL+SPL loader failed! Can't find: ${ini}"
627		return
628	fi
629
630	# Copy to .temp folder
631	if [ -d ${RKBIN}/.temp ]; then
632		rm ${RKBIN}/.temp -rf
633	else
634		mkdir ${RKBIN}/.temp
635	fi
636	cp ${OUTDIR}/spl/u-boot-spl.bin ${RKBIN}/.temp/
637	cp ${OUTDIR}/tpl/u-boot-tpl.bin ${RKBIN}/.temp/
638	cp ${ini} ${RKBIN}/.temp/${RKCHIP_LOADER}MINIALL.ini -f
639
640	cd ${RKBIN}
641	if [ "$mode" = 'spl' ]; then	# pack tpl+spl
642		# Update ini
643		label="TPL+SPL"
644		header=`sed -n '/NAME=/s/NAME=//p' ${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini`
645		dd if=${RKBIN}/.temp/u-boot-tpl.bin of=${RKBIN}/.temp/tpl.bin bs=1 skip=4
646		sed -i "1s/^/${header:0:4}/" ${RKBIN}/.temp/tpl.bin
647		sed -i "s/FlashData=.*$/FlashData=.\/.temp\/tpl.bin/"     ${temp_ini}
648	fi
649
650	sed -i "s/FlashBoot=.*$/FlashBoot=.\/.temp\/u-boot-spl.bin/"  ${temp_ini}
651
652	${RKTOOLS}/boot_merger ${BIN_PATH_FIXUP} ${temp_ini}
653	rm ${RKBIN}/.temp -rf
654	cd -
655	ls *_loader_*.bin >/dev/null 2>&1 && rm *_loader_*.bin
656	mv ${RKBIN}/*_loader_*.bin ./
657	echo "pack loader(${label}) okay! Input: ${ini}"
658	ls ./*_loader_*.bin
659}
660
661pack_loader_image()
662{
663	local mode=$1 files ini=${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini
664
665	if [ "$FILE" != "" ]; then
666		ini=$FILE;
667	fi
668
669	if [ ! -f $ini ]; then
670		echo "pack loader failed! Can't find: $ini"
671		return
672	fi
673
674	ls *_loader_*.bin >/dev/null 2>&1 && rm *_loader_*.bin
675	cd ${RKBIN}
676
677	if [ "${mode}" = 'all' ]; then
678		files=`ls ${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL*.ini`
679		for ini in $files
680		do
681			if [ -f "$ini" ]; then
682				${RKTOOLS}/boot_merger ${BIN_PATH_FIXUP} $ini
683				echo "pack loader okay! Input: $ini"
684			fi
685		done
686	else
687		${RKTOOLS}/boot_merger ${BIN_PATH_FIXUP} $ini
688		echo "pack loader okay! Input: $ini"
689	fi
690
691	cd - && mv ${RKBIN}/*_loader_*.bin ./
692}
693
694__pack_32bit_trust_image()
695{
696	local ini=$1 TOS TOS_TA DARM_BASE TEE_LOAD_ADDR TEE_OUTPUT TEE_OFFSET
697
698	if [ ! -f ${ini} ]; then
699		echo "pack trust failed! Can't find: ${ini}"
700		return
701	fi
702
703	# Parse orignal path
704	TOS=`sed -n "/TOS=/s/TOS=//p" ${ini} |tr -d '\r'`
705	TOS_TA=`sed -n "/TOSTA=/s/TOSTA=//p" ${ini} |tr -d '\r'`
706
707	# Parse address and output name
708	TEE_OUTPUT=`sed -n "/OUTPUT=/s/OUTPUT=//p" ${ini} |tr -d '\r'`
709	if [ "$TEE_OUTPUT" = "" ]; then
710		TEE_OUTPUT="./trust.img"
711	fi
712	TEE_OFFSET=`sed -n "/ADDR=/s/ADDR=//p" ${ini} |tr -d '\r'`
713	if [ "$TEE_OFFSET" = "" ]; then
714		TEE_OFFSET=0x8400000
715	fi
716
717	# OP-TEE is 132M(0x8400000) offset from DRAM base.
718	DARM_BASE=`sed -n "/CONFIG_SYS_SDRAM_BASE=/s/CONFIG_SYS_SDRAM_BASE=//p" ${OUTDIR}/include/autoconf.mk|tr -d '\r'`
719	TEE_LOAD_ADDR=$((DARM_BASE+TEE_OFFSET))
720
721	# Convert Dec to Hex
722	TEE_LOAD_ADDR=$(echo "obase=16;${TEE_LOAD_ADDR}"|bc)
723
724	# Replace "./tools/rk_tools/" with "./" to compatible legacy ini content of rkdevelop branch
725	TOS=$(echo ${TOS} | sed "s/tools\/rk_tools\//\.\//g")
726	TOS_TA=$(echo ${TOS_TA} | sed "s/tools\/rk_tools\//\.\//g")
727
728	if [ $TOS_TA ]; then
729		${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS_TA} ${TEE_OUTPUT} ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE}
730	elif [ $TOS ]; then
731		${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS}    ${TEE_OUTPUT} ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE}
732	else
733		echo "Can't find any tee bin"
734		exit 1
735	fi
736
737	echo "pack trust okay! Input: ${ini}"
738	echo
739}
740
741__pack_64bit_trust_image()
742{
743	local ini=$1
744
745	if [ ! -f ${ini} ]; then
746		echo "pack trust failed! Can't find: ${ini}"
747		return
748	fi
749
750	cd ${RKBIN}
751	${RKTOOLS}/trust_merger ${PLATFORM_SHA} ${PLATFORM_RSA} ${PLATFORM_TRUST_IMG_SIZE} ${BIN_PATH_FIXUP} \
752				${PACK_IGNORE_BL32} ${ini}
753
754	cd - && mv ${RKBIN}/trust*.img ./
755	echo "pack trust okay! Input: ${ini}"
756	echo
757}
758
759pack_trust_image()
760{
761	local mode=$1 files ini
762
763	ls trust*.img >/dev/null && rm trust*.img
764	# ARM64 uses trust_merger
765	if grep -Eq ''^CONFIG_ARM64=y'|'^CONFIG_ARM64_BOOT_AARCH32=y'' ${OUTDIR}/.config ; then
766		ini=${RKBIN}/RKTRUST/${RKCHIP_TRUST}TRUST.ini
767		if [ "$FILE" != "" ]; then
768			ini=$FILE;
769		fi
770
771		if [ "${mode}" = 'all' ]; then
772			files=`ls ${RKBIN}/RKTRUST/${RKCHIP_TRUST}TRUST*.ini`
773			for ini in $files
774			do
775				__pack_64bit_trust_image ${ini}
776			done
777		else
778			__pack_64bit_trust_image ${ini}
779		fi
780	# ARM uses loaderimage
781	else
782		ini=${RKBIN}/RKTRUST/${RKCHIP_TRUST}TOS.ini
783		if [ "$FILE" != "" ]; then
784			ini=$FILE;
785		fi
786		if [ "${mode}" = 'all' ]; then
787			files=`ls ${RKBIN}/RKTRUST/${RKCHIP_TRUST}TOS*.ini`
788			for ini in $files
789			do
790				__pack_32bit_trust_image ${ini}
791			done
792		else
793			__pack_32bit_trust_image ${ini}
794		fi
795	fi
796}
797
798finish()
799{
800	echo
801	if [ "$BOARD" = '' ]; then
802		echo "Platform ${RKCHIP_LABEL} is build OK, with exist .config"
803	else
804		echo "Platform ${RKCHIP_LABEL} is build OK, with new .config(make ${BOARD}_defconfig)"
805	fi
806}
807
808prepare
809select_toolchain
810select_chip_info
811fixup_platform_configure
812sub_commands
813make CROSS_COMPILE=${TOOLCHAIN_GCC}  all --jobs=${JOB} ${OUTOPT}
814pack_uboot_image
815pack_loader_image
816pack_trust_image
817finish
818