Subversion Repositories HelenOS

Rev

Rev 3582 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1894 jermar 1
/*
2071 jermar 2
 * Copyright (c) 2006 Jakub Jermar
1894 jermar 3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
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
15
 *   derived from this software without specific prior written permission.
16
 *
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
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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
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
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
29
/** @addtogroup ofw
30
 * @{
31
 */
32
/**
33
 * @file
34
 * @brief   OpenFirmware device tree navigation.
35
 *
36
 */
37
 
1895 jermar 38
#include <genarch/ofw/ofw_tree.h>
39
#include <arch/memstr.h>
1972 jermar 40
#include <mm/slab.h>
1895 jermar 41
#include <func.h>
42
#include <print.h>
43
#include <panic.h>
44
 
45
#define PATH_MAX_LEN    80
46
#define NAME_BUF_LEN    50
47
 
48
static ofw_tree_node_t *ofw_root;
49
 
50
void ofw_tree_init(ofw_tree_node_t *root)
51
{
52
    ofw_root = root;
53
}
54
 
1896 jermar 55
/** Get OpenFirmware node property.
56
 *
57
 * @param node Node in which to lookup the property.
58
 * @param name Name of the property.
59
 *
60
 * @return Pointer to the property structure or NULL if no such property.
61
 */
62
ofw_tree_property_t *ofw_tree_getprop(const ofw_tree_node_t *node, const char *name)
63
{
2745 decky 64
    unsigned int i;
1896 jermar 65
 
66
    for (i = 0; i < node->properties; i++) {
67
        if (strcmp(node->property[i].name, name) == 0)
68
            return &node->property[i];
69
    }
70
 
71
    return NULL;
72
}
73
 
1895 jermar 74
/** Return value of the 'name' property.
75
 *
76
 * @param node Node of interest.
77
 *
78
 * @return Value of the 'name' property belonging to the node.
79
 */
80
const char *ofw_tree_node_name(const ofw_tree_node_t *node)
81
{
1896 jermar 82
    ofw_tree_property_t *prop;
1895 jermar 83
 
1896 jermar 84
    prop = ofw_tree_getprop(node, "name");
85
    if (!prop)
86
        panic("Node without name property.\n");
87
 
88
    if (prop->size < 2)
89
        panic("Invalid name property.\n");
1895 jermar 90
 
1896 jermar 91
    return prop->value;
1895 jermar 92
}
93
 
94
/** Lookup child of given name.
95
 *
96
 * @param node Node whose child is being looked up.
1896 jermar 97
 * @param name Name of the child being looked up.
1895 jermar 98
 *
99
 * @return NULL if there is no such child or pointer to the matching child node.
100
 */
1899 jermar 101
ofw_tree_node_t *ofw_tree_find_child(ofw_tree_node_t *node, const char *name)
1895 jermar 102
{
103
    ofw_tree_node_t *cur;
104
 
1896 jermar 105
    /*
106
     * Try to find the disambigued name.
107
     */
1895 jermar 108
    for (cur = node->child; cur; cur = cur->peer) {
1896 jermar 109
        if (strcmp(cur->da_name, name) == 0)
1895 jermar 110
            return cur;
111
    }
112
 
1896 jermar 113
    /*
114
     * Disambigued name not found.
115
     * Lets try our luck with possibly ambiguous "name" property.
116
     *
117
     * We need to do this because paths stored in "/aliases"
118
     * are not always fully-qualified.
119
     */
120
    for (cur = node->child; cur; cur = cur->peer) {
121
        if (strcmp(ofw_tree_node_name(cur), name) == 0)
122
            return cur;
123
    }
124
 
1895 jermar 125
    return NULL;
126
}
127
 
1899 jermar 128
/** Lookup first child of given device type.
129
 *
130
 * @param node Node whose child is being looked up.
131
 * @param name Device type of the child being looked up.
132
 *
133
 * @return NULL if there is no such child or pointer to the matching child node.
134
 */
135
ofw_tree_node_t *ofw_tree_find_child_by_device_type(ofw_tree_node_t *node, const char *name)
136
{
137
    ofw_tree_node_t *cur;
138
    ofw_tree_property_t *prop;
139
 
140
    for (cur = node->child; cur; cur = cur->peer) {
141
        prop = ofw_tree_getprop(cur, "device_type");
142
        if (!prop || !prop->value)
143
            continue;
144
        if (strcmp(prop->value, name) == 0)
145
            return cur;
146
    }
147
 
148
    return NULL;
149
}
150
 
1908 jermar 151
/** Lookup node with matching node_handle.
152
 *
1974 jermar 153
 * Child nodes are looked up recursively contrary to peer nodes that
154
 * are looked up iteratively to avoid stack overflow.
155
 *
1908 jermar 156
 * @param root Root of the searched subtree.
157
 * @param handle OpenFirmware handle.
158
 *
159
 * @return NULL if there is no such node or pointer to the matching node.
160
 */
