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