block: add logical_block_size property
[qemu.git] / hw / qdev-properties.c
blob92d6793747094a5111a508dab3ea38afc6a4164f
1 #include "sysemu.h"
2 #include "net.h"
3 #include "qdev.h"
5 void *qdev_get_prop_ptr(DeviceState *dev, Property *prop)
7 void *ptr = dev;
8 ptr += prop->offset;
9 return ptr;
12 static uint32_t qdev_get_prop_mask(Property *prop)
14 assert(prop->info->type == PROP_TYPE_BIT);
15 return 0x1 << prop->bitnr;
18 static void bit_prop_set(DeviceState *dev, Property *props, bool val)
20 uint32_t *p = qdev_get_prop_ptr(dev, props);
21 uint32_t mask = qdev_get_prop_mask(props);
22 if (val)
23 *p |= mask;
24 else
25 *p &= ~mask;
28 static void qdev_prop_cpy(DeviceState *dev, Property *props, void *src)
30 if (props->info->type == PROP_TYPE_BIT) {
31 bool *defval = src;
32 bit_prop_set(dev, props, *defval);
33 } else {
34 char *dst = qdev_get_prop_ptr(dev, props);
35 memcpy(dst, src, props->info->size);
39 /* Bit */
40 static int parse_bit(DeviceState *dev, Property *prop, const char *str)
42 if (!strncasecmp(str, "on", 2))
43 bit_prop_set(dev, prop, true);
44 else if (!strncasecmp(str, "off", 3))
45 bit_prop_set(dev, prop, false);
46 else
47 return -EINVAL;
48 return 0;
51 static int print_bit(DeviceState *dev, Property *prop, char *dest, size_t len)
53 uint8_t *p = qdev_get_prop_ptr(dev, prop);
54 return snprintf(dest, len, (*p & qdev_get_prop_mask(prop)) ? "on" : "off");
57 PropertyInfo qdev_prop_bit = {
58 .name = "on/off",
59 .type = PROP_TYPE_BIT,
60 .size = sizeof(uint32_t),
61 .parse = parse_bit,
62 .print = print_bit,
65 /* --- 8bit integer --- */
67 static int parse_uint8(DeviceState *dev, Property *prop, const char *str)
69 uint8_t *ptr = qdev_get_prop_ptr(dev, prop);
70 const char *fmt;
72 /* accept both hex and decimal */
73 fmt = strncasecmp(str, "0x",2) == 0 ? "%" PRIx8 : "%" PRIu8;
74 if (sscanf(str, fmt, ptr) != 1)
75 return -EINVAL;
76 return 0;
79 static int print_uint8(DeviceState *dev, Property *prop, char *dest, size_t len)
81 uint8_t *ptr = qdev_get_prop_ptr(dev, prop);
82 return snprintf(dest, len, "%" PRIu8, *ptr);
85 PropertyInfo qdev_prop_uint8 = {
86 .name = "uint8",
87 .type = PROP_TYPE_UINT8,
88 .size = sizeof(uint8_t),
89 .parse = parse_uint8,
90 .print = print_uint8,
93 /* --- 16bit integer --- */
95 static int parse_uint16(DeviceState *dev, Property *prop, const char *str)
97 uint16_t *ptr = qdev_get_prop_ptr(dev, prop);
98 const char *fmt;
100 /* accept both hex and decimal */
101 fmt = strncasecmp(str, "0x",2) == 0 ? "%" PRIx16 : "%" PRIu16;
102 if (sscanf(str, fmt, ptr) != 1)
103 return -EINVAL;
104 return 0;
107 static int print_uint16(DeviceState *dev, Property *prop, char *dest, size_t len)
109 uint16_t *ptr = qdev_get_prop_ptr(dev, prop);
110 return snprintf(dest, len, "%" PRIu16, *ptr);
113 PropertyInfo qdev_prop_uint16 = {
114 .name = "uint16",
115 .type = PROP_TYPE_UINT16,
116 .size = sizeof(uint16_t),
117 .parse = parse_uint16,
118 .print = print_uint16,
121 /* --- 32bit integer --- */
123 static int parse_uint32(DeviceState *dev, Property *prop, const char *str)
125 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
126 const char *fmt;
128 /* accept both hex and decimal */
129 fmt = strncasecmp(str, "0x",2) == 0 ? "%" PRIx32 : "%" PRIu32;
130 if (sscanf(str, fmt, ptr) != 1)
131 return -EINVAL;
132 return 0;
135 static int print_uint32(DeviceState *dev, Property *prop, char *dest, size_t len)
137 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
138 return snprintf(dest, len, "%" PRIu32, *ptr);
141 PropertyInfo qdev_prop_uint32 = {
142 .name = "uint32",
143 .type = PROP_TYPE_UINT32,
144 .size = sizeof(uint32_t),
145 .parse = parse_uint32,
146 .print = print_uint32,
149 static int parse_int32(DeviceState *dev, Property *prop, const char *str)
151 int32_t *ptr = qdev_get_prop_ptr(dev, prop);
153 if (sscanf(str, "%" PRId32, ptr) != 1)
154 return -EINVAL;
155 return 0;
158 static int print_int32(DeviceState *dev, Property *prop, char *dest, size_t len)
160 int32_t *ptr = qdev_get_prop_ptr(dev, prop);
161 return snprintf(dest, len, "%" PRId32, *ptr);
164 PropertyInfo qdev_prop_int32 = {
165 .name = "int32",
166 .type = PROP_TYPE_INT32,
167 .size = sizeof(int32_t),
168 .parse = parse_int32,
169 .print = print_int32,
172 /* --- 32bit hex value --- */
174 static int parse_hex32(DeviceState *dev, Property *prop, const char *str)
176 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
178 if (sscanf(str, "%" PRIx32, ptr) != 1)
179 return -EINVAL;
180 return 0;
183 static int print_hex32(DeviceState *dev, Property *prop, char *dest, size_t len)
185 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
186 return snprintf(dest, len, "0x%" PRIx32, *ptr);
189 PropertyInfo qdev_prop_hex32 = {
190 .name = "hex32",
191 .type = PROP_TYPE_UINT32,
192 .size = sizeof(uint32_t),
193 .parse = parse_hex32,
194 .print = print_hex32,
197 /* --- 64bit integer --- */
199 static int parse_uint64(DeviceState *dev, Property *prop, const char *str)
201 uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
202 const char *fmt;
204 /* accept both hex and decimal */
205 fmt = strncasecmp(str, "0x",2) == 0 ? "%" PRIx64 : "%" PRIu64;
206 if (sscanf(str, fmt, ptr) != 1)
207 return -EINVAL;
208 return 0;
211 static int print_uint64(DeviceState *dev, Property *prop, char *dest, size_t len)
213 uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
214 return snprintf(dest, len, "%" PRIu64, *ptr);
217 PropertyInfo qdev_prop_uint64 = {
218 .name = "uint64",
219 .type = PROP_TYPE_UINT64,
220 .size = sizeof(uint64_t),
221 .parse = parse_uint64,
222 .print = print_uint64,
225 /* --- 64bit hex value --- */
227 static int parse_hex64(DeviceState *dev, Property *prop, const char *str)
229 uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
231 if (sscanf(str, "%" PRIx64, ptr) != 1)
232 return -EINVAL;
233 return 0;
236 static int print_hex64(DeviceState *dev, Property *prop, char *dest, size_t len)
238 uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
239 return snprintf(dest, len, "0x%" PRIx64, *ptr);
242 PropertyInfo qdev_prop_hex64 = {
243 .name = "hex64",
244 .type = PROP_TYPE_UINT64,
245 .size = sizeof(uint64_t),
246 .parse = parse_hex64,
247 .print = print_hex64,
250 /* --- string --- */
252 static int parse_string(DeviceState *dev, Property *prop, const char *str)
254 char **ptr = qdev_get_prop_ptr(dev, prop);
256 if (*ptr)
257 qemu_free(*ptr);
258 *ptr = qemu_strdup(str);
259 return 0;
262 static int print_string(DeviceState *dev, Property *prop, char *dest, size_t len)
264 char **ptr = qdev_get_prop_ptr(dev, prop);
265 if (!*ptr)
266 return snprintf(dest, len, "<null>");
267 return snprintf(dest, len, "\"%s\"", *ptr);
270 PropertyInfo qdev_prop_string = {
271 .name = "string",
272 .type = PROP_TYPE_STRING,
273 .size = sizeof(char*),
274 .parse = parse_string,
275 .print = print_string,
278 /* --- drive --- */
280 static int parse_drive(DeviceState *dev, Property *prop, const char *str)
282 DriveInfo **ptr = qdev_get_prop_ptr(dev, prop);
284 *ptr = drive_get_by_id(str);
285 if (*ptr == NULL)
286 return -ENOENT;
287 return 0;
290 static int print_drive(DeviceState *dev, Property *prop, char *dest, size_t len)
292 DriveInfo **ptr = qdev_get_prop_ptr(dev, prop);
293 return snprintf(dest, len, "%s", (*ptr) ? (*ptr)->id : "<null>");
296 PropertyInfo qdev_prop_drive = {
297 .name = "drive",
298 .type = PROP_TYPE_DRIVE,
299 .size = sizeof(DriveInfo*),
300 .parse = parse_drive,
301 .print = print_drive,
304 /* --- character device --- */
306 static int parse_chr(DeviceState *dev, Property *prop, const char *str)
308 CharDriverState **ptr = qdev_get_prop_ptr(dev, prop);
310 *ptr = qemu_chr_find(str);
311 if (*ptr == NULL)
312 return -ENOENT;
313 return 0;
316 static int print_chr(DeviceState *dev, Property *prop, char *dest, size_t len)
318 CharDriverState **ptr = qdev_get_prop_ptr(dev, prop);
320 if (*ptr && (*ptr)->label) {
321 return snprintf(dest, len, "%s", (*ptr)->label);
322 } else {
323 return snprintf(dest, len, "<null>");
327 PropertyInfo qdev_prop_chr = {
328 .name = "chr",
329 .type = PROP_TYPE_CHR,
330 .size = sizeof(CharDriverState*),
331 .parse = parse_chr,
332 .print = print_chr,
335 /* --- netdev device --- */
337 static int parse_netdev(DeviceState *dev, Property *prop, const char *str)
339 VLANClientState **ptr = qdev_get_prop_ptr(dev, prop);
341 *ptr = qemu_find_netdev(str);
342 if (*ptr == NULL)
343 return -ENOENT;
344 if ((*ptr)->peer) {
345 return -EEXIST;
347 return 0;
350 static int print_netdev(DeviceState *dev, Property *prop, char *dest, size_t len)
352 VLANClientState **ptr = qdev_get_prop_ptr(dev, prop);
354 if (*ptr && (*ptr)->name) {
355 return snprintf(dest, len, "%s", (*ptr)->name);
356 } else {
357 return snprintf(dest, len, "<null>");
361 PropertyInfo qdev_prop_netdev = {
362 .name = "netdev",
363 .type = PROP_TYPE_NETDEV,
364 .size = sizeof(VLANClientState*),
365 .parse = parse_netdev,
366 .print = print_netdev,
369 /* --- vlan --- */
371 static int parse_vlan(DeviceState *dev, Property *prop, const char *str)
373 VLANState **ptr = qdev_get_prop_ptr(dev, prop);
374 int id;
376 if (sscanf(str, "%d", &id) != 1)
377 return -EINVAL;
378 *ptr = qemu_find_vlan(id, 1);
379 if (*ptr == NULL)
380 return -ENOENT;
381 return 0;
384 static int print_vlan(DeviceState *dev, Property *prop, char *dest, size_t len)
386 VLANState **ptr = qdev_get_prop_ptr(dev, prop);
388 if (*ptr) {
389 return snprintf(dest, len, "%d", (*ptr)->id);
390 } else {
391 return snprintf(dest, len, "<null>");
395 PropertyInfo qdev_prop_vlan = {
396 .name = "vlan",
397 .type = PROP_TYPE_VLAN,
398 .size = sizeof(VLANClientState*),
399 .parse = parse_vlan,
400 .print = print_vlan,
403 /* --- pointer --- */
405 /* Not a proper property, just for dirty hacks. TODO Remove it! */
406 PropertyInfo qdev_prop_ptr = {
407 .name = "ptr",
408 .type = PROP_TYPE_PTR,
409 .size = sizeof(void*),
412 /* --- mac address --- */
415 * accepted syntax versions:
416 * 01:02:03:04:05:06
417 * 01-02-03-04-05-06
419 static int parse_mac(DeviceState *dev, Property *prop, const char *str)
421 MACAddr *mac = qdev_get_prop_ptr(dev, prop);
422 int i, pos;
423 char *p;
425 for (i = 0, pos = 0; i < 6; i++, pos += 3) {
426 if (!qemu_isxdigit(str[pos]))
427 return -EINVAL;
428 if (!qemu_isxdigit(str[pos+1]))
429 return -EINVAL;
430 if (i == 5) {
431 if (str[pos+2] != '\0')
432 return -EINVAL;
433 } else {
434 if (str[pos+2] != ':' && str[pos+2] != '-')
435 return -EINVAL;
437 mac->a[i] = strtol(str+pos, &p, 16);
439 return 0;
442 static int print_mac(DeviceState *dev, Property *prop, char *dest, size_t len)
444 MACAddr *mac = qdev_get_prop_ptr(dev, prop);
446 return snprintf(dest, len, "%02x:%02x:%02x:%02x:%02x:%02x",
447 mac->a[0], mac->a[1], mac->a[2],
448 mac->a[3], mac->a[4], mac->a[5]);
451 PropertyInfo qdev_prop_macaddr = {
452 .name = "macaddr",
453 .type = PROP_TYPE_MACADDR,
454 .size = sizeof(MACAddr),
455 .parse = parse_mac,
456 .print = print_mac,
459 /* --- pci address --- */
462 * bus-local address, i.e. "$slot" or "$slot.$fn"
464 static int parse_pci_devfn(DeviceState *dev, Property *prop, const char *str)
466 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
467 unsigned int slot, fn, n;
469 if (sscanf(str, "%x.%x%n", &slot, &fn, &n) != 2) {
470 fn = 0;
471 if (sscanf(str, "%x%n", &slot, &n) != 1) {
472 return -EINVAL;
475 if (str[n] != '\0')
476 return -EINVAL;
477 if (fn > 7)
478 return -EINVAL;
479 *ptr = slot << 3 | fn;
480 return 0;
483 static int print_pci_devfn(DeviceState *dev, Property *prop, char *dest, size_t len)
485 uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
487 if (*ptr == -1) {
488 return snprintf(dest, len, "<unset>");
489 } else {
490 return snprintf(dest, len, "%02x.%x", *ptr >> 3, *ptr & 7);
494 PropertyInfo qdev_prop_pci_devfn = {
495 .name = "pci-devfn",
496 .type = PROP_TYPE_UINT32,
497 .size = sizeof(uint32_t),
498 .parse = parse_pci_devfn,
499 .print = print_pci_devfn,
502 /* --- public helpers --- */
504 static Property *qdev_prop_walk(Property *props, const char *name)
506 if (!props)
507 return NULL;
508 while (props->name) {
509 if (strcmp(props->name, name) == 0)
510 return props;
511 props++;
513 return NULL;
516 static Property *qdev_prop_find(DeviceState *dev, const char *name)
518 Property *prop;
520 /* device properties */
521 prop = qdev_prop_walk(dev->info->props, name);
522 if (prop)
523 return prop;
525 /* bus properties */
526 prop = qdev_prop_walk(dev->parent_bus->info->props, name);
527 if (prop)
528 return prop;
530 return NULL;
533 int qdev_prop_exists(DeviceState *dev, const char *name)
535 return qdev_prop_find(dev, name) ? true : false;
538 int qdev_prop_parse(DeviceState *dev, const char *name, const char *value)
540 Property *prop;
541 int ret;
543 prop = qdev_prop_find(dev, name);
545 * TODO Properties without a parse method are just for dirty
546 * hacks. qdev_prop_ptr is the only such PropertyInfo. It's
547 * marked for removal. The test !prop->info->parse should be
548 * removed along with it.
550 if (!prop || !prop->info->parse) {
551 qerror_report(QERR_PROPERTY_NOT_FOUND, dev->info->name, name);
552 return -1;
554 ret = prop->info->parse(dev, prop, value);
555 if (ret < 0) {
556 switch (ret) {
557 case -EEXIST:
558 qerror_report(QERR_PROPERTY_VALUE_IN_USE,
559 dev->info->name, name, value);
560 break;
561 default:
562 case -EINVAL:
563 qerror_report(QERR_PROPERTY_VALUE_BAD,
564 dev->info->name, name, value);
565 break;
566 case -ENOENT:
567 qerror_report(QERR_PROPERTY_VALUE_NOT_FOUND,
568 dev->info->name, name, value);
569 break;
571 return -1;
573 return 0;
576 void qdev_prop_set(DeviceState *dev, const char *name, void *src, enum PropertyType type)
578 Property *prop;
580 prop = qdev_prop_find(dev, name);
581 if (!prop) {
582 fprintf(stderr, "%s: property \"%s.%s\" not found\n",
583 __FUNCTION__, dev->info->name, name);
584 abort();
586 if (prop->info->type != type) {
587 fprintf(stderr, "%s: property \"%s.%s\" type mismatch\n",
588 __FUNCTION__, dev->info->name, name);
589 abort();
591 qdev_prop_cpy(dev, prop, src);
594 void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value)
596 qdev_prop_set(dev, name, &value, PROP_TYPE_UINT8);
599 void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value)
601 qdev_prop_set(dev, name, &value, PROP_TYPE_UINT16);
604 void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value)
606 qdev_prop_set(dev, name, &value, PROP_TYPE_UINT32);
609 void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value)
611 qdev_prop_set(dev, name, &value, PROP_TYPE_INT32);
614 void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value)
616 qdev_prop_set(dev, name, &value, PROP_TYPE_UINT64);
619 void qdev_prop_set_drive(DeviceState *dev, const char *name, DriveInfo *value)
621 qdev_prop_set(dev, name, &value, PROP_TYPE_DRIVE);
624 void qdev_prop_set_chr(DeviceState *dev, const char *name, CharDriverState *value)
626 qdev_prop_set(dev, name, &value, PROP_TYPE_CHR);
629 void qdev_prop_set_netdev(DeviceState *dev, const char *name, VLANClientState *value)
631 qdev_prop_set(dev, name, &value, PROP_TYPE_NETDEV);
634 void qdev_prop_set_vlan(DeviceState *dev, const char *name, VLANState *value)
636 qdev_prop_set(dev, name, &value, PROP_TYPE_VLAN);
639 void qdev_prop_set_macaddr(DeviceState *dev, const char *name, uint8_t *value)
641 qdev_prop_set(dev, name, value, PROP_TYPE_MACADDR);
644 void qdev_prop_set_ptr(DeviceState *dev, const char *name, void *value)
646 qdev_prop_set(dev, name, &value, PROP_TYPE_PTR);
649 void qdev_prop_set_defaults(DeviceState *dev, Property *props)
651 if (!props)
652 return;
653 while (props->name) {
654 if (props->defval) {
655 qdev_prop_cpy(dev, props, props->defval);
657 props++;
661 static QTAILQ_HEAD(, GlobalProperty) global_props = QTAILQ_HEAD_INITIALIZER(global_props);
663 void qdev_prop_register_global(GlobalProperty *prop)
665 QTAILQ_INSERT_TAIL(&global_props, prop, next);
668 void qdev_prop_register_global_list(GlobalProperty *props)
670 int i;
672 for (i = 0; props[i].driver != NULL; i++) {
673 qdev_prop_register_global(props+i);
677 void qdev_prop_set_globals(DeviceState *dev)
679 GlobalProperty *prop;
681 QTAILQ_FOREACH(prop, &global_props, next) {
682 if (strcmp(dev->info->name, prop->driver) != 0 &&
683 strcmp(dev->info->bus_info->name, prop->driver) != 0) {
684 continue;
686 if (qdev_prop_parse(dev, prop->property, prop->value) != 0) {
687 exit(1);