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
33@skipUnless(NUMPY_AVAILABLE, 'Python NumPy not installed')
34@skipUnless(CV2_AVAILABLE, 'Python OpenCV not installed')
35class MaltaMachineFramebuffer(LinuxKernelTest):
36
37    timeout = 30
38
39    ASSET_KERNEL_4_7_0 = Asset(
40        ('https://github.com/philmd/qemu-testing-blob/raw/a5966ca4b5/'
41         'mips/malta/mips64el/vmlinux-4.7.0-rc1.I6400.gz'),
42        '1f64efc59968a3c328672e6b10213fe574bb2308d9d2ed44e75e40be59e9fbc2')
43
44    ASSET_TUXLOGO = Asset(
45        ('https://github.com/torvalds/linux/raw/v2.6.12/'
46         'drivers/video/logo/logo_linux_vga16.ppm'),
47        'b762f0d91ec018887ad1b334543c2fdf9be9fdfc87672b409211efaa3ea0ef79')
48
49    def do_test_i6400_framebuffer_logo(self, cpu_cores_count):
50        """
51        Boot Linux kernel and check Tux logo is displayed on the framebuffer.
52        """
53        screendump_path = os.path.join(self.workdir, 'screendump.pbm')
54
55        kernel_path_gz = self.ASSET_KERNEL_4_7_0.fetch()
56        kernel_path = self.workdir + "vmlinux"
57        gzip_uncompress(kernel_path_gz, kernel_path)
58
59        tuxlogo_path = self.ASSET_TUXLOGO.fetch()
60
61        self.set_machine('malta')
62        self.vm.set_console()
63        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
64                               'clocksource=GIC console=tty0 console=ttyS0')
65        self.vm.add_args('-kernel', kernel_path,
66                         '-cpu', 'I6400',
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        self.wait_for_console_pattern(framebuffer_ready)
73        self.vm.cmd('human-monitor-command', command_line='stop')
74        self.vm.cmd('human-monitor-command',
75                    command_line='screendump %s' % screendump_path)
76        logger = logging.getLogger('framebuffer')
77
78        match_threshold = 0.95
79        screendump_bgr = cv2.imread(screendump_path, cv2.IMREAD_COLOR)
80        tuxlogo_bgr = cv2.imread(tuxlogo_path, cv2.IMREAD_COLOR)
81        result = cv2.matchTemplate(screendump_bgr, tuxlogo_bgr,
82                                   cv2.TM_CCOEFF_NORMED)
83        loc = np.where(result >= match_threshold)
84        tuxlogo_count = 0
85        h, w = tuxlogo_bgr.shape[:2]
86        debug_png = os.getenv('AVOCADO_CV2_SCREENDUMP_PNG_PATH')
87        for tuxlogo_count, pt in enumerate(zip(*loc[::-1]), start=1):
88            logger.debug('found Tux at position (x, y) = %s', pt)
89            cv2.rectangle(screendump_bgr, pt,
90                          (pt[0] + w, pt[1] + h), (0, 0, 255), 2)
91        if debug_png:
92            cv2.imwrite(debug_png, screendump_bgr)
93        self.assertGreaterEqual(tuxlogo_count, cpu_cores_count)
94
95    def test_mips_malta_i6400_framebuffer_logo_1core(self):
96        self.do_test_i6400_framebuffer_logo(1)
97
98    @skipUnless(os.getenv('QEMU_TEST_FLAKY_TESTS'), 'Test is unstable on GitLab')
99    def test_mips_malta_i6400_framebuffer_logo_7cores(self):
100        self.do_test_i6400_framebuffer_logo(7)
101
102    @skipUnless(os.getenv('QEMU_TEST_FLAKY_TESTS'), 'Test is unstable on GitLab')
103    def test_mips_malta_i6400_framebuffer_logo_8cores(self):
104        self.do_test_i6400_framebuffer_logo(8)
105
106
107if __name__ == '__main__':
108    LinuxKernelTest.main()
109