Subversion Repositories HelenOS

Rev

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

Rev Author Line No. Line
1893 jermar 1
/*
2
 * Copyright (C) 2006 Jakub 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>
1893 jermar 34
 
35
static ofw_tree_node_t *ofw_tree_node_alloc(void)
36
{
1894 jermar 37
	return balloc(sizeof(ofw_tree_node_t), sizeof(ofw_tree_node_t));
1893 jermar 38
}
39
 
40
static ofw_tree_property_t *ofw_tree_properties_alloc(unsigned count)
41
{
1894 jermar 42
	return balloc(count * sizeof(ofw_tree_property_t), sizeof(ofw_tree_property_t));
1893 jermar 43
}
44
 
45
static void * ofw_tree_space_alloc(size_t size)
46
{
1894 jermar 47
	return balloc(size, size);
1893 jermar 48
}
49
 
50
/** Transfer information from one OpenFirmware node into its memory representation.
51
 *
52
 * Transfer entire information from the OpenFirmware device tree 'current' node to
53
 * its memory representation in 'current_node'. This function recursively processes
54
 * all node's peers and children.
55
 *
56
 * @param current_node	Pointer to uninitialized ofw_tree_node structure that will
57
 * 			become the memory represenation of 'current'.
58
 * @param parent_node	Parent ofw_tree_node structure or NULL in case of root node.
59
 * @param current	OpenFirmware phandle to the current device tree node.
60
 */
61
static void ofw_tree_node_process(ofw_tree_node_t *current_node,
62
	ofw_tree_node_t *parent_node, phandle current)
63
{
64
	phandle peer;
65
	phandle child;
66
	unsigned properties = 0;
67
	char name[OFW_TREE_PROPERTY_MAX_NAMELEN];
68
 
69
	/*
70
	 * Initialize node.
71
	 */
72
	current_node->parent = parent_node;
73
	current_node->peer = NULL;
74
	current_node->child = NULL;
75
	current_node->properties = 0;
76
	current_node->property = NULL;
77
 
78
	/*
79
	 * Recursively process the potential peer node.
80
	 */
81
	peer = ofw_get_peer_node(current);
82
	if (peer != 0 && peer != -1) {
83
		ofw_tree_node_t *peer_node;
84
 
85
		peer_node = ofw_tree_node_alloc();
86
		if (peer_node) {
87
			ofw_tree_node_process(peer_node, current_node, peer);
88
			current_node->peer = peer_node;
89
		}
90
	}
91
 
92
	/*
93
	 * Recursively process the potential child node.
94
	 */
95
	child = ofw_get_child_node(current);
96
	if (child != 0 && child != -1) {
97
		ofw_tree_node_t *child_node;
98
 
99
		child_node = ofw_tree_node_alloc();
100
		if (child_node) {
101
			ofw_tree_node_process(child_node, current_node, child);
102
			current_node->child = child_node;
103
		}
104
	}
105
 
106
	/*
107
	 * Count properties.
108
	 */
109
	name[0] = '\0';
110
	while (ofw_next_property(current, name, name) == 1)
111
		current_node->properties++;
112
 
113
	if (!current_node->properties)
114
		return;
115
 
116
	/*
117
	 * Copy properties.
118
	 */
119
	current_node->property = ofw_tree_properties_alloc(current_node->properties);
120
	if (!current_node->property)
121
		return;
122
 
123
	int i = 0;
124
 
125
	name[0] = '\0';
126
	for (i = 0; ofw_next_property(current, name, name) == 1; i++) {
127
		size_t size;
128
 
129
		if (i == current_node->properties)
130
			break;
131
 
132
		strncpy(current_node->property[i].name, name, sizeof(name));
133
 
134
		size = ofw_get_proplen(current, name);
135
		current_node->property[i].size = size;
136
		if (size) {
137
			void *buf;
138
 
139
			buf = ofw_tree_space_alloc(size);
140
			if (current_node->property[i].value = buf) {
141
				/*
142
				 * Copy property value to memory node.
143
				 */
144
				(void) ofw_get_property(current, name, buf, size);
145
			}
146
		} else {
147
			current_node->property[i].value = NULL;
148
		}
149
	}
150
 
151
	current_node->properties = i;	/* Just in case we ran out of memory. */
152
}
153
 
154
/** Construct memory representation of OpenFirmware device tree.
155
 *
156
 * @return NULL on failure or pointer to the root node.
157
 */
158
ofw_tree_node_t *ofw_tree_build(void)
159
{
160
	ofw_tree_node_t *root;
161
 
162
	root = ofw_tree_node_alloc();
163
	if (root)
164
		ofw_tree_node_process(root, NULL, ofw_root);
165
 
166
	return root;
167
}