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