xref: /rk3399_rockchip-uboot/make.sh (revision e1aab64013804e120d3fc5d6940ed7245d8f9c2a)
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		# Delete u-boot.img and u-boot-dtb.img, which makes users not be confused with final uboot.img
476		ls u-boot.img >/dev/null 2>&1 && rm u-boot.img -rf
477		ls u-boot-dtb.img >/dev/null 2>&1 && rm u-boot-dtb.img -rf
478		echo "pack uboot okay! Input: u-boot.bin"
479	fi
480}
481
482pack_uboot_itb_image()
483{
484	local ini TEE_OFFSET
485
486	# ARM64
487	if grep -Eq ''^CONFIG_ARM64=y'|'^CONFIG_ARM64_BOOT_AARCH32=y'' .config ; then
488		ini=${RKBIN}/RKTRUST/${RKCHIP_TRUST}${PLATFORM_AARCH32}TRUST.ini
489		if [ ! -f ${ini} ]; then
490			echo "pack trust failed! Can't find: ${ini}"
491			return
492		fi
493
494		bl31=`sed -n '/_bl31_/s/PATH=//p' ${ini} |tr -d '\r'`
495
496		cp ${RKBIN}/${bl31} bl31.elf
497		make CROSS_COMPILE=${TOOLCHAIN_GCC} u-boot.itb
498		echo "pack u-boot.itb okay! Input: ${ini}"
499	else
500		ini=${RKBIN}/RKTRUST/${RKCHIP_TRUST}TOS.ini
501		if [ ! -f ${ini} ]; then
502			echo "pack trust failed! Can't find: ${ini}"
503			return
504		fi
505
506		TOS=`sed -n "/TOS=/s/TOS=//p" ${ini} |tr -d '\r'`
507		TOS_TA=`sed -n "/TOSTA=/s/TOSTA=//p" ${ini} |tr -d '\r'`
508
509		if [ $TOS_TA ]; then
510			cp ${RKBIN}/${TOS_TA} tee.bin
511		elif [ $TOS ]; then
512			cp ${RKBIN}/${TOS} tee.bin
513		else
514			echo "Can't find any tee bin"
515			exit 1
516		fi
517
518		TEE_OFFSET=`sed -n "/ADDR=/s/ADDR=//p" ${ini} |tr -d '\r'`
519		if [ "$TEE_OFFSET" = "" ]; then
520			TEE_OFFSET=0x8400000
521		fi
522
523		SPL_FIT_SOURCE=`sed -n "/CONFIG_SPL_FIT_SOURCE=/s/CONFIG_SPL_FIT_SOURCE=//p" .config | tr -d '""'`
524		if [ ! -z $SPL_FIT_SOURCE ]; then
525			cp $SPL_FIT_SOURCE u-boot.its
526		else
527			SPL_FIT_GENERATOR=`sed -n "/CONFIG_SPL_FIT_GENERATOR=/s/CONFIG_SPL_FIT_GENERATOR=//p" .config | tr -d '""'`
528			$SPL_FIT_GENERATOR $TEE_OFFSET > u-boot.its
529		fi
530
531		./tools/mkimage -f u-boot.its -E u-boot.itb
532		echo "pack u-boot.itb okay! Input: ${ini}"
533	fi
534}
535
536pack_spl_loader_image()
537{
538	local header label="SPL" mode=$1
539	local ini=${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini
540	local temp_ini=${RKBIN}/.temp/${RKCHIP_LOADER}MINIALL.ini
541
542	if [ "$FILE" != "" ]; then
543		ini=$FILE;
544	fi
545
546	if [ ! -f ${ini} ]; then
547		echo "pack TPL+SPL loader failed! Can't find: ${ini}"
548		return
549	fi
550
551	ls ${RKBIN}/.temp >/dev/null 2>&1 && rm ${RKBIN}/.temp -rf
552	mkdir ${RKBIN}/.temp
553
554	# Copy to .temp folder
555	cp spl/u-boot-spl.bin ${RKBIN}/.temp/
556	cp ${ini} ${RKBIN}/.temp/${RKCHIP_LOADER}MINIALL.ini -f
557
558	cd ${RKBIN}
559	if [ "$mode" = 'spl' ]; then	# pack tpl+spl
560		cp tpl/u-boot-tpl.bin ${RKBIN}/.temp/
561		# Update ini
562		label="TPL+SPL"
563		header=`sed -n '/NAME=/s/NAME=//p' ${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini`
564		dd if=${RKBIN}/.temp/u-boot-tpl.bin of=${RKBIN}/.temp/tpl.bin bs=1 skip=4
565		sed -i "1s/^/${header:0:4}/" ${RKBIN}/.temp/tpl.bin
566		sed -i "s/FlashData=.*$/FlashData=.\/.temp\/tpl.bin/"     ${temp_ini}
567	fi
568
569	sed -i "s/FlashBoot=.*$/FlashBoot=.\/.temp\/u-boot-spl.bin/"  ${temp_ini}
570
571	${RKTOOLS}/boot_merger ${BIN_PATH_FIXUP} ${temp_ini}
572	rm ${RKBIN}/.temp -rf
573	cd -
574	ls *_loader_*.bin >/dev/null 2>&1 && rm *_loader_*.bin
575	mv ${RKBIN}/*_loader_*.bin ./
576	filename=`basename *_loader_*.bin`
577	if [[ $filename != *spl* ]]; then
578		rename 's/loader_/spl_loader_/' *_loader_*.bin
579	fi
580	echo "pack loader(${label}) okay! Input: ${ini}"
581	ls ./*_loader_*.bin
582}
583
584pack_loader_image()
585{
586	local ini=${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini
587
588	if [ "$FILE" != "" ]; then
589		ini=$FILE;
590	fi
591
592	if [ ! -f $ini ]; then
593		echo "pack loader failed! Can't find: $ini"
594		return
595	fi
596
597	ls *_loader_*.bin >/dev/null 2>&1 && rm *_loader_*.bin
598
599	numline=`cat $ini | wc -l`
600	if [ $numline -eq 1 ]; then
601		image=`sed -n "/PATH=/p" $ini | tr -d '\r' | cut -d '=' -f 2`
602		cp ${RKBIN}/${image} ./
603		echo "pack loader okay! Input: ${ini}"
604		return;
605	fi
606
607	cd ${RKBIN}
608
609	${RKTOOLS}/boot_merger ${BIN_PATH_FIXUP} $ini
610	echo "pack loader okay! Input: $ini"
611
612	cd - && mv ${RKBIN}/*_loader_*.bin ./
613}
614
615pack_32bit_trust_image()
616{
617	local ini=$1 TOS TOS_TA DARM_BASE TEE_LOAD_ADDR TEE_OUTPUT TEE_OFFSET FORMAT
618
619	if [ ! -f ${ini} ]; then
620		echo "pack trust failed! Can't find: ${ini}"
621		return
622	fi
623
624	# Parse orignal path
625	TOS=`sed -n "/TOS=/s/TOS=//p" ${ini} |tr -d '\r'`
626	TOS_TA=`sed -n "/TOSTA=/s/TOSTA=//p" ${ini} |tr -d '\r'`
627
628	# Parse address and output name
629	TEE_OUTPUT=`sed -n "/OUTPUT=/s/OUTPUT=//p" ${ini} |tr -d '\r'`
630	if [ "$TEE_OUTPUT" = "" ]; then
631		TEE_OUTPUT="./trust.img"
632	fi
633	TEE_OFFSET=`sed -n "/ADDR=/s/ADDR=//p" ${ini} |tr -d '\r'`
634	if [ "$TEE_OFFSET" = "" ]; then
635		TEE_OFFSET=0x8400000
636	fi
637
638	# OP-TEE is 132M(0x8400000) offset from DRAM base.
639	DARM_BASE=`sed -n "/CONFIG_SYS_SDRAM_BASE=/s/CONFIG_SYS_SDRAM_BASE=//p" include/autoconf.mk|tr -d '\r'`
640	TEE_LOAD_ADDR=$((DARM_BASE+TEE_OFFSET))
641
642	# Convert Dec to Hex
643	TEE_LOAD_ADDR=$(echo "obase=16;${TEE_LOAD_ADDR}"|bc)
644
645	# Replace "./tools/rk_tools/" with "./" to compatible legacy ini content of rkdevelop branch
646	TOS=$(echo ${TOS} | sed "s/tools\/rk_tools\//\.\//g")
647	TOS_TA=$(echo ${TOS_TA} | sed "s/tools\/rk_tools\//\.\//g")
648
649	FORMAT=`sed -n "/FIT=/s/FIT=//p" ${ini} |tr -d '\r'`
650	if [ "$FORMAT" = "1" ]; then
651		PACK_FORMAT="fit"
652		./scripts/fit-vboot-uboot.sh --no-vboot --no-rebuild >/dev/null
653		ls uboot.img >/dev/null 2>&1 && rm uboot.img -rf
654		ls trust.img >/dev/null 2>&1 && rm trust.img -rf
655		echo "pack uboot.fit (with uboot trust) okay! Input: ${ini}"
656	else
657		if [ $TOS_TA ]; then
658			${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS_TA} ${TEE_OUTPUT} ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE}
659		elif [ $TOS ]; then
660			${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS}    ${TEE_OUTPUT} ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE}
661		else
662			echo "Can't find any tee bin"
663			exit 1
664		fi
665
666		echo "pack trust okay! Input: ${ini}"
667	fi
668
669	echo
670}
671
672pack_64bit_trust_image()
673{
674	local ini=$1
675
676	if [ ! -f ${ini} ]; then
677		echo "pack trust failed! Can't find: ${ini}"
678		return
679	fi
680
681	cd ${RKBIN}
682	${RKTOOLS}/trust_merger ${PLATFORM_SHA} ${PLATFORM_RSA} ${PLATFORM_TRUST_IMG_SIZE} ${BIN_PATH_FIXUP} \
683				${PACK_IGNORE_BL32} ${ini}
684
685	cd - && mv ${RKBIN}/trust*.img ./
686	echo "pack trust okay! Input: ${ini}"
687	echo
688}
689
690pack_trust_image()
691{
692	local ini
693
694	ls trust*.img >/dev/null 2>&1 && rm trust*.img
695
696	# ARM64 uses trust_merger
697	if grep -Eq ''^CONFIG_ARM64=y'|'^CONFIG_ARM64_BOOT_AARCH32=y'' .config ; then
698		ini=${RKBIN}/RKTRUST/${RKCHIP_TRUST}TRUST.ini
699		if [ "$FILE" != "" ]; then
700			ini=$FILE;
701		fi
702
703		numline=`cat $ini | wc -l`
704		if [ $numline -eq 1 ]; then
705			image=`sed -n "/PATH=/p" $ini | tr -d '\r' | cut -d '=' -f 2`
706			cp ${RKBIN}/${image} ./trust.img
707			echo "pack trust okay! Input: ${ini}"
708			return;
709		fi
710		pack_64bit_trust_image ${ini}
711	# ARM uses loaderimage
712	else
713		ini=${RKBIN}/RKTRUST/${RKCHIP_TRUST}TOS.ini
714		if [ "$FILE" != "" ]; then
715			ini=$FILE;
716		fi
717		pack_32bit_trust_image ${ini}
718	fi
719}
720
721finish()
722{
723	echo
724	if [ ! -z "$OPTION" ]; then
725		echo "Platform ${RKCHIP_LABEL} is build OK, with exist .config ($OPTION)"
726	elif [ "$BOARD" = '' ]; then
727		echo "Platform ${RKCHIP_LABEL} is build OK, with exist .config"
728	else
729		echo "Platform ${RKCHIP_LABEL} is build OK, with new .config(make ${BOARD}_defconfig)"
730	fi
731}
732
733prepare
734select_toolchain
735select_chip_info
736fixup_platform_configure
737sub_commands
738make CROSS_COMPILE=${TOOLCHAIN_GCC} ${OPTION} all --jobs=${JOB}
739pack_uboot_image
740pack_loader_image
741pack_trust_image
742finish
743