Rev 4296 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4296 | Rev 4537 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2006 Jakub Jermar |
2 | * Copyright (c) 2006 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup ofw |
29 | /** @addtogroup ofw |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | /** |
32 | /** |
33 | * @file |
33 | * @file |
34 | * @brief OpenFirmware device tree navigation. |
34 | * @brief OpenFirmware device tree navigation. |
35 | * |
35 | * |
36 | */ |
36 | */ |
37 | 37 | ||
38 | #include <genarch/ofw/ofw_tree.h> |
38 | #include <genarch/ofw/ofw_tree.h> |
39 | #include <arch/memstr.h> |
39 | #include <arch/memstr.h> |
40 | #include <mm/slab.h> |
40 | #include <mm/slab.h> |
41 | #include <string.h> |
41 | #include <string.h> |
42 | #include <print.h> |
42 | #include <print.h> |
43 | #include <panic.h> |
43 | #include <panic.h> |
44 | 44 | ||
45 | #define PATH_MAX_LEN 80 |
45 | #define PATH_MAX_LEN 80 |
46 | #define NAME_BUF_LEN 50 |
46 | #define NAME_BUF_LEN 50 |
47 | 47 | ||
48 | static ofw_tree_node_t *ofw_root; |
48 | static ofw_tree_node_t *ofw_root; |
49 | 49 | ||
50 | void ofw_tree_init(ofw_tree_node_t *root) |
50 | void ofw_tree_init(ofw_tree_node_t *root) |
51 | { |
51 | { |
52 | ofw_root = root; |
52 | ofw_root = root; |
53 | } |
53 | } |
54 | 54 | ||
55 | /** Get OpenFirmware node property. |
55 | /** Get OpenFirmware node property. |
56 | * |
56 | * |
57 | * @param node Node in which to lookup the property. |
57 | * @param node Node in which to lookup the property. |
58 | * @param name Name of the property. |
58 | * @param name Name of the property. |
59 | * |
59 | * |
60 | * @return Pointer to the property structure or NULL if no such |
60 | * @return Pointer to the property structure or NULL if no such |
61 | * property. |
61 | * property. |
62 | */ |
62 | */ |
63 | ofw_tree_property_t * |
63 | ofw_tree_property_t * |
64 | ofw_tree_getprop(const ofw_tree_node_t *node, const char *name) |
64 | ofw_tree_getprop(const ofw_tree_node_t *node, const char *name) |
65 | { |
65 | { |
66 | unsigned int i; |
66 | unsigned int i; |
67 | 67 | ||
68 | for (i = 0; i < node->properties; i++) { |
68 | for (i = 0; i < node->properties; i++) { |
69 | if (str_cmp(node->property[i].name, name) == 0) |
69 | if (str_cmp(node->property[i].name, name) == 0) |
70 | return &node->property[i]; |
70 | return &node->property[i]; |
71 | } |
71 | } |
72 | 72 | ||
73 | return NULL; |
73 | return NULL; |
74 | } |
74 | } |
75 | 75 | ||
76 | /** Return value of the 'name' property. |
76 | /** Return value of the 'name' property. |
77 | * |
77 | * |
78 | * @param node Node of interest. |
78 | * @param node Node of interest. |
79 | * |
79 | * |
80 | * @return Value of the 'name' property belonging to the node. |
80 | * @return Value of the 'name' property belonging to the node. |
81 | */ |
81 | */ |
82 | const char *ofw_tree_node_name(const ofw_tree_node_t *node) |
82 | const char *ofw_tree_node_name(const ofw_tree_node_t *node) |
83 | { |
83 | { |
84 | ofw_tree_property_t *prop; |
84 | ofw_tree_property_t *prop; |
85 | 85 | ||
86 | prop = ofw_tree_getprop(node, "name"); |
86 | prop = ofw_tree_getprop(node, "name"); |
87 | if (!prop) |
87 | if (!prop) |
88 | panic("Node without name property."); |
88 | panic("Node without name property."); |
89 | 89 | ||
90 | if (prop->size < 2) |
90 | if (prop->size < 2) |
91 | panic("Invalid name property."); |
91 | panic("Invalid name property."); |
92 | 92 | ||
93 | return prop->value; |
93 | return prop->value; |
94 | } |
94 | } |
95 | 95 | ||
96 | /** Lookup child of given name. |
96 | /** Lookup child of given name. |
97 | * |
97 | * |
98 | * @param node Node whose child is being looked up. |
98 | * @param node Node whose child is being looked up. |
99 | * @param name Name of the child being looked up. |
99 | * @param name Name of the child being looked up. |
100 | * |
100 | * |
101 | * @return NULL if there is no such child or pointer to the |
101 | * @return NULL if there is no such child or pointer to the |
102 | * matching child node. |
102 | * matching child node. |
103 | */ |
103 | */ |
104 | ofw_tree_node_t *ofw_tree_find_child(ofw_tree_node_t *node, const char *name) |
104 | ofw_tree_node_t *ofw_tree_find_child(ofw_tree_node_t *node, const char *name) |
105 | { |
105 | { |
106 | ofw_tree_node_t *cur; |
106 | ofw_tree_node_t *cur; |
107 | 107 | ||
108 | /* |
108 | /* |
109 | * Try to find the disambigued name. |
109 | * Try to find the disambigued name. |
110 | */ |
110 | */ |
111 | for (cur = node->child; cur; cur = cur->peer) { |
111 | for (cur = node->child; cur; cur = cur->peer) { |
112 | if (str_cmp(cur->da_name, name) == 0) |
112 | if (str_cmp(cur->da_name, name) == 0) |
113 | return cur; |
113 | return cur; |
114 | } |
114 | } |
115 | 115 | ||
116 | /* |
116 | /* |
117 | * Disambigued name not found. |
117 | * Disambigued name not found. |
118 | * Lets try our luck with possibly ambiguous "name" property. |
118 | * Lets try our luck with possibly ambiguous "name" property. |
119 | * |
119 | * |
120 | * We need to do this because paths stored in "/aliases" |
120 | * We need to do this because paths stored in "/aliases" |
121 | * are not always fully-qualified. |
121 | * are not always fully-qualified. |
122 | */ |
122 | */ |
123 | for (cur = node->child; cur; cur = cur->peer) { |
123 | for (cur = node->child; cur; cur = cur->peer) { |
124 | if (str_cmp(ofw_tree_node_name(cur), name) == 0) |
124 | if (str_cmp(ofw_tree_node_name(cur), name) == 0) |
125 | return cur; |
125 | return cur; |
126 | } |
126 | } |
127 | 127 | ||
128 | return NULL; |
128 | return NULL; |
129 | } |
129 | } |
130 | 130 | ||
131 | /** Lookup first child of given device type. |
131 | /** Lookup first child of given device type. |
132 | * |
132 | * |
133 | * @param node Node whose child is being looked up. |
133 | * @param node Node whose child is being looked up. |
134 | * @param name Device type of the child being looked up. |
134 | * @param name Device type of the child being looked up. |
135 | * |
135 | * |
136 | * @return NULL if there is no such child or pointer to the |
136 | * @return NULL if there is no such child or pointer to the |
137 | * matching child node. |
137 | * matching child node. |
138 | */ |
138 | */ |
139 | ofw_tree_node_t * |
139 | ofw_tree_node_t * |
140 | ofw_tree_find_child_by_device_type(ofw_tree_node_t *node, const char *name) |
140 | ofw_tree_find_child_by_device_type(ofw_tree_node_t *node, const char *name) |
141 | { |
141 | { |
142 | ofw_tree_node_t *cur; |
142 | ofw_tree_node_t *cur; |
143 | ofw_tree_property_t *prop; |
143 | ofw_tree_property_t *prop; |
144 | 144 | ||
145 | for (cur = node->child; cur; cur = cur->peer) { |
145 | for (cur = node->child; cur; cur = cur->peer) { |
146 | prop = ofw_tree_getprop(cur, "device_type"); |
146 | prop = ofw_tree_getprop(cur, "device_type"); |
147 | if (!prop || !prop->value) |
147 | if (!prop || !prop->value) |
148 | continue; |
148 | continue; |
149 | if (str_cmp(prop->value, name) == 0) |
149 | if (str_cmp(prop->value, name) == 0) |
150 | return cur; |
150 | return cur; |
151 | } |
151 | } |
152 | 152 | ||
153 | return NULL; |
153 | return NULL; |
154 | } |
154 | } |
155 | 155 | ||
156 | /** Lookup node with matching node_handle. |
156 | /** Lookup node with matching node_handle. |
157 | * |
157 | * |
158 | * Child nodes are looked up recursively contrary to peer nodes that |
158 | * Child nodes are looked up recursively contrary to peer nodes that |
159 | * are looked up iteratively to avoid stack overflow. |
159 | * are looked up iteratively to avoid stack overflow. |
160 | * |
160 | * |
161 | * @param root Root of the searched subtree. |
161 | * @param root Root of the searched subtree. |
162 | * @param handle OpenFirmware handle. |
162 | * @param handle OpenFirmware handle. |
163 | * |
163 | * |
164 | * @return NULL if there is no such node or pointer to the matching |
164 | * @return NULL if there is no such node or pointer to the matching |
165 | * node. |
165 | * node. |
166 | */ |
166 | */ |
167 | ofw_tree_node_t * |
167 | ofw_tree_node_t * |
168 | ofw_tree_find_node_by_handle(ofw_tree_node_t *root, uint32_t handle) |
168 | ofw_tree_find_node_by_handle(ofw_tree_node_t *root, uint32_t handle) |
169 | { |
169 | { |
170 | ofw_tree_node_t *cur; |
170 | ofw_tree_node_t *cur; |
171 | 171 | ||
172 | for (cur = root; cur; cur = cur->peer) { |
172 | for (cur = root; cur; cur = cur->peer) { |
173 | if (cur->node_handle == handle) |
173 | if (cur->node_handle == handle) |
174 | return cur; |
174 | return cur; |
175 | 175 | ||
176 | if (cur->child) { |
176 | if (cur->child) { |
177 | ofw_tree_node_t *node; |
177 | ofw_tree_node_t *node; |
178 | 178 | ||
179 | node = ofw_tree_find_node_by_handle(cur->child, handle); |
179 | node = ofw_tree_find_node_by_handle(cur->child, handle); |
180 | if (node) |
180 | if (node) |
181 | return node; |
181 | return node; |
182 | } |
182 | } |
183 | } |
183 | } |
184 | 184 | ||
185 | return NULL; |
185 | return NULL; |
186 | } |
186 | } |
187 | 187 | ||
188 | /** Lookup first peer of given device type. |
188 | /** Lookup first peer of given device type. |
189 | * |
189 | * |
190 | * @param node Node whose peer is being looked up. |
190 | * @param node Node whose peer is being looked up. |
191 | * @param name Device type of the child being looked up. |
191 | * @param name Device type of the child being looked up. |
192 | * |
192 | * |
193 | * @return NULL if there is no such child or pointer to the |
193 | * @return NULL if there is no such child or pointer to the |
194 | * matching child node. |
194 | * matching child node. |
195 | */ |
195 | */ |
196 | ofw_tree_node_t * |
196 | ofw_tree_node_t * |
197 | ofw_tree_find_peer_by_device_type(ofw_tree_node_t *node, const char *name) |
197 | ofw_tree_find_peer_by_device_type(ofw_tree_node_t *node, const char *name) |
198 | { |
198 | { |
199 | ofw_tree_node_t *cur; |
199 | ofw_tree_node_t *cur; |
200 | ofw_tree_property_t *prop; |
200 | ofw_tree_property_t *prop; |
201 | 201 | ||
202 | for (cur = node->peer; cur; cur = cur->peer) { |
202 | for (cur = node->peer; cur; cur = cur->peer) { |
203 | prop = ofw_tree_getprop(cur, "device_type"); |
203 | prop = ofw_tree_getprop(cur, "device_type"); |
204 | if (!prop || !prop->value) |
204 | if (!prop || !prop->value) |
205 | continue; |
205 | continue; |
206 | if (str_cmp(prop->value, name) == 0) |
206 | if (str_cmp(prop->value, name) == 0) |
207 | return cur; |
207 | return cur; |
208 | } |
208 | } |
209 | 209 | ||
210 | return NULL; |
210 | return NULL; |
211 | } |
211 | } |
212 | 212 | ||
213 | 213 | ||
214 | /** Lookup first peer of given name. |
214 | /** Lookup first peer of given name. |
215 | * |
215 | * |
216 | * @param node Node whose peer is being looked up. |
216 | * @param node Node whose peer is being looked up. |
217 | * @param name Name of the child being looked up. |
217 | * @param name Name of the child being looked up. |
218 | * |
218 | * |
219 | * @return NULL if there is no such peer or pointer to the matching |
219 | * @return NULL if there is no such peer or pointer to the matching |
220 | * peer node. |
220 | * peer node. |
221 | */ |
221 | */ |
222 | ofw_tree_node_t * |
222 | ofw_tree_node_t * |
223 | ofw_tree_find_peer_by_name(ofw_tree_node_t *node, const char *name) |
223 | ofw_tree_find_peer_by_name(ofw_tree_node_t *node, const char *name) |
224 | { |
224 | { |
225 | ofw_tree_node_t *cur; |
225 | ofw_tree_node_t *cur; |
226 | ofw_tree_property_t *prop; |
226 | ofw_tree_property_t *prop; |
227 | 227 | ||
228 | for (cur = node->peer; cur; cur = cur->peer) { |
228 | for (cur = node->peer; cur; cur = cur->peer) { |
229 | prop = ofw_tree_getprop(cur, "name"); |
229 | prop = ofw_tree_getprop(cur, "name"); |
230 | if (!prop || !prop->value) |
230 | if (!prop || !prop->value) |
231 | continue; |
231 | continue; |
232 | if (str_cmp(prop->value, name) == 0) |
232 | if (str_cmp(prop->value, name) == 0) |
233 | return cur; |
233 | return cur; |
234 | } |
234 | } |
235 | 235 | ||
236 | return NULL; |
236 | return NULL; |
237 | } |
237 | } |
238 | 238 | ||
239 | /** Lookup OpenFirmware node by its path. |
239 | /** Lookup OpenFirmware node by its path. |
240 | * |
240 | * |
241 | * @param path Path to the node. |
241 | * @param path Path to the node. |
242 | * |
242 | * |
243 | * @return NULL if there is no such node or pointer to the leaf |
243 | * @return NULL if there is no such node or pointer to the leaf |
244 | * node. |
244 | * node. |
245 | */ |
245 | */ |
246 | ofw_tree_node_t *ofw_tree_lookup(const char *path) |
246 | ofw_tree_node_t *ofw_tree_lookup(const char *path) |
247 | { |
247 | { |
248 | char buf[NAME_BUF_LEN + 1]; |
248 | char buf[NAME_BUF_LEN + 1]; |
249 | ofw_tree_node_t *node = ofw_root; |
249 | ofw_tree_node_t *node = ofw_root; |
- | 250 | size_t i; |
|
250 | index_t i, j; |
251 | size_t j; |
251 | 252 | ||
252 | if (path[0] != '/') |
253 | if (path[0] != '/') |
253 | return NULL; |
254 | return NULL; |
254 | 255 | ||
255 | for (i = 1; (i < str_size(path)) && (node); i = j + 1) { |
256 | for (i = 1; (i < str_size(path)) && (node); i = j + 1) { |
256 | for (j = i; (j < str_size(path)) && (path[j] != '/'); j++); |
257 | for (j = i; (j < str_size(path)) && (path[j] != '/'); j++); |
257 | 258 | ||
258 | /* Skip extra slashes */ |
259 | /* Skip extra slashes */ |
259 | if (i == j) |
260 | if (i == j) |
260 | continue; |
261 | continue; |
261 | 262 | ||
262 | memcpy(buf, &path[i], j - i); |
263 | memcpy(buf, &path[i], j - i); |
263 | buf[j - i] = 0; |
264 | buf[j - i] = 0; |
264 | node = ofw_tree_find_child(node, buf); |
265 | node = ofw_tree_find_child(node, buf); |
265 | } |
266 | } |
266 | 267 | ||
267 | return node; |
268 | return node; |
268 | } |
269 | } |
269 | 270 | ||
270 | /** Print OpenFirmware device subtree rooted in a node. |
271 | /** Print OpenFirmware device subtree rooted in a node. |
271 | * |
272 | * |
272 | * Child nodes are processed recursively and peer nodes are processed |
273 | * Child nodes are processed recursively and peer nodes are processed |
273 | * iteratively in order to avoid stack overflow. |
274 | * iteratively in order to avoid stack overflow. |
274 | * |
275 | * |
275 | * @param node Root of the subtree. |
276 | * @param node Root of the subtree. |
276 | * @param path Current path, NULL for the very root of the entire tree. |
277 | * @param path Current path, NULL for the very root of the entire tree. |
277 | */ |
278 | */ |
278 | static void ofw_tree_node_print(const ofw_tree_node_t *node, const char *path) |
279 | static void ofw_tree_node_print(const ofw_tree_node_t *node, const char *path) |
279 | { |
280 | { |
280 | char *p; |
281 | char *p; |
281 | const ofw_tree_node_t *cur; |
282 | const ofw_tree_node_t *cur; |
282 | 283 | ||
283 | p = (char *) malloc(PATH_MAX_LEN, 0); |
284 | p = (char *) malloc(PATH_MAX_LEN, 0); |
284 | 285 | ||
285 | for (cur = node; cur; cur = cur->peer) { |
286 | for (cur = node; cur; cur = cur->peer) { |
286 | if (cur->parent) { |
287 | if (cur->parent) { |
287 | snprintf(p, PATH_MAX_LEN, "%s/%s", path, cur->da_name); |
288 | snprintf(p, PATH_MAX_LEN, "%s/%s", path, cur->da_name); |
288 | printf("%s\n", p); |
289 | printf("%s\n", p); |
289 | } else { |
290 | } else { |
290 | snprintf(p, PATH_MAX_LEN, "%s", cur->da_name); |
291 | snprintf(p, PATH_MAX_LEN, "%s", cur->da_name); |
291 | printf("/\n"); |
292 | printf("/\n"); |
292 | } |
293 | } |
293 | 294 | ||
294 | if (cur->child) |
295 | if (cur->child) |
295 | ofw_tree_node_print(cur->child, p); |
296 | ofw_tree_node_print(cur->child, p); |
296 | } |
297 | } |
297 | 298 | ||
298 | free(p); |
299 | free(p); |
299 | } |
300 | } |
300 | 301 | ||
301 | /** Print the structure of the OpenFirmware device tree. */ |
302 | /** Print the structure of the OpenFirmware device tree. */ |
302 | void ofw_tree_print(void) |
303 | void ofw_tree_print(void) |
303 | { |
304 | { |
304 | ofw_tree_node_print(ofw_root, NULL); |
305 | ofw_tree_node_print(ofw_root, NULL); |
305 | } |
306 | } |
306 | 307 | ||
307 | /** @} |
308 | /** @} |
308 | */ |
309 | */ |
309 | 310 |