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