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