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