xref: /openbmc/qemu/tests/avocado/boot_linux_console.py (revision e732f00f32e3826668db2b8b81fcf48a3a60a283)
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
27class LinuxKernelTest(QemuSystemTest):
28    KERNEL_COMMON_COMMAND_LINE = 'printk.time=0 '
29
30    def wait_for_console_pattern(self, success_message, vm=None):
31        wait_for_console_pattern(self, success_message,
32                                 failure_message='Kernel panic - not syncing',
33                                 vm=vm)
34
35    def extract_from_deb(self, deb, path):
36        """
37        Extracts a file from a deb package into the test workdir
38
39        :param deb: path to the deb archive
40        :param path: path within the deb archive of the file to be extracted
41        :returns: path of the extracted file
42        """
43        cwd = os.getcwd()
44        os.chdir(self.workdir)
45        file_path = process.run("ar t %s" % deb).stdout_text.split()[2]
46        process.run("ar x %s %s" % (deb, file_path))
47        archive.extract(file_path, self.workdir)
48        os.chdir(cwd)
49        # Return complete path to extracted file.  Because callers to
50        # extract_from_deb() specify 'path' with a leading slash, it is
51        # necessary to use os.path.relpath() as otherwise os.path.join()
52        # interprets it as an absolute path and drops the self.workdir part.
53        return os.path.normpath(os.path.join(self.workdir,
54                                             os.path.relpath(path, '/')))
55
56    def extract_from_rpm(self, rpm, path):
57        """
58        Extracts a file from an RPM package into the test workdir.
59
60        :param rpm: path to the rpm archive
61        :param path: path within the rpm archive of the file to be extracted
62                     needs to be a relative path (starting with './') because
63                     cpio(1), which is used to extract the file, expects that.
64        :returns: path of the extracted file
65        """
66        cwd = os.getcwd()
67        os.chdir(self.workdir)
68        process.run("rpm2cpio %s | cpio -id %s" % (rpm, path), shell=True)
69        os.chdir(cwd)
70        return os.path.normpath(os.path.join(self.workdir, path))
71
72class BootLinuxConsole(LinuxKernelTest):
73    """
74    Boots a Linux kernel and checks that the console is operational and the
75    kernel command line is properly passed from QEMU to the kernel
76    """
77    timeout = 90
78
79    def test_x86_64_pc(self):
80        """
81        :avocado: tags=arch:x86_64
82        :avocado: tags=machine:pc
83        """
84        kernel_url = ('https://archives.fedoraproject.org/pub/archive/fedora'
85                      '/linux/releases/29/Everything/x86_64/os/images/pxeboot'
86                      '/vmlinuz')
87        kernel_hash = '23bebd2680757891cf7adedb033532163a792495'
88        kernel_path = self.fetch_asset(kernel_url, asset_hash=kernel_hash)
89
90        self.vm.set_console()
91        kernel_command_line = self.KERNEL_COMMON_COMMAND_LINE + 'console=ttyS0'
92        self.vm.add_args('-kernel', kernel_path,
93                         '-append', kernel_command_line)
94        self.vm.launch()
95        console_pattern = 'Kernel command line: %s' % kernel_command_line
96        self.wait_for_console_pattern(console_pattern)
97
98    def test_arm_virt(self):
99        """
100        :avocado: tags=arch:arm
101        :avocado: tags=machine:virt
102        :avocado: tags=accel:tcg
103        """
104        kernel_url = ('https://archives.fedoraproject.org/pub/archive/fedora'
105                      '/linux/releases/29/Everything/armhfp/os/images/pxeboot'
106                      '/vmlinuz')
107        kernel_hash = 'e9826d741b4fb04cadba8d4824d1ed3b7fb8b4d4'
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 +
112                               'console=ttyAMA0')
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    @skipUnless(os.getenv('AVOCADO_TIMEOUT_EXPECTED'), 'Test might timeout')
120    def test_arm_quanta_gsj(self):
121        """
122        :avocado: tags=arch:arm
123        :avocado: tags=machine:quanta-gsj
124        :avocado: tags=accel:tcg
125        """
126        # 25 MiB compressed, 32 MiB uncompressed.
127        image_url = (
128                'https://github.com/hskinnemoen/openbmc/releases/download/'
129                '20200711-gsj-qemu-0/obmc-phosphor-image-gsj.static.mtd.gz')
130        image_hash = '14895e634923345cb5c8776037ff7876df96f6b1'
131        image_path_gz = self.fetch_asset(image_url, asset_hash=image_hash)
132        image_name = 'obmc.mtd'
133        image_path = os.path.join(self.workdir, image_name)
134        archive.gzip_uncompress(image_path_gz, image_path)
135
136        self.vm.set_console()
137        drive_args = 'file=' + image_path + ',if=mtd,bus=0,unit=0'
138        self.vm.add_args('-drive', drive_args)
139        self.vm.launch()
140
141        # Disable drivers and services that stall for a long time during boot,
142        # to avoid running past the 90-second timeout. These may be removed
143        # as the corresponding device support is added.
144        kernel_command_line = self.KERNEL_COMMON_COMMAND_LINE + (
145                'console=${console} '
146                'mem=${mem} '
147                'initcall_blacklist=npcm_i2c_bus_driver_init '
148                'systemd.mask=systemd-random-seed.service '
149                'systemd.mask=dropbearkey.service '
150        )
151
152        self.wait_for_console_pattern('> BootBlock by Nuvoton')
153        self.wait_for_console_pattern('>Device: Poleg BMC NPCM730')
154        self.wait_for_console_pattern('>Skip DDR init.')
155        self.wait_for_console_pattern('U-Boot ')
156        interrupt_interactive_console_until_pattern(
157                self, 'Hit any key to stop autoboot:', 'U-Boot>')
158        exec_command_and_wait_for_pattern(
159                self, "setenv bootargs ${bootargs} " + kernel_command_line,
160                'U-Boot>')
161        exec_command_and_wait_for_pattern(
162                self, 'run romboot', 'Booting Kernel from flash')
163        self.wait_for_console_pattern('Booting Linux on physical CPU 0x0')
164        self.wait_for_console_pattern('CPU1: thread -1, cpu 1, socket 0')
165        self.wait_for_console_pattern('OpenBMC Project Reference Distro')
166        self.wait_for_console_pattern('gsj login:')
167
168    def test_arm_quanta_gsj_initrd(self):
169        """
170        :avocado: tags=arch:arm
171        :avocado: tags=machine:quanta-gsj
172        :avocado: tags=accel:tcg
173        """
174        initrd_url = (
175                'https://github.com/hskinnemoen/openbmc/releases/download/'
176                '20200711-gsj-qemu-0/obmc-phosphor-initramfs-gsj.cpio.xz')
177        initrd_hash = '98fefe5d7e56727b1eb17d5c00311b1b5c945300'
178        initrd_path = self.fetch_asset(initrd_url, asset_hash=initrd_hash)
179        kernel_url = (
180                'https://github.com/hskinnemoen/openbmc/releases/download/'
181                '20200711-gsj-qemu-0/uImage-gsj.bin')
182        kernel_hash = 'fa67b2f141d56d39b3c54305c0e8a899c99eb2c7'
183        kernel_path = self.fetch_asset(kernel_url, asset_hash=kernel_hash)
184        dtb_url = (
185                'https://github.com/hskinnemoen/openbmc/releases/download/'
186                '20200711-gsj-qemu-0/nuvoton-npcm730-gsj.dtb')
187        dtb_hash = '18315f7006d7b688d8312d5c727eecd819aa36a4'
188        dtb_path = self.fetch_asset(dtb_url, asset_hash=dtb_hash)
189
190        self.vm.set_console()
191        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
192                               'console=ttyS0,115200n8 '
193                               'earlycon=uart8250,mmio32,0xf0001000')
194        self.vm.add_args('-kernel', kernel_path,
195                         '-initrd', initrd_path,
196                         '-dtb', dtb_path,
197                         '-append', kernel_command_line)
198        self.vm.launch()
199
200        self.wait_for_console_pattern('Booting Linux on physical CPU 0x0')
201        self.wait_for_console_pattern('CPU1: thread -1, cpu 1, socket 0')
202        self.wait_for_console_pattern(
203                'Give root password for system maintenance')
204