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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a0e1b48 was 16da5f8e, checked in by Jiri Svoboda <jirik.svoboda@…>, 16 years ago

String functions should be declared in string.h (and implemented in string.c) in the kernel.

  • Property mode set to 100644
File size: 7.4 KB
RevLine 
[61e90dd]1/*
[df4ed85]2 * Copyright (c) 2006 Jakub Jermar
[61e90dd]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
[16529d5]38#include <genarch/ofw/ofw_tree.h>
39#include <arch/memstr.h>
[1b43a04]40#include <mm/slab.h>
[16da5f8e]41#include <string.h>
[16529d5]42#include <print.h>
43#include <panic.h>
44
45#define PATH_MAX_LEN 80
46#define NAME_BUF_LEN 50
47
48static ofw_tree_node_t *ofw_root;
49
50void ofw_tree_init(ofw_tree_node_t *root)
51{
52 ofw_root = root;
53}
54
[28ecadb]55/** Get OpenFirmware node property.
56 *
[965dc18]57 * @param node Node in which to lookup the property.
58 * @param name Name of the property.
[28ecadb]59 *
[965dc18]60 * @return Pointer to the property structure or NULL if no such
61 * property.
[28ecadb]62 */
[965dc18]63ofw_tree_property_t *
64ofw_tree_getprop(const ofw_tree_node_t *node, const char *name)
[28ecadb]65{
[6c441cf8]66 unsigned int i;
[28ecadb]67
68 for (i = 0; i < node->properties; i++) {
69 if (strcmp(node->property[i].name, name) == 0)
70 return &node->property[i];
71 }
72
73 return NULL;
74}
75
[16529d5]76/** Return value of the 'name' property.
77 *
[965dc18]78 * @param node Node of interest.
[16529d5]79 *
[965dc18]80 * @return Value of the 'name' property belonging to the node.
[16529d5]81 */
82const char *ofw_tree_node_name(const ofw_tree_node_t *node)
83{
[28ecadb]84 ofw_tree_property_t *prop;
[16529d5]85
[28ecadb]86 prop = ofw_tree_getprop(node, "name");
87 if (!prop)
[f651e80]88 panic("Node without name property.");
[28ecadb]89
90 if (prop->size < 2)
[f651e80]91 panic("Invalid name property.");
[16529d5]92
[28ecadb]93 return prop->value;
[16529d5]94}
95
96/** Lookup child of given name.
97 *
[965dc18]98 * @param node Node whose child is being looked up.
99 * @param name Name of the child being looked up.
[16529d5]100 *
[965dc18]101 * @return NULL if there is no such child or pointer to the
102 * matching child node.
[16529d5]103 */
[45b26dad]104ofw_tree_node_t *ofw_tree_find_child(ofw_tree_node_t *node, const char *name)
[16529d5]105{
106 ofw_tree_node_t *cur;
107
[28ecadb]108 /*
109 * Try to find the disambigued name.
110 */
[16529d5]111 for (cur = node->child; cur; cur = cur->peer) {
[28ecadb]112 if (strcmp(cur->da_name, name) == 0)
[16529d5]113 return cur;
114 }
115
[28ecadb]116 /*
117 * Disambigued name not found.
118 * Lets try our luck with possibly ambiguous "name" property.
119 *
120 * We need to do this because paths stored in "/aliases"
121 * are not always fully-qualified.
122 */
123 for (cur = node->child; cur; cur = cur->peer) {
124 if (strcmp(ofw_tree_node_name(cur), name) == 0)
125 return cur;
126 }
127
[16529d5]128 return NULL;
129}
130
[45b26dad]131/** Lookup first child of given device type.
132 *
[965dc18]133 * @param node Node whose child is being looked up.
134 * @param name Device type of the child being looked up.
[45b26dad]135 *
[965dc18]136 * @return NULL if there is no such child or pointer to the
137 * matching child node.
[45b26dad]138 */
[965dc18]139ofw_tree_node_t *
140ofw_tree_find_child_by_device_type(ofw_tree_node_t *node, const char *name)
[45b26dad]141{
142 ofw_tree_node_t *cur;
143 ofw_tree_property_t *prop;
144
145 for (cur = node->child; cur; cur = cur->peer) {
146 prop = ofw_tree_getprop(cur, "device_type");
147 if (!prop || !prop->value)
148 continue;
149 if (strcmp(prop->value, name) == 0)
150 return cur;
151 }
152
153 return NULL;
154}
155
[36db5ac]156/** Lookup node with matching node_handle.
[2f8f480]157 *
158 * Child nodes are looked up recursively contrary to peer nodes that
159 * are looked up iteratively to avoid stack overflow.
[36db5ac]160 *
[965dc18]161 * @param root Root of the searched subtree.
162 * @param handle OpenFirmware handle.
[36db5ac]163 *
[965dc18]164 * @return NULL if there is no such node or pointer to the matching
165 * node.
[36db5ac]166 */
[965dc18]167ofw_tree_node_t *
168ofw_tree_find_node_by_handle(ofw_tree_node_t *root, uint32_t handle)
[36db5ac]169{
[2f8f480]170 ofw_tree_node_t *cur;
[36db5ac]171
[2f8f480]172 for (cur = root; cur; cur = cur->peer) {
173 if (cur->node_handle == handle)
174 return cur;
[36db5ac]175
[2f8f480]176 if (cur->child) {
177 ofw_tree_node_t *node;
178
179 node = ofw_tree_find_node_by_handle(cur->child, handle);
180 if (node)
181 return node;
182 }
[36db5ac]183 }
184
185 return NULL;
186}
187
[45b26dad]188/** Lookup first peer of given device type.
189 *
[965dc18]190 * @param node Node whose peer is being looked up.
191 * @param name Device type of the child being looked up.
[45b26dad]192 *
[965dc18]193 * @return NULL if there is no such child or pointer to the
194 * matching child node.
[45b26dad]195 */
[965dc18]196ofw_tree_node_t *
197ofw_tree_find_peer_by_device_type(ofw_tree_node_t *node, const char *name)
[45b26dad]198{
199 ofw_tree_node_t *cur;
200 ofw_tree_property_t *prop;
201
202 for (cur = node->peer; cur; cur = cur->peer) {
203 prop = ofw_tree_getprop(cur, "device_type");
204 if (!prop || !prop->value)
205 continue;
206 if (strcmp(prop->value, name) == 0)
207 return cur;
208 }
209
210 return NULL;
211}
212
213
[965dc18]214/** Lookup first peer of given name.
215 *
216 * @param node Node whose peer is being looked up.
217 * @param name Name of the child being looked up.
218 *
219 * @return NULL if there is no such peer or pointer to the matching
220 * peer node.
221 */
222ofw_tree_node_t *
223ofw_tree_find_peer_by_name(ofw_tree_node_t *node, const char *name)
224{
225 ofw_tree_node_t *cur;
226 ofw_tree_property_t *prop;
227
228 for (cur = node->peer; cur; cur = cur->peer) {
229 prop = ofw_tree_getprop(cur, "name");
230 if (!prop || !prop->value)
231 continue;
232 if (strcmp(prop->value, name) == 0)
233 return cur;
234 }
235
236 return NULL;
237}
238
[16529d5]239/** Lookup OpenFirmware node by its path.
240 *
[965dc18]241 * @param path Path to the node.
[16529d5]242 *
[965dc18]243 * @return NULL if there is no such node or pointer to the leaf
244 * node.
[16529d5]245 */
246ofw_tree_node_t *ofw_tree_lookup(const char *path)
247{
[965dc18]248 char buf[NAME_BUF_LEN + 1];
[16529d5]249 ofw_tree_node_t *node = ofw_root;
250 index_t i, j;
251
252 if (path[0] != '/')
253 return NULL;
254
255 for (i = 1; i < strlen(path) && node; i = j + 1) {
256 for (j = i; j < strlen(path) && path[j] != '/'; j++)
257 ;
258 if (i == j) /* skip extra slashes */
259 continue;
260
261 memcpy(buf, &path[i], j - i);
262 buf[j - i] = '\0';
263 node = ofw_tree_find_child(node, buf);
264 }
265
266 return node;
267}
268
[2f8f480]269/** Print OpenFirmware device subtree rooted in a node.
270 *
271 * Child nodes are processed recursively and peer nodes are processed
272 * iteratively in order to avoid stack overflow.
[16529d5]273 *
[965dc18]274 * @param node Root of the subtree.
275 * @param path Current path, NULL for the very root of the entire tree.
[16529d5]276 */
277static void ofw_tree_node_print(const ofw_tree_node_t *node, const char *path)
278{
[1b43a04]279 char *p;
[2f8f480]280 const ofw_tree_node_t *cur;
[1b43a04]281
282 p = (char *) malloc(PATH_MAX_LEN, 0);
283
[2f8f480]284 for (cur = node; cur; cur = cur->peer) {
285 if (cur->parent) {
286 snprintf(p, PATH_MAX_LEN, "%s/%s", path, cur->da_name);
287 printf("%s\n", p);
288 } else {
289 snprintf(p, PATH_MAX_LEN, "%s", cur->da_name);
290 printf("/\n");
291 }
[16529d5]292
[2f8f480]293 if (cur->child)
294 ofw_tree_node_print(cur->child, p);
295 }
[1b43a04]296
297 free(p);
[16529d5]298}
299
300/** Print the structure of the OpenFirmware device tree. */
301void ofw_tree_print(void)
302{
303 ofw_tree_node_print(ofw_root, NULL);
304}
305
[61e90dd]306/** @}
307 */
Note: See TracBrowser for help on using the repository browser.