2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * @OSF_FREE_COPYRIGHT@
32 #include <pexpert/protos.h>
33 #include <pexpert/boot.h>
34 #include <pexpert/device_tree.h>
36 #include <mach/mach_types.h>
37 #include <mach/machine/vm_types.h>
38 #include <kern/kern_types.h>
39 #include <kern/kalloc.h>
41 #include <sys/types.h>
43 #include <i386/fakePPCStructs.h>
47 #define NULL ((void *) 0)
50 #define round_long(x) (((x) + 3) & -4)
51 #define next_prop(x) ((DeviceTreeNodeProperty *) (((int)x) + sizeof(DeviceTreeNodeProperty) + round_long(x->length)))
54 typedef DeviceTreeNode
*RealDTEntry
;
56 typedef struct DTSavedScope
{
57 struct DTSavedScope
* nextScope
;
64 typedef struct OpaqueDTEntryIterator
{
65 RealDTEntry outerScope
;
66 RealDTEntry currentScope
;
67 RealDTEntry currentEntry
;
68 DTSavedScopePtr savedScope
;
69 unsigned long currentIndex
;
70 } *RealDTEntryIterator
;
72 /* Property Iterator*/
73 typedef struct OpaqueDTPropertyIterator
{
75 DeviceTreeNodeProperty
*currentProperty
;
76 unsigned long currentIndex
;
77 } *RealDTPropertyIterator
;
79 static int DTInitialized
;
80 static RealDTEntry DTRootNode
;
82 void DTInit(void *base
);
88 skipProperties(RealDTEntry entry
)
90 DeviceTreeNodeProperty
*prop
;
93 if (entry
== NULL
|| entry
->nProperties
== 0) {
96 prop
= (DeviceTreeNodeProperty
*) (entry
+ 1);
97 for (k
= 0; k
< entry
->nProperties
; k
++) {
98 prop
= next_prop(prop
);
101 return ((RealDTEntry
) prop
);
105 skipTree(RealDTEntry root
)
110 entry
= skipProperties(root
);
114 for (k
= 0; k
< root
->nChildren
; k
++) {
115 entry
= skipTree(entry
);
121 GetFirstChild(RealDTEntry parent
)
123 return skipProperties(parent
);
127 GetNextChild(RealDTEntry sibling
)
129 return skipTree(sibling
);
133 GetNextComponent(const char *cp
, char *bp
)
136 if (*cp
== kDTPathNameSeparator
) {
147 FindChild(RealDTEntry cur
, char *buf
)
154 if (cur
->nChildren
== 0) {
158 child
= GetFirstChild(cur
);
160 if (DTGetProperty(child
, "name", (void **)&str
, &dummy
) != kSuccess
) {
163 if (strcmp(str
, buf
) == 0) {
166 if (index
>= cur
->nChildren
) {
169 child
= GetNextChild(child
);
182 DTRootNode
= (RealDTEntry
) base
;
183 DTInitialized
= (DTRootNode
!= 0);
187 DTEntryIsEqual(const DTEntry ref1
, const DTEntry ref2
)
189 /* equality of pointers */
190 return (ref1
== ref2
);
193 static char *startingP
; // needed for find_entry
194 int find_entry(const char *propName
, const char *propValue
, DTEntry
*entryH
);
196 int DTFindEntry(const char *propName
, const char *propValue
, DTEntry
*entryH
)
198 if (!DTInitialized
) {
202 startingP
= (char *)DTRootNode
;
203 return(find_entry(propName
, propValue
, entryH
));
206 int find_entry(const char *propName
, const char *propValue
, DTEntry
*entryH
)
208 DeviceTreeNode
*nodeP
= (DeviceTreeNode
*) startingP
;
211 if (nodeP
->nProperties
== 0) return(kError
); // End of the list of nodes
212 startingP
= (char *) (nodeP
+ 1);
214 // Search current entry
215 for (k
= 0; k
< nodeP
->nProperties
; ++k
) {
216 DeviceTreeNodeProperty
*propP
= (DeviceTreeNodeProperty
*) startingP
;
218 startingP
+= sizeof (*propP
) + ((propP
->length
+ 3) & -4);
220 if (strcmp (propP
->name
, propName
) == 0) {
221 if (strcmp( (char *)(propP
+ 1), propValue
) == 0)
223 *entryH
= (DTEntry
)nodeP
;
229 // Search child nodes
230 for (k
= 0; k
< nodeP
->nChildren
; ++k
)
232 if (find_entry(propName
, propValue
, entryH
) == kSuccess
)
239 DTLookupEntry(const DTEntry searchPoint
, const char *pathName
, DTEntry
*foundEntry
)
245 if (!DTInitialized
) {
248 if (searchPoint
== NULL
) {
254 if (*cp
== kDTPathNameSeparator
) {
262 cp
= GetNextComponent(cp
, buf
);
273 cur
= FindChild(cur
, buf
);
275 } while (cur
!= NULL
);
281 DTCreateEntryIterator(const DTEntry startEntry
, DTEntryIterator
*iterator
)
283 RealDTEntryIterator iter
;
285 if (!DTInitialized
) {
289 iter
= (RealDTEntryIterator
) kalloc(sizeof(struct OpaqueDTEntryIterator
));
290 if (startEntry
!= NULL
) {
291 iter
->outerScope
= (RealDTEntry
) startEntry
;
292 iter
->currentScope
= (RealDTEntry
) startEntry
;
294 iter
->outerScope
= DTRootNode
;
295 iter
->currentScope
= DTRootNode
;
297 iter
->currentEntry
= NULL
;
298 iter
->savedScope
= NULL
;
299 iter
->currentIndex
= 0;
306 DTDisposeEntryIterator(DTEntryIterator iterator
)
308 RealDTEntryIterator iter
= iterator
;
309 DTSavedScopePtr scope
;
311 while ((scope
= iter
->savedScope
) != NULL
) {
312 iter
->savedScope
= scope
->nextScope
;
313 kfree(scope
, sizeof(struct DTSavedScope
));
315 kfree(iterator
, sizeof(struct OpaqueDTEntryIterator
));
320 DTEnterEntry(DTEntryIterator iterator
, DTEntry childEntry
)
322 RealDTEntryIterator iter
= iterator
;
323 DTSavedScopePtr newScope
;
325 if (childEntry
== NULL
) {
328 newScope
= (DTSavedScopePtr
) kalloc(sizeof(struct DTSavedScope
));
329 newScope
->nextScope
= iter
->savedScope
;
330 newScope
->scope
= iter
->currentScope
;
331 newScope
->entry
= iter
->currentEntry
;
332 newScope
->index
= iter
->currentIndex
;
334 iter
->currentScope
= childEntry
;
335 iter
->currentEntry
= NULL
;
336 iter
->savedScope
= newScope
;
337 iter
->currentIndex
= 0;
343 DTExitEntry(DTEntryIterator iterator
, DTEntry
*currentPosition
)
345 RealDTEntryIterator iter
= iterator
;
346 DTSavedScopePtr newScope
;
348 newScope
= iter
->savedScope
;
349 if (newScope
== NULL
) {
352 iter
->savedScope
= newScope
->nextScope
;
353 iter
->currentScope
= newScope
->scope
;
354 iter
->currentEntry
= newScope
->entry
;
355 iter
->currentIndex
= newScope
->index
;
356 *currentPosition
= iter
->currentEntry
;
358 kfree(newScope
, sizeof(struct DTSavedScope
));
364 DTIterateEntries(DTEntryIterator iterator
, DTEntry
*nextEntry
)
366 RealDTEntryIterator iter
= iterator
;
368 if (iter
->currentIndex
>= iter
->currentScope
->nChildren
) {
370 return kIterationDone
;
372 iter
->currentIndex
++;
373 if (iter
->currentIndex
== 1) {
374 iter
->currentEntry
= GetFirstChild(iter
->currentScope
);
376 iter
->currentEntry
= GetNextChild(iter
->currentEntry
);
378 *nextEntry
= iter
->currentEntry
;
384 DTRestartEntryIteration(DTEntryIterator iterator
)
386 RealDTEntryIterator iter
= iterator
;
388 // This commented out code allows a second argument (outer)
389 // which (if true) causes restarting at the outer scope
390 // rather than the current scope.
391 DTSavedScopePtr scope
;
394 while ((scope
= iter
->savedScope
) != NULL
) {
395 iter
->savedScope
= scope
->nextScope
;
396 kfree((vm_offset_t
) scope
, sizeof(struct DTSavedScope
));
398 iter
->currentScope
= iter
->outerScope
;
401 iter
->currentEntry
= NULL
;
402 iter
->currentIndex
= 0;
407 DTGetProperty(const DTEntry entry
, const char *propertyName
, void **propertyValue
, int *propertySize
)
409 DeviceTreeNodeProperty
*prop
;
412 if (entry
== NULL
|| entry
->nProperties
== 0) {
415 prop
= (DeviceTreeNodeProperty
*) (entry
+ 1);
416 for (k
= 0; k
< entry
->nProperties
; k
++) {
417 if (strcmp(prop
->name
, propertyName
) == 0) {
418 *propertyValue
= (void *) (((int)prop
)
419 + sizeof(DeviceTreeNodeProperty
));
420 *propertySize
= prop
->length
;
423 prop
= next_prop(prop
);
430 DTCreatePropertyIterator(const DTEntry entry
, DTPropertyIterator
*iterator
)
432 RealDTPropertyIterator iter
;
434 iter
= (RealDTPropertyIterator
) kalloc(sizeof(struct OpaqueDTPropertyIterator
));
436 iter
->currentProperty
= NULL
;
437 iter
->currentIndex
= 0;
444 DTDisposePropertyIterator(DTPropertyIterator iterator
)
446 kfree(iterator
, sizeof(struct OpaqueDTPropertyIterator
));
451 DTIterateProperties(DTPropertyIterator iterator
, char **foundProperty
)
453 RealDTPropertyIterator iter
= iterator
;
455 if (iter
->currentIndex
>= iter
->entry
->nProperties
) {
456 *foundProperty
= NULL
;
457 return kIterationDone
;
459 iter
->currentIndex
++;
460 if (iter
->currentIndex
== 1) {
461 iter
->currentProperty
= (DeviceTreeNodeProperty
*) (iter
->entry
+ 1);
463 iter
->currentProperty
= next_prop(iter
->currentProperty
);
465 *foundProperty
= iter
->currentProperty
->name
;
471 DTRestartPropertyIteration(DTPropertyIterator iterator
)
473 RealDTPropertyIterator iter
= iterator
;
475 iter
->currentProperty
= NULL
;
476 iter
->currentIndex
= 0;