161
ofw_tree_node_t *ofw_tree_find_node_by_handle(ofw_tree_node_t *root, uint32_t handle)
162
{
1974 jermar 163
    ofw_tree_node_t *cur;
1908 jermar 164
 
1974 jermar 165
    for (cur = root; cur; cur = cur->peer) {       
166
        if (cur->node_handle == handle)
167
            return cur;
1908 jermar 168
 
1974 jermar 169
        if (cur->child) {
170
            ofw_tree_node_t *node;
171
 
172
            node = ofw_tree_find_node_by_handle(cur->child, handle);
173
            if (node)
174
                return node;
175
        }
1908 jermar 176
    }
177
 
178
    return NULL;   
179
}
180
 
1899 jermar 181
/** Lookup first peer of given device type.
182
 *
183
 * @param node Node whose peer is being looked up.
184
 * @param name Device type of the child being looked up.
185
 *
186
 * @return NULL if there is no such child or pointer to the matching child node.
187
 */
188
ofw_tree_node_t *ofw_tree_find_peer_by_device_type(ofw_tree_node_t *node, const char *name)
189
{
190
    ofw_tree_node_t *cur;
191
    ofw_tree_property_t *prop;
192
 
193
    for (cur = node->peer; cur; cur = cur->peer) {
194
        prop = ofw_tree_getprop(cur, "device_type");
195
        if (!prop || !prop->value)
196
            continue;
197
        if (strcmp(prop->value, name) == 0)
198
            return cur;
199
    }
200
 
201
    return NULL;
202
}
203
 
204
 
3582 rimsky 205
/** Lookup first peer of given name.
206
 *
207
 * @param node Node whose peer is being looked up.
208
 * @param name Name of the child being looked up.
209
 *
210
 * @return NULL if there is no such peer or pointer to the matching peer node.
211
 */
3618 rimsky 212
ofw_tree_node_t *ofw_tree_find_peer_by_name(ofw_tree_node_t *node,
213
    const char *name)
3582 rimsky 214
{
215
    ofw_tree_node_t *cur;
216
    ofw_tree_property_t *prop;
217
 
218
    for (cur = node->peer; cur; cur = cur->peer) {
219
        prop = ofw_tree_getprop(cur, "name");
220
        if (!prop || !prop->value)
221
            continue;
222
        if (strcmp(prop->value, name) == 0)
223
            return cur;
224
    }
225
 
226
    return NULL;
227
}
228
 
1895 jermar 229
/** Lookup OpenFirmware node by its path.
230
 *
231
 * @param path Path to the node.
232
 *
233
 * @return NULL if there is no such node or pointer to the leaf node.
234
 */
235
ofw_tree_node_t *ofw_tree_lookup(const char *path)
236
{
237
    char buf[NAME_BUF_LEN+1];
238
    ofw_tree_node_t *node = ofw_root;
239
    index_t i, j;
240
 
241
    if (path[0] != '/')
242
        return NULL;
243
 
244
    for (i = 1; i < strlen(path) && node; i = j + 1) {
245
        for (j = i; j < strlen(path) && path[j] != '/'; j++)
246
            ;
247
        if (i == j) /* skip extra slashes */
248
            continue;
249
 
250
        memcpy(buf, &path[i], j - i);
251
        buf[j - i] = '\0';
252
        node = ofw_tree_find_child(node, buf);
253
    }
254
 
255
    return node;
256
}
257
 
1974 jermar 258
/** Print OpenFirmware device subtree rooted in a node.
1895 jermar 259
 *
1974 jermar 260
 * Child nodes are processed recursively and peer nodes are processed
261
 * iteratively in order to avoid stack overflow.
262
 *
1895 jermar 263
 * @param node Root of the subtree.
264
 * @param path Current path, NULL for the very root of the entire tree.
265
 */
266
static void ofw_tree_node_print(const ofw_tree_node_t *node, const char *path)
267
{
1972 jermar 268
    char *p;
1974 jermar 269
    const ofw_tree_node_t *cur;
1972 jermar 270
 
271
    p = (char *) malloc(PATH_MAX_LEN, 0);
272
 
1974 jermar 273
    for (cur = node; cur; cur = cur->peer) {
274
        if (cur->parent) {
275
            snprintf(p, PATH_MAX_LEN, "%s/%s", path, cur->da_name);
276
            printf("%s\n", p);
277
        } else {
278
            snprintf(p, PATH_MAX_LEN, "%s", cur->da_name);
279
            printf("/\n");
280
        }
281
 
282
        if (cur->child)
283
            ofw_tree_node_print(cur->child, p);
1895 jermar 284
    }
285
 
1972 jermar 286
    free(p);
1895 jermar 287
}
288
 
289
/** Print the structure of the OpenFirmware device tree. */
290
void ofw_tree_print(void)
291
{
292
    ofw_tree_node_print(ofw_root, NULL);
293
}
294
 
1894 jermar 295
/** @}
296
 */