xref: /xnu-11215/libsa/bootstrap.cpp (revision c1dac77f)
1 /*
2  * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3  *
4  * @APPLE_LICENSE_HEADER_START@
5  *
6  * The contents of this file constitute Original Code as defined in and
7  * are subject to the Apple Public Source License Version 1.1 (the
8  * "License").  You may not use this file except in compliance with the
9  * License.  Please obtain a copy of the License at
10  * http://www.apple.com/publicsource and read it before using this file.
11  *
12  * This Original Code and all software distributed under the License are
13  * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
17  * License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * @APPLE_LICENSE_HEADER_END@
21  */
22 #include <IOKit/IOLib.h>
23 #include <mach/kmod.h>
24 #include <libkern/c++/OSDictionary.h>
25 
26 #include <libsa/kmod.h>
27 #include <libsa/catalogue.h>
28 extern "C" {
29 #include <libsa/malloc.h>
30 };
31 
32 extern "C" {
33 /*****
34  * This function is used by IOCatalogue to load a kernel
35  * extension. libsa initially sets it to be a function
36  * that uses libkld to load and link the extension from
37  * within the kernel. Once the root filesystem is up,
38  * this gets switch to the kmod_load_extension() function,
39  * which merely queues the extension for loading by the
40  * kmodload utility.
41  */
42 extern kern_return_t (*kmod_load_function)(char *extension_name);
43 extern bool (*record_startup_extensions_function)(void);
44 extern bool (*add_from_mkext_function)(OSData * mkext);
45 extern void (*remove_startup_extension_function)(const char * name);
46 
47 /****
48  * IOCatalogue uses this variable to make a few decisions
49  * about loading and matching drivers.
50  */
51 extern int kernelLinkerPresent;
52 
53 extern IOLock * kld_lock;
54 };
55 
56 
57 class KLDBootstrap {
58 public:
59     KLDBootstrap();
60     ~KLDBootstrap();
61 };
62 
63 
64 static KLDBootstrap bootstrap_obj;
65 
66 
67 /* The constructor creates a lock and puts entries into a dispatch
68  * table for functions used to record and load kmods.
69  */
70 KLDBootstrap::KLDBootstrap() {
71 
72     kld_lock = IOLockAlloc();
73     IOLockLock(kld_lock);
74 
75     kmod_load_function = &load_kernel_extension;
76 
77     record_startup_extensions_function = &recordStartupExtensions;
78     add_from_mkext_function = &addExtensionsFromArchive;
79     remove_startup_extension_function = &removeStartupExtension;
80 
81     kernelLinkerPresent = 1;
82 
83     IOLockUnlock(kld_lock);
84 }
85 
86 /* The destructor frees all wired memory regions held
87  * by libsa's malloc package and disposes of the lock.
88  */
89 KLDBootstrap::~KLDBootstrap() {
90 
91     OSDictionary * startupExtensions;
92 
93     IOLockLock(kld_lock);
94 
95     malloc_reset();
96 
97     startupExtensions = getStartupExtensions();
98     if (startupExtensions) startupExtensions->release();
99 
100     IOLockUnlock(kld_lock);
101     IOLockFree(kld_lock);
102 
103 }
104