Subversion Repositories HelenOS

Rev

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

Rev 1907 Rev 1909
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), sizeof(ofw_tree_property_t));
46
}
46
}
47
 
47
 
48
static void * ofw_tree_space_alloc(size_t size)
48
static void * ofw_tree_space_alloc(size_t size)
49
{
49
{
50
    char *addr;
50
    char *addr;
51
 
51
 
52
    /*
52
    /*
53
     * What we do here is a nasty hack :-)
53
     * What we do here is a nasty hack :-)
54
     * Problem: string property values that are allocated via this
54
     * Problem: string property values that are allocated via this
55
     * function typically do not contain the trailing '\0'. This
55
     * function typically do not contain the trailing '\0'. This
56
     * is very uncomfortable for kernel, which is supposed to deal
56
     * is very uncomfortable for kernel, which is supposed to deal
57
     * with the properties.
57
     * with the properties.
58
     * Solution: when allocating space via this function, we always
58
     * Solution: when allocating space via this function, we always
59
     * allocate space for the extra '\0' character that we store
59
     * allocate space for the extra '\0' character that we store
60
     * behind the requested memory.
60
     * behind the requested memory.
61
     */
61
     */
62
    addr = balloc(size + 1, size);
62
    addr = balloc(size + 1, size);
63
    if (addr)
63
    if (addr)
64
        addr[size] = '\0';
64
        addr[size] = '\0';
65
    return addr;
65
    return addr;
66
}
66
}
67
 
67
 
68
/** Transfer information from one OpenFirmware node into its memory representation.
68
/** Transfer information from one OpenFirmware node into its memory representation.
69
 *
69
 *
70
 * Transfer entire information from the OpenFirmware device tree 'current' node to
70
 * Transfer entire information from the OpenFirmware device tree 'current' node to
71
 * its memory representation in 'current_node'. This function recursively processes
71
 * its memory representation in 'current_node'. This function recursively processes
72
 * all node's peers and children.
72
 * all node's peers and children.
73
 *
73
 *
74
 * @param current_node  Pointer to uninitialized ofw_tree_node structure that will
74
 * @param current_node  Pointer to uninitialized ofw_tree_node structure that will
75
 *          become the memory represenation of 'current'.
75
 *          become the memory represenation of 'current'.
76
 * @param parent_node   Parent ofw_tree_node structure or NULL in case of root node.
76
 * @param parent_node   Parent ofw_tree_node structure or NULL in case of root node.
77
 * @param current   OpenFirmware phandle to the current device tree node.
77
 * @param current   OpenFirmware phandle to the current device tree node.
78
 */
78
 */
79
static void ofw_tree_node_process(ofw_tree_node_t *current_node,
79
static void ofw_tree_node_process(ofw_tree_node_t *current_node,
80
    ofw_tree_node_t *parent_node, phandle current)
80
    ofw_tree_node_t *parent_node, phandle current)
