File: device_tree.c

package info (click to toggle)
mol 0.9.61-6
  • links: PTS
  • area: contrib
  • in suites: woody
  • size: 6,140 kB
  • ctags: 8,491
  • sloc: ansic: 50,560; asm: 2,826; sh: 458; makefile: 373; perl: 165; lex: 135; yacc: 131
file content (238 lines) | stat: -rw-r--r-- 5,541 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* 
 *   MOL adaption of BootX, 11/01/2201
 *
 *   Samuel Rydh, <samuel@ibrium.se>
 *
 */
/*
 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
 *
 * @APPLE_LICENSE_HEADER_START@
 * 
 * The contents of this file constitute Original Code as defined in and
 * are subject to the Apple Public Source License Version 1.1 (the
 * "License").  You may not use this file except in compliance with the
 * License.  Please obtain a copy of the License at
 * http://www.apple.com/publicsource and read it before using this file.
 * 
 * This Original Code and all software distributed under the License are
 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
 * License for the specific language governing rights and limitations
 * under the License.
 * 
 * @APPLE_LICENSE_HEADER_END@
 */
/*
 *  device_tree.c - Functions for flattening the Device Tree.
 *
 *  Copyright (c) 1998-2000 Apple Computer, Inc.
 *
 *  DRI: Josh de Cesare
 */

#include "mol_config.h"

#include "bootx.h"
#include "sl.h"
#include "device_tree.h"
#include "promif.h"
#include "debugger.h"

typedef mol_device_node_t *CICell;

static void FlatenNode(mol_device_node_t *dn, long nodeAddr, long *nodeSize);
static void FlatenProps(mol_device_node_t *ph, long propAddr, long *propSize,
			long *numProps);

// Public Functions

void 
FlattenDeviceTree(void)
{
	gBootX.deviceTreeAddr = AllocateKernelMemory(0);
	
	FlatenNode(prom_get_root(), gBootX.deviceTreeAddr, &gBootX.deviceTreeSize);
	
	AllocateKernelMemory(gBootX.deviceTreeSize);
}


#if 0
CICell 
SearchForNode(CICell ph, long top, char *prop, char *value)
{
	CICell curChild, result;
	
	if (ph == 0) ph = Peer(0);
	
	if (top == 0) {
		// Look for it in the current node.
		if (GetProp(ph, prop, gTempStr, 4095) != -1) {
			if (strcmp(value, gTempStr) == 0) {
				return ph;
			}
		}
	}
	
	// Look for it in the children.
	curChild = Child(ph);
	
	while (curChild != 0) {
		result = SearchForNode(curChild, 0, prop, value);
		if (result != 0) return result;
		curChild = Peer(curChild);
	}
	
	if (top != 0) {
		while (ph != 0) {
			curChild = Peer(ph);
			while (curChild != 0) {
				result = SearchForNode(curChild, 0, prop, value);
				if (result != 0) return result;
				curChild = Peer(curChild);
			}
			
			ph = Parent(ph);
		}
	}
	
	return 0;
}


CICell
SearchForNodeMatching(CICell ph, long top, char *value)
{
	CICell curChild, result;
	
	if (ph == 0) ph = Peer(0);
	
	if (top == 0) {
		// Look for it in the current node.
		if (MatchThis(ph, value) == 0) return ph;
	}
	
	// Look for it in the children.
	curChild = Child(ph);
	
	while (curChild != 0) {
		result = SearchForNodeMatching(curChild, 0, value);
		if (result != 0) return result;
		curChild = Peer(curChild);
	}
	
	if (top != 0) {
		while (ph != 0) {
			curChild = Peer(ph);
			while (curChild != 0) {
				result = SearchForNodeMatching(curChild, 0, value);
				if (result != 0) return result;
				curChild = Peer(curChild);
			}
			
			ph = Parent(ph);
		}
	}
	
	return 0;
}
#endif

// Private Functions

void
FlatenNode(mol_device_node_t *dn, long nodeAddr, long *nodeSize)
{
	DTNodePtr node;
	CICell    childDN;
	long      curAddr;
	long      propSize, numProps, childSize, numChildren;
	
	node = (DTNodePtr)nodeAddr;
	curAddr = nodeAddr + sizeof(DTNode);
	numProps = 0;
	numChildren = 0;
	
	FlatenProps(dn, curAddr, &propSize, &numProps);
	
	curAddr += propSize;
	node->nProperties = numProps;
	
	childDN = dn->child;
	while( childDN != 0) {
		FlatenNode(childDN, curAddr, &childSize);
		curAddr += childSize;
		numChildren++;
		
		childDN = childDN->sibling;
	}
	
	node->nChildren = numChildren;
	*nodeSize = curAddr - nodeAddr;
}


void 
FlatenProps(mol_device_node_t *dn, long propAddr, long *propSize, long *numProps)
{
	DTPropertyPtr prop;
	long          ret, cnt, curAddr, valueAddr, nProps;
	p_property_t  *p;
	
	curAddr = propAddr;
	nProps = 0;
	
	// make the first property the phandle
	prop = (DTPropertyPtr)curAddr;
	valueAddr = curAddr + sizeof(DTProperty);
	strcpy(prop->name, "AAPL,phandle");
	*((long *)valueAddr) = prom_dn_to_phandle(dn);
	prop->length = 4;
	curAddr = valueAddr + 4;
	nProps++;
	
	// Make the AAPL,unit-string property.

	ret = strlen(dn->full_name)+1;
	// ret = PackageToPath(ph, gTmpStr, 4095);
	if (ret > 0) {
		cnt = ret - 1;
		while( cnt && (dn->full_name[cnt - 1] != '@') && (dn->full_name[cnt - 1] != '/'))
			cnt--;
		
		if( dn->full_name[cnt - 1] == '@') {
			prop = (DTPropertyPtr)curAddr;
			valueAddr = curAddr + sizeof(DTProperty);
			strcpy(prop->name, "AAPL,unit-string");
			strcpy((char *)valueAddr, &dn->full_name[cnt]);
			prop->length = ret - cnt;
			curAddr = valueAddr + ((prop->length + 3) & ~3);
			nProps++;
		}
	}
	
	for( p=dn->properties; p; p=p->next ) {
		prop = (DTPropertyPtr)curAddr;
		strncpy( prop->name, p->name, sizeof(prop->name) );
		prop->length = p->length;
		valueAddr = curAddr + sizeof(DTProperty);
		memcpy( (char*)valueAddr, p->value, prop->length );
		
		// Save the address of the value if this is
		// the memory map property for the device tree.
		if( (prom_dn_to_phandle(dn) == gBootX.memoryMapPH) && !strcmp(prop->name, "DeviceTree")) {
			gBootX.deviceTreeMMTmp = (long *)valueAddr;
		}

		nProps++;
		curAddr = valueAddr + ((prop->length + 3) & ~3);
	};
	
	*numProps = nProps;
	*propSize = curAddr - propAddr;
}