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/IOCatalogue.h>
30 #include <IOKit/IOLib.h>
31 #include <libsa/kext.h>
32 #include <libsa/catalogue.h>
35 #include <mach-o/kld.h>
36 #include <libsa/vers_rsrc.h>
37 #include <libsa/stdlib.h>
38 #include <mach/kmod.h>
39 #include <vm/vm_kern.h>
40 #include <mach/kern_return.h>
41 #include <mach-o/fat.h>
42 #include <mach_loader.h>
44 #include "kld_patch.h"
57 kmod_destroy_internal(kmod_t id
);
64 mach_msg_type_number_t
*dataCount
);
66 extern kern_return_t
kmod_retain(kmod_t id
);
67 extern kern_return_t
kmod_release(kmod_t id
);
69 extern void flush_dcache(vm_offset_t addr
, unsigned cnt
, int phys
);
70 extern void invalidate_icache(vm_offset_t addr
, unsigned cnt
, int phys
);
75 #define LOG_DELAY(x) IODelay((x) * 1000000)
76 #define VTYELLOW "\033[33m"
77 #define VTRESET "\033[0m"
84 /*********************************************************************
86 *********************************************************************/
89 const char * bundleid
,
90 OSDictionary
** plist
,
91 unsigned char ** code
,
92 unsigned long * code_size
,
93 bool * caller_owns_code
)
96 OSDictionary
* extensionsDict
; // don't release
97 OSDictionary
* extDict
; // don't release
98 OSDictionary
* extPlist
; // don't release
99 unsigned long code_size_local
;
101 /* Get the dictionary of startup extensions.
102 * This is keyed by module name.
104 extensionsDict
= getStartupExtensions();
105 if (!extensionsDict
) {
106 IOLog("startup extensions dictionary is missing\n");
111 /* Get the requested extension's dictionary entry and its property
112 * list, containing module dependencies.
114 extDict
= OSDynamicCast(OSDictionary
,
115 extensionsDict
->getObject(bundleid
));
118 IOLog("extension \"%s\" cannot be found\n",
125 extPlist
= OSDynamicCast(OSDictionary
, extDict
->getObject("plist"));
127 IOLog("extension \"%s\" has no info dictionary\n",
137 /* If asking for code, the caller must provide a return buffer
140 if (!caller_owns_code
) {
141 IOLog("getKext(): invalid usage (caller_owns_code not provided)\n");
150 *caller_owns_code
= false;
152 *code
= (unsigned char *)kld_file_getaddr(bundleid
,
153 (long *)&code_size_local
);
156 *code_size
= code_size_local
;
159 OSData
* driverCode
= 0; // release only if uncompressing!
161 driverCode
= OSDynamicCast(OSData
, extDict
->getObject("code"));
163 *code
= (unsigned char *)driverCode
->getBytesNoCopy();
165 *code_size
= driverCode
->getLength();
167 } else { // Look for compressed code and uncompress it
168 OSData
* compressedCode
= 0;
169 compressedCode
= OSDynamicCast(OSData
,
170 extDict
->getObject("compressedCode"));
171 if (compressedCode
) {
172 if (!uncompressModule(compressedCode
, &driverCode
)) {
173 IOLog("extension \"%s\": couldn't uncompress code\n",
178 *caller_owns_code
= true;
179 *code
= (unsigned char *)driverCode
->getBytesNoCopy();
181 *code_size
= driverCode
->getLength();
183 driverCode
->release();
195 /*********************************************************************
197 *********************************************************************/
199 bool verifyCompatibility(OSString
* extName
, OSString
* requiredVersion
)
201 OSDictionary
* extPlist
; // don't release
202 OSString
* extVersion
; // don't release
203 OSString
* extCompatVersion
; // don't release
204 VERS_version ext_version
;
205 VERS_version ext_compat_version
;
206 VERS_version required_version
;
208 if (!getKext(extName
->getCStringNoCopy(), &extPlist
, NULL
, NULL
, NULL
)) {
212 extVersion
= OSDynamicCast(OSString
,
213 extPlist
->getObject("CFBundleVersion"));
215 IOLog("verifyCompatibility(): "
216 "Extension \"%s\" has no \"CFBundleVersion\" property.\n",
217 extName
->getCStringNoCopy());
221 extCompatVersion
= OSDynamicCast(OSString
,
222 extPlist
->getObject("OSBundleCompatibleVersion"));
223 if (!extCompatVersion
) {
224 IOLog("verifyCompatibility(): "
225 "Extension \"%s\" has no \"OSBundleCompatibleVersion\" property.\n",
226 extName
->getCStringNoCopy());
230 required_version
= VERS_parse_string(requiredVersion
->getCStringNoCopy());
231 if (required_version
< 0) {
232 IOLog("verifyCompatibility(): "
233 "Can't parse required version \"%s\" of dependency %s.\n",
234 requiredVersion
->getCStringNoCopy(),
235 extName
->getCStringNoCopy());
238 ext_version
= VERS_parse_string(extVersion
->getCStringNoCopy());
239 if (ext_version
< 0) {
240 IOLog("verifyCompatibility(): "
241 "Can't parse version \"%s\" of dependency %s.\n",
242 extVersion
->getCStringNoCopy(),
243 extName
->getCStringNoCopy());
246 ext_compat_version
= VERS_parse_string(extCompatVersion
->getCStringNoCopy());
247 if (ext_compat_version
< 0) {
248 IOLog("verifyCompatibility(): "
249 "Can't parse compatible version \"%s\" of dependency %s.\n",
250 extCompatVersion
->getCStringNoCopy(),
251 extName
->getCStringNoCopy());
255 if (required_version
> ext_version
|| required_version
< ext_compat_version
) {
262 /*********************************************************************
263 *********************************************************************/
265 bool kextIsDependency(const char * kext_name
, char * is_kernel
) {
267 OSDictionary
* extensionsDict
= 0; // don't release
268 OSDictionary
* extDict
= 0; // don't release
269 OSDictionary
* extPlist
= 0; // don't release
270 OSBoolean
* isKernelResourceObj
= 0; // don't release
271 OSData
* driverCode
= 0; // don't release
272 OSData
* compressedCode
= 0; // don't release
278 /* Get the dictionary of startup extensions.
279 * This is keyed by module name.
281 extensionsDict
= getStartupExtensions();
282 if (!extensionsDict
) {
283 IOLog("startup extensions dictionary is missing\n");
288 /* Get the requested extension's dictionary entry and its property
289 * list, containing module dependencies.
291 extDict
= OSDynamicCast(OSDictionary
,
292 extensionsDict
->getObject(kext_name
));
295 IOLog("extension \"%s\" cannot be found\n",
301 extPlist
= OSDynamicCast(OSDictionary
, extDict
->getObject("plist"));
303 IOLog("extension \"%s\" has no info dictionary\n",
309 /* A kext that is a kernel component is still a dependency, as there
310 * are fake kmod entries for them.
312 isKernelResourceObj
= OSDynamicCast(OSBoolean
,
313 extPlist
->getObject("OSKernelResource"));
314 if (isKernelResourceObj
&& isKernelResourceObj
->isTrue()) {
320 driverCode
= OSDynamicCast(OSData
, extDict
->getObject("code"));
321 compressedCode
= OSDynamicCast(OSData
,
322 extDict
->getObject("compressedCode"));
324 if ((driverCode
|| compressedCode
) && is_kernel
&& *is_kernel
) {
328 if (!driverCode
&& !compressedCode
&& !isKernelResourceObj
) {
338 /*********************************************************************
339 *********************************************************************/
341 addDependenciesForKext(OSDictionary
* kextPlist
,
342 OSArray
* dependencyList
,
343 OSString
* trueParent
,
344 Boolean skipKernelDependencies
)
347 bool hasDirectKernelDependency
= false;
348 OSString
* kextName
= 0; // don't release
349 OSDictionary
* libraries
= 0; // don't release
350 OSCollectionIterator
* keyIterator
= 0; // must release
351 OSString
* libraryName
= 0; // don't release
352 OSString
* dependentName
= 0; // don't release
354 kextName
= OSDynamicCast(OSString
,
355 kextPlist
->getObject("CFBundleIdentifier"));
357 // XXX: Add log message
362 libraries
= OSDynamicCast(OSDictionary
,
363 kextPlist
->getObject("OSBundleLibraries"));
369 keyIterator
= OSCollectionIterator::withCollection(libraries
);
371 // XXX: Add log message
376 dependentName
= trueParent
? trueParent
: kextName
;
378 while ( (libraryName
= OSDynamicCast(OSString
,
379 keyIterator
->getNextObject())) ) {
381 OSString
* libraryVersion
= OSDynamicCast(OSString
,
382 libraries
->getObject(libraryName
));
383 if (!libraryVersion
) {
384 // XXX: Add log message
388 if (!verifyCompatibility(libraryName
, libraryVersion
)) {
392 char is_kernel_component
;
394 if (!kextIsDependency(libraryName
->getCStringNoCopy(),
395 &is_kernel_component
)) {
397 is_kernel_component
= false;
400 if (!skipKernelDependencies
|| !is_kernel_component
) {
401 dependencyList
->setObject(dependentName
);
402 dependencyList
->setObject(libraryName
);
404 if (!hasDirectKernelDependency
&& is_kernel_component
) {
405 hasDirectKernelDependency
= true;
409 if (!hasDirectKernelDependency
) {
410 const OSSymbol
* kernelName
= 0;
412 /* a kext without any kernel dependency is assumed dependent on 6.0 */
413 dependencyList
->setObject(dependentName
);
415 kernelName
= OSSymbol::withCString("com.apple.kernel.libkern");
417 // XXX: Add log message
421 dependencyList
->setObject(kernelName
);
422 kernelName
->release();
424 IOLog("Extension \"%s\" has no kernel dependency.\n",
425 kextName
->getCStringNoCopy());
429 if (keyIterator
) keyIterator
->release();
433 /*********************************************************************
434 *********************************************************************/
436 bool getVersionForKext(OSDictionary
* kextPlist
, char ** version
)
438 OSString
* kextName
= 0; // don't release
439 OSString
* kextVersion
; // don't release
441 kextName
= OSDynamicCast(OSString
,
442 kextPlist
->getObject("CFBundleIdentifier"));
444 // XXX: Add log message
448 kextVersion
= OSDynamicCast(OSString
,
449 kextPlist
->getObject("CFBundleVersion"));
451 IOLog("getVersionForKext(): "
452 "Extension \"%s\" has no \"CFBundleVersion\" property.\n",
453 kextName
->getCStringNoCopy());
458 *version
= (char *)kextVersion
->getCStringNoCopy();
464 /*********************************************************************
465 *********************************************************************/
467 bool add_dependencies_for_kmod(const char * kmod_name
, dgraph_t
* dgraph
)
470 OSDictionary
* kextPlist
= 0; // don't release
471 unsigned int index
= 0;
472 OSArray
* dependencyList
= 0; // must release
473 unsigned char * code
= 0;
474 unsigned long code_length
= 0;
475 bool code_is_kmem
= false;
476 char * kmod_vers
= 0; // from plist, don't free
477 char is_kernel_component
= false;
478 dgraph_entry_t
* dgraph_entry
= 0; // don't free
479 dgraph_entry_t
* dgraph_dependency
= 0; // don't free
480 bool kext_is_dependency
= true;
483 * Set up the root kmod.
485 if (!getKext(kmod_name
, &kextPlist
, &code
, &code_length
,
487 IOLog("can't find extension %s\n", kmod_name
);
492 if (!kextIsDependency(kmod_name
, &is_kernel_component
)) {
493 IOLog("extension %s is not loadable\n", kmod_name
);
498 if (!getVersionForKext(kextPlist
, &kmod_vers
)) {
499 IOLog("can't get version for extension %s\n", kmod_name
);
504 dgraph_entry
= dgraph_add_dependent(dgraph
, kmod_name
,
505 code
, code_length
, code_is_kmem
,
506 kmod_name
, kmod_vers
,
507 0 /* load_address not yet known */, is_kernel_component
);
509 IOLog("can't record %s in dependency graph\n", kmod_name
);
511 // kmem_alloc()ed code is freed in finish: block.
515 // pass ownership of code to kld patcher
517 if (kload_map_entry(dgraph_entry
) != kload_error_none
) {
518 IOLog("can't map %s in preparation for loading\n", kmod_name
);
520 // kmem_alloc()ed code is freed in finish: block.
524 // clear local record of code
527 code_is_kmem
= false;
530 * Now handle all the dependencies.
532 dependencyList
= OSArray::withCapacity(5);
533 if (!dependencyList
) {
534 IOLog("memory allocation failure\n");
540 if (!addDependenciesForKext(kextPlist
, dependencyList
, NULL
, false)) {
541 IOLog("can't determine immediate dependencies for extension %s\n",
547 /* IMPORTANT: loop condition gets list count every time through, as the
548 * array CAN change each iteration.
550 for (index
= 0; index
< dependencyList
->getCount(); index
+= 2) {
551 OSString
* dependentName
= 0;
552 OSString
* libraryName
= 0;
553 const char * dependent_name
= 0;
554 const char * library_name
= 0;
556 /* 255 is an arbitrary limit. Multiplied by 2 because the dependency
557 * list is stocked with pairs (dependent -> dependency).
559 if (index
> (2 * 255)) {
560 IOLog("extension dependency graph ridiculously long, indicating a loop\n");
565 dependentName
= OSDynamicCast(OSString
,
566 dependencyList
->getObject(index
));
567 libraryName
= OSDynamicCast(OSString
,
568 dependencyList
->getObject(index
+ 1));
570 if (!dependentName
|| !libraryName
) {
571 IOLog("malformed dependency list\n");
576 dependent_name
= dependentName
->getCStringNoCopy();
577 library_name
= libraryName
->getCStringNoCopy();
579 if (!getKext(library_name
, &kextPlist
, NULL
, NULL
, NULL
)) {
581 IOLog("can't find extension %s\n", library_name
);
586 OSString
* string
= OSDynamicCast(OSString
,
587 kextPlist
->getObject("OSBundleSharedExecutableIdentifier"));
589 library_name
= string
->getCStringNoCopy();
590 if (!getKext(library_name
, &kextPlist
, NULL
, NULL
, NULL
)) {
591 IOLog("can't find extension %s\n", library_name
);
597 kext_is_dependency
= kextIsDependency(library_name
,
598 &is_kernel_component
);
600 if (kext_is_dependency
) {
601 dgraph_entry
= dgraph_find_dependent(dgraph
, dependent_name
);
603 IOLog("internal error with dependency graph\n");
609 if (!getVersionForKext(kextPlist
, &kmod_vers
)) {
610 IOLog("can't get version for extension %s\n", library_name
);
615 /* It's okay for code to be zero, as for a pseudokext
616 * representing a kernel component.
618 if (!getKext(library_name
, NULL
/* already got it */,
619 &code
, &code_length
, &code_is_kmem
)) {
620 IOLog("can't find extension %s\n", library_name
);
625 dgraph_dependency
= dgraph_add_dependency(dgraph
, dgraph_entry
,
626 library_name
, code
, code_length
, code_is_kmem
,
627 library_name
, kmod_vers
,
628 0 /* load_address not yet known */, is_kernel_component
);
630 if (!dgraph_dependency
) {
631 IOLog("can't record dependency %s -> %s\n", dependent_name
,
634 // kmem_alloc()ed code is freed in finish: block.
638 // pass ownership of code to kld patcher
640 if (kload_map_entry(dgraph_dependency
) != kload_error_none
) {
641 IOLog("can't map %s in preparation for loading\n", library_name
);
643 // kmem_alloc()ed code is freed in finish: block.
647 // clear local record of code
650 code_is_kmem
= false;
653 /* Now put the library's dependencies onto the pending set.
655 if (!addDependenciesForKext(kextPlist
, dependencyList
,
656 kext_is_dependency
? NULL
: dependentName
, !kext_is_dependency
)) {
658 IOLog("can't determine immediate dependencies for extension %s\n",
666 if (code
&& code_is_kmem
) {
667 kmem_free(kernel_map
, (unsigned int)code
, code_length
);
669 if (dependencyList
) dependencyList
->release();
674 /*********************************************************************
675 * This is the function that IOCatalogue calls in order to load a kmod.
676 * It first checks whether the kmod is already loaded. If the kmod
677 * isn't loaded, this function builds a dependency list and calls
678 * load_kmod() repeatedly to guarantee that each dependency is in fact
680 *********************************************************************/
682 kern_return_t
load_kernel_extension(char * kmod_name
)
684 kern_return_t result
= KERN_SUCCESS
;
685 kload_error load_result
= kload_error_none
;
687 bool free_dgraph
= false;
688 kmod_info_t
* kmod_info
;
690 // Put this in for lots of messages about kext loading.
692 kload_set_log_level(kload_log_level_load_details
);
695 /* See if the kmod is already loaded.
697 if ((kmod_info
= kmod_lookupbyname_locked(kmod_name
))) {
698 kfree(kmod_info
, sizeof(kmod_info_t
));
702 if (dgraph_init(&dgraph
) != dgraph_valid
) {
703 IOLog("Can't initialize dependency graph to load %s.\n",
705 result
= KERN_FAILURE
;
710 if (!add_dependencies_for_kmod(kmod_name
, &dgraph
)) {
711 IOLog("Can't determine dependencies for %s.\n",
713 result
= KERN_FAILURE
;
717 dgraph
.root
= dgraph_find_root(&dgraph
);
720 IOLog("Dependency graph to load %s has no root.\n",
722 result
= KERN_FAILURE
;
726 /* A kernel component is built in and need not be loaded.
728 if (dgraph
.root
->is_kernel_component
) {
729 result
= KERN_SUCCESS
;
733 dgraph_establish_load_order(&dgraph
);
735 load_result
= kload_load_dgraph(&dgraph
);
736 if (load_result
!= kload_error_none
&&
737 load_result
!= kload_error_already_loaded
) {
739 IOLog(VTYELLOW
"Failed to load extension %s.\n" VTRESET
, kmod_name
);
741 result
= KERN_FAILURE
;
748 dgraph_free(&dgraph
, 0 /* don't free dgraph itself */);