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