2009-07-31 14:25:40 +04:00
|
|
|
#include "sysemu.h"
|
2009-10-21 17:25:23 +04:00
|
|
|
#include "net.h"
|
2009-07-15 15:43:31 +04:00
|
|
|
#include "qdev.h"
|
2010-03-22 12:29:00 +03:00
|
|
|
#include "qerror.h"
|
2009-07-15 15:43:31 +04:00
|
|
|
|
|
|
|
void *qdev_get_prop_ptr(DeviceState *dev, Property *prop)
|
|
|
|
{
|
|
|
|
void *ptr = dev;
|
|
|
|
ptr += prop->offset;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2010-01-10 14:52:41 +03:00
|
|
|
static uint32_t qdev_get_prop_mask(Property *prop)
|
|
|
|
{
|
|
|
|
assert(prop->info->type == PROP_TYPE_BIT);
|
|
|
|
return 0x1 << prop->bitnr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bit_prop_set(DeviceState *dev, Property *props, bool val)
|
|
|
|
{
|
|
|
|
uint32_t *p = qdev_get_prop_ptr(dev, props);
|
|
|
|
uint32_t mask = qdev_get_prop_mask(props);
|
|
|
|
if (val)
|
2010-01-12 22:16:59 +03:00
|
|
|
*p |= mask;
|
2010-01-10 14:52:41 +03:00
|
|
|
else
|
|
|
|
*p &= ~mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void qdev_prop_cpy(DeviceState *dev, Property *props, void *src)
|
|
|
|
{
|
|
|
|
if (props->info->type == PROP_TYPE_BIT) {
|
|
|
|
bool *defval = src;
|
|
|
|
bit_prop_set(dev, props, *defval);
|
|
|
|
} else {
|
|
|
|
char *dst = qdev_get_prop_ptr(dev, props);
|
|
|
|
memcpy(dst, src, props->info->size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Bit */
|
|
|
|
static int parse_bit(DeviceState *dev, Property *prop, const char *str)
|
|
|
|
{
|
|
|
|
if (!strncasecmp(str, "on", 2))
|
|
|
|
bit_prop_set(dev, prop, true);
|
|
|
|
else if (!strncasecmp(str, "off", 3))
|
|
|
|
bit_prop_set(dev, prop, false);
|
|
|
|
else
|
2010-02-26 17:50:50 +03:00
|
|
|
return -EINVAL;
|
2010-01-10 14:52:41 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int print_bit(DeviceState *dev, Property *prop, char *dest, size_t len)
|
|
|
|
{
|
|
|
|
uint8_t *p = qdev_get_prop_ptr(dev, prop);
|
|
|
|
return snprintf(dest, len, (*p & qdev_get_prop_mask(prop)) ? "on" : "off");
|
|
|
|
}
|
|
|
|
|
|
|
|
PropertyInfo qdev_prop_bit = {
|
|
|
|
.name = "on/off",
|
|
|
|
.type = PROP_TYPE_BIT,
|
|
|
|
.size = sizeof(uint32_t),
|
|
|
|
.parse = parse_bit,
|
|
|
|
.print = print_bit,
|
|
|
|
};
|
|
|
|
|
2009-09-30 00:48:25 +04:00
|
|
|
/* --- 8bit integer --- */
|
|
|
|
|
|
|
|
static int parse_uint8(DeviceState *dev, Property *prop, const char *str)
|
|
|
|
{
|
|
|
|
uint8_t *ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
const char *fmt;
|
|
|
|
|
|
|
|
/* accept both hex and decimal */
|
|
|
|
fmt = strncasecmp(str, "0x",2) == 0 ? "%" PRIx8 : "%" PRIu8;
|
|
|
|
if (sscanf(str, fmt, ptr) != 1)
|
2010-02-26 17:50:50 +03:00
|
|
|
return -EINVAL;
|
2009-09-30 00:48:25 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int print_uint8(DeviceState *dev, Property *prop, char *dest, size_t len)
|
|
|
|
{
|
|
|
|
uint8_t *ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
return snprintf(dest, len, "%" PRIu8, *ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
PropertyInfo qdev_prop_uint8 = {
|
|
|
|
.name = "uint8",
|
|
|
|
.type = PROP_TYPE_UINT8,
|
|
|
|
.size = sizeof(uint8_t),
|
|
|
|
.parse = parse_uint8,
|
|
|
|
.print = print_uint8,
|
|
|
|
};
|
|
|
|
|
2009-07-15 15:43:31 +04:00
|
|
|
/* --- 16bit integer --- */
|
|
|
|
|
|
|
|
static int parse_uint16(DeviceState *dev, Property *prop, const char *str)
|
|
|
|
{
|
|
|
|
uint16_t *ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
const char *fmt;
|
|
|
|
|
|
|
|
/* accept both hex and decimal */
|
|
|
|
fmt = strncasecmp(str, "0x",2) == 0 ? "%" PRIx16 : "%" PRIu16;
|
|
|
|
if (sscanf(str, fmt, ptr) != 1)
|
2010-02-26 17:50:50 +03:00
|
|
|
return -EINVAL;
|
2009-07-15 15:43:31 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int print_uint16(DeviceState *dev, Property *prop, char *dest, size_t len)
|
|
|
|
{
|
|
|
|
uint16_t *ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
return snprintf(dest, len, "%" PRIu16, *ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
PropertyInfo qdev_prop_uint16 = {
|
|
|
|
.name = "uint16",
|
|
|
|
.type = PROP_TYPE_UINT16,
|
|
|
|
.size = sizeof(uint16_t),
|
|
|
|
.parse = parse_uint16,
|
|
|
|
.print = print_uint16,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* --- 32bit integer --- */
|
|
|
|
|
|
|
|
static int parse_uint32(DeviceState *dev, Property *prop, const char *str)
|
|
|
|
{
|
|
|
|
uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
const char *fmt;
|
|
|
|
|
|
|
|
/* accept both hex and decimal */
|
|
|
|
fmt = strncasecmp(str, "0x",2) == 0 ? "%" PRIx32 : "%" PRIu32;
|
|
|
|
if (sscanf(str, fmt, ptr) != 1)
|
2010-02-26 17:50:50 +03:00
|
|
|
return -EINVAL;
|
2009-07-15 15:43:31 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int print_uint32(DeviceState *dev, Property *prop, char *dest, size_t len)
|
|
|
|
{
|
|
|
|
uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
return snprintf(dest, len, "%" PRIu32, *ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
PropertyInfo qdev_prop_uint32 = {
|
|
|
|
.name = "uint32",
|
|
|
|
.type = PROP_TYPE_UINT32,
|
|
|
|
.size = sizeof(uint32_t),
|
|
|
|
.parse = parse_uint32,
|
|
|
|
.print = print_uint32,
|
|
|
|
};
|
|
|
|
|
2009-09-10 13:43:25 +04:00
|
|
|
static int parse_int32(DeviceState *dev, Property *prop, const char *str)
|
|
|
|
{
|
|
|
|
int32_t *ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
|
|
|
|
if (sscanf(str, "%" PRId32, ptr) != 1)
|
2010-02-26 17:50:50 +03:00
|
|
|
return -EINVAL;
|
2009-09-10 13:43:25 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int print_int32(DeviceState *dev, Property *prop, char *dest, size_t len)
|
|
|
|
{
|
|
|
|
int32_t *ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
return snprintf(dest, len, "%" PRId32, *ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
PropertyInfo qdev_prop_int32 = {
|
|
|
|
.name = "int32",
|
|
|
|
.type = PROP_TYPE_INT32,
|
|
|
|
.size = sizeof(int32_t),
|
|
|
|
.parse = parse_int32,
|
|
|
|
.print = print_int32,
|
|
|
|
};
|
|
|
|
|
2009-07-15 15:43:31 +04:00
|
|
|
/* --- 32bit hex value --- */
|
|
|
|
|
|
|
|
static int parse_hex32(DeviceState *dev, Property *prop, const char *str)
|
|
|
|
{
|
|
|
|
uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
|
|
|
|
if (sscanf(str, "%" PRIx32, ptr) != 1)
|
2010-02-26 17:50:50 +03:00
|
|
|
return -EINVAL;
|
2009-07-15 15:43:31 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int print_hex32(DeviceState *dev, Property *prop, char *dest, size_t len)
|
|
|
|
{
|
|
|
|
uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
return snprintf(dest, len, "0x%" PRIx32, *ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
PropertyInfo qdev_prop_hex32 = {
|
|
|
|
.name = "hex32",
|
|
|
|
.type = PROP_TYPE_UINT32,
|
|
|
|
.size = sizeof(uint32_t),
|
|
|
|
.parse = parse_hex32,
|
|
|
|
.print = print_hex32,
|
|
|
|
};
|
|
|
|
|
2009-07-21 15:10:41 +04:00
|
|
|
/* --- 64bit integer --- */
|
|
|
|
|
|
|
|
static int parse_uint64(DeviceState *dev, Property *prop, const char *str)
|
|
|
|
{
|
|
|
|
uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
const char *fmt;
|
|
|
|
|
|
|
|
/* accept both hex and decimal */
|
|
|
|
fmt = strncasecmp(str, "0x",2) == 0 ? "%" PRIx64 : "%" PRIu64;
|
|
|
|
if (sscanf(str, fmt, ptr) != 1)
|
2010-02-26 17:50:50 +03:00
|
|
|
return -EINVAL;
|
2009-07-21 15:10:41 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int print_uint64(DeviceState *dev, Property *prop, char *dest, size_t len)
|
|
|
|
{
|
|
|
|
uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
return snprintf(dest, len, "%" PRIu64, *ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
PropertyInfo qdev_prop_uint64 = {
|
|
|
|
.name = "uint64",
|
|
|
|
.type = PROP_TYPE_UINT64,
|
|
|
|
.size = sizeof(uint64_t),
|
|
|
|
.parse = parse_uint64,
|
|
|
|
.print = print_uint64,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* --- 64bit hex value --- */
|
|
|
|
|
|
|
|
static int parse_hex64(DeviceState *dev, Property *prop, const char *str)
|
|
|
|
{
|
|
|
|
uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
|
|
|
|
if (sscanf(str, "%" PRIx64, ptr) != 1)
|
2010-02-26 17:50:50 +03:00
|
|
|
return -EINVAL;
|
2009-07-21 15:10:41 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int print_hex64(DeviceState *dev, Property *prop, char *dest, size_t len)
|
|
|
|
{
|
|
|
|
uint64_t *ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
return snprintf(dest, len, "0x%" PRIx64, *ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
PropertyInfo qdev_prop_hex64 = {
|
|
|
|
.name = "hex64",
|
|
|
|
.type = PROP_TYPE_UINT64,
|
|
|
|
.size = sizeof(uint64_t),
|
|
|
|
.parse = parse_hex64,
|
|
|
|
.print = print_hex64,
|
|
|
|
};
|
|
|
|
|
2009-10-12 15:45:47 +04:00
|
|
|
/* --- string --- */
|
|
|
|
|
|
|
|
static int parse_string(DeviceState *dev, Property *prop, const char *str)
|
|
|
|
{
|
|
|
|
char **ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
|
|
|
|
if (*ptr)
|
|
|
|
qemu_free(*ptr);
|
|
|
|
*ptr = qemu_strdup(str);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int print_string(DeviceState *dev, Property *prop, char *dest, size_t len)
|
|
|
|
{
|
|
|
|
char **ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
if (!*ptr)
|
|
|
|
return snprintf(dest, len, "<null>");
|
|
|
|
return snprintf(dest, len, "\"%s\"", *ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
PropertyInfo qdev_prop_string = {
|
|
|
|
.name = "string",
|
|
|
|
.type = PROP_TYPE_STRING,
|
|
|
|
.size = sizeof(char*),
|
|
|
|
.parse = parse_string,
|
|
|
|
.print = print_string,
|
|
|
|
};
|
|
|
|
|
2009-07-31 14:25:40 +04:00
|
|
|
/* --- drive --- */
|
|
|
|
|
|
|
|
static int parse_drive(DeviceState *dev, Property *prop, const char *str)
|
|
|
|
{
|
|
|
|
DriveInfo **ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
|
|
|
|
*ptr = drive_get_by_id(str);
|
|
|
|
if (*ptr == NULL)
|
2010-02-26 17:50:50 +03:00
|
|
|
return -ENOENT;
|
2009-07-31 14:25:40 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int print_drive(DeviceState *dev, Property *prop, char *dest, size_t len)
|
|
|
|
{
|
|
|
|
DriveInfo **ptr = qdev_get_prop_ptr(dev, prop);
|
2009-09-22 15:53:20 +04:00
|
|
|
return snprintf(dest, len, "%s", (*ptr) ? (*ptr)->id : "<null>");
|
2009-07-31 14:25:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
PropertyInfo qdev_prop_drive = {
|
|
|
|
.name = "drive",
|
|
|
|
.type = PROP_TYPE_DRIVE,
|
|
|
|
.size = sizeof(DriveInfo*),
|
|
|
|
.parse = parse_drive,
|
|
|
|
.print = print_drive,
|
|
|
|
};
|
|
|
|
|
2009-08-03 19:35:18 +04:00
|
|
|
/* --- character device --- */
|
|
|
|
|
2009-09-10 12:58:53 +04:00
|
|
|
static int parse_chr(DeviceState *dev, Property *prop, const char *str)
|
|
|
|
{
|
|
|
|
CharDriverState **ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
|
|
|
|
*ptr = qemu_chr_find(str);
|
|
|
|
if (*ptr == NULL)
|
2010-02-26 17:50:50 +03:00
|
|
|
return -ENOENT;
|
2009-09-10 12:58:53 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-03 19:35:18 +04:00
|
|
|
static int print_chr(DeviceState *dev, Property *prop, char *dest, size_t len)
|
|
|
|
{
|
|
|
|
CharDriverState **ptr = qdev_get_prop_ptr(dev, prop);
|
2009-08-13 20:26:52 +04:00
|
|
|
|
|
|
|
if (*ptr && (*ptr)->label) {
|
|
|
|
return snprintf(dest, len, "%s", (*ptr)->label);
|
|
|
|
} else {
|
|
|
|
return snprintf(dest, len, "<null>");
|
|
|
|
}
|
2009-08-03 19:35:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
PropertyInfo qdev_prop_chr = {
|
|
|
|
.name = "chr",
|
|
|
|
.type = PROP_TYPE_CHR,
|
|
|
|
.size = sizeof(CharDriverState*),
|
2009-09-10 12:58:53 +04:00
|
|
|
.parse = parse_chr,
|
2009-08-03 19:35:18 +04:00
|
|
|
.print = print_chr,
|
|
|
|
};
|
|
|
|
|
2009-10-21 17:25:24 +04:00
|
|
|
/* --- netdev device --- */
|
|
|
|
|
|
|
|
static int parse_netdev(DeviceState *dev, Property *prop, const char *str)
|
|
|
|
{
|
|
|
|
VLANClientState **ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
|
|
|
|
*ptr = qemu_find_netdev(str);
|
|
|
|
if (*ptr == NULL)
|
2010-02-26 17:50:50 +03:00
|
|
|
return -ENOENT;
|
2010-02-26 17:50:51 +03:00
|
|
|
if ((*ptr)->peer) {
|
|
|
|
return -EEXIST;
|
|
|
|
}
|
2009-10-21 17:25:24 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int print_netdev(DeviceState *dev, Property *prop, char *dest, size_t len)
|
|
|
|
{
|
|
|
|
VLANClientState **ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
|
|
|
|
if (*ptr && (*ptr)->name) {
|
|
|
|
return snprintf(dest, len, "%s", (*ptr)->name);
|
|
|
|
} else {
|
|
|
|
return snprintf(dest, len, "<null>");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PropertyInfo qdev_prop_netdev = {
|
|
|
|
.name = "netdev",
|
|
|
|
.type = PROP_TYPE_NETDEV,
|
|
|
|
.size = sizeof(VLANClientState*),
|
|
|
|
.parse = parse_netdev,
|
|
|
|
.print = print_netdev,
|
|
|
|
};
|
|
|
|
|
2009-10-21 17:25:25 +04:00
|
|
|
/* --- vlan --- */
|
|
|
|
|
|
|
|
static int parse_vlan(DeviceState *dev, Property *prop, const char *str)
|
|
|
|
{
|
|
|
|
VLANState **ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
int id;
|
|
|
|
|
|
|
|
if (sscanf(str, "%d", &id) != 1)
|
2010-02-26 17:50:50 +03:00
|
|
|
return -EINVAL;
|
2009-10-21 17:25:25 +04:00
|
|
|
*ptr = qemu_find_vlan(id, 1);
|
|
|
|
if (*ptr == NULL)
|
2010-02-26 17:50:50 +03:00
|
|
|
return -ENOENT;
|
2009-10-21 17:25:25 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int print_vlan(DeviceState *dev, Property *prop, char *dest, size_t len)
|
|
|
|
{
|
|
|
|
VLANState **ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
|
|
|
|
if (*ptr) {
|
|
|
|
return snprintf(dest, len, "%d", (*ptr)->id);
|
|
|
|
} else {
|
|
|
|
return snprintf(dest, len, "<null>");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PropertyInfo qdev_prop_vlan = {
|
|
|
|
.name = "vlan",
|
|
|
|
.type = PROP_TYPE_VLAN,
|
|
|
|
.size = sizeof(VLANClientState*),
|
|
|
|
.parse = parse_vlan,
|
|
|
|
.print = print_vlan,
|
|
|
|
};
|
|
|
|
|
2009-07-15 15:43:31 +04:00
|
|
|
/* --- pointer --- */
|
|
|
|
|
2010-02-19 13:47:06 +03:00
|
|
|
/* Not a proper property, just for dirty hacks. TODO Remove it! */
|
2009-07-15 15:43:31 +04:00
|
|
|
PropertyInfo qdev_prop_ptr = {
|
|
|
|
.name = "ptr",
|
|
|
|
.type = PROP_TYPE_PTR,
|
|
|
|
.size = sizeof(void*),
|
|
|
|
};
|
|
|
|
|
|
|
|
/* --- mac address --- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* accepted syntax versions:
|
|
|
|
* 01:02:03:04:05:06
|
|
|
|
* 01-02-03-04-05-06
|
|
|
|
*/
|
|
|
|
static int parse_mac(DeviceState *dev, Property *prop, const char *str)
|
|
|
|
{
|
2009-10-21 17:25:23 +04:00
|
|
|
MACAddr *mac = qdev_get_prop_ptr(dev, prop);
|
2009-07-15 15:43:31 +04:00
|
|
|
int i, pos;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
for (i = 0, pos = 0; i < 6; i++, pos += 3) {
|
2009-07-17 21:48:02 +04:00
|
|
|
if (!qemu_isxdigit(str[pos]))
|
2010-02-26 17:50:50 +03:00
|
|
|
return -EINVAL;
|
2009-07-17 21:48:02 +04:00
|
|
|
if (!qemu_isxdigit(str[pos+1]))
|
2010-02-26 17:50:50 +03:00
|
|
|
return -EINVAL;
|
2009-10-21 17:25:23 +04:00
|
|
|
if (i == 5) {
|
|
|
|
if (str[pos+2] != '\0')
|
2010-02-26 17:50:50 +03:00
|
|
|
return -EINVAL;
|
2009-10-21 17:25:23 +04:00
|
|
|
} else {
|
|
|
|
if (str[pos+2] != ':' && str[pos+2] != '-')
|
2010-02-26 17:50:50 +03:00
|
|
|
return -EINVAL;
|
2009-10-21 17:25:23 +04:00
|
|
|
}
|
|
|
|
mac->a[i] = strtol(str+pos, &p, 16);
|
2009-07-15 15:43:31 +04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int print_mac(DeviceState *dev, Property *prop, char *dest, size_t len)
|
|
|
|
{
|
2009-10-21 17:25:23 +04:00
|
|
|
MACAddr *mac = qdev_get_prop_ptr(dev, prop);
|
|
|
|
|
2009-07-15 15:43:31 +04:00
|
|
|
return snprintf(dest, len, "%02x:%02x:%02x:%02x:%02x:%02x",
|
2009-10-21 17:25:23 +04:00
|
|
|
mac->a[0], mac->a[1], mac->a[2],
|
|
|
|
mac->a[3], mac->a[4], mac->a[5]);
|
2009-07-15 15:43:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
PropertyInfo qdev_prop_macaddr = {
|
2009-10-21 17:25:23 +04:00
|
|
|
.name = "macaddr",
|
2009-07-15 15:43:31 +04:00
|
|
|
.type = PROP_TYPE_MACADDR,
|
2009-10-21 17:25:23 +04:00
|
|
|
.size = sizeof(MACAddr),
|
2009-07-15 15:43:31 +04:00
|
|
|
.parse = parse_mac,
|
|
|
|
.print = print_mac,
|
|
|
|
};
|
|
|
|
|
2009-07-15 15:59:22 +04:00
|
|
|
/* --- pci address --- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* bus-local address, i.e. "$slot" or "$slot.$fn"
|
|
|
|
*/
|
|
|
|
static int parse_pci_devfn(DeviceState *dev, Property *prop, const char *str)
|
|
|
|
{
|
|
|
|
uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
unsigned int slot, fn, n;
|
|
|
|
|
|
|
|
if (sscanf(str, "%x.%x%n", &slot, &fn, &n) != 2) {
|
|
|
|
fn = 0;
|
|
|
|
if (sscanf(str, "%x%n", &slot, &n) != 1) {
|
2010-02-26 17:50:50 +03:00
|
|
|
return -EINVAL;
|
2009-07-15 15:59:22 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (str[n] != '\0')
|
2010-02-26 17:50:50 +03:00
|
|
|
return -EINVAL;
|
2009-07-15 15:59:22 +04:00
|
|
|
if (fn > 7)
|
2010-02-26 17:50:50 +03:00
|
|
|
return -EINVAL;
|
2009-07-15 15:59:22 +04:00
|
|
|
*ptr = slot << 3 | fn;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int print_pci_devfn(DeviceState *dev, Property *prop, char *dest, size_t len)
|
|
|
|
{
|
|
|
|
uint32_t *ptr = qdev_get_prop_ptr(dev, prop);
|
|
|
|
|
2009-08-28 23:12:16 +04:00
|
|
|
if (*ptr == -1) {
|
2009-07-15 15:59:22 +04:00
|
|
|
return snprintf(dest, len, "<unset>");
|
|
|
|
} else {
|
|
|
|
return snprintf(dest, len, "%02x.%x", *ptr >> 3, *ptr & 7);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PropertyInfo qdev_prop_pci_devfn = {
|
|
|
|
.name = "pci-devfn",
|
|
|
|
.type = PROP_TYPE_UINT32,
|
|
|
|
.size = sizeof(uint32_t),
|
|
|
|
.parse = parse_pci_devfn,
|
|
|
|
.print = print_pci_devfn,
|
|
|
|
};
|
|
|
|
|
2009-07-15 15:43:31 +04:00
|
|
|
/* --- public helpers --- */
|
|
|
|
|
|
|
|
static Property *qdev_prop_walk(Property *props, const char *name)
|
|
|
|
{
|
|
|
|
if (!props)
|
|
|
|
return NULL;
|
|
|
|
while (props->name) {
|
|
|
|
if (strcmp(props->name, name) == 0)
|
|
|
|
return props;
|
|
|
|
props++;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Property *qdev_prop_find(DeviceState *dev, const char *name)
|
|
|
|
{
|
|
|
|
Property *prop;
|
|
|
|
|
|
|
|
/* device properties */
|
|
|
|
prop = qdev_prop_walk(dev->info->props, name);
|
|
|
|
if (prop)
|
|
|
|
return prop;
|
|
|
|
|
|
|
|
/* bus properties */
|
|
|
|
prop = qdev_prop_walk(dev->parent_bus->info->props, name);
|
|
|
|
if (prop)
|
|
|
|
return prop;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-10-21 17:25:28 +04:00
|
|
|
int qdev_prop_exists(DeviceState *dev, const char *name)
|
|
|
|
{
|
|
|
|
return qdev_prop_find(dev, name) ? true : false;
|
|
|
|
}
|
|
|
|
|
2009-07-15 15:43:31 +04:00
|
|
|
int qdev_prop_parse(DeviceState *dev, const char *name, const char *value)
|
|
|
|
{
|
|
|
|
Property *prop;
|
2010-02-26 17:50:50 +03:00
|
|
|
int ret;
|
2009-07-15 15:43:31 +04:00
|
|
|
|
|
|
|
prop = qdev_prop_find(dev, name);
|
2010-02-19 13:47:06 +03:00
|
|
|
/*
|
|
|
|
* TODO Properties without a parse method are just for dirty
|
|
|
|
* hacks. qdev_prop_ptr is the only such PropertyInfo. It's
|
|
|
|
* marked for removal. The test !prop->info->parse should be
|
|
|
|
* removed along with it.
|
|
|
|
*/
|
|
|
|
if (!prop || !prop->info->parse) {
|
2010-02-19 15:20:23 +03:00
|
|
|
qerror_report(QERR_PROPERTY_NOT_FOUND, dev->info->name, name);
|
2009-07-15 15:43:31 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2010-02-26 17:50:50 +03:00
|
|
|
ret = prop->info->parse(dev, prop, value);
|
|
|
|
if (ret < 0) {
|
|
|
|
switch (ret) {
|
2010-02-26 17:50:51 +03:00
|
|
|
case -EEXIST:
|
2010-02-19 15:20:23 +03:00
|
|
|
qerror_report(QERR_PROPERTY_VALUE_IN_USE,
|
|
|
|
dev->info->name, name, value);
|
2010-02-26 17:50:51 +03:00
|
|
|
break;
|
2010-02-26 17:50:50 +03:00
|
|
|
default:
|
|
|
|
case -EINVAL:
|
2010-02-19 15:20:23 +03:00
|
|
|
qerror_report(QERR_PROPERTY_VALUE_BAD,
|
|
|
|
dev->info->name, name, value);
|
2010-02-26 17:50:50 +03:00
|
|
|
break;
|
|
|
|
case -ENOENT:
|
2010-02-19 15:20:23 +03:00
|
|
|
qerror_report(QERR_PROPERTY_VALUE_NOT_FOUND,
|
|
|
|
dev->info->name, name, value);
|
2010-02-26 17:50:50 +03:00
|
|
|
break;
|
|
|
|
}
|
2009-12-16 16:22:11 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
2009-07-15 15:43:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void qdev_prop_set(DeviceState *dev, const char *name, void *src, enum PropertyType type)
|
|
|
|
{
|
|
|
|
Property *prop;
|
|
|
|
|
|
|
|
prop = qdev_prop_find(dev, name);
|
|
|
|
if (!prop) {
|
|
|
|
fprintf(stderr, "%s: property \"%s.%s\" not found\n",
|
|
|
|
__FUNCTION__, dev->info->name, name);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
if (prop->info->type != type) {
|
|
|
|
fprintf(stderr, "%s: property \"%s.%s\" type mismatch\n",
|
|
|
|
__FUNCTION__, dev->info->name, name);
|
|
|
|
abort();
|
|
|
|
}
|
2010-01-10 14:52:41 +03:00
|
|
|
qdev_prop_cpy(dev, prop, src);
|
2009-07-15 15:43:31 +04:00
|
|
|
}
|
|
|
|
|
2009-09-30 00:48:25 +04:00
|
|
|
void qdev_prop_set_uint8(DeviceState *dev, const char *name, uint8_t value)
|
|
|
|
{
|
|
|
|
qdev_prop_set(dev, name, &value, PROP_TYPE_UINT8);
|
|
|
|
}
|
|
|
|
|
2009-07-15 15:43:31 +04:00
|
|
|
void qdev_prop_set_uint16(DeviceState *dev, const char *name, uint16_t value)
|
|
|
|
{
|
|
|
|
qdev_prop_set(dev, name, &value, PROP_TYPE_UINT16);
|
|
|
|
}
|
|
|
|
|
|
|
|
void qdev_prop_set_uint32(DeviceState *dev, const char *name, uint32_t value)
|
|
|
|
{
|
|
|
|
qdev_prop_set(dev, name, &value, PROP_TYPE_UINT32);
|
|
|
|
}
|
|
|
|
|
2009-09-10 13:43:25 +04:00
|
|
|
void qdev_prop_set_int32(DeviceState *dev, const char *name, int32_t value)
|
|
|
|
{
|
|
|
|
qdev_prop_set(dev, name, &value, PROP_TYPE_INT32);
|
|
|
|
}
|
|
|
|
|
2009-07-21 15:10:41 +04:00
|
|
|
void qdev_prop_set_uint64(DeviceState *dev, const char *name, uint64_t value)
|
|
|
|
{
|
|
|
|
qdev_prop_set(dev, name, &value, PROP_TYPE_UINT64);
|
|
|
|
}
|
|
|
|
|
2009-07-31 14:25:40 +04:00
|
|
|
void qdev_prop_set_drive(DeviceState *dev, const char *name, DriveInfo *value)
|
|
|
|
{
|
|
|
|
qdev_prop_set(dev, name, &value, PROP_TYPE_DRIVE);
|
|
|
|
}
|
|
|
|
|
2009-08-03 19:35:18 +04:00
|
|
|
void qdev_prop_set_chr(DeviceState *dev, const char *name, CharDriverState *value)
|
|
|
|
{
|
|
|
|
qdev_prop_set(dev, name, &value, PROP_TYPE_CHR);
|
|
|
|
}
|
|
|
|
|
2009-10-21 17:25:24 +04:00
|
|
|
void qdev_prop_set_netdev(DeviceState *dev, const char *name, VLANClientState *value)
|
|
|
|
{
|
|
|
|
qdev_prop_set(dev, name, &value, PROP_TYPE_NETDEV);
|
|
|
|
}
|
|
|
|
|
2009-10-21 17:25:25 +04:00
|
|
|
void qdev_prop_set_vlan(DeviceState *dev, const char *name, VLANState *value)
|
|
|
|
{
|
|
|
|
qdev_prop_set(dev, name, &value, PROP_TYPE_VLAN);
|
|
|
|
}
|
|
|
|
|
2009-10-21 17:25:23 +04:00
|
|
|
void qdev_prop_set_macaddr(DeviceState *dev, const char *name, uint8_t *value)
|
|
|
|
{
|
|
|
|
qdev_prop_set(dev, name, value, PROP_TYPE_MACADDR);
|
|
|
|
}
|
|
|
|
|
2009-07-15 15:43:31 +04:00
|
|
|
void qdev_prop_set_ptr(DeviceState *dev, const char *name, void *value)
|
|
|
|
{
|
|
|
|
qdev_prop_set(dev, name, &value, PROP_TYPE_PTR);
|
|
|
|
}
|
|
|
|
|
|
|
|
void qdev_prop_set_defaults(DeviceState *dev, Property *props)
|
|
|
|
{
|
|
|
|
if (!props)
|
|
|
|
return;
|
|
|
|
while (props->name) {
|
|
|
|
if (props->defval) {
|
2010-01-10 14:52:41 +03:00
|
|
|
qdev_prop_cpy(dev, props, props->defval);
|
2009-07-15 15:43:31 +04:00
|
|
|
}
|
|
|
|
props++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-08 15:11:33 +03:00
|
|
|
static QTAILQ_HEAD(, GlobalProperty) global_props = QTAILQ_HEAD_INITIALIZER(global_props);
|
2009-07-15 15:48:21 +04:00
|
|
|
|
2010-03-15 19:01:24 +03:00
|
|
|
static void qdev_prop_register_global(GlobalProperty *prop)
|
2009-07-15 15:48:21 +04:00
|
|
|
{
|
2009-12-08 15:11:33 +03:00
|
|
|
QTAILQ_INSERT_TAIL(&global_props, prop, next);
|
2009-07-15 15:48:21 +04:00
|
|
|
}
|
|
|
|
|
2009-12-08 15:11:33 +03:00
|
|
|
void qdev_prop_register_global_list(GlobalProperty *props)
|
2009-07-15 15:48:21 +04:00
|
|
|
{
|
2009-12-08 15:11:33 +03:00
|
|
|
int i;
|
2009-07-15 15:48:21 +04:00
|
|
|
|
2009-12-08 15:11:33 +03:00
|
|
|
for (i = 0; props[i].driver != NULL; i++) {
|
|
|
|
qdev_prop_register_global(props+i);
|
2009-07-15 15:48:21 +04:00
|
|
|
}
|
2009-12-08 15:11:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void qdev_prop_set_globals(DeviceState *dev)
|
|
|
|
{
|
|
|
|
GlobalProperty *prop;
|
|
|
|
|
|
|
|
QTAILQ_FOREACH(prop, &global_props, next) {
|
2009-12-08 15:11:35 +03:00
|
|
|
if (strcmp(dev->info->name, prop->driver) != 0 &&
|
|
|
|
strcmp(dev->info->bus_info->name, prop->driver) != 0) {
|
2009-07-15 15:48:21 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (qdev_prop_parse(dev, prop->property, prop->value) != 0) {
|
2009-12-16 16:22:11 +03:00
|
|
|
exit(1);
|
2009-07-15 15:48:21 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-03-15 19:01:24 +03:00
|
|
|
|
|
|
|
static int qdev_add_one_global(QemuOpts *opts, void *opaque)
|
|
|
|
{
|
|
|
|
GlobalProperty *g;
|
|
|
|
|
|
|
|
g = qemu_mallocz(sizeof(*g));
|
|
|
|
g->driver = qemu_opt_get(opts, "driver");
|
|
|
|
g->property = qemu_opt_get(opts, "property");
|
|
|
|
g->value = qemu_opt_get(opts, "value");
|
|
|
|
qdev_prop_register_global(g);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void qemu_add_globals(void)
|
|
|
|
{
|
|
|
|
qemu_opts_foreach(&qemu_global_opts, qdev_add_one_global, NULL, 0);
|
|
|
|
}
|