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    """
24
25    timeout = 180
26
27    def fetch_firmware(self):
28        """
29        Flash volumes generated using:
30
31        - Fedora GNU Toolchain version 13.2.1 20230728 (Red Hat 13.2.1-1)
32
33        - Trusted Firmware-A
34          https://github.com/ARM-software/arm-trusted-firmware/tree/7c3ff62d
35
36        - Tianocore EDK II
37          https://github.com/tianocore/edk2/tree/0f9283429dd4
38          https://github.com/tianocore/edk2/tree/ad1c0394b177
39          https://github.com/tianocore/edk2-platforms/tree/d03a60523a60
40        """
41
42        # Secure BootRom (TF-A code)
43        fs0_xz_url = (
44            "https://fileserver.linaro.org/s/rE43RJyTfxPtBkc/"
45            "download/SBSA_FLASH0.fd.xz"
46        )
47        fs0_xz_hash = "cdb8e4ffdaaa79292b7b465693f9e5fae6b7062d"
48        tar_xz_path = self.fetch_asset(fs0_xz_url, asset_hash=fs0_xz_hash)
49        archive.extract(tar_xz_path, self.workdir)
50        fs0_path = os.path.join(self.workdir, "SBSA_FLASH0.fd")
51
52        # Non-secure rom (UEFI and EFI variables)
53        fs1_xz_url = (
54            "https://fileserver.linaro.org/s/AGWPDXbcqJTKS4R/"
55            "download/SBSA_FLASH1.fd.xz"
56        )
57        fs1_xz_hash = "411155ae6984334714dff08d5d628178e790c875"
58        tar_xz_path = self.fetch_asset(fs1_xz_url, asset_hash=fs1_xz_hash)
59        archive.extract(tar_xz_path, self.workdir)
60        fs1_path = os.path.join(self.workdir, "SBSA_FLASH1.fd")
61
62        for path in [fs0_path, fs1_path]:
63            with open(path, "ab+") as fd:
64                fd.truncate(256 << 20)  # Expand volumes to 256MiB
65
66        self.vm.set_console()
67        self.vm.add_args(
68            "-drive",
69            f"if=pflash,file={fs0_path},format=raw",
70            "-drive",
71            f"if=pflash,file={fs1_path},format=raw",
72            "-smp",
73            "1",
74            "-machine",
75            "sbsa-ref",
76        )
77
78    def test_sbsaref_edk2_firmware(self):
79        """
80        :avocado: tags=cpu:cortex-a57
81        """
82
83        self.fetch_firmware()
84        self.vm.launch()
85
86        # TF-A boot sequence:
87        #
88        # https://github.com/ARM-software/arm-trusted-firmware/blob/v2.8.0/\
89        #     docs/design/trusted-board-boot.rst#trusted-board-boot-sequence
90        # https://trustedfirmware-a.readthedocs.io/en/v2.8/\
91        #     design/firmware-design.html#cold-boot
92
93        # AP Trusted ROM
94        wait_for_console_pattern(self, "Booting Trusted Firmware")
95        wait_for_console_pattern(self, "BL1: v2.9(release):v2.9")
96        wait_for_console_pattern(self, "BL1: Booting BL2")
97
98        # Trusted Boot Firmware
99        wait_for_console_pattern(self, "BL2: v2.9(release)")
100        wait_for_console_pattern(self, "Booting BL31")
101
102        # EL3 Runtime Software
103        wait_for_console_pattern(self, "BL31: v2.9(release)")
104
105        # Non-trusted Firmware
106        wait_for_console_pattern(self, "UEFI firmware (version 1.0")
107        interrupt_interactive_console_until_pattern(self, "QEMU SBSA-REF Machine")
108
109    # This tests the whole boot chain from EFI to Userspace
110    # We only boot a whole OS for the current top level CPU and GIC
111    # Other test profiles should use more minimal boots
112    def boot_alpine_linux(self, cpu):
113        self.fetch_firmware()
114
115        iso_url = (
116            "https://dl-cdn.alpinelinux.org/"
117            "alpine/v3.17/releases/aarch64/alpine-standard-3.17.2-aarch64.iso"
118        )
119
120        iso_hash = "5a36304ecf039292082d92b48152a9ec21009d3a62f459de623e19c4bd9dc027"
121        iso_path = self.fetch_asset(iso_url, algorithm="sha256", asset_hash=iso_hash)
122
123        self.vm.set_console()
124        self.vm.add_args(
125            "-cpu",
126            cpu,
127            "-drive",
128            f"file={iso_path},format=raw",
129            "-device",
130            "virtio-rng-pci,rng=rng0",
131            "-object",
132            "rng-random,id=rng0,filename=/dev/urandom",
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        """
142        self.boot_alpine_linux("cortex-a57")
143
144    def test_sbsaref_alpine_linux_neoverse_n1(self):
145        """
146        :avocado: tags=cpu:neoverse-n1
147        """
148        self.boot_alpine_linux("neoverse-n1")
149
150    def test_sbsaref_alpine_linux_max(self):
151        """
152        :avocado: tags=cpu:max
153        """
154        self.boot_alpine_linux("max")
155
156
157    # This tests the whole boot chain from EFI to Userspace
158    # We only boot a whole OS for the current top level CPU and GIC
159    # Other test profiles should use more minimal boots
160    def boot_openbsd73(self, cpu):
161        self.fetch_firmware()
162
163        img_url = (
164            "https://cdn.openbsd.org/pub/OpenBSD/7.3/arm64/miniroot73.img"
165        )
166
167        img_hash = "7fc2c75401d6f01fbfa25f4953f72ad7d7c18650056d30755c44b9c129b707e5"
168        img_path = self.fetch_asset(img_url, algorithm="sha256", asset_hash=img_hash)
169
170        self.vm.set_console()
171        self.vm.add_args(
172            "-cpu",
173            cpu,
174            "-drive",
175            f"file={img_path},format=raw",
176            "-device",
177            "virtio-rng-pci,rng=rng0",
178            "-object",
179            "rng-random,id=rng0,filename=/dev/urandom",
180        )
181
182        self.vm.launch()
183        wait_for_console_pattern(self,
184                                 "Welcome to the OpenBSD/arm64"
185                                 " 7.3 installation program.")
186
187    def test_sbsaref_openbsd73_cortex_a57(self):
188        """
189        :avocado: tags=cpu:cortex-a57
190        """
191        self.boot_openbsd73("cortex-a57")
192
193    def test_sbsaref_openbsd73_neoverse_n1(self):
194        """
195        :avocado: tags=cpu:neoverse-n1
196        """
197        self.boot_openbsd73("neoverse-n1")
198
199    def test_sbsaref_openbsd73_max(self):
200        """
201        :avocado: tags=cpu:max
202        """
203        self.boot_openbsd73("max")
204
205