2 * Copyright (c) 1998-2006 Apple Computer, Inc. All rights reserved.
3 * Copyright (c) 2007-2012 Apple Inc. All rights reserved.
5 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. The rights granted to you under the License
11 * may not be used to create, or enable the creation or redistribution of,
12 * unlawful or unlicensed copies of an Apple operating system, or to
13 * circumvent, violate, or enable the circumvention or violation of, any
14 * terms of an Apple operating system software license agreement.
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this file.
19 * The Original Code and all software distributed under the License are
20 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
21 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
22 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
24 * Please see the License for the specific language governing rights and
25 * limitations under the License.
27 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 #include <IOKit/IOLib.h>
31 #include <IOKit/IONVRAM.h>
32 #include <IOKit/IOPlatformExpert.h>
33 #include <IOKit/IOUserClient.h>
34 #include <IOKit/IOKitKeys.h>
35 #include <IOKit/IOKitKeysPrivate.h>
36 #include <kern/debug.h>
37 #include <pexpert/pexpert.h>
41 #include <security/mac.h>
42 #include <security/mac_framework.h>
46 #define super IOService
48 #define kIONVRAMPrivilege kIOClientPrivilegeAdministrator
49 //#define kIONVRAMPrivilege kIOClientPrivilegeLocalUser
51 OSDefineMetaClassAndStructors(IODTNVRAM
, IOService
);
53 bool IODTNVRAM::init(IORegistryEntry
*old
, const IORegistryPlane
*plane
)
57 if (!super::init(old
, plane
)) return false;
59 dict
= OSDictionary::withCapacity(1);
60 if (dict
== 0) return false;
61 setPropertyTable(dict
);
63 _nvramImage
= IONew(UInt8
, kIODTNVRAMImageSize
);
64 if (_nvramImage
== 0) return false;
66 _nvramPartitionOffsets
= OSDictionary::withCapacity(1);
67 if (_nvramPartitionOffsets
== 0) return false;
69 _nvramPartitionLengths
= OSDictionary::withCapacity(1);
70 if (_nvramPartitionLengths
== 0) return false;
72 _registryPropertiesKey
= OSSymbol::withCStringNoCopy("aapl,pci");
73 if (_registryPropertiesKey
== 0) return false;
75 // <rdar://problem/9529235> race condition possible between
76 // IODTNVRAM and IONVRAMController (restore loses boot-args)
82 void IODTNVRAM::initProxyData(void)
84 IORegistryEntry
*entry
;
85 const char *key
= "nvram-proxy-data";
90 entry
= IORegistryEntry::fromPath("/chosen", gIODTPlane
);
92 prop
= entry
->getProperty(key
);
94 data
= OSDynamicCast(OSData
, prop
);
96 bytes
= data
->getBytesNoCopy();
97 if ((bytes
!= 0) && (data
->getLength() <= kIODTNVRAMImageSize
)) {
98 bcopy(bytes
, _nvramImage
, data
->getLength());
104 entry
->removeProperty(key
);
109 void IODTNVRAM::registerNVRAMController(IONVRAMController
*nvram
)
111 if (_nvramController
!= 0) return;
113 _nvramController
= nvram
;
115 // <rdar://problem/9529235> race condition possible between
116 // IODTNVRAM and IONVRAMController (restore loses boot-args)
118 _nvramController
->read(0, _nvramImage
, kIODTNVRAMImageSize
);
122 (void) syncVariables();
123 IOLockUnlock(_ofLock
);
127 void IODTNVRAM::initNVRAMImage(void)
129 char partitionID
[18];
130 UInt32 partitionOffset
, partitionLength
;
131 UInt32 freePartitionOffset
, freePartitionSize
;
132 UInt32 currentLength
, currentOffset
= 0;
133 OSNumber
*partitionOffsetNumber
, *partitionLengthNumber
;
135 // Find the offsets for the OF, XPRAM, NameRegistry and PanicInfo partitions.
136 _ofPartitionOffset
= 0xFFFFFFFF;
137 _piPartitionOffset
= 0xFFFFFFFF;
138 freePartitionOffset
= 0xFFFFFFFF;
139 freePartitionSize
= 0;
141 // Look through the partitions to find the OF, MacOS partitions.
142 while (currentOffset
< kIODTNVRAMImageSize
) {
143 currentLength
= ((UInt16
*)(_nvramImage
+ currentOffset
))[1] * 16;
145 if (currentLength
< 16) break;
146 partitionOffset
= currentOffset
+ 16;
147 partitionLength
= currentLength
- 16;
148 if ((partitionOffset
+ partitionLength
) > kIODTNVRAMImageSize
) break;
150 if (strncmp((const char *)_nvramImage
+ currentOffset
+ 4,
151 kIODTNVRAMOFPartitionName
, 12) == 0) {
152 _ofPartitionOffset
= partitionOffset
;
153 _ofPartitionSize
= partitionLength
;
154 } else if (strncmp((const char *)_nvramImage
+ currentOffset
+ 4,
155 kIODTNVRAMXPRAMPartitionName
, 12) == 0) {
156 } else if (strncmp((const char *)_nvramImage
+ currentOffset
+ 4,
157 kIODTNVRAMPanicInfoPartitonName
, 12) == 0) {
158 _piPartitionOffset
= partitionOffset
;
159 _piPartitionSize
= partitionLength
;
160 } else if (strncmp((const char *)_nvramImage
+ currentOffset
+ 4,
161 kIODTNVRAMFreePartitionName
, 12) == 0) {
162 freePartitionOffset
= currentOffset
;
163 freePartitionSize
= currentLength
;
165 // Construct the partition ID from the signature and name.
166 snprintf(partitionID
, sizeof(partitionID
), "0x%02x,",
167 *(UInt8
*)(_nvramImage
+ currentOffset
));
168 strncpy(partitionID
+ 5,
169 (const char *)(_nvramImage
+ currentOffset
+ 4), 12);
170 partitionID
[17] = '\0';
172 partitionOffsetNumber
= OSNumber::withNumber(partitionOffset
, 32);
173 partitionLengthNumber
= OSNumber::withNumber(partitionLength
, 32);
175 // Save the partition offset and length
176 _nvramPartitionOffsets
->setObject(partitionID
, partitionOffsetNumber
);
177 _nvramPartitionLengths
->setObject(partitionID
, partitionLengthNumber
);
179 partitionOffsetNumber
->release();
180 partitionLengthNumber
->release();
182 currentOffset
+= currentLength
;
185 if (_ofPartitionOffset
!= 0xFFFFFFFF)
186 _ofImage
= _nvramImage
+ _ofPartitionOffset
;
188 if (_piPartitionOffset
== 0xFFFFFFFF) {
189 if (freePartitionSize
> 0x20) {
190 // Set the signature to 0xa1.
191 _nvramImage
[freePartitionOffset
] = 0xa1;
192 // Set the checksum to 0.
193 _nvramImage
[freePartitionOffset
+ 1] = 0;
194 // Set the name for the Panic Info partition.
195 strncpy((char *)(_nvramImage
+ freePartitionOffset
+ 4),
196 kIODTNVRAMPanicInfoPartitonName
, 12);
198 // Calculate the partition offset and size.
199 _piPartitionOffset
= freePartitionOffset
+ 0x10;
200 _piPartitionSize
= 0x800;
201 if (_piPartitionSize
+ 0x20 > freePartitionSize
)
202 _piPartitionSize
= freePartitionSize
- 0x20;
204 _piImage
= _nvramImage
+ _piPartitionOffset
;
206 // Zero the new partition.
207 bzero(_piImage
, _piPartitionSize
);
209 // Set the partition size.
210 *(UInt16
*)(_nvramImage
+ freePartitionOffset
+ 2) =
211 (_piPartitionSize
/ 0x10) + 1;
213 // Set the partition checksum.
214 _nvramImage
[freePartitionOffset
+ 1] =
215 calculatePartitionChecksum(_nvramImage
+ freePartitionOffset
);
217 // Calculate the free partition offset and size.
218 freePartitionOffset
+= _piPartitionSize
+ 0x10;
219 freePartitionSize
-= _piPartitionSize
+ 0x10;
221 // Set the signature to 0x7f.
222 _nvramImage
[freePartitionOffset
] = 0x7f;
223 // Set the checksum to 0.
224 _nvramImage
[freePartitionOffset
+ 1] = 0;
225 // Set the name for the free partition.
226 strncpy((char *)(_nvramImage
+ freePartitionOffset
+ 4),
227 kIODTNVRAMFreePartitionName
, 12);
228 // Set the partition size.
229 *(UInt16
*)(_nvramImage
+ freePartitionOffset
+ 2) =
230 freePartitionSize
/ 0x10;
231 // Set the partition checksum.
232 _nvramImage
[freePartitionOffset
+ 1] =
233 calculatePartitionChecksum(_nvramImage
+ freePartitionOffset
);
235 if (_nvramController
!= 0) {
236 _nvramController
->write(0, _nvramImage
, kIODTNVRAMImageSize
);
240 _piImage
= _nvramImage
+ _piPartitionOffset
;
244 _freshInterval
= TRUE
; // we will allow sync() even before the first 15 minutes have passed.
249 void IODTNVRAM::syncInternal(bool rateLimit
)
251 // Don't try to perform controller operations if none has been registered.
252 if (_nvramController
== 0) return;
254 // Rate limit requests to sync. Drivers that need this rate limiting will
255 // shadow the data and only write to flash when they get a sync call
256 if (rateLimit
&& !safeToSync()) return;
258 _nvramController
->sync();
261 void IODTNVRAM::sync(void)
266 bool IODTNVRAM::serializeProperties(OSSerialize
*s
) const
268 bool result
, hasPrivilege
;
272 OSCollectionIterator
*iter
= 0;
274 // Verify permissions.
275 hasPrivilege
= (kIOReturnSuccess
== IOUserClient::clientHasPrivilege(current_task(), kIONVRAMPrivilege
));
278 /* No nvram. Return an empty dictionary. */
279 dict
= OSDictionary::withCapacity(1);
280 if (dict
== 0) return false;
283 dict
= OSDictionary::withDictionary(_ofDict
);
284 IOLockUnlock(_ofLock
);
285 if (dict
== 0) return false;
287 /* Copy properties with client privilege. */
288 iter
= OSCollectionIterator::withCollection(dict
);
294 key
= OSDynamicCast(OSSymbol
, iter
->getNextObject());
297 variablePerm
= getOFVariablePerm(key
);
298 if ((hasPrivilege
|| (variablePerm
!= kOFVariablePermRootOnly
)) &&
299 ( ! (variablePerm
== kOFVariablePermKernelOnly
&& current_task() != kernel_task
) )
301 && (current_task() == kernel_task
|| mac_iokit_check_nvram_get(kauth_cred_get(), key
->getCStringNoCopy()) == 0)
305 dict
->removeObject(key
);
311 result
= dict
->serialize(s
);
314 if (iter
!= 0) iter
->release();
319 OSObject
*IODTNVRAM::copyProperty(const OSSymbol
*aKey
) const
325 if (_ofDict
== 0) return 0;
327 // Verify permissions.
328 variablePerm
= getOFVariablePerm(aKey
);
329 result
= IOUserClient::clientHasPrivilege(current_task(), kIONVRAMPrivilege
);
330 if (result
!= kIOReturnSuccess
) {
331 if (variablePerm
== kOFVariablePermRootOnly
) return 0;
333 if (variablePerm
== kOFVariablePermKernelOnly
&& current_task() != kernel_task
) return 0;
336 if (current_task() != kernel_task
&&
337 mac_iokit_check_nvram_get(kauth_cred_get(), aKey
->getCStringNoCopy()) != 0)
342 theObject
= _ofDict
->getObject(aKey
);
343 if (theObject
) theObject
->retain();
344 IOLockUnlock(_ofLock
);
349 OSObject
*IODTNVRAM::copyProperty(const char *aKey
) const
351 const OSSymbol
*keySymbol
;
352 OSObject
*theObject
= 0;
354 keySymbol
= OSSymbol::withCString(aKey
);
355 if (keySymbol
!= 0) {
356 theObject
= copyProperty(keySymbol
);
357 keySymbol
->release();
363 OSObject
*IODTNVRAM::getProperty(const OSSymbol
*aKey
) const
367 theObject
= copyProperty(aKey
);
368 if (theObject
) theObject
->release();
373 OSObject
*IODTNVRAM::getProperty(const char *aKey
) const
377 theObject
= copyProperty(aKey
);
378 if (theObject
) theObject
->release();
383 bool IODTNVRAM::setProperty(const OSSymbol
*aKey
, OSObject
*anObject
)
386 UInt32 propType
, propPerm
;
388 OSObject
*propObject
= 0, *oldObject
;
390 if (_ofDict
== 0) return false;
392 // Verify permissions.
393 propPerm
= getOFVariablePerm(aKey
);
394 if (IOUserClient::clientHasPrivilege(current_task(), kIONVRAMPrivilege
) != kIOReturnSuccess
) {
395 if (propPerm
!= kOFVariablePermUserWrite
) return false;
397 if (propPerm
== kOFVariablePermKernelOnly
&& current_task() != kernel_task
) return 0;
399 // Don't allow change of 'aapl,panic-info'.
400 if (aKey
->isEqualTo(kIODTNVRAMPanicInfoKey
)) return false;
403 if (current_task() != kernel_task
&&
404 mac_iokit_check_nvram_set(kauth_cred_get(), aKey
->getCStringNoCopy(), anObject
) != 0)
408 // Make sure the object is of the correct type.
409 propType
= getOFVariableType(aKey
);
411 case kOFVariableTypeBoolean
:
412 propObject
= OSDynamicCast(OSBoolean
, anObject
);
415 case kOFVariableTypeNumber
:
416 propObject
= OSDynamicCast(OSNumber
, anObject
);
419 case kOFVariableTypeString
:
420 propObject
= OSDynamicCast(OSString
, anObject
);
423 case kOFVariableTypeData
:
424 propObject
= OSDynamicCast(OSData
, anObject
);
425 if (propObject
== 0) {
426 tmpString
= OSDynamicCast(OSString
, anObject
);
427 if (tmpString
!= 0) {
428 propObject
= OSData::withBytes(tmpString
->getCStringNoCopy(),
429 tmpString
->getLength());
435 if (propObject
== 0) return false;
439 oldObject
= _ofDict
->getObject(aKey
);
443 result
= _ofDict
->setObject(aKey
, propObject
);
446 if (syncVariables() != kIOReturnSuccess
) {
448 _ofDict
->setObject(aKey
, oldObject
);
451 _ofDict
->removeObject(aKey
);
453 (void) syncVariables();
459 oldObject
->release();
462 IOLockUnlock(_ofLock
);
467 void IODTNVRAM::removeProperty(const OSSymbol
*aKey
)
472 if (_ofDict
== 0) return;
474 // Verify permissions.
475 propPerm
= getOFVariablePerm(aKey
);
476 result
= IOUserClient::clientHasPrivilege(current_task(), kIOClientPrivilegeAdministrator
);
477 if (result
!= kIOReturnSuccess
) {
478 if (propPerm
!= kOFVariablePermUserWrite
) return;
480 if (propPerm
== kOFVariablePermKernelOnly
&& current_task() != kernel_task
) return;
482 // Don't allow change of 'aapl,panic-info'.
483 if (aKey
->isEqualTo(kIODTNVRAMPanicInfoKey
)) return;
486 if (current_task() != kernel_task
&&
487 mac_iokit_check_nvram_delete(kauth_cred_get(), aKey
->getCStringNoCopy()) != 0)
491 // If the object exists, remove it from the dictionary.
494 result
= _ofDict
->getObject(aKey
) != 0;
496 _ofDict
->removeObject(aKey
);
500 (void) syncVariables();
503 IOLockUnlock(_ofLock
);
506 IOReturn
IODTNVRAM::setProperties(OSObject
*properties
)
511 const OSString
*tmpStr
;
513 OSCollectionIterator
*iter
;
515 dict
= OSDynamicCast(OSDictionary
, properties
);
516 if (dict
== 0) return kIOReturnBadArgument
;
518 iter
= OSCollectionIterator::withCollection(dict
);
519 if (iter
== 0) return kIOReturnBadArgument
;
522 key
= OSDynamicCast(OSSymbol
, iter
->getNextObject());
525 object
= dict
->getObject(key
);
526 if (object
== 0) continue;
528 if (key
->isEqualTo(kIONVRAMDeletePropertyKey
)) {
529 tmpStr
= OSDynamicCast(OSString
, object
);
531 key
= OSSymbol::withString(tmpStr
);
538 } else if(key
->isEqualTo(kIONVRAMSyncNowPropertyKey
) || key
->isEqualTo(kIONVRAMForceSyncNowPropertyKey
)) {
539 tmpStr
= OSDynamicCast(OSString
, object
);
544 // We still want to throttle NVRAM commit rate for SyncNow. ForceSyncNow is provided as a really big hammer.
546 syncInternal(key
->isEqualTo(kIONVRAMSyncNowPropertyKey
));
553 result
= setProperty(key
, object
);
560 if (result
) return kIOReturnSuccess
;
561 else return kIOReturnError
;
564 IOReturn
IODTNVRAM::readXPRAM(IOByteCount offset
, UInt8
*buffer
,
567 return kIOReturnUnsupported
;
570 IOReturn
IODTNVRAM::writeXPRAM(IOByteCount offset
, UInt8
*buffer
,
573 return kIOReturnUnsupported
;
576 IOReturn
IODTNVRAM::readNVRAMProperty(IORegistryEntry
*entry
,
577 const OSSymbol
**name
,
582 err
= readNVRAMPropertyType1(entry
, name
, value
);
587 IOReturn
IODTNVRAM::writeNVRAMProperty(IORegistryEntry
*entry
,
588 const OSSymbol
*name
,
593 err
= writeNVRAMPropertyType1(entry
, name
, value
);
598 OSDictionary
*IODTNVRAM::getNVRAMPartitions(void)
600 return _nvramPartitionLengths
;
603 IOReturn
IODTNVRAM::readNVRAMPartition(const OSSymbol
*partitionID
,
604 IOByteCount offset
, UInt8
*buffer
,
607 OSNumber
*partitionOffsetNumber
, *partitionLengthNumber
;
608 UInt32 partitionOffset
, partitionLength
, end
;
610 partitionOffsetNumber
=
611 (OSNumber
*)_nvramPartitionOffsets
->getObject(partitionID
);
612 partitionLengthNumber
=
613 (OSNumber
*)_nvramPartitionLengths
->getObject(partitionID
);
615 if ((partitionOffsetNumber
== 0) || (partitionLengthNumber
== 0))
616 return kIOReturnNotFound
;
618 partitionOffset
= partitionOffsetNumber
->unsigned32BitValue();
619 partitionLength
= partitionLengthNumber
->unsigned32BitValue();
621 if (os_add_overflow(offset
, length
, &end
)) return kIOReturnBadArgument
;
622 if ((buffer
== 0) || (length
== 0) || (end
> partitionLength
))
623 return kIOReturnBadArgument
;
625 bcopy(_nvramImage
+ partitionOffset
+ offset
, buffer
, length
);
627 return kIOReturnSuccess
;
630 IOReturn
IODTNVRAM::writeNVRAMPartition(const OSSymbol
*partitionID
,
631 IOByteCount offset
, UInt8
*buffer
,
634 OSNumber
*partitionOffsetNumber
, *partitionLengthNumber
;
635 UInt32 partitionOffset
, partitionLength
, end
;
637 partitionOffsetNumber
=
638 (OSNumber
*)_nvramPartitionOffsets
->getObject(partitionID
);
639 partitionLengthNumber
=
640 (OSNumber
*)_nvramPartitionLengths
->getObject(partitionID
);
642 if ((partitionOffsetNumber
== 0) || (partitionLengthNumber
== 0))
643 return kIOReturnNotFound
;
645 partitionOffset
= partitionOffsetNumber
->unsigned32BitValue();
646 partitionLength
= partitionLengthNumber
->unsigned32BitValue();
648 if (os_add_overflow(offset
, length
, &end
)) return kIOReturnBadArgument
;
649 if ((buffer
== 0) || (length
== 0) || (end
> partitionLength
))
650 return kIOReturnBadArgument
;
652 bcopy(buffer
, _nvramImage
+ partitionOffset
+ offset
, length
);
654 if (_nvramController
!= 0) {
655 _nvramController
->write(0, _nvramImage
, kIODTNVRAMImageSize
);
658 return kIOReturnSuccess
;
661 IOByteCount
IODTNVRAM::savePanicInfo(UInt8
*buffer
, IOByteCount length
)
663 if ((_piImage
== 0) || (length
<= 0)) return 0;
665 if (length
> (_piPartitionSize
- 4))
666 length
= _piPartitionSize
- 4;
668 // Save the Panic Info.
669 bcopy(buffer
, _piImage
+ 4, length
);
671 // Save the Panic Info length.
672 *(UInt32
*)_piImage
= length
;
674 if (_nvramController
!= 0) {
675 _nvramController
->write(0, _nvramImage
, kIODTNVRAMImageSize
);
678 * This prevents OF variables from being committed if the system has panicked
680 _systemPaniced
= true;
681 /* The call to sync() forces the NVRAM controller to write the panic info
682 * partition to NVRAM.
691 UInt8
IODTNVRAM::calculatePartitionChecksum(UInt8
*partitionHeader
)
693 UInt8 cnt
, isum
, csum
= 0;
695 for (cnt
= 0; cnt
< 0x10; cnt
++) {
696 isum
= csum
+ partitionHeader
[cnt
];
697 if (isum
< csum
) isum
++;
704 IOReturn
IODTNVRAM::initOFVariables(void)
707 UInt8
*propName
, *propData
;
708 UInt32 propNameLength
, propDataLength
;
709 const OSSymbol
*propSymbol
;
710 OSObject
*propObject
;
712 if (_ofImage
== 0) return kIOReturnNotReady
;
714 _ofDict
= OSDictionary::withCapacity(1);
715 _ofLock
= IOLockAlloc();
716 if (!_ofDict
|| !_ofLock
) return kIOReturnNoMemory
;
719 while (cnt
< _ofPartitionSize
) {
720 // Break if there is no name.
721 if (_ofImage
[cnt
] == '\0') break;
723 // Find the length of the name.
724 propName
= _ofImage
+ cnt
;
725 for (propNameLength
= 0; (cnt
+ propNameLength
) < _ofPartitionSize
;
727 if (_ofImage
[cnt
+ propNameLength
] == '=') break;
730 // Break if the name goes past the end of the partition.
731 if ((cnt
+ propNameLength
) >= _ofPartitionSize
) break;
732 cnt
+= propNameLength
+ 1;
734 propData
= _ofImage
+ cnt
;
735 for (propDataLength
= 0; (cnt
+ propDataLength
) < _ofPartitionSize
;
737 if (_ofImage
[cnt
+ propDataLength
] == '\0') break;
740 // Break if the data goes past the end of the partition.
741 if ((cnt
+ propDataLength
) >= _ofPartitionSize
) break;
742 cnt
+= propDataLength
+ 1;
744 if (convertPropToObject(propName
, propNameLength
,
745 propData
, propDataLength
,
746 &propSymbol
, &propObject
)) {
747 _ofDict
->setObject(propSymbol
, propObject
);
748 propSymbol
->release();
749 propObject
->release();
753 // Create the boot-args property if it is not in the dictionary.
754 if (_ofDict
->getObject("boot-args") == 0) {
755 propObject
= OSString::withCStringNoCopy("");
756 if (propObject
!= 0) {
757 _ofDict
->setObject("boot-args", propObject
);
758 propObject
->release();
763 propDataLength
= *(UInt32
*)_piImage
;
764 if ((propDataLength
!= 0) && (propDataLength
<= (_piPartitionSize
- 4))) {
765 propObject
= OSData::withBytes(_piImage
+ 4, propDataLength
);
766 _ofDict
->setObject(kIODTNVRAMPanicInfoKey
, propObject
);
767 propObject
->release();
769 // Clear the length from _piImage and mark dirty.
770 *(UInt32
*)_piImage
= 0;
771 if (_nvramController
!= 0) {
772 _nvramController
->write(0, _nvramImage
, kIODTNVRAMImageSize
);
777 return kIOReturnSuccess
;
780 IOReturn
IODTNVRAM::syncOFVariables(void)
782 return kIOReturnUnsupported
;
785 IOReturn
IODTNVRAM::syncVariables(void)
788 UInt32 length
, maxLength
;
789 UInt8
*buffer
, *tmpBuffer
;
790 const OSSymbol
*tmpSymbol
;
792 OSCollectionIterator
*iter
;
794 IOLockAssert(_ofLock
, kIOLockAssertOwned
);
796 if ((_ofImage
== 0) || (_ofDict
== 0) || _systemPaniced
) return kIOReturnNotReady
;
798 buffer
= tmpBuffer
= IONew(UInt8
, _ofPartitionSize
);
799 if (buffer
== 0) return kIOReturnNoMemory
;
800 bzero(buffer
, _ofPartitionSize
);
803 maxLength
= _ofPartitionSize
;
805 iter
= OSCollectionIterator::withCollection(_ofDict
);
806 if (iter
== 0) ok
= false;
809 tmpSymbol
= OSDynamicCast(OSSymbol
, iter
->getNextObject());
810 if (tmpSymbol
== 0) break;
812 // Don't save 'aapl,panic-info'.
813 if (tmpSymbol
->isEqualTo(kIODTNVRAMPanicInfoKey
)) continue;
815 tmpObject
= _ofDict
->getObject(tmpSymbol
);
818 ok
= convertObjectToProp(tmpBuffer
, &length
, tmpSymbol
, tmpObject
);
827 bcopy(buffer
, _ofImage
, _ofPartitionSize
);
830 IODelete(buffer
, UInt8
, _ofPartitionSize
);
832 if (!ok
) return kIOReturnBadArgument
;
834 if (_nvramController
!= 0) {
835 return _nvramController
->write(0, _nvramImage
, kIODTNVRAMImageSize
);
838 return kIOReturnNotReady
;
842 const char *variableName
;
845 SInt32 variableOffset
;
847 typedef struct OFVariable OFVariable
;
850 kOWVariableOffsetNumber
= 8,
851 kOWVariableOffsetString
= 17
855 OFVariable gOFVariables
[] = {
856 {"little-endian?", kOFVariableTypeBoolean
, kOFVariablePermUserRead
, 0},
857 {"real-mode?", kOFVariableTypeBoolean
, kOFVariablePermUserRead
, 1},
858 {"auto-boot?", kOFVariableTypeBoolean
, kOFVariablePermUserRead
, 2},
859 {"diag-switch?", kOFVariableTypeBoolean
, kOFVariablePermUserRead
, 3},
860 {"fcode-debug?", kOFVariableTypeBoolean
, kOFVariablePermUserRead
, 4},
861 {"oem-banner?", kOFVariableTypeBoolean
, kOFVariablePermUserRead
, 5},
862 {"oem-logo?", kOFVariableTypeBoolean
, kOFVariablePermUserRead
, 6},
863 {"use-nvramrc?", kOFVariableTypeBoolean
, kOFVariablePermUserRead
, 7},
864 {"use-generic?", kOFVariableTypeBoolean
, kOFVariablePermUserRead
, -1},
865 {"default-mac-address?", kOFVariableTypeBoolean
, kOFVariablePermUserRead
,-1},
866 {"real-base", kOFVariableTypeNumber
, kOFVariablePermUserRead
, 8},
867 {"real-size", kOFVariableTypeNumber
, kOFVariablePermUserRead
, 9},
868 {"virt-base", kOFVariableTypeNumber
, kOFVariablePermUserRead
, 10},
869 {"virt-size", kOFVariableTypeNumber
, kOFVariablePermUserRead
, 11},
870 {"load-base", kOFVariableTypeNumber
, kOFVariablePermUserRead
, 12},
871 {"pci-probe-list", kOFVariableTypeNumber
, kOFVariablePermUserRead
, 13},
872 {"pci-probe-mask", kOFVariableTypeNumber
, kOFVariablePermUserRead
, -1},
873 {"screen-#columns", kOFVariableTypeNumber
, kOFVariablePermUserRead
, 14},
874 {"screen-#rows", kOFVariableTypeNumber
, kOFVariablePermUserRead
, 15},
875 {"selftest-#megs", kOFVariableTypeNumber
, kOFVariablePermUserRead
, 16},
876 {"boot-device", kOFVariableTypeString
, kOFVariablePermUserRead
, 17},
877 {"boot-file", kOFVariableTypeString
, kOFVariablePermUserRead
, 18},
878 {"boot-screen", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
879 {"console-screen", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
880 {"diag-device", kOFVariableTypeString
, kOFVariablePermUserRead
, 19},
881 {"diag-file", kOFVariableTypeString
, kOFVariablePermUserRead
, 20},
882 {"input-device", kOFVariableTypeString
, kOFVariablePermUserRead
, 21},
883 {"output-device", kOFVariableTypeString
, kOFVariablePermUserRead
, 22},
884 {"input-device-1", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
885 {"output-device-1", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
886 {"mouse-device", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
887 {"oem-banner", kOFVariableTypeString
, kOFVariablePermUserRead
, 23},
888 {"oem-logo", kOFVariableTypeString
, kOFVariablePermUserRead
, 24},
889 {"nvramrc", kOFVariableTypeString
, kOFVariablePermUserRead
, 25},
890 {"boot-command", kOFVariableTypeString
, kOFVariablePermUserRead
, 26},
891 {"default-client-ip", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
892 {"default-server-ip", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
893 {"default-gateway-ip", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
894 {"default-subnet-mask", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
895 {"default-router-ip", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
896 {"boot-script", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
897 {"boot-args", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
898 {"aapl,pci", kOFVariableTypeData
, kOFVariablePermRootOnly
, -1},
899 {"security-mode", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
900 {"security-password", kOFVariableTypeData
, kOFVariablePermRootOnly
, -1},
901 {"boot-image", kOFVariableTypeData
, kOFVariablePermUserWrite
, -1},
902 {"com.apple.System.fp-state", kOFVariableTypeData
, kOFVariablePermKernelOnly
, -1},
904 {"backlight-level", kOFVariableTypeData
, kOFVariablePermUserWrite
, -1},
905 {"com.apple.System.sep.art", kOFVariableTypeData
, kOFVariablePermKernelOnly
, -1},
906 {"com.apple.System.boot-nonce", kOFVariableTypeString
, kOFVariablePermKernelOnly
, -1},
907 {"darkboot", kOFVariableTypeBoolean
, kOFVariablePermUserWrite
, -1},
908 {"acc-mb-ld-lifetime", kOFVariableTypeNumber
, kOFVariablePermKernelOnly
, -1},
909 {"acc-cm-override-charger-count", kOFVariableTypeNumber
, kOFVariablePermKernelOnly
, -1},
910 {"acc-cm-override-count", kOFVariableTypeNumber
, kOFVariablePermKernelOnly
, -1},
911 {"enter-tdm-mode", kOFVariableTypeBoolean
, kOFVariablePermUserWrite
, -1},
913 {0, kOFVariableTypeData
, kOFVariablePermUserRead
, -1}
916 UInt32
IODTNVRAM::getOFVariableType(const OSSymbol
*propSymbol
) const
918 const OFVariable
*ofVar
;
920 ofVar
= gOFVariables
;
922 if ((ofVar
->variableName
== 0) ||
923 propSymbol
->isEqualTo(ofVar
->variableName
)) break;
927 return ofVar
->variableType
;
930 UInt32
IODTNVRAM::getOFVariablePerm(const OSSymbol
*propSymbol
) const
932 const OFVariable
*ofVar
;
934 ofVar
= gOFVariables
;
936 if ((ofVar
->variableName
== 0) ||
937 propSymbol
->isEqualTo(ofVar
->variableName
)) break;
941 return ofVar
->variablePerm
;
944 bool IODTNVRAM::getOWVariableInfo(UInt32 variableNumber
, const OSSymbol
**propSymbol
,
945 UInt32
*propType
, UInt32
*propOffset
)
947 const OFVariable
*ofVar
;
949 ofVar
= gOFVariables
;
951 if (ofVar
->variableName
== 0) return false;
953 if (ofVar
->variableOffset
== (SInt32
) variableNumber
) break;
958 *propSymbol
= OSSymbol::withCStringNoCopy(ofVar
->variableName
);
959 *propType
= ofVar
->variableType
;
962 case kOFVariableTypeBoolean
:
963 *propOffset
= 1 << (31 - variableNumber
);
966 case kOFVariableTypeNumber
:
967 *propOffset
= variableNumber
- kOWVariableOffsetNumber
;
970 case kOFVariableTypeString
:
971 *propOffset
= variableNumber
- kOWVariableOffsetString
;
978 bool IODTNVRAM::convertPropToObject(UInt8
*propName
, UInt32 propNameLength
,
979 UInt8
*propData
, UInt32 propDataLength
,
980 const OSSymbol
**propSymbol
,
981 OSObject
**propObject
)
984 const OSSymbol
*tmpSymbol
;
989 // Create the symbol.
990 propName
[propNameLength
] = '\0';
991 tmpSymbol
= OSSymbol::withCString((const char *)propName
);
992 propName
[propNameLength
] = '=';
993 if (tmpSymbol
== 0) {
997 propType
= getOFVariableType(tmpSymbol
);
999 // Create the object.
1002 case kOFVariableTypeBoolean
:
1003 if (!strncmp("true", (const char *)propData
, propDataLength
)) {
1004 tmpObject
= kOSBooleanTrue
;
1005 } else if (!strncmp("false", (const char *)propData
, propDataLength
)) {
1006 tmpObject
= kOSBooleanFalse
;
1010 case kOFVariableTypeNumber
:
1011 tmpNumber
= OSNumber::withNumber(strtol((const char *)propData
, 0, 0), 32);
1012 if (tmpNumber
!= 0) tmpObject
= tmpNumber
;
1015 case kOFVariableTypeString
:
1016 tmpString
= OSString::withCString((const char *)propData
);
1017 if (tmpString
!= 0) tmpObject
= tmpString
;
1020 case kOFVariableTypeData
:
1021 tmpObject
= unescapeBytesToData(propData
, propDataLength
);
1025 if (tmpObject
== 0) {
1026 tmpSymbol
->release();
1030 *propSymbol
= tmpSymbol
;
1031 *propObject
= tmpObject
;
1036 bool IODTNVRAM::convertObjectToProp(UInt8
*buffer
, UInt32
*length
,
1037 const OSSymbol
*propSymbol
, OSObject
*propObject
)
1039 const UInt8
*propName
;
1040 UInt32 propNameLength
, propDataLength
, remaining
;
1041 UInt32 propType
, tmpValue
;
1042 OSBoolean
*tmpBoolean
= 0;
1043 OSNumber
*tmpNumber
= 0;
1044 OSString
*tmpString
= 0;
1045 OSData
*tmpData
= 0;
1047 propName
= (const UInt8
*)propSymbol
->getCStringNoCopy();
1048 propNameLength
= propSymbol
->getLength();
1049 propType
= getOFVariableType(propSymbol
);
1051 // Get the size of the data.
1052 propDataLength
= 0xFFFFFFFF;
1054 case kOFVariableTypeBoolean
:
1055 tmpBoolean
= OSDynamicCast(OSBoolean
, propObject
);
1056 if (tmpBoolean
!= 0) propDataLength
= 5;
1059 case kOFVariableTypeNumber
:
1060 tmpNumber
= OSDynamicCast(OSNumber
, propObject
);
1061 if (tmpNumber
!= 0) propDataLength
= 10;
1064 case kOFVariableTypeString
:
1065 tmpString
= OSDynamicCast(OSString
, propObject
);
1066 if (tmpString
!= 0) propDataLength
= tmpString
->getLength();
1069 case kOFVariableTypeData
:
1070 tmpData
= OSDynamicCast(OSData
, propObject
);
1072 tmpData
= escapeDataToData(tmpData
);
1073 propDataLength
= tmpData
->getLength();
1078 // Make sure the propertySize is known and will fit.
1079 if (propDataLength
== 0xFFFFFFFF) return false;
1080 if ((propNameLength
+ propDataLength
+ 2) > *length
) return false;
1082 // Copy the property name equal sign.
1083 buffer
+= snprintf((char *)buffer
, *length
, "%s=", propName
);
1084 remaining
= *length
- propNameLength
- 1;
1087 case kOFVariableTypeBoolean
:
1088 if (tmpBoolean
->getValue()) {
1089 strlcpy((char *)buffer
, "true", remaining
);
1091 strlcpy((char *)buffer
, "false", remaining
);
1095 case kOFVariableTypeNumber
:
1096 tmpValue
= tmpNumber
->unsigned32BitValue();
1097 if (tmpValue
== 0xFFFFFFFF) {
1098 strlcpy((char *)buffer
, "-1", remaining
);
1099 } else if (tmpValue
< 1000) {
1100 snprintf((char *)buffer
, remaining
, "%d", (uint32_t)tmpValue
);
1102 snprintf((char *)buffer
, remaining
, "0x%x", (uint32_t)tmpValue
);
1106 case kOFVariableTypeString
:
1107 strlcpy((char *)buffer
, tmpString
->getCStringNoCopy(), remaining
);
1110 case kOFVariableTypeData
:
1111 bcopy(tmpData
->getBytesNoCopy(), buffer
, propDataLength
);
1116 propDataLength
= strlen((const char *)buffer
);
1118 *length
= propNameLength
+ propDataLength
+ 2;
1124 UInt16
IODTNVRAM::generateOWChecksum(UInt8
*buffer
)
1126 UInt32 cnt
, checksum
= 0;
1127 UInt16
*tmpBuffer
= (UInt16
*)buffer
;
1129 for (cnt
= 0; cnt
< _ofPartitionSize
/ 2; cnt
++)
1130 checksum
+= tmpBuffer
[cnt
];
1132 return checksum
% 0x0000FFFF;
1135 bool IODTNVRAM::validateOWChecksum(UInt8
*buffer
)
1137 UInt32 cnt
, checksum
, sum
= 0;
1138 UInt16
*tmpBuffer
= (UInt16
*)buffer
;
1140 for (cnt
= 0; cnt
< _ofPartitionSize
/ 2; cnt
++)
1141 sum
+= tmpBuffer
[cnt
];
1143 checksum
= (sum
>> 16) + (sum
& 0x0000FFFF);
1144 if (checksum
== 0x10000) checksum
--;
1145 checksum
= (checksum
^ 0x0000FFFF) & 0x0000FFFF;
1147 return checksum
== 0;
1150 void IODTNVRAM::updateOWBootArgs(const OSSymbol
*key
, OSObject
*value
)
1152 bool wasBootArgs
, bootr
= false;
1154 OSString
*tmpString
, *bootCommand
, *bootArgs
= 0;
1155 const UInt8
*bootCommandData
, *bootArgsData
;
1157 UInt32 bootCommandDataLength
, bootArgsDataLength
, tmpDataLength
;
1159 tmpString
= OSDynamicCast(OSString
, value
);
1160 if (tmpString
== 0) return;
1162 if (key
->isEqualTo("boot-command")) {
1163 wasBootArgs
= false;
1164 bootCommand
= tmpString
;
1165 } else if (key
->isEqualTo("boot-args")) {
1167 bootArgs
= tmpString
;
1168 bootCommand
= OSDynamicCast(OSString
, _ofDict
->getObject("boot-command"));
1169 if (bootCommand
== 0) return;
1172 bootCommandData
= (const UInt8
*)bootCommand
->getCStringNoCopy();
1173 bootCommandDataLength
= bootCommand
->getLength();
1175 if (bootCommandData
== 0) return;
1177 for (cnt
= 0; cnt
< bootCommandDataLength
; cnt
++) {
1178 if ((bootCommandData
[cnt
] == 'b') &&
1179 !strncmp("bootr", (const char *)bootCommandData
+ cnt
, 5)) {
1181 while (bootCommandData
[cnt
] == ' ') cnt
++;
1187 _ofDict
->removeObject("boot-args");
1192 bootArgsData
= (const UInt8
*)bootArgs
->getCStringNoCopy();
1193 bootArgsDataLength
= bootArgs
->getLength();
1194 if (bootArgsData
== 0) return;
1196 tmpDataLength
= cnt
+ bootArgsDataLength
;
1197 tmpData
= IONew(UInt8
, tmpDataLength
+ 1);
1198 if (tmpData
== 0) return;
1200 cnt
-= strlcpy((char *)tmpData
, (const char *)bootCommandData
, cnt
);
1201 strlcat((char *)tmpData
, (const char *)bootArgsData
, cnt
);
1203 bootCommand
= OSString::withCString((const char *)tmpData
);
1204 if (bootCommand
!= 0) {
1205 _ofDict
->setObject("boot-command", bootCommand
);
1206 bootCommand
->release();
1209 IODelete(tmpData
, UInt8
, tmpDataLength
+ 1);
1211 bootArgs
= OSString::withCString((const char *)(bootCommandData
+ cnt
));
1212 if (bootArgs
!= 0) {
1213 _ofDict
->setObject("boot-args", bootArgs
);
1214 bootArgs
->release();
1219 bool IODTNVRAM::searchNVRAMProperty(IONVRAMDescriptor
*hdr
, UInt32
*where
)
1224 IOReturn
IODTNVRAM::readNVRAMPropertyType0(IORegistryEntry
*entry
,
1225 const OSSymbol
**name
,
1228 return kIOReturnUnsupported
;
1232 IOReturn
IODTNVRAM::writeNVRAMPropertyType0(IORegistryEntry
*entry
,
1233 const OSSymbol
*name
,
1236 return kIOReturnUnsupported
;
1240 OSData
*IODTNVRAM::unescapeBytesToData(const UInt8
*bytes
, UInt32 length
)
1243 UInt32 totalLength
= 0;
1248 // Calculate the actual length of the data.
1251 for (cnt
= 0; cnt
< length
;) {
1252 byte
= bytes
[cnt
++];
1254 byte
= bytes
[cnt
++];
1262 totalLength
+= cnt2
;
1266 // Create an empty OSData of the correct size.
1267 data
= OSData::withCapacity(totalLength
);
1269 for (cnt
= 0; cnt
< length
;) {
1270 byte
= bytes
[cnt
++];
1272 byte
= bytes
[cnt
++];
1274 byte
= (byte
& 0x80) ? 0xFF : 0x00;
1277 data
->appendByte(byte
, cnt2
);
1285 OSData
* IODTNVRAM::escapeDataToData(OSData
* value
)
1288 const UInt8
* startPtr
;
1289 const UInt8
* endPtr
;
1290 const UInt8
* wherePtr
;
1294 wherePtr
= (const UInt8
*) value
->getBytesNoCopy();
1295 endPtr
= wherePtr
+ value
->getLength();
1297 result
= OSData::withCapacity(endPtr
- wherePtr
);
1301 while (wherePtr
< endPtr
) {
1302 startPtr
= wherePtr
;
1304 if ((byte
== 0x00) || (byte
== 0xFF)) {
1306 ((wherePtr
- startPtr
) < 0x80) && (wherePtr
< endPtr
) && (byte
== *wherePtr
);
1308 ok
&= result
->appendByte(0xff, 1);
1309 byte
= (byte
& 0x80) | (wherePtr
- startPtr
);
1311 ok
&= result
->appendByte(byte
, 1);
1313 ok
&= result
->appendByte(0, 1);
1323 static bool IsApplePropertyName(const char * propName
)
1326 while ((c
= *propName
++)) {
1327 if ((c
>= 'A') && (c
<= 'Z'))
1334 IOReturn
IODTNVRAM::readNVRAMPropertyType1(IORegistryEntry
*entry
,
1335 const OSSymbol
**name
,
1338 IOReturn err
= kIOReturnNoResources
;
1340 const UInt8
*startPtr
;
1341 const UInt8
*endPtr
;
1342 const UInt8
*wherePtr
;
1343 const UInt8
*nvPath
= 0;
1344 const char *nvName
= 0;
1345 const char *resultName
= 0;
1346 const UInt8
*resultValue
= 0;
1347 UInt32 resultValueLen
= 0;
1350 if (_ofDict
== 0) return err
;
1352 IOLockLock(_ofLock
);
1353 data
= OSDynamicCast(OSData
, _ofDict
->getObject(_registryPropertiesKey
));
1354 IOLockUnlock(_ofLock
);
1356 if (data
== 0) return err
;
1358 startPtr
= (const UInt8
*) data
->getBytesNoCopy();
1359 endPtr
= startPtr
+ data
->getLength();
1361 wherePtr
= startPtr
;
1362 while (wherePtr
< endPtr
) {
1363 byte
= *(wherePtr
++);
1369 else if (nvName
== 0)
1370 nvName
= (const char *) startPtr
;
1372 IORegistryEntry
* compareEntry
= IORegistryEntry::fromPath((const char *) nvPath
, gIODTPlane
);
1374 compareEntry
->release();
1375 if (entry
== compareEntry
) {
1376 bool appleProp
= IsApplePropertyName(nvName
);
1377 if (!appleProp
|| !resultName
) {
1378 resultName
= nvName
;
1379 resultValue
= startPtr
;
1380 resultValueLen
= wherePtr
- startPtr
- 1;
1388 startPtr
= wherePtr
;
1391 *name
= OSSymbol::withCString(resultName
);
1392 *value
= unescapeBytesToData(resultValue
, resultValueLen
);
1393 if ((*name
!= 0) && (*value
!= 0))
1394 err
= kIOReturnSuccess
;
1396 err
= kIOReturnNoMemory
;
1401 IOReturn
IODTNVRAM::writeNVRAMPropertyType1(IORegistryEntry
*entry
,
1402 const OSSymbol
*propName
,
1405 OSData
*oldData
, *escapedData
;
1407 const UInt8
*startPtr
;
1408 const UInt8
*propStart
;
1409 const UInt8
*endPtr
;
1410 const UInt8
*wherePtr
;
1411 const UInt8
*nvPath
= 0;
1412 const char *nvName
= 0;
1417 bool settingAppleProp
;
1419 if (_ofDict
== 0) return kIOReturnNoResources
;
1421 settingAppleProp
= IsApplePropertyName(propName
->getCStringNoCopy());
1423 // copy over existing properties for other entries
1425 IOLockLock(_ofLock
);
1427 oldData
= OSDynamicCast(OSData
, _ofDict
->getObject(_registryPropertiesKey
));
1430 startPtr
= (const UInt8
*) oldData
->getBytesNoCopy();
1431 endPtr
= startPtr
+ oldData
->getLength();
1433 propStart
= startPtr
;
1434 wherePtr
= startPtr
;
1435 while (wherePtr
< endPtr
) {
1436 byte
= *(wherePtr
++);
1441 else if (nvName
== 0)
1442 nvName
= (const char *) startPtr
;
1444 IORegistryEntry
* compareEntry
= IORegistryEntry::fromPath((const char *) nvPath
, gIODTPlane
);
1446 compareEntry
->release();
1447 if (entry
== compareEntry
) {
1448 if ((settingAppleProp
&& propName
->isEqualTo(nvName
))
1449 || (!settingAppleProp
&& !IsApplePropertyName(nvName
))) {
1450 // delete old property (nvPath -> wherePtr)
1451 data
= OSData::withBytes(propStart
, nvPath
- propStart
);
1453 ok
&= data
->appendBytes(wherePtr
, endPtr
- wherePtr
);
1461 startPtr
= wherePtr
;
1465 // make the new property
1469 data
= OSData::withData(oldData
);
1471 data
= OSData::withCapacity(16);
1472 if (!data
) ok
= false;
1475 if (ok
&& value
&& value
->getLength()) do {
1476 // get entries in path
1477 OSArray
*array
= OSArray::withCapacity(5);
1483 array
->setObject(entry
);
1484 while ((entry
= entry
->getParentEntry(gIODTPlane
)));
1487 for (int i
= array
->getCount() - 3;
1488 (entry
= (IORegistryEntry
*) array
->getObject(i
));
1491 name
= entry
->getName(gIODTPlane
);
1492 comp
= entry
->getLocation(gIODTPlane
);
1493 if (comp
) ok
&= data
->appendBytes("/@", 2);
1495 if (!name
) continue;
1496 ok
&= data
->appendByte('/', 1);
1499 ok
&= data
->appendBytes(comp
, strlen(comp
));
1501 ok
&= data
->appendByte(0, 1);
1505 ok
&= data
->appendBytes(propName
->getCStringNoCopy(), propName
->getLength() + 1);
1507 // append escaped data
1508 escapedData
= escapeDataToData(value
);
1509 ok
&= (escapedData
!= 0);
1510 if (ok
) ok
&= data
->appendBytes(escapedData
);
1516 ok
= _ofDict
->setObject(_registryPropertiesKey
, data
);
1519 if (data
) data
->release();
1522 if (syncVariables() != kIOReturnSuccess
) {
1524 _ofDict
->setObject(_registryPropertiesKey
, oldData
);
1527 _ofDict
->removeObject(_registryPropertiesKey
);
1529 (void) syncVariables();
1538 IOLockUnlock(_ofLock
);
1540 return ok
? kIOReturnSuccess
: kIOReturnNoMemory
;
1543 bool IODTNVRAM::safeToSync(void)
1549 // delta interval went by
1550 clock_get_uptime(&delta
);
1552 // Figure it in seconds.
1553 absolutetime_to_nanoseconds(delta
, &delta_ns
);
1554 delta_secs
= (SInt32
)(delta_ns
/ NSEC_PER_SEC
);
1556 if ((delta_secs
> (_lastDeviceSync
+ MIN_SYNC_NOW_INTERVAL
)) || _freshInterval
)
1558 _lastDeviceSync
= delta_secs
;
1559 _freshInterval
= FALSE
;