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 <libkern/c++/OSContainers.h>
26 #include <IOKit/IOCatalogue.h>
27 #include <IOKit/IOLib.h>
28 #include <libsa/kext.h>
29 #include <libsa/catalogue.h>
32 #include <mach-o/kld.h>
33 #include <libsa/vers_rsrc.h>
34 #include <libsa/stdlib.h>
35 #include <mach/kmod.h>
36 #include <vm/vm_kern.h>
37 #include <mach/kern_return.h>
38 #include <mach-o/fat.h>
39 #include <mach_loader.h>
41 #include "kld_patch.h"
54 kmod_destroy_internal(kmod_t id
);
61 mach_msg_type_number_t
*dataCount
);
63 extern kern_return_t
kmod_retain(kmod_t id
);
64 extern kern_return_t
kmod_release(kmod_t id
);
66 extern void flush_dcache(vm_offset_t addr
, unsigned cnt
, int phys
);
67 extern void invalidate_icache(vm_offset_t addr
, unsigned cnt
, int phys
);
72 #define LOG_DELAY(x) IODelay((x) * 1000000)
73 #define VTYELLOW "\033[33m"
74 #define VTRESET "\033[0m"
81 /*********************************************************************
83 *********************************************************************/
86 const char * bundleid
,
87 OSDictionary
** plist
,
88 unsigned char ** code
,
89 unsigned long * code_size
,
90 bool * caller_owns_code
)
93 OSDictionary
* extensionsDict
; // don't release
94 OSDictionary
* extDict
; // don't release
95 OSDictionary
* extPlist
; // don't release
96 unsigned long code_size_local
;
98 /* Get the dictionary of startup extensions.
99 * This is keyed by module name.
101 extensionsDict
= getStartupExtensions();
102 if (!extensionsDict
) {
103 IOLog("startup extensions dictionary is missing\n");
108 /* Get the requested extension's dictionary entry and its property
109 * list, containing module dependencies.
111 extDict
= OSDynamicCast(OSDictionary
,
112 extensionsDict
->getObject(bundleid
));
115 IOLog("extension \"%s\" cannot be found\n",
122 extPlist
= OSDynamicCast(OSDictionary
, extDict
->getObject("plist"));
124 IOLog("extension \"%s\" has no info dictionary\n",
134 /* If asking for code, the caller must provide a return buffer
137 if (!caller_owns_code
) {
138 IOLog("getKext(): invalid usage (caller_owns_code not provided)\n");
147 *caller_owns_code
= false;
149 *code
= (unsigned char *)kld_file_getaddr(bundleid
,
150 (long *)&code_size_local
);
153 *code_size
= code_size_local
;
156 OSData
* driverCode
= 0; // release only if uncompressing!
158 driverCode
= OSDynamicCast(OSData
, extDict
->getObject("code"));
160 *code
= (unsigned char *)driverCode
->getBytesNoCopy();
162 *code_size
= driverCode
->getLength();
164 } else { // Look for compressed code and uncompress it
165 OSData
* compressedCode
= 0;
166 compressedCode
= OSDynamicCast(OSData
,
167 extDict
->getObject("compressedCode"));
168 if (compressedCode
) {
169 if (!uncompressModule(compressedCode
, &driverCode
)) {
170 IOLog("extension \"%s\": couldn't uncompress code\n",
176 *caller_owns_code
= true;
177 *code
= (unsigned char *)driverCode
->getBytesNoCopy();
179 *code_size
= driverCode
->getLength();
181 driverCode
->release();
193 /*********************************************************************
195 *********************************************************************/
197 bool verifyCompatibility(OSString
* extName
, OSString
* requiredVersion
)
199 OSDictionary
* extPlist
; // don't release
200 OSString
* extVersion
; // don't release
201 OSString
* extCompatVersion
; // don't release
202 VERS_version ext_version
;
203 VERS_version ext_compat_version
;
204 VERS_version required_version
;
206 if (!getKext(extName
->getCStringNoCopy(), &extPlist
, NULL
, NULL
, NULL
)) {
210 extVersion
= OSDynamicCast(OSString
,
211 extPlist
->getObject("CFBundleVersion"));
213 IOLog("verifyCompatibility(): "
214 "Extension \"%s\" has no \"CFBundleVersion\" property.\n",
215 extName
->getCStringNoCopy());
219 extCompatVersion
= OSDynamicCast(OSString
,
220 extPlist
->getObject("OSBundleCompatibleVersion"));
221 if (!extCompatVersion
) {
222 IOLog("verifyCompatibility(): "
223 "Extension \"%s\" has no \"OSBundleCompatibleVersion\" property.\n",
224 extName
->getCStringNoCopy());
228 required_version
= VERS_parse_string(requiredVersion
->getCStringNoCopy());
229 if (required_version
< 0) {
230 IOLog("verifyCompatibility(): "
231 "Can't parse required version \"%s\" of dependency %s.\n",
232 requiredVersion
->getCStringNoCopy(),
233 extName
->getCStringNoCopy());
236 ext_version
= VERS_parse_string(extVersion
->getCStringNoCopy());
237 if (ext_version
< 0) {
238 IOLog("verifyCompatibility(): "
239 "Can't parse version \"%s\" of dependency %s.\n",
240 extVersion
->getCStringNoCopy(),
241 extName
->getCStringNoCopy());
244 ext_compat_version
= VERS_parse_string(extCompatVersion
->getCStringNoCopy());
245 if (ext_compat_version
< 0) {
246 IOLog("verifyCompatibility(): "
247 "Can't parse compatible version \"%s\" of dependency %s.\n",
248 extCompatVersion
->getCStringNoCopy(),
249 extName
->getCStringNoCopy());
253 if (required_version
> ext_version
|| required_version
< ext_compat_version
) {
260 /*********************************************************************
261 *********************************************************************/
263 bool kextIsDependency(const char * kext_name
, char * is_kernel
) {
265 OSDictionary
* extensionsDict
= 0; // don't release
266 OSDictionary
* extDict
= 0; // don't release
267 OSDictionary
* extPlist
= 0; // don't release
268 OSBoolean
* isKernelResourceObj
= 0; // don't release
269 OSData
* driverCode
= 0; // don't release
270 OSData
* compressedCode
= 0; // don't release
276 /* Get the dictionary of startup extensions.
277 * This is keyed by module name.
279 extensionsDict
= getStartupExtensions();
280 if (!extensionsDict
) {
281 IOLog("startup extensions dictionary is missing\n");
286 /* Get the requested extension's dictionary entry and its property
287 * list, containing module dependencies.
289 extDict
= OSDynamicCast(OSDictionary
,
290 extensionsDict
->getObject(kext_name
));
293 IOLog("extension \"%s\" cannot be found\n",
299 extPlist
= OSDynamicCast(OSDictionary
, extDict
->getObject("plist"));
301 IOLog("extension \"%s\" has no info dictionary\n",
307 /* A kext that is a kernel component is still a dependency, as there
308 * are fake kmod entries for them.
310 isKernelResourceObj
= OSDynamicCast(OSBoolean
,
311 extPlist
->getObject("OSKernelResource"));
312 if (isKernelResourceObj
&& isKernelResourceObj
->isTrue()) {
318 driverCode
= OSDynamicCast(OSData
, extDict
->getObject("code"));
319 compressedCode
= OSDynamicCast(OSData
,
320 extDict
->getObject("compressedCode"));
322 if ((driverCode
|| compressedCode
) && is_kernel
&& *is_kernel
) {
326 if (!driverCode
&& !compressedCode
&& !isKernelResourceObj
) {
336 /*********************************************************************
337 *********************************************************************/
339 figureDependenciesForKext(OSDictionary
* kextPlist
,
340 OSDictionary
* dependencies
,
341 OSString
* trueParent
)
344 OSString
* kextName
= 0; // don't release
345 OSDictionary
* libraries
= 0; // don't release
346 OSCollectionIterator
* keyIterator
= 0; // must release
347 OSString
* libraryName
= 0; // don't release
349 kextName
= OSDynamicCast(OSString
,
350 kextPlist
->getObject("CFBundleIdentifier"));
352 // XXX: Add log message
357 libraries
= OSDynamicCast(OSDictionary
,
358 kextPlist
->getObject("OSBundleLibraries"));
364 keyIterator
= OSCollectionIterator::withCollection(libraries
);
366 // XXX: Add log message
371 while ( (libraryName
= OSDynamicCast(OSString
,
372 keyIterator
->getNextObject())) ) {
374 OSString
* libraryVersion
= OSDynamicCast(OSString
,
375 libraries
->getObject(libraryName
));
376 if (!libraryVersion
) {
377 // XXX: Add log message
381 if (!verifyCompatibility(libraryName
, libraryVersion
)) {
385 dependencies
->setObject(libraryName
,
386 trueParent
? trueParent
: kextName
);
391 if (keyIterator
) keyIterator
->release();
395 /*********************************************************************
396 *********************************************************************/
398 bool getVersionForKext(OSDictionary
* kextPlist
, char ** version
)
400 OSString
* kextName
= 0; // don't release
401 OSString
* kextVersion
; // don't release
403 kextName
= OSDynamicCast(OSString
,
404 kextPlist
->getObject("CFBundleIdentifier"));
406 // XXX: Add log message
410 kextVersion
= OSDynamicCast(OSString
,
411 kextPlist
->getObject("CFBundleVersion"));
413 IOLog("getVersionForKext(): "
414 "Extension \"%s\" has no \"CFBundleVersion\" property.\n",
415 kextName
->getCStringNoCopy());
420 *version
= (char *)kextVersion
->getCStringNoCopy();
426 /*********************************************************************
427 *********************************************************************/
429 bool add_dependencies_for_kmod(const char * kmod_name
, dgraph_t
* dgraph
)
432 OSDictionary
* kextPlist
= 0; // don't release
433 OSDictionary
* workingDependencies
= 0; // must release
434 OSDictionary
* pendingDependencies
= 0; // must release
435 OSDictionary
* swapDict
= 0; // don't release
436 OSString
* dependentName
= 0; // don't release
437 const char * dependent_name
= 0; // don't free
438 OSString
* libraryName
= 0; // don't release
439 const char * library_name
= 0; // don't free
440 OSCollectionIterator
* dependencyIterator
= 0; // must release
441 unsigned char * code
= 0;
442 unsigned long code_length
= 0;
443 bool code_is_kmem
= false;
444 char * kmod_vers
= 0; // from plist, don't free
445 char is_kernel_component
= false;
446 dgraph_entry_t
* dgraph_entry
= 0; // don't free
447 dgraph_entry_t
* dgraph_dependency
= 0; // don't free
448 unsigned int graph_depth
= 0;
449 bool kext_is_dependency
= true;
451 if (!getKext(kmod_name
, &kextPlist
, &code
, &code_length
,
453 IOLog("can't find extension %s\n", kmod_name
);
458 if (!kextIsDependency(kmod_name
, &is_kernel_component
)) {
459 IOLog("extension %s is not loadable\n", kmod_name
);
464 if (!getVersionForKext(kextPlist
, &kmod_vers
)) {
465 IOLog("can't get version for extension %s\n", kmod_name
);
470 dgraph_entry
= dgraph_add_dependent(dgraph
, kmod_name
,
471 code
, code_length
, code_is_kmem
,
472 kmod_name
, kmod_vers
,
473 0 /* load_address not yet known */, is_kernel_component
);
475 IOLog("can't record %s in dependency graph\n", kmod_name
);
477 // kmem_alloc()ed code is freed in finish: block.
481 // pass ownership of code to kld patcher
484 if (kload_map_entry(dgraph_entry
) != kload_error_none
) {
485 IOLog("can't map %s in preparation for loading\n", kmod_name
);
487 // kmem_alloc()ed code is freed in finish: block.
491 // clear local record of code
494 code_is_kmem
= false;
496 workingDependencies
= OSDictionary::withCapacity(5);
497 if (!workingDependencies
) {
498 IOLog("memory allocation failure\n");
503 pendingDependencies
= OSDictionary::withCapacity(5);
504 if (!pendingDependencies
) {
505 IOLog("memory allocation failure\n");
510 if (!figureDependenciesForKext(kextPlist
, workingDependencies
, NULL
)) {
511 IOLog("can't determine immediate dependencies for extension %s\n",
518 while (workingDependencies
->getCount()) {
519 if (graph_depth
> 255) {
520 IOLog("extension dependency graph ridiculously long, indicating a loop\n");
525 if (dependencyIterator
) {
526 dependencyIterator
->release();
527 dependencyIterator
= 0;
530 dependencyIterator
= OSCollectionIterator::withCollection(
531 workingDependencies
);
532 if (!dependencyIterator
) {
533 IOLog("memory allocation failure\n");
538 while ( (libraryName
=
539 OSDynamicCast(OSString
, dependencyIterator
->getNextObject())) ) {
541 library_name
= libraryName
->getCStringNoCopy();
543 dependentName
= OSDynamicCast(OSString
,
544 workingDependencies
->getObject(libraryName
));
546 dependent_name
= dependentName
->getCStringNoCopy();
548 if (!getKext(library_name
, &kextPlist
, NULL
, NULL
, NULL
)) {
549 IOLog("can't find extension %s\n", library_name
);
555 if ((string
= OSDynamicCast(OSString
,
556 kextPlist
->getObject("OSBundleSharedExecutableIdentifier"))))
558 library_name
= string
->getCStringNoCopy();
559 if (!getKext(library_name
, &kextPlist
, NULL
, NULL
, NULL
)) {
560 IOLog("can't find extension %s\n", library_name
);
566 kext_is_dependency
= kextIsDependency(library_name
,
567 &is_kernel_component
);
569 if (!kext_is_dependency
) {
571 /* For binaryless kexts, add a new pending dependency from the
572 * original dependent onto the dependencies of the current,
573 * binaryless, dependency.
575 if (!figureDependenciesForKext(kextPlist
, pendingDependencies
,
578 IOLog("can't determine immediate dependencies for extension %s\n",
585 dgraph_entry
= dgraph_find_dependent(dgraph
, dependent_name
);
587 IOLog("internal error with dependency graph\n");
593 if (!getVersionForKext(kextPlist
, &kmod_vers
)) {
594 IOLog("can't get version for extension %s\n", library_name
);
599 /* It's okay for code to be zero, as for a pseudokext
600 * representing a kernel component.
602 if (!getKext(library_name
, NULL
/* already got it */,
603 &code
, &code_length
, &code_is_kmem
)) {
604 IOLog("can't find extension %s\n", library_name
);
609 dgraph_dependency
= dgraph_add_dependency(dgraph
, dgraph_entry
,
610 library_name
, code
, code_length
, code_is_kmem
,
611 library_name
, kmod_vers
,
612 0 /* load_address not yet known */, is_kernel_component
);
614 if (!dgraph_dependency
) {
615 IOLog("can't record dependency %s -> %s\n", dependent_name
,
618 // kmem_alloc()ed code is freed in finish: block.
622 // pass ownership of code to kld patcher
624 if (kload_map_entry(dgraph_dependency
) != kload_error_none
) {
625 IOLog("can't map %s in preparation for loading\n", library_name
);
627 // kmem_alloc()ed code is freed in finish: block.
631 // clear local record of code
634 code_is_kmem
= false;
637 /* Now put the library's dependencies onto the pending set.
639 if (!figureDependenciesForKext(kextPlist
, pendingDependencies
,
642 IOLog("can't determine immediate dependencies for extension %s\n",
649 dependencyIterator
->release();
650 dependencyIterator
= 0;
652 workingDependencies
->flushCollection();
653 swapDict
= workingDependencies
;
654 workingDependencies
= pendingDependencies
;
655 pendingDependencies
= swapDict
;
660 if (code
&& code_is_kmem
) {
661 kmem_free(kernel_map
, (unsigned int)code
, code_length
);
663 if (workingDependencies
) workingDependencies
->release();
664 if (pendingDependencies
) pendingDependencies
->release();
665 if (dependencyIterator
) dependencyIterator
->release();
669 /*********************************************************************
670 * This is the function that IOCatalogue calls in order to load a kmod.
671 * It first checks whether the kmod is already loaded. If the kmod
672 * isn't loaded, this function builds a dependency list and calls
673 * load_kmod() repeatedly to guarantee that each dependency is in fact
675 *********************************************************************/
677 kern_return_t
load_kernel_extension(char * kmod_name
)
679 kern_return_t result
= KERN_SUCCESS
;
680 kload_error load_result
= kload_error_none
;
682 bool free_dgraph
= false;
683 kmod_info_t
* kmod_info
;
685 // Put this in for lots of messages about kext loading.
687 kload_set_log_level(kload_log_level_load_details
);
690 /* See if the kmod is already loaded.
692 if ((kmod_info
= kmod_lookupbyname_locked(kmod_name
))) {
693 kfree((vm_offset_t
) kmod_info
, sizeof(kmod_info_t
));
697 if (dgraph_init(&dgraph
) != dgraph_valid
) {
698 IOLog("Can't initialize dependency graph to load %s.\n",
700 result
= KERN_FAILURE
;
705 if (!add_dependencies_for_kmod(kmod_name
, &dgraph
)) {
706 IOLog("Can't determine dependencies for %s.\n",
708 result
= KERN_FAILURE
;
712 dgraph
.root
= dgraph_find_root(&dgraph
);
715 IOLog("Dependency graph to load %s has no root.\n",
717 result
= KERN_FAILURE
;
721 /* A kernel component is built in and need not be loaded.
723 if (dgraph
.root
->is_kernel_component
) {
724 result
= KERN_SUCCESS
;
728 dgraph_establish_load_order(&dgraph
);
730 load_result
= kload_load_dgraph(&dgraph
);
731 if (load_result
!= kload_error_none
&&
732 load_result
!= kload_error_already_loaded
) {
734 IOLog(VTYELLOW
"Failed to load extension %s.\n" VTRESET
, kmod_name
);
736 result
= KERN_FAILURE
;
743 dgraph_free(&dgraph
, 0 /* don't free dgraph itself */);