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