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