+void di_root_ramfile( IORegistryEntry * entry )
+{
+ OSData * data;
+ IOMemoryDescriptor * mem;
+ uint64_t dmgSize;
+ uint64_t remain, length;
+ OSData * extentData = 0;
+ IOAddressRange * extentList;
+ uint64_t extentSize;
+ uint32_t extentCount;
+
+ do {
+ data = OSDynamicCast(OSData, entry->getProperty("boot-ramdmg-size"));
+ if (!data || (data->getLength() != sizeof(uint64_t)))
+ break; // bad disk image size
+
+ dmgSize = *(uint64_t *) data->getBytesNoCopy();
+ if (!dmgSize)
+ break;
+
+ data = OSDynamicCast(OSData, entry->getProperty("boot-ramdmg-extents"));
+ if (!data || (data->getLength() == 0) ||
+ ((data->getLength() & (sizeof(IOAddressRange)-1)) != 0))
+ break; // bad extents
+
+ // make modifications to local copy
+ extentData = OSData::withData(data);
+ assert(extentData);
+
+ extentList = (IOAddressRange *) extentData->getBytesNoCopy();
+ extentCount = extentData->getLength() / sizeof(IOAddressRange);
+ extentSize = 0;
+ remain = dmgSize;
+
+ // truncate extent length to enclosing disk image
+ for (uint32_t i = 0; i < extentCount; i++)
+ {
+ length = extentList[i].length;
+ if (!length) break;
+
+ extentSize += length;
+ if (length >= remain)
+ {
+ extentList[i].length = remain;
+ extentCount = i + 1;
+ break;
+ }
+ remain -= length;
+ }
+ if (extentSize < dmgSize)
+ break; // not enough extent bytes for enclosing disk image
+
+ mem = IOMemoryDescriptor::withAddressRanges(
+ extentList, extentCount,
+ kIODirectionOut | kIOMemoryMapperNone, NULL);
+
+ if (mem)
+ {
+ IOService * controller = di_load_controller();
+ if (controller)
+ {
+ controller->setProperty(kDIRootRamFileKey, mem);
+ controller->release();
+ }
+ mem->release();
+ }
+ } while (false);
+
+ if (extentData)
+ extentData->release();
+}
+