aboutsummaryrefslogtreecommitdiff
path: root/lt-qcom-linux-integration/builders.sh
blob: 53a69883783d899623ae345a3e2283fffabecfc9 (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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#!/bin/bash

# Init script to use ramdisk used in bootrr testing
INITRAMFS_RAMDISK=$(cat <<EOF
#!/bin/sh

HOME=/root
PATH=/sbin:/bin:/usr/sbin:/usr/bin
PS1="linaro-test [rc=\$(echo \\\$?)]# "
export HOME PS1 PATH

do_mount_fs() {
	grep -q "\$1" /proc/filesystems || return
	test -d "\$2" || mkdir -p "\$2"
	mount -t "\$1" "\$1" "\$2"
}

do_mknod() {
	test -e "\$1" || mknod "\$1" "\$2" "\$3" "\$4"
}

mkdir -p /proc
mount -t proc proc /proc

do_mount_fs sysfs /sys
do_mount_fs debugfs /sys/kernel/debug
do_mount_fs devtmpfs /dev
do_mount_fs devpts /dev/pts
do_mount_fs tmpfs /dev/shm

mkdir -p /run
mkdir -p /var/run

/sbin/udevd --daemon
/bin/udevadm trigger

do_mknod /dev/console c 5 1
do_mknod /dev/null c 1 3
do_mknod /dev/zero c 1 5

echo -n 'BOOT TIME: '
cat /proc/uptime

if \$(grep -q bootrr-auto /proc/cmdline); then
	for TEST in \$(tr "\0" "\n" < /proc/device-tree/compatible); do
		if [ -x "/usr/bin/\${TEST}" ]; then
			/usr/bin/\${TEST}
		fi
	done

	echo ~~~~~~~~~~~~~~~~~~~~~
fi

exec sh </dev/console >/dev/console 2>/dev/console
EOF
)

# Init script to mount rootfs mainly used in functional testing
INITRAMFS_ROOTFS=$(cat <<EOF
#!/bin/sh

HOME=/root
PATH=/sbin:/bin:/usr/sbin:/usr/bin
export HOME PATH

do_mount_fs() {
	grep -qa "\$1" /proc/filesystems || return
	test -d "\$2" || mkdir -p "\$2"
	mount -t "\$1" "\$1" "\$2"
}

do_mknod() {
	test -e "\$1" || mknod "\$1" "\$2" "\$3" "\$4"
}

rescue_shell() {
	echo "Failed to mount rootfs (__ROOTFS_PARTITION__), executing rescue shell..."
	export PS1="linaro-test [rc=\$(echo \\\$?)]# "
	exec sh </dev/console >/dev/console 2>/dev/console
}

mkdir -p /proc
mount -t proc proc /proc

do_mount_fs sysfs /sys
do_mount_fs devtmpfs /dev

mkdir -p /run
mkdir -p /var/run
/sbin/udevd --daemon
/bin/udevadm trigger

do_mknod /dev/console c 5 1
do_mknod /dev/null c 1 3
do_mknod /dev/zero c 1 5

if [ ! -b "__ROOTFS_PARTITION__" ]; then
	echo "Waiting for root device __ROOTFS_PARTITION__ ..."
	while [ ! -b "__ROOTFS_PARTITION__" ]; do
		sleep 1s
	done;
fi

mkdir -p /rootfs
mount -o ro __ROOTFS_PARTITION__ /rootfs || rescue_shell

umount /proc
umount /sys
umount /dev

echo "All done. Switching to real root."
exec switch_root /rootfs /sbin/init
EOF
)

set -x

wget_error() {
	wget -c $1 -P out/
	retcode=$?
	if [ $retcode -ne 0 ]; then
		exit $retcode
	fi
}

function copy_tarball_to_rootfs() {
	tarball_file=$1
	target_file=$2
	target_file_type=$3

	if [[ $target_file_type = *"cpio archive"* ]]; then
		mkdir -p out/tarball
		tar -xvf $tarball_file -C out/tarball
		cd out/tarball
		find . | cpio -oA -H newc -F ../../$target_file
		cd ../../
		rm -rf out/tarball
	else
		required_size=$(${GZ} -l $tarball_file | tail -1 | awk '{print $2}')
		required_size=$(( $required_size / 1024 ))

		sudo e2fsck -y $target_file
		block_count=$(sudo dumpe2fs -h $target_file | grep "Block count" | awk '{print $3}')
		block_size=$(sudo dumpe2fs -h $target_file | grep "Block size" | awk '{print $3}')
		current_size=$(( $block_size * $block_count / 1024 ))

		final_size=$(( $current_size + $required_size + 32768 ))
		sudo resize2fs -p $target_file "$final_size"K

		mkdir -p out/rootfs_mount
		sudo mount -o loop $target_file out/rootfs_mount
		sudo tar -xvf $tarball_file -C out/rootfs_mount
		sudo umount out/rootfs_mount
		rm -rf out/rootfs_mount
	fi
}

function create_ramdisk_from_folder() {
	ramdisk_name=$1
	ramdisk_folder=$2
	ramdisk="$ramdisk_name.cpio"

	cd $ramdisk_folder
	find . | cpio -ov -H newc > "../../out/$ramdisk"
	${GZ} "../../out/$ramdisk"
	ramdisk=$ramdisk.gz
	echo "$ramdisk"
	cd ../
}

function overlay_ramdisk_from_git() {
	git_repo=$1
	git_branch=$2

	# clone git repo and get revision details
	project_name="$(basename "$git_repo" .git)"
	project_folder="$project_name"
	project_ramdisk_folder="$(realpath $project_folder)/rootfs"
	git clone -b "$git_branch" --depth 1 "$git_repo" "$project_folder"
	cd "$project_folder"
	DESTDIR="$project_ramdisk_folder" prefix="/usr" make install 2>&1 > /dev/null
	project_name="$project_name-$(git rev-parse --short HEAD)"

	# created the overlayed ramdisk involves the creation of new ramdisk from folder and
	# concat both into a single file
	project_ramdisk_overlay=$(create_ramdisk_from_folder $project_name $project_ramdisk_folder)
	cd ../

	overlayed_ramdisk_file="$(basename $ramdisk_file)+$(basename $project_ramdisk_overlay)"
	cat "$ramdisk_file" "out/$project_ramdisk_overlay" > "out/$overlayed_ramdisk_file"
	echo "$overlayed_ramdisk_file"
	rm -rf "$project_folder"
}

function overlay_ramdisk_from_file() {
	file_name=$1
	file_cpio="out/$2.cpio"

	echo $file_name | cpio -ov -H newc > $file_cpio
	${GZ} $file_cpio
	file_cpio=$file_cpio.gz

	overlayed_ramdisk_file="$(basename $ramdisk_file)+$(basename $file_cpio)"
	cat "$ramdisk_file" "$file_cpio" > "out/$overlayed_ramdisk_file"
	echo "$overlayed_ramdisk_file"
}

# Set default tools to use
if [ -z "${GZ}" ]; then
	export GZ=gzip
fi

# Generic/default variables
KERNEL_CI_PLATFORM=${MACHINE}
BOOTIMG_PAGESIZE=2048
BOOTIMG_BASE=0x80000000
RAMDISK_BASE=0x84000000
SERIAL_CONSOLE=ttyMSM0
KERNEL_CI_MACH=qcom

# Set per MACHINE configuration
case "${MACHINE}" in
	apq8016-sbc)
		KERNEL_DT_URL=${KERNEL_DT_URL_apq8016_sbc}
		RAMDISK_URL=${RAMDISK_URL_apq8016_sbc}
		ROOTFS_URL=${ROOTFS_URL_apq8016_sbc}
		FIRMWARE_URL=${FIRMWARE_URL_apq8016_sbc}
		ROOTFS_PARTITION=/dev/mmcblk0p10
		;;
	apq8096-db820c)
		KERNEL_DT_URL=${KERNEL_DT_URL_apq8096_db820c}
		RAMDISK_URL=${RAMDISK_URL_apq8096_db820c}
		ROOTFS_URL=${ROOTFS_URL_apq8096_db820c}
		FIRMWARE_URL=${FIRMWARE_URL_apq8096_db820c}
		BOOTIMG_PAGESIZE=4096
		ROOTFS_PARTITION=/dev/sda1
		;;
	sdm845-mtp)
		KERNEL_DT_URL=${KERNEL_DT_URL_sdm845_mtp}
		RAMDISK_URL=${RAMDISK_URL_sdm845_mtp}
		ROOTFS_URL=${ROOTFS_URL_sdm845_mtp}
		FIRMWARE_URL=${FIRMWARE_URL_sdm845_mtp}
		ROOTFS_PARTITION=/dev/sda8 # XXX: using Android userdata since we don't have Linux parttable
		;;
	qcs404-evb-1000)
		KERNEL_DT_URL=${KERNEL_DT_URL_qcs404_evb_1000}
		RAMDISK_URL=${RAMDISK_URL_qcs404_evb_1000}
		ROOTFS_URL=${ROOTFS_URL_qcs404_evb_1000}
		FIRMWARE_URL=${FIRMWARE_URL_qcs404_evb_1000}
		ROOTFS_PARTITION=/dev/mmcblk0p26
		;;
	qcs404-evb-4000)
		KERNEL_DT_URL=${KERNEL_DT_URL_qcs404_evb_4000}
		RAMDISK_URL=${RAMDISK_URL_qcs404_evb_4000}
		ROOTFS_URL=${ROOTFS_URL_qcs404_evb_4000}
		FIRMWARE_URL=${FIRMWARE_URL_qcs404_evb_4000}
		ROOTFS_PARTITION=/dev/mmcblk0p26
		;;
	*)
		echo "Currently MACHINE: ${MACHINE} isn't supported"
		exit 1
		;;
