1 | /* |
2 | * Copyright (c) 1998-2019 Apple Inc. All rights reserved. |
3 | * |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
5 | * |
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. |
14 | * |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. |
17 | * |
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. |
25 | * |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
27 | */ |
28 | |
29 | #include <IOKit/IORegistryEntry.h> |
30 | #include <libkern/c++/OSContainers.h> |
31 | #include <IOKit/IOService.h> |
32 | #include <IOKit/IOKitKeys.h> |
33 | #include <IOKit/IOTimeStamp.h> |
34 | #include <libkern/c++/OSSharedPtr.h> |
35 | #include <libkern/c++/OSBoundedPtr.h> |
36 | |
37 | #include <IOKit/IOLib.h> |
38 | #include <stdatomic.h> |
39 | #include <IOKit/assert.h> |
40 | #include <machine/atomic.h> |
41 | |
42 | #include "IOKitKernelInternal.h" |
43 | |
44 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
45 | |
46 | #define super OSObject |
47 | |
48 | OSDefineMetaClassAndStructors(IORegistryEntry, OSObject) |
49 | |
50 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
51 | |
52 | #define kIORegPlaneParentSuffix "ParentLinks" |
53 | #define kIORegPlaneChildSuffix "ChildLinks" |
54 | #define kIORegPlaneNameSuffix "Name" |
55 | #define kIORegPlaneLocationSuffix "Location" |
56 | |
57 | #define kIORegPlaneParentSuffixLen (sizeof(kIORegPlaneParentSuffix) - 1) |
58 | #define kIORegPlaneChildSuffixLen (sizeof(kIORegPlaneChildSuffix) - 1) |
59 | #define kIORegPlaneNameSuffixLen (sizeof(kIORegPlaneNameSuffix) - 1) |
60 | #define kIORegPlaneLocationSuffixLen (sizeof(kIORegPlaneLocationSuffix) - 1) |
61 | |
62 | #define KASLR_IOREG_DEBUG 0 |
63 | |
64 | struct IORegistryEntry::ExpansionData { |
65 | IORecursiveLock * fLock; |
66 | uint64_t fRegistryEntryID; |
67 | SInt32 fRegistryEntryGenerationCount; |
68 | OSObject **_Atomic fIndexedProperties; |
69 | }; |
70 | |
71 | |
72 | static IORegistryEntry * gRegistryRoot; |
73 | static OSDictionary * gIORegistryPlanes; |
74 | |
75 | const OSSymbol * gIONameKey; |
76 | const OSSymbol * gIOLocationKey; |
77 | const OSSymbol * gIORegistryEntryIDKey; |
78 | const OSSymbol * gIORegistryEntryPropertyKeysKey; |
79 | const OSSymbol * gIORegistryEntryAllowableSetPropertiesKey; |
80 | const OSSymbol * gIORegistryEntryDefaultLockingSetPropertiesKey; |
81 | |
82 | enum { |
83 | kParentSetIndex = 0, |
84 | kChildSetIndex = 1, |
85 | kNumSetIndex |
86 | }; |
87 | enum { |
88 | kIOMaxPlaneName = 32 |
89 | }; |
90 | |
91 | enum { kIORegistryIDReserved = (1ULL << 32) + 255 }; |
92 | |
93 | static uint64_t gIORegistryLastID = kIORegistryIDReserved; |
94 | |
95 | class IORegistryPlane : public OSObject { |
96 | friend class IORegistryEntry; |
97 | |
98 | OSDeclareAbstractStructors(IORegistryPlane); |
99 | |
100 | const OSSymbol * nameKey; |
101 | const OSSymbol * keys[kNumSetIndex]; |
102 | const OSSymbol * pathNameKey; |
103 | const OSSymbol * pathLocationKey; |
104 | int reserved[2]; |
105 | |
106 | public: |
107 | virtual bool serialize(OSSerialize *s) const APPLE_KEXT_OVERRIDE; |
108 | }; |
109 | |
110 | OSDefineMetaClassAndStructors(IORegistryPlane, OSObject) |
111 | |
112 | |
113 | static SInt32 gIORegistryGenerationCount; |
114 | |
115 | #define UNLOCK lck_rw_done( &gIORegistryLock ) |
116 | #define RLOCK lck_rw_lock_shared( &gIORegistryLock ) |
117 | #define WLOCK lck_rw_lock_exclusive( &gIORegistryLock ); \ |
118 | gIORegistryGenerationCount++ |
119 | // make atomic |
120 | |
121 | #define PUNLOCK IORecursiveLockUnlock( reserved->fLock ) |
122 | #define PLOCK IORecursiveLockLock( reserved->fLock ) |
123 | |
124 | #define IOREGSPLITTABLES |
125 | |
126 | #ifdef IOREGSPLITTABLES |
127 | #define registryTable() fRegistryTable |
128 | #else |
129 | #define registryTable() fPropertyTable |
130 | #endif |
131 | |
132 | #define DEBUG_FREE 1 |
133 | |
134 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
135 | |
136 | lck_rw_t gIORegistryLock; |
137 | lck_grp_t *gIORegistryLockGrp; |
138 | lck_grp_attr_t *gIORegistryLockGrpAttr; |
139 | lck_attr_t *gIORegistryLockAttr; |
140 | |
141 | |
142 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
143 | |
144 | IORegistryEntry * |
145 | IORegistryEntry::initialize( void ) |
146 | { |
147 | bool ok; |
148 | |
149 | if (!gRegistryRoot) { |
150 | gIORegistryLockGrpAttr = lck_grp_attr_alloc_init(); |
151 | gIORegistryLockGrp = lck_grp_alloc_init(grp_name: "IORegistryLock" , attr: gIORegistryLockGrpAttr); |
152 | gIORegistryLockAttr = lck_attr_alloc_init(); |
153 | lck_attr_rw_shared_priority(attr: gIORegistryLockAttr); |
154 | lck_rw_init( lck: &gIORegistryLock, grp: gIORegistryLockGrp, attr: gIORegistryLockAttr); |
155 | |
156 | gRegistryRoot = new IORegistryEntry; |
157 | gIORegistryPlanes = OSDictionary::withCapacity( capacity: 1 ); |
158 | |
159 | assert( gRegistryRoot && gIORegistryPlanes ); |
160 | ok = gRegistryRoot->init(); |
161 | |
162 | if (ok) { |
163 | gRegistryRoot->reserved->fRegistryEntryID = ++gIORegistryLastID; |
164 | } |
165 | |
166 | gIONameKey = OSSymbol::withCStringNoCopy( cString: "IOName" ); |
167 | gIOLocationKey = OSSymbol::withCStringNoCopy( cString: "IOLocation" ); |
168 | gIORegistryEntryIDKey = OSSymbol::withCStringNoCopy( kIORegistryEntryIDKey ); |
169 | gIORegistryEntryPropertyKeysKey = OSSymbol::withCStringNoCopy( kIORegistryEntryPropertyKeysKey ); |
170 | gIORegistryEntryAllowableSetPropertiesKey = OSSymbol::withCStringNoCopy( kIORegistryEntryAllowableSetPropertiesKey ); |
171 | gIORegistryEntryDefaultLockingSetPropertiesKey = OSSymbol::withCStringNoCopy( kIORegistryEntryDefaultLockingSetPropertiesKey ); |
172 | |
173 | assert( ok && gIONameKey && gIOLocationKey ); |
174 | |
175 | gRegistryRoot->setName( name: "Root" ); |
176 | gRegistryRoot->setProperty( kIORegistryPlanesKey, anObject: gIORegistryPlanes ); |
177 | } |
178 | |
179 | return gRegistryRoot; |
180 | } |
181 | |
182 | IORegistryEntry * |
183 | IORegistryEntry::getRegistryRoot( void ) |
184 | { |
185 | return gRegistryRoot; |
186 | } |
187 | |
188 | SInt32 |
189 | IORegistryEntry::getGenerationCount( void ) |
190 | { |
191 | return gIORegistryGenerationCount; |
192 | } |
193 | |
194 | SInt32 |
195 | IORegistryEntry::getRegistryEntryGenerationCount(void) const |
196 | { |
197 | return reserved->fRegistryEntryGenerationCount; |
198 | } |
199 | |
200 | const IORegistryPlane * |
201 | IORegistryEntry::makePlane( const char * name ) |
202 | { |
203 | IORegistryPlane * plane; |
204 | const OSSymbol * nameKey; |
205 | const OSSymbol * parentKey; |
206 | const OSSymbol * childKey; |
207 | const OSSymbol * pathNameKey; |
208 | const OSSymbol * pathLocationKey; |
209 | char key[kIOMaxPlaneName + 16]; |
210 | char * end; |
211 | |
212 | strlcpy( dst: key, src: name, n: kIOMaxPlaneName + 1 ); |
213 | end = key + strlen( s: key ); |
214 | |
215 | nameKey = OSSymbol::withCString( cString: key); |
216 | |
217 | strlcpy( dst: end, kIORegPlaneParentSuffix, kIORegPlaneParentSuffixLen + 1 ); |
218 | parentKey = OSSymbol::withCString( cString: key); |
219 | |
220 | strlcpy( dst: end, kIORegPlaneChildSuffix, kIORegPlaneChildSuffixLen + 1 ); |
221 | childKey = OSSymbol::withCString( cString: key); |
222 | |
223 | strlcpy( dst: end, kIORegPlaneNameSuffix, kIORegPlaneNameSuffixLen + 1 ); |
224 | pathNameKey = OSSymbol::withCString( cString: key); |
225 | |
226 | strlcpy( dst: end, kIORegPlaneLocationSuffix, kIORegPlaneLocationSuffixLen + 1 ); |
227 | pathLocationKey = OSSymbol::withCString( cString: key); |
228 | |
229 | plane = new IORegistryPlane; |
230 | |
231 | if (plane && plane->init() |
232 | && nameKey && parentKey && childKey |
233 | && pathNameKey && pathLocationKey) { |
234 | plane->nameKey = nameKey; |
235 | plane->keys[kParentSetIndex] = parentKey; |
236 | plane->keys[kChildSetIndex] = childKey; |
237 | plane->pathNameKey = pathNameKey; |
238 | plane->pathLocationKey = pathLocationKey; |
239 | |
240 | WLOCK; |
241 | gIORegistryPlanes->setObject( aKey: nameKey, anObject: plane ); |
242 | UNLOCK; |
243 | } else { |
244 | if (plane) { |
245 | plane->release(); |
246 | } |
247 | if (pathLocationKey) { |
248 | pathLocationKey->release(); |
249 | } |
250 | if (pathNameKey) { |
251 | pathNameKey->release(); |
252 | } |
253 | if (parentKey) { |
254 | parentKey->release(); |
255 | } |
256 | if (childKey) { |
257 | childKey->release(); |
258 | } |
259 | if (nameKey) { |
260 | nameKey->release(); |
261 | } |
262 | plane = NULL; |
263 | } |
264 | |
265 | return plane; |
266 | } |
267 | |
268 | const IORegistryPlane * |
269 | IORegistryEntry::getPlane( const char * name ) |
270 | { |
271 | const IORegistryPlane * plane; |
272 | |
273 | RLOCK; |
274 | plane = (const IORegistryPlane *) gIORegistryPlanes->getObject( aKey: name ); |
275 | UNLOCK; |
276 | |
277 | return plane; |
278 | } |
279 | |
280 | bool |
281 | IORegistryPlane::serialize(OSSerialize *s) const |
282 | { |
283 | return nameKey->serialize(serializer: s); |
284 | } |
285 | |
286 | enum { kIORegCapacityIncrement = 4 }; |
287 | |
288 | bool |
289 | IORegistryEntry::init( OSDictionary * dict ) |
290 | { |
291 | OSString * prop; |
292 | |
293 | if (!super::init()) { |
294 | return false; |
295 | } |
296 | |
297 | if (!reserved) { |
298 | reserved = IOMallocType(ExpansionData); |
299 | reserved->fLock = IORecursiveLockAlloc(); |
300 | if (!reserved->fLock) { |
301 | return false; |
302 | } |
303 | } |
304 | if (dict) { |
305 | if (OSCollection::kImmutable & dict->setOptions(options: 0, mask: 0)) { |
306 | dict = (OSDictionary *) dict->copyCollection(); |
307 | if (!dict) { |
308 | return false; |
309 | } |
310 | } else { |
311 | dict->retain(); |
312 | } |
313 | if (fPropertyTable) { |
314 | fPropertyTable->release(); |
315 | } |
316 | fPropertyTable = dict; |
317 | } else if (!fPropertyTable) { |
318 | fPropertyTable = OSDictionary::withCapacity( capacity: kIORegCapacityIncrement ); |
319 | if (fPropertyTable) { |
320 | fPropertyTable->setCapacityIncrement( kIORegCapacityIncrement ); |
321 | } |
322 | } |
323 | |
324 | if (!fPropertyTable) { |
325 | return false; |
326 | } |
327 | |
328 | #ifdef IOREGSPLITTABLES |
329 | if (!fRegistryTable) { |
330 | fRegistryTable = OSDictionary::withCapacity( capacity: kIORegCapacityIncrement ); |
331 | assertf(fRegistryTable, "Unable to allocate small capacity" ); |
332 | fRegistryTable->setCapacityIncrement( kIORegCapacityIncrement ); |
333 | } |
334 | |
335 | if ((prop = OSDynamicCast( OSString, getProperty( gIONameKey)))) { |
336 | OSSymbol * sym = (OSSymbol *)OSSymbol::withString( aString: prop); |
337 | // ok for OSSymbol too |
338 | setName( name: sym); |
339 | sym->release(); |
340 | } |
341 | |
342 | #endif /* IOREGSPLITTABLES */ |
343 | |
344 | return true; |
345 | } |
346 | |
347 | bool |
348 | IORegistryEntry::init( IORegistryEntry * old, |
349 | const IORegistryPlane * plane ) |
350 | { |
351 | OSArray * all; |
352 | IORegistryEntry * next; |
353 | unsigned int index; |
354 | |
355 | if (!super::init()) { |
356 | return false; |
357 | } |
358 | |
359 | if (!reserved) { |
360 | reserved = IOMallocType(ExpansionData); |
361 | reserved->fLock = IORecursiveLockAlloc(); |
362 | if (!reserved->fLock) { |
363 | return false; |
364 | } |
365 | } |
366 | |
367 | WLOCK; |
368 | |
369 | reserved->fRegistryEntryID = ++gIORegistryLastID; |
370 | |
371 | fPropertyTable = old->dictionaryWithProperties(); |
372 | #ifdef IOREGSPLITTABLES |
373 | fRegistryTable = OSDictionary::withCapacity( capacity: kIORegCapacityIncrement ); |
374 | assertf(fRegistryTable, "Unable to allocate small capacity" ); |
375 | fRegistryTable->setCapacityIncrement( kIORegCapacityIncrement ); |
376 | |
377 | fRegistryTable->setObject(aKey: gIONameKey, anObject: old->fRegistryTable->getObject(aKey: gIONameKey)); |
378 | fRegistryTable->setObject(aKey: gIOLocationKey, anObject: old->fRegistryTable->getObject(aKey: gIOLocationKey)); |
379 | fRegistryTable->setObject(aKey: plane->nameKey, anObject: old->fRegistryTable->getObject(aKey: plane->nameKey)); |
380 | fRegistryTable->setObject(aKey: plane->pathNameKey, anObject: old->fRegistryTable->getObject(aKey: plane->pathNameKey)); |
381 | fRegistryTable->setObject(aKey: plane->pathLocationKey, anObject: old->fRegistryTable->getObject(aKey: plane->pathLocationKey)); |
382 | fRegistryTable->setObject(aKey: plane->keys[kParentSetIndex], anObject: old->fRegistryTable->getObject(aKey: plane->keys[kParentSetIndex])); |
383 | fRegistryTable->setObject(aKey: plane->keys[kChildSetIndex], anObject: old->fRegistryTable->getObject(aKey: plane->keys[kChildSetIndex])); |
384 | #endif /* IOREGSPLITTABLES */ |
385 | |
386 | old->registryTable()->removeObject( aKey: plane->keys[kParentSetIndex] ); |
387 | old->registryTable()->removeObject( aKey: plane->keys[kChildSetIndex] ); |
388 | |
389 | all = getParentSetReference( plane ); |
390 | if (all) { |
391 | for (index = 0; |
392 | (next = (IORegistryEntry *) all->getObject(index)); |
393 | index++) { |
394 | next->makeLink( to: this, relation: kChildSetIndex, plane ); |
395 | next->breakLink( to: old, relation: kChildSetIndex, plane ); |
396 | } |
397 | } |
398 | |
399 | all = getChildSetReference( plane ); |
400 | if (all) { |
401 | for (index = 0; |
402 | (next = (IORegistryEntry *) all->getObject(index)); |
403 | index++) { |
404 | next->makeLink( to: this, relation: kParentSetIndex, plane ); |
405 | next->breakLink( to: old, relation: kParentSetIndex, plane ); |
406 | } |
407 | } |
408 | |
409 | UNLOCK; |
410 | |
411 | return true; |
412 | } |
413 | |
414 | void |
415 | IORegistryEntry::free( void ) |
416 | { |
417 | #if DEBUG_FREE |
418 | if (registryTable() && gIOServicePlane) { |
419 | if (getParentSetReference( plane: gIOServicePlane ) |
420 | || getChildSetReference( plane: gIOServicePlane )) { |
421 | RLOCK; |
422 | if (getParentSetReference( plane: gIOServicePlane ) |
423 | || getChildSetReference( plane: gIOServicePlane )) { |
424 | panic("%s: attached at free()" , getName()); |
425 | } |
426 | UNLOCK; |
427 | } |
428 | } |
429 | #endif |
430 | |
431 | if (getPropertyTable()) { |
432 | getPropertyTable()->release(); |
433 | } |
434 | |
435 | #ifdef IOREGSPLITTABLES |
436 | if (registryTable()) { |
437 | registryTable()->release(); |
438 | } |
439 | #endif /* IOREGSPLITTABLES */ |
440 | |
441 | if (reserved) { |
442 | OSObject ** array = os_atomic_load(&reserved->fIndexedProperties, acquire); |
443 | if (array) { |
444 | for (int idx = 0; idx < kIORegistryEntryIndexedPropertyCount; idx++) { |
445 | if (array[idx]) { |
446 | array[idx]->release(); |
447 | } |
448 | } |
449 | IODelete(array, OSObject *, kIORegistryEntryIndexedPropertyCount); |
450 | } |
451 | if (reserved->fLock) { |
452 | IORecursiveLockFree(lock: reserved->fLock); |
453 | } |
454 | IOFreeType(reserved, ExpansionData); |
455 | } |
456 | |
457 | super::free(); |
458 | } |
459 | |
460 | void |
461 | IORegistryEntry::setPropertyTable( OSDictionary * dict ) |
462 | { |
463 | PLOCK; |
464 | if (dict) { |
465 | dict->retain(); |
466 | } |
467 | if (fPropertyTable) { |
468 | fPropertyTable->release(); |
469 | } |
470 | |
471 | fPropertyTable = dict; |
472 | PUNLOCK; |
473 | } |
474 | |
475 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
476 | |
477 | /* Wrappers to synchronize property table */ |
478 | |
479 | #define wrap2(type, constant) \ |
480 | OSObject * \ |
481 | IORegistryEntry::copyProperty( type * aKey) constant \ |
482 | { \ |
483 | OSObject * obj; \ |
484 | \ |
485 | PLOCK; \ |
486 | obj = getProperty( aKey ); \ |
487 | if( obj) \ |
488 | obj->retain(); \ |
489 | PUNLOCK; \ |
490 | \ |
491 | return( obj ); \ |
492 | } |
493 | |
494 | #define wrap4(type, constant) \ |
495 | OSObject * \ |
496 | IORegistryEntry::getProperty( type * aKey, \ |
497 | const IORegistryPlane * plane, \ |
498 | IOOptionBits options ) constant \ |
499 | { \ |
500 | OSObject * obj = getProperty( aKey ); \ |
501 | \ |
502 | if ( (NULL == obj) && plane && (options & kIORegistryIterateRecursively) ) { \ |
503 | IORegistryEntry * entry = (IORegistryEntry *) this; \ |
504 | IORegistryIterator * iter; \ |
505 | iter = IORegistryIterator::iterateOver( entry, plane, options ); \ |
506 | \ |
507 | if(iter) { \ |
508 | while ( (NULL == obj) && (entry = iter->getNextObject()) ) { \ |
509 | obj = entry->getProperty( aKey ); \ |
510 | } \ |
511 | iter->release(); \ |
512 | } \ |
513 | } \ |
514 | \ |
515 | return( obj ); \ |
516 | } |
517 | |
518 | #define wrap5(type, constant) \ |
519 | OSObject * \ |
520 | IORegistryEntry::copyProperty( type * aKey, \ |
521 | const IORegistryPlane * plane, \ |
522 | IOOptionBits options ) constant \ |
523 | { \ |
524 | OSObject * obj = copyProperty( aKey ); \ |
525 | \ |
526 | if ( (NULL == obj) && plane && (options & kIORegistryIterateRecursively) ) { \ |
527 | IORegistryEntry * entry = (IORegistryEntry *) this; \ |
528 | IORegistryIterator * iter; \ |
529 | iter = IORegistryIterator::iterateOver( entry, plane, options ); \ |
530 | \ |
531 | if(iter) { \ |
532 | while ( (NULL == obj) && (entry = iter->getNextObject()) ) { \ |
533 | obj = entry->copyProperty( aKey ); \ |
534 | } \ |
535 | iter->release(); \ |
536 | } \ |
537 | } \ |
538 | \ |
539 | return( obj ); \ |
540 | } |
541 | |
542 | bool |
543 | IORegistryEntry::serializeProperties( OSSerialize * s ) const |
544 | { |
545 | // setProperty( getRetainCount(), 32, "__retain" ); |
546 | |
547 | PLOCK; |
548 | OSCollection *snapshotProperties = getPropertyTable()->copyCollection(); |
549 | PUNLOCK; |
550 | |
551 | if (!snapshotProperties) { |
552 | return false; |
553 | } |
554 | |
555 | bool ok = snapshotProperties->serialize( serializer: s ); |
556 | snapshotProperties->release(); |
557 | return ok; |
558 | } |
559 | |
560 | OSArray * |
561 | IORegistryEntry::copyPropertyKeys(void) const |
562 | { |
563 | PLOCK; |
564 | OSArray * keys = getPropertyTable()->copyKeys(); |
565 | PUNLOCK; |
566 | |
567 | return keys; |
568 | } |
569 | |
570 | OSDictionary * |
571 | IORegistryEntry::dictionaryWithProperties( void ) const |
572 | { |
573 | OSDictionary * dict; |
574 | |
575 | PLOCK; |
576 | dict = OSDictionary::withDictionary( dict: getPropertyTable(), |
577 | capacity: getPropertyTable()->getCapacity()); |
578 | PUNLOCK; |
579 | |
580 | return dict; |
581 | } |
582 | |
583 | IOReturn |
584 | IORegistryEntry::setProperties( OSObject * properties ) |
585 | { |
586 | return kIOReturnUnsupported; |
587 | } |
588 | |
589 | wrap2(const OSSymbol, const) // copyProperty() definition |
590 | wrap2(const OSString, const) // copyProperty() definition |
591 | wrap2(const char, const) // copyProperty() definition |
592 | |
593 | wrap4(const OSSymbol, const) // getProperty() w/plane definition |
594 | wrap4(const OSString, const) // getProperty() w/plane definition |
595 | wrap4(const char, const) // getProperty() w/plane definition |
596 | |
597 | wrap5(const OSSymbol, const) // copyProperty() w/plane definition |
598 | wrap5(const OSString, const) // copyProperty() w/plane definition |
599 | wrap5(const char, const) // copyProperty() w/plane definition |
600 | |
601 | |
602 | bool |
603 | IORegistryEntry::propertyExists(const OSSymbol * aKey) |
604 | { |
605 | return NULL != getProperty(aKey); |
606 | } |
607 | |
608 | bool |
609 | IORegistryEntry::propertyExists(const OSString * aKey) |
610 | { |
611 | return NULL != getProperty(aKey); |
612 | } |
613 | |
614 | bool |
615 | IORegistryEntry::propertyExists(const char * aKey) |
616 | { |
617 | return NULL != getProperty(aKey); |
618 | } |
619 | |
620 | |
621 | bool |
622 | IORegistryEntry::propertyHasValue(const OSSymbol * aKey, |
623 | const OSObject * value) |
624 | { |
625 | const OSObject * found; |
626 | bool result; |
627 | |
628 | found = copyProperty(aKey); |
629 | result = (!found && !value) || (found && value && value->isEqualTo(anObject: found)); |
630 | OSSafeReleaseNULL(found); |
631 | return result; |
632 | } |
633 | |
634 | bool |
635 | IORegistryEntry::propertyHasValue(const OSString * aKey, |
636 | const OSObject * value) |
637 | { |
638 | const OSObject * found; |
639 | bool result; |
640 | |
641 | found = copyProperty(aKey); |
642 | result = (!found && !value) || (found && value && value->isEqualTo(anObject: found)); |
643 | OSSafeReleaseNULL(found); |
644 | return result; |
645 | } |
646 | |
647 | bool |
648 | IORegistryEntry::propertyHasValue(const char * aKey, |
649 | const OSObject * value) |
650 | { |
651 | const OSObject * found; |
652 | bool result; |
653 | |
654 | found = copyProperty(aKey); |
655 | result = (!found && !value) || (found && value && value->isEqualTo(anObject: found)); |
656 | OSSafeReleaseNULL(found); |
657 | return result; |
658 | } |
659 | |
660 | |
661 | bool |
662 | IORegistryEntry::propertyExists(const OSSymbol * aKey, |
663 | const IORegistryPlane * plane, |
664 | uint32_t options) const |
665 | { |
666 | return NULL != getProperty(aKey, plane, options); |
667 | } |
668 | |
669 | bool |
670 | IORegistryEntry::propertyExists(const OSString * aKey, |
671 | const IORegistryPlane * plane, |
672 | uint32_t options) const |
673 | { |
674 | return NULL != getProperty(aKey, plane, options); |
675 | } |
676 | bool |
677 | IORegistryEntry::propertyExists(const char * aKey, |
678 | const IORegistryPlane * plane, |
679 | uint32_t options) const |
680 | { |
681 | return NULL != getProperty(aKey, plane, options); |
682 | } |
683 | |
684 | |
685 | bool |
686 | IORegistryEntry::propertyHasValue(const OSSymbol * aKey, |
687 | const OSObject * value, |
688 | const IORegistryPlane * plane, |
689 | uint32_t options) const |
690 | { |
691 | const OSObject * found; |
692 | bool result; |
693 | |
694 | found = copyProperty(aKey, plane, options); |
695 | result = (!found && !value) || (found && value && value->isEqualTo(anObject: found)); |
696 | OSSafeReleaseNULL(found); |
697 | return result; |
698 | } |
699 | |
700 | bool |
701 | IORegistryEntry::propertyHasValue(const OSString * aKey, |
702 | const OSObject * value, |
703 | const IORegistryPlane * plane, |
704 | uint32_t options) const |
705 | { |
706 | const OSObject * found; |
707 | bool result; |
708 | |
709 | found = copyProperty(aKey, plane, options); |
710 | result = (!found && !value) || (found && value && value->isEqualTo(anObject: found)); |
711 | OSSafeReleaseNULL(found); |
712 | return result; |
713 | } |
714 | |
715 | bool |
716 | IORegistryEntry::propertyHasValue(const char * aKey, |
717 | const OSObject * value, |
718 | const IORegistryPlane * plane, |
719 | uint32_t options) const |
720 | { |
721 | const OSObject * found; |
722 | bool result; |
723 | |
724 | found = copyProperty(aKey, plane, options); |
725 | result = (!found && !value) || (found && value && value->isEqualTo(anObject: found)); |
726 | OSSafeReleaseNULL(found); |
727 | return result; |
728 | } |
729 | |
730 | |
731 | OSObject * |
732 | IORegistryEntry::getProperty( const OSSymbol * aKey) const |
733 | { |
734 | OSObject * obj; |
735 | |
736 | PLOCK; |
737 | obj = getPropertyTable()->getObject( aKey ); |
738 | PUNLOCK; |
739 | |
740 | return obj; |
741 | } |
742 | |
743 | void |
744 | IORegistryEntry::removeProperty( const OSSymbol * aKey) |
745 | { |
746 | PLOCK; |
747 | getPropertyTable()->removeObject( aKey ); |
748 | PUNLOCK; |
749 | } |
750 | |
751 | #if KASLR_IOREG_DEBUG |
752 | extern "C" { |
753 | bool ScanForAddrInObject(OSObject * theObject, |
754 | int indent); |
755 | }; /* extern "C" */ |
756 | #endif |
757 | |
758 | bool |
759 | IORegistryEntry::setProperty( const OSSymbol * aKey, OSObject * anObject) |
760 | { |
761 | bool ret = false; |
762 | |
763 | // If we are inserting a collection class and the current entry |
764 | // is attached into the registry (inPlane()) then mark the collection |
765 | // as immutable. |
766 | OSCollection *coll = OSDynamicCast(OSCollection, anObject); |
767 | bool makeImmutable = (coll && inPlane()); |
768 | |
769 | PLOCK; |
770 | if (makeImmutable) { |
771 | coll->setOptions( options: OSCollection::kMASK, mask: OSCollection::kImmutable ); |
772 | } |
773 | |
774 | ret = getPropertyTable()->setObject( aKey, anObject ); |
775 | PUNLOCK; |
776 | |
777 | #if KASLR_IOREG_DEBUG |
778 | if (anObject && strcmp(kIOKitDiagnosticsKey, aKey->getCStringNoCopy()) != 0) { |
779 | if (ScanForAddrInObject(anObject, 0)) { |
780 | IOLog("%s: IORegistryEntry name %s with key \"%s\" \n" , |
781 | __FUNCTION__, |
782 | getName(0), |
783 | aKey->getCStringNoCopy()); |
784 | } |
785 | } |
786 | #endif |
787 | |
788 | return ret; |
789 | } |
790 | |
791 | IOReturn |
792 | IORegistryEntry:: |
793 | runPropertyAction(Action inAction, OSObject *target, |
794 | void *arg0, void *arg1, void *arg2, void *arg3) |
795 | { |
796 | IOReturn res; |
797 | |
798 | // closeGate is recursive so don't worry if we already hold the lock. |
799 | PLOCK; |
800 | res = (*inAction)(target, arg0, arg1, arg2, arg3); |
801 | PUNLOCK; |
802 | |
803 | return res; |
804 | } |
805 | |
806 | static IOReturn |
807 | IORegistryEntryActionToBlock(OSObject *target, |
808 | void *arg0, void *arg1, |
809 | void *arg2, void *arg3) |
810 | { |
811 | IORegistryEntry::ActionBlock block = (typeof(block))arg0; |
812 | return block(); |
813 | } |
814 | |
815 | IOReturn |
816 | IORegistryEntry::runPropertyActionBlock(ActionBlock block) |
817 | { |
818 | IOReturn res; |
819 | |
820 | res = runPropertyAction(inAction: &IORegistryEntryActionToBlock, target: this, arg0: block); |
821 | |
822 | return res; |
823 | } |
824 | |
825 | OSObject * |
826 | IORegistryEntry::getProperty( const OSString * aKey) const |
827 | { |
828 | const OSSymbol * tmpKey = OSSymbol::withString( aString: aKey ); |
829 | OSObject * obj = getProperty( aKey: tmpKey ); |
830 | |
831 | tmpKey->release(); |
832 | return obj; |
833 | } |
834 | |
835 | OSObject * |
836 | IORegistryEntry::getProperty( const char * aKey) const |
837 | { |
838 | const OSSymbol * tmpKey = OSSymbol::withCString( cString: aKey ); |
839 | OSObject * obj = getProperty( aKey: tmpKey ); |
840 | |
841 | tmpKey->release(); |
842 | return obj; |
843 | } |
844 | |
845 | |
846 | void |
847 | IORegistryEntry::removeProperty( const OSString * aKey) |
848 | { |
849 | const OSSymbol * tmpKey = OSSymbol::withString( aString: aKey ); |
850 | removeProperty( aKey: tmpKey ); |
851 | tmpKey->release(); |
852 | } |
853 | |
854 | void |
855 | IORegistryEntry::removeProperty( const char * aKey) |
856 | { |
857 | const OSSymbol * tmpKey = OSSymbol::withCString( cString: aKey ); |
858 | removeProperty( aKey: tmpKey ); |
859 | tmpKey->release(); |
860 | } |
861 | |
862 | bool |
863 | IORegistryEntry::setProperty( const OSString * aKey, OSObject * anObject) |
864 | { |
865 | const OSSymbol * tmpKey = OSSymbol::withString( aString: aKey ); |
866 | bool ret = setProperty( aKey: tmpKey, anObject ); |
867 | |
868 | tmpKey->release(); |
869 | return ret; |
870 | } |
871 | |
872 | bool |
873 | IORegistryEntry::setProperty( const char * aKey, OSObject * anObject) |
874 | { |
875 | const OSSymbol * tmpKey = OSSymbol::withCString( cString: aKey ); |
876 | bool ret = setProperty( aKey: tmpKey, anObject ); |
877 | |
878 | tmpKey->release(); |
879 | return ret; |
880 | } |
881 | |
882 | bool |
883 | IORegistryEntry::setProperty(const char * aKey, const char * aString) |
884 | { |
885 | bool ret = false; |
886 | OSSymbol * aSymbol = (OSSymbol *) OSSymbol::withCString( cString: aString ); |
887 | |
888 | if (aSymbol) { |
889 | const OSSymbol * tmpKey = OSSymbol::withCString( cString: aKey ); |
890 | ret = setProperty( aKey: tmpKey, anObject: aSymbol ); |
891 | |
892 | tmpKey->release(); |
893 | aSymbol->release(); |
894 | } |
895 | return ret; |
896 | } |
897 | |
898 | bool |
899 | IORegistryEntry::setProperty(const char * aKey, bool aBoolean) |
900 | { |
901 | bool ret = false; |
902 | OSBoolean * aBooleanObj = OSBoolean::withBoolean( value: aBoolean ); |
903 | |
904 | if (aBooleanObj) { |
905 | const OSSymbol * tmpKey = OSSymbol::withCString( cString: aKey ); |
906 | ret = setProperty( aKey: tmpKey, anObject: aBooleanObj ); |
907 | |
908 | tmpKey->release(); |
909 | aBooleanObj->release(); |
910 | } |
911 | return ret; |
912 | } |
913 | |
914 | bool |
915 | IORegistryEntry::setProperty( const char * aKey, |
916 | unsigned long long aValue, |
917 | unsigned int aNumberOfBits) |
918 | { |
919 | bool ret = false; |
920 | OSNumber * anOffset = OSNumber::withNumber( value: aValue, numberOfBits: aNumberOfBits ); |
921 | |
922 | if (anOffset) { |
923 | const OSSymbol * tmpKey = OSSymbol::withCString( cString: aKey ); |
924 | ret = setProperty( aKey: tmpKey, anObject: anOffset ); |
925 | |
926 | tmpKey->release(); |
927 | anOffset->release(); |
928 | } |
929 | return ret; |
930 | } |
931 | |
932 | bool |
933 | IORegistryEntry::setProperty( const char * aKey, |
934 | void * bytes, |
935 | unsigned int length) |
936 | { |
937 | bool ret = false; |
938 | OSData * data = OSData::withBytes( bytes, numBytes: length ); |
939 | |
940 | if (data) { |
941 | const OSSymbol * tmpKey = OSSymbol::withCString( cString: aKey ); |
942 | ret = setProperty( aKey: tmpKey, anObject: data ); |
943 | |
944 | tmpKey->release(); |
945 | data->release(); |
946 | } |
947 | return ret; |
948 | } |
949 | |
950 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
951 | |
952 | OSObject * |
953 | IORegistryEntry::setIndexedProperty(uint32_t index, OSObject * anObject) |
954 | { |
955 | OSObject ** array; |
956 | OSObject * prior; |
957 | |
958 | if (index >= kIORegistryEntryIndexedPropertyCount) { |
959 | return NULL; |
960 | } |
961 | |
962 | array = os_atomic_load(&reserved->fIndexedProperties, acquire); |
963 | if (!array) { |
964 | array = IONew(OSObject *, kIORegistryEntryIndexedPropertyCount); |
965 | if (!array) { |
966 | return NULL; |
967 | } |
968 | bzero(s: array, n: kIORegistryEntryIndexedPropertyCount * sizeof(array[0])); |
969 | if (!os_atomic_cmpxchg(&reserved->fIndexedProperties, NULL, array, release)) { |
970 | IODelete(array, OSObject *, kIORegistryEntryIndexedPropertyCount); |
971 | array = os_atomic_load(&reserved->fIndexedProperties, acquire); |
972 | } |
973 | } |
974 | |
975 | if (!array) { |
976 | return NULL; |
977 | } |
978 | |
979 | prior = array[index]; |
980 | if (anObject) { |
981 | anObject->retain(); |
982 | } |
983 | array[index] = anObject; |
984 | |
985 | return prior; |
986 | } |
987 | |
988 | OSObject * |
989 | IORegistryEntry::getIndexedProperty(uint32_t index) const |
990 | { |
991 | if (index >= kIORegistryEntryIndexedPropertyCount) { |
992 | return NULL; |
993 | } |
994 | |
995 | OSObject ** array = os_atomic_load(&reserved->fIndexedProperties, acquire); |
996 | if (!array) { |
997 | return NULL; |
998 | } |
999 | |
1000 | return array[index]; |
1001 | } |
1002 | |
1003 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
1004 | |
1005 | /* Name, location, paths */ |
1006 | |
1007 | const char * |
1008 | IORegistryEntry::getName( const IORegistryPlane * plane ) const |
1009 | { |
1010 | OSSymbol * sym = NULL; |
1011 | |
1012 | RLOCK; |
1013 | if (plane) { |
1014 | sym = (OSSymbol *) registryTable()->getObject( aKey: plane->pathNameKey ); |
1015 | } |
1016 | if (!sym) { |
1017 | sym = (OSSymbol *) registryTable()->getObject( aKey: gIONameKey ); |
1018 | } |
1019 | UNLOCK; |
1020 | |
1021 | if (sym) { |
1022 | return sym->getCStringNoCopy(); |
1023 | } else { |
1024 | return (getMetaClass())->getClassName(); |
1025 | } |
1026 | } |
1027 | |
1028 | const OSSymbol * |
1029 | IORegistryEntry::copyName( |
1030 | const IORegistryPlane * plane ) const |
1031 | { |
1032 | OSSymbol * sym = NULL; |
1033 | |
1034 | RLOCK; |
1035 | if (plane) { |
1036 | sym = (OSSymbol *) registryTable()->getObject( aKey: plane->pathNameKey ); |
1037 | } |
1038 | if (!sym) { |
1039 | sym = (OSSymbol *) registryTable()->getObject( aKey: gIONameKey ); |
1040 | } |
1041 | if (sym) { |
1042 | sym->retain(); |
1043 | } |
1044 | UNLOCK; |
1045 | |
1046 | if (sym) { |
1047 | return sym; |
1048 | } else { |
1049 | return OSSymbol::withCString(cString: (getMetaClass())->getClassName()); |
1050 | } |
1051 | } |
1052 | |
1053 | const OSSymbol * |
1054 | IORegistryEntry::copyLocation( |
1055 | const IORegistryPlane * plane ) const |
1056 | { |
1057 | OSSymbol * sym = NULL; |
1058 | |
1059 | RLOCK; |
1060 | if (plane) { |
1061 | sym = (OSSymbol *) registryTable()->getObject( aKey: plane->pathLocationKey ); |
1062 | } |
1063 | if (!sym) { |
1064 | sym = (OSSymbol *) registryTable()->getObject( aKey: gIOLocationKey ); |
1065 | } |
1066 | if (sym) { |
1067 | sym->retain(); |
1068 | } |
1069 | UNLOCK; |
1070 | |
1071 | return sym; |
1072 | } |
1073 | |
1074 | const char * |
1075 | IORegistryEntry::getLocation( const IORegistryPlane * plane ) const |
1076 | { |
1077 | const OSSymbol * sym = copyLocation( plane ); |
1078 | const char * result = NULL; |
1079 | |
1080 | if (sym) { |
1081 | result = sym->getCStringNoCopy(); |
1082 | sym->release(); |
1083 | } |
1084 | |
1085 | return result; |
1086 | } |
1087 | |
1088 | void |
1089 | IORegistryEntry::setName( const OSSymbol * name, |
1090 | const IORegistryPlane * plane ) |
1091 | { |
1092 | const OSSymbol * key; |
1093 | |
1094 | if (name) { |
1095 | if (plane) { |
1096 | key = plane->pathNameKey; |
1097 | } else { |
1098 | key = gIONameKey; |
1099 | } |
1100 | |
1101 | if (gIOKitTrace && reserved && reserved->fRegistryEntryID) { |
1102 | uint64_t str_id = 0; |
1103 | uint64_t __unused regID = getRegistryEntryID(); |
1104 | kernel_debug_string(IODBG_IOREGISTRY(IOREGISTRYENTRY_NAME_STRING), str_id: &str_id, str: name->getCStringNoCopy()); |
1105 | KERNEL_DEBUG_CONSTANT(IODBG_IOREGISTRY(IOREGISTRYENTRY_NAME), |
1106 | (uintptr_t) regID, |
1107 | (uintptr_t) (regID >> 32), |
1108 | (uintptr_t) str_id, |
1109 | (uintptr_t) (str_id >> 32), |
1110 | 0); |
1111 | } |
1112 | |
1113 | WLOCK; |
1114 | registryTable()->setObject( aKey: key, anObject: (OSObject *) name); |
1115 | UNLOCK; |
1116 | } |
1117 | } |
1118 | |
1119 | void |
1120 | IORegistryEntry::setName( const char * name, |
1121 | const IORegistryPlane * plane ) |
1122 | { |
1123 | OSSymbol * sym = (OSSymbol *)OSSymbol::withCString( cString: name ); |
1124 | if (sym) { |
1125 | setName( name: sym, plane ); |
1126 | sym->release(); |
1127 | } |
1128 | } |
1129 | |
1130 | void |
1131 | IORegistryEntry::setName( const OSString * name, |
1132 | const IORegistryPlane * plane ) |
1133 | { |
1134 | const OSSymbol * sym = OSSymbol::withString( aString: name ); |
1135 | if (sym) { |
1136 | setName( name: sym, plane ); |
1137 | sym->release(); |
1138 | } |
1139 | } |
1140 | |
1141 | void |
1142 | IORegistryEntry::setLocation( const OSSymbol * location, |
1143 | const IORegistryPlane * plane ) |
1144 | { |
1145 | const OSSymbol * key; |
1146 | |
1147 | if (location) { |
1148 | if (plane) { |
1149 | key = plane->pathLocationKey; |
1150 | } else { |
1151 | key = gIOLocationKey; |
1152 | } |
1153 | |
1154 | WLOCK; |
1155 | registryTable()->setObject( aKey: key, anObject: (OSObject *) location); |
1156 | UNLOCK; |
1157 | } |
1158 | } |
1159 | |
1160 | void |
1161 | IORegistryEntry::setLocation( const char * location, |
1162 | const IORegistryPlane * plane ) |
1163 | { |
1164 | OSSymbol * sym = (OSSymbol *)OSSymbol::withCString( cString: location ); |
1165 | if (sym) { |
1166 | setLocation( location: sym, plane ); |
1167 | sym->release(); |
1168 | } |
1169 | } |
1170 | |
1171 | |
1172 | bool |
1173 | IORegistryEntry::compareName( OSString * name, OSString ** matched ) const |
1174 | { |
1175 | const OSSymbol * sym = copyName(); |
1176 | bool isEqual; |
1177 | |
1178 | isEqual = (sym && sym->isEqualTo(anObject: name)); |
1179 | |
1180 | if (isEqual && matched) { |
1181 | name->retain(); |
1182 | *matched = name; |
1183 | } |
1184 | |
1185 | if (sym) { |
1186 | sym->release(); |
1187 | } |
1188 | |
1189 | return isEqual; |
1190 | } |
1191 | |
1192 | bool |
1193 | IORegistryEntry::compareNames( OSObject * names, OSString ** matched ) const |
1194 | { |
1195 | OSString * string; |
1196 | OSCollection * collection; |
1197 | OSIterator * iter = NULL; |
1198 | bool result = false; |
1199 | |
1200 | if ((collection = OSDynamicCast( OSCollection, names))) { |
1201 | iter = OSCollectionIterator::withCollection( inColl: collection ); |
1202 | string = NULL; |
1203 | } else { |
1204 | string = OSDynamicCast( OSString, names); |
1205 | } |
1206 | |
1207 | do { |
1208 | if (string) { |
1209 | result = compareName( name: string, matched ); |
1210 | } |
1211 | } while ((false == result) |
1212 | && iter && (string = OSDynamicCast( OSString, iter->getNextObject()))); |
1213 | |
1214 | if (iter) { |
1215 | iter->release(); |
1216 | } |
1217 | |
1218 | return result; |
1219 | } |
1220 | |
1221 | bool |
1222 | IORegistryEntry::compareName( OSString * name, OSSharedPtr<OSString>& matched ) const |
1223 | { |
1224 | OSString* matchedRaw = NULL; |
1225 | bool result = compareName(name, matched: &matchedRaw); |
1226 | matched.reset(p: matchedRaw, OSNoRetain); |
1227 | return result; |
1228 | } |
1229 | |
1230 | bool |
1231 | IORegistryEntry::compareNames( OSObject * names, OSSharedPtr<OSString>& matched ) const |
1232 | { |
1233 | OSString* matchedRaw = NULL; |
1234 | bool result = compareNames(names, matched: &matchedRaw); |
1235 | matched.reset(p: matchedRaw, OSNoRetain); |
1236 | return result; |
1237 | } |
1238 | |
1239 | bool |
1240 | IORegistryEntry::getPath( char * path, int * length, |
1241 | const IORegistryPlane * plane ) const |
1242 | { |
1243 | OSArray * stack; |
1244 | IORegistryEntry * root; |
1245 | const IORegistryEntry * entry; |
1246 | const IORegistryEntry * parent; |
1247 | const OSSymbol * alias; |
1248 | int index; |
1249 | int len, maxLength, compLen, aliasLen; |
1250 | OSBoundedPtr<char> nextComp; |
1251 | bool ok; |
1252 | size_t init_length; |
1253 | |
1254 | if (!path || !length || !plane) { |
1255 | return false; |
1256 | } |
1257 | |
1258 | len = 0; |
1259 | init_length = *length; |
1260 | maxLength = *length - 2; |
1261 | nextComp = OSBoundedPtr<char>(path, path, path + init_length); |
1262 | |
1263 | len = plane->nameKey->getLength(); |
1264 | if (len >= maxLength) { |
1265 | return false; |
1266 | } |
1267 | strlcpy( dst: nextComp.discard_bounds(), src: plane->nameKey->getCStringNoCopy(), n: len + 1); |
1268 | nextComp[len++] = ':'; |
1269 | nextComp += len; |
1270 | |
1271 | if ((alias = hasAlias( plane ))) { |
1272 | aliasLen = alias->getLength(); |
1273 | len += aliasLen; |
1274 | ok = (maxLength > len); |
1275 | *length = len; |
1276 | if (ok) { |
1277 | strlcpy( dst: nextComp.discard_bounds(), src: alias->getCStringNoCopy(), n: aliasLen + 1); |
1278 | } |
1279 | return ok; |
1280 | } |
1281 | |
1282 | stack = OSArray::withCapacity( capacity: getDepth( plane )); |
1283 | if (!stack) { |
1284 | return false; |
1285 | } |
1286 | |
1287 | RLOCK; |
1288 | |
1289 | parent = entry = this; |
1290 | root = gRegistryRoot->getChildEntry( plane ); |
1291 | while (parent && (parent != root)) { |
1292 | // stop below root |
1293 | entry = parent; |
1294 | parent = entry->getParentEntry( plane ); |
1295 | stack->setObject((OSObject *) entry ); |
1296 | } |
1297 | |
1298 | ok = (NULL != parent); |
1299 | if (ok) { |
1300 | index = stack->getCount(); |
1301 | if (0 == index) { |
1302 | *nextComp++ = '/'; |
1303 | *nextComp = 0; |
1304 | len++; |
1305 | } else { |
1306 | while (ok && ((--index) >= 0)) { |
1307 | entry = (IORegistryEntry *) stack->getObject(index: (unsigned int) index ); |
1308 | assert( entry ); |
1309 | |
1310 | if ((alias = entry->hasAlias( plane ))) { |
1311 | len = plane->nameKey->getLength() + 1; |
1312 | //pointer is to the first argument, with next 2 arguments describing the start and end bounds |
1313 | nextComp = OSBoundedPtr<char>(path + len, path, path + init_length); |
1314 | |
1315 | compLen = alias->getLength(); |
1316 | ok = (maxLength > (len + compLen)); |
1317 | if (ok) { |
1318 | strlcpy( dst: nextComp.discard_bounds(), src: alias->getCStringNoCopy(), n: compLen + 1); |
1319 | } |
1320 | } else { |
1321 | compLen = maxLength - len; |
1322 | ok = entry->getPathComponent( path: nextComp.discard_bounds() + 1, length: &compLen, plane ); |
1323 | |
1324 | if (ok && compLen) { |
1325 | compLen++; |
1326 | *nextComp = '/'; |
1327 | } |
1328 | } |
1329 | |
1330 | if (ok) { |
1331 | len += compLen; |
1332 | nextComp += compLen; |
1333 | } |
1334 | } |
1335 | } |
1336 | *length = len; |
1337 | } |
1338 | UNLOCK; |
1339 | stack->release(); |
1340 | |
1341 | return ok; |
1342 | } |
1343 | |
1344 | bool |
1345 | IORegistryEntry::getPathComponent( char * path, int * length, |
1346 | const IORegistryPlane * plane ) const |
1347 | { |
1348 | int len, locLen, maxLength; |
1349 | const char * compName; |
1350 | const char * loc; |
1351 | bool ok; |
1352 | |
1353 | maxLength = *length; |
1354 | |
1355 | compName = getName( plane ); |
1356 | len = (int) strnlen( s: compName, n: sizeof(io_name_t)); |
1357 | if ((loc = getLocation( plane ))) { |
1358 | locLen = 1 + ((int) strnlen( s: loc, n: sizeof(io_name_t))); |
1359 | } else { |
1360 | locLen = 0; |
1361 | } |
1362 | |
1363 | ok = ((len + locLen + 1) < maxLength); |
1364 | if (ok) { |
1365 | strlcpy( dst: path, src: compName, n: len + 1 ); |
1366 | if (loc) { |
1367 | path += len; |
1368 | len += locLen; |
1369 | *path++ = '@'; |
1370 | strlcpy( dst: path, src: loc, n: locLen ); |
1371 | } |
1372 | *length = len; |
1373 | } |
1374 | |
1375 | return ok; |
1376 | } |
1377 | |
1378 | const char * |
1379 | IORegistryEntry::matchPathLocation( const char * cmp, |
1380 | const IORegistryPlane * plane ) |
1381 | { |
1382 | const char * str; |
1383 | const char * result = NULL; |
1384 | u_quad_t num1, num2; |
1385 | char lastPathChar, lastLocationChar; |
1386 | |
1387 | str = getLocation( plane ); |
1388 | if (str) { |
1389 | lastPathChar = cmp[0]; |
1390 | lastLocationChar = str[0]; |
1391 | do { |
1392 | if (lastPathChar) { |
1393 | num1 = strtouq( cmp, (char **) &cmp, 16 ); |
1394 | lastPathChar = *cmp++; |
1395 | } else { |
1396 | num1 = 0; |
1397 | } |
1398 | |
1399 | if (lastLocationChar) { |
1400 | num2 = strtouq( str, (char **) &str, 16 ); |
1401 | lastLocationChar = *str++; |
1402 | } else { |
1403 | num2 = 0; |
1404 | } |
1405 | |
1406 | if (num1 != num2) { |
1407 | break; |
1408 | } |
1409 | |
1410 | if (!lastPathChar && !lastLocationChar) { |
1411 | result = cmp - 1; |
1412 | break; |
1413 | } |
1414 | |
1415 | if ((',' != lastPathChar) && (':' != lastPathChar)) { |
1416 | lastPathChar = 0; |
1417 | } |
1418 | |
1419 | if (lastPathChar && lastLocationChar && (lastPathChar != lastLocationChar)) { |
1420 | break; |
1421 | } |
1422 | } while (true); |
1423 | } |
1424 | |
1425 | return result; |
1426 | } |
1427 | |
1428 | IORegistryEntry * |
1429 | IORegistryEntry::getChildFromComponent( const char ** opath, |
1430 | const IORegistryPlane * plane ) |
1431 | { |
1432 | IORegistryEntry * entry = NULL; |
1433 | OSArray * set; |
1434 | unsigned int index; |
1435 | const char * path; |
1436 | const char * cmp = NULL; |
1437 | char c; |
1438 | size_t len; |
1439 | const char * str; |
1440 | |
1441 | set = getChildSetReference( plane ); |
1442 | if (set) { |
1443 | path = *opath; |
1444 | |
1445 | for (index = 0; |
1446 | (entry = (IORegistryEntry *) set->getObject(index)); |
1447 | index++) { |
1448 | cmp = path; |
1449 | |
1450 | if (*cmp != '@') { |
1451 | str = entry->getName( plane ); |
1452 | len = strlen( s: str ); |
1453 | if (strncmp( s1: str, s2: cmp, n: len )) { |
1454 | continue; |
1455 | } |
1456 | cmp += len; |
1457 | |
1458 | c = *cmp; |
1459 | if ((c == 0) || (c == '/') || (c == ':')) { |
1460 | break; |
1461 | } |
1462 | if (c != '@') { |
1463 | continue; |
1464 | } |
1465 | } |
1466 | cmp++; |
1467 | if ((cmp = entry->matchPathLocation( cmp, plane ))) { |
1468 | break; |
1469 | } |
1470 | } |
1471 | if (entry) { |
1472 | *opath = cmp; |
1473 | } |
1474 | } |
1475 | |
1476 | return entry; |
1477 | } |
1478 | |
1479 | const OSSymbol * |
1480 | IORegistryEntry::hasAlias( const IORegistryPlane * plane, |
1481 | char * opath, int * length ) const |
1482 | { |
1483 | IORegistryEntry * entry; |
1484 | IORegistryEntry * entry2; |
1485 | const OSSymbol * key; |
1486 | const OSSymbol * bestKey = NULL; |
1487 | OSIterator * iter; |
1488 | OSData * data; |
1489 | const char * path = "/aliases" ; |
1490 | |
1491 | entry = IORegistryEntry::fromPath( path, plane ); |
1492 | if (entry) { |
1493 | RLOCK; |
1494 | if ((iter = OSCollectionIterator::withCollection( |
1495 | inColl: entry->getPropertyTable()))) { |
1496 | while ((key = (OSSymbol *) iter->getNextObject())) { |
1497 | data = (OSData *) entry->getProperty( aKey: key ); |
1498 | path = (const char *) data->getBytesNoCopy(); |
1499 | if ((entry2 = IORegistryEntry::fromPath( path, plane, |
1500 | residualPath: opath, residualLength: length ))) { |
1501 | if (this == entry2) { |
1502 | if (!bestKey |
1503 | || (bestKey->getLength() > key->getLength())) { |
1504 | // pick the smallest alias |
1505 | bestKey = key; |
1506 | } |
1507 | } |
1508 | entry2->release(); |
1509 | } |
1510 | } |
1511 | iter->release(); |
1512 | } |
1513 | entry->release(); |
1514 | UNLOCK; |
1515 | } |
1516 | return bestKey; |
1517 | } |
1518 | |
1519 | const char * |
1520 | IORegistryEntry::dealiasPath( |
1521 | const char ** opath, |
1522 | const IORegistryPlane * plane ) |
1523 | { |
1524 | IORegistryEntry * entry; |
1525 | OSData * data; |
1526 | const char * path = *opath; |
1527 | const char * rpath = NULL; |
1528 | const char * end; |
1529 | char c; |
1530 | char temp[kIOMaxPlaneName + 1]; |
1531 | |
1532 | if (path[0] == '/') { |
1533 | return rpath; |
1534 | } |
1535 | |
1536 | // check for alias |
1537 | end = path; |
1538 | while ((c = *end++) && (c != '/') && (c != ':')) { |
1539 | } |
1540 | end--; |
1541 | if ((end - path) < kIOMaxPlaneName) { |
1542 | strlcpy( dst: temp, src: path, n: end - path + 1 ); |
1543 | |
1544 | RLOCK; |
1545 | entry = IORegistryEntry::fromPath( path: "/aliases" , plane ); |
1546 | if (entry) { |
1547 | data = (OSData *) entry->getProperty( aKey: temp ); |
1548 | if (data) { |
1549 | rpath = (const char *) data->getBytesNoCopy(); |
1550 | if (rpath) { |
1551 | *opath = end; |
1552 | } |
1553 | } |
1554 | entry->release(); |
1555 | } |
1556 | UNLOCK; |
1557 | } |
1558 | |
1559 | return rpath; |
1560 | } |
1561 | |
1562 | IORegistryEntry * |
1563 | IORegistryEntry::fromPath( |
1564 | const char * path, |
1565 | const IORegistryPlane * plane, |
1566 | char * opath, |
1567 | int * length, |
1568 | IORegistryEntry * fromEntry ) |
1569 | { |
1570 | IORegistryEntry * where = NULL; |
1571 | IORegistryEntry * aliasEntry = NULL; |
1572 | IORegistryEntry * next; |
1573 | const char * alias; |
1574 | const char * end; |
1575 | int len = 0; |
1576 | int len2; |
1577 | char c; |
1578 | char temp[kIOMaxPlaneName + 1]; |
1579 | |
1580 | if (NULL == path) { |
1581 | return NULL; |
1582 | } |
1583 | |
1584 | if (NULL == plane) { |
1585 | // get plane name |
1586 | end = strchr( s: path, c: ':' ); |
1587 | if (end && ((end - path) < kIOMaxPlaneName)) { |
1588 | strlcpy( dst: temp, src: path, n: end - path + 1 ); |
1589 | plane = getPlane( name: temp ); |
1590 | path = end + 1; |
1591 | } |
1592 | } |
1593 | if (NULL == plane) { |
1594 | return NULL; |
1595 | } |
1596 | |
1597 | // check for alias |
1598 | end = path; |
1599 | if ((alias = dealiasPath( opath: &end, plane))) { |
1600 | if (length) { |
1601 | len = *length; |
1602 | } |
1603 | aliasEntry = IORegistryEntry::fromPath( path: alias, plane, |
1604 | opath, length: &len, fromEntry ); |
1605 | where = aliasEntry; |
1606 | if (where) { |
1607 | path = end; |
1608 | } else { |
1609 | len = 0; |
1610 | } |
1611 | } |
1612 | |
1613 | RLOCK; |
1614 | |
1615 | do { |
1616 | if (NULL == where) { |
1617 | if ((NULL == fromEntry) && (*path++ == '/')) { |
1618 | fromEntry = gRegistryRoot->getChildEntry( plane ); |
1619 | } |
1620 | where = fromEntry; |
1621 | if (NULL == where) { |
1622 | break; |
1623 | } |
1624 | } else { |
1625 | c = *path++; |
1626 | if (c != '/') { |
1627 | if (c && (c != ':')) { // check valid terminator |
1628 | where = NULL; |
1629 | } |
1630 | break; |
1631 | } |
1632 | } |
1633 | next = where->getChildFromComponent( opath: &path, plane ); |
1634 | if (next) { |
1635 | where = next; |
1636 | } |
1637 | } while (next); |
1638 | |
1639 | if (where) { |
1640 | // check residual path |
1641 | if (where != fromEntry) { |
1642 | path--; |
1643 | } |
1644 | |
1645 | if (opath && length) { |
1646 | // copy out residual path |
1647 | len2 = (int) strnlen(s: path, n: 65536); |
1648 | if ((len + len2) < *length) { |
1649 | strlcpy( dst: opath + len, src: path, n: len2 + 1 ); |
1650 | } |
1651 | *length = (len + len2); |
1652 | } else if (path[0]) { |
1653 | // no residual path => must be no tail for success |
1654 | where = NULL; |
1655 | } |
1656 | } |
1657 | |
1658 | if (where) { |
1659 | where->retain(); |
1660 | } |
1661 | if (aliasEntry) { |
1662 | aliasEntry->release(); |
1663 | } |
1664 | |
1665 | UNLOCK; |
1666 | |
1667 | return where; |
1668 | } |
1669 | |
1670 | IORegistryEntry * |
1671 | IORegistryEntry::childFromPath( |
1672 | const char * path, |
1673 | const IORegistryPlane * plane, |
1674 | char * opath, |
1675 | int * len ) |
1676 | { |
1677 | return IORegistryEntry::fromPath( path, plane, opath, length: len, fromEntry: this ); |
1678 | } |
1679 | |
1680 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
1681 | |
1682 | #define IOLinkIterator OSCollectionIterator |
1683 | |
1684 | #undef super |
1685 | #define super OSObject |
1686 | |
1687 | inline bool |
1688 | IORegistryEntry::arrayMember( OSArray * set, |
1689 | const IORegistryEntry * member, |
1690 | unsigned int * index ) const |
1691 | { |
1692 | int i; |
1693 | OSObject * probeObject; |
1694 | |
1695 | for (i = 0; (probeObject = set->getObject(index: i)); i++) { |
1696 | if (probeObject == (OSObject *) member) { |
1697 | if (index) { |
1698 | *index = i; |
1699 | } |
1700 | return true; |
1701 | } |
1702 | } |
1703 | return false; |
1704 | } |
1705 | |
1706 | bool |
1707 | IORegistryEntry::makeLink( IORegistryEntry * to, |
1708 | unsigned int relation, |
1709 | const IORegistryPlane * plane ) const |
1710 | { |
1711 | OSArray * links; |
1712 | bool result = false; |
1713 | |
1714 | if ((links = (OSArray *) |
1715 | registryTable()->getObject( aKey: plane->keys[relation] ))) { |
1716 | result = arrayMember( set: links, member: to ); |
1717 | if (!result) { |
1718 | result = links->setObject( to ); |
1719 | } |
1720 | } else { |
1721 | links = OSArray::withObjects(objects: (const OSObject **) &to, count: 1, capacity: 1 ); |
1722 | result = (links != NULL); |
1723 | if (result) { |
1724 | result = registryTable()->setObject( aKey: plane->keys[relation], |
1725 | anObject: links ); |
1726 | links->release(); |
1727 | } |
1728 | } |
1729 | reserved->fRegistryEntryGenerationCount++; |
1730 | |
1731 | return result; |
1732 | } |
1733 | |
1734 | void |
1735 | IORegistryEntry::breakLink( IORegistryEntry * to, |
1736 | unsigned int relation, |
1737 | const IORegistryPlane * plane ) const |
1738 | { |
1739 | OSArray * links; |
1740 | unsigned int index; |
1741 | |
1742 | if ((links = (OSArray *) |
1743 | registryTable()->getObject( aKey: plane->keys[relation]))) { |
1744 | if (arrayMember( set: links, member: to, index: &index )) { |
1745 | links->removeObject( index ); |
1746 | if (0 == links->getCount()) { |
1747 | registryTable()->removeObject( aKey: plane->keys[relation]); |
1748 | } |
1749 | } |
1750 | } |
1751 | reserved->fRegistryEntryGenerationCount++; |
1752 | } |
1753 | |
1754 | |
1755 | OSArray * |
1756 | IORegistryEntry::getParentSetReference( |
1757 | const IORegistryPlane * plane ) const |
1758 | { |
1759 | if (plane) { |
1760 | return (OSArray *) registryTable()->getObject( |
1761 | aKey: plane->keys[kParentSetIndex]); |
1762 | } else { |
1763 | return NULL; |
1764 | } |
1765 | } |
1766 | |
1767 | OSIterator * |
1768 | IORegistryEntry::getParentIterator( |
1769 | const IORegistryPlane * plane ) const |
1770 | { |
1771 | OSArray * links; |
1772 | OSIterator * iter; |
1773 | |
1774 | if (!plane) { |
1775 | return NULL; |
1776 | } |
1777 | |
1778 | RLOCK; |
1779 | links = getParentSetReference( plane ); |
1780 | if (NULL == links) { |
1781 | links = OSArray::withCapacity( capacity: 1 ); |
1782 | } else { |
1783 | links = OSArray::withArray( array: links, capacity: links->getCount()); |
1784 | } |
1785 | UNLOCK; |
1786 | |
1787 | iter = IOLinkIterator::withCollection( inColl: links ); |
1788 | |
1789 | if (links) { |
1790 | links->release(); |
1791 | } |
1792 | |
1793 | return iter; |
1794 | } |
1795 | |
1796 | IORegistryEntry * |
1797 | IORegistryEntry::copyParentEntry( const IORegistryPlane * plane ) const |
1798 | { |
1799 | IORegistryEntry * entry = NULL; |
1800 | OSArray * links; |
1801 | |
1802 | RLOCK; |
1803 | |
1804 | if ((links = getParentSetReference( plane ))) { |
1805 | entry = (IORegistryEntry *) links->getObject( index: 0 ); |
1806 | entry->retain(); |
1807 | } |
1808 | |
1809 | UNLOCK; |
1810 | |
1811 | return entry; |
1812 | } |
1813 | |
1814 | IORegistryEntry * |
1815 | IORegistryEntry::getParentEntry( const IORegistryPlane * plane ) const |
1816 | { |
1817 | IORegistryEntry * entry; |
1818 | |
1819 | entry = copyParentEntry( plane ); |
1820 | if (entry) { |
1821 | entry->release(); |
1822 | } |
1823 | |
1824 | return entry; |
1825 | } |
1826 | |
1827 | OSArray * |
1828 | IORegistryEntry::getChildSetReference( const IORegistryPlane * plane ) const |
1829 | { |
1830 | if (plane) { |
1831 | return (OSArray *) registryTable()->getObject( |
1832 | aKey: plane->keys[kChildSetIndex]); |
1833 | } else { |
1834 | return NULL; |
1835 | } |
1836 | } |
1837 | |
1838 | OSIterator * |
1839 | IORegistryEntry::getChildIterator( const IORegistryPlane * plane ) const |
1840 | { |
1841 | OSArray * links; |
1842 | OSIterator * iter; |
1843 | |
1844 | if (!plane) { |
1845 | return NULL; |
1846 | } |
1847 | |
1848 | RLOCK; |
1849 | links = getChildSetReference( plane ); |
1850 | if (NULL == links) { |
1851 | links = OSArray::withCapacity( capacity: 1 ); |
1852 | } else { |
1853 | links = OSArray::withArray( array: links, capacity: links->getCount()); |
1854 | } |
1855 | UNLOCK; |
1856 | |
1857 | iter = IOLinkIterator::withCollection( inColl: links ); |
1858 | |
1859 | if (links) { |
1860 | links->release(); |
1861 | } |
1862 | |
1863 | return iter; |
1864 | } |
1865 | |
1866 | uint32_t |
1867 | IORegistryEntry::getChildCount( const IORegistryPlane * plane ) const |
1868 | { |
1869 | OSArray * links; |
1870 | uint32_t count = 0; |
1871 | |
1872 | RLOCK; |
1873 | links = getChildSetReference( plane ); |
1874 | if (links) { |
1875 | count = links->getCount(); |
1876 | } |
1877 | UNLOCK; |
1878 | |
1879 | return count; |
1880 | } |
1881 | |
1882 | IORegistryEntry * |
1883 | IORegistryEntry::copyChildEntry( |
1884 | const IORegistryPlane * plane ) const |
1885 | { |
1886 | IORegistryEntry * entry = NULL; |
1887 | OSArray * links; |
1888 | |
1889 | RLOCK; |
1890 | |
1891 | if ((links = getChildSetReference( plane ))) { |
1892 | entry = (IORegistryEntry *) links->getObject( index: 0 ); |
1893 | entry->retain(); |
1894 | } |
1895 | |
1896 | UNLOCK; |
1897 | |
1898 | return entry; |
1899 | } |
1900 | |
1901 | // FIXME: Implementation of this function is hidden from the static analyzer. |
1902 | // The analyzer is worried that this release might as well be the last release. |
1903 | // Feel free to remove the #ifndef and address the warning! |
1904 | // See also rdar://problem/63023165. |
1905 | #ifndef __clang_analyzer__ |
1906 | IORegistryEntry * |
1907 | IORegistryEntry::getChildEntry( |
1908 | const IORegistryPlane * plane ) const |
1909 | { |
1910 | IORegistryEntry * entry; |
1911 | |
1912 | entry = copyChildEntry( plane ); |
1913 | if (entry) { |
1914 | entry->release(); |
1915 | } |
1916 | |
1917 | return entry; |
1918 | } |
1919 | #endif // __clang_analyzer__ |
1920 | |
1921 | void |
1922 | IORegistryEntry::applyToChildren( IORegistryEntryApplierFunction applier, |
1923 | void * context, |
1924 | const IORegistryPlane * plane ) const |
1925 | { |
1926 | OSArray * array; |
1927 | unsigned int index; |
1928 | IORegistryEntry * next; |
1929 | |
1930 | if (!plane) { |
1931 | return; |
1932 | } |
1933 | |
1934 | RLOCK; |
1935 | array = OSArray::withArray( array: getChildSetReference( plane )); |
1936 | UNLOCK; |
1937 | if (array) { |
1938 | for (index = 0; |
1939 | (next = (IORegistryEntry *) array->getObject( index )); |
1940 | index++) { |
1941 | (*applier)(next, context); |
1942 | } |
1943 | array->release(); |
1944 | } |
1945 | } |
1946 | |
1947 | void |
1948 | IORegistryEntry::applyToParents( IORegistryEntryApplierFunction applier, |
1949 | void * context, |
1950 | const IORegistryPlane * plane ) const |
1951 | { |
1952 | OSArray * array; |
1953 | unsigned int index; |
1954 | IORegistryEntry * next; |
1955 | |
1956 | if (!plane) { |
1957 | return; |
1958 | } |
1959 | |
1960 | RLOCK; |
1961 | array = OSArray::withArray( array: getParentSetReference( plane )); |
1962 | UNLOCK; |
1963 | if (array) { |
1964 | for (index = 0; |
1965 | (next = (IORegistryEntry *) array->getObject( index )); |
1966 | index++) { |
1967 | (*applier)(next, context); |
1968 | } |
1969 | array->release(); |
1970 | } |
1971 | } |
1972 | |
1973 | bool |
1974 | IORegistryEntry::isChild( IORegistryEntry * child, |
1975 | const IORegistryPlane * plane, |
1976 | bool onlyChild ) const |
1977 | { |
1978 | OSArray * links; |
1979 | bool ret = false; |
1980 | |
1981 | RLOCK; |
1982 | |
1983 | if ((links = getChildSetReference( plane ))) { |
1984 | if ((!onlyChild) || (1 == links->getCount())) { |
1985 | ret = arrayMember( set: links, member: child ); |
1986 | } |
1987 | } |
1988 | if (ret && (links = child->getParentSetReference( plane ))) { |
1989 | ret = arrayMember( set: links, member: this ); |
1990 | } |
1991 | |
1992 | UNLOCK; |
1993 | |
1994 | return ret; |
1995 | } |
1996 | |
1997 | bool |
1998 | IORegistryEntry::isParent( IORegistryEntry * parent, |
1999 | const IORegistryPlane * plane, |
2000 | bool onlyParent ) const |
2001 | { |
2002 | OSArray * links; |
2003 | bool ret = false; |
2004 | |
2005 | RLOCK; |
2006 | |
2007 | if ((links = getParentSetReference( plane ))) { |
2008 | if ((!onlyParent) || (1 == links->getCount())) { |
2009 | ret = arrayMember( set: links, member: parent ); |
2010 | } |
2011 | } |
2012 | if (ret && (links = parent->getChildSetReference( plane ))) { |
2013 | ret = arrayMember( set: links, member: this ); |
2014 | } |
2015 | |
2016 | UNLOCK; |
2017 | |
2018 | return ret; |
2019 | } |
2020 | |
2021 | bool |
2022 | IORegistryEntry::inPlane( const IORegistryPlane * plane ) const |
2023 | { |
2024 | bool ret; |
2025 | |
2026 | RLOCK; |
2027 | |
2028 | if (plane) { |
2029 | ret = (NULL != getParentSetReference( plane )); |
2030 | } else { |
2031 | // Check to see if this is in any plane. If it is in a plane |
2032 | // then the registryTable will contain a key with the ParentLinks |
2033 | // suffix. When we iterate over the keys looking for that suffix |
2034 | ret = false; |
2035 | |
2036 | OSCollectionIterator *iter = |
2037 | OSCollectionIterator::withCollection( registryTable()); |
2038 | if (iter) { |
2039 | const OSSymbol *key; |
2040 | |
2041 | while ((key = (OSSymbol *) iter->getNextObject())) { |
2042 | size_t keysuffix; |
2043 | |
2044 | // Get a pointer to this keys suffix |
2045 | keysuffix = key->getLength(); |
2046 | if (keysuffix <= kIORegPlaneParentSuffixLen) { |
2047 | continue; |
2048 | } |
2049 | keysuffix -= kIORegPlaneParentSuffixLen; |
2050 | if (!strncmp(s1: key->getCStringNoCopy() + keysuffix, |
2051 | kIORegPlaneParentSuffix, |
2052 | kIORegPlaneParentSuffixLen + 1)) { |
2053 | ret = true; |
2054 | break; |
2055 | } |
2056 | } |
2057 | iter->release(); |
2058 | } |
2059 | } |
2060 | |
2061 | UNLOCK; |
2062 | |
2063 | return ret; |
2064 | } |
2065 | |
2066 | bool |
2067 | IORegistryEntry::attachToParent( IORegistryEntry * parent, |
2068 | const IORegistryPlane * plane ) |
2069 | { |
2070 | OSArray * links; |
2071 | bool ret; |
2072 | bool needParent; |
2073 | bool traceName = false; |
2074 | |
2075 | if (this == parent) { |
2076 | return false; |
2077 | } |
2078 | |
2079 | WLOCK; |
2080 | |
2081 | if (!reserved->fRegistryEntryID) { |
2082 | reserved->fRegistryEntryID = ++gIORegistryLastID; |
2083 | traceName = (0 != gIOKitTrace); |
2084 | } |
2085 | |
2086 | ret = makeLink( to: parent, relation: kParentSetIndex, plane ); |
2087 | |
2088 | if ((links = parent->getChildSetReference( plane ))) { |
2089 | needParent = (false == arrayMember( set: links, member: this )); |
2090 | } else { |
2091 | needParent = true; |
2092 | } |
2093 | if (needParent) { |
2094 | ret &= parent->makeLink(to: this, relation: kChildSetIndex, plane); |
2095 | } |
2096 | |
2097 | UNLOCK; |
2098 | |
2099 | if (traceName) { |
2100 | uint64_t str_id = 0; |
2101 | uint64_t __unused regID = getRegistryEntryID(); |
2102 | kernel_debug_string(IODBG_IOREGISTRY(IOREGISTRYENTRY_NAME_STRING), str_id: &str_id, str: getName()); |
2103 | KERNEL_DEBUG_CONSTANT(IODBG_IOREGISTRY(IOREGISTRYENTRY_NAME), |
2104 | (uintptr_t) regID, |
2105 | (uintptr_t) (regID >> 32), |
2106 | (uintptr_t) str_id, |
2107 | (uintptr_t) (str_id >> 32), |
2108 | 0); |
2109 | } |
2110 | |
2111 | PLOCK; |
2112 | |
2113 | // Mark any collections in the property list as immutable |
2114 | OSDictionary *ptable = getPropertyTable(); |
2115 | OSCollectionIterator *iter = |
2116 | OSCollectionIterator::withCollection( inColl: ptable ); |
2117 | if (iter) { |
2118 | const OSSymbol *key; |
2119 | |
2120 | while ((key = (OSSymbol *) iter->getNextObject())) { |
2121 | // Is object for key a collection? |
2122 | OSCollection *coll = |
2123 | OSDynamicCast( OSCollection, ptable->getObject( key )); |
2124 | |
2125 | if (coll) { |
2126 | // Yup so mark it as immutable |
2127 | coll->setOptions( options: OSCollection::kMASK, |
2128 | mask: OSCollection::kImmutable ); |
2129 | } |
2130 | } |
2131 | iter->release(); |
2132 | } |
2133 | |
2134 | PUNLOCK; |
2135 | |
2136 | if (needParent) { |
2137 | ret &= parent->attachToChild( child: this, plane ); |
2138 | } |
2139 | |
2140 | return ret; |
2141 | } |
2142 | |
2143 | uint64_t |
2144 | IORegistryEntry::getRegistryEntryID( void ) |
2145 | { |
2146 | if (reserved) { |
2147 | return reserved->fRegistryEntryID; |
2148 | } else { |
2149 | return 0; |
2150 | } |
2151 | } |
2152 | |
2153 | bool |
2154 | IORegistryEntry::attachToChild( IORegistryEntry * child, |
2155 | const IORegistryPlane * plane ) |
2156 | { |
2157 | OSArray * links; |
2158 | bool ret; |
2159 | bool needChild; |
2160 | |
2161 | if (this == child) { |
2162 | return false; |
2163 | } |
2164 | |
2165 | WLOCK; |
2166 | |
2167 | ret = makeLink( to: child, relation: kChildSetIndex, plane ); |
2168 | |
2169 | if ((links = child->getParentSetReference( plane ))) { |
2170 | needChild = (false == arrayMember( set: links, member: this )); |
2171 | } else { |
2172 | needChild = true; |
2173 | } |
2174 | if (needChild) { |
2175 | ret &= child->makeLink(to: this, relation: kParentSetIndex, plane); |
2176 | } |
2177 | |
2178 | UNLOCK; |
2179 | |
2180 | if (needChild) { |
2181 | ret &= child->attachToParent( parent: this, plane ); |
2182 | } |
2183 | |
2184 | return ret; |
2185 | } |
2186 | |
2187 | void |
2188 | IORegistryEntry::detachFromParent( IORegistryEntry * parent, |
2189 | const IORegistryPlane * plane ) |
2190 | { |
2191 | OSArray * links; |
2192 | bool needParent; |
2193 | |
2194 | WLOCK; |
2195 | |
2196 | parent->retain(); |
2197 | |
2198 | breakLink( to: parent, relation: kParentSetIndex, plane ); |
2199 | |
2200 | if ((links = parent->getChildSetReference( plane ))) { |
2201 | needParent = arrayMember( set: links, member: this ); |
2202 | } else { |
2203 | needParent = false; |
2204 | } |
2205 | if (needParent) { |
2206 | parent->breakLink( to: this, relation: kChildSetIndex, plane ); |
2207 | } |
2208 | |
2209 | UNLOCK; |
2210 | |
2211 | if (needParent) { |
2212 | parent->detachFromChild( child: this, plane ); |
2213 | } |
2214 | |
2215 | parent->release(); |
2216 | } |
2217 | |
2218 | void |
2219 | IORegistryEntry::detachFromChild( IORegistryEntry * child, |
2220 | const IORegistryPlane * plane ) |
2221 | { |
2222 | OSArray * links; |
2223 | bool needChild; |
2224 | |
2225 | WLOCK; |
2226 | |
2227 | child->retain(); |
2228 | |
2229 | breakLink( to: child, relation: kChildSetIndex, plane ); |
2230 | |
2231 | if ((links = child->getParentSetReference( plane ))) { |
2232 | needChild = arrayMember( set: links, member: this ); |
2233 | } else { |
2234 | needChild = false; |
2235 | } |
2236 | if (needChild) { |
2237 | child->breakLink( to: this, relation: kParentSetIndex, plane ); |
2238 | } |
2239 | |
2240 | UNLOCK; |
2241 | |
2242 | if (needChild) { |
2243 | child->detachFromParent( parent: this, plane ); |
2244 | } |
2245 | |
2246 | child->release(); |
2247 | } |
2248 | |
2249 | void |
2250 | IORegistryEntry::detachAbove( const IORegistryPlane * plane ) |
2251 | { |
2252 | IORegistryEntry * parent; |
2253 | |
2254 | retain(); |
2255 | while ((parent = copyParentEntry( plane ))) { |
2256 | detachFromParent( parent, plane ); |
2257 | parent->release(); |
2258 | } |
2259 | release(); |
2260 | } |
2261 | |
2262 | void |
2263 | IORegistryEntry::detachAll( const IORegistryPlane * plane ) |
2264 | { |
2265 | OSOrderedSet * all; |
2266 | IORegistryEntry * next; |
2267 | IORegistryIterator * regIter; |
2268 | |
2269 | regIter = IORegistryIterator::iterateOver( start: this, plane, options: true ); |
2270 | if (NULL == regIter) { |
2271 | return; |
2272 | } |
2273 | all = regIter->iterateAll(); |
2274 | regIter->release(); |
2275 | |
2276 | detachAbove( plane ); |
2277 | if (all) { |
2278 | while ((next = (IORegistryEntry *) all->getLastObject())) { |
2279 | next->retain(); |
2280 | all->removeObject(anObject: next); |
2281 | |
2282 | next->detachAbove( plane ); |
2283 | next->release(); |
2284 | } |
2285 | all->release(); |
2286 | } |
2287 | } |
2288 | |
2289 | unsigned int |
2290 | IORegistryEntry::getDepth( const IORegistryPlane * plane ) const |
2291 | { |
2292 | unsigned int depth = 1; |
2293 | OSArray * parents; |
2294 | unsigned int oneDepth, maxParentDepth, count; |
2295 | IORegistryEntry * one; |
2296 | const IORegistryEntry * next; |
2297 | unsigned int index; |
2298 | |
2299 | RLOCK; |
2300 | |
2301 | next = this; |
2302 | while ((parents = next->getParentSetReference( plane ))) { |
2303 | count = parents->getCount(); |
2304 | if (0 == count) { |
2305 | break; |
2306 | } |
2307 | if (1 == count) { |
2308 | depth++; |
2309 | next = (IORegistryEntry *) parents->getObject( index: 0 ); |
2310 | } else { |
2311 | // painful |
2312 | maxParentDepth = 0; |
2313 | for (index = 0; |
2314 | (one = (IORegistryEntry *) parents->getObject( index )); |
2315 | index++) { |
2316 | oneDepth = one->getDepth( plane ); |
2317 | if (oneDepth > maxParentDepth) { |
2318 | maxParentDepth = oneDepth; |
2319 | } |
2320 | } |
2321 | depth += maxParentDepth; |
2322 | break; |
2323 | } |
2324 | } |
2325 | |
2326 | UNLOCK; |
2327 | |
2328 | return depth; |
2329 | } |
2330 | |
2331 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
2332 | |
2333 | #undef super |
2334 | #define super OSIterator |
2335 | |
2336 | OSDefineMetaClassAndStructors(IORegistryIterator, OSIterator) |
2337 | |
2338 | enum { kIORegistryIteratorInvalidFlag = 0x80000000 }; |
2339 | |
2340 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
2341 | |
2342 | IORegistryIterator * |
2343 | IORegistryIterator::iterateOver( IORegistryEntry * root, |
2344 | const IORegistryPlane * plane, |
2345 | IOOptionBits options ) |
2346 | { |
2347 | IORegistryIterator * create; |
2348 | |
2349 | if (NULL == root) { |
2350 | return NULL; |
2351 | } |
2352 | if (NULL == plane) { |
2353 | return NULL; |
2354 | } |
2355 | |
2356 | create = new IORegistryIterator; |
2357 | if (create) { |
2358 | if (create->init()) { |
2359 | root->retain(); |
2360 | create->root = root; |
2361 | create->where = &create->start; |
2362 | create->start.current = root; |
2363 | create->plane = plane; |
2364 | create->options = options & ~kIORegistryIteratorInvalidFlag; |
2365 | } else { |
2366 | create->release(); |
2367 | create = NULL; |
2368 | } |
2369 | } |
2370 | return create; |
2371 | } |
2372 | |
2373 | IORegistryIterator * |
2374 | IORegistryIterator::iterateOver( const IORegistryPlane * plane, |
2375 | IOOptionBits options ) |
2376 | { |
2377 | return iterateOver( root: gRegistryRoot, plane, options ); |
2378 | } |
2379 | |
2380 | bool |
2381 | IORegistryIterator::isValid( void ) |
2382 | { |
2383 | bool ok; |
2384 | IORegCursor * next; |
2385 | |
2386 | next = where; |
2387 | |
2388 | RLOCK; |
2389 | |
2390 | ok = (0 == (kIORegistryIteratorInvalidFlag & options)); |
2391 | |
2392 | while (ok && next) { |
2393 | if (where->iter) { |
2394 | ok = where->iter->isValid(); |
2395 | } |
2396 | next = next->next; |
2397 | } |
2398 | UNLOCK; |
2399 | |
2400 | return ok; |
2401 | } |
2402 | |
2403 | void |
2404 | IORegistryIterator::enterEntry( const IORegistryPlane * enterPlane ) |
2405 | { |
2406 | IORegCursor * prev; |
2407 | |
2408 | prev = where; |
2409 | where = IOMallocType(IORegCursor); |
2410 | assert( where); |
2411 | |
2412 | if (where) { |
2413 | where->iter = NULL; |
2414 | where->next = prev; |
2415 | where->current = prev->current; |
2416 | plane = enterPlane; |
2417 | } |
2418 | } |
2419 | |
2420 | void |
2421 | IORegistryIterator::enterEntry( void ) |
2422 | { |
2423 | enterEntry( enterPlane: plane ); |
2424 | } |
2425 | |
2426 | bool |
2427 | IORegistryIterator::exitEntry( void ) |
2428 | { |
2429 | IORegCursor * gone; |
2430 | |
2431 | if (where->iter) { |
2432 | where->iter->release(); |
2433 | where->iter = NULL; |
2434 | if (where->current) {// && (where != &start)) |
2435 | where->current->release(); |
2436 | } |
2437 | } |
2438 | |
2439 | if (where != &start) { |
2440 | gone = where; |
2441 | where = gone->next; |
2442 | IOFreeType(gone, IORegCursor); |
2443 | return true; |
2444 | } else { |
2445 | return false; |
2446 | } |
2447 | } |
2448 | |
2449 | void |
2450 | IORegistryIterator::reset( void ) |
2451 | { |
2452 | while (exitEntry()) { |
2453 | } |
2454 | |
2455 | if (done) { |
2456 | done->release(); |
2457 | done = NULL; |
2458 | } |
2459 | |
2460 | where->current = root; |
2461 | options &= ~kIORegistryIteratorInvalidFlag; |
2462 | } |
2463 | |
2464 | void |
2465 | IORegistryIterator::free( void ) |
2466 | { |
2467 | reset(); |
2468 | |
2469 | if (root) { |
2470 | root->release(); |
2471 | } |
2472 | |
2473 | super::free(); |
2474 | } |
2475 | |
2476 | |
2477 | IORegistryEntry * |
2478 | IORegistryIterator::getNextObjectFlat( void ) |
2479 | { |
2480 | IORegistryEntry * next = NULL; |
2481 | OSArray * links = NULL; |
2482 | |
2483 | RLOCK; |
2484 | |
2485 | if ((NULL == where->iter)) { |
2486 | // just entered - create new iter |
2487 | if (isValid() |
2488 | && where->current |
2489 | && (links = ((options & kIORegistryIterateParents) ? |
2490 | where->current->getParentSetReference( plane ) : |
2491 | where->current->getChildSetReference( plane )))) { |
2492 | where->iter = OSCollectionIterator::withCollection( inColl: links ); |
2493 | } |
2494 | } else |
2495 | // next sibling - release current |
2496 | if (where->current) { |
2497 | where->current->release(); |
2498 | } |
2499 | |
2500 | if (where->iter) { |
2501 | next = (IORegistryEntry *) where->iter->getNextObject(); |
2502 | |
2503 | if (next) { |
2504 | next->retain(); |
2505 | } else if (!where->iter->isValid()) { |
2506 | options |= kIORegistryIteratorInvalidFlag; |
2507 | } |
2508 | } |
2509 | |
2510 | where->current = next; |
2511 | |
2512 | UNLOCK; |
2513 | |
2514 | return next; |
2515 | } |
2516 | |
2517 | IORegistryEntry * |
2518 | IORegistryIterator::getNextObjectRecursive( void ) |
2519 | { |
2520 | IORegistryEntry * next; |
2521 | |
2522 | do{ |
2523 | next = getNextObjectFlat(); |
2524 | } while ((NULL == next) && exitEntry()); |
2525 | |
2526 | if (next) { |
2527 | if (NULL == done) { |
2528 | done = OSOrderedSet::withCapacity( capacity: 10 ); |
2529 | } |
2530 | if (done->setObject((OSObject *) next)) { |
2531 | // done set didn't contain this one, so recurse |
2532 | enterEntry(); |
2533 | } |
2534 | } |
2535 | return next; |
2536 | } |
2537 | |
2538 | IORegistryEntry * |
2539 | IORegistryIterator::getNextObject( void ) |
2540 | { |
2541 | if (options & kIORegistryIterateRecursively) { |
2542 | return getNextObjectRecursive(); |
2543 | } else { |
2544 | return getNextObjectFlat(); |
2545 | } |
2546 | } |
2547 | |
2548 | IORegistryEntry * |
2549 | IORegistryIterator::getCurrentEntry( void ) |
2550 | { |
2551 | if (isValid()) { |
2552 | return where->current; |
2553 | } else { |
2554 | return NULL; |
2555 | } |
2556 | } |
2557 | |
2558 | OSOrderedSet * |
2559 | IORegistryIterator::iterateAll( void ) |
2560 | { |
2561 | reset(); |
2562 | while (getNextObjectRecursive()) { |
2563 | } |
2564 | if (done) { |
2565 | done->retain(); |
2566 | } |
2567 | return done; |
2568 | } |
2569 | |
2570 | #if __LP64__ |
2571 | OSMetaClassDefineReservedUnused(IORegistryEntry, 0); |
2572 | OSMetaClassDefineReservedUnused(IORegistryEntry, 1); |
2573 | OSMetaClassDefineReservedUnused(IORegistryEntry, 2); |
2574 | OSMetaClassDefineReservedUnused(IORegistryEntry, 3); |
2575 | OSMetaClassDefineReservedUnused(IORegistryEntry, 4); |
2576 | OSMetaClassDefineReservedUnused(IORegistryEntry, 5); |
2577 | #else |
2578 | OSMetaClassDefineReservedUsedX86(IORegistryEntry, 0); |
2579 | OSMetaClassDefineReservedUsedX86(IORegistryEntry, 1); |
2580 | OSMetaClassDefineReservedUsedX86(IORegistryEntry, 2); |
2581 | OSMetaClassDefineReservedUsedX86(IORegistryEntry, 3); |
2582 | OSMetaClassDefineReservedUsedX86(IORegistryEntry, 4); |
2583 | OSMetaClassDefineReservedUsedX86(IORegistryEntry, 5); |
2584 | #endif |
2585 | OSMetaClassDefineReservedUnused(IORegistryEntry, 6); |
2586 | OSMetaClassDefineReservedUnused(IORegistryEntry, 7); |
2587 | OSMetaClassDefineReservedUnused(IORegistryEntry, 8); |
2588 | OSMetaClassDefineReservedUnused(IORegistryEntry, 9); |
2589 | OSMetaClassDefineReservedUnused(IORegistryEntry, 10); |
2590 | OSMetaClassDefineReservedUnused(IORegistryEntry, 11); |
2591 | OSMetaClassDefineReservedUnused(IORegistryEntry, 12); |
2592 | OSMetaClassDefineReservedUnused(IORegistryEntry, 13); |
2593 | OSMetaClassDefineReservedUnused(IORegistryEntry, 14); |
2594 | OSMetaClassDefineReservedUnused(IORegistryEntry, 15); |
2595 | OSMetaClassDefineReservedUnused(IORegistryEntry, 16); |
2596 | OSMetaClassDefineReservedUnused(IORegistryEntry, 17); |
2597 | OSMetaClassDefineReservedUnused(IORegistryEntry, 18); |
2598 | OSMetaClassDefineReservedUnused(IORegistryEntry, 19); |
2599 | OSMetaClassDefineReservedUnused(IORegistryEntry, 20); |
2600 | OSMetaClassDefineReservedUnused(IORegistryEntry, 21); |
2601 | OSMetaClassDefineReservedUnused(IORegistryEntry, 22); |
2602 | OSMetaClassDefineReservedUnused(IORegistryEntry, 23); |
2603 | OSMetaClassDefineReservedUnused(IORegistryEntry, 24); |
2604 | OSMetaClassDefineReservedUnused(IORegistryEntry, 25); |
2605 | OSMetaClassDefineReservedUnused(IORegistryEntry, 26); |
2606 | OSMetaClassDefineReservedUnused(IORegistryEntry, 27); |
2607 | OSMetaClassDefineReservedUnused(IORegistryEntry, 28); |
2608 | OSMetaClassDefineReservedUnused(IORegistryEntry, 29); |
2609 | OSMetaClassDefineReservedUnused(IORegistryEntry, 30); |
2610 | OSMetaClassDefineReservedUnused(IORegistryEntry, 31); |
2611 | |
2612 | /* inline function implementation */ |
2613 | OSDictionary * |
2614 | IORegistryEntry::getPropertyTable( void ) const |
2615 | { |
2616 | return fPropertyTable; |
2617 | } |
2618 | |