142f45505SThomas Huth /* 242f45505SThomas Huth * Various tests for emulated CD-ROM drives. 342f45505SThomas Huth * 442f45505SThomas Huth * Copyright (c) 2018 Red Hat Inc. 542f45505SThomas Huth * 642f45505SThomas Huth * Author: 742f45505SThomas Huth * Thomas Huth <thuth@redhat.com> 842f45505SThomas Huth * 942f45505SThomas Huth * This work is licensed under the terms of the GNU GPL, version 2 1042f45505SThomas Huth * or later. See the COPYING file in the top-level directory. 1142f45505SThomas Huth */ 1242f45505SThomas Huth 1342f45505SThomas Huth #include "qemu/osdep.h" 14907b5105SMarc-André Lureau #include "libqtest.h" 1542f45505SThomas Huth #include "boot-sector.h" 1649d43a59SThomas Huth #include "qapi/qmp/qdict.h" 1742f45505SThomas Huth 1842f45505SThomas Huth static char isoimage[] = "cdrom-boot-iso-XXXXXX"; 1942f45505SThomas Huth 20da900078SAni Sinha static int exec_xorrisofs(const char **args) 2142f45505SThomas Huth { 2242f45505SThomas Huth gchar *out_err = NULL; 2342f45505SThomas Huth gint exit_status = -1; 2442f45505SThomas Huth bool success; 2542f45505SThomas Huth 2642f45505SThomas Huth success = g_spawn_sync(NULL, (gchar **)args, NULL, 2742f45505SThomas Huth G_SPAWN_SEARCH_PATH | G_SPAWN_STDOUT_TO_DEV_NULL, 2842f45505SThomas Huth NULL, NULL, NULL, &out_err, &exit_status, NULL); 2942f45505SThomas Huth if (!success) { 3042f45505SThomas Huth return -ENOENT; 3142f45505SThomas Huth } 3242f45505SThomas Huth if (out_err) { 3342f45505SThomas Huth fputs(out_err, stderr); 3442f45505SThomas Huth g_free(out_err); 3542f45505SThomas Huth } 3642f45505SThomas Huth 3742f45505SThomas Huth return exit_status; 3842f45505SThomas Huth } 3942f45505SThomas Huth 40e33ba60bSPhilippe Mathieu-Daudé static int prepare_image(const char *arch, char *isoimagepath) 4142f45505SThomas Huth { 4242f45505SThomas Huth char srcdir[] = "cdrom-test-dir-XXXXXX"; 4342f45505SThomas Huth char *codefile = NULL; 4442f45505SThomas Huth int ifh, ret = -1; 4542f45505SThomas Huth const char *args[] = { 46da900078SAni Sinha "xorrisofs", "-quiet", "-l", "-no-emul-boot", 47e33ba60bSPhilippe Mathieu-Daudé "-b", NULL, "-o", isoimagepath, srcdir, NULL 4842f45505SThomas Huth }; 4942f45505SThomas Huth 50e33ba60bSPhilippe Mathieu-Daudé ifh = mkstemp(isoimagepath); 5142f45505SThomas Huth if (ifh < 0) { 5242f45505SThomas Huth perror("Error creating temporary iso image file"); 5342f45505SThomas Huth return -1; 5442f45505SThomas Huth } 553c239aa7SBin Meng if (!g_mkdtemp(srcdir)) { 5642f45505SThomas Huth perror("Error creating temporary directory"); 5742f45505SThomas Huth goto cleanup; 5842f45505SThomas Huth } 5942f45505SThomas Huth 6042f45505SThomas Huth if (g_str_equal(arch, "i386") || g_str_equal(arch, "x86_64") || 6142f45505SThomas Huth g_str_equal(arch, "s390x")) { 6242f45505SThomas Huth codefile = g_strdup_printf("%s/bootcode-XXXXXX", srcdir); 6342f45505SThomas Huth ret = boot_sector_init(codefile); 6442f45505SThomas Huth if (ret) { 6542f45505SThomas Huth goto cleanup; 6642f45505SThomas Huth } 6742f45505SThomas Huth } else { 6842f45505SThomas Huth /* Just create a dummy file */ 6942f45505SThomas Huth char txt[] = "empty disc"; 7042f45505SThomas Huth codefile = g_strdup_printf("%s/readme.txt", srcdir); 7142f45505SThomas Huth if (!g_file_set_contents(codefile, txt, sizeof(txt) - 1, NULL)) { 7242f45505SThomas Huth fprintf(stderr, "Failed to create '%s'\n", codefile); 7342f45505SThomas Huth goto cleanup; 7442f45505SThomas Huth } 7542f45505SThomas Huth } 7642f45505SThomas Huth 7742f45505SThomas Huth args[5] = strchr(codefile, '/') + 1; 78da900078SAni Sinha ret = exec_xorrisofs(args); 7942f45505SThomas Huth if (ret) { 80da900078SAni Sinha fprintf(stderr, "xorrisofs failed: %i\n", ret); 8142f45505SThomas Huth } 8242f45505SThomas Huth 8342f45505SThomas Huth unlink(codefile); 8442f45505SThomas Huth 8542f45505SThomas Huth cleanup: 8642f45505SThomas Huth g_free(codefile); 8742f45505SThomas Huth rmdir(srcdir); 8842f45505SThomas Huth close(ifh); 8942f45505SThomas Huth 9042f45505SThomas Huth return ret; 9142f45505SThomas Huth } 9242f45505SThomas Huth 9349d43a59SThomas Huth /** 9449d43a59SThomas Huth * Check that at least the -cdrom parameter is basically working, i.e. we can 9549d43a59SThomas Huth * see the filename of the ISO image in the output of "info block" afterwards 9649d43a59SThomas Huth */ 9749d43a59SThomas Huth static void test_cdrom_param(gconstpointer data) 9849d43a59SThomas Huth { 9949d43a59SThomas Huth QTestState *qts; 10049d43a59SThomas Huth char *resp; 10149d43a59SThomas Huth 10288b988c8SMarkus Armbruster qts = qtest_initf("-M %s -cdrom %s", (const char *)data, isoimage); 10349d43a59SThomas Huth resp = qtest_hmp(qts, "info block"); 10449d43a59SThomas Huth g_assert(strstr(resp, isoimage) != 0); 10549d43a59SThomas Huth g_free(resp); 10649d43a59SThomas Huth qtest_quit(qts); 10749d43a59SThomas Huth } 10849d43a59SThomas Huth 10949d43a59SThomas Huth static void add_cdrom_param_tests(const char **machines) 11049d43a59SThomas Huth { 11149d43a59SThomas Huth while (*machines) { 112719051caSThomas Huth if (qtest_has_machine(*machines)) { 11349d43a59SThomas Huth char *testname = g_strdup_printf("cdrom/param/%s", *machines); 11449d43a59SThomas Huth qtest_add_data_func(testname, *machines, test_cdrom_param); 11549d43a59SThomas Huth g_free(testname); 116719051caSThomas Huth } 11749d43a59SThomas Huth machines++; 11849d43a59SThomas Huth } 11949d43a59SThomas Huth } 12049d43a59SThomas Huth 12142f45505SThomas Huth static void test_cdboot(gconstpointer data) 12242f45505SThomas Huth { 12342f45505SThomas Huth QTestState *qts; 12442f45505SThomas Huth 1256f6e1698SPaolo Bonzini qts = qtest_initf("-accel kvm -accel tcg -no-shutdown %s%s", (const char *)data, 12642f45505SThomas Huth isoimage); 12742f45505SThomas Huth boot_sector_test(qts); 12842f45505SThomas Huth qtest_quit(qts); 12942f45505SThomas Huth } 13042f45505SThomas Huth 13142f45505SThomas Huth static void add_x86_tests(void) 13242f45505SThomas Huth { 133c726fa70SFabiano Rosas if (!qtest_has_accel("tcg") && !qtest_has_accel("kvm")) { 134c726fa70SFabiano Rosas g_test_skip("No KVM or TCG accelerator available, skipping boot tests"); 135c726fa70SFabiano Rosas return; 136c726fa70SFabiano Rosas } 137c726fa70SFabiano Rosas 138d822b883SThomas Huth if (qtest_has_machine("pc")) { 13942f45505SThomas Huth qtest_add_data_func("cdrom/boot/default", "-cdrom ", test_cdboot); 140b49056b5SThomas Huth if (qtest_has_device("virtio-scsi-ccw")) { 14142f45505SThomas Huth qtest_add_data_func("cdrom/boot/virtio-scsi", 14242f45505SThomas Huth "-device virtio-scsi -device scsi-cd,drive=cdr " 143b49056b5SThomas Huth "-blockdev file,node-name=cdr,filename=", 144b49056b5SThomas Huth test_cdboot); 145b49056b5SThomas Huth } 146d822b883SThomas Huth 14795c0b770SThomas Huth if (qtest_has_device("am53c974")) { 14842f45505SThomas Huth qtest_add_data_func("cdrom/boot/am53c974", 14942f45505SThomas Huth "-device am53c974 -device scsi-cd,drive=cd1 " 15095c0b770SThomas Huth "-drive if=none,id=cd1,format=raw,file=", 15195c0b770SThomas Huth test_cdboot); 15295c0b770SThomas Huth } 15395c0b770SThomas Huth if (qtest_has_device("dc390")) { 15442f45505SThomas Huth qtest_add_data_func("cdrom/boot/dc390", 15542f45505SThomas Huth "-device dc390 -device scsi-cd,drive=cd1 " 15695c0b770SThomas Huth "-blockdev file,node-name=cd1,filename=", 15795c0b770SThomas Huth test_cdboot); 15895c0b770SThomas Huth } 15995c0b770SThomas Huth if (qtest_has_device("lsi53c895a")) { 16042f45505SThomas Huth qtest_add_data_func("cdrom/boot/lsi53c895a", 16142f45505SThomas Huth "-device lsi53c895a -device scsi-cd,drive=cd1 " 16295c0b770SThomas Huth "-blockdev file,node-name=cd1,filename=", 16395c0b770SThomas Huth test_cdboot); 16495c0b770SThomas Huth } 165d822b883SThomas Huth } 166d822b883SThomas Huth 167d822b883SThomas Huth /* 168d822b883SThomas Huth * Unstable CI test under load 169d822b883SThomas Huth * See https://lists.gnu.org/archive/html/qemu-devel/2019-02/msg05509.html 170d822b883SThomas Huth */ 171d822b883SThomas Huth if (g_test_slow() && qtest_has_machine("isapc")) { 172d822b883SThomas Huth qtest_add_data_func("cdrom/boot/isapc", "-M isapc " 173d822b883SThomas Huth "-drive if=ide,media=cdrom,file=", test_cdboot); 174d822b883SThomas Huth } 175d822b883SThomas Huth 176d822b883SThomas Huth if (qtest_has_machine("q35")) { 17795c0b770SThomas Huth if (qtest_has_device("megasas")) { 17842f45505SThomas Huth qtest_add_data_func("cdrom/boot/megasas", "-M q35 " 17942f45505SThomas Huth "-device megasas -device scsi-cd,drive=cd1 " 18095c0b770SThomas Huth "-blockdev file,node-name=cd1,filename=", 18195c0b770SThomas Huth test_cdboot); 18295c0b770SThomas Huth } 18395c0b770SThomas Huth if (qtest_has_device("megasas-gen2")) { 18442f45505SThomas Huth qtest_add_data_func("cdrom/boot/megasas-gen2", "-M q35 " 18542f45505SThomas Huth "-device megasas-gen2 -device scsi-cd,drive=cd1 " 18695c0b770SThomas Huth "-blockdev file,node-name=cd1,filename=", 18795c0b770SThomas Huth test_cdboot); 18895c0b770SThomas Huth } 18942f45505SThomas Huth } 190d822b883SThomas Huth } 19142f45505SThomas Huth 19242f45505SThomas Huth static void add_s390x_tests(void) 19342f45505SThomas Huth { 194c726fa70SFabiano Rosas if (!qtest_has_accel("tcg") && !qtest_has_accel("kvm")) { 195c726fa70SFabiano Rosas g_test_skip("No KVM or TCG accelerator available, skipping boot tests"); 196b49056b5SThomas Huth } 197b49056b5SThomas Huth if (!qtest_has_device("virtio-blk-ccw")) { 198c726fa70SFabiano Rosas return; 199c726fa70SFabiano Rosas } 200c726fa70SFabiano Rosas 20142f45505SThomas Huth qtest_add_data_func("cdrom/boot/default", "-cdrom ", test_cdboot); 202b49056b5SThomas Huth 203b49056b5SThomas Huth if (!qtest_has_device("virtio-scsi-ccw")) { 204b49056b5SThomas Huth return; 205b49056b5SThomas Huth } 206b49056b5SThomas Huth 20742f45505SThomas Huth qtest_add_data_func("cdrom/boot/virtio-scsi", 20842f45505SThomas Huth "-device virtio-scsi -device scsi-cd,drive=cdr " 20942f45505SThomas Huth "-blockdev file,node-name=cdr,filename=", test_cdboot); 210eb32abd8SThomas Huth qtest_add_data_func("cdrom/boot/with-bootindex", 211eb32abd8SThomas Huth "-device virtio-serial -device virtio-scsi " 212eb32abd8SThomas Huth "-device virtio-blk,drive=d1 " 213eb32abd8SThomas Huth "-drive driver=null-co,read-zeroes=on,if=none,id=d1 " 214eb32abd8SThomas Huth "-device virtio-blk,drive=d2,bootindex=1 " 215eb32abd8SThomas Huth "-drive if=none,id=d2,media=cdrom,file=", test_cdboot); 216*f5aa2d9dSJared Rossi qtest_add_data_func("cdrom/boot/as-fallback-device", 217*f5aa2d9dSJared Rossi "-device virtio-serial -device virtio-scsi " 218*f5aa2d9dSJared Rossi "-device virtio-blk,drive=d1,bootindex=1 " 219*f5aa2d9dSJared Rossi "-drive driver=null-co,read-zeroes=on,if=none,id=d1 " 220*f5aa2d9dSJared Rossi "-device virtio-blk,drive=d2,bootindex=2 " 221*f5aa2d9dSJared Rossi "-drive if=none,id=d2,media=cdrom,file=", test_cdboot); 222*f5aa2d9dSJared Rossi qtest_add_data_func("cdrom/boot/as-last-option", 223*f5aa2d9dSJared Rossi "-device virtio-serial -device virtio-scsi " 224*f5aa2d9dSJared Rossi "-device virtio-blk,drive=d1,bootindex=1 " 225*f5aa2d9dSJared Rossi "-drive driver=null-co,read-zeroes=on,if=none,id=d1 " 226*f5aa2d9dSJared Rossi "-device virtio-blk,drive=d2,bootindex=2 " 227*f5aa2d9dSJared Rossi "-drive driver=null-co,read-zeroes=on,if=none,id=d2 " 228*f5aa2d9dSJared Rossi "-device virtio-blk,drive=d3,bootindex=3 " 229*f5aa2d9dSJared Rossi "-drive driver=null-co,read-zeroes=on,if=none,id=d3 " 230*f5aa2d9dSJared Rossi "-device scsi-hd,drive=d4,bootindex=4 " 231*f5aa2d9dSJared Rossi "-drive driver=null-co,read-zeroes=on,if=none,id=d4 " 232*f5aa2d9dSJared Rossi "-device scsi-hd,drive=d5,bootindex=5 " 233*f5aa2d9dSJared Rossi "-drive driver=null-co,read-zeroes=on,if=none,id=d5 " 234*f5aa2d9dSJared Rossi "-device virtio-blk,drive=d6,bootindex=6 " 235*f5aa2d9dSJared Rossi "-drive driver=null-co,read-zeroes=on,if=none,id=d6 " 236*f5aa2d9dSJared Rossi "-device scsi-hd,drive=d7,bootindex=7 " 237*f5aa2d9dSJared Rossi "-drive driver=null-co,read-zeroes=on,if=none,id=d7 " 238*f5aa2d9dSJared Rossi "-device scsi-cd,drive=d8,bootindex=8 " 239*f5aa2d9dSJared Rossi "-drive if=none,id=d8,media=cdrom,file=", test_cdboot); 24095c0b770SThomas Huth if (qtest_has_device("x-terminal3270")) { 241eb32abd8SThomas Huth qtest_add_data_func("cdrom/boot/without-bootindex", 242eb32abd8SThomas Huth "-device virtio-scsi -device virtio-serial " 243eb32abd8SThomas Huth "-device x-terminal3270 -device virtio-blk,drive=d1 " 244eb32abd8SThomas Huth "-drive driver=null-co,read-zeroes=on,if=none,id=d1 " 245eb32abd8SThomas Huth "-device virtio-blk,drive=d2 " 24695c0b770SThomas Huth "-drive if=none,id=d2,media=cdrom,file=", 24795c0b770SThomas Huth test_cdboot); 24895c0b770SThomas Huth } 24942f45505SThomas Huth } 25042f45505SThomas Huth 25142f45505SThomas Huth int main(int argc, char **argv) 25242f45505SThomas Huth { 25342f45505SThomas Huth int ret; 25442f45505SThomas Huth const char *arch = qtest_get_arch(); 255da900078SAni Sinha const char *xorrisocheck[] = { "xorrisofs", "-version", NULL }; 25642f45505SThomas Huth 25742f45505SThomas Huth g_test_init(&argc, &argv, NULL); 25842f45505SThomas Huth 259da900078SAni Sinha if (exec_xorrisofs(xorrisocheck)) { 260da900078SAni Sinha /* xorrisofs not available - so can't run tests */ 2614848cb3dSPaolo Bonzini return g_test_run(); 26242f45505SThomas Huth } 26342f45505SThomas Huth 26442f45505SThomas Huth ret = prepare_image(arch, isoimage); 26542f45505SThomas Huth if (ret) { 26642f45505SThomas Huth return ret; 26742f45505SThomas Huth } 26842f45505SThomas Huth 26942f45505SThomas Huth if (g_str_equal(arch, "i386") || g_str_equal(arch, "x86_64")) { 27042f45505SThomas Huth add_x86_tests(); 27142f45505SThomas Huth } else if (g_str_equal(arch, "s390x")) { 27242f45505SThomas Huth add_s390x_tests(); 27349d43a59SThomas Huth } else if (g_str_equal(arch, "ppc64")) { 27449d43a59SThomas Huth const char *ppcmachines[] = { 275b2ce76a0SThomas Huth "pseries", "mac99", "g3beige", "40p", NULL 27649d43a59SThomas Huth }; 27749d43a59SThomas Huth add_cdrom_param_tests(ppcmachines); 27849d43a59SThomas Huth } else if (g_str_equal(arch, "sparc")) { 27949d43a59SThomas Huth const char *sparcmachines[] = { 28049d43a59SThomas Huth "LX", "SPARCClassic", "SPARCbook", "SS-10", "SS-20", "SS-4", 28149d43a59SThomas Huth "SS-5", "SS-600MP", "Voyager", "leon3_generic", NULL 28249d43a59SThomas Huth }; 28349d43a59SThomas Huth add_cdrom_param_tests(sparcmachines); 28449d43a59SThomas Huth } else if (g_str_equal(arch, "sparc64")) { 28549d43a59SThomas Huth const char *sparc64machines[] = { 28649d43a59SThomas Huth "niagara", "sun4u", "sun4v", NULL 28749d43a59SThomas Huth }; 28849d43a59SThomas Huth add_cdrom_param_tests(sparc64machines); 28949d43a59SThomas Huth } else if (!strncmp(arch, "mips64", 6)) { 29049d43a59SThomas Huth const char *mips64machines[] = { 291f169413cSPhilippe Mathieu-Daudé "magnum", "malta", "pica61", NULL 29249d43a59SThomas Huth }; 29349d43a59SThomas Huth add_cdrom_param_tests(mips64machines); 29449d43a59SThomas Huth } else if (g_str_equal(arch, "arm") || g_str_equal(arch, "aarch64")) { 29549d43a59SThomas Huth const char *armmachines[] = { 29649d43a59SThomas Huth "realview-eb", "realview-eb-mpcore", "realview-pb-a8", 29749d43a59SThomas Huth "realview-pbx-a9", "versatileab", "versatilepb", "vexpress-a15", 2988c730de7SThomas Huth "vexpress-a9", NULL 29949d43a59SThomas Huth }; 30049d43a59SThomas Huth add_cdrom_param_tests(armmachines); 3018c730de7SThomas Huth if (qtest_has_device("virtio-blk-pci")) { 3028c730de7SThomas Huth const char *virtmachine[] = { "virt", NULL }; 3038c730de7SThomas Huth add_cdrom_param_tests(virtmachine); 3048c730de7SThomas Huth } 3050b76a1a9SBibo Mao } else if (g_str_equal(arch, "loongarch64")) { 3060b76a1a9SBibo Mao if (qtest_has_device("virtio-blk-pci")) { 3070b76a1a9SBibo Mao const char *virtmachine[] = { "virt", NULL }; 3080b76a1a9SBibo Mao add_cdrom_param_tests(virtmachine); 3090b76a1a9SBibo Mao } 31049d43a59SThomas Huth } else { 31149d43a59SThomas Huth const char *nonemachine[] = { "none", NULL }; 31249d43a59SThomas Huth add_cdrom_param_tests(nonemachine); 31342f45505SThomas Huth } 31442f45505SThomas Huth 31542f45505SThomas Huth ret = g_test_run(); 31642f45505SThomas Huth 31742f45505SThomas Huth unlink(isoimage); 31842f45505SThomas Huth 31942f45505SThomas Huth return ret; 32042f45505SThomas Huth } 321