Subversion Repositories HelenOS

Rev

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

Rev Author Line No. Line
1893 jermar 1
/*
2071 jermar 2
 * Copyright (c) 2006 Jakub Jermar
1893 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
#include <ofw_tree.h>
30
#include <ofw.h>
31
#include <types.h>
32
#include <string.h>
1894 jermar 33
#include <balloc.h>
1895 jermar 34
#include <asm.h>
1893 jermar 35
 
1895 jermar 36
#define MAX_PATH_LEN    256
37
 
1893 jermar 38
static ofw_tree_node_t *ofw_tree_node_alloc(void)
39
{
1894 jermar 40
    return balloc(sizeof(ofw_tree_node_t), sizeof(ofw_tree_node_t));
1893 jermar 41
}
42
 
43
static ofw_tree_property_t *ofw_tree_properties_alloc(unsigned count)
44
{
1894 jermar 45
    return balloc(count * sizeof(ofw_tree_property_t), sizeof(ofw_tree_property_t));
1893 jermar 46
}
47
 
48
static void * ofw_tree_space_alloc(size_t size)
49
{
1896 jermar 50
    char *addr;
51
 
52
    /*
53
     * What we do here is a nasty hack :-)
54
     * Problem: string property values that are allocated via this
55
     * function typically do not contain the trailing '\0'. This
56
     * is very uncomfortable for kernel, which is supposed to deal
57
     * with the properties.
58
     * Solution: when allocating space via this function, we always
59
     * allocate space for the extra '\0' character that we store
60
     * behind the requested memory.
61
     */
62
    addr = balloc(size + 1, size);
63
    if (addr)
64
        addr[size] = '\0';
65
    return addr;
1893 jermar 66
}
67
 
68
/** Transfer information from one OpenFirmware node into its memory representation.
69
 *
70
 * Transfer entire information from the OpenFirmware device tree 'current' node to
71
 * its memory representation in 'current_node'. This function recursively processes
1973 jermar 72
 * all node's children. Node's peers are processed iteratively in order to prevent
73
 * stack from overflowing.
1893 jermar 74
 *
75
 * @param current_node  Pointer to uninitialized ofw_tree_node structure that will
76
 *          become the memory represenation of 'current'.
77
 * @param parent_node   Parent ofw_tree_node structure or NULL in case of root node.
78
 * @param current   OpenFirmware phandle to the current device tree node.
79
 */
80
static void ofw_tree_node_process(ofw_tree_node_t *current_node,
81
    ofw_tree_node_t *parent_node, phandle current)
