Subversion Repositories HelenOS

Rev

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

Rev 1974 Rev 2071
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
/** @addtogroup ofw
29
/** @addtogroup ofw
30
 * @{
30
 * @{
31
 */
31
 */
32
/**
32
/**
33
 * @file
33
 * @file
34
 * @brief   OpenFirmware device tree navigation.
34
 * @brief   OpenFirmware device tree navigation.
35
 *
35
 *
36
 */
36
 */
37
 
37
 
38
#include <genarch/ofw/ofw_tree.h>
38
#include <genarch/ofw/ofw_tree.h>
39
#include <arch/memstr.h>
39
#include <arch/memstr.h>
40
#include <mm/slab.h>
40
#include <mm/slab.h>
41
#include <func.h>
41
#include <func.h>
42
#include <print.h>
42
#include <print.h>
43
#include <panic.h>
43
#include <panic.h>
44
 
44
 
45
#define PATH_MAX_LEN    80
45
#define PATH_MAX_LEN    80
46
#define NAME_BUF_LEN    50
46
#define NAME_BUF_LEN    50
47
 
47
 
48
static ofw_tree_node_t *ofw_root;
48
static ofw_tree_node_t *ofw_root;
49
 
49
 
50
void ofw_tree_init(ofw_tree_node_t *root)
50
void ofw_tree_init(ofw_tree_node_t *root)
51
{
51
{
52
    ofw_root = root;
52
    ofw_root = root;
53
}
53
}
54
 
54
 
55
/** Get OpenFirmware node property.
55
/** Get OpenFirmware node property.
56
 *
56
 *
57
 * @param node Node in which to lookup the property.
57
 * @param node Node in which to lookup the property.
58
 * @param name Name of the property.
58
 * @param name Name of the property.
59
 *
59
 *
60
 * @return Pointer to the property structure or NULL if no such property.
60
 * @return Pointer to the property structure or NULL if no such property.
61
 */
61
 */
62
ofw_tree_property_t *ofw_tree_getprop(const ofw_tree_node_t *node, const char *name)
62
ofw_tree_property_t *ofw_tree_getprop(const ofw_tree_node_t *node, const char *name)
63
{
63
{
64
    int i;
64
    int i;
65
   
65
   
66
    for (i = 0; i < node->properties; i++) {
66
    for (i = 0; i < node->properties; i++) {
67
        if (strcmp(node->property[i].name, name) == 0)
67
        if (strcmp(node->property[i].name, name) == 0)
68
            return &node->property[i];
68
            return &node->property[i];
69
    }
69
    }
70
 
70
 
71
    return NULL;
71
    return NULL;
72
}
72
}
73
 
73
 
74
/** Return value of the 'name' property.
74
/** Return value of the 'name' property.
75
 *
75
 *
76
 * @param node Node of interest.
76
 * @param node Node of interest.
77
 *
77
 *
78
 * @return Value of the 'name' property belonging to the node.
78
 * @return Value of the 'name' property belonging to the node.
79
 */
79
 */
80
const char *ofw_tree_node_name(const ofw_tree_node_t *node)
80
const char *ofw_tree_node_name(const ofw_tree_node_t *node)
81
{
81
{
82
    ofw_tree_property_t *prop;
82
    ofw_tree_property_t *prop;
83
   
83
   
84
    prop = ofw_tree_getprop(node, "name");
84
    prop = ofw_tree_getprop(node, "name");
85
    if (!prop)
85
    if (!prop)
86
        panic("Node without name property.\n");
86
        panic("Node without name property.\n");
87
       
87
       
88
    if (prop->size < 2)
88
    if (prop->size < 2)
89
        panic("Invalid name property.\n");
89
        panic("Invalid name property.\n");
90
   
90
   
91
    return prop->value;
91
    return prop->value;
92
}
92
}
93
 
93
 
