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