1#!/usr/bin/env python3
2#
3# Functional test that boots a kernel and checks the console
4#
5# SPDX-FileCopyrightText: 2023-2024 Linaro Ltd.
6# SPDX-FileContributor: Philippe Mathieu-Daudé <philmd@linaro.org>
7# SPDX-FileContributor: Marcin Juszkiewicz <marcin.juszkiewicz@linaro.org>
8#
9# SPDX-License-Identifier: GPL-2.0-or-later
10
11import os
12
13from qemu_test import QemuSystemTest, Asset
14from qemu_test import wait_for_console_pattern
15from qemu_test import interrupt_interactive_console_until_pattern
16from qemu_test.utils import lzma_uncompress
17from unittest import skipUnless
18
19
20class Aarch64SbsarefMachine(QemuSystemTest):
21    """
22    As firmware runs at a higher privilege level than the hypervisor we
23    can only run these tests under TCG emulation.
24    """
25
26    timeout = 180
27
28    ASSET_FLASH0 = Asset(
29        ('https://artifacts.codelinaro.org/artifactory/linaro-419-sbsa-ref/'
30         '20240619-148232/edk2/SBSA_FLASH0.fd.xz'),
31        '0c954842a590988f526984de22e21ae0ab9cb351a0c99a8a58e928f0c7359cf7')
32
33    ASSET_FLASH1 = Asset(
34        ('https://artifacts.codelinaro.org/artifactory/linaro-419-sbsa-ref/'
35         '20240619-148232/edk2/SBSA_FLASH1.fd.xz'),
36        'c6ec39374c4d79bb9e9cdeeb6db44732d90bb4a334cec92002b3f4b9cac4b5ee')
37
38    def fetch_firmware(self):
39        """
40        Flash volumes generated using:
41
42        Toolchain from Debian:
43        aarch64-linux-gnu-gcc (Debian 12.2.0-14) 12.2.0
44
45        Used components:
46
47        - Trusted Firmware         v2.11.0
48        - Tianocore EDK2           4d4f569924
49        - Tianocore EDK2-platforms 3f08401
50
51        """
52
53        # Secure BootRom (TF-A code)
54        fs0_xz_path = self.ASSET_FLASH0.fetch()
55        fs0_path = os.path.join(self.workdir, "SBSA_FLASH0.fd")
56        lzma_uncompress(fs0_xz_path, fs0_path)
57
58        # Non-secure rom (UEFI and EFI variables)
59        fs1_xz_path = self.ASSET_FLASH1.fetch()
60        fs1_path = os.path.join(self.workdir, "SBSA_FLASH1.fd")
61        lzma_uncompress(fs1_xz_path, fs1_path)
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.set_machine('sbsa-ref')
68        self.vm.set_console()
69        self.vm.add_args(
70            "-drive", f"if=pflash,file={fs0_path},format=raw",
71            "-drive", f"if=pflash,file={fs1_path},format=raw",
72        )
73
74    def test_sbsaref_edk2_firmware(self):
75
76        self.fetch_firmware()
77
78        self.vm.add_args('-cpu', 'cortex-a57')
79        self.vm.launch()
80
81        # TF-A boot sequence:
82        #
83        # https://github.com/ARM-software/arm-trusted-firmware/blob/v2.8.0/\
84        #     docs/design/trusted-board-boot.rst#trusted-board-boot-sequence
85        # https://trustedfirmware-a.readthedocs.io/en/v2.8/\
86        #     design/firmware-design.html#cold-boot
87
88        # AP Trusted ROM
89        wait_for_console_pattern(self, "Booting Trusted Firmware")
90        wait_for_console_pattern(self, "BL1: v2.11.0(release):")
91        wait_for_console_pattern(self, "BL1: Booting BL2")
92
93        # Trusted Boot Firmware
94        wait_for_console_pattern(self, "BL2: v2.11.0(release)")
95        wait_for_console_pattern(self, "Booting BL31")
96
97        # EL3 Runtime Software
98        wait_for_console_pattern(self, "BL31: v2.11.0(release)")
99
100        # Non-trusted Firmware
101        wait_for_console_pattern(self, "UEFI firmware (version 1.0")
102        interrupt_interactive_console_until_pattern(self, "QEMU SBSA-REF Machine")
103
104
105    ASSET_ALPINE_ISO = Asset(
106        ('https://dl-cdn.alpinelinux.org/'
107         'alpine/v3.17/releases/aarch64/alpine-standard-3.17.2-aarch64.iso'),
108        '5a36304ecf039292082d92b48152a9ec21009d3a62f459de623e19c4bd9dc027')
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=None):
114        self.fetch_firmware()
115
116        iso_path = self.ASSET_ALPINE_ISO.fetch()
117
118        self.vm.set_console()
119        self.vm.add_args(
120            "-drive", f"file={iso_path},media=cdrom,format=raw",
121        )
122        if cpu:
123            self.vm.add_args("-cpu", cpu)
124
125        self.vm.launch()
126        wait_for_console_pattern(self, "Welcome to Alpine Linux 3.17")
127
128    def test_sbsaref_alpine_linux_cortex_a57(self):
129        self.boot_alpine_linux("cortex-a57")
130
131    def test_sbsaref_alpine_linux_default_cpu(self):
132        self.boot_alpine_linux()
133
134    def test_sbsaref_alpine_linux_max_pauth_off(self):
135        self.boot_alpine_linux("max,pauth=off")
136
137    def test_sbsaref_alpine_linux_max_pauth_impdef(self):
138        self.boot_alpine_linux("max,pauth-impdef=on")
139
140    @skipUnless(os.getenv('QEMU_TEST_TIMEOUT_EXPECTED'),
141                'Test might timeout due to PAuth emulation')
142    def test_sbsaref_alpine_linux_max(self):
143        self.boot_alpine_linux("max")
144
145
146    ASSET_OPENBSD_ISO = Asset(
147        ('https://cdn.openbsd.org/pub/OpenBSD/7.3/arm64/miniroot73.img'),
148        '7fc2c75401d6f01fbfa25f4953f72ad7d7c18650056d30755c44b9c129b707e5')
149
150    # This tests the whole boot chain from EFI to Userspace
151    # We only boot a whole OS for the current top level CPU and GIC
152    # Other test profiles should use more minimal boots
153    def boot_openbsd73(self, cpu=None):
154        self.fetch_firmware()
155
156        img_path = self.ASSET_OPENBSD_ISO.fetch()
157
158        self.vm.set_console()
159        self.vm.add_args(
160            "-drive", f"file={img_path},format=raw,snapshot=on",
161        )
162        if cpu:
163            self.vm.add_args("-cpu", cpu)
164
165        self.vm.launch()
166        wait_for_console_pattern(self,
167                                 "Welcome to the OpenBSD/arm64"
168                                 " 7.3 installation program.")
169
170    def test_sbsaref_openbsd73_cortex_a57(self):
171        self.boot_openbsd73("cortex-a57")
172
173    def test_sbsaref_openbsd73_default_cpu(self):
174        self.boot_openbsd73()
175
176    def test_sbsaref_openbsd73_max_pauth_off(self):
177        self.boot_openbsd73("max,pauth=off")
178
179    @skipUnless(os.getenv('QEMU_TEST_TIMEOUT_EXPECTED'),
180                'Test might timeout due to PAuth emulation')
181    def test_sbsaref_openbsd73_max_pauth_impdef(self):
182        self.boot_openbsd73("max,pauth-impdef=on")
183
184    @skipUnless(os.getenv('QEMU_TEST_TIMEOUT_EXPECTED'),
185                'Test might timeout due to PAuth emulation')
186    def test_sbsaref_openbsd73_max(self):
187        self.boot_openbsd73("max")
188
189
190    ASSET_FREEBSD_ISO = Asset(
191        ('https://download.freebsd.org/releases/arm64/aarch64/ISO-IMAGES/'
192         '14.1/FreeBSD-14.1-RELEASE-arm64-aarch64-bootonly.iso'),
193        '44cdbae275ef1bb6dab1d5fbb59473d4f741e1c8ea8a80fd9e906b531d6ad461')
194
195    # This tests the whole boot chain from EFI to Userspace
196    # We only boot a whole OS for the current top level CPU and GIC
197    # Other test profiles should use more minimal boots
198    def boot_freebsd14(self, cpu=None):
199        self.fetch_firmware()
200
201        img_path = self.ASSET_FREEBSD_ISO.fetch()
202
203        self.vm.set_console()
204        self.vm.add_args(
205            "-drive", f"file={img_path},format=raw,snapshot=on",
206        )
207        if cpu:
208            self.vm.add_args("-cpu", cpu)
209
210        self.vm.launch()
211        wait_for_console_pattern(self, 'Welcome to FreeBSD!')
212
213    def test_sbsaref_freebsd14_cortex_a57(self):
214        self.boot_freebsd14("cortex-a57")
215
216    def test_sbsaref_freebsd14_default_cpu(self):
217        self.boot_freebsd14()
218
219    def test_sbsaref_freebsd14_max_pauth_off(self):
220        self.boot_freebsd14("max,pauth=off")
221
222    @skipUnless(os.getenv('QEMU_TEST_TIMEOUT_EXPECTED'),
223                'Test might timeout due to PAuth emulation')
224    def test_sbsaref_freebsd14_max_pauth_impdef(self):
225        self.boot_freebsd14("max,pauth-impdef=on")
226
227    @skipUnless(os.getenv('QEMU_TEST_TIMEOUT_EXPECTED'),
228                'Test might timeout due to PAuth emulation')
229    def test_sbsaref_freebsd14_max(self):
230        self.boot_freebsd14("max")
231
232
233if __name__ == '__main__':
234    QemuSystemTest.main()
235