94
/** Lookup child of given name.
94
/** Lookup child of given name.
95
 *
95
 *
96
 * @param node Node whose child is being looked up.
96
 * @param node Node whose child is being looked up.
97
 * @param name Name of the child being looked up.
97
 * @param name Name of the child being looked up.
98
 *
98
 *
99
 * @return NULL if there is no such child or pointer to the matching child node.
99
 * @return NULL if there is no such child or pointer to the matching child node.
100
 */
100
 */
101
ofw_tree_node_t *ofw_tree_find_child(ofw_tree_node_t *node, const char *name)
101
ofw_tree_node_t *ofw_tree_find_child(ofw_tree_node_t *node, const char *name)
102
{
102
{
103
    ofw_tree_node_t *cur;
103
    ofw_tree_node_t *cur;
104
   
104
   
105
    /*
105
    /*
106
     * Try to find the disambigued name.
106
     * Try to find the disambigued name.
107
     */
107
     */
108
    for (cur = node->child; cur; cur = cur->peer) {
108
    for (cur = node->child; cur; cur = cur->peer) {
109
        if (strcmp(cur->da_name, name) == 0)
109
        if (strcmp(cur->da_name, name) == 0)
110
            return cur;
110
            return cur;
111
    }
111
    }
112
   
112
   
113
    /*
113
    /*
114
     * Disambigued name not found.
114
     * Disambigued name not found.
115
     * Lets try our luck with possibly ambiguous "name" property.
115
     * Lets try our luck with possibly ambiguous "name" property.
116
     *
116
     *
117
     * We need to do this because paths stored in "/aliases"
117
     * We need to do this because paths stored in "/aliases"
118
     * are not always fully-qualified.
118
     * are not always fully-qualified.
119
     */
119
     */
120
    for (cur = node->child; cur; cur = cur->peer) {
120
    for (cur = node->child; cur; cur = cur->peer) {
121
        if (strcmp(ofw_tree_node_name(cur), name) == 0)
121
        if (strcmp(ofw_tree_node_name(cur), name) == 0)
122
            return cur;
122
            return cur;
123
    }
123
    }
124
       
124
       
125
    return NULL;
125
    return NULL;
126
}
126
}
127
 
127
 
128
/** Lookup first child of given device type.
128
/** Lookup first child of given device type.
129
 *
129
 *
130
 * @param node Node whose child is being looked up.
130
 * @param node Node whose child is being looked up.
131
 * @param name Device type of the child being looked up.
131
 * @param name Device type of the child being looked up.
132
 *
132
 *
133
 * @return NULL if there is no such child or pointer to the matching child node.
133
 * @return NULL if there is no such child or pointer to the matching child node.
134
 */
134
 */
135
ofw_tree_node_t *ofw_tree_find_child_by_device_type(ofw_tree_node_t *node, const char *name)
135
ofw_tree_node_t *ofw_tree_find_child_by_device_type(ofw_tree_node_t *node, const char *name)
136
{
136
{
137
    ofw_tree_node_t *cur;
137
    ofw_tree_node_t *cur;
138
    ofw_tree_property_t *prop;
138
    ofw_tree_property_t *prop;
139
   
139
   
140
    for (cur = node->child; cur; cur = cur->peer) {
140
    for (cur = node->child; cur; cur = cur->peer) {
141
        prop = ofw_tree_getprop(cur, "device_type");
141
        prop = ofw_tree_getprop(cur, "device_type");
142
        if (!prop || !prop->value)
142
        if (!prop || !prop->value)
143
            continue;
143
            continue;
144
        if (strcmp(prop->value, name) == 0)
144
        if (strcmp(prop->value, name) == 0)
145
            return cur;
145
            return cur;
146
    }
146
    }
147
           
147
           
148
    return NULL;
148
    return NULL;
149
}
149
}
150
 
150
 
