1#!/bin/bash -e
2# SPDX-License-Identifier: GPL-2.0+
3# (C) 2020 Pali Rohár <pali@kernel.org>
4
5# External tools needed for this test:
6echo '
7	wget
8	git
9	truncate
10	tar
11	dpkg
12	dd
13	make
14	gcc
15	arm-linux-gnueabi-gcc
16	fakeroot		(homepage http://fakeroot-ng.lingnu.com/)
17	mcopy			(from mtools, homepage http://www.gnu.org/software/mtools/)
18	mformat			(from mtools, homepage http://www.gnu.org/software/mtools/)
19	/usr/sbin/mkfs.ubifs	(from mtd-utils, homepage http://www.linux-mtd.infradead.org/)
20	/usr/sbin/ubinize	(from mtd-utils, homepage http://www.linux-mtd.infradead.org/)
21' | while read tool info; do
22	if test -z "$tool"; then continue; fi
23	if ! which $tool 1>/dev/null 2>&1; then
24		echo "Tool $tool was not found and is required to run this test"
25		echo "First install $tool $info"
26		exit 1
27	fi
28done || exit 1
29
30echo
31echo "============================================================"
32echo "========== Compiling U-Boot for Nokia RX-51 board =========="
33echo "============================================================"
34echo
35
36# First compile u-boot.bin binary for Nokia RX-51 board
37make nokia_rx51_config
38make -j4 u-boot.bin ARCH=arm CROSS_COMPILE=arm-linux-gnueabi-
39
40# And then do all stuff in temporary directory
41mkdir -p nokia_rx51_tmp
42cd nokia_rx51_tmp
43
44test -f mkimage || ln -s ../tools/mkimage .
45test -f u-boot.bin || ln -s ../u-boot.bin .
46
47echo
48echo "=========================================================================="
49echo "========== Downloading and compiling qemu from qemu-linaro fork =========="
50echo "=========================================================================="
51echo
52
53# Download and compile linaro version qemu which has support for n900 machine
54# Last working commit is 8f8d8e0796efe1a6f34cdd83fb798f3c41217ec1
55if ! test -f qemu-system-arm; then
56	test -d qemu-linaro || git clone https://git.linaro.org/qemu/qemu-linaro.git
57	cd qemu-linaro
58	git checkout 8f8d8e0796efe1a6f34cdd83fb798f3c41217ec1
59	./configure --enable-system --target-list=arm-softmmu --disable-sdl --disable-gtk --disable-curses --audio-drv-list= --audio-card-list= --disable-werror --disable-xen --disable-xen-pci-passthrough --disable-brlapi --disable-vnc --disable-curl --disable-slirp --disable-kvm --disable-user --disable-linux-user --disable-bsd-user --disable-guest-base --disable-uuid --disable-vde --disable-linux-aio --disable-cap-ng --disable-attr --disable-blobs --disable-docs --disable-spice --disable-libiscsi --disable-smartcard-nss --disable-usb-redir --disable-guest-agent --disable-seccomp --disable-glusterfs --disable-nptl --disable-fdt
60	make -j4
61	cd ..
62	ln -s qemu-linaro/arm-softmmu/qemu-system-arm .
63fi
64
65echo
66echo "==================================================="
67echo "========== Downloading external binaries =========="
68echo "==================================================="
69echo
70
71# Download qflasher and nolo images
72# This is proprietary qemu flasher tool with first stage images, but license allows non-commercial redistribution
73wget -c http://repository.maemo.org/qemu-n900/qemu-n900.tar.gz
74tar -xf qemu-n900.tar.gz
75
76# Download Maemo script u-boot-gen-combined
77if ! test -f u-boot-gen-combined; then
78	test -d u-boot-maemo || git clone https://github.com/pali/u-boot-maemo.git
79	chmod +x u-boot-maemo/debian/u-boot-gen-combined
80	ln -s u-boot-maemo/debian/u-boot-gen-combined .
81fi
82
83# Download Maemo fiasco kernel
84wget -c http://repository.maemo.org/pool/maemo5.0/free/k/kernel/kernel_2.6.28-20103103+0m5_armel.deb
85dpkg -x kernel_2.6.28-20103103+0m5_armel.deb kernel_2.6.28
86
87# Download Maemo libc
88wget -c http://repository.maemo.org/pool/maemo5.0/free/g/glibc/libc6_2.5.1-1eglibc27+0m5_armel.deb
89dpkg -x libc6_2.5.1-1eglibc27+0m5_armel.deb libc6_2.5.1
90
91# Download Maemo busybox
92wget -c http://repository.maemo.org/pool/maemo5.0/free/b/busybox/busybox_1.10.2.legal-1osso30+0m5_armel.deb
93dpkg -x busybox_1.10.2.legal-1osso30+0m5_armel.deb busybox_1.10.2
94
95echo
96echo "======================================="
97echo "========== Generating images =========="
98echo "======================================="
99echo
100
101# Generate rootfs directory
102mkdir -p rootfs
103mkdir -p rootfs/dev/
104mkdir -p rootfs/bin/
105mkdir -p rootfs/sbin/
106mkdir -p rootfs/lib/
107cp -a busybox_1.10.2/bin/busybox rootfs/bin/
108cp -a libc6_2.5.1/lib/ld-linux.so.3 rootfs/lib/
109cp -a libc6_2.5.1/lib/ld-2.5.so rootfs/lib/
110cp -a libc6_2.5.1/lib/libc.so.6 rootfs/lib/
111cp -a libc6_2.5.1/lib/libc-2.5.so rootfs/lib/
112cp -a libc6_2.5.1/lib/libcrypt.so.1 rootfs/lib/
113cp -a libc6_2.5.1/lib/libcrypt-2.5.so rootfs/lib/
114test -f rootfs/bin/sh || ln -sf busybox rootfs/bin/sh
115test -f rootfs/sbin/poweroff || ln -sf ../bin/busybox rootfs/sbin/poweroff
116cat > rootfs/sbin/preinit << EOF
117#!/bin/sh
118echo
119echo "Successfully booted"
120echo
121/sbin/poweroff -f
122EOF
123chmod +x rootfs/sbin/preinit
124
125# Generate ubi config file for ubi rootfs image
126cat > ubi.ini << EOF
127[rootfs]
128mode=ubi
129image=ubifs.img
130vol_id=0
131vol_size=160MiB
132vol_type=dynamic
133vol_name=rootfs
134vol_alignment=1
135vol_flags=autoresize
136EOF
137
138# Generate ubi rootfs image from rootfs directory
139# NOTE: Character device on host filesystem can be created only by root
140#       But we do not need it on host filesystem, just in ubifs image
141#       So run mknod and mkfs.ubifs commands under fakeroot program
142#       which via LD_PRELOAD simulate mknod() and stat() functions
143#       so mkfs.ubifs will see dev/console as character device and
144#       put it correctly as character device into final ubifs image
145#       Therefore we can run whole script as non-root nobody user
146fakeroot sh -c '
147	rm -f rootfs/dev/console;
148	mknod rootfs/dev/console c 5 1;
149	/usr/sbin/mkfs.ubifs -m 2048 -e 129024 -c 2047 -r rootfs ubifs.img;
150'
151/usr/sbin/ubinize -o ubi.img -p 128KiB -m 2048 -s 512 ubi.ini
152
153# Generate bootmenu for U-Boot serial console testing
154cat > bootmenu_uboot << EOF
155setenv bootmenu_0 'Serial console test=echo; echo "Testing serial console"; echo; echo "Successfully booted"; echo; poweroff';
156setenv bootmenu_1;
157setenv bootmenu_delay 1;
158setenv bootdelay 1;
159EOF
160./mkimage -A arm -O linux -T script -C none -a 0 -e 0 -n bootmenu_uboot -d bootmenu_uboot bootmenu_uboot.scr
161
162# Generate bootmenu for eMMC booting
163cat > bootmenu_emmc << EOF
164setenv bootmenu_0 'uImage-2.6.28-omap1 from eMMC=setenv mmcnum 1; setenv mmcpart 1; setenv mmctype fat; setenv bootargs; setenv setup_omap_atag 1; setenv mmckernfile uImage-2.6.28-omap1; run trymmckernboot';
165setenv bootmenu_1;
166setenv bootmenu_delay 1;
167setenv bootdelay 1;
168EOF
169./mkimage -A arm -O linux -T script -C none -a 0 -e 0 -n bootmenu_emmc -d bootmenu_emmc bootmenu_emmc.scr
170
171# Generate bootmenu for OneNAND booting
172cat > bootmenu_nand << EOF
173setenv bootmenu_0 'uImage-2.6.28-omap1 from OneNAND=mtd read initfs \${kernaddr}; setenv bootargs; setenv setup_omap_atag 1; bootm \${kernaddr}';
174setenv bootmenu_1;
175setenv bootmenu_delay 1;
176setenv bootdelay 1;
177EOF
178./mkimage -A arm -O linux -T script -C none -a 0 -e 0 -n bootmenu_nand -d bootmenu_nand bootmenu_nand.scr
179
180# Generate combined image from u-boot and Maemo fiasco kernel
181dd if=kernel_2.6.28/boot/zImage-2.6.28-20103103+0m5.fiasco of=zImage-2.6.28-omap1 skip=95 bs=1
182./mkimage -A arm -O linux -T kernel -C none -a 80008000 -e 80008000 -n zImage-2.6.28-omap1 -d zImage-2.6.28-omap1 uImage-2.6.28-omap1
183./u-boot-gen-combined u-boot.bin uImage-2.6.28-omap1 combined.bin
184
185# Generate combined hack image from u-boot and Maemo fiasco kernel (kernel starts at 2MB offset and qflasher puts 2kB header before supplied image)
186cp u-boot.bin combined_hack.bin
187dd if=uImage-2.6.28-omap1 of=combined_hack.bin bs=1024 seek=$((2048-2))
188
189# Generate FAT32 eMMC image for U-Boot serial console testing
190truncate -s 50MiB emmc_uboot.img
191mformat -m 0xf8 -F -h 4 -s 16 -c 1 -t $((50*1024*1024/(4*16*512))) :: -i emmc_uboot.img
192mcopy bootmenu_uboot.scr ::/bootmenu.scr -i emmc_uboot.img
193
194# Generate FAT32 eMMC image for eMMC booting
195truncate -s 50MiB emmc_emmc.img
196mformat -m 0xf8 -F -h 4 -s 16 -c 1 -t $((50*1024*1024/(4*16*512))) :: -i emmc_emmc.img
197mcopy uImage-2.6.28-omap1 ::/uImage-2.6.28-omap1 -i emmc_emmc.img
198mcopy bootmenu_emmc.scr ::/bootmenu.scr -i emmc_emmc.img
199
200# Generate FAT32 eMMC image for OneNAND booting
201truncate -s 50MiB emmc_nand.img
202mformat -m 0xf8 -F -h 4 -s 16 -c 1 -t $((50*1024*1024/(4*16*512))) :: -i emmc_nand.img
203mcopy bootmenu_nand.scr ::/bootmenu.scr -i emmc_nand.img
204
205# Generate MTD image for U-Boot serial console testing
206rm -f mtd_uboot.img
207./qflasher -v -x xloader-qemu.bin -s secondary-qemu.bin -k u-boot.bin -m rx51 -o mtd_uboot.img
208
209# Generate MTD image for RAM booting from bootloader nolo images, compiled image and rootfs image
210rm -f mtd_ram.img
211./qflasher -v -x xloader-qemu.bin -s secondary-qemu.bin -k combined.bin -r ubi.img -m rx51 -o mtd_ram.img
212
213# Generate MTD image for eMMC booting from bootloader nolo images, u-boot image and rootfs image
214rm -f mtd_emmc.img
215./qflasher -v -x xloader-qemu.bin -s secondary-qemu.bin -k u-boot.bin -r ubi.img -m rx51 -o mtd_emmc.img
216
217# Generate MTD image for OneNAND booting from bootloader nolo images, combined hacked image and rootfs image
218# Kernel image is put into initfs area, but qflasher reject to copy kernel image into initfs area because it does not have initfs signature
219# This is hack to workaround this problem, tell qflasher that kernel area for u-boot is bigger and put big combined hacked image (u-boot + kernel with correct offset)
220rm -f mtd_nand.img
221./qflasher -v -x xloader-qemu.bin -s secondary-qemu.bin -k combined_hack.bin -r ubi.img -m rx51 -p k=4094,i=2 -o mtd_nand.img
222
223echo
224echo "======================================================"
225echo "========== Running test images in n900 qemu =========="
226echo "======================================================"
227echo
228
229# Run MTD image in qemu and wait for 300s if U-Boot prints testing string to serial console and poweroff
230rm -f qemu_uboot.log
231./qemu-system-arm -M n900 -mtdblock mtd_uboot.img -sd emmc_uboot.img -serial /dev/stdout -display none > qemu_uboot.log &
232qemu_pid=$!
233tail -F qemu_uboot.log &
234tail_pid=$!
235sleep 300 &
236sleep_pid=$!
237wait -n $sleep_pid $qemu_pid || true
238kill -9 $tail_pid $sleep_pid $qemu_pid 2>/dev/null || true
239wait || true
240
241# Run MTD image in qemu and wait for 300s if kernel from RAM is correctly booted
242rm -f qemu_ram.log
243./qemu-system-arm -M n900 -mtdblock mtd_ram.img -serial /dev/stdout -display none > qemu_ram.log &
244qemu_pid=$!
245tail -F qemu_ram.log &
246tail_pid=$!
247sleep 300 &
248sleep_pid=$!
249wait -n $sleep_pid $qemu_pid || true
250kill -9 $tail_pid $sleep_pid $qemu_pid 2>/dev/null || true
251wait || true
252
253# Run MTD image in qemu and wait for 300s if kernel from eMMC is correctly booted
254rm -f qemu_emmc.log
255./qemu-system-arm -M n900 -mtdblock mtd_emmc.img -sd emmc_emmc.img -serial /dev/stdout -display none > qemu_emmc.log &
256qemu_pid=$!
257tail -F qemu_emmc.log &
258tail_pid=$!
259sleep 300 &
260sleep_pid=$!
261wait -n $sleep_pid $qemu_pid || true
262kill -9 $tail_pid $sleep_pid $qemu_pid 2>/dev/null || true
263wait || true
264
265# Run MTD image in qemu and wait for 300s if kernel from OneNAND is correctly booted
266rm -f qemu_nand.log
267./qemu-system-arm -M n900 -mtdblock mtd_nand.img -sd emmc_nand.img -serial /dev/stdout -display none > qemu_nand.log &
268qemu_pid=$!
269tail -F qemu_nand.log &
270tail_pid=$!
271sleep 300 &
272sleep_pid=$!
273wait -n $sleep_pid $qemu_pid || true
274kill -9 $tail_pid $sleep_pid $qemu_pid 2>/dev/null || true
275wait || true
276
277echo
278echo "============================="
279echo "========== Results =========="
280echo "============================="
281echo
282
283if grep -q 'Successfully booted' qemu_uboot.log; then echo "U-Boot serial console is working"; else echo "U-Boot serial console test failed"; fi
284if grep -q 'Successfully booted' qemu_ram.log; then echo "Kernel was successfully booted from RAM"; else echo "Failed to boot kernel from RAM"; fi
285if grep -q 'Successfully booted' qemu_emmc.log; then echo "Kernel was successfully booted from eMMC"; else echo "Failed to boot kernel from eMMC"; fi
286if grep -q 'Successfully booted' qemu_nand.log; then echo "Kernel was successfully booted from OneNAND"; else echo "Failed to boot kernel from OneNAND"; fi
287
288echo
289
290if grep -q 'Successfully booted' qemu_uboot.log && grep -q 'Successfully booted' qemu_ram.log && grep -q 'Successfully booted' qemu_emmc.log && grep -q 'Successfully booted' qemu_nand.log; then
291	echo "All tests passed"
292	exit 0
293else
294	echo "Some tests failed"
295	exit 1
296fi
297