esac

# Validate required parameters
if [ -z "${KERNEL_IMAGE_URL}" ]; then
	echo "ERROR: KERNEL_IMAGE_URL is empty"
	exit 1
fi
if [ -z "${RAMDISK_URL}" ]; then
	echo "ERROR: RAMDISK_URL is empty"
	exit 1
fi
if [ -z "${ROOTFS_URL}" ]; then
	echo "ERROR: RAMDISK_URL is empty"
	exit 1
fi

# Build information
mkdir -p out
cat > out/HEADER.textile << EOF

h4. QCOM Landing Team - $BUILD_DISPLAY_NAME

Build description:
* Build URL: "$BUILD_URL":$BUILD_URL
* Kernel image URL: $KERNEL_IMAGE_URL
* Kernel dt URL: $KERNEL_DT_URL
* kernel modules URL: $KERNEL_MODULES_URL
* Ramdisk URL: $RAMDISK_URL
* RootFS URL: $ROOTFS_URL
* Firmware URL: $FIRMWARE_URL
EOF

# Ramdisk/RootFS image, modules populate
wget_error ${RAMDISK_URL}
ramdisk_file=out/$(basename ${RAMDISK_URL})
ramdisk_file_type=$(file $ramdisk_file)

wget_error ${ROOTFS_URL}
rootfs_file=out/$(basename ${ROOTFS_URL})
rootfs_file_type=$(file $rootfs_file)

