1# Functional tests for the MIPS Malta board
2#
3# Copyright (c) Philippe Mathieu-Daudé <f4bug@amsat.org>
4#
5# This work is licensed under the terms of the GNU GPL, version 2 or later.
6# See the COPYING file in the top-level directory.
7#
8# SPDX-License-Identifier: GPL-2.0-or-later
9
10import os
11import gzip
12import logging
13
14from avocado import skipUnless
15from avocado import skipUnless
16from avocado.utils import archive
17from avocado_qemu import QemuSystemTest
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
21
22
23NUMPY_AVAILABLE = True
24try:
25    import numpy as np
26except ImportError:
27    NUMPY_AVAILABLE = False
28
29CV2_AVAILABLE = True
30try:
31    import cv2
32except ImportError:
33    CV2_AVAILABLE = False
34
35
36@skipUnless(NUMPY_AVAILABLE, 'Python NumPy not installed')
37@skipUnless(CV2_AVAILABLE, 'Python OpenCV not installed')
38class MaltaMachineFramebuffer(QemuSystemTest):
39
40    timeout = 30
41
42    KERNEL_COMMON_COMMAND_LINE = 'printk.time=0 '
43
44    def do_test_i6400_framebuffer_logo(self, cpu_cores_count):
45        """
46        Boot Linux kernel and check Tux logo is displayed on the framebuffer.
47        """
48        screendump_path = os.path.join(self.workdir, 'screendump.pbm')
49
50        kernel_url = ('https://github.com/philmd/qemu-testing-blob/raw/'
51                      'a5966ca4b5/mips/malta/mips64el/'
52                      'vmlinux-4.7.0-rc1.I6400.gz')
53        kernel_hash = '096f50c377ec5072e6a366943324622c312045f6'
54        kernel_path_gz = self.fetch_asset(kernel_url, asset_hash=kernel_hash)
55        kernel_path = self.workdir + "vmlinux"
56        archive.gzip_uncompress(kernel_path_gz, kernel_path)
57
58        tuxlogo_url = ('https://github.com/torvalds/linux/raw/v2.6.12/'
59                       'drivers/video/logo/logo_linux_vga16.ppm')
60        tuxlogo_hash = '3991c2ddbd1ddaecda7601f8aafbcf5b02dc86af'
61        tuxlogo_path = self.fetch_asset(tuxlogo_url, asset_hash=tuxlogo_hash)
62
63        self.vm.set_console()
64        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
65                               'clocksource=GIC console=tty0 console=ttyS0')
66        self.vm.add_args('-kernel', kernel_path,
67                         '-smp', '%u' % cpu_cores_count,
68                         '-vga', 'std',
69                         '-append', kernel_command_line)
70        self.vm.launch()
71        framebuffer_ready = 'Console: switching to colour frame buffer device'
72        wait_for_console_pattern(self, framebuffer_ready,
73                                 failure_message='Kernel panic - not syncing')
74        self.vm.cmd('human-monitor-command', command_line='stop')
75        self.vm.cmd('human-monitor-command',
76                    command_line='screendump %s' % screendump_path)
77        logger = logging.getLogger('framebuffer')
78
79        match_threshold = 0.95
80        screendump_bgr = cv2.imread(screendump_path, cv2.IMREAD_COLOR)
81        tuxlogo_bgr = cv2.imread(tuxlogo_path, cv2.IMREAD_COLOR)
82        result = cv2.matchTemplate(screendump_bgr, tuxlogo_bgr,
83                                   cv2.TM_CCOEFF_NORMED)
84        loc = np.where(result >= match_threshold)
85        tuxlogo_count = 0
86        h, w = tuxlogo_bgr.shape[:2]
87        debug_png = os.getenv('AVOCADO_CV2_SCREENDUMP_PNG_PATH')
88        for tuxlogo_count, pt in enumerate(zip(*loc[::-1]), start=1):
89            logger.debug('found Tux at position (x, y) = %s', pt)
90            cv2.rectangle(screendump_bgr, pt,
91                          (pt[0] + w, pt[1] + h), (0, 0, 255), 2)
92        if debug_png:
93            cv2.imwrite(debug_png, screendump_bgr)
94        self.assertGreaterEqual(tuxlogo_count, cpu_cores_count)
95
96    def test_mips_malta_i6400_framebuffer_logo_1core(self):
97        """
98        :avocado: tags=arch:mips64el
99        :avocado: tags=machine:malta
100        :avocado: tags=cpu:I6400
101        """
102        self.do_test_i6400_framebuffer_logo(1)
103
104    @skipUnless(os.getenv('QEMU_TEST_FLAKY_TESTS'), 'Test is unstable on GitLab')
105    def test_mips_malta_i6400_framebuffer_logo_7cores(self):
106        """
107        :avocado: tags=arch:mips64el
108        :avocado: tags=machine:malta
109        :avocado: tags=cpu:I6400
110        :avocado: tags=mips:smp
111        :avocado: tags=flaky
112        """
113        self.do_test_i6400_framebuffer_logo(7)
114
115    @skipUnless(os.getenv('QEMU_TEST_FLAKY_TESTS'), 'Test is unstable on GitLab')
116    def test_mips_malta_i6400_framebuffer_logo_8cores(self):
117        """
118        :avocado: tags=arch:mips64el
119        :avocado: tags=machine:malta
120        :avocado: tags=cpu:I6400
121        :avocado: tags=mips:smp
122        :avocado: tags=flaky
123        """
124        self.do_test_i6400_framebuffer_logo(8)
125
126class MaltaMachine(QemuSystemTest):
127
128    def do_test_yamon(self):
129        rom_url = ('https://s3-eu-west-1.amazonaws.com/'
130                   'downloads-mips/mips-downloads/'
131                   'YAMON/yamon-bin-02.22.zip')
132        rom_hash = '8da7ecddbc5312704b8b324341ee238189bde480'
133        zip_path = self.fetch_asset(rom_url, asset_hash=rom_hash)
134
135        archive.extract(zip_path, self.workdir)
136        yamon_path = os.path.join(self.workdir, 'yamon-02.22.bin')
137
138        self.vm.set_console()
139        self.vm.add_args('-bios', yamon_path)
140        self.vm.launch()
141
142        prompt =  'YAMON>'
143        pattern = 'YAMON ROM Monitor'
144        interrupt_interactive_console_until_pattern(self, pattern, prompt)
145        wait_for_console_pattern(self, prompt)
146        self.vm.shutdown()
147
148    def test_mipsel_malta_yamon(self):
149        """
150        :avocado: tags=arch:mipsel
151        :avocado: tags=machine:malta
152        :avocado: tags=endian:little
153        """
154        self.do_test_yamon()
155
156    def test_mips64el_malta_yamon(self):
157        """
158        :avocado: tags=arch:mips64el
159        :avocado: tags=machine:malta
160        :avocado: tags=endian:little
161        """
162        self.do_test_yamon()
163