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