1# Functional test that boots a Linux kernel and checks the console 2# 3# Copyright (c) 2018 Red Hat, Inc. 4# 5# Author: 6# Cleber Rosa <crosa@redhat.com> 7# 8# This work is licensed under the terms of the GNU GPL, version 2 or 9# later. See the COPYING file in the top-level directory. 10 11import os 12import lzma 13import gzip 14import shutil 15 16from avocado import skip 17from avocado import skipUnless 18from avocado import skipUnless 19from avocado_qemu import QemuSystemTest 20from avocado_qemu import exec_command 21from avocado_qemu import exec_command_and_wait_for_pattern 22from avocado_qemu import interrupt_interactive_console_until_pattern 23from avocado_qemu import wait_for_console_pattern 24from avocado.utils import process 25from avocado.utils import archive 26 27""" 28Round up to next power of 2 29""" 30def pow2ceil(x): 31 return 1 if x == 0 else 2**(x - 1).bit_length() 32 33def file_truncate(path, size): 34 if size != os.path.getsize(path): 35 with open(path, 'ab+') as fd: 36 fd.truncate(size) 37 38""" 39Expand file size to next power of 2 40""" 41def image_pow2ceil_expand(path): 42 size = os.path.getsize(path) 43 size_aligned = pow2ceil(size) 44 if size != size_aligned: 45 with open(path, 'ab+') as fd: 46 fd.truncate(size_aligned) 47 48class LinuxKernelTest(QemuSystemTest): 49 KERNEL_COMMON_COMMAND_LINE = 'printk.time=0 ' 50 51 def wait_for_console_pattern(self, success_message, vm=None): 52 wait_for_console_pattern(self, success_message, 53 failure_message='Kernel panic - not syncing', 54 vm=vm) 55 56 def extract_from_deb(self, deb, path): 57 """ 58 Extracts a file from a deb package into the test workdir 59 60 :param deb: path to the deb archive 61 :param path: path within the deb archive of the file to be extracted 62 :returns: path of the extracted file 63 """ 64 cwd = os.getcwd() 65 os.chdir(self.workdir) 66 file_path = process.run("ar t %s" % deb).stdout_text.split()[2] 67 process.run("ar x %s %s" % (deb, file_path)) 68 archive.extract(file_path, self.workdir) 69 os.chdir(cwd) 70 # Return complete path to extracted file. Because callers to 71 # extract_from_deb() specify 'path' with a leading slash, it is 72 # necessary to use os.path.relpath() as otherwise os.path.join() 73 # interprets it as an absolute path and drops the self.workdir part. 74 return os.path.normpath(os.path.join(self.workdir, 75 os.path.relpath(path, '/'))) 76 77 def extract_from_rpm(self, rpm, path): 78 """ 79 Extracts a file from an RPM package into the test workdir. 80 81 :param rpm: path to the rpm archive 82 :param path: path within the rpm archive of the file to be extracted 83 needs to be a relative path (starting with './') because 84 cpio(1), which is used to extract the file, expects that. 85 :returns: path of the extracted file 86 """ 87 cwd = os.getcwd() 88 os.chdir(self.workdir) 89 process.run("rpm2cpio %s | cpio -id %s" % (rpm, path), shell=True) 90 os.chdir(cwd) 91 return os.path.normpath(os.path.join(self.workdir, path)) 92 93class BootLinuxConsole(LinuxKernelTest): 94 """ 95 Boots a Linux kernel and checks that the console is operational and the 96 kernel command line is properly passed from QEMU to the kernel 97 """ 98 timeout = 90 99 100 def test_x86_64_pc(self): 101 """ 102 :avocado: tags=arch:x86_64 103 :avocado: tags=machine:pc 104 """ 105 kernel_url = ('https://archives.fedoraproject.org/pub/archive/fedora' 106 '/linux/releases/29/Everything/x86_64/os/images/pxeboot' 107 '/vmlinuz') 108 kernel_hash = '23bebd2680757891cf7adedb033532163a792495' 109 kernel_path = self.fetch_asset(kernel_url, asset_hash=kernel_hash) 110 111 self.vm.set_console() 112 kernel_command_line = self.KERNEL_COMMON_COMMAND_LINE + 'console=ttyS0' 113 self.vm.add_args('-kernel', kernel_path, 114 '-append', kernel_command_line) 115 self.vm.launch() 116 console_pattern = 'Kernel command line: %s' % kernel_command_line 117 self.wait_for_console_pattern(console_pattern) 118 119 def test_aarch64_xlnx_versal_virt(self): 120 """ 121 :avocado: tags=arch:aarch64 122 :avocado: tags=machine:xlnx-versal-virt 123 :avocado: tags=device:pl011 124 :avocado: tags=device:arm_gicv3 125 :avocado: tags=accel:tcg 126 """ 127 images_url = ('http://ports.ubuntu.com/ubuntu-ports/dists/' 128 'bionic-updates/main/installer-arm64/' 129 '20101020ubuntu543.19/images/') 130 kernel_url = images_url + 'netboot/ubuntu-installer/arm64/linux' 131 kernel_hash = 'e167757620640eb26de0972f578741924abb3a82' 132 kernel_path = self.fetch_asset(kernel_url, asset_hash=kernel_hash) 133 134 initrd_url = images_url + 'netboot/ubuntu-installer/arm64/initrd.gz' 135 initrd_hash = 'cab5cb3fcefca8408aa5aae57f24574bfce8bdb9' 136 initrd_path = self.fetch_asset(initrd_url, asset_hash=initrd_hash) 137 138 self.vm.set_console() 139 self.vm.add_args('-m', '2G', 140 '-accel', 'tcg', 141 '-kernel', kernel_path, 142 '-initrd', initrd_path) 143 self.vm.launch() 144 self.wait_for_console_pattern('Checked W+X mappings: passed') 145 146 def test_arm_virt(self): 147 """ 148 :avocado: tags=arch:arm 149 :avocado: tags=machine:virt 150 :avocado: tags=accel:tcg 151 """ 152 kernel_url = ('https://archives.fedoraproject.org/pub/archive/fedora' 153 '/linux/releases/29/Everything/armhfp/os/images/pxeboot' 154 '/vmlinuz') 155 kernel_hash = 'e9826d741b4fb04cadba8d4824d1ed3b7fb8b4d4' 156 kernel_path = self.fetch_asset(kernel_url, asset_hash=kernel_hash) 157 158 self.vm.set_console() 159 kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE + 160 'console=ttyAMA0') 161 self.vm.add_args('-kernel', kernel_path, 162 '-append', kernel_command_line) 163 self.vm.launch() 164 console_pattern = 'Kernel command line: %s' % kernel_command_line 165 self.wait_for_console_pattern(console_pattern) 166 167 def test_arm_emcraft_sf2(self): 168 """ 169 :avocado: tags=arch:arm 170 :avocado: tags=machine:emcraft-sf2 171 :avocado: tags=endian:little 172 :avocado: tags=u-boot 173 :avocado: tags=accel:tcg 174 """ 175 self.require_netdev('user') 176 177 uboot_url = ('https://raw.githubusercontent.com/' 178 'Subbaraya-Sundeep/qemu-test-binaries/' 179 'fe371d32e50ca682391e1e70ab98c2942aeffb01/u-boot') 180 uboot_hash = 'cbb8cbab970f594bf6523b9855be209c08374ae2' 181 uboot_path = self.fetch_asset(uboot_url, asset_hash=uboot_hash) 182 spi_url = ('https://raw.githubusercontent.com/' 183 'Subbaraya-Sundeep/qemu-test-binaries/' 184 'fe371d32e50ca682391e1e70ab98c2942aeffb01/spi.bin') 185 spi_hash = '65523a1835949b6f4553be96dec1b6a38fb05501' 186 spi_path = self.fetch_asset(spi_url, asset_hash=spi_hash) 187 spi_path_rw = os.path.join(self.workdir, os.path.basename(spi_path)) 188 shutil.copy(spi_path, spi_path_rw) 189 190 file_truncate(spi_path_rw, 16 << 20) # Spansion S25FL128SDPBHICO is 16 MiB 191 192 self.vm.set_console() 193 kernel_command_line = self.KERNEL_COMMON_COMMAND_LINE 194 self.vm.add_args('-kernel', uboot_path, 195 '-append', kernel_command_line, 196 '-drive', 'file=' + spi_path_rw + ',if=mtd,format=raw', 197 '-no-reboot') 198 self.vm.launch() 199 self.wait_for_console_pattern('Enter \'help\' for a list') 200 201 exec_command_and_wait_for_pattern(self, 'ifconfig eth0 10.0.2.15', 202 'eth0: link becomes ready') 203 exec_command_and_wait_for_pattern(self, 'ping -c 3 10.0.2.2', 204 '3 packets transmitted, 3 packets received, 0% packet loss') 205 206 def test_arm_exynos4210_initrd(self): 207 """ 208 :avocado: tags=arch:arm 209 :avocado: tags=machine:smdkc210 210 :avocado: tags=accel:tcg 211 """ 212 deb_url = ('https://snapshot.debian.org/archive/debian/' 213 '20190928T224601Z/pool/main/l/linux/' 214 'linux-image-4.19.0-6-armmp_4.19.67-2+deb10u1_armhf.deb') 215 deb_hash = 'fa9df4a0d38936cb50084838f2cb933f570d7d82' 216 deb_path = self.fetch_asset(deb_url, asset_hash=deb_hash) 217 kernel_path = self.extract_from_deb(deb_path, 218 '/boot/vmlinuz-4.19.0-6-armmp') 219 dtb_path = '/usr/lib/linux-image-4.19.0-6-armmp/exynos4210-smdkv310.dtb' 220 dtb_path = self.extract_from_deb(deb_path, dtb_path) 221 222 initrd_url = ('https://github.com/groeck/linux-build-test/raw/' 223 '2eb0a73b5d5a28df3170c546ddaaa9757e1e0848/rootfs/' 224 'arm/rootfs-armv5.cpio.gz') 225 initrd_hash = '2b50f1873e113523967806f4da2afe385462ff9b' 226 initrd_path_gz = self.fetch_asset(initrd_url, asset_hash=initrd_hash) 227 initrd_path = os.path.join(self.workdir, 'rootfs.cpio') 228 archive.gzip_uncompress(initrd_path_gz, initrd_path) 229 230 self.vm.set_console() 231 kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE + 232 'earlycon=exynos4210,0x13800000 earlyprintk ' + 233 'console=ttySAC0,115200n8 ' + 234 'random.trust_cpu=off cryptomgr.notests ' + 235 'cpuidle.off=1 panic=-1 noreboot') 236 237 self.vm.add_args('-kernel', kernel_path, 238 '-dtb', dtb_path, 239 '-initrd', initrd_path, 240 '-append', kernel_command_line, 241 '-no-reboot') 242 self.vm.launch() 243 244 self.wait_for_console_pattern('Boot successful.') 245 # TODO user command, for now the uart is stuck 246 247 def test_arm_cubieboard_initrd(self): 248 """ 249 :avocado: tags=arch:arm 250 :avocado: tags=machine:cubieboard 251 :avocado: tags=accel:tcg 252 """ 253 deb_url = ('https://apt.armbian.com/pool/main/l/' 254 'linux-6.6.16/linux-image-current-sunxi_24.2.1_armhf__6.6.16-Seb3e-D6b4a-P2359-Ce96bHfe66-HK01ba-V014b-B067e-R448a.deb') 255 deb_hash = 'f7c3c8c5432f765445dc6e7eab02f3bbe668256b' 256 deb_path = self.fetch_asset(deb_url, asset_hash=deb_hash) 257 kernel_path = self.extract_from_deb(deb_path, 258 '/boot/vmlinuz-6.6.16-current-sunxi') 259 dtb_path = '/usr/lib/linux-image-6.6.16-current-sunxi/sun4i-a10-cubieboard.dtb' 260 dtb_path = self.extract_from_deb(deb_path, dtb_path) 261 initrd_url = ('https://github.com/groeck/linux-build-test/raw/' 262 '2eb0a73b5d5a28df3170c546ddaaa9757e1e0848/rootfs/' 263 'arm/rootfs-armv5.cpio.gz') 264 initrd_hash = '2b50f1873e113523967806f4da2afe385462ff9b' 265 initrd_path_gz = self.fetch_asset(initrd_url, asset_hash=initrd_hash) 266 initrd_path = os.path.join(self.workdir, 'rootfs.cpio') 267 archive.gzip_uncompress(initrd_path_gz, initrd_path) 268 269 self.vm.set_console() 270 kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE + 271 'console=ttyS0,115200 ' 272 'usbcore.nousb ' 273 'panic=-1 noreboot') 274 self.vm.add_args('-kernel', kernel_path, 275 '-dtb', dtb_path, 276 '-initrd', initrd_path, 277 '-append', kernel_command_line, 278 '-no-reboot') 279 self.vm.launch() 280 self.wait_for_console_pattern('Boot successful.') 281 282 exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo', 283 'Allwinner sun4i/sun5i') 284 exec_command_and_wait_for_pattern(self, 'cat /proc/iomem', 285 'system-control@1c00000') 286 exec_command_and_wait_for_pattern(self, 'reboot', 287 'reboot: Restarting system') 288 # Wait for VM to shut down gracefully 289 self.vm.wait() 290 291 def test_arm_cubieboard_sata(self): 292 """ 293 :avocado: tags=arch:arm 294 :avocado: tags=machine:cubieboard 295 :avocado: tags=accel:tcg 296 """ 297 deb_url = ('https://apt.armbian.com/pool/main/l/' 298 'linux-6.6.16/linux-image-current-sunxi_24.2.1_armhf__6.6.16-Seb3e-D6b4a-P2359-Ce96bHfe66-HK01ba-V014b-B067e-R448a.deb') 299 deb_hash = 'f7c3c8c5432f765445dc6e7eab02f3bbe668256b' 300 deb_path = self.fetch_asset(deb_url, asset_hash=deb_hash) 301 kernel_path = self.extract_from_deb(deb_path, 302 '/boot/vmlinuz-6.6.16-current-sunxi') 303 dtb_path = '/usr/lib/linux-image-6.6.16-current-sunxi/sun4i-a10-cubieboard.dtb' 304 dtb_path = self.extract_from_deb(deb_path, dtb_path) 305 rootfs_url = ('https://github.com/groeck/linux-build-test/raw/' 306 '2eb0a73b5d5a28df3170c546ddaaa9757e1e0848/rootfs/' 307 'arm/rootfs-armv5.ext2.gz') 308 rootfs_hash = '093e89d2b4d982234bf528bc9fb2f2f17a9d1f93' 309 rootfs_path_gz = self.fetch_asset(rootfs_url, asset_hash=rootfs_hash) 310 rootfs_path = os.path.join(self.workdir, 'rootfs.cpio') 311 archive.gzip_uncompress(rootfs_path_gz, rootfs_path) 312 313 self.vm.set_console() 314 kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE + 315 'console=ttyS0,115200 ' 316 'usbcore.nousb ' 317 'root=/dev/sda ro ' 318 'panic=-1 noreboot') 319 self.vm.add_args('-kernel', kernel_path, 320 '-dtb', dtb_path, 321 '-drive', 'if=none,format=raw,id=disk0,file=' 322 + rootfs_path, 323 '-device', 'ide-hd,bus=ide.0,drive=disk0', 324 '-append', kernel_command_line, 325 '-no-reboot') 326 self.vm.launch() 327 self.wait_for_console_pattern('Boot successful.') 328 329 exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo', 330 'Allwinner sun4i/sun5i') 331 exec_command_and_wait_for_pattern(self, 'cat /proc/partitions', 332 'sda') 333 exec_command_and_wait_for_pattern(self, 'reboot', 334 'reboot: Restarting system') 335 # Wait for VM to shut down gracefully 336 self.vm.wait() 337 338 @skipUnless(os.getenv('AVOCADO_ALLOW_LARGE_STORAGE'), 'storage limited') 339 def test_arm_cubieboard_openwrt_22_03_2(self): 340 """ 341 :avocado: tags=arch:arm 342 :avocado: tags=machine:cubieboard 343 :avocado: tags=device:sd 344 """ 345 346 # This test download a 7.5 MiB compressed image and expand it 347 # to 126 MiB. 348 image_url = ('https://downloads.openwrt.org/releases/22.03.2/targets/' 349 'sunxi/cortexa8/openwrt-22.03.2-sunxi-cortexa8-' 350 'cubietech_a10-cubieboard-ext4-sdcard.img.gz') 351 image_hash = ('94b5ecbfbc0b3b56276e5146b899eafa' 352 '2ac5dc2d08733d6705af9f144f39f554') 353 image_path_gz = self.fetch_asset(image_url, asset_hash=image_hash, 354 algorithm='sha256') 355 image_path = archive.extract(image_path_gz, self.workdir) 356 image_pow2ceil_expand(image_path) 357 358 self.vm.set_console() 359 self.vm.add_args('-drive', 'file=' + image_path + ',if=sd,format=raw', 360 '-nic', 'user', 361 '-no-reboot') 362 self.vm.launch() 363 364 kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE + 365 'usbcore.nousb ' 366 'noreboot') 367 368 self.wait_for_console_pattern('U-Boot SPL') 369 370 interrupt_interactive_console_until_pattern( 371 self, 'Hit any key to stop autoboot:', '=>') 372 exec_command_and_wait_for_pattern(self, "setenv extraargs '" + 373 kernel_command_line + "'", '=>') 374 exec_command_and_wait_for_pattern(self, 'boot', 'Starting kernel ...'); 375 376 self.wait_for_console_pattern( 377 'Please press Enter to activate this console.') 378 379 exec_command_and_wait_for_pattern(self, ' ', 'root@') 380 381 exec_command_and_wait_for_pattern(self, 'cat /proc/cpuinfo', 382 'Allwinner sun4i/sun5i') 383 exec_command_and_wait_for_pattern(self, 'reboot', 384 'reboot: Restarting system') 385 # Wait for VM to shut down gracefully 386 self.vm.wait() 387 388 @skipUnless(os.getenv('AVOCADO_TIMEOUT_EXPECTED'), 'Test might timeout') 389 def test_arm_quanta_gsj(self): 390 """ 391 :avocado: tags=arch:arm 392 :avocado: tags=machine:quanta-gsj 393 :avocado: tags=accel:tcg 394 """ 395 # 25 MiB compressed, 32 MiB uncompressed. 396 image_url = ( 397 'https://github.com/hskinnemoen/openbmc/releases/download/' 398 '20200711-gsj-qemu-0/obmc-phosphor-image-gsj.static.mtd.gz') 399 image_hash = '14895e634923345cb5c8776037ff7876df96f6b1' 400 image_path_gz = self.fetch_asset(image_url, asset_hash=image_hash) 401 image_name = 'obmc.mtd' 402 image_path = os.path.join(self.workdir, image_name) 403 archive.gzip_uncompress(image_path_gz, image_path) 404 405 self.vm.set_console() 406 drive_args = 'file=' + image_path + ',if=mtd,bus=0,unit=0' 407 self.vm.add_args('-drive', drive_args) 408 self.vm.launch() 409 410 # Disable drivers and services that stall for a long time during boot, 411 # to avoid running past the 90-second timeout. These may be removed 412 # as the corresponding device support is added. 413 kernel_command_line = self.KERNEL_COMMON_COMMAND_LINE + ( 414 'console=${console} ' 415 'mem=${mem} ' 416 'initcall_blacklist=npcm_i2c_bus_driver_init ' 417 'systemd.mask=systemd-random-seed.service ' 418 'systemd.mask=dropbearkey.service ' 419 ) 420 421 self.wait_for_console_pattern('> BootBlock by Nuvoton') 422 self.wait_for_console_pattern('>Device: Poleg BMC NPCM730') 423 self.wait_for_console_pattern('>Skip DDR init.') 424 self.wait_for_console_pattern('U-Boot ') 425 interrupt_interactive_console_until_pattern( 426 self, 'Hit any key to stop autoboot:', 'U-Boot>') 427 exec_command_and_wait_for_pattern( 428 self, "setenv bootargs ${bootargs} " + kernel_command_line, 429 'U-Boot>') 430 exec_command_and_wait_for_pattern( 431 self, 'run romboot', 'Booting Kernel from flash') 432 self.wait_for_console_pattern('Booting Linux on physical CPU 0x0') 433 self.wait_for_console_pattern('CPU1: thread -1, cpu 1, socket 0') 434 self.wait_for_console_pattern('OpenBMC Project Reference Distro') 435 self.wait_for_console_pattern('gsj login:') 436 437 def test_arm_quanta_gsj_initrd(self): 438 """ 439 :avocado: tags=arch:arm 440 :avocado: tags=machine:quanta-gsj 441 :avocado: tags=accel:tcg 442 """ 443 initrd_url = ( 444 'https://github.com/hskinnemoen/openbmc/releases/download/' 445 '20200711-gsj-qemu-0/obmc-phosphor-initramfs-gsj.cpio.xz') 446 initrd_hash = '98fefe5d7e56727b1eb17d5c00311b1b5c945300' 447 initrd_path = self.fetch_asset(initrd_url, asset_hash=initrd_hash) 448 kernel_url = ( 449 'https://github.com/hskinnemoen/openbmc/releases/download/' 450 '20200711-gsj-qemu-0/uImage-gsj.bin') 451 kernel_hash = 'fa67b2f141d56d39b3c54305c0e8a899c99eb2c7' 452 kernel_path = self.fetch_asset(kernel_url, asset_hash=kernel_hash) 453 dtb_url = ( 454 'https://github.com/hskinnemoen/openbmc/releases/download/' 455 '20200711-gsj-qemu-0/nuvoton-npcm730-gsj.dtb') 456 dtb_hash = '18315f7006d7b688d8312d5c727eecd819aa36a4' 457 dtb_path = self.fetch_asset(dtb_url, asset_hash=dtb_hash) 458 459 self.vm.set_console() 460 kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE + 461 'console=ttyS0,115200n8 ' 462 'earlycon=uart8250,mmio32,0xf0001000') 463 self.vm.add_args('-kernel', kernel_path, 464 '-initrd', initrd_path, 465 '-dtb', dtb_path, 466 '-append', kernel_command_line) 467 self.vm.launch() 468 469 self.wait_for_console_pattern('Booting Linux on physical CPU 0x0') 470 self.wait_for_console_pattern('CPU1: thread -1, cpu 1, socket 0') 471 self.wait_for_console_pattern( 472 'Give root password for system maintenance') 473 474 def test_arm_ast2600_debian(self): 475 """ 476 :avocado: tags=arch:arm 477 :avocado: tags=machine:rainier-bmc 478 """ 479 deb_url = ('http://snapshot.debian.org/archive/debian/' 480 '20220606T211338Z/' 481 'pool/main/l/linux/' 482 'linux-image-5.17.0-2-armmp_5.17.6-1%2Bb1_armhf.deb') 483 deb_hash = '8acb2b4439faedc2f3ed4bdb2847ad4f6e0491f73debaeb7f660c8abe4dcdc0e' 484 deb_path = self.fetch_asset(deb_url, asset_hash=deb_hash, 485 algorithm='sha256') 486 kernel_path = self.extract_from_deb(deb_path, '/boot/vmlinuz-5.17.0-2-armmp') 487 dtb_path = self.extract_from_deb(deb_path, 488 '/usr/lib/linux-image-5.17.0-2-armmp/aspeed-bmc-ibm-rainier.dtb') 489 490 self.vm.set_console() 491 self.vm.add_args('-kernel', kernel_path, 492 '-dtb', dtb_path, 493 '-net', 'nic') 494 self.vm.launch() 495 self.wait_for_console_pattern("Booting Linux on physical CPU 0xf00") 496 self.wait_for_console_pattern("SMP: Total of 2 processors activated") 497 self.wait_for_console_pattern("No filesystem could mount root") 498 499