]> git.saurik.com Git - apple/xnu.git/blobdiff - iokit/Kernel/IOPlatformExpert.cpp
xnu-3789.51.2.tar.gz
[apple/xnu.git] / iokit / Kernel / IOPlatformExpert.cpp
index 269ea125ef368ae555fe878a310d72ccff79aabc..c83a71fd5400adf0c098acb0ad650af4836878dc 100644 (file)
@@ -1,16 +1,19 @@
 /*
 /*
- * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 1998-2014 Apple Inc. All rights reserved.
  *
  *
- * @APPLE_LICENSE_HEADER_START@
- * 
- * Copyright (c) 1999-2003 Apple Computer, Inc.  All Rights Reserved.
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  * 
  * 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
  * 
  * 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. Please obtain a copy of the License at
- * http://www.opensource.apple.com/apsl/ and read it before using this
- * file.
+ * 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.
+ * 
+ * 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
  * 
  * The Original Code and all software distributed under the License are
  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * Please see the License for the specific language governing rights and
  * limitations under the License.
  * 
  * Please see the License for the specific language governing rights and
  * limitations under the License.
  * 
- * @APPLE_LICENSE_HEADER_END@
- */
-/*
- * HISTORY
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
  */
  
  */
  
-#include <IOKit/system.h>
-#include <IOKit/IOPlatformExpert.h>
 #include <IOKit/IOCPU.h>
 #include <IOKit/IODeviceTreeSupport.h>
 #include <IOKit/IOCPU.h>
 #include <IOKit/IODeviceTreeSupport.h>
-#include <IOKit/IORangeAllocator.h>
-#include <IOKit/IONVRAM.h>
 #include <IOKit/IOKitDebug.h>
 #include <IOKit/IOKitDebug.h>
+#include <IOKit/IOMapper.h>
+#include <IOKit/IOMessage.h>
+#include <IOKit/IONVRAM.h>
+#include <IOKit/IOPlatformExpert.h>
+#include <IOKit/IORangeAllocator.h>
 #include <IOKit/IOWorkLoop.h>
 #include <IOKit/pwr_mgt/RootDomain.h>
 #include <IOKit/IOWorkLoop.h>
 #include <IOKit/pwr_mgt/RootDomain.h>
-#include <IOKit/IOMessage.h>
-#include <libkern/c++/OSContainers.h>
+#include <IOKit/IOKitKeys.h>
+#include <IOKit/IOTimeStamp.h>
+#include <IOKit/IOUserClient.h>
+#include <IOKit/IOKitDiagnosticsUserClient.h>
 
 
+#include <IOKit/system.h>
+
+#include <libkern/c++/OSContainers.h>
+#include <libkern/crypto/sha1.h>
+#include <libkern/OSAtomic.h>
 
 extern "C" {
 #include <machine/machine_routines.h>
 #include <pexpert/pexpert.h>
 
 extern "C" {
 #include <machine/machine_routines.h>
 #include <pexpert/pexpert.h>
+#include <uuid/uuid.h>
 }
 
 void printDictionaryKeys (OSDictionary * inDictionary, char * inMsg);
 }
 
 void printDictionaryKeys (OSDictionary * inDictionary, char * inMsg);
-static void getCStringForObject (OSObject * inObj, char * outStr);
+static void getCStringForObject(OSObject *inObj, char *outStr, size_t outStrLen);
 
 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
 
 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
@@ -54,11 +63,11 @@ static void getCStringForObject (OSObject * inObj, char * outStr);
 OSDefineMetaClassAndStructors(IOPlatformExpert, IOService)
 
 OSMetaClassDefineReservedUsed(IOPlatformExpert,  0);
 OSDefineMetaClassAndStructors(IOPlatformExpert, IOService)
 
 OSMetaClassDefineReservedUsed(IOPlatformExpert,  0);
