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