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