]> git.saurik.com Git - apple/xnu.git/blobdiff - iokit/Kernel/IOPlatformExpert.cpp
xnu-1456.1.26.tar.gz
[apple/xnu.git] / iokit / Kernel / IOPlatformExpert.cpp
index 6d85f274877dceb16b3f16d983a5815d400c2f95..8109e190e3472f3d07e43c3434bb096528f31a7e 100644 (file)
@@ -1,23 +1,29 @@
 /*
- * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 1998-2008 Apple Inc. All rights reserved.
  *
- * @APPLE_LICENSE_HEADER_START@
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  * 
- * The contents of this file constitute Original Code as defined in and
- * are subject to the Apple Public Source License Version 1.1 (the
- * "License").  You may not use this file except in compliance with the
- * License.  Please obtain a copy of the License at
- * http://www.apple.com/publicsource and read it before using this file.
+ * This file contains Original Code and/or Modifications of Original Code
+ * as defined in and that are subject to the Apple Public Source License
+ * Version 2.0 (the 'License'). You may not use this file except in
+ * compliance with the License. The rights granted to you under the License
+ * may not be used to create, or enable the creation or redistribution of,
+ * unlawful or unlicensed copies of an Apple operating system, or to
+ * circumvent, violate, or enable the circumvention or violation of, any
+ * terms of an Apple operating system software license agreement.
  * 
- * This Original Code and all software distributed under the License are
- * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * Please obtain a copy of the License at
+ * http://www.opensource.apple.com/apsl/ and read it before using this file.
+ * 
+ * The Original Code and all software distributed under the License are
+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
- * License for the specific language governing rights and limitations
- * under the License.
+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
+ * Please see the License for the specific language governing rights and
+ * limitations under the License.
  * 
- * @APPLE_LICENSE_HEADER_END@
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
  */
 /*
  * HISTORY
 #include <IOKit/pwr_mgt/RootDomain.h>
 #include <IOKit/IOKitKeys.h>
 #include <IOKit/IOTimeStamp.h>
+#include <IOKit/IOUserClient.h>
 
 #include <IOKit/system.h>
 
 #include <libkern/c++/OSContainers.h>
+#include <libkern/crypto/sha1.h>
 
 extern "C" {
 #include <machine/machine_routines.h>
 #include <pexpert/pexpert.h>
+#include <uuid/uuid.h>
 }
 
-/* Delay period for UPS halt */
-#define kUPSDelayHaltCPU_msec   (1000*60*5)
-
 void printDictionaryKeys (OSDictionary * inDictionary, char * inMsg);
-static void getCStringForObject (OSObject * inObj, char * outStr);
+static void getCStringForObject(OSObject *inObj, char *outStr, size_t outStrLen);
 
 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
@@ -98,7 +104,7 @@ bool IOPlatformExpert::start( IOService * provider )
       return false;
     
     // Override the mapper present flag is requested by boot arguments.
-    if (PE_parse_boot_arg("dart", &debugFlags) && (debugFlags == 0))
+    if (PE_parse_boot_argn("dart", &debugFlags, sizeof (debugFlags)) && (debugFlags == 0))
       removeProperty(kIOPlatformMapperPresentKey);
     
     // Register the presence or lack thereof a system 
