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