1f7d6b772SThomas Huth#!/usr/bin/env python3 2f7d6b772SThomas Huth# 3f7d6b772SThomas Huth# Functional test that boots a Linux kernel on a Banana Pi machine 4f7d6b772SThomas Huth# and checks the console 5f7d6b772SThomas Huth# 6f7d6b772SThomas Huth# SPDX-License-Identifier: GPL-2.0-or-later 7f7d6b772SThomas Huth 8f7d6b772SThomas Huthimport os 9f7d6b772SThomas Huth 10f7d6b772SThomas Huthfrom qemu_test import LinuxKernelTest, exec_command_and_wait_for_pattern 11f7d6b772SThomas Huthfrom qemu_test import Asset, interrupt_interactive_console_until_pattern 12*8a6253a4SDaniel P. Berrangéfrom qemu_test.utils import gzip_uncompress, lzma_uncompress 13f7d6b772SThomas Huthfrom qemu_test.utils import image_pow2ceil_expand 14f7d6b772SThomas Huthfrom unittest import skipUnless 15f7d6b772SThomas Huth 16f7d6b772SThomas Huthclass BananaPiMachine(LinuxKernelTest): 17f7d6b772SThomas Huth 18f7d6b772SThomas Huth ASSET_DEB = Asset( 19f7d6b772SThomas Huth ('https://apt.armbian.com/pool/main/l/linux-6.6.16/' 20f7d6b772SThomas Huth 'linux-image-current-sunxi_24.2.1_armhf__6.6.16-Seb3e-D6b4a-P2359-Ce96bHfe66-HK01ba-V014b-B067e-R448a.deb'), 21f7d6b772SThomas Huth '3d968c15b121ede871dce49d13ee7644d6f74b6b121b84c9a40f51b0c80d6d22') 22f7d6b772SThomas Huth 23f7d6b772SThomas Huth ASSET_INITRD = Asset( 24f7d6b772SThomas Huth ('https://github.com/groeck/linux-build-test/raw/' 25f7d6b772SThomas Huth '2eb0a73b5d5a28df3170c546ddaaa9757e1e0848/rootfs/' 26f7d6b772SThomas Huth 'arm/rootfs-armv7a.cpio.gz'), 27f7d6b772SThomas Huth '2c8dbdb16ea7af2dfbcbea96044dde639fb07d09fd3c4fb31f2027ef71e55ddd') 28f7d6b772SThomas Huth 29f7d6b772SThomas Huth ASSET_ROOTFS = Asset( 30f7d6b772SThomas Huth ('http://storage.kernelci.org/images/rootfs/buildroot/' 31f7d6b772SThomas Huth 'buildroot-baseline/20230703.0/armel/rootfs.ext2.xz'), 32f7d6b772SThomas Huth '42b44a12965ac0afe9a88378527fb698a7dc76af50495efc2361ee1595b4e5c6') 33f7d6b772SThomas Huth 34f7d6b772SThomas Huth ASSET_SD_IMAGE = Asset( 35f7d6b772SThomas Huth ('https://downloads.openwrt.org/releases/22.03.3/targets/sunxi/cortexa7/' 36f7d6b772SThomas Huth 'openwrt-22.03.3-sunxi-cortexa7-sinovoip_bananapi-m2-ultra-ext4-sdcard.img.gz'), 37f7d6b772SThomas Huth '5b41b4e11423e562c6011640f9a7cd3bdd0a3d42b83430f7caa70a432e6cd82c') 38f7d6b772SThomas Huth 39f7d6b772SThomas Huth def test_arm_bpim2u(self): 40f7d6b772SThomas Huth self.set_machine('bpim2u') 41f7d6b772SThomas Huth deb_path = self.ASSET_DEB.fetch() 42f7d6b772SThomas Huth kernel_path = self.extract_from_deb(deb_path, 43f7d6b772SThomas Huth '/boot/vmlinuz-6.6.16-current-sunxi') 44f7d6b772SThomas Huth dtb_path = ('/usr/lib/linux-image-6.6.16-current-sunxi/' 45f7d6b772SThomas Huth 'sun8i-r40-bananapi-m2-ultra.dtb') 46f7d6b772SThomas Huth dtb_path = self.extract_from_deb(deb_path, dtb_path) 47f7d6b772SThomas Huth 48f7d6b772SThomas Huth self.vm.set_console() 49f7d6b772SThomas Huth kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE + 50f7d6b772SThomas Huth 'console=ttyS0,115200n8 ' 51f7d6b772SThomas Huth 'earlycon=uart,mmio32,0x1c28000') 52f7d6b772SThomas Huth self.vm.add_args('-kernel', kernel_path, 53f7d6b772SThomas Huth '-dtb', dtb_path, 54f7d6b772SThomas Huth '-append', kernel_command_line) 55f7d6b772SThomas Huth self.vm.launch() 56f7d6b772SThomas Huth console_pattern = 'Kernel command line: %s' % kernel_command_line 57f7d6b772SThomas Huth self.wait_for_console_pattern(console_pattern) 58f7d6b772SThomas Huth os.remove(kernel_path) 59f7d6b772SThomas Huth os.remove(dtb_path) 60f7d6b772SThomas Huth 61f7d6b772SThomas Huth def test_arm_bpim2u_initrd(self): 62f7d6b772SThomas Huth self.set_machine('bpim2u') 63f7d6b772SThomas Huth deb_path = self.ASSET_DEB.fetch() 64f7d6b772SThomas Huth kernel_path = self.extract_from_deb(deb_path, 65f7d6b772SThomas Huth '/boot/vmlinuz-6.6.16-current-sunxi') 66f7d6b772SThomas Huth dtb_path = ('/usr/lib/linux-image-6.6.16-current-sunxi/' 67f7d6b772SThomas Huth 'sun8i-r40-bananapi-m2-ultra.dtb') 68f7d6b772SThomas Huth dtb_path = self.extract_from_deb(deb_path, dtb_path) 69f7d6b772SThomas Huth initrd_path_gz = self.ASSET_INITRD.fetch() 70f7d6b772SThomas Huth initrd_path = os.path.join(self.workdir, 'rootfs.cpio') 71f7d6b772SThomas Huth gzip_uncompress(initrd_path_gz, initrd_path) 72f7d6b772SThomas Huth 73f7d6b772SThomas Huth self.vm.set_console() 74f7d6b772SThomas Huth kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE + 75f7d6b772SThomas Huth 'console=ttyS0,115200 ' 76f7d6b772SThomas Huth 'panic=-1 noreboot') 77f7d6b772SThomas Huth self.vm.add_args('-kernel', kernel_path, 78f7d6b772SThomas Huth '-dtb', dtb_path, 79f7d6b772SThomas Huth '-initrd', initrd_path, 80f7d6b772SThomas Huth '-append', kernel_command_line, 81f7d6b772SThomas Huth '-no-reboot') 82f7d6b772SThomas Huth self.vm.launch() 83f7d6b772SThomas Huth self.wait_for_console_pattern('Boot successful.') 84f7d6b772SThomas Huth 85f7d6b772SThomas Huth exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo', 86f7d6b772SThomas Huth 'Allwinner sun8i Family') 87f7d6b772SThomas Huth exec_command_and_wait_for_pattern(self, 'cat /proc/iomem', 88f7d6b772SThomas Huth 'system-control@1c00000') 89f7d6b772SThomas Huth exec_command_and_wait_for_pattern(self, 'reboot', 90f7d6b772SThomas Huth 'reboot: Restarting system') 91f7d6b772SThomas Huth # Wait for VM to shut down gracefully 92f7d6b772SThomas Huth self.vm.wait() 93f7d6b772SThomas Huth os.remove(kernel_path) 94f7d6b772SThomas Huth os.remove(dtb_path) 95f7d6b772SThomas Huth os.remove(initrd_path) 96f7d6b772SThomas Huth 97f7d6b772SThomas Huth def test_arm_bpim2u_gmac(self): 98f7d6b772SThomas Huth self.set_machine('bpim2u') 99f7d6b772SThomas Huth self.require_netdev('user') 100f7d6b772SThomas Huth 101f7d6b772SThomas Huth deb_path = self.ASSET_DEB.fetch() 102f7d6b772SThomas Huth kernel_path = self.extract_from_deb(deb_path, 103f7d6b772SThomas Huth '/boot/vmlinuz-6.6.16-current-sunxi') 104f7d6b772SThomas Huth dtb_path = ('/usr/lib/linux-image-6.6.16-current-sunxi/' 105f7d6b772SThomas Huth 'sun8i-r40-bananapi-m2-ultra.dtb') 106f7d6b772SThomas Huth dtb_path = self.extract_from_deb(deb_path, dtb_path) 107f7d6b772SThomas Huth rootfs_path_xz = self.ASSET_ROOTFS.fetch() 108f7d6b772SThomas Huth rootfs_path = os.path.join(self.workdir, 'rootfs.cpio') 109f7d6b772SThomas Huth lzma_uncompress(rootfs_path_xz, rootfs_path) 110f7d6b772SThomas Huth image_pow2ceil_expand(rootfs_path) 111f7d6b772SThomas Huth 112f7d6b772SThomas Huth self.vm.set_console() 113f7d6b772SThomas Huth kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE + 114f7d6b772SThomas Huth 'console=ttyS0,115200 ' 115f7d6b772SThomas Huth 'root=b300 rootwait rw ' 116f7d6b772SThomas Huth 'panic=-1 noreboot') 117f7d6b772SThomas Huth self.vm.add_args('-kernel', kernel_path, 118f7d6b772SThomas Huth '-dtb', dtb_path, 119f7d6b772SThomas Huth '-drive', 'file=' + rootfs_path + ',if=sd,format=raw', 120f7d6b772SThomas Huth '-net', 'nic,model=gmac,netdev=host_gmac', 121f7d6b772SThomas Huth '-netdev', 'user,id=host_gmac', 122f7d6b772SThomas Huth '-append', kernel_command_line, 123f7d6b772SThomas Huth '-no-reboot') 124f7d6b772SThomas Huth self.vm.launch() 125f7d6b772SThomas Huth shell_ready = "/bin/sh: can't access tty; job control turned off" 126f7d6b772SThomas Huth self.wait_for_console_pattern(shell_ready) 127f7d6b772SThomas Huth 128f7d6b772SThomas Huth exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo', 129f7d6b772SThomas Huth 'Allwinner sun8i Family') 130f7d6b772SThomas Huth exec_command_and_wait_for_pattern(self, 'cat /proc/partitions', 131f7d6b772SThomas Huth 'mmcblk') 132f7d6b772SThomas Huth exec_command_and_wait_for_pattern(self, 'ifconfig eth0 up', 133f7d6b772SThomas Huth 'eth0: Link is Up') 134f7d6b772SThomas Huth exec_command_and_wait_for_pattern(self, 'udhcpc eth0', 135f7d6b772SThomas Huth 'udhcpc: lease of 10.0.2.15 obtained') 136f7d6b772SThomas Huth exec_command_and_wait_for_pattern(self, 'ping -c 3 10.0.2.2', 137f7d6b772SThomas Huth '3 packets transmitted, 3 packets received, 0% packet loss') 138f7d6b772SThomas Huth exec_command_and_wait_for_pattern(self, 'reboot', 139f7d6b772SThomas Huth 'reboot: Restarting system') 140f7d6b772SThomas Huth # Wait for VM to shut down gracefully 141f7d6b772SThomas Huth self.vm.wait() 142f7d6b772SThomas Huth os.remove(kernel_path) 143f7d6b772SThomas Huth os.remove(dtb_path) 144f7d6b772SThomas Huth os.remove(rootfs_path) 145f7d6b772SThomas Huth 146f7d6b772SThomas Huth @skipUnless(os.getenv('QEMU_TEST_ALLOW_LARGE_STORAGE'), 'storage limited') 147f7d6b772SThomas Huth def test_arm_bpim2u_openwrt_22_03_3(self): 148f7d6b772SThomas Huth self.set_machine('bpim2u') 149f7d6b772SThomas Huth # This test download a 8.9 MiB compressed image and expand it 150f7d6b772SThomas Huth # to 127 MiB. 151f7d6b772SThomas Huth image_path_gz = self.ASSET_SD_IMAGE.fetch() 152f7d6b772SThomas Huth image_path = os.path.join(self.workdir, 'sdcard.img') 153f7d6b772SThomas Huth gzip_uncompress(image_path_gz, image_path) 154f7d6b772SThomas Huth image_pow2ceil_expand(image_path) 155f7d6b772SThomas Huth 156f7d6b772SThomas Huth self.vm.set_console() 157f7d6b772SThomas Huth self.vm.add_args('-drive', 'file=' + image_path + ',if=sd,format=raw', 158f7d6b772SThomas Huth '-nic', 'user', 159f7d6b772SThomas Huth '-no-reboot') 160f7d6b772SThomas Huth self.vm.launch() 161f7d6b772SThomas Huth 162f7d6b772SThomas Huth kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE + 163f7d6b772SThomas Huth 'usbcore.nousb ' 164f7d6b772SThomas Huth 'noreboot') 165f7d6b772SThomas Huth 166f7d6b772SThomas Huth self.wait_for_console_pattern('U-Boot SPL') 167f7d6b772SThomas Huth 168f7d6b772SThomas Huth interrupt_interactive_console_until_pattern( 169f7d6b772SThomas Huth self, 'Hit any key to stop autoboot:', '=>') 170f7d6b772SThomas Huth exec_command_and_wait_for_pattern(self, "setenv extraargs '" + 171f7d6b772SThomas Huth kernel_command_line + "'", '=>') 172f7d6b772SThomas Huth exec_command_and_wait_for_pattern(self, 'boot', 'Starting kernel ...'); 173f7d6b772SThomas Huth 174f7d6b772SThomas Huth self.wait_for_console_pattern( 175f7d6b772SThomas Huth 'Please press Enter to activate this console.') 176f7d6b772SThomas Huth 177f7d6b772SThomas Huth exec_command_and_wait_for_pattern(self, ' ', 'root@') 178f7d6b772SThomas Huth 179f7d6b772SThomas Huth exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo', 180f7d6b772SThomas Huth 'Allwinner sun8i Family') 181f7d6b772SThomas Huth exec_command_and_wait_for_pattern(self, 'cat /proc/iomem', 182f7d6b772SThomas Huth 'system-control@1c00000') 183f7d6b772SThomas Huth os.remove(image_path) 184f7d6b772SThomas Huth 185f7d6b772SThomas Huthif __name__ == '__main__': 186f7d6b772SThomas Huth LinuxKernelTest.main() 187