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 skip
12from avocado import skipUnless
13from avocado.utils import archive
14
15from avocado_qemu import QemuSystemTest
16from avocado_qemu import wait_for_console_pattern
17from avocado_qemu import interrupt_interactive_console_until_pattern
18
19
20class Aarch64SbsarefMachine(QemuSystemTest):
21    """
22    :avocado: tags=arch:aarch64
23    :avocado: tags=machine:sbsa-ref
24    """
25
26    timeout = 180
27
28    def fetch_firmware(self):
29        """
30        Flash volumes generated using:
31
32        - Fedora GNU Toolchain version 12.2.1 20220819 (Red Hat Cross 12.2.1-2)
33
34        - Trusted Firmware-A
35          https://github.com/ARM-software/arm-trusted-firmware/tree/5fdb2e54
36
37        - Tianocore EDK II
38          https://github.com/tianocore/edk2/tree/494127613b
39          https://github.com/tianocore/edk2-non-osi/tree/41876073
40          https://github.com/tianocore/edk2-platforms/tree/8efa4f42
41        """
42
43        # Secure BootRom (TF-A code)
44        fs0_xz_url = (
45            "https://fileserver.linaro.org/s/ATnSmq6k8SoXgbH/"
46            "download/SBSA_FLASH0.fd.xz"
47        )
48        fs0_xz_hash = "a210a09692bcbe0a3743ffd0df44e80e0c7ad8ab"
49        tar_xz_path = self.fetch_asset(fs0_xz_url, asset_hash=fs0_xz_hash)
50        archive.extract(tar_xz_path, self.workdir)
51        fs0_path = os.path.join(self.workdir, "SBSA_FLASH0.fd")
52
53        # Non-secure rom (UEFI and EFI variables)
54        fs1_xz_url = (
55            "https://fileserver.linaro.org/s/t8foNnMPz74DZZy/"
56            "download/SBSA_FLASH1.fd.xz"
57        )
58        fs1_xz_hash = "13a9a262953787c7fc5a9155dfaa26e703631e02"
59        tar_xz_path = self.fetch_asset(fs1_xz_url, asset_hash=fs1_xz_hash)
60        archive.extract(tar_xz_path, self.workdir)
61        fs1_path = os.path.join(self.workdir, "SBSA_FLASH1.fd")
62
63        for path in [fs0_path, fs1_path]:
64            with open(path, "ab+") as fd:
65                fd.truncate(256 << 20)  # Expand volumes to 256MiB
66
67        self.vm.set_console()
68        self.vm.add_args(
69            "-drive",
70            f"if=pflash,file={fs0_path},format=raw",
71            "-drive",
72            f"if=pflash,file={fs1_path},format=raw",
73            "-smp",
74            "1",
75            "-machine",
76            "sbsa-ref",
77        )
78
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.8(release):v2.8")
97        wait_for_console_pattern(self, "BL1: Booting BL2")
98
99        # Trusted Boot Firmware
100        wait_for_console_pattern(self, "BL2: v2.8(release)")
101        wait_for_console_pattern(self, "Booting BL31")
102
103        # EL3 Runtime Software
104        wait_for_console_pattern(self, "BL31: v2.8(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    @skipUnless(os.getenv("AVOCADO_TIMEOUT_EXPECTED"), "Test might timeout")
140    def test_sbsaref_alpine_linux_cortex_a57(self):
141        """
142        :avocado: tags=cpu:cortex-a57
143        """
144        self.boot_alpine_linux("cortex-a57")
145
146    @skipUnless(os.getenv("AVOCADO_TIMEOUT_EXPECTED"), "Test might timeout")
147    def test_sbsaref_alpine_linux_neoverse_n1(self):
148        """
149        :avocado: tags=cpu:max
150        """
151        self.boot_alpine_linux("neoverse-n1")
152
153    @skip("requires TF-A update to handle FEAT_FGT")
154    def test_sbsaref_alpine_linux_max(self):
155        """
156        :avocado: tags=cpu:max
157        """
158        self.boot_alpine_linux("max,pauth-impdef=on")
159