]> git.saurik.com Git - apple/xnu.git/blobdiff - iokit/Kernel/IONVRAM.cpp
xnu-201.42.3.tar.gz
[apple/xnu.git] / iokit / Kernel / IONVRAM.cpp
index 703dc94f47daa86189b228547a9b47e5402f2e5b..017f73c93c298a67e4687ecc6cdd7da32b069893 100644 (file)
@@ -42,7 +42,13 @@ bool IODTNVRAM::init(IORegistryEntry *old, const IORegistryPlane *plane)
   
   _nvramImage = IONew(UInt8, kIODTNVRAMImageSize);
   if (_nvramImage == 0) return false;
-
+  
+  _nvramPartitionOffsets = OSDictionary::withCapacity(1);
+  if (_nvramPartitionOffsets == 0) return false;
+  
+  _nvramPartitionLengths = OSDictionary::withCapacity(1);
+  if (_nvramPartitionLengths == 0) return false;
+  
   _registryPropertiesKey = OSSymbol::withCStringNoCopy("aapl,pci");
   if (_registryPropertiesKey == 0) return false;
   
@@ -51,7 +57,11 @@ bool IODTNVRAM::init(IORegistryEntry *old, const IORegistryPlane *plane)
 
 void IODTNVRAM::registerNVRAMController(IONVRAMController *nvram)
 {
-  UInt32 currentOffset = 0;
+  char   partitionID[18];
+  UInt32 partitionOffset, partitionLength;
+  UInt32 freePartitionOffset, freePartitionSize;
+  UInt32 currentLength, currentOffset = 0;
+  OSNumber *partitionOffsetNumber, *partitionLengthNumber;
   
   if (_nvramController != 0) return;
   
@@ -59,26 +69,53 @@ void IODTNVRAM::registerNVRAMController(IONVRAMController *nvram)
   
   _nvramController->read(0, _nvramImage, kIODTNVRAMImageSize);
   
-  // Find the offsets for the OF, XPRAM and NameRegistry partitions in NVRAM.
+  // Find the offsets for the OF, XPRAM, and NameRegistry partitions.
   _ofPartitionOffset = 0xFFFFFFFF;
   _xpramPartitionOffset = 0xFFFFFFFF;
   _nrPartitionOffset = 0xFFFFFFFF;
+  freePartitionOffset = 0xFFFFFFFF;
+  freePartitionSize = 0;
   if (getPlatform()->getBootROMType()) {
     // Look through the partitions to find the OF, MacOS partitions.
     while (currentOffset < kIODTNVRAMImageSize) {
-      if (strcmp((const char *)_nvramImage + currentOffset + 4, "common") == 0) {
-       _ofPartitionOffset = currentOffset + 16;
-       _ofPartitionSize =
-         (((UInt16 *)(_nvramImage + currentOffset))[1] - 1) * 0x10;
-      } else if (strcmp((const char *)_nvramImage + currentOffset + 4, "APL,MacOS75") == 0) {
-       _xpramPartitionOffset = currentOffset + 16;
+      currentLength = ((UInt16 *)(_nvramImage + currentOffset))[1] * 16;
+      
+      partitionOffset = currentOffset + 16;
+      partitionLength = currentLength - 16;
+      
+      if (strncmp((const char *)_nvramImage + currentOffset + 4,
+                 kIODTNVRAMOFPartitionName, 12) == 0) {
+       _ofPartitionOffset = partitionOffset;
+       _ofPartitionSize = partitionLength;
+      } else if (strncmp((const char *)_nvramImage + currentOffset + 4,
+                        kIODTNVRAMXPRAMPartitionName, 12) == 0) {
+       _xpramPartitionOffset = partitionOffset;
        _xpramPartitionSize = kIODTNVRAMXPRAMSize;
        _nrPartitionOffset = _xpramPartitionOffset + _xpramPartitionSize;
-       _nrPartitionSize =
-         (((UInt16 *)(_nvramImage + currentOffset))[1] - 1) * 0x10 -
-         _xpramPartitionSize;
+       _nrPartitionSize = partitionLength - _xpramPartitionSize;
+      } else if (strncmp((const char *)_nvramImage + currentOffset + 4,
+                        kIODTNVRAMFreePartitionName, 12) == 0) {
+       freePartitionOffset = currentOffset;
+       freePartitionSize = currentLength;
+      } else {
+       // Construct the partition ID from the signature and name.
+       sprintf(partitionID, "0x%02x,",
+               *(UInt8 *)(_nvramImage + currentOffset));
+       strncpy(partitionID + 5,
+               (const char *)(_nvramImage + currentOffset + 4), 12);
+       partitionID[17] = '\0';
+       
+       partitionOffsetNumber = OSNumber::withNumber(partitionOffset, 32);
+       partitionLengthNumber = OSNumber::withNumber(partitionLength, 32);
+       
+       // Save the partition offset and length
+       _nvramPartitionOffsets->setObject(partitionID, partitionOffsetNumber);
+       _nvramPartitionLengths->setObject(partitionID, partitionLengthNumber);
+       
+       partitionOffsetNumber->release();
+       partitionLengthNumber->release();
       }
-      currentOffset += ((short *)(_nvramImage + currentOffset))[1] * 16;
+      currentOffset += currentLength;
     }
   } else {
     // Use the fixed address for old world machines.
@@ -335,7 +372,66 @@ IOReturn IODTNVRAM::writeNVRAMProperty(IORegistryEntry *entry,
   return err;
 }
 
+OSDictionary *IODTNVRAM::getNVRAMPartitions(void)
+{
+  return _nvramPartitionLengths;
+}
+
+IOReturn IODTNVRAM::readNVRAMPartition(const OSSymbol *partitionID,
+                                      IOByteCount offset, UInt8 *buffer,
+                                      IOByteCount length)
+{
+  OSNumber *partitionOffsetNumber, *partitionLengthNumber;
+  UInt32   partitionOffset, partitionLength;
+  
+  partitionOffsetNumber =
+    (OSNumber *)_nvramPartitionOffsets->getObject(partitionID);
+  partitionLengthNumber =
+    (OSNumber *)_nvramPartitionLengths->getObject(partitionID);
+  
+  if ((partitionOffsetNumber == 0) || (partitionLengthNumber == 0))
+    return kIOReturnNotFound;
+  
+  partitionOffset = partitionOffsetNumber->unsigned32BitValue();
+  partitionLength = partitionLengthNumber->unsigned32BitValue();
+  
+  if ((buffer == 0) || (length <= 0) || (offset < 0) ||
+      (offset + length > partitionLength))
+    return kIOReturnBadArgument;
+  
+  bcopy(_nvramImage + partitionOffset + offset, buffer, length);
+  
+  return kIOReturnSuccess;
+}
 
+IOReturn IODTNVRAM::writeNVRAMPartition(const OSSymbol *partitionID,
+                                       IOByteCount offset, UInt8 *buffer,
+                                       IOByteCount length)
+{
+  OSNumber *partitionOffsetNumber, *partitionLengthNumber;
+  UInt32   partitionOffset, partitionLength;
+  
+  partitionOffsetNumber =
+    (OSNumber *)_nvramPartitionOffsets->getObject(partitionID);
+  partitionLengthNumber =
+    (OSNumber *)_nvramPartitionLengths->getObject(partitionID);
+  
+  if ((partitionOffsetNumber == 0) || (partitionLengthNumber == 0))
+    return kIOReturnNotFound;
+  
+  partitionOffset = partitionOffsetNumber->unsigned32BitValue();
+  partitionLength = partitionLengthNumber->unsigned32BitValue();
+  
+  if ((buffer == 0) || (length <= 0) || (offset < 0) ||
+      (offset + length > partitionLength))
+    return kIOReturnBadArgument;
+  
+  bcopy(buffer, _nvramImage + partitionOffset + offset, length);
+  
+  _nvramImageDirty = true;
+  
+  return kIOReturnSuccess;
+}
 
 // Private methods for Open Firmware variable access.