Anthony Liguori | bf2a38d | 2013-04-16 09:45:21 -0500 | [diff] [blame] | 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 | |
Peter Maydell | 681c28a | 2016-02-08 18:08:51 +0000 | [diff] [blame] | 13 | #include "qemu/osdep.h" |
Anthony Liguori | bf2a38d | 2013-04-16 09:45:21 -0500 | [diff] [blame] | 14 | |
Stefan Hajnoczi | 91f32b0 | 2014-02-08 11:41:07 +0100 | [diff] [blame] | 15 | #include "libqtest.h" |
Marc-André Lureau | 5be5df7 | 2018-08-17 17:59:10 +0200 | [diff] [blame] | 16 | #include "standard-headers/linux/qemu_fw_cfg.h" |
Stefan Hajnoczi | 91f32b0 | 2014-02-08 11:41:07 +0100 | [diff] [blame] | 17 | #include "libqos/fw_cfg.h" |
Li Qiang | b41e912 | 2019-04-24 07:06:42 -0700 | [diff] [blame] | 18 | #include "qemu/bswap.h" |
Stefan Hajnoczi | 91f32b0 | 2014-02-08 11:41:07 +0100 | [diff] [blame] | 19 | |
Anthony Liguori | bf2a38d | 2013-04-16 09:45:21 -0500 | [diff] [blame] | 20 | static uint64_t ram_size = 128 << 20; |
| 21 | static uint16_t nb_cpus = 1; |
| 22 | static uint16_t max_cpus = 1; |
| 23 | static uint64_t nb_nodes = 0; |
| 24 | static uint16_t boot_menu = 0; |
Anthony Liguori | bf2a38d | 2013-04-16 09:45:21 -0500 | [diff] [blame] | 25 | |
| 26 | static void test_fw_cfg_signature(void) |
| 27 | { |
Li Qiang | 7a44091 | 2019-05-18 21:35:10 +0200 | [diff] [blame] | 28 | QFWCFG *fw_cfg; |
| 29 | QTestState *s; |
Anthony Liguori | bf2a38d | 2013-04-16 09:45:21 -0500 | [diff] [blame] | 30 | char buf[5]; |
| 31 | |
Li Qiang | 7a44091 | 2019-05-18 21:35:10 +0200 | [diff] [blame] | 32 | s = qtest_init(""); |
| 33 | fw_cfg = pc_fw_cfg_init(s); |
| 34 | |
Anthony Liguori | bf2a38d | 2013-04-16 09:45:21 -0500 | [diff] [blame] | 35 | qfw_cfg_get(fw_cfg, FW_CFG_SIGNATURE, buf, 4); |
| 36 | buf[4] = 0; |
| 37 | |
| 38 | g_assert_cmpstr(buf, ==, "QEMU"); |
Li Qiang | 7a44091 | 2019-05-18 21:35:10 +0200 | [diff] [blame] | 39 | pc_fw_cfg_uninit(fw_cfg); |
| 40 | qtest_quit(s); |
Anthony Liguori | bf2a38d | 2013-04-16 09:45:21 -0500 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | static void test_fw_cfg_id(void) |
| 44 | { |
Li Qiang | 7a44091 | 2019-05-18 21:35:10 +0200 | [diff] [blame] | 45 | QFWCFG *fw_cfg; |
| 46 | QTestState *s; |
| 47 | uint32_t id; |
| 48 | |
| 49 | s = qtest_init(""); |
| 50 | fw_cfg = pc_fw_cfg_init(s); |
| 51 | |
| 52 | id = qfw_cfg_get_u32(fw_cfg, FW_CFG_ID); |
Marc Marí | a4c0d1d | 2015-10-08 17:02:55 +0200 | [diff] [blame] | 53 | g_assert((id == 1) || |
| 54 | (id == 3)); |
Li Qiang | 7a44091 | 2019-05-18 21:35:10 +0200 | [diff] [blame] | 55 | pc_fw_cfg_uninit(fw_cfg); |
| 56 | qtest_quit(s); |
Anthony Liguori | bf2a38d | 2013-04-16 09:45:21 -0500 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | static void test_fw_cfg_uuid(void) |
| 60 | { |
Li Qiang | 7a44091 | 2019-05-18 21:35:10 +0200 | [diff] [blame] | 61 | QFWCFG *fw_cfg; |
| 62 | QTestState *s; |
| 63 | |
Anthony Liguori | bf2a38d | 2013-04-16 09:45:21 -0500 | [diff] [blame] | 64 | uint8_t buf[16]; |
| 65 | static const uint8_t uuid[16] = { |
| 66 | 0x46, 0x00, 0xcb, 0x32, 0x38, 0xec, 0x4b, 0x2f, |
| 67 | 0x8a, 0xcb, 0x81, 0xc6, 0xea, 0x54, 0xf2, 0xd8, |
| 68 | }; |
| 69 | |
Li Qiang | 7a44091 | 2019-05-18 21:35:10 +0200 | [diff] [blame] | 70 | s = qtest_init("-uuid 4600cb32-38ec-4b2f-8acb-81c6ea54f2d8"); |
| 71 | fw_cfg = pc_fw_cfg_init(s); |
| 72 | |
Anthony Liguori | bf2a38d | 2013-04-16 09:45:21 -0500 | [diff] [blame] | 73 | qfw_cfg_get(fw_cfg, FW_CFG_UUID, buf, 16); |
| 74 | g_assert(memcmp(buf, uuid, sizeof(buf)) == 0); |
Li Qiang | 7a44091 | 2019-05-18 21:35:10 +0200 | [diff] [blame] | 75 | |
| 76 | pc_fw_cfg_uninit(fw_cfg); |
| 77 | qtest_quit(s); |
| 78 | |
Anthony Liguori | bf2a38d | 2013-04-16 09:45:21 -0500 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | static void test_fw_cfg_ram_size(void) |
| 82 | { |
Li Qiang | 7a44091 | 2019-05-18 21:35:10 +0200 | [diff] [blame] | 83 | QFWCFG *fw_cfg; |
| 84 | QTestState *s; |
| 85 | |
| 86 | s = qtest_init(""); |
| 87 | fw_cfg = pc_fw_cfg_init(s); |
| 88 | |
Anthony Liguori | bf2a38d | 2013-04-16 09:45:21 -0500 | [diff] [blame] | 89 | g_assert_cmpint(qfw_cfg_get_u64(fw_cfg, FW_CFG_RAM_SIZE), ==, ram_size); |
Li Qiang | 7a44091 | 2019-05-18 21:35:10 +0200 | [diff] [blame] | 90 | |
| 91 | pc_fw_cfg_uninit(fw_cfg); |
| 92 | qtest_quit(s); |
Anthony Liguori | bf2a38d | 2013-04-16 09:45:21 -0500 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | static void test_fw_cfg_nographic(void) |
| 96 | { |
Li Qiang | 7a44091 | 2019-05-18 21:35:10 +0200 | [diff] [blame] | 97 | QFWCFG *fw_cfg; |
| 98 | QTestState *s; |
| 99 | |
| 100 | s = qtest_init(""); |
| 101 | fw_cfg = pc_fw_cfg_init(s); |
| 102 | |
Anthony Liguori | bf2a38d | 2013-04-16 09:45:21 -0500 | [diff] [blame] | 103 | g_assert_cmpint(qfw_cfg_get_u16(fw_cfg, FW_CFG_NOGRAPHIC), ==, 0); |
Li Qiang | 7a44091 | 2019-05-18 21:35:10 +0200 | [diff] [blame] | 104 | |
| 105 | pc_fw_cfg_uninit(fw_cfg); |
| 106 | qtest_quit(s); |
Anthony Liguori | bf2a38d | 2013-04-16 09:45:21 -0500 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static void test_fw_cfg_nb_cpus(void) |
| 110 | { |
Li Qiang | 7a44091 | 2019-05-18 21:35:10 +0200 | [diff] [blame] | 111 | QFWCFG *fw_cfg; |
| 112 | QTestState *s; |
| 113 | |
| 114 | s = qtest_init(""); |
| 115 | fw_cfg = pc_fw_cfg_init(s); |
| 116 | |
Anthony Liguori | bf2a38d | 2013-04-16 09:45:21 -0500 | [diff] [blame] | 117 | g_assert_cmpint(qfw_cfg_get_u16(fw_cfg, FW_CFG_NB_CPUS), ==, nb_cpus); |
Li Qiang | 7a44091 | 2019-05-18 21:35:10 +0200 | [diff] [blame] | 118 | |
| 119 | pc_fw_cfg_uninit(fw_cfg); |
| 120 | qtest_quit(s); |
Anthony Liguori | bf2a38d | 2013-04-16 09:45:21 -0500 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static void test_fw_cfg_max_cpus(void) |
| 124 | { |
Li Qiang | 7a44091 | 2019-05-18 21:35:10 +0200 | [diff] [blame] | 125 | QFWCFG *fw_cfg; |
| 126 | QTestState *s; |
| 127 | |
| 128 | s = qtest_init(""); |
| 129 | fw_cfg = pc_fw_cfg_init(s); |
| 130 | |
Anthony Liguori | bf2a38d | 2013-04-16 09:45:21 -0500 | [diff] [blame] | 131 | g_assert_cmpint(qfw_cfg_get_u16(fw_cfg, FW_CFG_MAX_CPUS), ==, max_cpus); |
Li Qiang | 7a44091 | 2019-05-18 21:35:10 +0200 | [diff] [blame] | 132 | pc_fw_cfg_uninit(fw_cfg); |
| 133 | qtest_quit(s); |
Anthony Liguori | bf2a38d | 2013-04-16 09:45:21 -0500 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | static void test_fw_cfg_numa(void) |
| 137 | { |
Li Qiang | 7a44091 | 2019-05-18 21:35:10 +0200 | [diff] [blame] | 138 | QFWCFG *fw_cfg; |
| 139 | QTestState *s; |
Anthony Liguori | bf2a38d | 2013-04-16 09:45:21 -0500 | [diff] [blame] | 140 | uint64_t *cpu_mask; |
| 141 | uint64_t *node_mask; |
| 142 | |
Li Qiang | 7a44091 | 2019-05-18 21:35:10 +0200 | [diff] [blame] | 143 | s = qtest_init(""); |
| 144 | fw_cfg = pc_fw_cfg_init(s); |
| 145 | |
Anthony Liguori | bf2a38d | 2013-04-16 09:45:21 -0500 | [diff] [blame] | 146 | g_assert_cmpint(qfw_cfg_get_u64(fw_cfg, FW_CFG_NUMA), ==, nb_nodes); |
| 147 | |
Marc-André Lureau | 790bbb9 | 2017-10-06 20:49:56 -0300 | [diff] [blame] | 148 | cpu_mask = g_new0(uint64_t, max_cpus); |
| 149 | node_mask = g_new0(uint64_t, nb_nodes); |
Anthony Liguori | bf2a38d | 2013-04-16 09:45:21 -0500 | [diff] [blame] | 150 | |
| 151 | qfw_cfg_read_data(fw_cfg, cpu_mask, sizeof(uint64_t) * max_cpus); |
| 152 | qfw_cfg_read_data(fw_cfg, node_mask, sizeof(uint64_t) * nb_nodes); |
| 153 | |
| 154 | if (nb_nodes) { |
| 155 | g_assert(cpu_mask[0] & 0x01); |
| 156 | g_assert_cmpint(node_mask[0], ==, ram_size); |
| 157 | } |
| 158 | |
| 159 | g_free(node_mask); |
| 160 | g_free(cpu_mask); |
Li Qiang | 7a44091 | 2019-05-18 21:35:10 +0200 | [diff] [blame] | 161 | pc_fw_cfg_uninit(fw_cfg); |
| 162 | qtest_quit(s); |
Anthony Liguori | bf2a38d | 2013-04-16 09:45:21 -0500 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | static void test_fw_cfg_boot_menu(void) |
| 166 | { |
Li Qiang | 7a44091 | 2019-05-18 21:35:10 +0200 | [diff] [blame] | 167 | QFWCFG *fw_cfg; |
| 168 | QTestState *s; |
| 169 | |
| 170 | s = qtest_init(""); |
| 171 | fw_cfg = pc_fw_cfg_init(s); |
| 172 | |
Anthony Liguori | bf2a38d | 2013-04-16 09:45:21 -0500 | [diff] [blame] | 173 | g_assert_cmpint(qfw_cfg_get_u16(fw_cfg, FW_CFG_BOOT_MENU), ==, boot_menu); |
Li Qiang | 7a44091 | 2019-05-18 21:35:10 +0200 | [diff] [blame] | 174 | pc_fw_cfg_uninit(fw_cfg); |
| 175 | qtest_quit(s); |
Anthony Liguori | bf2a38d | 2013-04-16 09:45:21 -0500 | [diff] [blame] | 176 | } |
| 177 | |
Li Qiang | b41e912 | 2019-04-24 07:06:42 -0700 | [diff] [blame] | 178 | static void test_fw_cfg_reboot_timeout(void) |
| 179 | { |
| 180 | QFWCFG *fw_cfg; |
| 181 | QTestState *s; |
| 182 | uint32_t reboot_timeout = 0; |
| 183 | size_t filesize; |
| 184 | |
| 185 | s = qtest_init("-boot reboot-timeout=15"); |
| 186 | fw_cfg = pc_fw_cfg_init(s); |
| 187 | |
| 188 | filesize = qfw_cfg_get_file(fw_cfg, "etc/boot-fail-wait", |
| 189 | &reboot_timeout, sizeof(reboot_timeout)); |
| 190 | g_assert_cmpint(filesize, ==, sizeof(reboot_timeout)); |
| 191 | reboot_timeout = le32_to_cpu(reboot_timeout); |
| 192 | g_assert_cmpint(reboot_timeout, ==, 15); |
| 193 | pc_fw_cfg_uninit(fw_cfg); |
| 194 | qtest_quit(s); |
| 195 | } |
| 196 | |
Li Qiang | 3ae9dd1 | 2019-04-24 07:06:43 -0700 | [diff] [blame] | 197 | static void test_fw_cfg_splash_time(void) |
| 198 | { |
| 199 | QFWCFG *fw_cfg; |
| 200 | QTestState *s; |
| 201 | uint16_t splash_time = 0; |
| 202 | size_t filesize; |
| 203 | |
| 204 | s = qtest_init("-boot splash-time=12"); |
| 205 | fw_cfg = pc_fw_cfg_init(s); |
| 206 | |
| 207 | filesize = qfw_cfg_get_file(fw_cfg, "etc/boot-menu-wait", |
| 208 | &splash_time, sizeof(splash_time)); |
| 209 | g_assert_cmpint(filesize, ==, sizeof(splash_time)); |
| 210 | splash_time = le16_to_cpu(splash_time); |
| 211 | g_assert_cmpint(splash_time, ==, 12); |
| 212 | pc_fw_cfg_uninit(fw_cfg); |
| 213 | qtest_quit(s); |
| 214 | } |
| 215 | |
Anthony Liguori | bf2a38d | 2013-04-16 09:45:21 -0500 | [diff] [blame] | 216 | int main(int argc, char **argv) |
| 217 | { |
Anthony Liguori | bf2a38d | 2013-04-16 09:45:21 -0500 | [diff] [blame] | 218 | g_test_init(&argc, &argv, NULL); |
| 219 | |
Andreas Färber | 53850b8 | 2015-03-19 18:35:23 +0100 | [diff] [blame] | 220 | qtest_add_func("fw_cfg/signature", test_fw_cfg_signature); |
| 221 | qtest_add_func("fw_cfg/id", test_fw_cfg_id); |
| 222 | qtest_add_func("fw_cfg/uuid", test_fw_cfg_uuid); |
| 223 | qtest_add_func("fw_cfg/ram_size", test_fw_cfg_ram_size); |
| 224 | qtest_add_func("fw_cfg/nographic", test_fw_cfg_nographic); |
| 225 | qtest_add_func("fw_cfg/nb_cpus", test_fw_cfg_nb_cpus); |
Anthony Liguori | bf2a38d | 2013-04-16 09:45:21 -0500 | [diff] [blame] | 226 | #if 0 |
Andreas Färber | 53850b8 | 2015-03-19 18:35:23 +0100 | [diff] [blame] | 227 | qtest_add_func("fw_cfg/machine_id", test_fw_cfg_machine_id); |
| 228 | qtest_add_func("fw_cfg/kernel", test_fw_cfg_kernel); |
| 229 | qtest_add_func("fw_cfg/initrd", test_fw_cfg_initrd); |
| 230 | qtest_add_func("fw_cfg/boot_device", test_fw_cfg_boot_device); |
Anthony Liguori | bf2a38d | 2013-04-16 09:45:21 -0500 | [diff] [blame] | 231 | #endif |
Andreas Färber | 53850b8 | 2015-03-19 18:35:23 +0100 | [diff] [blame] | 232 | qtest_add_func("fw_cfg/max_cpus", test_fw_cfg_max_cpus); |
| 233 | qtest_add_func("fw_cfg/numa", test_fw_cfg_numa); |
| 234 | qtest_add_func("fw_cfg/boot_menu", test_fw_cfg_boot_menu); |
Li Qiang | b41e912 | 2019-04-24 07:06:42 -0700 | [diff] [blame] | 235 | qtest_add_func("fw_cfg/reboot_timeout", test_fw_cfg_reboot_timeout); |
Li Qiang | 3ae9dd1 | 2019-04-24 07:06:43 -0700 | [diff] [blame] | 236 | qtest_add_func("fw_cfg/splash_time", test_fw_cfg_splash_time); |
Anthony Liguori | bf2a38d | 2013-04-16 09:45:21 -0500 | [diff] [blame] | 237 | |
Li Qiang | 7a44091 | 2019-05-18 21:35:10 +0200 | [diff] [blame] | 238 | return g_test_run(); |
Anthony Liguori | bf2a38d | 2013-04-16 09:45:21 -0500 | [diff] [blame] | 239 | } |