2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 #include <libsa/kmod.h>
26 #include <libkern/c++/OSContainers.h>
27 #include <IOKit/IOCatalogue.h>
28 #include <IOKit/IOLib.h>
29 #include <libsa/kmod.h>
30 #include <libsa/catalogue.h>
33 #include <mach-o/kld.h>
34 #include <libsa/vers_rsrc.h>
35 #include <libsa/stdlib.h>
36 #include <mach/kmod.h>
37 #include <vm/vm_kern.h>
38 #include <mach/kern_return.h>
39 #include <mach-o/fat.h>
40 #include <mach_loader.h>
43 #include "kld_patch.h"
53 kmod_destroy_internal(kmod_t id
);
60 mach_msg_type_number_t
*dataCount
);
62 extern kern_return_t
kmod_retain(kmod_t id
);
63 extern kern_return_t
kmod_release(kmod_t id
);
65 extern void flush_dcache64(addr64_t addr
, unsigned cnt
, int phys
);
66 extern void invalidate_icache64(addr64_t addr
, unsigned cnt
, int phys
);
72 #define VTYELLOW "\033[33m"
73 #define VTRESET "\033[0m"
78 /*********************************************************************
80 *********************************************************************/
81 bool verifyCompatibility(OSString
* extName
, OSString
* requiredVersion
)
83 OSDictionary
* extensionsDict
; // don't release
84 OSDictionary
* extDict
; // don't release
85 OSDictionary
* extPlist
; // don't release
86 OSString
* extVersion
; // don't release
87 OSString
* extCompatVersion
; // don't release
89 UInt32 ext_compat_version
;
90 UInt32 required_version
;
92 /* Get the dictionary of startup extensions.
93 * This is keyed by module name.
95 extensionsDict
= getStartupExtensions();
96 if (!extensionsDict
) {
97 IOLog("verifyCompatibility(): No extensions dictionary.\n");
101 /* Get the requested extension's dictionary entry and its property
102 * list, containing module dependencies.
104 extDict
= OSDynamicCast(OSDictionary
,
105 extensionsDict
->getObject(extName
));
108 IOLog("verifyCompatibility(): "
109 "Extension \"%s\" cannot be found.\n",
110 extName
->getCStringNoCopy());
114 extPlist
= OSDynamicCast(OSDictionary
, extDict
->getObject("plist"));
116 IOLog("verifyCompatibility(): "
117 "Extension \"%s\" has no property list.\n",
118 extName
->getCStringNoCopy());
123 extVersion
= OSDynamicCast(OSString
,
124 extPlist
->getObject("CFBundleVersion"));
126 IOLog("verifyCompatibility(): "
127 "Extension \"%s\" has no \"CFBundleVersion\" property.\n",
128 extName
->getCStringNoCopy());
132 extCompatVersion
= OSDynamicCast(OSString
,
133 extPlist
->getObject("OSBundleCompatibleVersion"));
134 if (!extCompatVersion
) {
135 IOLog("verifyCompatibility(): "
136 "Extension \"%s\" has no \"OSBundleCompatibleVersion\" property.\n",
137 extName
->getCStringNoCopy());
141 if (!VERS_parse_string(requiredVersion
->getCStringNoCopy(),
142 &required_version
)) {
143 IOLog("verifyCompatibility(): "
144 "Can't parse required version \"%s\" of dependency %s.\n",
145 requiredVersion
->getCStringNoCopy(),
146 extName
->getCStringNoCopy());
149 if (!VERS_parse_string(extVersion
->getCStringNoCopy(),
151 IOLog("verifyCompatibility(): "
152 "Can't parse version \"%s\" of dependency %s.\n",
153 extVersion
->getCStringNoCopy(),
154 extName
->getCStringNoCopy());
157 if (!VERS_parse_string(extCompatVersion
->getCStringNoCopy(),
158 &ext_compat_version
)) {
159 IOLog("verifyCompatibility(): "
160 "Can't parse compatible version \"%s\" of dependency %s.\n",
161 extCompatVersion
->getCStringNoCopy(),
162 extName
->getCStringNoCopy());
166 if (required_version
> ext_version
|| required_version
< ext_compat_version
) {
173 /*********************************************************************
174 *********************************************************************/
176 Boolean
kextIsADependency(OSString
* name
) {
177 Boolean result
= true;
178 OSDictionary
* extensionsDict
= 0; // don't release
179 OSDictionary
* extDict
= 0; // don't release
180 OSDictionary
* extPlist
= 0; // don't release
181 OSBoolean
* isKernelResourceObj
= 0; // don't release
182 OSData
* driverCode
= 0; // don't release
183 OSData
* compressedCode
= 0; // don't release
185 extensionsDict
= getStartupExtensions();
186 if (!extensionsDict
) {
187 IOLog("kextIsADependency(): No extensions dictionary.\n");
194 extDict
= OSDynamicCast(OSDictionary
,
195 extensionsDict
->getObject(name
));
197 IOLog("kextIsADependency(): "
198 "Extension \"%s\" cannot be found.\n",
199 name
->getCStringNoCopy());
205 extPlist
= OSDynamicCast(OSDictionary
, extDict
->getObject("plist"));
207 IOLog("getDependencyListForKmod(): "
208 "Extension \"%s\" has no property list.\n",
209 name
->getCStringNoCopy());
215 /* A kext that is a kernel component is still a dependency, as there
216 * are fake kmod entries for them.
218 isKernelResourceObj
= OSDynamicCast(OSBoolean
,
219 extPlist
->getObject("OSKernelResource"));
220 if (isKernelResourceObj
&& isKernelResourceObj
->isTrue()) {
225 driverCode
= OSDynamicCast(OSData
, extDict
->getObject("code"));
226 compressedCode
= OSDynamicCast(OSData
,
227 extDict
->getObject("compressedCode"));
229 if (!driverCode
&& !compressedCode
) {
239 /*********************************************************************
240 * This function builds a uniqued, in-order list of modules that need
241 * to be loaded in order for kmod_name to be successfully loaded. This
242 * list ends with kmod_name itself.
243 *********************************************************************/
245 OSArray
* getDependencyListForKmod(const char * kmod_name
) {
249 OSDictionary
* extensionsDict
; // don't release
250 OSDictionary
* extDict
; // don't release
251 OSDictionary
* extPlist
; // don't release
252 OSString
* extName
; // don't release
253 OSArray
* dependencyList
= NULL
; // return value, caller releases
256 /* These are used to remove duplicates from the dependency list.
258 OSArray
* originalList
= NULL
; // must be released
259 OSDictionary
* encounteredNames
= NULL
; // must be release
262 /* Get the dictionary of startup extensions.
263 * This is keyed by module name.
265 extensionsDict
= getStartupExtensions();
266 if (!extensionsDict
) {
267 IOLog("getDependencyListForKmod(): No extensions dictionary.\n");
274 /* Get the requested extension's dictionary entry and its property
275 * list, containing module dependencies.
277 extDict
= OSDynamicCast(OSDictionary
,
278 extensionsDict
->getObject(kmod_name
));
281 IOLog("getDependencyListForKmod(): "
282 "Extension \"%s\" cannot be found.\n",
289 extPlist
= OSDynamicCast(OSDictionary
, extDict
->getObject("plist"));
291 IOLog("getDependencyListForKmod(): "
292 "Extension \"%s\" has no property list.\n",
300 /* Verify that the retrieved entry's "CFBundleIdentifier" property exists.
301 * This will be added to the dependency list.
303 extName
= OSDynamicCast(OSString
,
304 extPlist
->getObject("CFBundleIdentifier"));
306 IOLog("getDependencyListForKmod(): "
307 "Extension \"%s\" has no \"CFBundleIdentifier\" property.\n",
314 dependencyList
= OSArray::withCapacity(10);
315 if (!dependencyList
) {
316 IOLog("getDependencyListForKmod(): "
317 "Couldn't allocate dependency array for extension \"%s\".\n",
325 /* Okay, let's get started.
327 dependencyList
->setObject(extName
);
330 /* Here's a slightly tricky bit. This loop iterates through
331 * the dependency list until it runs off the end. Each time
332 * through, however, any number of dependencies can be added
333 * to the end of the list. Eventually some extensions won't
334 * have any more dependencies, no more names will be added
335 * to the list, and this loop will terminate.
337 for (i
= 0; i
< dependencyList
->getCount(); i
++) {
339 // None of these needs to be released, as they're all from plists.
341 OSDictionary
* curExtDict
;
342 OSDictionary
* curExtDepDict
;
343 OSDictionary
* curExtPlist
;
344 OSString
* curDepName
;
347 /* An arbitrary limit to prevent infinite loops.
350 IOLog("getDependencyListForKmod(): "
351 "max dependency list length exceeded for "
352 "extension \"%s\".\n",
359 curName
= OSDynamicCast(OSString
, dependencyList
->getObject(i
));
361 curExtDict
= OSDynamicCast(OSDictionary
,
362 extensionsDict
->getObject(curName
));
364 IOLog("getDependencyListForKmod(): "
365 "Extension \"%s\", required for extension \"%s\", "
366 "is not available.\n",
367 curName
->getCStringNoCopy(), kmod_name
);
373 curExtPlist
= OSDynamicCast(OSDictionary
,
374 curExtDict
->getObject("plist"));
376 IOLog("getDependencyListForKmod(): "
377 "Extension \"%s\", required for extension \"%s\", "
378 "has no property list.\n",
379 curName
->getCStringNoCopy(), kmod_name
);
385 curExtDepDict
= OSDynamicCast(OSDictionary
,
386 curExtPlist
->getObject("OSBundleLibraries"));
388 OSCollectionIterator
* keyIterator
=
389 OSCollectionIterator::withCollection(curExtDepDict
);
392 IOLog("getDependencyListForKmod(): "
393 "Couldn't allocate iterator for extension "
394 "\"%s\".\n", kmod_name
);
399 while ( (curDepName
=
400 OSDynamicCast(OSString
,
401 keyIterator
->getNextObject())) ) {
403 OSString
* requiredVersion
= OSDynamicCast(OSString
,
404 curExtDepDict
->getObject(curDepName
));
406 if (!verifyCompatibility(curDepName
, requiredVersion
)) {
407 IOLog("getDependencyListForKmod(): "
408 "Dependency %s of %s is not compatible or is unavailable.\n",
409 curDepName
->getCStringNoCopy(),
410 curName
->getCStringNoCopy());
416 dependencyList
->setObject(curDepName
);
419 keyIterator
->release();
425 * The dependency list now exists in the reverse order of required loads,
426 * and may have duplicates. Now we turn the list around and remove
429 originalList
= dependencyList
;
430 dependencyList
= OSArray::withCapacity(originalList
->getCount());
431 if (!dependencyList
) {
432 IOLog("getDependenciesForKmod(): "
433 "Couldn't allocate reversal dependency list for extension "
434 "\"%s\".\n", kmod_name
);
439 encounteredNames
= OSDictionary::withCapacity(originalList
->getCount());
440 if (!encounteredNames
) {
441 IOLog("getDependenciesForKmod(): "
442 "Couldn't allocate list of encountered names for extension "
443 "\"%s\".\n", kmod_name
);
450 /* Go backward through the original list, using the encounteredNames
451 * dictionary to check for duplicates. We put originalList in as the
452 * value because we need some non-NULL value. Here we also drop any
453 * extensions that aren't proper dependencies (that is, any that are
454 * nonkernel kexts without code).
456 i
= originalList
->getCount();
462 OSString
* item
= OSDynamicCast(OSString
,
463 originalList
->getObject(i
));
465 if ( (!encounteredNames
->getObject(item
)) &&
466 kextIsADependency(item
)) {
468 encounteredNames
->setObject(item
, originalList
);
469 dependencyList
->setObject(item
);
478 originalList
->release();
480 if (encounteredNames
) {
481 encounteredNames
->release();
484 if (dependencyList
) {
485 dependencyList
->release();
486 dependencyList
= NULL
;
490 return dependencyList
;
494 /*********************************************************************
495 *********************************************************************/
496 /* Used in address_for_loaded_kmod.
498 static kmod_info_t
* g_current_kmod_info
= NULL
;
499 static const char * g_current_kmod_name
= NULL
;
501 /* Globals to pass link buffer info from
502 * address_for_loaded_kmod() and alloc_for_kmod()
505 * link_load_address is the address used to lay
506 * down the linked code. It gets adjusted by the
507 * pad between the headers size and a full page
508 * multiple. If an error occurs this gets set to
509 * zero so that the kld client code can detect
510 * an address or allocation error even if kld
513 * link_load_size is the size of the image as
514 * created by kld_load_from_memory(). link_buffer_size
515 * is the size of the buffer allocated for the final
516 * laid-down image, and is adjusted by rounding the
517 * load size and header size up to full-page multiples.
519 * link_buffer_address is set only by alloc_for_kmod();
520 * its value is used as a check if kld_load_from_memory()
521 * fails so that the buffer can be deallocated.
523 static unsigned long link_load_address
= 0;
524 static unsigned long link_load_size
= 0;
525 static unsigned long link_buffer_size
= 0;
526 static unsigned long link_header_size
= 0;
527 static unsigned long link_buffer_address
= 0;
530 /*********************************************************************
531 * This function is registered before kmod_load_from_memory() is
532 * invoked to build symbol table entries for an already-loaded
533 * kmod. This function just checks the g_current_kmod_info variable
534 * to gets its load address, and futzes it by the header offset (pad).
535 * See lower comments for more info on load address futzing.
536 *********************************************************************/
538 unsigned long address_for_loaded_kmod(
540 unsigned long headers_size
) {
542 unsigned long round_headers_size
;
543 unsigned long headers_pad
;
545 if (!g_current_kmod_info
) {
546 IOLog("address_for_loaded_kmod(): No current kmod.\n");
548 link_load_address
= 0; // error sentinel for kld client
552 round_headers_size
= round_page_32(headers_size
);
553 headers_pad
= round_headers_size
- headers_size
;
555 link_load_address
= (unsigned long)g_current_kmod_info
->address
+
558 return link_load_address
;
562 /*********************************************************************
563 * This function is registered before kmod_load_from_memory() is
564 * invoked to actually load a new kmod. It rounds up the header and
565 * total sizes and vm_allocates a buffer for the kmod. Now, KLD doesn't
566 * enforce any alignment of headers or segments, and we want to make
567 * sure that the executable code of the kmod lies on a page boundary.
568 * to do so, this function figures the pad between the actual header
569 * size and the page-rounded header size, and returns that offset into
570 * the allocated buffer. After kmod_load_from_memory() returns, its
571 * caller will move the mach_header struct back to the beginning of the
572 * allocated buffer so that the kmod_info_t structure contains the
574 *********************************************************************/
576 unsigned long alloc_for_kmod(
578 unsigned long headers_size
) {
580 vm_address_t buffer
= 0;
581 kern_return_t k_result
;
583 unsigned long round_headers_size
;
584 unsigned long round_segments_size
;
585 unsigned long round_size
;
586 unsigned long headers_pad
;
588 round_headers_size
= round_page_32(headers_size
);
589 round_segments_size
= round_page_32(size
- headers_size
);
590 round_size
= round_headers_size
+ round_segments_size
;
591 headers_pad
= round_headers_size
- headers_size
;
593 k_result
= vm_allocate(kernel_map
, (vm_offset_t
*)&buffer
,
595 if (k_result
!= KERN_SUCCESS
) {
596 IOLog("alloc_for_kmod(): Can't allocate memory.\n");
598 link_buffer_address
= 0; // make sure it's clear
599 link_load_address
= 0; // error sentinel for kld client
603 link_load_size
= size
;
605 link_buffer_address
= buffer
;
606 link_buffer_size
= round_size
;
607 link_header_size
= headers_size
; // NOT rounded!
609 link_load_address
= link_buffer_address
+ headers_pad
;
611 return link_load_address
;
614 /*********************************************************************
615 * This function reads the startup extensions dictionary to get the
616 * address and length of the executable data for the requested kmod.
617 *********************************************************************/
619 int map_and_patch(const char * kmod_name
) {
623 // Does the kld system already know about this kmod?
624 address
= (char *) kld_file_getaddr(kmod_name
, NULL
);
628 // None of these needs to be released.
629 OSDictionary
* extensionsDict
;
630 OSDictionary
* kmodDict
;
631 OSData
* compressedCode
= 0;
633 // Driver Code may need to be released
636 /* Get the requested kmod's info dictionary from the global
637 * startup extensions dictionary.
639 extensionsDict
= getStartupExtensions();
640 if (!extensionsDict
) {
641 IOLog("map_and_patch(): No extensions dictionary.\n");
646 kmodDict
= OSDynamicCast(OSDictionary
,
647 extensionsDict
->getObject(kmod_name
));
649 IOLog("map_and_patch(): "
650 "Extension \"%s\" cannot be found.\n", kmod_name
);
657 driverCode
= OSDynamicCast(OSData
, kmodDict
->getObject("code"));
659 ret
= kld_file_map(kmod_name
,
660 (unsigned char *) driverCode
->getBytesNoCopy(),
661 (size_t) driverCode
->getLength(),
664 else { // May be an compressed extension
666 // If we have a compressed segment the uncompressModule
667 // will return a new OSData object that points to the kmem_alloced
668 // memory. Note we don't take a reference to driverCode so later
669 // when we release it we will actually free this driver. Ownership
670 // of the kmem has been handed of to kld_file.
671 compressedCode
= OSDynamicCast(OSData
,
672 kmodDict
->getObject("compressedCode"));
673 if (!compressedCode
) {
674 IOLog("map_and_patch(): "
675 "Extension \"%s\" has no \"code\" property.\n", kmod_name
);
679 if (!uncompressModule(compressedCode
, &driverCode
)) {
680 IOLog("map_and_patch(): "
681 "Extension \"%s\" Couldn't uncompress code.\n", kmod_name
);
686 unsigned char *driver
= (unsigned char *) driverCode
->getBytesNoCopy();
687 size_t driverSize
= driverCode
->getLength();
689 ret
= kld_file_map(kmod_name
, driver
, driverSize
, /* isKmem */ true);
690 driverCode
->release();
692 kmem_free(kernel_map
, (vm_address_t
) driver
, driverSize
);
696 IOLog("map_and_patch(): "
697 "Extension \"%s\" Didn't successfully load.\n", kmod_name
);
703 if (!kld_file_patch_OSObjects(kmod_name
)) {
704 IOLog("map_and_patch(): "
705 "Extension \"%s\" Error binding OSObjects.\n", kmod_name
);
708 // RY: Instead of returning here, set the return value.
709 // We still need to call kld_file_prepare_for_link because
710 // we might have patched files outside of the driver. Don't
711 // worry, it will know to ignore the damaged file
715 // Now repair any damage that the kld patcher may have done to the image
716 kld_file_prepare_for_link();
721 /*********************************************************************
722 *********************************************************************/
723 bool stamp_kmod(const char * kmod_name
, kmod_info_t
* kmod_info
) {
725 OSDictionary
* extensionsDict
= NULL
; // don't release
726 OSDictionary
* kmodDict
= NULL
; // don't release
727 OSDictionary
* plist
= NULL
; // don't release
728 OSString
* versionString
= NULL
; // don't release
729 const char * plist_version
= NULL
; // don't free
731 if (strlen(kmod_name
) + 1 > KMOD_MAX_NAME
) {
732 IOLog("stamp_kmod(): Kext identifier \"%s\" is too long.\n",
739 strcpy(kmod_info
->name
, kmod_name
);
741 /* Get the dictionary of startup extensions.
742 * This is keyed by module name.
744 extensionsDict
= getStartupExtensions();
745 if (!extensionsDict
) {
746 IOLog("stamp_kmod(): No extensions dictionary.\n");
752 kmodDict
= OSDynamicCast(OSDictionary
,
753 extensionsDict
->getObject(kmod_name
));
755 IOLog("stamp_kmod(): Can't find record for kmod \"%s\".\n",
762 plist
= OSDynamicCast(OSDictionary
,
763 kmodDict
->getObject("plist"));
765 IOLog("stamp_kmod(): Kmod \"%s\" has no property list.\n",
773 * Get the kext's version and stuff it into the kmod. This used
774 * to be a check that the kext & kmod had the same version, but
775 * now we just overwrite the kmod's version.
778 versionString
= OSDynamicCast(OSString
,
779 plist
->getObject("CFBundleVersion"));
780 if (!versionString
) {
781 IOLog("stamp_kmod(): Kmod \"%s\" has no \"CFBundleVersion\" "
789 plist_version
= versionString
->getCStringNoCopy();
790 if (!plist_version
) {
791 IOLog("stamp_kmod(): Can't get C string for kext version.\n");
797 if (strlen(plist_version
) + 1 > KMOD_MAX_NAME
) {
798 IOLog("stamp_kmod(): Version \"%s\" of kext \"%s\" is too long.\n",
799 plist_version
, kmod_name
);
805 strcpy(kmod_info
->version
, plist_version
);
815 /*********************************************************************
816 * This function takes a dependency list containing a series of
817 * already-loaded module names, followed by a single name for a module
818 * that hasn't yet been loaded. It invokes kld_load_from_memory() to
819 * build symbol info for the already-loaded modules, and then finally
820 * loads the actually requested module.
821 *********************************************************************/
823 kern_return_t
load_kmod(OSArray
* dependencyList
) {
824 kern_return_t result
= KERN_SUCCESS
;
826 unsigned int num_dependencies
= 0;
827 kmod_info_t
** kmod_dependencies
= NULL
;
829 OSString
* requestedKmodName
; // don't release
830 const char * requested_kmod_name
;
831 OSString
* currentKmodName
; // don't release
833 unsigned long kmod_size
;
834 struct mach_header
* kmod_header
;
835 unsigned long kld_result
;
836 int do_kld_unload
= 0;
837 kmod_info_t
* kmod_info_freeme
= 0;
838 kmod_info_t
* kmod_info
= 0;
842 /* Separate the requested kmod from its dependencies.
844 i
= dependencyList
->getCount();
846 IOLog("load_kmod(): Called with empty list.\n");
848 result
= KERN_FAILURE
;
851 i
--; // make i be the index of the last entry
854 requestedKmodName
= OSDynamicCast(OSString
, dependencyList
->getObject(i
));
855 if (!requestedKmodName
) {
856 IOLog("load_kmod(): Called with invalid list of kmod names.\n");
858 result
= KERN_FAILURE
;
861 requested_kmod_name
= requestedKmodName
->getCStringNoCopy();
862 dependencyList
->removeObject(i
);
864 /* If the requested kmod is already loaded, there's no work to do.
866 kmod_info_freeme
= kmod_lookupbyname_locked(requested_kmod_name
);
867 if (kmod_info_freeme
) {
868 // FIXME: Need to check for version mismatch if already loaded.
869 result
= KERN_SUCCESS
;
874 /* Do the KLD loads for the already-loaded modules in order to get
877 kld_address_func(&address_for_loaded_kmod
);
879 num_dependencies
= dependencyList
->getCount();
880 kmod_dependencies
= (kmod_info_t
**)kalloc(num_dependencies
*
881 sizeof(kmod_info_t
*));
882 if (!kmod_dependencies
) {
883 IOLog("load_kmod(): Failed to allocate memory for dependency array "
884 "during load of kmod \"%s\".\n", requested_kmod_name
);
886 result
= KERN_FAILURE
;
890 bzero(kmod_dependencies
, num_dependencies
*
891 sizeof(kmod_info_t
*));
893 for (i
= 0; i
< num_dependencies
; i
++) {
895 currentKmodName
= OSDynamicCast(OSString
,
896 dependencyList
->getObject(i
));
898 if (!currentKmodName
) {
899 IOLog("load_kmod(): Invalid dependency name at index %d for "
900 "kmod \"%s\".\n", i
, requested_kmod_name
);
902 result
= KERN_FAILURE
;
906 const char * current_kmod_name
= currentKmodName
->getCStringNoCopy();
908 // These globals are needed by the kld_address functions
909 g_current_kmod_info
= kmod_lookupbyname_locked(current_kmod_name
);
910 g_current_kmod_name
= current_kmod_name
;
912 if (!g_current_kmod_info
) {
913 IOLog("load_kmod(): Missing dependency \"%s\".\n",
916 result
= KERN_FAILURE
;
920 /* Record the current kmod as a dependency of the requested
921 * one. This will be used in building references after the
924 kmod_dependencies
[i
] = g_current_kmod_info
;
926 /* If the current kmod's size is zero it means that we have a
927 * fake in-kernel dependency. If so then don't have to arrange
928 * for its symbol table to be reloaded as it is
929 * part of the kernel's symbol table..
931 if (!g_current_kmod_info
->size
)
934 if (!kld_file_merge_OSObjects(current_kmod_name
)) {
935 IOLog("load_kmod(): Can't merge OSObjects \"%s\".\n",
938 result
= KERN_FAILURE
;
942 kmod_address
= (char *)
943 kld_file_getaddr(current_kmod_name
, (long *) &kmod_size
);
946 IOLog("load_kmod() failed for dependency kmod "
947 "\"%s\".\n", current_kmod_name
);
949 result
= KERN_FAILURE
;
953 kld_result
= kld_load_from_memory(&kmod_header
,
954 current_kmod_name
, kmod_address
, kmod_size
);
960 if (!kld_result
|| !link_load_address
) {
961 IOLog("kld_load_from_memory() failed for dependency kmod "
962 "\"%s\".\n", current_kmod_name
);
964 result
= KERN_FAILURE
;
968 kld_forget_symbol("_kmod_info");
972 * Now that we've done all the dependencies, which should have already
973 * been loaded, we do the last requested module, which should not have
974 * already been loaded.
976 kld_address_func(&alloc_for_kmod
);
978 g_current_kmod_name
= requested_kmod_name
;
979 g_current_kmod_info
= 0; // there is no kmod yet
981 if (!map_and_patch(requested_kmod_name
)) {
982 IOLog("load_kmod: map_and_patch() failed for "
983 "kmod \"%s\".\n", requested_kmod_name
);
985 result
= KERN_FAILURE
;
989 kmod_address
= (char *)
990 kld_file_getaddr(requested_kmod_name
, (long *) &kmod_size
);
992 IOLog("load_kmod: kld_file_getaddr() failed internal error "
993 "on \"%s\".\n", requested_kmod_name
);
995 result
= KERN_FAILURE
;
999 kld_result
= kld_load_from_memory(&kmod_header
,
1000 requested_kmod_name
, kmod_address
, kmod_size
);
1006 if (!kld_result
|| !link_load_address
) {
1007 IOLog("load_kmod(): kld_load_from_memory() failed for "
1008 "kmod \"%s\".\n", requested_kmod_name
);
1010 result
= KERN_FAILURE
;
1015 /* Copy the linked header and image into the vm_allocated buffer.
1016 * Move each onto the appropriate page-aligned boundary as given
1017 * by the global link_... variables.
1019 bzero((char *)link_buffer_address
, link_buffer_size
);
1020 // bcopy() is (from, to, length)
1021 bcopy((char *)kmod_header
, (char *)link_buffer_address
, link_header_size
);
1022 bcopy((char *)kmod_header
+ link_header_size
,
1023 (char *)link_buffer_address
+ round_page_32(link_header_size
),
1024 link_load_size
- link_header_size
);
1027 /* Get the kmod_info struct for the newly-loaded kmod.
1029 if (!kld_lookup("_kmod_info", (unsigned long *)&kmod_info
)) {
1030 IOLog("kld_lookup() of \"_kmod_info\" failed for "
1031 "kmod \"%s\".\n", requested_kmod_name
);
1033 result
= KERN_FAILURE
;
1038 if (!stamp_kmod(requested_kmod_name
, kmod_info
)) {
1039 // stamp_kmod() logs a meaningful message
1040 result
= KERN_FAILURE
;
1045 /* kld_lookup of _kmod_info yielded the actual linked address,
1046 * so now that we've copied the data into its real place,
1047 * we can set this stuff.
1049 kmod_info
->address
= link_buffer_address
;
1050 kmod_info
->size
= link_buffer_size
;
1051 kmod_info
->hdr_size
= round_page_32(link_header_size
);
1053 /* We've written data and instructions, so *flush* the data cache
1054 * and *invalidate* the instruction cache.
1056 flush_dcache64((addr64_t
)link_buffer_address
, link_buffer_size
, false);
1057 invalidate_icache64((addr64_t
)link_buffer_address
, link_buffer_size
, false);
1060 /* Register the new kmod with the kernel proper.
1062 if (kmod_create_internal(kmod_info
, &kmod_id
) != KERN_SUCCESS
) {
1063 IOLog("load_kmod(): kmod_create() failed for "
1064 "kmod \"%s\".\n", requested_kmod_name
);
1066 result
= KERN_FAILURE
;
1071 IOLog("kmod id %d successfully created at 0x%lx, size %ld.\n",
1072 (unsigned int)kmod_id
, link_buffer_address
, link_buffer_size
);
1076 /* Record dependencies for the newly-loaded kmod.
1078 for (i
= 0; i
< num_dependencies
; i
++) {
1079 kmod_info_t
* cur_dependency_info
;
1081 cur_dependency_info
= kmod_dependencies
[i
];
1082 packed_id
= KMOD_PACK_IDS(kmod_id
, cur_dependency_info
->id
);
1083 if (kmod_retain(packed_id
) != KERN_SUCCESS
) {
1084 IOLog("load_kmod(): kmod_retain() failed for "
1085 "kmod \"%s\".\n", requested_kmod_name
);
1087 kmod_destroy_internal(kmod_id
);
1088 result
= KERN_FAILURE
;
1093 /* Start the kmod (which invokes constructors for I/O Kit
1096 // kmod_start_or_stop(id, start?, user data, datalen)
1097 if (kmod_start_or_stop(kmod_id
, 1, 0, 0) != KERN_SUCCESS
) {
1098 IOLog("load_kmod(): kmod_start_or_stop() failed for "
1099 "kmod \"%s\".\n", requested_kmod_name
);
1101 kmod_destroy_internal(kmod_id
);
1102 result
= KERN_FAILURE
;
1108 if (kmod_info_freeme
) {
1109 kfree((unsigned int)kmod_info_freeme
, sizeof(kmod_info_t
));
1112 /* Only do a kld_unload_all() if at least one load happened.
1114 if (do_kld_unload
) {
1115 kld_unload_all(/* deallocate sets */ 1);
1118 /* If the link failed, blow away the allocated link buffer.
1120 if (result
!= KERN_SUCCESS
&& link_buffer_address
) {
1121 vm_deallocate(kernel_map
, link_buffer_address
, link_buffer_size
);
1124 if (kmod_dependencies
) {
1125 for (i
= 0; i
< num_dependencies
; i
++) {
1126 if (kmod_dependencies
[i
]) {
1127 kfree((unsigned int)kmod_dependencies
[i
], sizeof(kmod_info_t
));
1130 kfree((unsigned int)kmod_dependencies
,
1131 num_dependencies
* sizeof(kmod_info_t
*));
1134 /* Reset these static global variables for the next call.
1136 g_current_kmod_name
= NULL
;
1137 g_current_kmod_info
= NULL
;
1138 link_buffer_address
= 0;
1139 link_load_address
= 0;
1141 link_buffer_size
= 0;
1142 link_header_size
= 0;
1148 /*********************************************************************
1149 * This is the function that IOCatalogue calls in order to load a kmod.
1150 * It first checks whether the kmod is already loaded. If the kmod
1151 * isn't loaded, this function builds a dependency list and calls
1152 * load_kmod() repeatedly to guarantee that each dependency is in fact
1154 *********************************************************************/
1156 kern_return_t
load_kernel_extension(char * kmod_name
) {
1157 kern_return_t result
= KERN_SUCCESS
;
1158 kmod_info_t
* kmod_info
= 0; // must free
1159 OSArray
* dependencyList
= NULL
; // must release
1160 OSArray
* curDependencyList
= NULL
; // must release
1162 /* See if the kmod is already loaded.
1164 kmod_info
= kmod_lookupbyname_locked(kmod_name
);
1165 if (kmod_info
) { // NOT checked
1166 result
= KERN_SUCCESS
;
1170 /* It isn't loaded; build a dependency list and
1175 dependencyList
= getDependencyListForKmod(kmod_name
);
1176 if (!dependencyList
) {
1177 IOLog("load_kernel_extension(): "
1178 "Can't get dependencies for kernel extension \"%s\".\n",
1181 result
= KERN_FAILURE
;
1185 count
= dependencyList
->getCount();
1186 for (i
= 0; i
< count
; i
++) {
1187 kern_return_t load_result
;
1188 OSString
* curKmodName
; // don't release
1189 const char * cur_kmod_name
;
1191 curKmodName
= OSDynamicCast(OSString
,
1192 dependencyList
->getObject(i
));
1193 cur_kmod_name
= curKmodName
->getCStringNoCopy();
1194 curDependencyList
= getDependencyListForKmod(cur_kmod_name
);
1195 if (!curDependencyList
) {
1196 IOLog("load_kernel_extension(): "
1197 "Can't get dependencies for kernel extension \"%s\".\n",
1200 result
= KERN_FAILURE
;
1203 load_result
= load_kmod(curDependencyList
);
1204 if (load_result
!= KERN_SUCCESS
) {
1205 IOLog("load_kernel_extension(): "
1206 "load_kmod() failed for kmod \"%s\".\n",
1209 result
= load_result
;
1212 curDependencyList
->release();
1213 curDependencyList
= NULL
;
1221 kfree((unsigned int)kmod_info
, sizeof(kmod_info_t
));
1224 if (dependencyList
) {
1225 dependencyList
->release();
1226 dependencyList
= NULL
;
1228 if (curDependencyList
) {
1229 curDependencyList
->release();
1230 curDependencyList
= NULL
;