2 * Copyright (c) 2000-2012 Apple 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 #include <mach/kmod.h>
30 #include <libkern/kernel_mach_header.h>
31 #include <libkern/prelink.h>
34 #include <libkern/version.h>
35 #include <libkern/c++/OSContainers.h>
36 #include <libkern/OSKextLibPrivate.h>
37 #include <libkern/c++/OSKext.h>
38 #include <IOKit/IOLib.h>
39 #include <IOKit/IOService.h>
40 #include <IOKit/IODeviceTreeSupport.h>
41 #include <IOKit/IOCatalogue.h>
44 #define KASLR_KEXT_DEBUG 0
48 #pragma mark Bootstrap Declarations
50 /*********************************************************************
51 * Bootstrap Declarations
53 * The ENTIRE point of the libsa/KLD segment is to isolate bootstrap
54 * code from other parts of the kernel, so function symbols are not
55 * exported; rather pointers to those functions are exported.
57 * xxx - need to think about locking for handling the 'weak' refs.
58 * xxx - do export a non-KLD function that says you've called a
59 * xxx - bootstrap function that has been removed.
61 * ALL call-ins to this segment of the kernel must be done through
62 * exported pointers. The symbols themselves are private and not to
64 *********************************************************************/
66 extern void (*record_startup_extensions_function
)(void);
67 extern void (*load_security_extensions_function
)(void);
70 static void bootstrapRecordStartupExtensions(void);
71 static void bootstrapLoadSecurityExtensions(void);
75 extern "C" bool IORamDiskBSDRoot(void);
81 /*********************************************************************
83 *********************************************************************/
84 #define CONST_STRLEN(str) (sizeof(str) - 1)
87 #pragma mark Kernel Component Kext Identifiers
89 /*********************************************************************
90 * Kernel Component Kext Identifiers
92 * We could have each kernel resource kext automatically "load" as
93 * it's created, but it's nicer to have them listed in kextstat in
94 * the order of this list. We'll walk through this after setting up
95 * all the boot kexts and have them load up.
96 *********************************************************************/
97 static const char * sKernelComponentNames
[] = {
98 // The kexts for these IDs must have a version matching 'osrelease'.
101 "com.apple.kpi.dsep",
102 "com.apple.kpi.iokit",
103 "com.apple.kpi.libkern",
104 "com.apple.kpi.mach",
105 "com.apple.kpi.private",
106 "com.apple.kpi.unsupported",
107 "com.apple.iokit.IONVRAMFamily",
108 "com.apple.driver.AppleNMI",
109 "com.apple.iokit.IOSystemManagementFamily",
110 "com.apple.iokit.ApplePlatformFamily",
115 #pragma mark KLDBootstrap Class
117 /*********************************************************************
120 * We use a C++ class here so that it can be a friend of OSKext and
121 * get at private stuff. We can't hide the class itself, but we can
122 * hide the instance through which we invoke the functions.
123 *********************************************************************/
125 friend void bootstrapRecordStartupExtensions(void);
126 friend void bootstrapLoadSecurityExtensions(void);
129 void readStartupExtensions(void);
131 void readPrelinkedExtensions(
132 kernel_section_t
* prelinkInfoSect
);
133 void readBooterExtensions(void);
135 OSReturn
loadKernelComponentKexts(void);
136 void loadKernelExternalComponents(void);
137 void readBuiltinPersonalities(void);
139 void loadSecurityExtensions(void);
146 static KLDBootstrap sBootstrapObject
;
148 /*********************************************************************
149 * Set the function pointers for the entry points into the bootstrap
150 * segment upon C++ static constructor invocation.
151 *********************************************************************/
152 KLDBootstrap::KLDBootstrap(void)
154 if (this != &sBootstrapObject
) {
155 panic("Attempt to access bootstrap segment.");
157 record_startup_extensions_function
= &bootstrapRecordStartupExtensions
;
158 load_security_extensions_function
= &bootstrapLoadSecurityExtensions
;
161 /*********************************************************************
162 * Clear the function pointers for the entry points into the bootstrap
163 * segment upon C++ static destructor invocation.
164 *********************************************************************/
165 KLDBootstrap::~KLDBootstrap(void)
167 if (this != &sBootstrapObject
) {
168 panic("Attempt to access bootstrap segment.");
172 record_startup_extensions_function
= 0;
173 load_security_extensions_function
= 0;
176 /*********************************************************************
177 *********************************************************************/
179 KLDBootstrap::readStartupExtensions(void)
181 kernel_section_t
* prelinkInfoSect
= NULL
; // do not free
183 OSKextLog(/* kext */ NULL
,
184 kOSKextLogProgressLevel
|
185 kOSKextLogGeneralFlag
| kOSKextLogDirectoryScanFlag
|
186 kOSKextLogKextBookkeepingFlag
,
187 "Reading startup extensions.");
189 /* If the prelink info segment has a nonzero size, we are prelinked
190 * and won't have any individual kexts or mkexts to read.
191 * Otherwise, we need to read kexts or the mkext from what the booter
194 prelinkInfoSect
= getsectbyname(kPrelinkInfoSegment
, kPrelinkInfoSection
);
195 if (prelinkInfoSect
->size
) {
196 readPrelinkedExtensions(prelinkInfoSect
);
198 readBooterExtensions();
201 loadKernelComponentKexts();
202 loadKernelExternalComponents();
203 readBuiltinPersonalities();
204 OSKext::sendAllKextPersonalitiesToCatalog();
209 /*********************************************************************
210 *********************************************************************/
212 KLDBootstrap::readPrelinkedExtensions(
213 kernel_section_t
* prelinkInfoSect
)
215 OSArray
* infoDictArray
= NULL
; // do not release
216 OSObject
* parsedXML
= NULL
; // must release
217 OSDictionary
* prelinkInfoDict
= NULL
; // do not release
218 OSString
* errorString
= NULL
; // must release
219 OSKext
* theKernel
= NULL
; // must release
221 kernel_segment_command_t
* prelinkTextSegment
= NULL
; // see code
222 kernel_segment_command_t
* prelinkInfoSegment
= NULL
; // see code
224 /* We make some copies of data, but if anything fails we're basically
225 * going to fail the boot, so these won't be cleaned up on error.
227 void * prelinkData
= NULL
; // see code
228 vm_size_t prelinkLength
= 0;
231 OSDictionary
* infoDict
= NULL
; // do not release
233 IORegistryEntry
* registryRoot
= NULL
; // do not release
234 OSNumber
* prelinkCountObj
= NULL
; // must release
239 bool developerDevice
;
243 OSKextLog(/* kext */ NULL
,
244 kOSKextLogProgressLevel
|
245 kOSKextLogDirectoryScanFlag
| kOSKextLogArchiveFlag
,
246 "Starting from prelinked kernel.");
248 prelinkTextSegment
= getsegbyname(kPrelinkTextSegment
);
249 if (!prelinkTextSegment
) {
250 OSKextLog(/* kext */ NULL
,
251 kOSKextLogErrorLevel
|
252 kOSKextLogDirectoryScanFlag
| kOSKextLogArchiveFlag
,
253 "Can't find prelinked kexts' text segment.");
258 unsigned long scratchSize
;
259 vm_offset_t scratchAddr
;
261 IOLog("kaslr: prelinked kernel address info: \n");
263 scratchAddr
= (vm_offset_t
) getsegdatafromheader(&_mh_execute_header
, "__TEXT", &scratchSize
);
264 IOLog("kaslr: start 0x%lx end 0x%lx length %lu for __TEXT \n",
265 (unsigned long)scratchAddr
,
266 (unsigned long)(scratchAddr
+ scratchSize
),
269 scratchAddr
= (vm_offset_t
) getsegdatafromheader(&_mh_execute_header
, "__DATA", &scratchSize
);
270 IOLog("kaslr: start 0x%lx end 0x%lx length %lu for __DATA \n",
271 (unsigned long)scratchAddr
,
272 (unsigned long)(scratchAddr
+ scratchSize
),
275 scratchAddr
= (vm_offset_t
) getsegdatafromheader(&_mh_execute_header
, "__LINKEDIT", &scratchSize
);
276 IOLog("kaslr: start 0x%lx end 0x%lx length %lu for __LINKEDIT \n",
277 (unsigned long)scratchAddr
,
278 (unsigned long)(scratchAddr
+ scratchSize
),
281 scratchAddr
= (vm_offset_t
) getsegdatafromheader(&_mh_execute_header
, "__KLD", &scratchSize
);
282 IOLog("kaslr: start 0x%lx end 0x%lx length %lu for __KLD \n",
283 (unsigned long)scratchAddr
,
284 (unsigned long)(scratchAddr
+ scratchSize
),
287 scratchAddr
= (vm_offset_t
) getsegdatafromheader(&_mh_execute_header
, "__PRELINK_TEXT", &scratchSize
);
288 IOLog("kaslr: start 0x%lx end 0x%lx length %lu for __PRELINK_TEXT \n",
289 (unsigned long)scratchAddr
,
290 (unsigned long)(scratchAddr
+ scratchSize
),
293 scratchAddr
= (vm_offset_t
) getsegdatafromheader(&_mh_execute_header
, "__PRELINK_INFO", &scratchSize
);
294 IOLog("kaslr: start 0x%lx end 0x%lx length %lu for __PRELINK_INFO \n",
295 (unsigned long)scratchAddr
,
296 (unsigned long)(scratchAddr
+ scratchSize
),
300 prelinkData
= (void *) prelinkTextSegment
->vmaddr
;
301 prelinkLength
= prelinkTextSegment
->vmsize
;
304 /* Unserialize the info dictionary from the prelink info section.
306 parsedXML
= OSUnserializeXML((const char *)prelinkInfoSect
->addr
,
309 prelinkInfoDict
= OSDynamicCast(OSDictionary
, parsedXML
);
311 if (!prelinkInfoDict
) {
312 const char * errorCString
= "(unknown error)";
314 if (errorString
&& errorString
->getCStringNoCopy()) {
315 errorCString
= errorString
->getCStringNoCopy();
316 } else if (parsedXML
) {
317 errorCString
= "not a dictionary";
319 OSKextLog(/* kext */ NULL
, kOSKextLogErrorLevel
| kOSKextLogArchiveFlag
,
320 "Error unserializing prelink plist: %s.", errorCString
);
325 /* Check if we should keep developer kexts around.
326 * TODO: Check DeviceTree instead of a boot-arg <rdar://problem/10604201>
328 developerDevice
= true;
329 PE_parse_boot_argn("developer", &developerDevice
, sizeof(developerDevice
));
331 ramDiskBoot
= IORamDiskBSDRoot();
332 #endif /* NO_KEXTD */
334 infoDictArray
= OSDynamicCast(OSArray
,
335 prelinkInfoDict
->getObject(kPrelinkInfoDictionaryKey
));
336 if (!infoDictArray
) {
337 OSKextLog(/* kext */ NULL
, kOSKextLogErrorLevel
| kOSKextLogArchiveFlag
,
338 "The prelinked kernel has no kext info dictionaries");
342 /* Create dictionary of excluded kexts
344 OSKext::createExcludeListFromPrelinkInfo(infoDictArray
);
346 /* Create OSKext objects for each info dictionary.
348 for (i
= 0; i
< infoDictArray
->getCount(); ++i
) {
349 infoDict
= OSDynamicCast(OSDictionary
, infoDictArray
->getObject(i
));
351 OSKextLog(/* kext */ NULL
,
352 kOSKextLogErrorLevel
|
353 kOSKextLogDirectoryScanFlag
| kOSKextLogArchiveFlag
,
354 "Can't find info dictionary for prelinked kext #%d.", i
);
361 /* If we're not on a developer device, skip and free developer kexts.
363 if (developerDevice
== false) {
364 OSBoolean
*devOnlyBool
= OSDynamicCast(OSBoolean
,
365 infoDict
->getObject(kOSBundleDeveloperOnlyKey
));
366 if (devOnlyBool
== kOSBooleanTrue
) {
371 /* Skip and free kexts that are only needed when booted from a ram disk.
373 if (ramDiskBoot
== false) {
374 OSBoolean
*ramDiskOnlyBool
= OSDynamicCast(OSBoolean
,
375 infoDict
->getObject(kOSBundleRamDiskOnlyKey
));
376 if (ramDiskOnlyBool
== kOSBooleanTrue
) {
381 if (dontLoad
== true) {
382 OSString
*bundleID
= OSDynamicCast(OSString
,
383 infoDict
->getObject(kCFBundleIdentifierKey
));
385 OSKextLog(NULL
, kOSKextLogWarningLevel
| kOSKextLogGeneralFlag
,
386 "Kext %s not loading.", bundleID
->getCStringNoCopy());
389 OSNumber
*addressNum
= OSDynamicCast(OSNumber
,
390 infoDict
->getObject(kPrelinkExecutableLoadKey
));
391 OSNumber
*lengthNum
= OSDynamicCast(OSNumber
,
392 infoDict
->getObject(kPrelinkExecutableSizeKey
));
393 if (addressNum
&& lengthNum
) {
394 #error Pick the right way to free prelinked data on this arch
397 infoDictArray
->removeObject(i
--);
400 #endif /* NO_KEXTD */
402 /* Create the kext for the entry, then release it, because the
403 * kext system keeps them around until explicitly removed.
404 * Any creation/registration failures are already logged for us.
406 OSKext
* newKext
= OSKext::withPrelinkedInfoDict(infoDict
);
407 OSSafeReleaseNULL(newKext
);
410 /* Store the number of prelinked kexts in the registry so we can tell
411 * when the system has been started from a prelinked kernel.
413 registryRoot
= IORegistryEntry::getRegistryRoot();
414 assert(registryRoot
);
416 prelinkCountObj
= OSNumber::withNumber(
417 (unsigned long long)infoDictArray
->getCount(),
418 8 * sizeof(uint32_t));
419 assert(prelinkCountObj
);
420 if (prelinkCountObj
) {
421 registryRoot
->setProperty(kOSPrelinkKextCountKey
, prelinkCountObj
);
424 OSKextLog(/* kext */ NULL
,
425 kOSKextLogProgressLevel
|
426 kOSKextLogGeneralFlag
| kOSKextLogKextBookkeepingFlag
|
427 kOSKextLogDirectoryScanFlag
| kOSKextLogArchiveFlag
,
428 "%u prelinked kexts",
429 infoDictArray
->getCount());
431 #if CONFIG_KEXT_BASEMENT
432 /* On CONFIG_KEXT_BASEMENT systems, kexts are copied to their own
433 * special VM region during OSKext init time, so we can free the whole
436 ml_static_mfree((vm_offset_t
) prelinkData
, prelinkLength
);
437 #endif /* __x86_64__ */
439 /* Free the prelink info segment, we're done with it.
441 prelinkInfoSegment
= getsegbyname(kPrelinkInfoSegment
);
442 if (prelinkInfoSegment
) {
443 ml_static_mfree((vm_offset_t
)prelinkInfoSegment
->vmaddr
,
444 (vm_size_t
)prelinkInfoSegment
->vmsize
);
448 OSSafeRelease(errorString
);
449 OSSafeRelease(parsedXML
);
450 OSSafeRelease(theKernel
);
451 OSSafeRelease(prelinkCountObj
);
455 /*********************************************************************
456 *********************************************************************/
457 #define BOOTER_KEXT_PREFIX "Driver-"
459 typedef struct _DeviceTreeBuffer
{
465 KLDBootstrap::readBooterExtensions(void)
467 IORegistryEntry
* booterMemoryMap
= NULL
; // must release
468 OSDictionary
* propertyDict
= NULL
; // must release
469 OSCollectionIterator
* keyIterator
= NULL
; // must release
470 OSString
* deviceTreeName
= NULL
; // do not release
472 const _DeviceTreeBuffer
* deviceTreeBuffer
= NULL
; // do not free
473 char * booterDataPtr
= NULL
; // do not free
474 OSData
* booterData
= NULL
; // must release
476 OSKext
* aKext
= NULL
; // must release
478 OSKextLog(/* kext */ NULL
,
479 kOSKextLogProgressLevel
|
480 kOSKextLogDirectoryScanFlag
| kOSKextLogKextBookkeepingFlag
,
481 "Reading startup extensions from booter memory.");
483 booterMemoryMap
= IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane
);
485 if (!booterMemoryMap
) {
486 OSKextLog(/* kext */ NULL
,
487 kOSKextLogErrorLevel
|
488 kOSKextLogGeneralFlag
| kOSKextLogDirectoryScanFlag
,
489 "Can't read booter memory map.");
493 propertyDict
= booterMemoryMap
->dictionaryWithProperties();
495 OSKextLog(/* kext */ NULL
,
496 kOSKextLogErrorLevel
|
497 kOSKextLogDirectoryScanFlag
,
498 "Can't get property dictionary from memory map.");
502 keyIterator
= OSCollectionIterator::withCollection(propertyDict
);
504 OSKextLog(/* kext */ NULL
,
505 kOSKextLogErrorLevel
|
506 kOSKextLogGeneralFlag
,
507 "Can't allocate iterator for driver images.");
511 /* Create dictionary of excluded kexts
513 OSKext::createExcludeListFromBooterData(propertyDict
, keyIterator
);
514 keyIterator
->reset();
516 while ( ( deviceTreeName
=
517 OSDynamicCast(OSString
, keyIterator
->getNextObject() ))) {
519 const char * devTreeNameCString
= deviceTreeName
->getCStringNoCopy();
520 OSData
* deviceTreeEntry
= OSDynamicCast(OSData
,
521 propertyDict
->getObject(deviceTreeName
));
523 /* Clear out the booterData from the prior iteration.
525 OSSafeReleaseNULL(booterData
);
527 /* If there is no entry for the name, we can't do much with it. */
528 if (!deviceTreeEntry
) {
532 /* Make sure it is a kext */
533 if (strncmp(devTreeNameCString
,
535 CONST_STRLEN(BOOTER_KEXT_PREFIX
))) {
539 deviceTreeBuffer
= (const _DeviceTreeBuffer
*)
540 deviceTreeEntry
->getBytesNoCopy(0, sizeof(deviceTreeBuffer
));
541 if (!deviceTreeBuffer
) {
542 /* We can't get to the data, so we can't do anything,
543 * not even free it from physical memory (if it's there).
545 OSKextLog(/* kext */ NULL
,
546 kOSKextLogErrorLevel
|
547 kOSKextLogDirectoryScanFlag
,
548 "Device tree entry %s has NULL pointer.",
550 goto finish
; // xxx - continue, panic?
553 booterDataPtr
= (char *)ml_static_ptovirt(deviceTreeBuffer
->paddr
);
554 if (!booterDataPtr
) {
555 OSKextLog(/* kext */ NULL
,
556 kOSKextLogErrorLevel
|
557 kOSKextLogDirectoryScanFlag
,
558 "Can't get virtual address for device tree entry %s.",
563 /* Wrap the booter data buffer in an OSData and set a dealloc function
564 * so it will take care of the physical memory when freed. Kexts will
565 * retain the booterData for as long as they need it. Remove the entry
566 * from the booter memory map after this is done.
568 booterData
= OSData::withBytesNoCopy(booterDataPtr
,
569 deviceTreeBuffer
->length
);
571 OSKextLog(/* kext */ NULL
,
572 kOSKextLogErrorLevel
|
573 kOSKextLogGeneralFlag
,
574 "Error - Can't allocate OSData wrapper for device tree entry %s.",
578 booterData
->setDeallocFunction(osdata_phys_free
);
580 /* Create the kext for the entry, then release it, because the
581 * kext system keeps them around until explicitly removed.
582 * Any creation/registration failures are already logged for us.
584 OSKext
* newKext
= OSKext::withBooterData(deviceTreeName
, booterData
);
585 OSSafeRelease(newKext
);
587 booterMemoryMap
->removeProperty(deviceTreeName
);
589 } /* while ( (deviceTreeName = OSDynamicCast(OSString, ...) ) ) */
593 OSSafeRelease(booterMemoryMap
);
594 OSSafeRelease(propertyDict
);
595 OSSafeRelease(keyIterator
);
596 OSSafeRelease(booterData
);
597 OSSafeRelease(aKext
);
601 /*********************************************************************
602 *********************************************************************/
603 #define COM_APPLE "com.apple."
606 KLDBootstrap::loadSecurityExtensions(void)
608 OSDictionary
* extensionsDict
= NULL
; // must release
609 OSCollectionIterator
* keyIterator
= NULL
; // must release
610 OSString
* bundleID
= NULL
; // don't release
611 OSKext
* theKext
= NULL
; // don't release
612 OSBoolean
* isSecurityKext
= NULL
; // don't release
614 OSKextLog(/* kext */ NULL
,
615 kOSKextLogStepLevel
|
617 "Loading security extensions.");
619 extensionsDict
= OSKext::copyKexts();
620 if (!extensionsDict
) {
624 keyIterator
= OSCollectionIterator::withCollection(extensionsDict
);
626 OSKextLog(/* kext */ NULL
,
627 kOSKextLogErrorLevel
|
628 kOSKextLogGeneralFlag
,
629 "Failed to allocate iterator for security extensions.");
633 while ((bundleID
= OSDynamicCast(OSString
, keyIterator
->getNextObject()))) {
635 const char * bundle_id
= bundleID
->getCStringNoCopy();
637 /* Skip extensions whose bundle IDs don't start with "com.apple.".
640 (strncmp(bundle_id
, COM_APPLE
, CONST_STRLEN(COM_APPLE
)) != 0)) {
645 theKext
= OSDynamicCast(OSKext
, extensionsDict
->getObject(bundleID
));
650 isSecurityKext
= OSDynamicCast(OSBoolean
,
651 theKext
->getPropertyForHostArch(kAppleSecurityExtensionKey
));
652 if (isSecurityKext
&& isSecurityKext
->isTrue()) {
653 OSKextLog(/* kext */ NULL
,
654 kOSKextLogStepLevel
|
656 "Loading security extension %s.", bundleID
->getCStringNoCopy());
657 OSKext::loadKextWithIdentifier(bundleID
->getCStringNoCopy(),
658 /* allowDefer */ false);
663 OSSafeRelease(keyIterator
);
664 OSSafeRelease(extensionsDict
);
669 /*********************************************************************
670 * We used to require that all listed kernel components load, but
671 * nowadays we can get them from userland so we only try to load the
672 * ones we have. If an error occurs later, such is life.
674 * Note that we look the kexts up first, so we can avoid spurious
675 * (in this context, anyhow) log messages about kexts not being found.
677 * xxx - do we even need to do this any more? Check if the kernel
678 * xxx - compoonents just load in the regular paths
679 *********************************************************************/
681 KLDBootstrap::loadKernelComponentKexts(void)
683 OSReturn result
= kOSReturnSuccess
; // optimistic
684 OSKext
* theKext
= NULL
; // must release
685 const char ** kextIDPtr
= NULL
; // do not release
687 for (kextIDPtr
= &sKernelComponentNames
[0]; *kextIDPtr
; kextIDPtr
++) {
689 OSSafeReleaseNULL(theKext
);
690 theKext
= OSKext::lookupKextWithIdentifier(*kextIDPtr
);
693 if (kOSReturnSuccess
!= OSKext::loadKextWithIdentifier(
694 *kextIDPtr
, /* allowDefer */ false)) {
696 // xxx - check KextBookkeeping, might be redundant
697 OSKextLog(/* kext */ NULL
,
698 kOSKextLogErrorLevel
|
699 kOSKextLogDirectoryScanFlag
| kOSKextLogKextBookkeepingFlag
,
700 "Failed to initialize kernel component %s.", *kextIDPtr
);
701 result
= kOSReturnError
;
706 OSSafeRelease(theKext
);
710 /*********************************************************************
711 * Ensure that Kernel External Components are loaded early in boot,
712 * before other kext personalities get sent to the IOCatalogue. These
713 * kexts are treated specially because they may provide the implementation
714 * for kernel-vended KPI, so they must register themselves before
715 * general purpose IOKit probing begins.
716 *********************************************************************/
718 #define COM_APPLE_KEC "com.apple.kec."
721 KLDBootstrap::loadKernelExternalComponents(void)
723 OSDictionary
* extensionsDict
= NULL
; // must release
724 OSCollectionIterator
* keyIterator
= NULL
; // must release
725 OSString
* bundleID
= NULL
; // don't release
726 OSKext
* theKext
= NULL
; // don't release
727 OSBoolean
* isKernelExternalComponent
= NULL
; // don't release
729 OSKextLog(/* kext */ NULL
,
730 kOSKextLogStepLevel
|
732 "Loading Kernel External Components.");
734 extensionsDict
= OSKext::copyKexts();
735 if (!extensionsDict
) {
739 keyIterator
= OSCollectionIterator::withCollection(extensionsDict
);
741 OSKextLog(/* kext */ NULL
,
742 kOSKextLogErrorLevel
|
743 kOSKextLogGeneralFlag
,
744 "Failed to allocate iterator for Kernel External Components.");
748 while ((bundleID
= OSDynamicCast(OSString
, keyIterator
->getNextObject()))) {
750 const char * bundle_id
= bundleID
->getCStringNoCopy();
752 /* Skip extensions whose bundle IDs don't start with "com.apple.kec.".
755 (strncmp(bundle_id
, COM_APPLE_KEC
, CONST_STRLEN(COM_APPLE_KEC
)) != 0)) {
760 theKext
= OSDynamicCast(OSKext
, extensionsDict
->getObject(bundleID
));
765 isKernelExternalComponent
= OSDynamicCast(OSBoolean
,
766 theKext
->getPropertyForHostArch(kAppleKernelExternalComponentKey
));
767 if (isKernelExternalComponent
&& isKernelExternalComponent
->isTrue()) {
768 OSKextLog(/* kext */ NULL
,
769 kOSKextLogStepLevel
|
771 "Loading kernel external component %s.", bundleID
->getCStringNoCopy());
772 OSKext::loadKextWithIdentifier(bundleID
->getCStringNoCopy(),
773 /* allowDefer */ false);
778 OSSafeRelease(keyIterator
);
779 OSSafeRelease(extensionsDict
);
784 /*********************************************************************
785 *********************************************************************/
787 KLDBootstrap::readBuiltinPersonalities(void)
789 OSObject
* parsedXML
= NULL
; // must release
790 OSArray
* builtinExtensions
= NULL
; // do not release
791 OSArray
* allPersonalities
= NULL
; // must release
792 OSString
* errorString
= NULL
; // must release
793 kernel_section_t
* infosect
= NULL
; // do not free
794 OSCollectionIterator
* personalitiesIterator
= NULL
; // must release
795 unsigned int count
, i
;
797 OSKextLog(/* kext */ NULL
,
798 kOSKextLogStepLevel
|
800 "Reading built-in kernel personalities for I/O Kit drivers.");
802 /* Look in the __BUILTIN __info segment for an array of Info.plist
803 * entries. For each one, extract the personalities dictionary, add
804 * it to our array, then push them all (without matching) to
805 * the IOCatalogue. This can be used to augment the personalities
806 * in gIOKernelConfigTables, especially when linking entire kexts into
807 * the mach_kernel image.
809 infosect
= getsectbyname("__BUILTIN", "__info");
815 parsedXML
= OSUnserializeXML((const char *) (uintptr_t)infosect
->addr
,
818 builtinExtensions
= OSDynamicCast(OSArray
, parsedXML
);
820 if (!builtinExtensions
) {
821 const char * errorCString
= "(unknown error)";
823 if (errorString
&& errorString
->getCStringNoCopy()) {
824 errorCString
= errorString
->getCStringNoCopy();
825 } else if (parsedXML
) {
826 errorCString
= "not an array";
828 OSKextLog(/* kext */ NULL
,
829 kOSKextLogErrorLevel
|
831 "Error unserializing built-in personalities: %s.", errorCString
);
835 // estimate 3 personalities per Info.plist/kext
836 count
= builtinExtensions
->getCount();
837 allPersonalities
= OSArray::withCapacity(count
* 3);
839 for (i
= 0; i
< count
; i
++) {
840 OSDictionary
* infoDict
= NULL
; // do not release
841 OSString
* moduleName
= NULL
; // do not release
842 OSDictionary
* personalities
; // do not release
843 OSString
* personalityName
; // do not release
845 OSSafeReleaseNULL(personalitiesIterator
);
847 infoDict
= OSDynamicCast(OSDictionary
,
848 builtinExtensions
->getObject(i
));
853 moduleName
= OSDynamicCast(OSString
,
854 infoDict
->getObject(kCFBundleIdentifierKey
));
859 OSKextLog(/* kext */ NULL
,
860 kOSKextLogStepLevel
|
862 "Adding personalities for built-in driver %s:",
863 moduleName
->getCStringNoCopy());
865 personalities
= OSDynamicCast(OSDictionary
,
866 infoDict
->getObject("IOKitPersonalities"));
867 if (!personalities
) {
871 personalitiesIterator
= OSCollectionIterator::withCollection(personalities
);
872 if (!personalitiesIterator
) {
873 continue; // xxx - well really, what can we do? should we panic?
876 while ((personalityName
= OSDynamicCast(OSString
,
877 personalitiesIterator
->getNextObject()))) {
879 OSDictionary
* personality
= OSDynamicCast(OSDictionary
,
880 personalities
->getObject(personalityName
));
882 OSKextLog(/* kext */ NULL
,
883 kOSKextLogDetailLevel
|
885 "Adding built-in driver personality %s.",
886 personalityName
->getCStringNoCopy());
888 if (personality
&& !personality
->getObject(kCFBundleIdentifierKey
)) {
889 personality
->setObject(kCFBundleIdentifierKey
, moduleName
);
891 allPersonalities
->setObject(personality
);
895 gIOCatalogue
->addDrivers(allPersonalities
, false);
898 OSSafeRelease(parsedXML
);
899 OSSafeRelease(allPersonalities
);
900 OSSafeRelease(errorString
);
901 OSSafeRelease(personalitiesIterator
);
906 #pragma mark Bootstrap Functions
908 /*********************************************************************
909 * Bootstrap Functions
910 *********************************************************************/
911 static void bootstrapRecordStartupExtensions(void)
913 sBootstrapObject
.readStartupExtensions();
917 static void bootstrapLoadSecurityExtensions(void)
919 sBootstrapObject
.loadSecurityExtensions();