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