xref: /rk3399_rockchip-uboot/make.sh (revision d5f538dc02e53c7267fcd4a914104071fca889b5)
1#!/bin/sh
2set -e
3BOARD=$1
4SUBCMD=$2
5RKCHIP=${BOARD##*-}
6RKCHIP=$(echo ${RKCHIP} | tr '[a-z]' '[A-Z]')
7JOB=`sed -n "N;/processor/p" /proc/cpuinfo|wc -l`
8
9# Declare global default output dir and cmd, update in prepare()
10OUTDIR=.
11OUTOPT=
12
13# Declare global rkbin tools and rkbin Responsity path, updated in prepare()
14TOOLCHAIN_RKBIN=./
15RKBIN=./
16# RKTOOL path
17RKBIN_TOOLS=../rkbin/tools
18
19# Declare global toolchain path for CROSS_COMPILE, updated in select_toolchain()
20TOOLCHAIN_GCC=./
21TOOLCHAIN_OBJDUMP=./
22# GCC toolchain
23GCC_ARM32=arm-linux-gnueabihf-
24GCC_ARM64=aarch64-linux-gnu-
25TOOLCHAIN_ARM32=../prebuilts/gcc/linux-x86/arm/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabihf/bin
26TOOLCHAIN_ARM64=../prebuilts/gcc/linux-x86/aarch64/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin
27# OBJDMP
28OBJ_ARM32=arm-linux-gnueabihf-objdump
29OBJ_ARM64=aarch64-linux-gnu-objdump
30
31prepare()
32{
33	local absolute_path cmd
34
35	# Check invaid args and help
36	if [ "$BOARD" = '--help' -o "$BOARD" = '-h' -o "$BOARD" = '--h' -o "$BOARD" = '' ]; then
37		echo
38		echo "Usage: ./make.sh [board]"
39		echo "Example:"
40		echo "./make.sh evb-rk3399     ---- build for evb-rk3399_defconfig"
41		echo "./make.sh firefly-rk3288 ---- build for firefly-rk3288_defconfig"
42		exit 1
43	elif [ ! -f configs/${BOARD}_defconfig ]; then
44		echo "Can't find: configs/${BOARD}_defconfig"
45		exit 1
46	fi
47
48	# Initialize RKBIN and TOOLCHAIN_RKBIN
49	if [ -d ${RKBIN_TOOLS} ]; then
50		absolute_path=$(cd `dirname ${RKBIN_TOOLS}`; pwd)
51		RKBIN=${absolute_path}
52		TOOLCHAIN_RKBIN=${absolute_path}/tools
53	else
54		echo
55		echo "Can't find '../rkbin/' Responsity, please download it before pack image!"
56		echo "How to obtain? 3 ways:"
57		echo "	1. Login your Rockchip gerrit account: \"Projects\" -> \"List\" -> search \"rk/rkbin\" Responsity"
58		echo "	2. Github Responsity: https://github.com/rockchip-linux/rkbin"
59		echo "	3. Download full release SDK Responsity"
60		exit 1
61	fi
62
63	# Assign output directory
64	cmd=${SUBCMD%=*}
65	if [ "${cmd}" = 'O' ]; then
66		OUTDIR=${SUBCMD#*=}
67		OUTOPT=O=${OUTDIR}
68	fi
69}
70
71select_toolchain()
72{
73	local absolute_path
74
75	if grep  -q '^CONFIG_ARM64=y' ${OUTDIR}/.config ; then
76		if [ -d ${TOOLCHAIN_ARM64} ]; then
77			absolute_path=$(cd `dirname ${TOOLCHAIN_ARM64}`; pwd)
78			TOOLCHAIN_GCC=${absolute_path}/bin/${GCC_ARM64}
79			TOOLCHAIN_OBJDUMP=${absolute_path}/bin/${OBJ_ARM64}
80		else
81			echo "Can't find toolchain: ${TOOLCHAIN_ARM64}"
82			exit 1
83		fi
84	else
85		if [ -d ${TOOLCHAIN_ARM32} ]; then
86			absolute_path=$(cd `dirname ${TOOLCHAIN_ARM32}`; pwd)
87			TOOLCHAIN_GCC=${absolute_path}/bin/${GCC_ARM32}
88			TOOLCHAIN_OBJDUMP=${absolute_path}/bin/${OBJ_ARM32}
89		else
90			echo "Can't find toolchain: ${TOOLCHAIN_ARM32}"
91			exit 1
92		fi
93	fi
94
95	echo "toolchain: ${TOOLCHAIN_GCC}"
96}
97
98sub_commands()
99{
100	local elf=${SUBCMD%-*} opt=${SUBCMD#*-}
101
102	if [ "$elf" = 'elf' ]; then
103		if [ ! -f ${OUTDIR}/u-boot ]; then
104			echo "Can't find elf file: ${OUTDIR}/u-boot"
105			exit 1
106		else
107			# default 'elf' without option, use '-D'
108			if [ "${elf}" = 'elf' -a "${opt}" = 'elf' ]; then
109				opt=D
110			fi
111
112			${TOOLCHAIN_OBJDUMP} -${opt} ${OUTDIR}/u-boot | less
113			exit 0
114		fi
115	elif [ "$SUBCMD" = 'trust' ]; then
116		pack_trust_image
117		exit 0
118	elif [ "$SUBCMD" = 'loader' ]; then
119		pack_loader_image
120		exit 0
121	fi
122}
123
124fixup_chip_name()
125{
126	if [ "$RKCHIP" = 'RK3228' -o "$RKCHIP" = 'RK3229' ]; then
127		RKCHIP=RK322X
128	fi
129}
130
131pack_uboot_image()
132{
133	local UBOOT_LOAD_ADDR
134
135	UBOOT_LOAD_ADDR=`sed -n "/CONFIG_SYS_TEXT_BASE=/s/CONFIG_SYS_TEXT_BASE=//p" ${OUTDIR}/include/autoconf.mk|tr -d '\r'`
136	${TOOLCHAIN_RKBIN}/loaderimage --pack --uboot ${OUTDIR}/u-boot.bin uboot.img ${UBOOT_LOAD_ADDR}
137	rm ${OUTDIR}/u-boot.img ${OUTDIR}/u-boot-dtb.img
138	echo "pack uboot okay! Input: ${OUTDIR}/u-boot.bin"
139}
140
141pack_loader_image()
142{
143	if [ ! -f ${RKBIN}/RKBOOT/${RKCHIP}MINIALL.ini ]; then
144		echo "pack loader failed! Can't find: ${RKBIN}/RKBOOT/${RKCHIP}MINIALL.ini"
145		return
146	fi
147
148	cd ${RKBIN}
149	${TOOLCHAIN_RKBIN}/boot_merger --replace tools/rk_tools/ ./ ${RKBIN}/RKBOOT/${RKCHIP}MINIALL.ini
150	cd -
151	mv ${RKBIN}/*_loader_*.bin ./
152	echo "pack loader okay! Input: ${RKBIN}/RKBOOT/${RKCHIP}MINIALL.ini"
153}
154
155pack_trust_image()
156{
157	local TOS TOS_TA DARM_BASE TEE_LOAD_ADDR TEE_OFFSET=0x8400000
158
159	# ARM64 uses trust_merger
160	if grep  -q '^CONFIG_ARM64=y' ${OUTDIR}/.config ; then
161		if [ ! -f ${RKBIN}/RKTRUST/${RKCHIP}TRUST.ini ]; then
162			echo "pack trust failed! Can't find: ${RKBIN}/RKTRUST/${RKCHIP}TRUST.ini"
163			return
164		fi
165
166		cd ${RKBIN}
167		${TOOLCHAIN_RKBIN}/trust_merger --replace tools/rk_tools/ ./ ${RKBIN}/RKTRUST/${RKCHIP}TRUST.ini
168		cd -
169		mv ${RKBIN}/trust.img ./trust.img
170		echo "pack trust okay! Input: ${RKBIN}/RKTRUST/${RKCHIP}TRUST.ini"
171	# ARM uses loaderimage
172	else
173		if [ ! -f ${RKBIN}/RKTRUST/${RKCHIP}TOS.ini ]; then
174			echo "pack trust failed! Can't find: ${RKBIN}/RKTRUST/${RKCHIP}TOS.ini"
175			return
176		fi
177
178		# OP-TEE is 132M(0x8400000) offset from DRAM base.
179		DARM_BASE=`sed -n "/CONFIG_SYS_SDRAM_BASE=/s/CONFIG_SYS_SDRAM_BASE=//p" ${OUTDIR}/include/autoconf.mk|tr -d '\r'`
180		TEE_LOAD_ADDR=$((DARM_BASE+TEE_OFFSET))
181
182		# Convert Dec to Hex
183		TEE_LOAD_ADDR=$(echo "obase=16;${TEE_LOAD_ADDR}"|bc)
184
185		# Parse orignal path
186		TOS=`sed -n "/TOS=/s/TOS=//p" ${RKBIN}/RKTRUST/${RKCHIP}TOS.ini|tr -d '\r'`
187		TOS_TA=`sed -n "/TOSTA=/s/TOSTA=//p" ${RKBIN}/RKTRUST/${RKCHIP}TOS.ini|tr -d '\r'`
188
189		# replace "./tools/rk_tools/" with "./" to compatible legacy ini content of rkdevelop branch
190		TOS=$(echo ${TOS} | sed "s/tools\/rk_tools\//\.\//g")
191		TOS_TA=$(echo ${TOS_TA} | sed "s/tools\/rk_tools\//\.\//g")
192
193		if [ $TOS_TA -a $TOS ]; then
194			${TOOLCHAIN_RKBIN}/loaderimage --pack --trustos ${RKBIN}/${TOS} ./trust.img ${TEE_LOAD_ADDR}
195			${TOOLCHAIN_RKBIN}/loaderimage --pack --trustos ${RKBIN}/${TOS_TA} ./trust_with_ta.img ${TEE_LOAD_ADDR}
196			echo "Both trust.img and trust_with_ta.img are ready"
197		elif [ $TOS ]; then
198			${TOOLCHAIN_RKBIN}/loaderimage --pack --trustos ${RKBIN}/${TOS} ./trust.img ${TEE_LOAD_ADDR}
199			echo "trust.img is ready"
200		elif [ $TOS_TA ]; then
201			${TOOLCHAIN_RKBIN}/loaderimage --pack --trustos ${RKBIN}/${TOS_TA} ./trust.img ${TEE_LOAD_ADDR}
202			echo "trust.img with ta is ready"
203		else
204			echo "Can't find any tee bin"
205			exit 1
206		fi
207
208		echo "pack trust okay! Input: ${RKBIN}/RKTRUST/${RKCHIP}TOS.ini"
209	fi
210}
211
212prepare
213echo "make for ${BOARD}_defconfig by -j${JOB}"
214make ${BOARD}_defconfig ${OUTOPT}
215select_toolchain
216fixup_chip_name
217sub_commands
218make CROSS_COMPILE=${TOOLCHAIN_GCC}  all --jobs=${JOB} ${OUTOPT}
219pack_uboot_image
220pack_loader_image
221pack_trust_image
222