]> git.proxmox.com Git - qemu.git/blame - qemu-config.c
Add cpu model configuration support..
[qemu.git] / qemu-config.c
CommitLineData
7282a033
GH
1#include "qemu-common.h"
2#include "qemu-option.h"
3#include "qemu-config.h"
a861c453 4#include "sysemu.h"
d0fef6fb 5#include "hw/qdev.h"
7282a033
GH
6
7QemuOptsList qemu_drive_opts = {
8 .name = "drive",
72cf2d4f 9 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
7282a033
GH
10 .desc = {
11 {
12 .name = "bus",
13 .type = QEMU_OPT_NUMBER,
14 .help = "bus number",
15 },{
16 .name = "unit",
17 .type = QEMU_OPT_NUMBER,
18 .help = "unit number (i.e. lun for scsi)",
19 },{
20 .name = "if",
21 .type = QEMU_OPT_STRING,
22 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
23 },{
24 .name = "index",
25 .type = QEMU_OPT_NUMBER,
26 },{
27 .name = "cyls",
28 .type = QEMU_OPT_NUMBER,
29 .help = "number of cylinders (ide disk geometry)",
30 },{
31 .name = "heads",
32 .type = QEMU_OPT_NUMBER,
33 .help = "number of heads (ide disk geometry)",
34 },{
35 .name = "secs",
36 .type = QEMU_OPT_NUMBER,
37 .help = "number of sectors (ide disk geometry)",
38 },{
39 .name = "trans",
40 .type = QEMU_OPT_STRING,
41 .help = "chs translation (auto, lba. none)",
42 },{
43 .name = "media",
44 .type = QEMU_OPT_STRING,
45 .help = "media type (disk, cdrom)",
46 },{
47 .name = "snapshot",
48 .type = QEMU_OPT_BOOL,
49 },{
50 .name = "file",
51 .type = QEMU_OPT_STRING,
52 .help = "disk image",
53 },{
54 .name = "cache",
55 .type = QEMU_OPT_STRING,
56 .help = "host cache usage (none, writeback, writethrough)",
5c6c3a6c
CH
57 },{
58 .name = "aio",
59 .type = QEMU_OPT_STRING,
60 .help = "host AIO implementation (threads, native)",
7282a033
GH
61 },{
62 .name = "format",
63 .type = QEMU_OPT_STRING,
64 .help = "disk format (raw, qcow2, ...)",
65 },{
66 .name = "serial",
67 .type = QEMU_OPT_STRING,
e9b2e818
KW
68 },{
69 .name = "rerror",
70 .type = QEMU_OPT_STRING,
7282a033
GH
71 },{
72 .name = "werror",
73 .type = QEMU_OPT_STRING,
74 },{
75 .name = "addr",
76 .type = QEMU_OPT_STRING,
77 .help = "pci address (virtio only)",
59f2689d
NS
78 },{
79 .name = "readonly",
80 .type = QEMU_OPT_BOOL,
7282a033
GH
81 },
82 { /* end if list */ }
83 },
84};
85
191bc01b
GH
86QemuOptsList qemu_chardev_opts = {
87 .name = "chardev",
72cf2d4f 88 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
191bc01b 89 .desc = {
7d31544f
GH
90 {
91 .name = "backend",
92 .type = QEMU_OPT_STRING,
93 },{
94 .name = "path",
95 .type = QEMU_OPT_STRING,
aeb2c47a
GH
96 },{
97 .name = "host",
98 .type = QEMU_OPT_STRING,
99 },{
100 .name = "port",
101 .type = QEMU_OPT_STRING,
7e1b35b4
GH
102 },{
103 .name = "localaddr",
104 .type = QEMU_OPT_STRING,
105 },{
106 .name = "localport",
107 .type = QEMU_OPT_STRING,
aeb2c47a
GH
108 },{
109 .name = "to",
110 .type = QEMU_OPT_NUMBER,
111 },{
112 .name = "ipv4",
113 .type = QEMU_OPT_BOOL,
114 },{
115 .name = "ipv6",
116 .type = QEMU_OPT_BOOL,
117 },{
118 .name = "wait",
119 .type = QEMU_OPT_BOOL,
120 },{
121 .name = "server",
122 .type = QEMU_OPT_BOOL,
123 },{
124 .name = "delay",
125 .type = QEMU_OPT_BOOL,
126 },{
127 .name = "telnet",
128 .type = QEMU_OPT_BOOL,
6ea314d9
GH
129 },{
130 .name = "width",
131 .type = QEMU_OPT_NUMBER,
132 },{
133 .name = "height",
134 .type = QEMU_OPT_NUMBER,
135 },{
136 .name = "cols",
137 .type = QEMU_OPT_NUMBER,
138 },{
139 .name = "rows",
140 .type = QEMU_OPT_NUMBER,
c845f401
GH
141 },{
142 .name = "mux",
143 .type = QEMU_OPT_BOOL,
5989020b
KK
144 },{
145 .name = "signal",
146 .type = QEMU_OPT_BOOL,
7d31544f 147 },
191bc01b
GH
148 { /* end if list */ }
149 },
150};
151
f31d07d1
GH
152QemuOptsList qemu_device_opts = {
153 .name = "device",
72cf2d4f 154 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
f31d07d1
GH
155 .desc = {
156 /*
157 * no elements => accept any
158 * sanity checking will happen later
159 * when setting device properties
160 */
161 { /* end if list */ }
162 },
163};
164
a1ea458f
MM
165QemuOptsList qemu_netdev_opts = {
166 .name = "netdev",
167 .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
168 .desc = {
169 /*
170 * no elements => accept any params
171 * validation will happen later
172 */
173 { /* end of list */ }
174 },
175};
176
8119b33d
MM
177QemuOptsList qemu_net_opts = {
178 .name = "net",
179 .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
180 .desc = {
181 /*
182 * no elements => accept any params
183 * validation will happen later
184 */
185 { /* end of list */ }
186 },
187};
188
1ed2fc1f
JK
189QemuOptsList qemu_rtc_opts = {
190 .name = "rtc",
191 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
192 .desc = {
193 {
194 .name = "base",
195 .type = QEMU_OPT_STRING,
6875204c
JK
196 },{
197 .name = "clock",
198 .type = QEMU_OPT_STRING,
1ed2fc1f
JK
199#ifdef TARGET_I386
200 },{
201 .name = "driftfix",
202 .type = QEMU_OPT_STRING,
203#endif
204 },
205 { /* end if list */ }
206 },
207};
208
d0fef6fb
GH
209QemuOptsList qemu_global_opts = {
210 .name = "global",
211 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
212 .desc = {
213 {
214 .name = "driver",
215 .type = QEMU_OPT_STRING,
216 },{
217 .name = "property",
218 .type = QEMU_OPT_STRING,
219 },{
220 .name = "value",
221 .type = QEMU_OPT_STRING,
222 },
223 { /* end if list */ }
224 },
225};
226
88589343
GH
227QemuOptsList qemu_mon_opts = {
228 .name = "mon",
229 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
230 .desc = {
231 {
232 .name = "mode",
233 .type = QEMU_OPT_STRING,
234 },{
235 .name = "chardev",
236 .type = QEMU_OPT_STRING,
237 },{
238 .name = "default",
239 .type = QEMU_OPT_BOOL,
240 },
241 { /* end if list */ }
242 },
243};
244
b5ec5ce0 245QemuOptsList qemu_cpudef_opts = {
246 .name = "cpudef",
247 .head = QTAILQ_HEAD_INITIALIZER(qemu_cpudef_opts.head),
248 .desc = {
249 {
250 .name = "name",
251 .type = QEMU_OPT_STRING,
252 },{
253 .name = "level",
254 .type = QEMU_OPT_NUMBER,
255 },{
256 .name = "vendor",
257 .type = QEMU_OPT_STRING,
258 },{
259 .name = "family",
260 .type = QEMU_OPT_NUMBER,
261 },{
262 .name = "model",
263 .type = QEMU_OPT_NUMBER,
264 },{
265 .name = "stepping",
266 .type = QEMU_OPT_NUMBER,
267 },{
268 .name = "feature_edx", /* cpuid 0000_0001.edx */
269 .type = QEMU_OPT_STRING,
270 },{
271 .name = "feature_ecx", /* cpuid 0000_0001.ecx */
272 .type = QEMU_OPT_STRING,
273 },{
274 .name = "extfeature_edx", /* cpuid 8000_0001.edx */
275 .type = QEMU_OPT_STRING,
276 },{
277 .name = "extfeature_ecx", /* cpuid 8000_0001.ecx */
278 .type = QEMU_OPT_STRING,
279 },{
280 .name = "xlevel",
281 .type = QEMU_OPT_NUMBER,
282 },{
283 .name = "model_id",
284 .type = QEMU_OPT_STRING,
285 },{
286 .name = "vendor_override",
287 .type = QEMU_OPT_NUMBER,
288 },
289 { /* end of list */ }
290 },
291};
292
d058fe03
GH
293static QemuOptsList *lists[] = {
294 &qemu_drive_opts,
191bc01b 295 &qemu_chardev_opts,
f31d07d1 296 &qemu_device_opts,
a1ea458f 297 &qemu_netdev_opts,
8119b33d 298 &qemu_net_opts,
5fdfbf7e 299 &qemu_rtc_opts,
d0fef6fb 300 &qemu_global_opts,
88589343 301 &qemu_mon_opts,
b5ec5ce0 302 &qemu_cpudef_opts,
d058fe03
GH
303 NULL,
304};
305
ddc97855
GH
306static QemuOptsList *find_list(const char *group)
307{
308 int i;
309
310 for (i = 0; lists[i] != NULL; i++) {
311 if (strcmp(lists[i]->name, group) == 0)
312 break;
313 }
314 if (lists[i] == NULL) {
315 qemu_error("there is no option group \"%s\"\n", group);
316 }
317 return lists[i];
318}
319
d058fe03
GH
320int qemu_set_option(const char *str)
321{
322 char group[64], id[64], arg[64];
ddc97855 323 QemuOptsList *list;
d058fe03 324 QemuOpts *opts;
ddc97855 325 int rc, offset;
d058fe03
GH
326
327 rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
328 if (rc < 3 || str[offset] != '=') {
a861c453 329 qemu_error("can't parse: \"%s\"\n", str);
d058fe03
GH
330 return -1;
331 }
332
ddc97855
GH
333 list = find_list(group);
334 if (list == NULL) {
d058fe03
GH
335 return -1;
336 }
337
ddc97855 338 opts = qemu_opts_find(list, id);
d058fe03 339 if (!opts) {
a861c453 340 qemu_error("there is no %s \"%s\" defined\n",
ddc97855 341 list->name, id);
d058fe03
GH
342 return -1;
343 }
344
3df04ac3 345 if (qemu_opt_set(opts, arg, str+offset+1) == -1) {
d058fe03
GH
346 return -1;
347 }
348 return 0;
349}
350
d0fef6fb
GH
351int qemu_global_option(const char *str)
352{
353 char driver[64], property[64];
354 QemuOpts *opts;
355 int rc, offset;
356
357 rc = sscanf(str, "%63[^.].%63[^=]%n", driver, property, &offset);
358 if (rc < 2 || str[offset] != '=') {
359 qemu_error("can't parse: \"%s\"\n", str);
360 return -1;
361 }
362
363 opts = qemu_opts_create(&qemu_global_opts, NULL, 0);
364 qemu_opt_set(opts, "driver", driver);
365 qemu_opt_set(opts, "property", property);
366 qemu_opt_set(opts, "value", str+offset+1);
367 return 0;
368}
369
370static int qemu_add_one_global(QemuOpts *opts, void *opaque)
371{
372 GlobalProperty *g;
373
374 g = qemu_mallocz(sizeof(*g));
375 g->driver = qemu_opt_get(opts, "driver");
376 g->property = qemu_opt_get(opts, "property");
377 g->value = qemu_opt_get(opts, "value");
378 qdev_prop_register_global(g);
379 return 0;
380}
381
382void qemu_add_globals(void)
383{
384 qemu_opts_foreach(&qemu_global_opts, qemu_add_one_global, NULL, 0);
385}
386
9d993394
GH
387struct ConfigWriteData {
388 QemuOptsList *list;
389 FILE *fp;
390};
391
392static int config_write_opt(const char *name, const char *value, void *opaque)
393{
394 struct ConfigWriteData *data = opaque;
395
396 fprintf(data->fp, " %s = \"%s\"\n", name, value);
397 return 0;
398}
399
400static int config_write_opts(QemuOpts *opts, void *opaque)
401{
402 struct ConfigWriteData *data = opaque;
403 const char *id = qemu_opts_id(opts);
404
405 if (id) {
406 fprintf(data->fp, "[%s \"%s\"]\n", data->list->name, id);
407 } else {
408 fprintf(data->fp, "[%s]\n", data->list->name);
409 }
410 qemu_opt_foreach(opts, config_write_opt, data, 0);
411 fprintf(data->fp, "\n");
412 return 0;
413}
414
415void qemu_config_write(FILE *fp)
416{
417 struct ConfigWriteData data = { .fp = fp };
418 int i;
419
420 fprintf(fp, "# qemu config file\n\n");
421 for (i = 0; lists[i] != NULL; i++) {
422 data.list = lists[i];
423 qemu_opts_foreach(data.list, config_write_opts, &data, 0);
424 }
425}
42262ba8
GH
426
427int qemu_config_parse(FILE *fp)
428{
429 char line[1024], group[64], id[64], arg[64], value[1024];
430 QemuOptsList *list = NULL;
431 QemuOpts *opts = NULL;
432
433 while (fgets(line, sizeof(line), fp) != NULL) {
434 if (line[0] == '\n') {
435 /* skip empty lines */
436 continue;
437 }
438 if (line[0] == '#') {
439 /* comment */
440 continue;
441 }
442 if (sscanf(line, "[%63s \"%63[^\"]\"]", group, id) == 2) {
443 /* group with id */
444 list = find_list(group);
445 if (list == NULL)
446 return -1;
447 opts = qemu_opts_create(list, id, 1);
448 continue;
449 }
450 if (sscanf(line, "[%63[^]]]", group) == 1) {
451 /* group without id */
452 list = find_list(group);
453 if (list == NULL)
454 return -1;
455 opts = qemu_opts_create(list, NULL, 0);
456 continue;
457 }
458 if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2) {
459 /* arg = value */
460 if (opts == NULL) {
461 fprintf(stderr, "no group defined\n");
462 return -1;
463 }
464 if (qemu_opt_set(opts, arg, value) != 0) {
465 fprintf(stderr, "failed to set \"%s\" for %s\n",
466 arg, group);
467 return -1;
468 }
469 continue;
470 }
471 fprintf(stderr, "parse error: %s\n", line);
472 return -1;
473 }
474 return 0;
475}