2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 #include <libkern/c++/OSContainers.h>
23 #include <IOKit/IODeviceTreeSupport.h>
24 #include <IOKit/IORegistryEntry.h>
25 #include <IOKit/IOCatalogue.h>
26 #include <libkern/c++/OSUnserialize.h>
27 #include <libkern/OSByteOrder.h>
28 #include <libsa/catalogue.h>
31 #include <machine/machine_routines.h>
32 #include <mach/host_info.h>
33 #include <mach/kmod.h>
34 #include <libsa/mkext.h>
35 #include <libsa/vers_rsrc.h>
36 #include <mach-o/loader.h>
39 #include <IOKit/IOLib.h>
41 #include <IOKit/assert.h>
44 extern void IODTFreeLoaderInfo( char *key
, void *infoAddr
, int infoSize
);
45 extern kern_return_t
host_info(host_t host
,
48 mach_msg_type_number_t
*count
);
49 extern int check_cpu_subtype(cpu_subtype_t cpu_subtype
);
50 extern struct section
*
54 extern struct segment_command
*
55 getsegbyname(char *seg_name
);
61 #define VTYELLOW "\033[33m"
62 #define VTRESET "\033[0m"
68 /*********************************************************************
69 *********************************************************************/
70 static OSDictionary
* gStartupExtensions
= 0;
71 static OSArray
* gBootLoaderObjects
= 0;
72 extern OSArray
* gIOPrelinkedModules
;
74 OSDictionary
* getStartupExtensions(void) {
75 if (gStartupExtensions
) {
76 return gStartupExtensions
;
78 gStartupExtensions
= OSDictionary::withCapacity(1);
79 assert (gStartupExtensions
);
81 return gStartupExtensions
;
84 /* This array holds objects that are needed to be held around during
85 * boot before kextd starts up. Currently it contains OSData objects
86 * copied from OF entries for mkext archives in device ROMs. Because
87 * the Device Tree support code dumps these after initially handing
88 * them to us, we have to be able to clean them up later.
90 OSArray
* getBootLoaderObjects(void) {
91 if (gBootLoaderObjects
) {
92 return gBootLoaderObjects
;
94 gBootLoaderObjects
= OSArray::withCapacity(1);
95 assert (gBootLoaderObjects
);
97 return gBootLoaderObjects
;
100 /*********************************************************************
101 * This function checks that a driver dict has all the required
102 * entries and does a little bit of value checking too.
104 * index is nonnegative if the index of an entry from an mkext
106 *********************************************************************/
107 bool validateExtensionDict(OSDictionary
* extension
, int index
) {
110 bool not_a_dict
= false;
111 bool id_missing
= false;
112 bool is_kernel_resource
= false;
113 bool has_executable
= false;
114 OSString
* bundleIdentifier
= NULL
; // do not release
115 OSObject
* rawValue
= NULL
; // do not release
116 OSString
* stringValue
= NULL
; // do not release
117 OSBoolean
* booleanValue
= NULL
; // do not release
118 OSDictionary
* personalities
= NULL
; // do not release
119 OSDictionary
* libraries
= NULL
; // do not release
120 OSCollectionIterator
* keyIterator
= NULL
; // must release
121 OSString
* key
= NULL
; // do not release
123 VERS_version compatible_vers
;
125 // Info dict is a dictionary
126 if (!OSDynamicCast(OSDictionary
, extension
)) {
132 // CFBundleIdentifier is a string - REQUIRED
133 bundleIdentifier
= OSDynamicCast(OSString
,
134 extension
->getObject("CFBundleIdentifier"));
135 if (!bundleIdentifier
) {
141 // Length of CFBundleIdentifier is not >= KMOD_MAX_NAME
142 if (bundleIdentifier
->getLength() >= KMOD_MAX_NAME
) {
147 // CFBundlePackageType is "KEXT" - REQUIRED
148 stringValue
= OSDynamicCast(OSString
,
149 extension
->getObject("CFBundlePackageType"));
154 if (!stringValue
->isEqualTo("KEXT")) {
159 // CFBundleVersion is a string - REQUIRED
160 stringValue
= OSDynamicCast(OSString
,
161 extension
->getObject("CFBundleVersion"));
166 // CFBundleVersion is of valid form
167 vers
= VERS_parse_string(stringValue
->getCStringNoCopy());
173 // OSBundleCompatibleVersion is a string - OPTIONAL
174 rawValue
= extension
->getObject("OSBundleCompatibleVersion");
176 stringValue
= OSDynamicCast(OSString
, rawValue
);
182 // OSBundleCompatibleVersion is of valid form
183 compatible_vers
= VERS_parse_string(stringValue
->getCStringNoCopy());
184 if (compatible_vers
< 0) {
189 // OSBundleCompatibleVersion <= CFBundleVersion
190 if (compatible_vers
> vers
) {
196 // CFBundleExecutable is a string - OPTIONAL
197 rawValue
= extension
->getObject("CFBundleExecutable");
199 stringValue
= OSDynamicCast(OSString
, rawValue
);
200 if (!stringValue
|| stringValue
->getLength() == 0) {
204 has_executable
= true;
207 // OSKernelResource is a boolean value - OPTIONAL
208 rawValue
= extension
->getObject("OSKernelResource");
210 booleanValue
= OSDynamicCast(OSBoolean
, rawValue
);
215 is_kernel_resource
= booleanValue
->isTrue();
218 // IOKitPersonalities is a dictionary - OPTIONAL
219 rawValue
= extension
->getObject("IOKitPersonalities");
221 personalities
= OSDynamicCast(OSDictionary
, rawValue
);
222 if (!personalities
) {
227 keyIterator
= OSCollectionIterator::withCollection(personalities
);
229 IOLog("Error: Failed to allocate iterator for personalities.\n");
235 while ((key
= OSDynamicCast(OSString
, keyIterator
->getNextObject()))) {
236 OSDictionary
* personality
= NULL
; // do not release
238 // Each personality is a dictionary
239 personality
= OSDynamicCast(OSDictionary
,
240 personalities
->getObject(key
));
246 // IOClass exists as a string - REQUIRED
247 if (!OSDynamicCast(OSString
, personality
->getObject("IOClass"))) {
252 // IOProviderClass exists as a string - REQUIRED
253 if (!OSDynamicCast(OSString
,
254 personality
->getObject("IOProviderClass"))) {
260 // CFBundleIdentifier is a string - OPTIONAL - INSERT IF ABSENT!
261 rawValue
= personality
->getObject("CFBundleIdentifier");
263 personality
->setObject("CFBundleIdentifier", bundleIdentifier
);
265 OSString
* personalityID
= NULL
; // do not release
266 personalityID
= OSDynamicCast(OSString
, rawValue
);
267 if (!personalityID
) {
271 // Length of CFBundleIdentifier is not >= KMOD_MAX_NAME
272 if (personalityID
->getLength() >= KMOD_MAX_NAME
) {
279 // IOKitDebug is a number - OPTIONAL
280 rawValue
= personality
->getObject("IOKitDebug");
281 if (rawValue
&& !OSDynamicCast(OSNumber
, rawValue
)) {
287 keyIterator
->release();
292 // OSBundleLibraries is a dictionary - REQUIRED if
293 // not kernel resource & has executable
295 rawValue
= extension
->getObject("OSBundleLibraries");
296 if (!rawValue
&& !is_kernel_resource
&& has_executable
) {
302 libraries
= OSDynamicCast(OSDictionary
, rawValue
);
308 keyIterator
= OSCollectionIterator::withCollection(libraries
);
310 IOLog("Error: Failed to allocate iterator for libraries.\n");
316 while ((key
= OSDynamicCast(OSString
,
317 keyIterator
->getNextObject()))) {
319 OSString
* libraryVersion
= NULL
; // do not release
321 // Each key's length is not >= KMOD_MAX_NAME
322 if (key
->getLength() >= KMOD_MAX_NAME
) {
327 libraryVersion
= OSDynamicCast(OSString
,
328 libraries
->getObject(key
));
329 if (!libraryVersion
) {
334 // Each value is a valid version string
335 vers
= VERS_parse_string(libraryVersion
->getCStringNoCopy());
342 keyIterator
->release();
346 // OSBundleRequired is a legal value - *not* required at boot time
347 // so we can do install CDs and the like with mkext files containing
348 // all normally-used drivers.
349 rawValue
= extension
->getObject("OSBundleRequired");
351 stringValue
= OSDynamicCast(OSString
, rawValue
);
356 if (!stringValue
->isEqualTo("Root") &&
357 !stringValue
->isEqualTo("Local-Root") &&
358 !stringValue
->isEqualTo("Network-Root") &&
359 !stringValue
->isEqualTo("Safe Boot") &&
360 !stringValue
->isEqualTo("Console")) {
370 if (keyIterator
) keyIterator
->release();
375 IOLog(VTYELLOW
"mkext entry %d:." VTRESET
, index
);
377 IOLog(VTYELLOW
"kernel extension" VTRESET
);
379 IOLog(VTYELLOW
"info dictionary isn't a dictionary\n"
381 } else if (id_missing
) {
383 IOLog(VTYELLOW
"mkext entry %d:." VTRESET
, index
);
385 IOLog(VTYELLOW
"kernel extension" VTRESET
);
387 IOLog(VTYELLOW
"\"CFBundleIdentifier\" property is "
388 "missing or not a string\n"
391 IOLog(VTYELLOW
"kernel extension \"%s\": info dictionary is invalid\n"
392 VTRESET
, bundleIdentifier
->getCStringNoCopy());
401 /*********************************************************************
402 *********************************************************************/
403 OSDictionary
* compareExtensionVersions(
404 OSDictionary
* incumbent
,
405 OSDictionary
* candidate
) {
407 OSDictionary
* winner
= NULL
;
409 OSDictionary
* incumbentPlist
= NULL
;
410 OSDictionary
* candidatePlist
= NULL
;
411 OSString
* incumbentName
= NULL
;
412 OSString
* candidateName
= NULL
;
413 OSString
* incumbentVersionString
= NULL
;
414 OSString
* candidateVersionString
= NULL
;
415 VERS_version incumbent_vers
= 0;
416 VERS_version candidate_vers
= 0;
418 incumbentPlist
= OSDynamicCast(OSDictionary
,
419 incumbent
->getObject("plist"));
420 candidatePlist
= OSDynamicCast(OSDictionary
,
421 candidate
->getObject("plist"));
423 if (!incumbentPlist
|| !candidatePlist
) {
424 IOLog("compareExtensionVersions() called with invalid "
425 "extension dictionaries.\n");
431 incumbentName
= OSDynamicCast(OSString
,
432 incumbentPlist
->getObject("CFBundleIdentifier"));
433 candidateName
= OSDynamicCast(OSString
,
434 candidatePlist
->getObject("CFBundleIdentifier"));
435 incumbentVersionString
= OSDynamicCast(OSString
,
436 incumbentPlist
->getObject("CFBundleVersion"));
437 candidateVersionString
= OSDynamicCast(OSString
,
438 candidatePlist
->getObject("CFBundleVersion"));
440 if (!incumbentName
|| !candidateName
||
441 !incumbentVersionString
|| !candidateVersionString
) {
443 IOLog("compareExtensionVersions() called with invalid "
444 "extension dictionaries.\n");
450 if (strcmp(incumbentName
->getCStringNoCopy(),
451 candidateName
->getCStringNoCopy())) {
453 IOLog("compareExtensionVersions() called with different "
454 "extension names (%s and %s).\n",
455 incumbentName
->getCStringNoCopy(),
456 candidateName
->getCStringNoCopy());
462 incumbent_vers
= VERS_parse_string(incumbentVersionString
->getCStringNoCopy());
463 if (incumbent_vers
< 0) {
465 IOLog(VTYELLOW
"Error parsing version string for extension %s (%s)\n"
467 incumbentName
->getCStringNoCopy(),
468 incumbentVersionString
->getCStringNoCopy());
474 candidate_vers
= VERS_parse_string(candidateVersionString
->getCStringNoCopy());
475 if (candidate_vers
< 0) {
477 IOLog(VTYELLOW
"Error parsing version string for extension %s (%s)\n"
479 candidateName
->getCStringNoCopy(),
480 candidateVersionString
->getCStringNoCopy());
486 if (candidate_vers
> incumbent_vers
) {
487 IOLog(VTYELLOW
"Replacing extension \"%s\" with newer version "
488 "(%s -> %s).\n" VTRESET
,
489 incumbentName
->getCStringNoCopy(),
490 incumbentVersionString
->getCStringNoCopy(),
491 candidateVersionString
->getCStringNoCopy());
496 IOLog(VTYELLOW
"Skipping duplicate extension \"%s\" with older/same "
497 " version (%s -> %s).\n" VTRESET
,
498 candidateName
->getCStringNoCopy(),
499 candidateVersionString
->getCStringNoCopy(),
500 incumbentVersionString
->getCStringNoCopy());
508 // no cleanup, how nice
513 /*********************************************************************
514 * This function merges entries in the mergeFrom dictionary into the
515 * mergeInto dictionary. If it returns false, the two dictionaries are
516 * not altered. If it returns true, then mergeInto may have new
517 * entries; any keys that were already present in mergeInto are
518 * removed from mergeFrom, so that the caller can see what was
520 *********************************************************************/
521 bool mergeExtensionDictionaries(OSDictionary
* mergeInto
,
522 OSDictionary
* mergeFrom
) {
525 OSDictionary
* mergeIntoCopy
= NULL
; // must release
526 OSDictionary
* mergeFromCopy
= NULL
; // must release
527 OSCollectionIterator
* keyIterator
= NULL
; // must release
528 OSString
* key
; // don't release
530 /* Add 1 to count to guarantee copy can grow (grr).
532 mergeIntoCopy
= OSDictionary::withDictionary(mergeInto
,
533 mergeInto
->getCount() + 1);
534 if (!mergeIntoCopy
) {
535 IOLog("Error: Failed to copy 'into' extensions dictionary "
542 /* Add 1 to count to guarantee copy can grow (grr).
544 mergeFromCopy
= OSDictionary::withDictionary(mergeFrom
,
545 mergeFrom
->getCount() + 1);
546 if (!mergeFromCopy
) {
547 IOLog("Error: Failed to copy 'from' extensions dictionary "
554 keyIterator
= OSCollectionIterator::withCollection(mergeFrom
);
556 IOLog("Error: Failed to allocate iterator for extensions.\n");
564 * Loop through "from" dictionary, checking if the identifier already
565 * exists in the "into" dictionary and checking versions if it does.
567 while ((key
= OSDynamicCast(OSString
, keyIterator
->getNextObject()))) {
568 OSDictionary
* incumbentExt
= OSDynamicCast(OSDictionary
,
569 mergeIntoCopy
->getObject(key
));
570 OSDictionary
* candidateExt
= OSDynamicCast(OSDictionary
,
571 mergeFrom
->getObject(key
));
574 if (!mergeIntoCopy
->setObject(key
, candidateExt
)) {
576 /* This is a fatal error, so bail.
578 IOLog("mergeExtensionDictionaries(): Failed to add "
580 key
->getCStringNoCopy());
586 OSDictionary
* mostRecentExtension
=
587 compareExtensionVersions(incumbentExt
, candidateExt
);
589 if (mostRecentExtension
== incumbentExt
) {
590 mergeFromCopy
->removeObject(key
);
591 } else if (mostRecentExtension
== candidateExt
) {
593 if (!mergeIntoCopy
->setObject(key
, candidateExt
)) {
595 /* This is a fatal error, so bail.
597 IOLog("mergeExtensionDictionaries(): Failed to add "
599 key
->getCStringNoCopy());
604 } else /* should be NULL */ {
606 /* This is a nonfatal error, so continue doing others.
608 IOLog("mergeExtensionDictionaries(): Error comparing "
609 "versions of duplicate extensions %s.\n",
610 key
->getCStringNoCopy());
619 /* If successful, replace the contents of the original
620 * dictionaries with those of the modified copies.
623 mergeInto
->flushCollection();
624 mergeInto
->merge(mergeIntoCopy
);
625 mergeFrom
->flushCollection();
626 mergeFrom
->merge(mergeFromCopy
);
629 if (mergeIntoCopy
) mergeIntoCopy
->release();
630 if (mergeFromCopy
) mergeFromCopy
->release();
631 if (keyIterator
) keyIterator
->release();
638 * These bits are used to parse data made available by bootx.
640 #define BOOTX_KEXT_PREFIX "Driver-"
641 #define BOOTX_MULTIKEXT_PREFIX "DriversPackage-"
643 typedef struct MemoryMapFileInfo
{
648 typedef struct BootxDriverInfo
{
655 typedef struct MkextEntryInfo
{
656 vm_address_t base_address
;
657 mkext_file
* fileinfo
;
661 /*********************************************************************
662 * This private function reads the data for a single extension from
663 * the bootx memory-map's propery dict, returning a dictionary with
664 * keys "plist" for the extension's Info.plist as a parsed OSDictionary
665 * and "code" for the extension's executable code as an OSData.
666 *********************************************************************/
667 OSDictionary
* readExtension(OSDictionary
* propertyDict
,
668 const char * memory_map_name
) {
671 OSData
* bootxDriverDataObject
= NULL
;
672 OSDictionary
* driverPlist
= NULL
;
673 OSString
* driverName
= NULL
;
674 OSData
* driverCode
= NULL
;
675 OSString
* errorString
= NULL
;
676 OSDictionary
* driverDict
= NULL
;
678 MemoryMapFileInfo
* driverInfo
= 0;
679 BootxDriverInfo
* dataBuffer
;
681 kmod_info_t
* loaded_kmod
= NULL
;
683 bootxDriverDataObject
= OSDynamicCast(OSData
,
684 propertyDict
->getObject(memory_map_name
));
685 // don't release bootxDriverDataObject
687 if (!bootxDriverDataObject
) {
688 IOLog("Error: No driver data object "
689 "for device tree entry \"%s\".\n",
696 driverDict
= OSDictionary::withCapacity(2);
698 IOLog("Error: Couldn't allocate dictionary "
699 "for device tree entry \"%s\".\n", memory_map_name
);
705 driverInfo
= (MemoryMapFileInfo
*)
706 bootxDriverDataObject
->getBytesNoCopy(0,
707 sizeof(MemoryMapFileInfo
));
708 dataBuffer
= (BootxDriverInfo
*)ml_static_ptovirt(
711 IOLog("Error: No data buffer "
712 "for device tree entry \"%s\".\n", memory_map_name
);
718 driverPlist
= OSDynamicCast(OSDictionary
,
719 OSUnserializeXML(dataBuffer
->plistAddr
, &errorString
));
721 IOLog("Error: Couldn't read XML property list "
722 "for device tree entry \"%s\".\n", memory_map_name
);
725 IOLog("XML parse error: %s.\n",
726 errorString
->getCStringNoCopy());
734 driverName
= OSDynamicCast(OSString
,
735 driverPlist
->getObject("CFBundleIdentifier")); // do not release
737 IOLog("Error: Device tree entry \"%s\" has "
738 "no \"CFBundleIdentifier\" property.\n", memory_map_name
);
744 /* Check if kmod is already loaded and is a real loadable one (has
747 loaded_kmod
= kmod_lookupbyname_locked(driverName
->getCStringNoCopy());
748 if (loaded_kmod
&& loaded_kmod
->address
) {
749 IOLog("Skipping new extension \"%s\"; an extension named "
750 "\"%s\" is already loaded.\n",
751 driverName
->getCStringNoCopy(),
758 if (!validateExtensionDict(driverPlist
, -1)) {
759 // validateExtensionsDict() logs an error
764 driverDict
->setObject("plist", driverPlist
);
766 /* It's perfectly okay for a KEXT to have no executable.
767 * Check that moduleAddr is nonzero before attempting to
770 * NOTE: The driverCode object is created "no-copy", so
771 * it doesn't own that memory. The memory must be freed
772 * separately from the OSData object (see
773 * clearStartupExtensionsAndLoaderInfo() at the end of this file).
775 if (dataBuffer
->moduleAddr
&& dataBuffer
->moduleLength
) {
776 driverCode
= OSData::withBytesNoCopy(dataBuffer
->moduleAddr
,
777 dataBuffer
->moduleLength
);
779 IOLog("Error: Couldn't allocate data object "
780 "to hold code for device tree entry \"%s\".\n",
788 driverDict
->setObject("code", driverCode
);
795 kfree((unsigned int)loaded_kmod
, sizeof(kmod_info_t
));
798 // do not release bootxDriverDataObject
799 // do not release driverName
802 driverPlist
->release();
805 errorString
->release();
808 driverCode
->release();
812 driverDict
->release();
820 /*********************************************************************
821 * Used to uncompress a single file entry in an mkext archive.
823 * The OSData returned does not own its memory! You must deallocate
824 * that memory using kmem_free() before releasing the OSData().
825 *********************************************************************/
826 static bool uncompressFile(u_int8_t
*base_address
, mkext_file
* fileinfo
,
827 /* out */ OSData
** file
) {
830 kern_return_t kern_result
;
831 u_int8_t
* uncompressed_file
= 0; // kmem_free() on error
832 OSData
* uncompressedFile
= 0; // returned
833 size_t uncompressed_size
= 0;
835 size_t offset
= OSSwapBigToHostInt32(fileinfo
->offset
);
836 size_t compsize
= OSSwapBigToHostInt32(fileinfo
->compsize
);
837 size_t realsize
= OSSwapBigToHostInt32(fileinfo
->realsize
);
838 time_t modifiedsecs
= OSSwapBigToHostInt32(fileinfo
->modifiedsecs
);
842 /* If these four fields are zero there's no file, but that isn't
845 if (offset
== 0 && compsize
== 0 &&
846 realsize
== 0 && modifiedsecs
== 0) {
850 // Add 1 for '\0' to terminate XML string!
851 kern_result
= kmem_alloc(kernel_map
, (vm_offset_t
*)&uncompressed_file
,
853 if (kern_result
!= KERN_SUCCESS
) {
854 IOLog("Error: Couldn't allocate data buffer "
855 "to uncompress file.\n");
861 uncompressedFile
= OSData::withBytesNoCopy(uncompressed_file
,
863 if (!uncompressedFile
) {
864 IOLog("Error: Couldn't allocate data object "
865 "to uncompress file.\n");
872 uncompressed_size
= decompress_lzss(uncompressed_file
,
873 base_address
+ offset
,
875 if (uncompressed_size
!= realsize
) {
876 IOLog("Error: Uncompressed file is not the length "
882 uncompressed_file
[uncompressed_size
] = '\0';
884 bcopy(base_address
+ offset
, uncompressed_file
,
886 uncompressed_file
[realsize
] = '\0';
889 *file
= uncompressedFile
;
893 if (uncompressed_file
) {
894 kmem_free(kernel_map
, (vm_address_t
)uncompressed_file
,
897 if (uncompressedFile
) {
898 uncompressedFile
->release();
905 bool uncompressModule(OSData
*compData
, /* out */ OSData
** file
) {
907 MkextEntryInfo
*info
= (MkextEntryInfo
*) compData
->getBytesNoCopy();
909 return uncompressFile((u_int8_t
*) info
->base_address
,
910 info
->fileinfo
, file
);
914 /*********************************************************************
915 * Does the work of pulling extensions out of an mkext archive located
917 *********************************************************************/
918 bool extractExtensionsFromArchive(MemoryMapFileInfo
* mkext_file_info
,
919 OSDictionary
* extensions
) {
923 u_int8_t
* crc_address
= 0;
925 mkext_header
* mkext_data
= 0; // don't free
926 mkext_kext
* onekext_data
= 0; // don't free
927 mkext_file
* plist_file
= 0; // don't free
928 mkext_file
* module_file
= 0; // don't free
929 kmod_info_t
* loaded_kmod
= 0; // must free
931 OSData
* driverPlistDataObject
= 0; // must release
932 OSDictionary
* driverPlist
= 0; // must release
933 OSData
* driverCode
= 0; // must release
934 OSDictionary
* driverDict
= 0; // must release
935 OSString
* moduleName
= 0; // don't release
936 OSString
* errorString
= NULL
; // must release
938 OSData
* moduleInfo
= 0; // must release
939 MkextEntryInfo module_info
;
941 mkext_data
= (mkext_header
*)mkext_file_info
->paddr
;
943 if (OSSwapBigToHostInt32(mkext_data
->magic
) != MKEXT_MAGIC
||
944 OSSwapBigToHostInt32(mkext_data
->signature
) != MKEXT_SIGN
) {
945 IOLog("Error: Extension archive has invalid magic or signature.\n");
951 if (OSSwapBigToHostInt32(mkext_data
->length
) != mkext_file_info
->length
) {
952 IOLog("Error: Mismatch between extension archive & "
953 "recorded length.\n");
959 crc_address
= (u_int8_t
*)&mkext_data
->version
;
960 checksum
= adler32(crc_address
,
961 (unsigned int)mkext_data
+
962 OSSwapBigToHostInt32(mkext_data
->length
) - (unsigned int)crc_address
);
964 if (OSSwapBigToHostInt32(mkext_data
->adler32
) != checksum
) {
965 IOLog("Error: Extension archive has a bad checksum.\n");
971 /* If the MKEXT archive isn't fat, check that the CPU type & subtype
972 * match that of the running kernel.
974 if (OSSwapBigToHostInt32(mkext_data
->cputype
) != (UInt32
)CPU_TYPE_ANY
) {
975 kern_return_t kresult
= KERN_FAILURE
;
976 host_basic_info_data_t hostinfo
;
977 host_info_t hostinfo_ptr
= (host_info_t
)&hostinfo
;
978 mach_msg_type_number_t count
= sizeof(hostinfo
)/sizeof(integer_t
);
980 kresult
= host_info((host_t
)1, HOST_BASIC_INFO
,
981 hostinfo_ptr
, &count
);
982 if (kresult
!= KERN_SUCCESS
) {
983 IOLog("Error: Couldn't get current host info.\n");
988 if ((UInt32
)hostinfo
.cpu_type
!=
989 OSSwapBigToHostInt32(mkext_data
->cputype
)) {
991 IOLog("Error: Extension archive doesn't contain software "
992 "for this computer's CPU type.\n");
997 if (!check_cpu_subtype(OSSwapBigToHostInt32(mkext_data
->cpusubtype
))) {
998 IOLog("Error: Extension archive doesn't contain software "
999 "for this computer's CPU subtype.\n");
1006 for (unsigned int i
= 0;
1007 i
< OSSwapBigToHostInt32(mkext_data
->numkexts
);
1011 kfree((unsigned int)loaded_kmod
, sizeof(kmod_info_t
));
1015 if (driverPlistDataObject
) {
1016 kmem_free(kernel_map
,
1017 (unsigned int)driverPlistDataObject
->getBytesNoCopy(),
1018 driverPlistDataObject
->getLength());
1020 driverPlistDataObject
->release();
1021 driverPlistDataObject
= NULL
;
1024 driverPlist
->release();
1028 driverCode
->release();
1032 driverDict
->release();
1036 errorString
->release();
1040 onekext_data
= &mkext_data
->kext
[i
];
1041 plist_file
= &onekext_data
->plist
;
1042 module_file
= &onekext_data
->module;
1044 if (!uncompressFile((u_int8_t
*)mkext_data
, plist_file
,
1045 &driverPlistDataObject
)) {
1047 IOLog("Error: couldn't uncompress plist file "
1048 "from multikext archive entry %d.\n", i
);
1053 if (!driverPlistDataObject
) {
1054 IOLog("Error: No property list present "
1055 "for multikext archive entry %d.\n", i
);
1059 driverPlist
= OSDynamicCast(OSDictionary
,
1061 (char *)driverPlistDataObject
->getBytesNoCopy(),
1064 IOLog("Error: Couldn't read XML property list "
1065 "for multikext archive entry %d.\n", i
);
1068 IOLog("XML parse error: %s.\n",
1069 errorString
->getCStringNoCopy());
1075 if (!validateExtensionDict(driverPlist
, i
)) {
1076 // validateExtensionsDict() logs an error
1082 /* Get the extension's module name. This is used to record
1085 moduleName
= OSDynamicCast(OSString
,
1086 driverPlist
->getObject("CFBundleIdentifier")); // do not release
1088 IOLog("Error: Multikext archive entry %d has "
1089 "no \"CFBundleIdentifier\" property.\n", i
);
1091 continue; // assume a kext config error & continue
1094 /* Check if kmod is already loaded and is a real loadable one (has
1097 loaded_kmod
= kmod_lookupbyname_locked(moduleName
->getCStringNoCopy());
1098 if (loaded_kmod
&& loaded_kmod
->address
) {
1099 IOLog("Skipping new extension \"%s\"; an extension named "
1100 "\"%s\" is already loaded.\n",
1101 moduleName
->getCStringNoCopy(),
1107 driverDict
= OSDictionary::withCapacity(2);
1109 IOLog("Error: Couldn't allocate dictionary "
1110 "for multikext archive entry %d.\n", i
);
1116 driverDict
->setObject("plist", driverPlist
);
1119 * Prepare an entry to hold the mkext entry info for the
1120 * compressed binary module, if there is one. If all four fields
1121 * of the module entry are zero, there isn't one.
1123 if (!(loaded_kmod
&& loaded_kmod
->address
) && (OSSwapBigToHostInt32(module_file
->offset
) ||
1124 OSSwapBigToHostInt32(module_file
->compsize
) ||
1125 OSSwapBigToHostInt32(module_file
->realsize
) ||
1126 OSSwapBigToHostInt32(module_file
->modifiedsecs
))) {
1128 moduleInfo
= OSData::withCapacity(sizeof(MkextEntryInfo
));
1130 IOLog("Error: Couldn't allocate data object "
1131 "for multikext archive entry %d.\n", i
);
1137 module_info
.base_address
= (vm_address_t
)mkext_data
;
1138 module_info
.fileinfo
= module_file
;
1140 if (!moduleInfo
->appendBytes(&module_info
, sizeof(module_info
))) {
1141 IOLog("Error: Couldn't record info "
1142 "for multikext archive entry %d.\n", i
);
1148 driverDict
->setObject("compressedCode", moduleInfo
);
1151 OSDictionary
* incumbentExt
= OSDynamicCast(OSDictionary
,
1152 extensions
->getObject(moduleName
));
1154 if (!incumbentExt
) {
1155 extensions
->setObject(moduleName
, driverDict
);
1157 OSDictionary
* mostRecentExtension
=
1158 compareExtensionVersions(incumbentExt
, driverDict
);
1160 if (mostRecentExtension
== incumbentExt
) {
1161 /* Do nothing, we've got the most recent. */
1162 } else if (mostRecentExtension
== driverDict
) {
1163 if (!extensions
->setObject(moduleName
, driverDict
)) {
1165 /* This is a fatal error, so bail.
1167 IOLog("extractExtensionsFromArchive(): Failed to add "
1169 moduleName
->getCStringNoCopy());
1174 } else /* should be NULL */ {
1176 /* This is a nonfatal error, so continue.
1178 IOLog("extractExtensionsFromArchive(): Error comparing "
1179 "versions of duplicate extensions %s.\n",
1180 moduleName
->getCStringNoCopy());
1189 if (loaded_kmod
) kfree((unsigned int)loaded_kmod
, sizeof(kmod_info_t
));
1190 if (driverPlistDataObject
) {
1191 kmem_free(kernel_map
,
1192 (unsigned int)driverPlistDataObject
->getBytesNoCopy(),
1193 driverPlistDataObject
->getLength());
1194 driverPlistDataObject
->release();
1196 if (driverPlist
) driverPlist
->release();
1197 if (driverCode
) driverCode
->release();
1198 if (moduleInfo
) moduleInfo
->release();
1199 if (driverDict
) driverDict
->release();
1200 if (errorString
) errorString
->release();
1205 /*********************************************************************
1207 *********************************************************************/
1208 bool readExtensions(OSDictionary
* propertyDict
,
1209 const char * memory_map_name
,
1210 OSDictionary
* extensions
) {
1213 OSData
* mkextDataObject
= 0; // don't release
1214 MemoryMapFileInfo
* mkext_file_info
= 0; // don't free
1216 mkextDataObject
= OSDynamicCast(OSData
,
1217 propertyDict
->getObject(memory_map_name
));
1218 // don't release mkextDataObject
1220 if (!mkextDataObject
) {
1221 IOLog("Error: No mkext data object "
1222 "for device tree entry \"%s\".\n",
1229 mkext_file_info
= (MemoryMapFileInfo
*)mkextDataObject
->getBytesNoCopy();
1230 if (!mkext_file_info
) {
1235 result
= extractExtensionsFromArchive(mkext_file_info
, extensions
);
1239 if (!result
&& extensions
) {
1240 extensions
->flushCollection();
1247 /*********************************************************************
1248 * Adds the personalities for an extensions dictionary to the global
1250 *********************************************************************/
1251 bool addPersonalities(OSDictionary
* extensions
) {
1253 OSCollectionIterator
* keyIterator
= NULL
; // must release
1254 OSString
* key
; // don't release
1255 OSDictionary
* driverDict
= NULL
; // don't release
1256 OSDictionary
* driverPlist
= NULL
; // don't release
1257 OSDictionary
* thisDriverPersonalities
= NULL
; // don't release
1258 OSArray
* allDriverPersonalities
= NULL
; // must release
1260 allDriverPersonalities
= OSArray::withCapacity(1);
1261 if (!allDriverPersonalities
) {
1262 IOLog("Error: Couldn't allocate personality dictionary.\n");
1268 /* Record all personalities found so that they can be
1269 * added to the catalogue.
1270 * Note: Not all extensions have personalities.
1273 keyIterator
= OSCollectionIterator::withCollection(extensions
);
1275 IOLog("Error: Couldn't allocate iterator to record personalities.\n");
1281 while ( ( key
= OSDynamicCast(OSString
,
1282 keyIterator
->getNextObject() ))) {
1284 driverDict
= OSDynamicCast(OSDictionary
,
1285 extensions
->getObject(key
));
1286 driverPlist
= OSDynamicCast(OSDictionary
,
1287 driverDict
->getObject("plist"));
1288 thisDriverPersonalities
= OSDynamicCast(OSDictionary
,
1289 driverPlist
->getObject("IOKitPersonalities"));
1291 if (thisDriverPersonalities
) {
1292 OSCollectionIterator
* pIterator
;
1294 pIterator
= OSCollectionIterator::withCollection(
1295 thisDriverPersonalities
);
1297 IOLog("Error: Couldn't allocate iterator "
1298 "to record extension personalities.\n");
1302 while ( (key
= OSDynamicCast(OSString
,
1303 pIterator
->getNextObject())) ) {
1305 OSDictionary
* personality
= OSDynamicCast(
1307 thisDriverPersonalities
->getObject(key
));
1309 allDriverPersonalities
->setObject(personality
);
1312 pIterator
->release();
1314 } /* extract personalities */
1317 /* Add all personalities found to the IOCatalogue,
1318 * but don't start matching.
1320 gIOCatalogue
->addDrivers(allDriverPersonalities
, false);
1324 if (allDriverPersonalities
) allDriverPersonalities
->release();
1325 if (keyIterator
) keyIterator
->release();
1331 /*********************************************************************
1332 * Called from IOCatalogue to add extensions from an mkext archive.
1333 * This function makes a copy of the mkext object passed in because
1334 * the device tree support code dumps it after calling us (indirectly
1335 * through the IOCatalogue).
1336 *********************************************************************/
1337 bool addExtensionsFromArchive(OSData
* mkextDataObject
) {
1340 OSDictionary
* startupExtensions
= NULL
; // don't release
1341 OSArray
* bootLoaderObjects
= NULL
; // don't release
1342 OSDictionary
* extensions
= NULL
; // must release
1343 MemoryMapFileInfo mkext_file_info
;
1344 OSCollectionIterator
* keyIterator
= NULL
; // must release
1345 OSString
* key
= NULL
; // don't release
1347 startupExtensions
= getStartupExtensions();
1348 if (!startupExtensions
) {
1349 IOLog("Can't record extension archive; there is no"
1350 " extensions dictionary.\n");
1356 bootLoaderObjects
= getBootLoaderObjects();
1357 if (! bootLoaderObjects
) {
1358 IOLog("Error: Couldn't allocate array to hold temporary objects.\n");
1364 extensions
= OSDictionary::withCapacity(2);
1366 IOLog("Error: Couldn't allocate dictionary to unpack "
1367 "extension archive.\n");
1373 mkext_file_info
.paddr
= (UInt32
)mkextDataObject
->getBytesNoCopy();
1374 mkext_file_info
.length
= mkextDataObject
->getLength();
1376 /* Save the local mkext data object so that we can deallocate it later.
1378 bootLoaderObjects
->setObject(mkextDataObject
);
1380 result
= extractExtensionsFromArchive(&mkext_file_info
, extensions
);
1382 IOLog("Error: Failed to extract extensions from archive.\n");
1388 result
= mergeExtensionDictionaries(startupExtensions
, extensions
);
1390 IOLog("Error: Failed to merge new extensions into existing set.\n");
1395 result
= addPersonalities(extensions
);
1397 IOLog("Error: Failed to add personalities for extensions extracted "
1407 IOLog("Error: Failed to record extensions from archive.\n");
1410 keyIterator
= OSCollectionIterator::withCollection(
1414 while ( (key
= OSDynamicCast(OSString
,
1415 keyIterator
->getNextObject())) ) {
1417 IOLog("Added extension \"%s\" from archive.\n",
1418 key
->getCStringNoCopy());
1421 keyIterator
->release();
1425 if (extensions
) extensions
->release();
1431 /*********************************************************************
1432 * This function builds dictionaries for the startup extensions
1433 * put into memory by bootx, recording each in the startup extensions
1434 * dictionary. The dictionary format is this:
1437 * "plist" = (the extension's Info.plist as an OSDictionary)
1438 * "code" = (an OSData containing the executable file)
1441 * This function returns true if any extensions were found and
1442 * recorded successfully, or if there are no start extensions,
1443 * and false if an unrecoverable error occurred. An error reading
1444 * a single extension is not considered fatal, and this function
1445 * will simply skip the problematic extension to try the next one.
1446 *********************************************************************/
1448 bool recordStartupExtensions(void) {
1450 OSDictionary
* startupExtensions
= NULL
; // must release
1451 OSDictionary
* existingExtensions
= NULL
; // don't release
1452 OSDictionary
* mkextExtensions
= NULL
; // must release
1453 IORegistryEntry
* bootxMemoryMap
= NULL
; // must release
1454 OSDictionary
* propertyDict
= NULL
; // must release
1455 OSCollectionIterator
* keyIterator
= NULL
; // must release
1456 OSString
* key
= NULL
; // don't release
1458 OSDictionary
* newDriverDict
= NULL
; // must release
1459 OSDictionary
* driverPlist
= NULL
; // don't release
1461 struct section
* infosect
;
1462 struct section
* symsect
;
1463 unsigned int prelinkedCount
= 0;
1465 existingExtensions
= getStartupExtensions();
1466 if (!existingExtensions
) {
1467 IOLog("Error: There is no dictionary for startup extensions.\n");
1473 startupExtensions
= OSDictionary::withCapacity(1);
1474 if (!startupExtensions
) {
1475 IOLog("Error: Couldn't allocate dictionary "
1476 "to record startup extensions.\n");
1483 // add any prelinked modules as startup extensions
1485 infosect
= getsectbyname("__PRELINK", "__info");
1486 symsect
= getsectbyname("__PRELINK", "__symtab");
1487 if (infosect
&& infosect
->addr
&& infosect
->size
1488 && symsect
&& symsect
->addr
&& symsect
->size
) do
1490 gIOPrelinkedModules
= OSDynamicCast(OSArray
,
1491 OSUnserializeXML((const char *) infosect
->addr
, NULL
));
1493 if (!gIOPrelinkedModules
)
1495 for( unsigned int idx
= 0;
1496 (propertyDict
= OSDynamicCast(OSDictionary
, gIOPrelinkedModules
->getObject(idx
)));
1499 enum { kPrelinkReservedCount
= 4 };
1501 /* Get the extension's module name. This is used to record
1502 * the extension. Do *not* release the moduleName.
1504 OSString
* moduleName
= OSDynamicCast(OSString
,
1505 propertyDict
->getObject("CFBundleIdentifier"));
1507 IOLog("Error: Prelinked module entry has "
1508 "no \"CFBundleIdentifier\" property.\n");
1513 /* Add the kext, & its plist.
1515 newDriverDict
= OSDictionary::withCapacity(4);
1516 assert(newDriverDict
);
1517 newDriverDict
->setObject("plist", propertyDict
);
1518 startupExtensions
->setObject(moduleName
, newDriverDict
);
1519 newDriverDict
->release();
1521 /* Add the code if present.
1523 OSData
* data
= OSDynamicCast(OSData
, propertyDict
->getObject("OSBundlePrelink"));
1525 if (data
->getLength() < (kPrelinkReservedCount
* sizeof(UInt32
))) {
1526 IOLog("Error: Prelinked module entry has "
1527 "invalid \"OSBundlePrelink\" property.\n");
1532 prelink
= (UInt32
*) data
->getBytesNoCopy();
1533 kmod_info_t
* kmod_info
= (kmod_info_t
*) OSReadBigInt32(prelink
, 0);
1534 // end of "file" is end of symbol sect
1535 data
= OSData::withBytesNoCopy((void *) kmod_info
->address
,
1536 symsect
->addr
+ symsect
->size
- kmod_info
->address
);
1537 newDriverDict
->setObject("code", data
);
1542 /* Add the symbols if present.
1544 OSNumber
* num
= OSDynamicCast(OSNumber
, propertyDict
->getObject("OSBundlePrelinkSymbols"));
1546 UInt32 offset
= num
->unsigned32BitValue();
1547 data
= OSData::withBytesNoCopy((void *) (symsect
->addr
+ offset
), symsect
->size
- offset
);
1548 newDriverDict
->setObject("code", data
);
1554 if (gIOPrelinkedModules
)
1555 IOLog("%d prelinked modules\n", prelinkedCount
);
1559 virt
= ml_static_ptovirt(infosect
->addr
);
1561 ml_static_mfree(virt
, infosect
->size
);
1563 newDriverDict
= NULL
;
1569 IORegistryEntry::fromPath(
1570 "/chosen/memory-map", // path
1573 // return value is retained so be sure to release it
1575 if (!bootxMemoryMap
) {
1576 IOLog("Error: Couldn't read booter memory map.\n");
1582 propertyDict
= bootxMemoryMap
->dictionaryWithProperties();
1583 if (!propertyDict
) {
1584 IOLog("Error: Couldn't get property dictionary "
1585 "from memory map.\n");
1591 keyIterator
= OSCollectionIterator::withCollection(propertyDict
);
1593 IOLog("Error: Couldn't allocate iterator for driver images.\n");
1599 while ( (key
= OSDynamicCast(OSString
,
1600 keyIterator
->getNextObject())) ) {
1601 /* Clear newDriverDict & mkextExtensions upon entry to the loop,
1602 * handling both successful and unsuccessful iterations.
1604 if (newDriverDict
) {
1605 newDriverDict
->release();
1606 newDriverDict
= NULL
;
1608 if (mkextExtensions
) {
1609 mkextExtensions
->release();
1610 mkextExtensions
= NULL
;
1613 const char * keyValue
= key
->getCStringNoCopy();
1615 if ( !strncmp(keyValue
, BOOTX_KEXT_PREFIX
,
1616 strlen(BOOTX_KEXT_PREFIX
)) ) {
1618 /* Read the extension from the bootx-supplied memory.
1620 newDriverDict
= readExtension(propertyDict
, keyValue
);
1621 if (!newDriverDict
) {
1622 IOLog("Error: Couldn't read data "
1623 "for device tree entry \"%s\".\n", keyValue
);
1629 /* Preprare to record the extension by getting its info plist.
1631 driverPlist
= OSDynamicCast(OSDictionary
,
1632 newDriverDict
->getObject("plist"));
1634 IOLog("Error: Extension in device tree entry \"%s\" "
1635 "has no property list.\n", keyValue
);
1641 /* Get the extension's module name. This is used to record
1642 * the extension. Do *not* release the moduleName.
1644 OSString
* moduleName
= OSDynamicCast(OSString
,
1645 driverPlist
->getObject("CFBundleIdentifier"));
1647 IOLog("Error: Device tree entry \"%s\" has "
1648 "no \"CFBundleIdentifier\" property.\n", keyValue
);
1654 /* All has gone well so far, so record the extension under
1655 * its module name, checking for an existing duplicate.
1657 * Do not release moduleName, as it's part of the extension's
1660 OSDictionary
* incumbentExt
= OSDynamicCast(OSDictionary
,
1661 startupExtensions
->getObject(moduleName
));
1663 if (!incumbentExt
) {
1664 startupExtensions
->setObject(moduleName
, newDriverDict
);
1666 OSDictionary
* mostRecentExtension
=
1667 compareExtensionVersions(incumbentExt
, newDriverDict
);
1669 if (mostRecentExtension
== incumbentExt
) {
1670 /* Do nothing, we've got the most recent. */
1671 } else if (mostRecentExtension
== newDriverDict
) {
1672 if (!startupExtensions
->setObject(moduleName
,
1675 /* This is a fatal error, so bail.
1677 IOLog("recordStartupExtensions(): Failed to add "
1679 moduleName
->getCStringNoCopy());
1684 } else /* should be NULL */ {
1686 /* This is a nonfatal error, so continue.
1688 IOLog("recordStartupExtensions(): Error comparing "
1689 "versions of duplicate extensions %s.\n",
1690 moduleName
->getCStringNoCopy());
1697 } else if ( !strncmp(keyValue
, BOOTX_MULTIKEXT_PREFIX
,
1698 strlen(BOOTX_MULTIKEXT_PREFIX
)) ) {
1700 mkextExtensions
= OSDictionary::withCapacity(10);
1701 if (!mkextExtensions
) {
1702 IOLog("Error: Couldn't allocate dictionary to unpack "
1703 "multi-extension archive.\n");
1706 goto finish
; // allocation failure is fatal for this routine
1708 if (!readExtensions(propertyDict
, keyValue
, mkextExtensions
)) {
1709 IOLog("Error: Couldn't unpack multi-extension archive.\n");
1713 if (!mergeExtensionDictionaries(startupExtensions
,
1716 IOLog("Error: Failed to merge new extensions into "
1720 goto finish
; // merge error is fatal for this routine
1725 // Do not release key.
1727 } /* while ( (key = OSDynamicCast(OSString, ...) ) ) */
1729 if (!mergeExtensionDictionaries(existingExtensions
, startupExtensions
)) {
1730 IOLog("Error: Failed to merge new extensions into existing set.\n");
1736 result
= addPersonalities(startupExtensions
);
1738 IOLog("Error: Failed to add personalities for extensions extracted "
1747 // reused so clear first!
1749 keyIterator
->release();
1754 IOLog("Error: Failed to record startup extensions.\n");
1758 keyIterator
= OSCollectionIterator::withCollection(
1762 while ( (key
= OSDynamicCast(OSString
,
1763 keyIterator
->getNextObject())) ) {
1765 IOLog("Found extension \"%s\".\n",
1766 key
->getCStringNoCopy());
1769 keyIterator
->release();
1775 if (newDriverDict
) newDriverDict
->release();
1776 if (propertyDict
) propertyDict
->release();
1777 if (bootxMemoryMap
) bootxMemoryMap
->release();
1778 if (mkextExtensions
) mkextExtensions
->release();
1779 if (startupExtensions
) startupExtensions
->release();
1785 /*********************************************************************
1786 * This function removes an entry from the dictionary of startup
1787 * extensions. It's used when an extension can't be loaded, for
1788 * whatever reason. For drivers, this allows another matching driver
1789 * to be loaded, so that, for example, a driver for the root device
1791 *********************************************************************/
1792 void removeStartupExtension(const char * extensionName
) {
1793 OSDictionary
* startupExtensions
= NULL
; // don't release
1794 OSDictionary
* extensionDict
= NULL
; // don't release
1795 OSDictionary
* extensionPlist
= NULL
; // don't release
1796 OSDictionary
* extensionPersonalities
= NULL
; // don't release
1797 OSDictionary
* personality
= NULL
; // don't release
1798 OSCollectionIterator
* keyIterator
= NULL
; // must release
1799 OSString
* key
= NULL
; // don't release
1801 startupExtensions
= getStartupExtensions();
1802 if (!startupExtensions
) goto finish
;
1805 /* Find the extension's entry in the dictionary of
1806 * startup extensions.
1808 extensionDict
= OSDynamicCast(OSDictionary
,
1809 startupExtensions
->getObject(extensionName
));
1810 if (!extensionDict
) goto finish
;
1812 extensionPlist
= OSDynamicCast(OSDictionary
,
1813 extensionDict
->getObject("plist"));
1814 if (!extensionPlist
) goto finish
;
1816 extensionPersonalities
= OSDynamicCast(OSDictionary
,
1817 extensionPlist
->getObject("IOKitPersonalities"));
1818 if (!extensionPersonalities
) goto finish
;
1820 /* If it was there, remove it from the catalogue proper
1821 * by calling removeDrivers(). Pass true for the second
1822 * argument to trigger a new round of matching, and
1823 * then remove the extension from the dictionary of startup
1826 keyIterator
= OSCollectionIterator::withCollection(
1827 extensionPersonalities
);
1829 IOLog("Error: Couldn't allocate iterator to scan"
1830 " personalities for %s.\n", extensionName
);
1834 while ((key
= OSDynamicCast(OSString
, keyIterator
->getNextObject()))) {
1835 personality
= OSDynamicCast(OSDictionary
,
1836 extensionPersonalities
->getObject(key
));
1840 gIOCatalogue
->removeDrivers(personality
, true);
1844 startupExtensions
->removeObject(extensionName
);
1848 if (keyIterator
) keyIterator
->release();
1852 /*********************************************************************
1853 * FIXME: This function invalidates the globals gStartupExtensions and
1854 * FIXME: ...gBootLoaderObjects without setting them to NULL. Since
1855 * FIXME: ...the code itself is immediately unloaded, there may not be
1856 * FIXME: ...any reason to worry about that!
1857 *********************************************************************/
1858 void clearStartupExtensionsAndLoaderInfo(void)
1860 OSDictionary
* startupExtensions
= NULL
; // must release
1861 OSArray
* bootLoaderObjects
= NULL
; // must release
1863 IORegistryEntry
* bootxMemoryMap
= NULL
; // must release
1864 OSDictionary
* propertyDict
= NULL
; // must release
1865 OSCollectionIterator
* keyIterator
= NULL
; // must release
1866 OSString
* key
= NULL
; // don't release
1869 * Drop any temporarily held data objects.
1871 bootLoaderObjects
= getBootLoaderObjects();
1872 if (bootLoaderObjects
) {
1873 bootLoaderObjects
->release();
1877 * If any "code" entries in driver dictionaries are accompanied
1878 * by "compressedCode" entries, then those data objects were
1879 * created based of of kmem_alloc()'ed memory, which must be
1882 startupExtensions
= getStartupExtensions();
1883 if (startupExtensions
) {
1885 OSCollectionIterator::withCollection(startupExtensions
);
1887 IOLog("Error: Couldn't allocate iterator for startup "
1890 goto memory_map
; // bail to the memory_map label
1893 while ( (key
= OSDynamicCast(OSString
,
1894 keyIterator
->getNextObject())) ) {
1896 OSDictionary
* driverDict
= 0;
1897 OSData
* codeData
= 0;
1899 driverDict
= OSDynamicCast(OSDictionary
,
1900 startupExtensions
->getObject(key
));
1902 codeData
= OSDynamicCast(OSData
,
1903 driverDict
->getObject("code"));
1906 driverDict
->getObject("compressedCode")) {
1908 kmem_free(kernel_map
,
1909 (unsigned int)codeData
->getBytesNoCopy(),
1910 codeData
->getLength());
1915 keyIterator
->release();
1916 startupExtensions
->release();
1922 * Go through the device tree's memory map and remove any driver
1926 IORegistryEntry::fromPath(
1927 "/chosen/memory-map", // path
1930 // return value is retained so be sure to release it
1932 if (!bootxMemoryMap
) {
1933 IOLog("Error: Couldn't read booter memory map.\n");
1938 propertyDict
= bootxMemoryMap
->dictionaryWithProperties();
1939 if (!propertyDict
) {
1940 IOLog("Error: Couldn't get property dictionary "
1941 "from memory map.\n");
1946 keyIterator
= OSCollectionIterator::withCollection(propertyDict
);
1948 IOLog("Error: Couldn't allocate iterator for driver images.\n");
1953 while ( (key
= OSDynamicCast(OSString
,
1954 keyIterator
->getNextObject())) ) {
1956 const char * keyValue
= key
->getCStringNoCopy();
1958 if ( !strncmp(keyValue
, BOOTX_KEXT_PREFIX
,
1959 strlen(BOOTX_KEXT_PREFIX
)) ||
1960 !strncmp(keyValue
, BOOTX_MULTIKEXT_PREFIX
,
1961 strlen(BOOTX_MULTIKEXT_PREFIX
)) ) {
1963 OSData
* bootxDriverDataObject
= NULL
;
1964 MemoryMapFileInfo
* driverInfo
= 0;
1966 bootxDriverDataObject
= OSDynamicCast(OSData
,
1967 propertyDict
->getObject(keyValue
));
1968 // don't release bootxDriverDataObject
1970 if (!bootxDriverDataObject
) {
1973 driverInfo
= (MemoryMapFileInfo
*)
1974 bootxDriverDataObject
->getBytesNoCopy(0,
1975 sizeof(MemoryMapFileInfo
));
1976 IODTFreeLoaderInfo((char *)keyValue
,
1977 (void *)driverInfo
->paddr
,
1978 (int)driverInfo
->length
);
1983 if (bootxMemoryMap
) bootxMemoryMap
->release();
1984 if (propertyDict
) propertyDict
->release();
1985 if (keyIterator
) keyIterator
->release();