Loading...
iokit/bsddev/IOKitBSDInit.cpp xnu-3248.50.21 xnu-4570.20.62
--- xnu/xnu-3248.50.21/iokit/bsddev/IOKitBSDInit.cpp
+++ xnu/xnu-4570.20.62/iokit/bsddev/IOKitBSDInit.cpp
@@ -54,10 +54,17 @@
 extern dev_t mdevadd(int devid, uint64_t base, unsigned int size, int phys);
 extern dev_t mdevlookup(int devid);
 extern void mdevremoveall(void);
+extern int mdevgetrange(int devid, uint64_t *base, uint64_t *size);
 extern void di_root_ramfile(IORegistryEntry * entry);
 
 
-#if   DEVELOPMENT
+#if CONFIG_EMBEDDED
+#define IOPOLLED_COREFILE 	(CONFIG_KDP_INTERACTIVE_DEBUGGING)
+#define kIOCoreDumpPath 	"/private/var/vm/kernelcore"
+#define kIOCoreDumpSize		350ULL*1024ULL*1024ULL
+// leave free space on volume:
+#define kIOCoreDumpFreeSize	350ULL*1024ULL*1024ULL
+#elif DEVELOPMENT
 #define IOPOLLED_COREFILE  	1
 // no sizing
 #define kIOCoreDumpSize		0ULL
@@ -74,6 +81,13 @@
 		   IONotifier * notifier);
 #endif /* IOPOLLED_COREFILE */
 
+#if CONFIG_KDP_INTERACTIVE_DEBUGGING
+/*
+ * Touched by IOFindBSDRoot() if a RAMDisk is used for the root device.
+ */
+extern uint64_t kdp_core_ramdisk_addr;
+extern uint64_t kdp_core_ramdisk_size;
+#endif
 
 kern_return_t
 IOKitBSDInit( void )
@@ -356,10 +370,25 @@
     static int		mountAttempts = 0;
 				
     int xchar, dchar;
-                                    
+
+    // stall here for anyone matching on the IOBSD resource to finish (filesystems)
+    matching = IOService::serviceMatching(gIOResourcesKey);
+    assert(matching);
+    matching->setObject(gIOResourceMatchedKey, gIOBSDKey);
+
+	if ((service = IOService::waitForMatchingService(matching, 30ULL * kSecondScale))) {
+		service->release();
+	} else {
+		IOLog("!BSD\n");
+	}
+    matching->release();
+	matching = NULL;
 
     if( mountAttempts++)
+    {
+        IOLog("mount(%d) failed\n", mountAttempts);
 	IOSleep( 5 * 1000 );
+    }
 
     str = (char *) IOMalloc( kMaxPathBuf + kMaxBootVar );
     if( !str)
