2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * @OSF_FREE_COPYRIGHT@
26 #include <pexpert/protos.h>
27 #include <pexpert/boot.h>
28 #include <pexpert/device_tree.h>
30 #include <mach/mach_types.h>
31 #include <mach/machine/vm_types.h>
32 #include <kern/kern_types.h>
33 #include <kern/kalloc.h>
35 #include <sys/types.h>
37 #include <i386/fakePPCStructs.h>
41 #define NULL ((void *) 0)
44 #define round_long(x) (((x) + 3) & -4)
45 #define next_prop(x) ((DeviceTreeNodeProperty *) (((int)x) + sizeof(DeviceTreeNodeProperty) + round_long(x->length)))
48 typedef DeviceTreeNode
*RealDTEntry
;
50 typedef struct DTSavedScope
{
51 struct DTSavedScope
* nextScope
;
58 typedef struct OpaqueDTEntryIterator
{
59 RealDTEntry outerScope
;
60 RealDTEntry currentScope
;
61 RealDTEntry currentEntry
;
62 DTSavedScopePtr savedScope
;
63 unsigned long currentIndex
;
64 } *RealDTEntryIterator
;
66 /* Property Iterator*/
67 typedef struct OpaqueDTPropertyIterator
{
69 DeviceTreeNodeProperty
*currentProperty
;
70 unsigned long currentIndex
;
71 } *RealDTPropertyIterator
;
73 static int DTInitialized
;
74 static RealDTEntry DTRootNode
;
76 void DTInit(void *base
);
82 skipProperties(RealDTEntry entry
)
84 DeviceTreeNodeProperty
*prop
;
87 if (entry
== NULL
|| entry
->nProperties
== 0) {
90 prop
= (DeviceTreeNodeProperty
*) (entry
+ 1);
91 for (k
= 0; k
< entry
->nProperties
; k
++) {
92 prop
= next_prop(prop
);
95 return ((RealDTEntry
) prop
);
99 skipTree(RealDTEntry root
)
104 entry
= skipProperties(root
);
108 for (k
= 0; k
< root
->nChildren
; k
++) {
109 entry
= skipTree(entry
);
115 GetFirstChild(RealDTEntry parent
)
117 return skipProperties(parent
);
121 GetNextChild(RealDTEntry sibling
)
123 return skipTree(sibling
);
127 GetNextComponent(const char *cp
, char *bp
)
130 if (*cp
== kDTPathNameSeparator
) {
141 FindChild(RealDTEntry cur
, char *buf
)
148 if (cur
->nChildren
== 0) {
152 child
= GetFirstChild(cur
);
154 if (DTGetProperty(child
, "name", (void **)&str
, &dummy
) != kSuccess
) {
157 if (strcmp(str
, buf
) == 0) {
160 if (index
>= cur
->nChildren
) {
163 child
= GetNextChild(child
);
176 DTRootNode
= (RealDTEntry
) base
;
177 DTInitialized
= (DTRootNode
!= 0);
181 DTEntryIsEqual(const DTEntry ref1
, const DTEntry ref2
)
183 /* equality of pointers */
184 return (ref1
== ref2
);
187 static char *startingP
; // needed for find_entry
188 int find_entry(const char *propName
, const char *propValue
, DTEntry
*entryH
);
190 int DTFindEntry(const char *propName
, const char *propValue
, DTEntry
*entryH
)
192 if (!DTInitialized
) {
196 startingP
= (char *)DTRootNode
;
197 return(find_entry(propName
, propValue
, entryH
));
200 int find_entry(const char *propName
, const char *propValue
, DTEntry
*entryH
)
202 DeviceTreeNode
*nodeP
= (DeviceTreeNode
*) startingP
;
205 if (nodeP
->nProperties
== 0) return(kError
); // End of the list of nodes
206 startingP
= (char *) (nodeP
+ 1);
208 // Search current entry
209 for (k
= 0; k
< nodeP
->nProperties
; ++k
) {
210 DeviceTreeNodeProperty
*propP
= (DeviceTreeNodeProperty
*) startingP
;
212 startingP
+= sizeof (*propP
) + ((propP
->length
+ 3) & -4);
214 if (strcmp (propP
->name
, propName
) == 0) {
215 if (strcmp( (char *)(propP
+ 1), propValue
) == 0)
217 *entryH
= (DTEntry
)nodeP
;
223 // Search child nodes
224 for (k
= 0; k
< nodeP
->nChildren
; ++k
)
226 if (find_entry(propName
, propValue
, entryH
) == kSuccess
)
233 DTLookupEntry(const DTEntry searchPoint
, const char *pathName
, DTEntry
*foundEntry
)
239 if (!DTInitialized
) {
242 if (searchPoint
== NULL
) {
248 if (*cp
== kDTPathNameSeparator
) {
256 cp
= GetNextComponent(cp
, buf
);
267 cur
= FindChild(cur
, buf
);
269 } while (cur
!= NULL
);
275 DTCreateEntryIterator(const DTEntry startEntry
, DTEntryIterator
*iterator
)
277 RealDTEntryIterator iter
;
279 if (!DTInitialized
) {
283 iter
= (RealDTEntryIterator
) kalloc(sizeof(struct OpaqueDTEntryIterator
));
284 if (startEntry
!= NULL
) {
285 iter
->outerScope
= (RealDTEntry
) startEntry
;
286 iter
->currentScope
= (RealDTEntry
) startEntry
;
288 iter
->outerScope
= DTRootNode
;
289 iter
->currentScope
= DTRootNode
;
291 iter
->currentEntry
= NULL
;
292 iter
->savedScope
= NULL
;
293 iter
->currentIndex
= 0;
300 DTDisposeEntryIterator(DTEntryIterator iterator
)
302 RealDTEntryIterator iter
= iterator
;
303 DTSavedScopePtr scope
;
305 while ((scope
= iter
->savedScope
) != NULL
) {
306 iter
->savedScope
= scope
->nextScope
;
307 kfree(scope
, sizeof(struct DTSavedScope
));
309 kfree(iterator
, sizeof(struct OpaqueDTEntryIterator
));
314 DTEnterEntry(DTEntryIterator iterator
, DTEntry childEntry
)
316 RealDTEntryIterator iter
= iterator
;
317 DTSavedScopePtr newScope
;
319 if (childEntry
== NULL
) {
322 newScope
= (DTSavedScopePtr
) kalloc(sizeof(struct DTSavedScope
));
323 newScope
->nextScope
= iter
->savedScope
;
324 newScope
->scope
= iter
->currentScope
;
325 newScope
->entry
= iter
->currentEntry
;
326 newScope
->index
= iter
->currentIndex
;
328 iter
->currentScope
= childEntry
;
329 iter
->currentEntry
= NULL
;
330 iter
->savedScope
= newScope
;
331 iter
->currentIndex
= 0;
337 DTExitEntry(DTEntryIterator iterator
, DTEntry
*currentPosition
)
339 RealDTEntryIterator iter
= iterator
;
340 DTSavedScopePtr newScope
;
342 newScope
= iter
->savedScope
;
343 if (newScope
== NULL
) {
346 iter
->savedScope
= newScope
->nextScope
;
347 iter
->currentScope
= newScope
->scope
;
348 iter
->currentEntry
= newScope
->entry
;
349 iter
->currentIndex
= newScope
->index
;
350 *currentPosition
= iter
->currentEntry
;
352 kfree(newScope
, sizeof(struct DTSavedScope
));
358 DTIterateEntries(DTEntryIterator iterator
, DTEntry
*nextEntry
)
360 RealDTEntryIterator iter
= iterator
;
362 if (iter
->currentIndex
>= iter
->currentScope
->nChildren
) {
364 return kIterationDone
;
366 iter
->currentIndex
++;
367 if (iter
->currentIndex
== 1) {
368 iter
->currentEntry
= GetFirstChild(iter
->currentScope
);
370 iter
->currentEntry
= GetNextChild(iter
->currentEntry
);
372 *nextEntry
= iter
->currentEntry
;
378 DTRestartEntryIteration(DTEntryIterator iterator
)
380 RealDTEntryIterator iter
= iterator
;
382 // This commented out code allows a second argument (outer)
383 // which (if true) causes restarting at the outer scope
384 // rather than the current scope.
385 DTSavedScopePtr scope
;
388 while ((scope
= iter
->savedScope
) != NULL
) {
389 iter
->savedScope
= scope
->nextScope
;
390 kfree((vm_offset_t
) scope
, sizeof(struct DTSavedScope
));
392 iter
->currentScope
= iter
->outerScope
;
395 iter
->currentEntry
= NULL
;
396 iter
->currentIndex
= 0;
401 DTGetProperty(const DTEntry entry
, const char *propertyName
, void **propertyValue
, int *propertySize
)
403 DeviceTreeNodeProperty
*prop
;
406 if (entry
== NULL
|| entry
->nProperties
== 0) {
409 prop
= (DeviceTreeNodeProperty
*) (entry
+ 1);
410 for (k
= 0; k
< entry
->nProperties
; k
++) {
411 if (strcmp(prop
->name
, propertyName
) == 0) {
412 *propertyValue
= (void *) (((int)prop
)
413 + sizeof(DeviceTreeNodeProperty
));
414 *propertySize
= prop
->length
;
417 prop
= next_prop(prop
);
424 DTCreatePropertyIterator(const DTEntry entry
, DTPropertyIterator
*iterator
)
426 RealDTPropertyIterator iter
;
428 iter
= (RealDTPropertyIterator
) kalloc(sizeof(struct OpaqueDTPropertyIterator
));
430 iter
->currentProperty
= NULL
;
431 iter
->currentIndex
= 0;
438 DTDisposePropertyIterator(DTPropertyIterator iterator
)
440 kfree(iterator
, sizeof(struct OpaqueDTPropertyIterator
));
445 DTIterateProperties(DTPropertyIterator iterator
, char **foundProperty
)
447 RealDTPropertyIterator iter
= iterator
;
449 if (iter
->currentIndex
>= iter
->entry
->nProperties
) {
450 *foundProperty
= NULL
;
451 return kIterationDone
;
453 iter
->currentIndex
++;
454 if (iter
->currentIndex
== 1) {
455 iter
->currentProperty
= (DeviceTreeNodeProperty
*) (iter
->entry
+ 1);
457 iter
->currentProperty
= next_prop(iter
->currentProperty
);
459 *foundProperty
= iter
->currentProperty
->name
;
465 DTRestartPropertyIteration(DTPropertyIterator iterator
)
467 RealDTPropertyIterator iter
= iterator
;
469 iter
->currentProperty
= NULL
;
470 iter
->currentIndex
= 0;