1#!/usr/bin/env python3
2#
3# Functional test that boots a Linux kernel on an Orange Pi machine
4# and checks the console
5#
6# SPDX-License-Identifier: GPL-2.0-or-later
7
8import os
9import shutil
10
11from qemu_test import LinuxKernelTest, exec_command_and_wait_for_pattern
12from qemu_test import Asset, interrupt_interactive_console_until_pattern
13from qemu_test import wait_for_console_pattern
14from qemu_test.utils import archive_extract, gzip_uncompress, lzma_uncompress
15from qemu_test.utils import image_pow2ceil_expand
16from unittest import skipUnless
17
18class BananaPiMachine(LinuxKernelTest):
19
20    ASSET_DEB = Asset(
21        ('https://apt.armbian.com/pool/main/l/linux-6.6.16/'
22         'linux-image-current-sunxi_24.2.1_armhf__6.6.16-Seb3e-D6b4a-P2359-Ce96bHfe66-HK01ba-V014b-B067e-R448a.deb'),
23        '3d968c15b121ede871dce49d13ee7644d6f74b6b121b84c9a40f51b0c80d6d22')
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    ASSET_ROOTFS = Asset(
32        ('http://storage.kernelci.org/images/rootfs/buildroot/'
33         'buildroot-baseline/20230703.0/armel/rootfs.ext2.xz'),
34        '42b44a12965ac0afe9a88378527fb698a7dc76af50495efc2361ee1595b4e5c6')
35
36    ASSET_ARMBIAN = Asset(
37        ('https://k-space.ee.armbian.com/archive/orangepipc/archive/'
38         'Armbian_23.8.1_Orangepipc_jammy_current_6.1.47.img.xz'),
39        'b386dff6552513b5f164ea00f94814a6b0f1da9fb90b83725e949cf797e11afb')
40
41    ASSET_UBOOT = Asset(
42        ('http://snapshot.debian.org/archive/debian/20200108T145233Z/pool/'
43         'main/u/u-boot/u-boot-sunxi_2020.01%2Bdfsg-1_armhf.deb'),
44        '9223d94dc283ab54df41ce9d6f69025a5b47fece29fb67a714e23aa0cdf3bdfa')
45
46    ASSET_NETBSD = Asset(
47        ('https://archive.netbsd.org/pub/NetBSD-archive/NetBSD-9.0/'
48         'evbarm-earmv7hf/binary/gzimg/armv7.img.gz'),
49        '20d3e07dc057e15c12452620e90ecab2047f0f7940d9cba8182ebc795927177f')
50
51    def test_arm_orangepi(self):
52        self.set_machine('orangepi-pc')
53        deb_path = self.ASSET_DEB.fetch()
54        kernel_path = self.extract_from_deb(deb_path,
55                                            '/boot/vmlinuz-6.6.16-current-sunxi')
56        dtb_path = '/usr/lib/linux-image-6.6.16-current-sunxi/sun8i-h3-orangepi-pc.dtb'
57        dtb_path = self.extract_from_deb(deb_path, dtb_path)
58
59        self.vm.set_console()
60        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
61                               'console=ttyS0,115200n8 '
62                               'earlycon=uart,mmio32,0x1c28000')
63        self.vm.add_args('-kernel', kernel_path,
64                         '-dtb', dtb_path,
65                         '-append', kernel_command_line)
66        self.vm.launch()
67        console_pattern = 'Kernel command line: %s' % kernel_command_line
68        self.wait_for_console_pattern(console_pattern)
69        os.remove(kernel_path)
70        os.remove(dtb_path)
71
72    def test_arm_orangepi_initrd(self):
73        self.set_machine('orangepi-pc')
74        deb_path = self.ASSET_DEB.fetch()
75        kernel_path = self.extract_from_deb(deb_path,
76                                            '/boot/vmlinuz-6.6.16-current-sunxi')
77        dtb_path = '/usr/lib/linux-image-6.6.16-current-sunxi/sun8i-h3-orangepi-pc.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_orangepi_sd(self):
108        self.set_machine('orangepi-pc')
109        self.require_netdev('user')
110        deb_path = self.ASSET_DEB.fetch()
111        kernel_path = self.extract_from_deb(deb_path,
112                                            '/boot/vmlinuz-6.6.16-current-sunxi')
113        dtb_path = '/usr/lib/linux-image-6.6.16-current-sunxi/sun8i-h3-orangepi-pc.dtb'
114        dtb_path = self.extract_from_deb(deb_path, dtb_path)
115        rootfs_path_xz = self.ASSET_ROOTFS.fetch()
116        rootfs_path = os.path.join(self.workdir, 'rootfs.cpio')
117        lzma_uncompress(rootfs_path_xz, rootfs_path)
118        image_pow2ceil_expand(rootfs_path)
119
120        self.vm.set_console()
121        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
122                               'console=ttyS0,115200 '
123                               'root=/dev/mmcblk0 rootwait rw '
124                               'panic=-1 noreboot')
125        self.vm.add_args('-kernel', kernel_path,
126                         '-dtb', dtb_path,
127                         '-drive', 'file=' + rootfs_path + ',if=sd,format=raw',
128                         '-append', kernel_command_line,
129                         '-no-reboot')
130        self.vm.launch()
131        shell_ready = "/bin/sh: can't access tty; job control turned off"
132        self.wait_for_console_pattern(shell_ready)
133
134        exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo',
135                                                'Allwinner sun8i Family')
136        exec_command_and_wait_for_pattern(self, 'cat /proc/partitions',
137                                                'mmcblk0')
138        exec_command_and_wait_for_pattern(self, 'ifconfig eth0 up',
139                                                 'eth0: Link is Up')
140        exec_command_and_wait_for_pattern(self, 'udhcpc eth0',
141            'udhcpc: lease of 10.0.2.15 obtained')
142        exec_command_and_wait_for_pattern(self, 'ping -c 3 10.0.2.2',
143            '3 packets transmitted, 3 packets received, 0% packet loss')
144        exec_command_and_wait_for_pattern(self, 'reboot',
145                                                'reboot: Restarting system')
146        # Wait for VM to shut down gracefully
147        self.vm.wait()
148        os.remove(kernel_path)
149        os.remove(dtb_path)
150        os.remove(rootfs_path)
151
152    @skipUnless(os.getenv('QEMU_TEST_ALLOW_LARGE_STORAGE'), 'storage limited')
153    def test_arm_orangepi_armbian(self):
154        self.set_machine('orangepi-pc')
155        # This test download a 275 MiB compressed image and expand it
156        # to 1036 MiB, but the underlying filesystem is 1552 MiB...
157        # As we expand it to 2 GiB we are safe.
158        image_path_xz = self.ASSET_ARMBIAN.fetch()
159        image_path = os.path.join(self.workdir, 'armbian.img')
160        lzma_uncompress(image_path_xz, image_path)
161        image_pow2ceil_expand(image_path)
162
163        self.vm.set_console()
164        self.vm.add_args('-drive', 'file=' + image_path + ',if=sd,format=raw',
165                         '-nic', 'user',
166                         '-no-reboot')
167        self.vm.launch()
168
169        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
170                               'console=ttyS0,115200 '
171                               'loglevel=7 '
172                               'nosmp '
173                               'systemd.default_timeout_start_sec=9000 '
174                               'systemd.mask=armbian-zram-config.service '
175                               'systemd.mask=armbian-ramlog.service')
176
177        self.wait_for_console_pattern('U-Boot SPL')
178        self.wait_for_console_pattern('Autoboot in ')
179        exec_command_and_wait_for_pattern(self, ' ', '=>')
180        exec_command_and_wait_for_pattern(self, "setenv extraargs '" +
181                                                kernel_command_line + "'", '=>')
182        exec_command_and_wait_for_pattern(self, 'boot', 'Starting kernel ...');
183
184        self.wait_for_console_pattern('systemd[1]: Hostname set ' +
185                                      'to <orangepipc>')
186        self.wait_for_console_pattern('Starting Load Kernel Modules...')
187
188    @skipUnless(os.getenv('QEMU_TEST_ALLOW_LARGE_STORAGE'), 'storage limited')
189    def test_arm_orangepi_uboot_netbsd9(self):
190        self.set_machine('orangepi-pc')
191        # This test download a 304MB compressed image and expand it to 2GB
192        deb_path = self.ASSET_UBOOT.fetch()
193        # We use the common OrangePi PC 'plus' build of U-Boot for our secondary
194        # program loader (SPL). We will then set the path to the more specific
195        # OrangePi "PC" device tree blob with 'setenv fdtfile' in U-Boot prompt,
196        # before to boot NetBSD.
197        uboot_path = '/usr/lib/u-boot/orangepi_plus/u-boot-sunxi-with-spl.bin'
198        uboot_path = self.extract_from_deb(deb_path, uboot_path)
199        image_path_gz = self.ASSET_NETBSD.fetch()
200        image_path = os.path.join(self.workdir, 'armv7.img')
201        gzip_uncompress(image_path_gz, image_path)
202        image_pow2ceil_expand(image_path)
203        image_drive_args = 'if=sd,format=raw,snapshot=on,file=' + image_path
204
205        # dd if=u-boot-sunxi-with-spl.bin of=armv7.img bs=1K seek=8 conv=notrunc
206        with open(uboot_path, 'rb') as f_in:
207            with open(image_path, 'r+b') as f_out:
208                f_out.seek(8 * 1024)
209                shutil.copyfileobj(f_in, f_out)
210
211        self.vm.set_console()
212        self.vm.add_args('-nic', 'user',
213                         '-drive', image_drive_args,
214                         '-global', 'allwinner-rtc.base-year=2000',
215                         '-no-reboot')
216        self.vm.launch()
217        wait_for_console_pattern(self, 'U-Boot 2020.01+dfsg-1')
218        interrupt_interactive_console_until_pattern(self,
219                                       'Hit any key to stop autoboot:',
220                                       'switch to partitions #0, OK')
221
222        exec_command_and_wait_for_pattern(self, '', '=>')
223        cmd = 'setenv bootargs root=ld0a'
224        exec_command_and_wait_for_pattern(self, cmd, '=>')
225        cmd = 'setenv kernel netbsd-GENERIC.ub'
226        exec_command_and_wait_for_pattern(self, cmd, '=>')
227        cmd = 'setenv fdtfile dtb/sun8i-h3-orangepi-pc.dtb'
228        exec_command_and_wait_for_pattern(self, cmd, '=>')
229        cmd = ("setenv bootcmd 'fatload mmc 0:1 ${kernel_addr_r} ${kernel}; "
230               "fatload mmc 0:1 ${fdt_addr_r} ${fdtfile}; "
231               "fdt addr ${fdt_addr_r}; "
232               "bootm ${kernel_addr_r} - ${fdt_addr_r}'")
233        exec_command_and_wait_for_pattern(self, cmd, '=>')
234
235        exec_command_and_wait_for_pattern(self, 'boot',
236                                          'Booting kernel from Legacy Image')
237        wait_for_console_pattern(self, 'Starting kernel ...')
238        wait_for_console_pattern(self, 'NetBSD 9.0 (GENERIC)')
239        # Wait for user-space
240        wait_for_console_pattern(self, 'Starting root file system check')
241
242if __name__ == '__main__':
243    LinuxKernelTest.main()
244