1#!/usr/bin/env python3
2#
3# Functional test that boots a Linux 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):
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            "-cpu", cpu,
121            "-drive", f"file={iso_path},media=cdrom,format=raw",
122        )
123
124        self.vm.launch()
125        wait_for_console_pattern(self, "Welcome to Alpine Linux 3.17")
126
127    def test_sbsaref_alpine_linux_cortex_a57(self):
128        self.boot_alpine_linux("cortex-a57")
129
130    def test_sbsaref_alpine_linux_neoverse_n1(self):
131        self.boot_alpine_linux("neoverse-n1")
132
133    def test_sbsaref_alpine_linux_max_pauth_off(self):
134        self.boot_alpine_linux("max,pauth=off")
135
136    def test_sbsaref_alpine_linux_max_pauth_impdef(self):
137        self.boot_alpine_linux("max,pauth-impdef=on")
138
139    @skipUnless(os.getenv('QEMU_TEST_TIMEOUT_EXPECTED'), 'Test might timeout')
140    def test_sbsaref_alpine_linux_max(self):
141        self.boot_alpine_linux("max")
142
143
144    ASSET_OPENBSD_ISO = Asset(
145        ('https://cdn.openbsd.org/pub/OpenBSD/7.3/arm64/miniroot73.img'),
146        '7fc2c75401d6f01fbfa25f4953f72ad7d7c18650056d30755c44b9c129b707e5')
147
148    # This tests the whole boot chain from EFI to Userspace
149    # We only boot a whole OS for the current top level CPU and GIC
150    # Other test profiles should use more minimal boots
151    def boot_openbsd73(self, cpu):
152        self.fetch_firmware()
153
154        img_path = self.ASSET_OPENBSD_ISO.fetch()
155
156        self.vm.set_console()
157        self.vm.add_args(
158            "-cpu", cpu,
159            "-drive", f"file={img_path},format=raw,snapshot=on",
160        )
161
162        self.vm.launch()
163        wait_for_console_pattern(self,
164                                 "Welcome to the OpenBSD/arm64"
165                                 " 7.3 installation program.")
166
167    def test_sbsaref_openbsd73_cortex_a57(self):
168        self.boot_openbsd73("cortex-a57")
169
170    def test_sbsaref_openbsd73_neoverse_n1(self):
171        self.boot_openbsd73("neoverse-n1")
172
173    def test_sbsaref_openbsd73_max_pauth_off(self):
174        self.boot_openbsd73("max,pauth=off")
175
176    @skipUnless(os.getenv('QEMU_TEST_TIMEOUT_EXPECTED'), 'Test might timeout')
177    def test_sbsaref_openbsd73_max_pauth_impdef(self):
178        self.boot_openbsd73("max,pauth-impdef=on")
179
180    @skipUnless(os.getenv('QEMU_TEST_TIMEOUT_EXPECTED'), 'Test might timeout')
181    def test_sbsaref_openbsd73_max(self):
182        self.boot_openbsd73("max")
183
184
185if __name__ == '__main__':
186    QemuSystemTest.main()
187