Rev 1894 | Rev 1896 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1893 | jermar | 1 | /* |
2 | * Copyright (C) 2006 Jakub Jermar |
||
3 | * All rights reserved. |
||
4 | * |
||
5 | * Redistribution and use in source and binary forms, with or without |
||
6 | * modification, are permitted provided that the following conditions |
||
7 | * are met: |
||
8 | * |
||
9 | * - Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * - Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
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 |
||
15 | * derived from this software without specific prior written permission. |
||
16 | * |
||
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 |
||
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
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 |
||
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 |
||
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
27 | */ |
||
28 | |||
29 | #include <ofw_tree.h> |
||
30 | #include <ofw.h> |
||
31 | #include <types.h> |
||
32 | #include <string.h> |
||
1894 | jermar | 33 | #include <balloc.h> |
1895 | jermar | 34 | #include <asm.h> |
1893 | jermar | 35 | |
1895 | jermar | 36 | #define MAX_PATH_LEN 256 |
37 | |||
1893 | jermar | 38 | static ofw_tree_node_t *ofw_tree_node_alloc(void) |
39 | { |
||
1894 | jermar | 40 | return balloc(sizeof(ofw_tree_node_t), sizeof(ofw_tree_node_t)); |
1893 | jermar | 41 | } |
42 | |||
43 | static ofw_tree_property_t *ofw_tree_properties_alloc(unsigned count) |
||
44 | { |
||
1894 | jermar | 45 | return balloc(count * sizeof(ofw_tree_property_t), sizeof(ofw_tree_property_t)); |
1893 | jermar | 46 | } |
47 | |||
48 | static void * ofw_tree_space_alloc(size_t size) |
||
49 | { |
||
1894 | jermar | 50 | return balloc(size, size); |
1893 | jermar | 51 | } |
52 | |||
53 | /** Transfer information from one OpenFirmware node into its memory representation. |
||
54 | * |
||
55 | * Transfer entire information from the OpenFirmware device tree 'current' node to |
||
56 | * its memory representation in 'current_node'. This function recursively processes |
||
57 | * all node's peers and children. |
||
58 | * |
||
59 | * @param current_node Pointer to uninitialized ofw_tree_node structure that will |
||
60 | * become the memory represenation of 'current'. |
||
61 | * @param parent_node Parent ofw_tree_node structure or NULL in case of root node. |
||
62 | * @param current OpenFirmware phandle to the current device tree node. |
||
63 | */ |
||
64 | static void ofw_tree_node_process(ofw_tree_node_t *current_node, |
||
65 | ofw_tree_node_t *parent_node, phandle current) |
||
66 | { |
||
1895 | jermar | 67 | static char path[MAX_PATH_LEN+1]; |
68 | static char name[OFW_TREE_PROPERTY_MAX_NAMELEN]; |
||
1893 | jermar | 69 | phandle peer; |
70 | phandle child; |
||
71 | unsigned properties = 0; |
||
1895 | jermar | 72 | size_t len; |
73 | int i; |
||
1893 | jermar | 74 | |
75 | /* |
||
76 | * Initialize node. |
||
77 | */ |
||
78 | current_node->parent = parent_node; |
||
79 | current_node->peer = NULL; |
||
80 | current_node->child = NULL; |
||
81 | current_node->properties = 0; |
||
82 | current_node->property = NULL; |
||
83 | |||
84 | /* |
||
1895 | jermar | 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 | /* |
||
1893 | jermar | 105 | * Recursively process the potential peer node. |
106 | */ |
||
107 | peer = ofw_get_peer_node(current); |
||
108 | if (peer != 0 && peer != -1) { |
||
109 | ofw_tree_node_t *peer_node; |
||
110 | |||
111 | peer_node = ofw_tree_node_alloc(); |
||
112 | if (peer_node) { |
||
1895 | jermar | 113 | ofw_tree_node_process(peer_node, parent_node, peer); |
1893 | jermar | 114 | current_node->peer = peer_node; |
115 | } |
||
116 | } |
||
117 | |||
118 | /* |
||
119 | * Recursively process the potential child node. |
||
120 | */ |
||
121 | child = ofw_get_child_node(current); |
||
122 | if (child != 0 && child != -1) { |
||
123 | ofw_tree_node_t *child_node; |
||
124 | |||
125 | child_node = ofw_tree_node_alloc(); |
||
126 | if (child_node) { |
||
127 | ofw_tree_node_process(child_node, current_node, child); |
||
128 | current_node->child = child_node; |
||
129 | } |
||
130 | } |
||
131 | |||
132 | /* |
||
133 | * Count properties. |
||
134 | */ |
||
135 | name[0] = '\0'; |
||
136 | while (ofw_next_property(current, name, name) == 1) |
||
137 | current_node->properties++; |
||
138 | |||
139 | if (!current_node->properties) |
||
140 | return; |
||
141 | |||
142 | /* |
||
143 | * Copy properties. |
||
144 | */ |
||
145 | current_node->property = ofw_tree_properties_alloc(current_node->properties); |
||
146 | if (!current_node->property) |
||
147 | return; |
||
148 | |||
149 | name[0] = '\0'; |
||
150 | for (i = 0; ofw_next_property(current, name, name) == 1; i++) { |
||
151 | size_t size; |
||
152 | |||
153 | if (i == current_node->properties) |
||
154 | break; |
||
155 | |||
156 | strncpy(current_node->property[i].name, name, sizeof(name)); |
||
157 | |||
158 | size = ofw_get_proplen(current, name); |
||
159 | current_node->property[i].size = size; |
||
160 | if (size) { |
||
161 | void *buf; |
||
162 | |||
163 | buf = ofw_tree_space_alloc(size); |
||
164 | if (current_node->property[i].value = buf) { |
||
165 | /* |
||
166 | * Copy property value to memory node. |
||
167 | */ |
||
168 | (void) ofw_get_property(current, name, buf, size); |
||
169 | } |
||
170 | } else { |
||
171 | current_node->property[i].value = NULL; |
||
172 | } |
||
173 | } |
||
174 | |||
175 | current_node->properties = i; /* Just in case we ran out of memory. */ |
||
176 | } |
||
177 | |||
178 | /** Construct memory representation of OpenFirmware device tree. |
||
179 | * |
||
180 | * @return NULL on failure or pointer to the root node. |
||
181 | */ |
||
182 | ofw_tree_node_t *ofw_tree_build(void) |
||
183 | { |
||
184 | ofw_tree_node_t *root; |
||
185 | |||
186 | root = ofw_tree_node_alloc(); |
||
187 | if (root) |
||
188 | ofw_tree_node_process(root, NULL, ofw_root); |
||
189 | |||
190 | return root; |
||
191 | } |