Rev 1908 | Rev 1974 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1908 | Rev 1972 | ||
---|---|---|---|
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 <func.h> |
41 | #include <func.h> |
41 | #include <print.h> |
42 | #include <print.h> |
42 | #include <panic.h> |
43 | #include <panic.h> |
43 | 44 | ||
44 | #define PATH_MAX_LEN 80 |
45 | #define PATH_MAX_LEN 80 |
45 | #define NAME_BUF_LEN 50 |
46 | #define NAME_BUF_LEN 50 |
46 | 47 | ||
47 | static ofw_tree_node_t *ofw_root; |
48 | static ofw_tree_node_t *ofw_root; |
48 | 49 | ||
49 | void ofw_tree_init(ofw_tree_node_t *root) |
50 | void ofw_tree_init(ofw_tree_node_t *root) |
50 | { |
51 | { |
51 | ofw_root = root; |
52 | ofw_root = root; |
52 | } |
53 | } |
53 | 54 | ||
54 | /** Get OpenFirmware node property. |
55 | /** Get OpenFirmware node property. |
55 | * |
56 | * |
56 | * @param node Node in which to lookup the property. |
57 | * @param node Node in which to lookup the property. |
57 | * @param name Name of the property. |
58 | * @param name Name of the property. |
58 | * |
59 | * |
59 | * @return Pointer to the property structure or NULL if no such property. |
60 | * @return Pointer to the property structure or NULL if no such property. |
60 | */ |
61 | */ |
61 | ofw_tree_property_t *ofw_tree_getprop(const ofw_tree_node_t *node, const char *name) |
62 | ofw_tree_property_t *ofw_tree_getprop(const ofw_tree_node_t *node, const char *name) |
62 | { |
63 | { |
63 | int i; |
64 | int i; |
64 | 65 | ||
65 | for (i = 0; i < node->properties; i++) { |
66 | for (i = 0; i < node->properties; i++) { |
66 | if (strcmp(node->property[i].name, name) == 0) |
67 | if (strcmp(node->property[i].name, name) == 0) |
67 | return &node->property[i]; |
68 | return &node->property[i]; |
68 | } |
69 | } |
69 | 70 | ||
70 | return NULL; |
71 | return NULL; |
71 | } |
72 | } |
72 | 73 | ||
73 | /** Return value of the 'name' property. |
74 | /** Return value of the 'name' property. |
74 | * |
75 | * |
75 | * @param node Node of interest. |
76 | * @param node Node of interest. |
76 | * |
77 | * |
77 | * @return Value of the 'name' property belonging to the node. |
78 | * @return Value of the 'name' property belonging to the node. |
78 | */ |
79 | */ |
79 | const char *ofw_tree_node_name(const ofw_tree_node_t *node) |
80 | const char *ofw_tree_node_name(const ofw_tree_node_t *node) |
80 | { |
81 | { |
81 | ofw_tree_property_t *prop; |
82 | ofw_tree_property_t *prop; |
82 | 83 | ||
83 | prop = ofw_tree_getprop(node, "name"); |
84 | prop = ofw_tree_getprop(node, "name"); |
84 | if (!prop) |
85 | if (!prop) |
85 | panic("Node without name property.\n"); |
86 | panic("Node without name property.\n"); |
86 | 87 | ||
87 | if (prop->size < 2) |
88 | if (prop->size < 2) |
88 | panic("Invalid name property.\n"); |
89 | panic("Invalid name property.\n"); |
89 | 90 | ||
90 | return prop->value; |
91 | return prop->value; |
91 | } |
92 | } |
92 | 93 | ||
93 | /** Lookup child of given name. |
94 | /** Lookup child of given name. |
94 | * |
95 | * |
95 | * @param node Node whose child is being looked up. |
96 | * @param node Node whose child is being looked up. |
96 | * @param name Name of the child being looked up. |
97 | * @param name Name of the child being looked up. |
97 | * |
98 | * |
98 | * @return NULL if there is no such child or pointer to the matching child node. |
99 | * @return NULL if there is no such child or pointer to the matching child node. |
99 | */ |
100 | */ |
100 | ofw_tree_node_t *ofw_tree_find_child(ofw_tree_node_t *node, const char *name) |
101 | ofw_tree_node_t *ofw_tree_find_child(ofw_tree_node_t *node, const char *name) |
101 | { |
102 | { |
102 | ofw_tree_node_t *cur; |
103 | ofw_tree_node_t *cur; |
103 | 104 | ||
104 | /* |
105 | /* |
105 | * Try to find the disambigued name. |
106 | * Try to find the disambigued name. |
106 | */ |
107 | */ |
107 | for (cur = node->child; cur; cur = cur->peer) { |
108 | for (cur = node->child; cur; cur = cur->peer) { |
108 | if (strcmp(cur->da_name, name) == 0) |
109 | if (strcmp(cur->da_name, name) == 0) |
109 | return cur; |
110 | return cur; |
110 | } |
111 | } |
111 | 112 | ||
112 | /* |
113 | /* |
113 | * Disambigued name not found. |
114 | * Disambigued name not found. |
114 | * Lets try our luck with possibly ambiguous "name" property. |
115 | * Lets try our luck with possibly ambiguous "name" property. |
115 | * |
116 | * |
116 | * We need to do this because paths stored in "/aliases" |
117 | * We need to do this because paths stored in "/aliases" |
117 | * are not always fully-qualified. |
118 | * are not always fully-qualified. |
118 | */ |
119 | */ |
119 | for (cur = node->child; cur; cur = cur->peer) { |
120 | for (cur = node->child; cur; cur = cur->peer) { |
120 | if (strcmp(ofw_tree_node_name(cur), name) == 0) |
121 | if (strcmp(ofw_tree_node_name(cur), name) == 0) |
121 | return cur; |
122 | return cur; |
122 | } |
123 | } |
123 | 124 | ||
124 | return NULL; |
125 | return NULL; |
125 | } |
126 | } |
126 | 127 | ||
127 | /** Lookup first child of given device type. |
128 | /** Lookup first child of given device type. |
128 | * |
129 | * |
129 | * @param node Node whose child is being looked up. |
130 | * @param node Node whose child is being looked up. |
130 | * @param name Device type of the child being looked up. |
131 | * @param name Device type of the child being looked up. |
131 | * |
132 | * |
132 | * @return NULL if there is no such child or pointer to the matching child node. |
133 | * @return NULL if there is no such child or pointer to the matching child node. |
133 | */ |
134 | */ |
134 | ofw_tree_node_t *ofw_tree_find_child_by_device_type(ofw_tree_node_t *node, const char *name) |
135 | ofw_tree_node_t *ofw_tree_find_child_by_device_type(ofw_tree_node_t *node, const char *name) |
135 | { |
136 | { |
136 | ofw_tree_node_t *cur; |
137 | ofw_tree_node_t *cur; |
137 | ofw_tree_property_t *prop; |
138 | ofw_tree_property_t *prop; |
138 | 139 | ||
139 | for (cur = node->child; cur; cur = cur->peer) { |
140 | for (cur = node->child; cur; cur = cur->peer) { |
140 | prop = ofw_tree_getprop(cur, "device_type"); |
141 | prop = ofw_tree_getprop(cur, "device_type"); |
141 | if (!prop || !prop->value) |
142 | if (!prop || !prop->value) |
142 | continue; |
143 | continue; |
143 | if (strcmp(prop->value, name) == 0) |
144 | if (strcmp(prop->value, name) == 0) |
144 | return cur; |
145 | return cur; |
145 | } |
146 | } |
146 | 147 | ||
147 | return NULL; |
148 | return NULL; |
148 | } |
149 | } |
149 | 150 | ||
150 | /** Lookup node with matching node_handle. |
151 | /** Lookup node with matching node_handle. |
151 | * |
152 | * |
152 | * @param root Root of the searched subtree. |
153 | * @param root Root of the searched subtree. |
153 | * @param handle OpenFirmware handle. |
154 | * @param handle OpenFirmware handle. |
154 | * |
155 | * |
155 | * @return NULL if there is no such node or pointer to the matching node. |
156 | * @return NULL if there is no such node or pointer to the matching node. |
156 | */ |
157 | */ |
157 | ofw_tree_node_t *ofw_tree_find_node_by_handle(ofw_tree_node_t *root, uint32_t handle) |
158 | ofw_tree_node_t *ofw_tree_find_node_by_handle(ofw_tree_node_t *root, uint32_t handle) |
158 | { |
159 | { |
159 | ofw_tree_node_t *node; |
160 | ofw_tree_node_t *node; |
160 | 161 | ||
161 | if (root->node_handle == handle) |
162 | if (root->node_handle == handle) |
162 | return root; |
163 | return root; |
163 | 164 | ||
164 | if (root->peer) { |
165 | if (root->peer) { |
165 | node = ofw_tree_find_node_by_handle(root->peer, handle); |
166 | node = ofw_tree_find_node_by_handle(root->peer, handle); |
166 | if (node) |
167 | if (node) |
167 | return node; |
168 | return node; |
168 | } |
169 | } |
169 | 170 | ||
170 | if (root->child) { |
171 | if (root->child) { |
171 | node = ofw_tree_find_node_by_handle(root->child, handle); |
172 | node = ofw_tree_find_node_by_handle(root->child, handle); |
172 | if (node) |
173 | if (node) |
173 | return node; |
174 | return node; |
174 | } |
175 | } |
175 | 176 | ||
176 | return NULL; |
177 | return NULL; |
177 | } |
178 | } |
178 | 179 | ||
179 | /** Lookup first peer of given device type. |
180 | /** Lookup first peer of given device type. |
180 | * |
181 | * |
181 | * @param node Node whose peer is being looked up. |
182 | * @param node Node whose peer is being looked up. |
182 | * @param name Device type of the child being looked up. |
183 | * @param name Device type of the child being looked up. |
183 | * |
184 | * |
184 | * @return NULL if there is no such child or pointer to the matching child node. |
185 | * @return NULL if there is no such child or pointer to the matching child node. |
185 | */ |
186 | */ |
186 | ofw_tree_node_t *ofw_tree_find_peer_by_device_type(ofw_tree_node_t *node, const char *name) |
187 | ofw_tree_node_t *ofw_tree_find_peer_by_device_type(ofw_tree_node_t *node, const char *name) |
187 | { |
188 | { |
188 | ofw_tree_node_t *cur; |
189 | ofw_tree_node_t *cur; |
189 | ofw_tree_property_t *prop; |
190 | ofw_tree_property_t *prop; |
190 | 191 | ||
191 | for (cur = node->peer; cur; cur = cur->peer) { |
192 | for (cur = node->peer; cur; cur = cur->peer) { |
192 | prop = ofw_tree_getprop(cur, "device_type"); |
193 | prop = ofw_tree_getprop(cur, "device_type"); |
193 | if (!prop || !prop->value) |
194 | if (!prop || !prop->value) |
194 | continue; |
195 | continue; |
195 | if (strcmp(prop->value, name) == 0) |
196 | if (strcmp(prop->value, name) == 0) |
196 | return cur; |
197 | return cur; |
197 | } |
198 | } |
198 | 199 | ||
199 | return NULL; |
200 | return NULL; |
200 | } |
201 | } |
201 | 202 | ||
202 | 203 | ||
203 | /** Lookup OpenFirmware node by its path. |
204 | /** Lookup OpenFirmware node by its path. |
204 | * |
205 | * |
205 | * @param path Path to the node. |
206 | * @param path Path to the node. |
206 | * |
207 | * |
207 | * @return NULL if there is no such node or pointer to the leaf node. |
208 | * @return NULL if there is no such node or pointer to the leaf node. |
208 | */ |
209 | */ |
209 | ofw_tree_node_t *ofw_tree_lookup(const char *path) |
210 | ofw_tree_node_t *ofw_tree_lookup(const char *path) |
210 | { |
211 | { |
211 | char buf[NAME_BUF_LEN+1]; |
212 | char buf[NAME_BUF_LEN+1]; |
212 | ofw_tree_node_t *node = ofw_root; |
213 | ofw_tree_node_t *node = ofw_root; |
213 | index_t i, j; |
214 | index_t i, j; |
214 | 215 | ||
215 | if (path[0] != '/') |
216 | if (path[0] != '/') |
216 | return NULL; |
217 | return NULL; |
217 | 218 | ||
218 | for (i = 1; i < strlen(path) && node; i = j + 1) { |
219 | for (i = 1; i < strlen(path) && node; i = j + 1) { |
219 | for (j = i; j < strlen(path) && path[j] != '/'; j++) |
220 | for (j = i; j < strlen(path) && path[j] != '/'; j++) |
220 | ; |
221 | ; |
221 | if (i == j) /* skip extra slashes */ |
222 | if (i == j) /* skip extra slashes */ |
222 | continue; |
223 | continue; |
223 | 224 | ||
224 | memcpy(buf, &path[i], j - i); |
225 | memcpy(buf, &path[i], j - i); |
225 | buf[j - i] = '\0'; |
226 | buf[j - i] = '\0'; |
226 | node = ofw_tree_find_child(node, buf); |
227 | node = ofw_tree_find_child(node, buf); |
227 | } |
228 | } |
228 | 229 | ||
229 | return node; |
230 | return node; |
230 | } |
231 | } |
231 | 232 | ||
232 | /** Recursively print subtree rooted in a node. |
233 | /** Recursively print subtree rooted in a node. |
233 | * |
234 | * |
234 | * @param node Root of the subtree. |
235 | * @param node Root of the subtree. |
235 | * @param path Current path, NULL for the very root of the entire tree. |
236 | * @param path Current path, NULL for the very root of the entire tree. |
236 | */ |
237 | */ |
237 | static void ofw_tree_node_print(const ofw_tree_node_t *node, const char *path) |
238 | static void ofw_tree_node_print(const ofw_tree_node_t *node, const char *path) |
238 | { |
239 | { |
- | 240 | char *p; |
|
- | 241 | ||
239 | char p[PATH_MAX_LEN]; |
242 | p = (char *) malloc(PATH_MAX_LEN, 0); |
240 | 243 | ||
241 | if (node->parent) { |
244 | if (node->parent) { |
242 | snprintf(p, PATH_MAX_LEN, "%s/%s", path, node->da_name); |
245 | snprintf(p, PATH_MAX_LEN, "%s/%s", path, node->da_name); |
243 | printf("%s\n", p); |
246 | printf("%s\n", p); |
244 | } else { |
247 | } else { |
245 | snprintf(p, PATH_MAX_LEN, "%s", node->da_name); |
248 | snprintf(p, PATH_MAX_LEN, "%s", node->da_name); |
246 | printf("/\n"); |
249 | printf("/\n"); |
247 | } |
250 | } |
248 | 251 | ||
249 | if (node->child) |
252 | if (node->child) |
250 | ofw_tree_node_print(node->child, p); |
253 | ofw_tree_node_print(node->child, p); |
251 | 254 | ||
252 | if (node->peer) |
255 | if (node->peer) |
253 | ofw_tree_node_print(node->peer, path); |
256 | ofw_tree_node_print(node->peer, path); |
- | 257 | ||
- | 258 | free(p); |
|
254 | } |
259 | } |
255 | 260 | ||
256 | /** Print the structure of the OpenFirmware device tree. */ |
261 | /** Print the structure of the OpenFirmware device tree. */ |
257 | void ofw_tree_print(void) |
262 | void ofw_tree_print(void) |
258 | { |
263 | { |
259 | ofw_tree_node_print(ofw_root, NULL); |
264 | ofw_tree_node_print(ofw_root, NULL); |
260 | } |
265 | } |
261 | 266 | ||
262 | /** @} |
267 | /** @} |
263 | */ |
268 | */ |
264 | 269 |