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