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