Rev 1894 | Rev 1896 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1894 | Rev 1895 | ||
---|---|---|---|
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> |
|
- | 35 | ||
- | 36 | #define MAX_PATH_LEN 256 |
|
34 | 37 | ||
35 | static ofw_tree_node_t *ofw_tree_node_alloc(void) |
38 | static ofw_tree_node_t *ofw_tree_node_alloc(void) |
36 | { |
39 | { |
37 | 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)); |
38 | } |
41 | } |
39 | 42 | ||
40 | static ofw_tree_property_t *ofw_tree_properties_alloc(unsigned count) |
43 | static ofw_tree_property_t *ofw_tree_properties_alloc(unsigned count) |
41 | { |
44 | { |
42 | 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)); |
43 | } |
46 | } |
44 | 47 | ||
45 | static void * ofw_tree_space_alloc(size_t size) |
48 | static void * ofw_tree_space_alloc(size_t size) |
46 | { |
49 | { |
47 | return balloc(size, size); |
50 | return balloc(size, size); |
48 | } |
51 | } |
49 | 52 | ||
50 | /** Transfer information from one OpenFirmware node into its memory representation. |
53 | /** Transfer information from one OpenFirmware node into its memory representation. |
51 | * |
54 | * |
52 | * Transfer entire information from the OpenFirmware device tree 'current' node to |
55 | * Transfer entire information from the OpenFirmware device tree 'current' node to |
53 | * its memory representation in 'current_node'. This function recursively processes |
56 | * its memory representation in 'current_node'. This function recursively processes |
54 | * all node's peers and children. |
57 | * all node's peers and children. |
55 | * |
58 | * |
56 | * @param current_node Pointer to uninitialized ofw_tree_node structure that will |
59 | * @param current_node Pointer to uninitialized ofw_tree_node structure that will |
57 | * become the memory represenation of 'current'. |
60 | * become the memory represenation of 'current'. |
58 | * @param parent_node Parent ofw_tree_node structure or NULL in case of root node. |
61 | * @param parent_node Parent ofw_tree_node structure or NULL in case of root node. |
59 | * @param current OpenFirmware phandle to the current device tree node. |
62 | * @param current OpenFirmware phandle to the current device tree node. |
60 | */ |
63 | */ |
61 | static void ofw_tree_node_process(ofw_tree_node_t *current_node, |
64 | static void ofw_tree_node_process(ofw_tree_node_t *current_node, |
62 | ofw_tree_node_t *parent_node, phandle current) |
65 | ofw_tree_node_t *parent_node, phandle current) |
63 | { |
66 | { |
- | 67 | static char path[MAX_PATH_LEN+1]; |
|
- | 68 | static char name[OFW_TREE_PROPERTY_MAX_NAMELEN]; |
|
64 | phandle peer; |
69 | phandle peer; |
65 | phandle child; |
70 | phandle child; |
66 | unsigned properties = 0; |
71 | unsigned properties = 0; |
67 | char name[OFW_TREE_PROPERTY_MAX_NAMELEN]; |
72 | size_t len; |
- | 73 | int i; |
|
68 | 74 | ||
69 | /* |
75 | /* |
70 | * Initialize node. |
76 | * Initialize node. |
71 | */ |
77 | */ |
72 | current_node->parent = parent_node; |
78 | current_node->parent = parent_node; |
73 | current_node->peer = NULL; |
79 | current_node->peer = NULL; |
74 | current_node->child = NULL; |
80 | current_node->child = NULL; |
75 | current_node->properties = 0; |
81 | current_node->properties = 0; |
76 | current_node->property = NULL; |
82 | current_node->property = NULL; |
77 | 83 | ||
78 | /* |
84 | /* |
- | 85 | * Get the disambigued name. |
|
- | 86 | */ |
|
- | 87 | len = ofw_package_to_path(current, path, MAX_PATH_LEN); |
|
- | 88 | if (len == -1) |
|
- | 89 | return; |
|
- | 90 | ||
- | 91 | path[len] = '\0'; |
|
- | 92 | for (i = len - 1; i >= 0 && path[i] != '/'; i--) |
|
- | 93 | ; |
|
- | 94 | i++; /* do not include '/' */ |
|
- | 95 | ||
- | 96 | len -= i; |
|
- | 97 | current_node->da_name = ofw_tree_space_alloc(len + 1); /* add space for trailing '\0' */ |
|
- | 98 | if (!current_node->da_name) |
|
- | 99 | return; |
|
- | 100 | ||
- | 101 | memcpy(current_node->da_name, &path[i], len); |
|
- | 102 | current_node->da_name[len] = '\0'; |
|
- | 103 | ||
- | 104 | /* |
|
79 | * Recursively process the potential peer node. |
105 | * Recursively process the potential peer node. |
80 | */ |
106 | */ |
81 | peer = ofw_get_peer_node(current); |
107 | peer = ofw_get_peer_node(current); |
82 | if (peer != 0 && peer != -1) { |
108 | if (peer != 0 && peer != -1) { |
83 | ofw_tree_node_t *peer_node; |
109 | ofw_tree_node_t *peer_node; |
84 | 110 | ||
85 | peer_node = ofw_tree_node_alloc(); |
111 | peer_node = ofw_tree_node_alloc(); |
86 | if (peer_node) { |
112 | if (peer_node) { |
87 | ofw_tree_node_process(peer_node, current_node, peer); |
113 | ofw_tree_node_process(peer_node, parent_node, peer); |
88 | current_node->peer = peer_node; |
114 | current_node->peer = peer_node; |
89 | } |
115 | } |
90 | } |
116 | } |
91 | 117 | ||
92 | /* |
118 | /* |
93 | * Recursively process the potential child node. |
119 | * Recursively process the potential child node. |
94 | */ |
120 | */ |
95 | child = ofw_get_child_node(current); |
121 | child = ofw_get_child_node(current); |
96 | if (child != 0 && child != -1) { |
122 | if (child != 0 && child != -1) { |
97 | ofw_tree_node_t *child_node; |
123 | ofw_tree_node_t *child_node; |
98 | 124 | ||
99 | child_node = ofw_tree_node_alloc(); |
125 | child_node = ofw_tree_node_alloc(); |
100 | if (child_node) { |
126 | if (child_node) { |
101 | ofw_tree_node_process(child_node, current_node, child); |
127 | ofw_tree_node_process(child_node, current_node, child); |
102 | current_node->child = child_node; |
128 | current_node->child = child_node; |
103 | } |
129 | } |
104 | } |
130 | } |
105 | 131 | ||
106 | /* |
132 | /* |
107 | * Count properties. |
133 | * Count properties. |
108 | */ |
134 | */ |
109 | name[0] = '\0'; |
135 | name[0] = '\0'; |
110 | while (ofw_next_property(current, name, name) == 1) |
136 | while (ofw_next_property(current, name, name) == 1) |
111 | current_node->properties++; |
137 | current_node->properties++; |
112 | 138 | ||
113 | if (!current_node->properties) |
139 | if (!current_node->properties) |
114 | return; |
140 | return; |
115 | 141 | ||
116 | /* |
142 | /* |
117 | * Copy properties. |
143 | * Copy properties. |
118 | */ |
144 | */ |
119 | current_node->property = ofw_tree_properties_alloc(current_node->properties); |
145 | current_node->property = ofw_tree_properties_alloc(current_node->properties); |
120 | if (!current_node->property) |
146 | if (!current_node->property) |
121 | return; |
147 | return; |
122 | 148 | ||
123 | int i = 0; |
- | |
124 | - | ||
125 | name[0] = '\0'; |
149 | name[0] = '\0'; |
126 | for (i = 0; ofw_next_property(current, name, name) == 1; i++) { |
150 | for (i = 0; ofw_next_property(current, name, name) == 1; i++) { |
127 | size_t size; |
151 | size_t size; |
128 | 152 | ||
129 | if (i == current_node->properties) |
153 | if (i == current_node->properties) |
130 | break; |
154 | break; |
131 | 155 | ||
132 | strncpy(current_node->property[i].name, name, sizeof(name)); |
156 | strncpy(current_node->property[i].name, name, sizeof(name)); |
133 | 157 | ||
134 | size = ofw_get_proplen(current, name); |
158 | size = ofw_get_proplen(current, name); |
135 | current_node->property[i].size = size; |
159 | current_node->property[i].size = size; |
136 | if (size) { |
160 | if (size) { |
137 | void *buf; |
161 | void *buf; |
138 | 162 | ||
139 | buf = ofw_tree_space_alloc(size); |
163 | buf = ofw_tree_space_alloc(size); |
140 | if (current_node->property[i].value = buf) { |
164 | if (current_node->property[i].value = buf) { |
141 | /* |
165 | /* |
142 | * Copy property value to memory node. |
166 | * Copy property value to memory node. |
143 | */ |
167 | */ |
144 | (void) ofw_get_property(current, name, buf, size); |
168 | (void) ofw_get_property(current, name, buf, size); |
145 | } |
169 | } |
146 | } else { |
170 | } else { |
147 | current_node->property[i].value = NULL; |
171 | current_node->property[i].value = NULL; |
148 | } |
172 | } |
149 | } |
173 | } |
150 | 174 | ||
151 | current_node->properties = i; /* Just in case we ran out of memory. */ |
175 | current_node->properties = i; /* Just in case we ran out of memory. */ |
152 | } |
176 | } |
153 | 177 | ||
154 | /** Construct memory representation of OpenFirmware device tree. |
178 | /** Construct memory representation of OpenFirmware device tree. |
155 | * |
179 | * |
156 | * @return NULL on failure or pointer to the root node. |
180 | * @return NULL on failure or pointer to the root node. |
157 | */ |
181 | */ |
158 | ofw_tree_node_t *ofw_tree_build(void) |
182 | ofw_tree_node_t *ofw_tree_build(void) |
159 | { |
183 | { |
160 | ofw_tree_node_t *root; |
184 | ofw_tree_node_t *root; |
161 | 185 | ||
162 | root = ofw_tree_node_alloc(); |
186 | root = ofw_tree_node_alloc(); |
163 | if (root) |
187 | if (root) |
164 | ofw_tree_node_process(root, NULL, ofw_root); |
188 | ofw_tree_node_process(root, NULL, ofw_root); |
165 | 189 | ||
166 | return root; |
190 | return root; |
167 | } |
191 | } |
168 | 192 |