2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 #include <libkern/c++/OSContainers.h>
29 #include <IOKit/IODeviceTreeSupport.h>
30 #include <IOKit/IORegistryEntry.h>
31 #include <IOKit/IOCatalogue.h>
32 #include <IOKit/IOKitKeysPrivate.h>
33 #include <libkern/c++/OSUnserialize.h>
34 #include <libkern/OSByteOrder.h>
35 #include <libsa/catalogue.h>
38 #include <machine/machine_routines.h>
39 #include <mach/host_info.h>
40 #include <mach/kmod.h>
41 #include <libsa/mkext.h>
42 #include <libsa/vers_rsrc.h>
43 #include <mach-o/loader.h>
46 #include <IOKit/IOLib.h>
48 #include <IOKit/assert.h>
51 extern void IODTFreeLoaderInfo( char *key
, void *infoAddr
, int infoSize
);
52 // extern kern_return_t host_info(host_t host,
53 // host_flavor_t flavor,
55 // mach_msg_type_number_t *count);
56 extern int grade_binary(cpu_type_t exectype
, cpu_subtype_t execsubtype
);
57 // Return the address of the named Mach-O segment from the currently
58 // executing 32 bit kernel, or NULL.
59 extern struct segment_command
*getsegbyname(char *seg_name
);
60 // Return the address of the named section from the named Mach-O segment
61 // from the currently executing 32 bit kernel, or NULL.
62 extern struct section
*getsectbyname(char *segname
, char *sectname
);
68 #define VTYELLOW "\033[33m"
69 #define VTRESET "\033[0m"
75 /*********************************************************************
76 *********************************************************************/
77 static OSDictionary
* gStartupExtensions
= 0;
78 static OSArray
* gBootLoaderObjects
= 0;
79 extern OSArray
* gIOPrelinkedModules
;
81 OSDictionary
* getStartupExtensions(void) {
82 if (gStartupExtensions
) {
83 return gStartupExtensions
;
85 gStartupExtensions
= OSDictionary::withCapacity(1);
86 assert (gStartupExtensions
);
88 return gStartupExtensions
;
91 /* This array holds objects that are needed to be held around during
92 * boot before kextd starts up. Currently it contains OSData objects
93 * copied from OF entries for mkext archives in device ROMs. Because
94 * the Device Tree support code dumps these after initially handing
95 * them to us, we have to be able to clean them up later.
97 OSArray
* getBootLoaderObjects(void) {
98 if (gBootLoaderObjects
) {
99 return gBootLoaderObjects
;
101 gBootLoaderObjects
= OSArray::withCapacity(1);
102 assert (gBootLoaderObjects
);
104 return gBootLoaderObjects
;
107 /*********************************************************************
108 * This function checks that a driver dict has all the required
109 * entries and does a little bit of value checking too.
111 * index is nonnegative if the index of an entry from an mkext
113 *********************************************************************/
114 bool validateExtensionDict(OSDictionary
* extension
, int index
) {
117 bool not_a_dict
= false;
118 bool id_missing
= false;
119 bool is_kernel_resource
= false;
120 bool has_executable
= false;
121 bool ineligible_for_safe_boot
= false;
122 OSString
* bundleIdentifier
= NULL
; // do not release
123 OSObject
* rawValue
= NULL
; // do not release
124 OSString
* stringValue
= NULL
; // do not release
125 OSBoolean
* booleanValue
= NULL
; // do not release
126 OSDictionary
* personalities
= NULL
; // do not release
127 OSDictionary
* libraries
= NULL
; // do not release
128 OSCollectionIterator
* keyIterator
= NULL
; // must release
129 OSString
* key
= NULL
; // do not release
131 VERS_version compatible_vers
;
132 char namep
[16]; // unused but needed for PE_parse_boot_arg()
134 // Info dict is a dictionary
135 if (!OSDynamicCast(OSDictionary
, extension
)) {
141 // CFBundleIdentifier is a string - REQUIRED
142 bundleIdentifier
= OSDynamicCast(OSString
,
143 extension
->getObject("CFBundleIdentifier"));
144 if (!bundleIdentifier
) {
150 // Length of CFBundleIdentifier is not >= KMOD_MAX_NAME
151 if (bundleIdentifier
->getLength() >= KMOD_MAX_NAME
) {
156 // CFBundlePackageType is "KEXT" - REQUIRED
157 stringValue
= OSDynamicCast(OSString
,
158 extension
->getObject("CFBundlePackageType"));
163 if (!stringValue
->isEqualTo("KEXT")) {
168 // CFBundleVersion is a string - REQUIRED
169 stringValue
= OSDynamicCast(OSString
,
170 extension
->getObject("CFBundleVersion"));
175 // CFBundleVersion is of valid form
176 vers
= VERS_parse_string(stringValue
->getCStringNoCopy());
182 // OSBundleCompatibleVersion is a string - OPTIONAL
183 rawValue
= extension
->getObject("OSBundleCompatibleVersion");
185 stringValue
= OSDynamicCast(OSString
, rawValue
);
191 // OSBundleCompatibleVersion is of valid form
192 compatible_vers
= VERS_parse_string(stringValue
->getCStringNoCopy());
193 if (compatible_vers
< 0) {
198 // OSBundleCompatibleVersion <= CFBundleVersion
199 if (compatible_vers
> vers
) {
205 // CFBundleExecutable is a string - OPTIONAL
206 rawValue
= extension
->getObject("CFBundleExecutable");
208 stringValue
= OSDynamicCast(OSString
, rawValue
);
209 if (!stringValue
|| stringValue
->getLength() == 0) {
213 has_executable
= true;
216 // OSKernelResource is a boolean value - OPTIONAL
217 rawValue
= extension
->getObject("OSKernelResource");
219 booleanValue
= OSDynamicCast(OSBoolean
, rawValue
);
224 is_kernel_resource
= booleanValue
->isTrue();
227 // IOKitPersonalities is a dictionary - OPTIONAL
228 rawValue
= extension
->getObject("IOKitPersonalities");
230 personalities
= OSDynamicCast(OSDictionary
, rawValue
);
231 if (!personalities
) {
236 keyIterator
= OSCollectionIterator::withCollection(personalities
);
238 IOLog("Error: Failed to allocate iterator for personalities.\n");
244 while ((key
= OSDynamicCast(OSString
, keyIterator
->getNextObject()))) {
245 OSDictionary
* personality
= NULL
; // do not release
247 // Each personality is a dictionary
248 personality
= OSDynamicCast(OSDictionary
,
249 personalities
->getObject(key
));
255 // IOClass exists as a string - REQUIRED
256 if (!OSDynamicCast(OSString
, personality
->getObject("IOClass"))) {
261 // IOProviderClass exists as a string - REQUIRED
262 if (!OSDynamicCast(OSString
,
263 personality
->getObject("IOProviderClass"))) {
269 // CFBundleIdentifier is a string - OPTIONAL - INSERT IF ABSENT!
270 rawValue
= personality
->getObject("CFBundleIdentifier");
272 personality
->setObject("CFBundleIdentifier", bundleIdentifier
);
274 OSString
* personalityID
= NULL
; // do not release
275 personalityID
= OSDynamicCast(OSString
, rawValue
);
276 if (!personalityID
) {
280 // Length of CFBundleIdentifier is not >= KMOD_MAX_NAME
281 if (personalityID
->getLength() >= KMOD_MAX_NAME
) {
288 // IOKitDebug is a number - OPTIONAL
289 rawValue
= personality
->getObject("IOKitDebug");
290 if (rawValue
&& !OSDynamicCast(OSNumber
, rawValue
)) {
296 keyIterator
->release();
301 // OSBundleLibraries is a dictionary - REQUIRED if
302 // not kernel resource & has executable
304 rawValue
= extension
->getObject("OSBundleLibraries");
305 if (!rawValue
&& !is_kernel_resource
&& has_executable
) {
311 libraries
= OSDynamicCast(OSDictionary
, rawValue
);
317 keyIterator
= OSCollectionIterator::withCollection(libraries
);
319 IOLog("Error: Failed to allocate iterator for libraries.\n");
325 while ((key
= OSDynamicCast(OSString
,
326 keyIterator
->getNextObject()))) {
328 OSString
* libraryVersion
= NULL
; // do not release
330 // Each key's length is not >= KMOD_MAX_NAME
331 if (key
->getLength() >= KMOD_MAX_NAME
) {
336 libraryVersion
= OSDynamicCast(OSString
,
337 libraries
->getObject(key
));
338 if (!libraryVersion
) {
343 // Each value is a valid version string
344 vers
= VERS_parse_string(libraryVersion
->getCStringNoCopy());
351 keyIterator
->release();
355 // OSBundleRequired, if present, must have a legal value.
356 // If it is not present and if we are safe-booting,
357 // then the kext is not eligible.
359 rawValue
= extension
->getObject("OSBundleRequired");
361 stringValue
= OSDynamicCast(OSString
, rawValue
);
366 if (!stringValue
->isEqualTo("Root") &&
367 !stringValue
->isEqualTo("Local-Root") &&
368 !stringValue
->isEqualTo("Network-Root") &&
369 !stringValue
->isEqualTo("Safe Boot") &&
370 !stringValue
->isEqualTo("Console")) {
376 } else if (PE_parse_boot_arg("-x", namep
)) { /* safe boot */
377 ineligible_for_safe_boot
= true;
384 if (keyIterator
) keyIterator
->release();
387 if (ineligible_for_safe_boot
) {
388 IOLog(VTYELLOW
"Skipping extension \"%s\" during safe boot "
389 "(no OSBundleRequired property)\n"
391 bundleIdentifier
->getCStringNoCopy());
392 } else if (not_a_dict
) {
394 IOLog(VTYELLOW
"mkext entry %d: " VTRESET
, index
);
396 IOLog(VTYELLOW
"kernel extension " VTRESET
);
398 IOLog(VTYELLOW
"info dictionary isn't a dictionary\n"
400 } else if (id_missing
) {
402 IOLog(VTYELLOW
"mkext entry %d: " VTRESET
, index
);
404 IOLog(VTYELLOW
"kernel extension " VTRESET
);
406 IOLog(VTYELLOW
"\"CFBundleIdentifier\" property is "
407 "missing or not a string\n"
410 IOLog(VTYELLOW
"kernel extension \"%s\": info dictionary is invalid\n"
411 VTRESET
, bundleIdentifier
->getCStringNoCopy());
420 /*********************************************************************
421 *********************************************************************/
422 OSDictionary
* compareExtensionVersions(
423 OSDictionary
* incumbent
,
424 OSDictionary
* candidate
) {
426 OSDictionary
* winner
= NULL
;
428 OSDictionary
* incumbentPlist
= NULL
;
429 OSDictionary
* candidatePlist
= NULL
;
430 OSString
* incumbentName
= NULL
;
431 OSString
* candidateName
= NULL
;
432 OSString
* incumbentVersionString
= NULL
;
433 OSString
* candidateVersionString
= NULL
;
434 VERS_version incumbent_vers
= 0;
435 VERS_version candidate_vers
= 0;
437 incumbentPlist
= OSDynamicCast(OSDictionary
,
438 incumbent
->getObject("plist"));
439 candidatePlist
= OSDynamicCast(OSDictionary
,
440 candidate
->getObject("plist"));
442 if (!incumbentPlist
|| !candidatePlist
) {
443 IOLog("compareExtensionVersions() called with invalid "
444 "extension dictionaries.\n");
450 incumbentName
= OSDynamicCast(OSString
,
451 incumbentPlist
->getObject("CFBundleIdentifier"));
452 candidateName
= OSDynamicCast(OSString
,
453 candidatePlist
->getObject("CFBundleIdentifier"));
454 incumbentVersionString
= OSDynamicCast(OSString
,
455 incumbentPlist
->getObject("CFBundleVersion"));
456 candidateVersionString
= OSDynamicCast(OSString
,
457 candidatePlist
->getObject("CFBundleVersion"));
459 if (!incumbentName
|| !candidateName
||
460 !incumbentVersionString
|| !candidateVersionString
) {
462 IOLog("compareExtensionVersions() called with invalid "
463 "extension dictionaries.\n");
469 if (strcmp(incumbentName
->getCStringNoCopy(),
470 candidateName
->getCStringNoCopy())) {
472 IOLog("compareExtensionVersions() called with different "
473 "extension names (%s and %s).\n",
474 incumbentName
->getCStringNoCopy(),
475 candidateName
->getCStringNoCopy());
481 incumbent_vers
= VERS_parse_string(incumbentVersionString
->getCStringNoCopy());
482 if (incumbent_vers
< 0) {
484 IOLog(VTYELLOW
"Error parsing version string for extension %s (%s)\n"
486 incumbentName
->getCStringNoCopy(),
487 incumbentVersionString
->getCStringNoCopy());
493 candidate_vers
= VERS_parse_string(candidateVersionString
->getCStringNoCopy());
494 if (candidate_vers
< 0) {
496 IOLog(VTYELLOW
"Error parsing version string for extension %s (%s)\n"
498 candidateName
->getCStringNoCopy(),
499 candidateVersionString
->getCStringNoCopy());
505 if (candidate_vers
> incumbent_vers
) {
506 IOLog(VTYELLOW
"Replacing extension \"%s\" with newer version "
507 "(%s -> %s).\n" VTRESET
,
508 incumbentName
->getCStringNoCopy(),
509 incumbentVersionString
->getCStringNoCopy(),
510 candidateVersionString
->getCStringNoCopy());
515 IOLog(VTYELLOW
"Skipping duplicate extension \"%s\" with older/same "
516 " version (%s -> %s).\n" VTRESET
,
517 candidateName
->getCStringNoCopy(),
518 candidateVersionString
->getCStringNoCopy(),
519 incumbentVersionString
->getCStringNoCopy());
527 // no cleanup, how nice
532 /*********************************************************************
533 * This function merges entries in the mergeFrom dictionary into the
534 * mergeInto dictionary. If it returns false, the two dictionaries are
535 * not altered. If it returns true, then mergeInto may have new
536 * entries; any keys that were already present in mergeInto are
537 * removed from mergeFrom, so that the caller can see what was
539 *********************************************************************/
540 bool mergeExtensionDictionaries(OSDictionary
* mergeInto
,
541 OSDictionary
* mergeFrom
) {
544 OSDictionary
* mergeIntoCopy
= NULL
; // must release
545 OSDictionary
* mergeFromCopy
= NULL
; // must release
546 OSCollectionIterator
* keyIterator
= NULL
; // must release
547 OSString
* key
; // don't release
549 /* Add 1 to count to guarantee copy can grow (grr).
551 mergeIntoCopy
= OSDictionary::withDictionary(mergeInto
,
552 mergeInto
->getCount() + 1);
553 if (!mergeIntoCopy
) {
554 IOLog("Error: Failed to copy 'into' extensions dictionary "
561 /* Add 1 to count to guarantee copy can grow (grr).
563 mergeFromCopy
= OSDictionary::withDictionary(mergeFrom
,
564 mergeFrom
->getCount() + 1);
565 if (!mergeFromCopy
) {
566 IOLog("Error: Failed to copy 'from' extensions dictionary "
573 keyIterator
= OSCollectionIterator::withCollection(mergeFrom
);
575 IOLog("Error: Failed to allocate iterator for extensions.\n");
583 * Loop through "from" dictionary, checking if the identifier already
584 * exists in the "into" dictionary and checking versions if it does.
586 while ((key
= OSDynamicCast(OSString
, keyIterator
->getNextObject()))) {
587 OSDictionary
* incumbentExt
= OSDynamicCast(OSDictionary
,
588 mergeIntoCopy
->getObject(key
));
589 OSDictionary
* candidateExt
= OSDynamicCast(OSDictionary
,
590 mergeFrom
->getObject(key
));
593 if (!mergeIntoCopy
->setObject(key
, candidateExt
)) {
595 /* This is a fatal error, so bail.
597 IOLog("mergeExtensionDictionaries(): Failed to add "
599 key
->getCStringNoCopy());
605 OSDictionary
* mostRecentExtension
=
606 compareExtensionVersions(incumbentExt
, candidateExt
);
608 if (mostRecentExtension
== incumbentExt
) {
609 mergeFromCopy
->removeObject(key
);
610 } else if (mostRecentExtension
== candidateExt
) {
612 if (!mergeIntoCopy
->setObject(key
, candidateExt
)) {
614 /* This is a fatal error, so bail.
616 IOLog("mergeExtensionDictionaries(): Failed to add "
618 key
->getCStringNoCopy());
623 } else /* should be NULL */ {
625 /* This is a nonfatal error, so continue doing others.
627 IOLog("mergeExtensionDictionaries(): Error comparing "
628 "versions of duplicate extensions %s.\n",
629 key
->getCStringNoCopy());
638 /* If successful, replace the contents of the original
639 * dictionaries with those of the modified copies.
642 mergeInto
->flushCollection();
643 mergeInto
->merge(mergeIntoCopy
);
644 mergeFrom
->flushCollection();
645 mergeFrom
->merge(mergeFromCopy
);
648 if (mergeIntoCopy
) mergeIntoCopy
->release();
649 if (mergeFromCopy
) mergeFromCopy
->release();
650 if (keyIterator
) keyIterator
->release();
657 * These bits are used to parse data made available by bootx.
659 #define BOOTX_KEXT_PREFIX "Driver-"
660 #define BOOTX_MULTIKEXT_PREFIX "DriversPackage-"
662 typedef struct MemoryMapFileInfo
{
667 typedef struct BootxDriverInfo
{
674 typedef struct MkextEntryInfo
{
675 vm_address_t base_address
;
676 mkext_file
* fileinfo
;
680 /*********************************************************************
681 * This private function reads the data for a single extension from
682 * the bootx memory-map's propery dict, returning a dictionary with
683 * keys "plist" for the extension's Info.plist as a parsed OSDictionary
684 * and "code" for the extension's executable code as an OSData.
685 *********************************************************************/
686 OSDictionary
* readExtension(OSDictionary
* propertyDict
,
687 const char * memory_map_name
) {
690 OSData
* bootxDriverDataObject
= NULL
;
691 OSDictionary
* driverPlist
= NULL
;
692 OSString
* driverName
= NULL
;
693 OSData
* driverCode
= NULL
;
694 OSString
* errorString
= NULL
;
695 OSDictionary
* driverDict
= NULL
;
697 MemoryMapFileInfo
* driverInfo
= 0;
698 BootxDriverInfo
* dataBuffer
;
700 kmod_info_t
* loaded_kmod
= NULL
;
702 bootxDriverDataObject
= OSDynamicCast(OSData
,
703 propertyDict
->getObject(memory_map_name
));
704 // don't release bootxDriverDataObject
706 if (!bootxDriverDataObject
) {
707 IOLog("Error: No driver data object "
708 "for device tree entry \"%s\".\n",
715 driverDict
= OSDictionary::withCapacity(2);
717 IOLog("Error: Couldn't allocate dictionary "
718 "for device tree entry \"%s\".\n", memory_map_name
);
724 driverInfo
= (MemoryMapFileInfo
*)
725 bootxDriverDataObject
->getBytesNoCopy(0,
726 sizeof(MemoryMapFileInfo
));
727 #if defined (__ppc__)
728 dataBuffer
= (BootxDriverInfo
*)ml_static_ptovirt(driverInfo
->paddr
);
729 #elif defined (__i386__)
730 dataBuffer
= (BootxDriverInfo
*)ml_boot_ptovirt(driverInfo
->paddr
);
731 dataBuffer
->plistAddr
= (char *)ml_boot_ptovirt((vm_address_t
)dataBuffer
->plistAddr
);
732 if (dataBuffer
->moduleAddr
)
733 dataBuffer
->moduleAddr
= (void *)ml_boot_ptovirt((vm_address_t
)dataBuffer
->moduleAddr
);
735 #error unsupported architecture
738 IOLog("Error: No data buffer "
739 "for device tree entry \"%s\".\n", memory_map_name
);
745 driverPlist
= OSDynamicCast(OSDictionary
,
746 OSUnserializeXML(dataBuffer
->plistAddr
, &errorString
));
748 IOLog("Error: Couldn't read XML property list "
749 "for device tree entry \"%s\".\n", memory_map_name
);
752 IOLog("XML parse error: %s.\n",
753 errorString
->getCStringNoCopy());
761 driverName
= OSDynamicCast(OSString
,
762 driverPlist
->getObject("CFBundleIdentifier")); // do not release
764 IOLog("Error: Device tree entry \"%s\" has "
765 "no \"CFBundleIdentifier\" property.\n", memory_map_name
);
771 /* Check if kmod is already loaded and is a real loadable one (has
774 loaded_kmod
= kmod_lookupbyname_locked(driverName
->getCStringNoCopy());
775 if (loaded_kmod
&& loaded_kmod
->address
) {
776 IOLog("Skipping new extension \"%s\"; an extension named "
777 "\"%s\" is already loaded.\n",
778 driverName
->getCStringNoCopy(),
785 if (!validateExtensionDict(driverPlist
, -1)) {
786 // validateExtensionsDict() logs an error
791 driverDict
->setObject("plist", driverPlist
);
793 /* It's perfectly okay for a KEXT to have no executable.
794 * Check that moduleAddr is nonzero before attempting to
797 * NOTE: The driverCode object is created "no-copy", so
798 * it doesn't own that memory. The memory must be freed
799 * separately from the OSData object (see
800 * clearStartupExtensionsAndLoaderInfo() at the end of this file).
802 if (dataBuffer
->moduleAddr
&& dataBuffer
->moduleLength
) {
803 driverCode
= OSData::withBytesNoCopy(dataBuffer
->moduleAddr
,
804 dataBuffer
->moduleLength
);
806 IOLog("Error: Couldn't allocate data object "
807 "to hold code for device tree entry \"%s\".\n",
815 driverDict
->setObject("code", driverCode
);
822 kfree(loaded_kmod
, sizeof(kmod_info_t
));
825 // do not release bootxDriverDataObject
826 // do not release driverName
829 driverPlist
->release();
832 errorString
->release();
835 driverCode
->release();
839 driverDict
->release();
847 /*********************************************************************
848 * Used to uncompress a single file entry in an mkext archive.
850 * The OSData returned does not own its memory! You must deallocate
851 * that memory using kmem_free() before releasing the OSData().
852 *********************************************************************/
853 static bool uncompressFile(u_int8_t
*base_address
, mkext_file
* fileinfo
,
854 /* out */ OSData
** file
) {
857 kern_return_t kern_result
;
858 u_int8_t
* uncompressed_file
= 0; // kmem_free() on error
859 OSData
* uncompressedFile
= 0; // returned
860 size_t uncompressed_size
= 0;
862 size_t offset
= OSSwapBigToHostInt32(fileinfo
->offset
);
863 size_t compsize
= OSSwapBigToHostInt32(fileinfo
->compsize
);
864 size_t realsize
= OSSwapBigToHostInt32(fileinfo
->realsize
);
865 time_t modifiedsecs
= OSSwapBigToHostInt32(fileinfo
->modifiedsecs
);
869 /* If these four fields are zero there's no file, but that isn't
872 if (offset
== 0 && compsize
== 0 &&
873 realsize
== 0 && modifiedsecs
== 0) {
877 // Add 1 for '\0' to terminate XML string!
878 kern_result
= kmem_alloc(kernel_map
, (vm_offset_t
*)&uncompressed_file
,
880 if (kern_result
!= KERN_SUCCESS
) {
881 IOLog("Error: Couldn't allocate data buffer "
882 "to uncompress file.\n");
888 uncompressedFile
= OSData::withBytesNoCopy(uncompressed_file
,
890 if (!uncompressedFile
) {
891 IOLog("Error: Couldn't allocate data object "
892 "to uncompress file.\n");
899 uncompressed_size
= decompress_lzss(uncompressed_file
,
900 base_address
+ offset
,
902 if (uncompressed_size
!= realsize
) {
903 IOLog("Error: Uncompressed file is not the length "
909 uncompressed_file
[uncompressed_size
] = '\0';
911 bcopy(base_address
+ offset
, uncompressed_file
,
913 uncompressed_file
[realsize
] = '\0';
916 *file
= uncompressedFile
;
920 if (uncompressed_file
) {
921 kmem_free(kernel_map
, (vm_address_t
)uncompressed_file
,
924 if (uncompressedFile
) {
925 uncompressedFile
->release();
932 bool uncompressModule(OSData
*compData
, /* out */ OSData
** file
) {
934 MkextEntryInfo
*info
= (MkextEntryInfo
*) compData
->getBytesNoCopy();
936 return uncompressFile((u_int8_t
*) info
->base_address
,
937 info
->fileinfo
, file
);
941 /*********************************************************************
942 * Does the work of pulling extensions out of an mkext archive located
944 *********************************************************************/
945 bool extractExtensionsFromArchive(MemoryMapFileInfo
* mkext_file_info
,
946 OSDictionary
* extensions
) {
950 u_int8_t
* crc_address
= 0;
952 mkext_header
* mkext_data
= 0; // don't free
953 mkext_kext
* onekext_data
= 0; // don't free
954 mkext_file
* plist_file
= 0; // don't free
955 mkext_file
* module_file
= 0; // don't free
956 kmod_info_t
* loaded_kmod
= 0; // must free
958 OSData
* driverPlistDataObject
= 0; // must release
959 OSDictionary
* driverPlist
= 0; // must release
960 OSData
* driverCode
= 0; // must release
961 OSDictionary
* driverDict
= 0; // must release
962 OSString
* moduleName
= 0; // don't release
963 OSString
* errorString
= NULL
; // must release
965 OSData
* moduleInfo
= 0; // must release
966 MkextEntryInfo module_info
;
969 #if defined (__ppc__)
970 mkext_data
= (mkext_header
*)mkext_file_info
->paddr
;
971 #elif defined (__i386__)
972 mkext_data
= (mkext_header
*)ml_boot_ptovirt(mkext_file_info
->paddr
);
974 #error unsupported architecture
976 if (OSSwapBigToHostInt32(mkext_data
->magic
) != MKEXT_MAGIC
||
977 OSSwapBigToHostInt32(mkext_data
->signature
) != MKEXT_SIGN
) {
978 IOLog("Error: Extension archive has invalid magic or signature.\n");
984 if (OSSwapBigToHostInt32(mkext_data
->length
) != mkext_file_info
->length
) {
985 IOLog("Error: Mismatch between extension archive & "
986 "recorded length.\n");
992 crc_address
= (u_int8_t
*)&mkext_data
->version
;
993 checksum
= adler32(crc_address
,
994 (unsigned int)mkext_data
+
995 OSSwapBigToHostInt32(mkext_data
->length
) - (unsigned int)crc_address
);
997 if (OSSwapBigToHostInt32(mkext_data
->adler32
) != checksum
) {
998 IOLog("Error: Extension archive has a bad checksum.\n");
1004 IORegistryEntry
* root
= IORegistryEntry::getRegistryRoot();
1006 OSData
* checksumObj
= OSData::withBytes((void *)&checksum
,
1008 assert(checksumObj
);
1010 root
->setProperty(kIOStartupMkextCRC
, checksumObj
);
1011 checksumObj
->release();
1014 /* If the MKEXT archive isn't fat, check that the CPU type & subtype
1015 * match that of the running kernel.
1017 if (OSSwapBigToHostInt32(mkext_data
->cputype
) != (UInt32
)CPU_TYPE_ANY
) {
1018 kern_return_t kresult
= KERN_FAILURE
;
1019 host_basic_info_data_t hostinfo
;
1020 host_info_t hostinfo_ptr
= (host_info_t
)&hostinfo
;
1021 mach_msg_type_number_t count
= sizeof(hostinfo
)/sizeof(integer_t
);
1023 kresult
= host_info((host_t
)1, HOST_BASIC_INFO
,
1024 hostinfo_ptr
, &count
);
1025 if (kresult
!= KERN_SUCCESS
) {
1026 IOLog("Error: Couldn't get current host info.\n");
1031 if ((UInt32
)hostinfo
.cpu_type
!=
1032 OSSwapBigToHostInt32(mkext_data
->cputype
)) {
1034 IOLog("Error: Extension archive doesn't contain software "
1035 "for this computer's CPU type.\n");
1040 if (!grade_binary(OSSwapBigToHostInt32(mkext_data
->cputype
),
1041 OSSwapBigToHostInt32(mkext_data
->cpusubtype
))) {
1042 IOLog("Error: Extension archive doesn't contain software "
1043 "for this computer's CPU subtype.\n");
1050 for (unsigned int i
= 0;
1051 i
< OSSwapBigToHostInt32(mkext_data
->numkexts
);
1055 kfree(loaded_kmod
, sizeof(kmod_info_t
));
1059 if (driverPlistDataObject
) {
1060 kmem_free(kernel_map
,
1061 (unsigned int)driverPlistDataObject
->getBytesNoCopy(),
1062 driverPlistDataObject
->getLength());
1064 driverPlistDataObject
->release();
1065 driverPlistDataObject
= NULL
;
1068 driverPlist
->release();
1072 driverCode
->release();
1076 driverDict
->release();
1080 errorString
->release();
1084 onekext_data
= &mkext_data
->kext
[i
];
1085 plist_file
= &onekext_data
->plist
;
1086 module_file
= &onekext_data
->module;
1088 if (!uncompressFile((u_int8_t
*)mkext_data
, plist_file
,
1089 &driverPlistDataObject
)) {
1091 IOLog("Error: couldn't uncompress plist file "
1092 "from multikext archive entry %d.\n", i
);
1097 if (!driverPlistDataObject
) {
1098 IOLog("Error: No property list present "
1099 "for multikext archive entry %d.\n", i
);
1103 driverPlist
= OSDynamicCast(OSDictionary
,
1105 (char *)driverPlistDataObject
->getBytesNoCopy(),
1108 IOLog("Error: Couldn't read XML property list "
1109 "for multikext archive entry %d.\n", i
);
1112 IOLog("XML parse error: %s.\n",
1113 errorString
->getCStringNoCopy());
1119 if (!validateExtensionDict(driverPlist
, i
)) {
1120 // validateExtensionsDict() logs an error
1126 /* Get the extension's module name. This is used to record
1129 moduleName
= OSDynamicCast(OSString
,
1130 driverPlist
->getObject("CFBundleIdentifier")); // do not release
1132 IOLog("Error: Multikext archive entry %d has "
1133 "no \"CFBundleIdentifier\" property.\n", i
);
1135 continue; // assume a kext config error & continue
1138 /* Check if kmod is already loaded and is a real loadable one (has
1141 loaded_kmod
= kmod_lookupbyname_locked(moduleName
->getCStringNoCopy());
1142 if (loaded_kmod
&& loaded_kmod
->address
) {
1143 IOLog("Skipping new extension \"%s\"; an extension named "
1144 "\"%s\" is already loaded.\n",
1145 moduleName
->getCStringNoCopy(),
1151 driverDict
= OSDictionary::withCapacity(2);
1153 IOLog("Error: Couldn't allocate dictionary "
1154 "for multikext archive entry %d.\n", i
);
1160 driverDict
->setObject("plist", driverPlist
);
1163 * Prepare an entry to hold the mkext entry info for the
1164 * compressed binary module, if there is one. If all four fields
1165 * of the module entry are zero, there isn't one.
1167 if (!(loaded_kmod
&& loaded_kmod
->address
) && (OSSwapBigToHostInt32(module_file
->offset
) ||
1168 OSSwapBigToHostInt32(module_file
->compsize
) ||
1169 OSSwapBigToHostInt32(module_file
->realsize
) ||
1170 OSSwapBigToHostInt32(module_file
->modifiedsecs
))) {
1172 moduleInfo
= OSData::withCapacity(sizeof(MkextEntryInfo
));
1174 IOLog("Error: Couldn't allocate data object "
1175 "for multikext archive entry %d.\n", i
);
1181 module_info
.base_address
= (vm_address_t
)mkext_data
;
1182 module_info
.fileinfo
= module_file
;
1184 if (!moduleInfo
->appendBytes(&module_info
, sizeof(module_info
))) {
1185 IOLog("Error: Couldn't record info "
1186 "for multikext archive entry %d.\n", i
);
1192 driverDict
->setObject("compressedCode", moduleInfo
);
1195 OSDictionary
* incumbentExt
= OSDynamicCast(OSDictionary
,
1196 extensions
->getObject(moduleName
));
1198 if (!incumbentExt
) {
1199 extensions
->setObject(moduleName
, driverDict
);
1201 OSDictionary
* mostRecentExtension
=
1202 compareExtensionVersions(incumbentExt
, driverDict
);
1204 if (mostRecentExtension
== incumbentExt
) {
1205 /* Do nothing, we've got the most recent. */
1206 } else if (mostRecentExtension
== driverDict
) {
1207 if (!extensions
->setObject(moduleName
, driverDict
)) {
1209 /* This is a fatal error, so bail.
1211 IOLog("extractExtensionsFromArchive(): Failed to add "
1213 moduleName
->getCStringNoCopy());
1218 } else /* should be NULL */ {
1220 /* This is a nonfatal error, so continue.
1222 IOLog("extractExtensionsFromArchive(): Error comparing "
1223 "versions of duplicate extensions %s.\n",
1224 moduleName
->getCStringNoCopy());
1233 if (loaded_kmod
) kfree(loaded_kmod
, sizeof(kmod_info_t
));
1234 if (driverPlistDataObject
) {
1235 kmem_free(kernel_map
,
1236 (unsigned int)driverPlistDataObject
->getBytesNoCopy(),
1237 driverPlistDataObject
->getLength());
1238 driverPlistDataObject
->release();
1240 if (driverPlist
) driverPlist
->release();
1241 if (driverCode
) driverCode
->release();
1242 if (moduleInfo
) moduleInfo
->release();
1243 if (driverDict
) driverDict
->release();
1244 if (errorString
) errorString
->release();
1249 /*********************************************************************
1251 *********************************************************************/
1252 bool readExtensions(OSDictionary
* propertyDict
,
1253 const char * memory_map_name
,
1254 OSDictionary
* extensions
) {
1257 OSData
* mkextDataObject
= 0; // don't release
1258 MemoryMapFileInfo
* mkext_file_info
= 0; // don't free
1260 mkextDataObject
= OSDynamicCast(OSData
,
1261 propertyDict
->getObject(memory_map_name
));
1262 // don't release mkextDataObject
1264 if (!mkextDataObject
) {
1265 IOLog("Error: No mkext data object "
1266 "for device tree entry \"%s\".\n",
1273 mkext_file_info
= (MemoryMapFileInfo
*)mkextDataObject
->getBytesNoCopy();
1274 if (!mkext_file_info
) {
1279 result
= extractExtensionsFromArchive(mkext_file_info
, extensions
);
1283 if (!result
&& extensions
) {
1284 extensions
->flushCollection();
1291 /*********************************************************************
1292 * Adds the personalities for an extensions dictionary to the global
1294 *********************************************************************/
1295 bool addPersonalities(OSDictionary
* extensions
) {
1297 OSCollectionIterator
* keyIterator
= NULL
; // must release
1298 OSString
* key
; // don't release
1299 OSDictionary
* driverDict
= NULL
; // don't release
1300 OSDictionary
* driverPlist
= NULL
; // don't release
1301 OSDictionary
* thisDriverPersonalities
= NULL
; // don't release
1302 OSArray
* allDriverPersonalities
= NULL
; // must release
1304 allDriverPersonalities
= OSArray::withCapacity(1);
1305 if (!allDriverPersonalities
) {
1306 IOLog("Error: Couldn't allocate personality dictionary.\n");
1312 /* Record all personalities found so that they can be
1313 * added to the catalogue.
1314 * Note: Not all extensions have personalities.
1317 keyIterator
= OSCollectionIterator::withCollection(extensions
);
1319 IOLog("Error: Couldn't allocate iterator to record personalities.\n");
1325 while ( ( key
= OSDynamicCast(OSString
,
1326 keyIterator
->getNextObject() ))) {
1328 driverDict
= OSDynamicCast(OSDictionary
,
1329 extensions
->getObject(key
));
1330 driverPlist
= OSDynamicCast(OSDictionary
,
1331 driverDict
->getObject("plist"));
1332 thisDriverPersonalities
= OSDynamicCast(OSDictionary
,
1333 driverPlist
->getObject("IOKitPersonalities"));
1335 if (thisDriverPersonalities
) {
1336 OSCollectionIterator
* pIterator
;
1337 OSString
* locakKey
;
1338 pIterator
= OSCollectionIterator::withCollection(
1339 thisDriverPersonalities
);
1341 IOLog("Error: Couldn't allocate iterator "
1342 "to record extension personalities.\n");
1346 while ( (locakKey
= OSDynamicCast(OSString
,
1347 pIterator
->getNextObject())) ) {
1349 OSDictionary
* personality
= OSDynamicCast(
1351 thisDriverPersonalities
->getObject(locakKey
));
1353 allDriverPersonalities
->setObject(personality
);
1356 pIterator
->release();
1358 } /* extract personalities */
1361 /* Add all personalities found to the IOCatalogue,
1362 * but don't start matching.
1364 gIOCatalogue
->addDrivers(allDriverPersonalities
, false);
1368 if (allDriverPersonalities
) allDriverPersonalities
->release();
1369 if (keyIterator
) keyIterator
->release();
1375 /*********************************************************************
1376 * Called from IOCatalogue to add extensions from an mkext archive.
1377 * This function makes a copy of the mkext object passed in because
1378 * the device tree support code dumps it after calling us (indirectly
1379 * through the IOCatalogue).
1380 *********************************************************************/
1381 bool addExtensionsFromArchive(OSData
* mkextDataObject
) {
1384 OSDictionary
* startupExtensions
= NULL
; // don't release
1385 OSArray
* bootLoaderObjects
= NULL
; // don't release
1386 OSDictionary
* extensions
= NULL
; // must release
1387 MemoryMapFileInfo mkext_file_info
;
1388 OSCollectionIterator
* keyIterator
= NULL
; // must release
1389 OSString
* key
= NULL
; // don't release
1391 startupExtensions
= getStartupExtensions();
1392 if (!startupExtensions
) {
1393 IOLog("Can't record extension archive; there is no"
1394 " extensions dictionary.\n");
1400 bootLoaderObjects
= getBootLoaderObjects();
1401 if (! bootLoaderObjects
) {
1402 IOLog("Error: Couldn't allocate array to hold temporary objects.\n");
1408 extensions
= OSDictionary::withCapacity(2);
1410 IOLog("Error: Couldn't allocate dictionary to unpack "
1411 "extension archive.\n");
1417 mkext_file_info
.paddr
= (UInt32
)mkextDataObject
->getBytesNoCopy();
1418 mkext_file_info
.length
= mkextDataObject
->getLength();
1420 /* Save the local mkext data object so that we can deallocate it later.
1422 bootLoaderObjects
->setObject(mkextDataObject
);
1424 result
= extractExtensionsFromArchive(&mkext_file_info
, extensions
);
1426 IOLog("Error: Failed to extract extensions from archive.\n");
1432 result
= mergeExtensionDictionaries(startupExtensions
, extensions
);
1434 IOLog("Error: Failed to merge new extensions into existing set.\n");
1439 result
= addPersonalities(extensions
);
1441 IOLog("Error: Failed to add personalities for extensions extracted "
1451 IOLog("Error: Failed to record extensions from archive.\n");
1454 keyIterator
= OSCollectionIterator::withCollection(
1458 while ( (key
= OSDynamicCast(OSString
,
1459 keyIterator
->getNextObject())) ) {
1461 IOLog("Added extension \"%s\" from archive.\n",
1462 key
->getCStringNoCopy());
1465 keyIterator
->release();
1469 if (extensions
) extensions
->release();
1475 /*********************************************************************
1476 * This function builds dictionaries for the startup extensions
1477 * put into memory by bootx, recording each in the startup extensions
1478 * dictionary. The dictionary format is this:
1481 * "plist" = (the extension's Info.plist as an OSDictionary)
1482 * "code" = (an OSData containing the executable file)
1485 * This function returns true if any extensions were found and
1486 * recorded successfully, or if there are no start extensions,
1487 * and false if an unrecoverable error occurred. An error reading
1488 * a single extension is not considered fatal, and this function
1489 * will simply skip the problematic extension to try the next one.
1490 *********************************************************************/
1492 bool recordStartupExtensions(void) {
1494 OSDictionary
* startupExtensions
= NULL
; // must release
1495 OSDictionary
* existingExtensions
= NULL
; // don't release
1496 OSDictionary
* mkextExtensions
= NULL
; // must release
1497 IORegistryEntry
* bootxMemoryMap
= NULL
; // must release
1498 OSDictionary
* propertyDict
= NULL
; // must release
1499 OSCollectionIterator
* keyIterator
= NULL
; // must release
1500 OSString
* key
= NULL
; // don't release
1502 OSDictionary
* newDriverDict
= NULL
; // must release
1503 OSDictionary
* driverPlist
= NULL
; // don't release
1505 struct section
* infosect
;
1506 struct section
* symsect
;
1507 unsigned int prelinkedCount
= 0;
1509 existingExtensions
= getStartupExtensions();
1510 if (!existingExtensions
) {
1511 IOLog("Error: There is no dictionary for startup extensions.\n");
1517 startupExtensions
= OSDictionary::withCapacity(1);
1518 if (!startupExtensions
) {
1519 IOLog("Error: Couldn't allocate dictionary "
1520 "to record startup extensions.\n");
1527 // add any prelinked modules as startup extensions
1529 infosect
= getsectbyname("__PRELINK", "__info");
1530 symsect
= getsectbyname("__PRELINK", "__symtab");
1531 if (infosect
&& infosect
->addr
&& infosect
->size
1532 && symsect
&& symsect
->addr
&& symsect
->size
) do
1534 gIOPrelinkedModules
= OSDynamicCast(OSArray
,
1535 OSUnserializeXML((const char *) infosect
->addr
, NULL
));
1537 if (!gIOPrelinkedModules
)
1539 for( unsigned int idx
= 0;
1540 (propertyDict
= OSDynamicCast(OSDictionary
, gIOPrelinkedModules
->getObject(idx
)));
1543 enum { kPrelinkReservedCount
= 4 };
1545 /* Get the extension's module name. This is used to record
1546 * the extension. Do *not* release the moduleName.
1548 OSString
* moduleName
= OSDynamicCast(OSString
,
1549 propertyDict
->getObject("CFBundleIdentifier"));
1551 IOLog("Error: Prelinked module entry has "
1552 "no \"CFBundleIdentifier\" property.\n");
1557 /* Add the kext, & its plist.
1559 newDriverDict
= OSDictionary::withCapacity(4);
1560 assert(newDriverDict
);
1561 newDriverDict
->setObject("plist", propertyDict
);
1562 startupExtensions
->setObject(moduleName
, newDriverDict
);
1563 newDriverDict
->release();
1565 /* Add the code if present.
1567 OSData
* data
= OSDynamicCast(OSData
, propertyDict
->getObject("OSBundlePrelink"));
1569 if (data
->getLength() < (kPrelinkReservedCount
* sizeof(UInt32
))) {
1570 IOLog("Error: Prelinked module entry has "
1571 "invalid \"OSBundlePrelink\" property.\n");
1576 prelink
= (UInt32
*) data
->getBytesNoCopy();
1577 kmod_info_t
* kmod_info
= (kmod_info_t
*) OSReadBigInt32(prelink
, 0);
1578 // end of "file" is end of symbol sect
1579 data
= OSData::withBytesNoCopy((void *) kmod_info
->address
,
1580 symsect
->addr
+ symsect
->size
- kmod_info
->address
);
1581 newDriverDict
->setObject("code", data
);
1586 /* Add the symbols if present.
1588 OSNumber
* num
= OSDynamicCast(OSNumber
, propertyDict
->getObject("OSBundlePrelinkSymbols"));
1590 UInt32 offset
= num
->unsigned32BitValue();
1591 data
= OSData::withBytesNoCopy((void *) (symsect
->addr
+ offset
), symsect
->size
- offset
);
1592 newDriverDict
->setObject("code", data
);
1598 if (gIOPrelinkedModules
)
1599 IOLog("%d prelinked modules\n", prelinkedCount
);
1603 virt
= ml_static_ptovirt(infosect
->addr
);
1605 ml_static_mfree(virt
, infosect
->size
);
1607 newDriverDict
= NULL
;
1613 IORegistryEntry::fromPath(
1614 "/chosen/memory-map", // path
1617 // return value is retained so be sure to release it
1619 if (!bootxMemoryMap
) {
1620 IOLog("Error: Couldn't read booter memory map.\n");
1626 propertyDict
= bootxMemoryMap
->dictionaryWithProperties();
1627 if (!propertyDict
) {
1628 IOLog("Error: Couldn't get property dictionary "
1629 "from memory map.\n");
1635 keyIterator
= OSCollectionIterator::withCollection(propertyDict
);
1637 IOLog("Error: Couldn't allocate iterator for driver images.\n");
1643 while ( (key
= OSDynamicCast(OSString
,
1644 keyIterator
->getNextObject())) ) {
1645 /* Clear newDriverDict & mkextExtensions upon entry to the loop,
1646 * handling both successful and unsuccessful iterations.
1648 if (newDriverDict
) {
1649 newDriverDict
->release();
1650 newDriverDict
= NULL
;
1652 if (mkextExtensions
) {
1653 mkextExtensions
->release();
1654 mkextExtensions
= NULL
;
1657 const char * keyValue
= key
->getCStringNoCopy();
1659 if ( !strncmp(keyValue
, BOOTX_KEXT_PREFIX
,
1660 strlen(BOOTX_KEXT_PREFIX
)) ) {
1662 /* Read the extension from the bootx-supplied memory.
1664 newDriverDict
= readExtension(propertyDict
, keyValue
);
1665 if (!newDriverDict
) {
1666 IOLog("Error: Couldn't read data "
1667 "for device tree entry \"%s\".\n", keyValue
);
1673 /* Preprare to record the extension by getting its info plist.
1675 driverPlist
= OSDynamicCast(OSDictionary
,
1676 newDriverDict
->getObject("plist"));
1678 IOLog("Error: Extension in device tree entry \"%s\" "
1679 "has no property list.\n", keyValue
);
1685 /* Get the extension's module name. This is used to record
1686 * the extension. Do *not* release the moduleName.
1688 OSString
* moduleName
= OSDynamicCast(OSString
,
1689 driverPlist
->getObject("CFBundleIdentifier"));
1691 IOLog("Error: Device tree entry \"%s\" has "
1692 "no \"CFBundleIdentifier\" property.\n", keyValue
);
1698 /* All has gone well so far, so record the extension under
1699 * its module name, checking for an existing duplicate.
1701 * Do not release moduleName, as it's part of the extension's
1704 OSDictionary
* incumbentExt
= OSDynamicCast(OSDictionary
,
1705 startupExtensions
->getObject(moduleName
));
1707 if (!incumbentExt
) {
1708 startupExtensions
->setObject(moduleName
, newDriverDict
);
1710 OSDictionary
* mostRecentExtension
=
1711 compareExtensionVersions(incumbentExt
, newDriverDict
);
1713 if (mostRecentExtension
== incumbentExt
) {
1714 /* Do nothing, we've got the most recent. */
1715 } else if (mostRecentExtension
== newDriverDict
) {
1716 if (!startupExtensions
->setObject(moduleName
,
1719 /* This is a fatal error, so bail.
1721 IOLog("recordStartupExtensions(): Failed to add "
1723 moduleName
->getCStringNoCopy());
1728 } else /* should be NULL */ {
1730 /* This is a nonfatal error, so continue.
1732 IOLog("recordStartupExtensions(): Error comparing "
1733 "versions of duplicate extensions %s.\n",
1734 moduleName
->getCStringNoCopy());
1741 } else if ( !strncmp(keyValue
, BOOTX_MULTIKEXT_PREFIX
,
1742 strlen(BOOTX_MULTIKEXT_PREFIX
)) ) {
1744 mkextExtensions
= OSDictionary::withCapacity(10);
1745 if (!mkextExtensions
) {
1746 IOLog("Error: Couldn't allocate dictionary to unpack "
1747 "multi-extension archive.\n");
1750 goto finish
; // allocation failure is fatal for this routine
1752 if (!readExtensions(propertyDict
, keyValue
, mkextExtensions
)) {
1753 IOLog("Error: Couldn't unpack multi-extension archive.\n");
1757 if (!mergeExtensionDictionaries(startupExtensions
,
1760 IOLog("Error: Failed to merge new extensions into "
1764 goto finish
; // merge error is fatal for this routine
1769 // Do not release key.
1771 } /* while ( (key = OSDynamicCast(OSString, ...) ) ) */
1773 if (!mergeExtensionDictionaries(existingExtensions
, startupExtensions
)) {
1774 IOLog("Error: Failed to merge new extensions into existing set.\n");
1780 result
= addPersonalities(startupExtensions
);
1782 IOLog("Error: Failed to add personalities for extensions extracted "
1791 // reused so clear first!
1793 keyIterator
->release();
1798 IOLog("Error: Failed to record startup extensions.\n");
1802 keyIterator
= OSCollectionIterator::withCollection(
1806 while ( (key
= OSDynamicCast(OSString
,
1807 keyIterator
->getNextObject())) ) {
1809 IOLog("Found extension \"%s\".\n",
1810 key
->getCStringNoCopy());
1813 keyIterator
->release();
1819 if (newDriverDict
) newDriverDict
->release();
1820 if (propertyDict
) propertyDict
->release();
1821 if (bootxMemoryMap
) bootxMemoryMap
->release();
1822 if (mkextExtensions
) mkextExtensions
->release();
1823 if (startupExtensions
) startupExtensions
->release();
1829 /*********************************************************************
1830 * This function removes an entry from the dictionary of startup
1831 * extensions. It's used when an extension can't be loaded, for
1832 * whatever reason. For drivers, this allows another matching driver
1833 * to be loaded, so that, for example, a driver for the root device
1835 *********************************************************************/
1836 void removeStartupExtension(const char * extensionName
) {
1837 OSDictionary
* startupExtensions
= NULL
; // don't release
1838 OSDictionary
* extensionDict
= NULL
; // don't release
1839 OSDictionary
* extensionPlist
= NULL
; // don't release
1840 OSDictionary
* extensionPersonalities
= NULL
; // don't release
1841 OSDictionary
* personality
= NULL
; // don't release
1842 OSCollectionIterator
* keyIterator
= NULL
; // must release
1843 OSString
* key
= NULL
; // don't release
1845 startupExtensions
= getStartupExtensions();
1846 if (!startupExtensions
) goto finish
;
1849 /* Find the extension's entry in the dictionary of
1850 * startup extensions.
1852 extensionDict
= OSDynamicCast(OSDictionary
,
1853 startupExtensions
->getObject(extensionName
));
1854 if (!extensionDict
) goto finish
;
1856 extensionPlist
= OSDynamicCast(OSDictionary
,
1857 extensionDict
->getObject("plist"));
1858 if (!extensionPlist
) goto finish
;
1860 extensionPersonalities
= OSDynamicCast(OSDictionary
,
1861 extensionPlist
->getObject("IOKitPersonalities"));
1862 if (!extensionPersonalities
) goto finish
;
1864 /* If it was there, remove it from the catalogue proper
1865 * by calling removeDrivers(). Pass true for the second
1866 * argument to trigger a new round of matching, and
1867 * then remove the extension from the dictionary of startup
1870 keyIterator
= OSCollectionIterator::withCollection(
1871 extensionPersonalities
);
1873 IOLog("Error: Couldn't allocate iterator to scan"
1874 " personalities for %s.\n", extensionName
);
1878 while ((key
= OSDynamicCast(OSString
, keyIterator
->getNextObject()))) {
1879 personality
= OSDynamicCast(OSDictionary
,
1880 extensionPersonalities
->getObject(key
));
1884 gIOCatalogue
->removeDrivers(personality
, true);
1888 startupExtensions
->removeObject(extensionName
);
1892 if (keyIterator
) keyIterator
->release();
1896 /*********************************************************************
1897 * FIXME: This function invalidates the globals gStartupExtensions and
1898 * FIXME: ...gBootLoaderObjects without setting them to NULL. Since
1899 * FIXME: ...the code itself is immediately unloaded, there may not be
1900 * FIXME: ...any reason to worry about that!
1901 *********************************************************************/
1902 void clearStartupExtensionsAndLoaderInfo(void)
1904 OSDictionary
* startupExtensions
= NULL
; // must release
1905 OSArray
* bootLoaderObjects
= NULL
; // must release
1907 IORegistryEntry
* bootxMemoryMap
= NULL
; // must release
1908 OSDictionary
* propertyDict
= NULL
; // must release
1909 OSCollectionIterator
* keyIterator
= NULL
; // must release
1910 OSString
* key
= NULL
; // don't release
1913 * Drop any temporarily held data objects.
1915 bootLoaderObjects
= getBootLoaderObjects();
1916 if (bootLoaderObjects
) {
1917 bootLoaderObjects
->release();
1921 * If any "code" entries in driver dictionaries are accompanied
1922 * by "compressedCode" entries, then those data objects were
1923 * created based of of kmem_alloc()'ed memory, which must be
1926 startupExtensions
= getStartupExtensions();
1927 if (startupExtensions
) {
1929 OSCollectionIterator::withCollection(startupExtensions
);
1931 IOLog("Error: Couldn't allocate iterator for startup "
1934 goto memory_map
; // bail to the memory_map label
1937 while ( (key
= OSDynamicCast(OSString
,
1938 keyIterator
->getNextObject())) ) {
1940 OSDictionary
* driverDict
= 0;
1941 OSData
* codeData
= 0;
1943 driverDict
= OSDynamicCast(OSDictionary
,
1944 startupExtensions
->getObject(key
));
1946 codeData
= OSDynamicCast(OSData
,
1947 driverDict
->getObject("code"));
1950 driverDict
->getObject("compressedCode")) {
1952 kmem_free(kernel_map
,
1953 (unsigned int)codeData
->getBytesNoCopy(),
1954 codeData
->getLength());
1959 keyIterator
->release();
1960 startupExtensions
->release();
1966 * Go through the device tree's memory map and remove any driver
1970 IORegistryEntry::fromPath(
1971 "/chosen/memory-map", // path
1974 // return value is retained so be sure to release it
1976 if (!bootxMemoryMap
) {
1977 IOLog("Error: Couldn't read booter memory map.\n");
1982 propertyDict
= bootxMemoryMap
->dictionaryWithProperties();
1983 if (!propertyDict
) {
1984 IOLog("Error: Couldn't get property dictionary "
1985 "from memory map.\n");
1990 keyIterator
= OSCollectionIterator::withCollection(propertyDict
);
1992 IOLog("Error: Couldn't allocate iterator for driver images.\n");
1997 while ( (key
= OSDynamicCast(OSString
,
1998 keyIterator
->getNextObject())) ) {
2000 const char * keyValue
= key
->getCStringNoCopy();
2002 if ( !strncmp(keyValue
, BOOTX_KEXT_PREFIX
,
2003 strlen(BOOTX_KEXT_PREFIX
)) ||
2004 !strncmp(keyValue
, BOOTX_MULTIKEXT_PREFIX
,
2005 strlen(BOOTX_MULTIKEXT_PREFIX
)) ) {
2007 OSData
* bootxDriverDataObject
= NULL
;
2008 MemoryMapFileInfo
* driverInfo
= 0;
2010 bootxDriverDataObject
= OSDynamicCast(OSData
,
2011 propertyDict
->getObject(keyValue
));
2012 // don't release bootxDriverDataObject
2014 if (!bootxDriverDataObject
) {
2017 driverInfo
= (MemoryMapFileInfo
*)
2018 bootxDriverDataObject
->getBytesNoCopy(0,
2019 sizeof(MemoryMapFileInfo
));
2020 IODTFreeLoaderInfo((char *)keyValue
,
2021 (void *)driverInfo
->paddr
,
2022 (int)driverInfo
->length
);
2027 if (bootxMemoryMap
) bootxMemoryMap
->release();
2028 if (propertyDict
) propertyDict
->release();
2029 if (keyIterator
) keyIterator
->release();