boot-order-test.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Boot order test cases.
  3. *
  4. * Copyright (c) 2013 Red Hat Inc.
  5. *
  6. * Authors:
  7. * Markus Armbruster <armbru@redhat.com>,
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  10. * See the COPYING file in the top-level directory.
  11. */
  12. #include "qemu/osdep.h"
  13. #include "libqos/fw_cfg.h"
  14. #include "libqtest.h"
  15. #include "qapi/qmp/qdict.h"
  16. #include "standard-headers/linux/qemu_fw_cfg.h"
  17. /* TODO actually test the results and get rid of this */
  18. #define qmp_discard_response(qs, ...) qobject_unref(qtest_qmp(qs, __VA_ARGS__))
  19. typedef struct {
  20. const char *args;
  21. uint64_t expected_boot;
  22. uint64_t expected_reboot;
  23. } boot_order_test;
  24. static void test_a_boot_order(const char *machine,
  25. const char *test_args,
  26. uint64_t (*read_boot_order)(QTestState *),
  27. uint64_t expected_boot,
  28. uint64_t expected_reboot)
  29. {
  30. uint64_t actual;
  31. QTestState *qts;
  32. qts = qtest_initf("-nodefaults%s%s %s", machine ? " -M " : "",
  33. machine ?: "", test_args);
  34. actual = read_boot_order(qts);
  35. g_assert_cmphex(actual, ==, expected_boot);
  36. qmp_discard_response(qts, "{ 'execute': 'system_reset' }");
  37. /*
  38. * system_reset only requests reset. We get a RESET event after
  39. * the actual reset completes. Need to wait for that.
  40. */
  41. qtest_qmp_eventwait(qts, "RESET");
  42. actual = read_boot_order(qts);
  43. g_assert_cmphex(actual, ==, expected_reboot);
  44. qtest_quit(qts);
  45. }
  46. static void test_boot_orders(const char *machine,
  47. uint64_t (*read_boot_order)(QTestState *),
  48. const boot_order_test *tests)
  49. {
  50. int i;
  51. for (i = 0; tests[i].args; i++) {
  52. test_a_boot_order(machine, tests[i].args,
  53. read_boot_order,
  54. tests[i].expected_boot,
  55. tests[i].expected_reboot);
  56. }
  57. }
  58. static uint8_t read_mc146818(QTestState *qts, uint16_t port, uint8_t reg)
  59. {
  60. qtest_outb(qts, port, reg);
  61. return qtest_inb(qts, port + 1);
  62. }
  63. static uint64_t read_boot_order_pc(QTestState *qts)
  64. {
  65. uint8_t b1 = read_mc146818(qts, 0x70, 0x38);
  66. uint8_t b2 = read_mc146818(qts, 0x70, 0x3d);
  67. return b1 | (b2 << 8);
  68. }
  69. static const boot_order_test test_cases_pc[] = {
  70. { "",
  71. 0x1230, 0x1230 },
  72. { "-no-fd-bootchk",
  73. 0x1231, 0x1231 },
  74. { "-boot c",
  75. 0x0200, 0x0200 },
  76. { "-boot nda",
  77. 0x3410, 0x3410 },
  78. { "-boot order=",
  79. 0, 0 },
  80. { "-boot order= -boot order=c",
  81. 0x0200, 0x0200 },
  82. { "-boot once=a",
  83. 0x0100, 0x1230 },
  84. { "-boot once=a -no-fd-bootchk",
  85. 0x0101, 0x1231 },
  86. { "-boot once=a,order=c",
  87. 0x0100, 0x0200 },
  88. { "-boot once=d -boot order=nda",
  89. 0x0300, 0x3410 },
  90. { "-boot once=a -boot once=b -boot once=c",
  91. 0x0200, 0x1230 },
  92. {}
  93. };
  94. static void test_pc_boot_order(void)
  95. {
  96. test_boot_orders(NULL, read_boot_order_pc, test_cases_pc);
  97. }
  98. static uint8_t read_m48t59(QTestState *qts, uint64_t addr, uint16_t reg)
  99. {
  100. qtest_writeb(qts, addr, reg & 0xff);
  101. qtest_writeb(qts, addr + 1, reg >> 8);
  102. return qtest_readb(qts, addr + 3);
  103. }
  104. static uint64_t read_boot_order_prep(QTestState *qts)
  105. {
  106. return read_m48t59(qts, 0x80000000 + 0x74, 0x34);
  107. }
  108. static const boot_order_test test_cases_prep[] = {
  109. { "", 'c', 'c' },
  110. { "-boot c", 'c', 'c' },
  111. { "-boot d", 'd', 'd' },
  112. {}
  113. };
  114. static void test_prep_boot_order(void)
  115. {
  116. test_boot_orders("prep", read_boot_order_prep, test_cases_prep);
  117. }
  118. static uint64_t read_boot_order_pmac(QTestState *qts)
  119. {
  120. QFWCFG *fw_cfg = mm_fw_cfg_init(qts, 0xf0000510);
  121. return qfw_cfg_get_u16(fw_cfg, FW_CFG_BOOT_DEVICE);
  122. }
  123. static const boot_order_test test_cases_fw_cfg[] = {
  124. { "", 'c', 'c' },
  125. { "-boot c", 'c', 'c' },
  126. { "-boot d", 'd', 'd' },
  127. { "-boot once=d,order=c", 'd', 'c' },
  128. {}
  129. };
  130. static void test_pmac_oldworld_boot_order(void)
  131. {
  132. test_boot_orders("g3beige", read_boot_order_pmac, test_cases_fw_cfg);
  133. }
  134. static void test_pmac_newworld_boot_order(void)
  135. {
  136. test_boot_orders("mac99", read_boot_order_pmac, test_cases_fw_cfg);
  137. }
  138. static uint64_t read_boot_order_sun4m(QTestState *qts)
  139. {
  140. QFWCFG *fw_cfg = mm_fw_cfg_init(qts, 0xd00000510ULL);
  141. return qfw_cfg_get_u16(fw_cfg, FW_CFG_BOOT_DEVICE);
  142. }
  143. static void test_sun4m_boot_order(void)
  144. {
  145. test_boot_orders("SS-5", read_boot_order_sun4m, test_cases_fw_cfg);
  146. }
  147. static uint64_t read_boot_order_sun4u(QTestState *qts)
  148. {
  149. QFWCFG *fw_cfg = io_fw_cfg_init(qts, 0x510);
  150. return qfw_cfg_get_u16(fw_cfg, FW_CFG_BOOT_DEVICE);
  151. }
  152. static void test_sun4u_boot_order(void)
  153. {
  154. test_boot_orders("sun4u", read_boot_order_sun4u, test_cases_fw_cfg);
  155. }
  156. int main(int argc, char *argv[])
  157. {
  158. const char *arch = qtest_get_arch();
  159. g_test_init(&argc, &argv, NULL);
  160. if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
  161. qtest_add_func("boot-order/pc", test_pc_boot_order);
  162. } else if (strcmp(arch, "ppc") == 0 || strcmp(arch, "ppc64") == 0) {
  163. qtest_add_func("boot-order/prep", test_prep_boot_order);
  164. qtest_add_func("boot-order/pmac_oldworld",
  165. test_pmac_oldworld_boot_order);
  166. qtest_add_func("boot-order/pmac_newworld",
  167. test_pmac_newworld_boot_order);
  168. } else if (strcmp(arch, "sparc") == 0) {
  169. qtest_add_func("boot-order/sun4m", test_sun4m_boot_order);
  170. } else if (strcmp(arch, "sparc64") == 0) {
  171. qtest_add_func("boot-order/sun4u", test_sun4u_boot_order);
  172. }
  173. return g_test_run();
  174. }