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
19def fetch_firmware(test):
20    """
21    Flash volumes generated using:
22
23    Toolchain from Debian:
24    aarch64-linux-gnu-gcc (Debian 12.2.0-14) 12.2.0
25
26    Used components:
27
28    - Trusted Firmware         v2.11.0
29    - Tianocore EDK2           4d4f569924
30    - Tianocore EDK2-platforms 3f08401
31
32    """
33
34    # Secure BootRom (TF-A code)
35    fs0_xz_path = Aarch64SbsarefMachine.ASSET_FLASH0.fetch()
36    fs0_path = os.path.join(test.workdir, "SBSA_FLASH0.fd")
37    lzma_uncompress(fs0_xz_path, fs0_path)
38
39    # Non-secure rom (UEFI and EFI variables)
40    fs1_xz_path = Aarch64SbsarefMachine.ASSET_FLASH1.fetch()
41    fs1_path = os.path.join(test.workdir, "SBSA_FLASH1.fd")
42    lzma_uncompress(fs1_xz_path, fs1_path)
43
44    for path in [fs0_path, fs1_path]:
45        with open(path, "ab+") as fd:
46            fd.truncate(256 << 20)  # Expand volumes to 256MiB
47
48    test.set_machine('sbsa-ref')
49    test.vm.set_console()
50    test.vm.add_args(
51        "-drive", f"if=pflash,file={fs0_path},format=raw",
52        "-drive", f"if=pflash,file={fs1_path},format=raw",
53    )
54
55
56class Aarch64SbsarefMachine(QemuSystemTest):
57    """
58    As firmware runs at a higher privilege level than the hypervisor we
59    can only run these tests under TCG emulation.
60    """
61
62    timeout = 180
63
64    ASSET_FLASH0 = Asset(
65        ('https://artifacts.codelinaro.org/artifactory/linaro-419-sbsa-ref/'
66         '20240619-148232/edk2/SBSA_FLASH0.fd.xz'),
67        '0c954842a590988f526984de22e21ae0ab9cb351a0c99a8a58e928f0c7359cf7')
68
69    ASSET_FLASH1 = Asset(
70        ('https://artifacts.codelinaro.org/artifactory/linaro-419-sbsa-ref/'
71         '20240619-148232/edk2/SBSA_FLASH1.fd.xz'),
72        'c6ec39374c4d79bb9e9cdeeb6db44732d90bb4a334cec92002b3f4b9cac4b5ee')
73
74    def test_sbsaref_edk2_firmware(self):
75
76        fetch_firmware(self)
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
104if __name__ == '__main__':
105    QemuSystemTest.main()
106