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