]> git.saurik.com Git - apple/xnu.git/blob - iokit/bsddev/IOKitBSDInit.cpp
xnu-4570.61.1.tar.gz
[apple/xnu.git] / iokit / bsddev / IOKitBSDInit.cpp
1 /*
2 * Copyright (c) 1998-2018 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 #include <IOKit/IOBSD.h>
29 #include <IOKit/IOLib.h>
30 #include <IOKit/IOService.h>
31 #include <IOKit/IOCatalogue.h>
32 #include <IOKit/IODeviceTreeSupport.h>
33 #include <IOKit/IOKitKeys.h>
34 #include <IOKit/IOPlatformExpert.h>
35 #include <IOKit/IOUserClient.h>
36
37 extern "C" {
38
39 #include <pexpert/pexpert.h>
40 #include <kern/clock.h>
41 #include <uuid/uuid.h>
42 #include <sys/vnode_internal.h>
43 #include <sys/mount.h>
44
45 // how long to wait for matching root device, secs
46 #if DEBUG
47 #define ROOTDEVICETIMEOUT 120
48 #else
49 #define ROOTDEVICETIMEOUT 60
50 #endif
51
52 int panic_on_exception_triage = 0;
53
54 extern dev_t mdevadd(int devid, uint64_t base, unsigned int size, int phys);
55 extern dev_t mdevlookup(int devid);
56 extern void mdevremoveall(void);
57 extern int mdevgetrange(int devid, uint64_t *base, uint64_t *size);
58 extern void di_root_ramfile(IORegistryEntry * entry);
59
60
61 #if CONFIG_EMBEDDED
62 #define IOPOLLED_COREFILE (CONFIG_KDP_INTERACTIVE_DEBUGGING)
63
64 #if defined(XNU_TARGET_OS_BRIDGE)
65 #define kIOCoreDumpSize 150ULL*1024ULL*1024ULL
66 // leave free space on volume:
67 #define kIOCoreDumpFreeSize 150ULL*1024ULL*1024ULL
68 #define kIOCoreDumpPath "/private/var/internal/kernelcore"
69 #else
70 #define kIOCoreDumpSize 350ULL*1024ULL*1024ULL
71 // leave free space on volume:
72 #define kIOCoreDumpFreeSize 350ULL*1024ULL*1024ULL
73 #define kIOCoreDumpPath "/private/var/vm/kernelcore"
74 #endif
75
76 #elif DEVELOPMENT
77 #define IOPOLLED_COREFILE 1
78 // no sizing
79 #define kIOCoreDumpSize 0ULL
80 #define kIOCoreDumpFreeSize 0ULL
81 #else
82 #define IOPOLLED_COREFILE 0
83 #endif
84
85
86 #if IOPOLLED_COREFILE
87 static bool
88 NewKernelCoreMedia(void * target, void * refCon,
89 IOService * newService,
90 IONotifier * notifier);
91 #endif /* IOPOLLED_COREFILE */
92
93 #if CONFIG_KDP_INTERACTIVE_DEBUGGING
94 /*
95 * Touched by IOFindBSDRoot() if a RAMDisk is used for the root device.
96 */
97 extern uint64_t kdp_core_ramdisk_addr;
98 extern uint64_t kdp_core_ramdisk_size;
99 #endif
100
101 kern_return_t
102 IOKitBSDInit( void )
103 {
104 IOService::publishResource("IOBSD");
105
106 return( kIOReturnSuccess );
107 }
108
109 void
110 IOServicePublishResource( const char * property, boolean_t value )
111 {
112 if ( value)
113 IOService::publishResource( property, kOSBooleanTrue );
114 else
115 IOService::getResourceService()->removeProperty( property );
116 }
117
118 boolean_t
119 IOServiceWaitForMatchingResource( const char * property, uint64_t timeout )
120 {
121 OSDictionary * dict = 0;
122 IOService * match = 0;
123 boolean_t found = false;
124
125 do {
126
127 dict = IOService::resourceMatching( property );
128 if( !dict)
129 continue;
130 match = IOService::waitForMatchingService( dict, timeout );
131 if ( match)
132 found = true;
133
134 } while( false );
135
136 if( dict)
137 dict->release();
138 if( match)
139 match->release();
140
141 return( found );
142 }
143
144 boolean_t
145 IOCatalogueMatchingDriversPresent( const char * property )
146 {
147 OSDictionary * dict = 0;
148 OSOrderedSet * set = 0;
149 SInt32 generationCount = 0;
150 boolean_t found = false;
151
152 do {
153
154 dict = OSDictionary::withCapacity(1);
155 if( !dict)
156 continue;
157 dict->setObject( property, kOSBooleanTrue );
158 set = gIOCatalogue->findDrivers( dict, &generationCount );
159 if ( set && (set->getCount() > 0))
160 found = true;
161
162 } while( false );
163
164 if( dict)
165 dict->release();
166 if( set)
167 set->release();
168
169 return( found );
170 }
171
172 OSDictionary * IOBSDNameMatching( const char * name )
173 {
174 OSDictionary * dict;
175 const OSSymbol * str = 0;
176
177 do {
178
179 dict = IOService::serviceMatching( gIOServiceKey );
180 if( !dict)
181 continue;
182 str = OSSymbol::withCString( name );
183 if( !str)
184 continue;
185 dict->setObject( kIOBSDNameKey, (OSObject *) str );
186 str->release();
187
188 return( dict );
189
190 } while( false );
191
192 if( dict)
193 dict->release();
194 if( str)
195 str->release();
196
197 return( 0 );
198 }
199
200 OSDictionary * IOUUIDMatching( void )
201 {
202 return IOService::resourceMatching( "boot-uuid-media" );
203 }
204
205 OSDictionary * IONetworkNamePrefixMatching( const char * prefix )
206 {
207 OSDictionary * matching;
208 OSDictionary * propDict = 0;
209 const OSSymbol * str = 0;
210 char networkType[128];
211
212 do {
213 matching = IOService::serviceMatching( "IONetworkInterface" );
214 if ( matching == 0 )
215 continue;
216
217 propDict = OSDictionary::withCapacity(1);
218 if ( propDict == 0 )
219 continue;
220
221 str = OSSymbol::withCString( prefix );
222 if ( str == 0 )
223 continue;
224
225 propDict->setObject( "IOInterfaceNamePrefix", (OSObject *) str );
226 str->release();
227 str = 0;
228
229 // see if we're contrained to netroot off of specific network type
230 if(PE_parse_boot_argn( "network-type", networkType, 128 ))
231 {
232 str = OSSymbol::withCString( networkType );
233 if(str)
234 {
235 propDict->setObject( "IONetworkRootType", str);
236 str->release();
237 str = 0;
238 }
239 }
240
241 if ( matching->setObject( gIOPropertyMatchKey,
242 (OSObject *) propDict ) != true )
243 continue;
244
245 propDict->release();
246 propDict = 0;
247
248 return( matching );
249
250 } while ( false );
251
252 if ( matching ) matching->release();
253 if ( propDict ) propDict->release();
254 if ( str ) str->release();
255
256 return( 0 );
257 }
258
259 static bool IORegisterNetworkInterface( IOService * netif )
260 {
261 // A network interface is typically named and registered
262 // with BSD after receiving a request from a user space
263 // "namer". However, for cases when the system needs to
264 // root from the network, this registration task must be
265 // done inside the kernel and completed before the root
266 // device is handed to BSD.
267
268 IOService * stack;
269 OSNumber * zero = 0;
270 OSString * path = 0;
271 OSDictionary * dict = 0;
272 char * pathBuf = 0;
273 int len;
274 enum { kMaxPathLen = 512 };
275
276 do {
277 stack = IOService::waitForService(
278 IOService::serviceMatching("IONetworkStack") );
279 if ( stack == 0 ) break;
280
281 dict = OSDictionary::withCapacity(3);
282 if ( dict == 0 ) break;
283
284 zero = OSNumber::withNumber((UInt64) 0, 32);
285 if ( zero == 0 ) break;
286
287 pathBuf = (char *) IOMalloc( kMaxPathLen );
288 if ( pathBuf == 0 ) break;
289
290 len = kMaxPathLen;
291 if ( netif->getPath( pathBuf, &len, gIOServicePlane )
292 == false ) break;
293
294 path = OSString::withCStringNoCopy( pathBuf );
295 if ( path == 0 ) break;
296
297 dict->setObject( "IOInterfaceUnit", zero );
298 dict->setObject( kIOPathMatchKey, path );
299
300 stack->setProperties( dict );
301 }
302 while ( false );
303
304 if ( zero ) zero->release();
305 if ( path ) path->release();
306 if ( dict ) dict->release();
307 if ( pathBuf ) IOFree(pathBuf, kMaxPathLen);
308
309 return ( netif->getProperty( kIOBSDNameKey ) != 0 );
310 }
311
312 OSDictionary * IOOFPathMatching( const char * path, char * buf, int maxLen )
313 {
314 OSDictionary * matching = NULL;
315 OSString * str;
316 char * comp;
317 int len;
318
319 do {
320
321 len = strlen( kIODeviceTreePlane ":" );
322 maxLen -= len;
323 if( maxLen <= 0)
324 continue;
325
326 strlcpy( buf, kIODeviceTreePlane ":", len + 1 );
327 comp = buf + len;
328
329 len = strlen( path );
330 maxLen -= len;
331 if( maxLen <= 0)
332 continue;
333 strlcpy( comp, path, len + 1 );
334
335 matching = OSDictionary::withCapacity( 1 );
336 if( !matching)
337 continue;
338
339 str = OSString::withCString( buf );
340 if( !str)
341 continue;
342 matching->setObject( kIOPathMatchKey, str );
343 str->release();
344
345 return( matching );
346
347 } while( false );
348
349 if( matching)
350 matching->release();
351
352 return( 0 );
353 }
354
355 static int didRam = 0;
356 enum { kMaxPathBuf = 512, kMaxBootVar = 128 };
357
358 kern_return_t IOFindBSDRoot( char * rootName, unsigned int rootNameSize,
359 dev_t * root, u_int32_t * oflags )
360 {
361 mach_timespec_t t;
362 IOService * service;
363 IORegistryEntry * regEntry;
364 OSDictionary * matching = 0;
365 OSString * iostr;
366 OSNumber * off;
367 OSData * data = 0;
368
369 UInt32 flags = 0;
370 int mnr, mjr;
371 const char * mediaProperty = 0;
372 char * rdBootVar;
373 char * str;
374 const char * look = 0;
375 int len;
376 bool debugInfoPrintedOnce = false;
377 const char * uuidStr = NULL;
378
379 static int mountAttempts = 0;
380
381 int xchar, dchar;
382
383 // stall here for anyone matching on the IOBSD resource to finish (filesystems)
384 matching = IOService::serviceMatching(gIOResourcesKey);
385 assert(matching);
386 matching->setObject(gIOResourceMatchedKey, gIOBSDKey);
387
388 if ((service = IOService::waitForMatchingService(matching, 30ULL * kSecondScale))) {
389 service->release();
390 } else {
391 IOLog("!BSD\n");
392 }
393 matching->release();
394 matching = NULL;
395
396 if( mountAttempts++)
397 {
398 IOLog("mount(%d) failed\n", mountAttempts);
399 IOSleep( 5 * 1000 );
400 }
401
402 str = (char *) IOMalloc( kMaxPathBuf + kMaxBootVar );
403 if( !str)
404 return( kIOReturnNoMemory );
405 rdBootVar = str + kMaxPathBuf;
406
407 if (!PE_parse_boot_argn("rd", rdBootVar, kMaxBootVar )
408 && !PE_parse_boot_argn("rootdev", rdBootVar, kMaxBootVar ))
409 rdBootVar[0] = 0;
410
411 do {
412 if( (regEntry = IORegistryEntry::fromPath( "/chosen", gIODTPlane ))) {
413 di_root_ramfile(regEntry);
414 data = OSDynamicCast(OSData, regEntry->getProperty( "root-matching" ));
415 if (data) {
416 matching = OSDynamicCast(OSDictionary, OSUnserializeXML((char *)data->getBytesNoCopy()));
417 if (matching) {
418 continue;
419 }
420 }
421
422 data = (OSData *) regEntry->getProperty( "boot-uuid" );
423 if( data) {
424 uuidStr = (const char*)data->getBytesNoCopy();
425 OSString *uuidString = OSString::withCString( uuidStr );
426
427 // match the boot-args boot-uuid processing below
428 if( uuidString) {
429 IOLog("rooting via boot-uuid from /chosen: %s\n", uuidStr);
430 IOService::publishResource( "boot-uuid", uuidString );
431 uuidString->release();
432 matching = IOUUIDMatching();
433 mediaProperty = "boot-uuid-media";
434 regEntry->release();
435 continue;
436 } else {
437 uuidStr = NULL;
438 }
439 }
440 regEntry->release();
441 }
442 } while( false );
443
444 //
445 // See if we have a RAMDisk property in /chosen/memory-map. If so, make it into a device.
446 // It will become /dev/mdx, where x is 0-f.
447 //
448
449 if(!didRam) { /* Have we already build this ram disk? */
450 didRam = 1; /* Remember we did this */
451 if((regEntry = IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane ))) { /* Find the map node */
452 data = (OSData *)regEntry->getProperty("RAMDisk"); /* Find the ram disk, if there */
453 if(data) { /* We found one */
454 uintptr_t *ramdParms;
455 ramdParms = (uintptr_t *)data->getBytesNoCopy(); /* Point to the ram disk base and size */
456 (void)mdevadd(-1, ml_static_ptovirt(ramdParms[0]) >> 12, ramdParms[1] >> 12, 0); /* Initialize it and pass back the device number */
457 }
458 regEntry->release(); /* Toss the entry */
459 }
460 }
461
462 //
463 // Now check if we are trying to root on a memory device
464 //
465
466 if((rdBootVar[0] == 'm') && (rdBootVar[1] == 'd') && (rdBootVar[3] == 0)) {
467 dchar = xchar = rdBootVar[2]; /* Get the actual device */
468 if((xchar >= '0') && (xchar <= '9')) xchar = xchar - '0'; /* If digit, convert */
469 else {
470 xchar = xchar & ~' '; /* Fold to upper case */
471 if((xchar >= 'A') && (xchar <= 'F')) { /* Is this a valid digit? */
472 xchar = (xchar & 0xF) + 9; /* Convert the hex digit */
473 dchar = dchar | ' '; /* Fold to lower case */
474 }
475 else xchar = -1; /* Show bogus */
476 }
477 if(xchar >= 0) { /* Do we have a valid memory device name? */
478 *root = mdevlookup(xchar); /* Find the device number */
479 if(*root >= 0) { /* Did we find one? */
480 rootName[0] = 'm'; /* Build root name */
481 rootName[1] = 'd'; /* Build root name */
482 rootName[2] = dchar; /* Build root name */
483 rootName[3] = 0; /* Build root name */
484 IOLog("BSD root: %s, major %d, minor %d\n", rootName, major(*root), minor(*root));
485 *oflags = 0; /* Show that this is not network */
486
487 #if CONFIG_KDP_INTERACTIVE_DEBUGGING
488 /* retrieve final ramdisk range and initialize KDP variables */
489 if (mdevgetrange(xchar, &kdp_core_ramdisk_addr, &kdp_core_ramdisk_size) != 0) {
490 IOLog("Unable to retrieve range for root memory device %d\n", xchar);
491 kdp_core_ramdisk_addr = 0;
492 kdp_core_ramdisk_size = 0;
493 }
494 #endif
495
496 goto iofrootx; /* Join common exit... */
497 }
498 panic("IOFindBSDRoot: specified root memory device, %s, has not been configured\n", rdBootVar); /* Not there */
499 }
500 }
501
502 if( (!matching) && rdBootVar[0] ) {
503 // by BSD name
504 look = rdBootVar;
505 if( look[0] == '*')
506 look++;
507
508 if ( strncmp( look, "en", strlen( "en" )) == 0 ) {
509 matching = IONetworkNamePrefixMatching( "en" );
510 } else if ( strncmp( look, "uuid", strlen( "uuid" )) == 0 ) {
511 char *uuid;
512 OSString *uuidString;
513
514 uuid = (char *)IOMalloc( kMaxBootVar );
515
516 if ( uuid ) {
517 if (!PE_parse_boot_argn( "boot-uuid", uuid, kMaxBootVar )) {
518 panic( "rd=uuid but no boot-uuid=<value> specified" );
519 }
520 uuidString = OSString::withCString( uuid );
521 if ( uuidString ) {
522 IOService::publishResource( "boot-uuid", uuidString );
523 uuidString->release();
524 IOLog( "\nWaiting for boot volume with UUID %s\n", uuid );
525 matching = IOUUIDMatching();
526 mediaProperty = "boot-uuid-media";
527 }
528 IOFree( uuid, kMaxBootVar );
529 }
530 } else {
531 matching = IOBSDNameMatching( look );
532 }
533 }
534
535 if( !matching) {
536 OSString * astring;
537 // Match any HFS media
538
539 matching = IOService::serviceMatching( "IOMedia" );
540 astring = OSString::withCStringNoCopy("Apple_HFS");
541 if ( astring ) {
542 matching->setObject("Content", astring);
543 astring->release();
544 }
545 }
546
547 if( gIOKitDebug & kIOWaitQuietBeforeRoot ) {
548 IOLog( "Waiting for matching to complete\n" );
549 IOService::getPlatform()->waitQuiet();
550 }
551
552 if( true && matching) {
553 OSSerialize * s = OSSerialize::withCapacity( 5 );
554
555 if( matching->serialize( s )) {
556 IOLog( "Waiting on %s\n", s->text() );
557 s->release();
558 }
559 }
560
561 do {
562 t.tv_sec = ROOTDEVICETIMEOUT;
563 t.tv_nsec = 0;
564 matching->retain();
565 service = IOService::waitForService( matching, &t );
566 if( (!service) || (mountAttempts == 10)) {
567 PE_display_icon( 0, "noroot");
568 IOLog( "Still waiting for root device\n" );
569
570 if( !debugInfoPrintedOnce) {
571 debugInfoPrintedOnce = true;
572 if( gIOKitDebug & kIOLogDTree) {
573 IOLog("\nDT plane:\n");
574 IOPrintPlane( gIODTPlane );
575 }
576 if( gIOKitDebug & kIOLogServiceTree) {
577 IOLog("\nService plane:\n");
578 IOPrintPlane( gIOServicePlane );
579 }
580 if( gIOKitDebug & kIOLogMemory)
581 IOPrintMemory();
582 }
583 }
584 } while( !service);
585 matching->release();
586
587 if ( service && mediaProperty ) {
588 service = (IOService *)service->getProperty(mediaProperty);
589 }
590
591 mjr = 0;
592 mnr = 0;
593
594 // If the IOService we matched to is a subclass of IONetworkInterface,
595 // then make sure it has been registered with BSD and has a BSD name
596 // assigned.
597
598 if ( service
599 && service->metaCast( "IONetworkInterface" )
600 && !IORegisterNetworkInterface( service ) )
601 {
602 service = 0;
603 }
604
605 if( service) {
606
607 len = kMaxPathBuf;
608 service->getPath( str, &len, gIOServicePlane );
609 IOLog( "Got boot device = %s\n", str );
610
611 iostr = (OSString *) service->getProperty( kIOBSDNameKey );
612 if( iostr)
613 strlcpy( rootName, iostr->getCStringNoCopy(), rootNameSize );
614 off = (OSNumber *) service->getProperty( kIOBSDMajorKey );
615 if( off)
616 mjr = off->unsigned32BitValue();
617 off = (OSNumber *) service->getProperty( kIOBSDMinorKey );
618 if( off)
619 mnr = off->unsigned32BitValue();
620
621 if( service->metaCast( "IONetworkInterface" ))
622 flags |= 1;
623
624 } else {
625
626 IOLog( "Wait for root failed\n" );
627 strlcpy( rootName, "en0", rootNameSize );
628 flags |= 1;
629 }
630
631 IOLog( "BSD root: %s", rootName );
632 if( mjr)
633 IOLog(", major %d, minor %d\n", mjr, mnr );
634 else
635 IOLog("\n");
636
637 *root = makedev( mjr, mnr );
638 *oflags = flags;
639
640 IOFree( str, kMaxPathBuf + kMaxBootVar );
641
642 iofrootx:
643 if( (gIOKitDebug & (kIOLogDTree | kIOLogServiceTree | kIOLogMemory)) && !debugInfoPrintedOnce) {
644
645 IOService::getPlatform()->waitQuiet();
646 if( gIOKitDebug & kIOLogDTree) {
647 IOLog("\nDT plane:\n");
648 IOPrintPlane( gIODTPlane );
649 }
650 if( gIOKitDebug & kIOLogServiceTree) {
651 IOLog("\nService plane:\n");
652 IOPrintPlane( gIOServicePlane );
653 }
654 if( gIOKitDebug & kIOLogMemory)
655 IOPrintMemory();
656 }
657
658 return( kIOReturnSuccess );
659 }
660
661 bool IORamDiskBSDRoot(void)
662 {
663 char rdBootVar[kMaxBootVar];
664 if (PE_parse_boot_argn("rd", rdBootVar, kMaxBootVar )
665 || PE_parse_boot_argn("rootdev", rdBootVar, kMaxBootVar )) {
666 if((rdBootVar[0] == 'm') && (rdBootVar[1] == 'd') && (rdBootVar[3] == 0)) {
667 return true;
668 }
669 }
670 return false;
671 }
672
673 void IOSecureBSDRoot(const char * rootName)
674 {
675 #if CONFIG_EMBEDDED
676 int tmpInt;
677 IOReturn result;
678 IOPlatformExpert *pe;
679 OSDictionary *matching;
680 const OSSymbol *functionName = OSSymbol::withCStringNoCopy("SecureRootName");
681
682 matching = IOService::serviceMatching("IOPlatformExpert");
683 assert(matching);
684 pe = (IOPlatformExpert *) IOService::waitForMatchingService(matching, 30ULL * kSecondScale);
685 matching->release();
686 assert(pe);
687 // Returns kIOReturnNotPrivileged is the root device is not secure.
688 // Returns kIOReturnUnsupported if "SecureRootName" is not implemented.
689 result = pe->callPlatformFunction(functionName, false, (void *)rootName, (void *)0, (void *)0, (void *)0);
690 functionName->release();
691 OSSafeReleaseNULL(pe);
692
693 if (result == kIOReturnNotPrivileged) {
694 mdevremoveall();
695 } else if (result == kIOReturnSuccess) {
696 // If we are booting with a secure root, and we have the right
697 // boot-arg, we will want to panic on exception triage. This
698 // behavior is intended as a debug aid (we can look at why an
699 // exception occured in the kernel debugger).
700 if (PE_parse_boot_argn("-panic_on_exception_triage", &tmpInt, sizeof(tmpInt))) {
701 panic_on_exception_triage = 1;
702 }
703 }
704
705 #endif // CONFIG_EMBEDDED
706 }
707
708 void *
709 IOBSDRegistryEntryForDeviceTree(char * path)
710 {
711 return (IORegistryEntry::fromPath(path, gIODTPlane));
712 }
713
714 void
715 IOBSDRegistryEntryRelease(void * entry)
716 {
717 IORegistryEntry * regEntry = (IORegistryEntry *)entry;
718
719 if (regEntry)
720 regEntry->release();
721 return;
722 }
723
724 const void *
725 IOBSDRegistryEntryGetData(void * entry, char * property_name,
726 int * packet_length)
727 {
728 OSData * data;
729 IORegistryEntry * regEntry = (IORegistryEntry *)entry;
730
731 data = (OSData *) regEntry->getProperty(property_name);
732 if (data) {
733 *packet_length = data->getLength();
734 return (data->getBytesNoCopy());
735 }
736 return (NULL);
737 }
738
739 kern_return_t IOBSDGetPlatformUUID( uuid_t uuid, mach_timespec_t timeout )
740 {
741 IOService * resources;
742 OSString * string;
743
744 resources = IOService::waitForService( IOService::resourceMatching( kIOPlatformUUIDKey ), ( timeout.tv_sec || timeout.tv_nsec ) ? &timeout : 0 );
745 if ( resources == 0 ) return KERN_OPERATION_TIMED_OUT;
746
747 string = ( OSString * ) IOService::getPlatform( )->getProvider( )->getProperty( kIOPlatformUUIDKey );
748 if ( string == 0 ) return KERN_NOT_SUPPORTED;
749
750 uuid_parse( string->getCStringNoCopy( ), uuid );
751
752 return KERN_SUCCESS;
753 }
754
755 } /* extern "C" */
756
757 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
758
759 #include <sys/conf.h>
760 #include <sys/vnode.h>
761 #include <sys/vnode_internal.h>
762 #include <sys/fcntl.h>
763 #include <IOKit/IOPolledInterface.h>
764 #include <IOKit/IOBufferMemoryDescriptor.h>
765
766 IOPolledFileIOVars * gIOPolledCoreFileVars;
767
768 #if IOPOLLED_COREFILE
769
770 static IOReturn
771 IOOpenPolledCoreFile(const char * filename)
772 {
773 IOReturn err;
774 unsigned int debug;
775
776 if (gIOPolledCoreFileVars) return (kIOReturnBusy);
777 if (!IOPolledInterface::gMetaClass.getInstanceCount()) return (kIOReturnUnsupported);
778
779 debug = 0;
780 PE_parse_boot_argn("debug", &debug, sizeof (debug));
781 if (DB_DISABLE_LOCAL_CORE & debug) return (kIOReturnUnsupported);
782
783 err = IOPolledFileOpen(filename, kIOCoreDumpSize, kIOCoreDumpFreeSize,
784 NULL, 0,
785 &gIOPolledCoreFileVars, NULL, NULL, 0);
786 if (kIOReturnSuccess != err) return (err);
787
788 err = IOPolledFilePollersSetup(gIOPolledCoreFileVars, kIOPolledPreflightCoreDumpState);
789 if (kIOReturnSuccess != err)
790 {
791 IOPolledFileClose(&gIOPolledCoreFileVars, NULL, NULL, 0, 0, 0);
792 }
793
794 return (err);
795 }
796
797 static void
798 IOClosePolledCoreFile(void)
799 {
800 IOPolledFilePollersClose(gIOPolledCoreFileVars, kIOPolledPostflightCoreDumpState);
801 IOPolledFileClose(&gIOPolledCoreFileVars, NULL, NULL, 0, 0, 0);
802 }
803
804 static thread_call_t gIOOpenPolledCoreFileTC;
805 static IONotifier * gIOPolledCoreFileNotifier;
806 static IONotifier * gIOPolledCoreFileInterestNotifier;
807
808 static IOReturn
809 KernelCoreMediaInterest(void * target, void * refCon,
810 UInt32 messageType, IOService * provider,
811 void * messageArgument, vm_size_t argSize )
812 {
813 if (kIOMessageServiceIsTerminated == messageType)
814 {
815 gIOPolledCoreFileInterestNotifier->remove();
816 gIOPolledCoreFileInterestNotifier = 0;
817 IOClosePolledCoreFile();
818 }
819
820 return (kIOReturnSuccess);
821 }
822
823 static void
824 OpenKernelCoreMedia(thread_call_param_t p0, thread_call_param_t p1)
825 {
826 IOService * newService;
827 OSString * string;
828 char filename[16];
829
830 newService = (IOService *) p1;
831 do
832 {
833 if (gIOPolledCoreFileVars) break;
834 string = OSDynamicCast(OSString, newService->getProperty(kIOBSDNameKey));
835 if (!string) break;
836 snprintf(filename, sizeof(filename), "/dev/%s", string->getCStringNoCopy());
837 if (kIOReturnSuccess != IOOpenPolledCoreFile(filename)) break;
838 gIOPolledCoreFileInterestNotifier = newService->registerInterest(
839 gIOGeneralInterest, &KernelCoreMediaInterest, NULL, 0);
840 }
841 while (false);
842
843 newService->release();
844 }
845
846 static bool
847 NewKernelCoreMedia(void * target, void * refCon,
848 IOService * newService,
849 IONotifier * notifier)
850 {
851 static volatile UInt32 onlyOneCorePartition = 0;
852 do
853 {
854 if (!OSCompareAndSwap(0, 1, &onlyOneCorePartition)) break;
855 if (gIOPolledCoreFileVars) break;
856 if (!gIOOpenPolledCoreFileTC) break;
857 newService = newService->getProvider();
858 if (!newService) break;
859 newService->retain();
860 thread_call_enter1(gIOOpenPolledCoreFileTC, newService);
861 }
862 while (false);
863
864 return (false);
865 }
866
867 #endif /* IOPOLLED_COREFILE */
868
869 extern "C" void
870 IOBSDMountChange(struct mount * mp, uint32_t op)
871 {
872 #if IOPOLLED_COREFILE
873
874 OSDictionary * bsdMatching;
875 OSDictionary * mediaMatching;
876 OSString * string;
877
878 if (!gIOPolledCoreFileNotifier) do
879 {
880 if (!gIOOpenPolledCoreFileTC) gIOOpenPolledCoreFileTC = thread_call_allocate(&OpenKernelCoreMedia, NULL);
881 bsdMatching = IOService::serviceMatching("IOMediaBSDClient");
882 if (!bsdMatching) break;
883 mediaMatching = IOService::serviceMatching("IOMedia");
884 string = OSString::withCStringNoCopy("5361644D-6163-11AA-AA11-00306543ECAC");
885 if (!string || !mediaMatching) break;
886 mediaMatching->setObject("Content", string);
887 string->release();
888 bsdMatching->setObject(gIOParentMatchKey, mediaMatching);
889 mediaMatching->release();
890
891 gIOPolledCoreFileNotifier = IOService::addMatchingNotification(
892 gIOFirstMatchNotification, bsdMatching,
893 &NewKernelCoreMedia, NULL, NULL, -1000);
894 }
895 while (false);
896
897 #if CONFIG_EMBEDDED
898 uint64_t flags;
899 char path[128];
900 int pathLen;
901 vnode_t vn;
902 int result;
903
904 switch (op)
905 {
906 case kIOMountChangeMount:
907 case kIOMountChangeDidResize:
908
909 if (gIOPolledCoreFileVars) break;
910 flags = vfs_flags(mp);
911 if (MNT_RDONLY & flags) break;
912 if (!(MNT_LOCAL & flags)) break;
913
914 vn = vfs_vnodecovered(mp);
915 if (!vn) break;
916 pathLen = sizeof(path);
917 result = vn_getpath(vn, &path[0], &pathLen);
918 vnode_put(vn);
919 if (0 != result) break;
920 if (!pathLen) break;
921 #if defined(XNU_TARGET_OS_BRIDGE)
922 // on bridgeOS systems we put the core in /private/var/internal. We don't
923 // want to match with /private/var because /private/var/internal is often mounted
924 // over /private/var
925 if ((pathLen - 1) < (int) strlen("/private/var/internal")) break;
926 #endif
927 if (0 != strncmp(path, kIOCoreDumpPath, pathLen - 1)) break;
928 IOOpenPolledCoreFile(kIOCoreDumpPath);
929 break;
930
931 case kIOMountChangeUnmount:
932 case kIOMountChangeWillResize:
933 if (gIOPolledCoreFileVars && (mp == kern_file_mount(gIOPolledCoreFileVars->fileRef)))
934 {
935 IOClosePolledCoreFile();
936 }
937 break;
938 }
939 #endif /* CONFIG_EMBEDDED */
940 #endif /* IOPOLLED_COREFILE */
941 }
942
943
944 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
945
946 extern "C" boolean_t
947 IOTaskHasEntitlement(task_t task, const char * entitlement)
948 {
949 OSObject * obj;
950 obj = IOUserClient::copyClientEntitlement(task, entitlement);
951 if (!obj) return (false);
952 obj->release();
953 return (obj != kOSBooleanFalse);
954 }
955