@@ -243,17 +249,18 @@ int (*PE_halt_restart)(unsigned int type) = 0;
 
 int IOPlatformExpert::haltRestart(unsigned int type)
 {
-  if (type == kPEHangCPU) while (1);
-
-  if (type == kPEUPSDelayHaltCPU) {
-    // Stall shutdown for 5 minutes, and if no outside force has 
-    // removed our power at that point, proceed with a reboot.
-    IOSleep( kUPSDelayHaltCPU_msec );
+  if (type == kPEPanicSync) return 0;
 
-    // Ideally we never reach this point.
+  if (type == kPEHangCPU) while (true) {}
 
-    type = kPERestartCPU;
+  if (type == kPEUPSDelayHaltCPU) {
+    // RestartOnPowerLoss feature was turned on, proceed with shutdown.
+    type = kPEHaltCPU;
   }
+
+  // On ARM kPEPanicRestartCPU is supported in the drivers
+  if (type == kPEPanicRestartCPU)
+         type = kPERestartCPU;
   
   if (PE_halt_restart) return (*PE_halt_restart)(type);
   else return -1;
@@ -369,13 +376,14 @@ PMLog(const char *who, unsigned long event,
 
     if (debugFlags & kIOLogPower) {
 
-       uint32_t nows, nowus;
+       clock_sec_t nows;
+       clock_usec_t nowus;
        clock_get_system_microtime(&nows, &nowus);
        nowus += (nows % 1000) * 1000000;
 
-        kprintf("pm%u %x %.30s %d %x %x\n",
-               nowus, (unsigned) current_thread(), who,        // Identity
-               (int) event, param1, param2);                   // Args
+        kprintf("pm%u %p %.30s %d %lx %lx\n",
+               nowus, current_thread(), who,   // Identity
+               (int) event, (long) param1, (long) param2);                     // Args
 
        if (debugFlags & kIOLogTracePower) {
            static const UInt32 sStartStopBitField[] = 
@@ -397,7 +405,7 @@ PMLog(const char *who, unsigned long event,
            }
 
            // Record the timestamp, wish I had a this pointer
-           IOTimeStampConstant(code, (UInt32) who, event, param1, param2);
+           IOTimeStampConstant(code, (uintptr_t) who, event, param1, param2);
        }
     }
 }
@@ -418,7 +426,6 @@ void IOPlatformExpert::PMInstantiatePowerDomains ( void )
     root->init();
     root->attach(this);
     root->start(this);
-    root->youAreRoot();
 }
 
 
@@ -687,7 +694,8 @@ void printDictionaryKeys (OSDictionary * inDictionary, char * inMsg)
     if ( mkey->isEqualTo ("name") ) {
       char nameStr[64];
       nameStr[0] = 0;
-      getCStringForObject (inDictionary->getObject ("name"), nameStr );
+      getCStringForObject(inDictionary->getObject("name"), nameStr,
+                     sizeof(nameStr));
       if (strlen(nameStr) > 0)
         IOLog ("%s name is %s\n", inMsg, nameStr);
     }
@@ -700,7 +708,8 @@ void printDictionaryKeys (OSDictionary * inDictionary, char * inMsg)
   mcoll->release ();
 }
 
