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