xref: /rk3399_rockchip-uboot/make.sh (revision 6a7642592aeb7ed15f259e5d89ddf2c21d158f0b)
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]//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]//g') ] && [ ${FUNCADDR} ]; then
249			echo
250			sed -n "/${FUNCADDR}/p" ${OUTDIR}/u-boot.sym
251			${TOOLCHAIN_ADDR2LINE} -e ${OUTDIR}/u-boot ${FUNCADDR}
252			exit 0
253		fi
254		;;
255	esac
256}
257
258# Support platform special configure
259#	1. fixup chip name;
260#	2. fixup pack mode;
261#	3. fixup image size
262#	4. fixup ARM64 cpu boot with AArch32
263fixup_platform_configure()
264{
265	local count plat
266
267# <1> Get RKCHIP for searching trust/loader ini files
268	count=`grep -c '^CONFIG_ROCKCHIP_[R,P][X,V,K][0-9][0-9]' ${OUTDIR}/.config`
269	RKCHIP=`grep '^CONFIG_ROCKCHIP_[R,P][X,V,K][0-9][0-9]' ${OUTDIR}/.config`
270
271	if [ $count -eq 1 ]; then
272		RKCHIP=${RKCHIP%=*}
273		RKCHIP=${RKCHIP##*_}
274	elif [ $count -gt 1 ]; then
275		# Is RK3126 ?
276		plat=`grep '^CONFIG_ROCKCHIP_[R,P][X,V,K][0-9][0-9]' ${OUTDIR}/.config | sed -n "/CONFIG_ROCKCHIP_RK3126=y/p"`
277		if [ "$plat" = 'CONFIG_ROCKCHIP_RK3126=y' ]; then
278			RKCHIP=RK3126
279		fi
280		# Is RK3326 ?
281		plat=`grep '^CONFIG_ROCKCHIP_[R,P][X,V,K][0-9][0-9]' ${OUTDIR}/.config | sed -n "/CONFIG_ROCKCHIP_RK3326=y/p"`
282		if [ "$plat" = 'CONFIG_ROCKCHIP_RK3326=y' ]; then
283			RKCHIP=RK3326
284		fi
285	else
286		echo "Can't get Rockchip SoC definition in .config"
287		exit 1
288	fi
289
290# <2> Fixup rsa/sha pack mode for platforms
291	# RK3308/PX30/RK3326 use RSA-PKCS1 V2.1, it's pack magic is "3"
292	if [ $RKCHIP = "PX30" -o $RKCHIP = "RK3326" -o $RKCHIP = "RK3308" ]; then
293		PLATFORM_RSA="--rsa 3"
294	# RK3368 use rk big endian SHA256, it's pack magic is "2"
295	elif [ $RKCHIP = "RK3368" ]; then
296		PLATFORM_SHA="--sha 2"
297	# other platforms use default configure
298	fi
299
300# <3> Fixup images size pack for platforms
301	if [ $RKCHIP = "RK3308" ]; then
302		if grep -q '^CONFIG_ARM64_BOOT_AARCH32=y' ${OUTDIR}/.config ; then
303			PLATFORM_UBOOT_IMG_SIZE="--size 512 2"
304			PLATFORM_TRUST_IMG_SIZE="--size 512 2"
305		else
306			PLATFORM_UBOOT_IMG_SIZE="--size 1024 2"
307			PLATFORM_TRUST_IMG_SIZE="--size 1024 2"
308		fi
309	fi
310
311# <4> Fixup PLATFORM_AARCH32 for ARM64 cpu platforms
312	if [ $RKCHIP = "RK3308" ]; then
313		if grep -q '^CONFIG_ARM64_BOOT_AARCH32=y' ${OUTDIR}/.config ; then
314			PLATFORM_AARCH32="AARCH32"
315		fi
316	fi
317}
318
319pack_uboot_image()
320{
321	local UBOOT_LOAD_ADDR
322
323	UBOOT_LOAD_ADDR=`sed -n "/CONFIG_SYS_TEXT_BASE=/s/CONFIG_SYS_TEXT_BASE=//p" ${OUTDIR}/include/autoconf.mk|tr -d '\r'`
324	${RKTOOLS}/loaderimage --pack --uboot ${OUTDIR}/u-boot.bin uboot.img ${UBOOT_LOAD_ADDR} ${PLATFORM_UBOOT_IMG_SIZE}
325
326	# Delete u-boot.img and u-boot-dtb.img, which makes users not be confused with final uboot.img
327	if [ -f ${OUTDIR}/u-boot.img ]; then
328		rm ${OUTDIR}/u-boot.img
329	fi
330
331	if [ -f ${OUTDIR}/u-boot-dtb.img ]; then
332		rm ${OUTDIR}/u-boot-dtb.img
333	fi
334
335	echo "pack uboot okay! Input: ${OUTDIR}/u-boot.bin"
336}
337
338pack_loader_image()
339{
340	local mode=$1 files ini
341
342	if [ ! -f ${RKBIN}/RKBOOT/${RKCHIP}MINIALL.ini ]; then
343		echo "pack loader failed! Can't find: ${RKBIN}/RKBOOT/${RKCHIP}MINIALL.ini"
344		return
345	fi
346
347	cd ${RKBIN}
348
349	if [ "${mode}" = 'all' ]; then
350		files=`ls ${RKBIN}/RKBOOT/${RKCHIP}MINIALL*.ini`
351		for ini in $files
352		do
353			if [ -f "$ini" ]; then
354				${RKTOOLS}/boot_merger --replace tools/rk_tools/ ./ $ini
355				echo "pack loader okay! Input: $ini"
356			fi
357		done
358	else
359		${RKTOOLS}/boot_merger --replace tools/rk_tools/ ./ ${RKBIN}/RKBOOT/${RKCHIP}MINIALL.ini
360		echo "pack loader okay! Input: ${RKBIN}/RKBOOT/${RKCHIP}MINIALL.ini"
361	fi
362
363	cd - && mv ${RKBIN}/*_loader_*.bin ./
364}
365
366pack_trust_image()
367{
368	local TOS TOS_TA DARM_BASE TEE_LOAD_ADDR TEE_OFFSET=0x8400000
369
370	# ARM64 uses trust_merger
371	if grep -Eq ''^CONFIG_ARM64=y'|'^CONFIG_ARM64_BOOT_AARCH32=y'' ${OUTDIR}/.config ; then
372		if [ ! -f ${RKBIN}/RKTRUST/${RKCHIP}${PLATFORM_AARCH32}TRUST.ini ]; then
373			echo "pack trust failed! Can't find: ${RKBIN}/RKTRUST/${RKCHIP}${PLATFORM_AARCH32}TRUST.ini"
374			return
375		fi
376
377		cd ${RKBIN}
378		${RKTOOLS}/trust_merger ${PLATFORM_SHA} ${PLATFORM_RSA} ${PLATFORM_TRUST_IMG_SIZE} --replace tools/rk_tools/ ./ ${RKBIN}/RKTRUST/${RKCHIP}${PLATFORM_AARCH32}TRUST.ini
379
380		cd - && mv ${RKBIN}/trust.img ./trust.img
381		echo "pack trust okay! Input: ${RKBIN}/RKTRUST/${RKCHIP}${PLATFORM_AARCH32}TRUST.ini"
382	# ARM uses loaderimage
383	else
384		if [ ! -f ${RKBIN}/RKTRUST/${RKCHIP}TOS.ini ]; then
385			echo "pack trust failed! Can't find: ${RKBIN}/RKTRUST/${RKCHIP}TOS.ini"
386			return
387		fi
388
389		# OP-TEE is 132M(0x8400000) offset from DRAM base.
390		DARM_BASE=`sed -n "/CONFIG_SYS_SDRAM_BASE=/s/CONFIG_SYS_SDRAM_BASE=//p" ${OUTDIR}/include/autoconf.mk|tr -d '\r'`
391		TEE_LOAD_ADDR=$((DARM_BASE+TEE_OFFSET))
392
393		# Convert Dec to Hex
394		TEE_LOAD_ADDR=$(echo "obase=16;${TEE_LOAD_ADDR}"|bc)
395
396		# Parse orignal path
397		TOS=`sed -n "/TOS=/s/TOS=//p" ${RKBIN}/RKTRUST/${RKCHIP}TOS.ini|tr -d '\r'`
398		TOS_TA=`sed -n "/TOSTA=/s/TOSTA=//p" ${RKBIN}/RKTRUST/${RKCHIP}TOS.ini|tr -d '\r'`
399
400		# replace "./tools/rk_tools/" with "./" to compatible legacy ini content of rkdevelop branch
401		TOS=$(echo ${TOS} | sed "s/tools\/rk_tools\//\.\//g")
402		TOS_TA=$(echo ${TOS_TA} | sed "s/tools\/rk_tools\//\.\//g")
403
404		if [ $TOS_TA -a $TOS ]; then
405			${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS} ./trust.img ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE}
406			${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS_TA} ./trust_with_ta.img ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE}
407			echo "Both trust.img and trust_with_ta.img are ready"
408		elif [ $TOS ]; then
409			${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS} ./trust.img ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE}
410			echo "trust.img is ready"
411		elif [ $TOS_TA ]; then
412			${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS_TA} ./trust.img ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE}
413			echo "trust.img with ta is ready"
414		else
415			echo "Can't find any tee bin"
416			exit 1
417		fi
418
419		echo "pack trust okay! Input: ${RKBIN}/RKTRUST/${RKCHIP}TOS.ini"
420	fi
421}
422
423finish()
424{
425	echo
426	if [ "$BOARD" = '' ]; then
427		echo "Platform ${RKCHIP}${PLATFORM_AARCH32} is build OK, with exist .config"
428	else
429		echo "Platform ${RKCHIP}${PLATFORM_AARCH32} is build OK, with new .config(make ${BOARD}_defconfig)"
430	fi
431}
432
433prepare
434select_toolchain
435fixup_platform_configure
436sub_commands
437make CROSS_COMPILE=${TOOLCHAIN_GCC}  all --jobs=${JOB} ${OUTOPT}
438pack_uboot_image
439pack_loader_image
440pack_trust_image
441finish
442