xref: /rk3399_rockchip-uboot/make.sh (revision d156c9ff6fddc6358d6746127b8dc4e9ca224857)
1#!/bin/bash
2set -e
3BOARD=$1
4SUBCMD=$1
5FUNCADDR=$1
6JOB=`sed -n "N;/processor/p" /proc/cpuinfo|wc -l`
7SUPPORT_LIST=`ls configs/*[r,p][x,v,k][0-9][0-9]*_defconfig`
8
9# @target board: defined in arch/arm/mach-rockchip/<soc>/Kconfig
10# @label: show build message
11# @loader: search for ini file to pack loader
12# @trust: search for ini file to pack trust
13#
14# "NA" means use default name reading from .config
15#
16# Format:           target board               label         loader      trust
17RKCHIP_INI_DESC=("CONFIG_TARGET_GVA_RK3229       NA          RK322XAT     NA"
18# to be add...
19                )
20
21########################################### User can modify #############################################
22# User's rkbin tool relative path
23RKBIN_TOOLS=../rkbin/tools
24
25# User's GCC toolchain and relative path
26ADDR2LINE_ARM32=arm-linux-gnueabihf-addr2line
27ADDR2LINE_ARM64=aarch64-linux-gnu-addr2line
28OBJ_ARM32=arm-linux-gnueabihf-objdump
29OBJ_ARM64=aarch64-linux-gnu-objdump
30GCC_ARM32=arm-linux-gnueabihf-
31GCC_ARM64=aarch64-linux-gnu-
32TOOLCHAIN_ARM32=../prebuilts/gcc/linux-x86/arm/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabihf/bin
33TOOLCHAIN_ARM64=../prebuilts/gcc/linux-x86/aarch64/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin
34
35########################################### User not touch #############################################
36# Declare global INI file searching index name for every chip, update in select_chip_info()
37RKCHIP=
38RKCHIP_LABEL=
39RKCHIP_LOADER=
40RKCHIP_TRUST=
41
42# Declare global rkbin RKTOOLS and rkbin repository path, updated in prepare()
43RKTOOLS=
44RKBIN=
45
46# Declare global toolchain path for CROSS_COMPILE, updated in select_toolchain()
47TOOLCHAIN_GCC=
48TOOLCHAIN_OBJDUMP=
49TOOLCHAIN_ADDR2LINE=
50
51# Declare global default output dir and cmd, update in prepare()
52OUTDIR=$2
53OUTOPT=
54
55# Declare global plaform configure, updated in fixup_platform_configure()
56PLATFORM_RSA=
57PLATFORM_SHA=
58PLATFORM_UBOOT_IMG_SIZE=
59PLATFORM_TRUST_IMG_SIZE=
60PLATFORM_AARCH32=
61#########################################################################################################
62help()
63{
64	echo
65	echo "Usage:"
66	echo "	./make.sh [board|subcmd] [O=<dir>]"
67	echo
68	echo "	 - board: board name of defconfig"
69	echo "	 - subcmd: loader|loader-all|trust|uboot|elf|map|sym|<addr>|"
70	echo "	 - O=<dir>: assigned output directory"
71	echo
72	echo "Example:"
73	echo
74	echo "1. Build board:"
75	echo "	./make.sh evb-rk3399            ---- build for evb-rk3399_defconfig"
76	echo "	./make.sh evb-rk3399 O=rockdev  ---- build for evb-rk3399_defconfig with output dir "./rockdev""
77	echo "	./make.sh firefly-rk3288        ---- build for firefly-rk3288_defconfig"
78	echo "	./make.sh                       ---- build with exist .config"
79	echo
80	echo "	After build, images of uboot, loader and trust are all generated."
81	echo
82	echo "2. Pack helper:"
83	echo "	./make.sh trust         --- pack trust.img"
84	echo "	./make.sh uboot         --- pack uboot.img"
85	echo "	./make.sh loader        --- pack loader bin"
86	echo "	./make.sh loader-all	--- pack loader bin (all supported loaders)"
87	echo
88	echo "3. Debug helper:"
89	echo "	./make.sh elf           --- dump elf file with -D(default)"
90	echo "	./make.sh elf-S         --- dump elf file with -S"
91	echo "	./make.sh elf-d         --- dump elf file with -d"
92	echo "	./make.sh <addr>        --- dump function symbol and code position of address"
93	echo "	./make.sh map           --- cat u-boot.map"
94	echo "	./make.sh sym           --- cat u-boot.sym"
95}
96
97prepare()
98{
99	local absolute_path cmd dir count
100
101	# Parse output directory 'O=<dir>'
102	cmd=${OUTDIR%=*}
103	if [ "${cmd}" = 'O' ]; then
104		OUTDIR=${OUTDIR#*=}
105		OUTOPT=O=${OUTDIR}
106	else
107		case $BOARD in
108			# Parse from exit .config
109			''|elf*|loader*|trust|uboot|map|sym)
110			count=`find -name .config | wc -l`
111			dir=`find -name .config`
112			# Good, find only one .config
113			if [ $count -eq 1 ]; then
114				dir=${dir%/*}
115				OUTDIR=${dir#*/}
116				# Set OUTOPT if not current directory
117				if [ $OUTDIR != '.' ]; then
118					OUTOPT=O=${OUTDIR}
119				fi
120			elif [ $count -eq 0 ]; then
121				echo
122				echo "Build failed, Can't find .config"
123				help
124				exit 1
125			else
126				echo
127				echo "Build failed, find $count '.config': "
128				echo "$dir"
129				echo "Please leave only one of them"
130				exit 1
131			fi
132			;;
133
134			*)
135			OUTDIR=.
136			;;
137		esac
138	fi
139
140	# Parse help and make defconfig
141	case $BOARD in
142		#Help
143		--help|-help|help|--h|-h)
144		help
145		exit 0
146		;;
147
148		#Subcmd
149		''|elf*|loader*|trust|uboot|map|sym)
150		;;
151
152		*)
153		#Func address is valid ?
154		if [ -z $(echo ${FUNCADDR} | sed 's/[0-9,a-f,A-F,x,X]//g') ]; then
155			return
156		elif [ ! -f configs/${BOARD}_defconfig ]; then
157			echo
158			echo "Can't find: configs/${BOARD}_defconfig"
159			echo
160			echo "******** Rockchip Support List *************"
161			echo "${SUPPORT_LIST}"
162			echo "********************************************"
163			echo
164			exit 1
165		else
166			echo "make for ${BOARD}_defconfig by -j${JOB}"
167			make ${BOARD}_defconfig ${OUTOPT}
168		fi
169		;;
170	esac
171
172	# Initialize RKBIN and RKTOOLS
173	if [ -d ${RKBIN_TOOLS} ]; then
174		absolute_path=$(cd `dirname ${RKBIN_TOOLS}`; pwd)
175		RKBIN=${absolute_path}
176		RKTOOLS=${absolute_path}/tools
177	else
178		echo
179		echo "Can't find '../rkbin/' repository, please download it before pack image!"
180		echo "How to obtain? 3 ways:"
181		echo "	1. Login your Rockchip gerrit account: \"Projects\" -> \"List\" -> search \"rk/rkbin\" repository"
182		echo "	2. Github repository: https://github.com/rockchip-linux/rkbin"
183		echo "	3. Download full release SDK repository"
184		exit 1
185	fi
186}
187
188select_toolchain()
189{
190	local absolute_path
191
192	if grep  -q '^CONFIG_ARM64=y' ${OUTDIR}/.config ; then
193		if [ -d ${TOOLCHAIN_ARM64} ]; then
194			absolute_path=$(cd `dirname ${TOOLCHAIN_ARM64}`; pwd)
195			TOOLCHAIN_GCC=${absolute_path}/bin/${GCC_ARM64}
196			TOOLCHAIN_OBJDUMP=${absolute_path}/bin/${OBJ_ARM64}
197			TOOLCHAIN_ADDR2LINE=${absolute_path}/bin/${ADDR2LINE_ARM64}
198		else
199			echo "Can't find toolchain: ${TOOLCHAIN_ARM64}"
200			exit 1
201		fi
202	else
203		if [ -d ${TOOLCHAIN_ARM32} ]; then
204			absolute_path=$(cd `dirname ${TOOLCHAIN_ARM32}`; pwd)
205			TOOLCHAIN_GCC=${absolute_path}/bin/${GCC_ARM32}
206			TOOLCHAIN_OBJDUMP=${absolute_path}/bin/${OBJ_ARM32}
207			TOOLCHAIN_ADDR2LINE=${absolute_path}/bin/${ADDR2LINE_ARM32}
208		else
209			echo "Can't find toolchain: ${TOOLCHAIN_ARM32}"
210			exit 1
211		fi
212	fi
213
214	# echo "toolchain: ${TOOLCHAIN_GCC}"
215}
216
217sub_commands()
218{
219	local cmd=${SUBCMD%-*} opt=${SUBCMD#*-}
220
221	case $cmd in
222		elf)
223		if [ ! -f ${OUTDIR}/u-boot ]; then
224			echo "Can't find elf file: ${OUTDIR}/u-boot"
225			exit 1
226		else
227			# default 'cmd' without option, use '-D'
228			if [ "${cmd}" = 'elf' -a "${opt}" = 'elf' ]; then
229				opt=D
230			fi
231			${TOOLCHAIN_OBJDUMP} -${opt} ${OUTDIR}/u-boot | less
232			exit 0
233		fi
234		;;
235
236		map)
237		cat ${OUTDIR}/u-boot.map | less
238		exit 0
239		;;
240
241		sym)
242		cat ${OUTDIR}/u-boot.sym | less
243		exit 0
244		;;
245
246		trust)
247		pack_trust_image
248		exit 0
249		;;
250
251		loader)
252		pack_loader_image ${opt}
253		exit 0
254		;;
255
256		uboot)
257		pack_uboot_image
258		exit 0
259		;;
260
261		*)
262		# Search function and code position of address
263		if [ -z $(echo ${FUNCADDR} | sed 's/[0-9,a-f,A-F,x,X]//g') ] && [ ${FUNCADDR} ]; then
264			# With prefix: '0x' or '0X'
265			if [ `echo ${FUNCADDR} | sed -n "/0[x,X]/p" | wc -l` -ne 0 ]; then
266				FUNCADDR=`echo $FUNCADDR | awk '{ print strtonum($0) }'`
267				FUNCADDR=`echo "obase=16;${FUNCADDR}"|bc |tr '[A-Z]' '[a-z]'`
268			fi
269
270			echo
271			sed -n "/${FUNCADDR}/p" ${OUTDIR}/u-boot.sym
272			${TOOLCHAIN_ADDR2LINE} -e ${OUTDIR}/u-boot ${FUNCADDR}
273			exit 0
274		fi
275		;;
276	esac
277}
278
279# We select chip info to do:
280#	1. RKCHIP: fixup platform configure
281#	2. RKCHIP_LOADER: search ini file to pack loader
282#	3. RKCHIP_TRUST: search ini file to pack trust
283#	4. RKCHIP_LABEL: show build message
284#
285# We read chip info from .config and 'RKCHIP_INI_DESC'
286select_chip_info()
287{
288	local target_board item value
289
290	# Read RKCHIP firstly from .config
291	# The regular expression that matching:
292	#  - PX30, PX3SE
293	#  - RK????, RK????X
294	#  - RV????
295	local chip_reg='^CONFIG_ROCKCHIP_[R,P][X,V,K][0-9ESX]{2,5}'
296	count=`egrep -c ${chip_reg} ${OUTDIR}/.config`
297	# Obtain the matching only
298	RKCHIP=`egrep -o ${chip_reg} ${OUTDIR}/.config`
299
300	if [ $count -eq 1 ]; then
301		RKCHIP=${RKCHIP##*_}
302	elif [ $count -gt 1 ]; then
303		# Grep the RK CHIP variant
304		grep '^CONFIG_ROCKCHIP_PX3SE=y' ${OUTDIR}/.config > /dev/null \
305			&& RKCHIP=PX3SE
306		grep '^CONFIG_ROCKCHIP_RK3126=y' ${OUTDIR}/.config >/dev/null \
307			&& RKCHIP=RK3126
308		grep '^CONFIG_ROCKCHIP_RK3326=y' ${OUTDIR}/.config >/dev/null \
309			&& RKCHIP=RK3326
310		grep '^CONFIG_ROCKCHIP_RK3128X=y' ${OUTDIR}/.config >/dev/null \
311			&& RKCHIP=RK3128X
312	else
313		echo "Can't get Rockchip SoC definition in .config"
314		exit 1
315	fi
316
317	# Default use RKCHIP
318	RKCHIP_LABEL=${RKCHIP}
319	RKCHIP_LOADER=${RKCHIP}
320	RKCHIP_TRUST=${RKCHIP}
321
322	# Read from RKCHIP_INI_DESC
323	for item in "${RKCHIP_INI_DESC[@]}"
324	do
325		target_board=`echo $item | awk '{ print $1 }'`
326		if grep  -q "^${target_board}=y" ${OUTDIR}/.config ; then
327			value=`echo $item | awk '{ print $2 }'`
328			if [ "$value" != "NA" ]; then
329				RKCHIP_LABEL=${value};
330			fi
331			value=`echo $item | awk '{ print $3 }'`
332			if [ "$value" != "NA" ]; then
333				RKCHIP_LOADER=${value};
334			fi
335			value=`echo $item | awk '{ print $4 }'`
336			if [ "$value" != "NA" ]; then
337				RKCHIP_TRUST=${value};
338			fi
339		fi
340	done
341}
342
343# Fixup platform special configure
344#	1. fixup pack mode;
345#	2. fixup image size
346#	3. fixup ARM64 cpu boot with AArch32
347fixup_platform_configure()
348{
349	local count plat
350
351# <*> Fixup rsa/sha pack mode for platforms
352	# RK3308/PX30/RK3326 use RSA-PKCS1 V2.1, it's pack magic is "3"
353	if [ $RKCHIP = "PX30" -o $RKCHIP = "RK3326" -o $RKCHIP = "RK3308" ]; then
354		PLATFORM_RSA="--rsa 3"
355	# RK3368 use rk big endian SHA256, it's pack magic is "2"
356	elif [ $RKCHIP = "RK3368" ]; then
357		PLATFORM_SHA="--sha 2"
358	# other platforms use default configure
359	fi
360
361# <*> Fixup images size pack for platforms
362	if [ $RKCHIP = "RK3308" ]; then
363		if grep -q '^CONFIG_ARM64_BOOT_AARCH32=y' ${OUTDIR}/.config ; then
364			PLATFORM_UBOOT_IMG_SIZE="--size 512 2"
365			PLATFORM_TRUST_IMG_SIZE="--size 512 2"
366		else
367			PLATFORM_UBOOT_IMG_SIZE="--size 1024 2"
368			PLATFORM_TRUST_IMG_SIZE="--size 1024 2"
369		fi
370	fi
371
372# <*> Fixup PLATFORM_AARCH32 for ARM64 cpu platforms
373	if [ $RKCHIP = "RK3308" ]; then
374		if grep -q '^CONFIG_ARM64_BOOT_AARCH32=y' ${OUTDIR}/.config ; then
375			PLATFORM_AARCH32="AARCH32"
376		fi
377	fi
378}
379
380pack_uboot_image()
381{
382	local UBOOT_LOAD_ADDR
383
384	UBOOT_LOAD_ADDR=`sed -n "/CONFIG_SYS_TEXT_BASE=/s/CONFIG_SYS_TEXT_BASE=//p" ${OUTDIR}/include/autoconf.mk|tr -d '\r'`
385	${RKTOOLS}/loaderimage --pack --uboot ${OUTDIR}/u-boot.bin uboot.img ${UBOOT_LOAD_ADDR} ${PLATFORM_UBOOT_IMG_SIZE}
386
387	# Delete u-boot.img and u-boot-dtb.img, which makes users not be confused with final uboot.img
388	if [ -f ${OUTDIR}/u-boot.img ]; then
389		rm ${OUTDIR}/u-boot.img
390	fi
391
392	if [ -f ${OUTDIR}/u-boot-dtb.img ]; then
393		rm ${OUTDIR}/u-boot-dtb.img
394	fi
395
396	echo "pack uboot okay! Input: ${OUTDIR}/u-boot.bin"
397}
398
399pack_loader_image()
400{
401	local mode=$1 files ini
402
403	if [ ! -f ${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini ]; then
404		echo "pack loader failed! Can't find: ${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini"
405		return
406	fi
407
408	cd ${RKBIN}
409
410	if [ "${mode}" = 'all' ]; then
411		files=`ls ${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL*.ini`
412		for ini in $files
413		do
414			if [ -f "$ini" ]; then
415				${RKTOOLS}/boot_merger --replace tools/rk_tools/ ./ $ini
416				echo "pack loader okay! Input: $ini"
417			fi
418		done
419	else
420		${RKTOOLS}/boot_merger --replace tools/rk_tools/ ./ ${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini
421		echo "pack loader okay! Input: ${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini"
422	fi
423
424	cd - && mv ${RKBIN}/*_loader_*.bin ./
425}
426
427pack_trust_image()
428{
429	local TOS TOS_TA DARM_BASE TEE_LOAD_ADDR TEE_OFFSET=0x8400000
430
431	# ARM64 uses trust_merger
432	if grep -Eq ''^CONFIG_ARM64=y'|'^CONFIG_ARM64_BOOT_AARCH32=y'' ${OUTDIR}/.config ; then
433		if [ ! -f ${RKBIN}/RKTRUST/${RKCHIP_TRUST}${PLATFORM_AARCH32}TRUST.ini ]; then
434			echo "pack trust failed! Can't find: ${RKBIN}/RKTRUST/${RKCHIP_TRUST}${PLATFORM_AARCH32}TRUST.ini"
435			return
436		fi
437
438		cd ${RKBIN}
439		${RKTOOLS}/trust_merger ${PLATFORM_SHA} ${PLATFORM_RSA} ${PLATFORM_TRUST_IMG_SIZE} --replace tools/rk_tools/ ./ ${RKBIN}/RKTRUST/${RKCHIP_TRUST}${PLATFORM_AARCH32}TRUST.ini
440
441		cd - && mv ${RKBIN}/trust.img ./trust.img
442		echo "pack trust okay! Input: ${RKBIN}/RKTRUST/${RKCHIP_TRUST}${PLATFORM_AARCH32}TRUST.ini"
443	# ARM uses loaderimage
444	else
445		if [ ! -f ${RKBIN}/RKTRUST/${RKCHIP_TRUST}TOS.ini ]; then
446			echo "pack trust failed! Can't find: ${RKBIN}/RKTRUST/${RKCHIP_TRUST}TOS.ini"
447			return
448		fi
449
450		# OP-TEE is 132M(0x8400000) offset from DRAM base.
451		DARM_BASE=`sed -n "/CONFIG_SYS_SDRAM_BASE=/s/CONFIG_SYS_SDRAM_BASE=//p" ${OUTDIR}/include/autoconf.mk|tr -d '\r'`
452		TEE_LOAD_ADDR=$((DARM_BASE+TEE_OFFSET))
453
454		# Convert Dec to Hex
455		TEE_LOAD_ADDR=$(echo "obase=16;${TEE_LOAD_ADDR}"|bc)
456
457		# Parse orignal path
458		TOS=`sed -n "/TOS=/s/TOS=//p" ${RKBIN}/RKTRUST/${RKCHIP_TRUST}TOS.ini|tr -d '\r'`
459		TOS_TA=`sed -n "/TOSTA=/s/TOSTA=//p" ${RKBIN}/RKTRUST/${RKCHIP_TRUST}TOS.ini|tr -d '\r'`
460
461		# replace "./tools/rk_tools/" with "./" to compatible legacy ini content of rkdevelop branch
462		TOS=$(echo ${TOS} | sed "s/tools\/rk_tools\//\.\//g")
463		TOS_TA=$(echo ${TOS_TA} | sed "s/tools\/rk_tools\//\.\//g")
464
465		if [ x$TOS_TA != x -a x$TOS != x ]; then
466			${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS} ./trust.img ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE}
467			${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS_TA} ./trust_with_ta.img ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE}
468			echo "Both trust.img and trust_with_ta.img are ready"
469		elif [ $TOS ]; then
470			${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS} ./trust.img ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE}
471			echo "trust.img is ready"
472		elif [ $TOS_TA ]; then
473			${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS_TA} ./trust.img ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE}
474			echo "trust.img with ta is ready"
475		else
476			echo "Can't find any tee bin"
477			exit 1
478		fi
479
480		echo "pack trust okay! Input: ${RKBIN}/RKTRUST/${RKCHIP_TRUST}TOS.ini"
481	fi
482}
483
484finish()
485{
486	echo
487	if [ "$BOARD" = '' ]; then
488		echo "Platform ${RKCHIP_LABEL}${PLATFORM_AARCH32} is build OK, with exist .config"
489	else
490		echo "Platform ${RKCHIP_LABEL}${PLATFORM_AARCH32} is build OK, with new .config(make ${BOARD}_defconfig)"
491	fi
492}
493
494prepare
495select_toolchain
496select_chip_info
497fixup_platform_configure
498sub_commands
499make CROSS_COMPILE=${TOOLCHAIN_GCC}  all --jobs=${JOB} ${OUTOPT}
500pack_uboot_image
501pack_loader_image
502pack_trust_image
503finish
504