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        """
41        :avocado: tags=arch:arm
42        :avocado: tags=machine:bpim2u
43        :avocado: tags=accel:tcg
44        """
45        self.set_machine('bpim2u')
46        deb_path = self.ASSET_DEB.fetch()
47        kernel_path = self.extract_from_deb(deb_path,
48                                            '/boot/vmlinuz-6.6.16-current-sunxi')
49        dtb_path = ('/usr/lib/linux-image-6.6.16-current-sunxi/'
50                    'sun8i-r40-bananapi-m2-ultra.dtb')
51        dtb_path = self.extract_from_deb(deb_path, dtb_path)
52
53        self.vm.set_console()
54        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
55                               'console=ttyS0,115200n8 '
56                               'earlycon=uart,mmio32,0x1c28000')
57        self.vm.add_args('-kernel', kernel_path,
58                         '-dtb', dtb_path,
59                         '-append', kernel_command_line)
60        self.vm.launch()
61        console_pattern = 'Kernel command line: %s' % kernel_command_line
62        self.wait_for_console_pattern(console_pattern)
63        os.remove(kernel_path)
64        os.remove(dtb_path)
65
66    def test_arm_bpim2u_initrd(self):
67        """
68        :avocado: tags=arch:arm
69        :avocado: tags=accel:tcg
70        :avocado: tags=machine:bpim2u
71        """
72        self.set_machine('bpim2u')
73        deb_path = self.ASSET_DEB.fetch()
74        kernel_path = self.extract_from_deb(deb_path,
75                                            '/boot/vmlinuz-6.6.16-current-sunxi')
76        dtb_path = ('/usr/lib/linux-image-6.6.16-current-sunxi/'
77                    'sun8i-r40-bananapi-m2-ultra.dtb')
78        dtb_path = self.extract_from_deb(deb_path, dtb_path)
79        initrd_path_gz = self.ASSET_INITRD.fetch()
80        initrd_path = os.path.join(self.workdir, 'rootfs.cpio')
81        gzip_uncompress(initrd_path_gz, initrd_path)
82
83        self.vm.set_console()
84        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
85                               'console=ttyS0,115200 '
86                               'panic=-1 noreboot')
87        self.vm.add_args('-kernel', kernel_path,
88                         '-dtb', dtb_path,
89                         '-initrd', initrd_path,
90                         '-append', kernel_command_line,
91                         '-no-reboot')
92        self.vm.launch()
93        self.wait_for_console_pattern('Boot successful.')
94
95        exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo',
96                                                'Allwinner sun8i Family')
97        exec_command_and_wait_for_pattern(self, 'cat /proc/iomem',
98                                                'system-control@1c00000')
99        exec_command_and_wait_for_pattern(self, 'reboot',
100                                                'reboot: Restarting system')
101        # Wait for VM to shut down gracefully
102        self.vm.wait()
103        os.remove(kernel_path)
104        os.remove(dtb_path)
105        os.remove(initrd_path)
106
107    def test_arm_bpim2u_gmac(self):
108        """
109        :avocado: tags=arch:arm
110        :avocado: tags=machine:bpim2u
111        :avocado: tags=device:sd
112        """
113        self.set_machine('bpim2u')
114        self.require_netdev('user')
115
116        deb_path = self.ASSET_DEB.fetch()
117        kernel_path = self.extract_from_deb(deb_path,
118                                            '/boot/vmlinuz-6.6.16-current-sunxi')
119        dtb_path = ('/usr/lib/linux-image-6.6.16-current-sunxi/'
120                    'sun8i-r40-bananapi-m2-ultra.dtb')
121        dtb_path = self.extract_from_deb(deb_path, dtb_path)
122        rootfs_path_xz = self.ASSET_ROOTFS.fetch()
123        rootfs_path = os.path.join(self.workdir, 'rootfs.cpio')
124        lzma_uncompress(rootfs_path_xz, rootfs_path)
125        image_pow2ceil_expand(rootfs_path)
126
127        self.vm.set_console()
128        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
129                               'console=ttyS0,115200 '
130                               'root=b300 rootwait rw '
131                               'panic=-1 noreboot')
132        self.vm.add_args('-kernel', kernel_path,
133                         '-dtb', dtb_path,
134                         '-drive', 'file=' + rootfs_path + ',if=sd,format=raw',
135                         '-net', 'nic,model=gmac,netdev=host_gmac',
136                         '-netdev', 'user,id=host_gmac',
137                         '-append', kernel_command_line,
138                         '-no-reboot')
139        self.vm.launch()
140        shell_ready = "/bin/sh: can't access tty; job control turned off"
141        self.wait_for_console_pattern(shell_ready)
142
143        exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo',
144                                                'Allwinner sun8i Family')
145        exec_command_and_wait_for_pattern(self, 'cat /proc/partitions',
146                                                'mmcblk')
147        exec_command_and_wait_for_pattern(self, 'ifconfig eth0 up',
148                                                 'eth0: Link is Up')
149        exec_command_and_wait_for_pattern(self, 'udhcpc eth0',
150            'udhcpc: lease of 10.0.2.15 obtained')
151        exec_command_and_wait_for_pattern(self, 'ping -c 3 10.0.2.2',
152            '3 packets transmitted, 3 packets received, 0% packet loss')
153        exec_command_and_wait_for_pattern(self, 'reboot',
154                                                'reboot: Restarting system')
155        # Wait for VM to shut down gracefully
156        self.vm.wait()
157        os.remove(kernel_path)
158        os.remove(dtb_path)
159        os.remove(rootfs_path)
160
161    @skipUnless(os.getenv('QEMU_TEST_ALLOW_LARGE_STORAGE'), 'storage limited')
162    def test_arm_bpim2u_openwrt_22_03_3(self):
163        """
164        :avocado: tags=arch:arm
165        :avocado: tags=machine:bpim2u
166        :avocado: tags=device:sd
167        """
168        self.set_machine('bpim2u')
169        # This test download a 8.9 MiB compressed image and expand it
170        # to 127 MiB.
171        image_path_gz = self.ASSET_SD_IMAGE.fetch()
172        image_path = os.path.join(self.workdir, 'sdcard.img')
173        gzip_uncompress(image_path_gz, image_path)
174        image_pow2ceil_expand(image_path)
175
176        self.vm.set_console()
177        self.vm.add_args('-drive', 'file=' + image_path + ',if=sd,format=raw',
178                         '-nic', 'user',
179                         '-no-reboot')
180        self.vm.launch()
181
182        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
183                               'usbcore.nousb '
184                               'noreboot')
185
186        self.wait_for_console_pattern('U-Boot SPL')
187
188        interrupt_interactive_console_until_pattern(
189                self, 'Hit any key to stop autoboot:', '=>')
190        exec_command_and_wait_for_pattern(self, "setenv extraargs '" +
191                                                kernel_command_line + "'", '=>')
192        exec_command_and_wait_for_pattern(self, 'boot', 'Starting kernel ...');
193
194        self.wait_for_console_pattern(
195            'Please press Enter to activate this console.')
196
197        exec_command_and_wait_for_pattern(self, ' ', 'root@')
198
199        exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo',
200                                                'Allwinner sun8i Family')
201        exec_command_and_wait_for_pattern(self, 'cat /proc/iomem',
202                                                'system-control@1c00000')
203        os.remove(image_path)
204
205if __name__ == '__main__':
206    LinuxKernelTest.main()
207