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();
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
);
125 void IODTNVRAM::initNVRAMImage(void)
127 char partitionID
[18];
128 UInt32 partitionOffset
, partitionLength
;
129 UInt32 freePartitionOffset
, freePartitionSize
;
130 UInt32 currentLength
, currentOffset
= 0;
131 OSNumber
*partitionOffsetNumber
, *partitionLengthNumber
;
133 // Find the offsets for the OF, XPRAM, NameRegistry and PanicInfo partitions.
134 _ofPartitionOffset
= 0xFFFFFFFF;
135 _piPartitionOffset
= 0xFFFFFFFF;
136 freePartitionOffset
= 0xFFFFFFFF;
137 freePartitionSize
= 0;
139 // Look through the partitions to find the OF, MacOS partitions.
140 while (currentOffset
< kIODTNVRAMImageSize
) {
141 currentLength
= ((UInt16
*)(_nvramImage
+ currentOffset
))[1] * 16;
143 partitionOffset
= currentOffset
+ 16;
144 partitionLength
= currentLength
- 16;
146 if (strncmp((const char *)_nvramImage
+ currentOffset
+ 4,
147 kIODTNVRAMOFPartitionName
, 12) == 0) {
148 _ofPartitionOffset
= partitionOffset
;
149 _ofPartitionSize
= partitionLength
;
150 } else if (strncmp((const char *)_nvramImage
+ currentOffset
+ 4,
151 kIODTNVRAMXPRAMPartitionName
, 12) == 0) {
152 } else if (strncmp((const char *)_nvramImage
+ currentOffset
+ 4,
153 kIODTNVRAMPanicInfoPartitonName
, 12) == 0) {
154 _piPartitionOffset
= partitionOffset
;
155 _piPartitionSize
= partitionLength
;
156 } else if (strncmp((const char *)_nvramImage
+ currentOffset
+ 4,
157 kIODTNVRAMFreePartitionName
, 12) == 0) {
158 freePartitionOffset
= currentOffset
;
159 freePartitionSize
= currentLength
;
161 // Construct the partition ID from the signature and name.
162 snprintf(partitionID
, sizeof(partitionID
), "0x%02x,",
163 *(UInt8
*)(_nvramImage
+ currentOffset
));
164 strncpy(partitionID
+ 5,
165 (const char *)(_nvramImage
+ currentOffset
+ 4), 12);
166 partitionID
[17] = '\0';
168 partitionOffsetNumber
= OSNumber::withNumber(partitionOffset
, 32);
169 partitionLengthNumber
= OSNumber::withNumber(partitionLength
, 32);
171 // Save the partition offset and length
172 _nvramPartitionOffsets
->setObject(partitionID
, partitionOffsetNumber
);
173 _nvramPartitionLengths
->setObject(partitionID
, partitionLengthNumber
);
175 partitionOffsetNumber
->release();
176 partitionLengthNumber
->release();
178 currentOffset
+= currentLength
;
181 if (_ofPartitionOffset
!= 0xFFFFFFFF)
182 _ofImage
= _nvramImage
+ _ofPartitionOffset
;
184 if (_piPartitionOffset
== 0xFFFFFFFF) {
185 if (freePartitionSize
> 0x20) {
186 // Set the signature to 0xa1.
187 _nvramImage
[freePartitionOffset
] = 0xa1;
188 // Set the checksum to 0.
189 _nvramImage
[freePartitionOffset
+ 1] = 0;
190 // Set the name for the Panic Info partition.
191 strncpy((char *)(_nvramImage
+ freePartitionOffset
+ 4),
192 kIODTNVRAMPanicInfoPartitonName
, 12);
194 // Calculate the partition offset and size.
195 _piPartitionOffset
= freePartitionOffset
+ 0x10;
196 _piPartitionSize
= 0x800;
197 if (_piPartitionSize
+ 0x20 > freePartitionSize
)
198 _piPartitionSize
= freePartitionSize
- 0x20;
200 _piImage
= _nvramImage
+ _piPartitionOffset
;
202 // Zero the new partition.
203 bzero(_piImage
, _piPartitionSize
);
205 // Set the partition size.
206 *(UInt16
*)(_nvramImage
+ freePartitionOffset
+ 2) =
207 (_piPartitionSize
/ 0x10) + 1;
209 // Set the partition checksum.
210 _nvramImage
[freePartitionOffset
+ 1] =
211 calculatePartitionChecksum(_nvramImage
+ freePartitionOffset
);
213 // Calculate the free partition offset and size.
214 freePartitionOffset
+= _piPartitionSize
+ 0x10;
215 freePartitionSize
-= _piPartitionSize
+ 0x10;
217 // Set the signature to 0x7f.
218 _nvramImage
[freePartitionOffset
] = 0x7f;
219 // Set the checksum to 0.
220 _nvramImage
[freePartitionOffset
+ 1] = 0;
221 // Set the name for the free partition.
222 strncpy((char *)(_nvramImage
+ freePartitionOffset
+ 4),
223 kIODTNVRAMFreePartitionName
, 12);
224 // Set the partition size.
225 *(UInt16
*)(_nvramImage
+ freePartitionOffset
+ 2) =
226 freePartitionSize
/ 0x10;
227 // Set the partition checksum.
228 _nvramImage
[freePartitionOffset
+ 1] =
229 calculatePartitionChecksum(_nvramImage
+ freePartitionOffset
);
231 if (_nvramController
!= 0) {
232 _nvramController
->write(0, _nvramImage
, kIODTNVRAMImageSize
);
236 _piImage
= _nvramImage
+ _piPartitionOffset
;
240 _freshInterval
= TRUE
; // we will allow sync() even before the first 15 minutes have passed.
245 void IODTNVRAM::syncInternal(bool rateLimit
)
247 // Don't try to perform controller operations if none has been registered.
248 if (_nvramController
== 0) return;
250 // Rate limit requests to sync. Drivers that need this rate limiting will
251 // shadow the data and only write to flash when they get a sync call
252 if (rateLimit
&& !safeToSync()) return;
254 _nvramController
->sync();
257 void IODTNVRAM::sync(void)
262 bool IODTNVRAM::serializeProperties(OSSerialize
*s
) const
264 bool result
, hasPrivilege
;
268 OSCollectionIterator
*iter
= 0;
270 // Verify permissions.
271 hasPrivilege
= (kIOReturnSuccess
== IOUserClient::clientHasPrivilege(current_task(), kIONVRAMPrivilege
));
274 /* No nvram. Return an empty dictionary. */
275 dict
= OSDictionary::withCapacity(1);
276 if (dict
== 0) return false;
279 dict
= OSDictionary::withDictionary(_ofDict
);
280 IOLockUnlock(_ofLock
);
281 if (dict
== 0) return false;
283 /* Copy properties with client privilege. */
284 iter
= OSCollectionIterator::withCollection(dict
);
290 key
= OSDynamicCast(OSSymbol
, iter
->getNextObject());
293 variablePerm
= getOFVariablePerm(key
);
294 if ((hasPrivilege
|| (variablePerm
!= kOFVariablePermRootOnly
)) &&
295 ( ! (variablePerm
== kOFVariablePermKernelOnly
&& current_task() != kernel_task
) )
297 && (current_task() == kernel_task
|| mac_iokit_check_nvram_get(kauth_cred_get(), key
->getCStringNoCopy()) == 0)
300 else dict
->removeObject(key
);
304 result
= dict
->serialize(s
);
307 if (iter
!= 0) iter
->release();
312 OSObject
*IODTNVRAM::copyProperty(const OSSymbol
*aKey
) const
318 if (_ofDict
== 0) return 0;
320 // Verify permissions.
321 variablePerm
= getOFVariablePerm(aKey
);
322 result
= IOUserClient::clientHasPrivilege(current_task(), kIONVRAMPrivilege
);
323 if (result
!= kIOReturnSuccess
) {
324 if (variablePerm
== kOFVariablePermRootOnly
) return 0;
326 if (variablePerm
== kOFVariablePermKernelOnly
&& current_task() != kernel_task
) return 0;
329 if (current_task() != kernel_task
&&
330 mac_iokit_check_nvram_get(kauth_cred_get(), aKey
->getCStringNoCopy()) != 0)
335 theObject
= _ofDict
->getObject(aKey
);
336 if (theObject
) theObject
->retain();
337 IOLockUnlock(_ofLock
);
342 OSObject
*IODTNVRAM::copyProperty(const char *aKey
) const
344 const OSSymbol
*keySymbol
;
345 OSObject
*theObject
= 0;
347 keySymbol
= OSSymbol::withCString(aKey
);
348 if (keySymbol
!= 0) {
349 theObject
= copyProperty(keySymbol
);
350 keySymbol
->release();
356 OSObject
*IODTNVRAM::getProperty(const OSSymbol
*aKey
) const
360 theObject
= copyProperty(aKey
);
361 if (theObject
) theObject
->release();
366 OSObject
*IODTNVRAM::getProperty(const char *aKey
) const
370 theObject
= copyProperty(aKey
);
371 if (theObject
) theObject
->release();
376 bool IODTNVRAM::setProperty(const OSSymbol
*aKey
, OSObject
*anObject
)
379 UInt32 propType
, propPerm
;
381 OSObject
*propObject
= 0;
383 if (_ofDict
== 0) return false;
385 // Verify permissions.
386 propPerm
= getOFVariablePerm(aKey
);
387 result
= IOUserClient::clientHasPrivilege(current_task(), kIONVRAMPrivilege
);
388 if (result
!= kIOReturnSuccess
) {
389 if (propPerm
!= kOFVariablePermUserWrite
) return false;
391 if (propPerm
== kOFVariablePermKernelOnly
&& current_task() != kernel_task
) return 0;
393 // Don't allow change of 'aapl,panic-info'.
394 if (aKey
->isEqualTo(kIODTNVRAMPanicInfoKey
)) return false;
397 if (current_task() != kernel_task
&&
398 mac_iokit_check_nvram_set(kauth_cred_get(), aKey
->getCStringNoCopy(), anObject
) != 0)
402 // Make sure the object is of the correct type.
403 propType
= getOFVariableType(aKey
);
405 case kOFVariableTypeBoolean
:
406 propObject
= OSDynamicCast(OSBoolean
, anObject
);
409 case kOFVariableTypeNumber
:
410 propObject
= OSDynamicCast(OSNumber
, anObject
);
413 case kOFVariableTypeString
:
414 propObject
= OSDynamicCast(OSString
, anObject
);
417 case kOFVariableTypeData
:
418 propObject
= OSDynamicCast(OSData
, anObject
);
419 if (propObject
== 0) {
420 tmpString
= OSDynamicCast(OSString
, anObject
);
421 if (tmpString
!= 0) {
422 propObject
= OSData::withBytes(tmpString
->getCStringNoCopy(),
423 tmpString
->getLength());
429 if (propObject
== 0) return false;
432 result
= _ofDict
->setObject(aKey
, propObject
);
433 IOLockUnlock(_ofLock
);
442 void IODTNVRAM::removeProperty(const OSSymbol
*aKey
)
447 if (_ofDict
== 0) return;
449 // Verify permissions.
450 propPerm
= getOFVariablePerm(aKey
);
451 result
= IOUserClient::clientHasPrivilege(current_task(), kIOClientPrivilegeAdministrator
);
452 if (result
!= kIOReturnSuccess
) {
453 if (propPerm
!= kOFVariablePermUserWrite
) return;
455 if (propPerm
== kOFVariablePermKernelOnly
&& current_task() != kernel_task
) return;
457 // Don't allow change of 'aapl,panic-info'.
458 if (aKey
->isEqualTo(kIODTNVRAMPanicInfoKey
)) return;
461 if (current_task() != kernel_task
&&
462 mac_iokit_check_nvram_delete(kauth_cred_get(), aKey
->getCStringNoCopy()) != 0)
466 // If the object exists, remove it from the dictionary.
469 result
= _ofDict
->getObject(aKey
) != 0;
471 _ofDict
->removeObject(aKey
);
473 IOLockUnlock(_ofLock
);
480 IOReturn
IODTNVRAM::setProperties(OSObject
*properties
)
485 const OSString
*tmpStr
;
487 OSCollectionIterator
*iter
;
489 dict
= OSDynamicCast(OSDictionary
, properties
);
490 if (dict
== 0) return kIOReturnBadArgument
;
492 iter
= OSCollectionIterator::withCollection(dict
);
493 if (iter
== 0) return kIOReturnBadArgument
;
496 key
= OSDynamicCast(OSSymbol
, iter
->getNextObject());
499 object
= dict
->getObject(key
);
500 if (object
== 0) continue;
502 if (key
->isEqualTo(kIONVRAMDeletePropertyKey
)) {
503 tmpStr
= OSDynamicCast(OSString
, object
);
505 key
= OSSymbol::withString(tmpStr
);
512 } else if(key
->isEqualTo(kIONVRAMSyncNowPropertyKey
) || key
->isEqualTo(kIONVRAMForceSyncNowPropertyKey
)) {
513 tmpStr
= OSDynamicCast(OSString
, object
);
518 // We still want to throttle NVRAM commit rate for SyncNow. ForceSyncNow is provided as a really big hammer.
520 syncInternal(key
->isEqualTo(kIONVRAMSyncNowPropertyKey
));
527 result
= setProperty(key
, object
);
534 if (result
) return kIOReturnSuccess
;
535 else return kIOReturnError
;
538 IOReturn
IODTNVRAM::readXPRAM(IOByteCount offset
, UInt8
*buffer
,
541 return kIOReturnUnsupported
;
544 IOReturn
IODTNVRAM::writeXPRAM(IOByteCount offset
, UInt8
*buffer
,
547 return kIOReturnUnsupported
;
550 IOReturn
IODTNVRAM::readNVRAMProperty(IORegistryEntry
*entry
,
551 const OSSymbol
**name
,
556 err
= readNVRAMPropertyType1(entry
, name
, value
);
561 IOReturn
IODTNVRAM::writeNVRAMProperty(IORegistryEntry
*entry
,
562 const OSSymbol
*name
,
567 err
= writeNVRAMPropertyType1(entry
, name
, value
);
572 OSDictionary
*IODTNVRAM::getNVRAMPartitions(void)
574 return _nvramPartitionLengths
;
577 IOReturn
IODTNVRAM::readNVRAMPartition(const OSSymbol
*partitionID
,
578 IOByteCount offset
, UInt8
*buffer
,
581 OSNumber
*partitionOffsetNumber
, *partitionLengthNumber
;
582 UInt32 partitionOffset
, partitionLength
;
584 partitionOffsetNumber
=
585 (OSNumber
*)_nvramPartitionOffsets
->getObject(partitionID
);
586 partitionLengthNumber
=
587 (OSNumber
*)_nvramPartitionLengths
->getObject(partitionID
);
589 if ((partitionOffsetNumber
== 0) || (partitionLengthNumber
== 0))
590 return kIOReturnNotFound
;
592 partitionOffset
= partitionOffsetNumber
->unsigned32BitValue();
593 partitionLength
= partitionLengthNumber
->unsigned32BitValue();
595 if ((buffer
== 0) || (length
== 0) ||
596 (offset
+ length
> partitionLength
))
597 return kIOReturnBadArgument
;
599 bcopy(_nvramImage
+ partitionOffset
+ offset
, buffer
, length
);
601 return kIOReturnSuccess
;
604 IOReturn
IODTNVRAM::writeNVRAMPartition(const OSSymbol
*partitionID
,
605 IOByteCount offset
, UInt8
*buffer
,
608 OSNumber
*partitionOffsetNumber
, *partitionLengthNumber
;
609 UInt32 partitionOffset
, partitionLength
;
611 partitionOffsetNumber
=
612 (OSNumber
*)_nvramPartitionOffsets
->getObject(partitionID
);
613 partitionLengthNumber
=
614 (OSNumber
*)_nvramPartitionLengths
->getObject(partitionID
);
616 if ((partitionOffsetNumber
== 0) || (partitionLengthNumber
== 0))
617 return kIOReturnNotFound
;
619 partitionOffset
= partitionOffsetNumber
->unsigned32BitValue();
620 partitionLength
= partitionLengthNumber
->unsigned32BitValue();
622 if ((buffer
== 0) || (length
== 0) ||
623 (offset
+ length
> partitionLength
))
624 return kIOReturnBadArgument
;
626 bcopy(buffer
, _nvramImage
+ partitionOffset
+ offset
, length
);
628 if (_nvramController
!= 0) {
629 _nvramController
->write(0, _nvramImage
, kIODTNVRAMImageSize
);
632 return kIOReturnSuccess
;
635 IOByteCount
IODTNVRAM::savePanicInfo(UInt8
*buffer
, IOByteCount length
)
637 if ((_piImage
== 0) || (length
<= 0)) return 0;
639 if (length
> (_piPartitionSize
- 4))
640 length
= _piPartitionSize
- 4;
642 // Save the Panic Info.
643 bcopy(buffer
, _piImage
+ 4, length
);
645 // Save the Panic Info length.
646 *(UInt32
*)_piImage
= length
;
648 if (_nvramController
!= 0) {
649 _nvramController
->write(0, _nvramImage
, kIODTNVRAMImageSize
);
652 * This prevents OF variables from being committed if the system has panicked
654 _systemPaniced
= true;
655 /* The call to sync() forces the NVRAM controller to write the panic info
656 * partition to NVRAM.
665 UInt8
IODTNVRAM::calculatePartitionChecksum(UInt8
*partitionHeader
)
667 UInt8 cnt
, isum
, csum
= 0;
669 for (cnt
= 0; cnt
< 0x10; cnt
++) {
670 isum
= csum
+ partitionHeader
[cnt
];
671 if (isum
< csum
) isum
++;
678 IOReturn
IODTNVRAM::initOFVariables(void)
681 UInt8
*propName
, *propData
;
682 UInt32 propNameLength
, propDataLength
;
683 const OSSymbol
*propSymbol
;
684 OSObject
*propObject
;
686 if (_ofImage
== 0) return kIOReturnNotReady
;
688 _ofDict
= OSDictionary::withCapacity(1);
689 _ofLock
= IOLockAlloc();
690 if (!_ofDict
|| !_ofLock
) return kIOReturnNoMemory
;
693 while (cnt
< _ofPartitionSize
) {
694 // Break if there is no name.
695 if (_ofImage
[cnt
] == '\0') break;
697 // Find the length of the name.
698 propName
= _ofImage
+ cnt
;
699 for (propNameLength
= 0; (cnt
+ propNameLength
) < _ofPartitionSize
;
701 if (_ofImage
[cnt
+ propNameLength
] == '=') break;
704 // Break if the name goes past the end of the partition.
705 if ((cnt
+ propNameLength
) >= _ofPartitionSize
) break;
706 cnt
+= propNameLength
+ 1;
708 propData
= _ofImage
+ cnt
;
709 for (propDataLength
= 0; (cnt
+ propDataLength
) < _ofPartitionSize
;
711 if (_ofImage
[cnt
+ propDataLength
] == '\0') break;
714 // Break if the data goes past the end of the partition.
715 if ((cnt
+ propDataLength
) >= _ofPartitionSize
) break;
716 cnt
+= propDataLength
+ 1;
718 if (convertPropToObject(propName
, propNameLength
,
719 propData
, propDataLength
,
720 &propSymbol
, &propObject
)) {
721 _ofDict
->setObject(propSymbol
, propObject
);
722 propSymbol
->release();
723 propObject
->release();
727 // Create the boot-args property if it is not in the dictionary.
728 if (_ofDict
->getObject("boot-args") == 0) {
729 propObject
= OSString::withCStringNoCopy("");
730 if (propObject
!= 0) {
731 _ofDict
->setObject("boot-args", propObject
);
732 propObject
->release();
736 // Create the 'aapl,panic-info' property if needed.
738 propDataLength
= *(UInt32
*)_piImage
;
739 if ((propDataLength
!= 0) && (propDataLength
<= (_piPartitionSize
- 4))) {
740 propObject
= OSData::withBytes(_piImage
+ 4, propDataLength
);
741 _ofDict
->setObject(kIODTNVRAMPanicInfoKey
, propObject
);
742 propObject
->release();
744 // Clear the length from _piImage and mark dirty.
745 *(UInt32
*)_piImage
= 0;
746 if (_nvramController
!= 0) {
747 _nvramController
->write(0, _nvramImage
, kIODTNVRAMImageSize
);
752 return kIOReturnSuccess
;
755 IOReturn
IODTNVRAM::syncOFVariables(void)
758 UInt32 length
, maxLength
;
759 UInt8
*buffer
, *tmpBuffer
;
760 const OSSymbol
*tmpSymbol
;
762 OSCollectionIterator
*iter
;
764 if ((_ofImage
== 0) || (_ofDict
== 0) || _systemPaniced
) return kIOReturnNotReady
;
766 buffer
= tmpBuffer
= IONew(UInt8
, _ofPartitionSize
);
767 if (buffer
== 0) return kIOReturnNoMemory
;
768 bzero(buffer
, _ofPartitionSize
);
771 maxLength
= _ofPartitionSize
;
774 iter
= OSCollectionIterator::withCollection(_ofDict
);
775 if (iter
== 0) ok
= false;
778 tmpSymbol
= OSDynamicCast(OSSymbol
, iter
->getNextObject());
779 if (tmpSymbol
== 0) break;
781 // Don't save 'aapl,panic-info'.
782 if (tmpSymbol
->isEqualTo(kIODTNVRAMPanicInfoKey
)) continue;
784 tmpObject
= _ofDict
->getObject(tmpSymbol
);
787 ok
= convertObjectToProp(tmpBuffer
, &length
, tmpSymbol
, tmpObject
);
794 IOLockUnlock(_ofLock
);
797 bcopy(buffer
, _ofImage
, _ofPartitionSize
);
800 IODelete(buffer
, UInt8
, _ofPartitionSize
);
802 if (!ok
) return kIOReturnBadArgument
;
804 if (_nvramController
!= 0) {
805 _nvramController
->write(0, _nvramImage
, kIODTNVRAMImageSize
);
808 return kIOReturnSuccess
;
812 const char *variableName
;
815 SInt32 variableOffset
;
817 typedef struct OFVariable OFVariable
;
820 kOWVariableOffsetNumber
= 8,
821 kOWVariableOffsetString
= 17
824 OFVariable gOFVariables
[] = {
825 {"little-endian?", kOFVariableTypeBoolean
, kOFVariablePermUserRead
, 0},
826 {"real-mode?", kOFVariableTypeBoolean
, kOFVariablePermUserRead
, 1},
827 {"auto-boot?", kOFVariableTypeBoolean
, kOFVariablePermUserRead
, 2},
828 {"diag-switch?", kOFVariableTypeBoolean
, kOFVariablePermUserRead
, 3},
829 {"fcode-debug?", kOFVariableTypeBoolean
, kOFVariablePermUserRead
, 4},
830 {"oem-banner?", kOFVariableTypeBoolean
, kOFVariablePermUserRead
, 5},
831 {"oem-logo?", kOFVariableTypeBoolean
, kOFVariablePermUserRead
, 6},
832 {"use-nvramrc?", kOFVariableTypeBoolean
, kOFVariablePermUserRead
, 7},
833 {"use-generic?", kOFVariableTypeBoolean
, kOFVariablePermUserRead
, -1},
834 {"default-mac-address?", kOFVariableTypeBoolean
, kOFVariablePermUserRead
,-1},
835 {"real-base", kOFVariableTypeNumber
, kOFVariablePermUserRead
, 8},
836 {"real-size", kOFVariableTypeNumber
, kOFVariablePermUserRead
, 9},
837 {"virt-base", kOFVariableTypeNumber
, kOFVariablePermUserRead
, 10},
838 {"virt-size", kOFVariableTypeNumber
, kOFVariablePermUserRead
, 11},
839 {"load-base", kOFVariableTypeNumber
, kOFVariablePermUserRead
, 12},
840 {"pci-probe-list", kOFVariableTypeNumber
, kOFVariablePermUserRead
, 13},
841 {"pci-probe-mask", kOFVariableTypeNumber
, kOFVariablePermUserRead
, -1},
842 {"screen-#columns", kOFVariableTypeNumber
, kOFVariablePermUserRead
, 14},
843 {"screen-#rows", kOFVariableTypeNumber
, kOFVariablePermUserRead
, 15},
844 {"selftest-#megs", kOFVariableTypeNumber
, kOFVariablePermUserRead
, 16},
845 {"boot-device", kOFVariableTypeString
, kOFVariablePermUserRead
, 17},
846 {"boot-file", kOFVariableTypeString
, kOFVariablePermUserRead
, 18},
847 {"boot-screen", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
848 {"console-screen", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
849 {"diag-device", kOFVariableTypeString
, kOFVariablePermUserRead
, 19},
850 {"diag-file", kOFVariableTypeString
, kOFVariablePermUserRead
, 20},
851 {"input-device", kOFVariableTypeString
, kOFVariablePermUserRead
, 21},
852 {"output-device", kOFVariableTypeString
, kOFVariablePermUserRead
, 22},
853 {"input-device-1", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
854 {"output-device-1", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
855 {"mouse-device", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
856 {"oem-banner", kOFVariableTypeString
, kOFVariablePermUserRead
, 23},
857 {"oem-logo", kOFVariableTypeString
, kOFVariablePermUserRead
, 24},
858 {"nvramrc", kOFVariableTypeString
, kOFVariablePermUserRead
, 25},
859 {"boot-command", kOFVariableTypeString
, kOFVariablePermUserRead
, 26},
860 {"default-client-ip", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
861 {"default-server-ip", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
862 {"default-gateway-ip", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
863 {"default-subnet-mask", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
864 {"default-router-ip", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
865 {"boot-script", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
866 {"boot-args", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
867 {"aapl,pci", kOFVariableTypeData
, kOFVariablePermRootOnly
, -1},
868 {"security-mode", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
869 {"security-password", kOFVariableTypeData
, kOFVariablePermRootOnly
, -1},
870 {"boot-image", kOFVariableTypeData
, kOFVariablePermUserWrite
, -1},
871 {"com.apple.System.fp-state", kOFVariableTypeData
, kOFVariablePermKernelOnly
, -1},
872 {0, kOFVariableTypeData
, kOFVariablePermUserRead
, -1}
875 UInt32
IODTNVRAM::getOFVariableType(const OSSymbol
*propSymbol
) const
879 ofVar
= gOFVariables
;
881 if ((ofVar
->variableName
== 0) ||
882 propSymbol
->isEqualTo(ofVar
->variableName
)) break;
886 return ofVar
->variableType
;
889 UInt32
IODTNVRAM::getOFVariablePerm(const OSSymbol
*propSymbol
) const
893 ofVar
= gOFVariables
;
895 if ((ofVar
->variableName
== 0) ||
896 propSymbol
->isEqualTo(ofVar
->variableName
)) break;
900 return ofVar
->variablePerm
;
903 bool IODTNVRAM::getOWVariableInfo(UInt32 variableNumber
, const OSSymbol
**propSymbol
,
904 UInt32
*propType
, UInt32
*propOffset
)
908 ofVar
= gOFVariables
;
910 if (ofVar
->variableName
== 0) return false;
912 if (ofVar
->variableOffset
== (SInt32
) variableNumber
) break;
917 *propSymbol
= OSSymbol::withCStringNoCopy(ofVar
->variableName
);
918 *propType
= ofVar
->variableType
;
921 case kOFVariableTypeBoolean
:
922 *propOffset
= 1 << (31 - variableNumber
);
925 case kOFVariableTypeNumber
:
926 *propOffset
= variableNumber
- kOWVariableOffsetNumber
;
929 case kOFVariableTypeString
:
930 *propOffset
= variableNumber
- kOWVariableOffsetString
;
937 bool IODTNVRAM::convertPropToObject(UInt8
*propName
, UInt32 propNameLength
,
938 UInt8
*propData
, UInt32 propDataLength
,
939 const OSSymbol
**propSymbol
,
940 OSObject
**propObject
)
943 const OSSymbol
*tmpSymbol
;
948 // Create the symbol.
949 propName
[propNameLength
] = '\0';
950 tmpSymbol
= OSSymbol::withCString((const char *)propName
);
951 propName
[propNameLength
] = '=';
952 if (tmpSymbol
== 0) {
956 propType
= getOFVariableType(tmpSymbol
);
958 // Create the object.
961 case kOFVariableTypeBoolean
:
962 if (!strncmp("true", (const char *)propData
, propDataLength
)) {
963 tmpObject
= kOSBooleanTrue
;
964 } else if (!strncmp("false", (const char *)propData
, propDataLength
)) {
965 tmpObject
= kOSBooleanFalse
;
969 case kOFVariableTypeNumber
:
970 tmpNumber
= OSNumber::withNumber(strtol((const char *)propData
, 0, 0), 32);
971 if (tmpNumber
!= 0) tmpObject
= tmpNumber
;
974 case kOFVariableTypeString
:
975 tmpString
= OSString::withCString((const char *)propData
);
976 if (tmpString
!= 0) tmpObject
= tmpString
;
979 case kOFVariableTypeData
:
980 tmpObject
= unescapeBytesToData(propData
, propDataLength
);
984 if (tmpObject
== 0) {
985 tmpSymbol
->release();
989 *propSymbol
= tmpSymbol
;
990 *propObject
= tmpObject
;
995 bool IODTNVRAM::convertObjectToProp(UInt8
*buffer
, UInt32
*length
,
996 const OSSymbol
*propSymbol
, OSObject
*propObject
)
998 const UInt8
*propName
;
999 UInt32 propNameLength
, propDataLength
;
1000 UInt32 propType
, tmpValue
;
1001 OSBoolean
*tmpBoolean
= 0;
1002 OSNumber
*tmpNumber
= 0;
1003 OSString
*tmpString
= 0;
1004 OSData
*tmpData
= 0;
1006 propName
= (const UInt8
*)propSymbol
->getCStringNoCopy();
1007 propNameLength
= propSymbol
->getLength();
1008 propType
= getOFVariableType(propSymbol
);
1010 // Get the size of the data.
1011 propDataLength
= 0xFFFFFFFF;
1013 case kOFVariableTypeBoolean
:
1014 tmpBoolean
= OSDynamicCast(OSBoolean
, propObject
);
1015 if (tmpBoolean
!= 0) propDataLength
= 5;
1018 case kOFVariableTypeNumber
:
1019 tmpNumber
= OSDynamicCast(OSNumber
, propObject
);
1020 if (tmpNumber
!= 0) propDataLength
= 10;
1023 case kOFVariableTypeString
:
1024 tmpString
= OSDynamicCast(OSString
, propObject
);
1025 if (tmpString
!= 0) propDataLength
= tmpString
->getLength();
1028 case kOFVariableTypeData
:
1029 tmpData
= OSDynamicCast(OSData
, propObject
);
1031 tmpData
= escapeDataToData(tmpData
);
1032 propDataLength
= tmpData
->getLength();
1037 // Make sure the propertySize is known and will fit.
1038 if (propDataLength
== 0xFFFFFFFF) return false;
1039 if ((propNameLength
+ propDataLength
+ 2) > *length
) return false;
1041 // Copy the property name equal sign.
1042 buffer
+= snprintf((char *)buffer
, *length
, "%s=", propName
);
1045 case kOFVariableTypeBoolean
:
1046 if (tmpBoolean
->getValue()) {
1047 strlcpy((char *)buffer
, "true", *length
- propNameLength
);
1049 strlcpy((char *)buffer
, "false", *length
- propNameLength
);
1053 case kOFVariableTypeNumber
:
1054 tmpValue
= tmpNumber
->unsigned32BitValue();
1055 if (tmpValue
== 0xFFFFFFFF) {
1056 strlcpy((char *)buffer
, "-1", *length
- propNameLength
);
1057 } else if (tmpValue
< 1000) {
1058 snprintf((char *)buffer
, *length
- propNameLength
, "%d", (uint32_t)tmpValue
);
1060 snprintf((char *)buffer
, *length
- propNameLength
, "0x%x", (uint32_t)tmpValue
);
1064 case kOFVariableTypeString
:
1065 strlcpy((char *)buffer
, tmpString
->getCStringNoCopy(), *length
- propNameLength
);
1068 case kOFVariableTypeData
:
1069 bcopy(tmpData
->getBytesNoCopy(), buffer
, propDataLength
);
1074 propDataLength
= strlen((const char *)buffer
);
1076 *length
= propNameLength
+ propDataLength
+ 2;
1082 UInt16
IODTNVRAM::generateOWChecksum(UInt8
*buffer
)
1084 UInt32 cnt
, checksum
= 0;
1085 UInt16
*tmpBuffer
= (UInt16
*)buffer
;
1087 for (cnt
= 0; cnt
< _ofPartitionSize
/ 2; cnt
++)
1088 checksum
+= tmpBuffer
[cnt
];
1090 return checksum
% 0x0000FFFF;
1093 bool IODTNVRAM::validateOWChecksum(UInt8
*buffer
)
1095 UInt32 cnt
, checksum
, sum
= 0;
1096 UInt16
*tmpBuffer
= (UInt16
*)buffer
;
1098 for (cnt
= 0; cnt
< _ofPartitionSize
/ 2; cnt
++)
1099 sum
+= tmpBuffer
[cnt
];
1101 checksum
= (sum
>> 16) + (sum
& 0x0000FFFF);
1102 if (checksum
== 0x10000) checksum
--;
1103 checksum
= (checksum
^ 0x0000FFFF) & 0x0000FFFF;
1105 return checksum
== 0;
1108 void IODTNVRAM::updateOWBootArgs(const OSSymbol
*key
, OSObject
*value
)
1110 bool wasBootArgs
, bootr
= false;
1112 OSString
*tmpString
, *bootCommand
, *bootArgs
= 0;
1113 const UInt8
*bootCommandData
, *bootArgsData
;
1115 UInt32 bootCommandDataLength
, bootArgsDataLength
, tmpDataLength
;
1117 tmpString
= OSDynamicCast(OSString
, value
);
1118 if (tmpString
== 0) return;
1120 if (key
->isEqualTo("boot-command")) {
1121 wasBootArgs
= false;
1122 bootCommand
= tmpString
;
1123 } else if (key
->isEqualTo("boot-args")) {
1125 bootArgs
= tmpString
;
1126 bootCommand
= OSDynamicCast(OSString
, _ofDict
->getObject("boot-command"));
1127 if (bootCommand
== 0) return;
1130 bootCommandData
= (const UInt8
*)bootCommand
->getCStringNoCopy();
1131 bootCommandDataLength
= bootCommand
->getLength();
1133 if (bootCommandData
== 0) return;
1135 for (cnt
= 0; cnt
< bootCommandDataLength
; cnt
++) {
1136 if ((bootCommandData
[cnt
] == 'b') &&
1137 !strncmp("bootr", (const char *)bootCommandData
+ cnt
, 5)) {
1139 while (bootCommandData
[cnt
] == ' ') cnt
++;
1145 _ofDict
->removeObject("boot-args");
1150 bootArgsData
= (const UInt8
*)bootArgs
->getCStringNoCopy();
1151 bootArgsDataLength
= bootArgs
->getLength();
1152 if (bootArgsData
== 0) return;
1154 tmpDataLength
= cnt
+ bootArgsDataLength
;
1155 tmpData
= IONew(UInt8
, tmpDataLength
+ 1);
1156 if (tmpData
== 0) return;
1158 cnt
-= strlcpy((char *)tmpData
, (const char *)bootCommandData
, cnt
);
1159 strlcat((char *)tmpData
, (const char *)bootArgsData
, cnt
);
1161 bootCommand
= OSString::withCString((const char *)tmpData
);
1162 if (bootCommand
!= 0) {
1163 _ofDict
->setObject("boot-command", bootCommand
);
1164 bootCommand
->release();
1167 IODelete(tmpData
, UInt8
, tmpDataLength
+ 1);
1169 bootArgs
= OSString::withCString((const char *)(bootCommandData
+ cnt
));
1170 if (bootArgs
!= 0) {
1171 _ofDict
->setObject("boot-args", bootArgs
);
1172 bootArgs
->release();
1177 bool IODTNVRAM::searchNVRAMProperty(IONVRAMDescriptor
*hdr
, UInt32
*where
)
1182 IOReturn
IODTNVRAM::readNVRAMPropertyType0(IORegistryEntry
*entry
,
1183 const OSSymbol
**name
,
1186 return kIOReturnUnsupported
;
1190 IOReturn
IODTNVRAM::writeNVRAMPropertyType0(IORegistryEntry
*entry
,
1191 const OSSymbol
*name
,
1194 return kIOReturnUnsupported
;
1198 OSData
*IODTNVRAM::unescapeBytesToData(const UInt8
*bytes
, UInt32 length
)
1201 UInt32 totalLength
= 0;
1206 // Calculate the actual length of the data.
1209 for (cnt
= 0; cnt
< length
;) {
1210 byte
= bytes
[cnt
++];
1212 byte
= bytes
[cnt
++];
1220 totalLength
+= cnt2
;
1224 // Create an empty OSData of the correct size.
1225 data
= OSData::withCapacity(totalLength
);
1227 for (cnt
= 0; cnt
< length
;) {
1228 byte
= bytes
[cnt
++];
1230 byte
= bytes
[cnt
++];
1232 byte
= (byte
& 0x80) ? 0xFF : 0x00;
1235 data
->appendByte(byte
, cnt2
);
1243 OSData
* IODTNVRAM::escapeDataToData(OSData
* value
)
1246 const UInt8
* startPtr
;
1247 const UInt8
* endPtr
;
1248 const UInt8
* wherePtr
;
1252 wherePtr
= (const UInt8
*) value
->getBytesNoCopy();
1253 endPtr
= wherePtr
+ value
->getLength();
1255 result
= OSData::withCapacity(endPtr
- wherePtr
);
1259 while (wherePtr
< endPtr
) {
1260 startPtr
= wherePtr
;
1262 if ((byte
== 0x00) || (byte
== 0xFF)) {
1264 ((wherePtr
- startPtr
) < 0x80) && (wherePtr
< endPtr
) && (byte
== *wherePtr
);
1266 ok
&= result
->appendByte(0xff, 1);
1267 byte
= (byte
& 0x80) | (wherePtr
- startPtr
);
1269 ok
&= result
->appendByte(byte
, 1);
1271 ok
&= result
->appendByte(0, 1);
1281 static bool IsApplePropertyName(const char * propName
)
1284 while ((c
= *propName
++)) {
1285 if ((c
>= 'A') && (c
<= 'Z'))
1292 IOReturn
IODTNVRAM::readNVRAMPropertyType1(IORegistryEntry
*entry
,
1293 const OSSymbol
**name
,
1296 IOReturn err
= kIOReturnNoResources
;
1298 const UInt8
*startPtr
;
1299 const UInt8
*endPtr
;
1300 const UInt8
*wherePtr
;
1301 const UInt8
*nvPath
= 0;
1302 const char *nvName
= 0;
1303 const char *resultName
= 0;
1304 const UInt8
*resultValue
= 0;
1305 UInt32 resultValueLen
= 0;
1308 if (_ofDict
== 0) return err
;
1310 IOLockLock(_ofLock
);
1311 data
= OSDynamicCast(OSData
, _ofDict
->getObject(_registryPropertiesKey
));
1312 IOLockUnlock(_ofLock
);
1314 if (data
== 0) return err
;
1316 startPtr
= (const UInt8
*) data
->getBytesNoCopy();
1317 endPtr
= startPtr
+ data
->getLength();
1319 wherePtr
= startPtr
;
1320 while (wherePtr
< endPtr
) {
1321 byte
= *(wherePtr
++);
1327 else if (nvName
== 0)
1328 nvName
= (const char *) startPtr
;
1330 IORegistryEntry
* compareEntry
= IORegistryEntry::fromPath((const char *) nvPath
, gIODTPlane
);
1332 compareEntry
->release();
1333 if (entry
== compareEntry
) {
1334 bool appleProp
= IsApplePropertyName(nvName
);
1335 if (!appleProp
|| !resultName
) {
1336 resultName
= nvName
;
1337 resultValue
= startPtr
;
1338 resultValueLen
= wherePtr
- startPtr
- 1;
1346 startPtr
= wherePtr
;
1349 *name
= OSSymbol::withCString(resultName
);
1350 *value
= unescapeBytesToData(resultValue
, resultValueLen
);
1351 if ((*name
!= 0) && (*value
!= 0))
1352 err
= kIOReturnSuccess
;
1354 err
= kIOReturnNoMemory
;
1359 IOReturn
IODTNVRAM::writeNVRAMPropertyType1(IORegistryEntry
*entry
,
1360 const OSSymbol
*propName
,
1365 const UInt8
*startPtr
;
1366 const UInt8
*propStart
;
1367 const UInt8
*endPtr
;
1368 const UInt8
*wherePtr
;
1369 const UInt8
*nvPath
= 0;
1370 const char *nvName
= 0;
1375 bool settingAppleProp
;
1377 if (_ofDict
== 0) return kIOReturnNoResources
;
1379 settingAppleProp
= IsApplePropertyName(propName
->getCStringNoCopy());
1381 // copy over existing properties for other entries
1383 IOLockLock(_ofLock
);
1385 oldData
= OSDynamicCast(OSData
, _ofDict
->getObject(_registryPropertiesKey
));
1387 startPtr
= (const UInt8
*) oldData
->getBytesNoCopy();
1388 endPtr
= startPtr
+ oldData
->getLength();
1390 propStart
= startPtr
;
1391 wherePtr
= startPtr
;
1392 while (wherePtr
< endPtr
) {
1393 byte
= *(wherePtr
++);
1398 else if (nvName
== 0)
1399 nvName
= (const char *) startPtr
;
1401 IORegistryEntry
* compareEntry
= IORegistryEntry::fromPath((const char *) nvPath
, gIODTPlane
);
1403 compareEntry
->release();
1404 if (entry
== compareEntry
) {
1405 if ((settingAppleProp
&& propName
->isEqualTo(nvName
))
1406 || (!settingAppleProp
&& !IsApplePropertyName(nvName
))) {
1407 // delete old property (nvPath -> wherePtr)
1408 data
= OSData::withBytes(propStart
, nvPath
- propStart
);
1410 ok
&= data
->appendBytes(wherePtr
, endPtr
- wherePtr
);
1418 startPtr
= wherePtr
;
1422 // make the new property
1426 data
= OSData::withData(oldData
);
1428 data
= OSData::withCapacity(16);
1429 if (!data
) ok
= false;
1432 if (ok
&& value
&& value
->getLength()) do {
1433 // get entries in path
1434 OSArray
*array
= OSArray::withCapacity(5);
1440 array
->setObject(entry
);
1441 while ((entry
= entry
->getParentEntry(gIODTPlane
)));
1444 for (int i
= array
->getCount() - 3;
1445 (entry
= (IORegistryEntry
*) array
->getObject(i
));
1448 name
= entry
->getName(gIODTPlane
);
1449 comp
= entry
->getLocation(gIODTPlane
);
1450 if (comp
) ok
&= data
->appendBytes("/@", 2);
1452 if (!name
) continue;
1453 ok
&= data
->appendByte('/', 1);
1456 ok
&= data
->appendBytes(comp
, strlen(comp
));
1458 ok
&= data
->appendByte(0, 1);
1462 ok
&= data
->appendBytes(propName
->getCStringNoCopy(), propName
->getLength() + 1);
1464 // append escaped data
1465 oldData
= escapeDataToData(value
);
1466 ok
&= (oldData
!= 0);
1467 if (ok
) ok
&= data
->appendBytes(oldData
);
1472 ok
= _ofDict
->setObject(_registryPropertiesKey
, data
);
1475 IOLockUnlock(_ofLock
);
1476 if (data
) data
->release();
1478 if (ok
) syncOFVariables();
1480 return ok
? kIOReturnSuccess
: kIOReturnNoMemory
;
1483 bool IODTNVRAM::safeToSync(void)
1489 // delta interval went by
1490 clock_get_uptime(&delta
);
1492 // Figure it in seconds.
1493 absolutetime_to_nanoseconds(delta
, &delta_ns
);
1494 delta_secs
= (SInt32
)(delta_ns
/ NSEC_PER_SEC
);
1496 if ((delta_secs
> (_lastDeviceSync
+ MIN_SYNC_NOW_INTERVAL
)) || _freshInterval
)
1498 _lastDeviceSync
= delta_secs
;
1499 _freshInterval
= FALSE
;