xref: /rk3399_rockchip-uboot/make.sh (revision 30b04afc78b7b83a601dea5974bbefa8e9d731f9)
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`
8SUPPORT_LIST=`ls configs/*-[r,p][x,v,k][0-9][0-9]*_defconfig`
9
10########################################### User can modify #############################################
11# User's rkbin tool relative path
12RKBIN_TOOLS=../rkbin/tools
13
14# User's GCC toolchain and relative path
15OBJ_ARM32=arm-linux-gnueabihf-objdump
16OBJ_ARM64=aarch64-linux-gnu-objdump
17GCC_ARM32=arm-linux-gnueabihf-
18GCC_ARM64=aarch64-linux-gnu-
19TOOLCHAIN_ARM32=../prebuilts/gcc/linux-x86/arm/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabihf/bin
20TOOLCHAIN_ARM64=../prebuilts/gcc/linux-x86/aarch64/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin
21
22########################################### User not touch #############################################
23# Declare global rkbin RKTOOLS and rkbin repository path, updated in prepare()
24RKTOOLS=
25RKBIN=
26
27# Declare global toolchain path for CROSS_COMPILE, updated in select_toolchain()
28TOOLCHAIN_GCC=
29TOOLCHAIN_OBJDUMP=
30
31# Declare global default output dir and cmd, update in prepare()
32OUTDIR=
33OUTOPT=
34
35# Declare global plaform configure, updated in fixup_platform_configure()
36PLATFORM_RSA=
37PLATFORM_SHA=
38PLATFORM_UBOOT_IMG_SIZE=
39PLATFORM_TRUST_IMG_SIZE=
40PLATFORM_AARCH32=
41#########################################################################################################
42
43prepare()
44{
45	local absolute_path cmd count
46
47	# Assign output directory
48	cmd=${SUBCMD%=*}
49	if [ "${cmd}" = 'O' ]; then
50		OUTDIR=${SUBCMD#*=}
51		OUTOPT=O=${OUTDIR}
52	else
53		OUTDIR=.
54	fi
55
56	# Check invalid args and help
57	if [ "$BOARD" = '--help' -o "$BOARD" = '-help' -o "$BOARD" = 'help' -o "$BOARD" = '-h' -o "$BOARD" = '--h' ]; then
58		echo
59		echo "Usage: ./make.sh [board]"
60		echo "Example:"
61		echo "./make.sh		 ---- build with exist .config"
62		echo "./make.sh evb-rk3399     ---- build for evb-rk3399_defconfig"
63		echo "./make.sh firefly-rk3288 ---- build for firefly-rk3288_defconfig"
64		exit 1
65	elif [ $BOARD ] && [ ! -f configs/${BOARD}_defconfig ]; then
66		echo
67		echo "Can't find: configs/${BOARD}_defconfig"
68		echo
69		echo "******** Rockchip Support List *************"
70		echo "${SUPPORT_LIST}"
71		echo "********************************************"
72		echo
73		exit 1
74	fi
75
76	# Get RKCHIP from exist .config file
77	if [ "$RKCHIP" = '' ]; then
78		count=`grep -c '^CONFIG_ROCKCHIP_[R,P][X,V,K][0-9][0-9]' ${OUTDIR}/.config`
79		RKCHIP=`grep '^CONFIG_ROCKCHIP_[R,P][X,V,K][0-9][0-9]' ${OUTDIR}/.config`
80		if [ $count -gt 1 ]; then
81			echo "Find $count SoC in .config file:"
82			echo "$RKCHIP"
83			echo
84			echo "I'm confused, please compile with [board], like: ./make.sh [board]"
85			exit 1
86		else
87			RKCHIP=${RKCHIP%=*}
88			RKCHIP=${RKCHIP##*_}
89		fi
90	fi
91
92	# Initialize RKBIN and RKTOOLS
93	if [ -d ${RKBIN_TOOLS} ]; then
94		absolute_path=$(cd `dirname ${RKBIN_TOOLS}`; pwd)
95		RKBIN=${absolute_path}
96		RKTOOLS=${absolute_path}/tools
97	else
98		echo
99		echo "Can't find '../rkbin/' repository, please download it before pack image!"
100		echo "How to obtain? 3 ways:"
101		echo "	1. Login your Rockchip gerrit account: \"Projects\" -> \"List\" -> search \"rk/rkbin\" repository"
102		echo "	2. Github repository: https://github.com/rockchip-linux/rkbin"
103		echo "	3. Download full release SDK repository"
104		exit 1
105	fi
106}
107
108make_defconfig()
109{
110	if [ $BOARD ]; then
111		echo "make for ${BOARD}_defconfig by -j${JOB}"
112		make ${BOARD}_defconfig ${OUTOPT}
113	fi
114}
115
116select_toolchain()
117{
118	local absolute_path
119
120	if grep  -q '^CONFIG_ARM64=y' ${OUTDIR}/.config ; then
121		if [ -d ${TOOLCHAIN_ARM64} ]; then
122			absolute_path=$(cd `dirname ${TOOLCHAIN_ARM64}`; pwd)
123			TOOLCHAIN_GCC=${absolute_path}/bin/${GCC_ARM64}
124			TOOLCHAIN_OBJDUMP=${absolute_path}/bin/${OBJ_ARM64}
125		else
126			echo "Can't find toolchain: ${TOOLCHAIN_ARM64}"
127			exit 1
128		fi
129	else
130		if [ -d ${TOOLCHAIN_ARM32} ]; then
131			absolute_path=$(cd `dirname ${TOOLCHAIN_ARM32}`; pwd)
132			TOOLCHAIN_GCC=${absolute_path}/bin/${GCC_ARM32}
133			TOOLCHAIN_OBJDUMP=${absolute_path}/bin/${OBJ_ARM32}
134		else
135			echo "Can't find toolchain: ${TOOLCHAIN_ARM32}"
136			exit 1
137		fi
138	fi
139
140	echo "toolchain: ${TOOLCHAIN_GCC}"
141}
142
143# Support subcmd:
144#	./make.sh evb-rk3288 elf	--- dump elf file with -D(default)
145#	./make.sh evb-rk3288 elf-S	--- dump elf file with -S
146#	./make.sh evb-rk3288 trust	--- pack trust.img without compile u-boot
147#	./make.sh evb-rk3288 loader	--- pack loader bin without compile u-boot
148#	./make.sh evb-rk3288 uboot	--- pack uboot.img without compile u-boot
149sub_commands()
150{
151	local elf=${SUBCMD%-*} opt=${SUBCMD#*-}
152
153	if [ "$elf" = 'elf' ]; then
154		if [ ! -f ${OUTDIR}/u-boot ]; then
155			echo "Can't find elf file: ${OUTDIR}/u-boot"
156			exit 1
157		else
158			# default 'elf' without option, use '-D'
159			if [ "${elf}" = 'elf' -a "${opt}" = 'elf' ]; then
160				opt=D
161			fi
162
163			${TOOLCHAIN_OBJDUMP} -${opt} ${OUTDIR}/u-boot | less
164			exit 0
165		fi
166	elif [ "$SUBCMD" = 'trust' ]; then
167		pack_trust_image
168		exit 0
169	elif [ "$SUBCMD" = 'loader' ]; then
170		pack_loader_image
171		exit 0
172	elif [ "$SUBCMD" = 'uboot' ]; then
173		pack_uboot_image
174		exit 0
175	fi
176}
177
178# Support platform special configure
179#	1. fixup chip name;
180#	2. fixup pack mode;
181#	3. fixup image size
182#	4. fixup ARM64 cpu boot with AArch32
183fixup_platform_configure()
184{
185# <1> Fixup chip name for searching trust/loader ini files
186	if [ "$RKCHIP" = 'RK3228' -o "$RKCHIP" = 'RK3229' ]; then
187		RKCHIP=RK322X
188	fi
189
190# <2> Fixup rsa/sha pack mode for platforms
191	# RK3308/PX30/RK3326 use RSA-PKCS1 V2.1, it's pack magic is "3"
192	if [ $RKCHIP = "PX30" -o $RKCHIP = "RK3326" -o $RKCHIP = "RK3308" ]; then
193		PLATFORM_RSA="--rsa 3"
194	# RK3368 use rk big endian SHA256, it's pack magic is "2"
195	elif [ $RKCHIP = "RK3368" ]; then
196		PLATFORM_SHA="--sha 2"
197	# other platforms use default configure
198	fi
199
200# <3> Fixup images size pack for platforms
201	if [ $RKCHIP = "RK3308" ]; then
202		if grep -q '^CONFIG_ARM64_BOOT_AARCH32=y' ${OUTDIR}/.config ; then
203			PLATFORM_UBOOT_IMG_SIZE="--size 512 2"
204			PLATFORM_TRUST_IMG_SIZE="--size 512 2"
205		else
206			PLATFORM_UBOOT_IMG_SIZE="--size 1024 2"
207			PLATFORM_TRUST_IMG_SIZE="--size 1024 2"
208		fi
209	fi
210
211# <4> Fixup PLATFORM_AARCH32 for ARM64 cpu platforms
212	if [ $RKCHIP = "RK3308" ]; then
213		if grep -q '^CONFIG_ARM64_BOOT_AARCH32=y' ${OUTDIR}/.config ; then
214			PLATFORM_AARCH32="AARCH32"
215		fi
216	fi
217}
218
219pack_uboot_image()
220{
221	local UBOOT_LOAD_ADDR
222
223	UBOOT_LOAD_ADDR=`sed -n "/CONFIG_SYS_TEXT_BASE=/s/CONFIG_SYS_TEXT_BASE=//p" ${OUTDIR}/include/autoconf.mk|tr -d '\r'`
224	${RKTOOLS}/loaderimage --pack --uboot ${OUTDIR}/u-boot.bin uboot.img ${UBOOT_LOAD_ADDR} ${PLATFORM_UBOOT_IMG_SIZE}
225
226	# Delete u-boot.img and u-boot-dtb.img, which makes users not be confused with final uboot.img
227	if [ -f ${OUTDIR}/u-boot.img ]; then
228		rm ${OUTDIR}/u-boot.img
229	fi
230
231	if [ -f ${OUTDIR}/u-boot-dtb.img ]; then
232		rm ${OUTDIR}/u-boot-dtb.img
233	fi
234
235	echo "pack uboot okay! Input: ${OUTDIR}/u-boot.bin"
236}
237
238pack_loader_image()
239{
240	if [ ! -f ${RKBIN}/RKBOOT/${RKCHIP}MINIALL.ini ]; then
241		echo "pack loader failed! Can't find: ${RKBIN}/RKBOOT/${RKCHIP}MINIALL.ini"
242		return
243	fi
244
245	cd ${RKBIN}
246	${RKTOOLS}/boot_merger --replace tools/rk_tools/ ./ ${RKBIN}/RKBOOT/${RKCHIP}MINIALL.ini
247	cd - && mv ${RKBIN}/*_loader_*.bin ./
248	echo "pack loader okay! Input: ${RKBIN}/RKBOOT/${RKCHIP}MINIALL.ini"
249}
250
251pack_trust_image()
252{
253	local TOS TOS_TA DARM_BASE TEE_LOAD_ADDR TEE_OFFSET=0x8400000
254
255	# ARM64 uses trust_merger
256	if grep -Eq ''^CONFIG_ARM64=y'|'^CONFIG_ARM64_BOOT_AARCH32=y'' ${OUTDIR}/.config ; then
257		if [ ! -f ${RKBIN}/RKTRUST/${RKCHIP}${PLATFORM_AARCH32}TRUST.ini ]; then
258			echo "pack trust failed! Can't find: ${RKBIN}/RKTRUST/${RKCHIP}${PLATFORM_AARCH32}TRUST.ini"
259			return
260		fi
261
262		cd ${RKBIN}
263		${RKTOOLS}/trust_merger ${PLATFORM_SHA} ${PLATFORM_RSA} ${PLATFORM_TRUST_IMG_SIZE} --replace tools/rk_tools/ ./ ${RKBIN}/RKTRUST/${RKCHIP}${PLATFORM_AARCH32}TRUST.ini
264
265		cd - && mv ${RKBIN}/trust.img ./trust.img
266		echo "pack trust okay! Input: ${RKBIN}/RKTRUST/${RKCHIP}${PLATFORM_AARCH32}TRUST.ini"
267	# ARM uses loaderimage
268	else
269		if [ ! -f ${RKBIN}/RKTRUST/${RKCHIP}TOS.ini ]; then
270			echo "pack trust failed! Can't find: ${RKBIN}/RKTRUST/${RKCHIP}TOS.ini"
271			return
272		fi
273
274		# OP-TEE is 132M(0x8400000) offset from DRAM base.
275		DARM_BASE=`sed -n "/CONFIG_SYS_SDRAM_BASE=/s/CONFIG_SYS_SDRAM_BASE=//p" ${OUTDIR}/include/autoconf.mk|tr -d '\r'`
276		TEE_LOAD_ADDR=$((DARM_BASE+TEE_OFFSET))
277
278		# Convert Dec to Hex
279		TEE_LOAD_ADDR=$(echo "obase=16;${TEE_LOAD_ADDR}"|bc)
280
281		# Parse orignal path
282		TOS=`sed -n "/TOS=/s/TOS=//p" ${RKBIN}/RKTRUST/${RKCHIP}TOS.ini|tr -d '\r'`
283		TOS_TA=`sed -n "/TOSTA=/s/TOSTA=//p" ${RKBIN}/RKTRUST/${RKCHIP}TOS.ini|tr -d '\r'`
284
285		# replace "./tools/rk_tools/" with "./" to compatible legacy ini content of rkdevelop branch
286		TOS=$(echo ${TOS} | sed "s/tools\/rk_tools\//\.\//g")
287		TOS_TA=$(echo ${TOS_TA} | sed "s/tools\/rk_tools\//\.\//g")
288
289		if [ $TOS_TA -a $TOS ]; then
290			${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS} ./trust.img ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE}
291			${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS_TA} ./trust_with_ta.img ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE}
292			echo "Both trust.img and trust_with_ta.img are ready"
293		elif [ $TOS ]; then
294			${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS} ./trust.img ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE}
295			echo "trust.img is ready"
296		elif [ $TOS_TA ]; then
297			${RKTOOLS}/loaderimage --pack --trustos ${RKBIN}/${TOS_TA} ./trust.img ${TEE_LOAD_ADDR} ${PLATFORM_TRUST_IMG_SIZE}
298			echo "trust.img with ta is ready"
299		else
300			echo "Can't find any tee bin"
301			exit 1
302		fi
303
304		echo "pack trust okay! Input: ${RKBIN}/RKTRUST/${RKCHIP}TOS.ini"
305	fi
306}
307
308finish()
309{
310	echo
311	if [ "$BOARD" = '' ]; then
312		echo "Platform ${RKCHIP}${PLATFORM_AARCH32} is build OK, with exist .config"
313	else
314		echo "Platform ${RKCHIP}${PLATFORM_AARCH32} is build OK, with new .config(make ${BOARD}_defconfig)"
315	fi
316}
317
318prepare
319make_defconfig
320select_toolchain
321fixup_platform_configure
322sub_commands
323make CROSS_COMPILE=${TOOLCHAIN_GCC}  all --jobs=${JOB} ${OUTOPT}
324pack_uboot_image
325pack_loader_image
326pack_trust_image
327finish
328