Loading...
iokit/Kernel/IONVRAM.cpp xnu-201.42.3 xnu-517
--- xnu/xnu-201.42.3/iokit/Kernel/IONVRAM.cpp
+++ xnu/xnu-517/iokit/Kernel/IONVRAM.cpp
@@ -3,19 +3,22 @@
  *
  * @APPLE_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.
+ * Copyright (c) 1999-2003 Apple Computer, Inc.  All Rights Reserved.
  * 
- * This Original Code and all software distributed under the License are
- * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * 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.
+ * 
+ * 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@
  */
@@ -69,10 +72,11 @@
   
   _nvramController->read(0, _nvramImage, kIODTNVRAMImageSize);
   
-  // Find the offsets for the OF, XPRAM, and NameRegistry partitions.
+  // Find the offsets for the OF, XPRAM, NameRegistry and PanicInfo partitions.
   _ofPartitionOffset = 0xFFFFFFFF;
   _xpramPartitionOffset = 0xFFFFFFFF;
   _nrPartitionOffset = 0xFFFFFFFF;
+  _piPartitionOffset = 0xFFFFFFFF;
   freePartitionOffset = 0xFFFFFFFF;
   freePartitionSize = 0;
   if (getPlatform()->getBootROMType()) {
@@ -93,6 +97,10 @@
 	_xpramPartitionSize = kIODTNVRAMXPRAMSize;
 	_nrPartitionOffset = _xpramPartitionOffset + _xpramPartitionSize;
 	_nrPartitionSize = partitionLength - _xpramPartitionSize;
+      } else if (strncmp((const char *)_nvramImage + currentOffset + 4,
+			 kIODTNVRAMPanicInfoPartitonName, 12) == 0) {
+	_piPartitionOffset = partitionOffset;
+	_piPartitionSize = partitionLength;
       } else if (strncmp((const char *)_nvramImage + currentOffset + 4,
 			 kIODTNVRAMFreePartitionName, 12) == 0) {
 	freePartitionOffset = currentOffset;
@@ -134,6 +142,60 @@
   if (_nrPartitionOffset != 0xFFFFFFFF)
     _nrImage    = _nvramImage + _nrPartitionOffset;
   
+  if (_piPartitionOffset == 0xFFFFFFFF) {
+    if (freePartitionSize > 0x20) {
+      // Set the signature to 0xa1.
+      _nvramImage[freePartitionOffset] = 0xa1;
+      // Set the checksum to 0.
+      _nvramImage[freePartitionOffset + 1] = 0;
+      // Set the name for the Panic Info partition.
+      strncpy((char *)(_nvramImage + freePartitionOffset + 4),
+	      kIODTNVRAMPanicInfoPartitonName, 12);
+      
+      // Calculate the partition offset and size.
+      _piPartitionOffset = freePartitionOffset + 0x10;
+      _piPartitionSize = 0x800;
+      if (_piPartitionSize + 0x20 > freePartitionSize)
+	_piPartitionSize = freePartitionSize - 0x20;
+      
+      _piImage = _nvramImage + _piPartitionOffset;
+      
+      // Zero the new partition.
+      bzero(_piImage, _piPartitionSize);
+      
+      // Set the partition size.
+      *(UInt16 *)(_nvramImage + freePartitionOffset + 2) =
+	(_piPartitionSize / 0x10) + 1;
+      
+      // Set the partition checksum.
+      _nvramImage[freePartitionOffset + 1] =
+	calculatePartitionChecksum(_nvramImage + freePartitionOffset);
+      
+      // Calculate the free partition offset and size.
+      freePartitionOffset += _piPartitionSize + 0x10;
+      freePartitionSize -= _piPartitionSize + 0x10;
+      
+      // Set the signature to 0x7f.
+      _nvramImage[freePartitionOffset] = 0x7f;
+      // Set the checksum to 0.
+      _nvramImage[freePartitionOffset + 1] = 0;
+      // Set the name for the free partition.
+      strncpy((char *)(_nvramImage + freePartitionOffset + 4),
+	      kIODTNVRAMFreePartitionName, 12);
+      // Set the partition size.
+      *(UInt16 *)(_nvramImage + freePartitionOffset + 2) =
+	freePartitionSize / 0x10;
+      // Set the partition checksum.
+      _nvramImage[freePartitionOffset + 1] =
+	calculatePartitionChecksum(_nvramImage + freePartitionOffset);
+      
+      // Set the nvram image as dirty.
+      _nvramImageDirty = true;
+    }
+  } else {
+    _piImage = _nvramImage + _piPartitionOffset;
+  }
+  
   initOFVariables();
 }
 
