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