xref: /rk3399_rockchip-uboot/make.sh (revision 7c1937d6d1c7daf8e59b4760f8adc7ee42bd7bea)
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		grep '^CONFIG_ROCKCHIP_RK3368=y' ${OUTDIR}/.config >/dev/null \
303			&& RKCHIP=RK3368H
304	elif [ $count -gt 1 ]; then
305		# Grep the RK CHIP variant
306		grep '^CONFIG_ROCKCHIP_PX3SE=y' ${OUTDIR}/.config > /dev/null \
307			&& RKCHIP=PX3SE
308		grep '^CONFIG_ROCKCHIP_RK3126=y' ${OUTDIR}/.config >/dev/null \
309			&& RKCHIP=RK3126
310		grep '^CONFIG_ROCKCHIP_RK3326=y' ${OUTDIR}/.config >/dev/null \
311			&& RKCHIP=RK3326
312		grep '^CONFIG_ROCKCHIP_RK3128X=y' ${OUTDIR}/.config >/dev/null \
313			&& RKCHIP=RK3128X
314	else
315		echo "Can't get Rockchip SoC definition in .config"
316		exit 1
317	fi
318
319	# Default use RKCHIP
320	RKCHIP_LABEL=${RKCHIP}
321	RKCHIP_LOADER=${RKCHIP}
322	RKCHIP_TRUST=${RKCHIP}
323
324	# Read from RKCHIP_INI_DESC
325	for item in "${RKCHIP_INI_DESC[@]}"
326	do
327		target_board=`echo $item | awk '{ print $1 }'`
328		if grep  -q "^${target_board}=y" ${OUTDIR}/.config ; then
329			value=`echo $item | awk '{ print $2 }'`
330			if [ "$value" != "NA" ]; then
331				RKCHIP_LABEL=${value};
332			fi
333			value=`echo $item | awk '{ print $3 }'`
334			if [ "$value" != "NA" ]; then
335				RKCHIP_LOADER=${value};
336			fi
337			value=`echo $item | awk '{ print $4 }'`
338			if [ "$value" != "NA" ]; then
339				RKCHIP_TRUST=${value};
340			fi
341		fi
342	done
343}
344
345# Fixup platform special configure
346#	1. fixup pack mode;
347#	2. fixup image size
348#	3. fixup ARM64 cpu boot with AArch32
349fixup_platform_configure()
350{
351	local count plat
352
353# <*> Fixup rsa/sha pack mode for platforms
354	# RK3308/PX30/RK3326 use RSA-PKCS1 V2.1, it's pack magic is "3"
355	if [ $RKCHIP = "PX30" -o $RKCHIP = "RK3326" -o $RKCHIP = "RK3308" ]; then
356		PLATFORM_RSA="--rsa 3"
357	# RK3368 use rk big endian SHA256, it's pack magic is "2"
358	elif [ $RKCHIP = "RK3368" ]; then
359		PLATFORM_SHA="--sha 2"
360	# other platforms use default configure
361	fi
362
363# <*> Fixup images size pack for platforms
364	if [ $RKCHIP = "RK3308" ]; then
365		if grep -q '^CONFIG_ARM64_BOOT_AARCH32=y' ${OUTDIR}/.config ; then
366			PLATFORM_UBOOT_IMG_SIZE="--size 512 2"
367			PLATFORM_TRUST_IMG_SIZE="--size 512 2"
368		else
369			PLATFORM_UBOOT_IMG_SIZE="--size 1024 2"
370			PLATFORM_TRUST_IMG_SIZE="--size 1024 2"
371		fi
372	fi
373
374# <*> Fixup PLATFORM_AARCH32 for ARM64 cpu platforms
375	if [ $RKCHIP = "RK3308" ]; then
376		if grep -q '^CONFIG_ARM64_BOOT_AARCH32=y' ${OUTDIR}/.config ; then
377			PLATFORM_AARCH32="AARCH32"
378		fi
379	fi
380}
381
382pack_uboot_image()
383{
384	local UBOOT_LOAD_ADDR
385
386	UBOOT_LOAD_ADDR=`sed -n "/CONFIG_SYS_TEXT_BASE=/s/CONFIG_SYS_TEXT_BASE=//p" ${OUTDIR}/include/autoconf.mk|tr -d '\r'`
387	${RKTOOLS}/loaderimage --pack --uboot ${OUTDIR}/u-boot.bin uboot.img ${UBOOT_LOAD_ADDR} ${PLATFORM_UBOOT_IMG_SIZE}
388
389	# Delete u-boot.img and u-boot-dtb.img, which makes users not be confused with final uboot.img
390	if [ -f ${OUTDIR}/u-boot.img ]; then
391		rm ${OUTDIR}/u-boot.img
392	fi
393
394	if [ -f ${OUTDIR}/u-boot-dtb.img ]; then
395		rm ${OUTDIR}/u-boot-dtb.img
396	fi
397
398	echo "pack uboot okay! Input: ${OUTDIR}/u-boot.bin"
399}
400
401pack_loader_image()
402{
403	local mode=$1 files ini
404
405	if [ ! -f ${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini ]; then
406		echo "pack loader failed! Can't find: ${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini"
407		return
408	fi
409
410	cd ${RKBIN}
411
412	if [ "${mode}" = 'all' ]; then
413		files=`ls ${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL*.ini`
414		for ini in $files
415		do
416			if [ -f "$ini" ]; then
417				${RKTOOLS}/boot_merger --replace tools/rk_tools/ ./ $ini
418				echo "pack loader okay! Input: $ini"
419			fi
420		done
421	else
422		${RKTOOLS}/boot_merger --replace tools/rk_tools/ ./ ${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini
423		echo "pack loader okay! Input: ${RKBIN}/RKBOOT/${RKCHIP_LOADER}MINIALL.ini"
424	fi
425
426	cd - && mv ${RKBIN}/*_loader_*.bin ./
427}
428
429pack_trust_image()
430{
431	local TOS TOS_TA DARM_BASE TEE_LOAD_ADDR TEE_OFFSET=0x8400000
432
433	# ARM64 uses trust_merger
434	if grep -Eq ''^CONFIG_ARM64=y'|'^CONFIG_ARM64_BOOT_AARCH32=y'' ${OUTDIR}/.config ; then
435		if [ ! -f ${RKBIN}/RKTRUST/${RKCHIP_TRUST}${PLATFORM_AARCH32}TRUST.ini ]; then
436			echo "pack trust failed! Can't find: ${RKBIN}/RKTRUST/${RKCHIP_TRUST}${PLATFORM_AARCH32}TRUST.ini"
437			return
438		fi
439
440		cd ${RKBIN}
441		${RKTOOLS}/trust_merger ${PLATFORM_SHA} ${PLATFORM_RSA} ${PLATFORM_TRUST_IMG_SIZE} --replace tools/rk_tools/ ./ ${RKBIN}/RKTRUST/${RKCHIP_TRUST}${PLATFORM_AARCH32}TRUST.ini
442
443		cd - && mv ${RKBIN}/trust.img ./trust.img
444		echo "pack trust okay! Input: ${RKBIN}/RKTRUST/${RKCHIP_TRUST}${PLATFORM_AARCH32}TRUST.ini"
445	# ARM uses loaderimage
446	else
447		if [ ! -f ${RKBIN}/RKTRUST/${RKCHIP_TRUST}TOS.ini ]; then
448			echo "pack trust failed! Can't find: ${RKBIN}/RKTRUST/${RKCHIP_TRUST}TOS.ini"
449			return
450		fi
451
452		# OP-TEE is 132M(0x8400000) offset from DRAM base.
453		DARM_BASE=`sed -n "/CONFIG_SYS_SDRAM_BASE=/s/CONFIG_SYS_SDRAM_BASE=//p" ${OUTDIR}/include/autoconf.mk|tr -d '\r'`
454		TEE_LOAD_ADDR=$((DARM_BASE+TEE_OFFSET))
455
456		# Convert Dec to Hex
457		TEE_LOAD_ADDR=$(echo "obase=16;${TEE_LOAD_ADDR}"|bc)
458
459		# Parse orignal path
460		TOS=`sed -n "/TOS=/s/TOS=//p" ${RKBIN}/RKTRUST/${RKCHIP_TRUST}TOS.ini|tr -d '\r'`
461		TOS_TA=`sed -n "/TOSTA=/s/TOSTA=//p" ${RKBIN}/RKTRUST/${RKCHIP_TRUST}TOS.ini|tr -d '\r'`
462
463		# replace "./tools/rk_tools/" with "./" to compatible legacy ini content of rkdevelop branch
464		TOS=$(echo ${TOS} | sed "s/tools\/rk_tools\//\.\//g")
465		TOS_TA=$(echo ${TOS_TA} | sed "s/tools\/rk_tools\//\.\//g")
466
467		if [ x$TOS_TA != x -a x$TOS != x ]; then
468			${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS} ./trust.img ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE}
469			${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS_TA} ./trust_with_ta.img ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE}
470			echo "Both trust.img and trust_with_ta.img are ready"
471		elif [ $TOS ]; then
472			${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS} ./trust.img ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE}
473			echo "trust.img is ready"
474		elif [ $TOS_TA ]; then
475			${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS_TA} ./trust.img ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE}
476			echo "trust.img with ta is ready"
477		else
478			echo "Can't find any tee bin"
479			exit 1
480		fi
481
482		echo "pack trust okay! Input: ${RKBIN}/RKTRUST/${RKCHIP_TRUST}TOS.ini"
483	fi
484}
485
486finish()
487{
488	echo
489	if [ "$BOARD" = '' ]; then
490		echo "Platform ${RKCHIP_LABEL}${PLATFORM_AARCH32} is build OK, with exist .config"
491	else
492		echo "Platform ${RKCHIP_LABEL}${PLATFORM_AARCH32} is build OK, with new .config(make ${BOARD}_defconfig)"
493	fi
494}
495
496prepare
497select_toolchain
498select_chip_info
499fixup_platform_configure
500sub_commands
501make CROSS_COMPILE=${TOOLCHAIN_GCC}  all --jobs=${JOB} ${OUTOPT}
502pack_uboot_image
503pack_loader_image
504pack_trust_image
505finish
506