xref: /qemu/tests/functional/test_arm_orangepi.py (revision 55f5bf716a65f67663d0769bcb8c017764b3e53a)
1380f7268SThomas Huth#!/usr/bin/env python3
2380f7268SThomas Huth#
3380f7268SThomas Huth# Functional test that boots a Linux kernel on an Orange Pi machine
4380f7268SThomas Huth# and checks the console
5380f7268SThomas Huth#
6380f7268SThomas Huth# SPDX-License-Identifier: GPL-2.0-or-later
7380f7268SThomas Huth
8380f7268SThomas Huthimport os
9380f7268SThomas Huthimport shutil
10380f7268SThomas Huth
11380f7268SThomas Huthfrom qemu_test import LinuxKernelTest, exec_command_and_wait_for_pattern
12380f7268SThomas Huthfrom qemu_test import Asset, interrupt_interactive_console_until_pattern
133d593860SDaniel P. Berrangéfrom qemu_test import wait_for_console_pattern, skipBigDataTest
14380f7268SThomas Huthfrom qemu_test.utils import image_pow2ceil_expand
153d593860SDaniel P. Berrangé
16380f7268SThomas Huth
176d19d095SNiek Linnenbankclass OrangePiMachine(LinuxKernelTest):
18380f7268SThomas Huth
19380f7268SThomas Huth    ASSET_DEB = Asset(
20380f7268SThomas Huth        ('https://apt.armbian.com/pool/main/l/linux-6.6.16/'
21380f7268SThomas Huth         'linux-image-current-sunxi_24.2.1_armhf__6.6.16-Seb3e-D6b4a-P2359-Ce96bHfe66-HK01ba-V014b-B067e-R448a.deb'),
22380f7268SThomas Huth        '3d968c15b121ede871dce49d13ee7644d6f74b6b121b84c9a40f51b0c80d6d22')
23380f7268SThomas Huth
24380f7268SThomas Huth    ASSET_INITRD = Asset(
25380f7268SThomas Huth        ('https://github.com/groeck/linux-build-test/raw/'
26380f7268SThomas Huth         '2eb0a73b5d5a28df3170c546ddaaa9757e1e0848/rootfs/'
27380f7268SThomas Huth         'arm/rootfs-armv7a.cpio.gz'),
28380f7268SThomas Huth        '2c8dbdb16ea7af2dfbcbea96044dde639fb07d09fd3c4fb31f2027ef71e55ddd')
29380f7268SThomas Huth
30380f7268SThomas Huth    ASSET_ROOTFS = Asset(
31380f7268SThomas Huth        ('http://storage.kernelci.org/images/rootfs/buildroot/'
32380f7268SThomas Huth         'buildroot-baseline/20230703.0/armel/rootfs.ext2.xz'),
33380f7268SThomas Huth        '42b44a12965ac0afe9a88378527fb698a7dc76af50495efc2361ee1595b4e5c6')
34380f7268SThomas Huth
35380f7268SThomas Huth    ASSET_ARMBIAN = Asset(
36380f7268SThomas Huth        ('https://k-space.ee.armbian.com/archive/orangepipc/archive/'
37380f7268SThomas Huth         'Armbian_23.8.1_Orangepipc_jammy_current_6.1.47.img.xz'),
38380f7268SThomas Huth        'b386dff6552513b5f164ea00f94814a6b0f1da9fb90b83725e949cf797e11afb')
39380f7268SThomas Huth
40380f7268SThomas Huth    ASSET_UBOOT = Asset(
41380f7268SThomas Huth        ('http://snapshot.debian.org/archive/debian/20200108T145233Z/pool/'
42380f7268SThomas Huth         'main/u/u-boot/u-boot-sunxi_2020.01%2Bdfsg-1_armhf.deb'),
43380f7268SThomas Huth        '9223d94dc283ab54df41ce9d6f69025a5b47fece29fb67a714e23aa0cdf3bdfa')
44380f7268SThomas Huth
45380f7268SThomas Huth    ASSET_NETBSD = Asset(
46380f7268SThomas Huth        ('https://archive.netbsd.org/pub/NetBSD-archive/NetBSD-9.0/'
47380f7268SThomas Huth         'evbarm-earmv7hf/binary/gzimg/armv7.img.gz'),
48380f7268SThomas Huth        '20d3e07dc057e15c12452620e90ecab2047f0f7940d9cba8182ebc795927177f')
49380f7268SThomas Huth
50380f7268SThomas Huth    def test_arm_orangepi(self):
51380f7268SThomas Huth        self.set_machine('orangepi-pc')
525831ed84SDaniel P. Berrangé        kernel_path = self.archive_extract(
535831ed84SDaniel P. Berrangé            self.ASSET_DEB, member='boot/vmlinuz-6.6.16-current-sunxi')
545831ed84SDaniel P. Berrangé        dtb_path = ('usr/lib/linux-image-6.6.16-current-sunxi/' +
555831ed84SDaniel P. Berrangé                    'sun8i-h3-orangepi-pc.dtb')
565831ed84SDaniel P. Berrangé        dtb_path = self.archive_extract(self.ASSET_DEB, member=dtb_path)
57380f7268SThomas Huth
58380f7268SThomas Huth        self.vm.set_console()
59380f7268SThomas Huth        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
60380f7268SThomas Huth                               'console=ttyS0,115200n8 '
61380f7268SThomas Huth                               'earlycon=uart,mmio32,0x1c28000')
62380f7268SThomas Huth        self.vm.add_args('-kernel', kernel_path,
63380f7268SThomas Huth                         '-dtb', dtb_path,
64380f7268SThomas Huth                         '-append', kernel_command_line)
65380f7268SThomas Huth        self.vm.launch()
66380f7268SThomas Huth        console_pattern = 'Kernel command line: %s' % kernel_command_line
67380f7268SThomas Huth        self.wait_for_console_pattern(console_pattern)
68380f7268SThomas Huth        os.remove(kernel_path)
69380f7268SThomas Huth        os.remove(dtb_path)
70380f7268SThomas Huth
71380f7268SThomas Huth    def test_arm_orangepi_initrd(self):
72380f7268SThomas Huth        self.set_machine('orangepi-pc')
735831ed84SDaniel P. Berrangé        kernel_path = self.archive_extract(
745831ed84SDaniel P. Berrangé            self.ASSET_DEB, member='boot/vmlinuz-6.6.16-current-sunxi')
755831ed84SDaniel P. Berrangé        dtb_path = ('usr/lib/linux-image-6.6.16-current-sunxi/' +
765831ed84SDaniel P. Berrangé                    'sun8i-h3-orangepi-pc.dtb')
775831ed84SDaniel P. Berrangé        dtb_path = self.archive_extract(self.ASSET_DEB, member=dtb_path)
7865d35a4eSDaniel P. Berrangé        initrd_path = self.uncompress(self.ASSET_INITRD)
79380f7268SThomas Huth
80380f7268SThomas Huth        self.vm.set_console()
81380f7268SThomas Huth        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
82380f7268SThomas Huth                               'console=ttyS0,115200 '
83380f7268SThomas Huth                               'panic=-1 noreboot')
84380f7268SThomas Huth        self.vm.add_args('-kernel', kernel_path,
85380f7268SThomas Huth                         '-dtb', dtb_path,
86380f7268SThomas Huth                         '-initrd', initrd_path,
87380f7268SThomas Huth                         '-append', kernel_command_line,
88380f7268SThomas Huth                         '-no-reboot')
89380f7268SThomas Huth        self.vm.launch()
90380f7268SThomas Huth        self.wait_for_console_pattern('Boot successful.')
91380f7268SThomas Huth
92380f7268SThomas Huth        exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo',
93380f7268SThomas Huth                                                'Allwinner sun8i Family')
94380f7268SThomas Huth        exec_command_and_wait_for_pattern(self, 'cat /proc/iomem',
95380f7268SThomas Huth                                                'system-control@1c00000')
96380f7268SThomas Huth        exec_command_and_wait_for_pattern(self, 'reboot',
97380f7268SThomas Huth                                                'reboot: Restarting system')
98380f7268SThomas Huth        # Wait for VM to shut down gracefully
99380f7268SThomas Huth        self.vm.wait()
100380f7268SThomas Huth        os.remove(kernel_path)
101380f7268SThomas Huth        os.remove(dtb_path)
102380f7268SThomas Huth        os.remove(initrd_path)
103380f7268SThomas Huth
104380f7268SThomas Huth    def test_arm_orangepi_sd(self):
105380f7268SThomas Huth        self.set_machine('orangepi-pc')
106380f7268SThomas Huth        self.require_netdev('user')
1075831ed84SDaniel P. Berrangé        kernel_path = self.archive_extract(
1085831ed84SDaniel P. Berrangé            self.ASSET_DEB, member='boot/vmlinuz-6.6.16-current-sunxi')
1095831ed84SDaniel P. Berrangé        dtb_path = ('usr/lib/linux-image-6.6.16-current-sunxi/' +
1105831ed84SDaniel P. Berrangé                    'sun8i-h3-orangepi-pc.dtb')
1115831ed84SDaniel P. Berrangé        dtb_path = self.archive_extract(self.ASSET_DEB, member=dtb_path)
11265d35a4eSDaniel P. Berrangé        rootfs_path = self.uncompress(self.ASSET_ROOTFS)
113380f7268SThomas Huth        image_pow2ceil_expand(rootfs_path)
114380f7268SThomas Huth
115380f7268SThomas Huth        self.vm.set_console()
116380f7268SThomas Huth        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
117380f7268SThomas Huth                               'console=ttyS0,115200 '
118380f7268SThomas Huth                               'root=/dev/mmcblk0 rootwait rw '
119380f7268SThomas Huth                               'panic=-1 noreboot')
120380f7268SThomas Huth        self.vm.add_args('-kernel', kernel_path,
121380f7268SThomas Huth                         '-dtb', dtb_path,
122380f7268SThomas Huth                         '-drive', 'file=' + rootfs_path + ',if=sd,format=raw',
123380f7268SThomas Huth                         '-append', kernel_command_line,
124380f7268SThomas Huth                         '-no-reboot')
125380f7268SThomas Huth        self.vm.launch()
126380f7268SThomas Huth        shell_ready = "/bin/sh: can't access tty; job control turned off"
127380f7268SThomas Huth        self.wait_for_console_pattern(shell_ready)
128380f7268SThomas Huth
129380f7268SThomas Huth        exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo',
130380f7268SThomas Huth                                                'Allwinner sun8i Family')
131380f7268SThomas Huth        exec_command_and_wait_for_pattern(self, 'cat /proc/partitions',
132380f7268SThomas Huth                                                'mmcblk0')
133380f7268SThomas Huth        exec_command_and_wait_for_pattern(self, 'ifconfig eth0 up',
134380f7268SThomas Huth                                                 'eth0: Link is Up')
135380f7268SThomas Huth        exec_command_and_wait_for_pattern(self, 'udhcpc eth0',
136380f7268SThomas Huth            'udhcpc: lease of 10.0.2.15 obtained')
137380f7268SThomas Huth        exec_command_and_wait_for_pattern(self, 'ping -c 3 10.0.2.2',
138380f7268SThomas Huth            '3 packets transmitted, 3 packets received, 0% packet loss')
139380f7268SThomas Huth        exec_command_and_wait_for_pattern(self, 'reboot',
140380f7268SThomas Huth                                                'reboot: Restarting system')
141380f7268SThomas Huth        # Wait for VM to shut down gracefully
142380f7268SThomas Huth        self.vm.wait()
143380f7268SThomas Huth        os.remove(kernel_path)
144380f7268SThomas Huth        os.remove(dtb_path)
145380f7268SThomas Huth        os.remove(rootfs_path)
146380f7268SThomas Huth
1473d593860SDaniel P. Berrangé    @skipBigDataTest()
148380f7268SThomas Huth    def test_arm_orangepi_armbian(self):
149380f7268SThomas Huth        self.set_machine('orangepi-pc')
150dba0752fSThomas Huth        self.require_netdev('user')
151dba0752fSThomas Huth
152380f7268SThomas Huth        # This test download a 275 MiB compressed image and expand it
153380f7268SThomas Huth        # to 1036 MiB, but the underlying filesystem is 1552 MiB...
154380f7268SThomas Huth        # As we expand it to 2 GiB we are safe.
15565d35a4eSDaniel P. Berrangé        image_path = self.uncompress(self.ASSET_ARMBIAN)
156380f7268SThomas Huth        image_pow2ceil_expand(image_path)
157380f7268SThomas Huth
158380f7268SThomas Huth        self.vm.set_console()
159380f7268SThomas Huth        self.vm.add_args('-drive', 'file=' + image_path + ',if=sd,format=raw',
160380f7268SThomas Huth                         '-nic', 'user',
161380f7268SThomas Huth                         '-no-reboot')
162380f7268SThomas Huth        self.vm.launch()
163380f7268SThomas Huth
164380f7268SThomas Huth        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
165380f7268SThomas Huth                               'console=ttyS0,115200 '
166380f7268SThomas Huth                               'loglevel=7 '
167380f7268SThomas Huth                               'nosmp '
168380f7268SThomas Huth                               'systemd.default_timeout_start_sec=9000 '
169380f7268SThomas Huth                               'systemd.mask=armbian-zram-config.service '
170380f7268SThomas Huth                               'systemd.mask=armbian-ramlog.service')
171380f7268SThomas Huth
172380f7268SThomas Huth        self.wait_for_console_pattern('U-Boot SPL')
173380f7268SThomas Huth        self.wait_for_console_pattern('Autoboot in ')
174380f7268SThomas Huth        exec_command_and_wait_for_pattern(self, ' ', '=>')
175380f7268SThomas Huth        exec_command_and_wait_for_pattern(self, "setenv extraargs '" +
176380f7268SThomas Huth                                                kernel_command_line + "'", '=>')
177*858640eaSThomas Huth        exec_command_and_wait_for_pattern(self, 'boot', 'Starting kernel ...')
178380f7268SThomas Huth
179380f7268SThomas Huth        self.wait_for_console_pattern('systemd[1]: Hostname set ' +
180380f7268SThomas Huth                                      'to <orangepipc>')
181380f7268SThomas Huth        self.wait_for_console_pattern('Starting Load Kernel Modules...')
182380f7268SThomas Huth
1833d593860SDaniel P. Berrangé    @skipBigDataTest()
184380f7268SThomas Huth    def test_arm_orangepi_uboot_netbsd9(self):
185380f7268SThomas Huth        self.set_machine('orangepi-pc')
186dba0752fSThomas Huth        self.require_netdev('user')
187dba0752fSThomas Huth
188380f7268SThomas Huth        # This test download a 304MB compressed image and expand it to 2GB
189380f7268SThomas Huth        # We use the common OrangePi PC 'plus' build of U-Boot for our secondary
190380f7268SThomas Huth        # program loader (SPL). We will then set the path to the more specific
191380f7268SThomas Huth        # OrangePi "PC" device tree blob with 'setenv fdtfile' in U-Boot prompt,
192380f7268SThomas Huth        # before to boot NetBSD.
1935831ed84SDaniel P. Berrangé        uboot_path = 'usr/lib/u-boot/orangepi_plus/u-boot-sunxi-with-spl.bin'
1945831ed84SDaniel P. Berrangé        uboot_path = self.archive_extract(self.ASSET_UBOOT, member=uboot_path)
19565d35a4eSDaniel P. Berrangé        image_path = self.uncompress(self.ASSET_NETBSD)
196380f7268SThomas Huth        image_pow2ceil_expand(image_path)
197380f7268SThomas Huth        image_drive_args = 'if=sd,format=raw,snapshot=on,file=' + image_path
198380f7268SThomas Huth
199380f7268SThomas Huth        # dd if=u-boot-sunxi-with-spl.bin of=armv7.img bs=1K seek=8 conv=notrunc
200380f7268SThomas Huth        with open(uboot_path, 'rb') as f_in:
201380f7268SThomas Huth            with open(image_path, 'r+b') as f_out:
202380f7268SThomas Huth                f_out.seek(8 * 1024)
203380f7268SThomas Huth                shutil.copyfileobj(f_in, f_out)
204380f7268SThomas Huth
205380f7268SThomas Huth        self.vm.set_console()
206380f7268SThomas Huth        self.vm.add_args('-nic', 'user',
207380f7268SThomas Huth                         '-drive', image_drive_args,
208380f7268SThomas Huth                         '-global', 'allwinner-rtc.base-year=2000',
209380f7268SThomas Huth                         '-no-reboot')
210380f7268SThomas Huth        self.vm.launch()
211380f7268SThomas Huth        wait_for_console_pattern(self, 'U-Boot 2020.01+dfsg-1')
212380f7268SThomas Huth        interrupt_interactive_console_until_pattern(self,
213380f7268SThomas Huth                                       'Hit any key to stop autoboot:',
214380f7268SThomas Huth                                       'switch to partitions #0, OK')
215380f7268SThomas Huth
216380f7268SThomas Huth        exec_command_and_wait_for_pattern(self, '', '=>')
217380f7268SThomas Huth        cmd = 'setenv bootargs root=ld0a'
218380f7268SThomas Huth        exec_command_and_wait_for_pattern(self, cmd, '=>')
219380f7268SThomas Huth        cmd = 'setenv kernel netbsd-GENERIC.ub'
220380f7268SThomas Huth        exec_command_and_wait_for_pattern(self, cmd, '=>')
221380f7268SThomas Huth        cmd = 'setenv fdtfile dtb/sun8i-h3-orangepi-pc.dtb'
222380f7268SThomas Huth        exec_command_and_wait_for_pattern(self, cmd, '=>')
223380f7268SThomas Huth        cmd = ("setenv bootcmd 'fatload mmc 0:1 ${kernel_addr_r} ${kernel}; "
224380f7268SThomas Huth               "fatload mmc 0:1 ${fdt_addr_r} ${fdtfile}; "
225380f7268SThomas Huth               "fdt addr ${fdt_addr_r}; "
226380f7268SThomas Huth               "bootm ${kernel_addr_r} - ${fdt_addr_r}'")
227380f7268SThomas Huth        exec_command_and_wait_for_pattern(self, cmd, '=>')
228380f7268SThomas Huth
229380f7268SThomas Huth        exec_command_and_wait_for_pattern(self, 'boot',
230380f7268SThomas Huth                                          'Booting kernel from Legacy Image')
231380f7268SThomas Huth        wait_for_console_pattern(self, 'Starting kernel ...')
232380f7268SThomas Huth        wait_for_console_pattern(self, 'NetBSD 9.0 (GENERIC)')
233380f7268SThomas Huth        # Wait for user-space
234380f7268SThomas Huth        wait_for_console_pattern(self, 'Starting root file system check')
235380f7268SThomas Huth
236380f7268SThomas Huthif __name__ == '__main__':
237380f7268SThomas Huth    LinuxKernelTest.main()
238