if [[ ! -z "${KERNEL_MODULES_URL}" ]]; then
	wget_error ${KERNEL_MODULES_URL}
	modules_file="out/$(basename ${KERNEL_MODULES_URL})"

	# XXX: Compress modules to gzip for use copy_tarball_to_rootfs
	# generic code to calculate size in ext4 filesystem
	modules_file_type=$(file $modules_file)
	if [[ $modules_file_type = *"XZ compressed data"* ]]; then
		xz -d $modules_file
		modules_file="out/$(basename ${KERNEL_MODULES_URL} .xz)"
		${GZ} $modules_file
		modules_file=$modules_file.gz
	elif [[ $modules_file_type = *"bzip2 compressed data"* ]]; then
		bzip2 -d $modules_file
		modules_file="out/$(basename ${KERNEL_MODULES_URL} .bz2)"
		${GZ} $modules_file
		modules_file=$modules_file.gz
	fi
fi
if [[ ! -z "${FIRMWARE_URL}" ]]; then
	wget_error ${FIRMWARE_URL}
	firmware_file="out/$(basename ${FIRMWARE_URL} .bz2)"
fi

rootfs_comp=''
if [[ $rootfs_file_type = *"gzip compressed data"* ]]; then
	${GZ} -d $rootfs_file
	rootfs_file=out/$(basename ${ROOTFS_URL} .gz)
	rootfs_file_type=$(file $rootfs_file)
	rootfs_comp='gz'
fi
if [[ $ramdisk_file_type = *"gzip compressed data"* ]]; then
	${GZ} -d $ramdisk_file
	ramdisk_file=out/$(basename ${RAMDISK_URL} .gz)
	ramdisk_file_type=$(file $ramdisk_file)
	ramdisk_comp='gz'
fi

if [[ $rootfs_file_type = *"Android sparse image"* ]]; then
	rootfs_file_ext4=out/$(basename ${rootfs_file} .img).ext4
	simg2img $rootfs_file $rootfs_file_ext4
	rootfs_file=$rootfs_file_ext4
elif [[ $rootfs_file_type = *"ext4 filesystem data"* ]]; then
	rootfs_file=$rootfs_file
else
	echo "ERROR: ROOTFS_IMAGE type isn't supported: $rootfs_file_type"
	exit 1
fi

if [[ ! -z "$modules_file" ]]; then
	copy_tarball_to_rootfs "$modules_file" "$ramdisk_file" "$ramdisk_file_type"
	copy_tarball_to_rootfs "$modules_file" "$rootfs_file" "$rootfs_file_type"
fi

if [[ ! -z "${firmware_file}" ]]; then
	copy_tarball_to_rootfs "$firmware_file" "$ramdisk_file" "$ramdisk_file_type"
	copy_tarball_to_rootfs "$firmware_file" "$rootfs_file" "$rootfs_file_type"
fi

if [[ $rootfs_file_type = *"Android sparse image"* ]]; then
	rootfs_file_img=out/$(basename $rootfs_file .ext4).img
	img2simg $rootfs_file $rootfs_file_img
	rm $rootfs_file
	rootfs_file=$rootfs_file_img