+OSMetaClassDefineReservedUsed(IOPlatformExpert,  1);
+OSMetaClassDefineReservedUsed(IOPlatformExpert,  2);
+OSMetaClassDefineReservedUsed(IOPlatformExpert,  3);
+OSMetaClassDefineReservedUsed(IOPlatformExpert,  4);
 
 
-OSMetaClassDefineReservedUnused(IOPlatformExpert,  1);
-OSMetaClassDefineReservedUnused(IOPlatformExpert,  2);
-OSMetaClassDefineReservedUnused(IOPlatformExpert,  3);
-OSMetaClassDefineReservedUnused(IOPlatformExpert,  4);
 OSMetaClassDefineReservedUnused(IOPlatformExpert,  5);
 OSMetaClassDefineReservedUnused(IOPlatformExpert,  6);
 OSMetaClassDefineReservedUnused(IOPlatformExpert,  7);
 OSMetaClassDefineReservedUnused(IOPlatformExpert,  5);
 OSMetaClassDefineReservedUnused(IOPlatformExpert,  6);
 OSMetaClassDefineReservedUnused(IOPlatformExpert,  7);
@@ -70,6 +79,7 @@ OSMetaClassDefineReservedUnused(IOPlatformExpert, 11);
 static IOPlatformExpert * gIOPlatform;
 static OSDictionary * gIOInterruptControllers;
 static IOLock * gIOInterruptControllersLock;
 static IOPlatformExpert * gIOPlatform;
 static OSDictionary * gIOInterruptControllers;
 static IOLock * gIOInterruptControllersLock;
+static IODTNVRAM *gIOOptionsEntry;
 
 OSSymbol * gPlatformInterruptControllerName;
 
 
 OSSymbol * gPlatformInterruptControllerName;
 
@@ -88,10 +98,21 @@ bool IOPlatformExpert::start( IOService * provider )
 {
     IORangeAllocator * physicalRanges;
     OSData *           busFrequency;
 {
     IORangeAllocator * physicalRanges;
     OSData *           busFrequency;
+    uint32_t           debugFlags;
     
     if (!super::start(provider))
       return false;
     
     
     if (!super::start(provider))
       return false;
     
+    // Override the mapper present flag is requested by boot arguments.
+    if (PE_parse_boot_argn("dart", &debugFlags, sizeof (debugFlags)) && (debugFlags == 0))
+      removeProperty(kIOPlatformMapperPresentKey);
+    if (PE_parse_boot_argn("-x", &debugFlags, sizeof (debugFlags)))
+      removeProperty(kIOPlatformMapperPresentKey);
+
+    // Register the presence or lack thereof a system 
+    // PCI address mapper with the IOMapper class
+    IOMapper::setMapperRequired(0 != getProperty(kIOPlatformMapperPresentKey));
+    
     gIOInterruptControllers = OSDictionary::withCapacity(1);
     gIOInterruptControllersLock = IOLockAlloc();
     
     gIOInterruptControllers = OSDictionary::withCapacity(1);
     gIOInterruptControllersLock = IOLockAlloc();
     
@@ -112,6 +133,16 @@ bool IOPlatformExpert::start( IOService * provider )
     
     PMInstantiatePowerDomains();
     
     
     PMInstantiatePowerDomains();
     
+    // Parse the serial-number data and publish a user-readable string
+    OSData* mydata = (OSData*) (provider->getProperty("serial-number"));
+    if (mydata != NULL) {
+        OSString *serNoString = createSystemSerialNumberString(mydata);
+        if (serNoString != NULL) {
+            provider->setProperty(kIOPlatformSerialNumberKey, serNoString);
+            serNoString->release();
+        }
+    }
+    
     return( configure(provider) );
 }
 
     return( configure(provider) );
 }
 
@@ -155,7 +186,7 @@ IOService * IOPlatformExpert::createNub( OSDictionary * from )
 }
 
 bool IOPlatformExpert::compareNubName( const IOService * nub,
 }
 
 bool IOPlatformExpert::compareNubName( const IOService * nub,
-                               OSString * name, OSString ** matched = 0 ) const
+                               OSString * name, OSString ** matched ) const
 {
     return( nub->IORegistryEntry::compareName( name, matched ));
 }
 {
     return( nub->IORegistryEntry::compareName( name, matched ));
 }
@@ -205,6 +236,11 @@ bool IOPlatformExpert::getModelName( char * /*name*/, int /*maxLength*/)
     return( false );
 }
 
     return( false );
 }
 
