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         v2.11.0
41        - Tianocore EDK2           4d4f569924
42        - Tianocore EDK2-platforms 3f08401
43
44        """
45
46        # Secure BootRom (TF-A code)
47        fs0_xz_url = (
48            "https://artifacts.codelinaro.org/artifactory/linaro-419-sbsa-ref/"
49            "20240619-148232/edk2/SBSA_FLASH0.fd.xz"
50        )
51        fs0_xz_hash = "0c954842a590988f526984de22e21ae0ab9cb351a0c99a8a58e928f0c7359cf7"
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            "20240619-148232/edk2/SBSA_FLASH1.fd.xz"
61        )
62        fs1_xz_hash = "c6ec39374c4d79bb9e9cdeeb6db44732d90bb4a334cec92002b3f4b9cac4b5ee"
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            "-machine",
79            "sbsa-ref",
80        )
81
82    def test_sbsaref_edk2_firmware(self):
83        """
84        :avocado: tags=cpu:cortex-a57
85        """
86
87        self.fetch_firmware()
88        self.vm.launch()
89
90        # TF-A boot sequence:
91        #
92        # https://github.com/ARM-software/arm-trusted-firmware/blob/v2.8.0/\
93        #     docs/design/trusted-board-boot.rst#trusted-board-boot-sequence
94        # https://trustedfirmware-a.readthedocs.io/en/v2.8/\
95        #     design/firmware-design.html#cold-boot
96
97        # AP Trusted ROM
98        wait_for_console_pattern(self, "Booting Trusted Firmware")
99        wait_for_console_pattern(self, "BL1: v2.11.0(release):")
100        wait_for_console_pattern(self, "BL1: Booting BL2")
101
102        # Trusted Boot Firmware
103        wait_for_console_pattern(self, "BL2: v2.11.0(release)")
104        wait_for_console_pattern(self, "Booting BL31")
105
106        # EL3 Runtime Software
107        wait_for_console_pattern(self, "BL31: v2.11.0(release)")
108
109        # Non-trusted Firmware
110        wait_for_console_pattern(self, "UEFI firmware (version 1.0")
111        interrupt_interactive_console_until_pattern(self, "QEMU SBSA-REF Machine")
112
113    # This tests the whole boot chain from EFI to Userspace
114    # We only boot a whole OS for the current top level CPU and GIC
115    # Other test profiles should use more minimal boots
116    def boot_alpine_linux(self, cpu):
117        self.fetch_firmware()
118
119        iso_url = (
120            "https://dl-cdn.alpinelinux.org/"
121            "alpine/v3.17/releases/aarch64/alpine-standard-3.17.2-aarch64.iso"
122        )
123
124        iso_hash = "5a36304ecf039292082d92b48152a9ec21009d3a62f459de623e19c4bd9dc027"
125        iso_path = self.fetch_asset(iso_url, algorithm="sha256", asset_hash=iso_hash)
126
127        self.vm.set_console()
128        self.vm.add_args(
129            "-cpu",
130            cpu,
131            "-drive",
132            f"file={iso_path},format=raw",
133        )
134
135        self.vm.launch()
136        wait_for_console_pattern(self, "Welcome to Alpine Linux 3.17")
137
138    def test_sbsaref_alpine_linux_cortex_a57(self):
139        """
140        :avocado: tags=cpu:cortex-a57
141        :avocado: tags=os:linux
142        """
143        self.boot_alpine_linux("cortex-a57")
144
145    def test_sbsaref_alpine_linux_neoverse_n1(self):
146        """
147        :avocado: tags=cpu:neoverse-n1
148        :avocado: tags=os:linux
149        """
150        self.boot_alpine_linux("neoverse-n1")
151
152    def test_sbsaref_alpine_linux_max_pauth_off(self):
153        """
154        :avocado: tags=cpu:max
155        :avocado: tags=os:linux
156        """
157        self.boot_alpine_linux("max,pauth=off")
158
159    def test_sbsaref_alpine_linux_max_pauth_impdef(self):
160        """
161        :avocado: tags=cpu:max
162        :avocado: tags=os:linux
163        """
164        self.boot_alpine_linux("max,pauth-impdef=on")
165
166    @skipUnless(os.getenv('AVOCADO_TIMEOUT_EXPECTED'), 'Test might timeout')
167    def test_sbsaref_alpine_linux_max(self):
168        """
169        :avocado: tags=cpu:max
170        :avocado: tags=os:linux
171        """
172        self.boot_alpine_linux("max")
173
174
175    # This tests the whole boot chain from EFI to Userspace
176    # We only boot a whole OS for the current top level CPU and GIC
177    # Other test profiles should use more minimal boots
178    def boot_openbsd73(self, cpu):
179        self.fetch_firmware()
180
181        img_url = (
182            "https://cdn.openbsd.org/pub/OpenBSD/7.3/arm64/miniroot73.img"
183        )
184
185        img_hash = "7fc2c75401d6f01fbfa25f4953f72ad7d7c18650056d30755c44b9c129b707e5"
186        img_path = self.fetch_asset(img_url, algorithm="sha256", asset_hash=img_hash)
187
188        self.vm.set_console()
189        self.vm.add_args(
190            "-cpu",
191            cpu,
192            "-drive",
193            f"file={img_path},format=raw",
194        )
195
196        self.vm.launch()
197        wait_for_console_pattern(self,
198                                 "Welcome to the OpenBSD/arm64"
199                                 " 7.3 installation program.")
200
201    def test_sbsaref_openbsd73_cortex_a57(self):
202        """
203        :avocado: tags=cpu:cortex-a57
204        :avocado: tags=os:openbsd
205        """
206        self.boot_openbsd73("cortex-a57")
207
208    def test_sbsaref_openbsd73_neoverse_n1(self):
209        """
210        :avocado: tags=cpu:neoverse-n1
211        :avocado: tags=os:openbsd
212        """
213        self.boot_openbsd73("neoverse-n1")
214
215    def test_sbsaref_openbsd73_max_pauth_off(self):
216        """
217        :avocado: tags=cpu:max
218        :avocado: tags=os:openbsd
219        """
220        self.boot_openbsd73("max,pauth=off")
221
222    @skipUnless(os.getenv('AVOCADO_TIMEOUT_EXPECTED'), 'Test might timeout')
223    def test_sbsaref_openbsd73_max_pauth_impdef(self):
224        """
225        :avocado: tags=cpu:max
226        :avocado: tags=os:openbsd
227        """
228        self.boot_openbsd73("max,pauth-impdef=on")
229
230    @skipUnless(os.getenv('AVOCADO_TIMEOUT_EXPECTED'), 'Test might timeout')
231    def test_sbsaref_openbsd73_max(self):
232        """
233        :avocado: tags=cpu:max
234        :avocado: tags=os:openbsd
235        """
236        self.boot_openbsd73("max")
237