1 /* 2 * qtest fw_cfg test case 3 * 4 * Copyright IBM, Corp. 2012-2013 5 * 6 * Authors: 7 * Anthony Liguori <aliguori@us.ibm.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 13 #include "qemu/osdep.h" 14 15 #include "libqtest.h" 16 #include "standard-headers/linux/qemu_fw_cfg.h" 17 #include "libqos/fw_cfg.h" 18 19 static uint64_t ram_size = 128 << 20; 20 static uint16_t nb_cpus = 1; 21 static uint16_t max_cpus = 1; 22 static uint64_t nb_nodes = 0; 23 static uint16_t boot_menu = 0; 24 25 static void test_fw_cfg_signature(void) 26 { 27 QFWCFG *fw_cfg; 28 QTestState *s; 29 char buf[5]; 30 31 s = qtest_init(""); 32 fw_cfg = pc_fw_cfg_init(s); 33 34 qfw_cfg_get(fw_cfg, FW_CFG_SIGNATURE, buf, 4); 35 buf[4] = 0; 36 37 g_assert_cmpstr(buf, ==, "QEMU"); 38 pc_fw_cfg_uninit(fw_cfg); 39 qtest_quit(s); 40 } 41 42 static void test_fw_cfg_id(void) 43 { 44 QFWCFG *fw_cfg; 45 QTestState *s; 46 uint32_t id; 47 48 s = qtest_init(""); 49 fw_cfg = pc_fw_cfg_init(s); 50 51 id = qfw_cfg_get_u32(fw_cfg, FW_CFG_ID); 52 g_assert((id == 1) || 53 (id == 3)); 54 pc_fw_cfg_uninit(fw_cfg); 55 qtest_quit(s); 56 } 57 58 static void test_fw_cfg_uuid(void) 59 { 60 QFWCFG *fw_cfg; 61 QTestState *s; 62 63 uint8_t buf[16]; 64 static const uint8_t uuid[16] = { 65 0x46, 0x00, 0xcb, 0x32, 0x38, 0xec, 0x4b, 0x2f, 66 0x8a, 0xcb, 0x81, 0xc6, 0xea, 0x54, 0xf2, 0xd8, 67 }; 68 69 s = qtest_init("-uuid 4600cb32-38ec-4b2f-8acb-81c6ea54f2d8"); 70 fw_cfg = pc_fw_cfg_init(s); 71 72 qfw_cfg_get(fw_cfg, FW_CFG_UUID, buf, 16); 73 g_assert(memcmp(buf, uuid, sizeof(buf)) == 0); 74 75 pc_fw_cfg_uninit(fw_cfg); 76 qtest_quit(s); 77 78 } 79 80 static void test_fw_cfg_ram_size(void) 81 { 82 QFWCFG *fw_cfg; 83 QTestState *s; 84 85 s = qtest_init(""); 86 fw_cfg = pc_fw_cfg_init(s); 87 88 g_assert_cmpint(qfw_cfg_get_u64(fw_cfg, FW_CFG_RAM_SIZE), ==, ram_size); 89 90 pc_fw_cfg_uninit(fw_cfg); 91 qtest_quit(s); 92 } 93 94 static void test_fw_cfg_nographic(void) 95 { 96 QFWCFG *fw_cfg; 97 QTestState *s; 98 99 s = qtest_init(""); 100 fw_cfg = pc_fw_cfg_init(s); 101 102 g_assert_cmpint(qfw_cfg_get_u16(fw_cfg, FW_CFG_NOGRAPHIC), ==, 0); 103 104 pc_fw_cfg_uninit(fw_cfg); 105 qtest_quit(s); 106 } 107 108 static void test_fw_cfg_nb_cpus(void) 109 { 110 QFWCFG *fw_cfg; 111 QTestState *s; 112 113 s = qtest_init(""); 114 fw_cfg = pc_fw_cfg_init(s); 115 116 g_assert_cmpint(qfw_cfg_get_u16(fw_cfg, FW_CFG_NB_CPUS), ==, nb_cpus); 117 118 pc_fw_cfg_uninit(fw_cfg); 119 qtest_quit(s); 120 } 121 122 static void test_fw_cfg_max_cpus(void) 123 { 124 QFWCFG *fw_cfg; 125 QTestState *s; 126 127 s = qtest_init(""); 128 fw_cfg = pc_fw_cfg_init(s); 129 130 g_assert_cmpint(qfw_cfg_get_u16(fw_cfg, FW_CFG_MAX_CPUS), ==, max_cpus); 131 pc_fw_cfg_uninit(fw_cfg); 132 qtest_quit(s); 133 } 134 135 static void test_fw_cfg_numa(void) 136 { 137 QFWCFG *fw_cfg; 138 QTestState *s; 139 uint64_t *cpu_mask; 140 uint64_t *node_mask; 141 142 s = qtest_init(""); 143 fw_cfg = pc_fw_cfg_init(s); 144 145 g_assert_cmpint(qfw_cfg_get_u64(fw_cfg, FW_CFG_NUMA), ==, nb_nodes); 146 147 cpu_mask = g_new0(uint64_t, max_cpus); 148 node_mask = g_new0(uint64_t, nb_nodes); 149 150 qfw_cfg_read_data(fw_cfg, cpu_mask, sizeof(uint64_t) * max_cpus); 151 qfw_cfg_read_data(fw_cfg, node_mask, sizeof(uint64_t) * nb_nodes); 152 153 if (nb_nodes) { 154 g_assert(cpu_mask[0] & 0x01); 155 g_assert_cmpint(node_mask[0], ==, ram_size); 156 } 157 158 g_free(node_mask); 159 g_free(cpu_mask); 160 pc_fw_cfg_uninit(fw_cfg); 161 qtest_quit(s); 162 } 163 164 static void test_fw_cfg_boot_menu(void) 165 { 166 QFWCFG *fw_cfg; 167 QTestState *s; 168 169 s = qtest_init(""); 170 fw_cfg = pc_fw_cfg_init(s); 171 172 g_assert_cmpint(qfw_cfg_get_u16(fw_cfg, FW_CFG_BOOT_MENU), ==, boot_menu); 173 pc_fw_cfg_uninit(fw_cfg); 174 qtest_quit(s); 175 } 176 177 int main(int argc, char **argv) 178 { 179 g_test_init(&argc, &argv, NULL); 180 181 qtest_add_func("fw_cfg/signature", test_fw_cfg_signature); 182 qtest_add_func("fw_cfg/id", test_fw_cfg_id); 183 qtest_add_func("fw_cfg/uuid", test_fw_cfg_uuid); 184 qtest_add_func("fw_cfg/ram_size", test_fw_cfg_ram_size); 185 qtest_add_func("fw_cfg/nographic", test_fw_cfg_nographic); 186 qtest_add_func("fw_cfg/nb_cpus", test_fw_cfg_nb_cpus); 187 #if 0 188 qtest_add_func("fw_cfg/machine_id", test_fw_cfg_machine_id); 189 qtest_add_func("fw_cfg/kernel", test_fw_cfg_kernel); 190 qtest_add_func("fw_cfg/initrd", test_fw_cfg_initrd); 191 qtest_add_func("fw_cfg/boot_device", test_fw_cfg_boot_device); 192 #endif 193 qtest_add_func("fw_cfg/max_cpus", test_fw_cfg_max_cpus); 194 qtest_add_func("fw_cfg/numa", test_fw_cfg_numa); 195 qtest_add_func("fw_cfg/boot_menu", test_fw_cfg_boot_menu); 196 197 return g_test_run(); 198 } 199