source: mainline/kernel/genarch/src/ofw/ofw_tree.c@ cb4f078

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since cb4f078 was cb4f078, checked in by Martin Decky <martin@…>, 14 years ago

unify kernel byte string implementations

  • Property mode set to 100644
File size: 9.1 KB
Line 
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 ofw
30 * @{
31 */
32/**
33 * @file
34 * @brief OpenFirmware device tree navigation.
35 *
36 */
37
38#include <genarch/ofw/ofw_tree.h>
39#include <mm/slab.h>
40#include <str.h>
41#include <panic.h>
42#include <print.h>
43
44#define PATH_MAX_LEN 256
45#define NAME_BUF_LEN 50
46
47static ofw_tree_node_t *ofw_root;
48
49void ofw_tree_init(ofw_tree_node_t *root)
50{
51 ofw_root = root;
52}
53
54/** Get OpenFirmware node property.
55 *
56 * @param node Node in which to lookup the property.
57 * @param name Name of the property.
58 *
59 * @return Pointer to the property structure or NULL if no such
60 * property.
61 *
62 */
63ofw_tree_property_t *ofw_tree_getprop(const ofw_tree_node_t *node,
64 const char *name)
65{
66 size_t i;
67
68 for (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 */
84const 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 */
102ofw_tree_node_t *ofw_tree_find_child(ofw_tree_node_t *node,
103 const char *name)
104{
105 ofw_tree_node_t *cur;
106
107 /*
108 * Try to find the disambigued name.
109 */
110 for (cur = node->child; cur; cur = cur->peer) {
111 if (str_cmp(cur->da_name, name) == 0)
112 return cur;
113 }
114
115 /*
116 * Disambigued name not found.
117 * Lets try our luck with possibly ambiguous "name" property.
118 *
119 * We need to do this because paths stored in "/aliases"
120 * are not always fully-qualified.
121 */
122 for (cur = node->child; cur; cur = cur->peer) {
123 if (str_cmp(ofw_tree_node_name(cur), name) == 0)
124 return cur;
125 }
126
127 return NULL;
128}
129
130/** Lookup first child of given device type.
131 *
132 * @param node Node whose child is being looked up.
133 * @param dtype Device type of the child being looked up.
134 *
135 * @return NULL if there is no such child or pointer to the
136 * matching child node.
137 *
138 */
139ofw_tree_node_t *ofw_tree_find_child_by_device_type(ofw_tree_node_t *node,
140 const char *dtype)
141{
142 ofw_tree_node_t *cur;
143
144 for (cur = node->child; cur; cur = cur->peer) {
145 ofw_tree_property_t *prop =
146 ofw_tree_getprop(cur, "device_type");
147
148 if ((!prop) || (!prop->value))
149 continue;
150
151 if (str_cmp(prop->value, dtype) == 0)
152 return cur;
153 }
154
155 return NULL;
156}
157
158/** Lookup node with matching node_handle.
159 *
160 * Child nodes are looked up recursively contrary to peer nodes that
161 * are looked up iteratively to avoid stack overflow.
162 *
163 * @param root Root of the searched subtree.
164 * @param handle OpenFirmware handle.
165 *
166 * @return NULL if there is no such node or pointer to the matching
167 * node.
168 *
169 */
170ofw_tree_node_t *ofw_tree_find_node_by_handle(ofw_tree_node_t *root,
171 phandle handle)
172{
173 ofw_tree_node_t *cur;
174
175 for (cur = root; cur; cur = cur->peer) {
176 if (cur->node_handle == handle)
177 return cur;
178
179 if (cur->child) {
180 ofw_tree_node_t *node
181 = ofw_tree_find_node_by_handle(cur->child, handle);
182 if (node)
183 return node;
184 }
185 }
186
187 return NULL;
188}
189
190/** Lookup first peer of given device type.
191 *
192 * @param node Node whose peer is being looked up.
193 * @param dtype Device type of the child being looked up.
194 *
195 * @return NULL if there is no such child or pointer to the
196 * matching child node.
197 *
198 */
199ofw_tree_node_t *ofw_tree_find_peer_by_device_type(ofw_tree_node_t *node,
200 const char *dtype)
201{
202 ofw_tree_node_t *cur;
203
204 for (cur = node->peer; cur; cur = cur->peer) {
205 ofw_tree_property_t *prop =
206 ofw_tree_getprop(cur, "device_type");
207
208 if ((!prop) || (!prop->value))
209 continue;
210
211 if (str_cmp(prop->value, dtype) == 0)
212 return cur;
213 }
214
215 return NULL;
216}
217
218/** Lookup first peer of given name.
219 *
220 * @param node Node whose peer is being looked up.
221 * @param name Name of the child being looked up.
222 *
223 * @return NULL if there is no such peer or pointer to the matching
224 * peer node.
225 *
226 */
227ofw_tree_node_t *ofw_tree_find_peer_by_name(ofw_tree_node_t *node,
228 const char *name)
229{
230 ofw_tree_node_t *cur;
231
232 for (cur = node->peer; cur; cur = cur->peer) {
233 ofw_tree_property_t *prop
234 = ofw_tree_getprop(cur, "name");
235
236 if ((!prop) || (!prop->value))
237 continue;
238
239 if (str_cmp(prop->value, name) == 0)
240 return cur;
241 }
242
243 return NULL;
244}
245
246/** Lookup OpenFirmware node by its path.
247 *
248 * @param path Path to the node.
249 *
250 * @return NULL if there is no such node or pointer to the leaf
251 * node.
252 *
253 */
254ofw_tree_node_t *ofw_tree_lookup(const char *path)
255{
256 if (path[0] != '/')
257 return NULL;
258
259 ofw_tree_node_t *node = ofw_root;
260 size_t i;
261 size_t j;
262
263 for (i = 1; (i < str_size(path)) && (node); i = j + 1) {
264 for (j = i; (j < str_size(path)) && (path[j] != '/'); j++);
265
266 /* Skip extra slashes */
267 if (i == j)
268 continue;
269
270 char buf[NAME_BUF_LEN + 1];
271 memcpy(buf, &path[i], j - i);
272 buf[j - i] = 0;
273 node = ofw_tree_find_child(node, buf);
274 }
275
276 return node;
277}
278
279/** Walk the OpenFirmware device subtree rooted in a node.
280 *
281 * Child nodes are processed recursively and peer nodes are processed
282 * iteratively in order to avoid stack overflow.
283 *
284 * @param node Root of the subtree.
285 * @param dtype Device type to look for.
286 * @param walker Routine to be invoked on found device.
287 * @param arg User argument to the walker.
288 *
289 * @return True if the walk should continue.
290 *
291 */
292static bool ofw_tree_walk_by_device_type_internal(ofw_tree_node_t *node,
293 const char *dtype, ofw_tree_walker_t walker, void *arg)
294{
295 ofw_tree_node_t *cur;
296
297 for (cur = node; cur; cur = cur->peer) {
298 ofw_tree_property_t *prop =
299 ofw_tree_getprop(cur, "device_type");
300
301 if ((prop) && (prop->value) && (str_cmp(prop->value, dtype) == 0)) {
302 bool ret = walker(cur, arg);
303 if (!ret)
304 return false;
305 }
306
307 if (cur->child) {
308 bool ret =
309 ofw_tree_walk_by_device_type_internal(cur->child, dtype, walker, arg);
310 if (!ret)
311 return false;
312 }
313 }
314
315 return true;
316}
317
318/** Walk the OpenFirmware device tree and find devices by type.
319 *
320 * Walk the whole OpenFirmware device tree and if any node has
321 * the property "device_type" equal to dtype, run a walker on it.
322 * If the walker returns false, the walk does not continue.
323 *
324 * @param dtype Device type to look for.
325 * @param walker Routine to be invoked on found device.
326 * @param arg User argument to the walker.
327 *
328 */
329void ofw_tree_walk_by_device_type(const char *dtype, ofw_tree_walker_t walker,
330 void *arg)
331{
332 (void) ofw_tree_walk_by_device_type_internal(ofw_root, dtype, walker, arg);
333}
334
335/** Print OpenFirmware device subtree rooted in a node.
336 *
337 * Child nodes are processed recursively and peer nodes are processed
338 * iteratively in order to avoid stack overflow.
339 *
340 * @param node Root of the subtree.
341 * @param path Current path, NULL for the very root of the entire tree.
342 *
343 */
344static void ofw_tree_node_print(ofw_tree_node_t *node, const char *path)
345{
346 char *cur_path = (char *) malloc(PATH_MAX_LEN, 0);
347 ofw_tree_node_t *cur;
348
349 for (cur = node; cur; cur = cur->peer) {
350 if ((cur->parent) && (path)) {
351 snprintf(cur_path, PATH_MAX_LEN, "%s/%s", path, cur->da_name);
352 printf("%s\n", cur_path);
353 } else {
354 snprintf(cur_path, PATH_MAX_LEN, "%s", cur->da_name);
355 printf("/\n");
356 }
357
358 if (cur->child)
359 ofw_tree_node_print(cur->child, cur_path);
360 }
361
362 free(cur_path);
363}
364
365/** Print the structure of the OpenFirmware device tree. */
366void ofw_tree_print(void)
367{
368 ofw_tree_node_print(ofw_root, NULL);
369}
370
371/** @}
372 */
Note: See TracBrowser for help on using the repository browser.