target-xxx: Use fprintf_function (format checking)
[qemu.git] / hw / qdev-properties.c
bloba493087a520db00934330833d63cc104ec3ee4db
1 #include "net.h"
2 #include "qdev.h"
3 #include "qerror.h"
4 #include "blockdev.h"
6 void *qdev_get_prop_ptr(DeviceState *dev, Property *prop)
8 void *ptr = dev;
9 ptr += prop->offset;
10 return ptr;
13 static uint32_t qdev_get_prop_mask(Property *prop)
15 assert(prop->info->type == PROP_TYPE_BIT);
16 return 0x1 << prop->bitnr;
19 static void bit_prop_set(DeviceState *dev, Property *props, bool val)
21 uint32_t *p = qdev_get_prop_ptr(dev, props);
22 uint32_t mask = qdev_get_prop_mask(props);
23 if (val)
24 *p |= mask;
25 else
26 *p &= ~mask;
29 static void qdev_prop_cpy(DeviceState *dev, Property *props, void *src)
31 if (props->info->type == PROP_TYPE_BIT) {
32 bool *defval = src;
33 bit_prop_set(dev, props, *defval);
34 } else {
35 char *dst = qdev_get_prop_ptr(dev, props);
36 memcpy(dst, src, props->info->size);
40 /* Bit */
41 static int parse_bit(DeviceState *dev, Property *prop, const char *str)
43 if (!strncasecmp(str, "on", 2))
44 bit_prop_set(dev, prop, true);
45 else if (!strncasecmp(str, "off", 3))
46 bit_prop_set(dev, prop, false);
47 else
48 return -EINVAL;
49 return 0;
52 static int print_bit(DeviceState *dev, Property *prop, char *dest, size_t len)
54 uint8_t *p = qdev_get_prop_ptr(dev, prop);
55 return snprintf(dest, len, (*p & qdev_get_prop_mask(prop)) ? "on" : "off");
58 PropertyInfo qdev_prop_bit = {
59 .name = "on/off",
60 .type = PROP_TYPE_BIT,
61 .size = sizeof(uint32_t),
62 .parse = parse_bit,
63 .print = print_bit,
66 /* --- 8bit integer --- */
68 static int parse_uint8(DeviceState *dev, Property *prop, const char *str)
70 uint8_t *ptr = qdev_get_prop_ptr(dev, prop);
71 char *end;
73 /* accept both hex and decimal */
74 *ptr = strtoul(str, &end, 0);
75 if ((*end != '\0') || (end == str)) {
76 return -EINVAL;
79 return 0;
82 static int print_uint8(DeviceState *dev, Property *prop, char *dest, size_t len)
84 uint8_t *ptr = qdev_get_prop_ptr(dev, prop);
85 return snprintf(dest, len, "%" PRIu8, *ptr);
88 PropertyInfo qdev_prop_uint8 = {
89 .name = "uint8",
90 .type = PROP_TYPE_UINT8,
91 .size = sizeof(uint8_t),
92 .parse = parse_uint8,
93 .print = print_uint8,
96 /* --- 16bit integer --- */
98 static int parse_uint16(DeviceState *dev, Property *prop, const char *str)
100 uint16_t *ptr = qdev_get_prop_ptr(dev, prop);
101 char *end;
103 /* accept both hex and decimal */
104 *ptr = strtoul(str, &end, 0);
105 if ((*end != '\0') || (end == str)) {
106 return -EINVAL;
109 return 0;
112 static int print_uint16(DeviceState *dev, Property *prop, char *dest, size_t len)
114 uint16_t *ptr = qdev_get_prop_ptr(dev, prop);
115 return snprintf(dest, len, "%" PRIu16, *ptr);
118 PropertyInfo qdev_prop_uint16 = {
119 .name = "uint16",
120 .type = PROP_TYPE_UINT16,
121 .size = sizeof(uint16_t),
122 .parse = parse_uint16,
123 .print = print_uint16,
126 /* --- 32bit integer --- */
128 static int parse_uint32(DeviceState *dev, Property *prop, const char *str)
130 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
131 char *end;
133 /* accept both hex and decimal */
134 *ptr = strtoul(str, &end, 0);
135 if ((*end != '\0') || (end == str)) {
136 return -EINVAL;
139 return 0;
142 static int print_uint32(DeviceState *dev, Property *prop, char *dest, size_t len)
144 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
145 return snprintf(dest, len, "%" PRIu32, *ptr);
148 PropertyInfo qdev_prop_uint32 = {
149 .name = "uint32",
150 .type = PROP_TYPE_UINT32,
151 .size = sizeof(uint32_t),
152 .parse = parse_uint32,
153 .print = print_uint32,
156 static int parse_int32(DeviceState *dev, Property *prop, const char *str)
158 int32_t *ptr = qdev_get_prop_ptr(dev, prop);
159 char *end;
161 *ptr = strtol(str, &end, 10);
162 if ((*end != '\0') || (end == str)) {
163 return -EINVAL;
166 return 0;
169 static int print_int32(DeviceState *dev, Property *prop, char *dest, size_t len)
171 int32_t *ptr = qdev_get_prop_ptr(dev, prop);
172 return snprintf(dest, len, "%" PRId32, *ptr);
175 PropertyInfo qdev_prop_int32 = {
176 .name = "int32",
177 .type = PROP_TYPE_INT32,
178 .size = sizeof(int32_t),
179 .parse = parse_int32,
180 .print = print_int32,
183 /* --- 32bit hex value --- */
185 static int parse_hex32(DeviceState *dev, Property *prop, const char *str)
187 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
188 char *end;
190 *ptr = strtoul(str, &end, 16);
191 if ((*end != '\0') || (end == str)) {
192 return -EINVAL;
195 return 0;
198 static int print_hex32(DeviceState *dev, Property *prop, char *dest, size_t len)
200 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
201 return snprintf(dest, len, "0x%" PRIx32, *ptr);
204 PropertyInfo qdev_prop_hex32 = {
205 .name = "hex32",
206 .type = PROP_TYPE_UINT32,
207 .size = sizeof(uint32_t),
208 .parse = parse_hex32,
209 .print = print_hex32,
212 /* --- 64bit integer --- */
214 static int parse_uint64(DeviceState *dev, Property *prop, const char *str)
216 uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
217 char *end;
219 /* accept both hex and decimal */
220 *ptr = strtoull(str, &end, 0);
221 if ((*end != '\0') || (end == str)) {
222 return -EINVAL;
225 return 0;
228 static int print_uint64(DeviceState *dev, Property *prop, char *dest, size_t len)
230 uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
231 return snprintf(dest, len, "%" PRIu64, *ptr);
234 PropertyInfo qdev_prop_uint64 = {
235 .name = "uint64",
236 .type = PROP_TYPE_UINT64,
237 .size = sizeof(uint64_t),
238 .parse = parse_uint64,
239 .print = print_uint64,
242 /* --- 64bit hex value --- */
244 static int parse_hex64(DeviceState *dev, Property *prop, const char *str)
246 uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
247 char *end;
249 *ptr = strtoull(str, &end, 16);
250 if ((*end != '\0') || (end == str)) {
251 return -EINVAL;
254 return 0;
257 static int print_hex64(DeviceState *dev, Property *prop, char *dest, size_t len)
259 uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
260 return snprintf(dest, len, "0x%" PRIx64, *ptr);
263 PropertyInfo qdev_prop_hex64 = {
264 .name = "hex64",
265 .type = PROP_TYPE_UINT64,
266 .size = sizeof(uint64_t),
267 .parse = parse_hex64,
268 .print = print_hex64,
271 /* --- string --- */
273 static int parse_string(DeviceState *dev, Property *prop, const char *str)
275 char **ptr = qdev_get_prop_ptr(dev, prop);
277 if (*ptr)
278 qemu_free(*ptr);
279 *ptr = qemu_strdup(str);
280 return 0;
283 static void free_string(DeviceState *dev, Property *prop)
285 qemu_free(*(char **)qdev_get_prop_ptr(dev, prop));
288 static int print_string(DeviceState *dev, Property *prop, char *dest, size_t len)
290 char **ptr = qdev_get_prop_ptr(dev, prop);
291 if (!*ptr)
292 return snprintf(dest, len, "<null>");
293 return snprintf(dest, len, "\"%s\"", *ptr);
296 PropertyInfo qdev_prop_string = {
297 .name = "string",
298 .type = PROP_TYPE_STRING,
299 .size = sizeof(char*),
300 .parse = parse_string,
301 .print = print_string,
302 .free = free_string,
305 /* --- drive --- */
307 static int parse_drive(DeviceState *dev, Property *prop, const char *str)
309 BlockDriverState **ptr = qdev_get_prop_ptr(dev, prop);
310 BlockDriverState *bs;
312 bs = bdrv_find(str);
313 if (bs == NULL)
314 return -ENOENT;
315 if (bdrv_attach(bs, dev) < 0)
316 return -EEXIST;
317 *ptr = bs;
318 return 0;
321 static void free_drive(DeviceState *dev, Property *prop)
323 BlockDriverState **ptr = qdev_get_prop_ptr(dev, prop);
325 if (*ptr) {
326 bdrv_detach(*ptr, dev);
327 blockdev_auto_del(*ptr);
331 static int print_drive(DeviceState *dev, Property *prop, char *dest, size_t len)
333 BlockDriverState **ptr = qdev_get_prop_ptr(dev, prop);
334 return snprintf(dest, len, "%s",
335 *ptr ? bdrv_get_device_name(*ptr) : "<null>");
338 PropertyInfo qdev_prop_drive = {
339 .name = "drive",
340 .type = PROP_TYPE_DRIVE,
341 .size = sizeof(BlockDriverState *),
342 .parse = parse_drive,
343 .print = print_drive,
344 .free = free_drive,
347 /* --- character device --- */
349 static int parse_chr(DeviceState *dev, Property *prop, const char *str)
351 CharDriverState **ptr = qdev_get_prop_ptr(dev, prop);
353 *ptr = qemu_chr_find(str);
354 if (*ptr == NULL)
355 return -ENOENT;
356 return 0;
359 static int print_chr(DeviceState *dev, Property *prop, char *dest, size_t len)
361 CharDriverState **ptr = qdev_get_prop_ptr(dev, prop);
363 if (*ptr && (*ptr)->label) {
364 return snprintf(dest, len, "%s", (*ptr)->label);
365 } else {
366 return snprintf(dest, len, "<null>");
370 PropertyInfo qdev_prop_chr = {
371 .name = "chr",
372 .type = PROP_TYPE_CHR,
373 .size = sizeof(CharDriverState*),
374 .parse = parse_chr,
375 .print = print_chr,
378 /* --- netdev device --- */
380 static int parse_netdev(DeviceState *dev, Property *prop, const char *str)
382 VLANClientState **ptr = qdev_get_prop_ptr(dev, prop);
384 *ptr = qemu_find_netdev(str);
385 if (*ptr == NULL)
386 return -ENOENT;
387 if ((*ptr)->peer) {
388 return -EEXIST;
390 return 0;
393 static int print_netdev(DeviceState *dev, Property *prop, char *dest, size_t len)
395 VLANClientState **ptr = qdev_get_prop_ptr(dev, prop);
397 if (*ptr && (*ptr)->name) {
398 return snprintf(dest, len, "%s", (*ptr)->name);
399 } else {
400 return snprintf(dest, len, "<null>");
404 PropertyInfo qdev_prop_netdev = {
405 .name = "netdev",
406 .type = PROP_TYPE_NETDEV,
407 .size = sizeof(VLANClientState*),
408 .parse = parse_netdev,
409 .print = print_netdev,
412 /* --- vlan --- */
414 static int parse_vlan(DeviceState *dev, Property *prop, const char *str)
416 VLANState **ptr = qdev_get_prop_ptr(dev, prop);
417 int id;
419 if (sscanf(str, "%d", &id) != 1)
420 return -EINVAL;
421 *ptr = qemu_find_vlan(id, 1);
422 if (*ptr == NULL)
423 return -ENOENT;
424 return 0;
427 static int print_vlan(DeviceState *dev, Property *prop, char *dest, size_t len)
429 VLANState **ptr = qdev_get_prop_ptr(dev, prop);
431 if (*ptr) {
432 return snprintf(dest, len, "%d", (*ptr)->id);
433 } else {
434 return snprintf(dest, len, "<null>");
438 PropertyInfo qdev_prop_vlan = {
439 .name = "vlan",
440 .type = PROP_TYPE_VLAN,
441 .size = sizeof(VLANClientState*),
442 .parse = parse_vlan,
443 .print = print_vlan,
446 /* --- pointer --- */
448 /* Not a proper property, just for dirty hacks. TODO Remove it! */
449 PropertyInfo qdev_prop_ptr = {
450 .name = "ptr",
451 .type = PROP_TYPE_PTR,
452 .size = sizeof(void*),
455 /* --- mac address --- */
458 * accepted syntax versions:
459 * 01:02:03:04:05:06
460 * 01-02-03-04-05-06
462 static int parse_mac(DeviceState *dev, Property *prop, const char *str)
464 MACAddr *mac = qdev_get_prop_ptr(dev, prop);
465 int i, pos;
466 char *p;
468 for (i = 0, pos = 0; i < 6; i++, pos += 3) {
469 if (!qemu_isxdigit(str[pos]))
470 return -EINVAL;
471 if (!qemu_isxdigit(str[pos+1]))
472 return -EINVAL;
473 if (i == 5) {
474 if (str[pos+2] != '\0')
475 return -EINVAL;
476 } else {
477 if (str[pos+2] != ':' && str[pos+2] != '-')
478 return -EINVAL;
480 mac->a[i] = strtol(str+pos, &p, 16);
482 return 0;
485 static int print_mac(DeviceState *dev, Property *prop, char *dest, size_t len)
487 MACAddr *mac = qdev_get_prop_ptr(dev, prop);
489 return snprintf(dest, len, "%02x:%02x:%02x:%02x:%02x:%02x",
490 mac->a[0], mac->a[1], mac->a[2],
491 mac->a[3], mac->a[4], mac->a[5]);
494 PropertyInfo qdev_prop_macaddr = {
495 .name = "macaddr",
496 .type = PROP_TYPE_MACADDR,
497 .size = sizeof(MACAddr),
498 .parse = parse_mac,
499 .print = print_mac,
502 /* --- pci address --- */
505 * bus-local address, i.e. "$slot" or "$slot.$fn"
507 static int parse_pci_devfn(DeviceState *dev, Property *prop, const char *str)
509 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
510 unsigned int slot, fn, n;
512 if (sscanf(str, "%x.%x%n", &slot, &fn, &n) != 2) {
513 fn = 0;
514 if (sscanf(str, "%x%n", &slot, &n) != 1) {
515 return -EINVAL;
518 if (str[n] != '\0')
519 return -EINVAL;
520 if (fn > 7)
521 return -EINVAL;
522 *ptr = slot << 3 | fn;
523 return 0;
526 static int print_pci_devfn(DeviceState *dev, Property *prop, char *dest, size_t len)
528 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
530 if (*ptr == -1) {
531 return snprintf(dest, len, "<unset>");
532 } else {
533 return snprintf(dest, len, "%02x.%x", *ptr >> 3, *ptr & 7);
537 PropertyInfo qdev_prop_pci_devfn = {
538 .name = "pci-devfn",
539 .type = PROP_TYPE_UINT32,
540 .size = sizeof(uint32_t),
541 .parse = parse_pci_devfn,
542 .print = print_pci_devfn,
545 /* --- public helpers --- */
547 static Property *qdev_prop_walk(Property *props, const char *name)
549 if (!props)
550 return NULL;
551 while (props->name) {
552 if (strcmp(props->name, name) == 0)
553 return props;
554 props++;
556 return NULL;
559 static Property *qdev_prop_find(DeviceState *dev, const char *name)
561 Property *prop;
563 /* device properties */
564 prop = qdev_prop_walk(dev->info->props, name);
565 if (prop)
566 return prop;
568 /* bus properties */
569 prop = qdev_prop_walk(dev->parent_bus->info->props, name);
570 if (prop)
571 return prop;
573 return NULL;
576 int qdev_prop_exists(DeviceState *dev, const char *name)
578 return qdev_prop_find(dev, name) ? true : false;
581 int qdev_prop_parse(DeviceState *dev, const char *name, const char *value)
583 Property *prop;
584 int ret;
586 prop = qdev_prop_find(dev, name);
588 * TODO Properties without a parse method are just for dirty
589 * hacks. qdev_prop_ptr is the only such PropertyInfo. It's
590 * marked for removal. The test !prop->info->parse should be
591 * removed along with it.
593 if (!prop || !prop->info->parse) {
594 qerror_report(QERR_PROPERTY_NOT_FOUND, dev->info->name, name);
595 return -1;
597 ret = prop->info->parse(dev, prop, value);
598 if (ret < 0) {
599 switch (ret) {
600 case -EEXIST:
601 qerror_report(QERR_PROPERTY_VALUE_IN_USE,
602 dev->info->name, name, value);
603 break;
604 default:
605 case -EINVAL:
606 qerror_report(QERR_PROPERTY_VALUE_BAD,
607 dev->info->name, name, value);
608 break;
609 case -ENOENT:
610 qerror_report(QERR_PROPERTY_VALUE_NOT_FOUND,
611 dev->info->name, name, value);
612 break;
614 return -1;
616 return 0;
619 void qdev_prop_set(DeviceState *dev, const char *name, void *src, enum PropertyType type)
621 Property *prop;
623 prop = qdev_prop_find(dev, name);
624 if (!prop) {
625 fprintf(stderr, "%s: property \"%s.%s\" not found\n",
626 __FUNCTION__, dev->info->name, name);
627 abort();
629 if (prop->info->type != type) {
630 fprintf(stderr, "%s: property \"%s.%s\" type mismatch\n",
631 __FUNCTION__, dev->info->name, name);
632 abort();
634 qdev_prop_cpy(dev, prop, src);
637 void qdev_prop_set_bit(DeviceState *dev, const char *name, bool value)
639 qdev_prop_set(dev, name, &value, PROP_TYPE_BIT);
642 void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value)
644 qdev_prop_set(dev, name, &value, PROP_TYPE_UINT8);
647 void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value)
649 qdev_prop_set(dev, name, &value, PROP_TYPE_UINT16);
652 void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value)
654 qdev_prop_set(dev, name, &value, PROP_TYPE_UINT32);
657 void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value)
659 qdev_prop_set(dev, name, &value, PROP_TYPE_INT32);
662 void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value)
664 qdev_prop_set(dev, name, &value, PROP_TYPE_UINT64);
667 void qdev_prop_set_string(DeviceState *dev, const char *name, char *value)
669 qdev_prop_set(dev, name, &value, PROP_TYPE_STRING);
672 int qdev_prop_set_drive(DeviceState *dev, const char *name, BlockDriverState *value)
674 int res;
676 res = bdrv_attach(value, dev);
677 if (res < 0) {
678 error_report("Can't attach drive %s to %s.%s: %s",
679 bdrv_get_device_name(value),
680 dev->id ? dev->id : dev->info->name,
681 name, strerror(-res));
682 return -1;
684 qdev_prop_set(dev, name, &value, PROP_TYPE_DRIVE);
685 return 0;
688 void qdev_prop_set_drive_nofail(DeviceState *dev, const char *name, BlockDriverState *value)
690 if (qdev_prop_set_drive(dev, name, value) < 0) {
691 exit(1);
694 void qdev_prop_set_chr(DeviceState *dev, const char *name, CharDriverState *value)
696 qdev_prop_set(dev, name, &value, PROP_TYPE_CHR);
699 void qdev_prop_set_netdev(DeviceState *dev, const char *name, VLANClientState *value)
701 qdev_prop_set(dev, name, &value, PROP_TYPE_NETDEV);
704 void qdev_prop_set_vlan(DeviceState *dev, const char *name, VLANState *value)
706 qdev_prop_set(dev, name, &value, PROP_TYPE_VLAN);
709 void qdev_prop_set_macaddr(DeviceState *dev, const char *name, uint8_t *value)
711 qdev_prop_set(dev, name, value, PROP_TYPE_MACADDR);
714 void qdev_prop_set_ptr(DeviceState *dev, const char *name, void *value)
716 qdev_prop_set(dev, name, &value, PROP_TYPE_PTR);
719 void qdev_prop_set_defaults(DeviceState *dev, Property *props)
721 if (!props)
722 return;
723 while (props->name) {
724 if (props->defval) {
725 qdev_prop_cpy(dev, props, props->defval);
727 props++;
731 static QTAILQ_HEAD(, GlobalProperty) global_props = QTAILQ_HEAD_INITIALIZER(global_props);
733 static void qdev_prop_register_global(GlobalProperty *prop)
735 QTAILQ_INSERT_TAIL(&global_props, prop, next);
738 void qdev_prop_register_global_list(GlobalProperty *props)
740 int i;
742 for (i = 0; props[i].driver != NULL; i++) {
743 qdev_prop_register_global(props+i);
747 void qdev_prop_set_globals(DeviceState *dev)
749 GlobalProperty *prop;
751 QTAILQ_FOREACH(prop, &global_props, next) {
752 if (strcmp(dev->info->name, prop->driver) != 0 &&
753 strcmp(dev->info->bus_info->name, prop->driver) != 0) {
754 continue;
756 if (qdev_prop_parse(dev, prop->property, prop->value) != 0) {
757 exit(1);
762 static int qdev_add_one_global(QemuOpts *opts, void *opaque)
764 GlobalProperty *g;
766 g = qemu_mallocz(sizeof(*g));
767 g->driver = qemu_opt_get(opts, "driver");
768 g->property = qemu_opt_get(opts, "property");
769 g->value = qemu_opt_get(opts, "value");
770 qdev_prop_register_global(g);
771 return 0;
774 void qemu_add_globals(void)
776 qemu_opts_foreach(qemu_find_opts("global"), qdev_add_one_global, NULL, 0);