xref: /openbmc/qemu/hw/core/qdev-fw.c (revision b14df228)
1 /*
2  *  qdev fw helpers
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 
18 #include "qemu/osdep.h"
19 #include "hw/fw-path-provider.h"
20 #include "hw/qdev-core.h"
21 
22 const char *qdev_fw_name(DeviceState *dev)
23 {
24     DeviceClass *dc = DEVICE_GET_CLASS(dev);
25 
26     if (dc->fw_name) {
27         return dc->fw_name;
28     }
29 
30     return object_get_typename(OBJECT(dev));
31 }
32 
33 static char *bus_get_fw_dev_path(BusState *bus, DeviceState *dev)
34 {
35     BusClass *bc = BUS_GET_CLASS(bus);
36 
37     if (bc->get_fw_dev_path) {
38         return bc->get_fw_dev_path(dev);
39     }
40 
41     return NULL;
42 }
43 
44 static char *qdev_get_fw_dev_path_from_handler(BusState *bus, DeviceState *dev)
45 {
46     Object *obj = OBJECT(dev);
47     char *d = NULL;
48 
49     while (!d && obj->parent) {
50         obj = obj->parent;
51         d = fw_path_provider_try_get_dev_path(obj, bus, dev);
52     }
53     return d;
54 }
55 
56 char *qdev_get_own_fw_dev_path_from_handler(BusState *bus, DeviceState *dev)
57 {
58     Object *obj = OBJECT(dev);
59 
60     return fw_path_provider_try_get_dev_path(obj, bus, dev);
61 }
62 
63 static int qdev_get_fw_dev_path_helper(DeviceState *dev, char *p, int size)
64 {
65     int l = 0;
66 
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);
73         }
74         if (d) {
75             l += snprintf(p + l, size - l, "%s", d);
76             g_free(d);
77         } else {
78             return l;
79         }
80     }
81     l += snprintf(p + l , size - l, "/");
82 
83     return l;
84 }
85 
86 char *qdev_get_fw_dev_path(DeviceState *dev)
87 {
88     char path[128];
89     int l;
90 
91     l = qdev_get_fw_dev_path_helper(dev, path, 128);
92 
93     path[l - 1] = '\0';
94 
95     return g_strdup(path);
96 }
97