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 fmt
= strncasecmp(str
, "0x",2) == 0 ? "%" PRIx8
: "%" PRIu8
;
75 if (sscanf(str
, fmt
, ptr
) != 1)
80 static int print_uint8(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
82 uint8_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
83 return snprintf(dest
, len
, "%" PRIu8
, *ptr
);
86 PropertyInfo qdev_prop_uint8
= {
88 .type
= PROP_TYPE_UINT8
,
89 .size
= sizeof(uint8_t),
94 /* --- 16bit integer --- */
96 static int parse_uint16(DeviceState
*dev
, Property
*prop
, const char *str
)
98 uint16_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
101 /* accept both hex and decimal */
102 fmt
= strncasecmp(str
, "0x",2) == 0 ? "%" PRIx16
: "%" PRIu16
;
103 if (sscanf(str
, fmt
, ptr
) != 1)
108 static int print_uint16(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
110 uint16_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
111 return snprintf(dest
, len
, "%" PRIu16
, *ptr
);
114 PropertyInfo qdev_prop_uint16
= {
116 .type
= PROP_TYPE_UINT16
,
117 .size
= sizeof(uint16_t),
118 .parse
= parse_uint16
,
119 .print
= print_uint16
,
122 /* --- 32bit integer --- */
124 static int parse_uint32(DeviceState
*dev
, Property
*prop
, const char *str
)
126 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
129 /* accept both hex and decimal */
130 fmt
= strncasecmp(str
, "0x",2) == 0 ? "%" PRIx32
: "%" PRIu32
;
131 if (sscanf(str
, fmt
, ptr
) != 1)
136 static int print_uint32(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
138 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
139 return snprintf(dest
, len
, "%" PRIu32
, *ptr
);
142 PropertyInfo qdev_prop_uint32
= {
144 .type
= PROP_TYPE_UINT32
,
145 .size
= sizeof(uint32_t),
146 .parse
= parse_uint32
,
147 .print
= print_uint32
,
150 static int parse_int32(DeviceState
*dev
, Property
*prop
, const char *str
)
152 int32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
154 if (sscanf(str
, "%" PRId32
, ptr
) != 1)
159 static int print_int32(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
161 int32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
162 return snprintf(dest
, len
, "%" PRId32
, *ptr
);
165 PropertyInfo qdev_prop_int32
= {
167 .type
= PROP_TYPE_INT32
,
168 .size
= sizeof(int32_t),
169 .parse
= parse_int32
,
170 .print
= print_int32
,
173 /* --- 32bit hex value --- */
175 static int parse_hex32(DeviceState
*dev
, Property
*prop
, const char *str
)
177 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
179 if (sscanf(str
, "%" PRIx32
, ptr
) != 1)
184 static int print_hex32(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
186 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
187 return snprintf(dest
, len
, "0x%" PRIx32
, *ptr
);
190 PropertyInfo qdev_prop_hex32
= {
192 .type
= PROP_TYPE_UINT32
,
193 .size
= sizeof(uint32_t),
194 .parse
= parse_hex32
,
195 .print
= print_hex32
,
198 /* --- 64bit integer --- */
200 static int parse_uint64(DeviceState
*dev
, Property
*prop
, const char *str
)
202 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
205 /* accept both hex and decimal */
206 fmt
= strncasecmp(str
, "0x",2) == 0 ? "%" PRIx64
: "%" PRIu64
;
207 if (sscanf(str
, fmt
, ptr
) != 1)
212 static int print_uint64(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
214 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
215 return snprintf(dest
, len
, "%" PRIu64
, *ptr
);
218 PropertyInfo qdev_prop_uint64
= {
220 .type
= PROP_TYPE_UINT64
,
221 .size
= sizeof(uint64_t),
222 .parse
= parse_uint64
,
223 .print
= print_uint64
,
226 /* --- 64bit hex value --- */
228 static int parse_hex64(DeviceState
*dev
, Property
*prop
, const char *str
)
230 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
232 if (sscanf(str
, "%" PRIx64
, ptr
) != 1)
237 static int print_hex64(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
239 uint64_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
240 return snprintf(dest
, len
, "0x%" PRIx64
, *ptr
);
243 PropertyInfo qdev_prop_hex64
= {
245 .type
= PROP_TYPE_UINT64
,
246 .size
= sizeof(uint64_t),
247 .parse
= parse_hex64
,
248 .print
= print_hex64
,
253 static int parse_string(DeviceState
*dev
, Property
*prop
, const char *str
)
255 char **ptr
= qdev_get_prop_ptr(dev
, prop
);
259 *ptr
= qemu_strdup(str
);
263 static int print_string(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
265 char **ptr
= qdev_get_prop_ptr(dev
, prop
);
267 return snprintf(dest
, len
, "<null>");
268 return snprintf(dest
, len
, "\"%s\"", *ptr
);
271 PropertyInfo qdev_prop_string
= {
273 .type
= PROP_TYPE_STRING
,
274 .size
= sizeof(char*),
275 .parse
= parse_string
,
276 .print
= print_string
,
281 static int parse_drive(DeviceState
*dev
, Property
*prop
, const char *str
)
283 DriveInfo
**ptr
= qdev_get_prop_ptr(dev
, prop
);
285 *ptr
= drive_get_by_id(str
);
291 static int print_drive(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
293 DriveInfo
**ptr
= qdev_get_prop_ptr(dev
, prop
);
294 return snprintf(dest
, len
, "%s", (*ptr
) ? (*ptr
)->id
: "<null>");
297 PropertyInfo qdev_prop_drive
= {
299 .type
= PROP_TYPE_DRIVE
,
300 .size
= sizeof(DriveInfo
*),
301 .parse
= parse_drive
,
302 .print
= print_drive
,
305 /* --- character device --- */
307 static int parse_chr(DeviceState
*dev
, Property
*prop
, const char *str
)
309 CharDriverState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
311 *ptr
= qemu_chr_find(str
);
317 static int print_chr(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
319 CharDriverState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
321 if (*ptr
&& (*ptr
)->label
) {
322 return snprintf(dest
, len
, "%s", (*ptr
)->label
);
324 return snprintf(dest
, len
, "<null>");
328 PropertyInfo qdev_prop_chr
= {
330 .type
= PROP_TYPE_CHR
,
331 .size
= sizeof(CharDriverState
*),
336 /* --- netdev device --- */
338 static int parse_netdev(DeviceState
*dev
, Property
*prop
, const char *str
)
340 VLANClientState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
342 *ptr
= qemu_find_netdev(str
);
351 static int print_netdev(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
353 VLANClientState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
355 if (*ptr
&& (*ptr
)->name
) {
356 return snprintf(dest
, len
, "%s", (*ptr
)->name
);
358 return snprintf(dest
, len
, "<null>");
362 PropertyInfo qdev_prop_netdev
= {
364 .type
= PROP_TYPE_NETDEV
,
365 .size
= sizeof(VLANClientState
*),
366 .parse
= parse_netdev
,
367 .print
= print_netdev
,
372 static int parse_vlan(DeviceState
*dev
, Property
*prop
, const char *str
)
374 VLANState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
377 if (sscanf(str
, "%d", &id
) != 1)
379 *ptr
= qemu_find_vlan(id
, 1);
385 static int print_vlan(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
387 VLANState
**ptr
= qdev_get_prop_ptr(dev
, prop
);
390 return snprintf(dest
, len
, "%d", (*ptr
)->id
);
392 return snprintf(dest
, len
, "<null>");
396 PropertyInfo qdev_prop_vlan
= {
398 .type
= PROP_TYPE_VLAN
,
399 .size
= sizeof(VLANClientState
*),
404 /* --- pointer --- */
406 /* Not a proper property, just for dirty hacks. TODO Remove it! */
407 PropertyInfo qdev_prop_ptr
= {
409 .type
= PROP_TYPE_PTR
,
410 .size
= sizeof(void*),
413 /* --- mac address --- */
416 * accepted syntax versions:
420 static int parse_mac(DeviceState
*dev
, Property
*prop
, const char *str
)
422 MACAddr
*mac
= qdev_get_prop_ptr(dev
, prop
);
426 for (i
= 0, pos
= 0; i
< 6; i
++, pos
+= 3) {
427 if (!qemu_isxdigit(str
[pos
]))
429 if (!qemu_isxdigit(str
[pos
+1]))
432 if (str
[pos
+2] != '\0')
435 if (str
[pos
+2] != ':' && str
[pos
+2] != '-')
438 mac
->a
[i
] = strtol(str
+pos
, &p
, 16);
443 static int print_mac(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
445 MACAddr
*mac
= qdev_get_prop_ptr(dev
, prop
);
447 return snprintf(dest
, len
, "%02x:%02x:%02x:%02x:%02x:%02x",
448 mac
->a
[0], mac
->a
[1], mac
->a
[2],
449 mac
->a
[3], mac
->a
[4], mac
->a
[5]);
452 PropertyInfo qdev_prop_macaddr
= {
454 .type
= PROP_TYPE_MACADDR
,
455 .size
= sizeof(MACAddr
),
460 /* --- pci address --- */
463 * bus-local address, i.e. "$slot" or "$slot.$fn"
465 static int parse_pci_devfn(DeviceState
*dev
, Property
*prop
, const char *str
)
467 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
468 unsigned int slot
, fn
, n
;
470 if (sscanf(str
, "%x.%x%n", &slot
, &fn
, &n
) != 2) {
472 if (sscanf(str
, "%x%n", &slot
, &n
) != 1) {
480 *ptr
= slot
<< 3 | fn
;
484 static int print_pci_devfn(DeviceState
*dev
, Property
*prop
, char *dest
, size_t len
)
486 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
489 return snprintf(dest
, len
, "<unset>");
491 return snprintf(dest
, len
, "%02x.%x", *ptr
>> 3, *ptr
& 7);
495 PropertyInfo qdev_prop_pci_devfn
= {
497 .type
= PROP_TYPE_UINT32
,
498 .size
= sizeof(uint32_t),
499 .parse
= parse_pci_devfn
,
500 .print
= print_pci_devfn
,
503 /* --- public helpers --- */
505 static Property
*qdev_prop_walk(Property
*props
, const char *name
)
509 while (props
->name
) {
510 if (strcmp(props
->name
, name
) == 0)
517 static Property
*qdev_prop_find(DeviceState
*dev
, const char *name
)
521 /* device properties */
522 prop
= qdev_prop_walk(dev
->info
->props
, name
);
527 prop
= qdev_prop_walk(dev
->parent_bus
->info
->props
, name
);
534 int qdev_prop_exists(DeviceState
*dev
, const char *name
)
536 return qdev_prop_find(dev
, name
) ? true : false;
539 int qdev_prop_parse(DeviceState
*dev
, const char *name
, const char *value
)
544 prop
= qdev_prop_find(dev
, name
);
546 * TODO Properties without a parse method are just for dirty
547 * hacks. qdev_prop_ptr is the only such PropertyInfo. It's
548 * marked for removal. The test !prop->info->parse should be
549 * removed along with it.
551 if (!prop
|| !prop
->info
->parse
) {
552 qerror_report(QERR_PROPERTY_NOT_FOUND
, dev
->info
->name
, name
);
555 ret
= prop
->info
->parse(dev
, prop
, value
);
559 qerror_report(QERR_PROPERTY_VALUE_IN_USE
,
560 dev
->info
->name
, name
, value
);
564 qerror_report(QERR_PROPERTY_VALUE_BAD
,
565 dev
->info
->name
, name
, value
);
568 qerror_report(QERR_PROPERTY_VALUE_NOT_FOUND
,
569 dev
->info
->name
, name
, value
);
577 void qdev_prop_set(DeviceState
*dev
, const char *name
, void *src
, enum PropertyType type
)
581 prop
= qdev_prop_find(dev
, name
);
583 fprintf(stderr
, "%s: property \"%s.%s\" not found\n",
584 __FUNCTION__
, dev
->info
->name
, name
);
587 if (prop
->info
->type
!= type
) {
588 fprintf(stderr
, "%s: property \"%s.%s\" type mismatch\n",
589 __FUNCTION__
, dev
->info
->name
, name
);
592 qdev_prop_cpy(dev
, prop
, src
);
595 void qdev_prop_set_uint8(DeviceState
*dev
, const char *name
, uint8_t value
)
597 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_UINT8
);
600 void qdev_prop_set_uint16(DeviceState
*dev
, const char *name
, uint16_t value
)
602 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_UINT16
);
605 void qdev_prop_set_uint32(DeviceState
*dev
, const char *name
, uint32_t value
)
607 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_UINT32
);
610 void qdev_prop_set_int32(DeviceState
*dev
, const char *name
, int32_t value
)
612 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_INT32
);
615 void qdev_prop_set_uint64(DeviceState
*dev
, const char *name
, uint64_t value
)
617 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_UINT64
);
620 void qdev_prop_set_drive(DeviceState
*dev
, const char *name
, DriveInfo
*value
)
622 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_DRIVE
);
625 void qdev_prop_set_chr(DeviceState
*dev
, const char *name
, CharDriverState
*value
)
627 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_CHR
);
630 void qdev_prop_set_netdev(DeviceState
*dev
, const char *name
, VLANClientState
*value
)
632 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_NETDEV
);
635 void qdev_prop_set_vlan(DeviceState
*dev
, const char *name
, VLANState
*value
)
637 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_VLAN
);
640 void qdev_prop_set_macaddr(DeviceState
*dev
, const char *name
, uint8_t *value
)
642 qdev_prop_set(dev
, name
, value
, PROP_TYPE_MACADDR
);
645 void qdev_prop_set_ptr(DeviceState
*dev
, const char *name
, void *value
)
647 qdev_prop_set(dev
, name
, &value
, PROP_TYPE_PTR
);
650 void qdev_prop_set_defaults(DeviceState
*dev
, Property
*props
)
654 while (props
->name
) {
656 qdev_prop_cpy(dev
, props
, props
->defval
);
662 static QTAILQ_HEAD(, GlobalProperty
) global_props
= QTAILQ_HEAD_INITIALIZER(global_props
);
664 static void qdev_prop_register_global(GlobalProperty
*prop
)
666 QTAILQ_INSERT_TAIL(&global_props
, prop
, next
);
669 void qdev_prop_register_global_list(GlobalProperty
*props
)
673 for (i
= 0; props
[i
].driver
!= NULL
; i
++) {
674 qdev_prop_register_global(props
+i
);
678 void qdev_prop_set_globals(DeviceState
*dev
)
680 GlobalProperty
*prop
;
682 QTAILQ_FOREACH(prop
, &global_props
, next
) {
683 if (strcmp(dev
->info
->name
, prop
->driver
) != 0 &&
684 strcmp(dev
->info
->bus_info
->name
, prop
->driver
) != 0) {
687 if (qdev_prop_parse(dev
, prop
->property
, prop
->value
) != 0) {
693 static int qdev_add_one_global(QemuOpts
*opts
, void *opaque
)
697 g
= qemu_mallocz(sizeof(*g
));
698 g
->driver
= qemu_opt_get(opts
, "driver");
699 g
->property
= qemu_opt_get(opts
, "property");
700 g
->value
= qemu_opt_get(opts
, "value");
701 qdev_prop_register_global(g
);
705 void qemu_add_globals(void)
707 qemu_opts_foreach(&qemu_global_opts
, qdev_add_one_global
, NULL
, 0);