fi


if [[ $ramdisk_comp = "gz" ]]; then
	${GZ} $ramdisk_file
	ramdisk_file="$ramdisk_file".gz
	ramdisk_file_type=$(file $ramdisk_file)
	ramdisk_comp=""
fi
if [[ $rootfs_comp = "gz" ]]; then
	${GZ} $rootfs_file
	rootfs_file="$rootfs_file".gz
	rootfs_file_type=$(file $rootfs_file)
	rootfs_comp=""
fi

# Compress kernel image if isn't
wget_error ${KERNEL_IMAGE_URL}
kernel_file=out/$(basename ${KERNEL_IMAGE_URL})
kernel_file_type=$(file $kernel_file)
if [[ ! $kernel_file_type = *"gzip compressed data"* ]]; then
	${GZ} -kf $kernel_file
	kernel_file=$kernel_file.gz
fi

# Making android boot img
dt_mkbootimg_arg=""
if [[ ! -z "${KERNEL_DT_URL}" ]]; then
	wget_error ${KERNEL_DT_URL}
	dt_mkbootimg_arg="--dt out/$(basename ${KERNEL_DT_URL})"
fi

# Overlay ramdisk to install tools, artifacts, etc
if [[ ! -z "${BOOTRR_GIT_REPO}" ]]; then
	overlayed_ramdisk_file="out/$(overlay_ramdisk_from_git "${BOOTRR_GIT_REPO}" "${BOOTRR_GIT_BRANCH}")"
	ramdisk_file=$overlayed_ramdisk_file
fi

# Create boot image (bootrr), overlay the init script to setup the ramdisk
init_file=init
echo "${INITRAMFS_RAMDISK}" > ./$init_file
chmod +x ./$init_file
overlayed_ramdisk_file="out/$(overlay_ramdisk_from_file "$init_file" "init_ramdisk")"
rm -f $init_file

boot_file=boot-${KERNEL_FLAVOR}-${KERNEL_VERSION}-${BUILD_NUMBER}-${MACHINE}.img
skales-mkbootimg \
	--kernel $kernel_file \
	--ramdisk $overlayed_ramdisk_file \
	--output out/$boot_file \
	$dt_mkbootimg_arg \
	--pagesize "${BOOTIMG_PAGESIZE}" \
	--base "${BOOTIMG_BASE}" \
	--ramdisk_base "${RAMDISK_BASE}" \
	--cmdline "root=/dev/ram0 init=/init rw console=tty0 console=${SERIAL_CONSOLE},115200n8"

# Create boot image (functional), sdm845-mtp requires an initramfs to mount the rootfs and then
# exec switch_rootfs, use the same method in other boards too
boot_rootfs_file=boot-rootfs-${KERNEL_FLAVOR}-${KERNEL_VERSION}-${BUILD_NUMBER}-${MACHINE}.img
init_file=init
echo "${INITRAMFS_ROOTFS}" | sed -e "s|__ROOTFS_PARTITION__|${ROOTFS_PARTITION}|g" > ./$init_file
chmod +x ./$init_file
overlayed_ramdisk_file="out/$(overlay_ramdisk_from_file "$init_file" "init_rootfs")"
rm -f $init_file

skales-mkbootimg \
	--kernel $kernel_file \
	--ramdisk $overlayed_ramdisk_file \
	--output out/$boot_rootfs_file \
	$dt_mkbootimg_arg \
	--pagesize "${BOOTIMG_PAGESIZE}" \
	--base "${BOOTIMG_BASE}" \
	--ramdisk_base "${RAMDISK_BASE}" \
	--cmdline "root=/dev/ram0 init=/init rw console=tty0 console=${SERIAL_CONSOLE},115200n8"

echo BOOT_FILE=$boot_file >> builders_out_parameters
echo BOOT_ROOTFS_FILE=$boot_rootfs_file >> builders_out_parameters
echo ROOTFS_FILE="$(basename $rootfs_file)" >> builders_out_parameters

# Kernel CI parameters in LAVA jobs
echo KERNEL_IMAGE="$(basename $KERNEL_IMAGE_URL)" >> builders_out_parameters
echo KERNEL_DT="$(basename $KERNEL_DT_URL)" >> builders_out_parameters
echo KERNEL_CI_PLATFORM="${KERNEL_CI_PLATFORM}" >> builders_out_parameters
echo KERNEL_CI_MACH="${KERNEL_CI_MACH}" >> builders_out_parameters
echo RAMDISK_URL="${RAMDISK_URL}" >> builders_out_parameters
echo KERNEL_DT_URL="${KERNEL_DT_URL}" >> builders_out_parameters

ls -l out/