2 * Copyright (c) 1998-2006 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 * Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
32 * 12 Nov 98 sdouglas created.
36 #include <IOKit/IORegistryEntry.h>
37 #include <libkern/c++/OSContainers.h>
38 #include <IOKit/IOService.h>
39 #include <IOKit/IOKitKeys.h>
41 #include <IOKit/IOLib.h>
43 #include <IOKit/assert.h>
45 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
47 #define super OSObject
49 OSDefineMetaClassAndStructors(IORegistryEntry
, OSObject
)
51 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
53 #define kIORegPlaneParentSuffix "ParentLinks"
54 #define kIORegPlaneChildSuffix "ChildLinks"
55 #define kIORegPlaneNameSuffix "Name"
56 #define kIORegPlaneLocationSuffix "Location"
58 #define kIORegPlaneParentSuffixLen (sizeof(kIORegPlaneParentSuffix) - 1)
59 #define kIORegPlaneChildSuffixLen (sizeof(kIORegPlaneChildSuffix) - 1)
60 #define kIORegPlaneNameSuffixLen (sizeof(kIORegPlaneNameSuffix) - 1)
61 #define kIORegPlaneLocationSuffixLen (sizeof(kIORegPlaneLocationSuffix) - 1)
63 #define KASLR_IOREG_DEBUG 0
65 static IORegistryEntry
* gRegistryRoot
;
66 static OSDictionary
* gIORegistryPlanes
;
68 const OSSymbol
* gIONameKey
;
69 const OSSymbol
* gIOLocationKey
;
70 const OSSymbol
* gIORegistryEntryIDKey
;
81 enum { kIORegistryIDReserved
= (1ULL << 32) + 255 };
83 static uint64_t gIORegistryLastID
= kIORegistryIDReserved
;
85 class IORegistryPlane
: public OSObject
{
87 friend class IORegistryEntry
;
89 OSDeclareAbstractStructors(IORegistryPlane
)
91 const OSSymbol
* nameKey
;
92 const OSSymbol
* keys
[ kNumSetIndex
];
93 const OSSymbol
* pathNameKey
;
94 const OSSymbol
* pathLocationKey
;
98 virtual bool serialize(OSSerialize
*s
) const;
101 OSDefineMetaClassAndStructors(IORegistryPlane
, OSObject
)
104 static IORecursiveLock
* gPropertiesLock
;
105 static SInt32 gIORegistryGenerationCount
;
107 #define UNLOCK lck_rw_done( &gIORegistryLock )
108 #define RLOCK lck_rw_lock_shared( &gIORegistryLock )
109 #define WLOCK lck_rw_lock_exclusive( &gIORegistryLock ); \
110 gIORegistryGenerationCount++
113 #define PUNLOCK IORecursiveLockUnlock( gPropertiesLock )
114 #define PLOCK IORecursiveLockLock( gPropertiesLock )
116 #define IOREGSPLITTABLES
118 #ifdef IOREGSPLITTABLES
119 #define registryTable() fRegistryTable
121 #define registryTable() fPropertyTable
126 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
128 lck_rw_t gIORegistryLock
;
129 lck_grp_t
*gIORegistryLockGrp
;
130 lck_grp_attr_t
*gIORegistryLockGrpAttr
;
131 lck_attr_t
*gIORegistryLockAttr
;
134 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
136 IORegistryEntry
* IORegistryEntry::initialize( void )
140 if( !gRegistryRoot
) {
143 gIORegistryLockGrpAttr
= lck_grp_attr_alloc_init();
144 //lck_grp_attr_setstat(gIORegistryLockGrpAttr);
145 gIORegistryLockGrp
= lck_grp_alloc_init("IORegistryLock", gIORegistryLockGrpAttr
);
146 gIORegistryLockAttr
= lck_attr_alloc_init();
147 lck_attr_rw_shared_priority(gIORegistryLockAttr
);
148 //lck_attr_setdebug(gIORegistryLockAttr);
149 lck_rw_init( &gIORegistryLock
, gIORegistryLockGrp
, gIORegistryLockAttr
);
151 gRegistryRoot
= new IORegistryEntry
;
152 gPropertiesLock
= IORecursiveLockAlloc();
153 gIORegistryPlanes
= OSDictionary::withCapacity( 1 );
155 assert( gRegistryRoot
&& gPropertiesLock
156 && gIORegistryPlanes
);
157 ok
= gRegistryRoot
->init();
160 gRegistryRoot
->reserved
->fRegistryEntryID
= ++gIORegistryLastID
;
162 gIONameKey
= OSSymbol::withCStringNoCopy( "IOName" );
163 gIOLocationKey
= OSSymbol::withCStringNoCopy( "IOLocation" );
164 gIORegistryEntryIDKey
= OSSymbol::withCStringNoCopy( kIORegistryEntryIDKey
);
166 assert( ok
&& gIONameKey
&& gIOLocationKey
);
168 gRegistryRoot
->setName( "Root" );
169 gRegistryRoot
->setProperty( kIORegistryPlanesKey
, gIORegistryPlanes
);
172 return( gRegistryRoot
);
175 IORegistryEntry
* IORegistryEntry::getRegistryRoot( void )
177 return( gRegistryRoot
);
180 SInt32
IORegistryEntry::getGenerationCount( void )
182 return( gIORegistryGenerationCount
);
186 const IORegistryPlane
* IORegistryEntry::makePlane( const char * name
)
188 IORegistryPlane
* plane
;
189 const OSSymbol
* nameKey
;
190 const OSSymbol
* parentKey
;
191 const OSSymbol
* childKey
;
192 const OSSymbol
* pathNameKey
;
193 const OSSymbol
* pathLocationKey
;
194 char key
[ kIOMaxPlaneName
+ 16 ];
197 strlcpy( key
, name
, kIOMaxPlaneName
+ 1 );
198 end
= key
+ strlen( key
);
200 nameKey
= OSSymbol::withCString( key
);
202 strlcpy( end
, kIORegPlaneParentSuffix
, kIORegPlaneParentSuffixLen
+ 1 );
203 parentKey
= OSSymbol::withCString( key
);
205 strlcpy( end
, kIORegPlaneChildSuffix
, kIORegPlaneChildSuffixLen
+ 1 );
206 childKey
= OSSymbol::withCString( key
);
208 strlcpy( end
, kIORegPlaneNameSuffix
, kIORegPlaneNameSuffixLen
+ 1 );
209 pathNameKey
= OSSymbol::withCString( key
);
211 strlcpy( end
, kIORegPlaneLocationSuffix
, kIORegPlaneLocationSuffixLen
+ 1 );
212 pathLocationKey
= OSSymbol::withCString( key
);
214 plane
= new IORegistryPlane
;
216 if( plane
&& plane
->init()
217 && nameKey
&& parentKey
&& childKey
218 && pathNameKey
&& pathLocationKey
) {
220 plane
->nameKey
= nameKey
;
221 plane
->keys
[ kParentSetIndex
] = parentKey
;
222 plane
->keys
[ kChildSetIndex
] = childKey
;
223 plane
->pathNameKey
= pathNameKey
;
224 plane
->pathLocationKey
= pathLocationKey
;
227 gIORegistryPlanes
->setObject( nameKey
, plane
);
235 pathLocationKey
->release();
237 pathNameKey
->release();
239 parentKey
->release();
250 const IORegistryPlane
* IORegistryEntry::getPlane( const char * name
)
252 const IORegistryPlane
* plane
;
255 plane
= (const IORegistryPlane
*) gIORegistryPlanes
->getObject( name
);
261 bool IORegistryPlane::serialize(OSSerialize
*s
) const
263 return( nameKey
->serialize(s
) );
266 enum { kIORegCapacityIncrement
= 4 };
268 bool IORegistryEntry::init( OSDictionary
* dict
)
277 reserved
= IONew(ExpansionData
, 1);
280 bzero(reserved
, sizeof(ExpansionData
));
283 if (OSCollection::kImmutable
& dict
->setOptions(0, 0)) {
284 dict
= (OSDictionary
*) dict
->copyCollection();
290 fPropertyTable
->release();
291 fPropertyTable
= dict
;
293 } else if( !fPropertyTable
) {
294 fPropertyTable
= OSDictionary::withCapacity( kIORegCapacityIncrement
);
296 fPropertyTable
->setCapacityIncrement( kIORegCapacityIncrement
);
302 #ifdef IOREGSPLITTABLES
303 if( !fRegistryTable
) {
304 fRegistryTable
= OSDictionary::withCapacity( kIORegCapacityIncrement
);
306 fRegistryTable
->setCapacityIncrement( kIORegCapacityIncrement
);
309 if( (prop
= OSDynamicCast( OSString
, getProperty( gIONameKey
)))) {
310 OSSymbol
* sym
= (OSSymbol
*)OSSymbol::withString( prop
);
311 // ok for OSSymbol too
316 #endif /* IOREGSPLITTABLES */
321 bool IORegistryEntry::init( IORegistryEntry
* old
,
322 const IORegistryPlane
* plane
)
325 IORegistryEntry
* next
;
333 reserved
= old
->reserved
;
334 old
->reserved
= NULL
;
336 fPropertyTable
= old
->getPropertyTable();
337 fPropertyTable
->retain();
338 #ifdef IOREGSPLITTABLES
339 fRegistryTable
= old
->fRegistryTable
;
340 old
->fRegistryTable
= (OSDictionary
*) fRegistryTable
->copyCollection();
341 #endif /* IOREGSPLITTABLES */
343 old
->registryTable()->removeObject( plane
->keys
[ kParentSetIndex
] );
344 old
->registryTable()->removeObject( plane
->keys
[ kChildSetIndex
] );
346 all
= getParentSetReference( plane
);
347 if( all
) for( index
= 0;
348 (next
= (IORegistryEntry
*) all
->getObject(index
));
350 next
->makeLink( this, kChildSetIndex
, plane
);
351 next
->breakLink( old
, kChildSetIndex
, plane
);
354 all
= getChildSetReference( plane
);
355 if( all
) for( index
= 0;
356 (next
= (IORegistryEntry
*) all
->getObject(index
));
358 next
->makeLink( this, kParentSetIndex
, plane
);
359 next
->breakLink( old
, kParentSetIndex
, plane
);
367 void IORegistryEntry::free( void )
370 if( registryTable() && gIOServicePlane
) {
371 if( getParentSetReference( gIOServicePlane
)
372 || getChildSetReference( gIOServicePlane
)) {
373 panic("%s: attached at free()", getName());
378 if( getPropertyTable())
379 getPropertyTable()->release();
381 #ifdef IOREGSPLITTABLES
383 registryTable()->release();
384 #endif /* IOREGSPLITTABLES */
387 IODelete(reserved
, ExpansionData
, 1);
392 void IORegistryEntry::setPropertyTable( OSDictionary
* dict
)
395 fPropertyTable
->release();
398 fPropertyTable
= dict
;
401 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
403 /* Wrappers to synchronize property table */
405 #define wrap2(type, constant) \
407 IORegistryEntry::copyProperty( type * aKey) constant \
412 obj = getProperty( aKey ); \
420 #define wrap4(type,constant) \
422 IORegistryEntry::getProperty( type * aKey, \
423 const IORegistryPlane * plane, \
424 IOOptionBits options ) constant \
426 OSObject * obj = getProperty( aKey ); \
428 if ( (0 == obj) && plane && (options & kIORegistryIterateRecursively) ) { \
429 IORegistryEntry * entry = (IORegistryEntry *) this; \
430 IORegistryIterator * iter; \
431 iter = IORegistryIterator::iterateOver( entry, plane, options ); \
434 while ( (0 == obj) && (entry = iter->getNextObject()) ) { \
435 obj = entry->getProperty( aKey ); \
444 #define wrap5(type,constant) \
446 IORegistryEntry::copyProperty( type * aKey, \
447 const IORegistryPlane * plane, \
448 IOOptionBits options ) constant \
450 OSObject * obj = copyProperty( aKey ); \
452 if ( (0 == obj) && plane && (options & kIORegistryIterateRecursively) ) { \
453 IORegistryEntry * entry = (IORegistryEntry *) this; \
454 IORegistryIterator * iter; \
455 iter = IORegistryIterator::iterateOver( entry, plane, options ); \
458 while ( (0 == obj) && (entry = iter->getNextObject()) ) { \
459 obj = entry->copyProperty( aKey ); \
468 bool IORegistryEntry::serializeProperties( OSSerialize
* s
) const
470 // setProperty( getRetainCount(), 32, "__retain" );
473 OSCollection
*snapshotProperties
= getPropertyTable()->copyCollection();
476 bool ok
= snapshotProperties
->serialize( s
);
477 snapshotProperties
->release();
481 OSDictionary
* IORegistryEntry::dictionaryWithProperties( void ) const
486 dict
= OSDictionary::withDictionary( getPropertyTable(),
487 getPropertyTable()->getCapacity() );
493 IOReturn
IORegistryEntry::setProperties( OSObject
* properties
)
495 return( kIOReturnUnsupported
);
498 wrap2(const OSSymbol
, const) // copyProperty() definition
499 wrap2(const OSString
, const) // copyProperty() definition
500 wrap2(const char, const) // copyProperty() definition
502 wrap4(const OSSymbol
, const) // getProperty() w/plane definition
503 wrap4(const OSString
, const) // getProperty() w/plane definition
504 wrap4(const char, const) // getProperty() w/plane definition
506 wrap5(const OSSymbol
, const) // copyProperty() w/plane definition
507 wrap5(const OSString
, const) // copyProperty() w/plane definition
508 wrap5(const char, const) // copyProperty() w/plane definition
512 IORegistryEntry::getProperty( const OSSymbol
* aKey
) const
517 obj
= getPropertyTable()->getObject( aKey
);
524 IORegistryEntry::removeProperty( const OSSymbol
* aKey
)
527 getPropertyTable()->removeObject( aKey
);
531 #if KASLR_IOREG_DEBUG
534 bool ScanForAddrInObject(OSObject
* theObject
,
541 IORegistryEntry::setProperty( const OSSymbol
* aKey
, OSObject
* anObject
)
545 // If we are inserting a collection class and the current entry
546 // is attached into the registry (inPlane()) then mark the collection
548 OSCollection
*coll
= OSDynamicCast(OSCollection
, anObject
);
549 bool makeImmutable
= (coll
&& inPlane());
553 coll
->setOptions( OSCollection::kMASK
, OSCollection::kImmutable
);
555 ret
= getPropertyTable()->setObject( aKey
, anObject
);
558 #if KASLR_IOREG_DEBUG
559 if ( anObject
&& strcmp(kIOKitDiagnosticsKey
, aKey
->getCStringNoCopy()) != 0 ) {
560 if (ScanForAddrInObject(anObject
, 0)) {
561 IOLog("%s: IORegistryEntry name %s with key \"%s\" \n",
564 aKey
->getCStringNoCopy() );
572 IOReturn
IORegistryEntry::
573 runPropertyAction(Action inAction
, OSObject
*target
,
574 void *arg0
, void *arg1
, void *arg2
, void *arg3
)
578 // closeGate is recursive so don't worry if we already hold the lock.
580 res
= (*inAction
)(target
, arg0
, arg1
, arg2
, arg3
);
587 IORegistryEntry::getProperty( const OSString
* aKey
) const
589 const OSSymbol
* tmpKey
= OSSymbol::withString( aKey
);
590 OSObject
* obj
= getProperty( tmpKey
);
597 IORegistryEntry::getProperty( const char * aKey
) const
599 const OSSymbol
* tmpKey
= OSSymbol::withCString( aKey
);
600 OSObject
* obj
= getProperty( tmpKey
);
608 IORegistryEntry::removeProperty( const OSString
* aKey
)
610 const OSSymbol
* tmpKey
= OSSymbol::withString( aKey
);
611 removeProperty( tmpKey
);
616 IORegistryEntry::removeProperty( const char * aKey
)
618 const OSSymbol
* tmpKey
= OSSymbol::withCString( aKey
);
619 removeProperty( tmpKey
);
624 IORegistryEntry::setProperty( const OSString
* aKey
, OSObject
* anObject
)
626 const OSSymbol
* tmpKey
= OSSymbol::withString( aKey
);
627 bool ret
= setProperty( tmpKey
, anObject
);
634 IORegistryEntry::setProperty( const char * aKey
, OSObject
* anObject
)
636 const OSSymbol
* tmpKey
= OSSymbol::withCString( aKey
);
637 bool ret
= setProperty( tmpKey
, anObject
);
644 IORegistryEntry::setProperty(const char * aKey
, const char * aString
)
647 OSSymbol
* aSymbol
= (OSSymbol
*) OSSymbol::withCString( aString
);
650 const OSSymbol
* tmpKey
= OSSymbol::withCString( aKey
);
651 ret
= setProperty( tmpKey
, aSymbol
);
660 IORegistryEntry::setProperty(const char * aKey
, bool aBoolean
)
663 OSBoolean
* aBooleanObj
= OSBoolean::withBoolean( aBoolean
);
666 const OSSymbol
* tmpKey
= OSSymbol::withCString( aKey
);
667 ret
= setProperty( tmpKey
, aBooleanObj
);
670 aBooleanObj
->release();
676 IORegistryEntry::setProperty( const char * aKey
,
677 unsigned long long aValue
,
678 unsigned int aNumberOfBits
)
681 OSNumber
* anOffset
= OSNumber::withNumber( aValue
, aNumberOfBits
);
684 const OSSymbol
* tmpKey
= OSSymbol::withCString( aKey
);
685 ret
= setProperty( tmpKey
, anOffset
);
694 IORegistryEntry::setProperty( const char * aKey
,
699 OSData
* data
= OSData::withBytes( bytes
, length
);
702 const OSSymbol
* tmpKey
= OSSymbol::withCString( aKey
);
703 ret
= setProperty( tmpKey
, data
);
711 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
713 /* Name, location, paths */
715 const char * IORegistryEntry::getName( const IORegistryPlane
* plane
) const
721 sym
= (OSSymbol
*) registryTable()->getObject( plane
->pathNameKey
);
723 sym
= (OSSymbol
*) registryTable()->getObject( gIONameKey
);
727 return( sym
->getCStringNoCopy());
729 return( (getMetaClass())->getClassName());
732 const OSSymbol
* IORegistryEntry::copyName(
733 const IORegistryPlane
* plane
) const
739 sym
= (OSSymbol
*) registryTable()->getObject( plane
->pathNameKey
);
741 sym
= (OSSymbol
*) registryTable()->getObject( gIONameKey
);
749 return( OSSymbol::withCString((getMetaClass())->getClassName()) );
752 const OSSymbol
* IORegistryEntry::copyLocation(
753 const IORegistryPlane
* plane
) const
759 sym
= (OSSymbol
*) registryTable()->getObject( plane
->pathLocationKey
);
761 sym
= (OSSymbol
*) registryTable()->getObject( gIOLocationKey
);
769 const char * IORegistryEntry::getLocation( const IORegistryPlane
* plane
) const
771 const OSSymbol
* sym
= copyLocation( plane
);
772 const char * result
= 0;
775 result
= sym
->getCStringNoCopy();
782 void IORegistryEntry::setName( const OSSymbol
* name
,
783 const IORegistryPlane
* plane
)
785 const OSSymbol
* key
;
789 key
= plane
->pathNameKey
;
794 registryTable()->setObject( key
, (OSObject
*) name
);
799 void IORegistryEntry::setName( const char * name
,
800 const IORegistryPlane
* plane
)
802 OSSymbol
* sym
= (OSSymbol
*)OSSymbol::withCString( name
);
804 setName( sym
, plane
);
809 void IORegistryEntry::setLocation( const OSSymbol
* location
,
810 const IORegistryPlane
* plane
)
812 const OSSymbol
* key
;
816 key
= plane
->pathLocationKey
;
818 key
= gIOLocationKey
;
821 registryTable()->setObject( key
, (OSObject
*) location
);
826 void IORegistryEntry::setLocation( const char * location
,
827 const IORegistryPlane
* plane
)
829 OSSymbol
* sym
= (OSSymbol
*)OSSymbol::withCString( location
);
831 setLocation( sym
, plane
);
837 IORegistryEntry::compareName( OSString
* name
, OSString
** matched
) const
839 const OSSymbol
* sym
= copyName();
842 isEqual
= sym
->isEqualTo( name
);
844 if( isEqual
&& matched
) {
856 IORegistryEntry::compareNames( OSObject
* names
, OSString
** matched
) const
859 OSCollection
* collection
;
860 OSIterator
* iter
= 0;
863 if( (collection
= OSDynamicCast( OSCollection
, names
))) {
864 iter
= OSCollectionIterator::withCollection( collection
);
867 string
= OSDynamicCast( OSString
, names
);
871 result
= compareName( string
, matched
);
873 } while( (false == result
)
874 && iter
&& (string
= OSDynamicCast( OSString
, iter
->getNextObject())));
883 bool IORegistryEntry::getPath( char * path
, int * length
,
884 const IORegistryPlane
* plane
) const
887 IORegistryEntry
* root
;
888 const IORegistryEntry
* entry
;
889 const IORegistryEntry
* parent
;
890 const OSSymbol
* alias
;
892 int len
, maxLength
, compLen
, aliasLen
;
896 if( !path
|| !length
|| !plane
)
900 maxLength
= *length
- 2;
903 len
= plane
->nameKey
->getLength();
904 if( len
>= maxLength
)
906 strlcpy( nextComp
, plane
->nameKey
->getCStringNoCopy(), len
+ 1);
907 nextComp
[ len
++ ] = ':';
910 if( (alias
= hasAlias( plane
))) {
911 aliasLen
= alias
->getLength();
913 ok
= (maxLength
> len
);
916 strlcpy( nextComp
, alias
->getCStringNoCopy(), aliasLen
+ 1);
920 stack
= OSArray::withCapacity( getDepth( plane
));
921 if (!stack
) return( false);
925 parent
= entry
= this;
926 root
= gRegistryRoot
->getChildEntry( plane
);
927 while (parent
&& (parent
!= root
))
931 parent
= entry
->getParentEntry( plane
);
932 stack
->setObject( (OSObject
*) entry
);
938 index
= stack
->getCount();
945 } else while( ok
&& ((--index
) >= 0)) {
947 entry
= (IORegistryEntry
*) stack
->getObject((unsigned int) index
);
950 if( (alias
= entry
->hasAlias( plane
))) {
951 len
= plane
->nameKey
->getLength() + 1;
952 nextComp
= path
+ len
;
954 compLen
= alias
->getLength();
955 ok
= (maxLength
> (len
+ compLen
));
957 strlcpy( nextComp
, alias
->getCStringNoCopy(), compLen
+ 1);
959 compLen
= maxLength
- len
;
960 ok
= entry
->getPathComponent( nextComp
+ 1, &compLen
, plane
);
981 bool IORegistryEntry::getPathComponent( char * path
, int * length
,
982 const IORegistryPlane
* plane
) const
984 int len
, locLen
, maxLength
;
985 const char * compName
;
991 compName
= getName( plane
);
992 len
= strlen( compName
);
993 if( (loc
= getLocation( plane
)))
994 locLen
= 1 + strlen( loc
);
998 ok
= ((len
+ locLen
+ 1) < maxLength
);
1000 strlcpy( path
, compName
, len
+ 1 );
1005 strlcpy( path
, loc
, locLen
);
1013 const char * IORegistryEntry::matchPathLocation( const char * cmp
,
1014 const IORegistryPlane
* plane
)
1017 const char * result
= 0;
1018 u_quad_t num1
, num2
;
1019 char lastPathChar
, lastLocationChar
;
1021 str
= getLocation( plane
);
1023 lastPathChar
= cmp
[0];
1024 lastLocationChar
= str
[0];
1027 num1
= strtouq( cmp
, (char **) &cmp
, 16 );
1028 lastPathChar
= *cmp
++;
1032 if( lastLocationChar
) {
1033 num2
= strtouq( str
, (char **) &str
, 16 );
1034 lastLocationChar
= *str
++;
1041 if (!lastPathChar
&& !lastLocationChar
) {
1046 if( (',' != lastPathChar
) && (':' != lastPathChar
))
1049 if (lastPathChar
&& lastLocationChar
&& (lastPathChar
!= lastLocationChar
))
1058 IORegistryEntry
* IORegistryEntry::getChildFromComponent( const char ** opath
,
1059 const IORegistryPlane
* plane
)
1061 IORegistryEntry
* entry
= 0;
1065 const char * cmp
= 0;
1070 set
= getChildSetReference( plane
);
1076 (entry
= (IORegistryEntry
*) set
->getObject(index
));
1082 str
= entry
->getName( plane
);
1083 len
= strlen( str
);
1084 if( strncmp( str
, cmp
, len
))
1089 if( (c
== 0) || (c
== '/') || (c
== ':'))
1095 if( (cmp
= entry
->matchPathLocation( cmp
, plane
)))
1105 const OSSymbol
* IORegistryEntry::hasAlias( const IORegistryPlane
* plane
,
1106 char * opath
, int * length
) const
1108 IORegistryEntry
* entry
;
1109 IORegistryEntry
* entry2
;
1110 const OSSymbol
* key
;
1111 const OSSymbol
* bestKey
= 0;
1114 const char * path
= "/aliases";
1116 entry
= IORegistryEntry::fromPath( path
, plane
);
1119 if( (iter
= OSCollectionIterator::withCollection(
1120 entry
->getPropertyTable() ))) {
1122 while( (key
= (OSSymbol
*) iter
->getNextObject())) {
1124 data
= (OSData
*) entry
->getProperty( key
);
1125 path
= (const char *) data
->getBytesNoCopy();
1126 if( (entry2
= IORegistryEntry::fromPath( path
, plane
,
1128 if( this == entry2
) {
1130 || (bestKey
->getLength() > key
->getLength()))
1131 // pick the smallest alias
1145 const char * IORegistryEntry::dealiasPath(
1146 const char ** opath
,
1147 const IORegistryPlane
* plane
)
1149 IORegistryEntry
* entry
;
1151 const char * path
= *opath
;
1152 const char * rpath
= 0;
1155 char temp
[ kIOMaxPlaneName
+ 1 ];
1162 while( (c
= *end
++) && (c
!= '/') && (c
!= ':'))
1165 if( (end
- path
) < kIOMaxPlaneName
) {
1166 strlcpy( temp
, path
, end
- path
+ 1 );
1169 entry
= IORegistryEntry::fromPath( "/aliases", plane
);
1171 data
= (OSData
*) entry
->getProperty( temp
);
1173 rpath
= (const char *) data
->getBytesNoCopy();
1185 IORegistryEntry
* IORegistryEntry::fromPath(
1187 const IORegistryPlane
* plane
,
1190 IORegistryEntry
* fromEntry
)
1192 IORegistryEntry
* where
= 0;
1193 IORegistryEntry
* aliasEntry
= 0;
1194 IORegistryEntry
* next
;
1200 char temp
[ kIOMaxPlaneName
+ 1 ];
1207 end
= strchr( path
, ':' );
1208 if( end
&& ((end
- path
) < kIOMaxPlaneName
)) {
1209 strlcpy( temp
, path
, end
- path
+ 1 );
1210 plane
= getPlane( temp
);
1219 if( (alias
= dealiasPath( &end
, plane
))) {
1222 aliasEntry
= IORegistryEntry::fromPath( alias
, plane
,
1223 opath
, &len
, fromEntry
);
1235 if( (0 == fromEntry
) && (*path
++ == '/'))
1236 fromEntry
= gRegistryRoot
->getChildEntry( plane
);
1243 if( c
&& (c
!= ':')) // check valid terminator
1248 next
= where
->getChildFromComponent( &path
, plane
);
1254 // check residual path
1255 if( where
!= fromEntry
)
1258 if( opath
&& length
) {
1259 // copy out residual path
1260 len2
= strlen( path
);
1261 if( (len
+ len2
) < *length
)
1262 strlcpy( opath
+ len
, path
, len2
+ 1 );
1263 *length
= (len
+ len2
);
1266 // no residual path => must be no tail for success
1273 aliasEntry
->release();
1280 IORegistryEntry
* IORegistryEntry::childFromPath(
1282 const IORegistryPlane
* plane
,
1286 return( IORegistryEntry::fromPath( path
, plane
, opath
, len
, this ));
1289 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1291 #define IOLinkIterator OSCollectionIterator
1294 #define super OSObject
1296 inline bool IORegistryEntry::arrayMember( OSArray
* set
,
1297 const IORegistryEntry
* member
,
1298 unsigned int * index
) const
1301 OSObject
* probeObject
;
1303 for( i
= 0; (probeObject
= set
->getObject(i
)); i
++) {
1304 if (probeObject
== (OSObject
*) member
) {
1313 bool IORegistryEntry::makeLink( IORegistryEntry
* to
,
1314 unsigned int relation
,
1315 const IORegistryPlane
* plane
) const
1318 bool result
= false;
1320 if( (links
= (OSArray
*)
1321 registryTable()->getObject( plane
->keys
[ relation
] ))) {
1323 result
= arrayMember( links
, to
);
1325 result
= links
->setObject( to
);
1329 links
= OSArray::withObjects( (const OSObject
**) &to
, 1, 1 );
1330 result
= (links
!= 0);
1332 result
= registryTable()->setObject( plane
->keys
[ relation
],
1341 void IORegistryEntry::breakLink( IORegistryEntry
* to
,
1342 unsigned int relation
,
1343 const IORegistryPlane
* plane
) const
1348 if( (links
= (OSArray
*)
1349 registryTable()->getObject( plane
->keys
[ relation
]))) {
1351 if( arrayMember( links
, to
, &index
)) {
1352 links
->removeObject( index
);
1353 if( 0 == links
->getCount())
1354 registryTable()->removeObject( plane
->keys
[ relation
]);
1360 OSArray
* IORegistryEntry::getParentSetReference(
1361 const IORegistryPlane
* plane
) const
1364 return( (OSArray
*) registryTable()->getObject(
1365 plane
->keys
[ kParentSetIndex
]));
1370 OSIterator
* IORegistryEntry::getParentIterator(
1371 const IORegistryPlane
* plane
) const
1380 links
= getParentSetReference( plane
);
1382 links
= OSArray::withCapacity( 1 );
1384 links
= OSArray::withArray( links
, links
->getCount() );
1387 iter
= IOLinkIterator::withCollection( links
);
1395 IORegistryEntry
* IORegistryEntry::copyParentEntry( const IORegistryPlane
* plane
) const
1397 IORegistryEntry
* entry
= 0;
1402 if( (links
= getParentSetReference( plane
))) {
1403 entry
= (IORegistryEntry
*) links
->getObject( 0 );
1412 IORegistryEntry
* IORegistryEntry::getParentEntry( const IORegistryPlane
* plane
) const
1414 IORegistryEntry
* entry
;
1416 entry
= copyParentEntry( plane
);
1423 OSArray
* IORegistryEntry::getChildSetReference( const IORegistryPlane
* plane
) const
1426 return( (OSArray
*) registryTable()->getObject(
1427 plane
->keys
[ kChildSetIndex
]));
1432 OSIterator
* IORegistryEntry::getChildIterator( const IORegistryPlane
* plane
) const
1441 links
= getChildSetReference( plane
);
1443 links
= OSArray::withCapacity( 1 );
1445 links
= OSArray::withArray( links
, links
->getCount() );
1448 iter
= IOLinkIterator::withCollection( links
);
1457 IORegistryEntry
* IORegistryEntry::copyChildEntry(
1458 const IORegistryPlane
* plane
) const
1460 IORegistryEntry
* entry
= 0;
1465 if( (links
= getChildSetReference( plane
))) {
1466 entry
= (IORegistryEntry
*) links
->getObject( 0 );
1475 IORegistryEntry
* IORegistryEntry::getChildEntry(
1476 const IORegistryPlane
* plane
) const
1478 IORegistryEntry
* entry
;
1480 entry
= copyChildEntry( plane
);
1487 void IORegistryEntry::applyToChildren( IORegistryEntryApplierFunction applier
,
1489 const IORegistryPlane
* plane
) const
1493 IORegistryEntry
* next
;
1499 array
= OSArray::withArray( getChildSetReference( plane
));
1503 (next
= (IORegistryEntry
*) array
->getObject( index
));
1505 (*applier
)(next
, context
);
1510 void IORegistryEntry::applyToParents( IORegistryEntryApplierFunction applier
,
1512 const IORegistryPlane
* plane
) const
1516 IORegistryEntry
* next
;
1522 array
= OSArray::withArray( getParentSetReference( plane
));
1526 (next
= (IORegistryEntry
*) array
->getObject( index
));
1528 (*applier
)(next
, context
);
1533 bool IORegistryEntry::isChild( IORegistryEntry
* child
,
1534 const IORegistryPlane
* plane
,
1535 bool onlyChild
) const
1542 if( (links
= getChildSetReference( plane
))) {
1543 if( (!onlyChild
) || (1 == links
->getCount()))
1544 ret
= arrayMember( links
, child
);
1546 if( ret
&& (links
= child
->getParentSetReference( plane
)))
1547 ret
= arrayMember( links
, this );
1554 bool IORegistryEntry::isParent( IORegistryEntry
* parent
,
1555 const IORegistryPlane
* plane
,
1556 bool onlyParent
) const
1564 if( (links
= getParentSetReference( plane
))) {
1565 if( (!onlyParent
) || (1 == links
->getCount()))
1566 ret
= arrayMember( links
, parent
);
1568 if( ret
&& (links
= parent
->getChildSetReference( plane
)))
1569 ret
= arrayMember( links
, this );
1576 bool IORegistryEntry::inPlane( const IORegistryPlane
* plane
) const
1583 ret
= (0 != getParentSetReference( plane
));
1586 // Check to see if this is in any plane. If it is in a plane
1587 // then the registryTable will contain a key with the ParentLinks
1588 // suffix. When we iterate over the keys looking for that suffix
1591 OSCollectionIterator
*iter
=
1592 OSCollectionIterator::withCollection( registryTable());
1594 const OSSymbol
*key
;
1596 while( (key
= (OSSymbol
*) iter
->getNextObject()) ) {
1599 // Get a pointer to this keys suffix
1600 keysuffix
= key
->getLength();
1601 if (keysuffix
<= kIORegPlaneParentSuffixLen
)
1603 keysuffix
-= kIORegPlaneParentSuffixLen
;
1604 if( !strncmp(key
->getCStringNoCopy() + keysuffix
,
1605 kIORegPlaneParentSuffix
,
1606 kIORegPlaneParentSuffixLen
+ 1) ) {
1620 bool IORegistryEntry::attachToParent( IORegistryEntry
* parent
,
1621 const IORegistryPlane
* plane
)
1632 if (!reserved
->fRegistryEntryID
)
1633 reserved
->fRegistryEntryID
= ++gIORegistryLastID
;
1635 ret
= makeLink( parent
, kParentSetIndex
, plane
);
1637 if( (links
= parent
->getChildSetReference( plane
)))
1638 needParent
= (false == arrayMember( links
, this ));
1646 // Mark any collections in the property list as immutable
1647 OSDictionary
*ptable
= getPropertyTable();
1648 OSCollectionIterator
*iter
=
1649 OSCollectionIterator::withCollection( ptable
);
1651 const OSSymbol
*key
;
1653 while( (key
= (OSSymbol
*) iter
->getNextObject( ))) {
1654 // Is object for key a collection?
1655 OSCollection
*coll
=
1656 OSDynamicCast( OSCollection
, ptable
->getObject( key
));
1659 // Yup so mark it as immutable
1660 coll
->setOptions( OSCollection::kMASK
,
1661 OSCollection::kImmutable
);
1670 ret
&= parent
->attachToChild( this, plane
);
1675 uint64_t IORegistryEntry::getRegistryEntryID( void )
1678 return (reserved
->fRegistryEntryID
);
1683 bool IORegistryEntry::attachToChild( IORegistryEntry
* child
,
1684 const IORegistryPlane
* plane
)
1695 ret
= makeLink( child
, kChildSetIndex
, plane
);
1697 if( (links
= child
->getParentSetReference( plane
)))
1698 needChild
= (false == arrayMember( links
, this ));
1705 ret
&= child
->attachToParent( this, plane
);
1710 void IORegistryEntry::detachFromParent( IORegistryEntry
* parent
,
1711 const IORegistryPlane
* plane
)
1720 breakLink( parent
, kParentSetIndex
, plane
);
1722 if( (links
= parent
->getChildSetReference( plane
)))
1723 needParent
= arrayMember( links
, this );
1727 // parent->breakLink( this, kChildSetIndex, plane );
1732 parent
->detachFromChild( this, plane
);
1737 void IORegistryEntry::detachFromChild( IORegistryEntry
* child
,
1738 const IORegistryPlane
* plane
)
1747 breakLink( child
, kChildSetIndex
, plane
);
1749 if( (links
= child
->getParentSetReference( plane
)))
1750 needChild
= arrayMember( links
, this );
1757 child
->detachFromParent( this, plane
);
1762 void IORegistryEntry::detachAbove( const IORegistryPlane
* plane
)
1764 IORegistryEntry
* parent
;
1767 while( (parent
= copyParentEntry( plane
)))
1769 detachFromParent( parent
, plane
);
1775 void IORegistryEntry::detachAll( const IORegistryPlane
* plane
)
1778 IORegistryEntry
* next
;
1779 IORegistryIterator
* regIter
;
1781 regIter
= IORegistryIterator::iterateOver( this, plane
, true );
1784 all
= regIter
->iterateAll();
1787 detachAbove( plane
);
1789 while( (next
= (IORegistryEntry
*) all
->getLastObject())) {
1792 all
->removeObject(next
);
1794 next
->detachAbove( plane
);
1801 unsigned int IORegistryEntry::getDepth( const IORegistryPlane
* plane
) const
1803 unsigned int depth
= 1;
1805 unsigned int oneDepth
, maxParentDepth
, count
;
1806 IORegistryEntry
* one
;
1807 const IORegistryEntry
* next
;
1813 while( (parents
= next
->getParentSetReference( plane
))) {
1815 count
= parents
->getCount();
1820 next
= (IORegistryEntry
*) parents
->getObject( 0 );
1825 (one
= (IORegistryEntry
*) parents
->getObject( index
));
1827 oneDepth
= one
->getDepth( plane
);
1828 if( oneDepth
> maxParentDepth
)
1829 maxParentDepth
= oneDepth
;
1831 depth
+= maxParentDepth
;
1841 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1844 #define super OSIterator
1846 OSDefineMetaClassAndStructors(IORegistryIterator
, OSIterator
)
1848 enum { kIORegistryIteratorInvalidFlag
= 0x80000000 };
1850 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1852 IORegistryIterator
*
1853 IORegistryIterator::iterateOver( IORegistryEntry
* root
,
1854 const IORegistryPlane
* plane
,
1855 IOOptionBits options
)
1857 IORegistryIterator
* create
;
1864 create
= new IORegistryIterator
;
1866 if( create
->init()) {
1869 create
->root
= root
;
1870 create
->where
= &create
->start
;
1871 create
->start
.current
= root
;
1872 create
->plane
= plane
;
1873 create
->options
= options
& ~kIORegistryIteratorInvalidFlag
;
1883 IORegistryIterator
*
1884 IORegistryIterator::iterateOver( const IORegistryPlane
* plane
,
1885 IOOptionBits options
)
1887 return( iterateOver( gRegistryRoot
, plane
, options
));
1890 bool IORegistryIterator::isValid( void )
1899 ok
= (0 == (kIORegistryIteratorInvalidFlag
& options
));
1901 while( ok
&& next
) {
1903 ok
= where
->iter
->isValid();
1911 void IORegistryIterator::enterEntry( const IORegistryPlane
* enterPlane
)
1916 where
= (IORegCursor
*) IOMalloc( sizeof(IORegCursor
));
1922 where
->current
= prev
->current
;
1927 void IORegistryIterator::enterEntry( void )
1929 enterEntry( plane
);
1932 bool IORegistryIterator::exitEntry( void )
1937 where
->iter
->release();
1939 if( where
->current
)// && (where != &start))
1940 where
->current
->release();
1943 if( where
!= &start
) {
1946 IOFree( gone
, sizeof(IORegCursor
));
1953 void IORegistryIterator::reset( void )
1963 where
->current
= root
;
1964 options
&= ~kIORegistryIteratorInvalidFlag
;
1967 void IORegistryIterator::free( void )
1978 IORegistryEntry
* IORegistryIterator::getNextObjectFlat( void )
1980 IORegistryEntry
* next
= 0;
1981 OSArray
* links
= 0;
1985 if( (0 == where
->iter
)) {
1986 // just entered - create new iter
1989 && (links
= ( (options
& kIORegistryIterateParents
) ?
1990 where
->current
->getParentSetReference( plane
) :
1991 where
->current
->getChildSetReference( plane
) )) )
1993 where
->iter
= OSCollectionIterator::withCollection( links
);
1996 // next sibling - release current
1998 where
->current
->release();
2002 next
= (IORegistryEntry
*) where
->iter
->getNextObject();
2006 else if( !where
->iter
->isValid())
2007 options
|= kIORegistryIteratorInvalidFlag
;
2010 where
->current
= next
;
2017 IORegistryEntry
* IORegistryIterator::getNextObjectRecursive( void )
2019 IORegistryEntry
* next
;
2022 next
= getNextObjectFlat();
2023 while( (0 == next
) && exitEntry());
2027 done
= OSOrderedSet::withCapacity( 10 );
2028 if( done
->setObject((OSObject
*) next
)) {
2029 // done set didn't contain this one, so recurse
2036 IORegistryEntry
* IORegistryIterator::getNextObject( void )
2038 if( options
& kIORegistryIterateRecursively
)
2039 return( getNextObjectRecursive());
2041 return( getNextObjectFlat());
2044 IORegistryEntry
* IORegistryIterator::getCurrentEntry( void )
2047 return( where
->current
);
2052 OSOrderedSet
* IORegistryIterator::iterateAll( void )
2055 while( getNextObjectRecursive())
2063 OSMetaClassDefineReservedUnused(IORegistryEntry
, 0);
2064 OSMetaClassDefineReservedUnused(IORegistryEntry
, 1);
2065 OSMetaClassDefineReservedUnused(IORegistryEntry
, 2);
2066 OSMetaClassDefineReservedUnused(IORegistryEntry
, 3);
2067 OSMetaClassDefineReservedUnused(IORegistryEntry
, 4);
2068 OSMetaClassDefineReservedUnused(IORegistryEntry
, 5);
2070 OSMetaClassDefineReservedUsed(IORegistryEntry
, 0);
2071 OSMetaClassDefineReservedUsed(IORegistryEntry
, 1);
2072 OSMetaClassDefineReservedUsed(IORegistryEntry
, 2);
2073 OSMetaClassDefineReservedUsed(IORegistryEntry
, 3);
2074 OSMetaClassDefineReservedUsed(IORegistryEntry
, 4);
2075 OSMetaClassDefineReservedUsed(IORegistryEntry
, 5);
2077 OSMetaClassDefineReservedUnused(IORegistryEntry
, 6);
2078 OSMetaClassDefineReservedUnused(IORegistryEntry
, 7);
2079 OSMetaClassDefineReservedUnused(IORegistryEntry
, 8);
2080 OSMetaClassDefineReservedUnused(IORegistryEntry
, 9);
2081 OSMetaClassDefineReservedUnused(IORegistryEntry
, 10);
2082 OSMetaClassDefineReservedUnused(IORegistryEntry
, 11);
2083 OSMetaClassDefineReservedUnused(IORegistryEntry
, 12);
2084 OSMetaClassDefineReservedUnused(IORegistryEntry
, 13);
2085 OSMetaClassDefineReservedUnused(IORegistryEntry
, 14);
2086 OSMetaClassDefineReservedUnused(IORegistryEntry
, 15);
2087 OSMetaClassDefineReservedUnused(IORegistryEntry
, 16);
2088 OSMetaClassDefineReservedUnused(IORegistryEntry
, 17);
2089 OSMetaClassDefineReservedUnused(IORegistryEntry
, 18);
2090 OSMetaClassDefineReservedUnused(IORegistryEntry
, 19);
2091 OSMetaClassDefineReservedUnused(IORegistryEntry
, 20);
2092 OSMetaClassDefineReservedUnused(IORegistryEntry
, 21);
2093 OSMetaClassDefineReservedUnused(IORegistryEntry
, 22);
2094 OSMetaClassDefineReservedUnused(IORegistryEntry
, 23);
2095 OSMetaClassDefineReservedUnused(IORegistryEntry
, 24);
2096 OSMetaClassDefineReservedUnused(IORegistryEntry
, 25);
2097 OSMetaClassDefineReservedUnused(IORegistryEntry
, 26);
2098 OSMetaClassDefineReservedUnused(IORegistryEntry
, 27);
2099 OSMetaClassDefineReservedUnused(IORegistryEntry
, 28);
2100 OSMetaClassDefineReservedUnused(IORegistryEntry
, 29);
2101 OSMetaClassDefineReservedUnused(IORegistryEntry
, 30);
2102 OSMetaClassDefineReservedUnused(IORegistryEntry
, 31);
2104 /* inline function implementation */
2105 OSDictionary
* IORegistryEntry::getPropertyTable( void ) const
2106 { return(fPropertyTable
); }