xref: /qemu/tests/functional/test_aarch64_raspi4.py (revision 5831ed84e7e450a652f215721aba34ed4e1ffb97)
1#!/usr/bin/env python3
2#
3# Functional test that boots a Linux kernel on a Raspberry Pi machine
4# and checks the console
5#
6# SPDX-License-Identifier: GPL-2.0-or-later
7
8from qemu_test import LinuxKernelTest, Asset
9from qemu_test import exec_command_and_wait_for_pattern
10from qemu_test.utils import gzip_uncompress
11
12
13class Aarch64Raspi4Machine(LinuxKernelTest):
14
15    """
16    The kernel can be rebuilt using the kernel source referenced
17    and following the instructions on the on:
18    https://www.raspberrypi.org/documentation/linux/kernel/building.md
19    """
20    ASSET_KERNEL_20190215 = Asset(
21        ('http://archive.raspberrypi.org/debian/'
22         'pool/main/r/raspberrypi-firmware/'
23         'raspberrypi-kernel_1.20230106-1_arm64.deb'),
24        '56d5713c8f6eee8a0d3f0e73600ec11391144fef318b08943e9abd94c0a9baf7')
25
26    ASSET_INITRD = Asset(
27        ('https://github.com/groeck/linux-build-test/raw/'
28         '86b2be1384d41c8c388e63078a847f1e1c4cb1de/rootfs/'
29         'arm64/rootfs.cpio.gz'),
30        '7c0b16d1853772f6f4c3ca63e789b3b9ff4936efac9c8a01fb0c98c05c7a7648')
31
32    def test_arm_raspi4(self):
33        kernel_path = self.archive_extract(self.ASSET_KERNEL_20190215,
34                                           member='boot/kernel8.img')
35        dtb_path = self.archive_extract(self.ASSET_KERNEL_20190215,
36                                        member='boot/bcm2711-rpi-4-b.dtb')
37
38        self.set_machine('raspi4b')
39        self.vm.set_console()
40        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
41                               'earlycon=pl011,mmio32,0xfe201000 ' +
42                               'console=ttyAMA0,115200 ' +
43                               'root=/dev/mmcblk1p2 rootwait ' +
44                               'dwc_otg.fiq_fsm_enable=0')
45        self.vm.add_args('-kernel', kernel_path,
46                         '-dtb', dtb_path,
47                         '-append', kernel_command_line)
48        # When PCI is supported we can add a USB controller:
49        #                '-device', 'qemu-xhci,bus=pcie.1,id=xhci',
50        #                '-device', 'usb-kbd,bus=xhci.0',
51        self.vm.launch()
52        console_pattern = 'Kernel command line: %s' % kernel_command_line
53        self.wait_for_console_pattern(console_pattern)
54        # When USB is enabled we can look for this
55        # console_pattern = 'Product: QEMU USB Keyboard'
56        # self.wait_for_console_pattern(console_pattern)
57        console_pattern = 'Waiting for root device'
58        self.wait_for_console_pattern(console_pattern)
59
60
61    def test_arm_raspi4_initrd(self):
62        kernel_path = self.archive_extract(self.ASSET_KERNEL_20190215,
63                                           member='boot/kernel8.img')
64        dtb_path = self.archive_extract(self.ASSET_KERNEL_20190215,
65                                        member='boot/bcm2711-rpi-4-b.dtb')
66        initrd_path_gz = self.ASSET_INITRD.fetch()
67        initrd_path = self.scratch_file('rootfs.cpio')
68        gzip_uncompress(initrd_path_gz, initrd_path)
69
70        self.set_machine('raspi4b')
71        self.vm.set_console()
72        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
73                               'earlycon=pl011,mmio32,0xfe201000 ' +
74                               'console=ttyAMA0,115200 ' +
75                               'panic=-1 noreboot ' +
76                               'dwc_otg.fiq_fsm_enable=0')
77        self.vm.add_args('-kernel', kernel_path,
78                         '-dtb', dtb_path,
79                         '-initrd', initrd_path,
80                         '-append', kernel_command_line,
81                         '-no-reboot')
82        # When PCI is supported we can add a USB controller:
83        #                '-device', 'qemu-xhci,bus=pcie.1,id=xhci',
84        #                '-device', 'usb-kbd,bus=xhci.0',
85        self.vm.launch()
86        self.wait_for_console_pattern('Boot successful.')
87
88        exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo',
89                                                'BCM2835')
90        exec_command_and_wait_for_pattern(self, 'cat /proc/iomem',
91                                                'cprman@7e101000')
92        exec_command_and_wait_for_pattern(self, 'halt', 'reboot: System halted')
93        # TODO: Raspberry Pi4 doesn't shut down properly with recent kernels
94        # Wait for VM to shut down gracefully
95        #self.vm.wait()
96
97
98if __name__ == '__main__':
99    LinuxKernelTest.main()
100