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