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 """ 53*380f7268SThomas Huth :avocado: tags=arch:arm 54*380f7268SThomas Huth :avocado: tags=machine:orangepi-pc 55*380f7268SThomas Huth :avocado: tags=accel:tcg 56*380f7268SThomas Huth """ 57*380f7268SThomas Huth self.set_machine('orangepi-pc') 58*380f7268SThomas Huth deb_path = self.ASSET_DEB.fetch() 59*380f7268SThomas Huth kernel_path = self.extract_from_deb(deb_path, 60*380f7268SThomas Huth '/boot/vmlinuz-6.6.16-current-sunxi') 61*380f7268SThomas Huth dtb_path = '/usr/lib/linux-image-6.6.16-current-sunxi/sun8i-h3-orangepi-pc.dtb' 62*380f7268SThomas Huth dtb_path = self.extract_from_deb(deb_path, dtb_path) 63*380f7268SThomas Huth 64*380f7268SThomas Huth self.vm.set_console() 65*380f7268SThomas Huth kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE + 66*380f7268SThomas Huth 'console=ttyS0,115200n8 ' 67*380f7268SThomas Huth 'earlycon=uart,mmio32,0x1c28000') 68*380f7268SThomas Huth self.vm.add_args('-kernel', kernel_path, 69*380f7268SThomas Huth '-dtb', dtb_path, 70*380f7268SThomas Huth '-append', kernel_command_line) 71*380f7268SThomas Huth self.vm.launch() 72*380f7268SThomas Huth console_pattern = 'Kernel command line: %s' % kernel_command_line 73*380f7268SThomas Huth self.wait_for_console_pattern(console_pattern) 74*380f7268SThomas Huth os.remove(kernel_path) 75*380f7268SThomas Huth os.remove(dtb_path) 76*380f7268SThomas Huth 77*380f7268SThomas Huth def test_arm_orangepi_initrd(self): 78*380f7268SThomas Huth """ 79*380f7268SThomas Huth :avocado: tags=arch:arm 80*380f7268SThomas Huth :avocado: tags=accel:tcg 81*380f7268SThomas Huth :avocado: tags=machine:orangepi-pc 82*380f7268SThomas Huth """ 83*380f7268SThomas Huth self.set_machine('orangepi-pc') 84*380f7268SThomas Huth deb_path = self.ASSET_DEB.fetch() 85*380f7268SThomas Huth kernel_path = self.extract_from_deb(deb_path, 86*380f7268SThomas Huth '/boot/vmlinuz-6.6.16-current-sunxi') 87*380f7268SThomas Huth dtb_path = '/usr/lib/linux-image-6.6.16-current-sunxi/sun8i-h3-orangepi-pc.dtb' 88*380f7268SThomas Huth dtb_path = self.extract_from_deb(deb_path, dtb_path) 89*380f7268SThomas Huth initrd_path_gz = self.ASSET_INITRD.fetch() 90*380f7268SThomas Huth initrd_path = os.path.join(self.workdir, 'rootfs.cpio') 91*380f7268SThomas Huth gzip_uncompress(initrd_path_gz, initrd_path) 92*380f7268SThomas Huth 93*380f7268SThomas Huth self.vm.set_console() 94*380f7268SThomas Huth kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE + 95*380f7268SThomas Huth 'console=ttyS0,115200 ' 96*380f7268SThomas Huth 'panic=-1 noreboot') 97*380f7268SThomas Huth self.vm.add_args('-kernel', kernel_path, 98*380f7268SThomas Huth '-dtb', dtb_path, 99*380f7268SThomas Huth '-initrd', initrd_path, 100*380f7268SThomas Huth '-append', kernel_command_line, 101*380f7268SThomas Huth '-no-reboot') 102*380f7268SThomas Huth self.vm.launch() 103*380f7268SThomas Huth self.wait_for_console_pattern('Boot successful.') 104*380f7268SThomas Huth 105*380f7268SThomas Huth exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo', 106*380f7268SThomas Huth 'Allwinner sun8i Family') 107*380f7268SThomas Huth exec_command_and_wait_for_pattern(self, 'cat /proc/iomem', 108*380f7268SThomas Huth 'system-control@1c00000') 109*380f7268SThomas Huth exec_command_and_wait_for_pattern(self, 'reboot', 110*380f7268SThomas Huth 'reboot: Restarting system') 111*380f7268SThomas Huth # Wait for VM to shut down gracefully 112*380f7268SThomas Huth self.vm.wait() 113*380f7268SThomas Huth os.remove(kernel_path) 114*380f7268SThomas Huth os.remove(dtb_path) 115*380f7268SThomas Huth os.remove(initrd_path) 116*380f7268SThomas Huth 117*380f7268SThomas Huth def test_arm_orangepi_sd(self): 118*380f7268SThomas Huth """ 119*380f7268SThomas Huth :avocado: tags=arch:arm 120*380f7268SThomas Huth :avocado: tags=accel:tcg 121*380f7268SThomas Huth :avocado: tags=machine:orangepi-pc 122*380f7268SThomas Huth :avocado: tags=device:sd 123*380f7268SThomas Huth """ 124*380f7268SThomas Huth self.set_machine('orangepi-pc') 125*380f7268SThomas Huth self.require_netdev('user') 126*380f7268SThomas Huth deb_path = self.ASSET_DEB.fetch() 127*380f7268SThomas Huth kernel_path = self.extract_from_deb(deb_path, 128*380f7268SThomas Huth '/boot/vmlinuz-6.6.16-current-sunxi') 129*380f7268SThomas Huth dtb_path = '/usr/lib/linux-image-6.6.16-current-sunxi/sun8i-h3-orangepi-pc.dtb' 130*380f7268SThomas Huth dtb_path = self.extract_from_deb(deb_path, dtb_path) 131*380f7268SThomas Huth rootfs_path_xz = self.ASSET_ROOTFS.fetch() 132*380f7268SThomas Huth rootfs_path = os.path.join(self.workdir, 'rootfs.cpio') 133*380f7268SThomas Huth lzma_uncompress(rootfs_path_xz, rootfs_path) 134*380f7268SThomas Huth image_pow2ceil_expand(rootfs_path) 135*380f7268SThomas Huth 136*380f7268SThomas Huth self.vm.set_console() 137*380f7268SThomas Huth kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE + 138*380f7268SThomas Huth 'console=ttyS0,115200 ' 139*380f7268SThomas Huth 'root=/dev/mmcblk0 rootwait rw ' 140*380f7268SThomas Huth 'panic=-1 noreboot') 141*380f7268SThomas Huth self.vm.add_args('-kernel', kernel_path, 142*380f7268SThomas Huth '-dtb', dtb_path, 143*380f7268SThomas Huth '-drive', 'file=' + rootfs_path + ',if=sd,format=raw', 144*380f7268SThomas Huth '-append', kernel_command_line, 145*380f7268SThomas Huth '-no-reboot') 146*380f7268SThomas Huth self.vm.launch() 147*380f7268SThomas Huth shell_ready = "/bin/sh: can't access tty; job control turned off" 148*380f7268SThomas Huth self.wait_for_console_pattern(shell_ready) 149*380f7268SThomas Huth 150*380f7268SThomas Huth exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo', 151*380f7268SThomas Huth 'Allwinner sun8i Family') 152*380f7268SThomas Huth exec_command_and_wait_for_pattern(self, 'cat /proc/partitions', 153*380f7268SThomas Huth 'mmcblk0') 154*380f7268SThomas Huth exec_command_and_wait_for_pattern(self, 'ifconfig eth0 up', 155*380f7268SThomas Huth 'eth0: Link is Up') 156*380f7268SThomas Huth exec_command_and_wait_for_pattern(self, 'udhcpc eth0', 157*380f7268SThomas Huth 'udhcpc: lease of 10.0.2.15 obtained') 158*380f7268SThomas Huth exec_command_and_wait_for_pattern(self, 'ping -c 3 10.0.2.2', 159*380f7268SThomas Huth '3 packets transmitted, 3 packets received, 0% packet loss') 160*380f7268SThomas Huth exec_command_and_wait_for_pattern(self, 'reboot', 161*380f7268SThomas Huth 'reboot: Restarting system') 162*380f7268SThomas Huth # Wait for VM to shut down gracefully 163*380f7268SThomas Huth self.vm.wait() 164*380f7268SThomas Huth os.remove(kernel_path) 165*380f7268SThomas Huth os.remove(dtb_path) 166*380f7268SThomas Huth os.remove(rootfs_path) 167*380f7268SThomas Huth 168*380f7268SThomas Huth @skipUnless(os.getenv('QEMU_TEST_ALLOW_LARGE_STORAGE'), 'storage limited') 169*380f7268SThomas Huth def test_arm_orangepi_armbian(self): 170*380f7268SThomas Huth """ 171*380f7268SThomas Huth :avocado: tags=arch:arm 172*380f7268SThomas Huth :avocado: tags=machine:orangepi-pc 173*380f7268SThomas Huth :avocado: tags=device:sd 174*380f7268SThomas Huth """ 175*380f7268SThomas Huth self.set_machine('orangepi-pc') 176*380f7268SThomas Huth # This test download a 275 MiB compressed image and expand it 177*380f7268SThomas Huth # to 1036 MiB, but the underlying filesystem is 1552 MiB... 178*380f7268SThomas Huth # As we expand it to 2 GiB we are safe. 179*380f7268SThomas Huth image_path_xz = self.ASSET_ARMBIAN.fetch() 180*380f7268SThomas Huth image_path = os.path.join(self.workdir, 'armbian.img') 181*380f7268SThomas Huth lzma_uncompress(image_path_xz, image_path) 182*380f7268SThomas Huth image_pow2ceil_expand(image_path) 183*380f7268SThomas Huth 184*380f7268SThomas Huth self.vm.set_console() 185*380f7268SThomas Huth self.vm.add_args('-drive', 'file=' + image_path + ',if=sd,format=raw', 186*380f7268SThomas Huth '-nic', 'user', 187*380f7268SThomas Huth '-no-reboot') 188*380f7268SThomas Huth self.vm.launch() 189*380f7268SThomas Huth 190*380f7268SThomas Huth kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE + 191*380f7268SThomas Huth 'console=ttyS0,115200 ' 192*380f7268SThomas Huth 'loglevel=7 ' 193*380f7268SThomas Huth 'nosmp ' 194*380f7268SThomas Huth 'systemd.default_timeout_start_sec=9000 ' 195*380f7268SThomas Huth 'systemd.mask=armbian-zram-config.service ' 196*380f7268SThomas Huth 'systemd.mask=armbian-ramlog.service') 197*380f7268SThomas Huth 198*380f7268SThomas Huth self.wait_for_console_pattern('U-Boot SPL') 199*380f7268SThomas Huth self.wait_for_console_pattern('Autoboot in ') 200*380f7268SThomas Huth exec_command_and_wait_for_pattern(self, ' ', '=>') 201*380f7268SThomas Huth exec_command_and_wait_for_pattern(self, "setenv extraargs '" + 202*380f7268SThomas Huth kernel_command_line + "'", '=>') 203*380f7268SThomas Huth exec_command_and_wait_for_pattern(self, 'boot', 'Starting kernel ...'); 204*380f7268SThomas Huth 205*380f7268SThomas Huth self.wait_for_console_pattern('systemd[1]: Hostname set ' + 206*380f7268SThomas Huth 'to <orangepipc>') 207*380f7268SThomas Huth self.wait_for_console_pattern('Starting Load Kernel Modules...') 208*380f7268SThomas Huth 209*380f7268SThomas Huth @skipUnless(os.getenv('QEMU_TEST_ALLOW_LARGE_STORAGE'), 'storage limited') 210*380f7268SThomas Huth def test_arm_orangepi_uboot_netbsd9(self): 211*380f7268SThomas Huth """ 212*380f7268SThomas Huth :avocado: tags=arch:arm 213*380f7268SThomas Huth :avocado: tags=machine:orangepi-pc 214*380f7268SThomas Huth :avocado: tags=device:sd 215*380f7268SThomas Huth :avocado: tags=os:netbsd 216*380f7268SThomas Huth """ 217*380f7268SThomas Huth self.set_machine('orangepi-pc') 218*380f7268SThomas Huth # This test download a 304MB compressed image and expand it to 2GB 219*380f7268SThomas Huth deb_path = self.ASSET_UBOOT.fetch() 220*380f7268SThomas Huth # We use the common OrangePi PC 'plus' build of U-Boot for our secondary 221*380f7268SThomas Huth # program loader (SPL). We will then set the path to the more specific 222*380f7268SThomas Huth # OrangePi "PC" device tree blob with 'setenv fdtfile' in U-Boot prompt, 223*380f7268SThomas Huth # before to boot NetBSD. 224*380f7268SThomas Huth uboot_path = '/usr/lib/u-boot/orangepi_plus/u-boot-sunxi-with-spl.bin' 225*380f7268SThomas Huth uboot_path = self.extract_from_deb(deb_path, uboot_path) 226*380f7268SThomas Huth image_path_gz = self.ASSET_NETBSD.fetch() 227*380f7268SThomas Huth image_path = os.path.join(self.workdir, 'armv7.img') 228*380f7268SThomas Huth gzip_uncompress(image_path_gz, image_path) 229*380f7268SThomas Huth image_pow2ceil_expand(image_path) 230*380f7268SThomas Huth image_drive_args = 'if=sd,format=raw,snapshot=on,file=' + image_path 231*380f7268SThomas Huth 232*380f7268SThomas Huth # dd if=u-boot-sunxi-with-spl.bin of=armv7.img bs=1K seek=8 conv=notrunc 233*380f7268SThomas Huth with open(uboot_path, 'rb') as f_in: 234*380f7268SThomas Huth with open(image_path, 'r+b') as f_out: 235*380f7268SThomas Huth f_out.seek(8 * 1024) 236*380f7268SThomas Huth shutil.copyfileobj(f_in, f_out) 237*380f7268SThomas Huth 238*380f7268SThomas Huth self.vm.set_console() 239*380f7268SThomas Huth self.vm.add_args('-nic', 'user', 240*380f7268SThomas Huth '-drive', image_drive_args, 241*380f7268SThomas Huth '-global', 'allwinner-rtc.base-year=2000', 242*380f7268SThomas Huth '-no-reboot') 243*380f7268SThomas Huth self.vm.launch() 244*380f7268SThomas Huth wait_for_console_pattern(self, 'U-Boot 2020.01+dfsg-1') 245*380f7268SThomas Huth interrupt_interactive_console_until_pattern(self, 246*380f7268SThomas Huth 'Hit any key to stop autoboot:', 247*380f7268SThomas Huth 'switch to partitions #0, OK') 248*380f7268SThomas Huth 249*380f7268SThomas Huth exec_command_and_wait_for_pattern(self, '', '=>') 250*380f7268SThomas Huth cmd = 'setenv bootargs root=ld0a' 251*380f7268SThomas Huth exec_command_and_wait_for_pattern(self, cmd, '=>') 252*380f7268SThomas Huth cmd = 'setenv kernel netbsd-GENERIC.ub' 253*380f7268SThomas Huth exec_command_and_wait_for_pattern(self, cmd, '=>') 254*380f7268SThomas Huth cmd = 'setenv fdtfile dtb/sun8i-h3-orangepi-pc.dtb' 255*380f7268SThomas Huth exec_command_and_wait_for_pattern(self, cmd, '=>') 256*380f7268SThomas Huth cmd = ("setenv bootcmd 'fatload mmc 0:1 ${kernel_addr_r} ${kernel}; " 257*380f7268SThomas Huth "fatload mmc 0:1 ${fdt_addr_r} ${fdtfile}; " 258*380f7268SThomas Huth "fdt addr ${fdt_addr_r}; " 259*380f7268SThomas Huth "bootm ${kernel_addr_r} - ${fdt_addr_r}'") 260*380f7268SThomas Huth exec_command_and_wait_for_pattern(self, cmd, '=>') 261*380f7268SThomas Huth 262*380f7268SThomas Huth exec_command_and_wait_for_pattern(self, 'boot', 263*380f7268SThomas Huth 'Booting kernel from Legacy Image') 264*380f7268SThomas Huth wait_for_console_pattern(self, 'Starting kernel ...') 265*380f7268SThomas Huth wait_for_console_pattern(self, 'NetBSD 9.0 (GENERIC)') 266*380f7268SThomas Huth # Wait for user-space 267*380f7268SThomas Huth wait_for_console_pattern(self, 'Starting root file system check') 268*380f7268SThomas Huth 269*380f7268SThomas Huthif __name__ == '__main__': 270*380f7268SThomas Huth LinuxKernelTest.main() 271