xref: /xnu-10063.141.1/libsa/bootstrap.cpp (revision d8b80295118ef25ac3a784134bcf95cd8e88109f)
1*d8b80295SApple OSS Distributions /*
2*d8b80295SApple OSS Distributions  * Copyright (c) 2000-2012 Apple Inc. All rights reserved.
3*d8b80295SApple OSS Distributions  *
4*d8b80295SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*d8b80295SApple OSS Distributions  *
6*d8b80295SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*d8b80295SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*d8b80295SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*d8b80295SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*d8b80295SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*d8b80295SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*d8b80295SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*d8b80295SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*d8b80295SApple OSS Distributions  *
15*d8b80295SApple OSS Distributions  * Please obtain a copy of the License at
16*d8b80295SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*d8b80295SApple OSS Distributions  *
18*d8b80295SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*d8b80295SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*d8b80295SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*d8b80295SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*d8b80295SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*d8b80295SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*d8b80295SApple OSS Distributions  * limitations under the License.
25*d8b80295SApple OSS Distributions  *
26*d8b80295SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*d8b80295SApple OSS Distributions  */
28*d8b80295SApple OSS Distributions extern "C" {
29*d8b80295SApple OSS Distributions #include <mach/kmod.h>
30*d8b80295SApple OSS Distributions #include <libkern/kernel_mach_header.h>
31*d8b80295SApple OSS Distributions #include <libkern/prelink.h>
32*d8b80295SApple OSS Distributions #include <libkern/crypto/sha2.h>
33*d8b80295SApple OSS Distributions }
34*d8b80295SApple OSS Distributions 
35*d8b80295SApple OSS Distributions #define IOKIT_ENABLE_SHARED_PTR
36*d8b80295SApple OSS Distributions 
37*d8b80295SApple OSS Distributions #include <libkern/version.h>
38*d8b80295SApple OSS Distributions #include <libkern/c++/OSContainers.h>
39*d8b80295SApple OSS Distributions #include <libkern/OSKextLibPrivate.h>
40*d8b80295SApple OSS Distributions #include <libkern/c++/OSKext.h>
41*d8b80295SApple OSS Distributions #include <IOKit/IOLib.h>
42*d8b80295SApple OSS Distributions #include <IOKit/IOService.h>
43*d8b80295SApple OSS Distributions #include <IOKit/IODeviceTreeSupport.h>
44*d8b80295SApple OSS Distributions #include <IOKit/IOCatalogue.h>
45*d8b80295SApple OSS Distributions 
46*d8b80295SApple OSS Distributions #if __x86_64__
47*d8b80295SApple OSS Distributions #define KASLR_KEXT_DEBUG 0
48*d8b80295SApple OSS Distributions #endif
49*d8b80295SApple OSS Distributions 
50*d8b80295SApple OSS Distributions #if PRAGMA_MARK
51*d8b80295SApple OSS Distributions #pragma mark Bootstrap Declarations
52*d8b80295SApple OSS Distributions #endif
53*d8b80295SApple OSS Distributions /*********************************************************************
54*d8b80295SApple OSS Distributions * Bootstrap Declarations
55*d8b80295SApple OSS Distributions *
56*d8b80295SApple OSS Distributions * The ENTIRE point of the libsa/KLD segment is to isolate bootstrap
57*d8b80295SApple OSS Distributions * code from other parts of the kernel, so function symbols are not
58*d8b80295SApple OSS Distributions * exported; rather pointers to those functions are exported.
59*d8b80295SApple OSS Distributions *
60*d8b80295SApple OSS Distributions * xxx - need to think about locking for handling the 'weak' refs.
61*d8b80295SApple OSS Distributions * xxx - do export a non-KLD function that says you've called a
62*d8b80295SApple OSS Distributions * xxx - bootstrap function that has been removed.
63*d8b80295SApple OSS Distributions *
64*d8b80295SApple OSS Distributions * ALL call-ins to this segment of the kernel must be done through
65*d8b80295SApple OSS Distributions * exported pointers. The symbols themselves are private and not to
66*d8b80295SApple OSS Distributions * be linked against.
67*d8b80295SApple OSS Distributions *********************************************************************/
68*d8b80295SApple OSS Distributions extern "C" {
69*d8b80295SApple OSS Distributions extern void (*record_startup_extensions_function)(void);
70*d8b80295SApple OSS Distributions extern void (*load_security_extensions_function)(void);
71*d8b80295SApple OSS Distributions };
72*d8b80295SApple OSS Distributions 
73*d8b80295SApple OSS Distributions static void bootstrapRecordStartupExtensions(void);
74*d8b80295SApple OSS Distributions static void bootstrapLoadSecurityExtensions(void);
75*d8b80295SApple OSS Distributions 
76*d8b80295SApple OSS Distributions 
77*d8b80295SApple OSS Distributions #if NO_KEXTD
78*d8b80295SApple OSS Distributions extern "C" bool IORamDiskBSDRoot(void);
79*d8b80295SApple OSS Distributions #endif
80*d8b80295SApple OSS Distributions 
81*d8b80295SApple OSS Distributions #if PRAGMA_MARK
82*d8b80295SApple OSS Distributions #pragma mark Macros
83*d8b80295SApple OSS Distributions #endif
84*d8b80295SApple OSS Distributions /*********************************************************************
85*d8b80295SApple OSS Distributions * Macros
86*d8b80295SApple OSS Distributions *********************************************************************/
87*d8b80295SApple OSS Distributions #define CONST_STRLEN(str) (sizeof(str) - 1)
88*d8b80295SApple OSS Distributions 
89*d8b80295SApple OSS Distributions #if PRAGMA_MARK
90*d8b80295SApple OSS Distributions #pragma mark Kernel Component Kext Identifiers
91*d8b80295SApple OSS Distributions #endif
92*d8b80295SApple OSS Distributions /*********************************************************************
93*d8b80295SApple OSS Distributions * Kernel Component Kext Identifiers
94*d8b80295SApple OSS Distributions *
95*d8b80295SApple OSS Distributions * We could have each kernel resource kext automatically "load" as
96*d8b80295SApple OSS Distributions * it's created, but it's nicer to have them listed in kextstat in
97*d8b80295SApple OSS Distributions * the order of this list. We'll walk through this after setting up
98*d8b80295SApple OSS Distributions * all the boot kexts and have them load up.
99*d8b80295SApple OSS Distributions *********************************************************************/
100*d8b80295SApple OSS Distributions static const char * sKernelComponentNames[] = {
101*d8b80295SApple OSS Distributions 	// The kexts for these IDs must have a version matching 'osrelease'.
102*d8b80295SApple OSS Distributions 	"com.apple.kernel",
103*d8b80295SApple OSS Distributions 	"com.apple.kpi.bsd",
104*d8b80295SApple OSS Distributions 	"com.apple.kpi.dsep",
105*d8b80295SApple OSS Distributions 	"com.apple.kpi.iokit",
106*d8b80295SApple OSS Distributions 	"com.apple.kpi.kasan",
107*d8b80295SApple OSS Distributions 	"com.apple.kpi.kcov",
108*d8b80295SApple OSS Distributions 	"com.apple.kpi.libkern",
109*d8b80295SApple OSS Distributions 	"com.apple.kpi.mach",
110*d8b80295SApple OSS Distributions 	"com.apple.kpi.private",
111*d8b80295SApple OSS Distributions 	"com.apple.kpi.unsupported",
112*d8b80295SApple OSS Distributions 	"com.apple.iokit.IONVRAMFamily",
113*d8b80295SApple OSS Distributions 	"com.apple.driver.AppleNMI",
114*d8b80295SApple OSS Distributions 	"com.apple.iokit.IOSystemManagementFamily",
115*d8b80295SApple OSS Distributions 	"com.apple.iokit.ApplePlatformFamily",
116*d8b80295SApple OSS Distributions 	NULL
117*d8b80295SApple OSS Distributions };
118*d8b80295SApple OSS Distributions 
119*d8b80295SApple OSS Distributions #if PRAGMA_MARK
120*d8b80295SApple OSS Distributions #pragma mark KLDBootstrap Class
121*d8b80295SApple OSS Distributions #endif
122*d8b80295SApple OSS Distributions /*********************************************************************
123*d8b80295SApple OSS Distributions * KLDBootstrap Class
124*d8b80295SApple OSS Distributions *
125*d8b80295SApple OSS Distributions * We use a C++ class here so that it can be a friend of OSKext and
126*d8b80295SApple OSS Distributions * get at private stuff. We can't hide the class itself, but we can
127*d8b80295SApple OSS Distributions * hide the instance through which we invoke the functions.
128*d8b80295SApple OSS Distributions *********************************************************************/
129*d8b80295SApple OSS Distributions class KLDBootstrap {
130*d8b80295SApple OSS Distributions 	friend void bootstrapRecordStartupExtensions(void);
131*d8b80295SApple OSS Distributions 	friend void bootstrapLoadSecurityExtensions(void);
132*d8b80295SApple OSS Distributions 
133*d8b80295SApple OSS Distributions private:
134*d8b80295SApple OSS Distributions 	void readStartupExtensions(void);
135*d8b80295SApple OSS Distributions 
136*d8b80295SApple OSS Distributions 	void readPrelinkedExtensions(kernel_mach_header_t *mh, kc_kind_t type);
137*d8b80295SApple OSS Distributions 	void readBooterExtensions(void);
138*d8b80295SApple OSS Distributions 
139*d8b80295SApple OSS Distributions 	OSReturn loadKernelComponentKexts(void);
140*d8b80295SApple OSS Distributions 	void     loadKernelExternalComponents(void);
141*d8b80295SApple OSS Distributions 	void     readBuiltinPersonalities(void);
142*d8b80295SApple OSS Distributions 
143*d8b80295SApple OSS Distributions 	void     loadSecurityExtensions(void);
144*d8b80295SApple OSS Distributions 
145*d8b80295SApple OSS Distributions public:
146*d8b80295SApple OSS Distributions 	KLDBootstrap(void);
147*d8b80295SApple OSS Distributions 	~KLDBootstrap(void);
148*d8b80295SApple OSS Distributions };
149*d8b80295SApple OSS Distributions 
150*d8b80295SApple OSS Distributions LIBKERN_ALWAYS_DESTROY static KLDBootstrap sBootstrapObject;
151*d8b80295SApple OSS Distributions 
152*d8b80295SApple OSS Distributions /*********************************************************************
153*d8b80295SApple OSS Distributions * Set the function pointers for the entry points into the bootstrap
154*d8b80295SApple OSS Distributions * segment upon C++ static constructor invocation.
155*d8b80295SApple OSS Distributions *********************************************************************/
KLDBootstrap(void)156*d8b80295SApple OSS Distributions KLDBootstrap::KLDBootstrap(void)
157*d8b80295SApple OSS Distributions {
158*d8b80295SApple OSS Distributions 	if (this != &sBootstrapObject) {
159*d8b80295SApple OSS Distributions 		panic("Attempt to access bootstrap segment.");
160*d8b80295SApple OSS Distributions 	}
161*d8b80295SApple OSS Distributions 	record_startup_extensions_function = &bootstrapRecordStartupExtensions;
162*d8b80295SApple OSS Distributions 	load_security_extensions_function = &bootstrapLoadSecurityExtensions;
163*d8b80295SApple OSS Distributions }
164*d8b80295SApple OSS Distributions 
165*d8b80295SApple OSS Distributions /*********************************************************************
166*d8b80295SApple OSS Distributions * Clear the function pointers for the entry points into the bootstrap
167*d8b80295SApple OSS Distributions * segment upon C++ static destructor invocation.
168*d8b80295SApple OSS Distributions *********************************************************************/
~KLDBootstrap(void)169*d8b80295SApple OSS Distributions KLDBootstrap::~KLDBootstrap(void)
170*d8b80295SApple OSS Distributions {
171*d8b80295SApple OSS Distributions 	if (this != &sBootstrapObject) {
172*d8b80295SApple OSS Distributions 		panic("Attempt to access bootstrap segment.");
173*d8b80295SApple OSS Distributions 	}
174*d8b80295SApple OSS Distributions 
175*d8b80295SApple OSS Distributions 
176*d8b80295SApple OSS Distributions 	record_startup_extensions_function = NULL;
177*d8b80295SApple OSS Distributions 	load_security_extensions_function = NULL;
178*d8b80295SApple OSS Distributions }
179*d8b80295SApple OSS Distributions 
180*d8b80295SApple OSS Distributions /*********************************************************************
181*d8b80295SApple OSS Distributions *********************************************************************/
182*d8b80295SApple OSS Distributions void
readStartupExtensions(void)183*d8b80295SApple OSS Distributions KLDBootstrap::readStartupExtensions(void)
184*d8b80295SApple OSS Distributions {
185*d8b80295SApple OSS Distributions 	kernel_section_t * prelinkInfoSect = NULL; // do not free
186*d8b80295SApple OSS Distributions 
187*d8b80295SApple OSS Distributions 	OSKextLog(/* kext */ NULL,
188*d8b80295SApple OSS Distributions 	    kOSKextLogProgressLevel |
189*d8b80295SApple OSS Distributions 	    kOSKextLogGeneralFlag | kOSKextLogDirectoryScanFlag |
190*d8b80295SApple OSS Distributions 	    kOSKextLogKextBookkeepingFlag,
191*d8b80295SApple OSS Distributions 	    "Reading startup extensions.");
192*d8b80295SApple OSS Distributions 
193*d8b80295SApple OSS Distributions 	kc_format_t kc_format;
194*d8b80295SApple OSS Distributions 	kernel_mach_header_t *mh = &_mh_execute_header;
195*d8b80295SApple OSS Distributions 	if (PE_get_primary_kc_format(&kc_format) && kc_format == KCFormatFileset) {
196*d8b80295SApple OSS Distributions 		mh = (kernel_mach_header_t *)PE_get_kc_header(KCKindPrimary);
197*d8b80295SApple OSS Distributions 	}
198*d8b80295SApple OSS Distributions 
199*d8b80295SApple OSS Distributions 	/* If the prelink info segment has a nonzero size, we are prelinked
200*d8b80295SApple OSS Distributions 	 * and won't have any individual kexts or mkexts to read.
201*d8b80295SApple OSS Distributions 	 * Otherwise, we need to read kexts or the mkext from what the booter
202*d8b80295SApple OSS Distributions 	 * has handed us.
203*d8b80295SApple OSS Distributions 	 */
204*d8b80295SApple OSS Distributions 	prelinkInfoSect = getsectbynamefromheader(mh, kPrelinkInfoSegment, kPrelinkInfoSection);
205*d8b80295SApple OSS Distributions 	if (prelinkInfoSect->size) {
206*d8b80295SApple OSS Distributions 		readPrelinkedExtensions(mh, KCKindPrimary);
207*d8b80295SApple OSS Distributions 	} else {
208*d8b80295SApple OSS Distributions 		readBooterExtensions();
209*d8b80295SApple OSS Distributions 	}
210*d8b80295SApple OSS Distributions 
211*d8b80295SApple OSS Distributions 	kernel_mach_header_t *akc_mh;
212*d8b80295SApple OSS Distributions 	akc_mh = (kernel_mach_header_t*)PE_get_kc_header(KCKindAuxiliary);
213*d8b80295SApple OSS Distributions 	if (akc_mh) {
214*d8b80295SApple OSS Distributions 		readPrelinkedExtensions(akc_mh, KCKindAuxiliary);
215*d8b80295SApple OSS Distributions 	}
216*d8b80295SApple OSS Distributions 
217*d8b80295SApple OSS Distributions 	loadKernelComponentKexts();
218*d8b80295SApple OSS Distributions 	loadKernelExternalComponents();
219*d8b80295SApple OSS Distributions 	readBuiltinPersonalities();
220*d8b80295SApple OSS Distributions 	OSKext::sendAllKextPersonalitiesToCatalog(true);
221*d8b80295SApple OSS Distributions 
222*d8b80295SApple OSS Distributions 	return;
223*d8b80295SApple OSS Distributions }
224*d8b80295SApple OSS Distributions 
225*d8b80295SApple OSS Distributions /*********************************************************************
226*d8b80295SApple OSS Distributions *********************************************************************/
227*d8b80295SApple OSS Distributions void
readPrelinkedExtensions(kernel_mach_header_t * mh,kc_kind_t type)228*d8b80295SApple OSS Distributions KLDBootstrap::readPrelinkedExtensions(kernel_mach_header_t *mh, kc_kind_t type)
229*d8b80295SApple OSS Distributions {
230*d8b80295SApple OSS Distributions 	bool ret;
231*d8b80295SApple OSS Distributions 	OSSharedPtr<OSData> loaded_kcUUID;
232*d8b80295SApple OSS Distributions 	OSSharedPtr<OSString> errorString;
233*d8b80295SApple OSS Distributions 	OSSharedPtr<OSObject> parsedXML;
234*d8b80295SApple OSS Distributions 	kernel_section_t *infoPlistSection = NULL;
235*d8b80295SApple OSS Distributions 	OSDictionary *infoDict = NULL;         // do not release
236*d8b80295SApple OSS Distributions 
237*d8b80295SApple OSS Distributions 	OSKextLog(/* kext */ NULL,
238*d8b80295SApple OSS Distributions 	    kOSKextLogProgressLevel |
239*d8b80295SApple OSS Distributions 	    kOSKextLogDirectoryScanFlag | kOSKextLogArchiveFlag,
240*d8b80295SApple OSS Distributions 	    "Starting from prelinked kernel.");
241*d8b80295SApple OSS Distributions 
242*d8b80295SApple OSS Distributions 	/*
243*d8b80295SApple OSS Distributions 	 * The 'infoPlistSection' should contains an XML dictionary that
244*d8b80295SApple OSS Distributions 	 * contains some meta data about the KC, and also describes each kext
245*d8b80295SApple OSS Distributions 	 * included in the kext collection. Unserialize this dictionary and
246*d8b80295SApple OSS Distributions 	 * then iterate over each kext.
247*d8b80295SApple OSS Distributions 	 */
248*d8b80295SApple OSS Distributions 	infoPlistSection = getsectbynamefromheader(mh, kPrelinkInfoSegment, kPrelinkInfoSection);
249*d8b80295SApple OSS Distributions 	parsedXML = OSUnserializeXML((const char *)infoPlistSection->addr, errorString);
250*d8b80295SApple OSS Distributions 	if (parsedXML) {
251*d8b80295SApple OSS Distributions 		infoDict = OSDynamicCast(OSDictionary, parsedXML.get());
252*d8b80295SApple OSS Distributions 	}
253*d8b80295SApple OSS Distributions 
254*d8b80295SApple OSS Distributions 	if (!infoDict) {
255*d8b80295SApple OSS Distributions 		const char *errorCString = "(unknown error)";
256*d8b80295SApple OSS Distributions 
257*d8b80295SApple OSS Distributions 		if (errorString && errorString->getCStringNoCopy()) {
258*d8b80295SApple OSS Distributions 			errorCString = errorString->getCStringNoCopy();
259*d8b80295SApple OSS Distributions 		} else if (parsedXML) {
260*d8b80295SApple OSS Distributions 			errorCString = "not a dictionary";
261*d8b80295SApple OSS Distributions 		}
262*d8b80295SApple OSS Distributions 		OSKextLog(/* kext */ NULL, kOSKextLogErrorLevel | kOSKextLogArchiveFlag,
263*d8b80295SApple OSS Distributions 		    "Error unserializing kext info plist section: %s.", errorCString);
264*d8b80295SApple OSS Distributions 		return;
265*d8b80295SApple OSS Distributions 	}
266*d8b80295SApple OSS Distributions 
267*d8b80295SApple OSS Distributions 	/* Validate that the Kext Collection is prelinked to the loaded KC */
268*d8b80295SApple OSS Distributions 	if (type == KCKindAuxiliary) {
269*d8b80295SApple OSS Distributions 		if (OSKext::validateKCFileSetUUID(infoDict, KCKindAuxiliary) != 0) {
270*d8b80295SApple OSS Distributions 			OSKextLog(/* kext */ NULL, kOSKextLogErrorLevel | kOSKextLogArchiveFlag,
271*d8b80295SApple OSS Distributions 			    "Early boot AuxKC  doesn't appear to be linked against the loaded BootKC.");
272*d8b80295SApple OSS Distributions 			return;
273*d8b80295SApple OSS Distributions 		}
274*d8b80295SApple OSS Distributions 
275*d8b80295SApple OSS Distributions 		/*
276*d8b80295SApple OSS Distributions 		 * Defer further processing of the AuxKC, but keep the
277*d8b80295SApple OSS Distributions 		 * processed info dictionary around so we can ml_static_free
278*d8b80295SApple OSS Distributions 		 * the segment.
279*d8b80295SApple OSS Distributions 		 */
280*d8b80295SApple OSS Distributions 		if (!OSKext::registerDeferredKextCollection(mh, parsedXML, KCKindAuxiliary)) {
281*d8b80295SApple OSS Distributions 			OSKextLog(/* kext */ NULL, kOSKextLogErrorLevel | kOSKextLogArchiveFlag,
282*d8b80295SApple OSS Distributions 			    "Error deferring AuxKC kext processing: Kexts in this collection will be unusable.");
283*d8b80295SApple OSS Distributions 		}
284*d8b80295SApple OSS Distributions 		goto skip_adding_kexts;
285*d8b80295SApple OSS Distributions 	}
286*d8b80295SApple OSS Distributions 
287*d8b80295SApple OSS Distributions 	/*
288*d8b80295SApple OSS Distributions 	 * this function does all the heavy lifting of adding OSKext objects
289*d8b80295SApple OSS Distributions 	 * and potentially sliding them if necessary
290*d8b80295SApple OSS Distributions 	 */
291*d8b80295SApple OSS Distributions 	ret = OSKext::addKextsFromKextCollection(mh, infoDict,
292*d8b80295SApple OSS Distributions 	    kPrelinkTextSegment, loaded_kcUUID, (mh->filetype == MH_FILESET) ? type : KCKindUnknown);
293*d8b80295SApple OSS Distributions 
294*d8b80295SApple OSS Distributions 	if (!ret) {
295*d8b80295SApple OSS Distributions 		OSKextLog(/* kext */ NULL, kOSKextLogErrorLevel | kOSKextLogArchiveFlag,
296*d8b80295SApple OSS Distributions 		    "Error loading kext info from prelinked primary KC");
297*d8b80295SApple OSS Distributions 		return;
298*d8b80295SApple OSS Distributions 	}
299*d8b80295SApple OSS Distributions 
300*d8b80295SApple OSS Distributions 	/* Copy in the kernelcache UUID */
301*d8b80295SApple OSS Distributions 	if (!loaded_kcUUID) {
302*d8b80295SApple OSS Distributions 		OSKextLog(/* kext */ NULL, kOSKextLogErrorLevel | kOSKextLogArchiveFlag,
303*d8b80295SApple OSS Distributions 		    "WARNING: did not find UUID in %s KC!", (type == KCKindAuxiliary) ? "Aux" : "Primary");
304*d8b80295SApple OSS Distributions 	} else if (type != KCKindAuxiliary) {
305*d8b80295SApple OSS Distributions 		kernelcache_uuid_valid = TRUE;
306*d8b80295SApple OSS Distributions 		memcpy((void *)&kernelcache_uuid, (const void *)loaded_kcUUID->getBytesNoCopy(), loaded_kcUUID->getLength());
307*d8b80295SApple OSS Distributions 		uuid_unparse_upper(kernelcache_uuid, kernelcache_uuid_string);
308*d8b80295SApple OSS Distributions 	} else {
309*d8b80295SApple OSS Distributions 		auxkc_uuid_valid = TRUE;
310*d8b80295SApple OSS Distributions 		memcpy((void *)&auxkc_uuid, (const void *)loaded_kcUUID->getBytesNoCopy(), loaded_kcUUID->getLength());
311*d8b80295SApple OSS Distributions 		uuid_unparse_upper(auxkc_uuid, auxkc_uuid_string);
312*d8b80295SApple OSS Distributions 	}
313*d8b80295SApple OSS Distributions 
314*d8b80295SApple OSS Distributions skip_adding_kexts:
315*d8b80295SApple OSS Distributions #if CONFIG_KEXT_BASEMENT
316*d8b80295SApple OSS Distributions 	if (mh->filetype != MH_FILESET) {
317*d8b80295SApple OSS Distributions 		/*
318*d8b80295SApple OSS Distributions 		 * On CONFIG_KEXT_BASEMENT systems which do _not_ boot the new
319*d8b80295SApple OSS Distributions 		 * MH_FILESET kext collection, kexts are copied to their own
320*d8b80295SApple OSS Distributions 		 * special VM region during OSKext init time, so we can free
321*d8b80295SApple OSS Distributions 		 * the whole segment now.
322*d8b80295SApple OSS Distributions 		 */
323*d8b80295SApple OSS Distributions 		kernel_segment_command_t *prelinkTextSegment = NULL;
324*d8b80295SApple OSS Distributions 		prelinkTextSegment = getsegbyname(kPrelinkTextSegment);
325*d8b80295SApple OSS Distributions 		if (!prelinkTextSegment) {
326*d8b80295SApple OSS Distributions 			OSKextLog(/* kext */ NULL,
327*d8b80295SApple OSS Distributions 			    kOSKextLogErrorLevel | kOSKextLogArchiveFlag,
328*d8b80295SApple OSS Distributions 			    "Can't find prelinked kexts' text segment.");
329*d8b80295SApple OSS Distributions 			return;
330*d8b80295SApple OSS Distributions 		}
331*d8b80295SApple OSS Distributions 
332*d8b80295SApple OSS Distributions 		ml_static_mfree((vm_offset_t)prelinkTextSegment->vmaddr, prelinkTextSegment->vmsize);
333*d8b80295SApple OSS Distributions 	}
334*d8b80295SApple OSS Distributions #endif /* CONFIG_KEXT_BASEMENT */
335*d8b80295SApple OSS Distributions 
336*d8b80295SApple OSS Distributions 	/*
337*d8b80295SApple OSS Distributions 	 * Free the prelink info segment, we're done with it.
338*d8b80295SApple OSS Distributions 	 */
339*d8b80295SApple OSS Distributions 
340*d8b80295SApple OSS Distributions #if !XNU_TARGET_OS_OSX
341*d8b80295SApple OSS Distributions 	/*
342*d8b80295SApple OSS Distributions 	 * For now, we are limiting this freeing to embedded platforms.
343*d8b80295SApple OSS Distributions 	 * To enable freeing of prelink info segment on macOS, we need to
344*d8b80295SApple OSS Distributions 	 * fix rdar://88929016
345*d8b80295SApple OSS Distributions 	 */
346*d8b80295SApple OSS Distributions 	bool freedPrelinkInfo = false;
347*d8b80295SApple OSS Distributions 	kernel_segment_command_t *prelinkInfoSegment = NULL;
348*d8b80295SApple OSS Distributions 	prelinkInfoSegment = getsegbynamefromheader(mh, kPrelinkInfoSegment);
349*d8b80295SApple OSS Distributions 	if (prelinkInfoSegment) {
350*d8b80295SApple OSS Distributions 		if (prelinkInfoSegment->vmsize != 0) {
351*d8b80295SApple OSS Distributions 			freedPrelinkInfo = true;
352*d8b80295SApple OSS Distributions 			ml_static_mfree((vm_offset_t)prelinkInfoSegment->vmaddr,
353*d8b80295SApple OSS Distributions 			    (vm_size_t)prelinkInfoSegment->vmsize);
354*d8b80295SApple OSS Distributions 		}
355*d8b80295SApple OSS Distributions 	}
356*d8b80295SApple OSS Distributions 
357*d8b80295SApple OSS Distributions 	if (!freedPrelinkInfo) {
358*d8b80295SApple OSS Distributions 		OSKextLog(NULL, kOSKextLogErrorLevel | kOSKextLogArchiveFlag, "Failed to free prelink info.");
359*d8b80295SApple OSS Distributions 	}
360*d8b80295SApple OSS Distributions #endif
361*d8b80295SApple OSS Distributions 	return;
362*d8b80295SApple OSS Distributions }
363*d8b80295SApple OSS Distributions 
364*d8b80295SApple OSS Distributions 
365*d8b80295SApple OSS Distributions /*********************************************************************
366*d8b80295SApple OSS Distributions *********************************************************************/
367*d8b80295SApple OSS Distributions #define BOOTER_KEXT_PREFIX   "Driver-"
368*d8b80295SApple OSS Distributions 
369*d8b80295SApple OSS Distributions typedef struct _DeviceTreeBuffer {
370*d8b80295SApple OSS Distributions 	uint32_t paddr;
371*d8b80295SApple OSS Distributions 	uint32_t length;
372*d8b80295SApple OSS Distributions } _DeviceTreeBuffer;
373*d8b80295SApple OSS Distributions 
374*d8b80295SApple OSS Distributions void
readBooterExtensions(void)375*d8b80295SApple OSS Distributions KLDBootstrap::readBooterExtensions(void)
376*d8b80295SApple OSS Distributions {
377*d8b80295SApple OSS Distributions 	OSSharedPtr<IORegistryEntry> booterMemoryMap;
378*d8b80295SApple OSS Distributions 	OSSharedPtr<OSDictionary>    propertyDict;
379*d8b80295SApple OSS Distributions 	OSSharedPtr<OSCollectionIterator>      keyIterator;
380*d8b80295SApple OSS Distributions 	OSString                  * deviceTreeName          = NULL;// do not release
381*d8b80295SApple OSS Distributions 
382*d8b80295SApple OSS Distributions 	const _DeviceTreeBuffer   * deviceTreeBuffer        = NULL;// do not free
383*d8b80295SApple OSS Distributions 	char                      * booterDataPtr           = NULL;// do not free
384*d8b80295SApple OSS Distributions 	OSSharedPtr<OSData>         booterData;
385*d8b80295SApple OSS Distributions 	OSSharedPtr<OSKext>         aKext;
386*d8b80295SApple OSS Distributions 
387*d8b80295SApple OSS Distributions 	OSKextLog(/* kext */ NULL,
388*d8b80295SApple OSS Distributions 	    kOSKextLogProgressLevel |
389*d8b80295SApple OSS Distributions 	    kOSKextLogDirectoryScanFlag | kOSKextLogKextBookkeepingFlag,
390*d8b80295SApple OSS Distributions 	    "Reading startup extensions from booter memory.");
391*d8b80295SApple OSS Distributions 
392*d8b80295SApple OSS Distributions 	booterMemoryMap = IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane);
393*d8b80295SApple OSS Distributions 
394*d8b80295SApple OSS Distributions 	if (!booterMemoryMap) {
395*d8b80295SApple OSS Distributions 		OSKextLog(/* kext */ NULL,
396*d8b80295SApple OSS Distributions 		    kOSKextLogErrorLevel |
397*d8b80295SApple OSS Distributions 		    kOSKextLogGeneralFlag | kOSKextLogDirectoryScanFlag,
398*d8b80295SApple OSS Distributions 		    "Can't read booter memory map.");
399*d8b80295SApple OSS Distributions 		goto finish;
400*d8b80295SApple OSS Distributions 	}
401*d8b80295SApple OSS Distributions 
402*d8b80295SApple OSS Distributions 	propertyDict = booterMemoryMap->dictionaryWithProperties();
403*d8b80295SApple OSS Distributions 	if (!propertyDict) {
404*d8b80295SApple OSS Distributions 		OSKextLog(/* kext */ NULL,
405*d8b80295SApple OSS Distributions 		    kOSKextLogErrorLevel |
406*d8b80295SApple OSS Distributions 		    kOSKextLogDirectoryScanFlag,
407*d8b80295SApple OSS Distributions 		    "Can't get property dictionary from memory map.");
408*d8b80295SApple OSS Distributions 		goto finish;
409*d8b80295SApple OSS Distributions 	}
410*d8b80295SApple OSS Distributions 
411*d8b80295SApple OSS Distributions 	keyIterator = OSCollectionIterator::withCollection(propertyDict.get());
412*d8b80295SApple OSS Distributions 	if (!keyIterator) {
413*d8b80295SApple OSS Distributions 		OSKextLog(/* kext */ NULL,
414*d8b80295SApple OSS Distributions 		    kOSKextLogErrorLevel |
415*d8b80295SApple OSS Distributions 		    kOSKextLogGeneralFlag,
416*d8b80295SApple OSS Distributions 		    "Can't allocate iterator for driver images.");
417*d8b80295SApple OSS Distributions 		goto finish;
418*d8b80295SApple OSS Distributions 	}
419*d8b80295SApple OSS Distributions 
420*d8b80295SApple OSS Distributions 	/* Create dictionary of excluded kexts
421*d8b80295SApple OSS Distributions 	 */
422*d8b80295SApple OSS Distributions #ifndef CONFIG_EMBEDDED
423*d8b80295SApple OSS Distributions 	OSKext::createExcludeListFromBooterData(propertyDict.get(), keyIterator.get());
424*d8b80295SApple OSS Distributions #endif
425*d8b80295SApple OSS Distributions 	// !! reset the iterator, not the pointer
426*d8b80295SApple OSS Distributions 	keyIterator->reset();
427*d8b80295SApple OSS Distributions 
428*d8b80295SApple OSS Distributions 	while ((deviceTreeName =
429*d8b80295SApple OSS Distributions 	    OSDynamicCast(OSString, keyIterator->getNextObject()))) {
430*d8b80295SApple OSS Distributions 		const char * devTreeNameCString = deviceTreeName->getCStringNoCopy();
431*d8b80295SApple OSS Distributions 		OSData * deviceTreeEntry = OSDynamicCast(OSData,
432*d8b80295SApple OSS Distributions 		    propertyDict->getObject(deviceTreeName));
433*d8b80295SApple OSS Distributions 
434*d8b80295SApple OSS Distributions 		/* If there is no entry for the name, we can't do much with it. */
435*d8b80295SApple OSS Distributions 		if (!deviceTreeEntry) {
436*d8b80295SApple OSS Distributions 			continue;
437*d8b80295SApple OSS Distributions 		}
438*d8b80295SApple OSS Distributions 
439*d8b80295SApple OSS Distributions 		/* Make sure it is a kext */
440*d8b80295SApple OSS Distributions 		if (strncmp(devTreeNameCString,
441*d8b80295SApple OSS Distributions 		    BOOTER_KEXT_PREFIX,
442*d8b80295SApple OSS Distributions 		    CONST_STRLEN(BOOTER_KEXT_PREFIX))) {
443*d8b80295SApple OSS Distributions 			continue;
444*d8b80295SApple OSS Distributions 		}
445*d8b80295SApple OSS Distributions 
446*d8b80295SApple OSS Distributions 		deviceTreeBuffer = (const _DeviceTreeBuffer *)
447*d8b80295SApple OSS Distributions 		    deviceTreeEntry->getBytesNoCopy(0, sizeof(deviceTreeBuffer));
448*d8b80295SApple OSS Distributions 		if (!deviceTreeBuffer) {
449*d8b80295SApple OSS Distributions 			/* We can't get to the data, so we can't do anything,
450*d8b80295SApple OSS Distributions 			 * not even free it from physical memory (if it's there).
451*d8b80295SApple OSS Distributions 			 */
452*d8b80295SApple OSS Distributions 			OSKextLog(/* kext */ NULL,
453*d8b80295SApple OSS Distributions 			    kOSKextLogErrorLevel |
454*d8b80295SApple OSS Distributions 			    kOSKextLogDirectoryScanFlag,
455*d8b80295SApple OSS Distributions 			    "Device tree entry %s has NULL pointer.",
456*d8b80295SApple OSS Distributions 			    devTreeNameCString);
457*d8b80295SApple OSS Distributions 			goto finish; // xxx - continue, panic?
458*d8b80295SApple OSS Distributions 		}
459*d8b80295SApple OSS Distributions 
460*d8b80295SApple OSS Distributions 		booterDataPtr = (char *)ml_static_ptovirt(deviceTreeBuffer->paddr);
461*d8b80295SApple OSS Distributions 		if (!booterDataPtr) {
462*d8b80295SApple OSS Distributions 			OSKextLog(/* kext */ NULL,
463*d8b80295SApple OSS Distributions 			    kOSKextLogErrorLevel |
464*d8b80295SApple OSS Distributions 			    kOSKextLogDirectoryScanFlag,
465*d8b80295SApple OSS Distributions 			    "Can't get virtual address for device tree entry %s.",
466*d8b80295SApple OSS Distributions 			    devTreeNameCString);
467*d8b80295SApple OSS Distributions 			goto finish;
468*d8b80295SApple OSS Distributions 		}
469*d8b80295SApple OSS Distributions 
470*d8b80295SApple OSS Distributions 		/* Wrap the booter data buffer in an OSData and set a dealloc function
471*d8b80295SApple OSS Distributions 		 * so it will take care of the physical memory when freed. Kexts will
472*d8b80295SApple OSS Distributions 		 * retain the booterData for as long as they need it. Remove the entry
473*d8b80295SApple OSS Distributions 		 * from the booter memory map after this is done.
474*d8b80295SApple OSS Distributions 		 */
475*d8b80295SApple OSS Distributions 		booterData = OSData::withBytesNoCopy(booterDataPtr,
476*d8b80295SApple OSS Distributions 		    deviceTreeBuffer->length);
477*d8b80295SApple OSS Distributions 		if (!booterData) {
478*d8b80295SApple OSS Distributions 			OSKextLog(/* kext */ NULL,
479*d8b80295SApple OSS Distributions 			    kOSKextLogErrorLevel |
480*d8b80295SApple OSS Distributions 			    kOSKextLogGeneralFlag,
481*d8b80295SApple OSS Distributions 			    "Error - Can't allocate OSData wrapper for device tree entry %s.",
482*d8b80295SApple OSS Distributions 			    devTreeNameCString);
483*d8b80295SApple OSS Distributions 			goto finish;
484*d8b80295SApple OSS Distributions 		}
485*d8b80295SApple OSS Distributions 		booterData->setDeallocFunction(osdata_phys_free);
486*d8b80295SApple OSS Distributions 
487*d8b80295SApple OSS Distributions 		/* Create the kext for the entry, then release it, because the
488*d8b80295SApple OSS Distributions 		 * kext system keeps them around until explicitly removed.
489*d8b80295SApple OSS Distributions 		 * Any creation/registration failures are already logged for us.
490*d8b80295SApple OSS Distributions 		 */
491*d8b80295SApple OSS Distributions 		OSSharedPtr<OSKext> newKext = OSKext::withBooterData(deviceTreeName, booterData.get());
492*d8b80295SApple OSS Distributions 
493*d8b80295SApple OSS Distributions 		booterMemoryMap->removeProperty(deviceTreeName);
494*d8b80295SApple OSS Distributions 	} /* while ( (deviceTreeName = OSDynamicCast(OSString, ...) ) ) */
495*d8b80295SApple OSS Distributions 
496*d8b80295SApple OSS Distributions finish:
497*d8b80295SApple OSS Distributions 	return;
498*d8b80295SApple OSS Distributions }
499*d8b80295SApple OSS Distributions 
500*d8b80295SApple OSS Distributions /*********************************************************************
501*d8b80295SApple OSS Distributions *********************************************************************/
502*d8b80295SApple OSS Distributions #define COM_APPLE  "com.apple."
503*d8b80295SApple OSS Distributions 
504*d8b80295SApple OSS Distributions void
loadSecurityExtensions(void)505*d8b80295SApple OSS Distributions KLDBootstrap::loadSecurityExtensions(void)
506*d8b80295SApple OSS Distributions {
507*d8b80295SApple OSS Distributions 	OSSharedPtr<OSDictionary>         extensionsDict;
508*d8b80295SApple OSS Distributions 	OSSharedPtr<OSCollectionIterator> keyIterator;
509*d8b80295SApple OSS Distributions 	OSString             * bundleID       = NULL;// don't release
510*d8b80295SApple OSS Distributions 	OSKext               * theKext        = NULL;// don't release
511*d8b80295SApple OSS Distributions 
512*d8b80295SApple OSS Distributions 	OSKextLog(/* kext */ NULL,
513*d8b80295SApple OSS Distributions 	    kOSKextLogStepLevel |
514*d8b80295SApple OSS Distributions 	    kOSKextLogLoadFlag,
515*d8b80295SApple OSS Distributions 	    "Loading security extensions.");
516*d8b80295SApple OSS Distributions 
517*d8b80295SApple OSS Distributions 	extensionsDict = OSKext::copyKexts();
518*d8b80295SApple OSS Distributions 	if (!extensionsDict) {
519*d8b80295SApple OSS Distributions 		return;
520*d8b80295SApple OSS Distributions 	}
521*d8b80295SApple OSS Distributions 
522*d8b80295SApple OSS Distributions 	keyIterator = OSCollectionIterator::withCollection(extensionsDict.get());
523*d8b80295SApple OSS Distributions 	if (!keyIterator) {
524*d8b80295SApple OSS Distributions 		OSKextLog(/* kext */ NULL,
525*d8b80295SApple OSS Distributions 		    kOSKextLogErrorLevel |
526*d8b80295SApple OSS Distributions 		    kOSKextLogGeneralFlag,
527*d8b80295SApple OSS Distributions 		    "Failed to allocate iterator for security extensions.");
528*d8b80295SApple OSS Distributions 		goto finish;
529*d8b80295SApple OSS Distributions 	}
530*d8b80295SApple OSS Distributions 
531*d8b80295SApple OSS Distributions 	while ((bundleID = OSDynamicCast(OSString, keyIterator->getNextObject()))) {
532*d8b80295SApple OSS Distributions 		const char * bundle_id = bundleID->getCStringNoCopy();
533*d8b80295SApple OSS Distributions 
534*d8b80295SApple OSS Distributions 		/* Skip extensions whose bundle IDs don't start with "com.apple.".
535*d8b80295SApple OSS Distributions 		 */
536*d8b80295SApple OSS Distributions 		if (!bundle_id ||
537*d8b80295SApple OSS Distributions 		    (strncmp(bundle_id, COM_APPLE, CONST_STRLEN(COM_APPLE)) != 0)) {
538*d8b80295SApple OSS Distributions 			continue;
539*d8b80295SApple OSS Distributions 		}
540*d8b80295SApple OSS Distributions 
541*d8b80295SApple OSS Distributions 		theKext = OSDynamicCast(OSKext, extensionsDict->getObject(bundleID));
542*d8b80295SApple OSS Distributions 		if (!theKext) {
543*d8b80295SApple OSS Distributions 			continue;
544*d8b80295SApple OSS Distributions 		}
545*d8b80295SApple OSS Distributions 
546*d8b80295SApple OSS Distributions 		if (kOSBooleanTrue == theKext->getPropertyForHostArch(kAppleSecurityExtensionKey)) {
547*d8b80295SApple OSS Distributions 			OSKextLog(/* kext */ NULL,
548*d8b80295SApple OSS Distributions 			    kOSKextLogStepLevel |
549*d8b80295SApple OSS Distributions 			    kOSKextLogLoadFlag,
550*d8b80295SApple OSS Distributions 			    "Loading security extension %s.", bundleID->getCStringNoCopy());
551*d8b80295SApple OSS Distributions 			OSKext::loadKextWithIdentifier(bundleID->getCStringNoCopy(),
552*d8b80295SApple OSS Distributions 			    /* allowDefer */ false);
553*d8b80295SApple OSS Distributions 		}
554*d8b80295SApple OSS Distributions 	}
555*d8b80295SApple OSS Distributions 
556*d8b80295SApple OSS Distributions finish:
557*d8b80295SApple OSS Distributions 	return;
558*d8b80295SApple OSS Distributions }
559*d8b80295SApple OSS Distributions 
560*d8b80295SApple OSS Distributions /*********************************************************************
561*d8b80295SApple OSS Distributions * We used to require that all listed kernel components load, but
562*d8b80295SApple OSS Distributions * nowadays we can get them from userland so we only try to load the
563*d8b80295SApple OSS Distributions * ones we have. If an error occurs later, such is life.
564*d8b80295SApple OSS Distributions *
565*d8b80295SApple OSS Distributions * Note that we look the kexts up first, so we can avoid spurious
566*d8b80295SApple OSS Distributions * (in this context, anyhow) log messages about kexts not being found.
567*d8b80295SApple OSS Distributions *
568*d8b80295SApple OSS Distributions * xxx - do we even need to do this any more? Check if the kernel
569*d8b80295SApple OSS Distributions * xxx - compoonents just load in the regular paths
570*d8b80295SApple OSS Distributions *********************************************************************/
571*d8b80295SApple OSS Distributions OSReturn
loadKernelComponentKexts(void)572*d8b80295SApple OSS Distributions KLDBootstrap::loadKernelComponentKexts(void)
573*d8b80295SApple OSS Distributions {
574*d8b80295SApple OSS Distributions 	OSReturn            result      = kOSReturnSuccess;// optimistic
575*d8b80295SApple OSS Distributions 	OSSharedPtr<OSKext> theKext;
576*d8b80295SApple OSS Distributions 	const char       ** kextIDPtr   = NULL;          // do not release
577*d8b80295SApple OSS Distributions 
578*d8b80295SApple OSS Distributions 	for (kextIDPtr = &sKernelComponentNames[0]; *kextIDPtr; kextIDPtr++) {
579*d8b80295SApple OSS Distributions 		theKext = OSKext::lookupKextWithIdentifier(*kextIDPtr);
580*d8b80295SApple OSS Distributions 
581*d8b80295SApple OSS Distributions 		if (theKext) {
582*d8b80295SApple OSS Distributions 			if (kOSReturnSuccess != OSKext::loadKextWithIdentifier(
583*d8b80295SApple OSS Distributions 				    *kextIDPtr, /* allowDefer */ false)) {
584*d8b80295SApple OSS Distributions 				// xxx - check KextBookkeeping, might be redundant
585*d8b80295SApple OSS Distributions 				OSKextLog(/* kext */ NULL,
586*d8b80295SApple OSS Distributions 				    kOSKextLogErrorLevel |
587*d8b80295SApple OSS Distributions 				    kOSKextLogDirectoryScanFlag | kOSKextLogKextBookkeepingFlag,
588*d8b80295SApple OSS Distributions 				    "Failed to initialize kernel component %s.", *kextIDPtr);
589*d8b80295SApple OSS Distributions 				result = kOSReturnError;
590*d8b80295SApple OSS Distributions 			}
591*d8b80295SApple OSS Distributions 		}
592*d8b80295SApple OSS Distributions 	}
593*d8b80295SApple OSS Distributions 
594*d8b80295SApple OSS Distributions 	return result;
595*d8b80295SApple OSS Distributions }
596*d8b80295SApple OSS Distributions 
597*d8b80295SApple OSS Distributions /*********************************************************************
598*d8b80295SApple OSS Distributions * Ensure that Kernel External Components are loaded early in boot,
599*d8b80295SApple OSS Distributions * before other kext personalities get sent to the IOCatalogue. These
600*d8b80295SApple OSS Distributions * kexts are treated specially because they may provide the implementation
601*d8b80295SApple OSS Distributions * for kernel-vended KPI, so they must register themselves before
602*d8b80295SApple OSS Distributions * general purpose IOKit probing begins.
603*d8b80295SApple OSS Distributions *********************************************************************/
604*d8b80295SApple OSS Distributions 
605*d8b80295SApple OSS Distributions #define COM_APPLE_KEC  "com.apple.kec."
606*d8b80295SApple OSS Distributions 
607*d8b80295SApple OSS Distributions void
loadKernelExternalComponents(void)608*d8b80295SApple OSS Distributions KLDBootstrap::loadKernelExternalComponents(void)
609*d8b80295SApple OSS Distributions {
610*d8b80295SApple OSS Distributions 	OSSharedPtr<OSDictionary>         extensionsDict;
611*d8b80295SApple OSS Distributions 	OSSharedPtr<OSCollectionIterator> keyIterator;
612*d8b80295SApple OSS Distributions 	OSString             * bundleID       = NULL;// don't release
613*d8b80295SApple OSS Distributions 	OSKext               * theKext        = NULL;// don't release
614*d8b80295SApple OSS Distributions 	OSBoolean            * isKernelExternalComponent = NULL;// don't release
615*d8b80295SApple OSS Distributions 
616*d8b80295SApple OSS Distributions 	OSKextLog(/* kext */ NULL,
617*d8b80295SApple OSS Distributions 	    kOSKextLogStepLevel |
618*d8b80295SApple OSS Distributions 	    kOSKextLogLoadFlag,
619*d8b80295SApple OSS Distributions 	    "Loading Kernel External Components.");
620*d8b80295SApple OSS Distributions 
621*d8b80295SApple OSS Distributions 	extensionsDict = OSKext::copyKexts();
622*d8b80295SApple OSS Distributions 	if (!extensionsDict) {
623*d8b80295SApple OSS Distributions 		return;
624*d8b80295SApple OSS Distributions 	}
625*d8b80295SApple OSS Distributions 
626*d8b80295SApple OSS Distributions 	keyIterator = OSCollectionIterator::withCollection(extensionsDict.get());
627*d8b80295SApple OSS Distributions 	if (!keyIterator) {
628*d8b80295SApple OSS Distributions 		OSKextLog(/* kext */ NULL,
629*d8b80295SApple OSS Distributions 		    kOSKextLogErrorLevel |
630*d8b80295SApple OSS Distributions 		    kOSKextLogGeneralFlag,
631*d8b80295SApple OSS Distributions 		    "Failed to allocate iterator for Kernel External Components.");
632*d8b80295SApple OSS Distributions 		goto finish;
633*d8b80295SApple OSS Distributions 	}
634*d8b80295SApple OSS Distributions 
635*d8b80295SApple OSS Distributions 	while ((bundleID = OSDynamicCast(OSString, keyIterator->getNextObject()))) {
636*d8b80295SApple OSS Distributions 		const char * bundle_id = bundleID->getCStringNoCopy();
637*d8b80295SApple OSS Distributions 
638*d8b80295SApple OSS Distributions 		/* Skip extensions whose bundle IDs don't start with "com.apple.kec.".
639*d8b80295SApple OSS Distributions 		 */
640*d8b80295SApple OSS Distributions 		if (!bundle_id ||
641*d8b80295SApple OSS Distributions 		    (strncmp(bundle_id, COM_APPLE_KEC, CONST_STRLEN(COM_APPLE_KEC)) != 0)) {
642*d8b80295SApple OSS Distributions 			continue;
643*d8b80295SApple OSS Distributions 		}
644*d8b80295SApple OSS Distributions 
645*d8b80295SApple OSS Distributions 		theKext = OSDynamicCast(OSKext, extensionsDict->getObject(bundleID));
646*d8b80295SApple OSS Distributions 		if (!theKext) {
647*d8b80295SApple OSS Distributions 			continue;
648*d8b80295SApple OSS Distributions 		}
649*d8b80295SApple OSS Distributions 
650*d8b80295SApple OSS Distributions 		isKernelExternalComponent = OSDynamicCast(OSBoolean,
651*d8b80295SApple OSS Distributions 		    theKext->getPropertyForHostArch(kAppleKernelExternalComponentKey));
652*d8b80295SApple OSS Distributions 		if (isKernelExternalComponent && isKernelExternalComponent->isTrue()) {
653*d8b80295SApple OSS Distributions 			OSKextLog(/* kext */ NULL,
654*d8b80295SApple OSS Distributions 			    kOSKextLogStepLevel |
655*d8b80295SApple OSS Distributions 			    kOSKextLogLoadFlag,
656*d8b80295SApple OSS Distributions 			    "Loading kernel external component %s.", bundleID->getCStringNoCopy());
657*d8b80295SApple OSS Distributions 			OSKext::loadKextWithIdentifier(bundleID->getCStringNoCopy(),
658*d8b80295SApple OSS Distributions 			    /* allowDefer */ false);
659*d8b80295SApple OSS Distributions 		}
660*d8b80295SApple OSS Distributions 	}
661*d8b80295SApple OSS Distributions 
662*d8b80295SApple OSS Distributions finish:
663*d8b80295SApple OSS Distributions 	return;
664*d8b80295SApple OSS Distributions }
665*d8b80295SApple OSS Distributions 
666*d8b80295SApple OSS Distributions /*********************************************************************
667*d8b80295SApple OSS Distributions *********************************************************************/
668*d8b80295SApple OSS Distributions void
readBuiltinPersonalities(void)669*d8b80295SApple OSS Distributions KLDBootstrap::readBuiltinPersonalities(void)
670*d8b80295SApple OSS Distributions {
671*d8b80295SApple OSS Distributions 	OSSharedPtr<OSObject>   parsedXML;
672*d8b80295SApple OSS Distributions 	OSArray               * builtinExtensions     = NULL;// do not release
673*d8b80295SApple OSS Distributions 	OSSharedPtr<OSArray>    allPersonalities;
674*d8b80295SApple OSS Distributions 	OSSharedPtr<OSString>   errorString;
675*d8b80295SApple OSS Distributions 	kernel_section_t      * infosect              = NULL;// do not free
676*d8b80295SApple OSS Distributions 	OSSharedPtr<OSCollectionIterator>  personalitiesIterator;
677*d8b80295SApple OSS Distributions 	unsigned int            count, i;
678*d8b80295SApple OSS Distributions 
679*d8b80295SApple OSS Distributions 	OSKextLog(/* kext */ NULL,
680*d8b80295SApple OSS Distributions 	    kOSKextLogStepLevel |
681*d8b80295SApple OSS Distributions 	    kOSKextLogLoadFlag,
682*d8b80295SApple OSS Distributions 	    "Reading built-in kernel personalities for I/O Kit drivers.");
683*d8b80295SApple OSS Distributions 
684*d8b80295SApple OSS Distributions 	/* Look in the __BUILTIN __info segment for an array of Info.plist
685*d8b80295SApple OSS Distributions 	 * entries. For each one, extract the personalities dictionary, add
686*d8b80295SApple OSS Distributions 	 * it to our array, then push them all (without matching) to
687*d8b80295SApple OSS Distributions 	 * the IOCatalogue. This can be used to augment the personalities
688*d8b80295SApple OSS Distributions 	 * in gIOKernelConfigTables, especially when linking entire kexts into
689*d8b80295SApple OSS Distributions 	 * the mach_kernel image.
690*d8b80295SApple OSS Distributions 	 */
691*d8b80295SApple OSS Distributions 	infosect   = getsectbyname("__BUILTIN", "__info");
692*d8b80295SApple OSS Distributions 	if (!infosect) {
693*d8b80295SApple OSS Distributions 		// this isn't fatal
694*d8b80295SApple OSS Distributions 		goto finish;
695*d8b80295SApple OSS Distributions 	}
696*d8b80295SApple OSS Distributions 
697*d8b80295SApple OSS Distributions 	parsedXML = OSUnserializeXML((const char *) (uintptr_t)infosect->addr,
698*d8b80295SApple OSS Distributions 	    errorString);
699*d8b80295SApple OSS Distributions 	if (parsedXML) {
700*d8b80295SApple OSS Distributions 		builtinExtensions = OSDynamicCast(OSArray, parsedXML.get());
701*d8b80295SApple OSS Distributions 	}
702*d8b80295SApple OSS Distributions 	if (!builtinExtensions) {
703*d8b80295SApple OSS Distributions 		const char * errorCString = "(unknown error)";
704*d8b80295SApple OSS Distributions 
705*d8b80295SApple OSS Distributions 		if (errorString && errorString->getCStringNoCopy()) {
706*d8b80295SApple OSS Distributions 			errorCString = errorString->getCStringNoCopy();
707*d8b80295SApple OSS Distributions 		} else if (parsedXML) {
708*d8b80295SApple OSS Distributions 			errorCString = "not an array";
709*d8b80295SApple OSS Distributions 		}
710*d8b80295SApple OSS Distributions 		OSKextLog(/* kext */ NULL,
711*d8b80295SApple OSS Distributions 		    kOSKextLogErrorLevel |
712*d8b80295SApple OSS Distributions 		    kOSKextLogLoadFlag,
713*d8b80295SApple OSS Distributions 		    "Error unserializing built-in personalities: %s.", errorCString);
714*d8b80295SApple OSS Distributions 		goto finish;
715*d8b80295SApple OSS Distributions 	}
716*d8b80295SApple OSS Distributions 
717*d8b80295SApple OSS Distributions 	// estimate 3 personalities per Info.plist/kext
718*d8b80295SApple OSS Distributions 	count = builtinExtensions->getCount();
719*d8b80295SApple OSS Distributions 	allPersonalities = OSArray::withCapacity(count * 3);
720*d8b80295SApple OSS Distributions 
721*d8b80295SApple OSS Distributions 	for (i = 0; i < count; i++) {
722*d8b80295SApple OSS Distributions 		OSDictionary            * infoDict = NULL;// do not release
723*d8b80295SApple OSS Distributions 		OSString                * moduleName = NULL;// do not release
724*d8b80295SApple OSS Distributions 		OSDictionary            * personalities;// do not release
725*d8b80295SApple OSS Distributions 		OSString                * personalityName;// do not release
726*d8b80295SApple OSS Distributions 
727*d8b80295SApple OSS Distributions 		infoDict = OSDynamicCast(OSDictionary,
728*d8b80295SApple OSS Distributions 		    builtinExtensions->getObject(i));
729*d8b80295SApple OSS Distributions 		if (!infoDict) {
730*d8b80295SApple OSS Distributions 			continue;
731*d8b80295SApple OSS Distributions 		}
732*d8b80295SApple OSS Distributions 
733*d8b80295SApple OSS Distributions 		moduleName = OSDynamicCast(OSString,
734*d8b80295SApple OSS Distributions 		    infoDict->getObject(kCFBundleIdentifierKey));
735*d8b80295SApple OSS Distributions 		if (!moduleName) {
736*d8b80295SApple OSS Distributions 			continue;
737*d8b80295SApple OSS Distributions 		}
738*d8b80295SApple OSS Distributions 
739*d8b80295SApple OSS Distributions 		OSKextLog(/* kext */ NULL,
740*d8b80295SApple OSS Distributions 		    kOSKextLogStepLevel |
741*d8b80295SApple OSS Distributions 		    kOSKextLogLoadFlag,
742*d8b80295SApple OSS Distributions 		    "Adding personalities for built-in driver %s:",
743*d8b80295SApple OSS Distributions 		    moduleName->getCStringNoCopy());
744*d8b80295SApple OSS Distributions 
745*d8b80295SApple OSS Distributions 		personalities = OSDynamicCast(OSDictionary,
746*d8b80295SApple OSS Distributions 		    infoDict->getObject("IOKitPersonalities"));
747*d8b80295SApple OSS Distributions 		if (!personalities) {
748*d8b80295SApple OSS Distributions 			continue;
749*d8b80295SApple OSS Distributions 		}
750*d8b80295SApple OSS Distributions 
751*d8b80295SApple OSS Distributions 		personalitiesIterator = OSCollectionIterator::withCollection(personalities);
752*d8b80295SApple OSS Distributions 		if (!personalitiesIterator) {
753*d8b80295SApple OSS Distributions 			continue; // xxx - well really, what can we do? should we panic?
754*d8b80295SApple OSS Distributions 		}
755*d8b80295SApple OSS Distributions 
756*d8b80295SApple OSS Distributions 		while ((personalityName = OSDynamicCast(OSString,
757*d8b80295SApple OSS Distributions 		    personalitiesIterator->getNextObject()))) {
758*d8b80295SApple OSS Distributions 			OSDictionary * personality = OSDynamicCast(OSDictionary,
759*d8b80295SApple OSS Distributions 			    personalities->getObject(personalityName));
760*d8b80295SApple OSS Distributions 
761*d8b80295SApple OSS Distributions 			OSKextLog(/* kext */ NULL,
762*d8b80295SApple OSS Distributions 			    kOSKextLogDetailLevel |
763*d8b80295SApple OSS Distributions 			    kOSKextLogLoadFlag,
764*d8b80295SApple OSS Distributions 			    "Adding built-in driver personality %s.",
765*d8b80295SApple OSS Distributions 			    personalityName->getCStringNoCopy());
766*d8b80295SApple OSS Distributions 
767*d8b80295SApple OSS Distributions 			if (personality && !personality->getObject(kCFBundleIdentifierKey)) {
768*d8b80295SApple OSS Distributions 				personality->setObject(kCFBundleIdentifierKey, moduleName);
769*d8b80295SApple OSS Distributions 			}
770*d8b80295SApple OSS Distributions 			allPersonalities->setObject(personality);
771*d8b80295SApple OSS Distributions 		}
772*d8b80295SApple OSS Distributions 	}
773*d8b80295SApple OSS Distributions 
774*d8b80295SApple OSS Distributions 	gIOCatalogue->addDrivers(allPersonalities.get(), false);
775*d8b80295SApple OSS Distributions 
776*d8b80295SApple OSS Distributions finish:
777*d8b80295SApple OSS Distributions 	return;
778*d8b80295SApple OSS Distributions }
779*d8b80295SApple OSS Distributions 
780*d8b80295SApple OSS Distributions #if PRAGMA_MARK
781*d8b80295SApple OSS Distributions #pragma mark Bootstrap Functions
782*d8b80295SApple OSS Distributions #endif
783*d8b80295SApple OSS Distributions /*********************************************************************
784*d8b80295SApple OSS Distributions * Bootstrap Functions
785*d8b80295SApple OSS Distributions *********************************************************************/
786*d8b80295SApple OSS Distributions static void
bootstrapRecordStartupExtensions(void)787*d8b80295SApple OSS Distributions bootstrapRecordStartupExtensions(void)
788*d8b80295SApple OSS Distributions {
789*d8b80295SApple OSS Distributions 	sBootstrapObject.readStartupExtensions();
790*d8b80295SApple OSS Distributions 	return;
791*d8b80295SApple OSS Distributions }
792*d8b80295SApple OSS Distributions 
793*d8b80295SApple OSS Distributions static void
bootstrapLoadSecurityExtensions(void)794*d8b80295SApple OSS Distributions bootstrapLoadSecurityExtensions(void)
795*d8b80295SApple OSS Distributions {
796*d8b80295SApple OSS Distributions 	sBootstrapObject.loadSecurityExtensions();
797*d8b80295SApple OSS Distributions 	return;
798*d8b80295SApple OSS Distributions }
799*d8b80295SApple OSS Distributions 
800