+OSString* IOPlatformExpert::createSystemSerialNumberString(OSData* myProperty)
+{
+    return NULL;
+}
+
 IORangeAllocator * IOPlatformExpert::getPhysicalRangeAllocator(void)
 {
     return(OSDynamicCast(IORangeAllocator,
 IORangeAllocator * IOPlatformExpert::getPhysicalRangeAllocator(void)
 {
     return(OSDynamicCast(IORangeAllocator,
@@ -215,8 +251,19 @@ int (*PE_halt_restart)(unsigned int type) = 0;
 
 int IOPlatformExpert::haltRestart(unsigned int type)
 {
 
 int IOPlatformExpert::haltRestart(unsigned int type)
 {
-  if (type == kPEHangCPU) while (1);
-  
+  if (type == kPEPanicSync) return 0;
+
+  if (type == kPEHangCPU) while (true) {}
+
+  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;
 }
   if (PE_halt_restart) return (*PE_halt_restart)(type);
   else return -1;
 }
@@ -278,6 +325,17 @@ IOReturn IOPlatformExpert::registerInterruptController(OSSymbol *name, IOInterru
   return kIOReturnSuccess;
 }
 
   return kIOReturnSuccess;
 }
 
+IOReturn IOPlatformExpert::deregisterInterruptController(OSSymbol *name)
+{
+  IOLockLock(gIOInterruptControllersLock);
+  
+  gIOInterruptControllers->removeObject(name);
+  
+  IOLockUnlock(gIOInterruptControllersLock);
+  
+  return kIOReturnSuccess;
+}
+
 IOInterruptController *IOPlatformExpert::lookUpInterruptController(OSSymbol *name)
 {
   OSObject              *object;
 IOInterruptController *IOPlatformExpert::lookUpInterruptController(OSSymbol *name)
 {
   OSObject              *object;
@@ -317,18 +375,35 @@ bool IOPlatformExpert::platformAdjustService(IOService */*service*/)
   return true;
 }
 
   return true;
 }
 
+void IOPlatformExpert::getUTCTimeOfDay(clock_sec_t * secs, clock_nsec_t * nsecs)
+{
+  *secs = getGMTTimeOfDay();
+  *nsecs = 0;
+}
+
+void IOPlatformExpert::setUTCTimeOfDay(clock_sec_t secs, __unused clock_nsec_t nsecs)
+{
+  setGMTTimeOfDay(secs);
+}
+
 
 //*********************************************************************************
 // PMLog
 //
 //*********************************************************************************
 
 
 //*********************************************************************************
 // PMLog
 //
 //*********************************************************************************
 
-void IOPlatformExpert::PMLog(const char * who,unsigned long event,unsigned long param1, unsigned long param2)
+void IOPlatformExpert::
+PMLog(const char *who, unsigned long event,
+      unsigned long param1, unsigned long param2)
 {
 {
-    if( gIOKitDebug & kIOLogPower) {
-        kprintf("%s %02d %08x %08x\n",who,event,param1,param2);
-//        IOLog("%s %02d %08x %08x\n",who,event,param1,param2);
-    }
+       clock_sec_t nows;
+       clock_usec_t nowus;
+       clock_get_system_microtime(&nows, &nowus);
+       nowus += (nows % 1000) * 1000000;
+
+    kprintf("pm%u %p %.30s %d %lx %lx\n",
+               nowus, OBFUSCATE(current_thread()), who,        // Identity
+               (int) event, (long)OBFUSCATE(param1), (long)OBFUSCATE(param2));                 // Args
 }
 
 
 }
 
 
@@ -347,7 +422,6 @@ void IOPlatformExpert::PMInstantiatePowerDomains ( void )
     root->init();
     root->attach(this);
     root->start(this);
     root->init();
     root->attach(this);
     root->start(this);
-    root->youAreRoot();
 }
 
 
 }
 
 
@@ -616,7 +690,8 @@ void printDictionaryKeys (OSDictionary * inDictionary, char * inMsg)
     if ( mkey->isEqualTo ("name") ) {
       char nameStr[64];
       nameStr[0] = 0;
     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);
     }
       if (strlen(nameStr) > 0)
         IOLog ("%s name is %s\n", inMsg, nameStr);
     }
@@ -629,7 +704,8 @@ void printDictionaryKeys (OSDictionary * inDictionary, char * inMsg)
   mcoll->release ();
 }
 
   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;
 {
    char * buffer;
    unsigned int    len, i;
@@ -639,10 +715,11 @@ static void getCStringForObject (OSObject * inObj, char * outStr)
 
    char * objString = (char *) (inObj->getMetaClass())->getClassName();
 
 
    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)) {
      len = ((OSData *)inObj)->getLength();
      buffer = (char *)((OSData *)inObj)->getBytesNoCopy();
      if (buffer && (len > 0)) {
@@ -654,15 +731,17 @@ static void getCStringForObject (OSObject * inObj, char * outStr)
    }
 }
 
    }
 }
 
