pc: Set fw_cfg data based on APIC ID calculation
[qemu/rayw.git] / hw / qdev-properties-system.c
blobce0f7933e602df8c112c12c16e31b30693552637
1 /*
2 * qdev property parsing and global properties
3 * (parts specific for qemu-system-*)
5 * This file is based on code from hw/qdev-properties.c from
6 * commit 074a86fccd185616469dfcdc0e157f438aebba18,
7 * Copyright (c) Gerd Hoffmann <kraxel@redhat.com> and other contributors.
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include "net/net.h"
14 #include "qdev.h"
15 #include "qapi/qmp/qerror.h"
16 #include "sysemu/blockdev.h"
17 #include "hw/block-common.h"
18 #include "net/hub.h"
19 #include "qapi/visitor.h"
20 #include "char/char.h"
22 static void get_pointer(Object *obj, Visitor *v, Property *prop,
23 const char *(*print)(void *ptr),
24 const char *name, Error **errp)
26 DeviceState *dev = DEVICE(obj);
27 void **ptr = qdev_get_prop_ptr(dev, prop);
28 char *p;
30 p = (char *) (*ptr ? print(*ptr) : "");
31 visit_type_str(v, &p, name, errp);
34 static void set_pointer(Object *obj, Visitor *v, Property *prop,
35 int (*parse)(DeviceState *dev, const char *str,
36 void **ptr),
37 const char *name, Error **errp)
39 DeviceState *dev = DEVICE(obj);
40 Error *local_err = NULL;
41 void **ptr = qdev_get_prop_ptr(dev, prop);
42 char *str;
43 int ret;
45 if (dev->realized) {
46 error_set(errp, QERR_PERMISSION_DENIED);
47 return;
50 visit_type_str(v, &str, name, &local_err);
51 if (local_err) {
52 error_propagate(errp, local_err);
53 return;
55 if (!*str) {
56 g_free(str);
57 *ptr = NULL;
58 return;
60 ret = parse(dev, str, ptr);
61 error_set_from_qdev_prop_error(errp, ret, dev, prop, str);
62 g_free(str);
65 /* --- drive --- */
67 static int parse_drive(DeviceState *dev, const char *str, void **ptr)
69 BlockDriverState *bs;
71 bs = bdrv_find(str);
72 if (bs == NULL) {
73 return -ENOENT;
75 if (bdrv_attach_dev(bs, dev) < 0) {
76 return -EEXIST;
78 *ptr = bs;
79 return 0;
82 static void release_drive(Object *obj, const char *name, void *opaque)
84 DeviceState *dev = DEVICE(obj);
85 Property *prop = opaque;
86 BlockDriverState **ptr = qdev_get_prop_ptr(dev, prop);
88 if (*ptr) {
89 bdrv_detach_dev(*ptr, dev);
90 blockdev_auto_del(*ptr);
94 static const char *print_drive(void *ptr)
96 return bdrv_get_device_name(ptr);
99 static void get_drive(Object *obj, Visitor *v, void *opaque,
100 const char *name, Error **errp)
102 get_pointer(obj, v, opaque, print_drive, name, errp);
105 static void set_drive(Object *obj, Visitor *v, void *opaque,
106 const char *name, Error **errp)
108 set_pointer(obj, v, opaque, parse_drive, name, errp);
111 PropertyInfo qdev_prop_drive = {
112 .name = "drive",
113 .get = get_drive,
114 .set = set_drive,
115 .release = release_drive,
118 /* --- character device --- */
120 static int parse_chr(DeviceState *dev, const char *str, void **ptr)
122 CharDriverState *chr = qemu_chr_find(str);
123 if (chr == NULL) {
124 return -ENOENT;
126 if (chr->avail_connections < 1) {
127 return -EEXIST;
129 *ptr = chr;
130 --chr->avail_connections;
131 return 0;
134 static void release_chr(Object *obj, const char *name, void *opaque)
136 DeviceState *dev = DEVICE(obj);
137 Property *prop = opaque;
138 CharDriverState **ptr = qdev_get_prop_ptr(dev, prop);
140 if (*ptr) {
141 qemu_chr_add_handlers(*ptr, NULL, NULL, NULL, NULL);
146 static const char *print_chr(void *ptr)
148 CharDriverState *chr = ptr;
150 return chr->label ? chr->label : "";
153 static void get_chr(Object *obj, Visitor *v, void *opaque,
154 const char *name, Error **errp)
156 get_pointer(obj, v, opaque, print_chr, name, errp);
159 static void set_chr(Object *obj, Visitor *v, void *opaque,
160 const char *name, Error **errp)
162 set_pointer(obj, v, opaque, parse_chr, name, errp);
165 PropertyInfo qdev_prop_chr = {
166 .name = "chr",
167 .get = get_chr,
168 .set = set_chr,
169 .release = release_chr,
172 /* --- netdev device --- */
174 static int parse_netdev(DeviceState *dev, const char *str, void **ptr)
176 NetClientState *netdev = qemu_find_netdev(str);
178 if (netdev == NULL) {
179 return -ENOENT;
181 if (netdev->peer) {
182 return -EEXIST;
184 *ptr = netdev;
185 return 0;
188 static const char *print_netdev(void *ptr)
190 NetClientState *netdev = ptr;
192 return netdev->name ? netdev->name : "";
195 static void get_netdev(Object *obj, Visitor *v, void *opaque,
196 const char *name, Error **errp)
198 get_pointer(obj, v, opaque, print_netdev, name, errp);
201 static void set_netdev(Object *obj, Visitor *v, void *opaque,
202 const char *name, Error **errp)
204 set_pointer(obj, v, opaque, parse_netdev, name, errp);
207 PropertyInfo qdev_prop_netdev = {
208 .name = "netdev",
209 .get = get_netdev,
210 .set = set_netdev,
213 /* --- vlan --- */
215 static int print_vlan(DeviceState *dev, Property *prop, char *dest, size_t len)
217 NetClientState **ptr = qdev_get_prop_ptr(dev, prop);
219 if (*ptr) {
220 int id;
221 if (!net_hub_id_for_client(*ptr, &id)) {
222 return snprintf(dest, len, "%d", id);
226 return snprintf(dest, len, "<null>");
229 static void get_vlan(Object *obj, Visitor *v, void *opaque,
230 const char *name, Error **errp)
232 DeviceState *dev = DEVICE(obj);
233 Property *prop = opaque;
234 NetClientState **ptr = qdev_get_prop_ptr(dev, prop);
235 int32_t id = -1;
237 if (*ptr) {
238 int hub_id;
239 if (!net_hub_id_for_client(*ptr, &hub_id)) {
240 id = hub_id;
244 visit_type_int32(v, &id, name, errp);
247 static void set_vlan(Object *obj, Visitor *v, void *opaque,
248 const char *name, Error **errp)
250 DeviceState *dev = DEVICE(obj);
251 Property *prop = opaque;
252 NetClientState **ptr = qdev_get_prop_ptr(dev, prop);
253 Error *local_err = NULL;
254 int32_t id;
255 NetClientState *hubport;
257 if (dev->realized) {
258 error_set(errp, QERR_PERMISSION_DENIED);
259 return;
262 visit_type_int32(v, &id, name, &local_err);
263 if (local_err) {
264 error_propagate(errp, local_err);
265 return;
267 if (id == -1) {
268 *ptr = NULL;
269 return;
272 hubport = net_hub_port_find(id);
273 if (!hubport) {
274 error_set(errp, QERR_INVALID_PARAMETER_VALUE,
275 name, prop->info->name);
276 return;
278 *ptr = hubport;
281 PropertyInfo qdev_prop_vlan = {
282 .name = "vlan",
283 .print = print_vlan,
284 .get = get_vlan,
285 .set = set_vlan,
288 int qdev_prop_set_drive(DeviceState *dev, const char *name,
289 BlockDriverState *value)
291 Error *errp = NULL;
292 const char *bdrv_name = value ? bdrv_get_device_name(value) : "";
293 object_property_set_str(OBJECT(dev), bdrv_name,
294 name, &errp);
295 if (errp) {
296 qerror_report_err(errp);
297 error_free(errp);
298 return -1;
300 return 0;
303 void qdev_prop_set_drive_nofail(DeviceState *dev, const char *name,
304 BlockDriverState *value)
306 if (qdev_prop_set_drive(dev, name, value) < 0) {
307 exit(1);
310 void qdev_prop_set_chr(DeviceState *dev, const char *name,
311 CharDriverState *value)
313 Error *errp = NULL;
314 assert(!value || value->label);
315 object_property_set_str(OBJECT(dev),
316 value ? value->label : "", name, &errp);
317 assert_no_error(errp);
320 void qdev_prop_set_netdev(DeviceState *dev, const char *name,
321 NetClientState *value)
323 Error *errp = NULL;
324 assert(!value || value->name);
325 object_property_set_str(OBJECT(dev),
326 value ? value->name : "", name, &errp);
327 assert_no_error(errp);
330 void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd)
332 qdev_prop_set_macaddr(dev, "mac", nd->macaddr.a);
333 if (nd->netdev) {
334 qdev_prop_set_netdev(dev, "netdev", nd->netdev);
336 if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED &&
337 object_property_find(OBJECT(dev), "vectors", NULL)) {
338 qdev_prop_set_uint32(dev, "vectors", nd->nvectors);
340 nd->instantiated = 1;
343 static int qdev_add_one_global(QemuOpts *opts, void *opaque)
345 GlobalProperty *g;
347 g = g_malloc0(sizeof(*g));
348 g->driver = qemu_opt_get(opts, "driver");
349 g->property = qemu_opt_get(opts, "property");
350 g->value = qemu_opt_get(opts, "value");
351 qdev_prop_register_global(g);
352 return 0;
355 void qemu_add_globals(void)
357 qemu_opts_foreach(qemu_find_opts("global"), qdev_add_one_global, NULL, 0);