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