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 skipUnless
17from avocado_qemu import Test
18from avocado_qemu import exec_command_and_wait_for_pattern
19from avocado_qemu import interrupt_interactive_console_until_pattern
20from avocado_qemu import wait_for_console_pattern
21from avocado.utils import process
22from avocado.utils import archive
23from avocado.utils.path import find_command, CmdNotFoundError
24
25P7ZIP_AVAILABLE = True
26try:
27    find_command('7z')
28except CmdNotFoundError:
29    P7ZIP_AVAILABLE = False
30
31"""
32Round up to next power of 2
33"""
34def pow2ceil(x):
35    return 1 if x == 0 else 2**(x - 1).bit_length()
36
37"""
38Expand file size to next power of 2
39"""
40def image_pow2ceil_expand(path):
41        size = os.path.getsize(path)
42        size_aligned = pow2ceil(size)
43        if size != size_aligned:
44            with open(path, 'ab+') as fd:
45                fd.truncate(size_aligned)
46
47class LinuxKernelTest(Test):
48    KERNEL_COMMON_COMMAND_LINE = 'printk.time=0 '
49
50    def wait_for_console_pattern(self, success_message, vm=None):
51        wait_for_console_pattern(self, success_message,
52                                 failure_message='Kernel panic - not syncing',
53                                 vm=vm)
54
55    def extract_from_deb(self, deb, path):
56        """
57        Extracts a file from a deb package into the test workdir
58
59        :param deb: path to the deb archive
60        :param path: path within the deb archive of the file to be extracted
61        :returns: path of the extracted file
62        """
63        cwd = os.getcwd()
64        os.chdir(self.workdir)
65        file_path = process.run("ar t %s" % deb).stdout_text.split()[2]
66        process.run("ar x %s %s" % (deb, file_path))
67        archive.extract(file_path, self.workdir)
68        os.chdir(cwd)
69        # Return complete path to extracted file.  Because callers to
70        # extract_from_deb() specify 'path' with a leading slash, it is
71        # necessary to use os.path.relpath() as otherwise os.path.join()
72        # interprets it as an absolute path and drops the self.workdir part.
73        return os.path.normpath(os.path.join(self.workdir,
74                                             os.path.relpath(path, '/')))
75
76    def extract_from_rpm(self, rpm, path):
77        """
78        Extracts a file from an RPM package into the test workdir.
79
80        :param rpm: path to the rpm archive
81        :param path: path within the rpm archive of the file to be extracted
82                     needs to be a relative path (starting with './') because
83                     cpio(1), which is used to extract the file, expects that.
84        :returns: path of the extracted file
85        """
86        cwd = os.getcwd()
87        os.chdir(self.workdir)
88        process.run("rpm2cpio %s | cpio -id %s" % (rpm, path), shell=True)
89        os.chdir(cwd)
90        return os.path.normpath(os.path.join(self.workdir, path))
91
92class BootLinuxConsole(LinuxKernelTest):
93    """
94    Boots a Linux kernel and checks that the console is operational and the
95    kernel command line is properly passed from QEMU to the kernel
96    """
97    timeout = 90
98
99    def test_x86_64_pc(self):
100        """
101        :avocado: tags=arch:x86_64
102        :avocado: tags=machine:pc
103        """
104        kernel_url = ('https://archives.fedoraproject.org/pub/archive/fedora'
105                      '/linux/releases/29/Everything/x86_64/os/images/pxeboot'
106                      '/vmlinuz')
107        kernel_hash = '23bebd2680757891cf7adedb033532163a792495'
108        kernel_path = self.fetch_asset(kernel_url, asset_hash=kernel_hash)
109
110        self.vm.set_console()
111        kernel_command_line = self.KERNEL_COMMON_COMMAND_LINE + 'console=ttyS0'
112        self.vm.add_args('-kernel', kernel_path,
113                         '-append', kernel_command_line)
114        self.vm.launch()
115        console_pattern = 'Kernel command line: %s' % kernel_command_line
116        self.wait_for_console_pattern(console_pattern)
117
118    def test_mips_malta(self):
119        """
120        :avocado: tags=arch:mips
121        :avocado: tags=machine:malta
122        :avocado: tags=endian:big
123        """
124        deb_url = ('http://snapshot.debian.org/archive/debian/'
125                   '20130217T032700Z/pool/main/l/linux-2.6/'
126                   'linux-image-2.6.32-5-4kc-malta_2.6.32-48_mips.deb')
127        deb_hash = 'a8cfc28ad8f45f54811fc6cf74fc43ffcfe0ba04'
128        deb_path = self.fetch_asset(deb_url, asset_hash=deb_hash)
129        kernel_path = self.extract_from_deb(deb_path,
130                                            '/boot/vmlinux-2.6.32-5-4kc-malta')
131
132        self.vm.set_console()
133        kernel_command_line = self.KERNEL_COMMON_COMMAND_LINE + 'console=ttyS0'
134        self.vm.add_args('-kernel', kernel_path,
135                         '-append', kernel_command_line)
136        self.vm.launch()
137        console_pattern = 'Kernel command line: %s' % kernel_command_line
138        self.wait_for_console_pattern(console_pattern)
139
140    def test_mips64el_malta(self):
141        """
142        This test requires the ar tool to extract "data.tar.gz" from
143        the Debian package.
144
145        The kernel can be rebuilt using this Debian kernel source [1] and
146        following the instructions on [2].
147
148        [1] http://snapshot.debian.org/package/linux-2.6/2.6.32-48/
149            #linux-source-2.6.32_2.6.32-48
150        [2] https://kernel-team.pages.debian.net/kernel-handbook/
151            ch-common-tasks.html#s-common-official
152
153        :avocado: tags=arch:mips64el
154        :avocado: tags=machine:malta
155        """
156        deb_url = ('http://snapshot.debian.org/archive/debian/'
157                   '20130217T032700Z/pool/main/l/linux-2.6/'
158                   'linux-image-2.6.32-5-5kc-malta_2.6.32-48_mipsel.deb')
159        deb_hash = '1aaec92083bf22fda31e0d27fa8d9a388e5fc3d5'
160        deb_path = self.fetch_asset(deb_url, asset_hash=deb_hash)
161        kernel_path = self.extract_from_deb(deb_path,
162                                            '/boot/vmlinux-2.6.32-5-5kc-malta')
163
164        self.vm.set_console()
165        kernel_command_line = self.KERNEL_COMMON_COMMAND_LINE + 'console=ttyS0'
166        self.vm.add_args('-kernel', kernel_path,
167                         '-append', kernel_command_line)
168        self.vm.launch()
169        console_pattern = 'Kernel command line: %s' % kernel_command_line
170        self.wait_for_console_pattern(console_pattern)
171
172    def test_mips_malta_cpio(self):
173        """
174        :avocado: tags=arch:mips
175        :avocado: tags=machine:malta
176        :avocado: tags=endian:big
177        """
178        deb_url = ('http://snapshot.debian.org/archive/debian/'
179                   '20160601T041800Z/pool/main/l/linux/'
180                   'linux-image-4.5.0-2-4kc-malta_4.5.5-1_mips.deb')
181        deb_hash = 'a3c84f3e88b54e06107d65a410d1d1e8e0f340f8'
182        deb_path = self.fetch_asset(deb_url, asset_hash=deb_hash)
183        kernel_path = self.extract_from_deb(deb_path,
184                                            '/boot/vmlinux-4.5.0-2-4kc-malta')
185        initrd_url = ('https://github.com/groeck/linux-build-test/raw/'
186                      '8584a59ed9e5eb5ee7ca91f6d74bbb06619205b8/rootfs/'
187                      'mips/rootfs.cpio.gz')
188        initrd_hash = 'bf806e17009360a866bf537f6de66590de349a99'
189        initrd_path_gz = self.fetch_asset(initrd_url, asset_hash=initrd_hash)
190        initrd_path = self.workdir + "rootfs.cpio"
191        archive.gzip_uncompress(initrd_path_gz, initrd_path)
192
193        self.vm.set_console()
194        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE
195                               + 'console=ttyS0 console=tty '
196                               + 'rdinit=/sbin/init noreboot')
197        self.vm.add_args('-kernel', kernel_path,
198                         '-initrd', initrd_path,
199                         '-append', kernel_command_line,
200                         '-no-reboot')
201        self.vm.launch()
202        self.wait_for_console_pattern('Boot successful.')
203
204        exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo',
205                                                'BogoMIPS')
206        exec_command_and_wait_for_pattern(self, 'uname -a',
207                                                'Debian')
208        exec_command_and_wait_for_pattern(self, 'reboot',
209                                                'reboot: Restarting system')
210
211    @skipUnless(os.getenv('AVOCADO_ALLOW_UNTRUSTED_CODE'), 'untrusted code')
212    def test_mips64el_malta_5KEc_cpio(self):
213        """
214        :avocado: tags=arch:mips64el
215        :avocado: tags=machine:malta
216        :avocado: tags=endian:little
217        """
218        kernel_url = ('https://github.com/philmd/qemu-testing-blob/'
219                      'raw/9ad2df38/mips/malta/mips64el/'
220                      'vmlinux-3.19.3.mtoman.20150408')
221        kernel_hash = '00d1d268fb9f7d8beda1de6bebcc46e884d71754'
222        kernel_path = self.fetch_asset(kernel_url, asset_hash=kernel_hash)
223        initrd_url = ('https://github.com/groeck/linux-build-test/'
224                      'raw/8584a59e/rootfs/'
225                      'mipsel64/rootfs.mipsel64r1.cpio.gz')
226        initrd_hash = '1dbb8a396e916847325284dbe2151167'
227        initrd_path_gz = self.fetch_asset(initrd_url, algorithm='md5',
228                                          asset_hash=initrd_hash)
229        initrd_path = self.workdir + "rootfs.cpio"
230        archive.gzip_uncompress(initrd_path_gz, initrd_path)
231
232        self.vm.set_console()
233        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE
234                               + 'console=ttyS0 console=tty '
235                               + 'rdinit=/sbin/init noreboot')
236        self.vm.add_args('-cpu', '5KEc',
237                         '-kernel', kernel_path,
238                         '-initrd', initrd_path,
239                         '-append', kernel_command_line,
240                         '-no-reboot')
241        self.vm.launch()
242        wait_for_console_pattern(self, 'Boot successful.')
243
244        exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo',
245                                                'MIPS 5KE')
246        exec_command_and_wait_for_pattern(self, 'uname -a',
247                                                '3.19.3.mtoman.20150408')
248        exec_command_and_wait_for_pattern(self, 'reboot',
249                                                'reboot: Restarting system')
250
251    def do_test_mips_malta32el_nanomips(self, kernel_url, kernel_hash):
252        kernel_path_xz = self.fetch_asset(kernel_url, asset_hash=kernel_hash)
253        kernel_path = self.workdir + "kernel"
254        with lzma.open(kernel_path_xz, 'rb') as f_in:
255            with open(kernel_path, 'wb') as f_out:
256                shutil.copyfileobj(f_in, f_out)
257
258        self.vm.set_console()
259        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE
260                               + 'mem=256m@@0x0 '
261                               + 'console=ttyS0')
262        self.vm.add_args('-no-reboot',
263                         '-cpu', 'I7200',
264                         '-kernel', kernel_path,
265                         '-append', kernel_command_line)
266        self.vm.launch()
267        console_pattern = 'Kernel command line: %s' % kernel_command_line
268        self.wait_for_console_pattern(console_pattern)
269
270    def test_mips_malta32el_nanomips_4k(self):
271        """
272        :avocado: tags=arch:mipsel
273        :avocado: tags=machine:malta
274        :avocado: tags=endian:little
275        """
276        kernel_url = ('https://mipsdistros.mips.com/LinuxDistro/nanomips/'
277                      'kernels/v4.15.18-432-gb2eb9a8b07a1-20180627102142/'
278                      'generic_nano32r6el_page4k.xz')
279        kernel_hash = '477456aafd2a0f1ddc9482727f20fe9575565dd6'
280        self.do_test_mips_malta32el_nanomips(kernel_url, kernel_hash)
281
282    def test_mips_malta32el_nanomips_16k_up(self):
283        """
284        :avocado: tags=arch:mipsel
285        :avocado: tags=machine:malta
286        :avocado: tags=endian:little
287        """
288        kernel_url = ('https://mipsdistros.mips.com/LinuxDistro/nanomips/'
289                      'kernels/v4.15.18-432-gb2eb9a8b07a1-20180627102142/'
290                      'generic_nano32r6el_page16k_up.xz')
291        kernel_hash = 'e882868f944c71c816e832e2303b7874d044a7bc'
292        self.do_test_mips_malta32el_nanomips(kernel_url, kernel_hash)
293
294    def test_mips_malta32el_nanomips_64k_dbg(self):
295        """
296        :avocado: tags=arch:mipsel
297        :avocado: tags=machine:malta
298        :avocado: tags=endian:little
299        """
300        kernel_url = ('https://mipsdistros.mips.com/LinuxDistro/nanomips/'
301                      'kernels/v4.15.18-432-gb2eb9a8b07a1-20180627102142/'
302                      'generic_nano32r6el_page64k_dbg.xz')
303        kernel_hash = '18d1c68f2e23429e266ca39ba5349ccd0aeb7180'
304        self.do_test_mips_malta32el_nanomips(kernel_url, kernel_hash)
305
306    def test_aarch64_virt(self):
307        """
308        :avocado: tags=arch:aarch64
309        :avocado: tags=machine:virt
310        """
311        kernel_url = ('https://archives.fedoraproject.org/pub/archive/fedora'
312                      '/linux/releases/29/Everything/aarch64/os/images/pxeboot'
313                      '/vmlinuz')
314        kernel_hash = '8c73e469fc6ea06a58dc83a628fc695b693b8493'
315        kernel_path = self.fetch_asset(kernel_url, asset_hash=kernel_hash)
316
317        self.vm.set_console()
318        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
319                               'console=ttyAMA0')
320        self.vm.add_args('-cpu', 'cortex-a53',
321                         '-kernel', kernel_path,
322                         '-append', kernel_command_line)
323        self.vm.launch()
324        console_pattern = 'Kernel command line: %s' % kernel_command_line
325        self.wait_for_console_pattern(console_pattern)
326
327    def test_arm_virt(self):
328        """
329        :avocado: tags=arch:arm
330        :avocado: tags=machine:virt
331        """
332        kernel_url = ('https://archives.fedoraproject.org/pub/archive/fedora'
333                      '/linux/releases/29/Everything/armhfp/os/images/pxeboot'
334                      '/vmlinuz')
335        kernel_hash = 'e9826d741b4fb04cadba8d4824d1ed3b7fb8b4d4'
336        kernel_path = self.fetch_asset(kernel_url, asset_hash=kernel_hash)
337
338        self.vm.set_console()
339        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
340                               'console=ttyAMA0')
341        self.vm.add_args('-kernel', kernel_path,
342                         '-append', kernel_command_line)
343        self.vm.launch()
344        console_pattern = 'Kernel command line: %s' % kernel_command_line
345        self.wait_for_console_pattern(console_pattern)
346
347    def test_arm_emcraft_sf2(self):
348        """
349        :avocado: tags=arch:arm
350        :avocado: tags=machine:emcraft-sf2
351        :avocado: tags=endian:little
352        :avocado: tags=u-boot
353        """
354        uboot_url = ('https://raw.githubusercontent.com/'
355                     'Subbaraya-Sundeep/qemu-test-binaries/'
356                     'fa030bd77a014a0b8e360d3b7011df89283a2f0b/u-boot')
357        uboot_hash = 'abba5d9c24cdd2d49cdc2a8aa92976cf20737eff'
358        uboot_path = self.fetch_asset(uboot_url, asset_hash=uboot_hash)
359        spi_url = ('https://raw.githubusercontent.com/'
360                   'Subbaraya-Sundeep/qemu-test-binaries/'
361                   'fa030bd77a014a0b8e360d3b7011df89283a2f0b/spi.bin')
362        spi_hash = '85f698329d38de63aea6e884a86fbde70890a78a'
363        spi_path = self.fetch_asset(spi_url, asset_hash=spi_hash)
364
365        self.vm.set_console()
366        kernel_command_line = self.KERNEL_COMMON_COMMAND_LINE
367        self.vm.add_args('-kernel', uboot_path,
368                         '-append', kernel_command_line,
369                         '-drive', 'file=' + spi_path + ',if=mtd,format=raw',
370                         '-no-reboot')
371        self.vm.launch()
372        self.wait_for_console_pattern('init started: BusyBox')
373
374    def do_test_arm_raspi2(self, uart_id):
375        """
376        The kernel can be rebuilt using the kernel source referenced
377        and following the instructions on the on:
378        https://www.raspberrypi.org/documentation/linux/kernel/building.md
379        """
380        serial_kernel_cmdline = {
381            0: 'earlycon=pl011,0x3f201000 console=ttyAMA0',
382        }
383        deb_url = ('http://archive.raspberrypi.org/debian/'
384                   'pool/main/r/raspberrypi-firmware/'
385                   'raspberrypi-kernel_1.20190215-1_armhf.deb')
386        deb_hash = 'cd284220b32128c5084037553db3c482426f3972'
387        deb_path = self.fetch_asset(deb_url, asset_hash=deb_hash)
388        kernel_path = self.extract_from_deb(deb_path, '/boot/kernel7.img')
389        dtb_path = self.extract_from_deb(deb_path, '/boot/bcm2709-rpi-2-b.dtb')
390
391        self.vm.set_console()
392        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
393                               serial_kernel_cmdline[uart_id])
394        self.vm.add_args('-kernel', kernel_path,
395                         '-dtb', dtb_path,
396                         '-append', kernel_command_line)
397        self.vm.launch()
398        console_pattern = 'Kernel command line: %s' % kernel_command_line
399        self.wait_for_console_pattern(console_pattern)
400
401    def test_arm_raspi2_uart0(self):
402        """
403        :avocado: tags=arch:arm
404        :avocado: tags=machine:raspi2
405        :avocado: tags=device:pl011
406        """
407        self.do_test_arm_raspi2(0)
408
409    def test_arm_exynos4210_initrd(self):
410        """
411        :avocado: tags=arch:arm
412        :avocado: tags=machine:smdkc210
413        """
414        deb_url = ('https://snapshot.debian.org/archive/debian/'
415                   '20190928T224601Z/pool/main/l/linux/'
416                   'linux-image-4.19.0-6-armmp_4.19.67-2+deb10u1_armhf.deb')
417        deb_hash = 'fa9df4a0d38936cb50084838f2cb933f570d7d82'
418        deb_path = self.fetch_asset(deb_url, asset_hash=deb_hash)
419        kernel_path = self.extract_from_deb(deb_path,
420                                            '/boot/vmlinuz-4.19.0-6-armmp')
421        dtb_path = '/usr/lib/linux-image-4.19.0-6-armmp/exynos4210-smdkv310.dtb'
422        dtb_path = self.extract_from_deb(deb_path, dtb_path)
423
424        initrd_url = ('https://github.com/groeck/linux-build-test/raw/'
425                      '2eb0a73b5d5a28df3170c546ddaaa9757e1e0848/rootfs/'
426                      'arm/rootfs-armv5.cpio.gz')
427        initrd_hash = '2b50f1873e113523967806f4da2afe385462ff9b'
428        initrd_path_gz = self.fetch_asset(initrd_url, asset_hash=initrd_hash)
429        initrd_path = os.path.join(self.workdir, 'rootfs.cpio')
430        archive.gzip_uncompress(initrd_path_gz, initrd_path)
431
432        self.vm.set_console()
433        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
434                               'earlycon=exynos4210,0x13800000 earlyprintk ' +
435                               'console=ttySAC0,115200n8 ' +
436                               'random.trust_cpu=off cryptomgr.notests ' +
437                               'cpuidle.off=1 panic=-1 noreboot')
438
439        self.vm.add_args('-kernel', kernel_path,
440                         '-dtb', dtb_path,
441                         '-initrd', initrd_path,
442                         '-append', kernel_command_line,
443                         '-no-reboot')
444        self.vm.launch()
445
446        self.wait_for_console_pattern('Boot successful.')
447        # TODO user command, for now the uart is stuck
448
449    def test_arm_cubieboard_initrd(self):
450        """
451        :avocado: tags=arch:arm
452        :avocado: tags=machine:cubieboard
453        """
454        deb_url = ('https://apt.armbian.com/pool/main/l/'
455                   'linux-4.20.7-sunxi/linux-image-dev-sunxi_5.75_armhf.deb')
456        deb_hash = '1334c29c44d984ffa05ed10de8c3361f33d78315'
457        deb_path = self.fetch_asset(deb_url, asset_hash=deb_hash)
458        kernel_path = self.extract_from_deb(deb_path,
459                                            '/boot/vmlinuz-4.20.7-sunxi')
460        dtb_path = '/usr/lib/linux-image-dev-sunxi/sun4i-a10-cubieboard.dtb'
461        dtb_path = self.extract_from_deb(deb_path, dtb_path)
462        initrd_url = ('https://github.com/groeck/linux-build-test/raw/'
463                      '2eb0a73b5d5a28df3170c546ddaaa9757e1e0848/rootfs/'
464                      'arm/rootfs-armv5.cpio.gz')
465        initrd_hash = '2b50f1873e113523967806f4da2afe385462ff9b'
466        initrd_path_gz = self.fetch_asset(initrd_url, asset_hash=initrd_hash)
467        initrd_path = os.path.join(self.workdir, 'rootfs.cpio')
468        archive.gzip_uncompress(initrd_path_gz, initrd_path)
469
470        self.vm.set_console()
471        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
472                               'console=ttyS0,115200 '
473                               'usbcore.nousb '
474                               'panic=-1 noreboot')
475        self.vm.add_args('-kernel', kernel_path,
476                         '-dtb', dtb_path,
477                         '-initrd', initrd_path,
478                         '-append', kernel_command_line,
479                         '-no-reboot')
480        self.vm.launch()
481        self.wait_for_console_pattern('Boot successful.')
482
483        exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo',
484                                                'Allwinner sun4i/sun5i')
485        exec_command_and_wait_for_pattern(self, 'cat /proc/iomem',
486                                                'system-control@1c00000')
487        exec_command_and_wait_for_pattern(self, 'reboot',
488                                                'reboot: Restarting system')
489
490    def test_arm_cubieboard_sata(self):
491        """
492        :avocado: tags=arch:arm
493        :avocado: tags=machine:cubieboard
494        """
495        deb_url = ('https://apt.armbian.com/pool/main/l/'
496                   'linux-4.20.7-sunxi/linux-image-dev-sunxi_5.75_armhf.deb')
497        deb_hash = '1334c29c44d984ffa05ed10de8c3361f33d78315'
498        deb_path = self.fetch_asset(deb_url, asset_hash=deb_hash)
499        kernel_path = self.extract_from_deb(deb_path,
500                                            '/boot/vmlinuz-4.20.7-sunxi')
501        dtb_path = '/usr/lib/linux-image-dev-sunxi/sun4i-a10-cubieboard.dtb'
502        dtb_path = self.extract_from_deb(deb_path, dtb_path)
503        rootfs_url = ('https://github.com/groeck/linux-build-test/raw/'
504                      '2eb0a73b5d5a28df3170c546ddaaa9757e1e0848/rootfs/'
505                      'arm/rootfs-armv5.ext2.gz')
506        rootfs_hash = '093e89d2b4d982234bf528bc9fb2f2f17a9d1f93'
507        rootfs_path_gz = self.fetch_asset(rootfs_url, asset_hash=rootfs_hash)
508        rootfs_path = os.path.join(self.workdir, 'rootfs.cpio')
509        archive.gzip_uncompress(rootfs_path_gz, rootfs_path)
510
511        self.vm.set_console()
512        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
513                               'console=ttyS0,115200 '
514                               'usbcore.nousb '
515                               'root=/dev/sda ro '
516                               'panic=-1 noreboot')
517        self.vm.add_args('-kernel', kernel_path,
518                         '-dtb', dtb_path,
519                         '-drive', 'if=none,format=raw,id=disk0,file='
520                                   + rootfs_path,
521                         '-device', 'ide-hd,bus=ide.0,drive=disk0',
522                         '-append', kernel_command_line,
523                         '-no-reboot')
524        self.vm.launch()
525        self.wait_for_console_pattern('Boot successful.')
526
527        exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo',
528                                                'Allwinner sun4i/sun5i')
529        exec_command_and_wait_for_pattern(self, 'cat /proc/partitions',
530                                                'sda')
531        exec_command_and_wait_for_pattern(self, 'reboot',
532                                                'reboot: Restarting system')
533
534    def test_arm_orangepi(self):
535        """
536        :avocado: tags=arch:arm
537        :avocado: tags=machine:orangepi-pc
538        """
539        deb_url = ('https://apt.armbian.com/pool/main/l/'
540                   'linux-4.20.7-sunxi/linux-image-dev-sunxi_5.75_armhf.deb')
541        deb_hash = '1334c29c44d984ffa05ed10de8c3361f33d78315'
542        deb_path = self.fetch_asset(deb_url, asset_hash=deb_hash)
543        kernel_path = self.extract_from_deb(deb_path,
544                                            '/boot/vmlinuz-4.20.7-sunxi')
545        dtb_path = '/usr/lib/linux-image-dev-sunxi/sun8i-h3-orangepi-pc.dtb'
546        dtb_path = self.extract_from_deb(deb_path, dtb_path)
547
548        self.vm.set_console()
549        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
550                               'console=ttyS0,115200n8 '
551                               'earlycon=uart,mmio32,0x1c28000')
552        self.vm.add_args('-kernel', kernel_path,
553                         '-dtb', dtb_path,
554                         '-append', kernel_command_line)
555        self.vm.launch()
556        console_pattern = 'Kernel command line: %s' % kernel_command_line
557        self.wait_for_console_pattern(console_pattern)
558
559    def test_arm_orangepi_initrd(self):
560        """
561        :avocado: tags=arch:arm
562        :avocado: tags=machine:orangepi-pc
563        """
564        deb_url = ('https://apt.armbian.com/pool/main/l/'
565                   'linux-4.20.7-sunxi/linux-image-dev-sunxi_5.75_armhf.deb')
566        deb_hash = '1334c29c44d984ffa05ed10de8c3361f33d78315'
567        deb_path = self.fetch_asset(deb_url, asset_hash=deb_hash)
568        kernel_path = self.extract_from_deb(deb_path,
569                                            '/boot/vmlinuz-4.20.7-sunxi')
570        dtb_path = '/usr/lib/linux-image-dev-sunxi/sun8i-h3-orangepi-pc.dtb'
571        dtb_path = self.extract_from_deb(deb_path, dtb_path)
572        initrd_url = ('https://github.com/groeck/linux-build-test/raw/'
573                      '2eb0a73b5d5a28df3170c546ddaaa9757e1e0848/rootfs/'
574                      'arm/rootfs-armv7a.cpio.gz')
575        initrd_hash = '604b2e45cdf35045846b8bbfbf2129b1891bdc9c'
576        initrd_path_gz = self.fetch_asset(initrd_url, asset_hash=initrd_hash)
577        initrd_path = os.path.join(self.workdir, 'rootfs.cpio')
578        archive.gzip_uncompress(initrd_path_gz, initrd_path)
579
580        self.vm.set_console()
581        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
582                               'console=ttyS0,115200 '
583                               'panic=-1 noreboot')
584        self.vm.add_args('-kernel', kernel_path,
585                         '-dtb', dtb_path,
586                         '-initrd', initrd_path,
587                         '-append', kernel_command_line,
588                         '-no-reboot')
589        self.vm.launch()
590        self.wait_for_console_pattern('Boot successful.')
591
592        exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo',
593                                                'Allwinner sun8i Family')
594        exec_command_and_wait_for_pattern(self, 'cat /proc/iomem',
595                                                'system-control@1c00000')
596        exec_command_and_wait_for_pattern(self, 'reboot',
597                                                'reboot: Restarting system')
598
599    def test_arm_orangepi_sd(self):
600        """
601        :avocado: tags=arch:arm
602        :avocado: tags=machine:orangepi-pc
603        :avocado: tags=device:sd
604        """
605        deb_url = ('https://apt.armbian.com/pool/main/l/'
606                   'linux-4.20.7-sunxi/linux-image-dev-sunxi_5.75_armhf.deb')
607        deb_hash = '1334c29c44d984ffa05ed10de8c3361f33d78315'
608        deb_path = self.fetch_asset(deb_url, asset_hash=deb_hash)
609        kernel_path = self.extract_from_deb(deb_path,
610                                            '/boot/vmlinuz-4.20.7-sunxi')
611        dtb_path = '/usr/lib/linux-image-dev-sunxi/sun8i-h3-orangepi-pc.dtb'
612        dtb_path = self.extract_from_deb(deb_path, dtb_path)
613        rootfs_url = ('http://storage.kernelci.org/images/rootfs/buildroot/'
614                      'kci-2019.02/armel/base/rootfs.ext2.xz')
615        rootfs_hash = '692510cb625efda31640d1de0a8d60e26040f061'
616        rootfs_path_xz = self.fetch_asset(rootfs_url, asset_hash=rootfs_hash)
617        rootfs_path = os.path.join(self.workdir, 'rootfs.cpio')
618        archive.lzma_uncompress(rootfs_path_xz, rootfs_path)
619        image_pow2ceil_expand(rootfs_path)
620
621        self.vm.set_console()
622        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
623                               'console=ttyS0,115200 '
624                               'root=/dev/mmcblk0 rootwait rw '
625                               'panic=-1 noreboot')
626        self.vm.add_args('-kernel', kernel_path,
627                         '-dtb', dtb_path,
628                         '-drive', 'file=' + rootfs_path + ',if=sd,format=raw',
629                         '-append', kernel_command_line,
630                         '-no-reboot')
631        self.vm.launch()
632        shell_ready = "/bin/sh: can't access tty; job control turned off"
633        self.wait_for_console_pattern(shell_ready)
634
635        exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo',
636                                                'Allwinner sun8i Family')
637        exec_command_and_wait_for_pattern(self, 'cat /proc/partitions',
638                                                'mmcblk0')
639        exec_command_and_wait_for_pattern(self, 'ifconfig eth0 up',
640                                                 'eth0: Link is Up')
641        exec_command_and_wait_for_pattern(self, 'udhcpc eth0',
642            'udhcpc: lease of 10.0.2.15 obtained')
643        exec_command_and_wait_for_pattern(self, 'ping -c 3 10.0.2.2',
644            '3 packets transmitted, 3 packets received, 0% packet loss')
645        exec_command_and_wait_for_pattern(self, 'reboot',
646                                                'reboot: Restarting system')
647
648    @skipUnless(os.getenv('AVOCADO_ALLOW_LARGE_STORAGE'), 'storage limited')
649    @skipUnless(P7ZIP_AVAILABLE, '7z not installed')
650    def test_arm_orangepi_bionic(self):
651        """
652        :avocado: tags=arch:arm
653        :avocado: tags=machine:orangepi-pc
654        :avocado: tags=device:sd
655        """
656
657        # This test download a 196MB compressed image and expand it to 1GB
658        image_url = ('https://dl.armbian.com/orangepipc/archive/'
659                     'Armbian_19.11.3_Orangepipc_bionic_current_5.3.9.7z')
660        image_hash = '196a8ffb72b0123d92cea4a070894813d305c71e'
661        image_path_7z = self.fetch_asset(image_url, asset_hash=image_hash)
662        image_name = 'Armbian_19.11.3_Orangepipc_bionic_current_5.3.9.img'
663        image_path = os.path.join(self.workdir, image_name)
664        process.run("7z e -o%s %s" % (self.workdir, image_path_7z))
665        image_pow2ceil_expand(image_path)
666
667        self.vm.set_console()
668        self.vm.add_args('-drive', 'file=' + image_path + ',if=sd,format=raw',
669                         '-nic', 'user',
670                         '-no-reboot')
671        self.vm.launch()
672
673        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
674                               'console=ttyS0,115200 '
675                               'loglevel=7 '
676                               'nosmp '
677                               'systemd.default_timeout_start_sec=9000 '
678                               'systemd.mask=armbian-zram-config.service '
679                               'systemd.mask=armbian-ramlog.service')
680
681        self.wait_for_console_pattern('U-Boot SPL')
682        self.wait_for_console_pattern('Autoboot in ')
683        exec_command_and_wait_for_pattern(self, ' ', '=>')
684        exec_command_and_wait_for_pattern(self, "setenv extraargs '" +
685                                                kernel_command_line + "'", '=>')
686        exec_command_and_wait_for_pattern(self, 'boot', 'Starting kernel ...');
687
688        self.wait_for_console_pattern('systemd[1]: Set hostname ' +
689                                      'to <orangepipc>')
690        self.wait_for_console_pattern('Starting Load Kernel Modules...')
691
692    @skipUnless(os.getenv('AVOCADO_ALLOW_LARGE_STORAGE'), 'storage limited')
693    def test_arm_orangepi_uboot_netbsd9(self):
694        """
695        :avocado: tags=arch:arm
696        :avocado: tags=machine:orangepi-pc
697        :avocado: tags=device:sd
698        """
699        # This test download a 304MB compressed image and expand it to 2GB
700        deb_url = ('http://snapshot.debian.org/archive/debian/'
701                   '20200108T145233Z/pool/main/u/u-boot/'
702                   'u-boot-sunxi_2020.01%2Bdfsg-1_armhf.deb')
703        deb_hash = 'f67f404a80753ca3d1258f13e38f2b060e13db99'
704        deb_path = self.fetch_asset(deb_url, asset_hash=deb_hash)
705        # We use the common OrangePi PC 'plus' build of U-Boot for our secondary
706        # program loader (SPL). We will then set the path to the more specific
707        # OrangePi "PC" device tree blob with 'setenv fdtfile' in U-Boot prompt,
708        # before to boot NetBSD.
709        uboot_path = '/usr/lib/u-boot/orangepi_plus/u-boot-sunxi-with-spl.bin'
710        uboot_path = self.extract_from_deb(deb_path, uboot_path)
711        image_url = ('https://cdn.netbsd.org/pub/NetBSD/NetBSD-9.0/'
712                     'evbarm-earmv7hf/binary/gzimg/armv7.img.gz')
713        image_hash = '2babb29d36d8360adcb39c09e31060945259917a'
714        image_path_gz = self.fetch_asset(image_url, asset_hash=image_hash)
715        image_path = os.path.join(self.workdir, 'armv7.img')
716        archive.gzip_uncompress(image_path_gz, image_path)
717        image_pow2ceil_expand(image_path)
718        image_drive_args = 'if=sd,format=raw,snapshot=on,file=' + image_path
719
720        # dd if=u-boot-sunxi-with-spl.bin of=armv7.img bs=1K seek=8 conv=notrunc
721        with open(uboot_path, 'rb') as f_in:
722            with open(image_path, 'r+b') as f_out:
723                f_out.seek(8 * 1024)
724                shutil.copyfileobj(f_in, f_out)
725
726        self.vm.set_console()
727        self.vm.add_args('-nic', 'user',
728                         '-drive', image_drive_args,
729                         '-global', 'allwinner-rtc.base-year=2000',
730                         '-no-reboot')
731        self.vm.launch()
732        wait_for_console_pattern(self, 'U-Boot 2020.01+dfsg-1')
733        interrupt_interactive_console_until_pattern(self,
734                                       'Hit any key to stop autoboot:',
735                                       'switch to partitions #0, OK')
736
737        exec_command_and_wait_for_pattern(self, '', '=>')
738        cmd = 'setenv bootargs root=ld0a'
739        exec_command_and_wait_for_pattern(self, cmd, '=>')
740        cmd = 'setenv kernel netbsd-GENERIC.ub'
741        exec_command_and_wait_for_pattern(self, cmd, '=>')
742        cmd = 'setenv fdtfile dtb/sun8i-h3-orangepi-pc.dtb'
743        exec_command_and_wait_for_pattern(self, cmd, '=>')
744        cmd = ("setenv bootcmd 'fatload mmc 0:1 ${kernel_addr_r} ${kernel}; "
745               "fatload mmc 0:1 ${fdt_addr_r} ${fdtfile}; "
746               "fdt addr ${fdt_addr_r}; "
747               "bootm ${kernel_addr_r} - ${fdt_addr_r}'")
748        exec_command_and_wait_for_pattern(self, cmd, '=>')
749
750        exec_command_and_wait_for_pattern(self, 'boot',
751                                          'Booting kernel from Legacy Image')
752        wait_for_console_pattern(self, 'Starting kernel ...')
753        wait_for_console_pattern(self, 'NetBSD 9.0 (GENERIC)')
754        # Wait for user-space
755        wait_for_console_pattern(self, 'Starting root file system check')
756
757    def test_s390x_s390_ccw_virtio(self):
758        """
759        :avocado: tags=arch:s390x
760        :avocado: tags=machine:s390-ccw-virtio
761        """
762        kernel_url = ('https://archives.fedoraproject.org/pub/archive'
763                      '/fedora-secondary/releases/29/Everything/s390x/os/images'
764                      '/kernel.img')
765        kernel_hash = 'e8e8439103ef8053418ef062644ffd46a7919313'
766        kernel_path = self.fetch_asset(kernel_url, asset_hash=kernel_hash)
767
768        self.vm.set_console()
769        kernel_command_line = self.KERNEL_COMMON_COMMAND_LINE + 'console=sclp0'
770        self.vm.add_args('-nodefaults',
771                         '-kernel', kernel_path,
772                         '-append', kernel_command_line)
773        self.vm.launch()
774        console_pattern = 'Kernel command line: %s' % kernel_command_line
775        self.wait_for_console_pattern(console_pattern)
776
777    def test_alpha_clipper(self):
778        """
779        :avocado: tags=arch:alpha
780        :avocado: tags=machine:clipper
781        """
782        kernel_url = ('http://archive.debian.org/debian/dists/lenny/main/'
783                      'installer-alpha/current/images/cdrom/vmlinuz')
784        kernel_hash = '3a943149335529e2ed3e74d0d787b85fb5671ba3'
785        kernel_path = self.fetch_asset(kernel_url, asset_hash=kernel_hash)
786
787        uncompressed_kernel = archive.uncompress(kernel_path, self.workdir)
788
789        self.vm.set_console()
790        kernel_command_line = self.KERNEL_COMMON_COMMAND_LINE + 'console=ttyS0'
791        self.vm.add_args('-nodefaults',
792                         '-kernel', uncompressed_kernel,
793                         '-append', kernel_command_line)
794        self.vm.launch()
795        console_pattern = 'Kernel command line: %s' % kernel_command_line
796        self.wait_for_console_pattern(console_pattern)
797
798    def test_ppc64_pseries(self):
799        """
800        :avocado: tags=arch:ppc64
801        :avocado: tags=machine:pseries
802        """
803        kernel_url = ('https://archives.fedoraproject.org/pub/archive'
804                      '/fedora-secondary/releases/29/Everything/ppc64le/os'
805                      '/ppc/ppc64/vmlinuz')
806        kernel_hash = '3fe04abfc852b66653b8c3c897a59a689270bc77'
807        kernel_path = self.fetch_asset(kernel_url, asset_hash=kernel_hash)
808
809        self.vm.set_console()
810        kernel_command_line = self.KERNEL_COMMON_COMMAND_LINE + 'console=hvc0'
811        self.vm.add_args('-kernel', kernel_path,
812                         '-append', kernel_command_line)
813        self.vm.launch()
814        console_pattern = 'Kernel command line: %s' % kernel_command_line
815        self.wait_for_console_pattern(console_pattern)
816
817    def test_m68k_q800(self):
818        """
819        :avocado: tags=arch:m68k
820        :avocado: tags=machine:q800
821        """
822        deb_url = ('https://snapshot.debian.org/archive/debian-ports'
823                   '/20191021T083923Z/pool-m68k/main'
824                   '/l/linux/kernel-image-5.3.0-1-m68k-di_5.3.7-1_m68k.udeb')
825        deb_hash = '044954bb9be4160a3ce81f8bc1b5e856b75cccd1'
826        deb_path = self.fetch_asset(deb_url, asset_hash=deb_hash)
827        kernel_path = self.extract_from_deb(deb_path,
828                                            '/boot/vmlinux-5.3.0-1-m68k')
829
830        self.vm.set_console()
831        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
832                               'console=ttyS0 vga=off')
833        self.vm.add_args('-kernel', kernel_path,
834                         '-append', kernel_command_line)
835        self.vm.launch()
836        console_pattern = 'Kernel command line: %s' % kernel_command_line
837        self.wait_for_console_pattern(console_pattern)
838        console_pattern = 'No filesystem could mount root'
839        self.wait_for_console_pattern(console_pattern)
840
841    def do_test_advcal_2018(self, day, tar_hash, kernel_name):
842        tar_url = ('https://www.qemu-advent-calendar.org'
843                   '/2018/download/day' + day + '.tar.xz')
844        file_path = self.fetch_asset(tar_url, asset_hash=tar_hash)
845        archive.extract(file_path, self.workdir)
846        self.vm.set_console()
847        self.vm.add_args('-kernel',
848                         self.workdir + '/day' + day + '/' + kernel_name)
849        self.vm.launch()
850        self.wait_for_console_pattern('QEMU advent calendar')
851
852    def test_arm_vexpressa9(self):
853        """
854        :avocado: tags=arch:arm
855        :avocado: tags=machine:vexpress-a9
856        """
857        tar_hash = '32b7677ce8b6f1471fb0059865f451169934245b'
858        self.vm.add_args('-dtb', self.workdir + '/day16/vexpress-v2p-ca9.dtb')
859        self.do_test_advcal_2018('16', tar_hash, 'winter.zImage')
860
861    def test_m68k_mcf5208evb(self):
862        """
863        :avocado: tags=arch:m68k
864        :avocado: tags=machine:mcf5208evb
865        """
866        tar_hash = 'ac688fd00561a2b6ce1359f9ff6aa2b98c9a570c'
867        self.do_test_advcal_2018('07', tar_hash, 'sanity-clause.elf')
868
869    def test_microblaze_s3adsp1800(self):
870        """
871        :avocado: tags=arch:microblaze
872        :avocado: tags=machine:petalogix-s3adsp1800
873        """
874        tar_hash = '08bf3e3bfb6b6c7ce1e54ab65d54e189f2caf13f'
875        self.do_test_advcal_2018('17', tar_hash, 'ballerina.bin')
876
877    def test_or1k_sim(self):
878        """
879        :avocado: tags=arch:or1k
880        :avocado: tags=machine:or1k-sim
881        """
882        tar_hash = '20334cdaf386108c530ff0badaecc955693027dd'
883        self.do_test_advcal_2018('20', tar_hash, 'vmlinux')
884
885    def test_nios2_10m50(self):
886        """
887        :avocado: tags=arch:nios2
888        :avocado: tags=machine:10m50-ghrd
889        """
890        tar_hash = 'e4251141726c412ac0407c5a6bceefbbff018918'
891        self.do_test_advcal_2018('14', tar_hash, 'vmlinux.elf')
892
893    def test_ppc64_e500(self):
894        """
895        :avocado: tags=arch:ppc64
896        :avocado: tags=machine:ppce500
897        """
898        tar_hash = '6951d86d644b302898da2fd701739c9406527fe1'
899        self.vm.add_args('-cpu', 'e5500')
900        self.do_test_advcal_2018('19', tar_hash, 'uImage')
901
902    def test_ppc_g3beige(self):
903        """
904        :avocado: tags=arch:ppc
905        :avocado: tags=machine:g3beige
906        """
907        tar_hash = 'e0b872a5eb8fdc5bed19bd43ffe863900ebcedfc'
908        self.vm.add_args('-M', 'graphics=off')
909        self.do_test_advcal_2018('15', tar_hash, 'invaders.elf')
910
911    def test_ppc_mac99(self):
912        """
913        :avocado: tags=arch:ppc
914        :avocado: tags=machine:mac99
915        """
916        tar_hash = 'e0b872a5eb8fdc5bed19bd43ffe863900ebcedfc'
917        self.vm.add_args('-M', 'graphics=off')
918        self.do_test_advcal_2018('15', tar_hash, 'invaders.elf')
919
920    def test_sparc_ss20(self):
921        """
922        :avocado: tags=arch:sparc
923        :avocado: tags=machine:SS-20
924        """
925        tar_hash = 'b18550d5d61c7615d989a06edace051017726a9f'
926        self.do_test_advcal_2018('11', tar_hash, 'zImage.elf')
927
928    def test_xtensa_lx60(self):
929        """
930        :avocado: tags=arch:xtensa
931        :avocado: tags=machine:lx60
932        """
933        tar_hash = '49e88d9933742f0164b60839886c9739cb7a0d34'
934        self.vm.add_args('-cpu', 'dc233c')
935        self.do_test_advcal_2018('02', tar_hash, 'santas-sleigh-ride.elf')
936