151
/** Lookup node with matching node_handle.
151
/** Lookup node with matching node_handle.
152
 *
152
 *
153
 * Child nodes are looked up recursively contrary to peer nodes that
153
 * Child nodes are looked up recursively contrary to peer nodes that
154
 * are looked up iteratively to avoid stack overflow.
154
 * are looked up iteratively to avoid stack overflow.
155
 *
155
 *
156
 * @param root Root of the searched subtree.
156
 * @param root Root of the searched subtree.
157
 * @param handle OpenFirmware handle.
157
 * @param handle OpenFirmware handle.
158
 *
158
 *
159
 * @return NULL if there is no such node or pointer to the matching node.
159
 * @return NULL if there is no such node or pointer to the matching node.
160
 */
160
 */
161
ofw_tree_node_t *ofw_tree_find_node_by_handle(ofw_tree_node_t *root, uint32_t handle)
161
ofw_tree_node_t *ofw_tree_find_node_by_handle(ofw_tree_node_t *root, uint32_t handle)
162
{
162
{
163
    ofw_tree_node_t *cur;
163
    ofw_tree_node_t *cur;
164
 
164
 
165
    for (cur = root; cur; cur = cur->peer) {       
165
    for (cur = root; cur; cur = cur->peer) {       
166
        if (cur->node_handle == handle)
166
        if (cur->node_handle == handle)
167
            return cur;
167
            return cur;
168
 
168
 
169
        if (cur->child) {
169
        if (cur->child) {
170
            ofw_tree_node_t *node;
170
            ofw_tree_node_t *node;
171
           
171
           
172
            node = ofw_tree_find_node_by_handle(cur->child, handle);
172
            node = ofw_tree_find_node_by_handle(cur->child, handle);
173
            if (node)
173
            if (node)
174
                return node;
174
                return node;
175
        }
175
        }
176
    }
176
    }
177
   
177
   
178
    return NULL;   
178
    return NULL;   
179
}
179
}
180
 
180
 
181
/** Lookup first peer of given device type.
181
/** Lookup first peer of given device type.
182
 *
182
 *
183
 * @param node Node whose peer is being looked up.
183
 * @param node Node whose peer is being looked up.
184
 * @param name Device type of the child being looked up.
184
 * @param name Device type of the child being looked up.
185
 *
185
 *
186
 * @return NULL if there is no such child or pointer to the matching child node.
186
 * @return NULL if there is no such child or pointer to the matching child node.
187
 */
187
 */
188
ofw_tree_node_t *ofw_tree_find_peer_by_device_type(ofw_tree_node_t *node, const char *name)
188
ofw_tree_node_t *ofw_tree_find_peer_by_device_type(ofw_tree_node_t *node, const char *name)
189
{
189
{
190
    ofw_tree_node_t *cur;
190
    ofw_tree_node_t *cur;
191
    ofw_tree_property_t *prop;
191
    ofw_tree_property_t *prop;
192
   
192
   
193
    for (cur = node->peer; cur; cur = cur->peer) {
193
    for (cur = node->peer; cur; cur = cur->peer) {
194
        prop = ofw_tree_getprop(cur, "device_type");
194
        prop = ofw_tree_getprop(cur, "device_type");
195
        if (!prop || !prop->value)
195
        if (!prop || !prop->value)
196
            continue;
196
            continue;
197
        if (strcmp(prop->value, name) == 0)
197
        if (strcmp(prop->value, name) == 0)
198
            return cur;
198
            return cur;
199
    }
199
    }
200
           
200
           
201
    return NULL;
201
    return NULL;
202
}
202
}
203
 
203
 
204
 
204
 
205
/** Lookup OpenFirmware node by its path.
205
/** Lookup OpenFirmware node by its path.
206
 *
206
 *
207
 * @param path Path to the node.
207
 * @param path Path to the node.
208
 *
208
 *
209
 * @return NULL if there is no such node or pointer to the leaf node.
209
 * @return NULL if there is no such node or pointer to the leaf node.
210
 */
210
 */
