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 13*3d593860SDaniel P. Berrangéfrom qemu_test import wait_for_console_pattern, skipBigDataTest 148a6253a4SDaniel P. Berrangéfrom qemu_test.utils import gzip_uncompress, lzma_uncompress 15380f7268SThomas Huthfrom qemu_test.utils import image_pow2ceil_expand 16*3d593860SDaniel P. Berrangé 17380f7268SThomas Huth 18380f7268SThomas Huthclass BananaPiMachine(LinuxKernelTest): 19380f7268SThomas Huth 20380f7268SThomas Huth ASSET_DEB = Asset( 21380f7268SThomas Huth ('https://apt.armbian.com/pool/main/l/linux-6.6.16/' 22380f7268SThomas Huth 'linux-image-current-sunxi_24.2.1_armhf__6.6.16-Seb3e-D6b4a-P2359-Ce96bHfe66-HK01ba-V014b-B067e-R448a.deb'), 23380f7268SThomas Huth '3d968c15b121ede871dce49d13ee7644d6f74b6b121b84c9a40f51b0c80d6d22') 24380f7268SThomas Huth 25380f7268SThomas Huth ASSET_INITRD = Asset( 26380f7268SThomas Huth ('https://github.com/groeck/linux-build-test/raw/' 27380f7268SThomas Huth '2eb0a73b5d5a28df3170c546ddaaa9757e1e0848/rootfs/' 28380f7268SThomas Huth 'arm/rootfs-armv7a.cpio.gz'), 29380f7268SThomas Huth '2c8dbdb16ea7af2dfbcbea96044dde639fb07d09fd3c4fb31f2027ef71e55ddd') 30380f7268SThomas Huth 31380f7268SThomas Huth ASSET_ROOTFS = Asset( 32380f7268SThomas Huth ('http://storage.kernelci.org/images/rootfs/buildroot/' 33380f7268SThomas Huth 'buildroot-baseline/20230703.0/armel/rootfs.ext2.xz'), 34380f7268SThomas Huth '42b44a12965ac0afe9a88378527fb698a7dc76af50495efc2361ee1595b4e5c6') 35380f7268SThomas Huth 36380f7268SThomas Huth ASSET_ARMBIAN = Asset( 37380f7268SThomas Huth ('https://k-space.ee.armbian.com/archive/orangepipc/archive/' 38380f7268SThomas Huth 'Armbian_23.8.1_Orangepipc_jammy_current_6.1.47.img.xz'), 39380f7268SThomas Huth 'b386dff6552513b5f164ea00f94814a6b0f1da9fb90b83725e949cf797e11afb') 40380f7268SThomas Huth 41380f7268SThomas Huth ASSET_UBOOT = Asset( 42380f7268SThomas Huth ('http://snapshot.debian.org/archive/debian/20200108T145233Z/pool/' 43380f7268SThomas Huth 'main/u/u-boot/u-boot-sunxi_2020.01%2Bdfsg-1_armhf.deb'), 44380f7268SThomas Huth '9223d94dc283ab54df41ce9d6f69025a5b47fece29fb67a714e23aa0cdf3bdfa') 45380f7268SThomas Huth 46380f7268SThomas Huth ASSET_NETBSD = Asset( 47380f7268SThomas Huth ('https://archive.netbsd.org/pub/NetBSD-archive/NetBSD-9.0/' 48380f7268SThomas Huth 'evbarm-earmv7hf/binary/gzimg/armv7.img.gz'), 49380f7268SThomas Huth '20d3e07dc057e15c12452620e90ecab2047f0f7940d9cba8182ebc795927177f') 50380f7268SThomas Huth 51380f7268SThomas Huth def test_arm_orangepi(self): 52380f7268SThomas Huth self.set_machine('orangepi-pc') 53380f7268SThomas Huth deb_path = self.ASSET_DEB.fetch() 54380f7268SThomas Huth kernel_path = self.extract_from_deb(deb_path, 55380f7268SThomas Huth '/boot/vmlinuz-6.6.16-current-sunxi') 56380f7268SThomas Huth dtb_path = '/usr/lib/linux-image-6.6.16-current-sunxi/sun8i-h3-orangepi-pc.dtb' 57380f7268SThomas Huth dtb_path = self.extract_from_deb(deb_path, dtb_path) 58380f7268SThomas Huth 59380f7268SThomas Huth self.vm.set_console() 60380f7268SThomas Huth kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE + 61380f7268SThomas Huth 'console=ttyS0,115200n8 ' 62380f7268SThomas Huth 'earlycon=uart,mmio32,0x1c28000') 63380f7268SThomas Huth self.vm.add_args('-kernel', kernel_path, 64380f7268SThomas Huth '-dtb', dtb_path, 65380f7268SThomas Huth '-append', kernel_command_line) 66380f7268SThomas Huth self.vm.launch() 67380f7268SThomas Huth console_pattern = 'Kernel command line: %s' % kernel_command_line 68380f7268SThomas Huth self.wait_for_console_pattern(console_pattern) 69380f7268SThomas Huth os.remove(kernel_path) 70380f7268SThomas Huth os.remove(dtb_path) 71380f7268SThomas Huth 72380f7268SThomas Huth def test_arm_orangepi_initrd(self): 73380f7268SThomas Huth self.set_machine('orangepi-pc') 74380f7268SThomas Huth deb_path = self.ASSET_DEB.fetch() 75380f7268SThomas Huth kernel_path = self.extract_from_deb(deb_path, 76380f7268SThomas Huth '/boot/vmlinuz-6.6.16-current-sunxi') 77380f7268SThomas Huth dtb_path = '/usr/lib/linux-image-6.6.16-current-sunxi/sun8i-h3-orangepi-pc.dtb' 78380f7268SThomas Huth dtb_path = self.extract_from_deb(deb_path, dtb_path) 79380f7268SThomas Huth initrd_path_gz = self.ASSET_INITRD.fetch() 80380f7268SThomas Huth initrd_path = os.path.join(self.workdir, 'rootfs.cpio') 81380f7268SThomas Huth gzip_uncompress(initrd_path_gz, initrd_path) 82380f7268SThomas Huth 83380f7268SThomas Huth self.vm.set_console() 84380f7268SThomas Huth kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE + 85380f7268SThomas Huth 'console=ttyS0,115200 ' 86380f7268SThomas Huth 'panic=-1 noreboot') 87380f7268SThomas Huth self.vm.add_args('-kernel', kernel_path, 88380f7268SThomas Huth '-dtb', dtb_path, 89380f7268SThomas Huth '-initrd', initrd_path, 90380f7268SThomas Huth '-append', kernel_command_line, 91380f7268SThomas Huth '-no-reboot') 92380f7268SThomas Huth self.vm.launch() 93380f7268SThomas Huth self.wait_for_console_pattern('Boot successful.') 94380f7268SThomas Huth 95380f7268SThomas Huth exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo', 96380f7268SThomas Huth 'Allwinner sun8i Family') 97380f7268SThomas Huth exec_command_and_wait_for_pattern(self, 'cat /proc/iomem', 98380f7268SThomas Huth 'system-control@1c00000') 99380f7268SThomas Huth exec_command_and_wait_for_pattern(self, 'reboot', 100380f7268SThomas Huth 'reboot: Restarting system') 101380f7268SThomas Huth # Wait for VM to shut down gracefully 102380f7268SThomas Huth self.vm.wait() 103380f7268SThomas Huth os.remove(kernel_path) 104380f7268SThomas Huth os.remove(dtb_path) 105380f7268SThomas Huth os.remove(initrd_path) 106380f7268SThomas Huth 107380f7268SThomas Huth def test_arm_orangepi_sd(self): 108380f7268SThomas Huth self.set_machine('orangepi-pc') 109380f7268SThomas Huth self.require_netdev('user') 110380f7268SThomas Huth deb_path = self.ASSET_DEB.fetch() 111380f7268SThomas Huth kernel_path = self.extract_from_deb(deb_path, 112380f7268SThomas Huth '/boot/vmlinuz-6.6.16-current-sunxi') 113380f7268SThomas Huth dtb_path = '/usr/lib/linux-image-6.6.16-current-sunxi/sun8i-h3-orangepi-pc.dtb' 114380f7268SThomas Huth dtb_path = self.extract_from_deb(deb_path, dtb_path) 115380f7268SThomas Huth rootfs_path_xz = self.ASSET_ROOTFS.fetch() 116380f7268SThomas Huth rootfs_path = os.path.join(self.workdir, 'rootfs.cpio') 117380f7268SThomas Huth lzma_uncompress(rootfs_path_xz, rootfs_path) 118380f7268SThomas Huth image_pow2ceil_expand(rootfs_path) 119380f7268SThomas Huth 120380f7268SThomas Huth self.vm.set_console() 121380f7268SThomas Huth kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE + 122380f7268SThomas Huth 'console=ttyS0,115200 ' 123380f7268SThomas Huth 'root=/dev/mmcblk0 rootwait rw ' 124380f7268SThomas Huth 'panic=-1 noreboot') 125380f7268SThomas Huth self.vm.add_args('-kernel', kernel_path, 126380f7268SThomas Huth '-dtb', dtb_path, 127380f7268SThomas Huth '-drive', 'file=' + rootfs_path + ',if=sd,format=raw', 128380f7268SThomas Huth '-append', kernel_command_line, 129380f7268SThomas Huth '-no-reboot') 130380f7268SThomas Huth self.vm.launch() 131380f7268SThomas Huth shell_ready = "/bin/sh: can't access tty; job control turned off" 132380f7268SThomas Huth self.wait_for_console_pattern(shell_ready) 133380f7268SThomas Huth 134380f7268SThomas Huth exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo', 135380f7268SThomas Huth 'Allwinner sun8i Family') 136380f7268SThomas Huth exec_command_and_wait_for_pattern(self, 'cat /proc/partitions', 137380f7268SThomas Huth 'mmcblk0') 138380f7268SThomas Huth exec_command_and_wait_for_pattern(self, 'ifconfig eth0 up', 139380f7268SThomas Huth 'eth0: Link is Up') 140380f7268SThomas Huth exec_command_and_wait_for_pattern(self, 'udhcpc eth0', 141380f7268SThomas Huth 'udhcpc: lease of 10.0.2.15 obtained') 142380f7268SThomas Huth exec_command_and_wait_for_pattern(self, 'ping -c 3 10.0.2.2', 143380f7268SThomas Huth '3 packets transmitted, 3 packets received, 0% packet loss') 144380f7268SThomas Huth exec_command_and_wait_for_pattern(self, 'reboot', 145380f7268SThomas Huth 'reboot: Restarting system') 146380f7268SThomas Huth # Wait for VM to shut down gracefully 147380f7268SThomas Huth self.vm.wait() 148380f7268SThomas Huth os.remove(kernel_path) 149380f7268SThomas Huth os.remove(dtb_path) 150380f7268SThomas Huth os.remove(rootfs_path) 151380f7268SThomas Huth 152*3d593860SDaniel P. Berrangé @skipBigDataTest() 153380f7268SThomas Huth def test_arm_orangepi_armbian(self): 154380f7268SThomas Huth self.set_machine('orangepi-pc') 155380f7268SThomas Huth # This test download a 275 MiB compressed image and expand it 156380f7268SThomas Huth # to 1036 MiB, but the underlying filesystem is 1552 MiB... 157380f7268SThomas Huth # As we expand it to 2 GiB we are safe. 158380f7268SThomas Huth image_path_xz = self.ASSET_ARMBIAN.fetch() 159380f7268SThomas Huth image_path = os.path.join(self.workdir, 'armbian.img') 160380f7268SThomas Huth lzma_uncompress(image_path_xz, image_path) 161380f7268SThomas Huth image_pow2ceil_expand(image_path) 162380f7268SThomas Huth 163380f7268SThomas Huth self.vm.set_console() 164380f7268SThomas Huth self.vm.add_args('-drive', 'file=' + image_path + ',if=sd,format=raw', 165380f7268SThomas Huth '-nic', 'user', 166380f7268SThomas Huth '-no-reboot') 167380f7268SThomas Huth self.vm.launch() 168380f7268SThomas Huth 169380f7268SThomas Huth kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE + 170380f7268SThomas Huth 'console=ttyS0,115200 ' 171380f7268SThomas Huth 'loglevel=7 ' 172380f7268SThomas Huth 'nosmp ' 173380f7268SThomas Huth 'systemd.default_timeout_start_sec=9000 ' 174380f7268SThomas Huth 'systemd.mask=armbian-zram-config.service ' 175380f7268SThomas Huth 'systemd.mask=armbian-ramlog.service') 176380f7268SThomas Huth 177380f7268SThomas Huth self.wait_for_console_pattern('U-Boot SPL') 178380f7268SThomas Huth self.wait_for_console_pattern('Autoboot in ') 179380f7268SThomas Huth exec_command_and_wait_for_pattern(self, ' ', '=>') 180380f7268SThomas Huth exec_command_and_wait_for_pattern(self, "setenv extraargs '" + 181380f7268SThomas Huth kernel_command_line + "'", '=>') 182380f7268SThomas Huth exec_command_and_wait_for_pattern(self, 'boot', 'Starting kernel ...'); 183380f7268SThomas Huth 184380f7268SThomas Huth self.wait_for_console_pattern('systemd[1]: Hostname set ' + 185380f7268SThomas Huth 'to <orangepipc>') 186380f7268SThomas Huth self.wait_for_console_pattern('Starting Load Kernel Modules...') 187380f7268SThomas Huth 188*3d593860SDaniel P. Berrangé @skipBigDataTest() 189380f7268SThomas Huth def test_arm_orangepi_uboot_netbsd9(self): 190380f7268SThomas Huth self.set_machine('orangepi-pc') 191380f7268SThomas Huth # This test download a 304MB compressed image and expand it to 2GB 192380f7268SThomas Huth deb_path = self.ASSET_UBOOT.fetch() 193380f7268SThomas Huth # We use the common OrangePi PC 'plus' build of U-Boot for our secondary 194380f7268SThomas Huth # program loader (SPL). We will then set the path to the more specific 195380f7268SThomas Huth # OrangePi "PC" device tree blob with 'setenv fdtfile' in U-Boot prompt, 196380f7268SThomas Huth # before to boot NetBSD. 197380f7268SThomas Huth uboot_path = '/usr/lib/u-boot/orangepi_plus/u-boot-sunxi-with-spl.bin' 198380f7268SThomas Huth uboot_path = self.extract_from_deb(deb_path, uboot_path) 199380f7268SThomas Huth image_path_gz = self.ASSET_NETBSD.fetch() 200380f7268SThomas Huth image_path = os.path.join(self.workdir, 'armv7.img') 201380f7268SThomas Huth gzip_uncompress(image_path_gz, image_path) 202380f7268SThomas Huth image_pow2ceil_expand(image_path) 203380f7268SThomas Huth image_drive_args = 'if=sd,format=raw,snapshot=on,file=' + image_path 204380f7268SThomas Huth 205380f7268SThomas Huth # dd if=u-boot-sunxi-with-spl.bin of=armv7.img bs=1K seek=8 conv=notrunc 206380f7268SThomas Huth with open(uboot_path, 'rb') as f_in: 207380f7268SThomas Huth with open(image_path, 'r+b') as f_out: 208380f7268SThomas Huth f_out.seek(8 * 1024) 209380f7268SThomas Huth shutil.copyfileobj(f_in, f_out) 210380f7268SThomas Huth 211380f7268SThomas Huth self.vm.set_console() 212380f7268SThomas Huth self.vm.add_args('-nic', 'user', 213380f7268SThomas Huth '-drive', image_drive_args, 214380f7268SThomas Huth '-global', 'allwinner-rtc.base-year=2000', 215380f7268SThomas Huth '-no-reboot') 216380f7268SThomas Huth self.vm.launch() 217380f7268SThomas Huth wait_for_console_pattern(self, 'U-Boot 2020.01+dfsg-1') 218380f7268SThomas Huth interrupt_interactive_console_until_pattern(self, 219380f7268SThomas Huth 'Hit any key to stop autoboot:', 220380f7268SThomas Huth 'switch to partitions #0, OK') 221380f7268SThomas Huth 222380f7268SThomas Huth exec_command_and_wait_for_pattern(self, '', '=>') 223380f7268SThomas Huth cmd = 'setenv bootargs root=ld0a' 224380f7268SThomas Huth exec_command_and_wait_for_pattern(self, cmd, '=>') 225380f7268SThomas Huth cmd = 'setenv kernel netbsd-GENERIC.ub' 226380f7268SThomas Huth exec_command_and_wait_for_pattern(self, cmd, '=>') 227380f7268SThomas Huth cmd = 'setenv fdtfile dtb/sun8i-h3-orangepi-pc.dtb' 228380f7268SThomas Huth exec_command_and_wait_for_pattern(self, cmd, '=>') 229380f7268SThomas Huth cmd = ("setenv bootcmd 'fatload mmc 0:1 ${kernel_addr_r} ${kernel}; " 230380f7268SThomas Huth "fatload mmc 0:1 ${fdt_addr_r} ${fdtfile}; " 231380f7268SThomas Huth "fdt addr ${fdt_addr_r}; " 232380f7268SThomas Huth "bootm ${kernel_addr_r} - ${fdt_addr_r}'") 233380f7268SThomas Huth exec_command_and_wait_for_pattern(self, cmd, '=>') 234380f7268SThomas Huth 235380f7268SThomas Huth exec_command_and_wait_for_pattern(self, 'boot', 236380f7268SThomas Huth 'Booting kernel from Legacy Image') 237380f7268SThomas Huth wait_for_console_pattern(self, 'Starting kernel ...') 238380f7268SThomas Huth wait_for_console_pattern(self, 'NetBSD 9.0 (GENERIC)') 239380f7268SThomas Huth # Wait for user-space 240380f7268SThomas Huth wait_for_console_pattern(self, 'Starting root file system check') 241380f7268SThomas Huth 242380f7268SThomas Huthif __name__ == '__main__': 243380f7268SThomas Huth LinuxKernelTest.main() 244