Subversion Repositories HelenOS

Rev

Rev 3022 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3022 Rev 4055
Line 40... Line 40...
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),
-
 
46
        sizeof(ofw_tree_property_t));
46
}
47
}
47
 
48
 
48
static void * ofw_tree_space_alloc(size_t size)
49
static void *ofw_tree_space_alloc(size_t size)
49
{
50
{
50
    char *addr;
51
    char *addr;
51
 
52
 
52
    /*
53
    /*
53
     * What we do here is a nasty hack :-)
54
     * What we do here is a nasty hack :-)
Line 63... Line 64...
63
    if (addr)
64
    if (addr)
64
        addr[size] = '\0';
65
        addr[size] = '\0';
65
    return addr;
66
    return addr;
66
}
67
}
67
 
68
 
68
/** Transfer information from one OpenFirmware node into its memory representation.
69
/** Transfer information from one OpenFirmware node into its memory
-
 
70
 * representation.
69
 *
71
 *
70
 * Transfer entire information from the OpenFirmware device tree 'current' node to
72
 * Transfer entire information from the OpenFirmware device tree 'current' node
71
 * its memory representation in 'current_node'. This function recursively processes
73
 * to its memory representation in 'current_node'. This function recursively
72
 * all node's children. Node's peers are processed iteratively in order to prevent
74
 * processes all node's children. Node's peers are processed iteratively in
73
 * stack from overflowing.
75
 * order to prevent stack from overflowing.
74
 *
76
 *
75
 * @param current_node  Pointer to uninitialized ofw_tree_node structure that will
77
 * @param current_node  Pointer to uninitialized ofw_tree_node structure that
76
 *          become the memory represenation of 'current'.
78
 *          will become the memory represenation of 'current'.
77
 * @param parent_node   Parent ofw_tree_node structure or NULL in case of root node.
79
 * @param parent_node   Parent ofw_tree_node structure or NULL in case of root
-
 
80
 *          node.
78
 * @param current   OpenFirmware phandle to the current device tree node.
81
 * @param current   OpenFirmware phandle to the current device tree node.
79
 */
82
 */
80
static void ofw_tree_node_process(ofw_tree_node_t *current_node,
83
static void ofw_tree_node_process(ofw_tree_node_t *current_node,
81
    ofw_tree_node_t *parent_node, phandle current)
84
    ofw_tree_node_t *parent_node, phandle current)
82
{
85
{
83
    static char path[MAX_PATH_LEN+1];
86
    static char path[MAX_PATH_LEN + 1];
84
    static char name[OFW_TREE_PROPERTY_MAX_NAMELEN];
87
    static char name[OFW_TREE_PROPERTY_MAX_NAMELEN];
-
 
88
    static char name2[OFW_TREE_PROPERTY_MAX_NAMELEN];
85
    phandle peer;
89
    phandle peer;
86
    phandle child;
90
    phandle child;
87
    size_t len;
91
    size_t len;
88
    int i;
92
    int i;
89
 
93
 
Line 119... Line 123...
119
            return;
123
            return;
120
   
124
   
121
        memcpy(current_node->da_name, &path[i], len);
125
        memcpy(current_node->da_name, &path[i], len);
122
        current_node->da_name[len] = '\0';
126
        current_node->da_name[len] = '\0';
123
   
127
   
124
   
-
 
125
        /*
128
        /*
126
         * Recursively process the potential child node.
129
         * Recursively process the potential child node.
127
         */
130
         */
128
        child = ofw_get_child_node(current);
131
        child = ofw_get_child_node(current);
129
        if (child != 0 && child != -1) {
132
        if (child != 0 && child != -1) {
130
            ofw_tree_node_t *child_node;
133
            ofw_tree_node_t *child_node;
131
       
134
       
132
            child_node = ofw_tree_node_alloc();
135
            child_node = ofw_tree_node_alloc();
133
            if (child_node) {
136
            if (child_node) {
134
                ofw_tree_node_process(child_node, current_node, child);
137
                ofw_tree_node_process(child_node, current_node,
-
 
138
                    child);
135
                current_node->child = child_node;
139
                current_node->child = child_node;
136
            }
140
            }
137
        }
141
        }
138
   
142
   
139
        /*
143
        /*
140
         * Count properties.
144
         * Count properties.
141
         */
145
         */
142
        name[0] = '\0';
146
        name[0] = '\0';
143
        while (ofw_next_property(current, name, name) == 1)
147
        while (ofw_next_property(current, name, name2) == 1) {
144
            current_node->properties++;
148
            current_node->properties++;
-
 
149
            memcpy(name, name2, OFW_TREE_PROPERTY_MAX_NAMELEN);
-
 
150
        }
145
   
151
 
146
        if (!current_node->properties)
152
        if (!current_node->properties)
147
            return;
153
            return;
148
   
154
   
149
        /*
155
        /*
150
         * Copy properties.
156
         * Copy properties.
151
         */
157
         */
-
 
158
        current_node->property =
152
        current_node->property = ofw_tree_properties_alloc(current_node->properties);
159
            ofw_tree_properties_alloc(current_node->properties);
153
        if (!current_node->property)
160
        if (!current_node->property)
154
            return;
161
            return;
155
       
162
       
156
        name[0] = '\0';
163
        name[0] = '\0';
157
        for (i = 0; ofw_next_property(current, name, name) == 1; i++) {
164
        for (i = 0; ofw_next_property(current, name, name2) == 1; i++) {
158
            size_t size;
165
            size_t size;
159
       
166
       
160
            if (i == current_node->properties)
167
            if (i == current_node->properties)
161
                break;
168
                break;
162
       
169
       
-
 
170
            memcpy(name, name2, OFW_TREE_PROPERTY_MAX_NAMELEN);
163
            memcpy(current_node->property[i].name, name,
171
            memcpy(current_node->property[i].name, name,
164
                OFW_TREE_PROPERTY_MAX_NAMELEN);
172
                OFW_TREE_PROPERTY_MAX_NAMELEN);
-
 
173
            current_node->property[i].name[
165
            current_node->property[i].name[OFW_TREE_PROPERTY_MAX_NAMELEN] = '\0';
174
                OFW_TREE_PROPERTY_MAX_NAMELEN] = '\0';
166
 
175
 
167
            size = ofw_get_proplen(current, name);
176
            size = ofw_get_proplen(current, name);
168
            current_node->property[i].size = size;
177
            current_node->property[i].size = size;
169
            if (size) {
178
            if (size) {
170
                void *buf;
179
                void *buf;
Line 172... Line 181...
172
                buf = ofw_tree_space_alloc(size);
181
                buf = ofw_tree_space_alloc(size);
173
                if (current_node->property[i].value = buf) {
182
                if (current_node->property[i].value = buf) {
174
                    /*
183
                    /*
175
                     * Copy property value to memory node.
184
                     * Copy property value to memory node.
176
                     */
185
                     */
177
                    (void) ofw_get_property(current, name, buf, size);
186
                    (void) ofw_get_property(current, name,
-
 
187
                        buf, size);
178
                }
188
                }
179
            } else {
189
            } else {
180
                current_node->property[i].value = NULL;
190
                current_node->property[i].value = NULL;
181
            }
191
            }
182
        }
192
        }
183
 
193
 
184
        current_node->properties = i;   /* Just in case we ran out of memory. */
194
        /* Just in case we ran out of memory. */
-
 
195
        current_node->properties = i;
185
 
196
 
186
        /*
197
        /*
187
         * Iteratively process the next peer node.
198
         * Iteratively process the next peer node.
188
         * Note that recursion is a bad idea here.
199
         * Note that recursion is a bad idea here.
189
         * Due to the topology of the OpenFirmware device tree,
200
         * Due to the topology of the OpenFirmware device tree,
Line 212... Line 223...
212
    }
223
    }
213
}
224
}
214
 
225
 
215
/** Construct memory representation of OpenFirmware device tree.
226
/** Construct memory representation of OpenFirmware device tree.
216
 *
227
 *
217
 * @return NULL on failure or pointer to the root node.
228
 * @return      NULL on failure or pointer to the root node.
218
 */
229
 */
219
ofw_tree_node_t *ofw_tree_build(void)
230
ofw_tree_node_t *ofw_tree_build(void)
220
{
231
{
221
    ofw_tree_node_t *root;
232
    ofw_tree_node_t *root;
-
 
233
    phandle ssm_node;
-
 
234
    ofw_tree_node_t *ssm;
222
   
235
   
223
    root = ofw_tree_node_alloc();
236
    root = ofw_tree_node_alloc();
224
    if (root)
237
    if (root)
225
        ofw_tree_node_process(root, NULL, ofw_root);
238
        ofw_tree_node_process(root, NULL, ofw_root);
-
 
239
 
-
 
240
    /*
-
 
241
     * The firmware client interface does not automatically include the
-
 
242
     * "ssm" node in the list of children of "/". A nasty yet working
-
 
243
     * solution is to explicitly stick "ssm" to the OFW tree.
-
 
244
     */
-
 
245
    ssm_node = ofw_find_device("/ssm@0,0");
-
 
246
    if (ssm_node != -1) {
-
 
247
        ssm = ofw_tree_node_alloc();
-
 
248
        if (ssm) {
-
 
249
            ofw_tree_node_process(ssm, root,
-
 
250
                ofw_find_device("/ssm@0,0"));
-
 
251
            ssm->peer = root->child;
-
 
252
            root->child = ssm;
-
 
253
        }
-
 
254
    }
226
   
255
   
227
    return root;
256
    return root;
228
}
257
}