11e8a1faeSThomas Huth /* 21e8a1faeSThomas Huth * Various tests for emulated CD-ROM drives. 31e8a1faeSThomas Huth * 41e8a1faeSThomas Huth * Copyright (c) 2018 Red Hat Inc. 51e8a1faeSThomas Huth * 61e8a1faeSThomas Huth * Author: 71e8a1faeSThomas Huth * Thomas Huth <thuth@redhat.com> 81e8a1faeSThomas Huth * 91e8a1faeSThomas Huth * This work is licensed under the terms of the GNU GPL, version 2 101e8a1faeSThomas Huth * or later. See the COPYING file in the top-level directory. 111e8a1faeSThomas Huth */ 121e8a1faeSThomas Huth 131e8a1faeSThomas Huth #include "qemu/osdep.h" 14a2ce7dbdSPaolo Bonzini #include "libqos/libqtest.h" 151e8a1faeSThomas Huth #include "boot-sector.h" 161e8a1faeSThomas Huth #include "qapi/qmp/qdict.h" 171e8a1faeSThomas Huth 181e8a1faeSThomas Huth static char isoimage[] = "cdrom-boot-iso-XXXXXX"; 191e8a1faeSThomas Huth 201e8a1faeSThomas Huth static int exec_genisoimg(const char **args) 211e8a1faeSThomas Huth { 221e8a1faeSThomas Huth gchar *out_err = NULL; 231e8a1faeSThomas Huth gint exit_status = -1; 241e8a1faeSThomas Huth bool success; 251e8a1faeSThomas Huth 261e8a1faeSThomas Huth success = g_spawn_sync(NULL, (gchar **)args, NULL, 271e8a1faeSThomas Huth G_SPAWN_SEARCH_PATH | G_SPAWN_STDOUT_TO_DEV_NULL, 281e8a1faeSThomas Huth NULL, NULL, NULL, &out_err, &exit_status, NULL); 291e8a1faeSThomas Huth if (!success) { 301e8a1faeSThomas Huth return -ENOENT; 311e8a1faeSThomas Huth } 321e8a1faeSThomas Huth if (out_err) { 331e8a1faeSThomas Huth fputs(out_err, stderr); 341e8a1faeSThomas Huth g_free(out_err); 351e8a1faeSThomas Huth } 361e8a1faeSThomas Huth 371e8a1faeSThomas Huth return exit_status; 381e8a1faeSThomas Huth } 391e8a1faeSThomas Huth 401e8a1faeSThomas Huth static int prepare_image(const char *arch, char *isoimage) 411e8a1faeSThomas Huth { 421e8a1faeSThomas Huth char srcdir[] = "cdrom-test-dir-XXXXXX"; 431e8a1faeSThomas Huth char *codefile = NULL; 441e8a1faeSThomas Huth int ifh, ret = -1; 451e8a1faeSThomas Huth const char *args[] = { 461e8a1faeSThomas Huth "genisoimage", "-quiet", "-l", "-no-emul-boot", 471e8a1faeSThomas Huth "-b", NULL, "-o", isoimage, srcdir, NULL 481e8a1faeSThomas Huth }; 491e8a1faeSThomas Huth 501e8a1faeSThomas Huth ifh = mkstemp(isoimage); 511e8a1faeSThomas Huth if (ifh < 0) { 521e8a1faeSThomas Huth perror("Error creating temporary iso image file"); 531e8a1faeSThomas Huth return -1; 541e8a1faeSThomas Huth } 551e8a1faeSThomas Huth if (!mkdtemp(srcdir)) { 561e8a1faeSThomas Huth perror("Error creating temporary directory"); 571e8a1faeSThomas Huth goto cleanup; 581e8a1faeSThomas Huth } 591e8a1faeSThomas Huth 601e8a1faeSThomas Huth if (g_str_equal(arch, "i386") || g_str_equal(arch, "x86_64") || 611e8a1faeSThomas Huth g_str_equal(arch, "s390x")) { 621e8a1faeSThomas Huth codefile = g_strdup_printf("%s/bootcode-XXXXXX", srcdir); 631e8a1faeSThomas Huth ret = boot_sector_init(codefile); 641e8a1faeSThomas Huth if (ret) { 651e8a1faeSThomas Huth goto cleanup; 661e8a1faeSThomas Huth } 671e8a1faeSThomas Huth } else { 681e8a1faeSThomas Huth /* Just create a dummy file */ 691e8a1faeSThomas Huth char txt[] = "empty disc"; 701e8a1faeSThomas Huth codefile = g_strdup_printf("%s/readme.txt", srcdir); 711e8a1faeSThomas Huth if (!g_file_set_contents(codefile, txt, sizeof(txt) - 1, NULL)) { 721e8a1faeSThomas Huth fprintf(stderr, "Failed to create '%s'\n", codefile); 731e8a1faeSThomas Huth goto cleanup; 741e8a1faeSThomas Huth } 751e8a1faeSThomas Huth } 761e8a1faeSThomas Huth 771e8a1faeSThomas Huth args[5] = strchr(codefile, '/') + 1; 781e8a1faeSThomas Huth ret = exec_genisoimg(args); 791e8a1faeSThomas Huth if (ret) { 801e8a1faeSThomas Huth fprintf(stderr, "genisoimage failed: %i\n", ret); 811e8a1faeSThomas Huth } 821e8a1faeSThomas Huth 831e8a1faeSThomas Huth unlink(codefile); 841e8a1faeSThomas Huth 851e8a1faeSThomas Huth cleanup: 861e8a1faeSThomas Huth g_free(codefile); 871e8a1faeSThomas Huth rmdir(srcdir); 881e8a1faeSThomas Huth close(ifh); 891e8a1faeSThomas Huth 901e8a1faeSThomas Huth return ret; 911e8a1faeSThomas Huth } 921e8a1faeSThomas Huth 931e8a1faeSThomas Huth /** 941e8a1faeSThomas Huth * Check that at least the -cdrom parameter is basically working, i.e. we can 951e8a1faeSThomas Huth * see the filename of the ISO image in the output of "info block" afterwards 961e8a1faeSThomas Huth */ 971e8a1faeSThomas Huth static void test_cdrom_param(gconstpointer data) 981e8a1faeSThomas Huth { 991e8a1faeSThomas Huth QTestState *qts; 1001e8a1faeSThomas Huth char *resp; 1011e8a1faeSThomas Huth 1021e8a1faeSThomas Huth qts = qtest_initf("-M %s -cdrom %s", (const char *)data, isoimage); 1031e8a1faeSThomas Huth resp = qtest_hmp(qts, "info block"); 1041e8a1faeSThomas Huth g_assert(strstr(resp, isoimage) != 0); 1051e8a1faeSThomas Huth g_free(resp); 1061e8a1faeSThomas Huth qtest_quit(qts); 1071e8a1faeSThomas Huth } 1081e8a1faeSThomas Huth 1091e8a1faeSThomas Huth static void add_cdrom_param_tests(const char **machines) 1101e8a1faeSThomas Huth { 1111e8a1faeSThomas Huth while (*machines) { 112*719051caSThomas Huth if (qtest_has_machine(*machines)) { 1131e8a1faeSThomas Huth char *testname = g_strdup_printf("cdrom/param/%s", *machines); 1141e8a1faeSThomas Huth qtest_add_data_func(testname, *machines, test_cdrom_param); 1151e8a1faeSThomas Huth g_free(testname); 116*719051caSThomas Huth } 1171e8a1faeSThomas Huth machines++; 1181e8a1faeSThomas Huth } 1191e8a1faeSThomas Huth } 1201e8a1faeSThomas Huth 1211e8a1faeSThomas Huth static void test_cdboot(gconstpointer data) 1221e8a1faeSThomas Huth { 1231e8a1faeSThomas Huth QTestState *qts; 1241e8a1faeSThomas Huth 1251e8a1faeSThomas Huth qts = qtest_initf("-accel kvm -accel tcg -no-shutdown %s%s", (const char *)data, 1261e8a1faeSThomas Huth isoimage); 1271e8a1faeSThomas Huth boot_sector_test(qts); 1281e8a1faeSThomas Huth qtest_quit(qts); 1291e8a1faeSThomas Huth } 1301e8a1faeSThomas Huth 1311e8a1faeSThomas Huth static void add_x86_tests(void) 1321e8a1faeSThomas Huth { 1331e8a1faeSThomas Huth qtest_add_data_func("cdrom/boot/default", "-cdrom ", test_cdboot); 1341e8a1faeSThomas Huth qtest_add_data_func("cdrom/boot/virtio-scsi", 1351e8a1faeSThomas Huth "-device virtio-scsi -device scsi-cd,drive=cdr " 1361e8a1faeSThomas Huth "-blockdev file,node-name=cdr,filename=", test_cdboot); 1371e8a1faeSThomas Huth /* 1381e8a1faeSThomas Huth * Unstable CI test under load 1391e8a1faeSThomas Huth * See https://lists.gnu.org/archive/html/qemu-devel/2019-02/msg05509.html 1401e8a1faeSThomas Huth */ 1411e8a1faeSThomas Huth if (g_test_slow()) { 1421e8a1faeSThomas Huth qtest_add_data_func("cdrom/boot/isapc", "-M isapc " 1431e8a1faeSThomas Huth "-drive if=ide,media=cdrom,file=", test_cdboot); 1441e8a1faeSThomas Huth } 1451e8a1faeSThomas Huth qtest_add_data_func("cdrom/boot/am53c974", 1461e8a1faeSThomas Huth "-device am53c974 -device scsi-cd,drive=cd1 " 1471e8a1faeSThomas Huth "-drive if=none,id=cd1,format=raw,file=", test_cdboot); 1481e8a1faeSThomas Huth qtest_add_data_func("cdrom/boot/dc390", 1491e8a1faeSThomas Huth "-device dc390 -device scsi-cd,drive=cd1 " 1501e8a1faeSThomas Huth "-blockdev file,node-name=cd1,filename=", test_cdboot); 1511e8a1faeSThomas Huth qtest_add_data_func("cdrom/boot/lsi53c895a", 1521e8a1faeSThomas Huth "-device lsi53c895a -device scsi-cd,drive=cd1 " 1531e8a1faeSThomas Huth "-blockdev file,node-name=cd1,filename=", test_cdboot); 1541e8a1faeSThomas Huth qtest_add_data_func("cdrom/boot/megasas", "-M q35 " 1551e8a1faeSThomas Huth "-device megasas -device scsi-cd,drive=cd1 " 1561e8a1faeSThomas Huth "-blockdev file,node-name=cd1,filename=", test_cdboot); 1571e8a1faeSThomas Huth qtest_add_data_func("cdrom/boot/megasas-gen2", "-M q35 " 1581e8a1faeSThomas Huth "-device megasas-gen2 -device scsi-cd,drive=cd1 " 1591e8a1faeSThomas Huth "-blockdev file,node-name=cd1,filename=", test_cdboot); 1601e8a1faeSThomas Huth } 1611e8a1faeSThomas Huth 1621e8a1faeSThomas Huth static void add_s390x_tests(void) 1631e8a1faeSThomas Huth { 1641e8a1faeSThomas Huth qtest_add_data_func("cdrom/boot/default", "-cdrom ", test_cdboot); 1651e8a1faeSThomas Huth qtest_add_data_func("cdrom/boot/virtio-scsi", 1661e8a1faeSThomas Huth "-device virtio-scsi -device scsi-cd,drive=cdr " 1671e8a1faeSThomas Huth "-blockdev file,node-name=cdr,filename=", test_cdboot); 168eb32abd8SThomas Huth qtest_add_data_func("cdrom/boot/with-bootindex", 169eb32abd8SThomas Huth "-device virtio-serial -device virtio-scsi " 170eb32abd8SThomas Huth "-device virtio-blk,drive=d1 " 171eb32abd8SThomas Huth "-drive driver=null-co,read-zeroes=on,if=none,id=d1 " 172eb32abd8SThomas Huth "-device virtio-blk,drive=d2,bootindex=1 " 173eb32abd8SThomas Huth "-drive if=none,id=d2,media=cdrom,file=", test_cdboot); 174eb32abd8SThomas Huth qtest_add_data_func("cdrom/boot/without-bootindex", 175eb32abd8SThomas Huth "-device virtio-scsi -device virtio-serial " 176eb32abd8SThomas Huth "-device x-terminal3270 -device virtio-blk,drive=d1 " 177eb32abd8SThomas Huth "-drive driver=null-co,read-zeroes=on,if=none,id=d1 " 178eb32abd8SThomas Huth "-device virtio-blk,drive=d2 " 179eb32abd8SThomas Huth "-drive if=none,id=d2,media=cdrom,file=", test_cdboot); 1801e8a1faeSThomas Huth } 1811e8a1faeSThomas Huth 1821e8a1faeSThomas Huth int main(int argc, char **argv) 1831e8a1faeSThomas Huth { 1841e8a1faeSThomas Huth int ret; 1851e8a1faeSThomas Huth const char *arch = qtest_get_arch(); 1861e8a1faeSThomas Huth const char *genisocheck[] = { "genisoimage", "-version", NULL }; 1871e8a1faeSThomas Huth 1881e8a1faeSThomas Huth g_test_init(&argc, &argv, NULL); 1891e8a1faeSThomas Huth 1901e8a1faeSThomas Huth if (exec_genisoimg(genisocheck)) { 1911e8a1faeSThomas Huth /* genisoimage not available - so can't run tests */ 1921e8a1faeSThomas Huth return g_test_run(); 1931e8a1faeSThomas Huth } 1941e8a1faeSThomas Huth 1951e8a1faeSThomas Huth ret = prepare_image(arch, isoimage); 1961e8a1faeSThomas Huth if (ret) { 1971e8a1faeSThomas Huth return ret; 1981e8a1faeSThomas Huth } 1991e8a1faeSThomas Huth 2001e8a1faeSThomas Huth if (g_str_equal(arch, "i386") || g_str_equal(arch, "x86_64")) { 2011e8a1faeSThomas Huth add_x86_tests(); 2021e8a1faeSThomas Huth } else if (g_str_equal(arch, "s390x")) { 2031e8a1faeSThomas Huth add_s390x_tests(); 2041e8a1faeSThomas Huth } else if (g_str_equal(arch, "ppc64")) { 2051e8a1faeSThomas Huth const char *ppcmachines[] = { 206b2ce76a0SThomas Huth "pseries", "mac99", "g3beige", "40p", NULL 2071e8a1faeSThomas Huth }; 2081e8a1faeSThomas Huth add_cdrom_param_tests(ppcmachines); 2091e8a1faeSThomas Huth } else if (g_str_equal(arch, "sparc")) { 2101e8a1faeSThomas Huth const char *sparcmachines[] = { 2111e8a1faeSThomas Huth "LX", "SPARCClassic", "SPARCbook", "SS-10", "SS-20", "SS-4", 2121e8a1faeSThomas Huth "SS-5", "SS-600MP", "Voyager", "leon3_generic", NULL 2131e8a1faeSThomas Huth }; 2141e8a1faeSThomas Huth add_cdrom_param_tests(sparcmachines); 2151e8a1faeSThomas Huth } else if (g_str_equal(arch, "sparc64")) { 2161e8a1faeSThomas Huth const char *sparc64machines[] = { 2171e8a1faeSThomas Huth "niagara", "sun4u", "sun4v", NULL 2181e8a1faeSThomas Huth }; 2191e8a1faeSThomas Huth add_cdrom_param_tests(sparc64machines); 2201e8a1faeSThomas Huth } else if (!strncmp(arch, "mips64", 6)) { 2211e8a1faeSThomas Huth const char *mips64machines[] = { 222f169413cSPhilippe Mathieu-Daudé "magnum", "malta", "pica61", NULL 2231e8a1faeSThomas Huth }; 2241e8a1faeSThomas Huth add_cdrom_param_tests(mips64machines); 2251e8a1faeSThomas Huth } else if (g_str_equal(arch, "arm") || g_str_equal(arch, "aarch64")) { 2261e8a1faeSThomas Huth const char *armmachines[] = { 2271e8a1faeSThomas Huth "realview-eb", "realview-eb-mpcore", "realview-pb-a8", 2281e8a1faeSThomas Huth "realview-pbx-a9", "versatileab", "versatilepb", "vexpress-a15", 2291e8a1faeSThomas Huth "vexpress-a9", "virt", NULL 2301e8a1faeSThomas Huth }; 2311e8a1faeSThomas Huth add_cdrom_param_tests(armmachines); 2321e8a1faeSThomas Huth } else { 2331e8a1faeSThomas Huth const char *nonemachine[] = { "none", NULL }; 2341e8a1faeSThomas Huth add_cdrom_param_tests(nonemachine); 2351e8a1faeSThomas Huth } 2361e8a1faeSThomas Huth 2371e8a1faeSThomas Huth ret = g_test_run(); 2381e8a1faeSThomas Huth 2391e8a1faeSThomas Huth unlink(isoimage); 2401e8a1faeSThomas Huth 2411e8a1faeSThomas Huth return ret; 2421e8a1faeSThomas Huth } 243