Subversion Repositories HelenOS

Rev

Rev 3796 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3796 Rev 3797
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),
-
 
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 :-)
54
     * Problem: string property values that are allocated via this
55
     * Problem: string property values that are allocated via this
55
     * function typically do not contain the trailing '\0'. This
56
     * function typically do not contain the trailing '\0'. This
56
     * is very uncomfortable for kernel, which is supposed to deal
57
     * is very uncomfortable for kernel, which is supposed to deal
57
     * with the properties.
58
     * with the properties.
58
     * Solution: when allocating space via this function, we always
59
     * Solution: when allocating space via this function, we always
59
     * allocate space for the extra '\0' character that we store
60
     * allocate space for the extra '\0' character that we store
60
     * behind the requested memory.
61
     * behind the requested memory.
61
     */
62
     */
62
    addr = balloc(size + 1, size);
63
    addr = balloc(size + 1, size);
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];
85
    static char name2[OFW_TREE_PROPERTY_MAX_NAMELEN];
88
    static char name2[OFW_TREE_PROPERTY_MAX_NAMELEN];
86
    phandle peer;
89
    phandle peer;
87
    phandle child;
90
    phandle child;
88
    size_t len;
91
    size_t len;
89
    int i;
92
    int i;
90
 
93
 
91
    while (current_node) {
94
    while (current_node) {
92
        /*
95
        /*
93
         * Initialize node.
96
         * Initialize node.
94
         */
97
         */
95
        current_node->parent = parent_node;
98
        current_node->parent = parent_node;
96
        current_node->peer = NULL;
99
        current_node->peer = NULL;
97
        current_node->child = NULL;
100
        current_node->child = NULL;
98
        current_node->node_handle = current;
101
        current_node->node_handle = current;
99
        current_node->properties = 0;
102
        current_node->properties = 0;
100
        current_node->property = NULL;
103
        current_node->property = NULL;
101
        current_node->device = NULL;
104
        current_node->device = NULL;
102
   
105
   
103
        /*
106
        /*
104
         * Get the disambigued name.
107
         * Get the disambigued name.
105
         */
108
         */
106
        len = ofw_package_to_path(current, path, MAX_PATH_LEN);
109
        len = ofw_package_to_path(current, path, MAX_PATH_LEN);
107
        if (len == -1)
110
        if (len == -1)
108
            return;
111
            return;
109
   
112
   
110
        path[len] = '\0';
113
        path[len] = '\0';
111
        for (i = len - 1; i >= 0 && path[i] != '/'; i--)
114
        for (i = len - 1; i >= 0 && path[i] != '/'; i--)
112
            ;
115
            ;
113
        i++;    /* do not include '/' */
116
        i++;    /* do not include '/' */
114
   
117
   
115
        len -= i;
118
        len -= i;
116
 
119
 
117
        /* add space for trailing '\0' */
120
        /* add space for trailing '\0' */
118
        current_node->da_name = ofw_tree_space_alloc(len + 1);
121
        current_node->da_name = ofw_tree_space_alloc(len + 1);
119
        if (!current_node->da_name)
122
        if (!current_node->da_name)
120
            return;
123
            return;
121
   
124
   
122
        memcpy(current_node->da_name, &path[i], len);
125
        memcpy(current_node->da_name, &path[i], len);
123
        current_node->da_name[len] = '\0';
126
        current_node->da_name[len] = '\0';
124
   
127
   
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, name2) == 1) {
147
        while (ofw_next_property(current, name, name2) == 1) {
144
            current_node->properties++;
148
            current_node->properties++;
145
            memcpy(name, name2, OFW_TREE_PROPERTY_MAX_NAMELEN);
149
            memcpy(name, name2, OFW_TREE_PROPERTY_MAX_NAMELEN);
146
        }
150
        }
147
 
151
 
148
        if (!current_node->properties)
152
        if (!current_node->properties)
149
            return;
153
            return;
150
   
154
   
151
        /*
155
        /*
152
         * Copy properties.
156
         * Copy properties.
153
         */
157
         */
-
 
158
        current_node->property =
154
        current_node->property = ofw_tree_properties_alloc(current_node->properties);
159
            ofw_tree_properties_alloc(current_node->properties);
155
        if (!current_node->property)
160
        if (!current_node->property)
156
            return;
161
            return;
157
       
162
       
158
        name[0] = '\0';
163
        name[0] = '\0';
