1# Functional test that boots a Linux kernel and checks the console
2#
3# SPDX-FileCopyrightText: 2023-2024 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    :avocado: tags=accel:tcg
24
25    As firmware runs at a higher privilege level than the hypervisor we
26    can only run these tests under TCG emulation.
27    """
28
29    timeout = 180
30
31    def fetch_firmware(self):
32        """
33        Flash volumes generated using:
34
35        Toolchain from Debian:
36        aarch64-linux-gnu-gcc (Debian 12.2.0-14) 12.2.0
37
38        Used components:
39
40        - Trusted Firmware 2.10.2
41        - Tianocore EDK2 stable202402
42        - Tianocore EDK2-platforms commit 085c2fb
43
44        """
45
46        # Secure BootRom (TF-A code)
47        fs0_xz_url = (
48            "https://artifacts.codelinaro.org/artifactory/linaro-419-sbsa-ref/"
49            "20240313-116475/edk2/SBSA_FLASH0.fd.xz"
50        )
51        fs0_xz_hash = "637593749cc307dea7dc13265c32e5d020267552f22b18a31850b8429fc5e159"
52        tar_xz_path = self.fetch_asset(fs0_xz_url, asset_hash=fs0_xz_hash,
53                                      algorithm='sha256')
54        archive.extract(tar_xz_path, self.workdir)
55        fs0_path = os.path.join(self.workdir, "SBSA_FLASH0.fd")
56
57        # Non-secure rom (UEFI and EFI variables)
58        fs1_xz_url = (
59            "https://artifacts.codelinaro.org/artifactory/linaro-419-sbsa-ref/"
60            "20240313-116475/edk2/SBSA_FLASH1.fd.xz"
61        )
62        fs1_xz_hash = "cb0a5e8cf5e303c5d3dc106cfd5943ffe9714b86afddee7164c69ee1dd41991c"
63        tar_xz_path = self.fetch_asset(fs1_xz_url, asset_hash=fs1_xz_hash,
64                                      algorithm='sha256')
65        archive.extract(tar_xz_path, self.workdir)
66        fs1_path = os.path.join(self.workdir, "SBSA_FLASH1.fd")
67
68        for path in [fs0_path, fs1_path]:
69            with open(path, "ab+") as fd:
70                fd.truncate(256 << 20)  # Expand volumes to 256MiB
71
72        self.vm.set_console()
73        self.vm.add_args(
74            "-drive",
75            f"if=pflash,file={fs0_path},format=raw",
76            "-drive",
77            f"if=pflash,file={fs1_path},format=raw",
78            "-smp",
79            "1",
80            "-machine",
81            "sbsa-ref",
82        )
83
84    def test_sbsaref_edk2_firmware(self):
85        """
86        :avocado: tags=cpu:cortex-a57
87        """
88
89        self.fetch_firmware()
90        self.vm.launch()
91
92        # TF-A boot sequence:
93        #
94        # https://github.com/ARM-software/arm-trusted-firmware/blob/v2.8.0/\
95        #     docs/design/trusted-board-boot.rst#trusted-board-boot-sequence
96        # https://trustedfirmware-a.readthedocs.io/en/v2.8/\
97        #     design/firmware-design.html#cold-boot
98
99        # AP Trusted ROM
100        wait_for_console_pattern(self, "Booting Trusted Firmware")
101        wait_for_console_pattern(self, "BL1: v2.10.2(release):")
102        wait_for_console_pattern(self, "BL1: Booting BL2")
103
104        # Trusted Boot Firmware
105        wait_for_console_pattern(self, "BL2: v2.10.2(release)")
106        wait_for_console_pattern(self, "Booting BL31")
107
108        # EL3 Runtime Software
109        wait_for_console_pattern(self, "BL31: v2.10.2(release)")
110
111        # Non-trusted Firmware
112        wait_for_console_pattern(self, "UEFI firmware (version 1.0")
113        interrupt_interactive_console_until_pattern(self, "QEMU SBSA-REF Machine")
114
115    # This tests the whole boot chain from EFI to Userspace
116    # We only boot a whole OS for the current top level CPU and GIC
117    # Other test profiles should use more minimal boots
118    def boot_alpine_linux(self, cpu):
119        self.fetch_firmware()
120
121        iso_url = (
122            "https://dl-cdn.alpinelinux.org/"
123            "alpine/v3.17/releases/aarch64/alpine-standard-3.17.2-aarch64.iso"
124        )
125
126        iso_hash = "5a36304ecf039292082d92b48152a9ec21009d3a62f459de623e19c4bd9dc027"
127        iso_path = self.fetch_asset(iso_url, algorithm="sha256", asset_hash=iso_hash)
128
129        self.vm.set_console()
130        self.vm.add_args(
131            "-cpu",
132            cpu,
133            "-drive",
134            f"file={iso_path},format=raw",
135        )
136
137        self.vm.launch()
138        wait_for_console_pattern(self, "Welcome to Alpine Linux 3.17")
139
140    def test_sbsaref_alpine_linux_cortex_a57(self):
141        """
142        :avocado: tags=cpu:cortex-a57
143        :avocado: tags=os:linux
144        """
145        self.boot_alpine_linux("cortex-a57")
146
147    def test_sbsaref_alpine_linux_neoverse_n1(self):
148        """
149        :avocado: tags=cpu:neoverse-n1
150        :avocado: tags=os:linux
151        """
152        self.boot_alpine_linux("neoverse-n1")
153
154    def test_sbsaref_alpine_linux_max_pauth_off(self):
155        """
156        :avocado: tags=cpu:max
157        :avocado: tags=os:linux
158        """
159        self.boot_alpine_linux("max,pauth=off")
160
161    def test_sbsaref_alpine_linux_max_pauth_impdef(self):
162        """
163        :avocado: tags=cpu:max
164        :avocado: tags=os:linux
165        """
166        self.boot_alpine_linux("max,pauth-impdef=on")
167
168    @skipUnless(os.getenv('AVOCADO_TIMEOUT_EXPECTED'), 'Test might timeout')
169    def test_sbsaref_alpine_linux_max(self):
170        """
171        :avocado: tags=cpu:max
172        :avocado: tags=os:linux
173        """
174        self.boot_alpine_linux("max")
175
176
177    # This tests the whole boot chain from EFI to Userspace
178    # We only boot a whole OS for the current top level CPU and GIC
179    # Other test profiles should use more minimal boots
180    def boot_openbsd73(self, cpu):
181        self.fetch_firmware()
182
183        img_url = (
184            "https://cdn.openbsd.org/pub/OpenBSD/7.3/arm64/miniroot73.img"
185        )
186
187        img_hash = "7fc2c75401d6f01fbfa25f4953f72ad7d7c18650056d30755c44b9c129b707e5"
188        img_path = self.fetch_asset(img_url, algorithm="sha256", asset_hash=img_hash)
189
190        self.vm.set_console()
191        self.vm.add_args(
192            "-cpu",
193            cpu,
194            "-drive",
195            f"file={img_path},format=raw",
196        )
197
198        self.vm.launch()
199        wait_for_console_pattern(self,
200                                 "Welcome to the OpenBSD/arm64"
201                                 " 7.3 installation program.")
202
203    def test_sbsaref_openbsd73_cortex_a57(self):
204        """
205        :avocado: tags=cpu:cortex-a57
206        :avocado: tags=os:openbsd
207        """
208        self.boot_openbsd73("cortex-a57")
209
210    def test_sbsaref_openbsd73_neoverse_n1(self):
211        """
212        :avocado: tags=cpu:neoverse-n1
213        :avocado: tags=os:openbsd
214        """
215        self.boot_openbsd73("neoverse-n1")
216
217    def test_sbsaref_openbsd73_max_pauth_off(self):
218        """
219        :avocado: tags=cpu:max
220        :avocado: tags=os:openbsd
221        """
222        self.boot_openbsd73("max,pauth=off")
223
224    @skipUnless(os.getenv('AVOCADO_TIMEOUT_EXPECTED'), 'Test might timeout')
225    def test_sbsaref_openbsd73_max_pauth_impdef(self):
226        """
227        :avocado: tags=cpu:max
228        :avocado: tags=os:openbsd
229        """
230        self.boot_openbsd73("max,pauth-impdef=on")
231
232    @skipUnless(os.getenv('AVOCADO_TIMEOUT_EXPECTED'), 'Test might timeout')
233    def test_sbsaref_openbsd73_max(self):
234        """
235        :avocado: tags=cpu:max
236        :avocado: tags=os:openbsd
237        """
238        self.boot_openbsd73("max")
239