2 * Copyright (c) 1998-2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <IOKit/IOLib.h>
30 #include <IOKit/IONVRAM.h>
31 #include <IOKit/IOPlatformExpert.h>
32 #include <IOKit/IOUserClient.h>
33 #include <IOKit/IOKitKeys.h>
34 #include <kern/debug.h>
35 #include <pexpert/pexpert.h>
37 #define super IOService
39 #define kIONVRAMPrivilege kIOClientPrivilegeAdministrator
40 //#define kIONVRAMPrivilege kIOClientPrivilegeLocalUser
43 OSDefineMetaClassAndStructors(IODTNVRAM
, IOService
);
45 bool IODTNVRAM::init(IORegistryEntry
*old
, const IORegistryPlane
*plane
)
49 if (!super::init(old
, plane
)) return false;
51 dict
= OSDictionary::withCapacity(1);
52 if (dict
== 0) return false;
53 setPropertyTable(dict
);
55 _nvramImage
= IONew(UInt8
, kIODTNVRAMImageSize
);
56 if (_nvramImage
== 0) return false;
58 _nvramPartitionOffsets
= OSDictionary::withCapacity(1);
59 if (_nvramPartitionOffsets
== 0) return false;
61 _nvramPartitionLengths
= OSDictionary::withCapacity(1);
62 if (_nvramPartitionLengths
== 0) return false;
64 _registryPropertiesKey
= OSSymbol::withCStringNoCopy("aapl,pci");
65 if (_registryPropertiesKey
== 0) return false;
70 void IODTNVRAM::registerNVRAMController(IONVRAMController
*nvram
)
73 UInt32 partitionOffset
, partitionLength
;
74 UInt32 freePartitionOffset
, freePartitionSize
;
75 UInt32 currentLength
, currentOffset
= 0;
76 OSNumber
*partitionOffsetNumber
, *partitionLengthNumber
;
78 if (_nvramController
!= 0) return;
80 _nvramController
= nvram
;
82 _nvramController
->read(0, _nvramImage
, kIODTNVRAMImageSize
);
84 // Find the offsets for the OF, XPRAM, NameRegistry and PanicInfo partitions.
85 _ofPartitionOffset
= 0xFFFFFFFF;
86 _xpramPartitionOffset
= 0xFFFFFFFF;
87 _nrPartitionOffset
= 0xFFFFFFFF;
88 _piPartitionOffset
= 0xFFFFFFFF;
89 freePartitionOffset
= 0xFFFFFFFF;
90 freePartitionSize
= 0;
91 if (getPlatform()->getBootROMType()) {
92 // Look through the partitions to find the OF, MacOS partitions.
93 while (currentOffset
< kIODTNVRAMImageSize
) {
94 currentLength
= ((UInt16
*)(_nvramImage
+ currentOffset
))[1] * 16;
96 partitionOffset
= currentOffset
+ 16;
97 partitionLength
= currentLength
- 16;
99 if (strncmp((const char *)_nvramImage
+ currentOffset
+ 4,
100 kIODTNVRAMOFPartitionName
, 12) == 0) {
101 _ofPartitionOffset
= partitionOffset
;
102 _ofPartitionSize
= partitionLength
;
103 } else if (strncmp((const char *)_nvramImage
+ currentOffset
+ 4,
104 kIODTNVRAMXPRAMPartitionName
, 12) == 0) {
105 _xpramPartitionOffset
= partitionOffset
;
106 _xpramPartitionSize
= kIODTNVRAMXPRAMSize
;
107 _nrPartitionOffset
= _xpramPartitionOffset
+ _xpramPartitionSize
;
108 _nrPartitionSize
= partitionLength
- _xpramPartitionSize
;
109 } else if (strncmp((const char *)_nvramImage
+ currentOffset
+ 4,
110 kIODTNVRAMPanicInfoPartitonName
, 12) == 0) {
111 _piPartitionOffset
= partitionOffset
;
112 _piPartitionSize
= partitionLength
;
113 } else if (strncmp((const char *)_nvramImage
+ currentOffset
+ 4,
114 kIODTNVRAMFreePartitionName
, 12) == 0) {
115 freePartitionOffset
= currentOffset
;
116 freePartitionSize
= currentLength
;
118 // Construct the partition ID from the signature and name.
119 snprintf(partitionID
, sizeof(partitionID
), "0x%02x,",
120 *(UInt8
*)(_nvramImage
+ currentOffset
));
121 strncpy(partitionID
+ 5,
122 (const char *)(_nvramImage
+ currentOffset
+ 4), 12);
123 partitionID
[17] = '\0';
125 partitionOffsetNumber
= OSNumber::withNumber(partitionOffset
, 32);
126 partitionLengthNumber
= OSNumber::withNumber(partitionLength
, 32);
128 // Save the partition offset and length
129 _nvramPartitionOffsets
->setObject(partitionID
, partitionOffsetNumber
);
130 _nvramPartitionLengths
->setObject(partitionID
, partitionLengthNumber
);
132 partitionOffsetNumber
->release();
133 partitionLengthNumber
->release();
135 currentOffset
+= currentLength
;
138 // Use the fixed address for old world machines.
139 _ofPartitionOffset
= 0x1800;
140 _ofPartitionSize
= 0x0800;
141 _xpramPartitionOffset
= 0x1300;
142 _xpramPartitionSize
= 0x0100;
143 _nrPartitionOffset
= 0x1400;
144 _nrPartitionSize
= 0x0400;
147 if (_ofPartitionOffset
!= 0xFFFFFFFF)
148 _ofImage
= _nvramImage
+ _ofPartitionOffset
;
149 if (_xpramPartitionOffset
!= 0xFFFFFFFF)
150 _xpramImage
= _nvramImage
+ _xpramPartitionOffset
;
151 if (_nrPartitionOffset
!= 0xFFFFFFFF)
152 _nrImage
= _nvramImage
+ _nrPartitionOffset
;
154 if (_piPartitionOffset
== 0xFFFFFFFF) {
155 if (freePartitionSize
> 0x20) {
156 // Set the signature to 0xa1.
157 _nvramImage
[freePartitionOffset
] = 0xa1;
158 // Set the checksum to 0.
159 _nvramImage
[freePartitionOffset
+ 1] = 0;
160 // Set the name for the Panic Info partition.
161 strncpy((char *)(_nvramImage
+ freePartitionOffset
+ 4),
162 kIODTNVRAMPanicInfoPartitonName
, 12);
164 // Calculate the partition offset and size.
165 _piPartitionOffset
= freePartitionOffset
+ 0x10;
166 _piPartitionSize
= 0x800;
167 if (_piPartitionSize
+ 0x20 > freePartitionSize
)
168 _piPartitionSize
= freePartitionSize
- 0x20;
170 _piImage
= _nvramImage
+ _piPartitionOffset
;
172 // Zero the new partition.
173 bzero(_piImage
, _piPartitionSize
);
175 // Set the partition size.
176 *(UInt16
*)(_nvramImage
+ freePartitionOffset
+ 2) =
177 (_piPartitionSize
/ 0x10) + 1;
179 // Set the partition checksum.
180 _nvramImage
[freePartitionOffset
+ 1] =
181 calculatePartitionChecksum(_nvramImage
+ freePartitionOffset
);
183 // Calculate the free partition offset and size.
184 freePartitionOffset
+= _piPartitionSize
+ 0x10;
185 freePartitionSize
-= _piPartitionSize
+ 0x10;
187 // Set the signature to 0x7f.
188 _nvramImage
[freePartitionOffset
] = 0x7f;
189 // Set the checksum to 0.
190 _nvramImage
[freePartitionOffset
+ 1] = 0;
191 // Set the name for the free partition.
192 strncpy((char *)(_nvramImage
+ freePartitionOffset
+ 4),
193 kIODTNVRAMFreePartitionName
, 12);
194 // Set the partition size.
195 *(UInt16
*)(_nvramImage
+ freePartitionOffset
+ 2) =
196 freePartitionSize
/ 0x10;
197 // Set the partition checksum.
198 _nvramImage
[freePartitionOffset
+ 1] =
199 calculatePartitionChecksum(_nvramImage
+ freePartitionOffset
);
201 // Set the nvram image as dirty.
202 _nvramImageDirty
= true;
205 _piImage
= _nvramImage
+ _piPartitionOffset
;
211 void IODTNVRAM::sync(void)
213 if (!_nvramImageDirty
&& !_ofImageDirty
) return;
215 // Don't try to sync OF Variables if the system has already paniced.
216 if (!_systemPaniced
) syncOFVariables();
218 _nvramController
->write(0, _nvramImage
, kIODTNVRAMImageSize
);
219 _nvramController
->sync();
221 _nvramImageDirty
= false;
224 bool IODTNVRAM::serializeProperties(OSSerialize
*s
) const
226 bool result
, hasPrivilege
;
229 OSDictionary
*dict
= 0, *tmpDict
= 0;
230 OSCollectionIterator
*iter
= 0;
232 if (_ofDict
== 0) return false;
234 // Verify permissions.
235 hasPrivilege
= (kIOReturnSuccess
== IOUserClient::clientHasPrivilege(current_task(), kIONVRAMPrivilege
));
237 tmpDict
= OSDictionary::withCapacity(1);
238 if (tmpDict
== 0) return false;
240 iter
= OSCollectionIterator::withCollection(_ofDict
);
241 if (iter
== 0) return false;
244 key
= OSDynamicCast(OSSymbol
, iter
->getNextObject());
247 variablePerm
= getOFVariablePerm(key
);
248 if ((hasPrivilege
|| (variablePerm
!= kOFVariablePermRootOnly
)) &&
249 ( ! (variablePerm
== kOFVariablePermKernelOnly
&& current_task() != kernel_task
) )) {
250 tmpDict
->setObject(key
, _ofDict
->getObject(key
));
255 result
= dict
->serialize(s
);
257 if (tmpDict
!= 0) tmpDict
->release();
258 if (iter
!= 0) iter
->release();
263 OSObject
*IODTNVRAM::getProperty(const OSSymbol
*aKey
) const
268 if (_ofDict
== 0) return 0;
270 // Verify permissions.
271 variablePerm
= getOFVariablePerm(aKey
);
272 result
= IOUserClient::clientHasPrivilege(current_task(), kIONVRAMPrivilege
);
273 if (result
!= kIOReturnSuccess
) {
274 if (variablePerm
== kOFVariablePermRootOnly
) return 0;
276 if (variablePerm
== kOFVariablePermKernelOnly
&& current_task() != kernel_task
) return 0;
278 return _ofDict
->getObject(aKey
);
281 OSObject
*IODTNVRAM::getProperty(const char *aKey
) const
283 const OSSymbol
*keySymbol
;
284 OSObject
*theObject
= 0;
286 keySymbol
= OSSymbol::withCStringNoCopy(aKey
);
287 if (keySymbol
!= 0) {
288 theObject
= getProperty(keySymbol
);
289 keySymbol
->release();
295 bool IODTNVRAM::setProperty(const OSSymbol
*aKey
, OSObject
*anObject
)
298 UInt32 propType
, propPerm
;
300 OSObject
*propObject
= 0;
302 if (_ofDict
== 0) return false;
304 // Verify permissions.
305 propPerm
= getOFVariablePerm(aKey
);
306 result
= IOUserClient::clientHasPrivilege(current_task(), kIONVRAMPrivilege
);
307 if (result
!= kIOReturnSuccess
) {
308 if (propPerm
!= kOFVariablePermUserWrite
) return false;
310 if (propPerm
== kOFVariablePermKernelOnly
&& current_task() != kernel_task
) return 0;
312 // Don't allow creation of new properties on old world machines.
313 if (getPlatform()->getBootROMType() == 0) {
314 if (_ofDict
->getObject(aKey
) == 0) return false;
317 // Don't allow change of 'aapl,panic-info'.
318 if (aKey
->isEqualTo(kIODTNVRAMPanicInfoKey
)) return false;
320 // Make sure the object is of the correct type.
321 propType
= getOFVariableType(aKey
);
323 case kOFVariableTypeBoolean
:
324 propObject
= OSDynamicCast(OSBoolean
, anObject
);
327 case kOFVariableTypeNumber
:
328 propObject
= OSDynamicCast(OSNumber
, anObject
);
331 case kOFVariableTypeString
:
332 propObject
= OSDynamicCast(OSString
, anObject
);
335 case kOFVariableTypeData
:
336 propObject
= OSDynamicCast(OSData
, anObject
);
337 if (propObject
== 0) {
338 tmpString
= OSDynamicCast(OSString
, anObject
);
339 if (tmpString
!= 0) {
340 propObject
= OSData::withBytes(tmpString
->getCStringNoCopy(),
341 tmpString
->getLength());
347 if (propObject
== 0) return false;
349 result
= _ofDict
->setObject(aKey
, propObject
);
352 if (getPlatform()->getBootROMType() == 0) {
353 updateOWBootArgs(aKey
, propObject
);
356 _ofImageDirty
= true;
362 void IODTNVRAM::removeProperty(const OSSymbol
*aKey
)
367 if (_ofDict
== 0) return;
369 // Verify permissions.
370 propPerm
= getOFVariablePerm(aKey
);
371 result
= IOUserClient::clientHasPrivilege(current_task(), kIOClientPrivilegeAdministrator
);
372 if (result
!= kIOReturnSuccess
) {
373 if (propPerm
!= kOFVariablePermUserWrite
) return;
375 if (propPerm
== kOFVariablePermKernelOnly
&& current_task() != kernel_task
) return;
377 // Don't allow removal of properties on old world machines.
378 if (getPlatform()->getBootROMType() == 0) return;
380 // Don't allow change of 'aapl,panic-info'.
381 if (aKey
->isEqualTo(kIODTNVRAMPanicInfoKey
)) return;
383 // If the object exists, remove it from the dictionary.
384 result
= _ofDict
->getObject(aKey
) != 0;
386 _ofDict
->removeObject(aKey
);
388 _ofImageDirty
= true;
392 IOReturn
IODTNVRAM::setProperties(OSObject
*properties
)
397 const OSString
*tmpStr
;
399 OSCollectionIterator
*iter
;
401 dict
= OSDynamicCast(OSDictionary
, properties
);
402 if (dict
== 0) return kIOReturnBadArgument
;
404 iter
= OSCollectionIterator::withCollection(dict
);
405 if (iter
== 0) return kIOReturnBadArgument
;
408 key
= OSDynamicCast(OSSymbol
, iter
->getNextObject());
411 object
= dict
->getObject(key
);
412 if (object
== 0) continue;
414 if (key
->isEqualTo(kIONVRAMDeletePropertyKey
)) {
415 tmpStr
= OSDynamicCast(OSString
, object
);
417 key
= OSSymbol::withString(tmpStr
);
425 result
= setProperty(key
, object
);
431 if (result
) return kIOReturnSuccess
;
432 else return kIOReturnError
;
435 IOReturn
IODTNVRAM::readXPRAM(IOByteCount offset
, UInt8
*buffer
,
438 if (_xpramImage
== 0) return kIOReturnUnsupported
;
440 if ((buffer
== 0) || (length
== 0) ||
441 (offset
+ length
> kIODTNVRAMXPRAMSize
))
442 return kIOReturnBadArgument
;
444 bcopy(_nvramImage
+ _xpramPartitionOffset
+ offset
, buffer
, length
);
446 return kIOReturnSuccess
;
449 IOReturn
IODTNVRAM::writeXPRAM(IOByteCount offset
, UInt8
*buffer
,
452 if (_xpramImage
== 0) return kIOReturnUnsupported
;
454 if ((buffer
== 0) || (length
== 0) ||
455 (offset
+ length
> kIODTNVRAMXPRAMSize
))
456 return kIOReturnBadArgument
;
458 bcopy(buffer
, _nvramImage
+ _xpramPartitionOffset
+ offset
, length
);
460 _nvramImageDirty
= true;
462 return kIOReturnSuccess
;
465 IOReturn
IODTNVRAM::readNVRAMProperty(IORegistryEntry
*entry
,
466 const OSSymbol
**name
,
471 if (getPlatform()->getBootROMType())
472 err
= readNVRAMPropertyType1(entry
, name
, value
);
474 err
= readNVRAMPropertyType0(entry
, name
, value
);
479 IOReturn
IODTNVRAM::writeNVRAMProperty(IORegistryEntry
*entry
,
480 const OSSymbol
*name
,
485 if (getPlatform()->getBootROMType())
486 err
= writeNVRAMPropertyType1(entry
, name
, value
);
488 err
= writeNVRAMPropertyType0(entry
, name
, value
);
493 OSDictionary
*IODTNVRAM::getNVRAMPartitions(void)
495 return _nvramPartitionLengths
;
498 IOReturn
IODTNVRAM::readNVRAMPartition(const OSSymbol
*partitionID
,
499 IOByteCount offset
, UInt8
*buffer
,
502 OSNumber
*partitionOffsetNumber
, *partitionLengthNumber
;
503 UInt32 partitionOffset
, partitionLength
;
505 partitionOffsetNumber
=
506 (OSNumber
*)_nvramPartitionOffsets
->getObject(partitionID
);
507 partitionLengthNumber
=
508 (OSNumber
*)_nvramPartitionLengths
->getObject(partitionID
);
510 if ((partitionOffsetNumber
== 0) || (partitionLengthNumber
== 0))
511 return kIOReturnNotFound
;
513 partitionOffset
= partitionOffsetNumber
->unsigned32BitValue();
514 partitionLength
= partitionLengthNumber
->unsigned32BitValue();
516 if ((buffer
== 0) || (length
== 0) ||
517 (offset
+ length
> partitionLength
))
518 return kIOReturnBadArgument
;
520 bcopy(_nvramImage
+ partitionOffset
+ offset
, buffer
, length
);
522 return kIOReturnSuccess
;
525 IOReturn
IODTNVRAM::writeNVRAMPartition(const OSSymbol
*partitionID
,
526 IOByteCount offset
, UInt8
*buffer
,
529 OSNumber
*partitionOffsetNumber
, *partitionLengthNumber
;
530 UInt32 partitionOffset
, partitionLength
;
532 partitionOffsetNumber
=
533 (OSNumber
*)_nvramPartitionOffsets
->getObject(partitionID
);
534 partitionLengthNumber
=
535 (OSNumber
*)_nvramPartitionLengths
->getObject(partitionID
);
537 if ((partitionOffsetNumber
== 0) || (partitionLengthNumber
== 0))
538 return kIOReturnNotFound
;
540 partitionOffset
= partitionOffsetNumber
->unsigned32BitValue();
541 partitionLength
= partitionLengthNumber
->unsigned32BitValue();
543 if ((buffer
== 0) || (length
== 0) ||
544 (offset
+ length
> partitionLength
))
545 return kIOReturnBadArgument
;
547 bcopy(buffer
, _nvramImage
+ partitionOffset
+ offset
, length
);
549 _nvramImageDirty
= true;
551 return kIOReturnSuccess
;
554 IOByteCount
IODTNVRAM::savePanicInfo(UInt8
*buffer
, IOByteCount length
)
556 if ((_piImage
== 0) || (length
<= 0)) return 0;
558 if (length
> (_piPartitionSize
- 4))
559 length
= _piPartitionSize
- 4;
561 // Save the Panic Info.
562 bcopy(buffer
, _piImage
+ 4, length
);
564 // Save the Panic Info length.
565 *(UInt32
*)_piImage
= length
;
567 _nvramImageDirty
= true;
569 * This prevents OF variables from being committed if the system has panicked
571 _systemPaniced
= true;
572 /* The call to sync() forces the NVRAM controller to write the panic info
573 * partition to NVRAM.
582 UInt8
IODTNVRAM::calculatePartitionChecksum(UInt8
*partitionHeader
)
584 UInt8 cnt
, isum
, csum
= 0;
586 for (cnt
= 0; cnt
< 0x10; cnt
++) {
587 isum
= csum
+ partitionHeader
[cnt
];
588 if (isum
< csum
) isum
++;
595 struct OWVariablesHeader
{
610 typedef struct OWVariablesHeader OWVariablesHeader
;
612 IOReturn
IODTNVRAM::initOFVariables(void)
614 UInt32 cnt
, propOffset
, propType
;
615 UInt8
*propName
, *propData
;
616 UInt32 propNameLength
, propDataLength
;
617 const OSSymbol
*propSymbol
;
618 OSObject
*propObject
;
619 OWVariablesHeader
*owHeader
;
621 if (_ofImage
== 0) return kIOReturnNotReady
;
623 _ofDict
= OSDictionary::withCapacity(1);
624 if (_ofDict
== 0) return kIOReturnNoMemory
;
626 if (getPlatform()->getBootROMType()) {
628 while (cnt
< _ofPartitionSize
) {
629 // Break if there is no name.
630 if (_ofImage
[cnt
] == '\0') break;
632 // Find the length of the name.
633 propName
= _ofImage
+ cnt
;
634 for (propNameLength
= 0; (cnt
+ propNameLength
) < _ofPartitionSize
;
636 if (_ofImage
[cnt
+ propNameLength
] == '=') break;
639 // Break if the name goes past the end of the partition.
640 if ((cnt
+ propNameLength
) >= _ofPartitionSize
) break;
641 cnt
+= propNameLength
+ 1;
643 propData
= _ofImage
+ cnt
;
644 for (propDataLength
= 0; (cnt
+ propDataLength
) < _ofPartitionSize
;
646 if (_ofImage
[cnt
+ propDataLength
] == '\0') break;
649 // Break if the data goes past the end of the partition.
650 if ((cnt
+ propDataLength
) >= _ofPartitionSize
) break;
651 cnt
+= propDataLength
+ 1;
653 if (convertPropToObject(propName
, propNameLength
,
654 propData
, propDataLength
,
655 &propSymbol
, &propObject
)) {
656 _ofDict
->setObject(propSymbol
, propObject
);
657 propSymbol
->release();
658 propObject
->release();
662 // Create the boot-args property if it is not in the dictionary.
663 if (_ofDict
->getObject("boot-args") == 0) {
664 propObject
= OSString::withCStringNoCopy("");
665 if (propObject
!= 0) {
666 _ofDict
->setObject("boot-args", propObject
);
667 propObject
->release();
671 // Create the 'aapl,panic-info' property if needed.
673 propDataLength
= *(UInt32
*)_piImage
;
674 if ((propDataLength
!= 0) && (propDataLength
<= (_piPartitionSize
- 4))) {
675 propObject
= OSData::withBytes(_piImage
+ 4, propDataLength
);
676 _ofDict
->setObject(kIODTNVRAMPanicInfoKey
, propObject
);
677 propObject
->release();
679 // Clear the length from _piImage and mark dirty.
680 *(UInt32
*)_piImage
= 0;
681 _nvramImageDirty
= true;
685 owHeader
= (OWVariablesHeader
*)_ofImage
;
686 if (!validateOWChecksum(_ofImage
)) {
689 return kIOReturnBadMedia
;
694 if (!getOWVariableInfo(cnt
++, &propSymbol
, &propType
, &propOffset
))
698 case kOFVariableTypeBoolean
:
699 propObject
= OSBoolean::withBoolean(owHeader
->owFlags
& propOffset
);
702 case kOFVariableTypeNumber
:
703 propObject
= OSNumber::withNumber(owHeader
->owNumbers
[propOffset
], 32);
706 case kOFVariableTypeString
:
707 propData
= _ofImage
+ owHeader
->owStrings
[propOffset
].offset
-
709 propDataLength
= owHeader
->owStrings
[propOffset
].length
;
710 propName
= IONew(UInt8
, propDataLength
+ 1);
712 strncpy((char *)propName
, (const char *)propData
, propDataLength
);
713 propName
[propDataLength
] = '\0';
714 propObject
= OSString::withCString((const char *)propName
);
715 IODelete(propName
, UInt8
, propDataLength
+ 1);
720 if (propObject
== 0) break;
722 _ofDict
->setObject(propSymbol
, propObject
);
723 propSymbol
->release();
724 propObject
->release();
727 // Create the boot-args property.
728 propSymbol
= OSSymbol::withCString("boot-command");
729 if (propSymbol
!= 0) {
730 propObject
= _ofDict
->getObject(propSymbol
);
731 if (propObject
!= 0) {
732 updateOWBootArgs(propSymbol
, propObject
);
734 propSymbol
->release();
738 return kIOReturnSuccess
;
741 IOReturn
IODTNVRAM::syncOFVariables(void)
744 UInt32 cnt
, length
, maxLength
;
745 UInt32 curOffset
, tmpOffset
, tmpType
, tmpDataLength
;
746 UInt8
*buffer
, *tmpBuffer
;
747 const UInt8
*tmpData
;
748 const OSSymbol
*tmpSymbol
;
750 OSBoolean
*tmpBoolean
;
753 OSCollectionIterator
*iter
;
754 OWVariablesHeader
*owHeader
, *owHeaderOld
;
756 if ((_ofImage
== 0) || (_ofDict
== 0)) return kIOReturnNotReady
;
758 if (!_ofImageDirty
) return kIOReturnSuccess
;
760 if (getPlatform()->getBootROMType()) {
761 buffer
= tmpBuffer
= IONew(UInt8
, _ofPartitionSize
);
762 if (buffer
== 0) return kIOReturnNoMemory
;
763 bzero(buffer
, _ofPartitionSize
);
766 maxLength
= _ofPartitionSize
;
768 iter
= OSCollectionIterator::withCollection(_ofDict
);
769 if (iter
== 0) ok
= false;
772 tmpSymbol
= OSDynamicCast(OSSymbol
, iter
->getNextObject());
773 if (tmpSymbol
== 0) break;
775 // Don't save 'aapl,panic-info'.
776 if (tmpSymbol
->isEqualTo(kIODTNVRAMPanicInfoKey
)) continue;
778 tmpObject
= _ofDict
->getObject(tmpSymbol
);
781 ok
= convertObjectToProp(tmpBuffer
, &length
, tmpSymbol
, tmpObject
);
790 bcopy(buffer
, _ofImage
, _ofPartitionSize
);
793 IODelete(buffer
, UInt8
, _ofPartitionSize
);
795 if (!ok
) return kIOReturnBadArgument
;
797 buffer
= IONew(UInt8
, _ofPartitionSize
);
798 if (buffer
== 0) return kIOReturnNoMemory
;
799 bzero(buffer
, _ofPartitionSize
);
801 owHeader
= (OWVariablesHeader
*)buffer
;
802 owHeaderOld
= (OWVariablesHeader
*)_ofImage
;
804 owHeader
->owMagic
= owHeaderOld
->owMagic
;
805 owHeader
->owVersion
= owHeaderOld
->owVersion
;
806 owHeader
->owPages
= owHeaderOld
->owPages
;
808 curOffset
= _ofPartitionSize
;
813 if (!getOWVariableInfo(cnt
++, &tmpSymbol
, &tmpType
, &tmpOffset
))
816 tmpObject
= _ofDict
->getObject(tmpSymbol
);
819 case kOFVariableTypeBoolean
:
820 tmpBoolean
= OSDynamicCast(OSBoolean
, tmpObject
);
821 if (tmpBoolean
->getValue()) owHeader
->owFlags
|= tmpOffset
;
824 case kOFVariableTypeNumber
:
825 tmpNumber
= OSDynamicCast(OSNumber
, tmpObject
);
826 owHeader
->owNumbers
[tmpOffset
] = tmpNumber
->unsigned32BitValue();
829 case kOFVariableTypeString
:
830 tmpString
= OSDynamicCast(OSString
, tmpObject
);
831 tmpData
= (const UInt8
*)tmpString
->getCStringNoCopy();
832 tmpDataLength
= tmpString
->getLength();
834 if ((curOffset
- tmpDataLength
) < sizeof(OWVariablesHeader
)) {
839 owHeader
->owStrings
[tmpOffset
].length
= tmpDataLength
;
840 curOffset
-= tmpDataLength
;
841 owHeader
->owStrings
[tmpOffset
].offset
= curOffset
+ _ofPartitionOffset
;
842 if (tmpDataLength
!= 0)
843 bcopy(tmpData
, buffer
+ curOffset
, tmpDataLength
);
849 owHeader
->owHere
= _ofPartitionOffset
+ sizeof(OWVariablesHeader
);
850 owHeader
->owTop
= _ofPartitionOffset
+ curOffset
;
851 owHeader
->owNext
= 0;
853 owHeader
->owChecksum
= 0;
854 owHeader
->owChecksum
= ~generateOWChecksum(buffer
);
856 bcopy(buffer
, _ofImage
, _ofPartitionSize
);
859 IODelete(buffer
, UInt8
, _ofPartitionSize
);
861 if (!ok
) return kIOReturnBadArgument
;
864 _ofImageDirty
= false;
865 _nvramImageDirty
= true;
867 return kIOReturnSuccess
;
871 const char *variableName
;
874 SInt32 variableOffset
;
876 typedef struct OFVariable OFVariable
;
879 kOWVariableOffsetNumber
= 8,
880 kOWVariableOffsetString
= 17
883 OFVariable gOFVariables
[] = {
884 {"little-endian?", kOFVariableTypeBoolean
, kOFVariablePermUserRead
, 0},
885 {"real-mode?", kOFVariableTypeBoolean
, kOFVariablePermUserRead
, 1},
886 {"auto-boot?", kOFVariableTypeBoolean
, kOFVariablePermUserRead
, 2},
887 {"diag-switch?", kOFVariableTypeBoolean
, kOFVariablePermUserRead
, 3},
888 {"fcode-debug?", kOFVariableTypeBoolean
, kOFVariablePermUserRead
, 4},
889 {"oem-banner?", kOFVariableTypeBoolean
, kOFVariablePermUserRead
, 5},
890 {"oem-logo?", kOFVariableTypeBoolean
, kOFVariablePermUserRead
, 6},
891 {"use-nvramrc?", kOFVariableTypeBoolean
, kOFVariablePermUserRead
, 7},
892 {"use-generic?", kOFVariableTypeBoolean
, kOFVariablePermUserRead
, -1},
893 {"default-mac-address?", kOFVariableTypeBoolean
, kOFVariablePermUserRead
,-1},
894 {"real-base", kOFVariableTypeNumber
, kOFVariablePermUserRead
, 8},
895 {"real-size", kOFVariableTypeNumber
, kOFVariablePermUserRead
, 9},
896 {"virt-base", kOFVariableTypeNumber
, kOFVariablePermUserRead
, 10},
897 {"virt-size", kOFVariableTypeNumber
, kOFVariablePermUserRead
, 11},
898 {"load-base", kOFVariableTypeNumber
, kOFVariablePermUserRead
, 12},
899 {"pci-probe-list", kOFVariableTypeNumber
, kOFVariablePermUserRead
, 13},
900 {"pci-probe-mask", kOFVariableTypeNumber
, kOFVariablePermUserRead
, -1},
901 {"screen-#columns", kOFVariableTypeNumber
, kOFVariablePermUserRead
, 14},
902 {"screen-#rows", kOFVariableTypeNumber
, kOFVariablePermUserRead
, 15},
903 {"selftest-#megs", kOFVariableTypeNumber
, kOFVariablePermUserRead
, 16},
904 {"boot-device", kOFVariableTypeString
, kOFVariablePermUserRead
, 17},
905 {"boot-file", kOFVariableTypeString
, kOFVariablePermUserRead
, 18},
906 {"boot-screen", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
907 {"console-screen", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
908 {"diag-device", kOFVariableTypeString
, kOFVariablePermUserRead
, 19},
909 {"diag-file", kOFVariableTypeString
, kOFVariablePermUserRead
, 20},
910 {"input-device", kOFVariableTypeString
, kOFVariablePermUserRead
, 21},
911 {"output-device", kOFVariableTypeString
, kOFVariablePermUserRead
, 22},
912 {"input-device-1", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
913 {"output-device-1", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
914 {"mouse-device", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
915 {"oem-banner", kOFVariableTypeString
, kOFVariablePermUserRead
, 23},
916 {"oem-logo", kOFVariableTypeString
, kOFVariablePermUserRead
, 24},
917 {"nvramrc", kOFVariableTypeString
, kOFVariablePermUserRead
, 25},
918 {"boot-command", kOFVariableTypeString
, kOFVariablePermUserRead
, 26},
919 {"default-client-ip", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
920 {"default-server-ip", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
921 {"default-gateway-ip", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
922 {"default-subnet-mask", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
923 {"default-router-ip", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
924 {"boot-script", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
925 {"boot-args", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
926 {"aapl,pci", kOFVariableTypeData
, kOFVariablePermRootOnly
, -1},
927 {"security-mode", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
928 {"security-password", kOFVariableTypeData
, kOFVariablePermRootOnly
, -1},
929 {"boot-image", kOFVariableTypeData
, kOFVariablePermUserWrite
, -1},
930 {"com.apple.System.fp-state", kOFVariableTypeData
, kOFVariablePermKernelOnly
, -1},
932 {"backlight-level", kOFVariableTypeData
, kOFVariablePermUserWrite
, -1},
934 {0, kOFVariableTypeData
, kOFVariablePermUserRead
, -1}
937 UInt32
IODTNVRAM::getOFVariableType(const OSSymbol
*propSymbol
) const
941 ofVar
= gOFVariables
;
943 if ((ofVar
->variableName
== 0) ||
944 propSymbol
->isEqualTo(ofVar
->variableName
)) break;
948 return ofVar
->variableType
;
951 UInt32
IODTNVRAM::getOFVariablePerm(const OSSymbol
*propSymbol
) const
955 ofVar
= gOFVariables
;
957 if ((ofVar
->variableName
== 0) ||
958 propSymbol
->isEqualTo(ofVar
->variableName
)) break;
962 return ofVar
->variablePerm
;
965 bool IODTNVRAM::getOWVariableInfo(UInt32 variableNumber
, const OSSymbol
**propSymbol
,
966 UInt32
*propType
, UInt32
*propOffset
)
970 ofVar
= gOFVariables
;
972 if (ofVar
->variableName
== 0) return false;
974 if (ofVar
->variableOffset
== (SInt32
) variableNumber
) break;
979 *propSymbol
= OSSymbol::withCStringNoCopy(ofVar
->variableName
);
980 *propType
= ofVar
->variableType
;
983 case kOFVariableTypeBoolean
:
984 *propOffset
= 1 << (31 - variableNumber
);
987 case kOFVariableTypeNumber
:
988 *propOffset
= variableNumber
- kOWVariableOffsetNumber
;
991 case kOFVariableTypeString
:
992 *propOffset
= variableNumber
- kOWVariableOffsetString
;
999 bool IODTNVRAM::convertPropToObject(UInt8
*propName
, UInt32 propNameLength
,
1000 UInt8
*propData
, UInt32 propDataLength
,
1001 const OSSymbol
**propSymbol
,
1002 OSObject
**propObject
)
1005 const OSSymbol
*tmpSymbol
;
1006 OSObject
*tmpObject
;
1007 OSNumber
*tmpNumber
;
1008 OSString
*tmpString
;
1010 // Create the symbol.
1011 propName
[propNameLength
] = '\0';
1012 tmpSymbol
= OSSymbol::withCString((const char *)propName
);
1013 propName
[propNameLength
] = '=';
1014 if (tmpSymbol
== 0) {
1018 propType
= getOFVariableType(tmpSymbol
);
1020 // Create the object.
1023 case kOFVariableTypeBoolean
:
1024 if (!strncmp("true", (const char *)propData
, propDataLength
)) {
1025 tmpObject
= kOSBooleanTrue
;
1026 } else if (!strncmp("false", (const char *)propData
, propDataLength
)) {
1027 tmpObject
= kOSBooleanFalse
;
1031 case kOFVariableTypeNumber
:
1032 tmpNumber
= OSNumber::withNumber(strtol((const char *)propData
, 0, 0), 32);
1033 if (tmpNumber
!= 0) tmpObject
= tmpNumber
;
1036 case kOFVariableTypeString
:
1037 tmpString
= OSString::withCString((const char *)propData
);
1038 if (tmpString
!= 0) tmpObject
= tmpString
;
1041 case kOFVariableTypeData
:
1042 tmpObject
= unescapeBytesToData(propData
, propDataLength
);
1046 if (tmpObject
== 0) {
1047 tmpSymbol
->release();
1051 *propSymbol
= tmpSymbol
;
1052 *propObject
= tmpObject
;
1057 bool IODTNVRAM::convertObjectToProp(UInt8
*buffer
, UInt32
*length
,
1058 const OSSymbol
*propSymbol
, OSObject
*propObject
)
1060 const UInt8
*propName
;
1061 UInt32 propNameLength
, propDataLength
;
1062 UInt32 propType
, tmpValue
;
1063 OSBoolean
*tmpBoolean
= 0;
1064 OSNumber
*tmpNumber
= 0;
1065 OSString
*tmpString
= 0;
1066 OSData
*tmpData
= 0;
1068 propName
= (const UInt8
*)propSymbol
->getCStringNoCopy();
1069 propNameLength
= propSymbol
->getLength();
1070 propType
= getOFVariableType(propSymbol
);
1072 // Get the size of the data.
1073 propDataLength
= 0xFFFFFFFF;
1075 case kOFVariableTypeBoolean
:
1076 tmpBoolean
= OSDynamicCast(OSBoolean
, propObject
);
1077 if (tmpBoolean
!= 0) propDataLength
= 5;
1080 case kOFVariableTypeNumber
:
1081 tmpNumber
= OSDynamicCast(OSNumber
, propObject
);
1082 if (tmpNumber
!= 0) propDataLength
= 10;
1085 case kOFVariableTypeString
:
1086 tmpString
= OSDynamicCast(OSString
, propObject
);
1087 if (tmpString
!= 0) propDataLength
= tmpString
->getLength();
1090 case kOFVariableTypeData
:
1091 tmpData
= OSDynamicCast(OSData
, propObject
);
1093 tmpData
= escapeDataToData(tmpData
);
1094 propDataLength
= tmpData
->getLength();
1099 // Make sure the propertySize is known and will fit.
1100 if (propDataLength
== 0xFFFFFFFF) return false;
1101 if ((propNameLength
+ propDataLength
+ 2) > *length
) return false;
1103 // Copy the property name equal sign.
1104 buffer
+= snprintf((char *)buffer
, *length
, "%s=", propName
);
1107 case kOFVariableTypeBoolean
:
1108 if (tmpBoolean
->getValue()) {
1109 strlcpy((char *)buffer
, "true", *length
- propNameLength
);
1111 strlcpy((char *)buffer
, "false", *length
- propNameLength
);
1115 case kOFVariableTypeNumber
:
1116 tmpValue
= tmpNumber
->unsigned32BitValue();
1117 if (tmpValue
== 0xFFFFFFFF) {
1118 strlcpy((char *)buffer
, "-1", *length
- propNameLength
);
1119 } else if (tmpValue
< 1000) {
1120 snprintf((char *)buffer
, *length
- propNameLength
, "%d", (uint32_t)tmpValue
);
1122 snprintf((char *)buffer
, *length
- propNameLength
, "0x%x", (uint32_t)tmpValue
);
1126 case kOFVariableTypeString
:
1127 strlcpy((char *)buffer
, tmpString
->getCStringNoCopy(), *length
- propNameLength
);
1130 case kOFVariableTypeData
:
1131 bcopy(tmpData
->getBytesNoCopy(), buffer
, propDataLength
);
1136 propDataLength
= strlen((const char *)buffer
);
1138 *length
= propNameLength
+ propDataLength
+ 2;
1144 UInt16
IODTNVRAM::generateOWChecksum(UInt8
*buffer
)
1146 UInt32 cnt
, checksum
= 0;
1147 UInt16
*tmpBuffer
= (UInt16
*)buffer
;
1149 for (cnt
= 0; cnt
< _ofPartitionSize
/ 2; cnt
++)
1150 checksum
+= tmpBuffer
[cnt
];
1152 return checksum
% 0x0000FFFF;
1155 bool IODTNVRAM::validateOWChecksum(UInt8
*buffer
)
1157 UInt32 cnt
, checksum
, sum
= 0;
1158 UInt16
*tmpBuffer
= (UInt16
*)buffer
;
1160 for (cnt
= 0; cnt
< _ofPartitionSize
/ 2; cnt
++)
1161 sum
+= tmpBuffer
[cnt
];
1163 checksum
= (sum
>> 16) + (sum
& 0x0000FFFF);
1164 if (checksum
== 0x10000) checksum
--;
1165 checksum
= (checksum
^ 0x0000FFFF) & 0x0000FFFF;
1167 return checksum
== 0;
1170 void IODTNVRAM::updateOWBootArgs(const OSSymbol
*key
, OSObject
*value
)
1172 bool wasBootArgs
, bootr
= false;
1174 OSString
*tmpString
, *bootCommand
, *bootArgs
= 0;
1175 const UInt8
*bootCommandData
, *bootArgsData
;
1177 UInt32 bootCommandDataLength
, bootArgsDataLength
, tmpDataLength
;
1179 tmpString
= OSDynamicCast(OSString
, value
);
1180 if (tmpString
== 0) return;
1182 if (key
->isEqualTo("boot-command")) {
1183 wasBootArgs
= false;
1184 bootCommand
= tmpString
;
1185 } else if (key
->isEqualTo("boot-args")) {
1187 bootArgs
= tmpString
;
1188 bootCommand
= OSDynamicCast(OSString
, _ofDict
->getObject("boot-command"));
1189 if (bootCommand
== 0) return;
1192 bootCommandData
= (const UInt8
*)bootCommand
->getCStringNoCopy();
1193 bootCommandDataLength
= bootCommand
->getLength();
1195 if (bootCommandData
== 0) return;
1197 for (cnt
= 0; cnt
< bootCommandDataLength
; cnt
++) {
1198 if ((bootCommandData
[cnt
] == 'b') &&
1199 !strncmp("bootr", (const char *)bootCommandData
+ cnt
, 5)) {
1201 while (bootCommandData
[cnt
] == ' ') cnt
++;
1207 _ofDict
->removeObject("boot-args");
1212 bootArgsData
= (const UInt8
*)bootArgs
->getCStringNoCopy();
1213 bootArgsDataLength
= bootArgs
->getLength();
1214 if (bootArgsData
== 0) return;
1216 tmpDataLength
= cnt
+ bootArgsDataLength
;
1217 tmpData
= IONew(UInt8
, tmpDataLength
+ 1);
1218 if (tmpData
== 0) return;
1220 cnt
-= strlcpy((char *)tmpData
, (const char *)bootCommandData
, cnt
);
1221 strlcat((char *)tmpData
, (const char *)bootArgsData
, cnt
);
1223 bootCommand
= OSString::withCString((const char *)tmpData
);
1224 if (bootCommand
!= 0) {
1225 _ofDict
->setObject("boot-command", bootCommand
);
1226 bootCommand
->release();
1229 IODelete(tmpData
, UInt8
, tmpDataLength
+ 1);
1231 bootArgs
= OSString::withCString((const char *)(bootCommandData
+ cnt
));
1232 if (bootArgs
!= 0) {
1233 _ofDict
->setObject("boot-args", bootArgs
);
1234 bootArgs
->release();
1240 // Private methods for Name Registry access.
1243 kMaxNVNameLength
= 4,
1244 kMaxNVDataLength
= 8
1247 struct NVRAMProperty
1249 IONVRAMDescriptor header
;
1251 UInt8 name
[ kMaxNVNameLength
];
1253 UInt8 data
[ kMaxNVDataLength
];
1256 bool IODTNVRAM::searchNVRAMProperty(IONVRAMDescriptor
*hdr
, UInt32
*where
)
1261 nvEnd
= *((UInt16
*)_nrImage
);
1262 if(getPlatform()->getBootROMType()) {
1263 // on NewWorld, offset to partition start
1266 // on old world, absolute
1267 nvEnd
-= _nrPartitionOffset
;
1269 if((nvEnd
< 0) || (nvEnd
>= kIODTNVRAMNameRegistrySize
))
1273 while ((offset
+ sizeof(NVRAMProperty
)) <= (UInt32
)nvEnd
) {
1274 if (bcmp(_nrImage
+ offset
, hdr
, sizeof(*hdr
)) == 0) {
1278 offset
+= sizeof(NVRAMProperty
);
1281 if ((nvEnd
+ sizeof(NVRAMProperty
)) <= kIODTNVRAMNameRegistrySize
)
1289 IOReturn
IODTNVRAM::readNVRAMPropertyType0(IORegistryEntry
*entry
,
1290 const OSSymbol
**name
,
1293 IONVRAMDescriptor hdr
;
1294 NVRAMProperty
*prop
;
1298 char nameBuf
[kMaxNVNameLength
+ 1];
1300 if (_nrImage
== 0) return kIOReturnUnsupported
;
1301 if ((entry
== 0) || (name
== 0) || (value
== 0)) return kIOReturnBadArgument
;
1303 err
= IODTMakeNVDescriptor(entry
, &hdr
);
1304 if (err
!= kIOReturnSuccess
) return err
;
1306 if (searchNVRAMProperty(&hdr
, &offset
)) {
1307 prop
= (NVRAMProperty
*)(_nrImage
+ offset
);
1309 length
= prop
->nameLength
;
1310 if (length
> kMaxNVNameLength
) length
= kMaxNVNameLength
;
1311 strncpy(nameBuf
, (const char *)prop
->name
, length
);
1312 nameBuf
[length
] = 0;
1313 *name
= OSSymbol::withCString(nameBuf
);
1315 length
= prop
->dataLength
;
1316 if (length
> kMaxNVDataLength
) length
= kMaxNVDataLength
;
1317 *value
= OSData::withBytes(prop
->data
, length
);
1319 if ((*name
!= 0) && (*value
!= 0)) return kIOReturnSuccess
;
1320 else return kIOReturnNoMemory
;
1323 return kIOReturnNoResources
;
1326 IOReturn
IODTNVRAM::writeNVRAMPropertyType0(IORegistryEntry
*entry
,
1327 const OSSymbol
*name
,
1330 IONVRAMDescriptor hdr
;
1331 NVRAMProperty
*prop
;
1332 IOByteCount nameLength
;
1333 IOByteCount dataLength
;
1339 if (_nrImage
== 0) return kIOReturnUnsupported
;
1340 if ((entry
== 0) || (name
== 0) || (value
== 0)) return kIOReturnBadArgument
;
1342 nameLength
= name
->getLength();
1343 dataLength
= value
->getLength();
1344 if (nameLength
> kMaxNVNameLength
) return kIOReturnNoSpace
;
1345 if (dataLength
> kMaxNVDataLength
) return kIOReturnNoSpace
;
1347 err
= IODTMakeNVDescriptor(entry
, &hdr
);
1348 if (err
!= kIOReturnSuccess
) return err
;
1350 exists
= searchNVRAMProperty(&hdr
, &offset
);
1351 if (offset
== 0) return kIOReturnNoMemory
;
1353 prop
= (NVRAMProperty
*)(_nrImage
+ offset
);
1354 if (!exists
) bcopy(&hdr
, &prop
->header
, sizeof(hdr
));
1356 prop
->nameLength
= nameLength
;
1357 bcopy(name
->getCStringNoCopy(), prop
->name
, nameLength
);
1358 prop
->dataLength
= dataLength
;
1359 bcopy(value
->getBytesNoCopy(), prop
->data
, dataLength
);
1362 nvLength
= offset
+ sizeof(NVRAMProperty
);
1363 if (getPlatform()->getBootROMType())
1366 nvLength
+= _nrPartitionOffset
;
1367 *((UInt16
*)_nrImage
) = nvLength
;
1370 _nvramImageDirty
= true;
1375 OSData
*IODTNVRAM::unescapeBytesToData(const UInt8
*bytes
, UInt32 length
)
1378 UInt32 totalLength
= 0;
1383 // Calculate the actual length of the data.
1386 for (cnt
= 0; cnt
< length
;) {
1387 byte
= bytes
[cnt
++];
1389 byte
= bytes
[cnt
++];
1397 totalLength
+= cnt2
;
1401 // Create an empty OSData of the correct size.
1402 data
= OSData::withCapacity(totalLength
);
1404 for (cnt
= 0; cnt
< length
;) {
1405 byte
= bytes
[cnt
++];
1407 byte
= bytes
[cnt
++];
1409 byte
= (byte
& 0x80) ? 0xFF : 0x00;
1412 data
->appendByte(byte
, cnt2
);
1420 OSData
* IODTNVRAM::escapeDataToData(OSData
* value
)
1423 const UInt8
* startPtr
;
1424 const UInt8
* endPtr
;
1425 const UInt8
* wherePtr
;
1429 wherePtr
= (const UInt8
*) value
->getBytesNoCopy();
1430 endPtr
= wherePtr
+ value
->getLength();
1432 result
= OSData::withCapacity(endPtr
- wherePtr
);
1436 while (wherePtr
< endPtr
) {
1437 startPtr
= wherePtr
;
1439 if ((byte
== 0x00) || (byte
== 0xFF)) {
1441 ((wherePtr
- startPtr
) < 0x80) && (wherePtr
< endPtr
) && (byte
== *wherePtr
);
1443 ok
&= result
->appendByte(0xff, 1);
1444 byte
= (byte
& 0x80) | (wherePtr
- startPtr
);
1446 ok
&= result
->appendByte(byte
, 1);
1448 ok
&= result
->appendByte(0, 1);
1458 static bool IsApplePropertyName(const char * propName
)
1461 while ((c
= *propName
++)) {
1462 if ((c
>= 'A') && (c
<= 'Z'))
1469 IOReturn
IODTNVRAM::readNVRAMPropertyType1(IORegistryEntry
*entry
,
1470 const OSSymbol
**name
,
1473 IOReturn err
= kIOReturnNoResources
;
1475 const UInt8
*startPtr
;
1476 const UInt8
*endPtr
;
1477 const UInt8
*wherePtr
;
1478 const UInt8
*nvPath
= 0;
1479 const char *nvName
= 0;
1480 const char *resultName
= 0;
1481 const UInt8
*resultValue
= 0;
1482 UInt32 resultValueLen
= 0;
1485 if (_ofDict
== 0) return err
;
1486 data
= OSDynamicCast(OSData
, _ofDict
->getObject(_registryPropertiesKey
));
1487 if (data
== 0) return err
;
1489 startPtr
= (const UInt8
*) data
->getBytesNoCopy();
1490 endPtr
= startPtr
+ data
->getLength();
1492 wherePtr
= startPtr
;
1493 while (wherePtr
< endPtr
) {
1494 byte
= *(wherePtr
++);
1500 else if (nvName
== 0)
1501 nvName
= (const char *) startPtr
;
1503 IORegistryEntry
* compareEntry
= IORegistryEntry::fromPath((const char *) nvPath
, gIODTPlane
);
1505 compareEntry
->release();
1506 if (entry
== compareEntry
) {
1507 bool appleProp
= IsApplePropertyName(nvName
);
1508 if (!appleProp
|| !resultName
) {
1509 resultName
= nvName
;
1510 resultValue
= startPtr
;
1511 resultValueLen
= wherePtr
- startPtr
- 1;
1519 startPtr
= wherePtr
;
1522 *name
= OSSymbol::withCString(resultName
);
1523 *value
= unescapeBytesToData(resultValue
, resultValueLen
);
1524 if ((*name
!= 0) && (*value
!= 0))
1525 err
= kIOReturnSuccess
;
1527 err
= kIOReturnNoMemory
;
1532 IOReturn
IODTNVRAM::writeNVRAMPropertyType1(IORegistryEntry
*entry
,
1533 const OSSymbol
*propName
,
1538 const UInt8
*startPtr
;
1539 const UInt8
*propStart
;
1540 const UInt8
*endPtr
;
1541 const UInt8
*wherePtr
;
1542 const UInt8
*nvPath
= 0;
1543 const char *nvName
= 0;
1548 bool settingAppleProp
;
1550 if (_ofDict
== 0) return kIOReturnNoResources
;
1552 settingAppleProp
= IsApplePropertyName(propName
->getCStringNoCopy());
1554 // copy over existing properties for other entries
1556 oldData
= OSDynamicCast(OSData
, _ofDict
->getObject(_registryPropertiesKey
));
1558 startPtr
= (const UInt8
*) oldData
->getBytesNoCopy();
1559 endPtr
= startPtr
+ oldData
->getLength();
1561 propStart
= startPtr
;
1562 wherePtr
= startPtr
;
1563 while (wherePtr
< endPtr
) {
1564 byte
= *(wherePtr
++);
1569 else if (nvName
== 0)
1570 nvName
= (const char *) startPtr
;
1572 IORegistryEntry
* compareEntry
= IORegistryEntry::fromPath((const char *) nvPath
, gIODTPlane
);
1574 compareEntry
->release();
1575 if (entry
== compareEntry
) {
1576 if ((settingAppleProp
&& propName
->isEqualTo(nvName
))
1577 || (!settingAppleProp
&& !IsApplePropertyName(nvName
))) {
1578 // delete old property (nvPath -> wherePtr)
1579 data
= OSData::withBytes(propStart
, nvPath
- propStart
);
1581 ok
&= data
->appendBytes(wherePtr
, endPtr
- wherePtr
);
1589 startPtr
= wherePtr
;
1593 // make the new property
1597 data
= OSData::withData(oldData
);
1599 data
= OSData::withCapacity(16);
1601 return kIOReturnNoMemory
;
1604 if (value
&& value
->getLength()) {
1605 // get entries in path
1606 OSArray
*array
= OSArray::withCapacity(5);
1609 return kIOReturnNoMemory
;
1612 array
->setObject(entry
);
1613 while ((entry
= entry
->getParentEntry(gIODTPlane
)));
1616 for (int i
= array
->getCount() - 3;
1617 (entry
= (IORegistryEntry
*) array
->getObject(i
));
1620 name
= entry
->getName(gIODTPlane
);
1621 comp
= entry
->getLocation(gIODTPlane
);
1622 if( comp
&& (0 == strncmp("pci", name
, sizeof("pci")))
1623 && (0 == strncmp("80000000", comp
, sizeof("80000000")))) {
1625 comp
= "/pci@80000000";
1628 ok
&= data
->appendBytes("/@", 2);
1632 ok
&= data
->appendByte('/', 1);
1636 ok
&= data
->appendBytes(comp
, strlen(comp
));
1638 ok
&= data
->appendByte(0, 1);
1642 ok
&= data
->appendBytes(propName
->getCStringNoCopy(), propName
->getLength() + 1);
1644 // append escaped data
1645 oldData
= escapeDataToData(value
);
1646 ok
&= (oldData
!= 0);
1648 ok
&= data
->appendBytes(oldData
);
1651 ok
= _ofDict
->setObject(_registryPropertiesKey
, data
);
1653 _ofImageDirty
= true;
1657 return ok
? kIOReturnSuccess
: kIOReturnNoMemory
;