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 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        - Fedora GNU Toolchain version 13.2.1 20230728 (Red Hat 13.2.1-1)
36
37        - Trusted Firmware-A
38          https://github.com/ARM-software/arm-trusted-firmware/tree/7c3ff62d
39
40        - Tianocore EDK II
41          https://github.com/tianocore/edk2/tree/0f9283429dd4
42          https://github.com/tianocore/edk2/tree/ad1c0394b177
43          https://github.com/tianocore/edk2-platforms/tree/d03a60523a60
44        """
45
46        # Secure BootRom (TF-A code)
47        fs0_xz_url = (
48            "https://fileserver.linaro.org/s/rE43RJyTfxPtBkc/"
49            "download/SBSA_FLASH0.fd.xz"
50        )
51        fs0_xz_hash = "cdb8e4ffdaaa79292b7b465693f9e5fae6b7062d"
52        tar_xz_path = self.fetch_asset(fs0_xz_url, asset_hash=fs0_xz_hash)
53        archive.extract(tar_xz_path, self.workdir)
54        fs0_path = os.path.join(self.workdir, "SBSA_FLASH0.fd")
55
56        # Non-secure rom (UEFI and EFI variables)
57        fs1_xz_url = (
58            "https://fileserver.linaro.org/s/AGWPDXbcqJTKS4R/"
59            "download/SBSA_FLASH1.fd.xz"
60        )
61        fs1_xz_hash = "411155ae6984334714dff08d5d628178e790c875"
62        tar_xz_path = self.fetch_asset(fs1_xz_url, asset_hash=fs1_xz_hash)
63        archive.extract(tar_xz_path, self.workdir)
64        fs1_path = os.path.join(self.workdir, "SBSA_FLASH1.fd")
65
66        for path in [fs0_path, fs1_path]:
67            with open(path, "ab+") as fd:
68                fd.truncate(256 << 20)  # Expand volumes to 256MiB
69
70        self.vm.set_console()
71        self.vm.add_args(
72            "-drive",
73            f"if=pflash,file={fs0_path},format=raw",
74            "-drive",
75            f"if=pflash,file={fs1_path},format=raw",
76            "-smp",
77            "1",
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.9(release):v2.9")
100        wait_for_console_pattern(self, "BL1: Booting BL2")
101
102        # Trusted Boot Firmware
103        wait_for_console_pattern(self, "BL2: v2.9(release)")
104        wait_for_console_pattern(self, "Booting BL31")
105
106        # EL3 Runtime Software
107        wait_for_console_pattern(self, "BL31: v2.9(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            "-device",
134            "virtio-rng-pci,rng=rng0",
135            "-object",
136            "rng-random,id=rng0,filename=/dev/urandom",
137        )
138
139        self.vm.launch()
140        wait_for_console_pattern(self, "Welcome to Alpine Linux 3.17")
141
142    def test_sbsaref_alpine_linux_cortex_a57(self):
143        """
144        :avocado: tags=cpu:cortex-a57
145        """
146        self.boot_alpine_linux("cortex-a57")
147
148    def test_sbsaref_alpine_linux_neoverse_n1(self):
149        """
150        :avocado: tags=cpu:neoverse-n1
151        """
152        self.boot_alpine_linux("neoverse-n1")
153
154    def test_sbsaref_alpine_linux_max(self):
155        """
156        :avocado: tags=cpu:max
157        """
158        self.boot_alpine_linux("max")
159
160
161    # This tests the whole boot chain from EFI to Userspace
162    # We only boot a whole OS for the current top level CPU and GIC
163    # Other test profiles should use more minimal boots
164    def boot_openbsd73(self, cpu):
165        self.fetch_firmware()
166
167        img_url = (
168            "https://cdn.openbsd.org/pub/OpenBSD/7.3/arm64/miniroot73.img"
169        )
170
171        img_hash = "7fc2c75401d6f01fbfa25f4953f72ad7d7c18650056d30755c44b9c129b707e5"
172        img_path = self.fetch_asset(img_url, algorithm="sha256", asset_hash=img_hash)
173
174        self.vm.set_console()
175        self.vm.add_args(
176            "-cpu",
177            cpu,
178            "-drive",
179            f"file={img_path},format=raw",
180            "-device",
181            "virtio-rng-pci,rng=rng0",
182            "-object",
183            "rng-random,id=rng0,filename=/dev/urandom",
184        )
185
186        self.vm.launch()
187        wait_for_console_pattern(self,
188                                 "Welcome to the OpenBSD/arm64"
189                                 " 7.3 installation program.")
190
191    def test_sbsaref_openbsd73_cortex_a57(self):
192        """
193        :avocado: tags=cpu:cortex-a57
194        """
195        self.boot_openbsd73("cortex-a57")
196
197    def test_sbsaref_openbsd73_neoverse_n1(self):
198        """
199        :avocado: tags=cpu:neoverse-n1
200        """
201        self.boot_openbsd73("neoverse-n1")
202
203    def test_sbsaref_openbsd73_max(self):
204        """
205        :avocado: tags=cpu:max
206        """
207        self.boot_openbsd73("max")
208
209