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