@@ -439,13 +468,22 @@
 		if(xchar >= 0) {										/* Do we have a valid memory device name? */
 			*root = mdevlookup(xchar);							/* Find the device number */
 			if(*root >= 0) {									/* Did we find one? */
-
 				rootName[0] = 'm';								/* Build root name */
 				rootName[1] = 'd';								/* Build root name */
 				rootName[2] = dchar;							/* Build root name */
 				rootName[3] = 0;								/* Build root name */
 				IOLog("BSD root: %s, major %d, minor %d\n", rootName, major(*root), minor(*root));
 				*oflags = 0;									/* Show that this is not network */
+
+#if CONFIG_KDP_INTERACTIVE_DEBUGGING
+                /* retrieve final ramdisk range and initialize KDP variables */
+                if (mdevgetrange(xchar, &kdp_core_ramdisk_addr, &kdp_core_ramdisk_size) != 0) {
+                    IOLog("Unable to retrieve range for root memory device %d\n", xchar);
+                    kdp_core_ramdisk_addr = 0;
+                    kdp_core_ramdisk_size = 0;
+                }
+#endif
+
 				goto iofrootx;									/* Join common exit... */
 			}
 			panic("IOFindBSDRoot: specified root memory device, %s, has not been configured\n", rdBootVar);	/* Not there */
@@ -625,6 +663,37 @@
 
 void IOSecureBSDRoot(const char * rootName)
 {
+#if CONFIG_EMBEDDED
+    int              tmpInt;
+    IOReturn         result;
+    IOPlatformExpert *pe;
+    OSDictionary     *matching;
+    const OSSymbol   *functionName = OSSymbol::withCStringNoCopy("SecureRootName");
+    
+    matching = IOService::serviceMatching("IOPlatformExpert");
+    assert(matching);
+    pe = (IOPlatformExpert *) IOService::waitForMatchingService(matching, 30ULL * kSecondScale);
+    matching->release();
+    assert(pe);
+    // Returns kIOReturnNotPrivileged is the root device is not secure.
+    // Returns kIOReturnUnsupported if "SecureRootName" is not implemented.
+    result = pe->callPlatformFunction(functionName, false, (void *)rootName, (void *)0, (void *)0, (void *)0);
+    functionName->release();
+    OSSafeReleaseNULL(pe);
+    
+    if (result == kIOReturnNotPrivileged) {
+        mdevremoveall();
+    } else if (result == kIOReturnSuccess) {
+        // If we are booting with a secure root, and we have the right
+	// boot-arg, we will want to panic on exception triage.  This
+	// behavior is intended as a debug aid (we can look at why an
+	// exception occured in the kernel debugger).
+        if (PE_parse_boot_argn("-panic_on_exception_triage", &tmpInt, sizeof(tmpInt))) {
+            panic_on_exception_triage = 1;
+        }
+    }
+
+#endif  // CONFIG_EMBEDDED
 }
 
 void *
@@ -672,117 +741,6 @@
     uuid_parse( string->getCStringNoCopy( ), uuid );
 
     return KERN_SUCCESS;
-}
-
-kern_return_t IOBSDGetPlatformSerialNumber( char *serial_number_str, u_int32_t len )
-{
-    OSDictionary * platform_dict;
-    IOService *platform;
-    OSString *  string;
-
-    if (len < 1) {
-	    return 0;
-    }
-    serial_number_str[0] = '\0';
-
-    platform_dict = IOService::serviceMatching( "IOPlatformExpertDevice" );
-    if (platform_dict == NULL) {
-	    return KERN_NOT_SUPPORTED;
-    }
-
-    platform = IOService::waitForService( platform_dict );
-    if (platform) {
-	    string = ( OSString * ) platform->getProperty( kIOPlatformSerialNumberKey );
-	    if ( string == 0 ) {
-		    return KERN_NOT_SUPPORTED;
-	    } else {
-		    strlcpy( serial_number_str, string->getCStringNoCopy( ), len );
-	    }
-    }
-    
-    return KERN_SUCCESS;
-}
-
-void IOBSDIterateMediaWithContent(const char *content_uuid_cstring, int (*func)(const char *bsd_dev_name, const char *uuid_str, void *arg), void *arg)
-{
-    OSDictionary *dictionary;
-    OSString *content_uuid_string;
-
-    dictionary = IOService::serviceMatching( "IOMedia" );
-    if( dictionary ) {
-	content_uuid_string = OSString::withCString( content_uuid_cstring );
-	if( content_uuid_string ) {
-	    IOService *service;
-	    OSIterator *iter;
-
-	    dictionary->setObject( "Content", content_uuid_string );
-	    dictionary->retain();
-
-	    iter = IOService::getMatchingServices(dictionary);
-	    while (iter && (service = (IOService *)iter->getNextObject())) {
-		    if( service ) {
-			    OSString *iostr = (OSString *) service->getProperty( kIOBSDNameKey );
-			    OSString *uuidstr = (OSString *) service->getProperty( "UUID" );
-			    const char *uuid;
-
-			    if( iostr) {
-				    if (uuidstr) {
-					    uuid = uuidstr->getCStringNoCopy();
-				    } else {
-					    uuid = "00000000-0000-0000-0000-000000000000";
-				    }
-
-				    // call the callback
-				    if (func && func(iostr->getCStringNoCopy(), uuid, arg) == 0) {
-					    break;
-				    }
-			    }
-		    }
-	    }
-	    if (iter)
-		    iter->release();
-	    
-	    content_uuid_string->release();
-	}
-	dictionary->release();
-    }
-}
-
-
-int IOBSDIsMediaEjectable( const char *cdev_name )
-{
-    int ret = 0;
-    OSDictionary *dictionary;
-    OSString *dev_name;
-
-    if (strncmp(cdev_name, "/dev/", 5) == 0) {
-	    cdev_name += 5;
-    }
-
-    dictionary = IOService::serviceMatching( "IOMedia" );
-    if( dictionary ) {
-	dev_name = OSString::withCString( cdev_name );
-	if( dev_name ) {
-	    IOService *service;
-	    mach_timespec_t tv = { 5, 0 };    // wait up to "timeout" seconds for the device
-
-	    dictionary->setObject( kIOBSDNameKey, dev_name );
-	    dictionary->retain();
-	    service = IOService::waitForService( dictionary, &tv );
-	    if( service ) {
-		OSBoolean *ejectable = (OSBoolean *) service->getProperty( "Ejectable" );
-
-		if( ejectable ) {
-			ret = (int)ejectable->getValue();
-		}
-
-	    }
-	    dev_name->release();
-	}
-	dictionary->release();
-    }
-
-    return ret;
 }
 
 } /* extern "C" */
@@ -881,8 +839,10 @@
 		   IOService * newService,
 		   IONotifier * notifier)
 {
+    static volatile UInt32 onlyOneCorePartition = 0;
     do
     {
+	if (!OSCompareAndSwap(0, 1, &onlyOneCorePartition)) break;
 	if (gIOPolledCoreFileVars)    break;
         if (!gIOOpenPolledCoreFileTC) break;
         newService = newService->getProvider();
@@ -925,6 +885,43 @@
     }
     while (false);
 
+#if CONFIG_EMBEDDED
+    uint64_t flags;
+    char path[128];
+    int pathLen;
+    vnode_t vn;
+    int result;
+
+    switch (op)
+    {
+	case kIOMountChangeMount:
+	case kIOMountChangeDidResize:
+
+	    if (gIOPolledCoreFileVars) break;
+	    flags = vfs_flags(mp);
+	    if (MNT_RDONLY & flags) break;
+	    if (!(MNT_LOCAL & flags)) break;
+
+	    vn = vfs_vnodecovered(mp);
+	    if (!vn) break;
+	    pathLen = sizeof(path);
+	    result = vn_getpath(vn, &path[0], &pathLen);
+	    vnode_put(vn);
+	    if (0 != result) break;
+	    if (!pathLen) break;
+	    if (0 != bcmp(path, kIOCoreDumpPath, pathLen - 1)) break;
+	    IOOpenPolledCoreFile(kIOCoreDumpPath);
+	    break;
+
+	case kIOMountChangeUnmount:
+	case kIOMountChangeWillResize:
+	    if (gIOPolledCoreFileVars && (mp == kern_file_mount(gIOPolledCoreFileVars->fileRef)))
+	    {
+		IOClosePolledCoreFile();
+	    }
+	    break;
+    }
+#endif /* CONFIG_EMBEDDED */
 #endif /* IOPOLLED_COREFILE */
 }