]>
git.saurik.com Git - apple/bootx.git/blob - bootx.tproj/sl.subproj/device_tree.c
0cc89949897934c8a796fcdd1efc6f060f6aec5c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * device_tree.c - Functions for flattening the Device Tree.
28 * Copyright (c) 1998-2000 Apple Computer, Inc.
34 #include <device_tree.h>
37 static long FlatenNode(CICell ph
, long nodeAddr
, long *nodeSize
);
38 static long FlatenProps(CICell ph
, long propAddr
, long *propSize
,
43 long FlattenDeviceTree(void)
48 gDeviceTreeAddr
= AllocateKernelMemory(0);
51 if (RootPH
== kCIError
) return -1;
53 ret
= FlatenNode(RootPH
, gDeviceTreeAddr
, &gDeviceTreeSize
);
55 AllocateKernelMemory(gDeviceTreeSize
);
61 CICell
SearchForNode(CICell ph
, long top
, char *prop
, char *value
)
63 CICell curChild
, result
;
65 if (ph
== 0) ph
= Peer(0);
68 // Look for it in the current node.
69 if (GetProp(ph
, prop
, gTempStr
, 4095) != -1) {
70 if (strcmp(value
, gTempStr
) == 0) {
76 // Look for it in the children.
79 while (curChild
!= 0) {
80 result
= SearchForNode(curChild
, 0, prop
, value
);
81 if (result
!= 0) return result
;
82 curChild
= Peer(curChild
);
88 while (curChild
!= 0) {
89 result
= SearchForNode(curChild
, 0, prop
, value
);
90 if (result
!= 0) return result
;
91 curChild
= Peer(curChild
);
102 CICell
SearchForNodeMatching(CICell ph
, long top
, char *value
)
104 CICell curChild
, result
;
106 if (ph
== 0) ph
= Peer(0);
109 // Look for it in the current node.
110 if (MatchThis(ph
, value
) == 0) return ph
;
113 // Look for it in the children.
114 curChild
= Child(ph
);
116 while (curChild
!= 0) {
117 result
= SearchForNodeMatching(curChild
, 0, value
);
118 if (result
!= 0) return result
;
119 curChild
= Peer(curChild
);
125 while (curChild
!= 0) {
126 result
= SearchForNodeMatching(curChild
, 0, value
);
127 if (result
!= 0) return result
;
128 curChild
= Peer(curChild
);
140 long FlatenNode(CICell ph
, long nodeAddr
, long *nodeSize
)
145 long propSize
, numProps
, childSize
, numChildren
;
147 node
= (DTNodePtr
)nodeAddr
;
148 curAddr
= nodeAddr
+ sizeof(DTNode
);
152 ret
= FlatenProps(ph
, curAddr
, &propSize
, &numProps
);
153 if (ret
!= 0) return ret
;
156 node
->nProperties
= numProps
;
159 if (childPH
== kCIError
) return -1;
161 while (childPH
!= 0) {
162 ret
= FlatenNode(childPH
, curAddr
, &childSize
);
163 curAddr
+= childSize
;
166 childPH
= Peer(childPH
);
167 if (childPH
== -1) return -1;
170 node
->nChildren
= numChildren
;
171 *nodeSize
= curAddr
- nodeAddr
;
177 long FlatenProps(CICell ph
, long propAddr
, long *propSize
, long *numProps
)
180 long ret
, cnt
, curAddr
, valueAddr
, valueSize
, nProps
;
187 // make the first property the phandle
188 prop
= (DTPropertyPtr
)curAddr
;
189 valueAddr
= curAddr
+ sizeof(DTProperty
);
190 strcpy(prop
->name
, "AAPL,phandle");
191 *((long *)valueAddr
) = ph
;
193 curAddr
= valueAddr
+ 4;
196 // Make the AAPL,unit-string property.
197 ret
= PackageToPath(ph
, gTempStr
, 4095);
200 while (cnt
&& (gTempStr
[cnt
- 1] != '@') &&
201 (gTempStr
[cnt
- 1] != '/')) cnt
--;
203 if (gTempStr
[cnt
- 1] == '@') {
204 prop
= (DTPropertyPtr
)curAddr
;
205 valueAddr
= curAddr
+ sizeof(DTProperty
);
206 strcpy(prop
->name
, "AAPL,unit-string");
207 strcpy((char *)valueAddr
, &gTempStr
[cnt
]);
208 prop
->length
= ret
- cnt
;
209 curAddr
= valueAddr
+ ((prop
->length
+ 3) & ~3);
215 prop
= (DTPropertyPtr
)curAddr
;
216 valueAddr
= curAddr
+ sizeof(DTProperty
);
218 ret
= NextProp(ph
, prevName
, prop
->name
);
219 if (ret
== -1) return -1;
222 valueSize
= GetProp(ph
, prop
->name
, (char *)valueAddr
,
223 kPropValueMaxLength
);
224 if (valueSize
== -1) return -1;
225 prop
->length
= valueSize
;
227 // Save the address of the value if this is
228 // the memory map property for the device tree.
229 if ((ph
== gMemoryMapPH
) && !strcmp(prop
->name
, "DeviceTree")) {
230 gDeviceTreeMMTmp
= (long *)valueAddr
;
234 curAddr
= valueAddr
+ ((valueSize
+ 3) & ~3);
235 prevName
= prop
->name
;
239 *propSize
= curAddr
- propAddr
;