xref: /qemu/tests/functional/test_arm_orangepi.py (revision 513823e7521a09ed7ad1e32e6454bac3b2cbf52d)
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, skipBigDataTest
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_ARMBIAN = Asset(
36        ('https://k-space.ee.armbian.com/archive/orangepipc/archive/'
37         'Armbian_23.8.1_Orangepipc_jammy_current_6.1.47.img.xz'),
38        'b386dff6552513b5f164ea00f94814a6b0f1da9fb90b83725e949cf797e11afb')
39
40    ASSET_UBOOT = Asset(
41        ('http://snapshot.debian.org/archive/debian/20200108T145233Z/pool/'
42         'main/u/u-boot/u-boot-sunxi_2020.01%2Bdfsg-1_armhf.deb'),
43        '9223d94dc283ab54df41ce9d6f69025a5b47fece29fb67a714e23aa0cdf3bdfa')
44
45    ASSET_NETBSD = Asset(
46        ('https://archive.netbsd.org/pub/NetBSD-archive/NetBSD-9.0/'
47         'evbarm-earmv7hf/binary/gzimg/armv7.img.gz'),
48        '20d3e07dc057e15c12452620e90ecab2047f0f7940d9cba8182ebc795927177f')
49
50    def test_arm_orangepi(self):
51        self.set_machine('orangepi-pc')
52        kernel_path = self.archive_extract(
53            self.ASSET_DEB, member='boot/vmlinuz-6.6.16-current-sunxi')
54        dtb_path = ('usr/lib/linux-image-6.6.16-current-sunxi/' +
55                    'sun8i-h3-orangepi-pc.dtb')
56        dtb_path = self.archive_extract(self.ASSET_DEB, member=dtb_path)
57
58        self.vm.set_console()
59        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
60                               'console=ttyS0,115200n8 '
61                               'earlycon=uart,mmio32,0x1c28000')
62        self.vm.add_args('-kernel', kernel_path,
63                         '-dtb', dtb_path,
64                         '-append', kernel_command_line)
65        self.vm.launch()
66        console_pattern = 'Kernel command line: %s' % kernel_command_line
67        self.wait_for_console_pattern(console_pattern)
68        os.remove(kernel_path)
69        os.remove(dtb_path)
70
71    def test_arm_orangepi_initrd(self):
72        self.set_machine('orangepi-pc')
73        kernel_path = self.archive_extract(
74            self.ASSET_DEB, member='boot/vmlinuz-6.6.16-current-sunxi')
75        dtb_path = ('usr/lib/linux-image-6.6.16-current-sunxi/' +
76                    'sun8i-h3-orangepi-pc.dtb')
77        dtb_path = self.archive_extract(self.ASSET_DEB, member=dtb_path)
78        initrd_path = self.uncompress(self.ASSET_INITRD)
79
80        self.vm.set_console()
81        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
82                               'console=ttyS0,115200 '
83                               'panic=-1 noreboot')
84        self.vm.add_args('-kernel', kernel_path,
85                         '-dtb', dtb_path,
86                         '-initrd', initrd_path,
87                         '-append', kernel_command_line,
88                         '-no-reboot')
89        self.vm.launch()
90        self.wait_for_console_pattern('Boot successful.')
91
92        exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo',
93                                                'Allwinner sun8i Family')
94        exec_command_and_wait_for_pattern(self, 'cat /proc/iomem',
95                                                'system-control@1c00000')
96        exec_command_and_wait_for_pattern(self, 'reboot',
97                                                'reboot: Restarting system')
98        # Wait for VM to shut down gracefully
99        self.vm.wait()
100        os.remove(kernel_path)
101        os.remove(dtb_path)
102        os.remove(initrd_path)
103
104    def test_arm_orangepi_sd(self):
105        self.set_machine('orangepi-pc')
106        self.require_netdev('user')
107        kernel_path = self.archive_extract(
108            self.ASSET_DEB, member='boot/vmlinuz-6.6.16-current-sunxi')
109        dtb_path = ('usr/lib/linux-image-6.6.16-current-sunxi/' +
110                    'sun8i-h3-orangepi-pc.dtb')
111        dtb_path = self.archive_extract(self.ASSET_DEB, member=dtb_path)
112        rootfs_path = self.uncompress(self.ASSET_ROOTFS)
113        image_pow2ceil_expand(rootfs_path)
114
115        self.vm.set_console()
116        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
117                               'console=ttyS0,115200 '
118                               'root=/dev/mmcblk0 rootwait rw '
119                               'panic=-1 noreboot')
120        self.vm.add_args('-kernel', kernel_path,
121                         '-dtb', dtb_path,
122                         '-drive', 'file=' + rootfs_path + ',if=sd,format=raw',
123                         '-append', kernel_command_line,
124                         '-no-reboot')
125        self.vm.launch()
126        shell_ready = "/bin/sh: can't access tty; job control turned off"
127        self.wait_for_console_pattern(shell_ready)
128
129        exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo',
130                                                'Allwinner sun8i Family')
131        exec_command_and_wait_for_pattern(self, 'cat /proc/partitions',
132                                                'mmcblk0')
133        exec_command_and_wait_for_pattern(self, 'ifconfig eth0 up',
134                                                 'eth0: Link is Up')
135        exec_command_and_wait_for_pattern(self, 'udhcpc eth0',
136            'udhcpc: lease of 10.0.2.15 obtained')
137        exec_command_and_wait_for_pattern(self, 'ping -c 3 10.0.2.2',
138            '3 packets transmitted, 3 packets received, 0% packet loss')
139        exec_command_and_wait_for_pattern(self, 'reboot',
140                                                'reboot: Restarting system')
141        # Wait for VM to shut down gracefully
142        self.vm.wait()
143        os.remove(kernel_path)
144        os.remove(dtb_path)
145        os.remove(rootfs_path)
146
147    @skipBigDataTest()
148    def test_arm_orangepi_armbian(self):
149        self.set_machine('orangepi-pc')
150        # This test download a 275 MiB compressed image and expand it
151        # to 1036 MiB, but the underlying filesystem is 1552 MiB...
152        # As we expand it to 2 GiB we are safe.
153        image_path = self.uncompress(self.ASSET_ARMBIAN)
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                               'console=ttyS0,115200 '
164                               'loglevel=7 '
165                               'nosmp '
166                               'systemd.default_timeout_start_sec=9000 '
167                               'systemd.mask=armbian-zram-config.service '
168                               'systemd.mask=armbian-ramlog.service')
169
170        self.wait_for_console_pattern('U-Boot SPL')
171        self.wait_for_console_pattern('Autoboot in ')
172        exec_command_and_wait_for_pattern(self, ' ', '=>')
173        exec_command_and_wait_for_pattern(self, "setenv extraargs '" +
174                                                kernel_command_line + "'", '=>')
175        exec_command_and_wait_for_pattern(self, 'boot', 'Starting kernel ...');
176
177        self.wait_for_console_pattern('systemd[1]: Hostname set ' +
178                                      'to <orangepipc>')
179        self.wait_for_console_pattern('Starting Load Kernel Modules...')
180
181    @skipBigDataTest()
182    def test_arm_orangepi_uboot_netbsd9(self):
183        self.set_machine('orangepi-pc')
184        # This test download a 304MB compressed image and expand it to 2GB
185        # We use the common OrangePi PC 'plus' build of U-Boot for our secondary
186        # program loader (SPL). We will then set the path to the more specific
187        # OrangePi "PC" device tree blob with 'setenv fdtfile' in U-Boot prompt,
188        # before to boot NetBSD.
189        uboot_path = 'usr/lib/u-boot/orangepi_plus/u-boot-sunxi-with-spl.bin'
190        uboot_path = self.archive_extract(self.ASSET_UBOOT, member=uboot_path)
191        image_path = self.uncompress(self.ASSET_NETBSD)
192        image_pow2ceil_expand(image_path)
193        image_drive_args = 'if=sd,format=raw,snapshot=on,file=' + image_path
194
195        # dd if=u-boot-sunxi-with-spl.bin of=armv7.img bs=1K seek=8 conv=notrunc
196        with open(uboot_path, 'rb') as f_in:
197            with open(image_path, 'r+b') as f_out:
198                f_out.seek(8 * 1024)
199                shutil.copyfileobj(f_in, f_out)
200
201        self.vm.set_console()
202        self.vm.add_args('-nic', 'user',
203                         '-drive', image_drive_args,
204                         '-global', 'allwinner-rtc.base-year=2000',
205                         '-no-reboot')
206        self.vm.launch()
207        wait_for_console_pattern(self, 'U-Boot 2020.01+dfsg-1')
208        interrupt_interactive_console_until_pattern(self,
209                                       'Hit any key to stop autoboot:',
210                                       'switch to partitions #0, OK')
211
212        exec_command_and_wait_for_pattern(self, '', '=>')
213        cmd = 'setenv bootargs root=ld0a'
214        exec_command_and_wait_for_pattern(self, cmd, '=>')
215        cmd = 'setenv kernel netbsd-GENERIC.ub'
216        exec_command_and_wait_for_pattern(self, cmd, '=>')
217        cmd = 'setenv fdtfile dtb/sun8i-h3-orangepi-pc.dtb'
218        exec_command_and_wait_for_pattern(self, cmd, '=>')
219        cmd = ("setenv bootcmd 'fatload mmc 0:1 ${kernel_addr_r} ${kernel}; "
220               "fatload mmc 0:1 ${fdt_addr_r} ${fdtfile}; "
221               "fdt addr ${fdt_addr_r}; "
222               "bootm ${kernel_addr_r} - ${fdt_addr_r}'")
223        exec_command_and_wait_for_pattern(self, cmd, '=>')
224
225        exec_command_and_wait_for_pattern(self, 'boot',
226                                          'Booting kernel from Legacy Image')
227        wait_for_console_pattern(self, 'Starting kernel ...')
228        wait_for_console_pattern(self, 'NetBSD 9.0 (GENERIC)')
229        # Wait for user-space
230        wait_for_console_pattern(self, 'Starting root file system check')
231
232if __name__ == '__main__':
233    LinuxKernelTest.main()
234