-/* IOPMPanicOnShutdownHang
+/* IOShutdownNotificationsTimedOut
  * - Called from a timer installed by PEHaltRestart
  */
  * - Called from a timer installed by PEHaltRestart
  */
-static void IOPMPanicOnShutdownHang(thread_call_param_t p0, thread_call_param_t p1)
+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 */
 
     /* 30 seconds has elapsed - resume shutdown */
-    gIOPlatform->haltRestart(type);
+    if(gIOPlatform) gIOPlatform->haltRestart(type);
 }
 
 
 }
 
 
@@ -698,13 +777,16 @@ int PEGetPlatformEpoch(void)
 
 int PEHaltRestart(unsigned int type)
 {
 
 int PEHaltRestart(unsigned int type)
 {
-  IOPMrootDomain    *pmRootDomain = IOService::getPMRootDomain();
-  bool              noWaitForResponses;
+  IOPMrootDomain    *pmRootDomain;
   AbsoluteTime      deadline;
   thread_call_t     shutdown_hang;
   AbsoluteTime      deadline;
   thread_call_t     shutdown_hang;
+  IORegistryEntry   *node;
+  OSData            *data;
+  uint32_t          timeout = 30;
   
   
-  if(type == kPEHaltCPU || type == kPERestartCPU)
+  if(type == kPEHaltCPU || type == kPERestartCPU || type == kPEUPSDelayHaltCPU)
   {
   {
+    pmRootDomain = IOService::getPMRootDomain();
     /* Notify IOKit PM clients of shutdown/restart
        Clients subscribe to this message with a call to
        IOService::registerInterest()
     /* Notify IOKit PM clients of shutdown/restart
        Clients subscribe to this message with a call to
        IOService::registerInterest()
@@ -712,23 +794,42 @@ int PEHaltRestart(unsigned int type)
     
     /* Spawn a thread that will panic in 30 seconds. 
        If all goes well the machine will be off by the time
     
     /* Spawn a thread that will panic in 30 seconds. 
        If all goes well the machine will be off by the time
-       the timer expires.
+       the timer expires. If the device wants a different
+       timeout, use that value instead of 30 seconds.
      */
      */
-    shutdown_hang = thread_call_allocate( &IOPMPanicOnShutdownHang, (thread_call_param_t) type);
-    clock_interval_to_deadline( 30, kSecondScale, &deadline );
+#define RESTART_NODE_PATH    "/chosen"
+    node = IORegistryEntry::fromPath( RESTART_NODE_PATH, gIODTPlane );
+    if ( node ) {
+      data = OSDynamicCast( OSData, node->getProperty( "halt-restart-timeout" ) );
+      if ( data && data->getLength() == 4 )
+        timeout = *((uint32_t *) data->getBytesNoCopy());
+    }
+
+    shutdown_hang = thread_call_allocate( &IOShutdownNotificationsTimedOut, 
+                        (thread_call_param_t)(uintptr_t) type);
+    clock_interval_to_deadline( timeout, kSecondScale, &deadline );
     thread_call_enter1_delayed( shutdown_hang, 0, deadline );
     thread_call_enter1_delayed( shutdown_hang, 0, deadline );
-    
-    noWaitForResponses = pmRootDomain->tellChangeDown2(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
     /* 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.
      */
    }
      */
    }
+   else if(type == kPEPanicRestartCPU || type == kPEPanicSync)
+   {
+       // Do an initial sync to flush as much panic data as possible,
+       // in case we have a problem in one of the platorm panic handlers.
+       // After running the platform handlers, do a final sync w/
+       // platform hardware quiesced for the panic.
+       PE_sync_panic_buffers();
+       IOCPURunPlatformPanicActions(type);
+       PE_sync_panic_buffers();
+   }
 
   if (gIOPlatform) return gIOPlatform->haltRestart(type);
   else return -1;
 
   if (gIOPlatform) return gIOPlatform->haltRestart(type);
   else return -1;
@@ -740,24 +841,264 @@ UInt32 PESavePanicInfo(UInt8 *buffer, UInt32 length)
   else return 0;
 }
 
   else return 0;
 }
 
+
+
+inline static int init_gIOOptionsEntry(void)
+{
+    IORegistryEntry *entry;
+    void *nvram_entry;
+    volatile void **options;
+    int ret = -1;
+
+    if (gIOOptionsEntry) 
+        return 0;
+
+    entry = IORegistryEntry::fromPath( "/options", gIODTPlane );
+    if (!entry)
+        return -1;
+
+    nvram_entry = (void *) OSDynamicCast(IODTNVRAM, entry);
+    if (!nvram_entry) 
+        goto release;
+
+    options = (volatile void **) &gIOOptionsEntry;
+    if (!OSCompareAndSwapPtr(NULL, nvram_entry, options)) {
+        ret = 0;
+        goto release;
+    }
+
+    return 0;
+
+release:
+    entry->release();
+    return ret;
+
+}
+
+/* pass in a NULL value if you just want to figure out the len */
+boolean_t PEReadNVRAMProperty(const char *symbol, void *value,
+                              unsigned int *len)
+{
+    OSObject  *obj;
+    OSData *data;
+    unsigned int vlen;
+
+    if (!symbol || !len)
+        goto err;
+
+    if (init_gIOOptionsEntry() < 0)
+        goto err;
+
+    vlen = *len;
+    *len = 0;
+
+    obj = gIOOptionsEntry->getProperty(symbol);
+    if (!obj)
+        goto err;
+
+    /* convert to data */
+    data = OSDynamicCast(OSData, obj);
+    if (!data) 
+        goto err;
+
+    *len  = data->getLength();
+    vlen  = min(vlen, *len);
+    if (value && vlen)
+        memcpy((void *) value, data->getBytesNoCopy(), vlen);
+
+    return TRUE;
+
+err:
+    return FALSE;
+}
+
+boolean_t
+PEWriteNVRAMBooleanProperty(const char *symbol, boolean_t value)
+{
+       const OSSymbol *sym = NULL;
+       OSBoolean *data = NULL;
+       bool ret = false;
+
+       if (symbol == NULL) {
+               goto exit;
+       }
+
+       if (init_gIOOptionsEntry() < 0) {
+               goto exit;
+       }
+
+       if ((sym = OSSymbol::withCStringNoCopy(symbol)) == NULL) {
+               goto exit;
+       }
+
+       data  = value ? kOSBooleanTrue : kOSBooleanFalse;
+       ret = gIOOptionsEntry->setProperty(sym, data);
+
+       sym->release();
+
+       /* success, force the NVRAM to flush writes */
+       if (ret == true) {
+               gIOOptionsEntry->sync();
+       }
+
+exit:
+       return ret;
+}
+
+boolean_t PEWriteNVRAMProperty(const char *symbol, const void *value, 
+                               const unsigned int len)
+{
+    const OSSymbol *sym;
+    OSData *data;
+    bool ret = false;
+
+    if (!symbol || !value || !len)
+        goto err;
+
+    if (init_gIOOptionsEntry() < 0)
+        goto err;
+
+    sym = OSSymbol::withCStringNoCopy(symbol);
+    if (!sym)
+        goto err;
+
+    data = OSData::withBytes((void *) value, len);
+    if (!data)
+        goto sym_done;
+
+    ret = gIOOptionsEntry->setProperty(sym, data);
+    data->release();
+
+sym_done:
+    sym->release();
+
+    if (ret == true) {
+        gIOOptionsEntry->sync();
+        return TRUE;
+    }
+
+err:
+    return FALSE;
+}
+
+
+boolean_t PERemoveNVRAMProperty(const char *symbol)
+{
+    const OSSymbol *sym;
+
+    if (!symbol)
+        goto err;
+
+    if (init_gIOOptionsEntry() < 0)
+        goto err;
+
+    sym = OSSymbol::withCStringNoCopy(symbol);
+    if (!sym)
+        goto err;
+
+    gIOOptionsEntry->removeProperty(sym);
+
+    sym->release();
+
+    gIOOptionsEntry->sync();
+    return TRUE;
+
+err:
+    return FALSE;
+
+}
+
 long PEGetGMTTimeOfDay(void)
 {
 long PEGetGMTTimeOfDay(void)
 {
-    if( gIOPlatform)
-       return( gIOPlatform->getGMTTimeOfDay());
-    else
-       return( 0 );
+    clock_sec_t     secs;
+    clock_usec_t    usecs;
+
+    PEGetUTCTimeOfDay(&secs, &usecs);
+    return secs;
 }
 
 void PESetGMTTimeOfDay(long secs)
 {
 }
 
 void PESetGMTTimeOfDay(long secs)
 {
-    if( gIOPlatform)
-       gIOPlatform->setGMTTimeOfDay(secs);
+    PESetUTCTimeOfDay(secs, 0);
+}
+
+void PEGetUTCTimeOfDay(clock_sec_t * secs, clock_usec_t * usecs)
+{
+    clock_nsec_t    nsecs = 0;
+
+    *secs = 0;
+       if (gIOPlatform)
+        gIOPlatform->getUTCTimeOfDay(secs, &nsecs);
+
+    assert(nsecs < NSEC_PER_SEC);
+    *usecs = nsecs / NSEC_PER_USEC;
+}
+
+void PESetUTCTimeOfDay(clock_sec_t secs, clock_usec_t usecs)
+{
+    assert(usecs < USEC_PER_SEC);
+       if (gIOPlatform)
+        gIOPlatform->setUTCTimeOfDay(secs, usecs * NSEC_PER_USEC);
 }
 
 } /* extern "C" */
 
 void IOPlatformExpert::registerNVRAMController(IONVRAMController * caller)
 {
 }
 
 } /* 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");
 }
 
     publishResource("IONVRAM");
 }
 
@@ -771,7 +1112,7 @@ IOReturn IOPlatformExpert::callPlatformFunction(const OSSymbol *functionName,
   if (waitForFunction) {
     _resources = waitForService(resourceMatching(functionName));
   } else {
   if (waitForFunction) {
     _resources = waitForService(resourceMatching(functionName));
   } else {
-    _resources = resources();
+    _resources = getResourceService();
   }
   if (_resources == 0) return kIOReturnUnsupported;
   
   }
   if (_resources == 0) return kIOReturnUnsupported;
   
@@ -863,7 +1204,7 @@ bool IODTPlatformExpert::createNubs( IOService * parent, OSIterator * iter )
     return( ok );
 }
 
     return( ok );
 }
 
-void IODTPlatformExpert::processTopLevel( IORegistryEntry * root )
+void IODTPlatformExpert::processTopLevel( IORegistryEntry * rootEntry )
 {
     OSIterator *       kids;
     IORegistryEntry *  next;
 {
     OSIterator *       kids;
     IORegistryEntry *  next;
@@ -871,7 +1212,7 @@ void IODTPlatformExpert::processTopLevel( IORegistryEntry * root )
     IORegistryEntry *  options;
 
     // infanticide
     IORegistryEntry *  options;
 
     // infanticide
-    kids = IODTFindMatchingEntries( root, 0, deleteList() );
+    kids = IODTFindMatchingEntries( rootEntry, 0, deleteList() );
     if( kids) {
        while( (next = (IORegistryEntry *)kids->getNextObject())) {
            next->detachAll( gIODTPlane);
     if( kids) {
        while( (next = (IORegistryEntry *)kids->getNextObject())) {
            next->detachAll( gIODTPlane);
@@ -880,7 +1221,7 @@ void IODTPlatformExpert::processTopLevel( IORegistryEntry * root )
     }
 
     // Publish an IODTNVRAM class on /options.
     }
 
     // Publish an IODTNVRAM class on /options.
-    options = root->childFromPath("options", gIODTPlane);
+    options = rootEntry->childFromPath("options", gIODTPlane);
     if (options) {
       dtNVRAM = new IODTNVRAM;
       if (dtNVRAM) {
     if (options) {
       dtNVRAM = new IODTNVRAM;
       if (dtNVRAM) {
@@ -890,17 +1231,21 @@ void IODTPlatformExpert::processTopLevel( IORegistryEntry * root )
         } else {
          dtNVRAM->attach(this);
          dtNVRAM->registerService();
         } else {
          dtNVRAM->attach(this);
          dtNVRAM->registerService();
+         options->release();
        }
       }
     }
 
     // Publish the cpus.
        }
       }
     }
 
     // Publish the cpus.
-    cpus = root->childFromPath( "cpus", gIODTPlane);
+    cpus = rootEntry->childFromPath( "cpus", gIODTPlane);
     if ( cpus)
     if ( cpus)
+    {
       createNubs( this, IODTFindMatchingEntries( cpus, kIODTExclusive, 0));
       createNubs( this, IODTFindMatchingEntries( cpus, kIODTExclusive, 0));
+      cpus->release();
+    }
 
     // publish top level, minus excludeList
 
     // publish top level, minus excludeList
-    createNubs( this, IODTFindMatchingEntries( root, kIODTExclusive, excludeList()));
+    createNubs( this, IODTFindMatchingEntries( rootEntry, kIODTExclusive, excludeList()));
 }
 
 IOReturn IODTPlatformExpert::getNubResources( IOService * nub )
 }
 
 IOReturn IODTPlatformExpert::getNubResources( IOService * nub )
@@ -963,7 +1308,7 @@ bool IODTPlatformExpert::getMachineName( char * name, int maxLength )
     ok = (0 != prop);
 
     if( ok )
     ok = (0 != prop);
 
     if( ok )
-       strncpy( name, (const char *) prop->getBytesNoCopy(), maxLength );
+       strlcpy( name, (const char *) prop->getBytesNoCopy(), maxLength );
 
     return( ok );
 }
 
     return( ok );
 }
@@ -1049,6 +1394,39 @@ IOByteCount IODTPlatformExpert::savePanicInfo(UInt8 *buffer, IOByteCount length)
   return lengthSaved;
 }
 
   return lengthSaved;
 }
 
+OSString* IODTPlatformExpert::createSystemSerialNumberString(OSData* myProperty) {
+    UInt8* serialNumber;
+    unsigned int serialNumberSize;
+    unsigned short pos = 0;
+    char* temp;
+    char SerialNo[30];
+    
+    if (myProperty != NULL) {
+        serialNumberSize = myProperty->getLength();
+        serialNumber = (UInt8*)(myProperty->getBytesNoCopy());
+        temp = (char*)serialNumber;
+        if (serialNumberSize > 0) {
+            // check to see if this is a CTO serial number...
+            while (pos < serialNumberSize && temp[pos] != '-') pos++;
+            
+            if (pos < serialNumberSize) { // there was a hyphen, so it's a CTO serial number
+                memcpy(SerialNo, serialNumber + 12, 8);
+                memcpy(&SerialNo[8], serialNumber, 3);
+                SerialNo[11] = '-';
+                memcpy(&SerialNo[12], serialNumber + 3, 8);
+                SerialNo[20] = 0;
+            } else { // just a normal serial number
+                memcpy(SerialNo, serialNumber + 13, 8);
+                memcpy(&SerialNo[8], serialNumber, 3);
+                SerialNo[11] = 0;
+            }
+            return OSString::withCString(SerialNo);
+        }
+    }
+    return NULL;
+}
+
+
 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
 #undef super
 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
 #undef super
@@ -1064,7 +1442,7 @@ OSMetaClassDefineReservedUnused(IOPlatformExpertDevice,  3);
 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
 bool IOPlatformExpertDevice::compareName( OSString * name,
 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
 bool IOPlatformExpertDevice::compareName( OSString * name,
-                                        OSString ** matched = 0 ) const
+                                        OSString ** matched ) const
 {
     return( IODTCompareNubName( this, name, matched ));
 }
 {
     return( IODTCompareNubName( this, name, matched ));
 }
@@ -1074,7 +1452,6 @@ IOPlatformExpertDevice::initWithArgs(
                             void * dtTop, void * p2, void * p3, void * p4 )
 {
     IORegistryEntry *  dt = 0;
                             void * dtTop, void * p2, void * p3, void * p4 )
 {
     IORegistryEntry *  dt = 0;
-    void *             argsData[ 4 ];
     bool               ok;
 
     // dtTop may be zero on non- device tree systems
     bool               ok;
 
     // dtTop may be zero on non- device tree systems
@@ -1086,17 +1463,11 @@ IOPlatformExpertDevice::initWithArgs(
     if( !ok)
        return( false);
 
     if( !ok)
        return( false);
 
+    reserved = NULL;
     workLoop = IOWorkLoop::workLoop();
     if (!workLoop)
         return false;
 
     workLoop = IOWorkLoop::workLoop();
     if (!workLoop)
         return false;
 
-    argsData[ 0 ] = dtTop;
-    argsData[ 1 ] = p2;
-    argsData[ 2 ] = p3;
-    argsData[ 3 ] = p4;
-
-    setProperty("IOPlatformArgs", (void *)argsData, sizeof( argsData));
-
     return( true);
 }
 
     return( true);
 }
 
@@ -1105,6 +1476,88 @@ IOWorkLoop *IOPlatformExpertDevice::getWorkLoop() const
     return workLoop;
 }
 
     return workLoop;
 }
 
+IOReturn IOPlatformExpertDevice::setProperties( OSObject * properties )
+{
+    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;
+}
+
+IOReturn IOPlatformExpertDevice::newUserClient( task_t owningTask, void * securityID,
+                                    UInt32 type,  OSDictionary * properties,
+                                    IOUserClient ** handler )
+{
+    IOReturn            err = kIOReturnSuccess;
+    IOUserClient *      newConnect = 0;
+    IOUserClient *      theConnect = 0;
+
+    switch (type)
+    {
+        case kIOKitDiagnosticsClientType:
+           newConnect = IOKitDiagnosticsClient::withTask(owningTask);
+           if (!newConnect) err = kIOReturnNotPermitted;
+            break;
+        default:
+            err = kIOReturnBadArgument;
+    }
+
+    if (newConnect)
+    {
+        if ((false == newConnect->attach(this))
+                || (false == newConnect->start(this)))
+        {
+            newConnect->detach( this );
+            newConnect->release();
+            err = kIOReturnNotPermitted;
+        }
+        else
+            theConnect = newConnect;
+    }
+
+    *handler = theConnect;
+    return (err);
+}
+
 void IOPlatformExpertDevice::free()
 {
     if (workLoop)
 void IOPlatformExpertDevice::free()
 {
     if (workLoop)
@@ -1126,7 +1579,7 @@ OSMetaClassDefineReservedUnused(IOPlatformDevice,  3);
 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
 bool IOPlatformDevice::compareName( OSString * name,
 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
 bool IOPlatformDevice::compareName( OSString * name,
-                                       OSString ** matched = 0 ) const
+                                       OSString ** matched ) const
 {
     return( ((IOPlatformExpert *)getProvider())->
                compareNubName( this, name, matched ));
 {
     return( ((IOPlatformExpert *)getProvider())->
                compareNubName( this, name, matched ));
@@ -1155,7 +1608,7 @@ class IOPanicPlatform : IOPlatformExpert {
     OSDeclareDefaultStructors(IOPanicPlatform);
 
 public:
     OSDeclareDefaultStructors(IOPanicPlatform);
 
 public:
-    bool start(IOService * provider);
+    bool start(IOService * provider) APPLE_KEXT_OVERRIDE;
 };
 
 
 };
 
 
@@ -1172,3 +1625,4 @@ bool IOPanicPlatform::start(IOService * provider) {
 
     return false;
 }
 
     return false;
 }
+