Merge remote-tracking branch 'remotes/dgilbert-gitlab/tags/pull-migration-20210726a...
[qemu/armbru.git] / hw / core / qdev-fw.c
bloba31958355ff64b894a6e27bafd713ef47b2bc650
1 /*
2 * qdev fw helpers
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License,
7 * or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 #include "qemu/osdep.h"
19 #include "hw/fw-path-provider.h"
20 #include "hw/qdev-core.h"
22 const char *qdev_fw_name(DeviceState *dev)
24 DeviceClass *dc = DEVICE_GET_CLASS(dev);
26 if (dc->fw_name) {
27 return dc->fw_name;
30 return object_get_typename(OBJECT(dev));
33 static char *bus_get_fw_dev_path(BusState *bus, DeviceState *dev)
35 BusClass *bc = BUS_GET_CLASS(bus);
37 if (bc->get_fw_dev_path) {
38 return bc->get_fw_dev_path(dev);
41 return NULL;
44 static char *qdev_get_fw_dev_path_from_handler(BusState *bus, DeviceState *dev)
46 Object *obj = OBJECT(dev);
47 char *d = NULL;
49 while (!d && obj->parent) {
50 obj = obj->parent;
51 d = fw_path_provider_try_get_dev_path(obj, bus, dev);
53 return d;
56 char *qdev_get_own_fw_dev_path_from_handler(BusState *bus, DeviceState *dev)
58 Object *obj = OBJECT(dev);
60 return fw_path_provider_try_get_dev_path(obj, bus, dev);
63 static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
65 int l = 0;
67 if (dev && dev->parent_bus) {
68 char *d;
69 l = qdev_get_fw_dev_path_helper(dev->parent_bus->parent, p, size);
70 d = qdev_get_fw_dev_path_from_handler(dev->parent_bus, dev);
71 if (!d) {
72 d = bus_get_fw_dev_path(dev->parent_bus, dev);
74 if (d) {
75 l += snprintf(p + l, size - l, "%s", d);
76 g_free(d);
77 } else {
78 return l;
81 l += snprintf(p + l , size - l, "/");
83 return l;
86 char *qdev_get_fw_dev_path(DeviceState *dev)
88 char path[128];
89 int l;
91 l = qdev_get_fw_dev_path_helper(dev, path, 128);
93 path[l - 1] = '\0';
95 return g_strdup(path);