159
        for (i = 0; ofw_next_property(current, name, name2) == 1; i++) {
164
        for (i = 0; ofw_next_property(current, name, name2) == 1; i++) {
160
            size_t size;
165
            size_t size;
161
       
166
       
162
            if (i == current_node->properties)
167
            if (i == current_node->properties)
163
                break;
168
                break;
164
       
169
       
165
            memcpy(name, name2, OFW_TREE_PROPERTY_MAX_NAMELEN);
170
            memcpy(name, name2, OFW_TREE_PROPERTY_MAX_NAMELEN);
166
            memcpy(current_node->property[i].name, name,
171
            memcpy(current_node->property[i].name, name,
167
                OFW_TREE_PROPERTY_MAX_NAMELEN);
172
                OFW_TREE_PROPERTY_MAX_NAMELEN);
-
 
173
            current_node->property[i].name[
168
            current_node->property[i].name[OFW_TREE_PROPERTY_MAX_NAMELEN] = '\0';
174
                OFW_TREE_PROPERTY_MAX_NAMELEN] = '\0';
169
 
175
 
170
            size = ofw_get_proplen(current, name);
176
            size = ofw_get_proplen(current, name);
171
            current_node->property[i].size = size;
177
            current_node->property[i].size = size;
172
            if (size) {
178
            if (size) {
173
                void *buf;
179
                void *buf;
174
           
180
           
175
                buf = ofw_tree_space_alloc(size);
181
                buf = ofw_tree_space_alloc(size);
176
                if (current_node->property[i].value = buf) {
182
                if (current_node->property[i].value = buf) {
177
                    /*
183
                    /*
178
                     * Copy property value to memory node.
184
                     * Copy property value to memory node.
179
                     */
185
                     */
180
                    (void) ofw_get_property(current, name, buf, size);
186
                    (void) ofw_get_property(current, name,
-
 
187
                        buf, size);
181
                }
188
                }
182
            } else {
189
            } else {
183
                current_node->property[i].value = NULL;
190
                current_node->property[i].value = NULL;
184
            }
191
            }
185
        }
192
        }
186
 
193
 
187
        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;
188
 
196
 
189
        /*
197
        /*
190
         * Iteratively process the next peer node.
198
         * Iteratively process the next peer node.
191
         * Note that recursion is a bad idea here.
199
         * Note that recursion is a bad idea here.
192
         * Due to the topology of the OpenFirmware device tree,
200
         * Due to the topology of the OpenFirmware device tree,
193
         * the nesting of peer nodes could be to wide and the
201
         * the nesting of peer nodes could be to wide and the
194
         * risk of overflowing the stack is too real.
202
         * risk of overflowing the stack is too real.
195
         */
203
         */
196
        peer = ofw_get_peer_node(current);
204
        peer = ofw_get_peer_node(current);
197
        if (peer != 0 && peer != -1) {
205
        if (peer != 0 && peer != -1) {
198
            ofw_tree_node_t *peer_node;
206
            ofw_tree_node_t *peer_node;
199
       
207
       
200
            peer_node = ofw_tree_node_alloc();
208
            peer_node = ofw_tree_node_alloc();
201
            if (peer_node) {
209
            if (peer_node) {
202
                current_node->peer = peer_node;
210
                current_node->peer = peer_node;
203
                current_node = peer_node;
211
                current_node = peer_node;
204
                current = peer;
212
                current = peer;
205
                /*
213
                /*
206
                 * Process the peer in next iteration.
214
                 * Process the peer in next iteration.
207
                 */
215
                 */
208
                continue;
216
                continue;
209
            }
217
            }
210
        }
218
        }
211
        /*
219
        /*
212
         * No more peers on this level.
220
         * No more peers on this level.
213
         */
221
         */
214
        break;
222
        break;
215
    }
223
    }
216
}
224
}
217
 
225
 
218
/** Construct memory representation of OpenFirmware device tree.
226
/** Construct memory representation of OpenFirmware device tree.
219
 *
227
 *
220
 * @return NULL on failure or pointer to the root node.
228
 * @return      NULL on failure or pointer to the root node.
221
 */
229
 */
222
ofw_tree_node_t *ofw_tree_build(void)
230
ofw_tree_node_t *ofw_tree_build(void)
223
{
231
{
224
    ofw_tree_node_t *root;
232
    ofw_tree_node_t *root;
225
    phandle ssm_node;
233
    phandle ssm_node;
226
    ofw_tree_node_t *ssm;
234
    ofw_tree_node_t *ssm;
227
   
235
   
228
    root = ofw_tree_node_alloc();
236
    root = ofw_tree_node_alloc();
229
    if (root)
237
    if (root)
230
        ofw_tree_node_process(root, NULL, ofw_root);
238
        ofw_tree_node_process(root, NULL, ofw_root);
231
 
239
 
232
    /*
240
    /*
233
     * The firmware client interface does not automatically include the
241
     * The firmware client interface does not automatically include the
234
     * "ssm" node in the list of children of "/". A nasty yet working
242
     * "ssm" node in the list of children of "/". A nasty yet working
235
     * solution is to explicitly stick "ssm" to the OFW tree.
243
     * solution is to explicitly stick "ssm" to the OFW tree.
236
     */
244
     */
237
    ssm_node = ofw_find_device("/ssm@0,0");
245
    ssm_node = ofw_find_device("/ssm@0,0");
238
    if (ssm_node != -1) {
246
    if (ssm_node != -1) {
239
        ssm = ofw_tree_node_alloc();
247
        ssm = ofw_tree_node_alloc();
240
        if (ssm) {
248
        if (ssm) {
241
            ofw_tree_node_process(
249
            ofw_tree_node_process(ssm, root,
242
                ssm, root, ofw_find_device("/ssm@0,0"));
250
                ofw_find_device("/ssm@0,0"));
243
            ssm->peer = root->child;
251
            ssm->peer = root->child;
244
            root->child = ssm;
252
            root->child = ssm;
245
        }
253
        }
246
    }
254
    }
247
   
255
   
248
    return root;
256
    return root;
249
}
257
}
250
 
258