6 void *qdev_get_prop_ptr(DeviceState
*dev
, Property
*prop
)
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
);
29 static void qdev_prop_cpy(DeviceState
*dev
, Property
*props
, void *src
)
31 if (props
->info
->type
== PROP_TYPE_BIT
) {
33 bit_prop_set(dev
, props
, *defval
);
35 char *dst
= qdev_get_prop_ptr(dev
, props
);
36 memcpy(dst
, src
, props
->info
->size
);
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);
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
= {
60 .type
= PROP_TYPE_BIT
,
61 .size
= sizeof(uint32_t),
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
);
73 /* accept both hex and decimal */
74 *ptr
= strtoul(str
, &end
, 0);
75 if ((*end
!= '\0') || (end
== str
)) {
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
= {
90 .type
= PROP_TYPE_UINT8
,
91 .size
= sizeof(uint8_t),
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
);
103 /* accept both hex and decimal */
104 *ptr
= strtoul(str
, &end
, 0);
105 if ((*end
!= '\0') || (end
== str
)) {
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
= {
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
);
133 /* accept both hex and decimal */
134 *ptr
= strtoul(str
, &end
, 0);
135 if ((*end
!= '\0') || (end
== str
)) {
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
= {
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
);
161 *ptr
= strtol(str
, &end
, 10);
162 if ((*end
!= '\0') || (end
== str
)) {
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
= {
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
);
190 *ptr
= strtoul(str
, &end
, 16);
191 if ((*end
!= '\0') || (end
== str
)) {
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
= {
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
);
219 /* accept both hex and decimal */
220 *ptr
= strtoull(str
, &end
, 0);
221 if ((*end
!= '\0') || (end
== str
)) {
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
= {
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
);
249 *ptr
= strtoull(str
, &end
, 16);
250 if ((*end
!= '\0') || (end
== str
)) {
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
= {
265 .type
= PROP_TYPE_UINT64
,
266 .size
= sizeof(uint64_t),
267 .parse
= parse_hex64
,
268 .print
= print_hex64
,
273 static int parse_string(DeviceState
*dev
, Property
*prop
, const char *str
)
275 char **ptr
= qdev_get_prop_ptr(dev
, prop
);
279 *ptr
= qemu_strdup(str
);
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
);
292 return snprintf(dest
, len
, "<null>");
293 return snprintf(dest
, len
, "\"%s\"", *ptr
);
296 PropertyInfo qdev_prop_string
= {
298 .type
= PROP_TYPE_STRING
,
299 .size
= sizeof(char*),
300 .parse
= parse_string
,
301 .print
= print_string
,
307 static int parse_drive(DeviceState
*dev
, Property
*prop
, const char *str
)
309 BlockDriverState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
310 BlockDriverState
*bs
;
315 if (bdrv_attach(bs
, dev
) < 0)
321 static void free_drive(DeviceState
*dev
, Property
*prop
)
323 BlockDriverState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
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
= {
340 .type
= PROP_TYPE_DRIVE
,
341 .size
= sizeof(BlockDriverState
*),
342 .parse
= parse_drive
,
343 .print
= print_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
);
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
);
366 return snprintf(dest
, len
, "<null>");
370 PropertyInfo qdev_prop_chr
= {
372 .type
= PROP_TYPE_CHR
,
373 .size
= sizeof(CharDriverState
*),
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
);
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
);
400 return snprintf(dest
, len
, "<null>");
404 PropertyInfo qdev_prop_netdev
= {
406 .type
= PROP_TYPE_NETDEV
,
407 .size
= sizeof(VLANClientState
*),
408 .parse
= parse_netdev
,
409 .print
= print_netdev
,
414 static int parse_vlan(DeviceState
*dev
, Property
*prop
, const char *str
)
416 VLANState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
419 if (sscanf(str
, "%d", &id
) != 1)
421 *ptr
= qemu_find_vlan(id
, 1);
427 static int print_vlan(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
429 VLANState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
432 return snprintf(dest
, len
, "%d", (*ptr
)->id
);
434 return snprintf(dest
, len
, "<null>");
438 PropertyInfo qdev_prop_vlan
= {
440 .type
= PROP_TYPE_VLAN
,
441 .size
= sizeof(VLANClientState
*),
446 /* --- pointer --- */
448 /* Not a proper property, just for dirty hacks. TODO Remove it! */
449 PropertyInfo qdev_prop_ptr
= {
451 .type
= PROP_TYPE_PTR
,
452 .size
= sizeof(void*),
455 /* --- mac address --- */
458 * accepted syntax versions:
462 static int parse_mac(DeviceState
*dev
, Property
*prop
, const char *str
)
464 MACAddr
*mac
= qdev_get_prop_ptr(dev
, prop
);
468 for (i
= 0, pos
= 0; i
< 6; i
++, pos
+= 3) {
469 if (!qemu_isxdigit(str
[pos
]))
471 if (!qemu_isxdigit(str
[pos
+1]))
474 if (str
[pos
+2] != '\0')
477 if (str
[pos
+2] != ':' && str
[pos
+2] != '-')
480 mac
->a
[i
] = strtol(str
+pos
, &p
, 16);
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
= {
496 .type
= PROP_TYPE_MACADDR
,
497 .size
= sizeof(MACAddr
),
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) {
514 if (sscanf(str
, "%x%n", &slot
, &n
) != 1) {
522 *ptr
= slot
<< 3 | fn
;
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
);
531 return snprintf(dest
, len
, "<unset>");
533 return snprintf(dest
, len
, "%02x.%x", *ptr
>> 3, *ptr
& 7);
537 PropertyInfo qdev_prop_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
)
551 while (props
->name
) {
552 if (strcmp(props
->name
, name
) == 0)
559 static Property
*qdev_prop_find(DeviceState
*dev
, const char *name
)
563 /* device properties */
564 prop
= qdev_prop_walk(dev
->info
->props
, name
);
569 prop
= qdev_prop_walk(dev
->parent_bus
->info
->props
, name
);
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
)
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
);
597 ret
= prop
->info
->parse(dev
, prop
, value
);
601 qerror_report(QERR_PROPERTY_VALUE_IN_USE
,
602 dev
->info
->name
, name
, value
);
606 qerror_report(QERR_PROPERTY_VALUE_BAD
,
607 dev
->info
->name
, name
, value
);
610 qerror_report(QERR_PROPERTY_VALUE_NOT_FOUND
,
611 dev
->info
->name
, name
, value
);
619 void qdev_prop_set(DeviceState
*dev
, const char *name
, void *src
, enum PropertyType type
)
623 prop
= qdev_prop_find(dev
, name
);
625 fprintf(stderr
, "%s: property \"%s.%s\" not found\n",
626 __FUNCTION__
, dev
->info
->name
, name
);
629 if (prop
->info
->type
!= type
) {
630 fprintf(stderr
, "%s: property \"%s.%s\" type mismatch\n",
631 __FUNCTION__
, dev
->info
->name
, name
);
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
)
676 res
= bdrv_attach(value
, dev
);
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
));
684 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_DRIVE
);
688 void qdev_prop_set_drive_nofail(DeviceState
*dev
, const char *name
, BlockDriverState
*value
)
690 if (qdev_prop_set_drive(dev
, name
, value
) < 0) {
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
)
723 while (props
->name
) {
725 qdev_prop_cpy(dev
, props
, props
->defval
);
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
)
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) {
756 if (qdev_prop_parse(dev
, prop
->property
, prop
->value
) != 0) {
762 static int qdev_add_one_global(QemuOpts
*opts
, void *opaque
)
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
);
774 void qemu_add_globals(void)
776 qemu_opts_foreach(qemu_find_opts("global"), qdev_add_one_global
, NULL
, 0);