+
+/*********************************************************************
+*********************************************************************/
+OSReturn
+KLDBootstrap::readMkextExtensions(
+ OSString * deviceTreeName,
+ OSData * booterData)
+{
+ OSReturn result = kOSReturnError;
+
+ uint32_t checksum;
+ IORegistryEntry * registryRoot = NULL; // do not release
+ OSData * checksumObj = NULL; // must release
+
+ OSKextLog(/* kext */ NULL,
+ kOSKextLogStepLevel |
+ kOSKextLogDirectoryScanFlag | kOSKextLogArchiveFlag,
+ "Reading startup mkext archive from device tree entry %s.",
+ deviceTreeName->getCStringNoCopy());
+
+ /* If we successfully read the archive,
+ * then save the mkext's checksum in the IORegistry.
+ * assumes we'll only ever have one mkext to boot
+ */
+ result = OSKext::readMkextArchive(booterData, &checksum);
+ if (result == kOSReturnSuccess) {
+
+ OSKextLog(/* kext */ NULL,
+ kOSKextLogProgressLevel |
+ kOSKextLogArchiveFlag,
+ "Startup mkext archive has checksum 0x%x.", (int)checksum);
+
+ registryRoot = IORegistryEntry::getRegistryRoot();
+ assert(registryRoot);
+ checksumObj = OSData::withBytes((void *)&checksum, sizeof(checksum));
+ assert(checksumObj);
+ if (checksumObj) {
+ registryRoot->setProperty(kOSStartupMkextCRC, checksumObj);
+ }
+ }
+
+ return result;
+}
+
+/*********************************************************************
+*********************************************************************/
+#define COM_APPLE "com.apple."
+
+void
+KLDBootstrap::loadSecurityExtensions(void)
+{
+ OSDictionary * extensionsDict = NULL; // must release
+ OSCollectionIterator * keyIterator = NULL; // must release
+ OSString * bundleID = NULL; // don't release
+ OSKext * theKext = NULL; // don't release
+ OSBoolean * isSecurityKext = NULL; // don't release
+
+ OSKextLog(/* kext */ NULL,
+ kOSKextLogStepLevel |
+ kOSKextLogLoadFlag,
+ "Loading security extensions.");
+
+ extensionsDict = OSKext::copyKexts();
+ if (!extensionsDict) {
+ return;
+ }
+
+ keyIterator = OSCollectionIterator::withCollection(extensionsDict);
+ if (!keyIterator) {
+ OSKextLog(/* kext */ NULL,
+ kOSKextLogErrorLevel |
+ kOSKextLogGeneralFlag,
+ "Failed to allocate iterator for security extensions.");
+ goto finish;
+ }
+
+ while ((bundleID = OSDynamicCast(OSString, keyIterator->getNextObject()))) {
+
+ const char * bundle_id = bundleID->getCStringNoCopy();
+
+ /* Skip extensions whose bundle IDs don't start with "com.apple.".
+ */
+ if (!bundle_id ||
+ (strncmp(bundle_id, COM_APPLE, CONST_STRLEN(COM_APPLE)) != 0)) {
+
+ continue;
+ }
+
+ theKext = OSDynamicCast(OSKext, extensionsDict->getObject(bundleID));
+ if (!theKext) {
+ continue;
+ }
+
+ isSecurityKext = OSDynamicCast(OSBoolean,
+ theKext->getPropertyForHostArch("AppleSecurityExtension"));
+ if (isSecurityKext && isSecurityKext->isTrue()) {
+ OSKextLog(/* kext */ NULL,
+ kOSKextLogStepLevel |
+ kOSKextLogLoadFlag,
+ "Loading security extension %s.", bundleID->getCStringNoCopy());
+ OSKext::loadKextWithIdentifier(bundleID->getCStringNoCopy(),
+ /* allowDefer */ false);
+ }
+ }
+
+finish:
+ OSSafeRelease(keyIterator);
+ OSSafeRelease(extensionsDict);
+
+ return;
+}
+
+/*********************************************************************
+* We used to require that all listed kernel components load, but
+* nowadays we can get them from userland so we only try to load the
+* ones we have. If an error occurs later, such is life.
+*
+* Note that we look the kexts up first, so we can avoid spurious
+* (in this context, anyhow) log messages about kexts not being found.
+*
+* xxx - do we even need to do this any more? Check if the kernel
+* xxx - compoonents just load in the regular paths
+*********************************************************************/
+OSReturn
+KLDBootstrap::loadKernelComponentKexts(void)
+{
+ OSReturn result = kOSReturnSuccess; // optimistic
+ OSKext * theKext = NULL; // must release
+ const char ** kextIDPtr = NULL; // do not release
+
+ for (kextIDPtr = &sKernelComponentNames[0]; *kextIDPtr; kextIDPtr++) {
+
+ OSSafeReleaseNULL(theKext);
+ theKext = OSKext::lookupKextWithIdentifier(*kextIDPtr);
+
+ if (theKext) {
+ if (kOSReturnSuccess != OSKext::loadKextWithIdentifier(
+ *kextIDPtr, /* allowDefer */ false)) {
+
+ // xxx - check KextBookkeeping, might be redundant
+ OSKextLog(/* kext */ NULL,
+ kOSKextLogErrorLevel |
+ kOSKextLogDirectoryScanFlag | kOSKextLogKextBookkeepingFlag,
+ "Failed to initialize kernel component %s.", *kextIDPtr);
+ result = kOSReturnError;
+ }
+ }
+ }
+
+ OSSafeRelease(theKext);
+ return result;
+}
+
+/*********************************************************************
+ *********************************************************************/
+void
+KLDBootstrap::readBuiltinPersonalities(void)
+{
+ OSObject * parsedXML = NULL; // must release
+ OSArray * builtinExtensions = NULL; // do not release
+ OSArray * allPersonalities = NULL; // must release
+ OSString * errorString = NULL; // must release
+ kernel_section_t * infosect = NULL; // do not free
+ OSCollectionIterator * personalitiesIterator = NULL; // must release
+ unsigned int count, i;
+
+ OSKextLog(/* kext */ NULL,
+ kOSKextLogStepLevel |
+ kOSKextLogLoadFlag,
+ "Reading built-in kernel personalities for I/O Kit drivers.");
+
+ /* Look in the __BUILTIN __info segment for an array of Info.plist
+ * entries. For each one, extract the personalities dictionary, add
+ * it to our array, then push them all (without matching) to
+ * the IOCatalogue. This can be used to augment the personalities
+ * in gIOKernelConfigTables, especially when linking entire kexts into
+ * the mach_kernel image.
+ */
+ infosect = getsectbyname("__BUILTIN", "__info");
+ if (!infosect) {
+ // this isn't fatal
+ goto finish;
+ }
+
+ parsedXML = OSUnserializeXML((const char *) (uintptr_t)infosect->addr,
+ &errorString);
+ if (parsedXML) {
+ builtinExtensions = OSDynamicCast(OSArray, parsedXML);
+ }
+ if (!builtinExtensions) {
+ const char * errorCString = "(unknown error)";
+
+ if (errorString && errorString->getCStringNoCopy()) {
+ errorCString = errorString->getCStringNoCopy();
+ } else if (parsedXML) {
+ errorCString = "not an array";
+ }
+ OSKextLog(/* kext */ NULL,
+ kOSKextLogErrorLevel |
+ kOSKextLogLoadFlag,
+ "Error unserializing built-in personalities: %s.", errorCString);
+ goto finish;
+ }
+
+ // estimate 3 personalities per Info.plist/kext
+ count = builtinExtensions->getCount();
+ allPersonalities = OSArray::withCapacity(count * 3);
+
+ for (i = 0; i < count; i++) {
+ OSDictionary * infoDict = NULL; // do not release
+ OSString * moduleName = NULL; // do not release
+ OSDictionary * personalities; // do not release
+ OSString * personalityName; // do not release
+
+ OSSafeReleaseNULL(personalitiesIterator);
+
+ infoDict = OSDynamicCast(OSDictionary,
+ builtinExtensions->getObject(i));
+ if (!infoDict) {
+ continue;
+ }
+
+ moduleName = OSDynamicCast(OSString,
+ infoDict->getObject(kCFBundleIdentifierKey));
+ if (!moduleName) {
+ continue;
+ }
+
+ OSKextLog(/* kext */ NULL,
+ kOSKextLogStepLevel |
+ kOSKextLogLoadFlag,
+ "Adding personalities for built-in driver %s:",
+ moduleName->getCStringNoCopy());
+
+ personalities = OSDynamicCast(OSDictionary,
+ infoDict->getObject("IOKitPersonalities"));
+ if (!personalities) {
+ continue;
+ }
+
+ personalitiesIterator = OSCollectionIterator::withCollection(personalities);
+ if (!personalitiesIterator) {
+ continue; // xxx - well really, what can we do? should we panic?
+ }
+
+ while ((personalityName = OSDynamicCast(OSString,
+ personalitiesIterator->getNextObject()))) {
+
+ OSDictionary * personality = OSDynamicCast(OSDictionary,
+ personalities->getObject(personalityName));
+
+ OSKextLog(/* kext */ NULL,
+ kOSKextLogDetailLevel |
+ kOSKextLogLoadFlag,
+ "Adding built-in driver personality %s.",
+ personalityName->getCStringNoCopy());
+
+ if (personality && !personality->getObject(kCFBundleIdentifierKey)) {
+ personality->setObject(kCFBundleIdentifierKey, moduleName);
+ }
+ allPersonalities->setObject(personality);
+ }
+ }
+
+ gIOCatalogue->addDrivers(allPersonalities, false);
+
+finish:
+ OSSafeRelease(parsedXML);
+ OSSafeRelease(allPersonalities);
+ OSSafeRelease(errorString);
+ OSSafeRelease(personalitiesIterator);
+ return;
+}
+
+#if PRAGMA_MARK
+#pragma mark Bootstrap Functions
+#endif
+/*********************************************************************
+* Bootstrap Functions
+*********************************************************************/
+static void bootstrapRecordStartupExtensions(void)
+{
+ sBootstrapObject.readStartupExtensions();
+ return;
+}
+
+static void bootstrapLoadSecurityExtensions(void)
+{
+ sBootstrapObject.loadSecurityExtensions();
+ return;
+}
+