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