1# Functional test that boots a Linux kernel and checks the console
2#
3# SPDX-FileCopyrightText: 2023 Linaro Ltd.
4# SPDX-FileContributor: Philippe Mathieu-Daudé <philmd@linaro.org>
5# SPDX-FileContributor: Marcin Juszkiewicz <marcin.juszkiewicz@linaro.org>
6#
7# SPDX-License-Identifier: GPL-2.0-or-later
8
9import os
10
11from avocado import skipUnless
12from avocado.utils import archive
13
14from avocado_qemu import QemuSystemTest
15from avocado_qemu import wait_for_console_pattern
16from avocado_qemu import interrupt_interactive_console_until_pattern
17
18
19class Aarch64SbsarefMachine(QemuSystemTest):
20    """
21    :avocado: tags=arch:aarch64
22    :avocado: tags=machine:sbsa-ref
23    """
24
25    timeout = 180
26
27    def fetch_firmware(self):
28        """
29        Flash volumes generated using:
30
31        - Fedora GNU Toolchain version 13.1.1 20230511 (Red Hat 13.1.1-2)
32
33        - Trusted Firmware-A
34          https://github.com/ARM-software/arm-trusted-firmware/tree/c0d8ee38
35
36        - Tianocore EDK II
37          https://github.com/tianocore/edk2/tree/0f9283429dd4
38          https://github.com/tianocore/edk2-non-osi/tree/f0bb00937ad6
39          https://github.com/tianocore/edk2-platforms/tree/7880b92e2a04
40        """
41
42        # Secure BootRom (TF-A code)
43        fs0_xz_url = (
44            "https://fileserver.linaro.org/s/HrYMCjP7MEccjRP/"
45            "download/SBSA_FLASH0.fd.xz"
46        )
47        fs0_xz_hash = "447eff64a90b84ce47703c6ec41fbfc25befaaea"
48        tar_xz_path = self.fetch_asset(fs0_xz_url, asset_hash=fs0_xz_hash)
49        archive.extract(tar_xz_path, self.workdir)
50        fs0_path = os.path.join(self.workdir, "SBSA_FLASH0.fd")
51
52        # Non-secure rom (UEFI and EFI variables)
53        fs1_xz_url = (
54            "https://fileserver.linaro.org/s/t8foNnMPz74DZZy/"
55            "download/SBSA_FLASH1.fd.xz"
56        )
57        fs1_xz_hash = "13a9a262953787c7fc5a9155dfaa26e703631e02"
58        tar_xz_path = self.fetch_asset(fs1_xz_url, asset_hash=fs1_xz_hash)
59        archive.extract(tar_xz_path, self.workdir)
60        fs1_path = os.path.join(self.workdir, "SBSA_FLASH1.fd")
61
62        for path in [fs0_path, fs1_path]:
63            with open(path, "ab+") as fd:
64                fd.truncate(256 << 20)  # Expand volumes to 256MiB
65
66        self.vm.set_console()
67        self.vm.add_args(
68            "-drive",
69            f"if=pflash,file={fs0_path},format=raw",
70            "-drive",
71            f"if=pflash,file={fs1_path},format=raw",
72            "-smp",
73            "1",
74            "-machine",
75            "sbsa-ref",
76        )
77
78    @skipUnless(os.getenv('QEMU_TEST_FLAKY_TESTS'), 'Test is not reliable')
79    def test_sbsaref_edk2_firmware(self):
80        """
81        :avocado: tags=cpu:cortex-a57
82        """
83
84        self.fetch_firmware()
85        self.vm.launch()
86
87        # TF-A boot sequence:
88        #
89        # https://github.com/ARM-software/arm-trusted-firmware/blob/v2.8.0/\
90        #     docs/design/trusted-board-boot.rst#trusted-board-boot-sequence
91        # https://trustedfirmware-a.readthedocs.io/en/v2.8/\
92        #     design/firmware-design.html#cold-boot
93
94        # AP Trusted ROM
95        wait_for_console_pattern(self, "Booting Trusted Firmware")
96        wait_for_console_pattern(self, "BL1: v2.9(release):v2.9")
97        wait_for_console_pattern(self, "BL1: Booting BL2")
98
99        # Trusted Boot Firmware
100        wait_for_console_pattern(self, "BL2: v2.9(release)")
101        wait_for_console_pattern(self, "Booting BL31")
102
103        # EL3 Runtime Software
104        wait_for_console_pattern(self, "BL31: v2.9(release)")
105
106        # Non-trusted Firmware
107        wait_for_console_pattern(self, "UEFI firmware (version 1.0")
108        interrupt_interactive_console_until_pattern(self, "QEMU SBSA-REF Machine")
109
110    # This tests the whole boot chain from EFI to Userspace
111    # We only boot a whole OS for the current top level CPU and GIC
112    # Other test profiles should use more minimal boots
113    def boot_alpine_linux(self, cpu):
114        self.fetch_firmware()
115
116        iso_url = (
117            "https://dl-cdn.alpinelinux.org/"
118            "alpine/v3.17/releases/aarch64/alpine-standard-3.17.2-aarch64.iso"
119        )
120
121        iso_hash = "5a36304ecf039292082d92b48152a9ec21009d3a62f459de623e19c4bd9dc027"
122        iso_path = self.fetch_asset(iso_url, algorithm="sha256", asset_hash=iso_hash)
123
124        self.vm.set_console()
125        self.vm.add_args(
126            "-cpu",
127            cpu,
128            "-drive",
129            f"file={iso_path},format=raw",
130            "-device",
131            "virtio-rng-pci,rng=rng0",
132            "-object",
133            "rng-random,id=rng0,filename=/dev/urandom",
134        )
135
136        self.vm.launch()
137        wait_for_console_pattern(self, "Welcome to Alpine Linux 3.17")
138
139    def test_sbsaref_alpine_linux_cortex_a57(self):
140        """
141        :avocado: tags=cpu:cortex-a57
142        """
143        self.boot_alpine_linux("cortex-a57")
144
145    def test_sbsaref_alpine_linux_neoverse_n1(self):
146        """
147        :avocado: tags=cpu:max
148        """
149        self.boot_alpine_linux("neoverse-n1")
150
151    def test_sbsaref_alpine_linux_max(self):
152        """
153        :avocado: tags=cpu:max
154        """
155        self.boot_alpine_linux("max,pauth-impdef=on")
156