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        """
53        :avocado: tags=arch:arm
54        :avocado: tags=machine:orangepi-pc
55        :avocado: tags=accel:tcg
56        """
57        self.set_machine('orangepi-pc')
58        deb_path = self.ASSET_DEB.fetch()
59        kernel_path = self.extract_from_deb(deb_path,
60                                            '/boot/vmlinuz-6.6.16-current-sunxi')
61        dtb_path = '/usr/lib/linux-image-6.6.16-current-sunxi/sun8i-h3-orangepi-pc.dtb'
62        dtb_path = self.extract_from_deb(deb_path, dtb_path)
63
64        self.vm.set_console()
65        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
66                               'console=ttyS0,115200n8 '
67                               'earlycon=uart,mmio32,0x1c28000')
68        self.vm.add_args('-kernel', kernel_path,
69                         '-dtb', dtb_path,
70                         '-append', kernel_command_line)
71        self.vm.launch()
72        console_pattern = 'Kernel command line: %s' % kernel_command_line
73        self.wait_for_console_pattern(console_pattern)
74        os.remove(kernel_path)
75        os.remove(dtb_path)
76
77    def test_arm_orangepi_initrd(self):
78        """
79        :avocado: tags=arch:arm
80        :avocado: tags=accel:tcg
81        :avocado: tags=machine:orangepi-pc
82        """
83        self.set_machine('orangepi-pc')
84        deb_path = self.ASSET_DEB.fetch()
85        kernel_path = self.extract_from_deb(deb_path,
86                                            '/boot/vmlinuz-6.6.16-current-sunxi')
87        dtb_path = '/usr/lib/linux-image-6.6.16-current-sunxi/sun8i-h3-orangepi-pc.dtb'
88        dtb_path = self.extract_from_deb(deb_path, dtb_path)
89        initrd_path_gz = self.ASSET_INITRD.fetch()
90        initrd_path = os.path.join(self.workdir, 'rootfs.cpio')
91        gzip_uncompress(initrd_path_gz, initrd_path)
92
93        self.vm.set_console()
94        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
95                               'console=ttyS0,115200 '
96                               'panic=-1 noreboot')
97        self.vm.add_args('-kernel', kernel_path,
98                         '-dtb', dtb_path,
99                         '-initrd', initrd_path,
100                         '-append', kernel_command_line,
101                         '-no-reboot')
102        self.vm.launch()
103        self.wait_for_console_pattern('Boot successful.')
104
105        exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo',
106                                                'Allwinner sun8i Family')
107        exec_command_and_wait_for_pattern(self, 'cat /proc/iomem',
108                                                'system-control@1c00000')
109        exec_command_and_wait_for_pattern(self, 'reboot',
110                                                'reboot: Restarting system')
111        # Wait for VM to shut down gracefully
112        self.vm.wait()
113        os.remove(kernel_path)
114        os.remove(dtb_path)
115        os.remove(initrd_path)
116
117    def test_arm_orangepi_sd(self):
118        """
119        :avocado: tags=arch:arm
120        :avocado: tags=accel:tcg
121        :avocado: tags=machine:orangepi-pc
122        :avocado: tags=device:sd
123        """
124        self.set_machine('orangepi-pc')
125        self.require_netdev('user')
126        deb_path = self.ASSET_DEB.fetch()
127        kernel_path = self.extract_from_deb(deb_path,
128                                            '/boot/vmlinuz-6.6.16-current-sunxi')
129        dtb_path = '/usr/lib/linux-image-6.6.16-current-sunxi/sun8i-h3-orangepi-pc.dtb'
130        dtb_path = self.extract_from_deb(deb_path, dtb_path)
131        rootfs_path_xz = self.ASSET_ROOTFS.fetch()
132        rootfs_path = os.path.join(self.workdir, 'rootfs.cpio')
133        lzma_uncompress(rootfs_path_xz, rootfs_path)
134        image_pow2ceil_expand(rootfs_path)
135
136        self.vm.set_console()
137        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
138                               'console=ttyS0,115200 '
139                               'root=/dev/mmcblk0 rootwait rw '
140                               'panic=-1 noreboot')
141        self.vm.add_args('-kernel', kernel_path,
142                         '-dtb', dtb_path,
143                         '-drive', 'file=' + rootfs_path + ',if=sd,format=raw',
144                         '-append', kernel_command_line,
145                         '-no-reboot')
146        self.vm.launch()
147        shell_ready = "/bin/sh: can't access tty; job control turned off"
148        self.wait_for_console_pattern(shell_ready)
149
150        exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo',
151                                                'Allwinner sun8i Family')
152        exec_command_and_wait_for_pattern(self, 'cat /proc/partitions',
153                                                'mmcblk0')
154        exec_command_and_wait_for_pattern(self, 'ifconfig eth0 up',
155                                                 'eth0: Link is Up')
156        exec_command_and_wait_for_pattern(self, 'udhcpc eth0',
157            'udhcpc: lease of 10.0.2.15 obtained')
158        exec_command_and_wait_for_pattern(self, 'ping -c 3 10.0.2.2',
159            '3 packets transmitted, 3 packets received, 0% packet loss')
160        exec_command_and_wait_for_pattern(self, 'reboot',
161                                                'reboot: Restarting system')
162        # Wait for VM to shut down gracefully
163        self.vm.wait()
164        os.remove(kernel_path)
165        os.remove(dtb_path)
166        os.remove(rootfs_path)
167
168    @skipUnless(os.getenv('QEMU_TEST_ALLOW_LARGE_STORAGE'), 'storage limited')
169    def test_arm_orangepi_armbian(self):
170        """
171        :avocado: tags=arch:arm
172        :avocado: tags=machine:orangepi-pc
173        :avocado: tags=device:sd
174        """
175        self.set_machine('orangepi-pc')
176        # This test download a 275 MiB compressed image and expand it
177        # to 1036 MiB, but the underlying filesystem is 1552 MiB...
178        # As we expand it to 2 GiB we are safe.
179        image_path_xz = self.ASSET_ARMBIAN.fetch()
180        image_path = os.path.join(self.workdir, 'armbian.img')
181        lzma_uncompress(image_path_xz, image_path)
182        image_pow2ceil_expand(image_path)
183
184        self.vm.set_console()
185        self.vm.add_args('-drive', 'file=' + image_path + ',if=sd,format=raw',
186                         '-nic', 'user',
187                         '-no-reboot')
188        self.vm.launch()
189
190        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
191                               'console=ttyS0,115200 '
192                               'loglevel=7 '
193                               'nosmp '
194                               'systemd.default_timeout_start_sec=9000 '
195                               'systemd.mask=armbian-zram-config.service '
196                               'systemd.mask=armbian-ramlog.service')
197
198        self.wait_for_console_pattern('U-Boot SPL')
199        self.wait_for_console_pattern('Autoboot in ')
200        exec_command_and_wait_for_pattern(self, ' ', '=>')
201        exec_command_and_wait_for_pattern(self, "setenv extraargs '" +
202                                                kernel_command_line + "'", '=>')
203        exec_command_and_wait_for_pattern(self, 'boot', 'Starting kernel ...');
204
205        self.wait_for_console_pattern('systemd[1]: Hostname set ' +
206                                      'to <orangepipc>')
207        self.wait_for_console_pattern('Starting Load Kernel Modules...')
208
209    @skipUnless(os.getenv('QEMU_TEST_ALLOW_LARGE_STORAGE'), 'storage limited')
210    def test_arm_orangepi_uboot_netbsd9(self):
211        """
212        :avocado: tags=arch:arm
213        :avocado: tags=machine:orangepi-pc
214        :avocado: tags=device:sd
215        :avocado: tags=os:netbsd
216        """
217        self.set_machine('orangepi-pc')
218        # This test download a 304MB compressed image and expand it to 2GB
219        deb_path = self.ASSET_UBOOT.fetch()
220        # We use the common OrangePi PC 'plus' build of U-Boot for our secondary
221        # program loader (SPL). We will then set the path to the more specific
222        # OrangePi "PC" device tree blob with 'setenv fdtfile' in U-Boot prompt,
223        # before to boot NetBSD.
224        uboot_path = '/usr/lib/u-boot/orangepi_plus/u-boot-sunxi-with-spl.bin'
225        uboot_path = self.extract_from_deb(deb_path, uboot_path)
226        image_path_gz = self.ASSET_NETBSD.fetch()
227        image_path = os.path.join(self.workdir, 'armv7.img')
228        gzip_uncompress(image_path_gz, image_path)
229        image_pow2ceil_expand(image_path)
230        image_drive_args = 'if=sd,format=raw,snapshot=on,file=' + image_path
231
232        # dd if=u-boot-sunxi-with-spl.bin of=armv7.img bs=1K seek=8 conv=notrunc
233        with open(uboot_path, 'rb') as f_in:
234            with open(image_path, 'r+b') as f_out:
235                f_out.seek(8 * 1024)
236                shutil.copyfileobj(f_in, f_out)
237
238        self.vm.set_console()
239        self.vm.add_args('-nic', 'user',
240                         '-drive', image_drive_args,
241                         '-global', 'allwinner-rtc.base-year=2000',
242                         '-no-reboot')
243        self.vm.launch()
244        wait_for_console_pattern(self, 'U-Boot 2020.01+dfsg-1')
245        interrupt_interactive_console_until_pattern(self,
246                                       'Hit any key to stop autoboot:',
247                                       'switch to partitions #0, OK')
248
249        exec_command_and_wait_for_pattern(self, '', '=>')
250        cmd = 'setenv bootargs root=ld0a'
251        exec_command_and_wait_for_pattern(self, cmd, '=>')
252        cmd = 'setenv kernel netbsd-GENERIC.ub'
253        exec_command_and_wait_for_pattern(self, cmd, '=>')
254        cmd = 'setenv fdtfile dtb/sun8i-h3-orangepi-pc.dtb'
255        exec_command_and_wait_for_pattern(self, cmd, '=>')
256        cmd = ("setenv bootcmd 'fatload mmc 0:1 ${kernel_addr_r} ${kernel}; "
257               "fatload mmc 0:1 ${fdt_addr_r} ${fdtfile}; "
258               "fdt addr ${fdt_addr_r}; "
259               "bootm ${kernel_addr_r} - ${fdt_addr_r}'")
260        exec_command_and_wait_for_pattern(self, cmd, '=>')
261
262        exec_command_and_wait_for_pattern(self, 'boot',
263                                          'Booting kernel from Legacy Image')
264        wait_for_console_pattern(self, 'Starting kernel ...')
265        wait_for_console_pattern(self, 'NetBSD 9.0 (GENERIC)')
266        # Wait for user-space
267        wait_for_console_pattern(self, 'Starting root file system check')
268
269if __name__ == '__main__':
270    LinuxKernelTest.main()
271