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