11e8a1faeSThomas Huth /*
21e8a1faeSThomas Huth * qtest fw_cfg test case
31e8a1faeSThomas Huth *
41e8a1faeSThomas Huth * Copyright IBM, Corp. 2012-2013
51e8a1faeSThomas Huth *
61e8a1faeSThomas Huth * Authors:
71e8a1faeSThomas Huth * Anthony Liguori <aliguori@us.ibm.com>
81e8a1faeSThomas Huth *
91e8a1faeSThomas Huth * This work is licensed under the terms of the GNU GPL, version 2 or later.
101e8a1faeSThomas Huth * See the COPYING file in the top-level directory.
111e8a1faeSThomas Huth */
121e8a1faeSThomas Huth
131e8a1faeSThomas Huth #include "qemu/osdep.h"
141e8a1faeSThomas Huth
15*907b5105SMarc-André Lureau #include "libqtest.h"
161e8a1faeSThomas Huth #include "standard-headers/linux/qemu_fw_cfg.h"
171e8a1faeSThomas Huth #include "libqos/fw_cfg.h"
181e8a1faeSThomas Huth #include "qemu/bswap.h"
191e8a1faeSThomas Huth
201e8a1faeSThomas Huth static uint64_t ram_size = 128 << 20;
211e8a1faeSThomas Huth static uint16_t nb_cpus = 1;
221e8a1faeSThomas Huth static uint16_t max_cpus = 1;
231e8a1faeSThomas Huth static uint64_t nb_nodes = 0;
241e8a1faeSThomas Huth static uint16_t boot_menu = 0;
251e8a1faeSThomas Huth
test_fw_cfg_signature(void)261e8a1faeSThomas Huth static void test_fw_cfg_signature(void)
271e8a1faeSThomas Huth {
281e8a1faeSThomas Huth QFWCFG *fw_cfg;
291e8a1faeSThomas Huth QTestState *s;
301e8a1faeSThomas Huth char buf[5];
311e8a1faeSThomas Huth
321e8a1faeSThomas Huth s = qtest_init("");
331e8a1faeSThomas Huth fw_cfg = pc_fw_cfg_init(s);
341e8a1faeSThomas Huth
351e8a1faeSThomas Huth qfw_cfg_get(fw_cfg, FW_CFG_SIGNATURE, buf, 4);
361e8a1faeSThomas Huth buf[4] = 0;
371e8a1faeSThomas Huth
381e8a1faeSThomas Huth g_assert_cmpstr(buf, ==, "QEMU");
391e8a1faeSThomas Huth pc_fw_cfg_uninit(fw_cfg);
401e8a1faeSThomas Huth qtest_quit(s);
411e8a1faeSThomas Huth }
421e8a1faeSThomas Huth
test_fw_cfg_id(void)431e8a1faeSThomas Huth static void test_fw_cfg_id(void)
441e8a1faeSThomas Huth {
451e8a1faeSThomas Huth QFWCFG *fw_cfg;
461e8a1faeSThomas Huth QTestState *s;
471e8a1faeSThomas Huth uint32_t id;
481e8a1faeSThomas Huth
491e8a1faeSThomas Huth s = qtest_init("");
501e8a1faeSThomas Huth fw_cfg = pc_fw_cfg_init(s);
511e8a1faeSThomas Huth
521e8a1faeSThomas Huth id = qfw_cfg_get_u32(fw_cfg, FW_CFG_ID);
531e8a1faeSThomas Huth g_assert((id == 1) ||
541e8a1faeSThomas Huth (id == 3));
551e8a1faeSThomas Huth pc_fw_cfg_uninit(fw_cfg);
561e8a1faeSThomas Huth qtest_quit(s);
571e8a1faeSThomas Huth }
581e8a1faeSThomas Huth
test_fw_cfg_uuid(void)591e8a1faeSThomas Huth static void test_fw_cfg_uuid(void)
601e8a1faeSThomas Huth {
611e8a1faeSThomas Huth QFWCFG *fw_cfg;
621e8a1faeSThomas Huth QTestState *s;
631e8a1faeSThomas Huth
641e8a1faeSThomas Huth uint8_t buf[16];
651e8a1faeSThomas Huth static const uint8_t uuid[16] = {
661e8a1faeSThomas Huth 0x46, 0x00, 0xcb, 0x32, 0x38, 0xec, 0x4b, 0x2f,
671e8a1faeSThomas Huth 0x8a, 0xcb, 0x81, 0xc6, 0xea, 0x54, 0xf2, 0xd8,
681e8a1faeSThomas Huth };
691e8a1faeSThomas Huth
701e8a1faeSThomas Huth s = qtest_init("-uuid 4600cb32-38ec-4b2f-8acb-81c6ea54f2d8");
711e8a1faeSThomas Huth fw_cfg = pc_fw_cfg_init(s);
721e8a1faeSThomas Huth
731e8a1faeSThomas Huth qfw_cfg_get(fw_cfg, FW_CFG_UUID, buf, 16);
741e8a1faeSThomas Huth g_assert(memcmp(buf, uuid, sizeof(buf)) == 0);
751e8a1faeSThomas Huth
761e8a1faeSThomas Huth pc_fw_cfg_uninit(fw_cfg);
771e8a1faeSThomas Huth qtest_quit(s);
781e8a1faeSThomas Huth
791e8a1faeSThomas Huth }
801e8a1faeSThomas Huth
test_fw_cfg_ram_size(void)811e8a1faeSThomas Huth static void test_fw_cfg_ram_size(void)
821e8a1faeSThomas Huth {
831e8a1faeSThomas Huth QFWCFG *fw_cfg;
841e8a1faeSThomas Huth QTestState *s;
851e8a1faeSThomas Huth
861e8a1faeSThomas Huth s = qtest_init("");
871e8a1faeSThomas Huth fw_cfg = pc_fw_cfg_init(s);
881e8a1faeSThomas Huth
891e8a1faeSThomas Huth g_assert_cmpint(qfw_cfg_get_u64(fw_cfg, FW_CFG_RAM_SIZE), ==, ram_size);
901e8a1faeSThomas Huth
911e8a1faeSThomas Huth pc_fw_cfg_uninit(fw_cfg);
921e8a1faeSThomas Huth qtest_quit(s);
931e8a1faeSThomas Huth }
941e8a1faeSThomas Huth
test_fw_cfg_nographic(void)951e8a1faeSThomas Huth static void test_fw_cfg_nographic(void)
961e8a1faeSThomas Huth {
971e8a1faeSThomas Huth QFWCFG *fw_cfg;
981e8a1faeSThomas Huth QTestState *s;
991e8a1faeSThomas Huth
1001e8a1faeSThomas Huth s = qtest_init("");
1011e8a1faeSThomas Huth fw_cfg = pc_fw_cfg_init(s);
1021e8a1faeSThomas Huth
1031e8a1faeSThomas Huth g_assert_cmpint(qfw_cfg_get_u16(fw_cfg, FW_CFG_NOGRAPHIC), ==, 0);
1041e8a1faeSThomas Huth
1051e8a1faeSThomas Huth pc_fw_cfg_uninit(fw_cfg);
1061e8a1faeSThomas Huth qtest_quit(s);
1071e8a1faeSThomas Huth }
1081e8a1faeSThomas Huth
test_fw_cfg_nb_cpus(void)1091e8a1faeSThomas Huth static void test_fw_cfg_nb_cpus(void)
1101e8a1faeSThomas Huth {
1111e8a1faeSThomas Huth QFWCFG *fw_cfg;
1121e8a1faeSThomas Huth QTestState *s;
1131e8a1faeSThomas Huth
1141e8a1faeSThomas Huth s = qtest_init("");
1151e8a1faeSThomas Huth fw_cfg = pc_fw_cfg_init(s);
1161e8a1faeSThomas Huth
1171e8a1faeSThomas Huth g_assert_cmpint(qfw_cfg_get_u16(fw_cfg, FW_CFG_NB_CPUS), ==, nb_cpus);
1181e8a1faeSThomas Huth
1191e8a1faeSThomas Huth pc_fw_cfg_uninit(fw_cfg);
1201e8a1faeSThomas Huth qtest_quit(s);
1211e8a1faeSThomas Huth }
1221e8a1faeSThomas Huth
test_fw_cfg_max_cpus(void)1231e8a1faeSThomas Huth static void test_fw_cfg_max_cpus(void)
1241e8a1faeSThomas Huth {
1251e8a1faeSThomas Huth QFWCFG *fw_cfg;
1261e8a1faeSThomas Huth QTestState *s;
1271e8a1faeSThomas Huth
1281e8a1faeSThomas Huth s = qtest_init("");
1291e8a1faeSThomas Huth fw_cfg = pc_fw_cfg_init(s);
1301e8a1faeSThomas Huth
1311e8a1faeSThomas Huth g_assert_cmpint(qfw_cfg_get_u16(fw_cfg, FW_CFG_MAX_CPUS), ==, max_cpus);
1321e8a1faeSThomas Huth pc_fw_cfg_uninit(fw_cfg);
1331e8a1faeSThomas Huth qtest_quit(s);
1341e8a1faeSThomas Huth }
1351e8a1faeSThomas Huth
test_fw_cfg_numa(void)1361e8a1faeSThomas Huth static void test_fw_cfg_numa(void)
1371e8a1faeSThomas Huth {
1381e8a1faeSThomas Huth QFWCFG *fw_cfg;
1391e8a1faeSThomas Huth QTestState *s;
1401e8a1faeSThomas Huth uint64_t *cpu_mask;
1411e8a1faeSThomas Huth uint64_t *node_mask;
1421e8a1faeSThomas Huth
1431e8a1faeSThomas Huth s = qtest_init("");
1441e8a1faeSThomas Huth fw_cfg = pc_fw_cfg_init(s);
1451e8a1faeSThomas Huth
1461e8a1faeSThomas Huth g_assert_cmpint(qfw_cfg_get_u64(fw_cfg, FW_CFG_NUMA), ==, nb_nodes);
1471e8a1faeSThomas Huth
1481e8a1faeSThomas Huth cpu_mask = g_new0(uint64_t, max_cpus);
1491e8a1faeSThomas Huth node_mask = g_new0(uint64_t, nb_nodes);
1501e8a1faeSThomas Huth
1511e8a1faeSThomas Huth qfw_cfg_read_data(fw_cfg, cpu_mask, sizeof(uint64_t) * max_cpus);
1521e8a1faeSThomas Huth qfw_cfg_read_data(fw_cfg, node_mask, sizeof(uint64_t) * nb_nodes);
1531e8a1faeSThomas Huth
1541e8a1faeSThomas Huth if (nb_nodes) {
1551e8a1faeSThomas Huth g_assert(cpu_mask[0] & 0x01);
1561e8a1faeSThomas Huth g_assert_cmpint(node_mask[0], ==, ram_size);
1571e8a1faeSThomas Huth }
1581e8a1faeSThomas Huth
1591e8a1faeSThomas Huth g_free(node_mask);
1601e8a1faeSThomas Huth g_free(cpu_mask);
1611e8a1faeSThomas Huth pc_fw_cfg_uninit(fw_cfg);
1621e8a1faeSThomas Huth qtest_quit(s);
1631e8a1faeSThomas Huth }
1641e8a1faeSThomas Huth
test_fw_cfg_boot_menu(void)1651e8a1faeSThomas Huth static void test_fw_cfg_boot_menu(void)
1661e8a1faeSThomas Huth {
1671e8a1faeSThomas Huth QFWCFG *fw_cfg;
1681e8a1faeSThomas Huth QTestState *s;
1691e8a1faeSThomas Huth
1701e8a1faeSThomas Huth s = qtest_init("");
1711e8a1faeSThomas Huth fw_cfg = pc_fw_cfg_init(s);
1721e8a1faeSThomas Huth
1731e8a1faeSThomas Huth g_assert_cmpint(qfw_cfg_get_u16(fw_cfg, FW_CFG_BOOT_MENU), ==, boot_menu);
1741e8a1faeSThomas Huth pc_fw_cfg_uninit(fw_cfg);
1751e8a1faeSThomas Huth qtest_quit(s);
1761e8a1faeSThomas Huth }
1771e8a1faeSThomas Huth
test_fw_cfg_reboot_timeout(void)1781e8a1faeSThomas Huth static void test_fw_cfg_reboot_timeout(void)
1791e8a1faeSThomas Huth {
1801e8a1faeSThomas Huth QFWCFG *fw_cfg;
1811e8a1faeSThomas Huth QTestState *s;
1821e8a1faeSThomas Huth uint32_t reboot_timeout = 0;
1831e8a1faeSThomas Huth size_t filesize;
1841e8a1faeSThomas Huth
1851e8a1faeSThomas Huth s = qtest_init("-boot reboot-timeout=15");
1861e8a1faeSThomas Huth fw_cfg = pc_fw_cfg_init(s);
1871e8a1faeSThomas Huth
1881e8a1faeSThomas Huth filesize = qfw_cfg_get_file(fw_cfg, "etc/boot-fail-wait",
1891e8a1faeSThomas Huth &reboot_timeout, sizeof(reboot_timeout));
1901e8a1faeSThomas Huth g_assert_cmpint(filesize, ==, sizeof(reboot_timeout));
1911e8a1faeSThomas Huth reboot_timeout = le32_to_cpu(reboot_timeout);
1921e8a1faeSThomas Huth g_assert_cmpint(reboot_timeout, ==, 15);
1931e8a1faeSThomas Huth pc_fw_cfg_uninit(fw_cfg);
1941e8a1faeSThomas Huth qtest_quit(s);
1951e8a1faeSThomas Huth }
1961e8a1faeSThomas Huth
test_fw_cfg_no_reboot_timeout(void)1971e8a1faeSThomas Huth static void test_fw_cfg_no_reboot_timeout(void)
1981e8a1faeSThomas Huth {
1991e8a1faeSThomas Huth QFWCFG *fw_cfg;
2001e8a1faeSThomas Huth QTestState *s;
2011e8a1faeSThomas Huth uint32_t reboot_timeout = 0;
2021e8a1faeSThomas Huth size_t filesize;
2031e8a1faeSThomas Huth
2041e8a1faeSThomas Huth /* Special value -1 means "don't reboot" */
2051e8a1faeSThomas Huth s = qtest_init("-boot reboot-timeout=-1");
2061e8a1faeSThomas Huth fw_cfg = pc_fw_cfg_init(s);
2071e8a1faeSThomas Huth
2081e8a1faeSThomas Huth filesize = qfw_cfg_get_file(fw_cfg, "etc/boot-fail-wait",
2091e8a1faeSThomas Huth &reboot_timeout, sizeof(reboot_timeout));
2101e8a1faeSThomas Huth g_assert_cmpint(filesize, ==, sizeof(reboot_timeout));
2111e8a1faeSThomas Huth reboot_timeout = le32_to_cpu(reboot_timeout);
2121e8a1faeSThomas Huth g_assert_cmpint(reboot_timeout, ==, UINT32_MAX);
2131e8a1faeSThomas Huth pc_fw_cfg_uninit(fw_cfg);
2141e8a1faeSThomas Huth qtest_quit(s);
2151e8a1faeSThomas Huth }
2161e8a1faeSThomas Huth
test_fw_cfg_splash_time(void)2171e8a1faeSThomas Huth static void test_fw_cfg_splash_time(void)
2181e8a1faeSThomas Huth {
2191e8a1faeSThomas Huth QFWCFG *fw_cfg;
2201e8a1faeSThomas Huth QTestState *s;
2211e8a1faeSThomas Huth uint16_t splash_time = 0;
2221e8a1faeSThomas Huth size_t filesize;
2231e8a1faeSThomas Huth
2241e8a1faeSThomas Huth s = qtest_init("-boot splash-time=12");
2251e8a1faeSThomas Huth fw_cfg = pc_fw_cfg_init(s);
2261e8a1faeSThomas Huth
2271e8a1faeSThomas Huth filesize = qfw_cfg_get_file(fw_cfg, "etc/boot-menu-wait",
2281e8a1faeSThomas Huth &splash_time, sizeof(splash_time));
2291e8a1faeSThomas Huth g_assert_cmpint(filesize, ==, sizeof(splash_time));
2301e8a1faeSThomas Huth splash_time = le16_to_cpu(splash_time);
2311e8a1faeSThomas Huth g_assert_cmpint(splash_time, ==, 12);
2321e8a1faeSThomas Huth pc_fw_cfg_uninit(fw_cfg);
2331e8a1faeSThomas Huth qtest_quit(s);
2341e8a1faeSThomas Huth }
2351e8a1faeSThomas Huth
main(int argc,char ** argv)2361e8a1faeSThomas Huth int main(int argc, char **argv)
2371e8a1faeSThomas Huth {
2381e8a1faeSThomas Huth g_test_init(&argc, &argv, NULL);
2391e8a1faeSThomas Huth
2401e8a1faeSThomas Huth qtest_add_func("fw_cfg/signature", test_fw_cfg_signature);
2411e8a1faeSThomas Huth qtest_add_func("fw_cfg/id", test_fw_cfg_id);
2421e8a1faeSThomas Huth qtest_add_func("fw_cfg/uuid", test_fw_cfg_uuid);
2431e8a1faeSThomas Huth qtest_add_func("fw_cfg/ram_size", test_fw_cfg_ram_size);
2441e8a1faeSThomas Huth qtest_add_func("fw_cfg/nographic", test_fw_cfg_nographic);
2451e8a1faeSThomas Huth qtest_add_func("fw_cfg/nb_cpus", test_fw_cfg_nb_cpus);
2461e8a1faeSThomas Huth #if 0
2471e8a1faeSThomas Huth qtest_add_func("fw_cfg/machine_id", test_fw_cfg_machine_id);
2481e8a1faeSThomas Huth qtest_add_func("fw_cfg/kernel", test_fw_cfg_kernel);
2491e8a1faeSThomas Huth qtest_add_func("fw_cfg/initrd", test_fw_cfg_initrd);
2501e8a1faeSThomas Huth qtest_add_func("fw_cfg/boot_device", test_fw_cfg_boot_device);
2511e8a1faeSThomas Huth #endif
2521e8a1faeSThomas Huth qtest_add_func("fw_cfg/max_cpus", test_fw_cfg_max_cpus);
2531e8a1faeSThomas Huth qtest_add_func("fw_cfg/numa", test_fw_cfg_numa);
2541e8a1faeSThomas Huth qtest_add_func("fw_cfg/boot_menu", test_fw_cfg_boot_menu);
2551e8a1faeSThomas Huth qtest_add_func("fw_cfg/reboot_timeout", test_fw_cfg_reboot_timeout);
2561e8a1faeSThomas Huth qtest_add_func("fw_cfg/no_reboot_timeout", test_fw_cfg_no_reboot_timeout);
2571e8a1faeSThomas Huth qtest_add_func("fw_cfg/splash_time", test_fw_cfg_splash_time);
2581e8a1faeSThomas Huth
2591e8a1faeSThomas Huth return g_test_run();
2601e8a1faeSThomas Huth }
261