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>
35 #define super IOService
37 #define kIONVRAMPrivilege kIOClientPrivilegeAdministrator
38 //#define kIONVRAMPrivilege kIOClientPrivilegeLocalUser
41 OSDefineMetaClassAndStructors(IODTNVRAM
, IOService
);
43 bool IODTNVRAM::init(IORegistryEntry
*old
, const IORegistryPlane
*plane
)
47 if (!super::init(old
, plane
)) return false;
49 dict
= OSDictionary::withCapacity(1);
50 if (dict
== 0) return false;
51 setPropertyTable(dict
);
53 _nvramImage
= IONew(UInt8
, kIODTNVRAMImageSize
);
54 if (_nvramImage
== 0) return false;
56 _nvramPartitionOffsets
= OSDictionary::withCapacity(1);
57 if (_nvramPartitionOffsets
== 0) return false;
59 _nvramPartitionLengths
= OSDictionary::withCapacity(1);
60 if (_nvramPartitionLengths
== 0) return false;
62 _registryPropertiesKey
= OSSymbol::withCStringNoCopy("aapl,pci");
63 if (_registryPropertiesKey
== 0) return false;
68 void IODTNVRAM::registerNVRAMController(IONVRAMController
*nvram
)
71 UInt32 partitionOffset
, partitionLength
;
72 UInt32 freePartitionOffset
, freePartitionSize
;
73 UInt32 currentLength
, currentOffset
= 0;
74 OSNumber
*partitionOffsetNumber
, *partitionLengthNumber
;
76 if (_nvramController
!= 0) return;
78 _nvramController
= nvram
;
80 _nvramController
->read(0, _nvramImage
, kIODTNVRAMImageSize
);
82 // Find the offsets for the OF, XPRAM, NameRegistry and PanicInfo partitions.
83 _ofPartitionOffset
= 0xFFFFFFFF;
84 _xpramPartitionOffset
= 0xFFFFFFFF;
85 _nrPartitionOffset
= 0xFFFFFFFF;
86 _piPartitionOffset
= 0xFFFFFFFF;
87 freePartitionOffset
= 0xFFFFFFFF;
88 freePartitionSize
= 0;
89 if (getPlatform()->getBootROMType()) {
90 // Look through the partitions to find the OF, MacOS partitions.
91 while (currentOffset
< kIODTNVRAMImageSize
) {
92 currentLength
= ((UInt16
*)(_nvramImage
+ currentOffset
))[1] * 16;
94 partitionOffset
= currentOffset
+ 16;
95 partitionLength
= currentLength
- 16;
97 if (strncmp((const char *)_nvramImage
+ currentOffset
+ 4,
98 kIODTNVRAMOFPartitionName
, 12) == 0) {
99 _ofPartitionOffset
= partitionOffset
;
100 _ofPartitionSize
= partitionLength
;
101 } else if (strncmp((const char *)_nvramImage
+ currentOffset
+ 4,
102 kIODTNVRAMXPRAMPartitionName
, 12) == 0) {
103 _xpramPartitionOffset
= partitionOffset
;
104 _xpramPartitionSize
= kIODTNVRAMXPRAMSize
;
105 _nrPartitionOffset
= _xpramPartitionOffset
+ _xpramPartitionSize
;
106 _nrPartitionSize
= partitionLength
- _xpramPartitionSize
;
107 } else if (strncmp((const char *)_nvramImage
+ currentOffset
+ 4,
108 kIODTNVRAMPanicInfoPartitonName
, 12) == 0) {
109 _piPartitionOffset
= partitionOffset
;
110 _piPartitionSize
= partitionLength
;
111 } else if (strncmp((const char *)_nvramImage
+ currentOffset
+ 4,
112 kIODTNVRAMFreePartitionName
, 12) == 0) {
113 freePartitionOffset
= currentOffset
;
114 freePartitionSize
= currentLength
;
116 // Construct the partition ID from the signature and name.
117 snprintf(partitionID
, sizeof(partitionID
), "0x%02x,",
118 *(UInt8
*)(_nvramImage
+ currentOffset
));
119 strncpy(partitionID
+ 5,
120 (const char *)(_nvramImage
+ currentOffset
+ 4), 12);
121 partitionID
[17] = '\0';
123 partitionOffsetNumber
= OSNumber::withNumber(partitionOffset
, 32);
124 partitionLengthNumber
= OSNumber::withNumber(partitionLength
, 32);
126 // Save the partition offset and length
127 _nvramPartitionOffsets
->setObject(partitionID
, partitionOffsetNumber
);
128 _nvramPartitionLengths
->setObject(partitionID
, partitionLengthNumber
);
130 partitionOffsetNumber
->release();
131 partitionLengthNumber
->release();
133 currentOffset
+= currentLength
;
136 // Use the fixed address for old world machines.
137 _ofPartitionOffset
= 0x1800;
138 _ofPartitionSize
= 0x0800;
139 _xpramPartitionOffset
= 0x1300;
140 _xpramPartitionSize
= 0x0100;
141 _nrPartitionOffset
= 0x1400;
142 _nrPartitionSize
= 0x0400;
145 if (_ofPartitionOffset
!= 0xFFFFFFFF)
146 _ofImage
= _nvramImage
+ _ofPartitionOffset
;
147 if (_xpramPartitionOffset
!= 0xFFFFFFFF)
148 _xpramImage
= _nvramImage
+ _xpramPartitionOffset
;
149 if (_nrPartitionOffset
!= 0xFFFFFFFF)
150 _nrImage
= _nvramImage
+ _nrPartitionOffset
;
152 if (_piPartitionOffset
== 0xFFFFFFFF) {
153 if (freePartitionSize
> 0x20) {
154 // Set the signature to 0xa1.
155 _nvramImage
[freePartitionOffset
] = 0xa1;
156 // Set the checksum to 0.
157 _nvramImage
[freePartitionOffset
+ 1] = 0;
158 // Set the name for the Panic Info partition.
159 strncpy((char *)(_nvramImage
+ freePartitionOffset
+ 4),
160 kIODTNVRAMPanicInfoPartitonName
, 12);
162 // Calculate the partition offset and size.
163 _piPartitionOffset
= freePartitionOffset
+ 0x10;
164 _piPartitionSize
= 0x800;
165 if (_piPartitionSize
+ 0x20 > freePartitionSize
)
166 _piPartitionSize
= freePartitionSize
- 0x20;
168 _piImage
= _nvramImage
+ _piPartitionOffset
;
170 // Zero the new partition.
171 bzero(_piImage
, _piPartitionSize
);
173 // Set the partition size.
174 *(UInt16
*)(_nvramImage
+ freePartitionOffset
+ 2) =
175 (_piPartitionSize
/ 0x10) + 1;
177 // Set the partition checksum.
178 _nvramImage
[freePartitionOffset
+ 1] =
179 calculatePartitionChecksum(_nvramImage
+ freePartitionOffset
);
181 // Calculate the free partition offset and size.
182 freePartitionOffset
+= _piPartitionSize
+ 0x10;
183 freePartitionSize
-= _piPartitionSize
+ 0x10;
185 // Set the signature to 0x7f.
186 _nvramImage
[freePartitionOffset
] = 0x7f;
187 // Set the checksum to 0.
188 _nvramImage
[freePartitionOffset
+ 1] = 0;
189 // Set the name for the free partition.
190 strncpy((char *)(_nvramImage
+ freePartitionOffset
+ 4),
191 kIODTNVRAMFreePartitionName
, 12);
192 // Set the partition size.
193 *(UInt16
*)(_nvramImage
+ freePartitionOffset
+ 2) =
194 freePartitionSize
/ 0x10;
195 // Set the partition checksum.
196 _nvramImage
[freePartitionOffset
+ 1] =
197 calculatePartitionChecksum(_nvramImage
+ freePartitionOffset
);
199 // Set the nvram image as dirty.
200 _nvramImageDirty
= true;
203 _piImage
= _nvramImage
+ _piPartitionOffset
;
209 void IODTNVRAM::sync(void)
211 if (!_nvramImageDirty
&& !_ofImageDirty
) return;
213 // Don't try to sync OF Variables if the system has already paniced.
214 if (!_systemPaniced
) syncOFVariables();
216 _nvramController
->write(0, _nvramImage
, kIODTNVRAMImageSize
);
217 _nvramController
->sync();
219 _nvramImageDirty
= false;
222 bool IODTNVRAM::serializeProperties(OSSerialize
*s
) const
227 OSDictionary
*dict
, *tmpDict
= 0;
228 OSCollectionIterator
*iter
= 0;
230 if (_ofDict
== 0) return false;
232 // Verify permissions.
233 result
= IOUserClient::clientHasPrivilege(current_task(), kIONVRAMPrivilege
);
234 if (result
!= kIOReturnSuccess
) {
235 tmpDict
= OSDictionary::withCapacity(1);
236 if (tmpDict
== 0) return false;
238 iter
= OSCollectionIterator::withCollection(_ofDict
);
239 if (iter
== 0) return false;
242 key
= OSDynamicCast(OSSymbol
, iter
->getNextObject());
245 variablePerm
= getOFVariablePerm(key
);
246 if (variablePerm
!= kOFVariablePermRootOnly
) {
247 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 result
= IOUserClient::clientHasPrivilege(current_task(), kIONVRAMPrivilege
);
272 if (result
!= kIOReturnSuccess
) {
273 variablePerm
= getOFVariablePerm(aKey
);
274 if (variablePerm
== kOFVariablePermRootOnly
) return 0;
277 return _ofDict
->getObject(aKey
);
280 OSObject
*IODTNVRAM::getProperty(const char *aKey
) const
282 const OSSymbol
*keySymbol
;
283 OSObject
*theObject
= 0;
285 keySymbol
= OSSymbol::withCStringNoCopy(aKey
);
286 if (keySymbol
!= 0) {
287 theObject
= getProperty(keySymbol
);
288 keySymbol
->release();
294 bool IODTNVRAM::setProperty(const OSSymbol
*aKey
, OSObject
*anObject
)
297 UInt32 propType
, propPerm
;
299 OSObject
*propObject
= 0;
301 if (_ofDict
== 0) return false;
303 // Verify permissions.
304 result
= IOUserClient::clientHasPrivilege(current_task(), kIONVRAMPrivilege
);
305 if (result
!= kIOReturnSuccess
) {
306 propPerm
= getOFVariablePerm(aKey
);
307 if (propPerm
!= kOFVariablePermUserWrite
) return false;
310 // Don't allow creation of new properties on old world machines.
311 if (getPlatform()->getBootROMType() == 0) {
312 if (_ofDict
->getObject(aKey
) == 0) return false;
315 // Don't allow change of 'aapl,panic-info'.
316 if (aKey
->isEqualTo(kIODTNVRAMPanicInfoKey
)) return false;
318 // Make sure the object is of the correct type.
319 propType
= getOFVariableType(aKey
);
321 case kOFVariableTypeBoolean
:
322 propObject
= OSDynamicCast(OSBoolean
, anObject
);
325 case kOFVariableTypeNumber
:
326 propObject
= OSDynamicCast(OSNumber
, anObject
);
329 case kOFVariableTypeString
:
330 propObject
= OSDynamicCast(OSString
, anObject
);
333 case kOFVariableTypeData
:
334 propObject
= OSDynamicCast(OSData
, anObject
);
335 if (propObject
== 0) {
336 tmpString
= OSDynamicCast(OSString
, anObject
);
337 if (tmpString
!= 0) {
338 propObject
= OSData::withBytes(tmpString
->getCStringNoCopy(),
339 tmpString
->getLength());
345 if (propObject
== 0) return false;
347 result
= _ofDict
->setObject(aKey
, propObject
);
350 if (getPlatform()->getBootROMType() == 0) {
351 updateOWBootArgs(aKey
, propObject
);
354 _ofImageDirty
= true;
360 void IODTNVRAM::removeProperty(const OSSymbol
*aKey
)
365 if (_ofDict
== 0) return;
367 // Verify permissions.
368 result
= IOUserClient::clientHasPrivilege(current_task(), kIOClientPrivilegeAdministrator
);
369 if (result
!= kIOReturnSuccess
) {
370 propPerm
= getOFVariablePerm(aKey
);
371 if (propPerm
!= kOFVariablePermUserWrite
) return;
374 // Don't allow removal of properties on old world machines.
375 if (getPlatform()->getBootROMType() == 0) return;
377 // Don't allow change of 'aapl,panic-info'.
378 if (aKey
->isEqualTo(kIODTNVRAMPanicInfoKey
)) return;
380 // If the object exists, remove it from the dictionary.
381 result
= _ofDict
->getObject(aKey
) != 0;
383 _ofDict
->removeObject(aKey
);
385 _ofImageDirty
= true;
389 IOReturn
IODTNVRAM::setProperties(OSObject
*properties
)
394 const OSString
*tmpStr
;
396 OSCollectionIterator
*iter
;
398 dict
= OSDynamicCast(OSDictionary
, properties
);
399 if (dict
== 0) return kIOReturnBadArgument
;
401 iter
= OSCollectionIterator::withCollection(dict
);
402 if (iter
== 0) return kIOReturnBadArgument
;
405 key
= OSDynamicCast(OSSymbol
, iter
->getNextObject());
408 object
= dict
->getObject(key
);
409 if (object
== 0) continue;
411 if (key
->isEqualTo(kIONVRAMDeletePropertyKey
)) {
412 tmpStr
= OSDynamicCast(OSString
, object
);
414 key
= OSSymbol::withString(tmpStr
);
422 result
= setProperty(key
, object
);
428 if (result
) return kIOReturnSuccess
;
429 else return kIOReturnError
;
432 IOReturn
IODTNVRAM::readXPRAM(IOByteCount offset
, UInt8
*buffer
,
435 if (_xpramImage
== 0) return kIOReturnUnsupported
;
437 if ((buffer
== 0) || (length
== 0) ||
438 (offset
+ length
> kIODTNVRAMXPRAMSize
))
439 return kIOReturnBadArgument
;
441 bcopy(_nvramImage
+ _xpramPartitionOffset
+ offset
, buffer
, length
);
443 return kIOReturnSuccess
;
446 IOReturn
IODTNVRAM::writeXPRAM(IOByteCount offset
, UInt8
*buffer
,
449 if (_xpramImage
== 0) return kIOReturnUnsupported
;
451 if ((buffer
== 0) || (length
== 0) ||
452 (offset
+ length
> kIODTNVRAMXPRAMSize
))
453 return kIOReturnBadArgument
;
455 bcopy(buffer
, _nvramImage
+ _xpramPartitionOffset
+ offset
, length
);
457 _nvramImageDirty
= true;
459 return kIOReturnSuccess
;
462 IOReturn
IODTNVRAM::readNVRAMProperty(IORegistryEntry
*entry
,
463 const OSSymbol
**name
,
468 if (getPlatform()->getBootROMType())
469 err
= readNVRAMPropertyType1(entry
, name
, value
);
471 err
= readNVRAMPropertyType0(entry
, name
, value
);
476 IOReturn
IODTNVRAM::writeNVRAMProperty(IORegistryEntry
*entry
,
477 const OSSymbol
*name
,
482 if (getPlatform()->getBootROMType())
483 err
= writeNVRAMPropertyType1(entry
, name
, value
);
485 err
= writeNVRAMPropertyType0(entry
, name
, value
);
490 OSDictionary
*IODTNVRAM::getNVRAMPartitions(void)
492 return _nvramPartitionLengths
;
495 IOReturn
IODTNVRAM::readNVRAMPartition(const OSSymbol
*partitionID
,
496 IOByteCount offset
, UInt8
*buffer
,
499 OSNumber
*partitionOffsetNumber
, *partitionLengthNumber
;
500 UInt32 partitionOffset
, partitionLength
;
502 partitionOffsetNumber
=
503 (OSNumber
*)_nvramPartitionOffsets
->getObject(partitionID
);
504 partitionLengthNumber
=
505 (OSNumber
*)_nvramPartitionLengths
->getObject(partitionID
);
507 if ((partitionOffsetNumber
== 0) || (partitionLengthNumber
== 0))
508 return kIOReturnNotFound
;
510 partitionOffset
= partitionOffsetNumber
->unsigned32BitValue();
511 partitionLength
= partitionLengthNumber
->unsigned32BitValue();
513 if ((buffer
== 0) || (length
== 0) ||
514 (offset
+ length
> partitionLength
))
515 return kIOReturnBadArgument
;
517 bcopy(_nvramImage
+ partitionOffset
+ offset
, buffer
, length
);
519 return kIOReturnSuccess
;
522 IOReturn
IODTNVRAM::writeNVRAMPartition(const OSSymbol
*partitionID
,
523 IOByteCount offset
, UInt8
*buffer
,
526 OSNumber
*partitionOffsetNumber
, *partitionLengthNumber
;
527 UInt32 partitionOffset
, partitionLength
;
529 partitionOffsetNumber
=
530 (OSNumber
*)_nvramPartitionOffsets
->getObject(partitionID
);
531 partitionLengthNumber
=
532 (OSNumber
*)_nvramPartitionLengths
->getObject(partitionID
);
534 if ((partitionOffsetNumber
== 0) || (partitionLengthNumber
== 0))
535 return kIOReturnNotFound
;
537 partitionOffset
= partitionOffsetNumber
->unsigned32BitValue();
538 partitionLength
= partitionLengthNumber
->unsigned32BitValue();
540 if ((buffer
== 0) || (length
== 0) ||
541 (offset
+ length
> partitionLength
))
542 return kIOReturnBadArgument
;
544 bcopy(buffer
, _nvramImage
+ partitionOffset
+ offset
, length
);
546 _nvramImageDirty
= true;
548 return kIOReturnSuccess
;
551 UInt32
IODTNVRAM::savePanicInfo(UInt8
*buffer
, IOByteCount length
)
553 if ((_piImage
== 0) || (length
<= 0)) return 0;
555 if (length
> (_piPartitionSize
- 4))
556 length
= _piPartitionSize
- 4;
558 // Save the Panic Info.
559 bcopy(buffer
, _piImage
+ 4, length
);
561 // Save the Panic Info length.
562 *(UInt32
*)_piImage
= length
;
564 _nvramImageDirty
= true;
566 * This prevents OF variables from being committed if the system has panicked
568 _systemPaniced
= true;
569 /* The call to sync() forces the NVRAM controller to write the panic info
570 * partition to NVRAM.
579 UInt8
IODTNVRAM::calculatePartitionChecksum(UInt8
*partitionHeader
)
581 UInt8 cnt
, isum
, csum
= 0;
583 for (cnt
= 0; cnt
< 0x10; cnt
++) {
584 isum
= csum
+ partitionHeader
[cnt
];
585 if (isum
< csum
) isum
++;
592 struct OWVariablesHeader
{
607 typedef struct OWVariablesHeader OWVariablesHeader
;
609 IOReturn
IODTNVRAM::initOFVariables(void)
611 UInt32 cnt
, propOffset
, propType
;
612 UInt8
*propName
, *propData
;
613 UInt32 propNameLength
, propDataLength
;
614 const OSSymbol
*propSymbol
;
615 OSObject
*propObject
;
616 OWVariablesHeader
*owHeader
;
618 if (_ofImage
== 0) return kIOReturnNotReady
;
620 _ofDict
= OSDictionary::withCapacity(1);
621 if (_ofDict
== 0) return kIOReturnNoMemory
;
623 if (getPlatform()->getBootROMType()) {
625 while (cnt
< _ofPartitionSize
) {
626 // Break if there is no name.
627 if (_ofImage
[cnt
] == '\0') break;
629 // Find the length of the name.
630 propName
= _ofImage
+ cnt
;
631 for (propNameLength
= 0; (cnt
+ propNameLength
) < _ofPartitionSize
;
633 if (_ofImage
[cnt
+ propNameLength
] == '=') break;
636 // Break if the name goes past the end of the partition.
637 if ((cnt
+ propNameLength
) >= _ofPartitionSize
) break;
638 cnt
+= propNameLength
+ 1;
640 propData
= _ofImage
+ cnt
;
641 for (propDataLength
= 0; (cnt
+ propDataLength
) < _ofPartitionSize
;
643 if (_ofImage
[cnt
+ propDataLength
] == '\0') break;
646 // Break if the data goes past the end of the partition.
647 if ((cnt
+ propDataLength
) >= _ofPartitionSize
) break;
648 cnt
+= propDataLength
+ 1;
650 if (convertPropToObject(propName
, propNameLength
,
651 propData
, propDataLength
,
652 &propSymbol
, &propObject
)) {
653 _ofDict
->setObject(propSymbol
, propObject
);
654 propSymbol
->release();
655 propObject
->release();
659 // Create the boot-args property if it is not in the dictionary.
660 if (_ofDict
->getObject("boot-args") == 0) {
661 propObject
= OSString::withCStringNoCopy("");
662 if (propObject
!= 0) {
663 _ofDict
->setObject("boot-args", propObject
);
664 propObject
->release();
668 // Create the 'aapl,panic-info' property if needed.
670 propDataLength
= *(UInt32
*)_piImage
;
671 if ((propDataLength
!= 0) && (propDataLength
<= (_piPartitionSize
- 4))) {
672 propObject
= OSData::withBytes(_piImage
+ 4, propDataLength
);
673 _ofDict
->setObject(kIODTNVRAMPanicInfoKey
, propObject
);
674 propObject
->release();
676 // Clear the length from _piImage and mark dirty.
677 *(UInt32
*)_piImage
= 0;
678 _nvramImageDirty
= true;
682 owHeader
= (OWVariablesHeader
*)_ofImage
;
683 if (!validateOWChecksum(_ofImage
)) {
686 return kIOReturnBadMedia
;
691 if (!getOWVariableInfo(cnt
++, &propSymbol
, &propType
, &propOffset
))
695 case kOFVariableTypeBoolean
:
696 propObject
= OSBoolean::withBoolean(owHeader
->owFlags
& propOffset
);
699 case kOFVariableTypeNumber
:
700 propObject
= OSNumber::withNumber(owHeader
->owNumbers
[propOffset
], 32);
703 case kOFVariableTypeString
:
704 propData
= _ofImage
+ owHeader
->owStrings
[propOffset
].offset
-
706 propDataLength
= owHeader
->owStrings
[propOffset
].length
;
707 propName
= IONew(UInt8
, propDataLength
+ 1);
709 strncpy((char *)propName
, (const char *)propData
, propDataLength
);
710 propName
[propDataLength
] = '\0';
711 propObject
= OSString::withCString((const char *)propName
);
712 IODelete(propName
, UInt8
, propDataLength
+ 1);
717 if (propObject
== 0) break;
719 _ofDict
->setObject(propSymbol
, propObject
);
720 propSymbol
->release();
721 propObject
->release();
724 // Create the boot-args property.
725 propSymbol
= OSSymbol::withCString("boot-command");
726 if (propSymbol
!= 0) {
727 propObject
= _ofDict
->getObject(propSymbol
);
728 if (propObject
!= 0) {
729 updateOWBootArgs(propSymbol
, propObject
);
731 propSymbol
->release();
735 return kIOReturnSuccess
;
738 IOReturn
IODTNVRAM::syncOFVariables(void)
741 UInt32 cnt
, length
, maxLength
;
742 UInt32 curOffset
, tmpOffset
, tmpType
, tmpDataLength
;
743 UInt8
*buffer
, *tmpBuffer
;
744 const UInt8
*tmpData
;
745 const OSSymbol
*tmpSymbol
;
747 OSBoolean
*tmpBoolean
;
750 OSCollectionIterator
*iter
;
751 OWVariablesHeader
*owHeader
, *owHeaderOld
;
753 if ((_ofImage
== 0) || (_ofDict
== 0)) return kIOReturnNotReady
;
755 if (!_ofImageDirty
) return kIOReturnSuccess
;
757 if (getPlatform()->getBootROMType()) {
758 buffer
= tmpBuffer
= IONew(UInt8
, _ofPartitionSize
);
759 if (buffer
== 0) return kIOReturnNoMemory
;
760 bzero(buffer
, _ofPartitionSize
);
763 maxLength
= _ofPartitionSize
;
765 iter
= OSCollectionIterator::withCollection(_ofDict
);
766 if (iter
== 0) ok
= false;
769 tmpSymbol
= OSDynamicCast(OSSymbol
, iter
->getNextObject());
770 if (tmpSymbol
== 0) break;
772 // Don't save 'aapl,panic-info'.
773 if (tmpSymbol
->isEqualTo(kIODTNVRAMPanicInfoKey
)) continue;
775 tmpObject
= _ofDict
->getObject(tmpSymbol
);
778 ok
= convertObjectToProp(tmpBuffer
, &length
, tmpSymbol
, tmpObject
);
787 bcopy(buffer
, _ofImage
, _ofPartitionSize
);
790 IODelete(buffer
, UInt8
, _ofPartitionSize
);
792 if (!ok
) return kIOReturnBadArgument
;
794 buffer
= IONew(UInt8
, _ofPartitionSize
);
795 if (buffer
== 0) return kIOReturnNoMemory
;
796 bzero(buffer
, _ofPartitionSize
);
798 owHeader
= (OWVariablesHeader
*)buffer
;
799 owHeaderOld
= (OWVariablesHeader
*)_ofImage
;
801 owHeader
->owMagic
= owHeaderOld
->owMagic
;
802 owHeader
->owVersion
= owHeaderOld
->owVersion
;
803 owHeader
->owPages
= owHeaderOld
->owPages
;
805 curOffset
= _ofPartitionSize
;
810 if (!getOWVariableInfo(cnt
++, &tmpSymbol
, &tmpType
, &tmpOffset
))
813 tmpObject
= _ofDict
->getObject(tmpSymbol
);
816 case kOFVariableTypeBoolean
:
817 tmpBoolean
= OSDynamicCast(OSBoolean
, tmpObject
);
818 if (tmpBoolean
->getValue()) owHeader
->owFlags
|= tmpOffset
;
821 case kOFVariableTypeNumber
:
822 tmpNumber
= OSDynamicCast(OSNumber
, tmpObject
);
823 owHeader
->owNumbers
[tmpOffset
] = tmpNumber
->unsigned32BitValue();
826 case kOFVariableTypeString
:
827 tmpString
= OSDynamicCast(OSString
, tmpObject
);
828 tmpData
= (const UInt8
*)tmpString
->getCStringNoCopy();
829 tmpDataLength
= tmpString
->getLength();
831 if ((curOffset
- tmpDataLength
) < sizeof(OWVariablesHeader
)) {
836 owHeader
->owStrings
[tmpOffset
].length
= tmpDataLength
;
837 curOffset
-= tmpDataLength
;
838 owHeader
->owStrings
[tmpOffset
].offset
= curOffset
+ _ofPartitionOffset
;
839 if (tmpDataLength
!= 0)
840 bcopy(tmpData
, buffer
+ curOffset
, tmpDataLength
);
846 owHeader
->owHere
= _ofPartitionOffset
+ sizeof(OWVariablesHeader
);
847 owHeader
->owTop
= _ofPartitionOffset
+ curOffset
;
848 owHeader
->owNext
= 0;
850 owHeader
->owChecksum
= 0;
851 owHeader
->owChecksum
= ~generateOWChecksum(buffer
);
853 bcopy(buffer
, _ofImage
, _ofPartitionSize
);
856 IODelete(buffer
, UInt8
, _ofPartitionSize
);
858 if (!ok
) return kIOReturnBadArgument
;
861 _ofImageDirty
= false;
862 _nvramImageDirty
= true;
864 return kIOReturnSuccess
;
868 const char *variableName
;
871 SInt32 variableOffset
;
873 typedef struct OFVariable OFVariable
;
876 kOWVariableOffsetNumber
= 8,
877 kOWVariableOffsetString
= 17
880 OFVariable gOFVariables
[] = {
881 {"little-endian?", kOFVariableTypeBoolean
, kOFVariablePermUserRead
, 0},
882 {"real-mode?", kOFVariableTypeBoolean
, kOFVariablePermUserRead
, 1},
883 {"auto-boot?", kOFVariableTypeBoolean
, kOFVariablePermUserRead
, 2},
884 {"diag-switch?", kOFVariableTypeBoolean
, kOFVariablePermUserRead
, 3},
885 {"fcode-debug?", kOFVariableTypeBoolean
, kOFVariablePermUserRead
, 4},
886 {"oem-banner?", kOFVariableTypeBoolean
, kOFVariablePermUserRead
, 5},
887 {"oem-logo?", kOFVariableTypeBoolean
, kOFVariablePermUserRead
, 6},
888 {"use-nvramrc?", kOFVariableTypeBoolean
, kOFVariablePermUserRead
, 7},
889 {"use-generic?", kOFVariableTypeBoolean
, kOFVariablePermUserRead
, -1},
890 {"default-mac-address?", kOFVariableTypeBoolean
, kOFVariablePermUserRead
,-1},
891 {"real-base", kOFVariableTypeNumber
, kOFVariablePermUserRead
, 8},
892 {"real-size", kOFVariableTypeNumber
, kOFVariablePermUserRead
, 9},
893 {"virt-base", kOFVariableTypeNumber
, kOFVariablePermUserRead
, 10},
894 {"virt-size", kOFVariableTypeNumber
, kOFVariablePermUserRead
, 11},
895 {"load-base", kOFVariableTypeNumber
, kOFVariablePermUserRead
, 12},
896 {"pci-probe-list", kOFVariableTypeNumber
, kOFVariablePermUserRead
, 13},
897 {"pci-probe-mask", kOFVariableTypeNumber
, kOFVariablePermUserRead
, -1},
898 {"screen-#columns", kOFVariableTypeNumber
, kOFVariablePermUserRead
, 14},
899 {"screen-#rows", kOFVariableTypeNumber
, kOFVariablePermUserRead
, 15},
900 {"selftest-#megs", kOFVariableTypeNumber
, kOFVariablePermUserRead
, 16},
901 {"boot-device", kOFVariableTypeString
, kOFVariablePermUserRead
, 17},
902 {"boot-file", kOFVariableTypeString
, kOFVariablePermUserRead
, 18},
903 {"boot-screen", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
904 {"console-screen", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
905 {"diag-device", kOFVariableTypeString
, kOFVariablePermUserRead
, 19},
906 {"diag-file", kOFVariableTypeString
, kOFVariablePermUserRead
, 20},
907 {"input-device", kOFVariableTypeString
, kOFVariablePermUserRead
, 21},
908 {"output-device", kOFVariableTypeString
, kOFVariablePermUserRead
, 22},
909 {"input-device-1", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
910 {"output-device-1", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
911 {"mouse-device", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
912 {"oem-banner", kOFVariableTypeString
, kOFVariablePermUserRead
, 23},
913 {"oem-logo", kOFVariableTypeString
, kOFVariablePermUserRead
, 24},
914 {"nvramrc", kOFVariableTypeString
, kOFVariablePermUserRead
, 25},
915 {"boot-command", kOFVariableTypeString
, kOFVariablePermUserRead
, 26},
916 {"default-client-ip", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
917 {"default-server-ip", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
918 {"default-gateway-ip", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
919 {"default-subnet-mask", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
920 {"default-router-ip", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
921 {"boot-script", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
922 {"boot-args", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
923 {"aapl,pci", kOFVariableTypeData
, kOFVariablePermRootOnly
, -1},
924 {"security-mode", kOFVariableTypeString
, kOFVariablePermUserRead
, -1},
925 {"security-password", kOFVariableTypeData
, kOFVariablePermRootOnly
, -1},
926 {"boot-image", kOFVariableTypeData
, kOFVariablePermUserWrite
, -1},
927 {0, kOFVariableTypeData
, kOFVariablePermUserRead
, -1}
930 UInt32
IODTNVRAM::getOFVariableType(const OSSymbol
*propSymbol
) const
934 ofVar
= gOFVariables
;
936 if ((ofVar
->variableName
== 0) ||
937 propSymbol
->isEqualTo(ofVar
->variableName
)) break;
941 return ofVar
->variableType
;
944 UInt32
IODTNVRAM::getOFVariablePerm(const OSSymbol
*propSymbol
) const
948 ofVar
= gOFVariables
;
950 if ((ofVar
->variableName
== 0) ||
951 propSymbol
->isEqualTo(ofVar
->variableName
)) break;
955 return ofVar
->variablePerm
;
958 bool IODTNVRAM::getOWVariableInfo(UInt32 variableNumber
, const OSSymbol
**propSymbol
,
959 UInt32
*propType
, UInt32
*propOffset
)
963 ofVar
= gOFVariables
;
965 if (ofVar
->variableName
== 0) return false;
967 if (ofVar
->variableOffset
== (SInt32
) variableNumber
) break;
972 *propSymbol
= OSSymbol::withCStringNoCopy(ofVar
->variableName
);
973 *propType
= ofVar
->variableType
;
976 case kOFVariableTypeBoolean
:
977 *propOffset
= 1 << (31 - variableNumber
);
980 case kOFVariableTypeNumber
:
981 *propOffset
= variableNumber
- kOWVariableOffsetNumber
;
984 case kOFVariableTypeString
:
985 *propOffset
= variableNumber
- kOWVariableOffsetString
;
992 bool IODTNVRAM::convertPropToObject(UInt8
*propName
, UInt32 propNameLength
,
993 UInt8
*propData
, UInt32 propDataLength
,
994 const OSSymbol
**propSymbol
,
995 OSObject
**propObject
)
998 const OSSymbol
*tmpSymbol
;
1000 OSNumber
*tmpNumber
;
1001 OSString
*tmpString
;
1003 // Create the symbol.
1004 propName
[propNameLength
] = '\0';
1005 tmpSymbol
= OSSymbol::withCString((const char *)propName
);
1006 propName
[propNameLength
] = '=';
1007 if (tmpSymbol
== 0) {
1011 propType
= getOFVariableType(tmpSymbol
);
1013 // Create the object.
1016 case kOFVariableTypeBoolean
:
1017 if (!strncmp("true", (const char *)propData
, propDataLength
)) {
1018 tmpObject
= kOSBooleanTrue
;
1019 } else if (!strncmp("false", (const char *)propData
, propDataLength
)) {
1020 tmpObject
= kOSBooleanFalse
;
1024 case kOFVariableTypeNumber
:
1025 tmpNumber
= OSNumber::withNumber(strtol((const char *)propData
, 0, 0), 32);
1026 if (tmpNumber
!= 0) tmpObject
= tmpNumber
;
1029 case kOFVariableTypeString
:
1030 tmpString
= OSString::withCString((const char *)propData
);
1031 if (tmpString
!= 0) tmpObject
= tmpString
;
1034 case kOFVariableTypeData
:
1035 tmpObject
= unescapeBytesToData(propData
, propDataLength
);
1039 if (tmpObject
== 0) {
1040 tmpSymbol
->release();
1044 *propSymbol
= tmpSymbol
;
1045 *propObject
= tmpObject
;
1050 bool IODTNVRAM::convertObjectToProp(UInt8
*buffer
, UInt32
*length
,
1051 const OSSymbol
*propSymbol
, OSObject
*propObject
)
1053 const UInt8
*propName
;
1054 UInt32 propNameLength
, propDataLength
;
1055 UInt32 propType
, tmpValue
;
1056 OSBoolean
*tmpBoolean
= 0;
1057 OSNumber
*tmpNumber
= 0;
1058 OSString
*tmpString
= 0;
1059 OSData
*tmpData
= 0;
1061 propName
= (const UInt8
*)propSymbol
->getCStringNoCopy();
1062 propNameLength
= propSymbol
->getLength();
1063 propType
= getOFVariableType(propSymbol
);
1065 // Get the size of the data.
1066 propDataLength
= 0xFFFFFFFF;
1068 case kOFVariableTypeBoolean
:
1069 tmpBoolean
= OSDynamicCast(OSBoolean
, propObject
);
1070 if (tmpBoolean
!= 0) propDataLength
= 5;
1073 case kOFVariableTypeNumber
:
1074 tmpNumber
= OSDynamicCast(OSNumber
, propObject
);
1075 if (tmpNumber
!= 0) propDataLength
= 10;
1078 case kOFVariableTypeString
:
1079 tmpString
= OSDynamicCast(OSString
, propObject
);
1080 if (tmpString
!= 0) propDataLength
= tmpString
->getLength();
1083 case kOFVariableTypeData
:
1084 tmpData
= OSDynamicCast(OSData
, propObject
);
1086 tmpData
= escapeDataToData(tmpData
);
1087 propDataLength
= tmpData
->getLength();
1092 // Make sure the propertySize is known and will fit.
1093 if (propDataLength
== 0xFFFFFFFF) return false;
1094 if ((propNameLength
+ propDataLength
+ 2) > *length
) return false;
1096 // Copy the property name equal sign.
1097 buffer
+= snprintf((char *)buffer
, *length
, "%s=", propName
);
1100 case kOFVariableTypeBoolean
:
1101 if (tmpBoolean
->getValue()) {
1102 strlcpy((char *)buffer
, "true", *length
- propNameLength
);
1104 strlcpy((char *)buffer
, "false", *length
- propNameLength
);
1108 case kOFVariableTypeNumber
:
1109 tmpValue
= tmpNumber
->unsigned32BitValue();
1110 if (tmpValue
== 0xFFFFFFFF) {
1111 strlcpy((char *)buffer
, "-1", *length
- propNameLength
);
1112 } else if (tmpValue
< 1000) {
1113 snprintf((char *)buffer
, *length
- propNameLength
, "%ld", tmpValue
);
1115 snprintf((char *)buffer
, *length
- propNameLength
, "0x%lx", tmpValue
);
1119 case kOFVariableTypeString
:
1120 strlcpy((char *)buffer
, tmpString
->getCStringNoCopy(), *length
- propNameLength
);
1123 case kOFVariableTypeData
:
1124 bcopy(tmpData
->getBytesNoCopy(), buffer
, propDataLength
);
1129 propDataLength
= strlen((const char *)buffer
);
1131 *length
= propNameLength
+ propDataLength
+ 2;
1137 UInt16
IODTNVRAM::generateOWChecksum(UInt8
*buffer
)
1139 UInt32 cnt
, checksum
= 0;
1140 UInt16
*tmpBuffer
= (UInt16
*)buffer
;
1142 for (cnt
= 0; cnt
< _ofPartitionSize
/ 2; cnt
++)
1143 checksum
+= tmpBuffer
[cnt
];
1145 return checksum
% 0x0000FFFF;
1148 bool IODTNVRAM::validateOWChecksum(UInt8
*buffer
)
1150 UInt32 cnt
, checksum
, sum
= 0;
1151 UInt16
*tmpBuffer
= (UInt16
*)buffer
;
1153 for (cnt
= 0; cnt
< _ofPartitionSize
/ 2; cnt
++)
1154 sum
+= tmpBuffer
[cnt
];
1156 checksum
= (sum
>> 16) + (sum
& 0x0000FFFF);
1157 if (checksum
== 0x10000) checksum
--;
1158 checksum
= (checksum
^ 0x0000FFFF) & 0x0000FFFF;
1160 return checksum
== 0;
1163 void IODTNVRAM::updateOWBootArgs(const OSSymbol
*key
, OSObject
*value
)
1165 bool wasBootArgs
, bootr
= false;
1167 OSString
*tmpString
, *bootCommand
, *bootArgs
= 0;
1168 const UInt8
*bootCommandData
, *bootArgsData
;
1170 UInt32 bootCommandDataLength
, bootArgsDataLength
, tmpDataLength
;
1172 tmpString
= OSDynamicCast(OSString
, value
);
1173 if (tmpString
== 0) return;
1175 if (key
->isEqualTo("boot-command")) {
1176 wasBootArgs
= false;
1177 bootCommand
= tmpString
;
1178 } else if (key
->isEqualTo("boot-args")) {
1180 bootArgs
= tmpString
;
1181 bootCommand
= OSDynamicCast(OSString
, _ofDict
->getObject("boot-command"));
1182 if (bootCommand
== 0) return;
1185 bootCommandData
= (const UInt8
*)bootCommand
->getCStringNoCopy();
1186 bootCommandDataLength
= bootCommand
->getLength();
1188 if (bootCommandData
== 0) return;
1190 for (cnt
= 0; cnt
< bootCommandDataLength
; cnt
++) {
1191 if ((bootCommandData
[cnt
] == 'b') &&
1192 !strncmp("bootr", (const char *)bootCommandData
+ cnt
, 5)) {
1194 while (bootCommandData
[cnt
] == ' ') cnt
++;
1200 _ofDict
->removeObject("boot-args");
1205 bootArgsData
= (const UInt8
*)bootArgs
->getCStringNoCopy();
1206 bootArgsDataLength
= bootArgs
->getLength();
1207 if (bootArgsData
== 0) return;
1209 tmpDataLength
= cnt
+ bootArgsDataLength
;
1210 tmpData
= IONew(UInt8
, tmpDataLength
+ 1);
1211 if (tmpData
== 0) return;
1213 cnt
-= strlcpy((char *)tmpData
, (const char *)bootCommandData
, cnt
);
1214 strlcat((char *)tmpData
, (const char *)bootArgsData
, cnt
);
1216 bootCommand
= OSString::withCString((const char *)tmpData
);
1217 if (bootCommand
!= 0) {
1218 _ofDict
->setObject("boot-command", bootCommand
);
1219 bootCommand
->release();
1222 IODelete(tmpData
, UInt8
, tmpDataLength
+ 1);
1224 bootArgs
= OSString::withCString((const char *)(bootCommandData
+ cnt
));
1225 if (bootArgs
!= 0) {
1226 _ofDict
->setObject("boot-args", bootArgs
);
1227 bootArgs
->release();
1233 // Private methods for Name Registry access.
1236 kMaxNVNameLength
= 4,
1237 kMaxNVDataLength
= 8
1240 #pragma options align=mac68k
1241 struct NVRAMProperty
1243 IONVRAMDescriptor header
;
1245 UInt8 name
[ kMaxNVNameLength
];
1247 UInt8 data
[ kMaxNVDataLength
];
1249 #pragma options align=reset
1251 bool IODTNVRAM::searchNVRAMProperty(IONVRAMDescriptor
*hdr
, UInt32
*where
)
1256 nvEnd
= *((UInt16
*)_nrImage
);
1257 if(getPlatform()->getBootROMType()) {
1258 // on NewWorld, offset to partition start
1261 // on old world, absolute
1262 nvEnd
-= _nrPartitionOffset
;
1264 if((nvEnd
< 0) || (nvEnd
>= kIODTNVRAMNameRegistrySize
))
1268 while ((offset
+ sizeof(NVRAMProperty
)) <= (UInt32
)nvEnd
) {
1269 if (bcmp(_nrImage
+ offset
, hdr
, sizeof(*hdr
)) == 0) {
1273 offset
+= sizeof(NVRAMProperty
);
1276 if ((nvEnd
+ sizeof(NVRAMProperty
)) <= kIODTNVRAMNameRegistrySize
)
1284 IOReturn
IODTNVRAM::readNVRAMPropertyType0(IORegistryEntry
*entry
,
1285 const OSSymbol
**name
,
1288 IONVRAMDescriptor hdr
;
1289 NVRAMProperty
*prop
;
1293 char nameBuf
[kMaxNVNameLength
+ 1];
1295 if (_nrImage
== 0) return kIOReturnUnsupported
;
1296 if ((entry
== 0) || (name
== 0) || (value
== 0)) return kIOReturnBadArgument
;
1298 err
= IODTMakeNVDescriptor(entry
, &hdr
);
1299 if (err
!= kIOReturnSuccess
) return err
;
1301 if (searchNVRAMProperty(&hdr
, &offset
)) {
1302 prop
= (NVRAMProperty
*)(_nrImage
+ offset
);
1304 length
= prop
->nameLength
;
1305 if (length
> kMaxNVNameLength
) length
= kMaxNVNameLength
;
1306 strncpy(nameBuf
, (const char *)prop
->name
, length
);
1307 nameBuf
[length
] = 0;
1308 *name
= OSSymbol::withCString(nameBuf
);
1310 length
= prop
->dataLength
;
1311 if (length
> kMaxNVDataLength
) length
= kMaxNVDataLength
;
1312 *value
= OSData::withBytes(prop
->data
, length
);
1314 if ((*name
!= 0) && (*value
!= 0)) return kIOReturnSuccess
;
1315 else return kIOReturnNoMemory
;
1318 return kIOReturnNoResources
;
1321 IOReturn
IODTNVRAM::writeNVRAMPropertyType0(IORegistryEntry
*entry
,
1322 const OSSymbol
*name
,
1325 IONVRAMDescriptor hdr
;
1326 NVRAMProperty
*prop
;
1327 IOByteCount nameLength
;
1328 IOByteCount dataLength
;
1334 if (_nrImage
== 0) return kIOReturnUnsupported
;
1335 if ((entry
== 0) || (name
== 0) || (value
== 0)) return kIOReturnBadArgument
;
1337 nameLength
= name
->getLength();
1338 dataLength
= value
->getLength();
1339 if (nameLength
> kMaxNVNameLength
) return kIOReturnNoSpace
;
1340 if (dataLength
> kMaxNVDataLength
) return kIOReturnNoSpace
;
1342 err
= IODTMakeNVDescriptor(entry
, &hdr
);
1343 if (err
!= kIOReturnSuccess
) return err
;
1345 exists
= searchNVRAMProperty(&hdr
, &offset
);
1346 if (offset
== 0) return kIOReturnNoMemory
;
1348 prop
= (NVRAMProperty
*)(_nrImage
+ offset
);
1349 if (!exists
) bcopy(&hdr
, &prop
->header
, sizeof(hdr
));
1351 prop
->nameLength
= nameLength
;
1352 bcopy(name
->getCStringNoCopy(), prop
->name
, nameLength
);
1353 prop
->dataLength
= dataLength
;
1354 bcopy(value
->getBytesNoCopy(), prop
->data
, dataLength
);
1357 nvLength
= offset
+ sizeof(NVRAMProperty
);
1358 if (getPlatform()->getBootROMType())
1361 nvLength
+= _nrPartitionOffset
;
1362 *((UInt16
*)_nrImage
) = nvLength
;
1365 _nvramImageDirty
= true;
1370 OSData
*IODTNVRAM::unescapeBytesToData(const UInt8
*bytes
, UInt32 length
)
1373 UInt32 totalLength
= 0;
1378 // Calculate the actual length of the data.
1381 for (cnt
= 0; cnt
< length
;) {
1382 byte
= bytes
[cnt
++];
1384 byte
= bytes
[cnt
++];
1392 totalLength
+= cnt2
;
1396 // Create an empty OSData of the correct size.
1397 data
= OSData::withCapacity(totalLength
);
1399 for (cnt
= 0; cnt
< length
;) {
1400 byte
= bytes
[cnt
++];
1402 byte
= bytes
[cnt
++];
1404 byte
= (byte
& 0x80) ? 0xFF : 0x00;
1407 data
->appendByte(byte
, cnt2
);
1415 OSData
* IODTNVRAM::escapeDataToData(OSData
* value
)
1418 const UInt8
* startPtr
;
1419 const UInt8
* endPtr
;
1420 const UInt8
* wherePtr
;
1424 wherePtr
= (const UInt8
*) value
->getBytesNoCopy();
1425 endPtr
= wherePtr
+ value
->getLength();
1427 result
= OSData::withCapacity(endPtr
- wherePtr
);
1431 while (wherePtr
< endPtr
) {
1432 startPtr
= wherePtr
;
1434 if ((byte
== 0x00) || (byte
== 0xFF)) {
1436 ((wherePtr
- startPtr
) < 0x80) && (wherePtr
< endPtr
) && (byte
== *wherePtr
);
1438 ok
&= result
->appendByte(0xff, 1);
1439 byte
= (byte
& 0x80) | (wherePtr
- startPtr
);
1441 ok
&= result
->appendByte(byte
, 1);
1443 ok
&= result
->appendByte(0, 1);
1453 static bool IsApplePropertyName(const char * propName
)
1456 while ((c
= *propName
++)) {
1457 if ((c
>= 'A') && (c
<= 'Z'))
1464 IOReturn
IODTNVRAM::readNVRAMPropertyType1(IORegistryEntry
*entry
,
1465 const OSSymbol
**name
,
1468 IOReturn err
= kIOReturnNoResources
;
1470 const UInt8
*startPtr
;
1471 const UInt8
*endPtr
;
1472 const UInt8
*wherePtr
;
1473 const UInt8
*nvPath
= 0;
1474 const char *nvName
= 0;
1475 const char *resultName
= 0;
1476 const UInt8
*resultValue
= 0;
1477 UInt32 resultValueLen
= 0;
1480 if (_ofDict
== 0) return err
;
1481 data
= OSDynamicCast(OSData
, _ofDict
->getObject(_registryPropertiesKey
));
1482 if (data
== 0) return err
;
1484 startPtr
= (const UInt8
*) data
->getBytesNoCopy();
1485 endPtr
= startPtr
+ data
->getLength();
1487 wherePtr
= startPtr
;
1488 while (wherePtr
< endPtr
) {
1489 byte
= *(wherePtr
++);
1495 else if (nvName
== 0)
1496 nvName
= (const char *) startPtr
;
1498 IORegistryEntry
* compareEntry
= IORegistryEntry::fromPath((const char *) nvPath
, gIODTPlane
);
1500 compareEntry
->release();
1501 if (entry
== compareEntry
) {
1502 bool appleProp
= IsApplePropertyName(nvName
);
1503 if (!appleProp
|| !resultName
) {
1504 resultName
= nvName
;
1505 resultValue
= startPtr
;
1506 resultValueLen
= wherePtr
- startPtr
- 1;
1514 startPtr
= wherePtr
;
1517 *name
= OSSymbol::withCString(resultName
);
1518 *value
= unescapeBytesToData(resultValue
, resultValueLen
);
1519 if ((*name
!= 0) && (*value
!= 0))
1520 err
= kIOReturnSuccess
;
1522 err
= kIOReturnNoMemory
;
1527 IOReturn
IODTNVRAM::writeNVRAMPropertyType1(IORegistryEntry
*entry
,
1528 const OSSymbol
*propName
,
1533 const UInt8
*startPtr
;
1534 const UInt8
*propStart
;
1535 const UInt8
*endPtr
;
1536 const UInt8
*wherePtr
;
1537 const UInt8
*nvPath
= 0;
1538 const char *nvName
= 0;
1543 bool settingAppleProp
;
1545 if (_ofDict
== 0) return kIOReturnNoResources
;
1547 settingAppleProp
= IsApplePropertyName(propName
->getCStringNoCopy());
1549 // copy over existing properties for other entries
1551 oldData
= OSDynamicCast(OSData
, _ofDict
->getObject(_registryPropertiesKey
));
1553 startPtr
= (const UInt8
*) oldData
->getBytesNoCopy();
1554 endPtr
= startPtr
+ oldData
->getLength();
1556 propStart
= startPtr
;
1557 wherePtr
= startPtr
;
1558 while (wherePtr
< endPtr
) {
1559 byte
= *(wherePtr
++);
1564 else if (nvName
== 0)
1565 nvName
= (const char *) startPtr
;
1567 IORegistryEntry
* compareEntry
= IORegistryEntry::fromPath((const char *) nvPath
, gIODTPlane
);
1569 compareEntry
->release();
1570 if (entry
== compareEntry
) {
1571 if ((settingAppleProp
&& propName
->isEqualTo(nvName
))
1572 || (!settingAppleProp
&& !IsApplePropertyName(nvName
))) {
1573 // delete old property (nvPath -> wherePtr)
1574 data
= OSData::withBytes(propStart
, nvPath
- propStart
);
1576 ok
&= data
->appendBytes(wherePtr
, endPtr
- wherePtr
);
1584 startPtr
= wherePtr
;
1588 // make the new property
1592 data
= OSData::withData(oldData
);
1594 data
= OSData::withCapacity(16);
1596 return kIOReturnNoMemory
;
1599 if (value
&& value
->getLength()) {
1600 // get entries in path
1601 OSArray
*array
= OSArray::withCapacity(5);
1604 return kIOReturnNoMemory
;
1607 array
->setObject(entry
);
1608 while ((entry
= entry
->getParentEntry(gIODTPlane
)));
1611 for (int i
= array
->getCount() - 3;
1612 (entry
= (IORegistryEntry
*) array
->getObject(i
));
1615 name
= entry
->getName(gIODTPlane
);
1616 comp
= entry
->getLocation(gIODTPlane
);
1617 if( comp
&& (0 == strncmp("pci", name
, sizeof("pci")))
1618 && (0 == strncmp("80000000", comp
, sizeof("80000000")))) {
1620 comp
= "/pci@80000000";
1623 ok
&= data
->appendBytes("/@", 2);
1627 ok
&= data
->appendByte('/', 1);
1631 ok
&= data
->appendBytes(comp
, strlen(comp
));
1633 ok
&= data
->appendByte(0, 1);
1637 ok
&= data
->appendBytes(propName
->getCStringNoCopy(), propName
->getLength() + 1);
1639 // append escaped data
1640 oldData
= escapeDataToData(value
);
1641 ok
&= (oldData
!= 0);
1643 ok
&= data
->appendBytes(oldData
);
1646 ok
= _ofDict
->setObject(_registryPropertiesKey
, data
);
1648 _ofImageDirty
= true;
1652 return ok
? kIOReturnSuccess
: kIOReturnNoMemory
;