xref: /openbmc/qemu/tests/functional/test_arm_bpim2u.py (revision 5831ed84e7e450a652f215721aba34ed4e1ffb97)
1#!/usr/bin/env python3
2#
3# Functional test that boots a Linux kernel on a Banana 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, exec_command_and_wait_for_pattern
11from qemu_test import Asset, interrupt_interactive_console_until_pattern
12from qemu_test import skipBigDataTest
13from qemu_test.utils import gzip_uncompress, lzma_uncompress
14from qemu_test.utils import image_pow2ceil_expand
15
16
17class BananaPiMachine(LinuxKernelTest):
18
19    ASSET_DEB = Asset(
20        ('https://apt.armbian.com/pool/main/l/linux-6.6.16/'
21         'linux-image-current-sunxi_24.2.1_armhf__6.6.16-Seb3e-D6b4a-P2359-Ce96bHfe66-HK01ba-V014b-B067e-R448a.deb'),
22        '3d968c15b121ede871dce49d13ee7644d6f74b6b121b84c9a40f51b0c80d6d22')
23
24    ASSET_INITRD = Asset(
25        ('https://github.com/groeck/linux-build-test/raw/'
26         '2eb0a73b5d5a28df3170c546ddaaa9757e1e0848/rootfs/'
27         'arm/rootfs-armv7a.cpio.gz'),
28        '2c8dbdb16ea7af2dfbcbea96044dde639fb07d09fd3c4fb31f2027ef71e55ddd')
29
30    ASSET_ROOTFS = Asset(
31        ('http://storage.kernelci.org/images/rootfs/buildroot/'
32         'buildroot-baseline/20230703.0/armel/rootfs.ext2.xz'),
33        '42b44a12965ac0afe9a88378527fb698a7dc76af50495efc2361ee1595b4e5c6')
34
35    ASSET_SD_IMAGE = Asset(
36        ('https://downloads.openwrt.org/releases/22.03.3/targets/sunxi/cortexa7/'
37         'openwrt-22.03.3-sunxi-cortexa7-sinovoip_bananapi-m2-ultra-ext4-sdcard.img.gz'),
38        '5b41b4e11423e562c6011640f9a7cd3bdd0a3d42b83430f7caa70a432e6cd82c')
39
40    def test_arm_bpim2u(self):
41        self.set_machine('bpim2u')
42        kernel_path = self.archive_extract(
43            self.ASSET_DEB, member='boot/vmlinuz-6.6.16-current-sunxi')
44        dtb_path = ('usr/lib/linux-image-6.6.16-current-sunxi/'
45                    'sun8i-r40-bananapi-m2-ultra.dtb')
46        dtb_path = self.archive_extract(self.ASSET_DEB, member=dtb_path)
47
48        self.vm.set_console()
49        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
50                               'console=ttyS0,115200n8 '
51                               'earlycon=uart,mmio32,0x1c28000')
52        self.vm.add_args('-kernel', kernel_path,
53                         '-dtb', dtb_path,
54                         '-append', kernel_command_line)
55        self.vm.launch()
56        console_pattern = 'Kernel command line: %s' % kernel_command_line
57        self.wait_for_console_pattern(console_pattern)
58        os.remove(kernel_path)
59        os.remove(dtb_path)
60
61    def test_arm_bpim2u_initrd(self):
62        self.set_machine('bpim2u')
63        kernel_path = self.archive_extract(
64            self.ASSET_DEB, member='boot/vmlinuz-6.6.16-current-sunxi')
65        dtb_path = ('usr/lib/linux-image-6.6.16-current-sunxi/'
66                    'sun8i-r40-bananapi-m2-ultra.dtb')
67        dtb_path = self.archive_extract(self.ASSET_DEB, member=dtb_path)
68        initrd_path_gz = self.ASSET_INITRD.fetch()
69        initrd_path = self.scratch_file('rootfs.cpio')
70        gzip_uncompress(initrd_path_gz, initrd_path)
71
72        self.vm.set_console()
73        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
74                               'console=ttyS0,115200 '
75                               'panic=-1 noreboot')
76        self.vm.add_args('-kernel', kernel_path,
77                         '-dtb', dtb_path,
78                         '-initrd', initrd_path,
79                         '-append', kernel_command_line,
80                         '-no-reboot')
81        self.vm.launch()
82        self.wait_for_console_pattern('Boot successful.')
83
84        exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo',
85                                                'Allwinner sun8i Family')
86        exec_command_and_wait_for_pattern(self, 'cat /proc/iomem',
87                                                'system-control@1c00000')
88        exec_command_and_wait_for_pattern(self, 'reboot',
89                                                'reboot: Restarting system')
90        # Wait for VM to shut down gracefully
91        self.vm.wait()
92        os.remove(kernel_path)
93        os.remove(dtb_path)
94        os.remove(initrd_path)
95
96    def test_arm_bpim2u_gmac(self):
97        self.set_machine('bpim2u')
98        self.require_netdev('user')
99
100        deb_path = self.ASSET_DEB.fetch()
101        kernel_path = self.archive_extract(
102            self.ASSET_DEB, member='boot/vmlinuz-6.6.16-current-sunxi')
103        dtb_path = ('usr/lib/linux-image-6.6.16-current-sunxi/'
104                    'sun8i-r40-bananapi-m2-ultra.dtb')
105        dtb_path = self.archive_extract(self.ASSET_DEB, member=dtb_path)
106        rootfs_path_xz = self.ASSET_ROOTFS.fetch()
107        rootfs_path = self.scratch_file('rootfs.cpio')
108        lzma_uncompress(rootfs_path_xz, rootfs_path)
109        image_pow2ceil_expand(rootfs_path)
110
111        self.vm.set_console()
112        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
113                               'console=ttyS0,115200 '
114                               'root=b300 rootwait rw '
115                               'panic=-1 noreboot')
116        self.vm.add_args('-kernel', kernel_path,
117                         '-dtb', dtb_path,
118                         '-drive', 'file=' + rootfs_path + ',if=sd,format=raw',
119                         '-net', 'nic,model=gmac,netdev=host_gmac',
120                         '-netdev', 'user,id=host_gmac',
121                         '-append', kernel_command_line,
122                         '-no-reboot')
123        self.vm.launch()
124        shell_ready = "/bin/sh: can't access tty; job control turned off"
125        self.wait_for_console_pattern(shell_ready)
126
127        exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo',
128                                                'Allwinner sun8i Family')
129        exec_command_and_wait_for_pattern(self, 'cat /proc/partitions',
130                                                'mmcblk')
131        exec_command_and_wait_for_pattern(self, 'ifconfig eth0 up',
132                                                 'eth0: Link is Up')
133        exec_command_and_wait_for_pattern(self, 'udhcpc eth0',
134            'udhcpc: lease of 10.0.2.15 obtained')
135        exec_command_and_wait_for_pattern(self, 'ping -c 3 10.0.2.2',
136            '3 packets transmitted, 3 packets received, 0% packet loss')
137        exec_command_and_wait_for_pattern(self, 'reboot',
138                                                'reboot: Restarting system')
139        # Wait for VM to shut down gracefully
140        self.vm.wait()
141        os.remove(kernel_path)
142        os.remove(dtb_path)
143        os.remove(rootfs_path)
144
145    @skipBigDataTest()
146    def test_arm_bpim2u_openwrt_22_03_3(self):
147        self.set_machine('bpim2u')
148        # This test download a 8.9 MiB compressed image and expand it
149        # to 127 MiB.
150        image_path_gz = self.ASSET_SD_IMAGE.fetch()
151        image_path = self.scratch_file('sdcard.img')
152        gzip_uncompress(image_path_gz, image_path)
153        image_pow2ceil_expand(image_path)
154
155        self.vm.set_console()
156        self.vm.add_args('-drive', 'file=' + image_path + ',if=sd,format=raw',
157                         '-nic', 'user',
158                         '-no-reboot')
159        self.vm.launch()
160
161        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
162                               'usbcore.nousb '
163                               'noreboot')
164
165        self.wait_for_console_pattern('U-Boot SPL')
166
167        interrupt_interactive_console_until_pattern(
168                self, 'Hit any key to stop autoboot:', '=>')
169        exec_command_and_wait_for_pattern(self, "setenv extraargs '" +
170                                                kernel_command_line + "'", '=>')
171        exec_command_and_wait_for_pattern(self, 'boot', 'Starting kernel ...');
172
173        self.wait_for_console_pattern(
174            'Please press Enter to activate this console.')
175
176        exec_command_and_wait_for_pattern(self, ' ', 'root@')
177
178        exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo',
179                                                'Allwinner sun8i Family')
180        exec_command_and_wait_for_pattern(self, 'cat /proc/iomem',
181                                                'system-control@1c00000')
182        os.remove(image_path)
183
184if __name__ == '__main__':
185    LinuxKernelTest.main()
186