Rev 3672 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3672 | Rev 3796 | ||
---|---|---|---|
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 | #include <ofw_tree.h> |
29 | #include <ofw_tree.h> |
30 | #include <ofw.h> |
30 | #include <ofw.h> |
31 | #include <types.h> |
31 | #include <types.h> |
32 | #include <string.h> |
32 | #include <string.h> |
33 | #include <balloc.h> |
33 | #include <balloc.h> |
34 | #include <asm.h> |
34 | #include <asm.h> |
35 | 35 | ||
36 | #define MAX_PATH_LEN 256 |
36 | #define MAX_PATH_LEN 256 |
37 | 37 | ||
38 | static ofw_tree_node_t *ofw_tree_node_alloc(void) |
38 | static ofw_tree_node_t *ofw_tree_node_alloc(void) |
39 | { |
39 | { |
40 | return balloc(sizeof(ofw_tree_node_t), sizeof(ofw_tree_node_t)); |
40 | return balloc(sizeof(ofw_tree_node_t), sizeof(ofw_tree_node_t)); |
41 | } |
41 | } |
42 | 42 | ||
43 | static ofw_tree_property_t *ofw_tree_properties_alloc(unsigned count) |
43 | static ofw_tree_property_t *ofw_tree_properties_alloc(unsigned count) |
44 | { |
44 | { |
45 | return balloc(count * sizeof(ofw_tree_property_t), sizeof(ofw_tree_property_t)); |
45 | return balloc(count * sizeof(ofw_tree_property_t), sizeof(ofw_tree_property_t)); |
46 | } |
46 | } |
47 | 47 | ||
48 | static void * ofw_tree_space_alloc(size_t size) |
48 | static void * ofw_tree_space_alloc(size_t size) |
49 | { |
49 | { |
50 | char *addr; |
50 | char *addr; |
51 | 51 | ||
52 | /* |
52 | /* |
53 | * What we do here is a nasty hack :-) |
53 | * What we do here is a nasty hack :-) |
54 | * Problem: string property values that are allocated via this |
54 | * Problem: string property values that are allocated via this |
55 | * function typically do not contain the trailing '\0'. This |
55 | * function typically do not contain the trailing '\0'. This |
56 | * is very uncomfortable for kernel, which is supposed to deal |
56 | * is very uncomfortable for kernel, which is supposed to deal |
57 | * with the properties. |
57 | * with the properties. |
58 | * Solution: when allocating space via this function, we always |
58 | * Solution: when allocating space via this function, we always |
59 | * allocate space for the extra '\0' character that we store |
59 | * allocate space for the extra '\0' character that we store |
60 | * behind the requested memory. |
60 | * behind the requested memory. |
61 | */ |
61 | */ |
62 | addr = balloc(size + 1, size); |
62 | addr = balloc(size + 1, size); |
63 | if (addr) |
63 | if (addr) |
64 | addr[size] = '\0'; |
64 | addr[size] = '\0'; |
65 | return addr; |
65 | return addr; |
66 | } |
66 | } |
67 | 67 | ||
68 | /** Transfer information from one OpenFirmware node into its memory representation. |
68 | /** Transfer information from one OpenFirmware node into its memory representation. |
69 | * |
69 | * |
70 | * Transfer entire information from the OpenFirmware device tree 'current' node to |
70 | * Transfer entire information from the OpenFirmware device tree 'current' node to |
71 | * its memory representation in 'current_node'. This function recursively processes |
71 | * its memory representation in 'current_node'. This function recursively processes |
72 | * all node's children. Node's peers are processed iteratively in order to prevent |
72 | * all node's children. Node's peers are processed iteratively in order to prevent |
73 | * stack from overflowing. |
73 | * stack from overflowing. |
74 | * |
74 | * |
75 | * @param current_node Pointer to uninitialized ofw_tree_node structure that will |
75 | * @param current_node Pointer to uninitialized ofw_tree_node structure that will |
76 | * become the memory represenation of 'current'. |
76 | * become the memory represenation of 'current'. |
77 | * @param parent_node Parent ofw_tree_node structure or NULL in case of root node. |
77 | * @param parent_node Parent ofw_tree_node structure or NULL in case of root node. |
78 | * @param current OpenFirmware phandle to the current device tree node. |
78 | * @param current OpenFirmware phandle to the current device tree node. |
79 | */ |
79 | */ |
80 | static void ofw_tree_node_process(ofw_tree_node_t *current_node, |
80 | static void ofw_tree_node_process(ofw_tree_node_t *current_node, |
81 | ofw_tree_node_t *parent_node, phandle current) |
81 | ofw_tree_node_t *parent_node, phandle current) |
82 | { |
82 | { |
83 | static char path[MAX_PATH_LEN+1]; |
83 | static char path[MAX_PATH_LEN+1]; |
84 | static char name[OFW_TREE_PROPERTY_MAX_NAMELEN]; |
84 | static char name[OFW_TREE_PROPERTY_MAX_NAMELEN]; |
- | 85 | static char name2[OFW_TREE_PROPERTY_MAX_NAMELEN]; |
|
85 | phandle peer; |
86 | phandle peer; |
86 | phandle child; |
87 | phandle child; |
87 | size_t len; |
88 | size_t len; |
88 | int i; |
89 | int i; |
89 | 90 | ||
90 | while (current_node) { |
91 | while (current_node) { |
91 | /* |
92 | /* |
92 | * Initialize node. |
93 | * Initialize node. |
93 | */ |
94 | */ |
94 | current_node->parent = parent_node; |
95 | current_node->parent = parent_node; |
95 | current_node->peer = NULL; |
96 | current_node->peer = NULL; |
96 | current_node->child = NULL; |
97 | current_node->child = NULL; |
97 | current_node->node_handle = current; |
98 | current_node->node_handle = current; |
98 | current_node->properties = 0; |
99 | current_node->properties = 0; |
99 | current_node->property = NULL; |
100 | current_node->property = NULL; |
100 | current_node->device = NULL; |
101 | current_node->device = NULL; |
101 | 102 | ||
102 | /* |
103 | /* |
103 | * Get the disambigued name. |
104 | * Get the disambigued name. |
104 | */ |
105 | */ |
105 | len = ofw_package_to_path(current, path, MAX_PATH_LEN); |
106 | len = ofw_package_to_path(current, path, MAX_PATH_LEN); |
106 | if (len == -1) |
107 | if (len == -1) |
107 | return; |
108 | return; |
108 | 109 | ||
109 | path[len] = '\0'; |
110 | path[len] = '\0'; |
110 | for (i = len - 1; i >= 0 && path[i] != '/'; i--) |
111 | for (i = len - 1; i >= 0 && path[i] != '/'; i--) |
111 | ; |
112 | ; |
112 | i++; /* do not include '/' */ |
113 | i++; /* do not include '/' */ |
113 | 114 | ||
114 | len -= i; |
115 | len -= i; |
115 | 116 | ||
116 | /* add space for trailing '\0' */ |
117 | /* add space for trailing '\0' */ |
117 | current_node->da_name = ofw_tree_space_alloc(len + 1); |
118 | current_node->da_name = ofw_tree_space_alloc(len + 1); |
118 | if (!current_node->da_name) |
119 | if (!current_node->da_name) |
119 | return; |
120 | return; |
120 | 121 | ||
121 | memcpy(current_node->da_name, &path[i], len); |
122 | memcpy(current_node->da_name, &path[i], len); |
122 | current_node->da_name[len] = '\0'; |
123 | current_node->da_name[len] = '\0'; |
123 | 124 | ||
124 | /* |
125 | /* |
125 | * Recursively process the potential child node. |
126 | * Recursively process the potential child node. |
126 | */ |
127 | */ |
127 | child = ofw_get_child_node(current); |
128 | child = ofw_get_child_node(current); |
128 | if (child != 0 && child != -1) { |
129 | if (child != 0 && child != -1) { |
129 | ofw_tree_node_t *child_node; |
130 | ofw_tree_node_t *child_node; |
130 | 131 | ||
131 | child_node = ofw_tree_node_alloc(); |
132 | child_node = ofw_tree_node_alloc(); |
132 | if (child_node) { |
133 | if (child_node) { |
133 | ofw_tree_node_process(child_node, current_node, child); |
134 | ofw_tree_node_process(child_node, current_node, child); |
134 | current_node->child = child_node; |
135 | current_node->child = child_node; |
135 | } |
136 | } |
136 | } |
137 | } |
137 | 138 | ||
138 | /* |
139 | /* |
139 | * Count properties. |
140 | * Count properties. |
140 | */ |
141 | */ |
141 | name[0] = '\0'; |
142 | name[0] = '\0'; |
142 | while (ofw_next_property(current, name, name) == 1) |
143 | while (ofw_next_property(current, name, name2) == 1) { |
143 | current_node->properties++; |
144 | current_node->properties++; |
- | 145 | memcpy(name, name2, OFW_TREE_PROPERTY_MAX_NAMELEN); |
|
- | 146 | } |
|
144 | 147 | ||
145 | if (!current_node->properties) |
148 | if (!current_node->properties) |
146 | return; |
149 | return; |
147 | 150 | ||
148 | /* |
151 | /* |
149 | * Copy properties. |
152 | * Copy properties. |
150 | */ |
153 | */ |
151 | current_node->property = ofw_tree_properties_alloc(current_node->properties); |
154 | current_node->property = ofw_tree_properties_alloc(current_node->properties); |
152 | if (!current_node->property) |
155 | if (!current_node->property) |
153 | return; |
156 | return; |
154 | 157 | ||
155 | name[0] = '\0'; |
158 | name[0] = '\0'; |
156 | for (i = 0; ofw_next_property(current, name, name) == 1; i++) { |
159 | for (i = 0; ofw_next_property(current, name, name2) == 1; i++) { |
157 | size_t size; |
160 | size_t size; |
158 | 161 | ||
159 | if (i == current_node->properties) |
162 | if (i == current_node->properties) |
160 | break; |
163 | break; |
161 | 164 | ||
- | 165 | memcpy(name, name2, OFW_TREE_PROPERTY_MAX_NAMELEN); |
|
162 | memcpy(current_node->property[i].name, name, |
166 | memcpy(current_node->property[i].name, name, |
163 | OFW_TREE_PROPERTY_MAX_NAMELEN); |
167 | OFW_TREE_PROPERTY_MAX_NAMELEN); |
164 | current_node->property[i].name[OFW_TREE_PROPERTY_MAX_NAMELEN] = '\0'; |
168 | current_node->property[i].name[OFW_TREE_PROPERTY_MAX_NAMELEN] = '\0'; |
165 | 169 | ||
166 | size = ofw_get_proplen(current, name); |
170 | size = ofw_get_proplen(current, name); |
167 | current_node->property[i].size = size; |
171 | current_node->property[i].size = size; |
168 | if (size) { |
172 | if (size) { |
169 | void *buf; |
173 | void *buf; |
170 | 174 | ||
171 | buf = ofw_tree_space_alloc(size); |
175 | buf = ofw_tree_space_alloc(size); |
172 | if (current_node->property[i].value = buf) { |
176 | if (current_node->property[i].value = buf) { |
173 | /* |
177 | /* |
174 | * Copy property value to memory node. |
178 | * Copy property value to memory node. |
175 | */ |
179 | */ |
176 | (void) ofw_get_property(current, name, buf, size); |
180 | (void) ofw_get_property(current, name, buf, size); |
177 | } |
181 | } |
178 | } else { |
182 | } else { |
179 | current_node->property[i].value = NULL; |
183 | current_node->property[i].value = NULL; |
180 | } |
184 | } |
181 | } |
185 | } |
182 | 186 | ||
183 | current_node->properties = i; /* Just in case we ran out of memory. */ |
187 | current_node->properties = i; /* Just in case we ran out of memory. */ |
184 | 188 | ||
185 | /* |
189 | /* |
186 | * Iteratively process the next peer node. |
190 | * Iteratively process the next peer node. |
187 | * Note that recursion is a bad idea here. |
191 | * Note that recursion is a bad idea here. |
188 | * Due to the topology of the OpenFirmware device tree, |
192 | * Due to the topology of the OpenFirmware device tree, |
189 | * the nesting of peer nodes could be to wide and the |
193 | * the nesting of peer nodes could be to wide and the |
190 | * risk of overflowing the stack is too real. |
194 | * risk of overflowing the stack is too real. |
191 | */ |
195 | */ |
192 | peer = ofw_get_peer_node(current); |
196 | peer = ofw_get_peer_node(current); |
193 | if (peer != 0 && peer != -1) { |
197 | if (peer != 0 && peer != -1) { |
194 | ofw_tree_node_t *peer_node; |
198 | ofw_tree_node_t *peer_node; |
195 | 199 | ||
196 | peer_node = ofw_tree_node_alloc(); |
200 | peer_node = ofw_tree_node_alloc(); |
197 | if (peer_node) { |
201 | if (peer_node) { |
198 | current_node->peer = peer_node; |
202 | current_node->peer = peer_node; |
199 | current_node = peer_node; |
203 | current_node = peer_node; |
200 | current = peer; |
204 | current = peer; |
201 | /* |
205 | /* |
202 | * Process the peer in next iteration. |
206 | * Process the peer in next iteration. |
203 | */ |
207 | */ |
204 | continue; |
208 | continue; |
205 | } |
209 | } |
206 | } |
210 | } |
207 | /* |
211 | /* |
208 | * No more peers on this level. |
212 | * No more peers on this level. |
209 | */ |
213 | */ |
210 | break; |
214 | break; |
211 | } |
215 | } |
212 | } |
216 | } |
213 | 217 | ||
214 | /** Construct memory representation of OpenFirmware device tree. |
218 | /** Construct memory representation of OpenFirmware device tree. |
215 | * |
219 | * |
216 | * @return NULL on failure or pointer to the root node. |
220 | * @return NULL on failure or pointer to the root node. |
217 | */ |
221 | */ |
218 | ofw_tree_node_t *ofw_tree_build(void) |
222 | ofw_tree_node_t *ofw_tree_build(void) |
219 | { |
223 | { |
220 | ofw_tree_node_t *root; |
224 | ofw_tree_node_t *root; |
221 | phandle ssm_node; |
225 | phandle ssm_node; |
222 | ofw_tree_node_t *ssm; |
226 | ofw_tree_node_t *ssm; |
223 | 227 | ||
224 | root = ofw_tree_node_alloc(); |
228 | root = ofw_tree_node_alloc(); |
225 | if (root) |
229 | if (root) |
226 | ofw_tree_node_process(root, NULL, ofw_root); |
230 | ofw_tree_node_process(root, NULL, ofw_root); |
227 | 231 | ||
228 | /* |
232 | /* |
229 | * The firmware client interface does not automatically include the |
233 | * The firmware client interface does not automatically include the |
230 | * "ssm" node in the list of children of "/". A nasty yet working |
234 | * "ssm" node in the list of children of "/". A nasty yet working |
231 | * solution is to explicitly stick "ssm" to the OFW tree. |
235 | * solution is to explicitly stick "ssm" to the OFW tree. |
232 | */ |
236 | */ |
233 | ssm_node = ofw_find_device("/ssm@0,0"); |
237 | ssm_node = ofw_find_device("/ssm@0,0"); |
234 | if (ssm_node != -1) { |
238 | if (ssm_node != -1) { |
235 | ssm = ofw_tree_node_alloc(); |
239 | ssm = ofw_tree_node_alloc(); |
236 | if (ssm) { |
240 | if (ssm) { |
237 | ofw_tree_node_process( |
241 | ofw_tree_node_process( |
238 | ssm, root, ofw_find_device("/ssm@0,0")); |
242 | ssm, root, ofw_find_device("/ssm@0,0")); |
239 | ssm->peer = root->child; |
243 | ssm->peer = root->child; |
240 | root->child = ssm; |
244 | root->child = ssm; |
241 | } |
245 | } |
242 | } |
246 | } |
243 | 247 | ||
244 | return root; |
248 | return root; |
245 | } |
249 | } |
246 | 250 |