82
{
1895 jermar 83
    static char path[MAX_PATH_LEN+1];
84
    static char name[OFW_TREE_PROPERTY_MAX_NAMELEN];
1893 jermar 85
    phandle peer;
86
    phandle child;
1895 jermar 87
    size_t len;
88
    int i;
1893 jermar 89
 
1973 jermar 90
    while (current_node) {
91
        /*
92
         * Initialize node.
93
         */
94
        current_node->parent = parent_node;
95
        current_node->peer = NULL;
96
        current_node->child = NULL;
97
        current_node->node_handle = current;
98
        current_node->properties = 0;
99
        current_node->property = NULL;
100
        current_node->device = NULL;
1893 jermar 101
 
1973 jermar 102
        /*
103
         * Get the disambigued name.
104
         */
105
        len = ofw_package_to_path(current, path, MAX_PATH_LEN);
106
        if (len == -1)
107
            return;
1895 jermar 108
 
1973 jermar 109
        path[len] = '\0';
110
        for (i = len - 1; i >= 0 && path[i] != '/'; i--)
111
            ;
112
        i++;    /* do not include '/' */
1895 jermar 113
 
1973 jermar 114
        len -= i;
115
 
116
        /* add space for trailing '\0' */
117
        current_node->da_name = ofw_tree_space_alloc(len + 1);
118
        if (!current_node->da_name)
119
            return;
1895 jermar 120
 
1973 jermar 121
        memcpy(current_node->da_name, &path[i], len);
122
        current_node->da_name[len] = '\0';
1895 jermar 123
 
1893 jermar 124
 
1973 jermar 125
        /*
126
         * Recursively process the potential child node.
127
         */
128
        child = ofw_get_child_node(current);
129
        if (child != 0 && child != -1) {
130
            ofw_tree_node_t *child_node;
1893 jermar 131
 
1973 jermar 132
            child_node = ofw_tree_node_alloc();
133
            if (child_node) {
134
                ofw_tree_node_process(child_node, current_node, child);
135
                current_node->child = child_node;
136
            }
1893 jermar 137
        }
138
 
1973 jermar 139
        /*
140
         * Count properties.
141
         */
142
        name[0] = '\0';
143
        while (ofw_next_property(current, name, name) == 1)
144
            current_node->properties++;
1893 jermar 145
 
1973 jermar 146
        if (!current_node->properties)
147
            return;
1893 jermar 148
 
1973 jermar 149
        /*
150
         * Copy properties.
151
         */
152
        current_node->property = ofw_tree_properties_alloc(current_node->properties);
153
        if (!current_node->property)
154
            return;
1893 jermar 155
 
1973 jermar 156
        name[0] = '\0';
157
        for (i = 0; ofw_next_property(current, name, name) == 1; i++) {
158
            size_t size;
1893 jermar 159
 
1973 jermar 160
            if (i == current_node->properties)
161
                break;
1893 jermar 162
 
1973 jermar 163
            memcpy(current_node->property[i].name, name,
164
                OFW_TREE_PROPERTY_MAX_NAMELEN);
165
            current_node->property[i].name[OFW_TREE_PROPERTY_MAX_NAMELEN] = '\0';
1893 jermar 166
 
1973 jermar 167
            size = ofw_get_proplen(current, name);
168
            current_node->property[i].size = size;
169
            if (size) {
170
                void *buf;
1893 jermar 171
 
1973 jermar 172
                buf = ofw_tree_space_alloc(size);
173
                if (current_node->property[i].value = buf) {
174
                    /*
175
                     * Copy property value to memory node.
176
                     */
177
                    (void) ofw_get_property(current, name, buf, size);
178
                }
179
            } else {
180
                current_node->property[i].value = NULL;
181
            }
182
        }
183
 
184
        current_node->properties = i;   /* Just in case we ran out of memory. */
185
 
186
        /*
187
         * Iteratively process the next peer node.
188
         * Note that recursion is a bad idea here.
189
         * Due to the topology of the OpenFirmware device tree,
190
         * the nesting of peer nodes could be to wide and the
191
         * risk of overflowing the stack is too real.
192
         */
193
        peer = ofw_get_peer_node(current);
194
        if (peer != 0 && peer != -1) {
195
            ofw_tree_node_t *peer_node;
196
 
197
            peer_node = ofw_tree_node_alloc();
198
            if (peer_node) {
199
                current_node->peer = peer_node;
200
                current_node = peer_node;
201
                current = peer;
1893 jermar 202
                /*
1973 jermar 203
                 * Process the peer in next iteration.
1893 jermar 204
                 */
1973 jermar 205
                continue;
1893 jermar 206
            }
207
        }
1973 jermar 208
        /*
209
         * No more peers on this level.
210
         */
211
        break;
1893 jermar 212
    }
213
}
214
 
215
/** Construct memory representation of OpenFirmware device tree.
216
 *
217
 * @return NULL on failure or pointer to the root node.
218
 */
219
ofw_tree_node_t *ofw_tree_build(void)
220
{
221
    ofw_tree_node_t *root;
222
 
223
    root = ofw_tree_node_alloc();
224
    if (root)
225
        ofw_tree_node_process(root, NULL, ofw_root);
226
 
227
    return root;
228
}