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