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 skipIf
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.command('human-monitor-command', command_line='stop')
75        self.vm.command('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    @skipIf(os.getenv('GITLAB_CI'), 'Running 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        """
112        self.do_test_i6400_framebuffer_logo(7)
113
114    @skipIf(os.getenv('GITLAB_CI'), 'Running on GitLab')
115    def test_mips_malta_i6400_framebuffer_logo_8cores(self):
116        """
117        :avocado: tags=arch:mips64el
118        :avocado: tags=machine:malta
119        :avocado: tags=cpu:I6400
120        :avocado: tags=mips:smp
121        """
122        self.do_test_i6400_framebuffer_logo(8)
123
124class MaltaMachine(QemuSystemTest):
125
126    def do_test_yamon(self):
127        rom_url = ('http://www.imgtec.com/tools/mips-tools/downloads/'
128                   'yamon/yamon-bin-02.22.zip')
129        rom_hash = '8da7ecddbc5312704b8b324341ee238189bde480'
130        zip_path = self.fetch_asset(rom_url, asset_hash=rom_hash)
131
132        archive.extract(zip_path, self.workdir)
133        yamon_path = os.path.join(self.workdir, 'yamon-02.22.bin')
134
135        self.vm.set_console()
136        self.vm.add_args('-bios', yamon_path)
137        self.vm.launch()
138
139        prompt =  'YAMON>'
140        pattern = 'YAMON ROM Monitor'
141        interrupt_interactive_console_until_pattern(self, pattern, prompt)
142        wait_for_console_pattern(self, prompt)
143        self.vm.shutdown()
144
145    def test_mipsel_malta_yamon(self):
146        """
147        :avocado: tags=arch:mipsel
148        :avocado: tags=machine:malta
149        :avocado: tags=endian:little
150        """
151        self.do_test_yamon()
152
153    def test_mips64el_malta_yamon(self):
154        """
155        :avocado: tags=arch:mips64el
156        :avocado: tags=machine:malta
157        :avocado: tags=endian:little
158        """
159        self.do_test_yamon()
160