xref: /rk3399_rockchip-uboot/make.sh (revision ad07c38f68e9ad3985f128f2a347460cdd3931a1)
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
9JOB=`sed -n "N;/processor/p" /proc/cpuinfo|wc -l`
10SUPPORT_LIST=`ls configs/*[r,p][x,v,k][0-9][0-9]*_defconfig`
11
12# @LOADER: map to $RKCHIP_LOADER for loader ini
13# @TRUST:  map to $RKCHIP_TRUST for trust ini
14# @LABEL:  map to $RKCHIP_LEBEL for verbose message
15# @-:      default state/value
16CHIP_TYPE_FIXUP_TABLE=(
17	# CONFIG_XXX                         RKCHIP         LOADER       TRUST         LABEL
18	"CONFIG_ROCKCHIP_RK3368              RK3368H         -            -             -"
19	"CONFIG_ROCKCHIP_RV1108              RV110X          -            -             -"
20	"CONFIG_ROCKCHIP_PX3SE               PX3SE           -            -             -"
21	"CONFIG_ROCKCHIP_RK3126              RK3126          -            -             -"
22	"CONFIG_ROCKCHIP_RK3326              RK3326          -            -             -"
23	"CONFIG_ROCKCHIP_RK3128X             RK3128X         -            -             -"
24	"CONFIG_ROCKCHIP_PX5                 PX5             -            -             -"
25	"CONFIG_ROCKCHIP_RK3399PRO           RK3399PRO       -            -             -"
26	"CONFIG_ROCKCHIP_RK1806              RK1806          -            -             -"
27	"CONFIG_TARGET_GVA_RK3229            RK322X          RK322XAT     -             -"
28	"CONFIG_COPROCESSOR_RK1808           RKNPU-LION      RKNPULION    RKNPULION     -"
29)
30
31# <*> Fixup rsa/sha pack mode for platforms
32#     RSA: RK3308/PX30/RK3326/RK1808 use RSA-PKCS1 V2.1, it's pack magic is "3", and others use default configure.
33#     SHA: RK3368 use rk big endian SHA256, it's pack magic is "2", and others use default configure.
34# <*> Fixup images size pack for platforms
35# <*> Fixup verbose message about AARCH32
36#
37# @RSA:     rsa mode
38# @SHA:     sha mode
39# @A64-KB:  arm64 platform image size: [uboot,trust]
40# @A64-NUM: arm64 platform image number of total: [uboot,trust]
41# @A32-KB:  arm32 platform image size: [uboot,trust]
42# @A32-NUM: arm32 platform image number of total: [uboot,trust]
43# @LOADER:  map to $RKCHIP_LOADER for loader ini
44# @TRUST:   map to $RKCHIP_TRUST for trust ini
45# @-:       default state/value
46CHIP_CFG_FIXUP_TABLE=(
47	# CONFIG_XXX              RSA     SHA     A64-KB      A64-NUM     A32-KB       A32-NUM      LOAER        TRUST
48	"CONFIG_ROCKCHIP_RK3368    -       2       -,-          -,-        -,-          -,-           -           -"
49	"CONFIG_ROCKCHIP_RK3036    -       -       512,512      1,1        -,-          -,-           -           -"
50	"CONFIG_ROCKCHIP_PX30      3       -       -,-          -,-        -,-          -,-           -           -"
51	"CONFIG_ROCKCHIP_RK3326    3       -       -,-          -,-        -,-          -,-           AARCH32     -"
52	"CONFIG_ROCKCHIP_RK3308    3       -       1024,1024    2,2        512,512      2,2           -           AARCH32"
53	"CONFIG_ROCKCHIP_RK1808    3       -       1024,1024    2,2        -,-          -,-           -           -"
54	"CONFIG_ROCKCHIP_RV1126    3       -       -,-          -,-        -,-          -,-           -           -"
55)
56
57CHIP_TPL_MAGIC_TABLE=(
58	"CONFIG_ROCKCHIP_PX30      RK33"
59	"CONFIG_ROCKCHIP_RV1126    110B"
60)
61
62########################################### User can modify #############################################
63# User's rkbin tool relative path
64RKBIN_TOOLS=../rkbin/tools
65
66# User's GCC toolchain and relative path
67ADDR2LINE_ARM32=arm-linux-gnueabihf-addr2line
68ADDR2LINE_ARM64=aarch64-linux-gnu-addr2line
69OBJ_ARM32=arm-linux-gnueabihf-objdump
70OBJ_ARM64=aarch64-linux-gnu-objdump
71GCC_ARM32=arm-linux-gnueabihf-
72GCC_ARM64=aarch64-linux-gnu-
73TOOLCHAIN_ARM32=../prebuilts/gcc/linux-x86/arm/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabihf/bin
74TOOLCHAIN_ARM64=../prebuilts/gcc/linux-x86/aarch64/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin
75
76########################################### User not touch #############################################
77RKTOOLS=./tools
78
79# Declare global INI file searching index name for every chip, update in select_chip_info()
80RKCHIP="-"
81RKCHIP_LABEL="-"
82RKCHIP_LOADER="-"
83RKCHIP_TRUST="-"
84
85INI_TRUST=
86INI_LOADER=
87
88# Declare rkbin repository path, updated in prepare()
89RKBIN=
90
91# Declare global toolchain path for CROSS_COMPILE, updated in select_toolchain()
92TOOLCHAIN_GCC=
93TOOLCHAIN_OBJDUMP=
94TOOLCHAIN_ADDR2LINE=
95
96# Declare global plaform configure, updated in fixup_platform_configure()
97PLATFORM_RSA=
98PLATFORM_SHA=
99PLATFORM_UBOOT_SIZE=
100PLATFORM_TRUST_SIZE=
101PLATFORM_TYPE="RKFW"
102
103#########################################################################################################
104function help()
105{
106	echo
107	echo "Usage:"
108	echo "	./make.sh [board|sub-command]"
109	echo
110	echo "	 - board:        board name of defconfig"
111	echo "	 - sub-command:  elf*|loader*|spl*|tpl*|itb|trust*|uboot|map|sym|<addr>"
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
147function prepare()
148{
149	if [ -d ${RKBIN_TOOLS} ]; then
150		absolute_path=$(cd `dirname ${RKBIN_TOOLS}`; pwd)
151		RKBIN=${absolute_path}
152	else
153		echo "ERROR: No ../rkbin repository"
154		exit 1
155	fi
156
157	if grep -Eq ''^CONFIG_ARM64=y'|'^CONFIG_ARM64_BOOT_AARCH32=y'' .config ; then
158		ARM64_TRUSTZONE="y"
159	fi
160
161	if grep  -q '^CONFIG_ROCKCHIP_FIT_IMAGE_PACK=y' .config ; then
162		PLATFORM_TYPE="FIT"
163	fi
164}
165
166function process_args()
167{
168	while [ $# -gt 0 ]; do
169		case $1 in
170			--help|-help|help|--h|-h)
171				help
172				exit 0
173				;;
174
175			''|loader|trust|uboot|spl*|tpl*|debug*|itb|env|fit*)
176				ARG_CMD=$1
177				shift 1
178				;;
179
180			--no-pack)
181				ARG_NO_PACK="y"
182				shift 1
183				;;
184
185			--no-uboot)
186				ARG_NO_UBOOT="y"
187				shift 1
188				;;
189
190			map|sym|elf*)
191				ARG_CMD=$1
192				if [ "$2" == "spl" -o "$2" == "tpl" ]; then
193					ARG_S_TPL=$2
194					shift 1
195				fi
196				shift 1
197				;;
198
199			*.ini)
200				if [ ! -f $1 ]; then
201					echo "ERROR: No $1"
202				fi
203				if grep -q 'CODE471_OPTION' $1 ; then
204					ARG_INI_LOADER=$1
205				elif grep -Eq ''BL31_OPTION'|'TOS'' $1 ; then
206					ARG_INI_TRUST=$1
207				fi
208				shift 1
209				;;
210
211			*)
212				# out scripts args
213				NUM=$(./scripts/fit-mkimg.sh --p-check $1)
214				if  [ ${NUM} -ne 0 ]; then
215					[ ${NUM} -eq 1 ] && ARG_LIST_FIT="${ARG_LIST_FIT} $1"
216					[ ${NUM} -eq 2 ] && ARG_LIST_FIT="${ARG_LIST_FIT} $1 $2"
217					shift ${NUM}
218					continue
219				# FUNC address
220				elif [ -z $(echo $1 | sed 's/[0-9,a-f,A-F,x,X,-]//g') ]; then
221					ARG_FUNCADDR=$1
222				# xxx_defconfig
223				else
224					ARG_BOARD=$1
225					if [ ! -f configs/${ARG_BOARD}_defconfig -a ! -f configs/${ARG_BOARD}.config ]; then
226						echo -e "\n${SUPPORT_LIST}\n"
227						echo "ERROR: No configs/${ARG_BOARD}_defconfig"
228						exit 1
229					elif [ -f configs/${ARG_BOARD}.config ]; then
230						BASE_DEFCONFIG=`sed -n "/CONFIG_BASE_DEFCONFIG=/s/CONFIG_BASE_DEFCONFIG=//p" configs/${ARG_BOARD}.config |tr -d '\r' | tr -d '"'`
231						MAKE_CMD="make ${BASE_DEFCONFIG} ${ARG_BOARD}.config -j${JOB}"
232						echo "## ${MAKE_CMD}"
233						make ${BASE_DEFCONFIG} ${ARG_BOARD}.config ${OPTION}
234					else
235						BASE_DEFCONFIG=`sed -n "/CONFIG_BASE_DEFCONFIG=/s/CONFIG_BASE_DEFCONFIG=//p" configs/${ARG_BOARD}_defconfig |tr -d '\r' | tr -d '"'`
236						if [ "${BASE_DEFCONFIG}" == "${ARG_BOARD}_defconfig" ]; then
237							echo "ERROR: configs/${ARG_BOARD}_defconfig is base defconfig for other [...].config"
238							exit 1
239						fi
240						MAKE_CMD="make ${ARG_BOARD}_defconfig -j${JOB}"
241						echo "## ${MAKE_CMD}"
242						make ${ARG_BOARD}_defconfig ${OPTION}
243					fi
244				fi
245				shift 1
246				;;
247		esac
248	done
249
250	if [ ! -f .config ]; then
251		echo
252		echo "ERROR: No .config"
253		help
254		exit 1
255	fi
256}
257
258function select_toolchain()
259{
260	if grep -q '^CONFIG_ARM64=y' .config ; then
261		if [ -d ${TOOLCHAIN_ARM64} ]; then
262			absolute_path=$(cd `dirname ${TOOLCHAIN_ARM64}`; pwd)
263			TOOLCHAIN_GCC=${absolute_path}/bin/${GCC_ARM64}
264			TOOLCHAIN_OBJDUMP=${absolute_path}/bin/${OBJ_ARM64}
265			TOOLCHAIN_ADDR2LINE=${absolute_path}/bin/${ADDR2LINE_ARM64}
266		else
267			echo "ERROR: No toolchain: ${TOOLCHAIN_ARM64}"
268			exit 1
269		fi
270	else
271		if [ -d ${TOOLCHAIN_ARM32} ]; then
272			absolute_path=$(cd `dirname ${TOOLCHAIN_ARM32}`; pwd)
273			TOOLCHAIN_GCC=${absolute_path}/bin/${GCC_ARM32}
274			TOOLCHAIN_OBJDUMP=${absolute_path}/bin/${OBJ_ARM32}
275			TOOLCHAIN_ADDR2LINE=${absolute_path}/bin/${ADDR2LINE_ARM32}
276		else
277			echo "ERROR: No toolchain: ${TOOLCHAIN_ARM32}"
278			exit 1
279		fi
280	fi
281}
282
283function sub_commands()
284{
285	# skip "--" parameter, such as "--rollback-index-..."
286	if [[ "${ARG_CMD}" != "--*" ]]; then
287		cmd=${ARG_CMD%-*}
288		arg=${ARG_CMD#*-}
289	else
290		cmd=${ARG_CMD}
291	fi
292
293	if [ "${ARG_S_TPL}" == "tpl" -o "${ARG_S_TPL}" == "spl" ]; then
294		elf=`find -name u-boot-${ARG_S_TPL}`
295		map=`find -name u-boot-${ARG_S_TPL}.map`
296		sym=`find -name u-boot-${ARG_S_TPL}.sym`
297	else
298		elf=u-boot
299		map=u-boot.map
300		sym=u-boot.sym
301	fi
302
303	case ${cmd} in
304		elf)
305			if [ ! -f ${elf} ]; then
306				echo "ERROR: No elf: ${elf}"
307				exit 1
308			else
309				if [ "${cmd}" == "elf" -a "${arg}" == "elf" ]; then
310					arg=D # default
311				fi
312				${TOOLCHAIN_OBJDUMP} -${arg} ${elf} | less
313				exit 0
314			fi
315			;;
316
317		debug)
318			./scripts/rkpatch.sh ${arg}
319			exit 0
320			;;
321
322		fit)
323			if [ "${arg}" == "ns" ]; then
324				./scripts/fit-mkimg.sh --uboot-itb --boot-itb --no-vboot ${ARG_LIST_FIT}
325			fi
326			exit 0
327			;;
328
329		map)
330			cat ${map} | less
331			exit 0
332			;;
333
334		sym)
335			cat ${sym} | less
336			exit 0
337			;;
338
339		trust)
340			pack_trust_image
341			exit 0
342			;;
343
344		loader)
345			pack_loader_image
346			exit 0
347			;;
348
349		tpl|spl)
350			pack_spl_loader_image ${ARG_CMD}
351			exit 0
352			;;
353
354		itb)
355			pack_uboot_itb_image
356			exit 0
357			;;
358
359		uboot)
360			pack_uboot_image
361			exit 0
362			;;
363
364		env)
365			make CROSS_COMPILE=${TOOLCHAIN_GCC} envtools
366			exit 0
367			;;
368
369		--rollback-index*)
370			pack_fit_image ${ARG_LIST_FIT}
371			exit 0
372			;;
373		*)
374			# Search function and code position of address
375			FUNCADDR=${ARG_FUNCADDR}
376			RELOC_OFF=${FUNCADDR#*-}
377			FUNCADDR=${FUNCADDR%-*}
378			if [ -z $(echo ${FUNCADDR} | sed 's/[0-9,a-f,A-F,x,X,-]//g') ] && [ ${FUNCADDR} ]; then
379				# With prefix: '0x' or '0X'
380				if [ `echo ${FUNCADDR} | sed -n "/0[x,X]/p" | wc -l` -ne 0 ]; then
381					FUNCADDR=`echo ${FUNCADDR} | awk '{ print strtonum($0) }'`
382					FUNCADDR=`echo "obase=16;${FUNCADDR}"|bc |tr '[A-Z]' '[a-z]'`
383				fi
384				if [ `echo ${RELOC_OFF} | sed -n "/0[x,X]/p" | wc -l` -ne 0 ] && [ ${RELOC_OFF} ]; then
385					RELOC_OFF=`echo ${RELOC_OFF} | awk '{ print strtonum($0) }'`
386					RELOC_OFF=`echo "obase=16;${RELOC_OFF}"|bc |tr '[A-Z]' '[a-z]'`
387				fi
388
389				# If reloc address is assigned, do sub
390				if [ "${FUNCADDR}" != "${RELOC_OFF}" ]; then
391					# Hex -> Dec -> SUB -> Hex
392					FUNCADDR=`echo $((16#${FUNCADDR}))`
393					RELOC_OFF=`echo $((16#${RELOC_OFF}))`
394					FUNCADDR=$((FUNCADDR-RELOC_OFF))
395					FUNCADDR=$(echo "obase=16;${FUNCADDR}"|bc |tr '[A-Z]' '[a-z]')
396				fi
397
398				echo
399				sed -n "/${FUNCADDR}/p" ${sym}
400				${TOOLCHAIN_ADDR2LINE} -e ${elf} ${FUNCADDR}
401				exit 0
402			fi
403			;;
404	esac
405}
406
407#
408# We select chip info to do:
409#	1. RKCHIP:        fixup platform configure
410#	2. RKCHIP_LOADER: search ini file to pack loader
411#	3. RKCHIP_TRUST:  search ini file to pack trust
412#	4. RKCHIP_LABEL:  show build message
413#
414function select_chip_info()
415{
416	# Read RKCHIP firstly from .config
417	# The regular expression that matching:
418	#  - PX30, PX3SE
419	#  - RK????, RK????X
420	#  - RV????
421	chip_pattern='^CONFIG_ROCKCHIP_[R,P][X,V,K][0-9ESX]{1,5}'
422	RKCHIP=`egrep -o ${chip_pattern} .config`
423
424	# default
425	RKCHIP=${RKCHIP##*_}
426	# fixup ?
427	for item in "${CHIP_TYPE_FIXUP_TABLE[@]}"
428	do
429		config_xxx=`echo ${item} | awk '{ print $1 }'`
430		if grep  -q "^${config_xxx}=y" .config ; then
431			RKCHIP=`echo ${item} | awk '{ print $2 }'`
432			RKCHIP_LOADER=`echo ${item} | awk '{ print $3 }'`
433			RKCHIP_TRUST=`echo  ${item} | awk '{ print $4 }'`
434			RKCHIP_LABEL=`echo  ${item} | awk '{ print $5 }'`
435		fi
436	done
437
438	if [ "${RKCHIP_LOADER}" == "-" ]; then
439		RKCHIP_LOADER=${RKCHIP}
440	fi
441	if [ "${RKCHIP_TRUST}" == "-" ]; then
442		RKCHIP_TRUST=${RKCHIP}
443	fi
444	if [ "${RKCHIP_LABEL}" == "-" ]; then
445		RKCHIP_LABEL=${RKCHIP}
446	fi
447}
448
449function fixup_platform_configure()
450{
451	u_kb="-" u_num="-" t_kb="-" t_num="-"  sha="-" rsa="-"
452
453	for item in "${CHIP_CFG_FIXUP_TABLE[@]}"
454	do
455		config_xxx=`echo ${item} | awk '{ print $1 }'`
456		if grep  -q "^${config_xxx}=y" .config ; then
457			# <*> Fixup rsa/sha pack mode for platforms
458			rsa=`echo ${item} | awk '{ print $2 }'`
459			sha=`echo ${item} | awk '{ print $3 }'`
460
461			# <*> Fixup images size pack for platforms, and ini file
462			if grep -q '^CONFIG_ARM64=y' .config ; then
463				u_kb=`echo  ${item} | awk '{ print $4 }' | awk -F "," '{ print $1 }'`
464				t_kb=`echo  ${item} | awk '{ print $4 }' | awk -F "," '{ print $2 }'`
465				u_num=`echo ${item} | awk '{ print $5 }' | awk -F "," '{ print $1 }'`
466				t_num=`echo ${item} | awk '{ print $5 }' | awk -F "," '{ print $2 }'`
467			else
468				u_kb=`echo  ${item} | awk '{ print $6 }' | awk -F "," '{ print $1 }'`
469				t_kb=`echo  ${item} | awk '{ print $6 }' | awk -F "," '{ print $2 }'`
470				u_num=`echo ${item} | awk '{ print $7 }' | awk -F "," '{ print $1 }'`
471				t_num=`echo ${item} | awk '{ print $7 }' | awk -F "," '{ print $2 }'`
472				# AArch32
473				if grep -q '^CONFIG_ARM64_BOOT_AARCH32=y' .config ; then
474					PADDING=`echo ${item} | awk '{ print $8 }'`
475					if [ "${PADDING}" != "-" ]; then
476						RKCHIP_LOADER=${RKCHIP_LOADER}${PADDING}
477					fi
478					PADDING=`echo  ${item} | awk '{ print $9 }'`
479					if [ "${PADDING}" != "-" ]; then
480						RKCHIP_TRUST=${RKCHIP_TRUST}${PADDING}
481					fi
482					RKCHIP_LABEL=${RKCHIP_LABEL}"AARCH32"
483				fi
484			fi
485		fi
486	done
487
488	if [ "${sha}" != "-" ]; then
489		PLATFORM_SHA="--sha ${sha}"
490	fi
491	if [ "${rsa}" != "-" ]; then
492		PLATFORM_RSA="--rsa ${rsa}"
493	fi
494	if [ "${u_kb}" != "-" ]; then
495		PLATFORM_UBOOT_SIZE="--size ${u_kb} ${u_num}"
496	fi
497	if [ "${t_kb}" != "-" ]; then
498		PLATFORM_TRUST_SIZE="--size ${t_kb} ${t_num}"
499	fi
500}
501
502function select_ini_file()
503{
504	# default
505	INI_LOADER=${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini
506	if [ "${ARM64_TRUSTZONE}" == "y" ]; then
507		INI_TRUST=${RKBIN}/RKTRUST/${RKCHIP_TRUST}TRUST.ini
508	else
509		INI_TRUST=${RKBIN}/RKTRUST/${RKCHIP_TRUST}TOS.ini
510	fi
511
512	# args
513	if [ "${ARG_INI_TRUST}" != "" ]; then
514		INI_TRUST=${ARG_INI_TRUST}
515	fi
516	if [ "${ARG_INI_LOADER}" != "" ]; then
517		INI_LOADER=${ARG_INI_LOADER}
518	fi
519}
520
521function handle_args_late()
522{
523	ARG_LIST_FIT="${ARG_LIST_FIT} --ini-trust ${INI_TRUST} --ini-loader ${INI_LOADER}"
524}
525
526function pack_uboot_image()
527{
528	if [ "${PLATFORM_TYPE}" != "RKFW" ]; then
529		return
530	fi
531
532	# Check file size
533	head_kb=2
534	uboot_kb=`ls -l u-boot.bin | awk '{ print $5 }'`
535	if [ "${PLATFORM_UBOOT_SIZE}" == "" ]; then
536		uboot_max_kb=1046528
537	else
538		uboot_max_kb=`echo ${PLATFORM_UBOOT_SIZE} | awk '{print strtonum($2)}'`
539		uboot_max_kb=$(((uboot_max_kb-head_kb)*1024))
540	fi
541
542	if [ ${uboot_kb} -gt ${uboot_max_kb} ]; then
543		echo
544		echo "ERROR: pack uboot failed! u-boot.bin actual: ${uboot_kb} bytes, max limit: ${uboot_max_kb} bytes"
545		exit 1
546	fi
547
548	# Pack
549	uboot_load_addr=`sed -n "/CONFIG_SYS_TEXT_BASE=/s/CONFIG_SYS_TEXT_BASE=//p" include/autoconf.mk|tr -d '\r'`
550	if [ -z ${uboot_load_addr} ]; then
551		echo "ERROR: No CONFIG_SYS_TEXT_BASE for u-boot";
552		exit 1
553	fi
554	${RKTOOLS}/loaderimage --pack --uboot u-boot.bin uboot.img ${uboot_load_addr} ${PLATFORM_UBOOT_SIZE}
555	rm u-boot.img u-boot-dtb.img -rf
556	echo "pack uboot okay! Input: u-boot.bin"
557}
558
559function pack_uboot_itb_image()
560{
561	ini=${INI_TRUST}
562	if [ ! -f ${INI_TRUST} ]; then
563		echo "pack trust failed! Can't find: ${INI_TRUST}"
564		return
565	fi
566
567	if [ "${ARM64_TRUSTZONE}" == "y" ]; then
568		bl31=`sed -n '/_bl31_/s/PATH=//p' ${ini} |tr -d '\r'`
569		cp ${RKBIN}/${bl31} bl31.elf
570		make CROSS_COMPILE=${TOOLCHAIN_GCC} u-boot.itb
571		echo "pack u-boot.itb okay! Input: ${ini}"
572	else
573		tos_image=`sed -n "/TOS=/s/TOS=//p" ${ini} |tr -d '\r'`
574		tosta_image=`sed -n "/TOSTA=/s/TOSTA=//p" ${ini} |tr -d '\r'`
575		if [ ${tosta_image} ]; then
576			cp ${RKBIN}/${tosta_image} tee.bin
577		elif [ ${tos_image} ]; then
578			cp ${RKBIN}/${tos_image}   tee.bin
579		else
580			echo "ERROR: No any tee bin"
581			exit 1
582		fi
583
584		tee_offset=`sed -n "/ADDR=/s/ADDR=//p" ${ini} |tr -d '\r'`
585		if [ "${tee_offset}" == "" ]; then
586			tee_offset=0x8400000
587		fi
588
589		mcu_enabled=`awk -F"," '/MCU=/ { printf $3 }' ${ini} | tr -d ' '`
590		if [ "${mcu_enabled}" == "enabled" ]; then
591			mcu_image=`awk -F"," '/MCU=/  { printf $1 }' ${ini} | tr -d ' ' | cut -c 5-`
592			cp ${RKBIN}/${mcu_image} mcu.bin
593			mcu_offset=`awk -F"," '/MCU=/ { printf $2 }' ${ini} | tr -d ' '`
594			optional_mcu="-m "${mcu_offset}
595		else
596			optional_mcu=
597		fi
598
599		compression=`awk -F"," '/COMPRESSION=/  { printf $1 }' ${ini} | tr -d ' ' | cut -c 13-`
600		if [ -z "${compression}" ]; then
601			compression="none"
602		fi
603
604		SPL_FIT_SOURCE=`sed -n "/CONFIG_SPL_FIT_SOURCE=/s/CONFIG_SPL_FIT_SOURCE=//p" .config | tr -d '""'`
605		if [ ! -z ${SPL_FIT_SOURCE} ]; then
606			cp ${SPL_FIT_SOURCE} u-boot.its
607		else
608			SPL_FIT_GENERATOR=`sed -n "/CONFIG_SPL_FIT_GENERATOR=/s/CONFIG_SPL_FIT_GENERATOR=//p" .config | tr -d '""'`
609			${SPL_FIT_GENERATOR} -u -t ${tee_offset} -c ${compression} ${optional_mcu} > u-boot.its
610		fi
611		./tools/mkimage -f u-boot.its -E u-boot.itb
612		echo "pack u-boot.itb okay! Input: ${ini}"
613	fi
614	echo
615}
616
617function pack_spl_loader_image()
618{
619	mode=$1
620	tmpdir=${RKBIN}/tmp
621	tmpini=${tmpdir}/${RKCHIP_LOADER}MINIALL.ini
622	ini=${INI_LOADER}
623	if [ ! -f ${INI_LOADER} ]; then
624		echo "pack loader failed! Can't find: ${INI_LOADER}"
625		return
626	fi
627
628	# Find magic for TPL
629	for item in "${CHIP_TPL_MAGIC_TABLE[@]}"
630	do
631		config_xxx=`echo ${item} | awk '{ print $1 }'`
632		if grep -q "^${config_xxx}=y" .config ; then
633			header=`echo ${item} | awk '{ print $2 }'`
634		fi
635	done
636
637	if [ -z ${header} ]; then
638		header=`sed -n '/NAME=/s/NAME=//p' ${ini}`
639	fi
640
641	# Prepare files
642	rm ${tmpdir} -rf && mkdir ${tmpdir} -p
643	cp spl/u-boot-spl.bin ${tmpdir}/ && cp ${ini} ${tmpini}
644
645	if [ "${mode}" == "tpl-spl" ]; then	# pack tpl+spl
646		label="TPL+SPL"
647		cp tpl/u-boot-tpl.bin ${tmpdir}/
648		dd if=${tmpdir}/u-boot-tpl.bin of=${tmpdir}/tpl.bin bs=1 skip=4
649		sed -i "1s/^/${header:0:4}/" ${tmpdir}/tpl.bin
650		sed -i "s/FlashData=.*$/FlashData=.\/tmp\/tpl.bin/" ${tmpini}
651		sed -i "0,/Path1=.*/s/Path1=.*$/Path1=.\/tmp\/tpl.bin/" ${tmpini}
652		sed -i "s/FlashBoot=.*$/FlashBoot=.\/tmp\/u-boot-spl.bin/" ${tmpini}
653	elif [ "${mode}" == "tpl" ]; then	# pack tpl
654		label="TPL"
655		cp tpl/u-boot-tpl.bin ${tmpdir}/
656		dd if=${tmpdir}/u-boot-tpl.bin of=${tmpdir}/tpl.bin bs=1 skip=4
657		sed -i "1s/^/${header:0:4}/" ${tmpdir}/tpl.bin
658		sed -i "s/FlashData=.*$/FlashData=.\/tmp\/tpl.bin/" ${tmpini}
659		sed -i "0,/Path1=.*/s/Path1=.*$/Path1=.\/tmp\/tpl.bin/" ${tmpini}
660	else
661		label="SPL"
662		sed -i "s/FlashBoot=.*$/FlashBoot=.\/tmp\/u-boot-spl.bin/" ${tmpini}
663	fi
664
665	# Pack
666	cd ${RKBIN}
667	${RKTOOLS}/boot_merger ${tmpini}
668
669	rm ${tmpdir} -rf && cd -
670	rm *_loader_*.bin -rf && mv ${RKBIN}/*_loader_*.bin ./
671	filename=`basename *_loader_*.bin`
672	if [[ ${filename} != *spl* ]]; then
673		rename 's/loader_/spl_loader_/' *_loader_*.bin
674	fi
675	echo "pack loader(${label}) okay! Input: ${ini}"
676}
677
678function pack_loader_image()
679{
680	ini=${INI_LOADER}
681	if [ ! -f ${INI_LOADER} ]; then
682		echo "pack loader failed! Can't find: ${INI_LOADER}"
683		return
684	fi
685
686	rm *_loader_*.bin -rf
687	numline=`cat ${ini} | wc -l`
688	if [ ${numline} -eq 1 ]; then
689		image=`sed -n "/PATH=/p" ${ini} | tr -d '\r' | cut -d '=' -f 2`
690		cp ${RKBIN}/${image} ./
691	else
692		cd ${RKBIN}
693		${RKTOOLS}/boot_merger ${ini}
694		cd - && mv ${RKBIN}/*_loader_*.bin ./
695	fi
696
697	file=`ls *loader*.bin`
698	echo "pack ${file} okay! Input: ${ini}"
699}
700
701function pack_arm32_trust_image()
702{
703	ini=$1
704	tos_image=`sed -n "/TOS=/s/TOS=//p" ${ini} |tr -d '\r'`
705	tosta_image=`sed -n "/TOSTA=/s/TOSTA=//p" ${ini} |tr -d '\r'`
706	tee_output=`sed -n "/OUTPUT=/s/OUTPUT=//p" ${ini} |tr -d '\r'`
707	if [ "${tee_output}" == "" ]; then
708		tee_output="./trust.img"
709	fi
710	tee_offset=`sed -n "/ADDR=/s/ADDR=//p" ${ini} |tr -d '\r'`
711	if [ "${tee_offset}" == "" ]; then
712		tee_offset=0x8400000
713	fi
714
715	# OP-TEE is 132M(0x8400000) offset from DRAM base.
716	dram_base=`sed -n "/CONFIG_SYS_SDRAM_BASE=/s/CONFIG_SYS_SDRAM_BASE=//p" include/autoconf.mk|tr -d '\r'`
717	tee_load_addr=$((dram_base+tee_offset))
718	tee_load_addr=$(echo "obase=16;${tee_load_addr}"|bc) # Convert Dec to Hex
719
720	if [ ${tosta_image} ]; then
721		${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${tosta_image} ${tee_output} ${tee_load_addr} ${PLATFORM_TRUST_SIZE}
722	elif [ ${tos_image} ]; then
723		${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${tos_image}   ${tee_output} ${tee_load_addr} ${PLATFORM_TRUST_SIZE}
724	else
725		echo "ERROR: No any tee bin"
726		exit 1
727	fi
728	echo "pack trust okay! Input: ${ini}"
729}
730
731function pack_arm64_trust_image()
732{
733	ini=$1
734	cd ${RKBIN}
735	${RKTOOLS}/trust_merger ${PLATFORM_SHA} ${PLATFORM_RSA} ${PLATFORM_TRUST_SIZE} ${ini}
736	cd - && mv ${RKBIN}/trust*.img ./
737	echo "pack trust okay! Input: ${ini}"
738}
739
740function pack_trust_image()
741{
742	if [ "${PLATFORM_TYPE}" != "RKFW" ]; then
743		return
744	fi
745
746	rm trust*.img -rf
747	ini=${INI_TRUST}
748	if [ ! -f ${INI_TRUST} ]; then
749		echo "pack trust failed! Can't find: ${INI_TRUST}"
750		return
751	fi
752
753	numline=`cat ${ini} | wc -l`
754	if [ ${numline} -eq 1 ]; then
755		image=`sed -n "/PATH=/p" ${ini} | tr -d '\r' | cut -d '=' -f 2`
756		cp ${RKBIN}/${image} ./trust.img
757		echo "pack trust okay! Input: ${ini}"
758		return;
759	else
760		if [ "${ARM64_TRUSTZONE}" == "y" ]; then
761			pack_arm64_trust_image ${ini}
762		else
763			pack_arm32_trust_image ${ini}
764		fi
765	fi
766}
767
768function pack_fit_image()
769{
770	if [ "${ARG_NO_UBOOT}" == "y" ]; then
771		rm u-boot-nodtb.bin u-boot.dtb -rf
772		touch u-boot-nodtb.bin u-boot.dtb
773	fi
774
775	if grep -q '^CONFIG_FIT_SIGNATURE=y' .config ; then
776		./scripts/fit-mkimg.sh --uboot-itb --boot-itb ${ARG_LIST_FIT}
777	else
778		rm uboot.img trust*.img -rf
779		./scripts/fit-mkimg.sh --uboot-itb --no-vboot --no-rebuild ${ARG_LIST_FIT}
780		echo "pack uboot.img okay! Input: ${INI_TRUST}"
781	fi
782}
783
784function pack_images()
785{
786	if [ "${ARG_NO_PACK}" != "y" ]; then
787		if [ "${PLATFORM_TYPE}" == "RKFW" ]; then
788			pack_uboot_image
789			pack_trust_image
790			pack_loader_image
791		elif [ "${PLATFORM_TYPE}" == "FIT" ]; then
792			pack_fit_image ${ARG_LIST_FIT}
793		fi
794	fi
795}
796
797function clean_files()
798{
799	rm spl/u-boot-spl.dtb tpl/u-boot-tpl.dtb u-boot.dtb -rf
800}
801
802function finish()
803{
804	echo
805	if [ "${ARG_BOARD}" == "" ]; then
806		echo "Platform ${RKCHIP_LABEL} is build OK, with exist .config"
807	else
808		echo "Platform ${RKCHIP_LABEL} is build OK, with new .config(${MAKE_CMD})"
809	fi
810}
811
812process_args $*
813prepare
814select_toolchain
815select_chip_info
816fixup_platform_configure
817select_ini_file
818handle_args_late
819sub_commands
820clean_files
821make CROSS_COMPILE=${TOOLCHAIN_GCC} all --jobs=${JOB}
822pack_images
823finish
824
825