summaryrefslogtreecommitdiff
path: root/build-rpi4.sh
blob: a453b292091484e844217c3182594970fb3f63fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/bin/bash

COLOR_RED_BG=$(tput setab 1)
COLOR_RED_BOLD=$(tput bold)$(tput setaf 1)
COLOR_RESET=$(tput sgr0)

MOD_DIR="usr/tmp_mod"
MOD_IMG="usr/modules.img"
MOD_SIZE=20
NCPUS=`cat /proc/cpuinfo | grep processor | wc -l`
NCPUS=$(($NCPUS * 2))

BUILD_ROOT=$PWD
KERNEL_PATH=`basename ${BUILD_ROOT}`

if [ $# == 0 ]; then
	echo "Usage : $0 <architecture> [rt] [bootimg]"
	echo "	architecture : arm or arm64"
	echo "	e.g) $0 arm"
	echo "	e.g) $0 arm64 rt"
	echo "	e.g) $0 arm64 bootimg"
	echo "	e.g) $0 arm rt bootimg"
	exit
fi

# Check this system has ccache
check_ccache()
{
	type ccache
	if [ "$?" -eq "0" ]; then
		CCACHE=ccache
	fi
}

check_sudo()
{
	[ -e /usr/bin/make_ext4fs ] && USE_MAKE_EXT4FS=1
	if [ "$USE_MAKE_EXT4FS" != "1" ] || [ "$IS_BOOTIMG" = "1" ]; then
		sudo ls > /dev/null
	fi

}

function mk_bootimg()
{
	TMP_PATH=./tmp_bootimg
	TMP_UBOOT_PATH=./tmp_uboot
	BOOT_PATH="rpi4/boot"
	USER_ID=`id -u`
	GROUP_ID=`id -g`

	rm -f boot.img
	rm -rf ${TMP_PATH}
	mkdir ${TMP_PATH}

	# Create boot.img
	mkfs.vfat -F 16 -C -n BOOT boot.img 65536
	sudo mount -o loop,uid=$USER_ID,gid=$GROUP_ID,showexec boot.img ${TMP_PATH}

	cp -a $BOOT_PATH/LICENCE.broadcom ${TMP_PATH}
	cp -a $BOOT_PATH/start*.elf ${TMP_PATH}
	cp -a $BOOT_PATH/fixup*.dat ${TMP_PATH}
	if [ "$ARM_ARCH" = "arm64" ]; then
		echo "Create 64bit boot image"
		cp -a $BOOT_PATH/config_64bit.txt ${TMP_PATH}/config.txt
		cp -a arch/arm64/boot/Image ${TMP_PATH}
		cp -a arch/arm64/boot/dts/broadcom/bcm*.dtb ${TMP_PATH}
	else
		echo "Create 32bit boot image"
		cp -a $BOOT_PATH/config.txt ${TMP_PATH}
		cp -a arch/arm/boot/zImage ${TMP_PATH}
		cp -a arch/arm/boot/dts/broadcom/bcm*.dtb ${TMP_PATH}
	fi
	mkdir -p ${TMP_PATH}/overlays
	cp -a arch/arm/boot/dts/overlays/*.dtbo ${TMP_PATH}/overlays

	# install u-boot files extracted from u-boot-rpi4 rpm package in download.tizen.org.
	rm -rf ${TMP_UBOOT_PATH}
	mkdir -p ${TMP_UBOOT_PATH}
	pushd ${TMP_UBOOT_PATH}
	if [ "$ARM_ARCH" = "arm64" ]; then
		REPO_URL=http://download.tizen.org/snapshots/TIZEN/Tizen/Tizen-Unified/latest/repos/standard/packages/aarch64/
	else
		REPO_URL=http://download.tizen.org/snapshots/TIZEN/Tizen/Tizen-Unified/latest/repos/standard/packages/armv7l/
	fi
	rm -f index.html*
	wget ${REPO_URL}
	UBOOT=`awk -F\" '{ print $2 }' index.html | grep u-boot-rpi4`
	wget ${REPO_URL}${UBOOT}
	unrpm ${UBOOT}
	popd

	cp -a ${TMP_UBOOT_PATH}/boot/* ${TMP_PATH}
	sync
	sudo umount ${TMP_PATH}

	rm -rf ${TMP_UBOOT_PATH}
	rm -rf ${TMP_PATH}
}

function mk_tizen_modules()
{
	if [ -e ${BUILD_ROOT}/../linux-tizen-modules-source ]; then
		pushd ${BUILD_ROOT}/../linux-tizen-modules-source

		ln -s ${PWD}/include kernel

		export BUILD_logger=m
		export LOGGER_MAIN_BUFFER_SIZE=256
		export LOGGER_EVENTS_BUFFER_SIZE=256
		export LOGGER_RADIO_BUFFER_SIZE=256
		export LOGGER_SYSTEM_BUFFER_SIZE=256

		export BUILD_proc_tsm=m
		export BUILD_kdbus=m
		export BUILD_zlogger=m

		ARCH="${ARM_ARCH}" CROSS_COMPILE="${CROSS_COMPILER}" make -C kernel KERNELDIR=${BUILD_ROOT}

		rm -f kernel/include

		ARCH="${ARM_ARCH}" CROSS_COMPILE="${CROSS_COMPILER}" make -C kernel modules_install KERNELDIR=${BUILD_ROOT} INSTALL_MOD_PATH=../${KERNEL_PATH}/${MOD_DIR} INSTALL_MOD_STRIP=1 INSTALL_MOD_DIR=extra

		popd

		if [ -e ${BUILD_ROOT}/../linux-tizen-modules ]; then
			mkdir -p ${MOD_DIR}/lib/modules/modprobe.d
			mkdir -p ${MOD_DIR}/lib/modules/modules-load.d
			cp ${BUILD_ROOT}/../linux-tizen-modules/conf/tizen-modprobe-rpi4.conf ${MOD_DIR}/lib/modules/modprobe.d
			cp ${BUILD_ROOT}/../linux-tizen-modules/conf/tizen-modules-rpi4.conf ${MOD_DIR}/lib/modules/modules-load.d
		fi
	else
		echo "linux-tizen-modules-source/linux-tizen-modules are not cloned in parent path, so skip building linux-tizen-modules."
		echo "To build linux-tizen-modules, please use below command:"
		echo "   git clone git://git.tizen.org/platform/kernel/linux-tizen-modules-source -b tizen ../linux-tizen-modules-source"
		echo "   git clone git://git.tizen.org/platform/kernel/linux-tizen-modules -b tizen ../linux-tizen-modules"
	fi
}

function mk_modules() {
	make ARCH=${ARM_ARCH} CROSS_COMPILE="$CROSS_COMPILER" modules_prepare
	make ARCH=${ARM_ARCH} CROSS_COMPILE="$CROSS_COMPILER" modules -j ${NCPUS}

	if [ "$?" != "0" ]; then
		echo "Failed to make modules"
		exit 1
	fi

	[ -d ${MOD_DIR} ] || mkdir ${MOD_DIR}

	make ARCH=${ARM_ARCH} CROSS_COMPILE="$CROSS_COMPILER" modules_install INSTALL_MOD_PATH=${MOD_DIR} INSTALL_MOD_STRIP=1

	# build and install linux-tizen-modules if cloned in parent path
	mk_tizen_modules

	if [ "$USE_MAKE_EXT4FS" == "1" ]; then
		/usr/bin/make_ext4fs -b 4096 -L modules -l ${MOD_SIZE}M $MOD_IMG ${MOD_DIR}/lib/modules/
	else
		dd if=/dev/zero of=${MOD_IMG} bs=1M count=${MOD_SIZE}
		mkfs.ext4 -F -b 4096 -L modules ${MOD_IMG}
		[ -d ${MOD_DIR}/mnt ] || mkdir ${MOD_DIR}/mnt
		sudo mount -o loop ${MOD_IMG} ${MOD_DIR}/mnt
		sudo cp -rf ${MOD_DIR}/lib/modules/* ${MOD_DIR}/mnt
		sync
		sudo umount ${MOD_DIR}/mnt
	fi
	rm -rf ${MOD_DIR}
	ls -al ${MOD_IMG}
}

check_ccache

function cleanup_localversion()
{
	rm -f localversion-rt
}

function create_tar()
{

	if [ ! -d output ] ; then
		mkdir ./output
	fi

	rm -f ./output/*

	cp $BOOTFILES ./output
	cp usr/modules.img ./output

	FILES=`ls output`
	DATE=`date +%Y%m%d%H%M`
	if [ $ARM_ARCH = "arm" ]; then
		tar cvf tizen-local-${DATE}-boot-armv7l-rpi4.tar -C output $FILES
		mv tizen-local-${DATE}-boot-armv7l-rpi4.tar ./output/
	elif [ $ARM_ARCH = "arm64" ]; then
		tar cvf tizen-local-${DATE}-boot-arm64-rpi4.tar -C output $FILES
		mv tizen-local-${DATE}-boot-arm64-rpi4.tar ./output/
	else
		echo "There is no proper architecture agrument!"
		exit 1;
	fi
}

DEFCONFIG=tizen_bcm2711_defconfig
touch .kernel_buildenv

IS_RT=0
if [ "$2" = "rt" ] || [ "$3" = "rt" ]; then
	IS_RT=1
fi

IS_BOOTIMG=0
if [ "$2" = "bootimg" ] || [ "$3" = "bootimg" ]; then
	IS_BOOTIMG=1
fi

if [ "$IS_RT" = "1" ]; then
	echo "Build RT-Kernel"
	BUILD_VERSION=`cat .kernel_buildenv`
	if [ "$BUILD_VERSION" != "RT" ]; then
		echo "Previous .config is for Normal Kernel, so remove"
		rm -f .config
	fi
	DEFCONFIG=tizen_bcm2711_rt_defconfig
	cp _localversion-rt localversion-rt
	trap cleanup_localversion EXIT
	echo "RT" > ./.kernel_buildenv
else
	echo "Build Normal-Kernel"
	BUILD_VERSION=`cat .kernel_buildenv`
	if [ "$BUILD_VERSION" == "RT" ]; then
		echo "Previous .config is for RT Kernel, so remove"
		rm -f .config
	fi
	echo "NORMAL" > ./.kernel_buildenv
fi

check_sudo

if [ $1 = "arm" ]; then
	ARM_ARCH="arm"
	CROSS_COMPILER="${CCACHE} arm-linux-gnueabi-"
	echo "ARM"
elif [ $1 = "arm64" ]; then
	ARM_ARCH="arm64"
	CROSS_COMPILER="${CCACHE} aarch64-linux-gnu-"
	echo "ARM64"
else
	echo "There is no arcitecture type..."
	exit 1;
fi

if ! [ -e .config ] ; then
	make ARCH="${ARM_ARCH}" CROSS_COMPILE="${CROSS_COMPILER}" ${DEFCONFIG}
fi

make ARCH="${ARM_ARCH}" CROSS_COMPILE="${CROSS_COMPILER}" -j ${NCPUS} 2>&1 | \
sed -e "/error:.*/{s/^.*error:/$(echo -e "${COLOR_RED_BG}&${COLOR_RESET}")/g;q1}" \
	-e "/Error.*/{s/^.*Error:/$(echo -e "${COLOR_RED_BG}&${COLOR_RESET}")/g;q1}" \
	-e "/ERROR.*/{s/^.*ERROR/$(echo -e "${COLOR_RED_BG}&${COLOR_RESET}")/g;q1}"
if [ $? -ne 0 ]; then
	echo -e "${COLOR_RED_BOLD}Failed to build${COLOR_RESET}"
	exit 1
fi

if [ "$IS_BOOTIMG" = "1" ]; then
	mk_bootimg
	BOOTFILES="boot.img"
else
	if [ $ARM_ARCH = "arm" ]; then
		BOOTFILES="./arch/arm/boot/zImage  ./arch/arm/boot/dts/broadcom/bcm2711-rpi-4-b.dtb"
	else
		BOOTFILES="./arch/arm64/boot/Image ./arch/arm64/boot/dts/broadcom/bcm2711-rpi-4-b.dtb"
	fi
fi

# Make module.img file
mk_modules

# Create Tar file
create_tar

cleanup_localversion