xref: /qemu/tests/functional/test_arm_raspi2.py (revision 70ce076fa6dff60585c229a4b641b13e64bf03cf)
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# Copyright (c) 2019 Philippe Mathieu-Daudé <f4bug@amsat.org>
7#
8# SPDX-License-Identifier: GPL-2.0-or-later
9
10from qemu_test import LinuxKernelTest, Asset
11from qemu_test import exec_command_and_wait_for_pattern
12
13
14class ArmRaspi2Machine(LinuxKernelTest):
15
16    ASSET_KERNEL_20190215 = Asset(
17        ('http://archive.raspberrypi.org/debian/'
18         'pool/main/r/raspberrypi-firmware/'
19         'raspberrypi-kernel_1.20190215-1_armhf.deb'),
20        '9f1759f7228113da24f5ee2aa6312946ec09a83e076aba9406c46ff776dfb291')
21
22    ASSET_INITRD = Asset(
23        ('https://github.com/groeck/linux-build-test/raw/'
24         '2eb0a73b5d5a28df3170c546ddaaa9757e1e0848/rootfs/'
25         'arm/rootfs-armv7a.cpio.gz'),
26        '2c8dbdb16ea7af2dfbcbea96044dde639fb07d09fd3c4fb31f2027ef71e55ddd')
27
28    def do_test_arm_raspi2(self, uart_id):
29        """
30        The kernel can be rebuilt using the kernel source referenced
31        and following the instructions on the on:
32        https://www.raspberrypi.org/documentation/linux/kernel/building.md
33        """
34        serial_kernel_cmdline = {
35            0: 'earlycon=pl011,0x3f201000 console=ttyAMA0',
36        }
37        kernel_path = self.archive_extract(self.ASSET_KERNEL_20190215,
38                                           member='boot/kernel7.img')
39        dtb_path = self.archive_extract(self.ASSET_KERNEL_20190215,
40                                        member='boot/bcm2709-rpi-2-b.dtb')
41
42        self.set_machine('raspi2b')
43        self.vm.set_console()
44        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
45                               serial_kernel_cmdline[uart_id] +
46                               ' root=/dev/mmcblk0p2 rootwait ' +
47                               'dwc_otg.fiq_fsm_enable=0')
48        self.vm.add_args('-kernel', kernel_path,
49                         '-dtb', dtb_path,
50                         '-append', kernel_command_line,
51                         '-device', 'usb-kbd')
52        self.vm.launch()
53
54        console_pattern = 'Kernel command line: %s' % kernel_command_line
55        self.wait_for_console_pattern(console_pattern)
56        self.wait_for_console_pattern('Product: QEMU USB Keyboard')
57
58    def test_arm_raspi2_uart0(self):
59        self.do_test_arm_raspi2(0)
60
61    def test_arm_raspi2_initrd(self):
62        kernel_path = self.archive_extract(self.ASSET_KERNEL_20190215,
63                                           member='boot/kernel7.img')
64        dtb_path = self.archive_extract(self.ASSET_KERNEL_20190215,
65                                        member='boot/bcm2709-rpi-2-b.dtb')
66        initrd_path = self.uncompress(self.ASSET_INITRD)
67
68        self.set_machine('raspi2b')
69        self.vm.set_console()
70        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
71                               'earlycon=pl011,0x3f201000 console=ttyAMA0 '
72                               'panic=-1 noreboot ' +
73                               'dwc_otg.fiq_fsm_enable=0')
74        self.vm.add_args('-kernel', kernel_path,
75                         '-dtb', dtb_path,
76                         '-initrd', initrd_path,
77                         '-append', kernel_command_line,
78                         '-no-reboot')
79        self.vm.launch()
80        self.wait_for_console_pattern('Boot successful.')
81
82        exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo',
83                                                'BCM2835')
84        exec_command_and_wait_for_pattern(self, 'cat /proc/iomem',
85                                                '/soc/cprman@7e101000')
86        exec_command_and_wait_for_pattern(self, 'halt', 'reboot: System halted')
87        # Wait for VM to shut down gracefully
88        self.vm.wait()
89
90
91if __name__ == '__main__':
92    LinuxKernelTest.main()
93