xref: /xnu-11215/libsa/bootstrap.cpp (revision e13b1fa5)
1 /*
2  * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
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.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
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.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 #include <IOKit/IOLib.h>
29 #include <mach/kmod.h>
30 #include <libkern/c++/OSDictionary.h>
31 
32 #include <libsa/kext.h>
33 #include <libsa/catalogue.h>
34 #include <libsa/malloc.h>
35 
36 #include "kld_patch.h"
37 
38 /*****
39  * This function is used by IOCatalogue to load a kernel
40  * extension. libsa initially sets it to be a function
41  * that uses libkld to load and link the extension from
42  * within the kernel. Once the root filesystem is up,
43  * this gets switch to the kmod_load_extension() function,
44  * which merely queues the extension for loading by the
45  * kmodload utility.
46  */
47 extern kern_return_t (*kmod_load_function)(char *extension_name);
48 extern bool (*record_startup_extensions_function)(void);
49 extern bool (*add_from_mkext_function)(OSData * mkext);
50 extern void (*remove_startup_extension_function)(const char * name);
51 
52 /****
53  * IOCatalogue uses this variable to make a few decisions
54  * about loading and matching drivers.
55  */
56 extern int kernelLinkerPresent;
57 
58 
59 class KLDBootstrap {
60 public:
61     KLDBootstrap();
62     ~KLDBootstrap();
63 };
64 
65 
66 static KLDBootstrap bootstrap_obj;
67 
68 
69 /* The constructor creates a lock and puts entries into a dispatch
70  * table for functions used to record and load kmods.
71  */
72 KLDBootstrap::KLDBootstrap() {
73 
74     malloc_init();
75 
76     kmod_load_function = &load_kernel_extension;
77 
78     record_startup_extensions_function = &recordStartupExtensions;
79     add_from_mkext_function = &addExtensionsFromArchive;
80     remove_startup_extension_function = &removeStartupExtension;
81 
82 #ifndef CONFIG_NOKLD
83     kernelLinkerPresent = 1;
84 #endif
85 }
86 
87 /* The destructor frees all wired memory regions held
88  * by libsa's malloc package and disposes of the lock.
89  */
90 KLDBootstrap::~KLDBootstrap() {
91 
92     kld_file_cleanup_all_resources();
93 
94    /* Dump all device-tree entries for boot drivers, and all
95     * info on startup extensions. The IOCatalogue will now
96     * get personalities from kextd.
97     */
98     clearStartupExtensionsAndLoaderInfo();
99 
100    /* Free all temporary malloc memory.
101     */
102     malloc_reset();
103 }
104