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_qemu import QemuSystemTest
16from avocado_qemu import wait_for_console_pattern
17from avocado.utils import archive
18from avocado import skipIf
19
20
21NUMPY_AVAILABLE = True
22try:
23    import numpy as np
24except ImportError:
25    NUMPY_AVAILABLE = False
26
27CV2_AVAILABLE = True
28try:
29    import cv2
30except ImportError:
31    CV2_AVAILABLE = False
32
33
34@skipUnless(NUMPY_AVAILABLE, 'Python NumPy not installed')
35@skipUnless(CV2_AVAILABLE, 'Python OpenCV not installed')
36class MaltaMachineFramebuffer(QemuSystemTest):
37
38    timeout = 30
39
40    KERNEL_COMMON_COMMAND_LINE = 'printk.time=0 '
41
42    def do_test_i6400_framebuffer_logo(self, cpu_cores_count):
43        """
44        Boot Linux kernel and check Tux logo is displayed on the framebuffer.
45        """
46        screendump_path = os.path.join(self.workdir, 'screendump.pbm')
47
48        kernel_url = ('https://github.com/philmd/qemu-testing-blob/raw/'
49                      'a5966ca4b5/mips/malta/mips64el/'
50                      'vmlinux-4.7.0-rc1.I6400.gz')
51        kernel_hash = '096f50c377ec5072e6a366943324622c312045f6'
52        kernel_path_gz = self.fetch_asset(kernel_url, asset_hash=kernel_hash)
53        kernel_path = self.workdir + "vmlinux"
54        archive.gzip_uncompress(kernel_path_gz, kernel_path)
55
56        tuxlogo_url = ('https://github.com/torvalds/linux/raw/v2.6.12/'
57                       'drivers/video/logo/logo_linux_vga16.ppm')
58        tuxlogo_hash = '3991c2ddbd1ddaecda7601f8aafbcf5b02dc86af'
59        tuxlogo_path = self.fetch_asset(tuxlogo_url, asset_hash=tuxlogo_hash)
60
61        self.vm.set_console()
62        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
63                               'clocksource=GIC console=tty0 console=ttyS0')
64        self.vm.add_args('-kernel', kernel_path,
65                         '-smp', '%u' % cpu_cores_count,
66                         '-vga', 'std',
67                         '-append', kernel_command_line)
68        self.vm.launch()
69        framebuffer_ready = 'Console: switching to colour frame buffer device'
70        wait_for_console_pattern(self, framebuffer_ready,
71                                 failure_message='Kernel panic - not syncing')
72        self.vm.command('human-monitor-command', command_line='stop')
73        self.vm.command('human-monitor-command',
74                        command_line='screendump %s' % screendump_path)
75        logger = logging.getLogger('framebuffer')
76
77        match_threshold = 0.95
78        screendump_bgr = cv2.imread(screendump_path, cv2.IMREAD_COLOR)
79        tuxlogo_bgr = cv2.imread(tuxlogo_path, cv2.IMREAD_COLOR)
80        result = cv2.matchTemplate(screendump_bgr, tuxlogo_bgr,
81                                   cv2.TM_CCOEFF_NORMED)
82        loc = np.where(result >= match_threshold)
83        tuxlogo_count = 0
84        h, w = tuxlogo_bgr.shape[:2]
85        debug_png = os.getenv('AVOCADO_CV2_SCREENDUMP_PNG_PATH')
86        for tuxlogo_count, pt in enumerate(zip(*loc[::-1]), start=1):
87            logger.debug('found Tux at position (x, y) = %s', pt)
88            cv2.rectangle(screendump_bgr, pt,
89                          (pt[0] + w, pt[1] + h), (0, 0, 255), 2)
90        if debug_png:
91            cv2.imwrite(debug_png, screendump_bgr)
92        self.assertGreaterEqual(tuxlogo_count, cpu_cores_count)
93
94    def test_mips_malta_i6400_framebuffer_logo_1core(self):
95        """
96        :avocado: tags=arch:mips64el
97        :avocado: tags=machine:malta
98        :avocado: tags=cpu:I6400
99        """
100        self.do_test_i6400_framebuffer_logo(1)
101
102    @skipIf(os.getenv('GITLAB_CI'), 'Running on GitLab')
103    def test_mips_malta_i6400_framebuffer_logo_7cores(self):
104        """
105        :avocado: tags=arch:mips64el
106        :avocado: tags=machine:malta
107        :avocado: tags=cpu:I6400
108        :avocado: tags=mips:smp
109        """
110        self.do_test_i6400_framebuffer_logo(7)
111
112    @skipIf(os.getenv('GITLAB_CI'), 'Running on GitLab')
113    def test_mips_malta_i6400_framebuffer_logo_8cores(self):
114        """
115        :avocado: tags=arch:mips64el
116        :avocado: tags=machine:malta
117        :avocado: tags=cpu:I6400
118        :avocado: tags=mips:smp
119        """
120        self.do_test_i6400_framebuffer_logo(8)
121