blob: be6a07ee4996f09c916282afc5e82058286f1be4 [file] [log] [blame]
Anthony Liguoriee46d8a2011-12-22 15:24:20 -06001/*
2 * Dynamic device configuration and creation.
3 *
4 * Copyright (c) 2009 CodeSourcery
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19
Peter Maydelld38ea872016-01-29 17:50:05 +000020#include "qemu/osdep.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010021#include "hw/qdev.h"
Andreas Färber2f7bd822013-04-16 03:50:21 +020022#include "hw/sysbus.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010023#include "monitor/monitor.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +010024#include "monitor/qdev.h"
Luiz Capitulinoa15fef22012-03-29 12:38:50 -030025#include "qmp-commands.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010026#include "sysemu/arch_init.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010027#include "qapi/qmp/qerror.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010028#include "qemu/config-file.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010029#include "qemu/error-report.h"
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060030
31/*
32 * Aliases were a bad idea from the start. Let's keep them
33 * from spreading further.
34 */
35typedef struct QDevAlias
36{
37 const char *typename;
38 const char *alias;
Alexander Graf5f629d92012-05-18 02:36:26 +020039 uint32_t arch_mask;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060040} QDevAlias;
41
Sascha Silbe36e99162016-02-18 22:44:13 +010042/* Please keep this table sorted by typename. */
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060043static const QDevAlias qdev_alias_table[] = {
Sascha Silbe36e99162016-02-18 22:44:13 +010044 { "e1000", "e1000-82540em" },
45 { "ich9-ahci", "ahci" },
46 { "kvm-pci-assign", "pci-assign" },
47 { "lsi53c895a", "lsi" },
Sascha Silbe588c36c2016-02-18 22:44:14 +010048 { "virtio-9p-ccw", "virtio-9p", QEMU_ARCH_S390X },
49 { "virtio-9p-pci", "virtio-9p", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
50 { "virtio-balloon-ccw", "virtio-balloon", QEMU_ARCH_S390X },
Alexander Graf5f629d92012-05-18 02:36:26 +020051 { "virtio-balloon-pci", "virtio-balloon",
52 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
Alexander Graf1f68f1d2015-06-16 23:06:33 +020053 { "virtio-blk-ccw", "virtio-blk", QEMU_ARCH_S390X },
Sascha Silbe36e99162016-02-18 22:44:13 +010054 { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
Sascha Silbe588c36c2016-02-18 22:44:14 +010055 { "virtio-gpu-ccw", "virtio-gpu", QEMU_ARCH_S390X },
56 { "virtio-gpu-pci", "virtio-gpu", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
57 { "virtio-input-host-ccw", "virtio-input-host", QEMU_ARCH_S390X },
58 { "virtio-input-host-pci", "virtio-input-host",
59 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
60 { "virtio-keyboard-ccw", "virtio-keyboard", QEMU_ARCH_S390X },
61 { "virtio-keyboard-pci", "virtio-keyboard",
62 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
63 { "virtio-mouse-ccw", "virtio-mouse", QEMU_ARCH_S390X },
64 { "virtio-mouse-pci", "virtio-mouse", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
Alexander Graf1f68f1d2015-06-16 23:06:33 +020065 { "virtio-net-ccw", "virtio-net", QEMU_ARCH_S390X },
Sascha Silbe36e99162016-02-18 22:44:13 +010066 { "virtio-net-pci", "virtio-net", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
Sascha Silbe588c36c2016-02-18 22:44:14 +010067 { "virtio-rng-ccw", "virtio-rng", QEMU_ARCH_S390X },
68 { "virtio-rng-pci", "virtio-rng", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
69 { "virtio-scsi-ccw", "virtio-scsi", QEMU_ARCH_S390X },
70 { "virtio-scsi-pci", "virtio-scsi", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
Alexander Graf1f68f1d2015-06-16 23:06:33 +020071 { "virtio-serial-ccw", "virtio-serial", QEMU_ARCH_S390X },
Sascha Silbe36e99162016-02-18 22:44:13 +010072 { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
Sascha Silbe588c36c2016-02-18 22:44:14 +010073 { "virtio-tablet-ccw", "virtio-tablet", QEMU_ARCH_S390X },
74 { "virtio-tablet-pci", "virtio-tablet", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060075 { }
76};
77
78static const char *qdev_class_get_alias(DeviceClass *dc)
79{
80 const char *typename = object_class_get_name(OBJECT_CLASS(dc));
81 int i;
82
83 for (i = 0; qdev_alias_table[i].typename; i++) {
Alexander Graf5f629d92012-05-18 02:36:26 +020084 if (qdev_alias_table[i].arch_mask &&
85 !(qdev_alias_table[i].arch_mask & arch_type)) {
86 continue;
87 }
88
Anthony Liguoriee46d8a2011-12-22 15:24:20 -060089 if (strcmp(qdev_alias_table[i].typename, typename) == 0) {
90 return qdev_alias_table[i].alias;
91 }
92 }
93
94 return NULL;
95}
96
97static bool qdev_class_has_alias(DeviceClass *dc)
98{
99 return (qdev_class_get_alias(dc) != NULL);
100}
101
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200102static void qdev_print_devinfo(DeviceClass *dc)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600103{
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200104 error_printf("name \"%s\"", object_class_get_name(OBJECT_CLASS(dc)));
Anthony Liguori0d936922012-05-02 09:00:20 +0200105 if (dc->bus_type) {
106 error_printf(", bus %s", dc->bus_type);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600107 }
108 if (qdev_class_has_alias(dc)) {
109 error_printf(", alias \"%s\"", qdev_class_get_alias(dc));
110 }
111 if (dc->desc) {
112 error_printf(", desc \"%s\"", dc->desc);
113 }
Markus Armbrusterefec3dd2013-11-28 17:26:54 +0100114 if (dc->cannot_instantiate_with_device_add_yet) {
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600115 error_printf(", no-user");
116 }
117 error_printf("\n");
118}
119
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200120static gint devinfo_cmp(gconstpointer a, gconstpointer b)
121{
122 return strcasecmp(object_class_get_name((ObjectClass *)a),
123 object_class_get_name((ObjectClass *)b));
124}
125
126static void qdev_print_devinfos(bool show_no_user)
127{
128 static const char *cat_name[DEVICE_CATEGORY_MAX + 1] = {
129 [DEVICE_CATEGORY_BRIDGE] = "Controller/Bridge/Hub",
130 [DEVICE_CATEGORY_USB] = "USB",
131 [DEVICE_CATEGORY_STORAGE] = "Storage",
132 [DEVICE_CATEGORY_NETWORK] = "Network",
133 [DEVICE_CATEGORY_INPUT] = "Input",
134 [DEVICE_CATEGORY_DISPLAY] = "Display",
135 [DEVICE_CATEGORY_SOUND] = "Sound",
136 [DEVICE_CATEGORY_MISC] = "Misc",
137 [DEVICE_CATEGORY_MAX] = "Uncategorized",
138 };
139 GSList *list, *elt;
140 int i;
141 bool cat_printed;
142
143 list = g_slist_sort(object_class_get_list(TYPE_DEVICE, false),
144 devinfo_cmp);
145
146 for (i = 0; i <= DEVICE_CATEGORY_MAX; i++) {
147 cat_printed = false;
148 for (elt = list; elt; elt = elt->next) {
149 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
150 TYPE_DEVICE);
151 if ((i < DEVICE_CATEGORY_MAX
152 ? !test_bit(i, dc->categories)
153 : !bitmap_empty(dc->categories, DEVICE_CATEGORY_MAX))
Markus Armbrusterefec3dd2013-11-28 17:26:54 +0100154 || (!show_no_user
155 && dc->cannot_instantiate_with_device_add_yet)) {
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200156 continue;
157 }
158 if (!cat_printed) {
159 error_printf("%s%s devices:\n", i ? "\n" : "",
160 cat_name[i]);
161 cat_printed = true;
162 }
163 qdev_print_devinfo(dc);
164 }
165 }
166
167 g_slist_free(list);
168}
169
Markus Armbruster71df1d82015-03-12 08:40:25 +0100170static int set_property(void *opaque, const char *name, const char *value,
171 Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600172{
Paolo Bonzini98a65282014-02-08 11:01:49 +0100173 Object *obj = opaque;
Andreas Färberb1fe9bc2013-05-01 16:10:24 +0200174 Error *err = NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600175
176 if (strcmp(name, "driver") == 0)
177 return 0;
178 if (strcmp(name, "bus") == 0)
179 return 0;
180
Paolo Bonzini98a65282014-02-08 11:01:49 +0100181 object_property_parse(obj, value, name, &err);
Andreas Färberb1fe9bc2013-05-01 16:10:24 +0200182 if (err != NULL) {
Markus Armbruster4caa4892015-03-12 13:58:02 +0100183 error_propagate(errp, err);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600184 return -1;
185 }
186 return 0;
187}
188
189static const char *find_typename_by_alias(const char *alias)
190{
191 int i;
192
193 for (i = 0; qdev_alias_table[i].alias; i++) {
Alexander Graf5f629d92012-05-18 02:36:26 +0200194 if (qdev_alias_table[i].arch_mask &&
195 !(qdev_alias_table[i].arch_mask & arch_type)) {
196 continue;
197 }
198
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600199 if (strcmp(qdev_alias_table[i].alias, alias) == 0) {
200 return qdev_alias_table[i].typename;
201 }
202 }
203
204 return NULL;
205}
206
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200207static DeviceClass *qdev_get_device_class(const char **driver, Error **errp)
208{
209 ObjectClass *oc;
210 DeviceClass *dc;
Sascha Silbef6b53192016-02-18 22:44:12 +0100211 const char *original_name = *driver;
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200212
213 oc = object_class_by_name(*driver);
214 if (!oc) {
215 const char *typename = find_typename_by_alias(*driver);
216
217 if (typename) {
218 *driver = typename;
219 oc = object_class_by_name(*driver);
220 }
221 }
222
223 if (!object_class_dynamic_cast(oc, TYPE_DEVICE)) {
Sascha Silbef6b53192016-02-18 22:44:12 +0100224 if (*driver != original_name) {
225 error_setg(errp, "'%s' (alias '%s') is not a valid device model"
226 " name", original_name, *driver);
227 } else {
228 error_setg(errp, "'%s' is not a valid device model name", *driver);
229 }
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200230 return NULL;
231 }
232
233 if (object_class_is_abstract(oc)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100234 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
235 "non-abstract device type");
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200236 return NULL;
237 }
238
239 dc = DEVICE_CLASS(oc);
240 if (dc->cannot_instantiate_with_device_add_yet ||
241 (qdev_hotplug && !dc->hotpluggable)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100242 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
243 "pluggable device type");
Eduardo Habkost43c95d72014-11-01 13:56:09 -0200244 return NULL;
245 }
246
247 return dc;
248}
249
250
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600251int qdev_device_help(QemuOpts *opts)
252{
Stefan Hajnoczief523582014-07-09 14:01:32 +0200253 Error *local_err = NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600254 const char *driver;
Stefan Hajnoczief523582014-07-09 14:01:32 +0200255 DevicePropertyInfoList *prop_list;
256 DevicePropertyInfoList *prop;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600257
258 driver = qemu_opt_get(opts, "driver");
Peter Maydellc8057f92012-08-02 13:45:54 +0100259 if (driver && is_help_option(driver)) {
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200260 qdev_print_devinfos(false);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600261 return 1;
262 }
263
Peter Maydellc8057f92012-08-02 13:45:54 +0100264 if (!driver || !qemu_opt_has_help_opt(opts)) {
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600265 return 0;
266 }
267
Markus Armbruster33fe9682015-10-01 10:59:59 +0200268 if (!object_class_by_name(driver)) {
269 const char *typename = find_typename_by_alias(driver);
270
271 if (typename) {
272 driver = typename;
273 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600274 }
275
Stefan Hajnoczief523582014-07-09 14:01:32 +0200276 prop_list = qmp_device_list_properties(driver, &local_err);
Gonglei0722eba2014-09-16 10:19:33 +0800277 if (local_err) {
Eduardo Habkost5185f0e2014-11-01 13:56:10 -0200278 goto error;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600279 }
Stefan Hajnoczief523582014-07-09 14:01:32 +0200280
281 for (prop = prop_list; prop; prop = prop->next) {
Gonglei07d09c52014-10-07 14:33:23 +0800282 error_printf("%s.%s=%s", driver,
Stefan Hajnoczief523582014-07-09 14:01:32 +0200283 prop->value->name,
284 prop->value->type);
Gonglei07d09c52014-10-07 14:33:23 +0800285 if (prop->value->has_description) {
286 error_printf(" (%s)\n", prop->value->description);
287 } else {
288 error_printf("\n");
289 }
Stefan Hajnoczief523582014-07-09 14:01:32 +0200290 }
291
292 qapi_free_DevicePropertyInfoList(prop_list);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600293 return 1;
Eduardo Habkost5185f0e2014-11-01 13:56:10 -0200294
295error:
Markus Armbruster78288672015-12-18 16:35:07 +0100296 error_report_err(local_err);
Eduardo Habkost5185f0e2014-11-01 13:56:10 -0200297 return 1;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600298}
299
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600300static Object *qdev_get_peripheral(void)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600301{
Anthony Liguori8b45d442011-12-23 09:08:05 -0600302 static Object *dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600303
304 if (dev == NULL) {
Andreas Färberdfe47e72012-04-05 13:21:46 +0200305 dev = container_get(qdev_get_machine(), "/peripheral");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600306 }
307
Anthony Liguori8b45d442011-12-23 09:08:05 -0600308 return dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600309}
310
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600311static Object *qdev_get_peripheral_anon(void)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600312{
Anthony Liguori8b45d442011-12-23 09:08:05 -0600313 static Object *dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600314
315 if (dev == NULL) {
Andreas Färberdfe47e72012-04-05 13:21:46 +0200316 dev = container_get(qdev_get_machine(), "/peripheral-anon");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600317 }
318
Anthony Liguori8b45d442011-12-23 09:08:05 -0600319 return dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600320}
321
Eric Blake50b7b002015-09-10 10:19:16 -0600322static void qbus_list_bus(DeviceState *dev, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600323{
324 BusState *child;
325 const char *sep = " ";
326
Eric Blake50b7b002015-09-10 10:19:16 -0600327 error_append_hint(errp, "child buses at \"%s\":",
328 dev->id ? dev->id : object_get_typename(OBJECT(dev)));
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600329 QLIST_FOREACH(child, &dev->child_bus, sibling) {
Eric Blake50b7b002015-09-10 10:19:16 -0600330 error_append_hint(errp, "%s\"%s\"", sep, child->name);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600331 sep = ", ";
332 }
Markus Armbruster543202c2015-12-17 17:35:14 +0100333 error_append_hint(errp, "\n");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600334}
335
Eric Blake50b7b002015-09-10 10:19:16 -0600336static void qbus_list_dev(BusState *bus, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600337{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600338 BusChild *kid;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600339 const char *sep = " ";
340
Eric Blake50b7b002015-09-10 10:19:16 -0600341 error_append_hint(errp, "devices at \"%s\":", bus->name);
Anthony Liguori0866aca2011-12-23 15:34:39 -0600342 QTAILQ_FOREACH(kid, &bus->children, sibling) {
343 DeviceState *dev = kid->child;
Eric Blake50b7b002015-09-10 10:19:16 -0600344 error_append_hint(errp, "%s\"%s\"", sep,
345 object_get_typename(OBJECT(dev)));
346 if (dev->id) {
347 error_append_hint(errp, "/\"%s\"", dev->id);
348 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600349 sep = ", ";
350 }
Markus Armbruster543202c2015-12-17 17:35:14 +0100351 error_append_hint(errp, "\n");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600352}
353
354static BusState *qbus_find_bus(DeviceState *dev, char *elem)
355{
356 BusState *child;
357
358 QLIST_FOREACH(child, &dev->child_bus, sibling) {
359 if (strcmp(child->name, elem) == 0) {
360 return child;
361 }
362 }
363 return NULL;
364}
365
366static DeviceState *qbus_find_dev(BusState *bus, char *elem)
367{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600368 BusChild *kid;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600369
370 /*
371 * try to match in order:
372 * (1) instance id, if present
373 * (2) driver name
374 * (3) driver alias, if present
375 */
Anthony Liguori0866aca2011-12-23 15:34:39 -0600376 QTAILQ_FOREACH(kid, &bus->children, sibling) {
377 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600378 if (dev->id && strcmp(dev->id, elem) == 0) {
379 return dev;
380 }
381 }
Anthony Liguori0866aca2011-12-23 15:34:39 -0600382 QTAILQ_FOREACH(kid, &bus->children, sibling) {
383 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600384 if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) {
385 return dev;
386 }
387 }
Anthony Liguori0866aca2011-12-23 15:34:39 -0600388 QTAILQ_FOREACH(kid, &bus->children, sibling) {
389 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600390 DeviceClass *dc = DEVICE_GET_CLASS(dev);
391
392 if (qdev_class_has_alias(dc) &&
393 strcmp(qdev_class_get_alias(dc), elem) == 0) {
394 return dev;
395 }
396 }
397 return NULL;
398}
399
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100400static inline bool qbus_is_full(BusState *bus)
401{
402 BusClass *bus_class = BUS_GET_CLASS(bus);
403 return bus_class->max_dev && bus->max_index >= bus_class->max_dev;
404}
405
406/*
407 * Search the tree rooted at @bus for a bus.
408 * If @name, search for a bus with that name. Note that bus names
409 * need not be unique. Yes, that's screwed up.
410 * Else search for a bus that is a subtype of @bus_typename.
411 * If more than one exists, prefer one that can take another device.
412 * Return the bus if found, else %NULL.
413 */
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600414static BusState *qbus_find_recursive(BusState *bus, const char *name,
Anthony Liguori0d936922012-05-02 09:00:20 +0200415 const char *bus_typename)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600416{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600417 BusChild *kid;
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100418 BusState *pick, *child, *ret;
419 bool match;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600420
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100421 assert(name || bus_typename);
422 if (name) {
423 match = !strcmp(bus->name, name);
424 } else {
425 match = !!object_dynamic_cast(OBJECT(bus), bus_typename);
KONRAD Frederic1395af62013-01-15 00:08:00 +0100426 }
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100427
428 if (match && !qbus_is_full(bus)) {
429 return bus; /* root matches and isn't full */
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600430 }
431
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100432 pick = match ? bus : NULL;
433
Anthony Liguori0866aca2011-12-23 15:34:39 -0600434 QTAILQ_FOREACH(kid, &bus->children, sibling) {
435 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600436 QLIST_FOREACH(child, &dev->child_bus, sibling) {
Anthony Liguori0d936922012-05-02 09:00:20 +0200437 ret = qbus_find_recursive(child, name, bus_typename);
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100438 if (ret && !qbus_is_full(ret)) {
439 return ret; /* a descendant matches and isn't full */
440 }
441 if (ret && !pick) {
442 pick = ret;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600443 }
444 }
445 }
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100446
447 /* root or a descendant matches, but is full */
448 return pick;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600449}
450
Markus Armbrusterd2828422015-03-11 19:16:04 +0100451static BusState *qbus_find(const char *path, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600452{
453 DeviceState *dev;
454 BusState *bus;
455 char elem[128];
456 int pos, len;
457
458 /* find start element */
459 if (path[0] == '/') {
460 bus = sysbus_get_default();
461 pos = 0;
462 } else {
463 if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
464 assert(!path[0]);
465 elem[0] = len = 0;
466 }
467 bus = qbus_find_recursive(sysbus_get_default(), elem, NULL);
468 if (!bus) {
Markus Armbrusterd2828422015-03-11 19:16:04 +0100469 error_setg(errp, "Bus '%s' not found", elem);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600470 return NULL;
471 }
472 pos = len;
473 }
474
475 for (;;) {
476 assert(path[pos] == '/' || !path[pos]);
477 while (path[pos] == '/') {
478 pos++;
479 }
480 if (path[pos] == '\0') {
Markus Armbrustered238ba2015-03-11 18:39:16 +0100481 break;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600482 }
483
484 /* find device */
485 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
Stefan Weildfc6f862013-07-25 18:21:28 +0200486 g_assert_not_reached();
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600487 elem[0] = len = 0;
488 }
489 pos += len;
490 dev = qbus_find_dev(bus, elem);
491 if (!dev) {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100492 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
493 "Device '%s' not found", elem);
Eric Blake50b7b002015-09-10 10:19:16 -0600494 qbus_list_dev(bus, errp);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600495 return NULL;
496 }
497
498 assert(path[pos] == '/' || !path[pos]);
499 while (path[pos] == '/') {
500 pos++;
501 }
502 if (path[pos] == '\0') {
503 /* last specified element is a device. If it has exactly
504 * one child bus accept it nevertheless */
Markus Armbrustered238ba2015-03-11 18:39:16 +0100505 if (dev->num_child_bus == 1) {
506 bus = QLIST_FIRST(&dev->child_bus);
507 break;
508 }
509 if (dev->num_child_bus) {
Markus Armbrusterd2828422015-03-11 19:16:04 +0100510 error_setg(errp, "Device '%s' has multiple child buses",
511 elem);
Eric Blake50b7b002015-09-10 10:19:16 -0600512 qbus_list_bus(dev, errp);
Markus Armbrustered238ba2015-03-11 18:39:16 +0100513 } else {
Markus Armbrusterd2828422015-03-11 19:16:04 +0100514 error_setg(errp, "Device '%s' has no child bus", elem);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600515 }
Markus Armbrustered238ba2015-03-11 18:39:16 +0100516 return NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600517 }
518
519 /* find bus */
520 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
Stefan Weildfc6f862013-07-25 18:21:28 +0200521 g_assert_not_reached();
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600522 elem[0] = len = 0;
523 }
524 pos += len;
525 bus = qbus_find_bus(dev, elem);
526 if (!bus) {
Markus Armbrusterd2828422015-03-11 19:16:04 +0100527 error_setg(errp, "Bus '%s' not found", elem);
Eric Blake50b7b002015-09-10 10:19:16 -0600528 qbus_list_bus(dev, errp);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600529 return NULL;
530 }
531 }
Markus Armbrustered238ba2015-03-11 18:39:16 +0100532
533 if (qbus_is_full(bus)) {
Markus Armbrusterd2828422015-03-11 19:16:04 +0100534 error_setg(errp, "Bus '%s' is full", path);
Markus Armbrustered238ba2015-03-11 18:39:16 +0100535 return NULL;
536 }
537 return bus;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600538}
539
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100540DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600541{
Andreas Färberf4d85792013-08-24 01:21:22 +0200542 DeviceClass *dc;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600543 const char *driver, *path, *id;
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200544 DeviceState *dev;
Andreas Färber2f7bd822013-04-16 03:50:21 +0200545 BusState *bus = NULL;
Andreas Färber852e2c52013-10-07 16:42:34 +0200546 Error *err = NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600547
548 driver = qemu_opt_get(opts, "driver");
549 if (!driver) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100550 error_setg(errp, QERR_MISSING_PARAMETER, "driver");
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600551 return NULL;
552 }
553
554 /* find driver */
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100555 dc = qdev_get_device_class(&driver, errp);
556 if (!dc) {
Markus Armbruster7ea5e782013-11-28 17:27:03 +0100557 return NULL;
558 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600559
560 /* find bus */
561 path = qemu_opt_get(opts, "bus");
562 if (path != NULL) {
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100563 bus = qbus_find(path, errp);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600564 if (!bus) {
565 return NULL;
566 }
Andreas Färberf4d85792013-08-24 01:21:22 +0200567 if (!object_dynamic_cast(OBJECT(bus), dc->bus_type)) {
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100568 error_setg(errp, "Device '%s' can't go on %s bus",
569 driver, object_get_typename(OBJECT(bus)));
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600570 return NULL;
571 }
Andreas Färberf4d85792013-08-24 01:21:22 +0200572 } else if (dc->bus_type != NULL) {
573 bus = qbus_find_recursive(sysbus_get_default(), NULL, dc->bus_type);
Markus Armbrustera5ec4942015-03-11 17:26:31 +0100574 if (!bus || qbus_is_full(bus)) {
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100575 error_setg(errp, "No '%s' bus found for device '%s'",
576 dc->bus_type, driver);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600577 return NULL;
578 }
579 }
Igor Mammedov39b888b2014-09-26 09:28:17 +0000580 if (qdev_hotplug && bus && !qbus_is_hotpluggable(bus)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100581 error_setg(errp, QERR_BUS_NO_HOTPLUG, bus->name);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600582 return NULL;
583 }
584
Amos Kong7b030942014-03-03 15:57:55 +0800585 /* create device */
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200586 dev = DEVICE(object_new(driver));
Andreas Färber2f7bd822013-04-16 03:50:21 +0200587
588 if (bus) {
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200589 qdev_set_parent_bus(dev, bus);
Andreas Färber2f7bd822013-04-16 03:50:21 +0200590 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600591
592 id = qemu_opts_id(opts);
593 if (id) {
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200594 dev->id = id;
Anthony Liguorib2d4b3f2012-02-12 11:36:24 -0600595 }
Amos Kong7b030942014-03-03 15:57:55 +0800596
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200597 if (dev->id) {
598 object_property_add_child(qdev_get_peripheral(), dev->id,
599 OBJECT(dev), NULL);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600600 } else {
601 static int anon_count;
602 gchar *name = g_strdup_printf("device[%d]", anon_count++);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600603 object_property_add_child(qdev_get_peripheral_anon(), name,
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200604 OBJECT(dev), NULL);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600605 g_free(name);
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200606 }
Bandan Das52aa17c2014-02-26 10:32:40 -0700607
Amos Kong7b030942014-03-03 15:57:55 +0800608 /* set properties */
Markus Armbruster4caa4892015-03-12 13:58:02 +0100609 if (qemu_opt_foreach(opts, set_property, dev, &err)) {
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100610 error_propagate(errp, err);
Amos Kong7b030942014-03-03 15:57:55 +0800611 object_unparent(OBJECT(dev));
612 object_unref(OBJECT(dev));
613 return NULL;
614 }
615
Bandan Das52aa17c2014-02-26 10:32:40 -0700616 dev->opts = opts;
Andreas Färber852e2c52013-10-07 16:42:34 +0200617 object_property_set_bool(OBJECT(dev), true, "realized", &err);
618 if (err != NULL) {
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100619 error_propagate(errp, err);
Bandan Das52aa17c2014-02-26 10:32:40 -0700620 dev->opts = NULL;
Andreas Färber852e2c52013-10-07 16:42:34 +0200621 object_unparent(OBJECT(dev));
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200622 object_unref(OBJECT(dev));
Paolo Bonzinif424d5c2012-03-27 18:38:46 +0200623 return NULL;
624 }
Andreas Färber2bcb0c62013-10-07 16:17:54 +0200625 return dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600626}
627
628
629#define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
630static void qbus_print(Monitor *mon, BusState *bus, int indent);
631
632static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
Paolo Bonzinibce54472012-03-28 18:12:47 +0200633 int indent)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600634{
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600635 if (!props)
636 return;
Paolo Bonzinid8229792012-02-02 09:47:13 +0100637 for (; props->name; props++) {
638 Error *err = NULL;
639 char *value;
640 char *legacy_name = g_strdup_printf("legacy-%s", props->name);
641 if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) {
642 value = object_property_get_str(OBJECT(dev), legacy_name, &err);
643 } else {
Paolo Bonzinidae3bda2014-02-08 11:01:51 +0100644 value = object_property_print(OBJECT(dev), props->name, true, &err);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600645 }
Paolo Bonzinid8229792012-02-02 09:47:13 +0100646 g_free(legacy_name);
647
648 if (err) {
649 error_free(err);
650 continue;
651 }
Paolo Bonzinibce54472012-03-28 18:12:47 +0200652 qdev_printf("%s = %s\n", props->name,
Paolo Bonzinid8229792012-02-02 09:47:13 +0100653 value && *value ? value : "<null>");
654 g_free(value);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600655 }
656}
657
Anthony Liguori0d936922012-05-02 09:00:20 +0200658static void bus_print_dev(BusState *bus, Monitor *mon, DeviceState *dev, int indent)
659{
660 BusClass *bc = BUS_GET_CLASS(bus);
661
662 if (bc->print_dev) {
663 bc->print_dev(mon, dev, indent);
664 }
665}
666
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600667static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
668{
Paolo Bonzinibce54472012-03-28 18:12:47 +0200669 ObjectClass *class;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600670 BusState *child;
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700671 NamedGPIOList *ngl;
672
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600673 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)),
674 dev->id ? dev->id : "");
675 indent += 2;
Peter Crosthwaitea5f54292014-05-19 23:30:58 -0700676 QLIST_FOREACH(ngl, &dev->gpios, node) {
677 if (ngl->num_in) {
678 qdev_printf("gpio-in \"%s\" %d\n", ngl->name ? ngl->name : "",
679 ngl->num_in);
680 }
681 if (ngl->num_out) {
682 qdev_printf("gpio-out \"%s\" %d\n", ngl->name ? ngl->name : "",
683 ngl->num_out);
684 }
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600685 }
Paolo Bonzinibce54472012-03-28 18:12:47 +0200686 class = object_get_class(OBJECT(dev));
687 do {
688 qdev_print_props(mon, dev, DEVICE_CLASS(class)->props, indent);
689 class = object_class_get_parent(class);
690 } while (class != object_class_by_name(TYPE_DEVICE));
Gerd Hoffmannda9fbe72012-07-11 12:21:23 +0200691 bus_print_dev(dev->parent_bus, mon, dev, indent);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600692 QLIST_FOREACH(child, &dev->child_bus, sibling) {
693 qbus_print(mon, child, indent);
694 }
695}
696
697static void qbus_print(Monitor *mon, BusState *bus, int indent)
698{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600699 BusChild *kid;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600700
701 qdev_printf("bus: %s\n", bus->name);
702 indent += 2;
Anthony Liguori0d936922012-05-02 09:00:20 +0200703 qdev_printf("type %s\n", object_get_typename(OBJECT(bus)));
Anthony Liguori0866aca2011-12-23 15:34:39 -0600704 QTAILQ_FOREACH(kid, &bus->children, sibling) {
705 DeviceState *dev = kid->child;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600706 qdev_print(mon, dev, indent);
707 }
708}
709#undef qdev_printf
710
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100711void hmp_info_qtree(Monitor *mon, const QDict *qdict)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600712{
713 if (sysbus_get_default())
714 qbus_print(mon, sysbus_get_default(), 0);
715}
716
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100717void hmp_info_qdm(Monitor *mon, const QDict *qdict)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600718{
Markus Armbrustera3400ae2013-10-10 15:00:21 +0200719 qdev_print_devinfos(true);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600720}
721
Andreas Färbera01ff752014-05-07 17:03:18 +0200722typedef struct QOMCompositionState {
723 Monitor *mon;
724 int indent;
725} QOMCompositionState;
726
727static void print_qom_composition(Monitor *mon, Object *obj, int indent);
728
729static int print_qom_composition_child(Object *obj, void *opaque)
730{
731 QOMCompositionState *s = opaque;
732
733 print_qom_composition(s->mon, obj, s->indent);
734
735 return 0;
736}
737
738static void print_qom_composition(Monitor *mon, Object *obj, int indent)
739{
740 QOMCompositionState s = {
741 .mon = mon,
742 .indent = indent + 2,
743 };
744 char *name;
745
746 if (obj == object_get_root()) {
747 name = g_strdup("");
748 } else {
749 name = object_get_canonical_path_component(obj);
750 }
751 monitor_printf(mon, "%*s/%s (%s)\n", indent, "", name,
752 object_get_typename(obj));
753 g_free(name);
754 object_child_foreach(obj, print_qom_composition_child, &s);
755}
756
757void hmp_info_qom_tree(Monitor *mon, const QDict *dict)
758{
759 const char *path = qdict_get_try_str(dict, "path");
760 Object *obj;
761 bool ambiguous = false;
762
763 if (path) {
764 obj = object_resolve_path(path, &ambiguous);
765 if (!obj) {
766 monitor_printf(mon, "Path '%s' could not be resolved.\n", path);
767 return;
768 }
769 if (ambiguous) {
770 monitor_printf(mon, "Warning: Path '%s' is ambiguous.\n", path);
771 return;
772 }
773 } else {
774 obj = qdev_get_machine();
775 }
776 print_qom_composition(mon, obj, 0);
777}
778
Markus Armbruster485febc2015-03-13 17:25:50 +0100779void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600780{
Luiz Capitulino4e899782012-04-18 17:24:01 -0300781 Error *local_err = NULL;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600782 QemuOpts *opts;
Paolo Bonzinib09995a2013-01-25 14:12:37 +0100783 DeviceState *dev;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600784
Luiz Capitulino4e899782012-04-18 17:24:01 -0300785 opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100786 if (local_err) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100787 error_propagate(errp, local_err);
788 return;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600789 }
790 if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
791 qemu_opts_del(opts);
Markus Armbruster485febc2015-03-13 17:25:50 +0100792 return;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600793 }
Markus Armbrusterf006cf72015-03-12 14:00:41 +0100794 dev = qdev_device_add(opts, &local_err);
Paolo Bonzinib09995a2013-01-25 14:12:37 +0100795 if (!dev) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100796 error_propagate(errp, local_err);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600797 qemu_opts_del(opts);
Markus Armbruster485febc2015-03-13 17:25:50 +0100798 return;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600799 }
Paolo Bonzinib09995a2013-01-25 14:12:37 +0100800 object_unref(OBJECT(dev));
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600801}
802
Luiz Capitulinoa15fef22012-03-29 12:38:50 -0300803void qmp_device_del(const char *id, Error **errp)
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600804{
Igor Mammedovb6cc36a2014-10-02 10:08:45 +0000805 Object *obj;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600806
Daniel P. Berrange6287d822015-09-11 13:33:56 +0100807 if (id[0] == '/') {
808 obj = object_resolve_path(id, NULL);
809 } else {
810 char *root_path = object_get_canonical_path(qdev_get_peripheral());
811 char *path = g_strdup_printf("%s/%s", root_path, id);
812
813 g_free(root_path);
814 obj = object_resolve_path_type(path, TYPE_DEVICE, NULL);
815 g_free(path);
816 }
Igor Mammedovb6cc36a2014-10-02 10:08:45 +0000817
818 if (!obj) {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100819 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
820 "Device '%s' not found", id);
Luiz Capitulinoa15fef22012-03-29 12:38:50 -0300821 return;
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600822 }
Luiz Capitulino56f91072012-03-14 17:37:38 -0300823
Daniel P. Berrange6287d822015-09-11 13:33:56 +0100824 if (!object_dynamic_cast(obj, TYPE_DEVICE)) {
825 error_setg(errp, "%s is not a hotpluggable device", id);
826 return;
827 }
828
Igor Mammedovb6cc36a2014-10-02 10:08:45 +0000829 qdev_unplug(DEVICE(obj), errp);
Anthony Liguoriee46d8a2011-12-22 15:24:20 -0600830}
831
832void qdev_machine_init(void)
833{
834 qdev_get_peripheral_anon();
835 qdev_get_peripheral();
836}
Paolo Bonzini4d454572012-11-26 16:03:42 +0100837
838QemuOptsList qemu_device_opts = {
839 .name = "device",
840 .implied_opt_name = "driver",
841 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
842 .desc = {
843 /*
844 * no elements => accept any
845 * sanity checking will happen later
846 * when setting device properties
847 */
848 { /* end of list */ }
849 },
850};
851
852QemuOptsList qemu_global_opts = {
853 .name = "global",
854 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
855 .desc = {
856 {
857 .name = "driver",
858 .type = QEMU_OPT_STRING,
859 },{
860 .name = "property",
861 .type = QEMU_OPT_STRING,
862 },{
863 .name = "value",
864 .type = QEMU_OPT_STRING,
865 },
866 { /* end of list */ }
867 },
868};
869
870int qemu_global_option(const char *str)
871{
872 char driver[64], property[64];
873 QemuOpts *opts;
874 int rc, offset;
875
Paolo Bonzini3751d7c2015-04-09 14:16:19 +0200876 rc = sscanf(str, "%63[^.=].%63[^=]%n", driver, property, &offset);
877 if (rc == 2 && str[offset] == '=') {
878 opts = qemu_opts_create(&qemu_global_opts, NULL, 0, &error_abort);
879 qemu_opt_set(opts, "driver", driver, &error_abort);
880 qemu_opt_set(opts, "property", property, &error_abort);
881 qemu_opt_set(opts, "value", str + offset + 1, &error_abort);
882 return 0;
883 }
884
Markus Armbruster70b94332015-02-13 12:50:26 +0100885 opts = qemu_opts_parse_noisily(&qemu_global_opts, str, false);
Paolo Bonzini3751d7c2015-04-09 14:16:19 +0200886 if (!opts) {
Paolo Bonzini4d454572012-11-26 16:03:42 +0100887 return -1;
888 }
889
Paolo Bonzini4d454572012-11-26 16:03:42 +0100890 return 0;
891}