xref: /openbmc/qemu/tests/qtest/cdrom-test.c (revision e33ba60b)
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"
14907b5105SMarc-André Lureau #include "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 
exec_xorrisofs(const char ** args)20da900078SAni Sinha static int exec_xorrisofs(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 
prepare_image(const char * arch,char * isoimagepath)40*e33ba60bSPhilippe Mathieu-Daudé static int prepare_image(const char *arch, char *isoimagepath)
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[] = {
46da900078SAni Sinha         "xorrisofs", "-quiet", "-l", "-no-emul-boot",
47*e33ba60bSPhilippe Mathieu-Daudé         "-b", NULL, "-o", isoimagepath, srcdir, NULL
481e8a1faeSThomas Huth     };
491e8a1faeSThomas Huth 
50*e33ba60bSPhilippe Mathieu-Daudé     ifh = mkstemp(isoimagepath);
511e8a1faeSThomas Huth     if (ifh < 0) {
521e8a1faeSThomas Huth         perror("Error creating temporary iso image file");
531e8a1faeSThomas Huth         return -1;
541e8a1faeSThomas Huth     }
553c239aa7SBin Meng     if (!g_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;
78da900078SAni Sinha     ret = exec_xorrisofs(args);
791e8a1faeSThomas Huth     if (ret) {
80da900078SAni Sinha         fprintf(stderr, "xorrisofs 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  */
test_cdrom_param(gconstpointer data)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 
add_cdrom_param_tests(const char ** machines)1091e8a1faeSThomas Huth static void add_cdrom_param_tests(const char **machines)
1101e8a1faeSThomas Huth {
1111e8a1faeSThomas Huth     while (*machines) {
112719051caSThomas 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);
116719051caSThomas Huth         }
1171e8a1faeSThomas Huth         machines++;
1181e8a1faeSThomas Huth     }
1191e8a1faeSThomas Huth }
1201e8a1faeSThomas Huth 
test_cdboot(gconstpointer data)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 
add_x86_tests(void)1311e8a1faeSThomas Huth static void add_x86_tests(void)
1321e8a1faeSThomas 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 
1381e8a1faeSThomas Huth     qtest_add_data_func("cdrom/boot/default", "-cdrom ", test_cdboot);
139b49056b5SThomas Huth     if (qtest_has_device("virtio-scsi-ccw")) {
1401e8a1faeSThomas Huth         qtest_add_data_func("cdrom/boot/virtio-scsi",
1411e8a1faeSThomas Huth                             "-device virtio-scsi -device scsi-cd,drive=cdr "
142b49056b5SThomas Huth                             "-blockdev file,node-name=cdr,filename=",
143b49056b5SThomas Huth                             test_cdboot);
144b49056b5SThomas Huth     }
1451e8a1faeSThomas Huth     /*
1461e8a1faeSThomas Huth      * Unstable CI test under load
1471e8a1faeSThomas Huth      * See https://lists.gnu.org/archive/html/qemu-devel/2019-02/msg05509.html
1481e8a1faeSThomas Huth      */
149274f5e63SThomas Huth     if (g_test_slow() && qtest_has_machine("isapc")) {
1501e8a1faeSThomas Huth         qtest_add_data_func("cdrom/boot/isapc", "-M isapc "
1511e8a1faeSThomas Huth                             "-drive if=ide,media=cdrom,file=", test_cdboot);
1521e8a1faeSThomas Huth     }
15395c0b770SThomas Huth     if (qtest_has_device("am53c974")) {
1541e8a1faeSThomas Huth         qtest_add_data_func("cdrom/boot/am53c974",
1551e8a1faeSThomas Huth                             "-device am53c974 -device scsi-cd,drive=cd1 "
15695c0b770SThomas Huth                             "-drive if=none,id=cd1,format=raw,file=",
15795c0b770SThomas Huth                             test_cdboot);
15895c0b770SThomas Huth     }
15995c0b770SThomas Huth     if (qtest_has_device("dc390")) {
1601e8a1faeSThomas Huth         qtest_add_data_func("cdrom/boot/dc390",
1611e8a1faeSThomas Huth                             "-device dc390 -device scsi-cd,drive=cd1 "
16295c0b770SThomas Huth                             "-blockdev file,node-name=cd1,filename=",
16395c0b770SThomas Huth                             test_cdboot);
16495c0b770SThomas Huth     }
16595c0b770SThomas Huth     if (qtest_has_device("lsi53c895a")) {
1661e8a1faeSThomas Huth         qtest_add_data_func("cdrom/boot/lsi53c895a",
1671e8a1faeSThomas Huth                             "-device lsi53c895a -device scsi-cd,drive=cd1 "
16895c0b770SThomas Huth                             "-blockdev file,node-name=cd1,filename=",
16995c0b770SThomas Huth                             test_cdboot);
17095c0b770SThomas Huth     }
17195c0b770SThomas Huth     if (qtest_has_device("megasas")) {
1721e8a1faeSThomas Huth         qtest_add_data_func("cdrom/boot/megasas", "-M q35 "
1731e8a1faeSThomas Huth                             "-device megasas -device scsi-cd,drive=cd1 "
17495c0b770SThomas Huth                             "-blockdev file,node-name=cd1,filename=",
17595c0b770SThomas Huth                             test_cdboot);
17695c0b770SThomas Huth     }
17795c0b770SThomas Huth     if (qtest_has_device("megasas-gen2")) {
1781e8a1faeSThomas Huth         qtest_add_data_func("cdrom/boot/megasas-gen2", "-M q35 "
1791e8a1faeSThomas Huth                             "-device megasas-gen2 -device scsi-cd,drive=cd1 "
18095c0b770SThomas Huth                             "-blockdev file,node-name=cd1,filename=",
18195c0b770SThomas Huth                             test_cdboot);
18295c0b770SThomas Huth     }
1831e8a1faeSThomas Huth }
1841e8a1faeSThomas Huth 
add_s390x_tests(void)1851e8a1faeSThomas Huth static void add_s390x_tests(void)
1861e8a1faeSThomas Huth {
187c726fa70SFabiano Rosas     if (!qtest_has_accel("tcg") && !qtest_has_accel("kvm")) {
188c726fa70SFabiano Rosas         g_test_skip("No KVM or TCG accelerator available, skipping boot tests");
189b49056b5SThomas Huth     }
190b49056b5SThomas Huth     if (!qtest_has_device("virtio-blk-ccw")) {
191c726fa70SFabiano Rosas         return;
192c726fa70SFabiano Rosas     }
193c726fa70SFabiano Rosas 
1941e8a1faeSThomas Huth     qtest_add_data_func("cdrom/boot/default", "-cdrom ", test_cdboot);
195b49056b5SThomas Huth 
196b49056b5SThomas Huth     if (!qtest_has_device("virtio-scsi-ccw")) {
197b49056b5SThomas Huth         return;
198b49056b5SThomas Huth     }
199b49056b5SThomas Huth 
2001e8a1faeSThomas Huth     qtest_add_data_func("cdrom/boot/virtio-scsi",
2011e8a1faeSThomas Huth                         "-device virtio-scsi -device scsi-cd,drive=cdr "
2021e8a1faeSThomas Huth                         "-blockdev file,node-name=cdr,filename=", test_cdboot);
203eb32abd8SThomas Huth     qtest_add_data_func("cdrom/boot/with-bootindex",
204eb32abd8SThomas Huth                         "-device virtio-serial -device virtio-scsi "
205eb32abd8SThomas Huth                         "-device virtio-blk,drive=d1 "
206eb32abd8SThomas Huth                         "-drive driver=null-co,read-zeroes=on,if=none,id=d1 "
207eb32abd8SThomas Huth                         "-device virtio-blk,drive=d2,bootindex=1 "
208eb32abd8SThomas Huth                         "-drive if=none,id=d2,media=cdrom,file=", test_cdboot);
20995c0b770SThomas Huth     if (qtest_has_device("x-terminal3270")) {
210eb32abd8SThomas Huth         qtest_add_data_func("cdrom/boot/without-bootindex",
211eb32abd8SThomas Huth                             "-device virtio-scsi -device virtio-serial "
212eb32abd8SThomas Huth                             "-device x-terminal3270 -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 "
21595c0b770SThomas Huth                             "-drive if=none,id=d2,media=cdrom,file=",
21695c0b770SThomas Huth                             test_cdboot);
21795c0b770SThomas Huth     }
2181e8a1faeSThomas Huth }
2191e8a1faeSThomas Huth 
main(int argc,char ** argv)2201e8a1faeSThomas Huth int main(int argc, char **argv)
2211e8a1faeSThomas Huth {
2221e8a1faeSThomas Huth     int ret;
2231e8a1faeSThomas Huth     const char *arch = qtest_get_arch();
224da900078SAni Sinha     const char *xorrisocheck[] = { "xorrisofs", "-version", NULL };
2251e8a1faeSThomas Huth 
2261e8a1faeSThomas Huth     g_test_init(&argc, &argv, NULL);
2271e8a1faeSThomas Huth 
228da900078SAni Sinha     if (exec_xorrisofs(xorrisocheck)) {
229da900078SAni Sinha         /* xorrisofs not available - so can't run tests */
2301e8a1faeSThomas Huth         return g_test_run();
2311e8a1faeSThomas Huth     }
2321e8a1faeSThomas Huth 
2331e8a1faeSThomas Huth     ret = prepare_image(arch, isoimage);
2341e8a1faeSThomas Huth     if (ret) {
2351e8a1faeSThomas Huth         return ret;
2361e8a1faeSThomas Huth     }
2371e8a1faeSThomas Huth 
2381e8a1faeSThomas Huth     if (g_str_equal(arch, "i386") || g_str_equal(arch, "x86_64")) {
2391e8a1faeSThomas Huth         add_x86_tests();
2401e8a1faeSThomas Huth     } else if (g_str_equal(arch, "s390x")) {
2411e8a1faeSThomas Huth         add_s390x_tests();
2421e8a1faeSThomas Huth     } else if (g_str_equal(arch, "ppc64")) {
2431e8a1faeSThomas Huth         const char *ppcmachines[] = {
244b2ce76a0SThomas Huth             "pseries", "mac99", "g3beige", "40p", NULL
2451e8a1faeSThomas Huth         };
2461e8a1faeSThomas Huth         add_cdrom_param_tests(ppcmachines);
2471e8a1faeSThomas Huth     } else if (g_str_equal(arch, "sparc")) {
2481e8a1faeSThomas Huth         const char *sparcmachines[] = {
2491e8a1faeSThomas Huth             "LX", "SPARCClassic", "SPARCbook", "SS-10", "SS-20", "SS-4",
2501e8a1faeSThomas Huth             "SS-5", "SS-600MP", "Voyager", "leon3_generic", NULL
2511e8a1faeSThomas Huth         };
2521e8a1faeSThomas Huth         add_cdrom_param_tests(sparcmachines);
2531e8a1faeSThomas Huth     } else if (g_str_equal(arch, "sparc64")) {
2541e8a1faeSThomas Huth         const char *sparc64machines[] = {
2551e8a1faeSThomas Huth             "niagara", "sun4u", "sun4v", NULL
2561e8a1faeSThomas Huth         };
2571e8a1faeSThomas Huth         add_cdrom_param_tests(sparc64machines);
2581e8a1faeSThomas Huth     } else if (!strncmp(arch, "mips64", 6)) {
2591e8a1faeSThomas Huth         const char *mips64machines[] = {
260f169413cSPhilippe Mathieu-Daudé             "magnum", "malta", "pica61", NULL
2611e8a1faeSThomas Huth         };
2621e8a1faeSThomas Huth         add_cdrom_param_tests(mips64machines);
2631e8a1faeSThomas Huth     } else if (g_str_equal(arch, "arm") || g_str_equal(arch, "aarch64")) {
2641e8a1faeSThomas Huth         const char *armmachines[] = {
2651e8a1faeSThomas Huth             "realview-eb", "realview-eb-mpcore", "realview-pb-a8",
2661e8a1faeSThomas Huth             "realview-pbx-a9", "versatileab", "versatilepb", "vexpress-a15",
2678c730de7SThomas Huth             "vexpress-a9", NULL
2681e8a1faeSThomas Huth         };
2691e8a1faeSThomas Huth         add_cdrom_param_tests(armmachines);
2708c730de7SThomas Huth         if (qtest_has_device("virtio-blk-pci")) {
2718c730de7SThomas Huth             const char *virtmachine[] = { "virt", NULL };
2728c730de7SThomas Huth             add_cdrom_param_tests(virtmachine);
2738c730de7SThomas Huth         }
2741e8a1faeSThomas Huth     } else {
2751e8a1faeSThomas Huth         const char *nonemachine[] = { "none", NULL };
2761e8a1faeSThomas Huth         add_cdrom_param_tests(nonemachine);
2771e8a1faeSThomas Huth     }
2781e8a1faeSThomas Huth 
2791e8a1faeSThomas Huth     ret = g_test_run();
2801e8a1faeSThomas Huth 
2811e8a1faeSThomas Huth     unlink(isoimage);
2821e8a1faeSThomas Huth 
2831e8a1faeSThomas Huth     return ret;
2841e8a1faeSThomas Huth }
285