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>
38 #include <IOKit/IOLib.h>
40 #include <IOKit/assert.h>
43 extern void IODTFreeLoaderInfo( char *key
, void *infoAddr
, int infoSize
);
44 extern kern_return_t
host_info(host_t host
,
47 mach_msg_type_number_t
*count
);
48 extern int check_cpu_subtype(cpu_subtype_t cpu_subtype
);
54 #define VTYELLOW "\033[33m"
55 #define VTRESET "\033[0m"
58 /*********************************************************************
59 *********************************************************************/
60 static OSDictionary
* gStartupExtensions
= 0;
61 static OSArray
* gBootLoaderObjects
= 0;
63 OSDictionary
* getStartupExtensions(void) {
64 if (gStartupExtensions
) {
65 return gStartupExtensions
;
67 gStartupExtensions
= OSDictionary::withCapacity(1);
68 if (!gStartupExtensions
) {
69 IOLog("Error: Couldn't allocate "
70 "startup extensions dictionary.\n");
73 return gStartupExtensions
;
76 /* This array holds objects that are needed to be held around during
77 * boot before kextd starts up. Currently it contains OSData objects
78 * copied from OF entries for mkext archives in device ROMs. Because
79 * the Device Tree support code dumps these after initially handing
80 * them to us, we have to be able to clean them up later.
82 OSArray
* getBootLoaderObjects(void) {
83 if (gBootLoaderObjects
) {
84 return gBootLoaderObjects
;
86 gBootLoaderObjects
= OSArray::withCapacity(1);
87 if (! gBootLoaderObjects
) {
88 IOLog("Error: Couldn't allocate "
89 "bootstrap objects array.\n");
92 return gBootLoaderObjects
;
96 /*********************************************************************
97 * This function checks that a driver dict has all the required
98 * entries and does a little bit of value checking too.
99 *********************************************************************/
100 bool validateExtensionDict(OSDictionary
* extension
) {
103 OSString
* name
; // do not release
104 OSString
* stringValue
; // do not release
107 name
= OSDynamicCast(OSString
,
108 extension
->getObject("CFBundleIdentifier"));
110 IOLog(VTYELLOW
"Extension has no \"CFBundleIdentifier\" property.\n"
117 stringValue
= OSDynamicCast(OSString
,
118 extension
->getObject("CFBundleVersion"));
120 IOLog(VTYELLOW
"Extension \"%s\" has no \"CFBundleVersion\" "
121 "property.\n" VTRESET
,
122 name
->getCStringNoCopy());
127 if (!VERS_parse_string(stringValue
->getCStringNoCopy(),
129 IOLog(VTYELLOW
"Extension \"%s\" has an invalid "
130 "\"CFBundleVersion\" property.\n" VTRESET
,
131 name
->getCStringNoCopy());
139 // FIXME: Make return real result after kext conversion
146 /*********************************************************************
147 *********************************************************************/
148 OSDictionary
* compareExtensionVersions(
149 OSDictionary
* incumbent
,
150 OSDictionary
* candidate
) {
152 OSDictionary
* winner
= NULL
;
154 OSDictionary
* incumbentPlist
= NULL
;
155 OSDictionary
* candidatePlist
= NULL
;
156 OSString
* incumbentName
= NULL
;
157 OSString
* candidateName
= NULL
;
158 OSString
* incumbentVersionString
= NULL
;
159 OSString
* candidateVersionString
= NULL
;
160 UInt32 incumbent_vers
= 0;
161 UInt32 candidate_vers
= 0;
163 incumbentPlist
= OSDynamicCast(OSDictionary
,
164 incumbent
->getObject("plist"));
165 candidatePlist
= OSDynamicCast(OSDictionary
,
166 candidate
->getObject("plist"));
168 if (!incumbentPlist
|| !candidatePlist
) {
169 IOLog("compareExtensionVersions() called with invalid "
170 "extension dictionaries.\n");
176 incumbentName
= OSDynamicCast(OSString
,
177 incumbentPlist
->getObject("CFBundleIdentifier"));
178 candidateName
= OSDynamicCast(OSString
,
179 candidatePlist
->getObject("CFBundleIdentifier"));
180 incumbentVersionString
= OSDynamicCast(OSString
,
181 incumbentPlist
->getObject("CFBundleVersion"));
182 candidateVersionString
= OSDynamicCast(OSString
,
183 candidatePlist
->getObject("CFBundleVersion"));
185 if (!incumbentName
|| !candidateName
||
186 !incumbentVersionString
|| !candidateVersionString
) {
188 IOLog("compareExtensionVersions() called with invalid "
189 "extension dictionaries.\n");
195 if (strcmp(incumbentName
->getCStringNoCopy(),
196 candidateName
->getCStringNoCopy())) {
198 IOLog("compareExtensionVersions() called with different "
199 "extension names (%s and %s).\n",
200 incumbentName
->getCStringNoCopy(),
201 candidateName
->getCStringNoCopy());
207 if (!VERS_parse_string(incumbentVersionString
->getCStringNoCopy(),
210 IOLog(VTYELLOW
"Error parsing version string for extension %s (%s)\n"
212 incumbentName
->getCStringNoCopy(),
213 incumbentVersionString
->getCStringNoCopy());
219 if (!VERS_parse_string(candidateVersionString
->getCStringNoCopy(),
222 IOLog(VTYELLOW
"Error parsing version string for extension %s (%s)\n"
224 candidateName
->getCStringNoCopy(),
225 candidateVersionString
->getCStringNoCopy());
231 if (candidate_vers
> incumbent_vers
) {
232 IOLog(VTYELLOW
"Replacing extension \"%s\" with newer version "
233 "(%s -> %s).\n" VTRESET
,
234 incumbentName
->getCStringNoCopy(),
235 incumbentVersionString
->getCStringNoCopy(),
236 candidateVersionString
->getCStringNoCopy());
241 IOLog(VTYELLOW
"Skipping duplicate extension \"%s\" with older/same "
242 " version (%s -> %s).\n" VTRESET
,
243 candidateName
->getCStringNoCopy(),
244 candidateVersionString
->getCStringNoCopy(),
245 incumbentVersionString
->getCStringNoCopy());
253 // no cleanup, how nice
258 /*********************************************************************
259 * This function merges entries in the mergeFrom dictionary into the
260 * mergeInto dictionary. If it returns false, the two dictionaries are
261 * not altered. If it returns true, then mergeInto may have new
262 * entries; any keys that were already present in mergeInto are
263 * removed from mergeFrom, so that the caller can see what was
265 *********************************************************************/
266 bool mergeExtensionDictionaries(OSDictionary
* mergeInto
,
267 OSDictionary
* mergeFrom
) {
270 OSDictionary
* mergeIntoCopy
= NULL
; // must release
271 OSDictionary
* mergeFromCopy
= NULL
; // must release
272 OSCollectionIterator
* keyIterator
= NULL
; // must release
273 OSString
* key
; // don't release
275 /* Add 1 to count to guarantee copy can grow (grr).
277 mergeIntoCopy
= OSDictionary::withDictionary(mergeInto
,
278 mergeInto
->getCount() + 1);
279 if (!mergeIntoCopy
) {
280 IOLog("Error: Failed to copy 'into' extensions dictionary "
287 /* Add 1 to count to guarantee copy can grow (grr).
289 mergeFromCopy
= OSDictionary::withDictionary(mergeFrom
,
290 mergeFrom
->getCount() + 1);
291 if (!mergeFromCopy
) {
292 IOLog("Error: Failed to copy 'from' extensions dictionary "
299 keyIterator
= OSCollectionIterator::withCollection(mergeFrom
);
301 IOLog("Error: Failed to allocate iterator for extensions.\n");
309 * Loop through "from" dictionary, checking if the identifier already
310 * exists in the "into" dictionary and checking versions if it does.
312 while ((key
= OSDynamicCast(OSString
, keyIterator
->getNextObject()))) {
313 OSDictionary
* incumbentExt
= OSDynamicCast(OSDictionary
,
314 mergeIntoCopy
->getObject(key
));
315 OSDictionary
* candidateExt
= OSDynamicCast(OSDictionary
,
316 mergeFrom
->getObject(key
));
319 if (!mergeIntoCopy
->setObject(key
, candidateExt
)) {
321 /* This is a fatal error, so bail.
323 IOLog("mergeExtensionDictionaries(): Failed to add "
325 key
->getCStringNoCopy());
331 OSDictionary
* mostRecentExtension
=
332 compareExtensionVersions(incumbentExt
, candidateExt
);
334 if (mostRecentExtension
== incumbentExt
) {
335 mergeFromCopy
->removeObject(key
);
336 } else if (mostRecentExtension
== candidateExt
) {
338 if (!mergeIntoCopy
->setObject(key
, candidateExt
)) {
340 /* This is a fatal error, so bail.
342 IOLog("mergeExtensionDictionaries(): Failed to add "
344 key
->getCStringNoCopy());
349 } else /* should be NULL */ {
351 /* This is a nonfatal error, so continue doing others.
353 IOLog("mergeExtensionDictionaries(): Error comparing "
354 "versions of duplicate extensions %s.\n",
355 key
->getCStringNoCopy());
364 /* If successful, replace the contents of the original
365 * dictionaries with those of the modified copies.
368 mergeInto
->flushCollection();
369 mergeInto
->merge(mergeIntoCopy
);
370 mergeFrom
->flushCollection();
371 mergeFrom
->merge(mergeFromCopy
);
374 if (mergeIntoCopy
) mergeIntoCopy
->release();
375 if (mergeFromCopy
) mergeFromCopy
->release();
376 if (keyIterator
) keyIterator
->release();
383 * These bits are used to parse data made available by bootx.
385 #define BOOTX_KEXT_PREFIX "Driver-"
386 #define BOOTX_MULTIKEXT_PREFIX "DriversPackage-"
388 typedef struct MemoryMapFileInfo
{
393 typedef struct BootxDriverInfo
{
400 typedef struct MkextEntryInfo
{
401 vm_address_t base_address
;
402 mkext_file
* fileinfo
;
406 /*********************************************************************
407 * This private function reads the data for a single extension from
408 * the bootx memory-map's propery dict, returning a dictionary with
409 * keys "plist" for the extension's Info.plist as a parsed OSDictionary
410 * and "code" for the extension's executable code as an OSData.
411 *********************************************************************/
412 OSDictionary
* readExtension(OSDictionary
* propertyDict
,
413 const char * memory_map_name
) {
416 OSData
* bootxDriverDataObject
= NULL
;
417 OSDictionary
* driverPlist
= NULL
;
418 OSString
* driverName
= NULL
;
419 OSData
* driverCode
= NULL
;
420 OSString
* errorString
= NULL
;
421 OSDictionary
* driverDict
= NULL
;
423 MemoryMapFileInfo
* driverInfo
= 0;
424 BootxDriverInfo
* dataBuffer
;
426 kmod_info_t
* loaded_kmod
= NULL
;
429 bootxDriverDataObject
= OSDynamicCast(OSData
,
430 propertyDict
->getObject(memory_map_name
));
431 // don't release bootxDriverDataObject
433 if (!bootxDriverDataObject
) {
434 IOLog("Error: No driver data object "
435 "for device tree entry \"%s\".\n",
442 driverDict
= OSDictionary::withCapacity(2);
444 IOLog("Error: Couldn't allocate dictionary "
445 "for device tree entry \"%s\".\n", memory_map_name
);
451 driverInfo
= (MemoryMapFileInfo
*)
452 bootxDriverDataObject
->getBytesNoCopy(0,
453 sizeof(MemoryMapFileInfo
));
454 dataBuffer
= (BootxDriverInfo
*)ml_static_ptovirt(
457 IOLog("Error: No data buffer "
458 "for device tree entry \"%s\".\n", memory_map_name
);
464 driverPlist
= OSDynamicCast(OSDictionary
,
465 OSUnserializeXML(dataBuffer
->plistAddr
, &errorString
));
467 IOLog("Error: Couldn't read XML property list "
468 "for device tree entry \"%s\".\n", memory_map_name
);
471 IOLog("XML parse error: %s.\n",
472 errorString
->getCStringNoCopy());
480 driverName
= OSDynamicCast(OSString
,
481 driverPlist
->getObject("CFBundleIdentifier")); // do not release
483 IOLog("Error: Device tree entry \"%s\" has "
484 "no \"CFBundleIdentifier\" property.\n", memory_map_name
);
490 /* Check if kmod is already loaded and is a real loadable one (has
493 loaded_kmod
= kmod_lookupbyname(driverName
->getCStringNoCopy());
494 if (loaded_kmod
&& loaded_kmod
->address
) {
495 IOLog("Skipping new extension \"%s\"; an extension named "
496 "\"%s\" is already loaded.\n",
497 driverName
->getCStringNoCopy(),
504 if (!validateExtensionDict(driverPlist
)) {
505 IOLog("Error: Failed to validate property list "
506 "for device tree entry \"%s\".\n", memory_map_name
);
512 driverDict
->setObject("plist", driverPlist
);
514 /* It's perfectly okay for a KEXT to have no executable.
515 * Check that moduleAddr is nonzero before attempting to
518 * NOTE: The driverCode object is created "no-copy", so
519 * it doesn't own that memory. The memory must be freed
520 * separately from the OSData object (see
521 * clearStartupExtensionsAndLoaderInfo() at the end of this file).
523 if (dataBuffer
->moduleAddr
&& dataBuffer
->moduleLength
) {
524 driverCode
= OSData::withBytesNoCopy(dataBuffer
->moduleAddr
,
525 dataBuffer
->moduleLength
);
527 IOLog("Error: Couldn't allocate data object "
528 "to hold code for device tree entry \"%s\".\n",
536 driverDict
->setObject("code", driverCode
);
542 // do not release bootxDriverDataObject
543 // do not release driverName
546 driverPlist
->release();
549 errorString
->release();
552 driverCode
->release();
556 driverDict
->release();
564 /*********************************************************************
565 * Used to uncompress a single file entry in an mkext archive.
567 * The OSData returned does not own its memory! You must deallocate
568 * that memory using kmem_free() before releasing the OSData().
569 *********************************************************************/
570 static bool uncompressFile(u_int8_t
*base_address
, mkext_file
* fileinfo
,
571 /* out */ OSData
** file
) {
574 kern_return_t kern_result
;
575 u_int8_t
* uncompressed_file
= 0; // kmem_free() on error
576 OSData
* uncompressedFile
= 0; // returned
577 size_t uncompressed_size
= 0;
579 size_t offset
= OSSwapBigToHostInt32(fileinfo
->offset
);
580 size_t compsize
= OSSwapBigToHostInt32(fileinfo
->compsize
);
581 size_t realsize
= OSSwapBigToHostInt32(fileinfo
->realsize
);
582 time_t modifiedsecs
= OSSwapBigToHostInt32(fileinfo
->modifiedsecs
);
586 /* If these four fields are zero there's no file, but that isn't
589 if (offset
== 0 && compsize
== 0 &&
590 realsize
== 0 && modifiedsecs
== 0) {
594 // Add 1 for '\0' to terminate XML string!
595 kern_result
= kmem_alloc(kernel_map
, (vm_offset_t
*)&uncompressed_file
,
597 if (kern_result
!= KERN_SUCCESS
) {
598 IOLog("Error: Couldn't allocate data buffer "
599 "to uncompress file.\n");
605 uncompressedFile
= OSData::withBytesNoCopy(uncompressed_file
,
607 if (!uncompressedFile
) {
608 IOLog("Error: Couldn't allocate data object "
609 "to uncompress file.\n");
616 uncompressed_size
= decompress_lzss(uncompressed_file
,
617 base_address
+ offset
,
619 if (uncompressed_size
!= realsize
) {
620 IOLog("Error: Uncompressed file is not the length "
627 bcopy(base_address
+ offset
, uncompressed_file
,
630 uncompressed_file
[uncompressed_size
] = '\0';
632 *file
= uncompressedFile
;
636 if (uncompressed_file
) {
637 kmem_free(kernel_map
, (vm_address_t
)uncompressed_file
,
640 if (uncompressedFile
) {
641 uncompressedFile
->release();
648 bool uncompressModule(OSData
*compData
, /* out */ OSData
** file
) {
650 MkextEntryInfo
*info
= (MkextEntryInfo
*) compData
->getBytesNoCopy();
652 return uncompressFile((u_int8_t
*) info
->base_address
,
653 info
->fileinfo
, file
);
657 /*********************************************************************
658 * Does the work of pulling extensions out of an mkext archive located
660 *********************************************************************/
661 bool extractExtensionsFromArchive(MemoryMapFileInfo
* mkext_file_info
,
662 OSDictionary
* extensions
) {
666 u_int8_t
* crc_address
= 0;
668 mkext_header
* mkext_data
= 0; // don't free
669 mkext_kext
* onekext_data
= 0; // don't free
670 mkext_file
* plist_file
= 0; // don't free
671 mkext_file
* module_file
= 0; // don't free
672 OSData
* driverPlistDataObject
= 0; // must release
673 OSDictionary
* driverPlist
= 0; // must release
674 OSData
* driverCode
= 0; // must release
675 OSDictionary
* driverDict
= 0; // must release
676 OSString
* moduleName
= 0; // don't release
677 OSString
* errorString
= NULL
; // must release
679 OSData
* moduleInfo
= 0; // must release
680 MkextEntryInfo module_info
;
682 mkext_data
= (mkext_header
*)mkext_file_info
->paddr
;
684 if (OSSwapBigToHostInt32(mkext_data
->magic
) != MKEXT_MAGIC
||
685 OSSwapBigToHostInt32(mkext_data
->signature
) != MKEXT_SIGN
) {
686 IOLog("Error: Extension archive has invalid magic or signature.\n");
692 if (OSSwapBigToHostInt32(mkext_data
->length
) != mkext_file_info
->length
) {
693 IOLog("Error: Mismatch between extension archive & "
694 "recorded length.\n");
700 crc_address
= (u_int8_t
*)&mkext_data
->version
;
701 checksum
= adler32(crc_address
,
702 (unsigned int)mkext_data
+
703 OSSwapBigToHostInt32(mkext_data
->length
) - (unsigned int)crc_address
);
705 if (OSSwapBigToHostInt32(mkext_data
->adler32
) != checksum
) {
706 IOLog("Error: Extension archive has a bad checksum.\n");
712 /* If the MKEXT archive isn't fat, check that the CPU type & subtype
713 * match that of the running kernel.
715 if (OSSwapBigToHostInt32(mkext_data
->cputype
) != (UInt32
)CPU_TYPE_ANY
) {
716 kern_return_t kresult
= KERN_FAILURE
;
717 host_basic_info_data_t hostinfo
;
718 host_info_t hostinfo_ptr
= (host_info_t
)&hostinfo
;
719 mach_msg_type_number_t count
= sizeof(hostinfo
)/sizeof(integer_t
);
721 kresult
= host_info((host_t
)1, HOST_BASIC_INFO
,
722 hostinfo_ptr
, &count
);
723 if (kresult
!= KERN_SUCCESS
) {
724 IOLog("Error: Couldn't get current host info.\n");
729 if ((UInt32
)hostinfo
.cpu_type
!=
730 OSSwapBigToHostInt32(mkext_data
->cputype
)) {
732 IOLog("Error: Extension archive doesn't contain software "
733 "for this computer's CPU type.\n");
738 if (!check_cpu_subtype(OSSwapBigToHostInt32(mkext_data
->cpusubtype
))) {
739 IOLog("Error: Extension archive doesn't contain software "
740 "for this computer's CPU subtype.\n");
747 for (unsigned int i
= 0;
748 i
< OSSwapBigToHostInt32(mkext_data
->numkexts
);
751 kmod_info_t
* loaded_kmod
= 0;
753 if (driverPlistDataObject
) {
754 driverPlistDataObject
->release();
755 driverPlistDataObject
= NULL
;
758 driverPlist
->release();
762 driverCode
->release();
766 driverDict
->release();
770 errorString
->release();
774 onekext_data
= &mkext_data
->kext
[i
];
775 plist_file
= &onekext_data
->plist
;
776 module_file
= &onekext_data
->module;
778 if (!uncompressFile((u_int8_t
*)mkext_data
, plist_file
,
779 &driverPlistDataObject
)) {
781 IOLog("Error: couldn't uncompress plist file "
782 "from multikext archive entry %d.\n", i
);
787 if (!driverPlistDataObject
) {
788 IOLog("Error: No property list present "
789 "for multikext archive entry %d.\n", i
);
793 driverPlist
= OSDynamicCast(OSDictionary
,
795 (char *)driverPlistDataObject
->getBytesNoCopy(),
798 IOLog("Error: Couldn't read XML property list "
799 "for multikext archive entry %d.\n", i
);
802 IOLog("XML parse error: %s.\n",
803 errorString
->getCStringNoCopy());
809 if (!validateExtensionDict(driverPlist
)) {
810 IOLog("Error: Failed to validate property list "
811 "for multikext archive entry %d.\n", i
);
818 /* Get the extension's module name. This is used to record
821 moduleName
= OSDynamicCast(OSString
,
822 driverPlist
->getObject("CFBundleIdentifier")); // do not release
824 IOLog("Error: Multikext archive entry %d has "
825 "no \"CFBundleIdentifier\" property.\n", i
);
827 continue; // assume a kext config error & continue
830 /* Check if kmod is already loaded and is a real loadable one (has
833 loaded_kmod
= kmod_lookupbyname(moduleName
->getCStringNoCopy());
834 if (loaded_kmod
&& loaded_kmod
->address
) {
835 IOLog("Skipping new extension \"%s\"; an extension named "
836 "\"%s\" is already loaded.\n",
837 moduleName
->getCStringNoCopy(),
843 driverDict
= OSDictionary::withCapacity(2);
845 IOLog("Error: Couldn't allocate dictionary "
846 "for multikext archive entry %d.\n", i
);
852 driverDict
->setObject("plist", driverPlist
);
855 * Prepare an entry to hold the mkext entry info for the
856 * compressed binary module, if there is one. If all four fields
857 * of the module entry are zero, there isn't one.
859 if (OSSwapBigToHostInt32(module_file
->offset
) ||
860 OSSwapBigToHostInt32(module_file
->compsize
) ||
861 OSSwapBigToHostInt32(module_file
->realsize
) ||
862 OSSwapBigToHostInt32(module_file
->modifiedsecs
)) {
864 moduleInfo
= OSData::withCapacity(sizeof(MkextEntryInfo
));
866 IOLog("Error: Couldn't allocate data object "
867 "for multikext archive entry %d.\n", i
);
873 module_info
.base_address
= (vm_address_t
)mkext_data
;
874 module_info
.fileinfo
= module_file
;
876 if (!moduleInfo
->appendBytes(&module_info
, sizeof(module_info
))) {
877 IOLog("Error: Couldn't record info "
878 "for multikext archive entry %d.\n", i
);
884 driverDict
->setObject("compressedCode", moduleInfo
);
887 OSDictionary
* incumbentExt
= OSDynamicCast(OSDictionary
,
888 extensions
->getObject(moduleName
));
891 extensions
->setObject(moduleName
, driverDict
);
893 OSDictionary
* mostRecentExtension
=
894 compareExtensionVersions(incumbentExt
, driverDict
);
896 if (mostRecentExtension
== incumbentExt
) {
897 /* Do nothing, we've got the most recent. */
898 } else if (mostRecentExtension
== driverDict
) {
899 if (!extensions
->setObject(moduleName
, driverDict
)) {
901 /* This is a fatal error, so bail.
903 IOLog("extractExtensionsFromArchive(): Failed to add "
905 moduleName
->getCStringNoCopy());
910 } else /* should be NULL */ {
912 /* This is a nonfatal error, so continue.
914 IOLog("extractExtensionsFromArchive(): Error comparing "
915 "versions of duplicate extensions %s.\n",
916 moduleName
->getCStringNoCopy());
925 if (driverPlistDataObject
) driverPlistDataObject
->release();
926 if (driverPlist
) driverPlist
->release();
927 if (driverCode
) driverCode
->release();
928 if (moduleInfo
) moduleInfo
->release();
929 if (driverDict
) driverDict
->release();
930 if (errorString
) errorString
->release();
935 /*********************************************************************
937 *********************************************************************/
938 bool readExtensions(OSDictionary
* propertyDict
,
939 const char * memory_map_name
,
940 OSDictionary
* extensions
) {
943 OSData
* mkextDataObject
= 0; // don't release
944 MemoryMapFileInfo
* mkext_file_info
= 0; // don't free
946 mkextDataObject
= OSDynamicCast(OSData
,
947 propertyDict
->getObject(memory_map_name
));
948 // don't release mkextDataObject
950 if (!mkextDataObject
) {
951 IOLog("Error: No mkext data object "
952 "for device tree entry \"%s\".\n",
959 mkext_file_info
= (MemoryMapFileInfo
*)mkextDataObject
->getBytesNoCopy();
960 if (!mkext_file_info
) {
965 result
= extractExtensionsFromArchive(mkext_file_info
, extensions
);
969 if (!result
&& extensions
) {
970 extensions
->flushCollection();
977 /*********************************************************************
978 * Adds the personalities for an extensions dictionary to the global
980 *********************************************************************/
981 bool addPersonalities(OSDictionary
* extensions
) {
983 OSCollectionIterator
* keyIterator
= NULL
; // must release
984 OSString
* key
; // don't release
985 OSDictionary
* driverDict
= NULL
; // don't release
986 OSDictionary
* driverPlist
= NULL
; // don't release
987 OSDictionary
* thisDriverPersonalities
= NULL
; // don't release
988 OSArray
* allDriverPersonalities
= NULL
; // must release
990 allDriverPersonalities
= OSArray::withCapacity(1);
991 if (!allDriverPersonalities
) {
992 IOLog("Error: Couldn't allocate personality dictionary.\n");
998 /* Record all personalities found so that they can be
999 * added to the catalogue.
1000 * Note: Not all extensions have personalities.
1003 keyIterator
= OSCollectionIterator::withCollection(extensions
);
1005 IOLog("Error: Couldn't allocate iterator to record personalities.\n");
1011 while ( ( key
= OSDynamicCast(OSString
,
1012 keyIterator
->getNextObject() ))) {
1014 driverDict
= OSDynamicCast(OSDictionary
,
1015 extensions
->getObject(key
));
1016 driverPlist
= OSDynamicCast(OSDictionary
,
1017 driverDict
->getObject("plist"));
1018 thisDriverPersonalities
= OSDynamicCast(OSDictionary
,
1019 driverPlist
->getObject("IOKitPersonalities"));
1021 if (thisDriverPersonalities
) {
1022 OSCollectionIterator
* pIterator
;
1024 pIterator
= OSCollectionIterator::withCollection(
1025 thisDriverPersonalities
);
1027 IOLog("Error: Couldn't allocate iterator "
1028 "to record extension personalities.\n");
1032 while ( (key
= OSDynamicCast(OSString
,
1033 pIterator
->getNextObject())) ) {
1035 OSDictionary
* personality
= OSDynamicCast(
1037 thisDriverPersonalities
->getObject(key
));
1039 allDriverPersonalities
->setObject(personality
);
1042 pIterator
->release();
1044 } /* extract personalities */
1047 /* Add all personalities found to the IOCatalogue,
1048 * but don't start matching.
1050 gIOCatalogue
->addDrivers(allDriverPersonalities
, false);
1054 if (allDriverPersonalities
) allDriverPersonalities
->release();
1055 if (keyIterator
) keyIterator
->release();
1061 /*********************************************************************
1062 * Called from IOCatalogue to add extensions from an mkext archive.
1063 * This function makes a copy of the mkext object passed in because
1064 * the device tree support code dumps it after calling us (indirectly
1065 * through the IOCatalogue).
1066 *********************************************************************/
1067 bool addExtensionsFromArchive(OSData
* mkextDataObject
) {
1070 OSDictionary
* startupExtensions
= NULL
; // don't release
1071 OSArray
* bootLoaderObjects
= NULL
; // don't release
1072 OSData
* localMkextDataObject
= NULL
; // don't release
1073 OSDictionary
* extensions
= NULL
; // must release
1074 MemoryMapFileInfo mkext_file_info
;
1075 OSCollectionIterator
* keyIterator
= NULL
; // must release
1076 OSString
* key
= NULL
; // don't release
1078 startupExtensions
= getStartupExtensions();
1079 if (!startupExtensions
) {
1080 IOLog("Can't record extension archive; there is no
1081 extensions dictionary.\n");
1087 bootLoaderObjects
= getBootLoaderObjects();
1088 if (! bootLoaderObjects
) {
1089 IOLog("Error: Couldn't allocate array to hold temporary objects.\n");
1095 extensions
= OSDictionary::withCapacity(2);
1097 IOLog("Error: Couldn't allocate dictionary to unpack "
1098 "extension archive.\n");
1104 /* The mkext we've been handed (or the data it references) can go away,
1105 * so we need to make a local copy to keep around as long as it might
1108 localMkextDataObject
= OSData::withData(mkextDataObject
);
1109 if (!localMkextDataObject
) {
1110 IOLog("Error: Couldn't copy extension archive.\n");
1116 mkext_file_info
.paddr
= (UInt32
)localMkextDataObject
->getBytesNoCopy();
1117 mkext_file_info
.length
= localMkextDataObject
->getLength();
1119 /* Save the local mkext data object so that we can deallocate it later.
1121 bootLoaderObjects
->setObject(localMkextDataObject
);
1122 localMkextDataObject
->release();
1124 result
= extractExtensionsFromArchive(&mkext_file_info
, extensions
);
1126 IOLog("Error: Failed to extract extensions from archive.\n");
1132 result
= mergeExtensionDictionaries(startupExtensions
, extensions
);
1134 IOLog("Error: Failed to merge new extensions into existing set.\n");
1139 result
= addPersonalities(extensions
);
1141 IOLog("Error: Failed to add personalities for extensions extracted "
1151 IOLog("Error: Failed to record extensions from archive.\n");
1154 keyIterator
= OSCollectionIterator::withCollection(
1158 while ( (key
= OSDynamicCast(OSString
,
1159 keyIterator
->getNextObject())) ) {
1161 IOLog("Added extension \"%s\" from archive.\n",
1162 key
->getCStringNoCopy());
1165 keyIterator
->release();
1169 if (extensions
) extensions
->release();
1175 /*********************************************************************
1176 * This function builds dictionaries for the startup extensions
1177 * put into memory by bootx, recording each in the startup extensions
1178 * dictionary. The dictionary format is this:
1181 * "plist" = (the extension's Info.plist as an OSDictionary)
1182 * "code" = (an OSData containing the executable file)
1185 * This function returns true if any extensions were found and
1186 * recorded successfully, or if there are no start extensions,
1187 * and false if an unrecoverable error occurred. An error reading
1188 * a single extension is not considered fatal, and this function
1189 * will simply skip the problematic extension to try the next one.
1190 *********************************************************************/
1191 bool recordStartupExtensions(void) {
1193 OSDictionary
* startupExtensions
= NULL
; // must release
1194 OSDictionary
* existingExtensions
= NULL
; // don't release
1195 OSDictionary
* mkextExtensions
= NULL
; // must release
1196 IORegistryEntry
* bootxMemoryMap
= NULL
; // must release
1197 OSDictionary
* propertyDict
= NULL
; // must release
1198 OSCollectionIterator
* keyIterator
= NULL
; // must release
1199 OSString
* key
= NULL
; // don't release
1201 OSDictionary
* newDriverDict
= NULL
; // must release
1202 OSDictionary
* driverPlist
= NULL
; // don't release
1204 IOLog("Recording startup extensions.\n");
1207 existingExtensions
= getStartupExtensions();
1208 if (!existingExtensions
) {
1209 IOLog("Error: There is no dictionary for startup extensions.\n");
1215 startupExtensions
= OSDictionary::withCapacity(1);
1216 if (!startupExtensions
) {
1217 IOLog("Error: Couldn't allocate dictionary "
1218 "to record startup extensions.\n");
1225 IORegistryEntry::fromPath(
1226 "/chosen/memory-map", // path
1229 // return value is retained so be sure to release it
1231 if (!bootxMemoryMap
) {
1232 IOLog("Error: Couldn't read booter memory map.\n");
1238 propertyDict
= bootxMemoryMap
->dictionaryWithProperties();
1239 if (!propertyDict
) {
1240 IOLog("Error: Couldn't get property dictionary "
1241 "from memory map.\n");
1247 keyIterator
= OSCollectionIterator::withCollection(propertyDict
);
1249 IOLog("Error: Couldn't allocate iterator for driver images.\n");
1255 while ( (key
= OSDynamicCast(OSString
,
1256 keyIterator
->getNextObject())) ) {
1258 /* Clear newDriverDict & mkextExtensions upon entry to the loop,
1259 * handling both successful and unsuccessful iterations.
1261 if (newDriverDict
) {
1262 newDriverDict
->release();
1263 newDriverDict
= NULL
;
1265 if (mkextExtensions
) {
1266 mkextExtensions
->release();
1267 mkextExtensions
= NULL
;
1270 const char * keyValue
= key
->getCStringNoCopy();
1272 if ( !strncmp(keyValue
, BOOTX_KEXT_PREFIX
,
1273 strlen(BOOTX_KEXT_PREFIX
)) ) {
1275 /* Read the extension from the bootx-supplied memory.
1277 newDriverDict
= readExtension(propertyDict
, keyValue
);
1278 if (!newDriverDict
) {
1279 IOLog("Error: Couldn't read data "
1280 "for device tree entry \"%s\".\n", keyValue
);
1286 /* Preprare to record the extension by getting its info plist.
1288 driverPlist
= OSDynamicCast(OSDictionary
,
1289 newDriverDict
->getObject("plist"));
1291 IOLog("Error: Extension in device tree entry \"%s\" "
1292 "has no property list.\n", keyValue
);
1298 /* Get the extension's module name. This is used to record
1299 * the extension. Do *not* release the moduleName.
1301 OSString
* moduleName
= OSDynamicCast(OSString
,
1302 driverPlist
->getObject("CFBundleIdentifier"));
1304 IOLog("Error: Device tree entry \"%s\" has "
1305 "no \"CFBundleIdentifier\" property.\n", keyValue
);
1311 /* All has gone well so far, so record the extension under
1312 * its module name, checking for an existing duplicate.
1314 * Do not release moduleName, as it's part of the extension's
1317 OSDictionary
* incumbentExt
= OSDynamicCast(OSDictionary
,
1318 startupExtensions
->getObject(moduleName
));
1320 if (!incumbentExt
) {
1321 startupExtensions
->setObject(moduleName
, newDriverDict
);
1323 OSDictionary
* mostRecentExtension
=
1324 compareExtensionVersions(incumbentExt
, newDriverDict
);
1326 if (mostRecentExtension
== incumbentExt
) {
1327 /* Do nothing, we've got the most recent. */
1328 } else if (mostRecentExtension
== newDriverDict
) {
1329 if (!startupExtensions
->setObject(moduleName
,
1332 /* This is a fatal error, so bail.
1334 IOLog("recordStartupExtensions(): Failed to add "
1336 moduleName
->getCStringNoCopy());
1341 } else /* should be NULL */ {
1343 /* This is a nonfatal error, so continue.
1345 IOLog("recordStartupExtensions(): Error comparing "
1346 "versions of duplicate extensions %s.\n",
1347 moduleName
->getCStringNoCopy());
1354 } else if ( !strncmp(keyValue
, BOOTX_MULTIKEXT_PREFIX
,
1355 strlen(BOOTX_MULTIKEXT_PREFIX
)) ) {
1357 mkextExtensions
= OSDictionary::withCapacity(10);
1358 if (!mkextExtensions
) {
1359 IOLog("Error: Couldn't allocate dictionary to unpack "
1360 "multi-extension archive.\n");
1363 goto finish
; // allocation failure is fatal for this routine
1365 if (!readExtensions(propertyDict
, keyValue
, mkextExtensions
)) {
1366 IOLog("Error: Couldn't unpack multi-extension archive.\n");
1370 if (!mergeExtensionDictionaries(startupExtensions
,
1373 IOLog("Error: Failed to merge new extensions into "
1377 goto finish
; // merge error is fatal for this routine
1382 // Do not release key.
1384 } /* while ( (key = OSDynamicCast(OSString, ... */
1386 if (!mergeExtensionDictionaries(existingExtensions
, startupExtensions
)) {
1387 IOLog("Error: Failed to merge new extensions into existing set.\n");
1393 result
= addPersonalities(startupExtensions
);
1395 IOLog("Error: Failed to add personalities for extensions extracted "
1404 // reused so clear first!
1406 keyIterator
->release();
1411 IOLog("Error: Failed to record startup extensions.\n");
1415 keyIterator
= OSCollectionIterator::withCollection(
1419 while ( (key
= OSDynamicCast(OSString
,
1420 keyIterator
->getNextObject())) ) {
1422 IOLog("Found extension \"%s\".\n",
1423 key
->getCStringNoCopy());
1426 keyIterator
->release();
1432 if (newDriverDict
) newDriverDict
->release();
1433 if (propertyDict
) propertyDict
->release();
1434 if (bootxMemoryMap
) bootxMemoryMap
->release();
1435 if (mkextExtensions
) mkextExtensions
->release();
1436 if (startupExtensions
) startupExtensions
->release();
1442 /*********************************************************************
1443 * This function removes an entry from the dictionary of startup
1444 * extensions. It's used when an extension can't be loaded, for
1445 * whatever reason. For drivers, this allows another matching driver
1446 * to be loaded, so that, for example, a driver for the root device
1448 *********************************************************************/
1449 void removeStartupExtension(const char * extensionName
) {
1450 OSDictionary
* startupExtensions
= NULL
; // don't release
1451 OSDictionary
* extensionDict
= NULL
; // don't release
1452 OSDictionary
* extensionPlist
= NULL
; // don't release
1453 OSDictionary
* extensionPersonalities
= NULL
; // don't release
1454 OSDictionary
* personality
= NULL
; // don't release
1455 OSCollectionIterator
* keyIterator
= NULL
; // must release
1456 OSString
* key
= NULL
; // don't release
1458 startupExtensions
= getStartupExtensions();
1459 if (!startupExtensions
) goto finish
;
1462 /* Find the extension's entry in the dictionary of
1463 * startup extensions.
1465 extensionDict
= OSDynamicCast(OSDictionary
,
1466 startupExtensions
->getObject(extensionName
));
1467 if (!extensionDict
) goto finish
;
1469 extensionPlist
= OSDynamicCast(OSDictionary
,
1470 extensionDict
->getObject("plist"));
1471 if (!extensionPlist
) goto finish
;
1473 extensionPersonalities
= OSDynamicCast(OSDictionary
,
1474 extensionPlist
->getObject("IOKitPersonalities"));
1475 if (!extensionPersonalities
) goto finish
;
1477 /* If it was there, remove it from the catalogue proper
1478 * by calling removeDrivers(). Pass true for the second
1479 * argument to trigger a new round of matching, and
1480 * then remove the extension from the dictionary of startup
1483 keyIterator
= OSCollectionIterator::withCollection(
1484 extensionPersonalities
);
1486 IOLog("Error: Couldn't allocate iterator to scan
1487 personalities for %s.\n", extensionName
);
1491 while ((key
= OSDynamicCast(OSString
, keyIterator
->getNextObject()))) {
1492 personality
= OSDynamicCast(OSDictionary
,
1493 extensionPersonalities
->getObject(key
));
1497 gIOCatalogue
->removeDrivers(personality
, true);
1501 startupExtensions
->removeObject(extensionName
);
1505 if (keyIterator
) keyIterator
->release();
1509 /*********************************************************************
1510 * FIXME: This function invalidates the globals gStartupExtensions and
1511 * FIXME: ...gBootLoaderObjects without setting them to NULL. Since
1512 * FIXME: ...the code itself is immediately unloaded, there may not be
1513 * FIXME: ...any reason to worry about that!
1514 *********************************************************************/
1515 void clearStartupExtensionsAndLoaderInfo(void)
1517 OSDictionary
* startupExtensions
= NULL
; // must release
1518 OSArray
* bootLoaderObjects
= NULL
; // must release
1520 IORegistryEntry
* bootxMemoryMap
= NULL
; // must release
1521 OSDictionary
* propertyDict
= NULL
; // must release
1522 OSCollectionIterator
* keyIterator
= NULL
; // must release
1523 OSString
* key
= NULL
; // don't release
1526 * Drop any temporarily held data objects.
1528 bootLoaderObjects
= getBootLoaderObjects();
1529 if (bootLoaderObjects
) {
1530 bootLoaderObjects
->release();
1534 * If any "code" entries in driver dictionaries are accompanied
1535 * by "compressedCode" entries, then those data objects were
1536 * created based of of kmem_alloc()'ed memory, which must be
1539 startupExtensions
= getStartupExtensions();
1540 if (startupExtensions
) {
1542 OSCollectionIterator::withCollection(startupExtensions
);
1544 IOLog("Error: Couldn't allocate iterator for startup "
1547 goto memory_map
; // bail to the memory_map label
1550 while ( (key
= OSDynamicCast(OSString
,
1551 keyIterator
->getNextObject())) ) {
1553 OSDictionary
* driverDict
= 0;
1554 OSData
* codeData
= 0;
1556 driverDict
= OSDynamicCast(OSDictionary
,
1557 startupExtensions
->getObject(key
));
1559 codeData
= OSDynamicCast(OSData
,
1560 driverDict
->getObject("code"));
1563 driverDict
->getObject("compressedCode")) {
1565 kmem_free(kernel_map
,
1566 (unsigned int)codeData
->getBytesNoCopy(),
1567 codeData
->getLength());
1572 keyIterator
->release();
1573 startupExtensions
->release();
1579 * Go through the device tree's memory map and remove any driver
1583 IORegistryEntry::fromPath(
1584 "/chosen/memory-map", // path
1587 // return value is retained so be sure to release it
1589 if (!bootxMemoryMap
) {
1590 IOLog("Error: Couldn't read booter memory map.\n");
1595 propertyDict
= bootxMemoryMap
->dictionaryWithProperties();
1596 if (!propertyDict
) {
1597 IOLog("Error: Couldn't get property dictionary "
1598 "from memory map.\n");
1603 keyIterator
= OSCollectionIterator::withCollection(propertyDict
);
1605 IOLog("Error: Couldn't allocate iterator for driver images.\n");
1610 while ( (key
= OSDynamicCast(OSString
,
1611 keyIterator
->getNextObject())) ) {
1613 const char * keyValue
= key
->getCStringNoCopy();
1615 if ( !strncmp(keyValue
, BOOTX_KEXT_PREFIX
,
1616 strlen(BOOTX_KEXT_PREFIX
)) ||
1617 !strncmp(keyValue
, BOOTX_MULTIKEXT_PREFIX
,
1618 strlen(BOOTX_MULTIKEXT_PREFIX
)) ) {
1620 OSData
* bootxDriverDataObject
= NULL
;
1621 MemoryMapFileInfo
* driverInfo
= 0;
1623 bootxDriverDataObject
= OSDynamicCast(OSData
,
1624 propertyDict
->getObject(keyValue
));
1625 // don't release bootxDriverDataObject
1627 if (!bootxDriverDataObject
) {
1630 driverInfo
= (MemoryMapFileInfo
*)
1631 bootxDriverDataObject
->getBytesNoCopy(0,
1632 sizeof(MemoryMapFileInfo
));
1633 IODTFreeLoaderInfo((char *)keyValue
,
1634 (void *)driverInfo
->paddr
,
1635 (int)driverInfo
->length
);
1640 if (bootxMemoryMap
) bootxMemoryMap
->release();
1641 if (propertyDict
) propertyDict
->release();
1642 if (keyIterator
) keyIterator
->release();