3 #include "qapi/qmp/qerror.h"
4 #include "sysemu/blockdev.h"
5 #include "hw/block-common.h"
7 #include "qapi/visitor.h"
10 void *qdev_get_prop_ptr(DeviceState *dev, Property *prop)
17 static void get_pointer(Object *obj, Visitor *v, Property *prop,
18 const char *(*print)(void *ptr),
19 const char *name, Error **errp)
21 DeviceState *dev = DEVICE(obj);
22 void **ptr = qdev_get_prop_ptr(dev, prop);
25 p = (char *) (*ptr ? print(*ptr) : "");
26 visit_type_str(v, &p, name, errp);
29 static void set_pointer(Object *obj, Visitor *v, Property *prop,
30 int (*parse)(DeviceState *dev, const char *str,
32 const char *name, Error **errp)
34 DeviceState *dev = DEVICE(obj);
35 Error *local_err = NULL;
36 void **ptr = qdev_get_prop_ptr(dev, prop);
40 if (dev->state != DEV_STATE_CREATED) {
41 error_set(errp, QERR_PERMISSION_DENIED);
45 visit_type_str(v, &str, name, &local_err);
47 error_propagate(errp, local_err);
55 ret = parse(dev, str, ptr);
56 error_set_from_qdev_prop_error(errp, ret, dev, prop, str);
60 static void get_enum(Object *obj, Visitor *v, void *opaque,
61 const char *name, Error **errp)
63 DeviceState *dev = DEVICE(obj);
64 Property *prop = opaque;
65 int *ptr = qdev_get_prop_ptr(dev, prop);
67 visit_type_enum(v, ptr, prop->info->enum_table,
68 prop->info->name, prop->name, errp);
71 static void set_enum(Object *obj, Visitor *v, void *opaque,
72 const char *name, Error **errp)
74 DeviceState *dev = DEVICE(obj);
75 Property *prop = opaque;
76 int *ptr = qdev_get_prop_ptr(dev, prop);
78 if (dev->state != DEV_STATE_CREATED) {
79 error_set(errp, QERR_PERMISSION_DENIED);
83 visit_type_enum(v, ptr, prop->info->enum_table,
84 prop->info->name, prop->name, errp);
89 static uint32_t qdev_get_prop_mask(Property *prop)
91 assert(prop->info == &qdev_prop_bit);
92 return 0x1 << prop->bitnr;
95 static void bit_prop_set(DeviceState *dev, Property *props, bool val)
97 uint32_t *p = qdev_get_prop_ptr(dev, props);
98 uint32_t mask = qdev_get_prop_mask(props);
105 static int print_bit(DeviceState *dev, Property *prop, char *dest, size_t len)
107 uint32_t *p = qdev_get_prop_ptr(dev, prop);
108 return snprintf(dest, len, (*p & qdev_get_prop_mask(prop)) ? "on" : "off");
111 static void get_bit(Object *obj, Visitor *v, void *opaque,
112 const char *name, Error **errp)
114 DeviceState *dev = DEVICE(obj);
115 Property *prop = opaque;
116 uint32_t *p = qdev_get_prop_ptr(dev, prop);
117 bool value = (*p & qdev_get_prop_mask(prop)) != 0;
119 visit_type_bool(v, &value, name, errp);
122 static void set_bit(Object *obj, Visitor *v, void *opaque,
123 const char *name, Error **errp)
125 DeviceState *dev = DEVICE(obj);
126 Property *prop = opaque;
127 Error *local_err = NULL;
130 if (dev->state != DEV_STATE_CREATED) {
131 error_set(errp, QERR_PERMISSION_DENIED);
135 visit_type_bool(v, &value, name, &local_err);
137 error_propagate(errp, local_err);
140 bit_prop_set(dev, prop, value);
143 PropertyInfo qdev_prop_bit = {
145 .legacy_name = "on/off",
151 /* --- 8bit integer --- */
153 static void get_uint8(Object *obj, Visitor *v, void *opaque,
154 const char *name, Error **errp)
156 DeviceState *dev = DEVICE(obj);
157 Property *prop = opaque;
158 uint8_t *ptr = qdev_get_prop_ptr(dev, prop);
160 visit_type_uint8(v, ptr, name, errp);
163 static void set_uint8(Object *obj, Visitor *v, void *opaque,
164 const char *name, Error **errp)
166 DeviceState *dev = DEVICE(obj);
167 Property *prop = opaque;
168 uint8_t *ptr = qdev_get_prop_ptr(dev, prop);
170 if (dev->state != DEV_STATE_CREATED) {
171 error_set(errp, QERR_PERMISSION_DENIED);
175 visit_type_uint8(v, ptr, name, errp);
178 PropertyInfo qdev_prop_uint8 = {
184 /* --- 8bit hex value --- */
186 static int parse_hex8(DeviceState *dev, Property *prop, const char *str)
188 uint8_t *ptr = qdev_get_prop_ptr(dev, prop);
191 if (str[0] != '0' || str[1] != 'x') {
195 *ptr = strtoul(str, &end, 16);
196 if ((*end != '\0') || (end == str)) {
203 static int print_hex8(DeviceState *dev, Property *prop, char *dest, size_t len)
205 uint8_t *ptr = qdev_get_prop_ptr(dev, prop);
206 return snprintf(dest, len, "0x%" PRIx8, *ptr);
209 PropertyInfo qdev_prop_hex8 = {
211 .legacy_name = "hex8",
218 /* --- 16bit integer --- */
220 static void get_uint16(Object *obj, Visitor *v, void *opaque,
221 const char *name, Error **errp)
223 DeviceState *dev = DEVICE(obj);
224 Property *prop = opaque;
225 uint16_t *ptr = qdev_get_prop_ptr(dev, prop);
227 visit_type_uint16(v, ptr, name, errp);
230 static void set_uint16(Object *obj, Visitor *v, void *opaque,
231 const char *name, Error **errp)
233 DeviceState *dev = DEVICE(obj);
234 Property *prop = opaque;
235 uint16_t *ptr = qdev_get_prop_ptr(dev, prop);
237 if (dev->state != DEV_STATE_CREATED) {
238 error_set(errp, QERR_PERMISSION_DENIED);
242 visit_type_uint16(v, ptr, name, errp);
245 PropertyInfo qdev_prop_uint16 = {
251 /* --- 32bit integer --- */
253 static void get_uint32(Object *obj, Visitor *v, void *opaque,
254 const char *name, Error **errp)
256 DeviceState *dev = DEVICE(obj);
257 Property *prop = opaque;
258 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
260 visit_type_uint32(v, ptr, name, errp);
263 static void set_uint32(Object *obj, Visitor *v, void *opaque,
264 const char *name, Error **errp)
266 DeviceState *dev = DEVICE(obj);
267 Property *prop = opaque;
268 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
270 if (dev->state != DEV_STATE_CREATED) {
271 error_set(errp, QERR_PERMISSION_DENIED);
275 visit_type_uint32(v, ptr, name, errp);
278 static void get_int32(Object *obj, Visitor *v, void *opaque,
279 const char *name, Error **errp)
281 DeviceState *dev = DEVICE(obj);
282 Property *prop = opaque;
283 int32_t *ptr = qdev_get_prop_ptr(dev, prop);
285 visit_type_int32(v, ptr, name, errp);
288 static void set_int32(Object *obj, Visitor *v, void *opaque,
289 const char *name, Error **errp)
291 DeviceState *dev = DEVICE(obj);
292 Property *prop = opaque;
293 int32_t *ptr = qdev_get_prop_ptr(dev, prop);
295 if (dev->state != DEV_STATE_CREATED) {
296 error_set(errp, QERR_PERMISSION_DENIED);
300 visit_type_int32(v, ptr, name, errp);
303 PropertyInfo qdev_prop_uint32 = {
309 PropertyInfo qdev_prop_int32 = {
315 /* --- 32bit hex value --- */
317 static int parse_hex32(DeviceState *dev, Property *prop, const char *str)
319 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
322 if (str[0] != '0' || str[1] != 'x') {
326 *ptr = strtoul(str, &end, 16);
327 if ((*end != '\0') || (end == str)) {
334 static int print_hex32(DeviceState *dev, Property *prop, char *dest, size_t len)
336 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
337 return snprintf(dest, len, "0x%" PRIx32, *ptr);
340 PropertyInfo qdev_prop_hex32 = {
342 .legacy_name = "hex32",
343 .parse = parse_hex32,
344 .print = print_hex32,
349 /* --- 64bit integer --- */
351 static void get_uint64(Object *obj, Visitor *v, void *opaque,
352 const char *name, Error **errp)
354 DeviceState *dev = DEVICE(obj);
355 Property *prop = opaque;
356 uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
358 visit_type_uint64(v, ptr, name, errp);
361 static void set_uint64(Object *obj, Visitor *v, void *opaque,
362 const char *name, Error **errp)
364 DeviceState *dev = DEVICE(obj);
365 Property *prop = opaque;
366 uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
368 if (dev->state != DEV_STATE_CREATED) {
369 error_set(errp, QERR_PERMISSION_DENIED);
373 visit_type_uint64(v, ptr, name, errp);
376 PropertyInfo qdev_prop_uint64 = {
382 /* --- 64bit hex value --- */
384 static int parse_hex64(DeviceState *dev, Property *prop, const char *str)
386 uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
389 if (str[0] != '0' || str[1] != 'x') {
393 *ptr = strtoull(str, &end, 16);
394 if ((*end != '\0') || (end == str)) {
401 static int print_hex64(DeviceState *dev, Property *prop, char *dest, size_t len)
403 uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
404 return snprintf(dest, len, "0x%" PRIx64, *ptr);
407 PropertyInfo qdev_prop_hex64 = {
409 .legacy_name = "hex64",
410 .parse = parse_hex64,
411 .print = print_hex64,
418 static void release_string(Object *obj, const char *name, void *opaque)
420 Property *prop = opaque;
421 g_free(*(char **)qdev_get_prop_ptr(DEVICE(obj), prop));
424 static int print_string(DeviceState *dev, Property *prop, char *dest, size_t len)
426 char **ptr = qdev_get_prop_ptr(dev, prop);
428 return snprintf(dest, len, "<null>");
429 return snprintf(dest, len, "\"%s\"", *ptr);
432 static void get_string(Object *obj, Visitor *v, void *opaque,
433 const char *name, Error **errp)
435 DeviceState *dev = DEVICE(obj);
436 Property *prop = opaque;
437 char **ptr = qdev_get_prop_ptr(dev, prop);
440 char *str = (char *)"";
441 visit_type_str(v, &str, name, errp);
443 visit_type_str(v, ptr, name, errp);
447 static void set_string(Object *obj, Visitor *v, void *opaque,
448 const char *name, Error **errp)
450 DeviceState *dev = DEVICE(obj);
451 Property *prop = opaque;
452 char **ptr = qdev_get_prop_ptr(dev, prop);
453 Error *local_err = NULL;
456 if (dev->state != DEV_STATE_CREATED) {
457 error_set(errp, QERR_PERMISSION_DENIED);
461 visit_type_str(v, &str, name, &local_err);
463 error_propagate(errp, local_err);
472 PropertyInfo qdev_prop_string = {
474 .print = print_string,
475 .release = release_string,
482 static int parse_drive(DeviceState *dev, const char *str, void **ptr)
484 BlockDriverState *bs;
489 if (bdrv_attach_dev(bs, dev) < 0)
495 static void release_drive(Object *obj, const char *name, void *opaque)
497 DeviceState *dev = DEVICE(obj);
498 Property *prop = opaque;
499 BlockDriverState **ptr = qdev_get_prop_ptr(dev, prop);
502 bdrv_detach_dev(*ptr, dev);
503 blockdev_auto_del(*ptr);
507 static const char *print_drive(void *ptr)
509 return bdrv_get_device_name(ptr);
512 static void get_drive(Object *obj, Visitor *v, void *opaque,
513 const char *name, Error **errp)
515 get_pointer(obj, v, opaque, print_drive, name, errp);
518 static void set_drive(Object *obj, Visitor *v, void *opaque,
519 const char *name, Error **errp)
521 set_pointer(obj, v, opaque, parse_drive, name, errp);
524 PropertyInfo qdev_prop_drive = {
528 .release = release_drive,
531 /* --- character device --- */
533 static int parse_chr(DeviceState *dev, const char *str, void **ptr)
535 CharDriverState *chr = qemu_chr_find(str);
539 if (chr->avail_connections < 1) {
543 --chr->avail_connections;
547 static void release_chr(Object *obj, const char *name, void *opaque)
549 DeviceState *dev = DEVICE(obj);
550 Property *prop = opaque;
551 CharDriverState **ptr = qdev_get_prop_ptr(dev, prop);
554 qemu_chr_add_handlers(*ptr, NULL, NULL, NULL, NULL);
559 static const char *print_chr(void *ptr)
561 CharDriverState *chr = ptr;
563 return chr->label ? chr->label : "";
566 static void get_chr(Object *obj, Visitor *v, void *opaque,
567 const char *name, Error **errp)
569 get_pointer(obj, v, opaque, print_chr, name, errp);
572 static void set_chr(Object *obj, Visitor *v, void *opaque,
573 const char *name, Error **errp)
575 set_pointer(obj, v, opaque, parse_chr, name, errp);
578 PropertyInfo qdev_prop_chr = {
582 .release = release_chr,
585 /* --- netdev device --- */
587 static int parse_netdev(DeviceState *dev, const char *str, void **ptr)
589 NetClientState *netdev = qemu_find_netdev(str);
591 if (netdev == NULL) {
601 static const char *print_netdev(void *ptr)
603 NetClientState *netdev = ptr;
605 return netdev->name ? netdev->name : "";
608 static void get_netdev(Object *obj, Visitor *v, void *opaque,
609 const char *name, Error **errp)
611 get_pointer(obj, v, opaque, print_netdev, name, errp);
614 static void set_netdev(Object *obj, Visitor *v, void *opaque,
615 const char *name, Error **errp)
617 set_pointer(obj, v, opaque, parse_netdev, name, errp);
620 PropertyInfo qdev_prop_netdev = {
628 static int print_vlan(DeviceState *dev, Property *prop, char *dest, size_t len)
630 NetClientState **ptr = qdev_get_prop_ptr(dev, prop);
634 if (!net_hub_id_for_client(*ptr, &id)) {
635 return snprintf(dest, len, "%d", id);
639 return snprintf(dest, len, "<null>");
642 static void get_vlan(Object *obj, Visitor *v, void *opaque,
643 const char *name, Error **errp)
645 DeviceState *dev = DEVICE(obj);
646 Property *prop = opaque;
647 NetClientState **ptr = qdev_get_prop_ptr(dev, prop);
652 if (!net_hub_id_for_client(*ptr, &hub_id)) {
657 visit_type_int32(v, &id, name, errp);
660 static void set_vlan(Object *obj, Visitor *v, void *opaque,
661 const char *name, Error **errp)
663 DeviceState *dev = DEVICE(obj);
664 Property *prop = opaque;
665 NetClientState **ptr = qdev_get_prop_ptr(dev, prop);
666 Error *local_err = NULL;
668 NetClientState *hubport;
670 if (dev->state != DEV_STATE_CREATED) {
671 error_set(errp, QERR_PERMISSION_DENIED);
675 visit_type_int32(v, &id, name, &local_err);
677 error_propagate(errp, local_err);
685 hubport = net_hub_port_find(id);
687 error_set(errp, QERR_INVALID_PARAMETER_VALUE,
688 name, prop->info->name);
694 PropertyInfo qdev_prop_vlan = {
701 /* --- pointer --- */
703 /* Not a proper property, just for dirty hacks. TODO Remove it! */
704 PropertyInfo qdev_prop_ptr = {
708 /* --- mac address --- */
711 * accepted syntax versions:
715 static void get_mac(Object *obj, Visitor *v, void *opaque,
716 const char *name, Error **errp)
718 DeviceState *dev = DEVICE(obj);
719 Property *prop = opaque;
720 MACAddr *mac = qdev_get_prop_ptr(dev, prop);
721 char buffer[2 * 6 + 5 + 1];
724 snprintf(buffer, sizeof(buffer), "%02x:%02x:%02x:%02x:%02x:%02x",
725 mac->a[0], mac->a[1], mac->a[2],
726 mac->a[3], mac->a[4], mac->a[5]);
728 visit_type_str(v, &p, name, errp);
731 static void set_mac(Object *obj, Visitor *v, void *opaque,
732 const char *name, Error **errp)
734 DeviceState *dev = DEVICE(obj);
735 Property *prop = opaque;
736 MACAddr *mac = qdev_get_prop_ptr(dev, prop);
737 Error *local_err = NULL;
741 if (dev->state != DEV_STATE_CREATED) {
742 error_set(errp, QERR_PERMISSION_DENIED);
746 visit_type_str(v, &str, name, &local_err);
748 error_propagate(errp, local_err);
752 for (i = 0, pos = 0; i < 6; i++, pos += 3) {
753 if (!qemu_isxdigit(str[pos]))
755 if (!qemu_isxdigit(str[pos+1]))
758 if (str[pos+2] != '\0')
761 if (str[pos+2] != ':' && str[pos+2] != '-')
764 mac->a[i] = strtol(str+pos, &p, 16);
770 error_set_from_qdev_prop_error(errp, EINVAL, dev, prop, str);
774 PropertyInfo qdev_prop_macaddr = {
780 /* --- lost tick policy --- */
782 static const char *lost_tick_policy_table[LOST_TICK_MAX+1] = {
783 [LOST_TICK_DISCARD] = "discard",
784 [LOST_TICK_DELAY] = "delay",
785 [LOST_TICK_MERGE] = "merge",
786 [LOST_TICK_SLEW] = "slew",
787 [LOST_TICK_MAX] = NULL,
790 QEMU_BUILD_BUG_ON(sizeof(LostTickPolicy) != sizeof(int));
792 PropertyInfo qdev_prop_losttickpolicy = {
793 .name = "LostTickPolicy",
794 .enum_table = lost_tick_policy_table,
799 /* --- BIOS CHS translation */
801 static const char *bios_chs_trans_table[] = {
802 [BIOS_ATA_TRANSLATION_AUTO] = "auto",
803 [BIOS_ATA_TRANSLATION_NONE] = "none",
804 [BIOS_ATA_TRANSLATION_LBA] = "lba",
807 PropertyInfo qdev_prop_bios_chs_trans = {
808 .name = "bios-chs-trans",
809 .enum_table = bios_chs_trans_table,
814 /* --- pci address --- */
817 * bus-local address, i.e. "$slot" or "$slot.$fn"
819 static void set_pci_devfn(Object *obj, Visitor *v, void *opaque,
820 const char *name, Error **errp)
822 DeviceState *dev = DEVICE(obj);
823 Property *prop = opaque;
824 int32_t value, *ptr = qdev_get_prop_ptr(dev, prop);
825 unsigned int slot, fn, n;
826 Error *local_err = NULL;
829 if (dev->state != DEV_STATE_CREATED) {
830 error_set(errp, QERR_PERMISSION_DENIED);
834 visit_type_str(v, &str, name, &local_err);
836 error_free(local_err);
838 visit_type_int32(v, &value, name, &local_err);
840 error_propagate(errp, local_err);
841 } else if (value < -1 || value > 255) {
842 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
850 if (sscanf(str, "%x.%x%n", &slot, &fn, &n) != 2) {
852 if (sscanf(str, "%x%n", &slot, &n) != 1) {
856 if (str[n] != '\0' || fn > 7 || slot > 31) {
859 *ptr = slot << 3 | fn;
864 error_set_from_qdev_prop_error(errp, EINVAL, dev, prop, str);
868 static int print_pci_devfn(DeviceState *dev, Property *prop, char *dest, size_t len)
870 int32_t *ptr = qdev_get_prop_ptr(dev, prop);
873 return snprintf(dest, len, "<unset>");
875 return snprintf(dest, len, "%02x.%x", *ptr >> 3, *ptr & 7);
879 PropertyInfo qdev_prop_pci_devfn = {
881 .legacy_name = "pci-devfn",
882 .print = print_pci_devfn,
884 .set = set_pci_devfn,
887 /* --- blocksize --- */
889 static void set_blocksize(Object *obj, Visitor *v, void *opaque,
890 const char *name, Error **errp)
892 DeviceState *dev = DEVICE(obj);
893 Property *prop = opaque;
894 uint16_t value, *ptr = qdev_get_prop_ptr(dev, prop);
895 Error *local_err = NULL;
896 const int64_t min = 512;
897 const int64_t max = 32768;
899 if (dev->state != DEV_STATE_CREATED) {
900 error_set(errp, QERR_PERMISSION_DENIED);
904 visit_type_uint16(v, &value, name, &local_err);
906 error_propagate(errp, local_err);
909 if (value < min || value > max) {
910 error_set(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE,
911 dev->id?:"", name, (int64_t)value, min, max);
915 /* We rely on power-of-2 blocksizes for bitmasks */
916 if ((value & (value - 1)) != 0) {
917 error_set(errp, QERR_PROPERTY_VALUE_NOT_POWER_OF_2,
918 dev->id?:"", name, (int64_t)value);
925 PropertyInfo qdev_prop_blocksize = {
928 .set = set_blocksize,
931 /* --- pci host address --- */
933 static void get_pci_host_devaddr(Object *obj, Visitor *v, void *opaque,
934 const char *name, Error **errp)
936 DeviceState *dev = DEVICE(obj);
937 Property *prop = opaque;
938 PCIHostDeviceAddress *addr = qdev_get_prop_ptr(dev, prop);
939 char buffer[] = "xxxx:xx:xx.x";
943 rc = snprintf(buffer, sizeof(buffer), "%04x:%02x:%02x.%d",
944 addr->domain, addr->bus, addr->slot, addr->function);
945 assert(rc == sizeof(buffer) - 1);
947 visit_type_str(v, &p, name, errp);
951 * Parse [<domain>:]<bus>:<slot>.<func>
952 * if <domain> is not supplied, it's assumed to be 0.
954 static void set_pci_host_devaddr(Object *obj, Visitor *v, void *opaque,
955 const char *name, Error **errp)
957 DeviceState *dev = DEVICE(obj);
958 Property *prop = opaque;
959 PCIHostDeviceAddress *addr = qdev_get_prop_ptr(dev, prop);
960 Error *local_err = NULL;
964 unsigned long dom = 0, bus = 0;
965 unsigned int slot = 0, func = 0;
967 if (dev->state != DEV_STATE_CREATED) {
968 error_set(errp, QERR_PERMISSION_DENIED);
972 visit_type_str(v, &str, name, &local_err);
974 error_propagate(errp, local_err);
979 val = strtoul(p, &e, 16);
980 if (e == p || *e != ':') {
986 val = strtoul(p, &e, 16);
994 val = strtoul(p, &e, 16);
1005 val = strtoul(p, &e, 10);
1011 if (dom > 0xffff || bus > 0xff || slot > 0x1f || func > 7) {
1022 addr->function = func;
1028 error_set_from_qdev_prop_error(errp, EINVAL, dev, prop, str);
1032 PropertyInfo qdev_prop_pci_host_devaddr = {
1033 .name = "pci-host-devaddr",
1034 .get = get_pci_host_devaddr,
1035 .set = set_pci_host_devaddr,
1038 /* --- public helpers --- */
1040 static Property *qdev_prop_walk(Property *props, const char *name)
1044 while (props->name) {
1045 if (strcmp(props->name, name) == 0)
1052 static Property *qdev_prop_find(DeviceState *dev, const char *name)
1057 /* device properties */
1058 class = object_get_class(OBJECT(dev));
1060 prop = qdev_prop_walk(DEVICE_CLASS(class)->props, name);
1064 class = object_class_get_parent(class);
1065 } while (class != object_class_by_name(TYPE_DEVICE));
1070 void error_set_from_qdev_prop_error(Error **errp, int ret, DeviceState *dev,
1071 Property *prop, const char *value)
1075 error_set(errp, QERR_PROPERTY_VALUE_IN_USE,
1076 object_get_typename(OBJECT(dev)), prop->name, value);
1080 error_set(errp, QERR_PROPERTY_VALUE_BAD,
1081 object_get_typename(OBJECT(dev)), prop->name, value);
1084 error_set(errp, QERR_PROPERTY_VALUE_NOT_FOUND,
1085 object_get_typename(OBJECT(dev)), prop->name, value);
1092 int qdev_prop_parse(DeviceState *dev, const char *name, const char *value)
1097 legacy_name = g_strdup_printf("legacy-%s", name);
1098 if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) {
1099 object_property_parse(OBJECT(dev), value, legacy_name, &err);
1101 object_property_parse(OBJECT(dev), value, name, &err);
1103 g_free(legacy_name);
1106 qerror_report_err(err);
1113 void qdev_prop_set_bit(DeviceState *dev, const char *name, bool value)
1116 object_property_set_bool(OBJECT(dev), value, name, &errp);
1117 assert_no_error(errp);
1120 void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value)
1123 object_property_set_int(OBJECT(dev), value, name, &errp);
1124 assert_no_error(errp);
1127 void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value)
1130 object_property_set_int(OBJECT(dev), value, name, &errp);
1131 assert_no_error(errp);
1134 void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value)
1137 object_property_set_int(OBJECT(dev), value, name, &errp);
1138 assert_no_error(errp);
1141 void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value)
1144 object_property_set_int(OBJECT(dev), value, name, &errp);
1145 assert_no_error(errp);
1148 void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value)
1151 object_property_set_int(OBJECT(dev), value, name, &errp);
1152 assert_no_error(errp);
1155 void qdev_prop_set_string(DeviceState *dev, const char *name, const char *value)
1158 object_property_set_str(OBJECT(dev), value, name, &errp);
1159 assert_no_error(errp);
1162 int qdev_prop_set_drive(DeviceState *dev, const char *name, BlockDriverState *value)
1165 const char *bdrv_name = value ? bdrv_get_device_name(value) : "";
1166 object_property_set_str(OBJECT(dev), bdrv_name,
1169 qerror_report_err(errp);
1176 void qdev_prop_set_drive_nofail(DeviceState *dev, const char *name, BlockDriverState *value)
1178 if (qdev_prop_set_drive(dev, name, value) < 0) {
1182 void qdev_prop_set_chr(DeviceState *dev, const char *name, CharDriverState *value)
1185 assert(!value || value->label);
1186 object_property_set_str(OBJECT(dev),
1187 value ? value->label : "", name, &errp);
1188 assert_no_error(errp);
1191 void qdev_prop_set_netdev(DeviceState *dev, const char *name, NetClientState *value)
1194 assert(!value || value->name);
1195 object_property_set_str(OBJECT(dev),
1196 value ? value->name : "", name, &errp);
1197 assert_no_error(errp);
1200 void qdev_prop_set_macaddr(DeviceState *dev, const char *name, uint8_t *value)
1203 char str[2 * 6 + 5 + 1];
1204 snprintf(str, sizeof(str), "%02x:%02x:%02x:%02x:%02x:%02x",
1205 value[0], value[1], value[2], value[3], value[4], value[5]);
1207 object_property_set_str(OBJECT(dev), str, name, &errp);
1208 assert_no_error(errp);
1211 void qdev_prop_set_enum(DeviceState *dev, const char *name, int value)
1216 prop = qdev_prop_find(dev, name);
1217 object_property_set_str(OBJECT(dev), prop->info->enum_table[value],
1219 assert_no_error(errp);
1222 void qdev_prop_set_ptr(DeviceState *dev, const char *name, void *value)
1227 prop = qdev_prop_find(dev, name);
1228 assert(prop && prop->info == &qdev_prop_ptr);
1229 ptr = qdev_get_prop_ptr(dev, prop);
1233 static QTAILQ_HEAD(, GlobalProperty) global_props = QTAILQ_HEAD_INITIALIZER(global_props);
1235 static void qdev_prop_register_global(GlobalProperty *prop)
1237 QTAILQ_INSERT_TAIL(&global_props, prop, next);
1240 void qdev_prop_register_global_list(GlobalProperty *props)
1244 for (i = 0; props[i].driver != NULL; i++) {
1245 qdev_prop_register_global(props+i);
1249 void qdev_prop_set_globals(DeviceState *dev)
1251 ObjectClass *class = object_get_class(OBJECT(dev));
1254 GlobalProperty *prop;
1255 QTAILQ_FOREACH(prop, &global_props, next) {
1256 if (strcmp(object_class_get_name(class), prop->driver) != 0) {
1259 if (qdev_prop_parse(dev, prop->property, prop->value) != 0) {
1263 class = object_class_get_parent(class);
1267 static int qdev_add_one_global(QemuOpts *opts, void *opaque)
1271 g = g_malloc0(sizeof(*g));
1272 g->driver = qemu_opt_get(opts, "driver");
1273 g->property = qemu_opt_get(opts, "property");
1274 g->value = qemu_opt_get(opts, "value");
1275 qdev_prop_register_global(g);
1279 void qemu_add_globals(void)
1281 qemu_opts_foreach(qemu_find_opts("global"), qdev_add_one_global, NULL, 0);