xref: /qemu/tests/functional/test_arm_raspi2.py (revision beaf88c895a5eda649777757c80ab4171de777ff) !
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
12from qemu_test.utils import gzip_uncompress
13
14
15class ArmRaspi2Machine(LinuxKernelTest):
16
17    ASSET_KERNEL_20190215 = Asset(
18        ('http://archive.raspberrypi.org/debian/'
19         'pool/main/r/raspberrypi-firmware/'
20         'raspberrypi-kernel_1.20190215-1_armhf.deb'),
21        '9f1759f7228113da24f5ee2aa6312946ec09a83e076aba9406c46ff776dfb291')
22
23    ASSET_INITRD = Asset(
24        ('https://github.com/groeck/linux-build-test/raw/'
25         '2eb0a73b5d5a28df3170c546ddaaa9757e1e0848/rootfs/'
26         'arm/rootfs-armv7a.cpio.gz'),
27        '2c8dbdb16ea7af2dfbcbea96044dde639fb07d09fd3c4fb31f2027ef71e55ddd')
28
29    def do_test_arm_raspi2(self, uart_id):
30        """
31        The kernel can be rebuilt using the kernel source referenced
32        and following the instructions on the on:
33        https://www.raspberrypi.org/documentation/linux/kernel/building.md
34        """
35        serial_kernel_cmdline = {
36            0: 'earlycon=pl011,0x3f201000 console=ttyAMA0',
37        }
38        deb_path = self.ASSET_KERNEL_20190215.fetch()
39        kernel_path = self.extract_from_deb(deb_path, '/boot/kernel7.img')
40        dtb_path = self.extract_from_deb(deb_path, '/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        deb_path = self.ASSET_KERNEL_20190215.fetch()
63        kernel_path = self.extract_from_deb(deb_path, '/boot/kernel7.img')
64        dtb_path = self.extract_from_deb(deb_path, '/boot/bcm2709-rpi-2-b.dtb')
65        initrd_path_gz = self.ASSET_INITRD.fetch()
66        initrd_path = self.scratch_file('rootfs.cpio')
67        gzip_uncompress(initrd_path_gz, initrd_path)
68
69        self.set_machine('raspi2b')
70        self.vm.set_console()
71        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
72                               'earlycon=pl011,0x3f201000 console=ttyAMA0 '
73                               'panic=-1 noreboot ' +
74                               'dwc_otg.fiq_fsm_enable=0')
75        self.vm.add_args('-kernel', kernel_path,
76                         '-dtb', dtb_path,
77                         '-initrd', initrd_path,
78                         '-append', kernel_command_line,
79                         '-no-reboot')
80        self.vm.launch()
81        self.wait_for_console_pattern('Boot successful.')
82
83        exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo',
84                                                'BCM2835')
85        exec_command_and_wait_for_pattern(self, 'cat /proc/iomem',
86                                                '/soc/cprman@7e101000')
87        exec_command_and_wait_for_pattern(self, 'halt', 'reboot: System halted')
88        # Wait for VM to shut down gracefully
89        self.vm.wait()
90
91
92if __name__ == '__main__':
93    LinuxKernelTest.main()
94