1c1dac77fSApple OSS Distributions /*
2e6231be0SApple OSS Distributions * Copyright (c) 1998-2021 Apple Inc. All rights reserved.
3c1dac77fSApple OSS Distributions *
4e13b1fa5SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5c1dac77fSApple OSS Distributions *
6e13b1fa5SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7e13b1fa5SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8e13b1fa5SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9e13b1fa5SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10e13b1fa5SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11e13b1fa5SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12e13b1fa5SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13e13b1fa5SApple OSS Distributions * terms of an Apple operating system software license agreement.
14c1dac77fSApple OSS Distributions *
15e13b1fa5SApple OSS Distributions * Please obtain a copy of the License at
16e13b1fa5SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17e13b1fa5SApple OSS Distributions *
18e13b1fa5SApple OSS Distributions * The Original Code and all software distributed under the License are
19e13b1fa5SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20c1dac77fSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21c1dac77fSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22e13b1fa5SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23e13b1fa5SApple OSS Distributions * Please see the License for the specific language governing rights and
24e13b1fa5SApple OSS Distributions * limitations under the License.
25c1dac77fSApple OSS Distributions *
26e13b1fa5SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27c1dac77fSApple OSS Distributions */
28c1dac77fSApple OSS Distributions #include <IOKit/IOBSD.h>
29c1dac77fSApple OSS Distributions #include <IOKit/IOLib.h>
30c1dac77fSApple OSS Distributions #include <IOKit/IOService.h>
31855239e5SApple OSS Distributions #include <IOKit/IOCatalogue.h>
32c1dac77fSApple OSS Distributions #include <IOKit/IODeviceTreeSupport.h>
33c1dac77fSApple OSS Distributions #include <IOKit/IOKitKeys.h>
34bb611c8fSApple OSS Distributions #include <IOKit/IONVRAM.h>
35c1dac77fSApple OSS Distributions #include <IOKit/IOPlatformExpert.h>
360f3703acSApple OSS Distributions #include <IOKit/IOUserClient.h>
37e6231be0SApple OSS Distributions #include <libkern/c++/OSAllocation.h>
38c1dac77fSApple OSS Distributions
39c1dac77fSApple OSS Distributions extern "C" {
40e6231be0SApple OSS Distributions #include <libkern/amfi/amfi.h>
41e6231be0SApple OSS Distributions #include <sys/codesign.h>
42aca3beaaSApple OSS Distributions #include <sys/code_signing.h>
43e6231be0SApple OSS Distributions #include <vm/pmap.h>
44e6231be0SApple OSS Distributions #include <vm/vm_map.h>
45c1dac77fSApple OSS Distributions #include <pexpert/pexpert.h>
46c1dac77fSApple OSS Distributions #include <kern/clock.h>
47e6231be0SApple OSS Distributions #if CONFIG_KDP_INTERACTIVE_DEBUGGING
48e6231be0SApple OSS Distributions #include <kern/debug.h>
49e6231be0SApple OSS Distributions #endif
50cc9a6355SApple OSS Distributions #include <mach/machine.h>
51e13b1fa5SApple OSS Distributions #include <uuid/uuid.h>
52a3bb9fccSApple OSS Distributions #include <sys/vnode_internal.h>
530f3703acSApple OSS Distributions #include <sys/mount.h>
545c2921b0SApple OSS Distributions #include <corecrypto/ccsha2.h>
5594d3b452SApple OSS Distributions #include <kdp/sk_core.h>
56*8d741a5dSApple OSS Distributions #include <pexpert/device_tree.h>
57*8d741a5dSApple OSS Distributions #include <kern/startup.h>
58c1dac77fSApple OSS Distributions
59c1dac77fSApple OSS Distributions // how long to wait for matching root device, secs
603ca3bd55SApple OSS Distributions #if DEBUG
613ca3bd55SApple OSS Distributions #define ROOTDEVICETIMEOUT 120
623ca3bd55SApple OSS Distributions #else
63c1dac77fSApple OSS Distributions #define ROOTDEVICETIMEOUT 60
643ca3bd55SApple OSS Distributions #endif
65c1dac77fSApple OSS Distributions
66855239e5SApple OSS Distributions extern dev_t mdevadd(int devid, uint64_t base, unsigned int size, int phys);
67368ad365SApple OSS Distributions extern dev_t mdevlookup(int devid);
683ca3bd55SApple OSS Distributions extern void mdevremoveall(void);
6988cc0b97SApple OSS Distributions extern int mdevgetrange(int devid, uint64_t *base, uint64_t *size);
70855239e5SApple OSS Distributions extern void di_root_ramfile(IORegistryEntry * entry);
71e6231be0SApple OSS Distributions extern int IODTGetDefault(const char *key, void *infoAddr, unsigned int infoSize);
72e6231be0SApple OSS Distributions extern boolean_t cpuid_vmm_present(void);
73c1dac77fSApple OSS Distributions
74a5e72196SApple OSS Distributions #define ROUNDUP(a, b) (((a) + ((b) - 1)) & (~((b) - 1)))
75a5e72196SApple OSS Distributions
7676e12aa3SApple OSS Distributions #define IOPOLLED_COREFILE (CONFIG_KDP_INTERACTIVE_DEBUGGING)
77cc9a6355SApple OSS Distributions
78cc9a6355SApple OSS Distributions #if defined(XNU_TARGET_OS_BRIDGE)
79cc9a6355SApple OSS Distributions #define kIOCoreDumpPath "/private/var/internal/kernelcore"
80bb611c8fSApple OSS Distributions #elif defined(XNU_TARGET_OS_OSX)
81bb611c8fSApple OSS Distributions #define kIOCoreDumpPath "/System/Volumes/VM/kernelcore"
82a5e72196SApple OSS Distributions #else
83cc9a6355SApple OSS Distributions #define kIOCoreDumpPath "/private/var/vm/kernelcore"
84a5e72196SApple OSS Distributions #endif
850f3703acSApple OSS Distributions
86*8d741a5dSApple OSS Distributions #define kIOCoreDumpPrebootPath "/private/preboot/kernelcore"
87*8d741a5dSApple OSS Distributions
88bb611c8fSApple OSS Distributions #define SYSTEM_NVRAM_PREFIX "40A0DDD2-77F8-4392-B4A3-1E7304206516:"
89bb611c8fSApple OSS Distributions
9088cc0b97SApple OSS Distributions #if CONFIG_KDP_INTERACTIVE_DEBUGGING
9188cc0b97SApple OSS Distributions /*
9288cc0b97SApple OSS Distributions * Touched by IOFindBSDRoot() if a RAMDisk is used for the root device.
9388cc0b97SApple OSS Distributions */
9488cc0b97SApple OSS Distributions extern uint64_t kdp_core_ramdisk_addr;
9588cc0b97SApple OSS Distributions extern uint64_t kdp_core_ramdisk_size;
96e6231be0SApple OSS Distributions
97e6231be0SApple OSS Distributions /*
98e6231be0SApple OSS Distributions * A callback to indicate that the polled-mode corefile is now available.
99e6231be0SApple OSS Distributions */
100e6231be0SApple OSS Distributions extern kern_return_t kdp_core_polled_io_polled_file_available(IOCoreFileAccessCallback access_data, void *access_context, void *recipient_context);
101e6231be0SApple OSS Distributions
102e6231be0SApple OSS Distributions /*
103e6231be0SApple OSS Distributions * A callback to indicate that the polled-mode corefile is no longer available.
104e6231be0SApple OSS Distributions */
105e6231be0SApple OSS Distributions extern kern_return_t kdp_core_polled_io_polled_file_unavailable(void);
10688cc0b97SApple OSS Distributions #endif
1070f3703acSApple OSS Distributions
108a5e72196SApple OSS Distributions #if IOPOLLED_COREFILE
109a5e72196SApple OSS Distributions static void IOOpenPolledCoreFile(thread_call_param_t __unused, thread_call_param_t corefilename);
110*8d741a5dSApple OSS Distributions static void IOResolveCoreFilePath();
111a5e72196SApple OSS Distributions
112a5e72196SApple OSS Distributions thread_call_t corefile_open_call = NULL;
113*8d741a5dSApple OSS Distributions SECURITY_READ_ONLY_LATE(const char*) kdp_corefile_path = kIOCoreDumpPath;
114a5e72196SApple OSS Distributions #endif
115a5e72196SApple OSS Distributions
116c1dac77fSApple OSS Distributions kern_return_t
IOKitBSDInit(void)117c1dac77fSApple OSS Distributions IOKitBSDInit( void )
118c1dac77fSApple OSS Distributions {
119c1dac77fSApple OSS Distributions IOService::publishResource("IOBSD");
120c1dac77fSApple OSS Distributions
121a5e72196SApple OSS Distributions #if IOPOLLED_COREFILE
122a5e72196SApple OSS Distributions corefile_open_call = thread_call_allocate_with_options(IOOpenPolledCoreFile, NULL, THREAD_CALL_PRIORITY_KERNEL, THREAD_CALL_OPTIONS_ONCE);
123a5e72196SApple OSS Distributions #endif
124a5e72196SApple OSS Distributions
125a5e72196SApple OSS Distributions return kIOReturnSuccess;
126c1dac77fSApple OSS Distributions }
127c1dac77fSApple OSS Distributions
128855239e5SApple OSS Distributions void
IOServicePublishResource(const char * property,boolean_t value)129855239e5SApple OSS Distributions IOServicePublishResource( const char * property, boolean_t value )
130855239e5SApple OSS Distributions {
131a5e72196SApple OSS Distributions if (value) {
132855239e5SApple OSS Distributions IOService::publishResource( property, kOSBooleanTrue );
133a5e72196SApple OSS Distributions } else {
134855239e5SApple OSS Distributions IOService::getResourceService()->removeProperty( property );
135855239e5SApple OSS Distributions }
136a5e72196SApple OSS Distributions }
137855239e5SApple OSS Distributions
138855239e5SApple OSS Distributions boolean_t
IOServiceWaitForMatchingResource(const char * property,uint64_t timeout)139855239e5SApple OSS Distributions IOServiceWaitForMatchingResource( const char * property, uint64_t timeout )
140855239e5SApple OSS Distributions {
141a5e72196SApple OSS Distributions OSDictionary * dict = NULL;
142a5e72196SApple OSS Distributions IOService * match = NULL;
143855239e5SApple OSS Distributions boolean_t found = false;
144855239e5SApple OSS Distributions
145855239e5SApple OSS Distributions do {
146855239e5SApple OSS Distributions dict = IOService::resourceMatching( property );
147a5e72196SApple OSS Distributions if (!dict) {
148855239e5SApple OSS Distributions continue;
149a5e72196SApple OSS Distributions }
150855239e5SApple OSS Distributions match = IOService::waitForMatchingService( dict, timeout );
151a5e72196SApple OSS Distributions if (match) {
152855239e5SApple OSS Distributions found = true;
153a5e72196SApple OSS Distributions }
154855239e5SApple OSS Distributions } while (false);
155855239e5SApple OSS Distributions
156a5e72196SApple OSS Distributions if (dict) {
157855239e5SApple OSS Distributions dict->release();
158a5e72196SApple OSS Distributions }
159a5e72196SApple OSS Distributions if (match) {
160855239e5SApple OSS Distributions match->release();
161a5e72196SApple OSS Distributions }
162855239e5SApple OSS Distributions
163a5e72196SApple OSS Distributions return found;
164855239e5SApple OSS Distributions }
165855239e5SApple OSS Distributions
166855239e5SApple OSS Distributions boolean_t
IOCatalogueMatchingDriversPresent(const char * property)167855239e5SApple OSS Distributions IOCatalogueMatchingDriversPresent( const char * property )
168855239e5SApple OSS Distributions {
169a5e72196SApple OSS Distributions OSDictionary * dict = NULL;
170a5e72196SApple OSS Distributions OSOrderedSet * set = NULL;
171855239e5SApple OSS Distributions SInt32 generationCount = 0;
172855239e5SApple OSS Distributions boolean_t found = false;
173855239e5SApple OSS Distributions
174855239e5SApple OSS Distributions do {
175855239e5SApple OSS Distributions dict = OSDictionary::withCapacity(1);
176a5e72196SApple OSS Distributions if (!dict) {
177855239e5SApple OSS Distributions continue;
178a5e72196SApple OSS Distributions }
179855239e5SApple OSS Distributions dict->setObject( property, kOSBooleanTrue );
180855239e5SApple OSS Distributions set = gIOCatalogue->findDrivers( dict, &generationCount );
181a5e72196SApple OSS Distributions if (set && (set->getCount() > 0)) {
182855239e5SApple OSS Distributions found = true;
183a5e72196SApple OSS Distributions }
184855239e5SApple OSS Distributions } while (false);
185855239e5SApple OSS Distributions
186a5e72196SApple OSS Distributions if (dict) {
187855239e5SApple OSS Distributions dict->release();
188a5e72196SApple OSS Distributions }
189a5e72196SApple OSS Distributions if (set) {
190855239e5SApple OSS Distributions set->release();
191855239e5SApple OSS Distributions }
192855239e5SApple OSS Distributions
193a5e72196SApple OSS Distributions return found;
194a5e72196SApple OSS Distributions }
195a5e72196SApple OSS Distributions
196a5e72196SApple OSS Distributions OSDictionary *
IOBSDNameMatching(const char * name)197a5e72196SApple OSS Distributions IOBSDNameMatching( const char * name )
198c1dac77fSApple OSS Distributions {
199c1dac77fSApple OSS Distributions OSDictionary * dict;
200a5e72196SApple OSS Distributions const OSSymbol * str = NULL;
201c1dac77fSApple OSS Distributions
202c1dac77fSApple OSS Distributions do {
203c1dac77fSApple OSS Distributions dict = IOService::serviceMatching( gIOServiceKey );
204a5e72196SApple OSS Distributions if (!dict) {
205c1dac77fSApple OSS Distributions continue;
206a5e72196SApple OSS Distributions }
207c1dac77fSApple OSS Distributions str = OSSymbol::withCString( name );
208a5e72196SApple OSS Distributions if (!str) {
209c1dac77fSApple OSS Distributions continue;
210a5e72196SApple OSS Distributions }
211c1dac77fSApple OSS Distributions dict->setObject( kIOBSDNameKey, (OSObject *) str );
212c1dac77fSApple OSS Distributions str->release();
213c1dac77fSApple OSS Distributions
214a5e72196SApple OSS Distributions return dict;
215c1dac77fSApple OSS Distributions } while (false);
216c1dac77fSApple OSS Distributions
217a5e72196SApple OSS Distributions if (dict) {
218c1dac77fSApple OSS Distributions dict->release();
219a5e72196SApple OSS Distributions }
220a5e72196SApple OSS Distributions if (str) {
221c1dac77fSApple OSS Distributions str->release();
222c1dac77fSApple OSS Distributions }
223c1dac77fSApple OSS Distributions
224a5e72196SApple OSS Distributions return NULL;
225a5e72196SApple OSS Distributions }
226a5e72196SApple OSS Distributions
227a5e72196SApple OSS Distributions OSDictionary *
IOUUIDMatching(void)228a5e72196SApple OSS Distributions IOUUIDMatching( void )
22914e3d835SApple OSS Distributions {
2301031c584SApple OSS Distributions OSObject * obj;
2311031c584SApple OSS Distributions OSDictionary * result;
2321031c584SApple OSS Distributions
2331031c584SApple OSS Distributions obj = OSUnserialize(
2341031c584SApple OSS Distributions "{"
2351031c584SApple OSS Distributions "'IOProviderClass' = 'IOResources';"
2361031c584SApple OSS Distributions "'IOResourceMatch' = ('IOBSD', 'boot-uuid-media');"
2371031c584SApple OSS Distributions "}",
2381031c584SApple OSS Distributions NULL);
2391031c584SApple OSS Distributions result = OSDynamicCast(OSDictionary, obj);
2401031c584SApple OSS Distributions assert(result);
2411031c584SApple OSS Distributions
2421031c584SApple OSS Distributions return result;
24314e3d835SApple OSS Distributions }
24414e3d835SApple OSS Distributions
245a5e72196SApple OSS Distributions OSDictionary *
IONetworkNamePrefixMatching(const char * prefix)246a5e72196SApple OSS Distributions IONetworkNamePrefixMatching( const char * prefix )
247c1dac77fSApple OSS Distributions {
248c1dac77fSApple OSS Distributions OSDictionary * matching;
249a5e72196SApple OSS Distributions OSDictionary * propDict = NULL;
250a5e72196SApple OSS Distributions const OSSymbol * str = NULL;
251e13b1fa5SApple OSS Distributions char networkType[128];
252c1dac77fSApple OSS Distributions
253c1dac77fSApple OSS Distributions do {
254c1dac77fSApple OSS Distributions matching = IOService::serviceMatching( "IONetworkInterface" );
255a5e72196SApple OSS Distributions if (matching == NULL) {
256c1dac77fSApple OSS Distributions continue;
257a5e72196SApple OSS Distributions }
258c1dac77fSApple OSS Distributions
259c1dac77fSApple OSS Distributions propDict = OSDictionary::withCapacity(1);
260a5e72196SApple OSS Distributions if (propDict == NULL) {
261c1dac77fSApple OSS Distributions continue;
262a5e72196SApple OSS Distributions }
263c1dac77fSApple OSS Distributions
264c1dac77fSApple OSS Distributions str = OSSymbol::withCString( prefix );
265a5e72196SApple OSS Distributions if (str == NULL) {
266c1dac77fSApple OSS Distributions continue;
267a5e72196SApple OSS Distributions }
268c1dac77fSApple OSS Distributions
2698149afccSApple OSS Distributions propDict->setObject( "IOInterfaceNamePrefix", (OSObject *) str );
270c1dac77fSApple OSS Distributions str->release();
271a5e72196SApple OSS Distributions str = NULL;
272c1dac77fSApple OSS Distributions
273e13b1fa5SApple OSS Distributions // see if we're contrained to netroot off of specific network type
274a5e72196SApple OSS Distributions if (PE_parse_boot_argn( "network-type", networkType, 128 )) {
275e13b1fa5SApple OSS Distributions str = OSSymbol::withCString( networkType );
276a5e72196SApple OSS Distributions if (str) {
277e13b1fa5SApple OSS Distributions propDict->setObject( "IONetworkRootType", str);
278e13b1fa5SApple OSS Distributions str->release();
279a5e72196SApple OSS Distributions str = NULL;
280e13b1fa5SApple OSS Distributions }
281e13b1fa5SApple OSS Distributions }
282e13b1fa5SApple OSS Distributions
283c1dac77fSApple OSS Distributions if (matching->setObject( gIOPropertyMatchKey,
284a5e72196SApple OSS Distributions (OSObject *) propDict ) != true) {
285c1dac77fSApple OSS Distributions continue;
286c1dac77fSApple OSS Distributions }
287c1dac77fSApple OSS Distributions
288a5e72196SApple OSS Distributions propDict->release();
289a5e72196SApple OSS Distributions propDict = NULL;
290a5e72196SApple OSS Distributions
291a5e72196SApple OSS Distributions return matching;
292a5e72196SApple OSS Distributions } while (false);
293a5e72196SApple OSS Distributions
294a5e72196SApple OSS Distributions if (matching) {
295a5e72196SApple OSS Distributions matching->release();
296a5e72196SApple OSS Distributions }
297a5e72196SApple OSS Distributions if (propDict) {
298a5e72196SApple OSS Distributions propDict->release();
299a5e72196SApple OSS Distributions }
300a5e72196SApple OSS Distributions if (str) {
301a5e72196SApple OSS Distributions str->release();
302a5e72196SApple OSS Distributions }
303a5e72196SApple OSS Distributions
304a5e72196SApple OSS Distributions return NULL;
305a5e72196SApple OSS Distributions }
306a5e72196SApple OSS Distributions
307a5e72196SApple OSS Distributions static bool
IORegisterNetworkInterface(IOService * netif)308a5e72196SApple OSS Distributions IORegisterNetworkInterface( IOService * netif )
309c1dac77fSApple OSS Distributions {
3108149afccSApple OSS Distributions // A network interface is typically named and registered
3118149afccSApple OSS Distributions // with BSD after receiving a request from a user space
3128149afccSApple OSS Distributions // "namer". However, for cases when the system needs to
3138149afccSApple OSS Distributions // root from the network, this registration task must be
3148149afccSApple OSS Distributions // done inside the kernel and completed before the root
3158149afccSApple OSS Distributions // device is handed to BSD.
316c1dac77fSApple OSS Distributions
3178149afccSApple OSS Distributions IOService * stack;
318a5e72196SApple OSS Distributions OSNumber * zero = NULL;
319a5e72196SApple OSS Distributions OSString * path = NULL;
320a5e72196SApple OSS Distributions OSDictionary * dict = NULL;
321e6231be0SApple OSS Distributions OSDataAllocation<char> pathBuf;
3228149afccSApple OSS Distributions int len;
3238149afccSApple OSS Distributions enum { kMaxPathLen = 512 };
3248149afccSApple OSS Distributions
3258149afccSApple OSS Distributions do {
3268149afccSApple OSS Distributions stack = IOService::waitForService(
3278149afccSApple OSS Distributions IOService::serviceMatching("IONetworkStack"));
328a5e72196SApple OSS Distributions if (stack == NULL) {
329a5e72196SApple OSS Distributions break;
330a5e72196SApple OSS Distributions }
3318149afccSApple OSS Distributions
3328149afccSApple OSS Distributions dict = OSDictionary::withCapacity(3);
333a5e72196SApple OSS Distributions if (dict == NULL) {
334a5e72196SApple OSS Distributions break;
335a5e72196SApple OSS Distributions }
3368149afccSApple OSS Distributions
3378149afccSApple OSS Distributions zero = OSNumber::withNumber((UInt64) 0, 32);
338a5e72196SApple OSS Distributions if (zero == NULL) {
339a5e72196SApple OSS Distributions break;
340a5e72196SApple OSS Distributions }
3418149afccSApple OSS Distributions
342e6231be0SApple OSS Distributions pathBuf = OSDataAllocation<char>( kMaxPathLen, OSAllocateMemory );
343e6231be0SApple OSS Distributions if (!pathBuf) {
344a5e72196SApple OSS Distributions break;
345a5e72196SApple OSS Distributions }
3468149afccSApple OSS Distributions
3478149afccSApple OSS Distributions len = kMaxPathLen;
348e6231be0SApple OSS Distributions if (netif->getPath( pathBuf.data(), &len, gIOServicePlane )
349a5e72196SApple OSS Distributions == false) {
350a5e72196SApple OSS Distributions break;
351a5e72196SApple OSS Distributions }
3528149afccSApple OSS Distributions
353e6231be0SApple OSS Distributions path = OSString::withCStringNoCopy(pathBuf.data());
354a5e72196SApple OSS Distributions if (path == NULL) {
355a5e72196SApple OSS Distributions break;
356a5e72196SApple OSS Distributions }
3578149afccSApple OSS Distributions
3588149afccSApple OSS Distributions dict->setObject( "IOInterfaceUnit", zero );
3598149afccSApple OSS Distributions dict->setObject( kIOPathMatchKey, path );
3608149afccSApple OSS Distributions
3618149afccSApple OSS Distributions stack->setProperties( dict );
362a5e72196SApple OSS Distributions }while (false);
363a5e72196SApple OSS Distributions
364a5e72196SApple OSS Distributions if (zero) {
365a5e72196SApple OSS Distributions zero->release();
366c1dac77fSApple OSS Distributions }
367a5e72196SApple OSS Distributions if (path) {
368a5e72196SApple OSS Distributions path->release();
369a5e72196SApple OSS Distributions }
370a5e72196SApple OSS Distributions if (dict) {
371a5e72196SApple OSS Distributions dict->release();
372a5e72196SApple OSS Distributions }
373c1dac77fSApple OSS Distributions
374a5e72196SApple OSS Distributions return netif->getProperty( kIOBSDNameKey ) != NULL;
375a5e72196SApple OSS Distributions }
376a5e72196SApple OSS Distributions
377a5e72196SApple OSS Distributions OSDictionary *
IOOFPathMatching(const char * path,char * buf,int maxLen)378a5e72196SApple OSS Distributions IOOFPathMatching( const char * path, char * buf, int maxLen )
379c1dac77fSApple OSS Distributions {
380186b8fceSApple OSS Distributions OSDictionary * matching = NULL;
38114e3d835SApple OSS Distributions OSString * str;
38214e3d835SApple OSS Distributions char * comp;
38314e3d835SApple OSS Distributions int len;
38414e3d835SApple OSS Distributions
38514e3d835SApple OSS Distributions do {
386bb611c8fSApple OSS Distributions len = ((int) strlen( kIODeviceTreePlane ":" ));
38714e3d835SApple OSS Distributions maxLen -= len;
388a5e72196SApple OSS Distributions if (maxLen <= 0) {
38914e3d835SApple OSS Distributions continue;
390a5e72196SApple OSS Distributions }
39114e3d835SApple OSS Distributions
3923ca3bd55SApple OSS Distributions strlcpy( buf, kIODeviceTreePlane ":", len + 1 );
39314e3d835SApple OSS Distributions comp = buf + len;
39414e3d835SApple OSS Distributions
395bb611c8fSApple OSS Distributions len = ((int) strnlen( path, INT_MAX ));
39614e3d835SApple OSS Distributions maxLen -= len;
397a5e72196SApple OSS Distributions if (maxLen <= 0) {
39814e3d835SApple OSS Distributions continue;
399a5e72196SApple OSS Distributions }
4003ca3bd55SApple OSS Distributions strlcpy( comp, path, len + 1 );
40114e3d835SApple OSS Distributions
40214e3d835SApple OSS Distributions matching = OSDictionary::withCapacity( 1 );
403a5e72196SApple OSS Distributions if (!matching) {
40414e3d835SApple OSS Distributions continue;
405a5e72196SApple OSS Distributions }
40614e3d835SApple OSS Distributions
40714e3d835SApple OSS Distributions str = OSString::withCString( buf );
408a5e72196SApple OSS Distributions if (!str) {
40914e3d835SApple OSS Distributions continue;
410a5e72196SApple OSS Distributions }
41114e3d835SApple OSS Distributions matching->setObject( kIOPathMatchKey, str );
41214e3d835SApple OSS Distributions str->release();
41314e3d835SApple OSS Distributions
414a5e72196SApple OSS Distributions return matching;
41514e3d835SApple OSS Distributions } while (false);
41614e3d835SApple OSS Distributions
417a5e72196SApple OSS Distributions if (matching) {
41814e3d835SApple OSS Distributions matching->release();
419a5e72196SApple OSS Distributions }
42014e3d835SApple OSS Distributions
421a5e72196SApple OSS Distributions return NULL;
422c1dac77fSApple OSS Distributions }
423c1dac77fSApple OSS Distributions
424368ad365SApple OSS Distributions static int didRam = 0;
425186b8fceSApple OSS Distributions enum { kMaxPathBuf = 512, kMaxBootVar = 128 };
426368ad365SApple OSS Distributions
427e6231be0SApple OSS Distributions bool
IOGetBootUUID(char * uuid)428e6231be0SApple OSS Distributions IOGetBootUUID(char *uuid)
429bb611c8fSApple OSS Distributions {
430bb611c8fSApple OSS Distributions IORegistryEntry *entry;
431e6231be0SApple OSS Distributions OSData *uuid_data = NULL;
432e6231be0SApple OSS Distributions bool result = false;
433bb611c8fSApple OSS Distributions
434bb611c8fSApple OSS Distributions if ((entry = IORegistryEntry::fromPath("/chosen", gIODTPlane))) {
435e6231be0SApple OSS Distributions uuid_data = (OSData *)entry->getProperty("boot-uuid");
436bb611c8fSApple OSS Distributions if (uuid_data) {
437e6231be0SApple OSS Distributions unsigned int length = uuid_data->getLength();
438e6231be0SApple OSS Distributions if (length <= sizeof(uuid_string_t)) {
439e6231be0SApple OSS Distributions /* ensure caller's buffer is fully initialized: */
440e6231be0SApple OSS Distributions bzero(uuid, sizeof(uuid_string_t));
441e6231be0SApple OSS Distributions /* copy the content of uuid_data->getBytesNoCopy() into uuid */
442e6231be0SApple OSS Distributions memcpy(uuid, uuid_data->getBytesNoCopy(), length);
443e6231be0SApple OSS Distributions /* guarantee nul-termination: */
444e6231be0SApple OSS Distributions uuid[sizeof(uuid_string_t) - 1] = '\0';
445e6231be0SApple OSS Distributions result = true;
446e6231be0SApple OSS Distributions } else {
447e6231be0SApple OSS Distributions uuid = NULL;
448bb611c8fSApple OSS Distributions }
449bb611c8fSApple OSS Distributions }
450e6231be0SApple OSS Distributions OSSafeReleaseNULL(entry);
451e6231be0SApple OSS Distributions }
452e6231be0SApple OSS Distributions return result;
453e6231be0SApple OSS Distributions }
454bb611c8fSApple OSS Distributions
455e6231be0SApple OSS Distributions bool
IOGetApfsPrebootUUID(char * uuid)456e6231be0SApple OSS Distributions IOGetApfsPrebootUUID(char *uuid)
457bb611c8fSApple OSS Distributions {
458bb611c8fSApple OSS Distributions IORegistryEntry *entry;
459e6231be0SApple OSS Distributions OSData *uuid_data = NULL;
460e6231be0SApple OSS Distributions bool result = false;
461bb611c8fSApple OSS Distributions
462bb611c8fSApple OSS Distributions if ((entry = IORegistryEntry::fromPath("/chosen", gIODTPlane))) {
463e6231be0SApple OSS Distributions uuid_data = (OSData *)entry->getProperty("apfs-preboot-uuid");
464e6231be0SApple OSS Distributions
465bb611c8fSApple OSS Distributions if (uuid_data) {
466e6231be0SApple OSS Distributions unsigned int length = uuid_data->getLength();
467e6231be0SApple OSS Distributions if (length <= sizeof(uuid_string_t)) {
468e6231be0SApple OSS Distributions /* ensure caller's buffer is fully initialized: */
469e6231be0SApple OSS Distributions bzero(uuid, sizeof(uuid_string_t));
470e6231be0SApple OSS Distributions /* copy the content of uuid_data->getBytesNoCopy() into uuid */
471e6231be0SApple OSS Distributions memcpy(uuid, uuid_data->getBytesNoCopy(), length);
472e6231be0SApple OSS Distributions /* guarantee nul-termination: */
473e6231be0SApple OSS Distributions uuid[sizeof(uuid_string_t) - 1] = '\0';
474e6231be0SApple OSS Distributions result = true;
475e6231be0SApple OSS Distributions } else {
476e6231be0SApple OSS Distributions uuid = NULL;
477bb611c8fSApple OSS Distributions }
478bb611c8fSApple OSS Distributions }
479e6231be0SApple OSS Distributions OSSafeReleaseNULL(entry);
480e6231be0SApple OSS Distributions }
481e6231be0SApple OSS Distributions return result;
482e6231be0SApple OSS Distributions }
483bb611c8fSApple OSS Distributions
484e6231be0SApple OSS Distributions bool
IOGetAssociatedApfsVolgroupUUID(char * uuid)485e6231be0SApple OSS Distributions IOGetAssociatedApfsVolgroupUUID(char *uuid)
486bb611c8fSApple OSS Distributions {
487bb611c8fSApple OSS Distributions IORegistryEntry *entry;
488e6231be0SApple OSS Distributions OSData *uuid_data = NULL;
489e6231be0SApple OSS Distributions bool result = false;
490bb611c8fSApple OSS Distributions
491bb611c8fSApple OSS Distributions if ((entry = IORegistryEntry::fromPath("/chosen", gIODTPlane))) {
492e6231be0SApple OSS Distributions uuid_data = (OSData *)entry->getProperty("associated-volume-group");
493e6231be0SApple OSS Distributions
494bb611c8fSApple OSS Distributions if (uuid_data) {
495e6231be0SApple OSS Distributions unsigned int length = uuid_data->getLength();
496e6231be0SApple OSS Distributions
497e6231be0SApple OSS Distributions if (length <= sizeof(uuid_string_t)) {
498e6231be0SApple OSS Distributions /* ensure caller's buffer is fully initialized: */
499e6231be0SApple OSS Distributions bzero(uuid, sizeof(uuid_string_t));
500e6231be0SApple OSS Distributions /* copy the content of uuid_data->getBytesNoCopy() into uuid */
501e6231be0SApple OSS Distributions memcpy(uuid, uuid_data->getBytesNoCopy(), length);
502e6231be0SApple OSS Distributions /* guarantee nul-termination: */
503e6231be0SApple OSS Distributions uuid[sizeof(uuid_string_t) - 1] = '\0';
504e6231be0SApple OSS Distributions result = true;
505e6231be0SApple OSS Distributions } else {
506e6231be0SApple OSS Distributions uuid = NULL;
507bb611c8fSApple OSS Distributions }
508bb611c8fSApple OSS Distributions }
509e6231be0SApple OSS Distributions OSSafeReleaseNULL(entry);
510e6231be0SApple OSS Distributions }
511e6231be0SApple OSS Distributions return result;
512e6231be0SApple OSS Distributions }
513bb611c8fSApple OSS Distributions
514e6231be0SApple OSS Distributions bool
IOGetBootObjectsPath(char * path_prefix)515e6231be0SApple OSS Distributions IOGetBootObjectsPath(char *path_prefix)
516bb611c8fSApple OSS Distributions {
517bb611c8fSApple OSS Distributions IORegistryEntry *entry;
518e6231be0SApple OSS Distributions OSData *path_prefix_data = NULL;
519e6231be0SApple OSS Distributions bool result = false;
520bb611c8fSApple OSS Distributions
521bb611c8fSApple OSS Distributions if ((entry = IORegistryEntry::fromPath("/chosen", gIODTPlane))) {
522e6231be0SApple OSS Distributions path_prefix_data = (OSData *)entry->getProperty("boot-objects-path");
523e6231be0SApple OSS Distributions
524bb611c8fSApple OSS Distributions if (path_prefix_data) {
525e6231be0SApple OSS Distributions unsigned int length = path_prefix_data->getLength();
526bb611c8fSApple OSS Distributions
527e6231be0SApple OSS Distributions if (length <= MAXPATHLEN) {
528e6231be0SApple OSS Distributions /* ensure caller's buffer is fully initialized: */
529e6231be0SApple OSS Distributions bzero(path_prefix, MAXPATHLEN);
530e6231be0SApple OSS Distributions /* copy the content of path_prefix_data->getBytesNoCopy() into path_prefix */
531e6231be0SApple OSS Distributions memcpy(path_prefix, path_prefix_data->getBytesNoCopy(), length);
532e6231be0SApple OSS Distributions /* guarantee nul-termination: */
533e6231be0SApple OSS Distributions path_prefix[MAXPATHLEN - 1] = '\0';
534e6231be0SApple OSS Distributions result = true;
535e6231be0SApple OSS Distributions } else {
536e6231be0SApple OSS Distributions path_prefix = NULL;
537e6231be0SApple OSS Distributions }
538e6231be0SApple OSS Distributions }
539e6231be0SApple OSS Distributions OSSafeReleaseNULL(entry);
540e6231be0SApple OSS Distributions }
541e6231be0SApple OSS Distributions return result;
542bb611c8fSApple OSS Distributions }
543bb611c8fSApple OSS Distributions
5445c2921b0SApple OSS Distributions
5455c2921b0SApple OSS Distributions bool
IOGetBootManifestHash(char * hash_data,size_t * hash_data_size)5465c2921b0SApple OSS Distributions IOGetBootManifestHash(char *hash_data, size_t *hash_data_size)
5475c2921b0SApple OSS Distributions {
5485c2921b0SApple OSS Distributions IORegistryEntry *entry = NULL;
5495c2921b0SApple OSS Distributions OSData *manifest_hash_data = NULL;
5505c2921b0SApple OSS Distributions bool result = false;
5515c2921b0SApple OSS Distributions
5525c2921b0SApple OSS Distributions if ((entry = IORegistryEntry::fromPath("/chosen", gIODTPlane))) {
5535c2921b0SApple OSS Distributions manifest_hash_data = (OSData *)entry->getProperty("boot-manifest-hash");
5545c2921b0SApple OSS Distributions if (manifest_hash_data) {
5555c2921b0SApple OSS Distributions unsigned int length = manifest_hash_data->getLength();
5565c2921b0SApple OSS Distributions /* hashed with SHA2-384 or SHA1, the boot manifest hash should be 48 Bytes or less */
5575c2921b0SApple OSS Distributions if ((length <= CCSHA384_OUTPUT_SIZE) && (*hash_data_size >= CCSHA384_OUTPUT_SIZE)) {
5585c2921b0SApple OSS Distributions /* ensure caller's buffer is fully initialized: */
5595c2921b0SApple OSS Distributions bzero(hash_data, CCSHA384_OUTPUT_SIZE);
5605c2921b0SApple OSS Distributions /* copy the content of manifest_hash_data->getBytesNoCopy() into hash_data */
5615c2921b0SApple OSS Distributions memcpy(hash_data, manifest_hash_data->getBytesNoCopy(), length);
5625c2921b0SApple OSS Distributions *hash_data_size = length;
5635c2921b0SApple OSS Distributions result = true;
5645c2921b0SApple OSS Distributions } else {
5655c2921b0SApple OSS Distributions hash_data = NULL;
5665c2921b0SApple OSS Distributions *hash_data_size = 0;
5675c2921b0SApple OSS Distributions }
5685c2921b0SApple OSS Distributions }
5695c2921b0SApple OSS Distributions OSSafeReleaseNULL(entry);
5705c2921b0SApple OSS Distributions }
5715c2921b0SApple OSS Distributions
5725c2921b0SApple OSS Distributions return result;
5735c2921b0SApple OSS Distributions }
5745c2921b0SApple OSS Distributions
575bb611c8fSApple OSS Distributions /*
576bb611c8fSApple OSS Distributions * Set NVRAM to boot into the right flavor of Recovery,
577bb611c8fSApple OSS Distributions * optionally passing a UUID of a volume that failed to boot.
578bb611c8fSApple OSS Distributions * If `reboot` is true, reboot immediately.
579bb611c8fSApple OSS Distributions *
580bb611c8fSApple OSS Distributions * Returns true if `mode` was understood, false otherwise.
581bb611c8fSApple OSS Distributions * (Does not return if `reboot` is true.)
582bb611c8fSApple OSS Distributions */
583bb611c8fSApple OSS Distributions boolean_t
IOSetRecoveryBoot(bsd_bootfail_mode_t mode,uuid_t volume_uuid,boolean_t reboot)584bb611c8fSApple OSS Distributions IOSetRecoveryBoot(bsd_bootfail_mode_t mode, uuid_t volume_uuid, boolean_t reboot)
585bb611c8fSApple OSS Distributions {
586bb611c8fSApple OSS Distributions IODTNVRAM *nvram = NULL;
587bb611c8fSApple OSS Distributions const OSSymbol *boot_command_sym = NULL;
588bb611c8fSApple OSS Distributions OSString *boot_command_recover = NULL;
589bb611c8fSApple OSS Distributions
590bb611c8fSApple OSS Distributions if (mode == BSD_BOOTFAIL_SEAL_BROKEN) {
591bb611c8fSApple OSS Distributions const char *boot_mode = "ssv-seal-broken";
592bb611c8fSApple OSS Distributions uuid_string_t volume_uuid_str;
593bb611c8fSApple OSS Distributions
594bb611c8fSApple OSS Distributions // Set `recovery-broken-seal-uuid = <volume_uuid>`.
595bb611c8fSApple OSS Distributions if (volume_uuid) {
596bb611c8fSApple OSS Distributions uuid_unparse_upper(volume_uuid, volume_uuid_str);
597bb611c8fSApple OSS Distributions
598bb611c8fSApple OSS Distributions if (!PEWriteNVRAMProperty(SYSTEM_NVRAM_PREFIX "recovery-broken-seal-uuid",
599bb611c8fSApple OSS Distributions volume_uuid_str, sizeof(uuid_string_t))) {
600bb611c8fSApple OSS Distributions IOLog("Failed to write recovery-broken-seal-uuid to NVRAM.\n");
601bb611c8fSApple OSS Distributions }
602bb611c8fSApple OSS Distributions }
603bb611c8fSApple OSS Distributions
604bb611c8fSApple OSS Distributions // Set `recovery-boot-mode = ssv-seal-broken`.
605bb611c8fSApple OSS Distributions if (!PEWriteNVRAMProperty(SYSTEM_NVRAM_PREFIX "recovery-boot-mode", boot_mode,
606bb611c8fSApple OSS Distributions (const unsigned int) strlen(boot_mode))) {
607bb611c8fSApple OSS Distributions IOLog("Failed to write recovery-boot-mode to NVRAM.\n");
608bb611c8fSApple OSS Distributions }
609bb611c8fSApple OSS Distributions } else if (mode == BSD_BOOTFAIL_MEDIA_MISSING) {
610bb611c8fSApple OSS Distributions const char *boot_picker_reason = "missing-boot-media";
611bb611c8fSApple OSS Distributions
612bb611c8fSApple OSS Distributions // Set `boot-picker-bringup-reason = missing-boot-media`.
613bb611c8fSApple OSS Distributions if (!PEWriteNVRAMProperty(SYSTEM_NVRAM_PREFIX "boot-picker-bringup-reason",
614bb611c8fSApple OSS Distributions boot_picker_reason, (const unsigned int) strlen(boot_picker_reason))) {
615bb611c8fSApple OSS Distributions IOLog("Failed to write boot-picker-bringup-reason to NVRAM.\n");
616bb611c8fSApple OSS Distributions }
617bb611c8fSApple OSS Distributions
618e6231be0SApple OSS Distributions // Set `boot-command = recover-system`.
619bb611c8fSApple OSS Distributions
620bb611c8fSApple OSS Distributions // Construct an OSSymbol and an OSString to be the (key, value) pair
621bb611c8fSApple OSS Distributions // we write to NVRAM. Unfortunately, since our value must be an OSString
622bb611c8fSApple OSS Distributions // instead of an OSData, we cannot use PEWriteNVRAMProperty() here.
623bb611c8fSApple OSS Distributions boot_command_sym = OSSymbol::withCStringNoCopy(SYSTEM_NVRAM_PREFIX "boot-command");
624e6231be0SApple OSS Distributions boot_command_recover = OSString::withCStringNoCopy("recover-system");
625bb611c8fSApple OSS Distributions if (boot_command_sym == NULL || boot_command_recover == NULL) {
626bb611c8fSApple OSS Distributions IOLog("Failed to create boot-command strings.\n");
627bb611c8fSApple OSS Distributions goto do_reboot;
628bb611c8fSApple OSS Distributions }
629bb611c8fSApple OSS Distributions
630bb611c8fSApple OSS Distributions // Wait for NVRAM to be readable...
631bb611c8fSApple OSS Distributions nvram = OSDynamicCast(IODTNVRAM, IOService::waitForService(
632bb611c8fSApple OSS Distributions IOService::serviceMatching("IODTNVRAM")));
633bb611c8fSApple OSS Distributions if (nvram == NULL) {
634bb611c8fSApple OSS Distributions IOLog("Failed to acquire IODTNVRAM object.\n");
635bb611c8fSApple OSS Distributions goto do_reboot;
636bb611c8fSApple OSS Distributions }
637bb611c8fSApple OSS Distributions
638bb611c8fSApple OSS Distributions // Wait for NVRAM to be writable...
639bb611c8fSApple OSS Distributions if (!IOServiceWaitForMatchingResource("IONVRAM", UINT64_MAX)) {
640bb611c8fSApple OSS Distributions IOLog("Failed to wait for IONVRAM service.\n");
641bb611c8fSApple OSS Distributions // attempt the work anyway...
642bb611c8fSApple OSS Distributions }
643bb611c8fSApple OSS Distributions
644bb611c8fSApple OSS Distributions // Write the new boot-command to NVRAM, and sync if successful.
645bb611c8fSApple OSS Distributions if (!nvram->setProperty(boot_command_sym, boot_command_recover)) {
646bb611c8fSApple OSS Distributions IOLog("Failed to save new boot-command to NVRAM.\n");
647bb611c8fSApple OSS Distributions } else {
648bb611c8fSApple OSS Distributions nvram->sync();
649bb611c8fSApple OSS Distributions }
650bb611c8fSApple OSS Distributions } else {
651bb611c8fSApple OSS Distributions IOLog("Unknown mode: %d\n", mode);
652bb611c8fSApple OSS Distributions return false;
653bb611c8fSApple OSS Distributions }
654bb611c8fSApple OSS Distributions
655bb611c8fSApple OSS Distributions // Clean up and reboot!
656bb611c8fSApple OSS Distributions do_reboot:
657bb611c8fSApple OSS Distributions if (boot_command_recover != NULL) {
658bb611c8fSApple OSS Distributions boot_command_recover->release();
659bb611c8fSApple OSS Distributions }
660bb611c8fSApple OSS Distributions
661bb611c8fSApple OSS Distributions if (boot_command_sym != NULL) {
662bb611c8fSApple OSS Distributions boot_command_sym->release();
663bb611c8fSApple OSS Distributions }
664bb611c8fSApple OSS Distributions
665bb611c8fSApple OSS Distributions if (reboot) {
666bb611c8fSApple OSS Distributions IOLog("\nAbout to reboot into Recovery!\n");
6671031c584SApple OSS Distributions (void)PEHaltRestart(kPEPanicRestartCPUNoCallouts);
668bb611c8fSApple OSS Distributions }
669bb611c8fSApple OSS Distributions
670bb611c8fSApple OSS Distributions return true;
671bb611c8fSApple OSS Distributions }
672bb611c8fSApple OSS Distributions
673a5e72196SApple OSS Distributions kern_return_t
IOFindBSDRoot(char * rootName,unsigned int rootNameSize,dev_t * root,u_int32_t * oflags)674a5e72196SApple OSS Distributions IOFindBSDRoot( char * rootName, unsigned int rootNameSize,
675c1dac77fSApple OSS Distributions dev_t * root, u_int32_t * oflags )
676c1dac77fSApple OSS Distributions {
677c1dac77fSApple OSS Distributions mach_timespec_t t;
678c1dac77fSApple OSS Distributions IOService * service;
679c1dac77fSApple OSS Distributions IORegistryEntry * regEntry;
680a5e72196SApple OSS Distributions OSDictionary * matching = NULL;
681c1dac77fSApple OSS Distributions OSString * iostr;
682c1dac77fSApple OSS Distributions OSNumber * off;
683a5e72196SApple OSS Distributions OSData * data = NULL;
684c1dac77fSApple OSS Distributions
685c1dac77fSApple OSS Distributions UInt32 flags = 0;
686e13b1fa5SApple OSS Distributions int mnr, mjr;
687a5e72196SApple OSS Distributions const char * mediaProperty = NULL;
688c1dac77fSApple OSS Distributions char * rdBootVar;
689e6231be0SApple OSS Distributions OSDataAllocation<char> str;
690a5e72196SApple OSS Distributions const char * look = NULL;
691c1dac77fSApple OSS Distributions int len;
692*8d741a5dSApple OSS Distributions int wdt = 0;
6938149afccSApple OSS Distributions bool debugInfoPrintedOnce = false;
694a5e72196SApple OSS Distributions bool needNetworkKexts = false;
69514e3d835SApple OSS Distributions const char * uuidStr = NULL;
696c1dac77fSApple OSS Distributions
697c1dac77fSApple OSS Distributions static int mountAttempts = 0;
698c1dac77fSApple OSS Distributions
699368ad365SApple OSS Distributions int xchar, dchar;
700368ad365SApple OSS Distributions
70188cc0b97SApple OSS Distributions // stall here for anyone matching on the IOBSD resource to finish (filesystems)
70288cc0b97SApple OSS Distributions matching = IOService::serviceMatching(gIOResourcesKey);
70388cc0b97SApple OSS Distributions assert(matching);
70488cc0b97SApple OSS Distributions matching->setObject(gIOResourceMatchedKey, gIOBSDKey);
70588cc0b97SApple OSS Distributions
70688cc0b97SApple OSS Distributions if ((service = IOService::waitForMatchingService(matching, 30ULL * kSecondScale))) {
707e6231be0SApple OSS Distributions OSSafeReleaseNULL(service);
70888cc0b97SApple OSS Distributions } else {
70988cc0b97SApple OSS Distributions IOLog("!BSD\n");
71088cc0b97SApple OSS Distributions }
71188cc0b97SApple OSS Distributions matching->release();
71288cc0b97SApple OSS Distributions matching = NULL;
713368ad365SApple OSS Distributions
714a5e72196SApple OSS Distributions if (mountAttempts++) {
71588cc0b97SApple OSS Distributions IOLog("mount(%d) failed\n", mountAttempts);
716c1dac77fSApple OSS Distributions IOSleep( 5 * 1000 );
71788cc0b97SApple OSS Distributions }
718c1dac77fSApple OSS Distributions
719e6231be0SApple OSS Distributions str = OSDataAllocation<char>( kMaxPathBuf + kMaxBootVar, OSAllocateMemory );
720a5e72196SApple OSS Distributions if (!str) {
721a5e72196SApple OSS Distributions return kIOReturnNoMemory;
722a5e72196SApple OSS Distributions }
723e6231be0SApple OSS Distributions rdBootVar = str.data() + kMaxPathBuf;
724c1dac77fSApple OSS Distributions
725e13b1fa5SApple OSS Distributions if (!PE_parse_boot_argn("rd", rdBootVar, kMaxBootVar )
726a5e72196SApple OSS Distributions && !PE_parse_boot_argn("rootdev", rdBootVar, kMaxBootVar )) {
727c1dac77fSApple OSS Distributions rdBootVar[0] = 0;
728a5e72196SApple OSS Distributions }
729c1dac77fSApple OSS Distributions
730c1dac77fSApple OSS Distributions if ((regEntry = IORegistryEntry::fromPath( "/chosen", gIODTPlane ))) {
731e6231be0SApple OSS Distributions do {
732855239e5SApple OSS Distributions di_root_ramfile(regEntry);
733e6231be0SApple OSS Distributions OSObject* unserializedContainer = NULL;
734e13b1fa5SApple OSS Distributions data = OSDynamicCast(OSData, regEntry->getProperty( "root-matching" ));
735e13b1fa5SApple OSS Distributions if (data) {
736e6231be0SApple OSS Distributions unserializedContainer = OSUnserializeXML((char *)data->getBytesNoCopy());
737e6231be0SApple OSS Distributions matching = OSDynamicCast(OSDictionary, unserializedContainer);
738e13b1fa5SApple OSS Distributions if (matching) {
739e13b1fa5SApple OSS Distributions continue;
740e13b1fa5SApple OSS Distributions }
741e13b1fa5SApple OSS Distributions }
742e6231be0SApple OSS Distributions OSSafeReleaseNULL(unserializedContainer);
743e13b1fa5SApple OSS Distributions
74414e3d835SApple OSS Distributions data = (OSData *) regEntry->getProperty( "boot-uuid" );
74514e3d835SApple OSS Distributions if (data) {
74614e3d835SApple OSS Distributions uuidStr = (const char*)data->getBytesNoCopy();
74714e3d835SApple OSS Distributions OSString *uuidString = OSString::withCString( uuidStr );
74814e3d835SApple OSS Distributions
74914e3d835SApple OSS Distributions // match the boot-args boot-uuid processing below
75014e3d835SApple OSS Distributions if (uuidString) {
75114e3d835SApple OSS Distributions IOLog("rooting via boot-uuid from /chosen: %s\n", uuidStr);
75214e3d835SApple OSS Distributions IOService::publishResource( "boot-uuid", uuidString );
75314e3d835SApple OSS Distributions uuidString->release();
75414e3d835SApple OSS Distributions matching = IOUUIDMatching();
75514e3d835SApple OSS Distributions mediaProperty = "boot-uuid-media";
75614e3d835SApple OSS Distributions continue;
75714e3d835SApple OSS Distributions } else {
75814e3d835SApple OSS Distributions uuidStr = NULL;
75914e3d835SApple OSS Distributions }
76014e3d835SApple OSS Distributions }
761c1dac77fSApple OSS Distributions } while (false);
762e6231be0SApple OSS Distributions OSSafeReleaseNULL(regEntry);
763e6231be0SApple OSS Distributions }
764c1dac77fSApple OSS Distributions
765368ad365SApple OSS Distributions //
766368ad365SApple OSS Distributions // See if we have a RAMDisk property in /chosen/memory-map. If so, make it into a device.
767368ad365SApple OSS Distributions // It will become /dev/mdx, where x is 0-f.
768368ad365SApple OSS Distributions //
769368ad365SApple OSS Distributions
770368ad365SApple OSS Distributions if (!didRam) { /* Have we already build this ram disk? */
771368ad365SApple OSS Distributions didRam = 1; /* Remember we did this */
772368ad365SApple OSS Distributions if ((regEntry = IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane ))) { /* Find the map node */
773368ad365SApple OSS Distributions data = (OSData *)regEntry->getProperty("RAMDisk"); /* Find the ram disk, if there */
774368ad365SApple OSS Distributions if (data) { /* We found one */
775186b8fceSApple OSS Distributions uintptr_t *ramdParms;
776*8d741a5dSApple OSS Distributions /* BEGIN IGNORE CODESTYLE */
777*8d741a5dSApple OSS Distributions __typed_allocators_ignore_push
778186b8fceSApple OSS Distributions ramdParms = (uintptr_t *)data->getBytesNoCopy(); /* Point to the ram disk base and size */
779*8d741a5dSApple OSS Distributions __typed_allocators_ignore_pop
780*8d741a5dSApple OSS Distributions /* END IGNORE CODESTYLE */
781bb611c8fSApple OSS Distributions #if __LP64__
782bb611c8fSApple OSS Distributions #define MAX_PHYS_RAM (((uint64_t)UINT_MAX) << 12)
783bb611c8fSApple OSS Distributions if (ramdParms[1] > MAX_PHYS_RAM) {
784bb611c8fSApple OSS Distributions panic("ramdisk params");
785bb611c8fSApple OSS Distributions }
786bb611c8fSApple OSS Distributions #endif /* __LP64__ */
787bb611c8fSApple 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 */
788368ad365SApple OSS Distributions }
789368ad365SApple OSS Distributions regEntry->release(); /* Toss the entry */
790368ad365SApple OSS Distributions }
791368ad365SApple OSS Distributions }
792368ad365SApple OSS Distributions
793368ad365SApple OSS Distributions //
794368ad365SApple OSS Distributions // Now check if we are trying to root on a memory device
795368ad365SApple OSS Distributions //
796368ad365SApple OSS Distributions
797368ad365SApple OSS Distributions if ((rdBootVar[0] == 'm') && (rdBootVar[1] == 'd') && (rdBootVar[3] == 0)) {
798368ad365SApple OSS Distributions dchar = xchar = rdBootVar[2]; /* Get the actual device */
799a5e72196SApple OSS Distributions if ((xchar >= '0') && (xchar <= '9')) {
800a5e72196SApple OSS Distributions xchar = xchar - '0'; /* If digit, convert */
801a5e72196SApple OSS Distributions } else {
802368ad365SApple OSS Distributions xchar = xchar & ~' '; /* Fold to upper case */
803368ad365SApple OSS Distributions if ((xchar >= 'A') && (xchar <= 'F')) { /* Is this a valid digit? */
804368ad365SApple OSS Distributions xchar = (xchar & 0xF) + 9; /* Convert the hex digit */
805368ad365SApple OSS Distributions dchar = dchar | ' '; /* Fold to lower case */
806a5e72196SApple OSS Distributions } else {
807a5e72196SApple OSS Distributions xchar = -1; /* Show bogus */
808368ad365SApple OSS Distributions }
809368ad365SApple OSS Distributions }
810368ad365SApple OSS Distributions if (xchar >= 0) { /* Do we have a valid memory device name? */
811e6231be0SApple OSS Distributions OSSafeReleaseNULL(matching);
812368ad365SApple OSS Distributions *root = mdevlookup(xchar); /* Find the device number */
813368ad365SApple OSS Distributions if (*root >= 0) { /* Did we find one? */
814368ad365SApple OSS Distributions rootName[0] = 'm'; /* Build root name */
815368ad365SApple OSS Distributions rootName[1] = 'd'; /* Build root name */
816bb611c8fSApple OSS Distributions rootName[2] = (char) dchar; /* Build root name */
817368ad365SApple OSS Distributions rootName[3] = 0; /* Build root name */
818368ad365SApple OSS Distributions IOLog("BSD root: %s, major %d, minor %d\n", rootName, major(*root), minor(*root));
819368ad365SApple OSS Distributions *oflags = 0; /* Show that this is not network */
82088cc0b97SApple OSS Distributions
82188cc0b97SApple OSS Distributions #if CONFIG_KDP_INTERACTIVE_DEBUGGING
82288cc0b97SApple OSS Distributions /* retrieve final ramdisk range and initialize KDP variables */
82388cc0b97SApple OSS Distributions if (mdevgetrange(xchar, &kdp_core_ramdisk_addr, &kdp_core_ramdisk_size) != 0) {
82488cc0b97SApple OSS Distributions IOLog("Unable to retrieve range for root memory device %d\n", xchar);
82588cc0b97SApple OSS Distributions kdp_core_ramdisk_addr = 0;
82688cc0b97SApple OSS Distributions kdp_core_ramdisk_size = 0;
82788cc0b97SApple OSS Distributions }
82888cc0b97SApple OSS Distributions #endif
82988cc0b97SApple OSS Distributions
830368ad365SApple OSS Distributions goto iofrootx; /* Join common exit... */
831368ad365SApple OSS Distributions }
832e6231be0SApple OSS Distributions panic("IOFindBSDRoot: specified root memory device, %s, has not been configured", rdBootVar); /* Not there */
833368ad365SApple OSS Distributions }
834368ad365SApple OSS Distributions }
835368ad365SApple OSS Distributions
836c1dac77fSApple OSS Distributions if ((!matching) && rdBootVar[0]) {
837c1dac77fSApple OSS Distributions // by BSD name
838c1dac77fSApple OSS Distributions look = rdBootVar;
839a5e72196SApple OSS Distributions if (look[0] == '*') {
840c1dac77fSApple OSS Distributions look++;
841a5e72196SApple OSS Distributions }
842c1dac77fSApple OSS Distributions
8438149afccSApple OSS Distributions if (strncmp( look, "en", strlen( "en" )) == 0) {
844c1dac77fSApple OSS Distributions matching = IONetworkNamePrefixMatching( "en" );
845a5e72196SApple OSS Distributions needNetworkKexts = true;
84614e3d835SApple OSS Distributions } else if (strncmp( look, "uuid", strlen( "uuid" )) == 0) {
847e6231be0SApple OSS Distributions OSDataAllocation<char> uuid( kMaxBootVar, OSAllocateMemory );
84814e3d835SApple OSS Distributions
84914e3d835SApple OSS Distributions if (uuid) {
850e6231be0SApple OSS Distributions OSString *uuidString;
851e6231be0SApple OSS Distributions
852e6231be0SApple OSS Distributions if (!PE_parse_boot_argn( "boot-uuid", uuid.data(), kMaxBootVar )) {
85314e3d835SApple OSS Distributions panic( "rd=uuid but no boot-uuid=<value> specified" );
85414e3d835SApple OSS Distributions }
855e6231be0SApple OSS Distributions uuidString = OSString::withCString(uuid.data());
85614e3d835SApple OSS Distributions if (uuidString) {
85714e3d835SApple OSS Distributions IOService::publishResource( "boot-uuid", uuidString );
85814e3d835SApple OSS Distributions uuidString->release();
859e6231be0SApple OSS Distributions IOLog("\nWaiting for boot volume with UUID %s\n", uuid.data());
86014e3d835SApple OSS Distributions matching = IOUUIDMatching();
86114e3d835SApple OSS Distributions mediaProperty = "boot-uuid-media";
86214e3d835SApple OSS Distributions }
86314e3d835SApple OSS Distributions }
8648149afccSApple OSS Distributions } else {
865c1dac77fSApple OSS Distributions matching = IOBSDNameMatching( look );
866c1dac77fSApple OSS Distributions }
8678149afccSApple OSS Distributions }
868c1dac77fSApple OSS Distributions
869c1dac77fSApple OSS Distributions if (!matching) {
870c1dac77fSApple OSS Distributions OSString * astring;
871e13b1fa5SApple OSS Distributions // Match any HFS media
872e13b1fa5SApple OSS Distributions
873c1dac77fSApple OSS Distributions matching = IOService::serviceMatching( "IOMedia" );
874e7776783SApple OSS Distributions assert(matching);
875368ad365SApple OSS Distributions astring = OSString::withCStringNoCopy("Apple_HFS");
876c1dac77fSApple OSS Distributions if (astring) {
8778149afccSApple OSS Distributions matching->setObject("Content", astring);
878c1dac77fSApple OSS Distributions astring->release();
879c1dac77fSApple OSS Distributions }
880c1dac77fSApple OSS Distributions }
881c1dac77fSApple OSS Distributions
882a3bb9fccSApple OSS Distributions if (gIOKitDebug & kIOWaitQuietBeforeRoot) {
883a3bb9fccSApple OSS Distributions IOLog( "Waiting for matching to complete\n" );
884a3bb9fccSApple OSS Distributions IOService::getPlatform()->waitQuiet();
885a3bb9fccSApple OSS Distributions }
886a3bb9fccSApple OSS Distributions
887e6231be0SApple OSS Distributions if (matching) {
888c1dac77fSApple OSS Distributions OSSerialize * s = OSSerialize::withCapacity( 5 );
889c1dac77fSApple OSS Distributions
890c1dac77fSApple OSS Distributions if (matching->serialize( s )) {
891c1dac77fSApple OSS Distributions IOLog( "Waiting on %s\n", s->text());
892c1dac77fSApple OSS Distributions }
893e6231be0SApple OSS Distributions s->release();
894c1dac77fSApple OSS Distributions }
895c1dac77fSApple OSS Distributions
896a5e72196SApple OSS Distributions char namep[8];
897a5e72196SApple OSS Distributions if (needNetworkKexts
898a5e72196SApple OSS Distributions || PE_parse_boot_argn("-s", namep, sizeof(namep))) {
899a5e72196SApple OSS Distributions IOService::startDeferredMatches();
900a5e72196SApple OSS Distributions }
901a5e72196SApple OSS Distributions
902*8d741a5dSApple OSS Distributions PE_parse_boot_argn("wdt", &wdt, sizeof(wdt));
903c1dac77fSApple OSS Distributions do {
904c1dac77fSApple OSS Distributions t.tv_sec = ROOTDEVICETIMEOUT;
905c1dac77fSApple OSS Distributions t.tv_nsec = 0;
906c1dac77fSApple OSS Distributions matching->retain();
907c1dac77fSApple OSS Distributions service = IOService::waitForService( matching, &t );
908*8d741a5dSApple OSS Distributions if ((-1 != wdt) && (!service || (mountAttempts == 10))) {
909bb611c8fSApple OSS Distributions #if !XNU_TARGET_OS_OSX || !defined(__arm64__)
910c1dac77fSApple OSS Distributions PE_display_icon( 0, "noroot");
911c1dac77fSApple OSS Distributions IOLog( "Still waiting for root device\n" );
912bb611c8fSApple OSS Distributions #endif
9138149afccSApple OSS Distributions
9148149afccSApple OSS Distributions if (!debugInfoPrintedOnce) {
9158149afccSApple OSS Distributions debugInfoPrintedOnce = true;
9168149afccSApple OSS Distributions if (gIOKitDebug & kIOLogDTree) {
9178149afccSApple OSS Distributions IOLog("\nDT plane:\n");
9188149afccSApple OSS Distributions IOPrintPlane( gIODTPlane );
9198149afccSApple OSS Distributions }
9208149afccSApple OSS Distributions if (gIOKitDebug & kIOLogServiceTree) {
9218149afccSApple OSS Distributions IOLog("\nService plane:\n");
9228149afccSApple OSS Distributions IOPrintPlane( gIOServicePlane );
9238149afccSApple OSS Distributions }
924a5e72196SApple OSS Distributions if (gIOKitDebug & kIOLogMemory) {
9258149afccSApple OSS Distributions IOPrintMemory();
9268149afccSApple OSS Distributions }
927c1dac77fSApple OSS Distributions }
928bb611c8fSApple OSS Distributions
929bb611c8fSApple OSS Distributions #if XNU_TARGET_OS_OSX && defined(__arm64__)
930e6231be0SApple OSS Distributions // The disk isn't found - have the user pick from System Recovery.
931bb611c8fSApple OSS Distributions (void)IOSetRecoveryBoot(BSD_BOOTFAIL_MEDIA_MISSING, NULL, true);
932*8d741a5dSApple OSS Distributions #elif XNU_TARGET_OS_IOS || XNU_TARGET_OS_XR
9335c2921b0SApple OSS Distributions panic("Failed to mount root device");
934bb611c8fSApple OSS Distributions #endif
935a5e72196SApple OSS Distributions }
936c1dac77fSApple OSS Distributions } while (!service);
937e6231be0SApple OSS Distributions
938e6231be0SApple OSS Distributions OSSafeReleaseNULL(matching);
939c1dac77fSApple OSS Distributions
940d0c1fef6SApple OSS Distributions if (service && mediaProperty) {
941e13b1fa5SApple OSS Distributions service = (IOService *)service->getProperty(mediaProperty);
942368ad365SApple OSS Distributions }
943368ad365SApple OSS Distributions
944e13b1fa5SApple OSS Distributions mjr = 0;
945e13b1fa5SApple OSS Distributions mnr = 0;
946c1dac77fSApple OSS Distributions
947c1dac77fSApple OSS Distributions // If the IOService we matched to is a subclass of IONetworkInterface,
948c1dac77fSApple OSS Distributions // then make sure it has been registered with BSD and has a BSD name
949c1dac77fSApple OSS Distributions // assigned.
950c1dac77fSApple OSS Distributions
951c1dac77fSApple OSS Distributions if (service
952c1dac77fSApple OSS Distributions && service->metaCast( "IONetworkInterface" )
953a5e72196SApple OSS Distributions && !IORegisterNetworkInterface( service )) {
954a5e72196SApple OSS Distributions service = NULL;
955c1dac77fSApple OSS Distributions }
956c1dac77fSApple OSS Distributions
957c1dac77fSApple OSS Distributions if (service) {
958c1dac77fSApple OSS Distributions len = kMaxPathBuf;
959e6231be0SApple OSS Distributions service->getPath( str.data(), &len, gIOServicePlane );
960e6231be0SApple OSS Distributions IOLog("Got boot device = %s\n", str.data());
961c1dac77fSApple OSS Distributions
962c1dac77fSApple OSS Distributions iostr = (OSString *) service->getProperty( kIOBSDNameKey );
963a5e72196SApple OSS Distributions if (iostr) {
9643ca3bd55SApple OSS Distributions strlcpy( rootName, iostr->getCStringNoCopy(), rootNameSize );
965a5e72196SApple OSS Distributions }
966c1dac77fSApple OSS Distributions off = (OSNumber *) service->getProperty( kIOBSDMajorKey );
967a5e72196SApple OSS Distributions if (off) {
968e13b1fa5SApple OSS Distributions mjr = off->unsigned32BitValue();
969a5e72196SApple OSS Distributions }
970c1dac77fSApple OSS Distributions off = (OSNumber *) service->getProperty( kIOBSDMinorKey );
971a5e72196SApple OSS Distributions if (off) {
972e13b1fa5SApple OSS Distributions mnr = off->unsigned32BitValue();
973a5e72196SApple OSS Distributions }
974c1dac77fSApple OSS Distributions
975a5e72196SApple OSS Distributions if (service->metaCast( "IONetworkInterface" )) {
976c1dac77fSApple OSS Distributions flags |= 1;
977a5e72196SApple OSS Distributions }
978c1dac77fSApple OSS Distributions } else {
979c1dac77fSApple OSS Distributions IOLog( "Wait for root failed\n" );
9803ca3bd55SApple OSS Distributions strlcpy( rootName, "en0", rootNameSize );
981c1dac77fSApple OSS Distributions flags |= 1;
982c1dac77fSApple OSS Distributions }
983c1dac77fSApple OSS Distributions
984c1dac77fSApple OSS Distributions IOLog( "BSD root: %s", rootName );
985a5e72196SApple OSS Distributions if (mjr) {
986e13b1fa5SApple OSS Distributions IOLog(", major %d, minor %d\n", mjr, mnr );
987a5e72196SApple OSS Distributions } else {
988c1dac77fSApple OSS Distributions IOLog("\n");
989a5e72196SApple OSS Distributions }
990c1dac77fSApple OSS Distributions
991e13b1fa5SApple OSS Distributions *root = makedev( mjr, mnr );
992c1dac77fSApple OSS Distributions *oflags = flags;
993c1dac77fSApple OSS Distributions
994368ad365SApple OSS Distributions iofrootx:
995e6231be0SApple OSS Distributions
996e6231be0SApple OSS Distributions IOService::setRootMedia(service);
997e6231be0SApple OSS Distributions
9988149afccSApple OSS Distributions if ((gIOKitDebug & (kIOLogDTree | kIOLogServiceTree | kIOLogMemory)) && !debugInfoPrintedOnce) {
9998149afccSApple OSS Distributions IOService::getPlatform()->waitQuiet();
1000c1dac77fSApple OSS Distributions if (gIOKitDebug & kIOLogDTree) {
1001c1dac77fSApple OSS Distributions IOLog("\nDT plane:\n");
1002c1dac77fSApple OSS Distributions IOPrintPlane( gIODTPlane );
1003c1dac77fSApple OSS Distributions }
1004c1dac77fSApple OSS Distributions if (gIOKitDebug & kIOLogServiceTree) {
1005c1dac77fSApple OSS Distributions IOLog("\nService plane:\n");
1006c1dac77fSApple OSS Distributions IOPrintPlane( gIOServicePlane );
1007c1dac77fSApple OSS Distributions }
1008a5e72196SApple OSS Distributions if (gIOKitDebug & kIOLogMemory) {
1009c1dac77fSApple OSS Distributions IOPrintMemory();
1010c1dac77fSApple OSS Distributions }
1011c1dac77fSApple OSS Distributions }
1012c1dac77fSApple OSS Distributions
1013a5e72196SApple OSS Distributions return kIOReturnSuccess;
1014a5e72196SApple OSS Distributions }
1015a5e72196SApple OSS Distributions
1016e6231be0SApple OSS Distributions void
IOSetImageBoot(void)1017e6231be0SApple OSS Distributions IOSetImageBoot(void)
1018e6231be0SApple OSS Distributions {
1019e6231be0SApple OSS Distributions // this will unhide all IOMedia, without waiting for kernelmanagement to start
1020e6231be0SApple OSS Distributions IOService::setRootMedia(NULL);
1021e6231be0SApple OSS Distributions }
1022e6231be0SApple OSS Distributions
1023a5e72196SApple OSS Distributions bool
IORamDiskBSDRoot(void)1024a5e72196SApple OSS Distributions IORamDiskBSDRoot(void)
1025186b8fceSApple OSS Distributions {
1026186b8fceSApple OSS Distributions char rdBootVar[kMaxBootVar];
1027186b8fceSApple OSS Distributions if (PE_parse_boot_argn("rd", rdBootVar, kMaxBootVar )
1028186b8fceSApple OSS Distributions || PE_parse_boot_argn("rootdev", rdBootVar, kMaxBootVar )) {
1029186b8fceSApple OSS Distributions if ((rdBootVar[0] == 'm') && (rdBootVar[1] == 'd') && (rdBootVar[3] == 0)) {
1030186b8fceSApple OSS Distributions return true;
1031186b8fceSApple OSS Distributions }
1032186b8fceSApple OSS Distributions }
1033186b8fceSApple OSS Distributions return false;
1034186b8fceSApple OSS Distributions }
1035186b8fceSApple OSS Distributions
1036a5e72196SApple OSS Distributions void
IOSecureBSDRoot(const char * rootName)1037a5e72196SApple OSS Distributions IOSecureBSDRoot(const char * rootName)
1038e13b1fa5SApple OSS Distributions {
1039bb611c8fSApple OSS Distributions #if CONFIG_SECURE_BSD_ROOT
104076e12aa3SApple OSS Distributions IOReturn result;
104176e12aa3SApple OSS Distributions IOPlatformExpert *pe;
104276e12aa3SApple OSS Distributions OSDictionary *matching;
104376e12aa3SApple OSS Distributions const OSSymbol *functionName = OSSymbol::withCStringNoCopy("SecureRootName");
104476e12aa3SApple OSS Distributions
104576e12aa3SApple OSS Distributions matching = IOService::serviceMatching("IOPlatformExpert");
104676e12aa3SApple OSS Distributions assert(matching);
104776e12aa3SApple OSS Distributions pe = (IOPlatformExpert *) IOService::waitForMatchingService(matching, 30ULL * kSecondScale);
104876e12aa3SApple OSS Distributions matching->release();
104976e12aa3SApple OSS Distributions assert(pe);
105076e12aa3SApple OSS Distributions // Returns kIOReturnNotPrivileged is the root device is not secure.
105176e12aa3SApple OSS Distributions // Returns kIOReturnUnsupported if "SecureRootName" is not implemented.
1052a5e72196SApple OSS Distributions result = pe->callPlatformFunction(functionName, false, (void *)rootName, (void *)NULL, (void *)NULL, (void *)NULL);
105376e12aa3SApple OSS Distributions functionName->release();
105476e12aa3SApple OSS Distributions OSSafeReleaseNULL(pe);
105576e12aa3SApple OSS Distributions
105676e12aa3SApple OSS Distributions if (result == kIOReturnNotPrivileged) {
105776e12aa3SApple OSS Distributions mdevremoveall();
105876e12aa3SApple OSS Distributions }
105976e12aa3SApple OSS Distributions
1060bb611c8fSApple OSS Distributions #endif // CONFIG_SECURE_BSD_ROOT
1061e13b1fa5SApple OSS Distributions }
1062e13b1fa5SApple OSS Distributions
1063fad439e7SApple OSS Distributions void *
IOBSDRegistryEntryForDeviceTree(char * path)1064fad439e7SApple OSS Distributions IOBSDRegistryEntryForDeviceTree(char * path)
1065fad439e7SApple OSS Distributions {
1066a5e72196SApple OSS Distributions return IORegistryEntry::fromPath(path, gIODTPlane);
1067fad439e7SApple OSS Distributions }
1068fad439e7SApple OSS Distributions
1069fad439e7SApple OSS Distributions void
IOBSDRegistryEntryRelease(void * entry)1070fad439e7SApple OSS Distributions IOBSDRegistryEntryRelease(void * entry)
1071fad439e7SApple OSS Distributions {
1072fad439e7SApple OSS Distributions IORegistryEntry * regEntry = (IORegistryEntry *)entry;
1073fad439e7SApple OSS Distributions
1074a5e72196SApple OSS Distributions if (regEntry) {
1075fad439e7SApple OSS Distributions regEntry->release();
1076a5e72196SApple OSS Distributions }
1077fad439e7SApple OSS Distributions return;
1078fad439e7SApple OSS Distributions }
1079fad439e7SApple OSS Distributions
1080fad439e7SApple OSS Distributions const void *
IOBSDRegistryEntryGetData(void * entry,char * property_name,int * packet_length)1081fad439e7SApple OSS Distributions IOBSDRegistryEntryGetData(void * entry, char * property_name,
1082fad439e7SApple OSS Distributions int * packet_length)
1083fad439e7SApple OSS Distributions {
1084fad439e7SApple OSS Distributions OSData * data;
1085fad439e7SApple OSS Distributions IORegistryEntry * regEntry = (IORegistryEntry *)entry;
1086fad439e7SApple OSS Distributions
1087fad439e7SApple OSS Distributions data = (OSData *) regEntry->getProperty(property_name);
1088fad439e7SApple OSS Distributions if (data) {
1089fad439e7SApple OSS Distributions *packet_length = data->getLength();
1090a5e72196SApple OSS Distributions return data->getBytesNoCopy();
1091fad439e7SApple OSS Distributions }
1092a5e72196SApple OSS Distributions return NULL;
1093fad439e7SApple OSS Distributions }
1094fad439e7SApple OSS Distributions
1095a5e72196SApple OSS Distributions kern_return_t
IOBSDGetPlatformUUID(uuid_t uuid,mach_timespec_t timeout)1096a5e72196SApple OSS Distributions IOBSDGetPlatformUUID( uuid_t uuid, mach_timespec_t timeout )
1097e13b1fa5SApple OSS Distributions {
1098e13b1fa5SApple OSS Distributions IOService * resources;
1099e13b1fa5SApple OSS Distributions OSString * string;
1100e13b1fa5SApple OSS Distributions
1101a5e72196SApple OSS Distributions resources = IOService::waitForService( IOService::resourceMatching( kIOPlatformUUIDKey ), (timeout.tv_sec || timeout.tv_nsec) ? &timeout : NULL );
1102a5e72196SApple OSS Distributions if (resources == NULL) {
1103a5e72196SApple OSS Distributions return KERN_OPERATION_TIMED_OUT;
1104a5e72196SApple OSS Distributions }
1105e13b1fa5SApple OSS Distributions
1106e13b1fa5SApple OSS Distributions string = (OSString *) IOService::getPlatform()->getProvider()->getProperty( kIOPlatformUUIDKey );
1107a5e72196SApple OSS Distributions if (string == NULL) {
1108a5e72196SApple OSS Distributions return KERN_NOT_SUPPORTED;
1109a5e72196SApple OSS Distributions }
1110e13b1fa5SApple OSS Distributions
1111e13b1fa5SApple OSS Distributions uuid_parse( string->getCStringNoCopy(), uuid );
1112e13b1fa5SApple OSS Distributions
1113e13b1fa5SApple OSS Distributions return KERN_SUCCESS;
1114e13b1fa5SApple OSS Distributions }
1115c1dac77fSApple OSS Distributions } /* extern "C" */
11160f3703acSApple OSS Distributions
11170f3703acSApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
11180f3703acSApple OSS Distributions
11190f3703acSApple OSS Distributions #include <sys/conf.h>
11205c2921b0SApple OSS Distributions #include <sys/lock.h>
11210f3703acSApple OSS Distributions #include <sys/vnode.h>
11225c2921b0SApple OSS Distributions #include <sys/vnode_if.h>
11230f3703acSApple OSS Distributions #include <sys/vnode_internal.h>
11240f3703acSApple OSS Distributions #include <sys/fcntl.h>
11255c2921b0SApple OSS Distributions #include <sys/fsctl.h>
11265c2921b0SApple OSS Distributions #include <sys/mount.h>
11270f3703acSApple OSS Distributions #include <IOKit/IOPolledInterface.h>
11280f3703acSApple OSS Distributions #include <IOKit/IOBufferMemoryDescriptor.h>
11290f3703acSApple OSS Distributions
11305c2921b0SApple OSS Distributions // see HFSIOC_VOLUME_STATUS in APFS/HFS
11315c2921b0SApple OSS Distributions #define HFS_IOCTL_VOLUME_STATUS _IOR('h', 24, u_int32_t)
11325c2921b0SApple OSS Distributions
11335c2921b0SApple OSS Distributions LCK_GRP_DECLARE(gIOPolledCoreFileGrp, "polled_corefile");
11345c2921b0SApple OSS Distributions LCK_MTX_DECLARE(gIOPolledCoreFileMtx, &gIOPolledCoreFileGrp);
11355c2921b0SApple OSS Distributions
11360f3703acSApple OSS Distributions IOPolledFileIOVars * gIOPolledCoreFileVars;
1137cc9a6355SApple OSS Distributions kern_return_t gIOPolledCoreFileOpenRet = kIOReturnNotReady;
1138a5e72196SApple OSS Distributions IOPolledCoreFileMode_t gIOPolledCoreFileMode = kIOPolledCoreFileModeNotInitialized;
1139a5e72196SApple OSS Distributions
11400f3703acSApple OSS Distributions #if IOPOLLED_COREFILE
11410f3703acSApple OSS Distributions
114294d3b452SApple OSS Distributions #define ONE_MB 1024ULL * 1024ULL
114394d3b452SApple OSS Distributions
1144a5e72196SApple OSS Distributions #if defined(XNU_TARGET_OS_BRIDGE)
1145a5e72196SApple OSS Distributions // On bridgeOS allocate a 150MB corefile and leave 150MB free
114694d3b452SApple OSS Distributions #define kIOCoreDumpSize 150ULL * ONE_MB
114794d3b452SApple OSS Distributions #define kIOCoreDumpFreeSize 150ULL * ONE_MB
1148a5e72196SApple OSS Distributions
114994d3b452SApple OSS Distributions #elif defined(XNU_TARGET_OS_OSX)
1150a5e72196SApple OSS Distributions
1151a5e72196SApple OSS Distributions // on macOS devices allocate a corefile sized at 1GB / 32GB of DRAM,
1152a5e72196SApple OSS Distributions // fallback to a 1GB corefile and leave at least 1GB free
115394d3b452SApple OSS Distributions #define kIOCoreDumpMinSize 1024ULL * ONE_MB
115494d3b452SApple OSS Distributions #define kIOCoreDumpIncrementalSize 1024ULL * ONE_MB
1155a5e72196SApple OSS Distributions
115694d3b452SApple OSS Distributions #define kIOCoreDumpFreeSize 1024ULL * ONE_MB
1157a5e72196SApple OSS Distributions
1158a5e72196SApple OSS Distributions // on older macOS devices we allocate a 1MB file at boot
1159a5e72196SApple OSS Distributions // to store a panic time stackshot
116094d3b452SApple OSS Distributions #define kIOStackshotFileSize ONE_MB
116194d3b452SApple OSS Distributions
1162*8d741a5dSApple OSS Distributions #elif defined(XNU_TARGET_OS_XR)
1163*8d741a5dSApple OSS Distributions
1164*8d741a5dSApple OSS Distributions // XR OS requries larger corefile storage because XNU core can take
1165*8d741a5dSApple OSS Distributions // up to ~500MB.
1166*8d741a5dSApple OSS Distributions
1167*8d741a5dSApple OSS Distributions #define kIOCoreDumpMinSize 350ULL * ONE_MB
1168*8d741a5dSApple OSS Distributions #define kIOCoreDumpLargeSize 750ULL * ONE_MB
1169*8d741a5dSApple OSS Distributions
1170*8d741a5dSApple OSS Distributions #define kIOCoreDumpFreeSize 350ULL * ONE_MB
1171*8d741a5dSApple OSS Distributions
117294d3b452SApple OSS Distributions #else /* defined(XNU_TARGET_OS_BRIDGE) */
117394d3b452SApple OSS Distributions
117494d3b452SApple OSS Distributions // On embedded devices with >3GB DRAM we allocate a 500MB corefile
117594d3b452SApple OSS Distributions // otherwise allocate a 350MB corefile. Leave 350 MB free
117694d3b452SApple OSS Distributions #define kIOCoreDumpMinSize 350ULL * ONE_MB
117794d3b452SApple OSS Distributions #define kIOCoreDumpLargeSize 500ULL * ONE_MB
117894d3b452SApple OSS Distributions
117994d3b452SApple OSS Distributions #define kIOCoreDumpFreeSize 350ULL * ONE_MB
1180a5e72196SApple OSS Distributions
1181a5e72196SApple OSS Distributions #endif /* defined(XNU_TARGET_OS_BRIDGE) */
1182a5e72196SApple OSS Distributions
1183a5e72196SApple OSS Distributions static IOPolledCoreFileMode_t
GetCoreFileMode()1184a5e72196SApple OSS Distributions GetCoreFileMode()
11850f3703acSApple OSS Distributions {
1186a5e72196SApple OSS Distributions if (on_device_corefile_enabled()) {
1187a5e72196SApple OSS Distributions return kIOPolledCoreFileModeCoredump;
1188a5e72196SApple OSS Distributions } else if (panic_stackshot_to_disk_enabled()) {
1189a5e72196SApple OSS Distributions return kIOPolledCoreFileModeStackshot;
1190a5e72196SApple OSS Distributions } else {
1191a5e72196SApple OSS Distributions return kIOPolledCoreFileModeDisabled;
1192a5e72196SApple OSS Distributions }
1193a5e72196SApple OSS Distributions }
11940f3703acSApple OSS Distributions
1195a5e72196SApple OSS Distributions static void
IOResolveCoreFilePath()1196*8d741a5dSApple OSS Distributions IOResolveCoreFilePath()
1197*8d741a5dSApple OSS Distributions {
1198*8d741a5dSApple OSS Distributions DTEntry node;
1199*8d741a5dSApple OSS Distributions const char *value = NULL;
1200*8d741a5dSApple OSS Distributions unsigned int size = 0;
1201*8d741a5dSApple OSS Distributions
1202*8d741a5dSApple OSS Distributions if (kSuccess != SecureDTLookupEntry(NULL, "/product", &node)) {
1203*8d741a5dSApple OSS Distributions return;
1204*8d741a5dSApple OSS Distributions }
1205*8d741a5dSApple OSS Distributions if (kSuccess != SecureDTGetProperty(node, "kernel-core-dump-location", (void const **) &value, &size)) {
1206*8d741a5dSApple OSS Distributions return;
1207*8d741a5dSApple OSS Distributions }
1208*8d741a5dSApple OSS Distributions if (size == 0) {
1209*8d741a5dSApple OSS Distributions return;
1210*8d741a5dSApple OSS Distributions }
1211*8d741a5dSApple OSS Distributions
1212*8d741a5dSApple OSS Distributions // The kdp_corefile_path is allowed to be one of 2 options to working locations.
1213*8d741a5dSApple OSS Distributions // This value is set on EARLY_BOOT since we need to know it before any volumes are mounted. The mount
1214*8d741a5dSApple OSS Distributions // event triggers IOOpenPolledCoreFile() which opens the file. Once we commit to using the path from EDT
1215*8d741a5dSApple OSS Distributions // we can't back out since a different path may reside in a different volume.
1216*8d741a5dSApple OSS Distributions // In case the path from EDT can't be opened, there will not be a kernel core-dump
1217*8d741a5dSApple OSS Distributions if (strlcmp(value, "preboot", size) == 0) {
1218*8d741a5dSApple OSS Distributions kdp_corefile_path = kIOCoreDumpPrebootPath;
1219*8d741a5dSApple OSS Distributions } else if (strlcmp(value, "default", size) != 0) {
1220*8d741a5dSApple OSS Distributions IOLog("corefile path selection in device-tree is not one of the allowed values: %s, Using default %s\n", value, kdp_corefile_path);
1221*8d741a5dSApple OSS Distributions return;
1222*8d741a5dSApple OSS Distributions }
1223*8d741a5dSApple OSS Distributions
1224*8d741a5dSApple OSS Distributions IOLog("corefile path selection in device-tree was set to: %s (value: %s)\n", kdp_corefile_path, value);
1225*8d741a5dSApple OSS Distributions }
1226*8d741a5dSApple OSS Distributions STARTUP(EARLY_BOOT, STARTUP_RANK_MIDDLE, IOResolveCoreFilePath);
1227*8d741a5dSApple OSS Distributions
1228*8d741a5dSApple OSS Distributions static void
IOCoreFileGetSize(uint64_t * ideal_size,uint64_t * fallback_size,uint64_t * free_space_to_leave,IOPolledCoreFileMode_t mode)1229a5e72196SApple OSS Distributions IOCoreFileGetSize(uint64_t *ideal_size, uint64_t *fallback_size, uint64_t *free_space_to_leave, IOPolledCoreFileMode_t mode)
1230a5e72196SApple OSS Distributions {
1231cc9a6355SApple OSS Distributions unsigned int requested_corefile_size = 0;
1232a5e72196SApple OSS Distributions
1233a5e72196SApple OSS Distributions *ideal_size = *fallback_size = *free_space_to_leave = 0;
1234a5e72196SApple OSS Distributions
12355c2921b0SApple OSS Distributions // If a custom size was requested, override the ideal and requested sizes
12365c2921b0SApple OSS Distributions if (PE_parse_boot_argn("corefile_size_mb", &requested_corefile_size,
12375c2921b0SApple OSS Distributions sizeof(requested_corefile_size))) {
12385c2921b0SApple OSS Distributions IOLog("Boot-args specify %d MB kernel corefile\n", requested_corefile_size);
12395c2921b0SApple OSS Distributions
124094d3b452SApple OSS Distributions *ideal_size = *fallback_size = (requested_corefile_size * ONE_MB);
12415c2921b0SApple OSS Distributions return;
12425c2921b0SApple OSS Distributions }
12435c2921b0SApple OSS Distributions
12445c2921b0SApple OSS Distributions unsigned int status_flags = 0;
12455c2921b0SApple OSS Distributions int error = VNOP_IOCTL(rootvnode, HFS_IOCTL_VOLUME_STATUS, (caddr_t)&status_flags, 0,
12465c2921b0SApple OSS Distributions vfs_context_kernel());
12475c2921b0SApple OSS Distributions if (!error) {
12485c2921b0SApple OSS Distributions if (status_flags & (VQ_VERYLOWDISK | VQ_LOWDISK | VQ_NEARLOWDISK)) {
12495c2921b0SApple OSS Distributions IOLog("Volume is low on space. Not allocating kernel corefile.\n");
12505c2921b0SApple OSS Distributions return;
12515c2921b0SApple OSS Distributions }
12525c2921b0SApple OSS Distributions } else {
12535c2921b0SApple OSS Distributions IOLog("Couldn't retrieve volume status. Error %d\n", error);
12545c2921b0SApple OSS Distributions }
12555c2921b0SApple OSS Distributions
1256a5e72196SApple OSS Distributions #if defined(XNU_TARGET_OS_BRIDGE)
1257a5e72196SApple OSS Distributions #pragma unused(mode)
1258a5e72196SApple OSS Distributions *ideal_size = *fallback_size = kIOCoreDumpSize;
1259a5e72196SApple OSS Distributions *free_space_to_leave = kIOCoreDumpFreeSize;
1260bb611c8fSApple OSS Distributions #elif !defined(XNU_TARGET_OS_OSX) /* defined(XNU_TARGET_OS_BRIDGE) */
1261a5e72196SApple OSS Distributions #pragma unused(mode)
1262a5e72196SApple OSS Distributions *ideal_size = *fallback_size = kIOCoreDumpMinSize;
1263a5e72196SApple OSS Distributions
126494d3b452SApple OSS Distributions if (max_mem > (3 * 1024ULL * ONE_MB)) {
1265a5e72196SApple OSS Distributions *ideal_size = kIOCoreDumpLargeSize;
1266a5e72196SApple OSS Distributions }
1267a5e72196SApple OSS Distributions
1268a5e72196SApple OSS Distributions *free_space_to_leave = kIOCoreDumpFreeSize;
1269a5e72196SApple OSS Distributions #else /* defined(XNU_TARGET_OS_BRIDGE) */
1270a5e72196SApple OSS Distributions if (mode == kIOPolledCoreFileModeCoredump) {
1271a5e72196SApple OSS Distributions *ideal_size = *fallback_size = kIOCoreDumpMinSize;
127294d3b452SApple OSS Distributions if (kIOCoreDumpIncrementalSize != 0 && max_mem > (32 * 1024ULL * ONE_MB)) {
127394d3b452SApple OSS Distributions *ideal_size = ((ROUNDUP(max_mem, (32 * 1024ULL * ONE_MB)) / (32 * 1024ULL * ONE_MB)) * kIOCoreDumpIncrementalSize);
1274a5e72196SApple OSS Distributions }
1275a5e72196SApple OSS Distributions *free_space_to_leave = kIOCoreDumpFreeSize;
1276a5e72196SApple OSS Distributions } else if (mode == kIOPolledCoreFileModeStackshot) {
1277a5e72196SApple OSS Distributions *ideal_size = *fallback_size = *free_space_to_leave = kIOStackshotFileSize;
1278a5e72196SApple OSS Distributions }
1279a5e72196SApple OSS Distributions #endif /* defined(XNU_TARGET_OS_BRIDGE) */
1280cc9a6355SApple OSS Distributions
128194d3b452SApple OSS Distributions #if EXCLAVES_COREDUMP
128294d3b452SApple OSS Distributions *ideal_size += sk_core_size();
128394d3b452SApple OSS Distributions #endif /* EXCLAVES_COREDUMP */
128494d3b452SApple OSS Distributions
1285a5e72196SApple OSS Distributions return;
1286a5e72196SApple OSS Distributions }
1287a5e72196SApple OSS Distributions
1288e6231be0SApple OSS Distributions static IOReturn
IOAccessCoreFileData(void * context,boolean_t write,uint64_t offset,int length,void * buffer)1289e6231be0SApple OSS Distributions IOAccessCoreFileData(void *context, boolean_t write, uint64_t offset, int length, void *buffer)
1290e6231be0SApple OSS Distributions {
1291e6231be0SApple OSS Distributions errno_t vnode_error = 0;
1292e6231be0SApple OSS Distributions vfs_context_t vfs_context;
1293e6231be0SApple OSS Distributions vnode_t vnode_ptr = (vnode_t) context;
1294e6231be0SApple OSS Distributions
1295e6231be0SApple OSS Distributions vfs_context = vfs_context_kernel();
1296e6231be0SApple OSS Distributions vnode_error = vn_rdwr(write ? UIO_WRITE : UIO_READ, vnode_ptr, (caddr_t)buffer, length, offset,
1297e6231be0SApple OSS Distributions UIO_SYSSPACE, IO_SWAP_DISPATCH | IO_SYNC | IO_NOCACHE | IO_UNIT, vfs_context_ucred(vfs_context), NULL, vfs_context_proc(vfs_context));
1298e6231be0SApple OSS Distributions
1299e6231be0SApple OSS Distributions if (vnode_error) {
1300e6231be0SApple OSS Distributions IOLog("Failed to %s the corefile. Error %d\n", write ? "write to" : "read from", vnode_error);
1301e6231be0SApple OSS Distributions return kIOReturnError;
1302e6231be0SApple OSS Distributions }
1303e6231be0SApple OSS Distributions
1304e6231be0SApple OSS Distributions return kIOReturnSuccess;
1305e6231be0SApple OSS Distributions }
1306e6231be0SApple OSS Distributions
1307a5e72196SApple OSS Distributions static void
IOOpenPolledCoreFile(thread_call_param_t __unused,thread_call_param_t corefilename)1308a5e72196SApple OSS Distributions IOOpenPolledCoreFile(thread_call_param_t __unused, thread_call_param_t corefilename)
1309a5e72196SApple OSS Distributions {
1310a5e72196SApple OSS Distributions assert(corefilename != NULL);
1311a5e72196SApple OSS Distributions
1312a5e72196SApple OSS Distributions IOReturn err;
1313a5e72196SApple OSS Distributions char *filename = (char *) corefilename;
1314a5e72196SApple OSS Distributions uint64_t corefile_size_bytes = 0, corefile_fallback_size_bytes = 0, free_space_to_leave_bytes = 0;
1315a5e72196SApple OSS Distributions IOPolledCoreFileMode_t mode_to_init = GetCoreFileMode();
1316a5e72196SApple OSS Distributions
1317a5e72196SApple OSS Distributions if (gIOPolledCoreFileVars) {
1318a5e72196SApple OSS Distributions return;
1319a5e72196SApple OSS Distributions }
1320a5e72196SApple OSS Distributions if (!IOPolledInterface::gMetaClass.getInstanceCount()) {
1321a5e72196SApple OSS Distributions return;
1322a5e72196SApple OSS Distributions }
1323a5e72196SApple OSS Distributions
13245c2921b0SApple OSS Distributions if (gIOPolledCoreFileMode == kIOPolledCoreFileModeUnlinked) {
13255c2921b0SApple OSS Distributions return;
13265c2921b0SApple OSS Distributions }
13275c2921b0SApple OSS Distributions
1328a5e72196SApple OSS Distributions if (mode_to_init == kIOPolledCoreFileModeDisabled) {
1329a5e72196SApple OSS Distributions gIOPolledCoreFileMode = kIOPolledCoreFileModeDisabled;
1330a5e72196SApple OSS Distributions return;
1331a5e72196SApple OSS Distributions }
1332a5e72196SApple OSS Distributions
1333a5e72196SApple OSS Distributions // We'll overwrite this once we open the file, we update this to mark that we have made
1334a5e72196SApple OSS Distributions // it past initialization
1335a5e72196SApple OSS Distributions gIOPolledCoreFileMode = kIOPolledCoreFileModeClosed;
1336a5e72196SApple OSS Distributions
1337a5e72196SApple OSS Distributions IOCoreFileGetSize(&corefile_size_bytes, &corefile_fallback_size_bytes, &free_space_to_leave_bytes, mode_to_init);
1338cc9a6355SApple OSS Distributions
13395c2921b0SApple OSS Distributions if (corefile_size_bytes == 0 && corefile_fallback_size_bytes == 0) {
13405c2921b0SApple OSS Distributions gIOPolledCoreFileMode = kIOPolledCoreFileModeUnlinked;
13415c2921b0SApple OSS Distributions return;
13425c2921b0SApple OSS Distributions }
13435c2921b0SApple OSS Distributions
1344cc9a6355SApple OSS Distributions do {
1345*8d741a5dSApple OSS Distributions // This file reference remains open long-term in case we need to write a core-dump
1346a5e72196SApple OSS Distributions err = IOPolledFileOpen(filename, kIOPolledFileCreate, corefile_size_bytes, free_space_to_leave_bytes,
1347a5e72196SApple OSS Distributions NULL, 0, &gIOPolledCoreFileVars, NULL, NULL, NULL);
1348a5e72196SApple OSS Distributions if (kIOReturnSuccess == err) {
1349cc9a6355SApple OSS Distributions break;
1350a5e72196SApple OSS Distributions } else if (kIOReturnNoSpace == err) {
1351*8d741a5dSApple OSS Distributions IOLog("Failed to open corefile of size %llu MB (low disk space)\n",
1352cc9a6355SApple OSS Distributions (corefile_size_bytes / (1024ULL * 1024ULL)));
1353a5e72196SApple OSS Distributions if (corefile_size_bytes == corefile_fallback_size_bytes) {
1354cc9a6355SApple OSS Distributions gIOPolledCoreFileOpenRet = err;
1355a5e72196SApple OSS Distributions return;
1356cc9a6355SApple OSS Distributions }
1357a5e72196SApple OSS Distributions } else {
1358cc9a6355SApple OSS Distributions IOLog("Failed to open corefile of size %llu MB (returned error 0x%x)\n",
1359cc9a6355SApple OSS Distributions (corefile_size_bytes / (1024ULL * 1024ULL)), err);
1360cc9a6355SApple OSS Distributions gIOPolledCoreFileOpenRet = err;
1361a5e72196SApple OSS Distributions return;
1362cc9a6355SApple OSS Distributions }
1363a5e72196SApple OSS Distributions
1364a5e72196SApple OSS Distributions err = IOPolledFileOpen(filename, kIOPolledFileCreate, corefile_fallback_size_bytes, free_space_to_leave_bytes,
1365a5e72196SApple OSS Distributions NULL, 0, &gIOPolledCoreFileVars, NULL, NULL, NULL);
1366a5e72196SApple OSS Distributions if (kIOReturnSuccess != err) {
1367cc9a6355SApple OSS Distributions IOLog("Failed to open corefile of size %llu MB (returned error 0x%x)\n",
1368a5e72196SApple OSS Distributions (corefile_fallback_size_bytes / (1024ULL * 1024ULL)), err);
1369cc9a6355SApple OSS Distributions gIOPolledCoreFileOpenRet = err;
1370a5e72196SApple OSS Distributions return;
1371cc9a6355SApple OSS Distributions }
1372cc9a6355SApple OSS Distributions } while (false);
13730f3703acSApple OSS Distributions
1374a5e72196SApple OSS Distributions gIOPolledCoreFileOpenRet = IOPolledFilePollersSetup(gIOPolledCoreFileVars, kIOPolledPreflightCoreDumpState);
1375a5e72196SApple OSS Distributions if (kIOReturnSuccess != gIOPolledCoreFileOpenRet) {
13765c2921b0SApple OSS Distributions IOPolledFileClose(&gIOPolledCoreFileVars, 0, NULL, 0, 0, 0, false);
1377cc9a6355SApple OSS Distributions IOLog("IOPolledFilePollersSetup for corefile failed with error: 0x%x\n", err);
1378a5e72196SApple OSS Distributions } else {
1379cc9a6355SApple OSS Distributions IOLog("Opened corefile of size %llu MB\n", (corefile_size_bytes / (1024ULL * 1024ULL)));
1380a5e72196SApple OSS Distributions gIOPolledCoreFileMode = mode_to_init;
13810f3703acSApple OSS Distributions }
13820f3703acSApple OSS Distributions
1383e6231be0SApple OSS Distributions // Provide the "polled file available" callback with a temporary way to read from the file
1384e6231be0SApple OSS Distributions (void) IOProvideCoreFileAccess(kdp_core_polled_io_polled_file_available, NULL);
1385e6231be0SApple OSS Distributions
1386a5e72196SApple OSS Distributions return;
13870f3703acSApple OSS Distributions }
13880f3703acSApple OSS Distributions
1389e6231be0SApple OSS Distributions kern_return_t
IOProvideCoreFileAccess(IOCoreFileAccessRecipient recipient,void * recipient_context)1390e6231be0SApple OSS Distributions IOProvideCoreFileAccess(IOCoreFileAccessRecipient recipient, void *recipient_context)
1391e6231be0SApple OSS Distributions {
1392e6231be0SApple OSS Distributions kern_return_t error = kIOReturnSuccess;
1393e6231be0SApple OSS Distributions errno_t vnode_error = 0;
1394e6231be0SApple OSS Distributions vfs_context_t vfs_context;
1395e6231be0SApple OSS Distributions vnode_t vnode_ptr;
1396e6231be0SApple OSS Distributions
1397e6231be0SApple OSS Distributions if (!recipient) {
1398e6231be0SApple OSS Distributions return kIOReturnBadArgument;
1399e6231be0SApple OSS Distributions }
1400e6231be0SApple OSS Distributions
1401e6231be0SApple OSS Distributions if (kIOReturnSuccess != gIOPolledCoreFileOpenRet) {
1402e6231be0SApple OSS Distributions return kIOReturnNotReady;
1403e6231be0SApple OSS Distributions }
1404e6231be0SApple OSS Distributions
1405e6231be0SApple OSS Distributions // Open the kernel corefile
1406e6231be0SApple OSS Distributions vfs_context = vfs_context_kernel();
1407*8d741a5dSApple OSS Distributions vnode_error = vnode_open(kdp_corefile_path, (FREAD | FWRITE | O_NOFOLLOW), 0600, 0, &vnode_ptr, vfs_context);
1408e6231be0SApple OSS Distributions if (vnode_error) {
1409e6231be0SApple OSS Distributions IOLog("Failed to open the corefile. Error %d\n", vnode_error);
1410e6231be0SApple OSS Distributions return kIOReturnError;
1411e6231be0SApple OSS Distributions }
1412e6231be0SApple OSS Distributions
1413e6231be0SApple OSS Distributions // Call the recipient function
1414e6231be0SApple OSS Distributions error = recipient(IOAccessCoreFileData, (void *)vnode_ptr, recipient_context);
1415e6231be0SApple OSS Distributions
1416e6231be0SApple OSS Distributions // Close the kernel corefile
1417e6231be0SApple OSS Distributions vnode_close(vnode_ptr, FREAD | FWRITE, vfs_context);
1418e6231be0SApple OSS Distributions
1419e6231be0SApple OSS Distributions return error;
1420e6231be0SApple OSS Distributions }
1421e6231be0SApple OSS Distributions
14220f3703acSApple OSS Distributions static void
IOClosePolledCoreFile(void)14230f3703acSApple OSS Distributions IOClosePolledCoreFile(void)
14240f3703acSApple OSS Distributions {
1425e6231be0SApple OSS Distributions // Notify kdp core that the corefile is no longer available
1426e6231be0SApple OSS Distributions (void) kdp_core_polled_io_polled_file_unavailable();
1427e6231be0SApple OSS Distributions
1428cc9a6355SApple OSS Distributions gIOPolledCoreFileOpenRet = kIOReturnNotOpen;
1429a5e72196SApple OSS Distributions gIOPolledCoreFileMode = kIOPolledCoreFileModeClosed;
143088cc0b97SApple OSS Distributions IOPolledFilePollersClose(gIOPolledCoreFileVars, kIOPolledPostflightCoreDumpState);
14315c2921b0SApple OSS Distributions IOPolledFileClose(&gIOPolledCoreFileVars, 0, NULL, 0, 0, 0, false);
14325c2921b0SApple OSS Distributions }
14335c2921b0SApple OSS Distributions
14345c2921b0SApple OSS Distributions static void
IOUnlinkPolledCoreFile(void)14355c2921b0SApple OSS Distributions IOUnlinkPolledCoreFile(void)
14365c2921b0SApple OSS Distributions {
14375c2921b0SApple OSS Distributions // Notify kdp core that the corefile is no longer available
14385c2921b0SApple OSS Distributions (void) kdp_core_polled_io_polled_file_unavailable();
14395c2921b0SApple OSS Distributions
14405c2921b0SApple OSS Distributions gIOPolledCoreFileOpenRet = kIOReturnNotOpen;
14415c2921b0SApple OSS Distributions gIOPolledCoreFileMode = kIOPolledCoreFileModeUnlinked;
14425c2921b0SApple OSS Distributions IOPolledFilePollersClose(gIOPolledCoreFileVars, kIOPolledPostflightCoreDumpState);
14435c2921b0SApple OSS Distributions IOPolledFileClose(&gIOPolledCoreFileVars, 0, NULL, 0, 0, 0, true);
14440f3703acSApple OSS Distributions }
14450f3703acSApple OSS Distributions
14460f3703acSApple OSS Distributions #endif /* IOPOLLED_COREFILE */
14470f3703acSApple OSS Distributions
14480f3703acSApple OSS Distributions extern "C" void
IOBSDMountChange(struct mount * mp,uint32_t op)14490f3703acSApple OSS Distributions IOBSDMountChange(struct mount * mp, uint32_t op)
14500f3703acSApple OSS Distributions {
14510f3703acSApple OSS Distributions #if IOPOLLED_COREFILE
145276e12aa3SApple OSS Distributions uint64_t flags;
145376e12aa3SApple OSS Distributions char path[128];
145476e12aa3SApple OSS Distributions int pathLen;
145576e12aa3SApple OSS Distributions vnode_t vn;
145676e12aa3SApple OSS Distributions int result;
145776e12aa3SApple OSS Distributions
14585c2921b0SApple OSS Distributions lck_mtx_lock(&gIOPolledCoreFileMtx);
14595c2921b0SApple OSS Distributions
1460a5e72196SApple OSS Distributions switch (op) {
146176e12aa3SApple OSS Distributions case kIOMountChangeMount:
146276e12aa3SApple OSS Distributions case kIOMountChangeDidResize:
146376e12aa3SApple OSS Distributions
1464a5e72196SApple OSS Distributions if (gIOPolledCoreFileVars) {
1465a5e72196SApple OSS Distributions break;
1466a5e72196SApple OSS Distributions }
146776e12aa3SApple OSS Distributions flags = vfs_flags(mp);
1468a5e72196SApple OSS Distributions if (MNT_RDONLY & flags) {
1469a5e72196SApple OSS Distributions break;
1470a5e72196SApple OSS Distributions }
1471a5e72196SApple OSS Distributions if (!(MNT_LOCAL & flags)) {
1472a5e72196SApple OSS Distributions break;
1473a5e72196SApple OSS Distributions }
147476e12aa3SApple OSS Distributions
147576e12aa3SApple OSS Distributions vn = vfs_vnodecovered(mp);
1476a5e72196SApple OSS Distributions if (!vn) {
1477a5e72196SApple OSS Distributions break;
1478a5e72196SApple OSS Distributions }
147976e12aa3SApple OSS Distributions pathLen = sizeof(path);
148076e12aa3SApple OSS Distributions result = vn_getpath(vn, &path[0], &pathLen);
148176e12aa3SApple OSS Distributions vnode_put(vn);
1482a5e72196SApple OSS Distributions if (0 != result) {
1483a5e72196SApple OSS Distributions break;
1484a5e72196SApple OSS Distributions }
1485a5e72196SApple OSS Distributions if (!pathLen) {
1486a5e72196SApple OSS Distributions break;
1487a5e72196SApple OSS Distributions }
1488cc9a6355SApple OSS Distributions #if defined(XNU_TARGET_OS_BRIDGE)
1489cc9a6355SApple OSS Distributions // on bridgeOS systems we put the core in /private/var/internal. We don't
1490cc9a6355SApple OSS Distributions // want to match with /private/var because /private/var/internal is often mounted
1491cc9a6355SApple OSS Distributions // over /private/var
1492a5e72196SApple OSS Distributions if ((pathLen - 1) < (int) strlen("/private/var/internal")) {
1493a5e72196SApple OSS Distributions break;
1494a5e72196SApple OSS Distributions }
1495cc9a6355SApple OSS Distributions #endif
1496*8d741a5dSApple OSS Distributions // Does this mount point include the kernel core-file?
1497*8d741a5dSApple OSS Distributions if (0 != strncmp(path, kdp_corefile_path, pathLen - 1)) {
1498a5e72196SApple OSS Distributions break;
1499a5e72196SApple OSS Distributions }
1500a5e72196SApple OSS Distributions
1501*8d741a5dSApple OSS Distributions thread_call_enter1(corefile_open_call, (void *) kdp_corefile_path);
150276e12aa3SApple OSS Distributions break;
150376e12aa3SApple OSS Distributions
150476e12aa3SApple OSS Distributions case kIOMountChangeUnmount:
150576e12aa3SApple OSS Distributions case kIOMountChangeWillResize:
1506a5e72196SApple OSS Distributions if (gIOPolledCoreFileVars && (mp == kern_file_mount(gIOPolledCoreFileVars->fileRef))) {
1507a5e72196SApple OSS Distributions thread_call_cancel_wait(corefile_open_call);
150876e12aa3SApple OSS Distributions IOClosePolledCoreFile();
150976e12aa3SApple OSS Distributions }
151076e12aa3SApple OSS Distributions break;
151176e12aa3SApple OSS Distributions }
15125c2921b0SApple OSS Distributions
15135c2921b0SApple OSS Distributions lck_mtx_unlock(&gIOPolledCoreFileMtx);
15140f3703acSApple OSS Distributions #endif /* IOPOLLED_COREFILE */
15150f3703acSApple OSS Distributions }
15160f3703acSApple OSS Distributions
15175c2921b0SApple OSS Distributions extern "C" void
IOBSDLowSpaceUnlinkKernelCore(void)15185c2921b0SApple OSS Distributions IOBSDLowSpaceUnlinkKernelCore(void)
15195c2921b0SApple OSS Distributions {
15205c2921b0SApple OSS Distributions #if IOPOLLED_COREFILE
15215c2921b0SApple OSS Distributions lck_mtx_lock(&gIOPolledCoreFileMtx);
15225c2921b0SApple OSS Distributions if (gIOPolledCoreFileVars) {
15235c2921b0SApple OSS Distributions thread_call_cancel_wait(corefile_open_call);
15245c2921b0SApple OSS Distributions IOUnlinkPolledCoreFile();
15255c2921b0SApple OSS Distributions }
15265c2921b0SApple OSS Distributions lck_mtx_unlock(&gIOPolledCoreFileMtx);
15275c2921b0SApple OSS Distributions #endif
15285c2921b0SApple OSS Distributions }
15295c2921b0SApple OSS Distributions
15300f3703acSApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15310f3703acSApple OSS Distributions
1532aca3beaaSApple OSS Distributions static char*
copyOSStringAsCString(OSString * string)1533aca3beaaSApple OSS Distributions copyOSStringAsCString(OSString *string)
1534aca3beaaSApple OSS Distributions {
1535aca3beaaSApple OSS Distributions size_t string_length = 0;
1536aca3beaaSApple OSS Distributions char *c_string = NULL;
1537aca3beaaSApple OSS Distributions
1538aca3beaaSApple OSS Distributions if (string == NULL) {
1539aca3beaaSApple OSS Distributions return NULL;
1540aca3beaaSApple OSS Distributions }
1541aca3beaaSApple OSS Distributions string_length = string->getLength() + 1;
1542aca3beaaSApple OSS Distributions
1543aca3beaaSApple OSS Distributions /* Allocate kernel data memory for the string */
1544aca3beaaSApple OSS Distributions c_string = (char*)kalloc_data(string_length, (zalloc_flags_t)(Z_ZERO | Z_WAITOK | Z_NOFAIL));
1545aca3beaaSApple OSS Distributions assert(c_string != NULL);
1546aca3beaaSApple OSS Distributions
1547aca3beaaSApple OSS Distributions /* Copy in the string */
1548aca3beaaSApple OSS Distributions strlcpy(c_string, string->getCStringNoCopy(), string_length);
1549aca3beaaSApple OSS Distributions
1550aca3beaaSApple OSS Distributions return c_string;
1551aca3beaaSApple OSS Distributions }
1552aca3beaaSApple OSS Distributions
1553aca3beaaSApple OSS Distributions extern "C" OS_ALWAYS_INLINE boolean_t
IOCurrentTaskHasStringEntitlement(const char * entitlement,const char * value)15545c2921b0SApple OSS Distributions IOCurrentTaskHasStringEntitlement(const char *entitlement, const char *value)
15555c2921b0SApple OSS Distributions {
15565c2921b0SApple OSS Distributions return IOTaskHasStringEntitlement(NULL, entitlement, value);
15575c2921b0SApple OSS Distributions }
15585c2921b0SApple OSS Distributions
15595c2921b0SApple OSS Distributions extern "C" boolean_t
IOTaskHasStringEntitlement(task_t task,const char * entitlement,const char * value)15605c2921b0SApple OSS Distributions IOTaskHasStringEntitlement(task_t task, const char *entitlement, const char *value)
15615c2921b0SApple OSS Distributions {
15625c2921b0SApple OSS Distributions if (task == NULL) {
15635c2921b0SApple OSS Distributions task = current_task();
15645c2921b0SApple OSS Distributions }
15655c2921b0SApple OSS Distributions
1566aca3beaaSApple OSS Distributions /* Validate input arguments */
1567aca3beaaSApple OSS Distributions if (task == kernel_task || entitlement == NULL || value == NULL) {
1568aca3beaaSApple OSS Distributions return false;
1569aca3beaaSApple OSS Distributions }
1570aca3beaaSApple OSS Distributions proc_t proc = (proc_t)get_bsdtask_info(task);
1571aca3beaaSApple OSS Distributions
1572aca3beaaSApple OSS Distributions kern_return_t ret = amfi->OSEntitlements.queryEntitlementStringWithProc(
1573aca3beaaSApple OSS Distributions proc,
1574aca3beaaSApple OSS Distributions entitlement,
1575aca3beaaSApple OSS Distributions value);
1576aca3beaaSApple OSS Distributions
1577aca3beaaSApple OSS Distributions if (ret == KERN_SUCCESS) {
1578aca3beaaSApple OSS Distributions return true;
1579aca3beaaSApple OSS Distributions }
1580aca3beaaSApple OSS Distributions
15815c2921b0SApple OSS Distributions return false;
15825c2921b0SApple OSS Distributions }
15835c2921b0SApple OSS Distributions
1584aca3beaaSApple OSS Distributions extern "C" OS_ALWAYS_INLINE boolean_t
IOCurrentTaskHasEntitlement(const char * entitlement)1585e6231be0SApple OSS Distributions IOCurrentTaskHasEntitlement(const char *entitlement)
1586e6231be0SApple OSS Distributions {
1587e6231be0SApple OSS Distributions return IOTaskHasEntitlement(NULL, entitlement);
1588e6231be0SApple OSS Distributions }
1589e6231be0SApple OSS Distributions
15900f3703acSApple OSS Distributions extern "C" boolean_t
IOTaskHasEntitlement(task_t task,const char * entitlement)15910f3703acSApple OSS Distributions IOTaskHasEntitlement(task_t task, const char *entitlement)
15920f3703acSApple OSS Distributions {
1593e6231be0SApple OSS Distributions if (task == NULL) {
1594e6231be0SApple OSS Distributions task = current_task();
1595e6231be0SApple OSS Distributions }
1596e6231be0SApple OSS Distributions
1597aca3beaaSApple OSS Distributions /* Validate input arguments */
1598aca3beaaSApple OSS Distributions if (task == kernel_task || entitlement == NULL) {
1599aca3beaaSApple OSS Distributions return false;
1600aca3beaaSApple OSS Distributions }
1601aca3beaaSApple OSS Distributions proc_t proc = (proc_t)get_bsdtask_info(task);
1602e6231be0SApple OSS Distributions
1603aca3beaaSApple OSS Distributions kern_return_t ret = amfi->OSEntitlements.queryEntitlementBooleanWithProc(
1604aca3beaaSApple OSS Distributions proc,
1605aca3beaaSApple OSS Distributions entitlement);
1606aca3beaaSApple OSS Distributions
1607aca3beaaSApple OSS Distributions if (ret == KERN_SUCCESS) {
1608aca3beaaSApple OSS Distributions return true;
1609aca3beaaSApple OSS Distributions }
1610aca3beaaSApple OSS Distributions
1611e6231be0SApple OSS Distributions return false;
1612e6231be0SApple OSS Distributions }
1613e6231be0SApple OSS Distributions
1614aca3beaaSApple OSS Distributions extern "C" OS_ALWAYS_INLINE char*
IOCurrentTaskGetEntitlement(const char * entitlement)1615aca3beaaSApple OSS Distributions IOCurrentTaskGetEntitlement(const char *entitlement)
1616aca3beaaSApple OSS Distributions {
1617aca3beaaSApple OSS Distributions return IOTaskGetEntitlement(NULL, entitlement);
1618e6231be0SApple OSS Distributions }
1619e6231be0SApple OSS Distributions
1620aca3beaaSApple OSS Distributions extern "C" char*
IOTaskGetEntitlement(task_t task,const char * entitlement)1621aca3beaaSApple OSS Distributions IOTaskGetEntitlement(task_t task, const char *entitlement)
1622aca3beaaSApple OSS Distributions {
1623aca3beaaSApple OSS Distributions void *entitlement_object = NULL;
1624aca3beaaSApple OSS Distributions char *return_value = NULL;
1625aca3beaaSApple OSS Distributions
1626aca3beaaSApple OSS Distributions if (task == NULL) {
1627aca3beaaSApple OSS Distributions task = current_task();
1628e6231be0SApple OSS Distributions }
1629e6231be0SApple OSS Distributions
1630aca3beaaSApple OSS Distributions /* Validate input arguments */
1631aca3beaaSApple OSS Distributions if (task == kernel_task || entitlement == NULL) {
1632aca3beaaSApple OSS Distributions return NULL;
1633e6231be0SApple OSS Distributions }
1634aca3beaaSApple OSS Distributions proc_t proc = (proc_t)get_bsdtask_info(task);
1635e6231be0SApple OSS Distributions
1636aca3beaaSApple OSS Distributions kern_return_t ret = amfi->OSEntitlements.copyEntitlementAsOSObjectWithProc(
1637aca3beaaSApple OSS Distributions proc,
1638aca3beaaSApple OSS Distributions entitlement,
1639aca3beaaSApple OSS Distributions &entitlement_object);
1640aca3beaaSApple OSS Distributions
1641aca3beaaSApple OSS Distributions if (ret != KERN_SUCCESS) {
1642aca3beaaSApple OSS Distributions return NULL;
1643aca3beaaSApple OSS Distributions }
1644aca3beaaSApple OSS Distributions assert(entitlement_object != NULL);
1645aca3beaaSApple OSS Distributions
1646aca3beaaSApple OSS Distributions OSObject *os_object = (OSObject*)entitlement_object;
1647aca3beaaSApple OSS Distributions OSString *os_string = OSDynamicCast(OSString, os_object);
1648aca3beaaSApple OSS Distributions
1649aca3beaaSApple OSS Distributions /* Get a C string version of the OSString */
1650aca3beaaSApple OSS Distributions return_value = copyOSStringAsCString(os_string);
1651aca3beaaSApple OSS Distributions
1652aca3beaaSApple OSS Distributions /* Free the OSObject which was given to us */
1653aca3beaaSApple OSS Distributions OSSafeReleaseNULL(os_object);
1654aca3beaaSApple OSS Distributions
1655aca3beaaSApple OSS Distributions return return_value;
1656a5e72196SApple OSS Distributions }
1657bb611c8fSApple OSS Distributions
1658bb611c8fSApple OSS Distributions extern "C" boolean_t
IOVnodeHasEntitlement(vnode_t vnode,int64_t off,const char * entitlement)1659bb611c8fSApple OSS Distributions IOVnodeHasEntitlement(vnode_t vnode, int64_t off, const char *entitlement)
1660bb611c8fSApple OSS Distributions {
1661bb611c8fSApple OSS Distributions OSObject * obj;
1662bb611c8fSApple OSS Distributions off_t offset = (off_t)off;
1663bb611c8fSApple OSS Distributions
1664bb611c8fSApple OSS Distributions obj = IOUserClient::copyClientEntitlementVnode(vnode, offset, entitlement);
1665bb611c8fSApple OSS Distributions if (!obj) {
1666bb611c8fSApple OSS Distributions return false;
1667bb611c8fSApple OSS Distributions }
1668bb611c8fSApple OSS Distributions obj->release();
1669bb611c8fSApple OSS Distributions return obj != kOSBooleanFalse;
1670bb611c8fSApple OSS Distributions }
1671bb611c8fSApple OSS Distributions
1672bb611c8fSApple OSS Distributions extern "C" char *
IOVnodeGetEntitlement(vnode_t vnode,int64_t off,const char * entitlement)1673bb611c8fSApple OSS Distributions IOVnodeGetEntitlement(vnode_t vnode, int64_t off, const char *entitlement)
1674bb611c8fSApple OSS Distributions {
1675bb611c8fSApple OSS Distributions OSObject *obj = NULL;
1676bb611c8fSApple OSS Distributions OSString *str = NULL;
1677bb611c8fSApple OSS Distributions size_t len;
1678bb611c8fSApple OSS Distributions char *value = NULL;
1679bb611c8fSApple OSS Distributions off_t offset = (off_t)off;
1680bb611c8fSApple OSS Distributions
1681bb611c8fSApple OSS Distributions obj = IOUserClient::copyClientEntitlementVnode(vnode, offset, entitlement);
1682bb611c8fSApple OSS Distributions if (obj != NULL) {
1683bb611c8fSApple OSS Distributions str = OSDynamicCast(OSString, obj);
1684bb611c8fSApple OSS Distributions if (str != NULL) {
1685bb611c8fSApple OSS Distributions len = str->getLength() + 1;
1686e6231be0SApple OSS Distributions value = (char *)kalloc_data(len, Z_WAITOK);
1687bb611c8fSApple OSS Distributions strlcpy(value, str->getCStringNoCopy(), len);
1688bb611c8fSApple OSS Distributions }
1689bb611c8fSApple OSS Distributions obj->release();
1690bb611c8fSApple OSS Distributions }
1691bb611c8fSApple OSS Distributions return value;
1692bb611c8fSApple OSS Distributions }
1693