81
{
81
{
82
    static char path[MAX_PATH_LEN+1];
82
    static char path[MAX_PATH_LEN+1];
83
    static char name[OFW_TREE_PROPERTY_MAX_NAMELEN];
83
    static char name[OFW_TREE_PROPERTY_MAX_NAMELEN];
84
    phandle peer;
84
    phandle peer;
85
    phandle child;
85
    phandle child;
86
    unsigned properties = 0;
86
    unsigned properties = 0;
87
    size_t len;
87
    size_t len;
88
    int i;
88
    int i;
89
 
89
 
90
    /*
90
    /*
91
     * Initialize node.
91
     * Initialize node.
92
     */
92
     */
93
    current_node->parent = parent_node;
93
    current_node->parent = parent_node;
94
    current_node->peer = NULL;
94
    current_node->peer = NULL;
95
    current_node->child = NULL;
95
    current_node->child = NULL;
96
    current_node->node_handle = current;
96
    current_node->node_handle = current;
97
    current_node->properties = 0;
97
    current_node->properties = 0;
98
    current_node->property = NULL;
98
    current_node->property = NULL;
-
 
99
    current_node->device = NULL;
99
   
100
   
100
    /*
101
    /*
101
     * Get the disambigued name.
102
     * Get the disambigued name.
102
     */
103
     */
103
    len = ofw_package_to_path(current, path, MAX_PATH_LEN);
104
    len = ofw_package_to_path(current, path, MAX_PATH_LEN);
104
    if (len == -1)
105
    if (len == -1)
105
        return;
106
        return;
106
   
107
   
107
    path[len] = '\0';
108
    path[len] = '\0';
108
    for (i = len - 1; i >= 0 && path[i] != '/'; i--)
109
    for (i = len - 1; i >= 0 && path[i] != '/'; i--)
109
        ;
110
        ;
110
    i++;                                /* do not include '/' */
111
    i++;                                /* do not include '/' */
111
   
112
   
112
    len -= i;
113
    len -= i;
113
    current_node->da_name = ofw_tree_space_alloc(len + 1);      /* add space for trailing '\0' */
114
    current_node->da_name = ofw_tree_space_alloc(len + 1);      /* add space for trailing '\0' */
114
    if (!current_node->da_name)
115
    if (!current_node->da_name)
115
        return;
116
        return;
116
   
117
   
117
    memcpy(current_node->da_name, &path[i], len);
118
    memcpy(current_node->da_name, &path[i], len);
118
    current_node->da_name[len] = '\0';
119
    current_node->da_name[len] = '\0';
119
   
120
   
120
    /*
121
    /*
121
     * Recursively process the potential peer node.
122
     * Recursively process the potential peer node.
122
     */
123
     */
123
    peer = ofw_get_peer_node(current);
124
    peer = ofw_get_peer_node(current);
124
    if (peer != 0 && peer != -1) {
125
    if (peer != 0 && peer != -1) {
125
        ofw_tree_node_t *peer_node;
126
        ofw_tree_node_t *peer_node;
126
       
127
       
127
        peer_node = ofw_tree_node_alloc();
128
        peer_node = ofw_tree_node_alloc();
128
        if (peer_node) {
129
        if (peer_node) {
129
            ofw_tree_node_process(peer_node, parent_node, peer);
130
            ofw_tree_node_process(peer_node, parent_node, peer);
130
            current_node->peer = peer_node;
131
            current_node->peer = peer_node;
131
        }
132
        }
132
    }
133
    }
133
   
134
   
134
    /*
135
    /*
135
     * Recursively process the potential child node.
136
     * Recursively process the potential child node.
136
     */
137
     */
137
    child = ofw_get_child_node(current);
138
    child = ofw_get_child_node(current);
138
    if (child != 0 && child != -1) {
139
    if (child != 0 && child != -1) {
139
        ofw_tree_node_t *child_node;
140
        ofw_tree_node_t *child_node;
140
       
141
       
141
        child_node = ofw_tree_node_alloc();
142
        child_node = ofw_tree_node_alloc();
142
        if (child_node) {
143
        if (child_node) {
143
            ofw_tree_node_process(child_node, current_node, child);
144
            ofw_tree_node_process(child_node, current_node, child);
144
            current_node->child = child_node;
145
            current_node->child = child_node;
145
        }
146
        }
146
    }
147
    }
147
   
148
   
148
    /*
149
    /*
149
     * Count properties.
150
     * Count properties.
150
     */
151
     */
151
    name[0] = '\0';
152
    name[0] = '\0';
152
    while (ofw_next_property(current, name, name) == 1)
153
    while (ofw_next_property(current, name, name) == 1)
153
        current_node->properties++;
154
        current_node->properties++;
154
   
155
   
155
    if (!current_node->properties)
156
    if (!current_node->properties)
156
        return;
157
        return;
157
   
158
   
158
    /*
159
    /*
159
     * Copy properties.
160
     * Copy properties.
160
     */
161
     */
161
    current_node->property = ofw_tree_properties_alloc(current_node->properties);
162
    current_node->property = ofw_tree_properties_alloc(current_node->properties);
162
    if (!current_node->property)
163
    if (!current_node->property)
163
        return;
164
        return;
164
       
165
       
165
    name[0] = '\0';
166
    name[0] = '\0';
166
    for (i = 0; ofw_next_property(current, name, name) == 1; i++) {
167
    for (i = 0; ofw_next_property(current, name, name) == 1; i++) {
167
        size_t size;
168
        size_t size;
168
       
169
       
169
        if (i == current_node->properties)
170
        if (i == current_node->properties)
170
            break;
171
            break;
171
       
172
       
172
        memcpy(current_node->property[i].name, name, OFW_TREE_PROPERTY_MAX_NAMELEN);
173
        memcpy(current_node->property[i].name, name, OFW_TREE_PROPERTY_MAX_NAMELEN);
173
        current_node->property[i].name[OFW_TREE_PROPERTY_MAX_NAMELEN] = '\0';
174
        current_node->property[i].name[OFW_TREE_PROPERTY_MAX_NAMELEN] = '\0';
174
 
175
 
175
        size = ofw_get_proplen(current, name);
176
        size = ofw_get_proplen(current, name);
176
        current_node->property[i].size = size;
177
        current_node->property[i].size = size;
177
        if (size) {
178
        if (size) {
178
            void *buf;
179
            void *buf;
179
           
180
           
180
            buf = ofw_tree_space_alloc(size);
181
            buf = ofw_tree_space_alloc(size);
181
            if (current_node->property[i].value = buf) {
182
            if (current_node->property[i].value = buf) {
182
                /*
183
                /*
183
                 * Copy property value to memory node.
184
                 * Copy property value to memory node.
184
                 */
185
                 */
185
                (void) ofw_get_property(current, name, buf, size);
186
                (void) ofw_get_property(current, name, buf, size);
186
            }
187
            }
187
        } else {
188
        } else {
188
            current_node->property[i].value = NULL;
189
            current_node->property[i].value = NULL;
189
        }
190
        }
190
    }
191
    }
191
       
192
       
192
    current_node->properties = i;   /* Just in case we ran out of memory. */
193
    current_node->properties = i;   /* Just in case we ran out of memory. */
193
}
194
}
194
 
195
 
195
/** Construct memory representation of OpenFirmware device tree.
196
/** Construct memory representation of OpenFirmware device tree.
196
 *
197
 *
197
 * @return NULL on failure or pointer to the root node.
198
 * @return NULL on failure or pointer to the root node.
198
 */
199
 */
199
ofw_tree_node_t *ofw_tree_build(void)
200
ofw_tree_node_t *ofw_tree_build(void)
200
{
201
{
201
    ofw_tree_node_t *root;
202
    ofw_tree_node_t *root;
202
   
203
   
203
    root = ofw_tree_node_alloc();
204
    root = ofw_tree_node_alloc();
204
    if (root)
205
    if (root)
205
        ofw_tree_node_process(root, NULL, ofw_root);
206
        ofw_tree_node_process(root, NULL, ofw_root);
206
   
207
   
207
    return root;
208
    return root;
208
}
209
}
209
 
210