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