xref: /xnu-8020.140.41/iokit/bsddev/IOKitBSDInit.cpp (revision 27b03b360a988dfd3dfdf34262bb0042026747cc)
1*27b03b36SApple OSS Distributions /*
2*27b03b36SApple OSS Distributions  * Copyright (c) 1998-2021 Apple Inc. All rights reserved.
3*27b03b36SApple OSS Distributions  *
4*27b03b36SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*27b03b36SApple OSS Distributions  *
6*27b03b36SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*27b03b36SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*27b03b36SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*27b03b36SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*27b03b36SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*27b03b36SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*27b03b36SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*27b03b36SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*27b03b36SApple OSS Distributions  *
15*27b03b36SApple OSS Distributions  * Please obtain a copy of the License at
16*27b03b36SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*27b03b36SApple OSS Distributions  *
18*27b03b36SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*27b03b36SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*27b03b36SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*27b03b36SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*27b03b36SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*27b03b36SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*27b03b36SApple OSS Distributions  * limitations under the License.
25*27b03b36SApple OSS Distributions  *
26*27b03b36SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*27b03b36SApple OSS Distributions  */
28*27b03b36SApple OSS Distributions #include <IOKit/IOBSD.h>
29*27b03b36SApple OSS Distributions #include <IOKit/IOLib.h>
30*27b03b36SApple OSS Distributions #include <IOKit/IOService.h>
31*27b03b36SApple OSS Distributions #include <IOKit/IOCatalogue.h>
32*27b03b36SApple OSS Distributions #include <IOKit/IODeviceTreeSupport.h>
33*27b03b36SApple OSS Distributions #include <IOKit/IOKitKeys.h>
34*27b03b36SApple OSS Distributions #include <IOKit/IONVRAM.h>
35*27b03b36SApple OSS Distributions #include <IOKit/IOPlatformExpert.h>
36*27b03b36SApple OSS Distributions #include <IOKit/IOUserClient.h>
37*27b03b36SApple OSS Distributions #include <libkern/c++/OSAllocation.h>
38*27b03b36SApple OSS Distributions 
39*27b03b36SApple OSS Distributions extern "C" {
40*27b03b36SApple OSS Distributions #include <libkern/amfi/amfi.h>
41*27b03b36SApple OSS Distributions #include <sys/codesign.h>
42*27b03b36SApple OSS Distributions #include <vm/pmap.h>
43*27b03b36SApple OSS Distributions #include <vm/vm_map.h>
44*27b03b36SApple OSS Distributions #include <pexpert/pexpert.h>
45*27b03b36SApple OSS Distributions #include <kern/clock.h>
46*27b03b36SApple OSS Distributions #if CONFIG_KDP_INTERACTIVE_DEBUGGING
47*27b03b36SApple OSS Distributions #include <kern/debug.h>
48*27b03b36SApple OSS Distributions #endif
49*27b03b36SApple OSS Distributions #include <mach/machine.h>
50*27b03b36SApple OSS Distributions #include <uuid/uuid.h>
51*27b03b36SApple OSS Distributions #include <sys/vnode_internal.h>
52*27b03b36SApple OSS Distributions #include <sys/mount.h>
53*27b03b36SApple OSS Distributions 
54*27b03b36SApple OSS Distributions // how long to wait for matching root device, secs
55*27b03b36SApple OSS Distributions #if DEBUG
56*27b03b36SApple OSS Distributions #define ROOTDEVICETIMEOUT       120
57*27b03b36SApple OSS Distributions #else
58*27b03b36SApple OSS Distributions #define ROOTDEVICETIMEOUT       60
59*27b03b36SApple OSS Distributions #endif
60*27b03b36SApple OSS Distributions 
61*27b03b36SApple OSS Distributions extern dev_t mdevadd(int devid, uint64_t base, unsigned int size, int phys);
62*27b03b36SApple OSS Distributions extern dev_t mdevlookup(int devid);
63*27b03b36SApple OSS Distributions extern void mdevremoveall(void);
64*27b03b36SApple OSS Distributions extern int mdevgetrange(int devid, uint64_t *base, uint64_t *size);
65*27b03b36SApple OSS Distributions extern void di_root_ramfile(IORegistryEntry * entry);
66*27b03b36SApple OSS Distributions extern int IODTGetDefault(const char *key, void *infoAddr, unsigned int infoSize);
67*27b03b36SApple OSS Distributions extern boolean_t cpuid_vmm_present(void);
68*27b03b36SApple OSS Distributions 
69*27b03b36SApple OSS Distributions #define ROUNDUP(a, b) (((a) + ((b) - 1)) & (~((b) - 1)))
70*27b03b36SApple OSS Distributions 
71*27b03b36SApple OSS Distributions #define IOPOLLED_COREFILE       (CONFIG_KDP_INTERACTIVE_DEBUGGING)
72*27b03b36SApple OSS Distributions 
73*27b03b36SApple OSS Distributions #if defined(XNU_TARGET_OS_BRIDGE)
74*27b03b36SApple OSS Distributions #define kIOCoreDumpPath         "/private/var/internal/kernelcore"
75*27b03b36SApple OSS Distributions #elif defined(XNU_TARGET_OS_OSX)
76*27b03b36SApple OSS Distributions #define kIOCoreDumpPath         "/System/Volumes/VM/kernelcore"
77*27b03b36SApple OSS Distributions #else
78*27b03b36SApple OSS Distributions #define kIOCoreDumpPath         "/private/var/vm/kernelcore"
79*27b03b36SApple OSS Distributions #endif
80*27b03b36SApple OSS Distributions 
81*27b03b36SApple OSS Distributions #define SYSTEM_NVRAM_PREFIX     "40A0DDD2-77F8-4392-B4A3-1E7304206516:"
82*27b03b36SApple OSS Distributions 
83*27b03b36SApple OSS Distributions #if CONFIG_KDP_INTERACTIVE_DEBUGGING
84*27b03b36SApple OSS Distributions /*
85*27b03b36SApple OSS Distributions  * Touched by IOFindBSDRoot() if a RAMDisk is used for the root device.
86*27b03b36SApple OSS Distributions  */
87*27b03b36SApple OSS Distributions extern uint64_t kdp_core_ramdisk_addr;
88*27b03b36SApple OSS Distributions extern uint64_t kdp_core_ramdisk_size;
89*27b03b36SApple OSS Distributions 
90*27b03b36SApple OSS Distributions /*
91*27b03b36SApple OSS Distributions  * A callback to indicate that the polled-mode corefile is now available.
92*27b03b36SApple OSS Distributions  */
93*27b03b36SApple OSS Distributions extern kern_return_t kdp_core_polled_io_polled_file_available(IOCoreFileAccessCallback access_data, void *access_context, void *recipient_context);
94*27b03b36SApple OSS Distributions 
95*27b03b36SApple OSS Distributions /*
96*27b03b36SApple OSS Distributions  * A callback to indicate that the polled-mode corefile is no longer available.
97*27b03b36SApple OSS Distributions  */
98*27b03b36SApple OSS Distributions extern kern_return_t kdp_core_polled_io_polled_file_unavailable(void);
99*27b03b36SApple OSS Distributions #endif
100*27b03b36SApple OSS Distributions 
101*27b03b36SApple OSS Distributions #if IOPOLLED_COREFILE
102*27b03b36SApple OSS Distributions static void IOOpenPolledCoreFile(thread_call_param_t __unused, thread_call_param_t corefilename);
103*27b03b36SApple OSS Distributions 
104*27b03b36SApple OSS Distributions thread_call_t corefile_open_call = NULL;
105*27b03b36SApple OSS Distributions #endif
106*27b03b36SApple OSS Distributions 
107*27b03b36SApple OSS Distributions kern_return_t
IOKitBSDInit(void)108*27b03b36SApple OSS Distributions IOKitBSDInit( void )
109*27b03b36SApple OSS Distributions {
110*27b03b36SApple OSS Distributions 	IOService::publishResource("IOBSD");
111*27b03b36SApple OSS Distributions 
112*27b03b36SApple OSS Distributions #if IOPOLLED_COREFILE
113*27b03b36SApple OSS Distributions 	corefile_open_call = thread_call_allocate_with_options(IOOpenPolledCoreFile, NULL, THREAD_CALL_PRIORITY_KERNEL, THREAD_CALL_OPTIONS_ONCE);
114*27b03b36SApple OSS Distributions #endif
115*27b03b36SApple OSS Distributions 
116*27b03b36SApple OSS Distributions 	return kIOReturnSuccess;
117*27b03b36SApple OSS Distributions }
118*27b03b36SApple OSS Distributions 
119*27b03b36SApple OSS Distributions void
IOServicePublishResource(const char * property,boolean_t value)120*27b03b36SApple OSS Distributions IOServicePublishResource( const char * property, boolean_t value )
121*27b03b36SApple OSS Distributions {
122*27b03b36SApple OSS Distributions 	if (value) {
123*27b03b36SApple OSS Distributions 		IOService::publishResource( property, kOSBooleanTrue );
124*27b03b36SApple OSS Distributions 	} else {
125*27b03b36SApple OSS Distributions 		IOService::getResourceService()->removeProperty( property );
126*27b03b36SApple OSS Distributions 	}
127*27b03b36SApple OSS Distributions }
128*27b03b36SApple OSS Distributions 
129*27b03b36SApple OSS Distributions boolean_t
IOServiceWaitForMatchingResource(const char * property,uint64_t timeout)130*27b03b36SApple OSS Distributions IOServiceWaitForMatchingResource( const char * property, uint64_t timeout )
131*27b03b36SApple OSS Distributions {
132*27b03b36SApple OSS Distributions 	OSDictionary *      dict = NULL;
133*27b03b36SApple OSS Distributions 	IOService *         match = NULL;
134*27b03b36SApple OSS Distributions 	boolean_t           found = false;
135*27b03b36SApple OSS Distributions 
136*27b03b36SApple OSS Distributions 	do {
137*27b03b36SApple OSS Distributions 		dict = IOService::resourceMatching( property );
138*27b03b36SApple OSS Distributions 		if (!dict) {
139*27b03b36SApple OSS Distributions 			continue;
140*27b03b36SApple OSS Distributions 		}
141*27b03b36SApple OSS Distributions 		match = IOService::waitForMatchingService( dict, timeout );
142*27b03b36SApple OSS Distributions 		if (match) {
143*27b03b36SApple OSS Distributions 			found = true;
144*27b03b36SApple OSS Distributions 		}
145*27b03b36SApple OSS Distributions 	} while (false);
146*27b03b36SApple OSS Distributions 
147*27b03b36SApple OSS Distributions 	if (dict) {
148*27b03b36SApple OSS Distributions 		dict->release();
149*27b03b36SApple OSS Distributions 	}
150*27b03b36SApple OSS Distributions 	if (match) {
151*27b03b36SApple OSS Distributions 		match->release();
152*27b03b36SApple OSS Distributions 	}
153*27b03b36SApple OSS Distributions 
154*27b03b36SApple OSS Distributions 	return found;
155*27b03b36SApple OSS Distributions }
156*27b03b36SApple OSS Distributions 
157*27b03b36SApple OSS Distributions boolean_t
IOCatalogueMatchingDriversPresent(const char * property)158*27b03b36SApple OSS Distributions IOCatalogueMatchingDriversPresent( const char * property )
159*27b03b36SApple OSS Distributions {
160*27b03b36SApple OSS Distributions 	OSDictionary *      dict = NULL;
161*27b03b36SApple OSS Distributions 	OSOrderedSet *      set = NULL;
162*27b03b36SApple OSS Distributions 	SInt32              generationCount = 0;
163*27b03b36SApple OSS Distributions 	boolean_t           found = false;
164*27b03b36SApple OSS Distributions 
165*27b03b36SApple OSS Distributions 	do {
166*27b03b36SApple OSS Distributions 		dict = OSDictionary::withCapacity(1);
167*27b03b36SApple OSS Distributions 		if (!dict) {
168*27b03b36SApple OSS Distributions 			continue;
169*27b03b36SApple OSS Distributions 		}
170*27b03b36SApple OSS Distributions 		dict->setObject( property, kOSBooleanTrue );
171*27b03b36SApple OSS Distributions 		set = gIOCatalogue->findDrivers( dict, &generationCount );
172*27b03b36SApple OSS Distributions 		if (set && (set->getCount() > 0)) {
173*27b03b36SApple OSS Distributions 			found = true;
174*27b03b36SApple OSS Distributions 		}
175*27b03b36SApple OSS Distributions 	} while (false);
176*27b03b36SApple OSS Distributions 
177*27b03b36SApple OSS Distributions 	if (dict) {
178*27b03b36SApple OSS Distributions 		dict->release();
179*27b03b36SApple OSS Distributions 	}
180*27b03b36SApple OSS Distributions 	if (set) {
181*27b03b36SApple OSS Distributions 		set->release();
182*27b03b36SApple OSS Distributions 	}
183*27b03b36SApple OSS Distributions 
184*27b03b36SApple OSS Distributions 	return found;
185*27b03b36SApple OSS Distributions }
186*27b03b36SApple OSS Distributions 
187*27b03b36SApple OSS Distributions OSDictionary *
IOBSDNameMatching(const char * name)188*27b03b36SApple OSS Distributions IOBSDNameMatching( const char * name )
189*27b03b36SApple OSS Distributions {
190*27b03b36SApple OSS Distributions 	OSDictionary *      dict;
191*27b03b36SApple OSS Distributions 	const OSSymbol *    str = NULL;
192*27b03b36SApple OSS Distributions 
193*27b03b36SApple OSS Distributions 	do {
194*27b03b36SApple OSS Distributions 		dict = IOService::serviceMatching( gIOServiceKey );
195*27b03b36SApple OSS Distributions 		if (!dict) {
196*27b03b36SApple OSS Distributions 			continue;
197*27b03b36SApple OSS Distributions 		}
198*27b03b36SApple OSS Distributions 		str = OSSymbol::withCString( name );
199*27b03b36SApple OSS Distributions 		if (!str) {
200*27b03b36SApple OSS Distributions 			continue;
201*27b03b36SApple OSS Distributions 		}
202*27b03b36SApple OSS Distributions 		dict->setObject( kIOBSDNameKey, (OSObject *) str );
203*27b03b36SApple OSS Distributions 		str->release();
204*27b03b36SApple OSS Distributions 
205*27b03b36SApple OSS Distributions 		return dict;
206*27b03b36SApple OSS Distributions 	} while (false);
207*27b03b36SApple OSS Distributions 
208*27b03b36SApple OSS Distributions 	if (dict) {
209*27b03b36SApple OSS Distributions 		dict->release();
210*27b03b36SApple OSS Distributions 	}
211*27b03b36SApple OSS Distributions 	if (str) {
212*27b03b36SApple OSS Distributions 		str->release();
213*27b03b36SApple OSS Distributions 	}
214*27b03b36SApple OSS Distributions 
215*27b03b36SApple OSS Distributions 	return NULL;
216*27b03b36SApple OSS Distributions }
217*27b03b36SApple OSS Distributions 
218*27b03b36SApple OSS Distributions OSDictionary *
IOUUIDMatching(void)219*27b03b36SApple OSS Distributions IOUUIDMatching( void )
220*27b03b36SApple OSS Distributions {
221*27b03b36SApple OSS Distributions 	return IOService::resourceMatching( "boot-uuid-media" );
222*27b03b36SApple OSS Distributions }
223*27b03b36SApple OSS Distributions 
224*27b03b36SApple OSS Distributions OSDictionary *
IONetworkNamePrefixMatching(const char * prefix)225*27b03b36SApple OSS Distributions IONetworkNamePrefixMatching( const char * prefix )
226*27b03b36SApple OSS Distributions {
227*27b03b36SApple OSS Distributions 	OSDictionary *       matching;
228*27b03b36SApple OSS Distributions 	OSDictionary *   propDict = NULL;
229*27b03b36SApple OSS Distributions 	const OSSymbol * str      = NULL;
230*27b03b36SApple OSS Distributions 	char networkType[128];
231*27b03b36SApple OSS Distributions 
232*27b03b36SApple OSS Distributions 	do {
233*27b03b36SApple OSS Distributions 		matching = IOService::serviceMatching( "IONetworkInterface" );
234*27b03b36SApple OSS Distributions 		if (matching == NULL) {
235*27b03b36SApple OSS Distributions 			continue;
236*27b03b36SApple OSS Distributions 		}
237*27b03b36SApple OSS Distributions 
238*27b03b36SApple OSS Distributions 		propDict = OSDictionary::withCapacity(1);
239*27b03b36SApple OSS Distributions 		if (propDict == NULL) {
240*27b03b36SApple OSS Distributions 			continue;
241*27b03b36SApple OSS Distributions 		}
242*27b03b36SApple OSS Distributions 
243*27b03b36SApple OSS Distributions 		str = OSSymbol::withCString( prefix );
244*27b03b36SApple OSS Distributions 		if (str == NULL) {
245*27b03b36SApple OSS Distributions 			continue;
246*27b03b36SApple OSS Distributions 		}
247*27b03b36SApple OSS Distributions 
248*27b03b36SApple OSS Distributions 		propDict->setObject( "IOInterfaceNamePrefix", (OSObject *) str );
249*27b03b36SApple OSS Distributions 		str->release();
250*27b03b36SApple OSS Distributions 		str = NULL;
251*27b03b36SApple OSS Distributions 
252*27b03b36SApple OSS Distributions 		// see if we're contrained to netroot off of specific network type
253*27b03b36SApple OSS Distributions 		if (PE_parse_boot_argn( "network-type", networkType, 128 )) {
254*27b03b36SApple OSS Distributions 			str = OSSymbol::withCString( networkType );
255*27b03b36SApple OSS Distributions 			if (str) {
256*27b03b36SApple OSS Distributions 				propDict->setObject( "IONetworkRootType", str);
257*27b03b36SApple OSS Distributions 				str->release();
258*27b03b36SApple OSS Distributions 				str = NULL;
259*27b03b36SApple OSS Distributions 			}
260*27b03b36SApple OSS Distributions 		}
261*27b03b36SApple OSS Distributions 
262*27b03b36SApple OSS Distributions 		if (matching->setObject( gIOPropertyMatchKey,
263*27b03b36SApple OSS Distributions 		    (OSObject *) propDict ) != true) {
264*27b03b36SApple OSS Distributions 			continue;
265*27b03b36SApple OSS Distributions 		}
266*27b03b36SApple OSS Distributions 
267*27b03b36SApple OSS Distributions 		propDict->release();
268*27b03b36SApple OSS Distributions 		propDict = NULL;
269*27b03b36SApple OSS Distributions 
270*27b03b36SApple OSS Distributions 		return matching;
271*27b03b36SApple OSS Distributions 	} while (false);
272*27b03b36SApple OSS Distributions 
273*27b03b36SApple OSS Distributions 	if (matching) {
274*27b03b36SApple OSS Distributions 		matching->release();
275*27b03b36SApple OSS Distributions 	}
276*27b03b36SApple OSS Distributions 	if (propDict) {
277*27b03b36SApple OSS Distributions 		propDict->release();
278*27b03b36SApple OSS Distributions 	}
279*27b03b36SApple OSS Distributions 	if (str) {
280*27b03b36SApple OSS Distributions 		str->release();
281*27b03b36SApple OSS Distributions 	}
282*27b03b36SApple OSS Distributions 
283*27b03b36SApple OSS Distributions 	return NULL;
284*27b03b36SApple OSS Distributions }
285*27b03b36SApple OSS Distributions 
286*27b03b36SApple OSS Distributions static bool
IORegisterNetworkInterface(IOService * netif)287*27b03b36SApple OSS Distributions IORegisterNetworkInterface( IOService * netif )
288*27b03b36SApple OSS Distributions {
289*27b03b36SApple OSS Distributions 	// A network interface is typically named and registered
290*27b03b36SApple OSS Distributions 	// with BSD after receiving a request from a user space
291*27b03b36SApple OSS Distributions 	// "namer". However, for cases when the system needs to
292*27b03b36SApple OSS Distributions 	// root from the network, this registration task must be
293*27b03b36SApple OSS Distributions 	// done inside the kernel and completed before the root
294*27b03b36SApple OSS Distributions 	// device is handed to BSD.
295*27b03b36SApple OSS Distributions 
296*27b03b36SApple OSS Distributions 	IOService *    stack;
297*27b03b36SApple OSS Distributions 	OSNumber *     zero    = NULL;
298*27b03b36SApple OSS Distributions 	OSString *     path    = NULL;
299*27b03b36SApple OSS Distributions 	OSDictionary * dict    = NULL;
300*27b03b36SApple OSS Distributions 	OSDataAllocation<char> pathBuf;
301*27b03b36SApple OSS Distributions 	int            len;
302*27b03b36SApple OSS Distributions 	enum { kMaxPathLen = 512 };
303*27b03b36SApple OSS Distributions 
304*27b03b36SApple OSS Distributions 	do {
305*27b03b36SApple OSS Distributions 		stack = IOService::waitForService(
306*27b03b36SApple OSS Distributions 			IOService::serviceMatching("IONetworkStack"));
307*27b03b36SApple OSS Distributions 		if (stack == NULL) {
308*27b03b36SApple OSS Distributions 			break;
309*27b03b36SApple OSS Distributions 		}
310*27b03b36SApple OSS Distributions 
311*27b03b36SApple OSS Distributions 		dict = OSDictionary::withCapacity(3);
312*27b03b36SApple OSS Distributions 		if (dict == NULL) {
313*27b03b36SApple OSS Distributions 			break;
314*27b03b36SApple OSS Distributions 		}
315*27b03b36SApple OSS Distributions 
316*27b03b36SApple OSS Distributions 		zero = OSNumber::withNumber((UInt64) 0, 32);
317*27b03b36SApple OSS Distributions 		if (zero == NULL) {
318*27b03b36SApple OSS Distributions 			break;
319*27b03b36SApple OSS Distributions 		}
320*27b03b36SApple OSS Distributions 
321*27b03b36SApple OSS Distributions 		pathBuf = OSDataAllocation<char>( kMaxPathLen, OSAllocateMemory );
322*27b03b36SApple OSS Distributions 		if (!pathBuf) {
323*27b03b36SApple OSS Distributions 			break;
324*27b03b36SApple OSS Distributions 		}
325*27b03b36SApple OSS Distributions 
326*27b03b36SApple OSS Distributions 		len = kMaxPathLen;
327*27b03b36SApple OSS Distributions 		if (netif->getPath( pathBuf.data(), &len, gIOServicePlane )
328*27b03b36SApple OSS Distributions 		    == false) {
329*27b03b36SApple OSS Distributions 			break;
330*27b03b36SApple OSS Distributions 		}
331*27b03b36SApple OSS Distributions 
332*27b03b36SApple OSS Distributions 		path = OSString::withCStringNoCopy(pathBuf.data());
333*27b03b36SApple OSS Distributions 		if (path == NULL) {
334*27b03b36SApple OSS Distributions 			break;
335*27b03b36SApple OSS Distributions 		}
336*27b03b36SApple OSS Distributions 
337*27b03b36SApple OSS Distributions 		dict->setObject( "IOInterfaceUnit", zero );
338*27b03b36SApple OSS Distributions 		dict->setObject( kIOPathMatchKey, path );
339*27b03b36SApple OSS Distributions 
340*27b03b36SApple OSS Distributions 		stack->setProperties( dict );
341*27b03b36SApple OSS Distributions 	}while (false);
342*27b03b36SApple OSS Distributions 
343*27b03b36SApple OSS Distributions 	if (zero) {
344*27b03b36SApple OSS Distributions 		zero->release();
345*27b03b36SApple OSS Distributions 	}
346*27b03b36SApple OSS Distributions 	if (path) {
347*27b03b36SApple OSS Distributions 		path->release();
348*27b03b36SApple OSS Distributions 	}
349*27b03b36SApple OSS Distributions 	if (dict) {
350*27b03b36SApple OSS Distributions 		dict->release();
351*27b03b36SApple OSS Distributions 	}
352*27b03b36SApple OSS Distributions 
353*27b03b36SApple OSS Distributions 	return netif->getProperty( kIOBSDNameKey ) != NULL;
354*27b03b36SApple OSS Distributions }
355*27b03b36SApple OSS Distributions 
356*27b03b36SApple OSS Distributions OSDictionary *
IOOFPathMatching(const char * path,char * buf,int maxLen)357*27b03b36SApple OSS Distributions IOOFPathMatching( const char * path, char * buf, int maxLen )
358*27b03b36SApple OSS Distributions {
359*27b03b36SApple OSS Distributions 	OSDictionary *      matching = NULL;
360*27b03b36SApple OSS Distributions 	OSString *          str;
361*27b03b36SApple OSS Distributions 	char *              comp;
362*27b03b36SApple OSS Distributions 	int                 len;
363*27b03b36SApple OSS Distributions 
364*27b03b36SApple OSS Distributions 	do {
365*27b03b36SApple OSS Distributions 		len = ((int) strlen( kIODeviceTreePlane ":" ));
366*27b03b36SApple OSS Distributions 		maxLen -= len;
367*27b03b36SApple OSS Distributions 		if (maxLen <= 0) {
368*27b03b36SApple OSS Distributions 			continue;
369*27b03b36SApple OSS Distributions 		}
370*27b03b36SApple OSS Distributions 
371*27b03b36SApple OSS Distributions 		strlcpy( buf, kIODeviceTreePlane ":", len + 1 );
372*27b03b36SApple OSS Distributions 		comp = buf + len;
373*27b03b36SApple OSS Distributions 
374*27b03b36SApple OSS Distributions 		len = ((int) strnlen( path, INT_MAX ));
375*27b03b36SApple OSS Distributions 		maxLen -= len;
376*27b03b36SApple OSS Distributions 		if (maxLen <= 0) {
377*27b03b36SApple OSS Distributions 			continue;
378*27b03b36SApple OSS Distributions 		}
379*27b03b36SApple OSS Distributions 		strlcpy( comp, path, len + 1 );
380*27b03b36SApple OSS Distributions 
381*27b03b36SApple OSS Distributions 		matching = OSDictionary::withCapacity( 1 );
382*27b03b36SApple OSS Distributions 		if (!matching) {
383*27b03b36SApple OSS Distributions 			continue;
384*27b03b36SApple OSS Distributions 		}
385*27b03b36SApple OSS Distributions 
386*27b03b36SApple OSS Distributions 		str = OSString::withCString( buf );
387*27b03b36SApple OSS Distributions 		if (!str) {
388*27b03b36SApple OSS Distributions 			continue;
389*27b03b36SApple OSS Distributions 		}
390*27b03b36SApple OSS Distributions 		matching->setObject( kIOPathMatchKey, str );
391*27b03b36SApple OSS Distributions 		str->release();
392*27b03b36SApple OSS Distributions 
393*27b03b36SApple OSS Distributions 		return matching;
394*27b03b36SApple OSS Distributions 	} while (false);
395*27b03b36SApple OSS Distributions 
396*27b03b36SApple OSS Distributions 	if (matching) {
397*27b03b36SApple OSS Distributions 		matching->release();
398*27b03b36SApple OSS Distributions 	}
399*27b03b36SApple OSS Distributions 
400*27b03b36SApple OSS Distributions 	return NULL;
401*27b03b36SApple OSS Distributions }
402*27b03b36SApple OSS Distributions 
403*27b03b36SApple OSS Distributions static int didRam = 0;
404*27b03b36SApple OSS Distributions enum { kMaxPathBuf = 512, kMaxBootVar = 128 };
405*27b03b36SApple OSS Distributions 
406*27b03b36SApple OSS Distributions bool
IOGetBootUUID(char * uuid)407*27b03b36SApple OSS Distributions IOGetBootUUID(char *uuid)
408*27b03b36SApple OSS Distributions {
409*27b03b36SApple OSS Distributions 	IORegistryEntry *entry;
410*27b03b36SApple OSS Distributions 	OSData *uuid_data = NULL;
411*27b03b36SApple OSS Distributions 	bool result = false;
412*27b03b36SApple OSS Distributions 
413*27b03b36SApple OSS Distributions 	if ((entry = IORegistryEntry::fromPath("/chosen", gIODTPlane))) {
414*27b03b36SApple OSS Distributions 		uuid_data = (OSData *)entry->getProperty("boot-uuid");
415*27b03b36SApple OSS Distributions 		if (uuid_data) {
416*27b03b36SApple OSS Distributions 			unsigned int length = uuid_data->getLength();
417*27b03b36SApple OSS Distributions 			if (length <= sizeof(uuid_string_t)) {
418*27b03b36SApple OSS Distributions 				/* ensure caller's buffer is fully initialized: */
419*27b03b36SApple OSS Distributions 				bzero(uuid, sizeof(uuid_string_t));
420*27b03b36SApple OSS Distributions 				/* copy the content of uuid_data->getBytesNoCopy() into uuid */
421*27b03b36SApple OSS Distributions 				memcpy(uuid, uuid_data->getBytesNoCopy(), length);
422*27b03b36SApple OSS Distributions 				/* guarantee nul-termination: */
423*27b03b36SApple OSS Distributions 				uuid[sizeof(uuid_string_t) - 1] = '\0';
424*27b03b36SApple OSS Distributions 				result = true;
425*27b03b36SApple OSS Distributions 			} else {
426*27b03b36SApple OSS Distributions 				uuid = NULL;
427*27b03b36SApple OSS Distributions 			}
428*27b03b36SApple OSS Distributions 		}
429*27b03b36SApple OSS Distributions 		OSSafeReleaseNULL(entry);
430*27b03b36SApple OSS Distributions 	}
431*27b03b36SApple OSS Distributions 	return result;
432*27b03b36SApple OSS Distributions }
433*27b03b36SApple OSS Distributions 
434*27b03b36SApple OSS Distributions bool
IOGetApfsPrebootUUID(char * uuid)435*27b03b36SApple OSS Distributions IOGetApfsPrebootUUID(char *uuid)
436*27b03b36SApple OSS Distributions {
437*27b03b36SApple OSS Distributions 	IORegistryEntry *entry;
438*27b03b36SApple OSS Distributions 	OSData *uuid_data = NULL;
439*27b03b36SApple OSS Distributions 	bool result = false;
440*27b03b36SApple OSS Distributions 
441*27b03b36SApple OSS Distributions 	if ((entry = IORegistryEntry::fromPath("/chosen", gIODTPlane))) {
442*27b03b36SApple OSS Distributions 		uuid_data = (OSData *)entry->getProperty("apfs-preboot-uuid");
443*27b03b36SApple OSS Distributions 
444*27b03b36SApple OSS Distributions 		if (uuid_data) {
445*27b03b36SApple OSS Distributions 			unsigned int length = uuid_data->getLength();
446*27b03b36SApple OSS Distributions 			if (length <= sizeof(uuid_string_t)) {
447*27b03b36SApple OSS Distributions 				/* ensure caller's buffer is fully initialized: */
448*27b03b36SApple OSS Distributions 				bzero(uuid, sizeof(uuid_string_t));
449*27b03b36SApple OSS Distributions 				/* copy the content of uuid_data->getBytesNoCopy() into uuid */
450*27b03b36SApple OSS Distributions 				memcpy(uuid, uuid_data->getBytesNoCopy(), length);
451*27b03b36SApple OSS Distributions 				/* guarantee nul-termination: */
452*27b03b36SApple OSS Distributions 				uuid[sizeof(uuid_string_t) - 1] = '\0';
453*27b03b36SApple OSS Distributions 				result = true;
454*27b03b36SApple OSS Distributions 			} else {
455*27b03b36SApple OSS Distributions 				uuid = NULL;
456*27b03b36SApple OSS Distributions 			}
457*27b03b36SApple OSS Distributions 		}
458*27b03b36SApple OSS Distributions 		OSSafeReleaseNULL(entry);
459*27b03b36SApple OSS Distributions 	}
460*27b03b36SApple OSS Distributions 	return result;
461*27b03b36SApple OSS Distributions }
462*27b03b36SApple OSS Distributions 
463*27b03b36SApple OSS Distributions bool
IOGetAssociatedApfsVolgroupUUID(char * uuid)464*27b03b36SApple OSS Distributions IOGetAssociatedApfsVolgroupUUID(char *uuid)
465*27b03b36SApple OSS Distributions {
466*27b03b36SApple OSS Distributions 	IORegistryEntry *entry;
467*27b03b36SApple OSS Distributions 	OSData *uuid_data = NULL;
468*27b03b36SApple OSS Distributions 	bool result = false;
469*27b03b36SApple OSS Distributions 
470*27b03b36SApple OSS Distributions 	if ((entry = IORegistryEntry::fromPath("/chosen", gIODTPlane))) {
471*27b03b36SApple OSS Distributions 		uuid_data = (OSData *)entry->getProperty("associated-volume-group");
472*27b03b36SApple OSS Distributions 
473*27b03b36SApple OSS Distributions 		if (uuid_data) {
474*27b03b36SApple OSS Distributions 			unsigned int length = uuid_data->getLength();
475*27b03b36SApple OSS Distributions 
476*27b03b36SApple OSS Distributions 			if (length <= sizeof(uuid_string_t)) {
477*27b03b36SApple OSS Distributions 				/* ensure caller's buffer is fully initialized: */
478*27b03b36SApple OSS Distributions 				bzero(uuid, sizeof(uuid_string_t));
479*27b03b36SApple OSS Distributions 				/* copy the content of uuid_data->getBytesNoCopy() into uuid */
480*27b03b36SApple OSS Distributions 				memcpy(uuid, uuid_data->getBytesNoCopy(), length);
481*27b03b36SApple OSS Distributions 				/* guarantee nul-termination: */
482*27b03b36SApple OSS Distributions 				uuid[sizeof(uuid_string_t) - 1] = '\0';
483*27b03b36SApple OSS Distributions 				result = true;
484*27b03b36SApple OSS Distributions 			} else {
485*27b03b36SApple OSS Distributions 				uuid = NULL;
486*27b03b36SApple OSS Distributions 			}
487*27b03b36SApple OSS Distributions 		}
488*27b03b36SApple OSS Distributions 		OSSafeReleaseNULL(entry);
489*27b03b36SApple OSS Distributions 	}
490*27b03b36SApple OSS Distributions 	return result;
491*27b03b36SApple OSS Distributions }
492*27b03b36SApple OSS Distributions 
493*27b03b36SApple OSS Distributions bool
IOGetBootObjectsPath(char * path_prefix)494*27b03b36SApple OSS Distributions IOGetBootObjectsPath(char *path_prefix)
495*27b03b36SApple OSS Distributions {
496*27b03b36SApple OSS Distributions 	IORegistryEntry *entry;
497*27b03b36SApple OSS Distributions 	OSData *path_prefix_data = NULL;
498*27b03b36SApple OSS Distributions 	bool result = false;
499*27b03b36SApple OSS Distributions 
500*27b03b36SApple OSS Distributions 	if ((entry = IORegistryEntry::fromPath("/chosen", gIODTPlane))) {
501*27b03b36SApple OSS Distributions 		path_prefix_data = (OSData *)entry->getProperty("boot-objects-path");
502*27b03b36SApple OSS Distributions 
503*27b03b36SApple OSS Distributions 		if (path_prefix_data) {
504*27b03b36SApple OSS Distributions 			unsigned int length = path_prefix_data->getLength();
505*27b03b36SApple OSS Distributions 
506*27b03b36SApple OSS Distributions 			if (length <= MAXPATHLEN) {
507*27b03b36SApple OSS Distributions 				/* ensure caller's buffer is fully initialized: */
508*27b03b36SApple OSS Distributions 				bzero(path_prefix, MAXPATHLEN);
509*27b03b36SApple OSS Distributions 				/* copy the content of path_prefix_data->getBytesNoCopy() into path_prefix */
510*27b03b36SApple OSS Distributions 				memcpy(path_prefix, path_prefix_data->getBytesNoCopy(), length);
511*27b03b36SApple OSS Distributions 				/* guarantee nul-termination: */
512*27b03b36SApple OSS Distributions 				path_prefix[MAXPATHLEN - 1] = '\0';
513*27b03b36SApple OSS Distributions 				result = true;
514*27b03b36SApple OSS Distributions 			} else {
515*27b03b36SApple OSS Distributions 				path_prefix = NULL;
516*27b03b36SApple OSS Distributions 			}
517*27b03b36SApple OSS Distributions 		}
518*27b03b36SApple OSS Distributions 		OSSafeReleaseNULL(entry);
519*27b03b36SApple OSS Distributions 	}
520*27b03b36SApple OSS Distributions 	return result;
521*27b03b36SApple OSS Distributions }
522*27b03b36SApple OSS Distributions 
523*27b03b36SApple OSS Distributions /*
524*27b03b36SApple OSS Distributions  * Set NVRAM to boot into the right flavor of Recovery,
525*27b03b36SApple OSS Distributions  * optionally passing a UUID of a volume that failed to boot.
526*27b03b36SApple OSS Distributions  * If `reboot` is true, reboot immediately.
527*27b03b36SApple OSS Distributions  *
528*27b03b36SApple OSS Distributions  * Returns true if `mode` was understood, false otherwise.
529*27b03b36SApple OSS Distributions  * (Does not return if `reboot` is true.)
530*27b03b36SApple OSS Distributions  */
531*27b03b36SApple OSS Distributions boolean_t
IOSetRecoveryBoot(bsd_bootfail_mode_t mode,uuid_t volume_uuid,boolean_t reboot)532*27b03b36SApple OSS Distributions IOSetRecoveryBoot(bsd_bootfail_mode_t mode, uuid_t volume_uuid, boolean_t reboot)
533*27b03b36SApple OSS Distributions {
534*27b03b36SApple OSS Distributions 	IODTNVRAM *nvram = NULL;
535*27b03b36SApple OSS Distributions 	const OSSymbol *boot_command_sym = NULL;
536*27b03b36SApple OSS Distributions 	OSString *boot_command_recover = NULL;
537*27b03b36SApple OSS Distributions 
538*27b03b36SApple OSS Distributions 	if (mode == BSD_BOOTFAIL_SEAL_BROKEN) {
539*27b03b36SApple OSS Distributions 		const char *boot_mode = "ssv-seal-broken";
540*27b03b36SApple OSS Distributions 		uuid_string_t volume_uuid_str;
541*27b03b36SApple OSS Distributions 
542*27b03b36SApple OSS Distributions 		// Set `recovery-broken-seal-uuid = <volume_uuid>`.
543*27b03b36SApple OSS Distributions 		if (volume_uuid) {
544*27b03b36SApple OSS Distributions 			uuid_unparse_upper(volume_uuid, volume_uuid_str);
545*27b03b36SApple OSS Distributions 
546*27b03b36SApple OSS Distributions 			if (!PEWriteNVRAMProperty(SYSTEM_NVRAM_PREFIX "recovery-broken-seal-uuid",
547*27b03b36SApple OSS Distributions 			    volume_uuid_str, sizeof(uuid_string_t))) {
548*27b03b36SApple OSS Distributions 				IOLog("Failed to write recovery-broken-seal-uuid to NVRAM.\n");
549*27b03b36SApple OSS Distributions 			}
550*27b03b36SApple OSS Distributions 		}
551*27b03b36SApple OSS Distributions 
552*27b03b36SApple OSS Distributions 		// Set `recovery-boot-mode = ssv-seal-broken`.
553*27b03b36SApple OSS Distributions 		if (!PEWriteNVRAMProperty(SYSTEM_NVRAM_PREFIX "recovery-boot-mode", boot_mode,
554*27b03b36SApple OSS Distributions 		    (const unsigned int) strlen(boot_mode))) {
555*27b03b36SApple OSS Distributions 			IOLog("Failed to write recovery-boot-mode to NVRAM.\n");
556*27b03b36SApple OSS Distributions 		}
557*27b03b36SApple OSS Distributions 	} else if (mode == BSD_BOOTFAIL_MEDIA_MISSING) {
558*27b03b36SApple OSS Distributions 		const char *boot_picker_reason = "missing-boot-media";
559*27b03b36SApple OSS Distributions 
560*27b03b36SApple OSS Distributions 		// Set `boot-picker-bringup-reason = missing-boot-media`.
561*27b03b36SApple OSS Distributions 		if (!PEWriteNVRAMProperty(SYSTEM_NVRAM_PREFIX "boot-picker-bringup-reason",
562*27b03b36SApple OSS Distributions 		    boot_picker_reason, (const unsigned int) strlen(boot_picker_reason))) {
563*27b03b36SApple OSS Distributions 			IOLog("Failed to write boot-picker-bringup-reason to NVRAM.\n");
564*27b03b36SApple OSS Distributions 		}
565*27b03b36SApple OSS Distributions 
566*27b03b36SApple OSS Distributions 		// Set `boot-command = recover-system`.
567*27b03b36SApple OSS Distributions 
568*27b03b36SApple OSS Distributions 		// Construct an OSSymbol and an OSString to be the (key, value) pair
569*27b03b36SApple OSS Distributions 		// we write to NVRAM. Unfortunately, since our value must be an OSString
570*27b03b36SApple OSS Distributions 		// instead of an OSData, we cannot use PEWriteNVRAMProperty() here.
571*27b03b36SApple OSS Distributions 		boot_command_sym = OSSymbol::withCStringNoCopy(SYSTEM_NVRAM_PREFIX "boot-command");
572*27b03b36SApple OSS Distributions 		boot_command_recover = OSString::withCStringNoCopy("recover-system");
573*27b03b36SApple OSS Distributions 		if (boot_command_sym == NULL || boot_command_recover == NULL) {
574*27b03b36SApple OSS Distributions 			IOLog("Failed to create boot-command strings.\n");
575*27b03b36SApple OSS Distributions 			goto do_reboot;
576*27b03b36SApple OSS Distributions 		}
577*27b03b36SApple OSS Distributions 
578*27b03b36SApple OSS Distributions 		// Wait for NVRAM to be readable...
579*27b03b36SApple OSS Distributions 		nvram = OSDynamicCast(IODTNVRAM, IOService::waitForService(
580*27b03b36SApple OSS Distributions 			    IOService::serviceMatching("IODTNVRAM")));
581*27b03b36SApple OSS Distributions 		if (nvram == NULL) {
582*27b03b36SApple OSS Distributions 			IOLog("Failed to acquire IODTNVRAM object.\n");
583*27b03b36SApple OSS Distributions 			goto do_reboot;
584*27b03b36SApple OSS Distributions 		}
585*27b03b36SApple OSS Distributions 
586*27b03b36SApple OSS Distributions 		// Wait for NVRAM to be writable...
587*27b03b36SApple OSS Distributions 		if (!IOServiceWaitForMatchingResource("IONVRAM", UINT64_MAX)) {
588*27b03b36SApple OSS Distributions 			IOLog("Failed to wait for IONVRAM service.\n");
589*27b03b36SApple OSS Distributions 			// attempt the work anyway...
590*27b03b36SApple OSS Distributions 		}
591*27b03b36SApple OSS Distributions 
592*27b03b36SApple OSS Distributions 		// Write the new boot-command to NVRAM, and sync if successful.
593*27b03b36SApple OSS Distributions 		if (!nvram->setProperty(boot_command_sym, boot_command_recover)) {
594*27b03b36SApple OSS Distributions 			IOLog("Failed to save new boot-command to NVRAM.\n");
595*27b03b36SApple OSS Distributions 		} else {
596*27b03b36SApple OSS Distributions 			nvram->sync();
597*27b03b36SApple OSS Distributions 		}
598*27b03b36SApple OSS Distributions 	} else {
599*27b03b36SApple OSS Distributions 		IOLog("Unknown mode: %d\n", mode);
600*27b03b36SApple OSS Distributions 		return false;
601*27b03b36SApple OSS Distributions 	}
602*27b03b36SApple OSS Distributions 
603*27b03b36SApple OSS Distributions 	// Clean up and reboot!
604*27b03b36SApple OSS Distributions do_reboot:
605*27b03b36SApple OSS Distributions 	if (boot_command_recover != NULL) {
606*27b03b36SApple OSS Distributions 		boot_command_recover->release();
607*27b03b36SApple OSS Distributions 	}
608*27b03b36SApple OSS Distributions 
609*27b03b36SApple OSS Distributions 	if (boot_command_sym != NULL) {
610*27b03b36SApple OSS Distributions 		boot_command_sym->release();
611*27b03b36SApple OSS Distributions 	}
612*27b03b36SApple OSS Distributions 
613*27b03b36SApple OSS Distributions 	if (reboot) {
614*27b03b36SApple OSS Distributions 		IOLog("\nAbout to reboot into Recovery!\n");
615*27b03b36SApple OSS Distributions 		(void)PEHaltRestart(kPERestartCPU);
616*27b03b36SApple OSS Distributions 	}
617*27b03b36SApple OSS Distributions 
618*27b03b36SApple OSS Distributions 	return true;
619*27b03b36SApple OSS Distributions }
620*27b03b36SApple OSS Distributions 
621*27b03b36SApple OSS Distributions int
IOGetVMMPresent(void)622*27b03b36SApple OSS Distributions IOGetVMMPresent(void)
623*27b03b36SApple OSS Distributions {
624*27b03b36SApple OSS Distributions 	int hv_vmm_present = 0;
625*27b03b36SApple OSS Distributions 
626*27b03b36SApple OSS Distributions #if defined(__arm64__)
627*27b03b36SApple OSS Distributions 	if (IODTGetDefault("vmm-present", &hv_vmm_present, sizeof(hv_vmm_present)) < 0) {
628*27b03b36SApple OSS Distributions 		return 0;
629*27b03b36SApple OSS Distributions 	}
630*27b03b36SApple OSS Distributions 
631*27b03b36SApple OSS Distributions 	if (hv_vmm_present != 0) {
632*27b03b36SApple OSS Distributions 		hv_vmm_present = 1;
633*27b03b36SApple OSS Distributions 	}
634*27b03b36SApple OSS Distributions #elif defined(__x86_64__)
635*27b03b36SApple OSS Distributions 	hv_vmm_present = cpuid_vmm_present();
636*27b03b36SApple OSS Distributions #endif
637*27b03b36SApple OSS Distributions 
638*27b03b36SApple OSS Distributions 	return hv_vmm_present;
639*27b03b36SApple OSS Distributions }
640*27b03b36SApple OSS Distributions 
641*27b03b36SApple OSS Distributions kern_return_t
IOFindBSDRoot(char * rootName,unsigned int rootNameSize,dev_t * root,u_int32_t * oflags)642*27b03b36SApple OSS Distributions IOFindBSDRoot( char * rootName, unsigned int rootNameSize,
643*27b03b36SApple OSS Distributions     dev_t * root, u_int32_t * oflags )
644*27b03b36SApple OSS Distributions {
645*27b03b36SApple OSS Distributions 	mach_timespec_t     t;
646*27b03b36SApple OSS Distributions 	IOService *         service;
647*27b03b36SApple OSS Distributions 	IORegistryEntry *   regEntry;
648*27b03b36SApple OSS Distributions 	OSDictionary *      matching = NULL;
649*27b03b36SApple OSS Distributions 	OSString *          iostr;
650*27b03b36SApple OSS Distributions 	OSNumber *          off;
651*27b03b36SApple OSS Distributions 	OSData *            data = NULL;
652*27b03b36SApple OSS Distributions 
653*27b03b36SApple OSS Distributions 	UInt32              flags = 0;
654*27b03b36SApple OSS Distributions 	int                 mnr, mjr;
655*27b03b36SApple OSS Distributions 	const char *        mediaProperty = NULL;
656*27b03b36SApple OSS Distributions 	char *              rdBootVar;
657*27b03b36SApple OSS Distributions 	OSDataAllocation<char> str;
658*27b03b36SApple OSS Distributions 	const char *        look = NULL;
659*27b03b36SApple OSS Distributions 	int                 len;
660*27b03b36SApple OSS Distributions 	bool                debugInfoPrintedOnce = false;
661*27b03b36SApple OSS Distributions 	bool                needNetworkKexts = false;
662*27b03b36SApple OSS Distributions 	const char *        uuidStr = NULL;
663*27b03b36SApple OSS Distributions 
664*27b03b36SApple OSS Distributions 	static int          mountAttempts = 0;
665*27b03b36SApple OSS Distributions 
666*27b03b36SApple OSS Distributions 	int xchar, dchar;
667*27b03b36SApple OSS Distributions 
668*27b03b36SApple OSS Distributions 	// stall here for anyone matching on the IOBSD resource to finish (filesystems)
669*27b03b36SApple OSS Distributions 	matching = IOService::serviceMatching(gIOResourcesKey);
670*27b03b36SApple OSS Distributions 	assert(matching);
671*27b03b36SApple OSS Distributions 	matching->setObject(gIOResourceMatchedKey, gIOBSDKey);
672*27b03b36SApple OSS Distributions 
673*27b03b36SApple OSS Distributions 	if ((service = IOService::waitForMatchingService(matching, 30ULL * kSecondScale))) {
674*27b03b36SApple OSS Distributions 		OSSafeReleaseNULL(service);
675*27b03b36SApple OSS Distributions 	} else {
676*27b03b36SApple OSS Distributions 		IOLog("!BSD\n");
677*27b03b36SApple OSS Distributions 	}
678*27b03b36SApple OSS Distributions 	matching->release();
679*27b03b36SApple OSS Distributions 	matching = NULL;
680*27b03b36SApple OSS Distributions 
681*27b03b36SApple OSS Distributions 	if (mountAttempts++) {
682*27b03b36SApple OSS Distributions 		IOLog("mount(%d) failed\n", mountAttempts);
683*27b03b36SApple OSS Distributions 		IOSleep( 5 * 1000 );
684*27b03b36SApple OSS Distributions 	}
685*27b03b36SApple OSS Distributions 
686*27b03b36SApple OSS Distributions 	str = OSDataAllocation<char>( kMaxPathBuf + kMaxBootVar, OSAllocateMemory );
687*27b03b36SApple OSS Distributions 	if (!str) {
688*27b03b36SApple OSS Distributions 		return kIOReturnNoMemory;
689*27b03b36SApple OSS Distributions 	}
690*27b03b36SApple OSS Distributions 	rdBootVar = str.data() + kMaxPathBuf;
691*27b03b36SApple OSS Distributions 
692*27b03b36SApple OSS Distributions 	if (!PE_parse_boot_argn("rd", rdBootVar, kMaxBootVar )
693*27b03b36SApple OSS Distributions 	    && !PE_parse_boot_argn("rootdev", rdBootVar, kMaxBootVar )) {
694*27b03b36SApple OSS Distributions 		rdBootVar[0] = 0;
695*27b03b36SApple OSS Distributions 	}
696*27b03b36SApple OSS Distributions 
697*27b03b36SApple OSS Distributions 	if ((regEntry = IORegistryEntry::fromPath( "/chosen", gIODTPlane ))) {
698*27b03b36SApple OSS Distributions 		do {
699*27b03b36SApple OSS Distributions 			di_root_ramfile(regEntry);
700*27b03b36SApple OSS Distributions 			OSObject* unserializedContainer = NULL;
701*27b03b36SApple OSS Distributions 			data = OSDynamicCast(OSData, regEntry->getProperty( "root-matching" ));
702*27b03b36SApple OSS Distributions 			if (data) {
703*27b03b36SApple OSS Distributions 				unserializedContainer = OSUnserializeXML((char *)data->getBytesNoCopy());
704*27b03b36SApple OSS Distributions 				matching = OSDynamicCast(OSDictionary, unserializedContainer);
705*27b03b36SApple OSS Distributions 				if (matching) {
706*27b03b36SApple OSS Distributions 					continue;
707*27b03b36SApple OSS Distributions 				}
708*27b03b36SApple OSS Distributions 			}
709*27b03b36SApple OSS Distributions 			OSSafeReleaseNULL(unserializedContainer);
710*27b03b36SApple OSS Distributions 
711*27b03b36SApple OSS Distributions 			data = (OSData *) regEntry->getProperty( "boot-uuid" );
712*27b03b36SApple OSS Distributions 			if (data) {
713*27b03b36SApple OSS Distributions 				uuidStr = (const char*)data->getBytesNoCopy();
714*27b03b36SApple OSS Distributions 				OSString *uuidString = OSString::withCString( uuidStr );
715*27b03b36SApple OSS Distributions 
716*27b03b36SApple OSS Distributions 				// match the boot-args boot-uuid processing below
717*27b03b36SApple OSS Distributions 				if (uuidString) {
718*27b03b36SApple OSS Distributions 					IOLog("rooting via boot-uuid from /chosen: %s\n", uuidStr);
719*27b03b36SApple OSS Distributions 					IOService::publishResource( "boot-uuid", uuidString );
720*27b03b36SApple OSS Distributions 					uuidString->release();
721*27b03b36SApple OSS Distributions 					matching = IOUUIDMatching();
722*27b03b36SApple OSS Distributions 					mediaProperty = "boot-uuid-media";
723*27b03b36SApple OSS Distributions 					continue;
724*27b03b36SApple OSS Distributions 				} else {
725*27b03b36SApple OSS Distributions 					uuidStr = NULL;
726*27b03b36SApple OSS Distributions 				}
727*27b03b36SApple OSS Distributions 			}
728*27b03b36SApple OSS Distributions 		} while (false);
729*27b03b36SApple OSS Distributions 		OSSafeReleaseNULL(regEntry);
730*27b03b36SApple OSS Distributions 	}
731*27b03b36SApple OSS Distributions 
732*27b03b36SApple OSS Distributions //
733*27b03b36SApple OSS Distributions //	See if we have a RAMDisk property in /chosen/memory-map.  If so, make it into a device.
734*27b03b36SApple OSS Distributions //	It will become /dev/mdx, where x is 0-f.
735*27b03b36SApple OSS Distributions //
736*27b03b36SApple OSS Distributions 
737*27b03b36SApple OSS Distributions 	if (!didRam) {                                                                                           /* Have we already build this ram disk? */
738*27b03b36SApple OSS Distributions 		didRam = 1;                                                                                             /* Remember we did this */
739*27b03b36SApple OSS Distributions 		if ((regEntry = IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane ))) {        /* Find the map node */
740*27b03b36SApple OSS Distributions 			data = (OSData *)regEntry->getProperty("RAMDisk");      /* Find the ram disk, if there */
741*27b03b36SApple OSS Distributions 			if (data) {                                                                                      /* We found one */
742*27b03b36SApple OSS Distributions 				uintptr_t *ramdParms;
743*27b03b36SApple OSS Distributions 				ramdParms = (uintptr_t *)data->getBytesNoCopy();        /* Point to the ram disk base and size */
744*27b03b36SApple OSS Distributions #if __LP64__
745*27b03b36SApple OSS Distributions #define MAX_PHYS_RAM    (((uint64_t)UINT_MAX) << 12)
746*27b03b36SApple OSS Distributions 				if (ramdParms[1] > MAX_PHYS_RAM) {
747*27b03b36SApple OSS Distributions 					panic("ramdisk params");
748*27b03b36SApple OSS Distributions 				}
749*27b03b36SApple OSS Distributions #endif /* __LP64__ */
750*27b03b36SApple OSS Distributions 				(void)mdevadd(-1, ml_static_ptovirt(ramdParms[0]) >> 12, (unsigned int) (ramdParms[1] >> 12), 0);        /* Initialize it and pass back the device number */
751*27b03b36SApple OSS Distributions 			}
752*27b03b36SApple OSS Distributions 			regEntry->release();                                                            /* Toss the entry */
753*27b03b36SApple OSS Distributions 		}
754*27b03b36SApple OSS Distributions 	}
755*27b03b36SApple OSS Distributions 
756*27b03b36SApple OSS Distributions //
757*27b03b36SApple OSS Distributions //	Now check if we are trying to root on a memory device
758*27b03b36SApple OSS Distributions //
759*27b03b36SApple OSS Distributions 
760*27b03b36SApple OSS Distributions 	if ((rdBootVar[0] == 'm') && (rdBootVar[1] == 'd') && (rdBootVar[3] == 0)) {
761*27b03b36SApple OSS Distributions 		dchar = xchar = rdBootVar[2];                                                   /* Get the actual device */
762*27b03b36SApple OSS Distributions 		if ((xchar >= '0') && (xchar <= '9')) {
763*27b03b36SApple OSS Distributions 			xchar = xchar - '0';                                    /* If digit, convert */
764*27b03b36SApple OSS Distributions 		} else {
765*27b03b36SApple OSS Distributions 			xchar = xchar & ~' ';                                                           /* Fold to upper case */
766*27b03b36SApple OSS Distributions 			if ((xchar >= 'A') && (xchar <= 'F')) {                          /* Is this a valid digit? */
767*27b03b36SApple OSS Distributions 				xchar = (xchar & 0xF) + 9;                                              /* Convert the hex digit */
768*27b03b36SApple OSS Distributions 				dchar = dchar | ' ';                                                    /* Fold to lower case */
769*27b03b36SApple OSS Distributions 			} else {
770*27b03b36SApple OSS Distributions 				xchar = -1;                                                                     /* Show bogus */
771*27b03b36SApple OSS Distributions 			}
772*27b03b36SApple OSS Distributions 		}
773*27b03b36SApple OSS Distributions 		if (xchar >= 0) {                                                                                /* Do we have a valid memory device name? */
774*27b03b36SApple OSS Distributions 			OSSafeReleaseNULL(matching);
775*27b03b36SApple OSS Distributions 			*root = mdevlookup(xchar);                                                      /* Find the device number */
776*27b03b36SApple OSS Distributions 			if (*root >= 0) {                                                                        /* Did we find one? */
777*27b03b36SApple OSS Distributions 				rootName[0] = 'm';                                                              /* Build root name */
778*27b03b36SApple OSS Distributions 				rootName[1] = 'd';                                                              /* Build root name */
779*27b03b36SApple OSS Distributions 				rootName[2] = (char) dchar;                                                     /* Build root name */
780*27b03b36SApple OSS Distributions 				rootName[3] = 0;                                                                /* Build root name */
781*27b03b36SApple OSS Distributions 				IOLog("BSD root: %s, major %d, minor %d\n", rootName, major(*root), minor(*root));
782*27b03b36SApple OSS Distributions 				*oflags = 0;                                                                    /* Show that this is not network */
783*27b03b36SApple OSS Distributions 
784*27b03b36SApple OSS Distributions #if CONFIG_KDP_INTERACTIVE_DEBUGGING
785*27b03b36SApple OSS Distributions 				/* retrieve final ramdisk range and initialize KDP variables */
786*27b03b36SApple OSS Distributions 				if (mdevgetrange(xchar, &kdp_core_ramdisk_addr, &kdp_core_ramdisk_size) != 0) {
787*27b03b36SApple OSS Distributions 					IOLog("Unable to retrieve range for root memory device %d\n", xchar);
788*27b03b36SApple OSS Distributions 					kdp_core_ramdisk_addr = 0;
789*27b03b36SApple OSS Distributions 					kdp_core_ramdisk_size = 0;
790*27b03b36SApple OSS Distributions 				}
791*27b03b36SApple OSS Distributions #endif
792*27b03b36SApple OSS Distributions 
793*27b03b36SApple OSS Distributions 				goto iofrootx;                                                                  /* Join common exit... */
794*27b03b36SApple OSS Distributions 			}
795*27b03b36SApple OSS Distributions 			panic("IOFindBSDRoot: specified root memory device, %s, has not been configured", rdBootVar); /* Not there */
796*27b03b36SApple OSS Distributions 		}
797*27b03b36SApple OSS Distributions 	}
798*27b03b36SApple OSS Distributions 
799*27b03b36SApple OSS Distributions 	if ((!matching) && rdBootVar[0]) {
800*27b03b36SApple OSS Distributions 		// by BSD name
801*27b03b36SApple OSS Distributions 		look = rdBootVar;
802*27b03b36SApple OSS Distributions 		if (look[0] == '*') {
803*27b03b36SApple OSS Distributions 			look++;
804*27b03b36SApple OSS Distributions 		}
805*27b03b36SApple OSS Distributions 
806*27b03b36SApple OSS Distributions 		if (strncmp( look, "en", strlen( "en" )) == 0) {
807*27b03b36SApple OSS Distributions 			matching = IONetworkNamePrefixMatching( "en" );
808*27b03b36SApple OSS Distributions 			needNetworkKexts = true;
809*27b03b36SApple OSS Distributions 		} else if (strncmp( look, "uuid", strlen( "uuid" )) == 0) {
810*27b03b36SApple OSS Distributions 			OSDataAllocation<char> uuid( kMaxBootVar, OSAllocateMemory );
811*27b03b36SApple OSS Distributions 
812*27b03b36SApple OSS Distributions 			if (uuid) {
813*27b03b36SApple OSS Distributions 				OSString *uuidString;
814*27b03b36SApple OSS Distributions 
815*27b03b36SApple OSS Distributions 				if (!PE_parse_boot_argn( "boot-uuid", uuid.data(), kMaxBootVar )) {
816*27b03b36SApple OSS Distributions 					panic( "rd=uuid but no boot-uuid=<value> specified" );
817*27b03b36SApple OSS Distributions 				}
818*27b03b36SApple OSS Distributions 				uuidString = OSString::withCString(uuid.data());
819*27b03b36SApple OSS Distributions 				if (uuidString) {
820*27b03b36SApple OSS Distributions 					IOService::publishResource( "boot-uuid", uuidString );
821*27b03b36SApple OSS Distributions 					uuidString->release();
822*27b03b36SApple OSS Distributions 					IOLog("\nWaiting for boot volume with UUID %s\n", uuid.data());
823*27b03b36SApple OSS Distributions 					matching = IOUUIDMatching();
824*27b03b36SApple OSS Distributions 					mediaProperty = "boot-uuid-media";
825*27b03b36SApple OSS Distributions 				}
826*27b03b36SApple OSS Distributions 			}
827*27b03b36SApple OSS Distributions 		} else {
828*27b03b36SApple OSS Distributions 			matching = IOBSDNameMatching( look );
829*27b03b36SApple OSS Distributions 		}
830*27b03b36SApple OSS Distributions 	}
831*27b03b36SApple OSS Distributions 
832*27b03b36SApple OSS Distributions 	if (!matching) {
833*27b03b36SApple OSS Distributions 		OSString * astring;
834*27b03b36SApple OSS Distributions 		// Match any HFS media
835*27b03b36SApple OSS Distributions 
836*27b03b36SApple OSS Distributions 		matching = IOService::serviceMatching( "IOMedia" );
837*27b03b36SApple OSS Distributions 		assert(matching);
838*27b03b36SApple OSS Distributions 		astring = OSString::withCStringNoCopy("Apple_HFS");
839*27b03b36SApple OSS Distributions 		if (astring) {
840*27b03b36SApple OSS Distributions 			matching->setObject("Content", astring);
841*27b03b36SApple OSS Distributions 			astring->release();
842*27b03b36SApple OSS Distributions 		}
843*27b03b36SApple OSS Distributions 	}
844*27b03b36SApple OSS Distributions 
845*27b03b36SApple OSS Distributions 	if (gIOKitDebug & kIOWaitQuietBeforeRoot) {
846*27b03b36SApple OSS Distributions 		IOLog( "Waiting for matching to complete\n" );
847*27b03b36SApple OSS Distributions 		IOService::getPlatform()->waitQuiet();
848*27b03b36SApple OSS Distributions 	}
849*27b03b36SApple OSS Distributions 
850*27b03b36SApple OSS Distributions 	if (matching) {
851*27b03b36SApple OSS Distributions 		OSSerialize * s = OSSerialize::withCapacity( 5 );
852*27b03b36SApple OSS Distributions 
853*27b03b36SApple OSS Distributions 		if (matching->serialize( s )) {
854*27b03b36SApple OSS Distributions 			IOLog( "Waiting on %s\n", s->text());
855*27b03b36SApple OSS Distributions 		}
856*27b03b36SApple OSS Distributions 		s->release();
857*27b03b36SApple OSS Distributions 	}
858*27b03b36SApple OSS Distributions 
859*27b03b36SApple OSS Distributions 	char namep[8];
860*27b03b36SApple OSS Distributions 	if (needNetworkKexts
861*27b03b36SApple OSS Distributions 	    || PE_parse_boot_argn("-s", namep, sizeof(namep))) {
862*27b03b36SApple OSS Distributions 		IOService::startDeferredMatches();
863*27b03b36SApple OSS Distributions 	}
864*27b03b36SApple OSS Distributions 
865*27b03b36SApple OSS Distributions 	do {
866*27b03b36SApple OSS Distributions 		t.tv_sec = ROOTDEVICETIMEOUT;
867*27b03b36SApple OSS Distributions 		t.tv_nsec = 0;
868*27b03b36SApple OSS Distributions 		matching->retain();
869*27b03b36SApple OSS Distributions 		service = IOService::waitForService( matching, &t );
870*27b03b36SApple OSS Distributions 		if ((!service) || (mountAttempts == 10)) {
871*27b03b36SApple OSS Distributions #if !XNU_TARGET_OS_OSX || !defined(__arm64__)
872*27b03b36SApple OSS Distributions 			PE_display_icon( 0, "noroot");
873*27b03b36SApple OSS Distributions 			IOLog( "Still waiting for root device\n" );
874*27b03b36SApple OSS Distributions #endif
875*27b03b36SApple OSS Distributions 
876*27b03b36SApple OSS Distributions 			if (!debugInfoPrintedOnce) {
877*27b03b36SApple OSS Distributions 				debugInfoPrintedOnce = true;
878*27b03b36SApple OSS Distributions 				if (gIOKitDebug & kIOLogDTree) {
879*27b03b36SApple OSS Distributions 					IOLog("\nDT plane:\n");
880*27b03b36SApple OSS Distributions 					IOPrintPlane( gIODTPlane );
881*27b03b36SApple OSS Distributions 				}
882*27b03b36SApple OSS Distributions 				if (gIOKitDebug & kIOLogServiceTree) {
883*27b03b36SApple OSS Distributions 					IOLog("\nService plane:\n");
884*27b03b36SApple OSS Distributions 					IOPrintPlane( gIOServicePlane );
885*27b03b36SApple OSS Distributions 				}
886*27b03b36SApple OSS Distributions 				if (gIOKitDebug & kIOLogMemory) {
887*27b03b36SApple OSS Distributions 					IOPrintMemory();
888*27b03b36SApple OSS Distributions 				}
889*27b03b36SApple OSS Distributions 			}
890*27b03b36SApple OSS Distributions 
891*27b03b36SApple OSS Distributions #if XNU_TARGET_OS_OSX && defined(__arm64__)
892*27b03b36SApple OSS Distributions 			// The disk isn't found - have the user pick from System Recovery.
893*27b03b36SApple OSS Distributions 			(void)IOSetRecoveryBoot(BSD_BOOTFAIL_MEDIA_MISSING, NULL, true);
894*27b03b36SApple OSS Distributions #endif
895*27b03b36SApple OSS Distributions 		}
896*27b03b36SApple OSS Distributions 	} while (!service);
897*27b03b36SApple OSS Distributions 
898*27b03b36SApple OSS Distributions 	OSSafeReleaseNULL(matching);
899*27b03b36SApple OSS Distributions 
900*27b03b36SApple OSS Distributions 	if (service && mediaProperty) {
901*27b03b36SApple OSS Distributions 		service = (IOService *)service->getProperty(mediaProperty);
902*27b03b36SApple OSS Distributions 	}
903*27b03b36SApple OSS Distributions 
904*27b03b36SApple OSS Distributions 	mjr = 0;
905*27b03b36SApple OSS Distributions 	mnr = 0;
906*27b03b36SApple OSS Distributions 
907*27b03b36SApple OSS Distributions 	// If the IOService we matched to is a subclass of IONetworkInterface,
908*27b03b36SApple OSS Distributions 	// then make sure it has been registered with BSD and has a BSD name
909*27b03b36SApple OSS Distributions 	// assigned.
910*27b03b36SApple OSS Distributions 
911*27b03b36SApple OSS Distributions 	if (service
912*27b03b36SApple OSS Distributions 	    && service->metaCast( "IONetworkInterface" )
913*27b03b36SApple OSS Distributions 	    && !IORegisterNetworkInterface( service )) {
914*27b03b36SApple OSS Distributions 		service = NULL;
915*27b03b36SApple OSS Distributions 	}
916*27b03b36SApple OSS Distributions 
917*27b03b36SApple OSS Distributions 	if (service) {
918*27b03b36SApple OSS Distributions 		len = kMaxPathBuf;
919*27b03b36SApple OSS Distributions 		service->getPath( str.data(), &len, gIOServicePlane );
920*27b03b36SApple OSS Distributions 		IOLog("Got boot device = %s\n", str.data());
921*27b03b36SApple OSS Distributions 
922*27b03b36SApple OSS Distributions 		iostr = (OSString *) service->getProperty( kIOBSDNameKey );
923*27b03b36SApple OSS Distributions 		if (iostr) {
924*27b03b36SApple OSS Distributions 			strlcpy( rootName, iostr->getCStringNoCopy(), rootNameSize );
925*27b03b36SApple OSS Distributions 		}
926*27b03b36SApple OSS Distributions 		off = (OSNumber *) service->getProperty( kIOBSDMajorKey );
927*27b03b36SApple OSS Distributions 		if (off) {
928*27b03b36SApple OSS Distributions 			mjr = off->unsigned32BitValue();
929*27b03b36SApple OSS Distributions 		}
930*27b03b36SApple OSS Distributions 		off = (OSNumber *) service->getProperty( kIOBSDMinorKey );
931*27b03b36SApple OSS Distributions 		if (off) {
932*27b03b36SApple OSS Distributions 			mnr = off->unsigned32BitValue();
933*27b03b36SApple OSS Distributions 		}
934*27b03b36SApple OSS Distributions 
935*27b03b36SApple OSS Distributions 		if (service->metaCast( "IONetworkInterface" )) {
936*27b03b36SApple OSS Distributions 			flags |= 1;
937*27b03b36SApple OSS Distributions 		}
938*27b03b36SApple OSS Distributions 	} else {
939*27b03b36SApple OSS Distributions 		IOLog( "Wait for root failed\n" );
940*27b03b36SApple OSS Distributions 		strlcpy( rootName, "en0", rootNameSize );
941*27b03b36SApple OSS Distributions 		flags |= 1;
942*27b03b36SApple OSS Distributions 	}
943*27b03b36SApple OSS Distributions 
944*27b03b36SApple OSS Distributions 	IOLog( "BSD root: %s", rootName );
945*27b03b36SApple OSS Distributions 	if (mjr) {
946*27b03b36SApple OSS Distributions 		IOLog(", major %d, minor %d\n", mjr, mnr );
947*27b03b36SApple OSS Distributions 	} else {
948*27b03b36SApple OSS Distributions 		IOLog("\n");
949*27b03b36SApple OSS Distributions 	}
950*27b03b36SApple OSS Distributions 
951*27b03b36SApple OSS Distributions 	*root = makedev( mjr, mnr );
952*27b03b36SApple OSS Distributions 	*oflags = flags;
953*27b03b36SApple OSS Distributions 
954*27b03b36SApple OSS Distributions iofrootx:
955*27b03b36SApple OSS Distributions 
956*27b03b36SApple OSS Distributions 	IOService::setRootMedia(service);
957*27b03b36SApple OSS Distributions 
958*27b03b36SApple OSS Distributions 	if ((gIOKitDebug & (kIOLogDTree | kIOLogServiceTree | kIOLogMemory)) && !debugInfoPrintedOnce) {
959*27b03b36SApple OSS Distributions 		IOService::getPlatform()->waitQuiet();
960*27b03b36SApple OSS Distributions 		if (gIOKitDebug & kIOLogDTree) {
961*27b03b36SApple OSS Distributions 			IOLog("\nDT plane:\n");
962*27b03b36SApple OSS Distributions 			IOPrintPlane( gIODTPlane );
963*27b03b36SApple OSS Distributions 		}
964*27b03b36SApple OSS Distributions 		if (gIOKitDebug & kIOLogServiceTree) {
965*27b03b36SApple OSS Distributions 			IOLog("\nService plane:\n");
966*27b03b36SApple OSS Distributions 			IOPrintPlane( gIOServicePlane );
967*27b03b36SApple OSS Distributions 		}
968*27b03b36SApple OSS Distributions 		if (gIOKitDebug & kIOLogMemory) {
969*27b03b36SApple OSS Distributions 			IOPrintMemory();
970*27b03b36SApple OSS Distributions 		}
971*27b03b36SApple OSS Distributions 	}
972*27b03b36SApple OSS Distributions 
973*27b03b36SApple OSS Distributions 	return kIOReturnSuccess;
974*27b03b36SApple OSS Distributions }
975*27b03b36SApple OSS Distributions 
976*27b03b36SApple OSS Distributions void
IOSetImageBoot(void)977*27b03b36SApple OSS Distributions IOSetImageBoot(void)
978*27b03b36SApple OSS Distributions {
979*27b03b36SApple OSS Distributions 	// this will unhide all IOMedia, without waiting for kernelmanagement to start
980*27b03b36SApple OSS Distributions 	IOService::setRootMedia(NULL);
981*27b03b36SApple OSS Distributions }
982*27b03b36SApple OSS Distributions 
983*27b03b36SApple OSS Distributions bool
IORamDiskBSDRoot(void)984*27b03b36SApple OSS Distributions IORamDiskBSDRoot(void)
985*27b03b36SApple OSS Distributions {
986*27b03b36SApple OSS Distributions 	char rdBootVar[kMaxBootVar];
987*27b03b36SApple OSS Distributions 	if (PE_parse_boot_argn("rd", rdBootVar, kMaxBootVar )
988*27b03b36SApple OSS Distributions 	    || PE_parse_boot_argn("rootdev", rdBootVar, kMaxBootVar )) {
989*27b03b36SApple OSS Distributions 		if ((rdBootVar[0] == 'm') && (rdBootVar[1] == 'd') && (rdBootVar[3] == 0)) {
990*27b03b36SApple OSS Distributions 			return true;
991*27b03b36SApple OSS Distributions 		}
992*27b03b36SApple OSS Distributions 	}
993*27b03b36SApple OSS Distributions 	return false;
994*27b03b36SApple OSS Distributions }
995*27b03b36SApple OSS Distributions 
996*27b03b36SApple OSS Distributions void
IOSecureBSDRoot(const char * rootName)997*27b03b36SApple OSS Distributions IOSecureBSDRoot(const char * rootName)
998*27b03b36SApple OSS Distributions {
999*27b03b36SApple OSS Distributions #if CONFIG_SECURE_BSD_ROOT
1000*27b03b36SApple OSS Distributions 	IOReturn         result;
1001*27b03b36SApple OSS Distributions 	IOPlatformExpert *pe;
1002*27b03b36SApple OSS Distributions 	OSDictionary     *matching;
1003*27b03b36SApple OSS Distributions 	const OSSymbol   *functionName = OSSymbol::withCStringNoCopy("SecureRootName");
1004*27b03b36SApple OSS Distributions 
1005*27b03b36SApple OSS Distributions 	matching = IOService::serviceMatching("IOPlatformExpert");
1006*27b03b36SApple OSS Distributions 	assert(matching);
1007*27b03b36SApple OSS Distributions 	pe = (IOPlatformExpert *) IOService::waitForMatchingService(matching, 30ULL * kSecondScale);
1008*27b03b36SApple OSS Distributions 	matching->release();
1009*27b03b36SApple OSS Distributions 	assert(pe);
1010*27b03b36SApple OSS Distributions 	// Returns kIOReturnNotPrivileged is the root device is not secure.
1011*27b03b36SApple OSS Distributions 	// Returns kIOReturnUnsupported if "SecureRootName" is not implemented.
1012*27b03b36SApple OSS Distributions 	result = pe->callPlatformFunction(functionName, false, (void *)rootName, (void *)NULL, (void *)NULL, (void *)NULL);
1013*27b03b36SApple OSS Distributions 	functionName->release();
1014*27b03b36SApple OSS Distributions 	OSSafeReleaseNULL(pe);
1015*27b03b36SApple OSS Distributions 
1016*27b03b36SApple OSS Distributions 	if (result == kIOReturnNotPrivileged) {
1017*27b03b36SApple OSS Distributions 		mdevremoveall();
1018*27b03b36SApple OSS Distributions 	}
1019*27b03b36SApple OSS Distributions 
1020*27b03b36SApple OSS Distributions #endif  // CONFIG_SECURE_BSD_ROOT
1021*27b03b36SApple OSS Distributions }
1022*27b03b36SApple OSS Distributions 
1023*27b03b36SApple OSS Distributions void *
IOBSDRegistryEntryForDeviceTree(char * path)1024*27b03b36SApple OSS Distributions IOBSDRegistryEntryForDeviceTree(char * path)
1025*27b03b36SApple OSS Distributions {
1026*27b03b36SApple OSS Distributions 	return IORegistryEntry::fromPath(path, gIODTPlane);
1027*27b03b36SApple OSS Distributions }
1028*27b03b36SApple OSS Distributions 
1029*27b03b36SApple OSS Distributions void
IOBSDRegistryEntryRelease(void * entry)1030*27b03b36SApple OSS Distributions IOBSDRegistryEntryRelease(void * entry)
1031*27b03b36SApple OSS Distributions {
1032*27b03b36SApple OSS Distributions 	IORegistryEntry * regEntry = (IORegistryEntry *)entry;
1033*27b03b36SApple OSS Distributions 
1034*27b03b36SApple OSS Distributions 	if (regEntry) {
1035*27b03b36SApple OSS Distributions 		regEntry->release();
1036*27b03b36SApple OSS Distributions 	}
1037*27b03b36SApple OSS Distributions 	return;
1038*27b03b36SApple OSS Distributions }
1039*27b03b36SApple OSS Distributions 
1040*27b03b36SApple OSS Distributions const void *
IOBSDRegistryEntryGetData(void * entry,char * property_name,int * packet_length)1041*27b03b36SApple OSS Distributions IOBSDRegistryEntryGetData(void * entry, char * property_name,
1042*27b03b36SApple OSS Distributions     int * packet_length)
1043*27b03b36SApple OSS Distributions {
1044*27b03b36SApple OSS Distributions 	OSData *            data;
1045*27b03b36SApple OSS Distributions 	IORegistryEntry *   regEntry = (IORegistryEntry *)entry;
1046*27b03b36SApple OSS Distributions 
1047*27b03b36SApple OSS Distributions 	data = (OSData *) regEntry->getProperty(property_name);
1048*27b03b36SApple OSS Distributions 	if (data) {
1049*27b03b36SApple OSS Distributions 		*packet_length = data->getLength();
1050*27b03b36SApple OSS Distributions 		return data->getBytesNoCopy();
1051*27b03b36SApple OSS Distributions 	}
1052*27b03b36SApple OSS Distributions 	return NULL;
1053*27b03b36SApple OSS Distributions }
1054*27b03b36SApple OSS Distributions 
1055*27b03b36SApple OSS Distributions kern_return_t
IOBSDGetPlatformUUID(uuid_t uuid,mach_timespec_t timeout)1056*27b03b36SApple OSS Distributions IOBSDGetPlatformUUID( uuid_t uuid, mach_timespec_t timeout )
1057*27b03b36SApple OSS Distributions {
1058*27b03b36SApple OSS Distributions 	IOService * resources;
1059*27b03b36SApple OSS Distributions 	OSString *  string;
1060*27b03b36SApple OSS Distributions 
1061*27b03b36SApple OSS Distributions 	resources = IOService::waitForService( IOService::resourceMatching( kIOPlatformUUIDKey ), (timeout.tv_sec || timeout.tv_nsec) ? &timeout : NULL );
1062*27b03b36SApple OSS Distributions 	if (resources == NULL) {
1063*27b03b36SApple OSS Distributions 		return KERN_OPERATION_TIMED_OUT;
1064*27b03b36SApple OSS Distributions 	}
1065*27b03b36SApple OSS Distributions 
1066*27b03b36SApple OSS Distributions 	string = (OSString *) IOService::getPlatform()->getProvider()->getProperty( kIOPlatformUUIDKey );
1067*27b03b36SApple OSS Distributions 	if (string == NULL) {
1068*27b03b36SApple OSS Distributions 		return KERN_NOT_SUPPORTED;
1069*27b03b36SApple OSS Distributions 	}
1070*27b03b36SApple OSS Distributions 
1071*27b03b36SApple OSS Distributions 	uuid_parse( string->getCStringNoCopy(), uuid );
1072*27b03b36SApple OSS Distributions 
1073*27b03b36SApple OSS Distributions 	return KERN_SUCCESS;
1074*27b03b36SApple OSS Distributions }
1075*27b03b36SApple OSS Distributions } /* extern "C" */
1076*27b03b36SApple OSS Distributions 
1077*27b03b36SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1078*27b03b36SApple OSS Distributions 
1079*27b03b36SApple OSS Distributions #include <sys/conf.h>
1080*27b03b36SApple OSS Distributions #include <sys/vnode.h>
1081*27b03b36SApple OSS Distributions #include <sys/vnode_internal.h>
1082*27b03b36SApple OSS Distributions #include <sys/fcntl.h>
1083*27b03b36SApple OSS Distributions #include <IOKit/IOPolledInterface.h>
1084*27b03b36SApple OSS Distributions #include <IOKit/IOBufferMemoryDescriptor.h>
1085*27b03b36SApple OSS Distributions 
1086*27b03b36SApple OSS Distributions IOPolledFileIOVars * gIOPolledCoreFileVars;
1087*27b03b36SApple OSS Distributions kern_return_t gIOPolledCoreFileOpenRet = kIOReturnNotReady;
1088*27b03b36SApple OSS Distributions IOPolledCoreFileMode_t gIOPolledCoreFileMode = kIOPolledCoreFileModeNotInitialized;
1089*27b03b36SApple OSS Distributions 
1090*27b03b36SApple OSS Distributions #if IOPOLLED_COREFILE
1091*27b03b36SApple OSS Distributions 
1092*27b03b36SApple OSS Distributions #if defined(XNU_TARGET_OS_BRIDGE)
1093*27b03b36SApple OSS Distributions // On bridgeOS allocate a 150MB corefile and leave 150MB free
1094*27b03b36SApple OSS Distributions #define kIOCoreDumpSize         150ULL*1024ULL*1024ULL
1095*27b03b36SApple OSS Distributions #define kIOCoreDumpFreeSize     150ULL*1024ULL*1024ULL
1096*27b03b36SApple OSS Distributions 
1097*27b03b36SApple OSS Distributions #elif !defined(XNU_TARGET_OS_OSX) /* defined(XNU_TARGET_OS_BRIDGE) */
1098*27b03b36SApple OSS Distributions // On embedded devices with >3GB DRAM we allocate a 500MB corefile
1099*27b03b36SApple OSS Distributions // otherwise allocate a 350MB corefile. Leave 350 MB free
1100*27b03b36SApple OSS Distributions 
1101*27b03b36SApple OSS Distributions #define kIOCoreDumpMinSize      350ULL*1024ULL*1024ULL
1102*27b03b36SApple OSS Distributions #define kIOCoreDumpLargeSize    500ULL*1024ULL*1024ULL
1103*27b03b36SApple OSS Distributions 
1104*27b03b36SApple OSS Distributions #define kIOCoreDumpFreeSize     350ULL*1024ULL*1024ULL
1105*27b03b36SApple OSS Distributions 
1106*27b03b36SApple OSS Distributions #else /* defined(XNU_TARGET_OS_BRIDGE) */
1107*27b03b36SApple OSS Distributions // on macOS devices allocate a corefile sized at 1GB / 32GB of DRAM,
1108*27b03b36SApple OSS Distributions // fallback to a 1GB corefile and leave at least 1GB free
1109*27b03b36SApple OSS Distributions #define kIOCoreDumpMinSize              1024ULL*1024ULL*1024ULL
1110*27b03b36SApple OSS Distributions #define kIOCoreDumpIncrementalSize      1024ULL*1024ULL*1024ULL
1111*27b03b36SApple OSS Distributions 
1112*27b03b36SApple OSS Distributions #define kIOCoreDumpFreeSize     1024ULL*1024ULL*1024ULL
1113*27b03b36SApple OSS Distributions 
1114*27b03b36SApple OSS Distributions // on older macOS devices we allocate a 1MB file at boot
1115*27b03b36SApple OSS Distributions // to store a panic time stackshot
1116*27b03b36SApple OSS Distributions #define kIOStackshotFileSize    1024ULL*1024ULL
1117*27b03b36SApple OSS Distributions 
1118*27b03b36SApple OSS Distributions #endif /* defined(XNU_TARGET_OS_BRIDGE) */
1119*27b03b36SApple OSS Distributions 
1120*27b03b36SApple OSS Distributions static IOPolledCoreFileMode_t
GetCoreFileMode()1121*27b03b36SApple OSS Distributions GetCoreFileMode()
1122*27b03b36SApple OSS Distributions {
1123*27b03b36SApple OSS Distributions 	if (on_device_corefile_enabled()) {
1124*27b03b36SApple OSS Distributions 		return kIOPolledCoreFileModeCoredump;
1125*27b03b36SApple OSS Distributions 	} else if (panic_stackshot_to_disk_enabled()) {
1126*27b03b36SApple OSS Distributions 		return kIOPolledCoreFileModeStackshot;
1127*27b03b36SApple OSS Distributions 	} else {
1128*27b03b36SApple OSS Distributions 		return kIOPolledCoreFileModeDisabled;
1129*27b03b36SApple OSS Distributions 	}
1130*27b03b36SApple OSS Distributions }
1131*27b03b36SApple OSS Distributions 
1132*27b03b36SApple OSS Distributions static void
IOCoreFileGetSize(uint64_t * ideal_size,uint64_t * fallback_size,uint64_t * free_space_to_leave,IOPolledCoreFileMode_t mode)1133*27b03b36SApple OSS Distributions IOCoreFileGetSize(uint64_t *ideal_size, uint64_t *fallback_size, uint64_t *free_space_to_leave, IOPolledCoreFileMode_t mode)
1134*27b03b36SApple OSS Distributions {
1135*27b03b36SApple OSS Distributions 	unsigned int requested_corefile_size = 0;
1136*27b03b36SApple OSS Distributions 
1137*27b03b36SApple OSS Distributions 	*ideal_size = *fallback_size = *free_space_to_leave = 0;
1138*27b03b36SApple OSS Distributions 
1139*27b03b36SApple OSS Distributions #if defined(XNU_TARGET_OS_BRIDGE)
1140*27b03b36SApple OSS Distributions #pragma unused(mode)
1141*27b03b36SApple OSS Distributions 	*ideal_size = *fallback_size = kIOCoreDumpSize;
1142*27b03b36SApple OSS Distributions 	*free_space_to_leave = kIOCoreDumpFreeSize;
1143*27b03b36SApple OSS Distributions #elif !defined(XNU_TARGET_OS_OSX) /* defined(XNU_TARGET_OS_BRIDGE) */
1144*27b03b36SApple OSS Distributions #pragma unused(mode)
1145*27b03b36SApple OSS Distributions 	*ideal_size = *fallback_size = kIOCoreDumpMinSize;
1146*27b03b36SApple OSS Distributions 
1147*27b03b36SApple OSS Distributions 	if (max_mem > (3 * 1024ULL * 1024ULL * 1024ULL)) {
1148*27b03b36SApple OSS Distributions 		*ideal_size = kIOCoreDumpLargeSize;
1149*27b03b36SApple OSS Distributions 	}
1150*27b03b36SApple OSS Distributions 
1151*27b03b36SApple OSS Distributions 	*free_space_to_leave = kIOCoreDumpFreeSize;
1152*27b03b36SApple OSS Distributions #else /* defined(XNU_TARGET_OS_BRIDGE) */
1153*27b03b36SApple OSS Distributions 	if (mode == kIOPolledCoreFileModeCoredump) {
1154*27b03b36SApple OSS Distributions 		*ideal_size = *fallback_size = kIOCoreDumpMinSize;
1155*27b03b36SApple OSS Distributions 		if (kIOCoreDumpIncrementalSize != 0 && max_mem > (32 * 1024ULL * 1024ULL * 1024ULL)) {
1156*27b03b36SApple OSS Distributions 			*ideal_size = ((ROUNDUP(max_mem, (32 * 1024ULL * 1024ULL * 1024ULL)) / (32 * 1024ULL * 1024ULL * 1024ULL)) * kIOCoreDumpIncrementalSize);
1157*27b03b36SApple OSS Distributions 		}
1158*27b03b36SApple OSS Distributions 		*free_space_to_leave = kIOCoreDumpFreeSize;
1159*27b03b36SApple OSS Distributions 	} else if (mode == kIOPolledCoreFileModeStackshot) {
1160*27b03b36SApple OSS Distributions 		*ideal_size = *fallback_size = *free_space_to_leave = kIOStackshotFileSize;
1161*27b03b36SApple OSS Distributions 	}
1162*27b03b36SApple OSS Distributions #endif /* defined(XNU_TARGET_OS_BRIDGE) */
1163*27b03b36SApple OSS Distributions 	// If a custom size was requested, override the ideal and requested sizes
1164*27b03b36SApple OSS Distributions 	if (PE_parse_boot_argn("corefile_size_mb", &requested_corefile_size, sizeof(requested_corefile_size))) {
1165*27b03b36SApple OSS Distributions 		IOLog("Boot-args specify %d MB kernel corefile\n", requested_corefile_size);
1166*27b03b36SApple OSS Distributions 
1167*27b03b36SApple OSS Distributions 		*ideal_size = *fallback_size = (requested_corefile_size * 1024ULL * 1024ULL);
1168*27b03b36SApple OSS Distributions 	}
1169*27b03b36SApple OSS Distributions 
1170*27b03b36SApple OSS Distributions 	return;
1171*27b03b36SApple OSS Distributions }
1172*27b03b36SApple OSS Distributions 
1173*27b03b36SApple OSS Distributions static IOReturn
IOAccessCoreFileData(void * context,boolean_t write,uint64_t offset,int length,void * buffer)1174*27b03b36SApple OSS Distributions IOAccessCoreFileData(void *context, boolean_t write, uint64_t offset, int length, void *buffer)
1175*27b03b36SApple OSS Distributions {
1176*27b03b36SApple OSS Distributions 	errno_t vnode_error = 0;
1177*27b03b36SApple OSS Distributions 	vfs_context_t vfs_context;
1178*27b03b36SApple OSS Distributions 	vnode_t vnode_ptr = (vnode_t) context;
1179*27b03b36SApple OSS Distributions 
1180*27b03b36SApple OSS Distributions 	vfs_context = vfs_context_kernel();
1181*27b03b36SApple OSS Distributions 	vnode_error = vn_rdwr(write ? UIO_WRITE : UIO_READ, vnode_ptr, (caddr_t)buffer, length, offset,
1182*27b03b36SApple OSS Distributions 	    UIO_SYSSPACE, IO_SWAP_DISPATCH | IO_SYNC | IO_NOCACHE | IO_UNIT, vfs_context_ucred(vfs_context), NULL, vfs_context_proc(vfs_context));
1183*27b03b36SApple OSS Distributions 
1184*27b03b36SApple OSS Distributions 	if (vnode_error) {
1185*27b03b36SApple OSS Distributions 		IOLog("Failed to %s the corefile. Error %d\n", write ? "write to" : "read from", vnode_error);
1186*27b03b36SApple OSS Distributions 		return kIOReturnError;
1187*27b03b36SApple OSS Distributions 	}
1188*27b03b36SApple OSS Distributions 
1189*27b03b36SApple OSS Distributions 	return kIOReturnSuccess;
1190*27b03b36SApple OSS Distributions }
1191*27b03b36SApple OSS Distributions 
1192*27b03b36SApple OSS Distributions static void
IOOpenPolledCoreFile(thread_call_param_t __unused,thread_call_param_t corefilename)1193*27b03b36SApple OSS Distributions IOOpenPolledCoreFile(thread_call_param_t __unused, thread_call_param_t corefilename)
1194*27b03b36SApple OSS Distributions {
1195*27b03b36SApple OSS Distributions 	assert(corefilename != NULL);
1196*27b03b36SApple OSS Distributions 
1197*27b03b36SApple OSS Distributions 	IOReturn err;
1198*27b03b36SApple OSS Distributions 	char *filename = (char *) corefilename;
1199*27b03b36SApple OSS Distributions 	uint64_t corefile_size_bytes = 0, corefile_fallback_size_bytes = 0, free_space_to_leave_bytes = 0;
1200*27b03b36SApple OSS Distributions 	IOPolledCoreFileMode_t mode_to_init = GetCoreFileMode();
1201*27b03b36SApple OSS Distributions 
1202*27b03b36SApple OSS Distributions 	if (gIOPolledCoreFileVars) {
1203*27b03b36SApple OSS Distributions 		return;
1204*27b03b36SApple OSS Distributions 	}
1205*27b03b36SApple OSS Distributions 	if (!IOPolledInterface::gMetaClass.getInstanceCount()) {
1206*27b03b36SApple OSS Distributions 		return;
1207*27b03b36SApple OSS Distributions 	}
1208*27b03b36SApple OSS Distributions 
1209*27b03b36SApple OSS Distributions 	if (mode_to_init == kIOPolledCoreFileModeDisabled) {
1210*27b03b36SApple OSS Distributions 		gIOPolledCoreFileMode = kIOPolledCoreFileModeDisabled;
1211*27b03b36SApple OSS Distributions 		return;
1212*27b03b36SApple OSS Distributions 	}
1213*27b03b36SApple OSS Distributions 
1214*27b03b36SApple OSS Distributions 	// We'll overwrite this once we open the file, we update this to mark that we have made
1215*27b03b36SApple OSS Distributions 	// it past initialization
1216*27b03b36SApple OSS Distributions 	gIOPolledCoreFileMode = kIOPolledCoreFileModeClosed;
1217*27b03b36SApple OSS Distributions 
1218*27b03b36SApple OSS Distributions 	IOCoreFileGetSize(&corefile_size_bytes, &corefile_fallback_size_bytes, &free_space_to_leave_bytes, mode_to_init);
1219*27b03b36SApple OSS Distributions 
1220*27b03b36SApple OSS Distributions 	do {
1221*27b03b36SApple OSS Distributions 		err = IOPolledFileOpen(filename, kIOPolledFileCreate, corefile_size_bytes, free_space_to_leave_bytes,
1222*27b03b36SApple OSS Distributions 		    NULL, 0, &gIOPolledCoreFileVars, NULL, NULL, NULL);
1223*27b03b36SApple OSS Distributions 		if (kIOReturnSuccess == err) {
1224*27b03b36SApple OSS Distributions 			break;
1225*27b03b36SApple OSS Distributions 		} else if (kIOReturnNoSpace == err) {
1226*27b03b36SApple OSS Distributions 			IOLog("Failed to open corefile of size %llu MB (low disk space)",
1227*27b03b36SApple OSS Distributions 			    (corefile_size_bytes / (1024ULL * 1024ULL)));
1228*27b03b36SApple OSS Distributions 			if (corefile_size_bytes == corefile_fallback_size_bytes) {
1229*27b03b36SApple OSS Distributions 				gIOPolledCoreFileOpenRet = err;
1230*27b03b36SApple OSS Distributions 				return;
1231*27b03b36SApple OSS Distributions 			}
1232*27b03b36SApple OSS Distributions 		} else {
1233*27b03b36SApple OSS Distributions 			IOLog("Failed to open corefile of size %llu MB (returned error 0x%x)\n",
1234*27b03b36SApple OSS Distributions 			    (corefile_size_bytes / (1024ULL * 1024ULL)), err);
1235*27b03b36SApple OSS Distributions 			gIOPolledCoreFileOpenRet = err;
1236*27b03b36SApple OSS Distributions 			return;
1237*27b03b36SApple OSS Distributions 		}
1238*27b03b36SApple OSS Distributions 
1239*27b03b36SApple OSS Distributions 		err = IOPolledFileOpen(filename, kIOPolledFileCreate, corefile_fallback_size_bytes, free_space_to_leave_bytes,
1240*27b03b36SApple OSS Distributions 		    NULL, 0, &gIOPolledCoreFileVars, NULL, NULL, NULL);
1241*27b03b36SApple OSS Distributions 		if (kIOReturnSuccess != err) {
1242*27b03b36SApple OSS Distributions 			IOLog("Failed to open corefile of size %llu MB (returned error 0x%x)\n",
1243*27b03b36SApple OSS Distributions 			    (corefile_fallback_size_bytes / (1024ULL * 1024ULL)), err);
1244*27b03b36SApple OSS Distributions 			gIOPolledCoreFileOpenRet = err;
1245*27b03b36SApple OSS Distributions 			return;
1246*27b03b36SApple OSS Distributions 		}
1247*27b03b36SApple OSS Distributions 	} while (false);
1248*27b03b36SApple OSS Distributions 
1249*27b03b36SApple OSS Distributions 	gIOPolledCoreFileOpenRet = IOPolledFilePollersSetup(gIOPolledCoreFileVars, kIOPolledPreflightCoreDumpState);
1250*27b03b36SApple OSS Distributions 	if (kIOReturnSuccess != gIOPolledCoreFileOpenRet) {
1251*27b03b36SApple OSS Distributions 		IOPolledFileClose(&gIOPolledCoreFileVars, 0, NULL, 0, 0, 0);
1252*27b03b36SApple OSS Distributions 		IOLog("IOPolledFilePollersSetup for corefile failed with error: 0x%x\n", err);
1253*27b03b36SApple OSS Distributions 	} else {
1254*27b03b36SApple OSS Distributions 		IOLog("Opened corefile of size %llu MB\n", (corefile_size_bytes / (1024ULL * 1024ULL)));
1255*27b03b36SApple OSS Distributions 		gIOPolledCoreFileMode = mode_to_init;
1256*27b03b36SApple OSS Distributions 	}
1257*27b03b36SApple OSS Distributions 
1258*27b03b36SApple OSS Distributions 	// Provide the "polled file available" callback with a temporary way to read from the file
1259*27b03b36SApple OSS Distributions 	(void) IOProvideCoreFileAccess(kdp_core_polled_io_polled_file_available, NULL);
1260*27b03b36SApple OSS Distributions 
1261*27b03b36SApple OSS Distributions 	return;
1262*27b03b36SApple OSS Distributions }
1263*27b03b36SApple OSS Distributions 
1264*27b03b36SApple OSS Distributions kern_return_t
IOProvideCoreFileAccess(IOCoreFileAccessRecipient recipient,void * recipient_context)1265*27b03b36SApple OSS Distributions IOProvideCoreFileAccess(IOCoreFileAccessRecipient recipient, void *recipient_context)
1266*27b03b36SApple OSS Distributions {
1267*27b03b36SApple OSS Distributions 	kern_return_t error = kIOReturnSuccess;
1268*27b03b36SApple OSS Distributions 	errno_t vnode_error = 0;
1269*27b03b36SApple OSS Distributions 	vfs_context_t vfs_context;
1270*27b03b36SApple OSS Distributions 	vnode_t vnode_ptr;
1271*27b03b36SApple OSS Distributions 
1272*27b03b36SApple OSS Distributions 	if (!recipient) {
1273*27b03b36SApple OSS Distributions 		return kIOReturnBadArgument;
1274*27b03b36SApple OSS Distributions 	}
1275*27b03b36SApple OSS Distributions 
1276*27b03b36SApple OSS Distributions 	if (kIOReturnSuccess != gIOPolledCoreFileOpenRet) {
1277*27b03b36SApple OSS Distributions 		return kIOReturnNotReady;
1278*27b03b36SApple OSS Distributions 	}
1279*27b03b36SApple OSS Distributions 
1280*27b03b36SApple OSS Distributions 	// Open the kernel corefile
1281*27b03b36SApple OSS Distributions 	vfs_context = vfs_context_kernel();
1282*27b03b36SApple OSS Distributions 	vnode_error = vnode_open(kIOCoreDumpPath, (FREAD | FWRITE | O_NOFOLLOW), 0600, 0, &vnode_ptr, vfs_context);
1283*27b03b36SApple OSS Distributions 	if (vnode_error) {
1284*27b03b36SApple OSS Distributions 		IOLog("Failed to open the corefile. Error %d\n", vnode_error);
1285*27b03b36SApple OSS Distributions 		return kIOReturnError;
1286*27b03b36SApple OSS Distributions 	}
1287*27b03b36SApple OSS Distributions 
1288*27b03b36SApple OSS Distributions 	// Call the recipient function
1289*27b03b36SApple OSS Distributions 	error = recipient(IOAccessCoreFileData, (void *)vnode_ptr, recipient_context);
1290*27b03b36SApple OSS Distributions 
1291*27b03b36SApple OSS Distributions 	// Close the kernel corefile
1292*27b03b36SApple OSS Distributions 	vnode_close(vnode_ptr, FREAD | FWRITE, vfs_context);
1293*27b03b36SApple OSS Distributions 
1294*27b03b36SApple OSS Distributions 	return error;
1295*27b03b36SApple OSS Distributions }
1296*27b03b36SApple OSS Distributions 
1297*27b03b36SApple OSS Distributions static void
IOClosePolledCoreFile(void)1298*27b03b36SApple OSS Distributions IOClosePolledCoreFile(void)
1299*27b03b36SApple OSS Distributions {
1300*27b03b36SApple OSS Distributions 	// Notify kdp core that the corefile is no longer available
1301*27b03b36SApple OSS Distributions 	(void) kdp_core_polled_io_polled_file_unavailable();
1302*27b03b36SApple OSS Distributions 
1303*27b03b36SApple OSS Distributions 	gIOPolledCoreFileOpenRet = kIOReturnNotOpen;
1304*27b03b36SApple OSS Distributions 	gIOPolledCoreFileMode = kIOPolledCoreFileModeClosed;
1305*27b03b36SApple OSS Distributions 	IOPolledFilePollersClose(gIOPolledCoreFileVars, kIOPolledPostflightCoreDumpState);
1306*27b03b36SApple OSS Distributions 	IOPolledFileClose(&gIOPolledCoreFileVars, 0, NULL, 0, 0, 0);
1307*27b03b36SApple OSS Distributions }
1308*27b03b36SApple OSS Distributions 
1309*27b03b36SApple OSS Distributions #endif /* IOPOLLED_COREFILE */
1310*27b03b36SApple OSS Distributions 
1311*27b03b36SApple OSS Distributions extern "C" void
IOBSDMountChange(struct mount * mp,uint32_t op)1312*27b03b36SApple OSS Distributions IOBSDMountChange(struct mount * mp, uint32_t op)
1313*27b03b36SApple OSS Distributions {
1314*27b03b36SApple OSS Distributions #if IOPOLLED_COREFILE
1315*27b03b36SApple OSS Distributions 	uint64_t flags;
1316*27b03b36SApple OSS Distributions 	char path[128];
1317*27b03b36SApple OSS Distributions 	int pathLen;
1318*27b03b36SApple OSS Distributions 	vnode_t vn;
1319*27b03b36SApple OSS Distributions 	int result;
1320*27b03b36SApple OSS Distributions 
1321*27b03b36SApple OSS Distributions 	switch (op) {
1322*27b03b36SApple OSS Distributions 	case kIOMountChangeMount:
1323*27b03b36SApple OSS Distributions 	case kIOMountChangeDidResize:
1324*27b03b36SApple OSS Distributions 
1325*27b03b36SApple OSS Distributions 		if (gIOPolledCoreFileVars) {
1326*27b03b36SApple OSS Distributions 			break;
1327*27b03b36SApple OSS Distributions 		}
1328*27b03b36SApple OSS Distributions 		flags = vfs_flags(mp);
1329*27b03b36SApple OSS Distributions 		if (MNT_RDONLY & flags) {
1330*27b03b36SApple OSS Distributions 			break;
1331*27b03b36SApple OSS Distributions 		}
1332*27b03b36SApple OSS Distributions 		if (!(MNT_LOCAL & flags)) {
1333*27b03b36SApple OSS Distributions 			break;
1334*27b03b36SApple OSS Distributions 		}
1335*27b03b36SApple OSS Distributions 
1336*27b03b36SApple OSS Distributions 		vn = vfs_vnodecovered(mp);
1337*27b03b36SApple OSS Distributions 		if (!vn) {
1338*27b03b36SApple OSS Distributions 			break;
1339*27b03b36SApple OSS Distributions 		}
1340*27b03b36SApple OSS Distributions 		pathLen = sizeof(path);
1341*27b03b36SApple OSS Distributions 		result = vn_getpath(vn, &path[0], &pathLen);
1342*27b03b36SApple OSS Distributions 		vnode_put(vn);
1343*27b03b36SApple OSS Distributions 		if (0 != result) {
1344*27b03b36SApple OSS Distributions 			break;
1345*27b03b36SApple OSS Distributions 		}
1346*27b03b36SApple OSS Distributions 		if (!pathLen) {
1347*27b03b36SApple OSS Distributions 			break;
1348*27b03b36SApple OSS Distributions 		}
1349*27b03b36SApple OSS Distributions #if defined(XNU_TARGET_OS_BRIDGE)
1350*27b03b36SApple OSS Distributions 		// on bridgeOS systems we put the core in /private/var/internal. We don't
1351*27b03b36SApple OSS Distributions 		// want to match with /private/var because /private/var/internal is often mounted
1352*27b03b36SApple OSS Distributions 		// over /private/var
1353*27b03b36SApple OSS Distributions 		if ((pathLen - 1) < (int) strlen("/private/var/internal")) {
1354*27b03b36SApple OSS Distributions 			break;
1355*27b03b36SApple OSS Distributions 		}
1356*27b03b36SApple OSS Distributions #endif
1357*27b03b36SApple OSS Distributions 		if (0 != strncmp(path, kIOCoreDumpPath, pathLen - 1)) {
1358*27b03b36SApple OSS Distributions 			break;
1359*27b03b36SApple OSS Distributions 		}
1360*27b03b36SApple OSS Distributions 
1361*27b03b36SApple OSS Distributions 		thread_call_enter1(corefile_open_call, (void *) kIOCoreDumpPath);
1362*27b03b36SApple OSS Distributions 		break;
1363*27b03b36SApple OSS Distributions 
1364*27b03b36SApple OSS Distributions 	case kIOMountChangeUnmount:
1365*27b03b36SApple OSS Distributions 	case kIOMountChangeWillResize:
1366*27b03b36SApple OSS Distributions 		if (gIOPolledCoreFileVars && (mp == kern_file_mount(gIOPolledCoreFileVars->fileRef))) {
1367*27b03b36SApple OSS Distributions 			thread_call_cancel_wait(corefile_open_call);
1368*27b03b36SApple OSS Distributions 			IOClosePolledCoreFile();
1369*27b03b36SApple OSS Distributions 		}
1370*27b03b36SApple OSS Distributions 		break;
1371*27b03b36SApple OSS Distributions 	}
1372*27b03b36SApple OSS Distributions #endif /* IOPOLLED_COREFILE */
1373*27b03b36SApple OSS Distributions }
1374*27b03b36SApple OSS Distributions 
1375*27b03b36SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1376*27b03b36SApple OSS Distributions 
1377*27b03b36SApple OSS Distributions 
1378*27b03b36SApple OSS Distributions extern "C"
1379*27b03b36SApple OSS Distributions OS_ALWAYS_INLINE
1380*27b03b36SApple OSS Distributions boolean_t
IOCurrentTaskHasEntitlement(const char * entitlement)1381*27b03b36SApple OSS Distributions IOCurrentTaskHasEntitlement(const char * entitlement)
1382*27b03b36SApple OSS Distributions {
1383*27b03b36SApple OSS Distributions 	return IOTaskHasEntitlement(NULL, entitlement);
1384*27b03b36SApple OSS Distributions }
1385*27b03b36SApple OSS Distributions 
1386*27b03b36SApple OSS Distributions extern "C" boolean_t
IOTaskHasEntitlement(task_t task,const char * entitlement)1387*27b03b36SApple OSS Distributions IOTaskHasEntitlement(task_t task, const char * entitlement)
1388*27b03b36SApple OSS Distributions {
1389*27b03b36SApple OSS Distributions 	// Don't do this
1390*27b03b36SApple OSS Distributions 	if (task == kernel_task || entitlement == NULL) {
1391*27b03b36SApple OSS Distributions 		return false;
1392*27b03b36SApple OSS Distributions 	}
1393*27b03b36SApple OSS Distributions 	size_t entlen = strlen(entitlement);
1394*27b03b36SApple OSS Distributions 	CEQuery_t query = {
1395*27b03b36SApple OSS Distributions 		CESelectDictValueDynamic((const uint8_t*)entitlement, entlen),
1396*27b03b36SApple OSS Distributions 		CEMatchBool(true)
1397*27b03b36SApple OSS Distributions 	};
1398*27b03b36SApple OSS Distributions 
1399*27b03b36SApple OSS Distributions #if PMAP_CS_ENABLE && !CONFIG_X86_64_COMPAT
1400*27b03b36SApple OSS Distributions 	if (pmap_cs_enabled()) {
1401*27b03b36SApple OSS Distributions 		if (task == NULL || task == current_task()) {
1402*27b03b36SApple OSS Distributions 			// NULL task means current task, which translated to the current pmap
1403*27b03b36SApple OSS Distributions 			return pmap_query_entitlements(NULL, query, 2, NULL);
1404*27b03b36SApple OSS Distributions 		}
1405*27b03b36SApple OSS Distributions 		vm_map_t task_map = get_task_map_reference(task);
1406*27b03b36SApple OSS Distributions 		if (task_map) {
1407*27b03b36SApple OSS Distributions 			pmap_t pmap = vm_map_get_pmap(task_map);
1408*27b03b36SApple OSS Distributions 			if (pmap && pmap_query_entitlements(pmap, query, 2, NULL)) {
1409*27b03b36SApple OSS Distributions 				vm_map_deallocate(task_map);
1410*27b03b36SApple OSS Distributions 				return true;
1411*27b03b36SApple OSS Distributions 			}
1412*27b03b36SApple OSS Distributions 			vm_map_deallocate(task_map);
1413*27b03b36SApple OSS Distributions 		}
1414*27b03b36SApple OSS Distributions 		return false;
1415*27b03b36SApple OSS Distributions 	}
1416*27b03b36SApple OSS Distributions #endif
1417*27b03b36SApple OSS Distributions 	if (task == NULL) {
1418*27b03b36SApple OSS Distributions 		task = current_task();
1419*27b03b36SApple OSS Distributions 	}
1420*27b03b36SApple OSS Distributions 
1421*27b03b36SApple OSS Distributions 	proc_t p = (proc_t)get_bsdtask_info(task);
1422*27b03b36SApple OSS Distributions 
1423*27b03b36SApple OSS Distributions 	if (p == NULL) {
1424*27b03b36SApple OSS Distributions 		return false;
1425*27b03b36SApple OSS Distributions 	}
1426*27b03b36SApple OSS Distributions 
1427*27b03b36SApple OSS Distributions 	struct cs_blob* csblob = csproc_get_blob(p);
1428*27b03b36SApple OSS Distributions 	if (csblob == NULL) {
1429*27b03b36SApple OSS Distributions 		return false;
1430*27b03b36SApple OSS Distributions 	}
1431*27b03b36SApple OSS Distributions 
1432*27b03b36SApple OSS Distributions 	void* osents = csblob_os_entitlements_get(csblob);
1433*27b03b36SApple OSS Distributions 	if (osents == NULL) {
1434*27b03b36SApple OSS Distributions 		return false;
1435*27b03b36SApple OSS Distributions 	}
1436*27b03b36SApple OSS Distributions 
1437*27b03b36SApple OSS Distributions 	if (!amfi) {
1438*27b03b36SApple OSS Distributions 		panic("CoreEntitlements: (IOTask): No AMFI\n");
1439*27b03b36SApple OSS Distributions 	}
1440*27b03b36SApple OSS Distributions 
1441*27b03b36SApple OSS Distributions 	return amfi->OSEntitlements_query(osents, (uint8_t*)csblob_get_cdhash(csblob), query, 2) == amfi->CoreEntitlements.kNoError;
1442*27b03b36SApple OSS Distributions }
1443*27b03b36SApple OSS Distributions 
1444*27b03b36SApple OSS Distributions extern "C" boolean_t
IOVnodeHasEntitlement(vnode_t vnode,int64_t off,const char * entitlement)1445*27b03b36SApple OSS Distributions IOVnodeHasEntitlement(vnode_t vnode, int64_t off, const char *entitlement)
1446*27b03b36SApple OSS Distributions {
1447*27b03b36SApple OSS Distributions 	OSObject * obj;
1448*27b03b36SApple OSS Distributions 	off_t offset = (off_t)off;
1449*27b03b36SApple OSS Distributions 
1450*27b03b36SApple OSS Distributions 	obj = IOUserClient::copyClientEntitlementVnode(vnode, offset, entitlement);
1451*27b03b36SApple OSS Distributions 	if (!obj) {
1452*27b03b36SApple OSS Distributions 		return false;
1453*27b03b36SApple OSS Distributions 	}
1454*27b03b36SApple OSS Distributions 	obj->release();
1455*27b03b36SApple OSS Distributions 	return obj != kOSBooleanFalse;
1456*27b03b36SApple OSS Distributions }
1457*27b03b36SApple OSS Distributions 
1458*27b03b36SApple OSS Distributions extern "C" char *
IOVnodeGetEntitlement(vnode_t vnode,int64_t off,const char * entitlement)1459*27b03b36SApple OSS Distributions IOVnodeGetEntitlement(vnode_t vnode, int64_t off, const char *entitlement)
1460*27b03b36SApple OSS Distributions {
1461*27b03b36SApple OSS Distributions 	OSObject *obj = NULL;
1462*27b03b36SApple OSS Distributions 	OSString *str = NULL;
1463*27b03b36SApple OSS Distributions 	size_t len;
1464*27b03b36SApple OSS Distributions 	char *value = NULL;
1465*27b03b36SApple OSS Distributions 	off_t offset = (off_t)off;
1466*27b03b36SApple OSS Distributions 
1467*27b03b36SApple OSS Distributions 	obj = IOUserClient::copyClientEntitlementVnode(vnode, offset, entitlement);
1468*27b03b36SApple OSS Distributions 	if (obj != NULL) {
1469*27b03b36SApple OSS Distributions 		str = OSDynamicCast(OSString, obj);
1470*27b03b36SApple OSS Distributions 		if (str != NULL) {
1471*27b03b36SApple OSS Distributions 			len = str->getLength() + 1;
1472*27b03b36SApple OSS Distributions 			value = (char *)kalloc_data(len, Z_WAITOK);
1473*27b03b36SApple OSS Distributions 			strlcpy(value, str->getCStringNoCopy(), len);
1474*27b03b36SApple OSS Distributions 		}
1475*27b03b36SApple OSS Distributions 		obj->release();
1476*27b03b36SApple OSS Distributions 	}
1477*27b03b36SApple OSS Distributions 	return value;
1478*27b03b36SApple OSS Distributions }
1479