1 | /*
|
---|
2 | * Copyright (c) 2006 Jakub Jermar
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup kernel_genarch
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | * @brief OpenFirmware device tree navigation.
|
---|
35 | *
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <genarch/ofw/ofw_tree.h>
|
---|
39 | #include <stdlib.h>
|
---|
40 | #include <sysinfo/sysinfo.h>
|
---|
41 | #include <memw.h>
|
---|
42 | #include <str.h>
|
---|
43 | #include <panic.h>
|
---|
44 | #include <stdio.h>
|
---|
45 |
|
---|
46 | #define PATH_MAX_LEN 256
|
---|
47 | #define NAME_BUF_LEN 50
|
---|
48 |
|
---|
49 | static ofw_tree_node_t *ofw_root;
|
---|
50 |
|
---|
51 | void ofw_tree_init(ofw_tree_node_t *root)
|
---|
52 | {
|
---|
53 | ofw_root = root;
|
---|
54 | }
|
---|
55 |
|
---|
56 | /** Get OpenFirmware node property.
|
---|
57 | *
|
---|
58 | * @param node Node in which to lookup the property.
|
---|
59 | * @param name Name of the property.
|
---|
60 | *
|
---|
61 | * @return Pointer to the property structure or NULL if no such
|
---|
62 | * property.
|
---|
63 | *
|
---|
64 | */
|
---|
65 | ofw_tree_property_t *ofw_tree_getprop(const ofw_tree_node_t *node,
|
---|
66 | const char *name)
|
---|
67 | {
|
---|
68 | for (size_t i = 0; i < node->properties; i++) {
|
---|
69 | if (str_cmp(node->property[i].name, name) == 0)
|
---|
70 | return &node->property[i];
|
---|
71 | }
|
---|
72 |
|
---|
73 | return NULL;
|
---|
74 | }
|
---|
75 |
|
---|
76 | /** Return value of the 'name' property.
|
---|
77 | *
|
---|
78 | * @param node Node of interest.
|
---|
79 | *
|
---|
80 | * @return Value of the 'name' property belonging to the node
|
---|
81 | * or NULL if the property is invalid.
|
---|
82 | *
|
---|
83 | */
|
---|
84 | const char *ofw_tree_node_name(const ofw_tree_node_t *node)
|
---|
85 | {
|
---|
86 | ofw_tree_property_t *prop = ofw_tree_getprop(node, "name");
|
---|
87 | if ((!prop) || (prop->size < 2))
|
---|
88 | return NULL;
|
---|
89 |
|
---|
90 | return prop->value;
|
---|
91 | }
|
---|
92 |
|
---|
93 | /** Lookup child of given name.
|
---|
94 | *
|
---|
95 | * @param node Node whose child is being looked up.
|
---|
96 | * @param name Name of the child being looked up.
|
---|
97 | *
|
---|
98 | * @return NULL if there is no such child or pointer to the
|
---|
99 | * matching child node.
|
---|
100 | *
|
---|
101 | */
|
---|
102 | ofw_tree_node_t *ofw_tree_find_child(ofw_tree_node_t *node,
|
---|
103 | const char *name)
|
---|
104 | {
|
---|
105 | /*
|
---|
106 | * Try to find the disambigued name.
|
---|
107 | */
|
---|
108 | for (ofw_tree_node_t *cur = node->child; cur; cur = cur->peer) {
|
---|
109 | if (str_cmp(cur->da_name, name) == 0)
|
---|
110 | return cur;
|
---|
111 | }
|
---|
112 |
|
---|
113 | /*
|
---|
114 | * Disambigued name not found.
|
---|
115 | * Lets try our luck with possibly ambiguous "name" property.
|
---|
116 | *
|
---|
117 | * We need to do this because paths stored in "/aliases"
|
---|
118 | * are not always fully-qualified.
|
---|
119 | */
|
---|
120 | for (ofw_tree_node_t *cur = node->child; cur; cur = cur->peer) {
|
---|
121 | if (str_cmp(ofw_tree_node_name(cur), name) == 0)
|
---|
122 | return cur;
|
---|
123 | }
|
---|
124 |
|
---|
125 | return NULL;
|
---|
126 | }
|
---|
127 |
|
---|
128 | /** Lookup first child of given device type.
|
---|
129 | *
|
---|
130 | * @param node Node whose child is being looked up.
|
---|
131 | * @param dtype Device type of the child being looked up.
|
---|
132 | *
|
---|
133 | * @return NULL if there is no such child or pointer to the
|
---|
134 | * matching child node.
|
---|
135 | *
|
---|
136 | */
|
---|
137 | ofw_tree_node_t *ofw_tree_find_child_by_device_type(ofw_tree_node_t *node,
|
---|
138 | const char *dtype)
|
---|
139 | {
|
---|
140 | for (ofw_tree_node_t *cur = node->child; cur; cur = cur->peer) {
|
---|
141 | ofw_tree_property_t *prop =
|
---|
142 | ofw_tree_getprop(cur, "device_type");
|
---|
143 |
|
---|
144 | if ((!prop) || (!prop->value))
|
---|
145 | continue;
|
---|
146 |
|
---|
147 | if (str_cmp(prop->value, dtype) == 0)
|
---|
148 | return cur;
|
---|
149 | }
|
---|
150 |
|
---|
151 | return NULL;
|
---|
152 | }
|
---|
153 |
|
---|
154 | /** Lookup node with matching node_handle.
|
---|
155 | *
|
---|
156 | * Child nodes are looked up recursively contrary to peer nodes that
|
---|
157 | * are looked up iteratively to avoid stack overflow.
|
---|
158 | *
|
---|
159 | * @param root Root of the searched subtree.
|
---|
160 | * @param handle OpenFirmware handle.
|
---|
161 | *
|
---|
162 | * @return NULL if there is no such node or pointer to the matching
|
---|
163 | * node.
|
---|
164 | *
|
---|
165 | */
|
---|
166 | ofw_tree_node_t *ofw_tree_find_node_by_handle(ofw_tree_node_t *root,
|
---|
167 | phandle handle)
|
---|
168 | {
|
---|
169 | for (ofw_tree_node_t *cur = root; cur; cur = cur->peer) {
|
---|
170 | if (cur->node_handle == handle)
|
---|
171 | return cur;
|
---|
172 |
|
---|
173 | if (cur->child) {
|
---|
174 | ofw_tree_node_t *node =
|
---|
175 | ofw_tree_find_node_by_handle(cur->child, handle);
|
---|
176 | if (node)
|
---|
177 | return node;
|
---|
178 | }
|
---|
179 | }
|
---|
180 |
|
---|
181 | return NULL;
|
---|
182 | }
|
---|
183 |
|
---|
184 | /** Lookup first peer of given device type.
|
---|
185 | *
|
---|
186 | * @param node Node whose peer is being looked up.
|
---|
187 | * @param dtype Device type of the child being looked up.
|
---|
188 | *
|
---|
189 | * @return NULL if there is no such child or pointer to the
|
---|
190 | * matching child node.
|
---|
191 | *
|
---|
192 | */
|
---|
193 | ofw_tree_node_t *ofw_tree_find_peer_by_device_type(ofw_tree_node_t *node,
|
---|
194 | const char *dtype)
|
---|
195 | {
|
---|
196 | for (ofw_tree_node_t *cur = node->peer; cur; cur = cur->peer) {
|
---|
197 | ofw_tree_property_t *prop =
|
---|
198 | ofw_tree_getprop(cur, "device_type");
|
---|
199 |
|
---|
200 | if ((!prop) || (!prop->value))
|
---|
201 | continue;
|
---|
202 |
|
---|
203 | if (str_cmp(prop->value, dtype) == 0)
|
---|
204 | return cur;
|
---|
205 | }
|
---|
206 |
|
---|
207 | return NULL;
|
---|
208 | }
|
---|
209 |
|
---|
210 | /** Lookup first peer of given name.
|
---|
211 | *
|
---|
212 | * @param node Node whose peer is being looked up.
|
---|
213 | * @param name Name of the child being looked up.
|
---|
214 | *
|
---|
215 | * @return NULL if there is no such peer or pointer to the matching
|
---|
216 | * peer node.
|
---|
217 | *
|
---|
218 | */
|
---|
219 | ofw_tree_node_t *ofw_tree_find_peer_by_name(ofw_tree_node_t *node,
|
---|
220 | const char *name)
|
---|
221 | {
|
---|
222 | for (ofw_tree_node_t *cur = node->peer; cur; cur = cur->peer) {
|
---|
223 | ofw_tree_property_t *prop =
|
---|
224 | ofw_tree_getprop(cur, "name");
|
---|
225 |
|
---|
226 | if ((!prop) || (!prop->value))
|
---|
227 | continue;
|
---|
228 |
|
---|
229 | if (str_cmp(prop->value, name) == 0)
|
---|
230 | return cur;
|
---|
231 | }
|
---|
232 |
|
---|
233 | return NULL;
|
---|
234 | }
|
---|
235 |
|
---|
236 | /** Lookup OpenFirmware node by its path.
|
---|
237 | *
|
---|
238 | * @param path Path to the node.
|
---|
239 | *
|
---|
240 | * @return NULL if there is no such node or pointer to the leaf
|
---|
241 | * node.
|
---|
242 | *
|
---|
243 | */
|
---|
244 | ofw_tree_node_t *ofw_tree_lookup(const char *path)
|
---|
245 | {
|
---|
246 | if (path[0] != '/')
|
---|
247 | return NULL;
|
---|
248 |
|
---|
249 | ofw_tree_node_t *node = ofw_root;
|
---|
250 | size_t j;
|
---|
251 |
|
---|
252 | for (size_t i = 1; (i < str_size(path)) && (node); i = j + 1) {
|
---|
253 | j = i;
|
---|
254 | while (j < str_size(path) && path[j] != '/')
|
---|
255 | j++;
|
---|
256 |
|
---|
257 | /* Skip extra slashes */
|
---|
258 | if (i == j)
|
---|
259 | continue;
|
---|
260 |
|
---|
261 | char buf[NAME_BUF_LEN + 1];
|
---|
262 | memcpy(buf, &path[i], j - i);
|
---|
263 | buf[j - i] = 0;
|
---|
264 | node = ofw_tree_find_child(node, buf);
|
---|
265 | }
|
---|
266 |
|
---|
267 | return node;
|
---|
268 | }
|
---|
269 |
|
---|
270 | /** Walk the OpenFirmware device subtree rooted in a node.
|
---|
271 | *
|
---|
272 | * Child nodes are processed recursively and peer nodes are processed
|
---|
273 | * iteratively in order to avoid stack overflow.
|
---|
274 | *
|
---|
275 | * @param node Root of the subtree.
|
---|
276 | * @param dtype Device type to look for.
|
---|
277 | * @param walker Routine to be invoked on found device.
|
---|
278 | * @param arg User argument to the walker.
|
---|
279 | *
|
---|
280 | * @return True if the walk should continue.
|
---|
281 | *
|
---|
282 | */
|
---|
283 | static bool ofw_tree_walk_by_device_type_internal(ofw_tree_node_t *node,
|
---|
284 | const char *dtype, ofw_tree_walker_t walker, void *arg)
|
---|
285 | {
|
---|
286 | for (ofw_tree_node_t *cur = node; cur; cur = cur->peer) {
|
---|
287 | ofw_tree_property_t *prop =
|
---|
288 | ofw_tree_getprop(cur, "device_type");
|
---|
289 |
|
---|
290 | if ((prop) && (prop->value) && (str_cmp(prop->value, dtype) == 0)) {
|
---|
291 | bool ret = walker(cur, arg);
|
---|
292 | if (!ret)
|
---|
293 | return false;
|
---|
294 | }
|
---|
295 |
|
---|
296 | if (cur->child) {
|
---|
297 | bool ret =
|
---|
298 | ofw_tree_walk_by_device_type_internal(cur->child, dtype, walker, arg);
|
---|
299 | if (!ret)
|
---|
300 | return false;
|
---|
301 | }
|
---|
302 | }
|
---|
303 |
|
---|
304 | return true;
|
---|
305 | }
|
---|
306 |
|
---|
307 | /** Walk the OpenFirmware device tree and find devices by type.
|
---|
308 | *
|
---|
309 | * Walk the whole OpenFirmware device tree and if any node has
|
---|
310 | * the property "device_type" equal to dtype, run a walker on it.
|
---|
311 | * If the walker returns false, the walk does not continue.
|
---|
312 | *
|
---|
313 | * @param dtype Device type to look for.
|
---|
314 | * @param walker Routine to be invoked on found device.
|
---|
315 | * @param arg User argument to the walker.
|
---|
316 | *
|
---|
317 | */
|
---|
318 | void ofw_tree_walk_by_device_type(const char *dtype, ofw_tree_walker_t walker,
|
---|
319 | void *arg)
|
---|
320 | {
|
---|
321 | (void) ofw_tree_walk_by_device_type_internal(ofw_root, dtype, walker, arg);
|
---|
322 | }
|
---|
323 |
|
---|
324 | /** Get OpenFirmware node properties.
|
---|
325 | *
|
---|
326 | * @param item Sysinfo item (unused).
|
---|
327 | * @param size Size of the returned data.
|
---|
328 | * @param dry_run Do not get the data, just calculate the size.
|
---|
329 | * @param data OpenFirmware node.
|
---|
330 | *
|
---|
331 | * @return Data containing a serialized dump of all node
|
---|
332 | * properties. If the return value is not NULL, it
|
---|
333 | * should be freed in the context of the sysinfo request.
|
---|
334 | *
|
---|
335 | */
|
---|
336 | static void *ofw_sysinfo_properties(struct sysinfo_item *item, size_t *size,
|
---|
337 | bool dry_run, void *data)
|
---|
338 | {
|
---|
339 | ofw_tree_node_t *node = (ofw_tree_node_t *) data;
|
---|
340 |
|
---|
341 | /* Compute serialized data size */
|
---|
342 | *size = 0;
|
---|
343 | for (size_t i = 0; i < node->properties; i++)
|
---|
344 | *size += str_size(node->property[i].name) + 1 +
|
---|
345 | sizeof(node->property[i].size) + node->property[i].size;
|
---|
346 |
|
---|
347 | if (dry_run)
|
---|
348 | return NULL;
|
---|
349 |
|
---|
350 | void *dump = malloc(*size);
|
---|
351 | if (dump == NULL) {
|
---|
352 | *size = 0;
|
---|
353 | return NULL;
|
---|
354 | }
|
---|
355 |
|
---|
356 | /* Serialize the data */
|
---|
357 | size_t pos = 0;
|
---|
358 | for (size_t i = 0; i < node->properties; i++) {
|
---|
359 | /* Property name */
|
---|
360 | str_cpy(dump + pos, *size - pos, node->property[i].name);
|
---|
361 | pos += str_size(node->property[i].name) + 1;
|
---|
362 |
|
---|
363 | /* Value size */
|
---|
364 | memcpy(dump + pos, &node->property[i].size,
|
---|
365 | sizeof(node->property[i].size));
|
---|
366 | pos += sizeof(node->property[i].size);
|
---|
367 |
|
---|
368 | /* Value */
|
---|
369 | memcpy(dump + pos, node->property[i].value,
|
---|
370 | node->property[i].size);
|
---|
371 | pos += node->property[i].size;
|
---|
372 | }
|
---|
373 |
|
---|
374 | return ((void *) dump);
|
---|
375 | }
|
---|
376 |
|
---|
377 | /** Map OpenFirmware device subtree rooted in a node into sysinfo.
|
---|
378 | *
|
---|
379 | * Child nodes are processed recursively and peer nodes are processed
|
---|
380 | * iteratively in order to avoid stack overflow.
|
---|
381 | *
|
---|
382 | * @param node Root of the subtree.
|
---|
383 | * @param path Current path, NULL for the very root of the entire tree.
|
---|
384 | *
|
---|
385 | */
|
---|
386 | static void ofw_tree_node_sysinfo(ofw_tree_node_t *node, const char *path)
|
---|
387 | {
|
---|
388 | char *cur_path = malloc(PATH_MAX_LEN);
|
---|
389 | if (!cur_path)
|
---|
390 | panic("Not enough memory to process OFW tree.");
|
---|
391 |
|
---|
392 | for (ofw_tree_node_t *cur = node; cur; cur = cur->peer) {
|
---|
393 | if ((cur->parent) && (path))
|
---|
394 | snprintf(cur_path, PATH_MAX_LEN, "%s.%s", path, cur->da_name);
|
---|
395 | else if (!str_size(cur->da_name))
|
---|
396 | snprintf(cur_path, PATH_MAX_LEN, "firmware.ofw");
|
---|
397 | else
|
---|
398 | snprintf(cur_path, PATH_MAX_LEN, "firmware.%s", cur->da_name);
|
---|
399 |
|
---|
400 | sysinfo_set_item_gen_data(cur_path, NULL, ofw_sysinfo_properties,
|
---|
401 | (void *) cur);
|
---|
402 |
|
---|
403 | if (cur->child)
|
---|
404 | ofw_tree_node_sysinfo(cur->child, cur_path);
|
---|
405 | }
|
---|
406 |
|
---|
407 | free(cur_path);
|
---|
408 | }
|
---|
409 |
|
---|
410 | /** Map the OpenFirmware device tree into sysinfo. */
|
---|
411 | void ofw_sysinfo_map(void)
|
---|
412 | {
|
---|
413 | ofw_tree_node_sysinfo(ofw_root, NULL);
|
---|
414 | }
|
---|
415 |
|
---|
416 | /** @}
|
---|
417 | */
|
---|