1#!/usr/bin/env python3
2#
3# Functional tests for the little-endian 64-bit MIPS Malta board
4#
5# Copyright (c) Philippe Mathieu-Daudé <f4bug@amsat.org>
6#
7# This work is licensed under the terms of the GNU GPL, version 2 or later.
8# See the COPYING file in the top-level directory.
9#
10# SPDX-License-Identifier: GPL-2.0-or-later
11
12import os
13import logging
14
15from qemu_test import LinuxKernelTest, Asset
16from qemu_test import exec_command_and_wait_for_pattern
17from qemu_test.utils import gzip_uncompress
18from unittest import skipUnless
19
20NUMPY_AVAILABLE = True
21try:
22    import numpy as np
23except ImportError:
24    NUMPY_AVAILABLE = False
25
26CV2_AVAILABLE = True
27try:
28    import cv2
29except ImportError:
30    CV2_AVAILABLE = False
31
32
33class MaltaMachineConsole(LinuxKernelTest):
34
35    ASSET_KERNEL_2_63_2 = Asset(
36        ('http://snapshot.debian.org/archive/debian/'
37         '20130217T032700Z/pool/main/l/linux-2.6/'
38         'linux-image-2.6.32-5-5kc-malta_2.6.32-48_mipsel.deb'),
39        '35eb476f03be589824b0310358f1c447d85e645b88cbcd2ac02b97ef560f9f8d')
40
41    def test_mips64el_malta(self):
42        """
43        This test requires the ar tool to extract "data.tar.gz" from
44        the Debian package.
45
46        The kernel can be rebuilt using this Debian kernel source [1] and
47        following the instructions on [2].
48
49        [1] http://snapshot.debian.org/package/linux-2.6/2.6.32-48/
50            #linux-source-2.6.32_2.6.32-48
51        [2] https://kernel-team.pages.debian.net/kernel-handbook/
52            ch-common-tasks.html#s-common-official
53        """
54        deb_path = self.ASSET_KERNEL_2_63_2.fetch()
55        kernel_path = self.extract_from_deb(deb_path,
56                                            '/boot/vmlinux-2.6.32-5-5kc-malta')
57
58        self.set_machine('malta')
59        self.vm.set_console()
60        kernel_command_line = self.KERNEL_COMMON_COMMAND_LINE + 'console=ttyS0'
61        self.vm.add_args('-kernel', kernel_path,
62                         '-append', kernel_command_line)
63        self.vm.launch()
64        console_pattern = 'Kernel command line: %s' % kernel_command_line
65        self.wait_for_console_pattern(console_pattern)
66
67    ASSET_KERNEL_3_19_3 = Asset(
68        ('https://github.com/philmd/qemu-testing-blob/'
69         'raw/9ad2df38/mips/malta/mips64el/'
70         'vmlinux-3.19.3.mtoman.20150408'),
71        '8d3beb003bc66051ead98e7172139017fcf9ce2172576541c57e86418dfa5ab8')
72
73    ASSET_CPIO_R1 = Asset(
74        ('https://github.com/groeck/linux-build-test/'
75         'raw/8584a59e/rootfs/mipsel64/'
76         'rootfs.mipsel64r1.cpio.gz'),
77        '75ba10cd35fb44e32948eeb26974f061b703c81c4ba2fab1ebcacf1d1bec3b61')
78
79    @skipUnless(os.getenv('QEMU_TEST_ALLOW_UNTRUSTED_CODE'), 'untrusted code')
80    def test_mips64el_malta_5KEc_cpio(self):
81        kernel_path = self.ASSET_KERNEL_3_19_3.fetch()
82        initrd_path_gz = self.ASSET_CPIO_R1.fetch()
83        initrd_path = os.path.join(self.workdir, 'rootfs.cpio')
84        gzip_uncompress(initrd_path_gz, initrd_path)
85
86        self.set_machine('malta')
87        self.vm.set_console()
88        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE
89                               + 'console=ttyS0 console=tty '
90                               + 'rdinit=/sbin/init noreboot')
91        self.vm.add_args('-cpu', '5KEc',
92                         '-kernel', kernel_path,
93                         '-initrd', initrd_path,
94                         '-append', kernel_command_line,
95                         '-no-reboot')
96        self.vm.launch()
97        self.wait_for_console_pattern('Boot successful.')
98
99        exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo',
100                                                'MIPS 5KE')
101        exec_command_and_wait_for_pattern(self, 'uname -a',
102                                                '3.19.3.mtoman.20150408')
103        exec_command_and_wait_for_pattern(self, 'reboot',
104                                                'reboot: Restarting system')
105        # Wait for VM to shut down gracefully
106        self.vm.wait()
107
108
109@skipUnless(NUMPY_AVAILABLE, 'Python NumPy not installed')
110@skipUnless(CV2_AVAILABLE, 'Python OpenCV not installed')
111class MaltaMachineFramebuffer(LinuxKernelTest):
112
113    timeout = 30
114
115    ASSET_KERNEL_4_7_0 = Asset(
116        ('https://github.com/philmd/qemu-testing-blob/raw/a5966ca4b5/'
117         'mips/malta/mips64el/vmlinux-4.7.0-rc1.I6400.gz'),
118        '1f64efc59968a3c328672e6b10213fe574bb2308d9d2ed44e75e40be59e9fbc2')
119
120    ASSET_TUXLOGO = Asset(
121        ('https://github.com/torvalds/linux/raw/v2.6.12/'
122         'drivers/video/logo/logo_linux_vga16.ppm'),
123        'b762f0d91ec018887ad1b334543c2fdf9be9fdfc87672b409211efaa3ea0ef79')
124
125    def do_test_i6400_framebuffer_logo(self, cpu_cores_count):
126        """
127        Boot Linux kernel and check Tux logo is displayed on the framebuffer.
128        """
129        screendump_path = os.path.join(self.workdir, 'screendump.pbm')
130
131        kernel_path_gz = self.ASSET_KERNEL_4_7_0.fetch()
132        kernel_path = self.workdir + "/vmlinux"
133        gzip_uncompress(kernel_path_gz, kernel_path)
134
135        tuxlogo_path = self.ASSET_TUXLOGO.fetch()
136
137        self.set_machine('malta')
138        self.vm.set_console()
139        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
140                               'clocksource=GIC console=tty0 console=ttyS0')
141        self.vm.add_args('-kernel', kernel_path,
142                         '-cpu', 'I6400',
143                         '-smp', '%u' % cpu_cores_count,
144                         '-vga', 'std',
145                         '-append', kernel_command_line)
146        self.vm.launch()
147        framebuffer_ready = 'Console: switching to colour frame buffer device'
148        self.wait_for_console_pattern(framebuffer_ready)
149        self.vm.cmd('human-monitor-command', command_line='stop')
150        self.vm.cmd('human-monitor-command',
151                    command_line='screendump %s' % screendump_path)
152        logger = logging.getLogger('framebuffer')
153
154        match_threshold = 0.95
155        screendump_bgr = cv2.imread(screendump_path, cv2.IMREAD_COLOR)
156        tuxlogo_bgr = cv2.imread(tuxlogo_path, cv2.IMREAD_COLOR)
157        result = cv2.matchTemplate(screendump_bgr, tuxlogo_bgr,
158                                   cv2.TM_CCOEFF_NORMED)
159        loc = np.where(result >= match_threshold)
160        tuxlogo_count = 0
161        h, w = tuxlogo_bgr.shape[:2]
162        debug_png = os.getenv('QEMU_TEST_CV2_SCREENDUMP_PNG_PATH')
163        for tuxlogo_count, pt in enumerate(zip(*loc[::-1]), start=1):
164            logger.debug('found Tux at position (x, y) = %s', pt)
165            cv2.rectangle(screendump_bgr, pt,
166                          (pt[0] + w, pt[1] + h), (0, 0, 255), 2)
167        if debug_png:
168            cv2.imwrite(debug_png, screendump_bgr)
169        self.assertGreaterEqual(tuxlogo_count, cpu_cores_count)
170
171    def test_mips_malta_i6400_framebuffer_logo_1core(self):
172        self.do_test_i6400_framebuffer_logo(1)
173
174    @skipUnless(os.getenv('QEMU_TEST_FLAKY_TESTS'), 'Test is unstable on GitLab')
175    def test_mips_malta_i6400_framebuffer_logo_7cores(self):
176        self.do_test_i6400_framebuffer_logo(7)
177
178    @skipUnless(os.getenv('QEMU_TEST_FLAKY_TESTS'), 'Test is unstable on GitLab')
179    def test_mips_malta_i6400_framebuffer_logo_8cores(self):
180        self.do_test_i6400_framebuffer_logo(8)
181
182
183from test_mipsel_malta import MaltaMachineYAMON
184
185if __name__ == '__main__':
186    LinuxKernelTest.main()
187