-static void getCStringForObject (OSObject * inObj, char * outStr)
+static void
+getCStringForObject(OSObject *inObj, char *outStr, size_t outStrLen)
 {
    char * buffer;
    unsigned int    len, i;
@@ -710,10 +719,11 @@ static void getCStringForObject (OSObject * inObj, char * outStr)
 
    char * objString = (char *) (inObj->getMetaClass())->getClassName();
 
-   if ((0 == strcmp(objString,"OSString")) || (0 == strcmp (objString, "OSSymbol")))
-     strcpy (outStr, ((OSString *)inObj)->getCStringNoCopy());
+   if ((0 == strncmp(objString, "OSString", sizeof("OSString"))) ||
+                  (0 == strncmp(objString, "OSSymbol", sizeof("OSSymbol"))))
+     strlcpy(outStr, ((OSString *)inObj)->getCStringNoCopy(), outStrLen);
 
-   else if (0 == strcmp(objString,"OSData")) {
+   else if (0 == strncmp(objString, "OSData", sizeof("OSData"))) {
      len = ((OSData *)inObj)->getLength();
      buffer = (char *)((OSData *)inObj)->getBytesNoCopy();
      if (buffer && (len > 0)) {
@@ -732,7 +742,7 @@ static void IOShutdownNotificationsTimedOut(
     thread_call_param_t p0, 
     thread_call_param_t p1)
 {
-    int type = (int)p0;
+    int type = (int)(long)p0;
 
     /* 30 seconds has elapsed - resume shutdown */
     if(gIOPlatform) gIOPlatform->haltRestart(type);
@@ -772,10 +782,8 @@ int PEGetPlatformEpoch(void)
 int PEHaltRestart(unsigned int type)
 {
   IOPMrootDomain    *pmRootDomain = IOService::getPMRootDomain();
-  bool              noWaitForResponses;
   AbsoluteTime      deadline;
   thread_call_t     shutdown_hang;
-  unsigned int      tell_type;
   
   if(type == kPEHaltCPU || type == kPERestartCPU || type == kPEUPSDelayHaltCPU)
   {
@@ -792,23 +800,15 @@ int PEHaltRestart(unsigned int type)
                         (thread_call_param_t) type);
     clock_interval_to_deadline( 30, kSecondScale, &deadline );
     thread_call_enter1_delayed( shutdown_hang, 0, deadline );
-    
-
-    if( kPEUPSDelayHaltCPU == type ) {
-        tell_type = kPEHaltCPU;
-    } else {
-        tell_type = type;
-    }
 
-    noWaitForResponses = pmRootDomain->tellChangeDown2(tell_type); 
+    pmRootDomain->handlePlatformHaltRestart(type); 
     /* This notification should have few clients who all do 
        their work synchronously.
              
        In this "shutdown notification" context we don't give
        drivers the option of working asynchronously and responding 
        later. PM internals make it very hard to wait for asynchronous
-       replies. In fact, it's a bad idea to even be calling
-       tellChangeDown2 from here at all.
+       replies.
      */
    }
 
@@ -826,22 +826,74 @@ long PEGetGMTTimeOfDay(void)
 {
        long    result = 0;
 
-    if( gIOPlatform)
-               result = gIOPlatform->getGMTTimeOfDay();
+       if( gIOPlatform)                result = gIOPlatform->getGMTTimeOfDay();
 
        return (result);
 }
 
 void PESetGMTTimeOfDay(long secs)
 {
-    if( gIOPlatform)
-               gIOPlatform->setGMTTimeOfDay(secs);
+    if( gIOPlatform)           gIOPlatform->setGMTTimeOfDay(secs);
 }
 
 } /* extern "C" */
 
 void IOPlatformExpert::registerNVRAMController(IONVRAMController * caller)
 {
+    OSData *          data;
+    IORegistryEntry * entry;
+    OSString *        string = 0;
+    uuid_string_t     uuid;
+
+    entry = IORegistryEntry::fromPath( "/efi/platform", gIODTPlane );
+    if ( entry )
+    {
+        data = OSDynamicCast( OSData, entry->getProperty( "system-id" ) );
+        if ( data && data->getLength( ) == 16 )
+        {
+            SHA1_CTX     context;
+            uint8_t      digest[ SHA_DIGEST_LENGTH ];
+            const uuid_t space = { 0x2A, 0x06, 0x19, 0x90, 0xD3, 0x8D, 0x44, 0x40, 0xA1, 0x39, 0xC4, 0x97, 0x70, 0x37, 0x65, 0xAC };
+
+            SHA1Init( &context );
+            SHA1Update( &context, space, sizeof( space ) );
+            SHA1Update( &context, data->getBytesNoCopy( ), data->getLength( ) );
+            SHA1Final( digest, &context );
+
+            digest[ 6 ] = ( digest[ 6 ] & 0x0F ) | 0x50;
+            digest[ 8 ] = ( digest[ 8 ] & 0x3F ) | 0x80;
+
+            uuid_unparse( digest, uuid );
+            string = OSString::withCString( uuid );
+        }
+
+        entry->release( );
+    }
+
+    if ( string == 0 )
+    {
+        entry = IORegistryEntry::fromPath( "/options", gIODTPlane );
+        if ( entry )
+        {
+            data = OSDynamicCast( OSData, entry->getProperty( "platform-uuid" ) );
+            if ( data && data->getLength( ) == sizeof( uuid_t ) )
+            {
+                uuid_unparse( ( uint8_t * ) data->getBytesNoCopy( ), uuid );
+                string = OSString::withCString( uuid );
+            }
+
+            entry->release( );
+        }
+    }
+
+    if ( string )
+    {
+        getProvider( )->setProperty( kIOPlatformUUIDKey, string );
+        publishResource( kIOPlatformUUIDKey, string );
+
+        string->release( );
+    }
+
     publishResource("IONVRAM");
 }
 
@@ -855,7 +907,7 @@ IOReturn IOPlatformExpert::callPlatformFunction(const OSSymbol *functionName,
   if (waitForFunction) {
     _resources = waitForService(resourceMatching(functionName));
   } else {
-    _resources = resources();
+    _resources = getResourceService();
   }
   if (_resources == 0) return kIOReturnUnsupported;
   
@@ -1047,7 +1099,7 @@ bool IODTPlatformExpert::getMachineName( char * name, int maxLength )
     ok = (0 != prop);
 
     if( ok )
-       strncpy( name, (const char *) prop->getBytesNoCopy(), maxLength );
+       strlcpy( name, (const char *) prop->getBytesNoCopy(), maxLength );
 
     return( ok );
 }
@@ -1212,7 +1264,7 @@ IOPlatformExpertDevice::initWithArgs(
     argsData[ 2 ] = p3;
     argsData[ 3 ] = p4;
 
-    setProperty("IOPlatformArgs", (void *)argsData, sizeof( argsData));
+    setProperty("IOPlatformArgs", (void *)argsData, sizeof(argsData));
 
     return( true);
 }
@@ -1222,16 +1274,57 @@ IOWorkLoop *IOPlatformExpertDevice::getWorkLoop() const
     return workLoop;
 }
 
-void IOPlatformExpertDevice::free()
+IOReturn IOPlatformExpertDevice::setProperties( OSObject * properties )
 {
-    if (workLoop)
-        workLoop->release();
+    OSDictionary * dictionary;
+    OSObject *     object;
+    IOReturn       status;
+
+    status = super::setProperties( properties );
+    if ( status != kIOReturnUnsupported ) return status;
+
+    status = IOUserClient::clientHasPrivilege( current_task( ), kIOClientPrivilegeAdministrator );
+    if ( status != kIOReturnSuccess ) return status;
+
+    dictionary = OSDynamicCast( OSDictionary, properties );
+    if ( dictionary == 0 ) return kIOReturnBadArgument;
+
+    object = dictionary->getObject( kIOPlatformUUIDKey );
+    if ( object )
+    {
+        IORegistryEntry * entry;
+        OSString *        string;
+        uuid_t            uuid;
+
+        string = ( OSString * ) getProperty( kIOPlatformUUIDKey );
+        if ( string ) return kIOReturnNotPermitted;
+
+        string = OSDynamicCast( OSString, object );
+        if ( string == 0 ) return kIOReturnBadArgument;
+
+        status = uuid_parse( string->getCStringNoCopy( ), uuid );
+        if ( status != 0 ) return kIOReturnBadArgument;
+
+        entry = IORegistryEntry::fromPath( "/options", gIODTPlane );
+        if ( entry )
+        {
+            entry->setProperty( "platform-uuid", uuid, sizeof( uuid_t ) );
+            entry->release( );
+        }
+
+        setProperty( kIOPlatformUUIDKey, string );
+        publishResource( kIOPlatformUUIDKey, string );
+
+        return kIOReturnSuccess;
+    }
+
+    return kIOReturnUnsupported;
 }
 
-bool IOPlatformExpertDevice::attachToChild( IORegistryEntry * child,
-                                        const IORegistryPlane * plane )
+void IOPlatformExpertDevice::free()
 {
-    return IOService::attachToChild( child, plane );
+    if (workLoop)
+        workLoop->release();
 }
 
 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
@@ -1295,3 +1388,4 @@ bool IOPanicPlatform::start(IOService * provider) {
 
     return false;
 }
+