@@ -141,7 +203,8 @@
 {
   if (!_nvramImageDirty && !_ofImageDirty) return;
   
-  syncOFVariables();
+  // Don't try to sync OF Variables if the system has already paniced.
+  if (!_systemPaniced) syncOFVariables();
   
   _nvramController->write(0, _nvramImage, kIODTNVRAMImageSize);
   _nvramController->sync();
@@ -198,7 +261,7 @@
   if (_ofDict == 0) return 0;
   
   // Verify permissions.
-  result = IOUserClient::clientHasPrivilege(current_task(), "root");
+  result = IOUserClient::clientHasPrivilege(current_task(), kIOClientPrivilegeAdministrator);
   if (result != kIOReturnSuccess) {
     variablePerm = getOFVariablePerm(aKey);
     if (variablePerm == kOFVariablePermRootOnly) return 0;
@@ -231,7 +294,7 @@
   if (_ofDict == 0) return false;
   
   // Verify permissions.
-  result = IOUserClient::clientHasPrivilege(current_task(), "root");
+  result = IOUserClient::clientHasPrivilege(current_task(), kIOClientPrivilegeAdministrator);
   if (result != kIOReturnSuccess) {
     propPerm = getOFVariablePerm(aKey);
     if (propPerm != kOFVariablePermUserWrite) return false;
@@ -241,6 +304,9 @@
   if (getPlatform()->getBootROMType() == 0) {
     if (_ofDict->getObject(aKey) == 0) return false;
   }
+  
+  // Don't allow change of 'aapl,panic-info'.
+  if (aKey->isEqualTo(kIODTNVRAMPanicInfoKey)) return false;
   
   // Make sure the object is of the correct type.
   propType = getOFVariableType(aKey);
@@ -433,7 +499,40 @@
   return kIOReturnSuccess;
 }
 
-// Private methods for Open Firmware variable access.
+UInt32 IODTNVRAM::savePanicInfo(UInt8 *buffer, IOByteCount length)
+{
+  if ((_piImage == 0) || (length <= 0)) return 0;
+  
+  if (length > (_piPartitionSize - 4))
+    length = _piPartitionSize - 4;
+  
+  // Save the Panic Info.
+  bcopy(buffer, _piImage + 4, length);
+  
+  // Save the Panic Info length.
+  *(UInt32 *)_piImage = length;
+  
+  _nvramImageDirty = true;
+  
+  _systemPaniced = true;
+  
+  return length;
+}
+
+// Private methods
+
+UInt8 IODTNVRAM::calculatePartitionChecksum(UInt8 *partitionHeader)
+{
+  UInt8 cnt, isum, csum = 0;
+  
+  for (cnt = 0; cnt < 0x10; cnt++) {
+    isum = csum + partitionHeader[cnt];
+    if (isum < csum) isum++;
+    csum = isum;
+  }
+  
+  return csum;
+}
 
 struct OWVariablesHeader {
   UInt16   owMagic;
@@ -508,6 +607,20 @@
       if (propObject != 0) {
 	_ofDict->setObject("boot-args", propObject);
 	propObject->release();
+      }
+    }
+    
+    // Create the 'aapl,panic-info' property if needed.
+    if (_piImage != 0) {
+      propDataLength = *(UInt32 *)_piImage;
+      if ((propDataLength != 0) && (propDataLength < (_piPartitionSize - 4))) {
+	propObject = OSData::withBytes(_piImage + 4, propDataLength);
+	_ofDict->setObject(kIODTNVRAMPanicInfoKey, propObject);
+	propObject->release();
+	
+	// Clear the length from _piImage and mark dirty.
+	*(UInt32 *)_piImage = 0;
+	_nvramImageDirty = true;
       }
     }
   } else {
@@ -599,6 +712,9 @@
     while (ok) {
       tmpSymbol = OSDynamicCast(OSSymbol, iter->getNextObject());
       if (tmpSymbol == 0) break;
+      
+      // Don't save 'aapl,panic-info'.
+      if (tmpSymbol->isEqualTo(kIODTNVRAMPanicInfoKey)) continue;
       
       tmpObject = _ofDict->getObject(tmpSymbol);
       
@@ -1308,18 +1424,23 @@
       nvPath = start;
     else if (nvName == 0)
       nvName = start;
-    else if (entry ==
-	     IORegistryEntry::fromPath((const char *) nvPath, gIODTPlane)) {
-      *name = OSSymbol::withCString((const char *) nvName);
-      *value = unescapeBytesToData(start, where - start - 1);
-      if ((*name != 0) && (*value != 0))
-        err = kIOReturnSuccess;
-      else
-        err = kIOReturnNoMemory;
-      break;
-    } else
+    else {
+      IORegistryEntry * compareEntry = IORegistryEntry::fromPath((const char *) nvPath, gIODTPlane);
+      if (entry == compareEntry) {
+        if (compareEntry)
+          compareEntry->release();
+	*name = OSSymbol::withCString((const char *) nvName);
+	*value = unescapeBytesToData(start, where - start - 1);
+	if ((*name != 0) && (*value != 0))
+	  err = kIOReturnSuccess;
+	else
+	  err = kIOReturnNoMemory;
+	break;
+      }
+      if (compareEntry)
+        compareEntry->release();
       nvPath = nvName = 0;
-    
+    }
     start = where;
   }
 
@@ -1362,15 +1483,21 @@
         nvPath = start;
       else if (nvName == 0)
         nvName = start;
-      else if (entry ==
-                IORegistryEntry::fromPath((const char *) nvPath, gIODTPlane)) {
-        // delete old property (nvPath -> where)
-        data = OSData::withBytes(propStart, nvPath - propStart);
-        if (data)
-          ok &= data->appendBytes(where, end - where);
-        break;
-      } else
+      else {
+        IORegistryEntry * compareEntry = IORegistryEntry::fromPath((const char *) nvPath, gIODTPlane);
+        if (entry == compareEntry) {
+          if (compareEntry)
+             compareEntry->release();
+           // delete old property (nvPath -> where)
+           data = OSData::withBytes(propStart, nvPath - propStart);
+           if (data)
+             ok &= data->appendBytes(where, end - where);
+	   break;
+        }
+        if (compareEntry)
+          compareEntry->release();
         nvPath = nvName = 0;
+      }
         
       start = where;
     }