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