1# Functional test that boots a Linux kernel and checks the console
2#
3# Copyright (c) 2018 Red Hat, Inc.
4#
5# Author:
6#  Cleber Rosa <crosa@redhat.com>
7#
8# This work is licensed under the terms of the GNU GPL, version 2 or
9# later.  See the COPYING file in the top-level directory.
10
11import os
12import lzma
13import gzip
14import shutil
15
16from avocado import skip
17from avocado import skipUnless
18from avocado import skipUnless
19from avocado_qemu import QemuSystemTest
20from avocado_qemu import exec_command
21from avocado_qemu import exec_command_and_wait_for_pattern
22from avocado_qemu import interrupt_interactive_console_until_pattern
23from avocado_qemu import wait_for_console_pattern
24from avocado.utils import process
25from avocado.utils import archive
26
27"""
28Round up to next power of 2
29"""
30def pow2ceil(x):
31    return 1 if x == 0 else 2**(x - 1).bit_length()
32
33def file_truncate(path, size):
34    if size != os.path.getsize(path):
35        with open(path, 'ab+') as fd:
36            fd.truncate(size)
37
38"""
39Expand file size to next power of 2
40"""
41def image_pow2ceil_expand(path):
42        size = os.path.getsize(path)
43        size_aligned = pow2ceil(size)
44        if size != size_aligned:
45            with open(path, 'ab+') as fd:
46                fd.truncate(size_aligned)
47
48class LinuxKernelTest(QemuSystemTest):
49    KERNEL_COMMON_COMMAND_LINE = 'printk.time=0 '
50
51    def wait_for_console_pattern(self, success_message, vm=None):
52        wait_for_console_pattern(self, success_message,
53                                 failure_message='Kernel panic - not syncing',
54                                 vm=vm)
55
56    def extract_from_deb(self, deb, path):
57        """
58        Extracts a file from a deb package into the test workdir
59
60        :param deb: path to the deb archive
61        :param path: path within the deb archive of the file to be extracted
62        :returns: path of the extracted file
63        """
64        cwd = os.getcwd()
65        os.chdir(self.workdir)
66        file_path = process.run("ar t %s" % deb).stdout_text.split()[2]
67        process.run("ar x %s %s" % (deb, file_path))
68        archive.extract(file_path, self.workdir)
69        os.chdir(cwd)
70        # Return complete path to extracted file.  Because callers to
71        # extract_from_deb() specify 'path' with a leading slash, it is
72        # necessary to use os.path.relpath() as otherwise os.path.join()
73        # interprets it as an absolute path and drops the self.workdir part.
74        return os.path.normpath(os.path.join(self.workdir,
75                                             os.path.relpath(path, '/')))
76
77    def extract_from_rpm(self, rpm, path):
78        """
79        Extracts a file from an RPM package into the test workdir.
80
81        :param rpm: path to the rpm archive
82        :param path: path within the rpm archive of the file to be extracted
83                     needs to be a relative path (starting with './') because
84                     cpio(1), which is used to extract the file, expects that.
85        :returns: path of the extracted file
86        """
87        cwd = os.getcwd()
88        os.chdir(self.workdir)
89        process.run("rpm2cpio %s | cpio -id %s" % (rpm, path), shell=True)
90        os.chdir(cwd)
91        return os.path.normpath(os.path.join(self.workdir, path))
92
93class BootLinuxConsole(LinuxKernelTest):
94    """
95    Boots a Linux kernel and checks that the console is operational and the
96    kernel command line is properly passed from QEMU to the kernel
97    """
98    timeout = 90
99
100    def test_x86_64_pc(self):
101        """
102        :avocado: tags=arch:x86_64
103        :avocado: tags=machine:pc
104        """
105        kernel_url = ('https://archives.fedoraproject.org/pub/archive/fedora'
106                      '/linux/releases/29/Everything/x86_64/os/images/pxeboot'
107                      '/vmlinuz')
108        kernel_hash = '23bebd2680757891cf7adedb033532163a792495'
109        kernel_path = self.fetch_asset(kernel_url, asset_hash=kernel_hash)
110
111        self.vm.set_console()
112        kernel_command_line = self.KERNEL_COMMON_COMMAND_LINE + 'console=ttyS0'
113        self.vm.add_args('-kernel', kernel_path,
114                         '-append', kernel_command_line)
115        self.vm.launch()
116        console_pattern = 'Kernel command line: %s' % kernel_command_line
117        self.wait_for_console_pattern(console_pattern)
118
119    def test_aarch64_xlnx_versal_virt(self):
120        """
121        :avocado: tags=arch:aarch64
122        :avocado: tags=machine:xlnx-versal-virt
123        :avocado: tags=device:pl011
124        :avocado: tags=device:arm_gicv3
125        :avocado: tags=accel:tcg
126        """
127        images_url = ('http://ports.ubuntu.com/ubuntu-ports/dists/'
128                      'bionic-updates/main/installer-arm64/'
129                      '20101020ubuntu543.19/images/')
130        kernel_url = images_url + 'netboot/ubuntu-installer/arm64/linux'
131        kernel_hash = 'e167757620640eb26de0972f578741924abb3a82'
132        kernel_path = self.fetch_asset(kernel_url, asset_hash=kernel_hash)
133
134        initrd_url = images_url + 'netboot/ubuntu-installer/arm64/initrd.gz'
135        initrd_hash = 'cab5cb3fcefca8408aa5aae57f24574bfce8bdb9'
136        initrd_path = self.fetch_asset(initrd_url, asset_hash=initrd_hash)
137
138        self.vm.set_console()
139        self.vm.add_args('-m', '2G',
140                         '-accel', 'tcg',
141                         '-kernel', kernel_path,
142                         '-initrd', initrd_path)
143        self.vm.launch()
144        self.wait_for_console_pattern('Checked W+X mappings: passed')
145
146    def test_arm_virt(self):
147        """
148        :avocado: tags=arch:arm
149        :avocado: tags=machine:virt
150        :avocado: tags=accel:tcg
151        """
152        kernel_url = ('https://archives.fedoraproject.org/pub/archive/fedora'
153                      '/linux/releases/29/Everything/armhfp/os/images/pxeboot'
154                      '/vmlinuz')
155        kernel_hash = 'e9826d741b4fb04cadba8d4824d1ed3b7fb8b4d4'
156        kernel_path = self.fetch_asset(kernel_url, asset_hash=kernel_hash)
157
158        self.vm.set_console()
159        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
160                               'console=ttyAMA0')
161        self.vm.add_args('-kernel', kernel_path,
162                         '-append', kernel_command_line)
163        self.vm.launch()
164        console_pattern = 'Kernel command line: %s' % kernel_command_line
165        self.wait_for_console_pattern(console_pattern)
166
167    def test_arm_emcraft_sf2(self):
168        """
169        :avocado: tags=arch:arm
170        :avocado: tags=machine:emcraft-sf2
171        :avocado: tags=endian:little
172        :avocado: tags=u-boot
173        :avocado: tags=accel:tcg
174        """
175        self.require_netdev('user')
176
177        uboot_url = ('https://raw.githubusercontent.com/'
178                     'Subbaraya-Sundeep/qemu-test-binaries/'
179                     'fe371d32e50ca682391e1e70ab98c2942aeffb01/u-boot')
180        uboot_hash = 'cbb8cbab970f594bf6523b9855be209c08374ae2'
181        uboot_path = self.fetch_asset(uboot_url, asset_hash=uboot_hash)
182        spi_url = ('https://raw.githubusercontent.com/'
183                   'Subbaraya-Sundeep/qemu-test-binaries/'
184                   'fe371d32e50ca682391e1e70ab98c2942aeffb01/spi.bin')
185        spi_hash = '65523a1835949b6f4553be96dec1b6a38fb05501'
186        spi_path = self.fetch_asset(spi_url, asset_hash=spi_hash)
187        spi_path_rw = os.path.join(self.workdir, os.path.basename(spi_path))
188        shutil.copy(spi_path, spi_path_rw)
189
190        file_truncate(spi_path_rw, 16 << 20) # Spansion S25FL128SDPBHICO is 16 MiB
191
192        self.vm.set_console()
193        kernel_command_line = self.KERNEL_COMMON_COMMAND_LINE
194        self.vm.add_args('-kernel', uboot_path,
195                         '-append', kernel_command_line,
196                         '-drive', 'file=' + spi_path_rw + ',if=mtd,format=raw',
197                         '-no-reboot')
198        self.vm.launch()
199        self.wait_for_console_pattern('Enter \'help\' for a list')
200
201        exec_command_and_wait_for_pattern(self, 'ifconfig eth0 10.0.2.15',
202                                                 'eth0: link becomes ready')
203        exec_command_and_wait_for_pattern(self, 'ping -c 3 10.0.2.2',
204            '3 packets transmitted, 3 packets received, 0% packet loss')
205
206    def test_arm_exynos4210_initrd(self):
207        """
208        :avocado: tags=arch:arm
209        :avocado: tags=machine:smdkc210
210        :avocado: tags=accel:tcg
211        """
212        deb_url = ('https://snapshot.debian.org/archive/debian/'
213                   '20190928T224601Z/pool/main/l/linux/'
214                   'linux-image-4.19.0-6-armmp_4.19.67-2+deb10u1_armhf.deb')
215        deb_hash = 'fa9df4a0d38936cb50084838f2cb933f570d7d82'
216        deb_path = self.fetch_asset(deb_url, asset_hash=deb_hash)
217        kernel_path = self.extract_from_deb(deb_path,
218                                            '/boot/vmlinuz-4.19.0-6-armmp')
219        dtb_path = '/usr/lib/linux-image-4.19.0-6-armmp/exynos4210-smdkv310.dtb'
220        dtb_path = self.extract_from_deb(deb_path, dtb_path)
221
222        initrd_url = ('https://github.com/groeck/linux-build-test/raw/'
223                      '2eb0a73b5d5a28df3170c546ddaaa9757e1e0848/rootfs/'
224                      'arm/rootfs-armv5.cpio.gz')
225        initrd_hash = '2b50f1873e113523967806f4da2afe385462ff9b'
226        initrd_path_gz = self.fetch_asset(initrd_url, asset_hash=initrd_hash)
227        initrd_path = os.path.join(self.workdir, 'rootfs.cpio')
228        archive.gzip_uncompress(initrd_path_gz, initrd_path)
229
230        self.vm.set_console()
231        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
232                               'earlycon=exynos4210,0x13800000 earlyprintk ' +
233                               'console=ttySAC0,115200n8 ' +
234                               'random.trust_cpu=off cryptomgr.notests ' +
235                               'cpuidle.off=1 panic=-1 noreboot')
236
237        self.vm.add_args('-kernel', kernel_path,
238                         '-dtb', dtb_path,
239                         '-initrd', initrd_path,
240                         '-append', kernel_command_line,
241                         '-no-reboot')
242        self.vm.launch()
243
244        self.wait_for_console_pattern('Boot successful.')
245        # TODO user command, for now the uart is stuck
246
247    def test_arm_cubieboard_initrd(self):
248        """
249        :avocado: tags=arch:arm
250        :avocado: tags=machine:cubieboard
251        :avocado: tags=accel:tcg
252        """
253        deb_url = ('https://apt.armbian.com/pool/main/l/'
254                   'linux-6.6.16/linux-image-current-sunxi_24.2.1_armhf__6.6.16-Seb3e-D6b4a-P2359-Ce96bHfe66-HK01ba-V014b-B067e-R448a.deb')
255        deb_hash = 'f7c3c8c5432f765445dc6e7eab02f3bbe668256b'
256        deb_path = self.fetch_asset(deb_url, asset_hash=deb_hash)
257        kernel_path = self.extract_from_deb(deb_path,
258                                            '/boot/vmlinuz-6.6.16-current-sunxi')
259        dtb_path = '/usr/lib/linux-image-6.6.16-current-sunxi/sun4i-a10-cubieboard.dtb'
260        dtb_path = self.extract_from_deb(deb_path, dtb_path)
261        initrd_url = ('https://github.com/groeck/linux-build-test/raw/'
262                      '2eb0a73b5d5a28df3170c546ddaaa9757e1e0848/rootfs/'
263                      'arm/rootfs-armv5.cpio.gz')
264        initrd_hash = '2b50f1873e113523967806f4da2afe385462ff9b'
265        initrd_path_gz = self.fetch_asset(initrd_url, asset_hash=initrd_hash)
266        initrd_path = os.path.join(self.workdir, 'rootfs.cpio')
267        archive.gzip_uncompress(initrd_path_gz, initrd_path)
268
269        self.vm.set_console()
270        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
271                               'console=ttyS0,115200 '
272                               'usbcore.nousb '
273                               'panic=-1 noreboot')
274        self.vm.add_args('-kernel', kernel_path,
275                         '-dtb', dtb_path,
276                         '-initrd', initrd_path,
277                         '-append', kernel_command_line,
278                         '-no-reboot')
279        self.vm.launch()
280        self.wait_for_console_pattern('Boot successful.')
281
282        exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo',
283                                                'Allwinner sun4i/sun5i')
284        exec_command_and_wait_for_pattern(self, 'cat /proc/iomem',
285                                                'system-control@1c00000')
286        exec_command_and_wait_for_pattern(self, 'reboot',
287                                                'reboot: Restarting system')
288        # Wait for VM to shut down gracefully
289        self.vm.wait()
290
291    def test_arm_cubieboard_sata(self):
292        """
293        :avocado: tags=arch:arm
294        :avocado: tags=machine:cubieboard
295        :avocado: tags=accel:tcg
296        """
297        deb_url = ('https://apt.armbian.com/pool/main/l/'
298                   'linux-6.6.16/linux-image-current-sunxi_24.2.1_armhf__6.6.16-Seb3e-D6b4a-P2359-Ce96bHfe66-HK01ba-V014b-B067e-R448a.deb')
299        deb_hash = 'f7c3c8c5432f765445dc6e7eab02f3bbe668256b'
300        deb_path = self.fetch_asset(deb_url, asset_hash=deb_hash)
301        kernel_path = self.extract_from_deb(deb_path,
302                                            '/boot/vmlinuz-6.6.16-current-sunxi')
303        dtb_path = '/usr/lib/linux-image-6.6.16-current-sunxi/sun4i-a10-cubieboard.dtb'
304        dtb_path = self.extract_from_deb(deb_path, dtb_path)
305        rootfs_url = ('https://github.com/groeck/linux-build-test/raw/'
306                      '2eb0a73b5d5a28df3170c546ddaaa9757e1e0848/rootfs/'
307                      'arm/rootfs-armv5.ext2.gz')
308        rootfs_hash = '093e89d2b4d982234bf528bc9fb2f2f17a9d1f93'
309        rootfs_path_gz = self.fetch_asset(rootfs_url, asset_hash=rootfs_hash)
310        rootfs_path = os.path.join(self.workdir, 'rootfs.cpio')
311        archive.gzip_uncompress(rootfs_path_gz, rootfs_path)
312
313        self.vm.set_console()
314        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
315                               'console=ttyS0,115200 '
316                               'usbcore.nousb '
317                               'root=/dev/sda ro '
318                               'panic=-1 noreboot')
319        self.vm.add_args('-kernel', kernel_path,
320                         '-dtb', dtb_path,
321                         '-drive', 'if=none,format=raw,id=disk0,file='
322                                   + rootfs_path,
323                         '-device', 'ide-hd,bus=ide.0,drive=disk0',
324                         '-append', kernel_command_line,
325                         '-no-reboot')
326        self.vm.launch()
327        self.wait_for_console_pattern('Boot successful.')
328
329        exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo',
330                                                'Allwinner sun4i/sun5i')
331        exec_command_and_wait_for_pattern(self, 'cat /proc/partitions',
332                                                'sda')
333        exec_command_and_wait_for_pattern(self, 'reboot',
334                                                'reboot: Restarting system')
335        # Wait for VM to shut down gracefully
336        self.vm.wait()
337
338    @skipUnless(os.getenv('AVOCADO_ALLOW_LARGE_STORAGE'), 'storage limited')
339    def test_arm_cubieboard_openwrt_22_03_2(self):
340        """
341        :avocado: tags=arch:arm
342        :avocado: tags=machine:cubieboard
343        :avocado: tags=device:sd
344        """
345
346        # This test download a 7.5 MiB compressed image and expand it
347        # to 126 MiB.
348        image_url = ('https://downloads.openwrt.org/releases/22.03.2/targets/'
349                     'sunxi/cortexa8/openwrt-22.03.2-sunxi-cortexa8-'
350                     'cubietech_a10-cubieboard-ext4-sdcard.img.gz')
351        image_hash = ('94b5ecbfbc0b3b56276e5146b899eafa'
352                      '2ac5dc2d08733d6705af9f144f39f554')
353        image_path_gz = self.fetch_asset(image_url, asset_hash=image_hash,
354                                         algorithm='sha256')
355        image_path = archive.extract(image_path_gz, self.workdir)
356        image_pow2ceil_expand(image_path)
357
358        self.vm.set_console()
359        self.vm.add_args('-drive', 'file=' + image_path + ',if=sd,format=raw',
360                         '-nic', 'user',
361                         '-no-reboot')
362        self.vm.launch()
363
364        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
365                               'usbcore.nousb '
366                               'noreboot')
367
368        self.wait_for_console_pattern('U-Boot SPL')
369
370        interrupt_interactive_console_until_pattern(
371                self, 'Hit any key to stop autoboot:', '=>')
372        exec_command_and_wait_for_pattern(self, "setenv extraargs '" +
373                                                kernel_command_line + "'", '=>')
374        exec_command_and_wait_for_pattern(self, 'boot', 'Starting kernel ...');
375
376        self.wait_for_console_pattern(
377            'Please press Enter to activate this console.')
378
379        exec_command_and_wait_for_pattern(self, ' ', 'root@')
380
381        exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo',
382                                                'Allwinner sun4i/sun5i')
383        exec_command_and_wait_for_pattern(self, 'reboot',
384                                                'reboot: Restarting system')
385        # Wait for VM to shut down gracefully
386        self.vm.wait()
387
388    @skipUnless(os.getenv('AVOCADO_TIMEOUT_EXPECTED'), 'Test might timeout')
389    def test_arm_quanta_gsj(self):
390        """
391        :avocado: tags=arch:arm
392        :avocado: tags=machine:quanta-gsj
393        :avocado: tags=accel:tcg
394        """
395        # 25 MiB compressed, 32 MiB uncompressed.
396        image_url = (
397                'https://github.com/hskinnemoen/openbmc/releases/download/'
398                '20200711-gsj-qemu-0/obmc-phosphor-image-gsj.static.mtd.gz')
399        image_hash = '14895e634923345cb5c8776037ff7876df96f6b1'
400        image_path_gz = self.fetch_asset(image_url, asset_hash=image_hash)
401        image_name = 'obmc.mtd'
402        image_path = os.path.join(self.workdir, image_name)
403        archive.gzip_uncompress(image_path_gz, image_path)
404
405        self.vm.set_console()
406        drive_args = 'file=' + image_path + ',if=mtd,bus=0,unit=0'
407        self.vm.add_args('-drive', drive_args)
408        self.vm.launch()
409
410        # Disable drivers and services that stall for a long time during boot,
411        # to avoid running past the 90-second timeout. These may be removed
412        # as the corresponding device support is added.
413        kernel_command_line = self.KERNEL_COMMON_COMMAND_LINE + (
414                'console=${console} '
415                'mem=${mem} '
416                'initcall_blacklist=npcm_i2c_bus_driver_init '
417                'systemd.mask=systemd-random-seed.service '
418                'systemd.mask=dropbearkey.service '
419        )
420
421        self.wait_for_console_pattern('> BootBlock by Nuvoton')
422        self.wait_for_console_pattern('>Device: Poleg BMC NPCM730')
423        self.wait_for_console_pattern('>Skip DDR init.')
424        self.wait_for_console_pattern('U-Boot ')
425        interrupt_interactive_console_until_pattern(
426                self, 'Hit any key to stop autoboot:', 'U-Boot>')
427        exec_command_and_wait_for_pattern(
428                self, "setenv bootargs ${bootargs} " + kernel_command_line,
429                'U-Boot>')
430        exec_command_and_wait_for_pattern(
431                self, 'run romboot', 'Booting Kernel from flash')
432        self.wait_for_console_pattern('Booting Linux on physical CPU 0x0')
433        self.wait_for_console_pattern('CPU1: thread -1, cpu 1, socket 0')
434        self.wait_for_console_pattern('OpenBMC Project Reference Distro')
435        self.wait_for_console_pattern('gsj login:')
436
437    def test_arm_quanta_gsj_initrd(self):
438        """
439        :avocado: tags=arch:arm
440        :avocado: tags=machine:quanta-gsj
441        :avocado: tags=accel:tcg
442        """
443        initrd_url = (
444                'https://github.com/hskinnemoen/openbmc/releases/download/'
445                '20200711-gsj-qemu-0/obmc-phosphor-initramfs-gsj.cpio.xz')
446        initrd_hash = '98fefe5d7e56727b1eb17d5c00311b1b5c945300'
447        initrd_path = self.fetch_asset(initrd_url, asset_hash=initrd_hash)
448        kernel_url = (
449                'https://github.com/hskinnemoen/openbmc/releases/download/'
450                '20200711-gsj-qemu-0/uImage-gsj.bin')
451        kernel_hash = 'fa67b2f141d56d39b3c54305c0e8a899c99eb2c7'
452        kernel_path = self.fetch_asset(kernel_url, asset_hash=kernel_hash)
453        dtb_url = (
454                'https://github.com/hskinnemoen/openbmc/releases/download/'
455                '20200711-gsj-qemu-0/nuvoton-npcm730-gsj.dtb')
456        dtb_hash = '18315f7006d7b688d8312d5c727eecd819aa36a4'
457        dtb_path = self.fetch_asset(dtb_url, asset_hash=dtb_hash)
458
459        self.vm.set_console()
460        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
461                               'console=ttyS0,115200n8 '
462                               'earlycon=uart8250,mmio32,0xf0001000')
463        self.vm.add_args('-kernel', kernel_path,
464                         '-initrd', initrd_path,
465                         '-dtb', dtb_path,
466                         '-append', kernel_command_line)
467        self.vm.launch()
468
469        self.wait_for_console_pattern('Booting Linux on physical CPU 0x0')
470        self.wait_for_console_pattern('CPU1: thread -1, cpu 1, socket 0')
471        self.wait_for_console_pattern(
472                'Give root password for system maintenance')
473
474    def test_arm_bpim2u(self):
475        """
476        :avocado: tags=arch:arm
477        :avocado: tags=machine:bpim2u
478        :avocado: tags=accel:tcg
479        """
480        deb_url = ('https://apt.armbian.com/pool/main/l/'
481                   'linux-6.6.16/linux-image-current-sunxi_24.2.1_armhf__6.6.16-Seb3e-D6b4a-P2359-Ce96bHfe66-HK01ba-V014b-B067e-R448a.deb')
482        deb_hash = 'f7c3c8c5432f765445dc6e7eab02f3bbe668256b'
483        deb_path = self.fetch_asset(deb_url, asset_hash=deb_hash)
484        kernel_path = self.extract_from_deb(deb_path,
485                                            '/boot/vmlinuz-6.6.16-current-sunxi')
486        dtb_path = ('/usr/lib/linux-image-6.6.16-current-sunxi/'
487                    'sun8i-r40-bananapi-m2-ultra.dtb')
488        dtb_path = self.extract_from_deb(deb_path, dtb_path)
489
490        self.vm.set_console()
491        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
492                               'console=ttyS0,115200n8 '
493                               'earlycon=uart,mmio32,0x1c28000')
494        self.vm.add_args('-kernel', kernel_path,
495                         '-dtb', dtb_path,
496                         '-append', kernel_command_line)
497        self.vm.launch()
498        console_pattern = 'Kernel command line: %s' % kernel_command_line
499        self.wait_for_console_pattern(console_pattern)
500
501    def test_arm_bpim2u_initrd(self):
502        """
503        :avocado: tags=arch:arm
504        :avocado: tags=accel:tcg
505        :avocado: tags=machine:bpim2u
506        """
507        deb_url = ('https://apt.armbian.com/pool/main/l/'
508                   'linux-6.6.16/linux-image-current-sunxi_24.2.1_armhf__6.6.16-Seb3e-D6b4a-P2359-Ce96bHfe66-HK01ba-V014b-B067e-R448a.deb')
509        deb_hash = 'f7c3c8c5432f765445dc6e7eab02f3bbe668256b'
510        deb_path = self.fetch_asset(deb_url, asset_hash=deb_hash)
511        kernel_path = self.extract_from_deb(deb_path,
512                                            '/boot/vmlinuz-6.6.16-current-sunxi')
513        dtb_path = ('/usr/lib/linux-image-6.6.16-current-sunxi/'
514                    'sun8i-r40-bananapi-m2-ultra.dtb')
515        dtb_path = self.extract_from_deb(deb_path, dtb_path)
516        initrd_url = ('https://github.com/groeck/linux-build-test/raw/'
517                      '2eb0a73b5d5a28df3170c546ddaaa9757e1e0848/rootfs/'
518                      'arm/rootfs-armv7a.cpio.gz')
519        initrd_hash = '604b2e45cdf35045846b8bbfbf2129b1891bdc9c'
520        initrd_path_gz = self.fetch_asset(initrd_url, asset_hash=initrd_hash)
521        initrd_path = os.path.join(self.workdir, 'rootfs.cpio')
522        archive.gzip_uncompress(initrd_path_gz, initrd_path)
523
524        self.vm.set_console()
525        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
526                               'console=ttyS0,115200 '
527                               'panic=-1 noreboot')
528        self.vm.add_args('-kernel', kernel_path,
529                         '-dtb', dtb_path,
530                         '-initrd', initrd_path,
531                         '-append', kernel_command_line,
532                         '-no-reboot')
533        self.vm.launch()
534        self.wait_for_console_pattern('Boot successful.')
535
536        exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo',
537                                                'Allwinner sun8i Family')
538        exec_command_and_wait_for_pattern(self, 'cat /proc/iomem',
539                                                'system-control@1c00000')
540        exec_command_and_wait_for_pattern(self, 'reboot',
541                                                'reboot: Restarting system')
542        # Wait for VM to shut down gracefully
543        self.vm.wait()
544
545    def test_arm_bpim2u_gmac(self):
546        """
547        :avocado: tags=arch:arm
548        :avocado: tags=accel:tcg
549        :avocado: tags=machine:bpim2u
550        :avocado: tags=device:sd
551        """
552        self.require_netdev('user')
553
554        deb_url = ('https://apt.armbian.com/pool/main/l/'
555                   'linux-6.6.16/linux-image-current-sunxi_24.2.1_armhf__6.6.16-Seb3e-D6b4a-P2359-Ce96bHfe66-HK01ba-V014b-B067e-R448a.deb')
556        deb_hash = 'f7c3c8c5432f765445dc6e7eab02f3bbe668256b'
557        deb_path = self.fetch_asset(deb_url, asset_hash=deb_hash)
558        kernel_path = self.extract_from_deb(deb_path,
559                                            '/boot/vmlinuz-6.6.16-current-sunxi')
560        dtb_path = ('/usr/lib/linux-image-6.6.16-current-sunxi/'
561                    'sun8i-r40-bananapi-m2-ultra.dtb')
562        dtb_path = self.extract_from_deb(deb_path, dtb_path)
563        rootfs_url = ('http://storage.kernelci.org/images/rootfs/buildroot/'
564                      'buildroot-baseline/20221116.0/armel/rootfs.ext2.xz')
565        rootfs_hash = 'fae32f337c7b87547b10f42599acf109da8b6d9a'
566        rootfs_path_xz = self.fetch_asset(rootfs_url, asset_hash=rootfs_hash)
567        rootfs_path = os.path.join(self.workdir, 'rootfs.cpio')
568        archive.lzma_uncompress(rootfs_path_xz, rootfs_path)
569        image_pow2ceil_expand(rootfs_path)
570
571        self.vm.set_console()
572        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
573                               'console=ttyS0,115200 '
574                               'root=b300 rootwait rw '
575                               'panic=-1 noreboot')
576        self.vm.add_args('-kernel', kernel_path,
577                         '-dtb', dtb_path,
578                         '-drive', 'file=' + rootfs_path + ',if=sd,format=raw',
579                         '-net', 'nic,model=gmac,netdev=host_gmac',
580                         '-netdev', 'user,id=host_gmac',
581                         '-append', kernel_command_line,
582                         '-no-reboot')
583        self.vm.launch()
584        shell_ready = "/bin/sh: can't access tty; job control turned off"
585        self.wait_for_console_pattern(shell_ready)
586
587        exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo',
588                                                'Allwinner sun8i Family')
589        exec_command_and_wait_for_pattern(self, 'cat /proc/partitions',
590                                                'mmcblk')
591        exec_command_and_wait_for_pattern(self, 'ifconfig eth0 up',
592                                                 'eth0: Link is Up')
593        exec_command_and_wait_for_pattern(self, 'udhcpc eth0',
594            'udhcpc: lease of 10.0.2.15 obtained')
595        exec_command_and_wait_for_pattern(self, 'ping -c 3 10.0.2.2',
596            '3 packets transmitted, 3 packets received, 0% packet loss')
597        exec_command_and_wait_for_pattern(self, 'reboot',
598                                                'reboot: Restarting system')
599        # Wait for VM to shut down gracefully
600        self.vm.wait()
601
602    @skipUnless(os.getenv('AVOCADO_ALLOW_LARGE_STORAGE'), 'storage limited')
603    def test_arm_bpim2u_openwrt_22_03_3(self):
604        """
605        :avocado: tags=arch:arm
606        :avocado: tags=machine:bpim2u
607        :avocado: tags=device:sd
608        """
609
610        # This test download a 8.9 MiB compressed image and expand it
611        # to 127 MiB.
612        image_url = ('https://downloads.openwrt.org/releases/22.03.3/targets/'
613                     'sunxi/cortexa7/openwrt-22.03.3-sunxi-cortexa7-'
614                     'sinovoip_bananapi-m2-ultra-ext4-sdcard.img.gz')
615        image_hash = ('5b41b4e11423e562c6011640f9a7cd3b'
616                      'dd0a3d42b83430f7caa70a432e6cd82c')
617        image_path_gz = self.fetch_asset(image_url, asset_hash=image_hash,
618                                         algorithm='sha256')
619        image_path = archive.extract(image_path_gz, self.workdir)
620        image_pow2ceil_expand(image_path)
621
622        self.vm.set_console()
623        self.vm.add_args('-drive', 'file=' + image_path + ',if=sd,format=raw',
624                         '-nic', 'user',
625                         '-no-reboot')
626        self.vm.launch()
627
628        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
629                               'usbcore.nousb '
630                               'noreboot')
631
632        self.wait_for_console_pattern('U-Boot SPL')
633
634        interrupt_interactive_console_until_pattern(
635                self, 'Hit any key to stop autoboot:', '=>')
636        exec_command_and_wait_for_pattern(self, "setenv extraargs '" +
637                                                kernel_command_line + "'", '=>')
638        exec_command_and_wait_for_pattern(self, 'boot', 'Starting kernel ...');
639
640        self.wait_for_console_pattern(
641            'Please press Enter to activate this console.')
642
643        exec_command_and_wait_for_pattern(self, ' ', 'root@')
644
645        exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo',
646                                                'Allwinner sun8i Family')
647        exec_command_and_wait_for_pattern(self, 'cat /proc/iomem',
648                                                'system-control@1c00000')
649
650    def test_arm_orangepi(self):
651        """
652        :avocado: tags=arch:arm
653        :avocado: tags=machine:orangepi-pc
654        :avocado: tags=accel:tcg
655        """
656        deb_url = ('https://apt.armbian.com/pool/main/l/'
657                   'linux-6.6.16/linux-image-current-sunxi_24.2.1_armhf__6.6.16-Seb3e-D6b4a-P2359-Ce96bHfe66-HK01ba-V014b-B067e-R448a.deb')
658        deb_hash = 'f7c3c8c5432f765445dc6e7eab02f3bbe668256b'
659        deb_path = self.fetch_asset(deb_url, asset_hash=deb_hash)
660        kernel_path = self.extract_from_deb(deb_path,
661                                            '/boot/vmlinuz-6.6.16-current-sunxi')
662        dtb_path = '/usr/lib/linux-image-6.6.16-current-sunxi/sun8i-h3-orangepi-pc.dtb'
663        dtb_path = self.extract_from_deb(deb_path, dtb_path)
664
665        self.vm.set_console()
666        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
667                               'console=ttyS0,115200n8 '
668                               'earlycon=uart,mmio32,0x1c28000')
669        self.vm.add_args('-kernel', kernel_path,
670                         '-dtb', dtb_path,
671                         '-append', kernel_command_line)
672        self.vm.launch()
673        console_pattern = 'Kernel command line: %s' % kernel_command_line
674        self.wait_for_console_pattern(console_pattern)
675
676    def test_arm_orangepi_initrd(self):
677        """
678        :avocado: tags=arch:arm
679        :avocado: tags=accel:tcg
680        :avocado: tags=machine:orangepi-pc
681        """
682        deb_url = ('https://apt.armbian.com/pool/main/l/'
683                   'linux-6.6.16/linux-image-current-sunxi_24.2.1_armhf__6.6.16-Seb3e-D6b4a-P2359-Ce96bHfe66-HK01ba-V014b-B067e-R448a.deb')
684        deb_hash = 'f7c3c8c5432f765445dc6e7eab02f3bbe668256b'
685        deb_path = self.fetch_asset(deb_url, asset_hash=deb_hash)
686        kernel_path = self.extract_from_deb(deb_path,
687                                            '/boot/vmlinuz-6.6.16-current-sunxi')
688        dtb_path = '/usr/lib/linux-image-6.6.16-current-sunxi/sun8i-h3-orangepi-pc.dtb'
689        dtb_path = self.extract_from_deb(deb_path, dtb_path)
690        initrd_url = ('https://github.com/groeck/linux-build-test/raw/'
691                      '2eb0a73b5d5a28df3170c546ddaaa9757e1e0848/rootfs/'
692                      'arm/rootfs-armv7a.cpio.gz')
693        initrd_hash = '604b2e45cdf35045846b8bbfbf2129b1891bdc9c'
694        initrd_path_gz = self.fetch_asset(initrd_url, asset_hash=initrd_hash)
695        initrd_path = os.path.join(self.workdir, 'rootfs.cpio')
696        archive.gzip_uncompress(initrd_path_gz, initrd_path)
697
698        self.vm.set_console()
699        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
700                               'console=ttyS0,115200 '
701                               'panic=-1 noreboot')
702        self.vm.add_args('-kernel', kernel_path,
703                         '-dtb', dtb_path,
704                         '-initrd', initrd_path,
705                         '-append', kernel_command_line,
706                         '-no-reboot')
707        self.vm.launch()
708        self.wait_for_console_pattern('Boot successful.')
709
710        exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo',
711                                                'Allwinner sun8i Family')
712        exec_command_and_wait_for_pattern(self, 'cat /proc/iomem',
713                                                'system-control@1c00000')
714        exec_command_and_wait_for_pattern(self, 'reboot',
715                                                'reboot: Restarting system')
716        # Wait for VM to shut down gracefully
717        self.vm.wait()
718
719    def test_arm_orangepi_sd(self):
720        """
721        :avocado: tags=arch:arm
722        :avocado: tags=accel:tcg
723        :avocado: tags=machine:orangepi-pc
724        :avocado: tags=device:sd
725        """
726        self.require_netdev('user')
727
728        deb_url = ('https://apt.armbian.com/pool/main/l/'
729                   'linux-6.6.16/linux-image-current-sunxi_24.2.1_armhf__6.6.16-Seb3e-D6b4a-P2359-Ce96bHfe66-HK01ba-V014b-B067e-R448a.deb')
730        deb_hash = 'f7c3c8c5432f765445dc6e7eab02f3bbe668256b'
731        deb_path = self.fetch_asset(deb_url, asset_hash=deb_hash)
732        kernel_path = self.extract_from_deb(deb_path,
733                                            '/boot/vmlinuz-6.6.16-current-sunxi')
734        dtb_path = '/usr/lib/linux-image-6.6.16-current-sunxi/sun8i-h3-orangepi-pc.dtb'
735        dtb_path = self.extract_from_deb(deb_path, dtb_path)
736        rootfs_url = ('http://storage.kernelci.org/images/rootfs/buildroot/'
737                      'buildroot-baseline/20221116.0/armel/rootfs.ext2.xz')
738        rootfs_hash = 'fae32f337c7b87547b10f42599acf109da8b6d9a'
739        rootfs_path_xz = self.fetch_asset(rootfs_url, asset_hash=rootfs_hash)
740        rootfs_path = os.path.join(self.workdir, 'rootfs.cpio')
741        archive.lzma_uncompress(rootfs_path_xz, rootfs_path)
742        image_pow2ceil_expand(rootfs_path)
743
744        self.vm.set_console()
745        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
746                               'console=ttyS0,115200 '
747                               'root=/dev/mmcblk0 rootwait rw '
748                               'panic=-1 noreboot')
749        self.vm.add_args('-kernel', kernel_path,
750                         '-dtb', dtb_path,
751                         '-drive', 'file=' + rootfs_path + ',if=sd,format=raw',
752                         '-append', kernel_command_line,
753                         '-no-reboot')
754        self.vm.launch()
755        shell_ready = "/bin/sh: can't access tty; job control turned off"
756        self.wait_for_console_pattern(shell_ready)
757
758        exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo',
759                                                'Allwinner sun8i Family')
760        exec_command_and_wait_for_pattern(self, 'cat /proc/partitions',
761                                                'mmcblk0')
762        exec_command_and_wait_for_pattern(self, 'ifconfig eth0 up',
763                                                 'eth0: Link is Up')
764        exec_command_and_wait_for_pattern(self, 'udhcpc eth0',
765            'udhcpc: lease of 10.0.2.15 obtained')
766        exec_command_and_wait_for_pattern(self, 'ping -c 3 10.0.2.2',
767            '3 packets transmitted, 3 packets received, 0% packet loss')
768        exec_command_and_wait_for_pattern(self, 'reboot',
769                                                'reboot: Restarting system')
770        # Wait for VM to shut down gracefully
771        self.vm.wait()
772
773    @skipUnless(os.getenv('AVOCADO_ALLOW_LARGE_STORAGE'), 'storage limited')
774    def test_arm_orangepi_bionic_20_08(self):
775        """
776        :avocado: tags=arch:arm
777        :avocado: tags=machine:orangepi-pc
778        :avocado: tags=device:sd
779        """
780
781        # This test download a 275 MiB compressed image and expand it
782        # to 1036 MiB, but the underlying filesystem is 1552 MiB...
783        # As we expand it to 2 GiB we are safe.
784
785        image_url = ('https://archive.armbian.com/orangepipc/archive/'
786                     'Armbian_20.08.1_Orangepipc_bionic_current_5.8.5.img.xz')
787        image_hash = ('b4d6775f5673486329e45a0586bf06b6'
788                      'dbe792199fd182ac6b9c7bb6c7d3e6dd')
789        image_path_xz = self.fetch_asset(image_url, asset_hash=image_hash,
790                                         algorithm='sha256')
791        image_path = archive.extract(image_path_xz, self.workdir)
792        image_pow2ceil_expand(image_path)
793
794        self.vm.set_console()
795        self.vm.add_args('-drive', 'file=' + image_path + ',if=sd,format=raw',
796                         '-nic', 'user',
797                         '-no-reboot')
798        self.vm.launch()
799
800        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
801                               'console=ttyS0,115200 '
802                               'loglevel=7 '
803                               'nosmp '
804                               'systemd.default_timeout_start_sec=9000 '
805                               'systemd.mask=armbian-zram-config.service '
806                               'systemd.mask=armbian-ramlog.service')
807
808        self.wait_for_console_pattern('U-Boot SPL')
809        self.wait_for_console_pattern('Autoboot in ')
810        exec_command_and_wait_for_pattern(self, ' ', '=>')
811        exec_command_and_wait_for_pattern(self, "setenv extraargs '" +
812                                                kernel_command_line + "'", '=>')
813        exec_command_and_wait_for_pattern(self, 'boot', 'Starting kernel ...');
814
815        self.wait_for_console_pattern('systemd[1]: Set hostname ' +
816                                      'to <orangepipc>')
817        self.wait_for_console_pattern('Starting Load Kernel Modules...')
818
819    @skipUnless(os.getenv('AVOCADO_ALLOW_LARGE_STORAGE'), 'storage limited')
820    def test_arm_orangepi_uboot_netbsd9(self):
821        """
822        :avocado: tags=arch:arm
823        :avocado: tags=machine:orangepi-pc
824        :avocado: tags=device:sd
825        :avocado: tags=os:netbsd
826        """
827        # This test download a 304MB compressed image and expand it to 2GB
828        deb_url = ('http://snapshot.debian.org/archive/debian/'
829                   '20200108T145233Z/pool/main/u/u-boot/'
830                   'u-boot-sunxi_2020.01%2Bdfsg-1_armhf.deb')
831        deb_hash = 'f67f404a80753ca3d1258f13e38f2b060e13db99'
832        deb_path = self.fetch_asset(deb_url, asset_hash=deb_hash)
833        # We use the common OrangePi PC 'plus' build of U-Boot for our secondary
834        # program loader (SPL). We will then set the path to the more specific
835        # OrangePi "PC" device tree blob with 'setenv fdtfile' in U-Boot prompt,
836        # before to boot NetBSD.
837        uboot_path = '/usr/lib/u-boot/orangepi_plus/u-boot-sunxi-with-spl.bin'
838        uboot_path = self.extract_from_deb(deb_path, uboot_path)
839        image_url = ('https://cdn.netbsd.org/pub/NetBSD/NetBSD-9.0/'
840                     'evbarm-earmv7hf/binary/gzimg/armv7.img.gz')
841        image_hash = '2babb29d36d8360adcb39c09e31060945259917a'
842        image_path_gz = self.fetch_asset(image_url, asset_hash=image_hash)
843        image_path = os.path.join(self.workdir, 'armv7.img')
844        archive.gzip_uncompress(image_path_gz, image_path)
845        image_pow2ceil_expand(image_path)
846        image_drive_args = 'if=sd,format=raw,snapshot=on,file=' + image_path
847
848        # dd if=u-boot-sunxi-with-spl.bin of=armv7.img bs=1K seek=8 conv=notrunc
849        with open(uboot_path, 'rb') as f_in:
850            with open(image_path, 'r+b') as f_out:
851                f_out.seek(8 * 1024)
852                shutil.copyfileobj(f_in, f_out)
853
854        self.vm.set_console()
855        self.vm.add_args('-nic', 'user',
856                         '-drive', image_drive_args,
857                         '-global', 'allwinner-rtc.base-year=2000',
858                         '-no-reboot')
859        self.vm.launch()
860        wait_for_console_pattern(self, 'U-Boot 2020.01+dfsg-1')
861        interrupt_interactive_console_until_pattern(self,
862                                       'Hit any key to stop autoboot:',
863                                       'switch to partitions #0, OK')
864
865        exec_command_and_wait_for_pattern(self, '', '=>')
866        cmd = 'setenv bootargs root=ld0a'
867        exec_command_and_wait_for_pattern(self, cmd, '=>')
868        cmd = 'setenv kernel netbsd-GENERIC.ub'
869        exec_command_and_wait_for_pattern(self, cmd, '=>')
870        cmd = 'setenv fdtfile dtb/sun8i-h3-orangepi-pc.dtb'
871        exec_command_and_wait_for_pattern(self, cmd, '=>')
872        cmd = ("setenv bootcmd 'fatload mmc 0:1 ${kernel_addr_r} ${kernel}; "
873               "fatload mmc 0:1 ${fdt_addr_r} ${fdtfile}; "
874               "fdt addr ${fdt_addr_r}; "
875               "bootm ${kernel_addr_r} - ${fdt_addr_r}'")
876        exec_command_and_wait_for_pattern(self, cmd, '=>')
877
878        exec_command_and_wait_for_pattern(self, 'boot',
879                                          'Booting kernel from Legacy Image')
880        wait_for_console_pattern(self, 'Starting kernel ...')
881        wait_for_console_pattern(self, 'NetBSD 9.0 (GENERIC)')
882        # Wait for user-space
883        wait_for_console_pattern(self, 'Starting root file system check')
884
885    def test_arm_ast2600_debian(self):
886        """
887        :avocado: tags=arch:arm
888        :avocado: tags=machine:rainier-bmc
889        """
890        deb_url = ('http://snapshot.debian.org/archive/debian/'
891                   '20220606T211338Z/'
892                   'pool/main/l/linux/'
893                   'linux-image-5.17.0-2-armmp_5.17.6-1%2Bb1_armhf.deb')
894        deb_hash = '8acb2b4439faedc2f3ed4bdb2847ad4f6e0491f73debaeb7f660c8abe4dcdc0e'
895        deb_path = self.fetch_asset(deb_url, asset_hash=deb_hash,
896                                    algorithm='sha256')
897        kernel_path = self.extract_from_deb(deb_path, '/boot/vmlinuz-5.17.0-2-armmp')
898        dtb_path = self.extract_from_deb(deb_path,
899                '/usr/lib/linux-image-5.17.0-2-armmp/aspeed-bmc-ibm-rainier.dtb')
900
901        self.vm.set_console()
902        self.vm.add_args('-kernel', kernel_path,
903                         '-dtb', dtb_path,
904                         '-net', 'nic')
905        self.vm.launch()
906        self.wait_for_console_pattern("Booting Linux on physical CPU 0xf00")
907        self.wait_for_console_pattern("SMP: Total of 2 processors activated")
908        self.wait_for_console_pattern("No filesystem could mount root")
909
910