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
8import os
9
10from qemu_test import LinuxKernelTest, Asset
11from qemu_test import exec_command_and_wait_for_pattern
12from qemu_test.utils import gzip_uncompress
13
14
15class Aarch64Raspi4Machine(LinuxKernelTest):
16
17    """
18    The kernel can be rebuilt using the kernel source referenced
19    and following the instructions on the on:
20    https://www.raspberrypi.org/documentation/linux/kernel/building.md
21    """
22    ASSET_KERNEL_20190215 = Asset(
23        ('http://archive.raspberrypi.org/debian/'
24         'pool/main/r/raspberrypi-firmware/'
25         'raspberrypi-kernel_1.20230106-1_arm64.deb'),
26        '56d5713c8f6eee8a0d3f0e73600ec11391144fef318b08943e9abd94c0a9baf7')
27
28    ASSET_INITRD = Asset(
29        ('https://github.com/groeck/linux-build-test/raw/'
30         '86b2be1384d41c8c388e63078a847f1e1c4cb1de/rootfs/'
31         'arm64/rootfs.cpio.gz'),
32        '7c0b16d1853772f6f4c3ca63e789b3b9ff4936efac9c8a01fb0c98c05c7a7648')
33
34    def test_arm_raspi4(self):
35        deb_path = self.ASSET_KERNEL_20190215.fetch()
36        kernel_path = self.extract_from_deb(deb_path, '/boot/kernel8.img')
37        dtb_path = self.extract_from_deb(deb_path, '/boot/bcm2711-rpi-4-b.dtb')
38
39        self.set_machine('raspi4b')
40        self.vm.set_console()
41        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
42                               'earlycon=pl011,mmio32,0xfe201000 ' +
43                               'console=ttyAMA0,115200 ' +
44                               'root=/dev/mmcblk1p2 rootwait ' +
45                               'dwc_otg.fiq_fsm_enable=0')
46        self.vm.add_args('-kernel', kernel_path,
47                         '-dtb', dtb_path,
48                         '-append', kernel_command_line)
49        # When PCI is supported we can add a USB controller:
50        #                '-device', 'qemu-xhci,bus=pcie.1,id=xhci',
51        #                '-device', 'usb-kbd,bus=xhci.0',
52        self.vm.launch()
53        console_pattern = 'Kernel command line: %s' % kernel_command_line
54        self.wait_for_console_pattern(console_pattern)
55        # When USB is enabled we can look for this
56        # console_pattern = 'Product: QEMU USB Keyboard'
57        # self.wait_for_console_pattern(console_pattern)
58        console_pattern = 'Waiting for root device'
59        self.wait_for_console_pattern(console_pattern)
60
61
62    def test_arm_raspi4_initrd(self):
63        deb_path = self.ASSET_KERNEL_20190215.fetch()
64        kernel_path = self.extract_from_deb(deb_path, '/boot/kernel8.img')
65        dtb_path = self.extract_from_deb(deb_path, '/boot/bcm2711-rpi-4-b.dtb')
66        initrd_path_gz = self.ASSET_INITRD.fetch()
67        initrd_path = os.path.join(self.workdir, '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