211
ofw_tree_node_t *ofw_tree_lookup(const char *path)
211
ofw_tree_node_t *ofw_tree_lookup(const char *path)
212
{
212
{
213
    char buf[NAME_BUF_LEN+1];
213
    char buf[NAME_BUF_LEN+1];
214
    ofw_tree_node_t *node = ofw_root;
214
    ofw_tree_node_t *node = ofw_root;
215
    index_t i, j;
215
    index_t i, j;
216
   
216
   
217
    if (path[0] != '/')
217
    if (path[0] != '/')
218
        return NULL;
218
        return NULL;
219
   
219
   
220
    for (i = 1; i < strlen(path) && node; i = j + 1) {
220
    for (i = 1; i < strlen(path) && node; i = j + 1) {
221
        for (j = i; j < strlen(path) && path[j] != '/'; j++)
221
        for (j = i; j < strlen(path) && path[j] != '/'; j++)
222
            ;
222
            ;
223
        if (i == j) /* skip extra slashes */
223
        if (i == j) /* skip extra slashes */
224
            continue;
224
            continue;
225
           
225
           
226
        memcpy(buf, &path[i], j - i);
226
        memcpy(buf, &path[i], j - i);
227
        buf[j - i] = '\0';
227
        buf[j - i] = '\0';
228
        node = ofw_tree_find_child(node, buf);
228
        node = ofw_tree_find_child(node, buf);
229
    }
229
    }
230
   
230
   
231
    return node;
231
    return node;
232
}
232
}
233
 
233
 
234
/** Print OpenFirmware device subtree rooted in a node.
234
/** Print OpenFirmware device subtree rooted in a node.
235
 *
235
 *
236
 * Child nodes are processed recursively and peer nodes are processed
236
 * Child nodes are processed recursively and peer nodes are processed
237
 * iteratively in order to avoid stack overflow.
237
 * iteratively in order to avoid stack overflow.
238
 *
238
 *
239
 * @param node Root of the subtree.
239
 * @param node Root of the subtree.
240
 * @param path Current path, NULL for the very root of the entire tree.
240
 * @param path Current path, NULL for the very root of the entire tree.
241
 */
241
 */
242
static void ofw_tree_node_print(const ofw_tree_node_t *node, const char *path)
242
static void ofw_tree_node_print(const ofw_tree_node_t *node, const char *path)
243
{
243
{
244
    char *p;
244
    char *p;
245
    const ofw_tree_node_t *cur;
245
    const ofw_tree_node_t *cur;
246
 
246
 
247
    p = (char *) malloc(PATH_MAX_LEN, 0);
247
    p = (char *) malloc(PATH_MAX_LEN, 0);
248
 
248
 
249
    for (cur = node; cur; cur = cur->peer) {
249
    for (cur = node; cur; cur = cur->peer) {
250
        if (cur->parent) {
250
        if (cur->parent) {
251
            snprintf(p, PATH_MAX_LEN, "%s/%s", path, cur->da_name);
251
            snprintf(p, PATH_MAX_LEN, "%s/%s", path, cur->da_name);
252
            printf("%s\n", p);
252
            printf("%s\n", p);
253
        } else {
253
        } else {
254
            snprintf(p, PATH_MAX_LEN, "%s", cur->da_name);
254
            snprintf(p, PATH_MAX_LEN, "%s", cur->da_name);
255
            printf("/\n");
255
            printf("/\n");
256
        }
256
        }
257
 
257
 
258
        if (cur->child)
258
        if (cur->child)
259
            ofw_tree_node_print(cur->child, p);
259
            ofw_tree_node_print(cur->child, p);
260
    }
260
    }
261
 
261
 
262
    free(p);
262
    free(p);
263
}
263
}
264
 
264
 
265
/** Print the structure of the OpenFirmware device tree. */
265
/** Print the structure of the OpenFirmware device tree. */
266
void ofw_tree_print(void)
266
void ofw_tree_print(void)
267
{
267
{
268
    ofw_tree_node_print(ofw_root, NULL);
268
    ofw_tree_node_print(ofw_root, NULL);
269
}
269
}
270
 
270
 
271
/** @}
271
/** @}
272
 */
272
 */
273
 
273