]> git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IONVRAM.cpp
17cd841bcefa9f0776f5d9487e0c3ae9f5c0cc50
[apple/xnu.git] / iokit / Kernel / IONVRAM.cpp
1 /*
2 * Copyright (c) 1998-2006 Apple Computer, Inc. All rights reserved.
3 * Copyright (c) 2007-2012 Apple Inc. All rights reserved.
4 *
5 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 *
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.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 *
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.
26 *
27 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 */
29
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>
38
39 #if CONFIG_MACF
40 extern "C" {
41 #include <security/mac.h>
42 #include <security/mac_framework.h>
43 };
44 #endif /* MAC */
45
46 #define super IOService
47
48 #define kIONVRAMPrivilege kIOClientPrivilegeAdministrator
49 //#define kIONVRAMPrivilege kIOClientPrivilegeLocalUser
50
51 OSDefineMetaClassAndStructors(IODTNVRAM, IOService);
52
53 bool IODTNVRAM::init(IORegistryEntry *old, const IORegistryPlane *plane)
54 {
55 OSDictionary *dict;
56
57 if (!super::init(old, plane)) return false;
58
59 dict = OSDictionary::withCapacity(1);
60 if (dict == 0) return false;
61 setPropertyTable(dict);
62
63 _nvramImage = IONew(UInt8, kIODTNVRAMImageSize);
64 if (_nvramImage == 0) return false;
65
66 _nvramPartitionOffsets = OSDictionary::withCapacity(1);
67 if (_nvramPartitionOffsets == 0) return false;
68
69 _nvramPartitionLengths = OSDictionary::withCapacity(1);
70 if (_nvramPartitionLengths == 0) return false;
71
72 _registryPropertiesKey = OSSymbol::withCStringNoCopy("aapl,pci");
73 if (_registryPropertiesKey == 0) return false;
74
75 // <rdar://problem/9529235> race condition possible between
76 // IODTNVRAM and IONVRAMController (restore loses boot-args)
77 initProxyData();
78
79 return true;
80 }
81
82 void IODTNVRAM::initProxyData(void)
83 {
84 IORegistryEntry *entry;
85 const char *key = "nvram-proxy-data";
86 OSObject *prop;
87 OSData *data;
88 const void *bytes;
89
90 entry = IORegistryEntry::fromPath("/chosen", gIODTPlane);
91 if (entry != 0) {
92 prop = entry->getProperty(key);
93 if (prop != 0) {
94 data = OSDynamicCast(OSData, prop);
95 if (data != 0) {
96 bytes = data->getBytesNoCopy();
97 if (bytes != 0) {
98 bcopy(bytes, _nvramImage, data->getLength());
99 initNVRAMImage();
100 _isProxied = true;
101 }
102 }
103 }
104 entry->removeProperty(key);
105 entry->release();
106 }
107 }
108
109 void IODTNVRAM::registerNVRAMController(IONVRAMController *nvram)
110 {
111 if (_nvramController != 0) return;
112
113 _nvramController = nvram;
114
115 // <rdar://problem/9529235> race condition possible between
116 // IODTNVRAM and IONVRAMController (restore loses boot-args)
117 if (!_isProxied) {
118 _nvramController->read(0, _nvramImage, kIODTNVRAMImageSize);
119 initNVRAMImage();
120 } else {
121 syncOFVariables();
122 }
123 }
124
125 void IODTNVRAM::initNVRAMImage(void)
126 {
127 char partitionID[18];
128 UInt32 partitionOffset, partitionLength;
129 UInt32 freePartitionOffset, freePartitionSize;
130 UInt32 currentLength, currentOffset = 0;
131 OSNumber *partitionOffsetNumber, *partitionLengthNumber;
132
133 // Find the offsets for the OF, XPRAM, NameRegistry and PanicInfo partitions.
134 _ofPartitionOffset = 0xFFFFFFFF;
135 _piPartitionOffset = 0xFFFFFFFF;
136 freePartitionOffset = 0xFFFFFFFF;
137 freePartitionSize = 0;
138
139 // Look through the partitions to find the OF, MacOS partitions.
140 while (currentOffset < kIODTNVRAMImageSize) {
141 currentLength = ((UInt16 *)(_nvramImage + currentOffset))[1] * 16;
142
143 partitionOffset = currentOffset + 16;
144 partitionLength = currentLength - 16;
145
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;
160 } else {
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';
167
168 partitionOffsetNumber = OSNumber::withNumber(partitionOffset, 32);
169 partitionLengthNumber = OSNumber::withNumber(partitionLength, 32);
170
171 // Save the partition offset and length
172 _nvramPartitionOffsets->setObject(partitionID, partitionOffsetNumber);
173 _nvramPartitionLengths->setObject(partitionID, partitionLengthNumber);
174
175 partitionOffsetNumber->release();
176 partitionLengthNumber->release();
177 }
178 currentOffset += currentLength;
179 }
180
181 if (_ofPartitionOffset != 0xFFFFFFFF)
182 _ofImage = _nvramImage + _ofPartitionOffset;
183
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);
193
194 // Calculate the partition offset and size.
195 _piPartitionOffset = freePartitionOffset + 0x10;
196 _piPartitionSize = 0x800;
197 if (_piPartitionSize + 0x20 > freePartitionSize)
198 _piPartitionSize = freePartitionSize - 0x20;
199
200 _piImage = _nvramImage + _piPartitionOffset;
201
202 // Zero the new partition.
203 bzero(_piImage, _piPartitionSize);
204
205 // Set the partition size.
206 *(UInt16 *)(_nvramImage + freePartitionOffset + 2) =
207 (_piPartitionSize / 0x10) + 1;
208
209 // Set the partition checksum.
210 _nvramImage[freePartitionOffset + 1] =
211 calculatePartitionChecksum(_nvramImage + freePartitionOffset);
212
213 // Calculate the free partition offset and size.
214 freePartitionOffset += _piPartitionSize + 0x10;
215 freePartitionSize -= _piPartitionSize + 0x10;
216
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);
230
231 if (_nvramController != 0) {
232 _nvramController->write(0, _nvramImage, kIODTNVRAMImageSize);
233 }
234 }
235 } else {
236 _piImage = _nvramImage + _piPartitionOffset;
237 }
238
239 _lastDeviceSync = 0;
240 _freshInterval = TRUE; // we will allow sync() even before the first 15 minutes have passed.
241
242 initOFVariables();
243 }
244
245 void IODTNVRAM::syncInternal(bool rateLimit)
246 {
247 // Don't try to perform controller operations if none has been registered.
248 if (_nvramController == 0) return;
249
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;
253
254 _nvramController->sync();
255 }
256
257 void IODTNVRAM::sync(void)
258 {
259 syncInternal(false);
260 }
261
262 bool IODTNVRAM::serializeProperties(OSSerialize *s) const
263 {
264 bool result, hasPrivilege;
265 UInt32 variablePerm;
266 const OSSymbol *key;
267 OSDictionary *dict;
268 OSCollectionIterator *iter = 0;
269
270 // Verify permissions.
271 hasPrivilege = (kIOReturnSuccess == IOUserClient::clientHasPrivilege(current_task(), kIONVRAMPrivilege));
272
273 if (_ofDict == 0) {
274 /* No nvram. Return an empty dictionary. */
275 dict = OSDictionary::withCapacity(1);
276 if (dict == 0) return false;
277 } else {
278 IOLockLock(_ofLock);
279 dict = OSDictionary::withDictionary(_ofDict);
280 IOLockUnlock(_ofLock);
281 if (dict == 0) return false;
282
283 /* Copy properties with client privilege. */
284 iter = OSCollectionIterator::withCollection(dict);
285 if (iter == 0) {
286 dict->release();
287 return false;
288 }
289 while (1) {
290 key = OSDynamicCast(OSSymbol, iter->getNextObject());
291 if (key == 0) break;
292
293 variablePerm = getOFVariablePerm(key);
294 if ((hasPrivilege || (variablePerm != kOFVariablePermRootOnly)) &&
295 ( ! (variablePerm == kOFVariablePermKernelOnly && current_task() != kernel_task) )
296 #if CONFIG_MACF
297 && (current_task() == kernel_task || mac_iokit_check_nvram_get(kauth_cred_get(), key->getCStringNoCopy()) == 0)
298 #endif
299 ) { }
300 else dict->removeObject(key);
301 }
302 }
303
304 result = dict->serialize(s);
305
306 dict->release();
307 if (iter != 0) iter->release();
308
309 return result;
310 }
311
312 OSObject *IODTNVRAM::copyProperty(const OSSymbol *aKey) const
313 {
314 IOReturn result;
315 UInt32 variablePerm;
316 OSObject *theObject;
317
318 if (_ofDict == 0) return 0;
319
320 // Verify permissions.
321 variablePerm = getOFVariablePerm(aKey);
322 result = IOUserClient::clientHasPrivilege(current_task(), kIONVRAMPrivilege);
323 if (result != kIOReturnSuccess) {
324 if (variablePerm == kOFVariablePermRootOnly) return 0;
325 }
326 if (variablePerm == kOFVariablePermKernelOnly && current_task() != kernel_task) return 0;
327
328 #if CONFIG_MACF
329 if (current_task() != kernel_task &&
330 mac_iokit_check_nvram_get(kauth_cred_get(), aKey->getCStringNoCopy()) != 0)
331 return 0;
332 #endif
333
334 IOLockLock(_ofLock);
335 theObject = _ofDict->getObject(aKey);
336 if (theObject) theObject->retain();
337 IOLockUnlock(_ofLock);
338
339 return theObject;
340 }
341
342 OSObject *IODTNVRAM::copyProperty(const char *aKey) const
343 {
344 const OSSymbol *keySymbol;
345 OSObject *theObject = 0;
346
347 keySymbol = OSSymbol::withCString(aKey);
348 if (keySymbol != 0) {
349 theObject = copyProperty(keySymbol);
350 keySymbol->release();
351 }
352
353 return theObject;
354 }
355
356 OSObject *IODTNVRAM::getProperty(const OSSymbol *aKey) const
357 {
358 OSObject *theObject;
359
360 theObject = copyProperty(aKey);
361 if (theObject) theObject->release();
362
363 return theObject;
364 }
365
366 OSObject *IODTNVRAM::getProperty(const char *aKey) const
367 {
368 OSObject *theObject;
369
370 theObject = copyProperty(aKey);
371 if (theObject) theObject->release();
372
373 return theObject;
374 }
375
376 bool IODTNVRAM::setProperty(const OSSymbol *aKey, OSObject *anObject)
377 {
378 bool result;
379 UInt32 propType, propPerm;
380 OSString *tmpString;
381 OSObject *propObject = 0;
382
383 if (_ofDict == 0) return false;
384
385 // Verify permissions.
386 propPerm = getOFVariablePerm(aKey);
387 result = IOUserClient::clientHasPrivilege(current_task(), kIONVRAMPrivilege);
388 if (result != kIOReturnSuccess) {
389 if (propPerm != kOFVariablePermUserWrite) return false;
390 }
391 if (propPerm == kOFVariablePermKernelOnly && current_task() != kernel_task) return 0;
392
393 // Don't allow change of 'aapl,panic-info'.
394 if (aKey->isEqualTo(kIODTNVRAMPanicInfoKey)) return false;
395
396 #if CONFIG_MACF
397 if (current_task() != kernel_task &&
398 mac_iokit_check_nvram_set(kauth_cred_get(), aKey->getCStringNoCopy(), anObject) != 0)
399 return false;
400 #endif
401
402 // Make sure the object is of the correct type.
403 propType = getOFVariableType(aKey);
404 switch (propType) {
405 case kOFVariableTypeBoolean :
406 propObject = OSDynamicCast(OSBoolean, anObject);
407 break;
408
409 case kOFVariableTypeNumber :
410 propObject = OSDynamicCast(OSNumber, anObject);
411 break;
412
413 case kOFVariableTypeString :
414 propObject = OSDynamicCast(OSString, anObject);
415 break;
416
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());
424 }
425 }
426 break;
427 }
428
429 if (propObject == 0) return false;
430
431 IOLockLock(_ofLock);
432 result = _ofDict->setObject(aKey, propObject);
433 IOLockUnlock(_ofLock);
434
435 if (result) {
436 syncOFVariables();
437 }
438
439 return result;
440 }
441
442 void IODTNVRAM::removeProperty(const OSSymbol *aKey)
443 {
444 bool result;
445 UInt32 propPerm;
446
447 if (_ofDict == 0) return;
448
449 // Verify permissions.
450 propPerm = getOFVariablePerm(aKey);
451 result = IOUserClient::clientHasPrivilege(current_task(), kIOClientPrivilegeAdministrator);
452 if (result != kIOReturnSuccess) {
453 if (propPerm != kOFVariablePermUserWrite) return;
454 }
455 if (propPerm == kOFVariablePermKernelOnly && current_task() != kernel_task) return;
456
457 // Don't allow change of 'aapl,panic-info'.
458 if (aKey->isEqualTo(kIODTNVRAMPanicInfoKey)) return;
459
460 #if CONFIG_MACF
461 if (current_task() != kernel_task &&
462 mac_iokit_check_nvram_delete(kauth_cred_get(), aKey->getCStringNoCopy()) != 0)
463 return;
464 #endif
465
466 // If the object exists, remove it from the dictionary.
467
468 IOLockLock(_ofLock);
469 result = _ofDict->getObject(aKey) != 0;
470 if (result) {
471 _ofDict->removeObject(aKey);
472 }
473 IOLockUnlock(_ofLock);
474
475 if (result) {
476 syncOFVariables();
477 }
478 }
479
480 IOReturn IODTNVRAM::setProperties(OSObject *properties)
481 {
482 bool result = true;
483 OSObject *object;
484 const OSSymbol *key;
485 const OSString *tmpStr;
486 OSDictionary *dict;
487 OSCollectionIterator *iter;
488
489 dict = OSDynamicCast(OSDictionary, properties);
490 if (dict == 0) return kIOReturnBadArgument;
491
492 iter = OSCollectionIterator::withCollection(dict);
493 if (iter == 0) return kIOReturnBadArgument;
494
495 while (result) {
496 key = OSDynamicCast(OSSymbol, iter->getNextObject());
497 if (key == 0) break;
498
499 object = dict->getObject(key);
500 if (object == 0) continue;
501
502 if (key->isEqualTo(kIONVRAMDeletePropertyKey)) {
503 tmpStr = OSDynamicCast(OSString, object);
504 if (tmpStr != 0) {
505 key = OSSymbol::withString(tmpStr);
506 removeProperty(key);
507 key->release();
508 result = true;
509 } else {
510 result = false;
511 }
512 } else if(key->isEqualTo(kIONVRAMSyncNowPropertyKey) || key->isEqualTo(kIONVRAMForceSyncNowPropertyKey)) {
513 tmpStr = OSDynamicCast(OSString, object);
514 if (tmpStr != 0) {
515
516 result = true;
517
518 // We still want to throttle NVRAM commit rate for SyncNow. ForceSyncNow is provided as a really big hammer.
519
520 syncInternal(key->isEqualTo(kIONVRAMSyncNowPropertyKey));
521
522 } else {
523 result = false;
524 }
525 }
526 else {
527 result = setProperty(key, object);
528 }
529
530 }
531
532 iter->release();
533
534 if (result) return kIOReturnSuccess;
535 else return kIOReturnError;
536 }
537
538 IOReturn IODTNVRAM::readXPRAM(IOByteCount offset, UInt8 *buffer,
539 IOByteCount length)
540 {
541 return kIOReturnUnsupported;
542 }
543
544 IOReturn IODTNVRAM::writeXPRAM(IOByteCount offset, UInt8 *buffer,
545 IOByteCount length)
546 {
547 return kIOReturnUnsupported;
548 }
549
550 IOReturn IODTNVRAM::readNVRAMProperty(IORegistryEntry *entry,
551 const OSSymbol **name,
552 OSData **value)
553 {
554 IOReturn err;
555
556 err = readNVRAMPropertyType1(entry, name, value);
557
558 return err;
559 }
560
561 IOReturn IODTNVRAM::writeNVRAMProperty(IORegistryEntry *entry,
562 const OSSymbol *name,
563 OSData *value)
564 {
565 IOReturn err;
566
567 err = writeNVRAMPropertyType1(entry, name, value);
568
569 return err;
570 }
571
572 OSDictionary *IODTNVRAM::getNVRAMPartitions(void)
573 {
574 return _nvramPartitionLengths;
575 }
576
577 IOReturn IODTNVRAM::readNVRAMPartition(const OSSymbol *partitionID,
578 IOByteCount offset, UInt8 *buffer,
579 IOByteCount length)
580 {
581 OSNumber *partitionOffsetNumber, *partitionLengthNumber;
582 UInt32 partitionOffset, partitionLength;
583
584 partitionOffsetNumber =
585 (OSNumber *)_nvramPartitionOffsets->getObject(partitionID);
586 partitionLengthNumber =
587 (OSNumber *)_nvramPartitionLengths->getObject(partitionID);
588
589 if ((partitionOffsetNumber == 0) || (partitionLengthNumber == 0))
590 return kIOReturnNotFound;
591
592 partitionOffset = partitionOffsetNumber->unsigned32BitValue();
593 partitionLength = partitionLengthNumber->unsigned32BitValue();
594
595 if ((buffer == 0) || (length == 0) ||
596 (offset + length > partitionLength))
597 return kIOReturnBadArgument;
598
599 bcopy(_nvramImage + partitionOffset + offset, buffer, length);
600
601 return kIOReturnSuccess;
602 }
603
604 IOReturn IODTNVRAM::writeNVRAMPartition(const OSSymbol *partitionID,
605 IOByteCount offset, UInt8 *buffer,
606 IOByteCount length)
607 {
608 OSNumber *partitionOffsetNumber, *partitionLengthNumber;
609 UInt32 partitionOffset, partitionLength;
610
611 partitionOffsetNumber =
612 (OSNumber *)_nvramPartitionOffsets->getObject(partitionID);
613 partitionLengthNumber =
614 (OSNumber *)_nvramPartitionLengths->getObject(partitionID);
615
616 if ((partitionOffsetNumber == 0) || (partitionLengthNumber == 0))
617 return kIOReturnNotFound;
618
619 partitionOffset = partitionOffsetNumber->unsigned32BitValue();
620 partitionLength = partitionLengthNumber->unsigned32BitValue();
621
622 if ((buffer == 0) || (length == 0) ||
623 (offset + length > partitionLength))
624 return kIOReturnBadArgument;
625
626 bcopy(buffer, _nvramImage + partitionOffset + offset, length);
627
628 if (_nvramController != 0) {
629 _nvramController->write(0, _nvramImage, kIODTNVRAMImageSize);
630 }
631
632 return kIOReturnSuccess;
633 }
634
635 IOByteCount IODTNVRAM::savePanicInfo(UInt8 *buffer, IOByteCount length)
636 {
637 if ((_piImage == 0) || (length <= 0)) return 0;
638
639 if (length > (_piPartitionSize - 4))
640 length = _piPartitionSize - 4;
641
642 // Save the Panic Info.
643 bcopy(buffer, _piImage + 4, length);
644
645 // Save the Panic Info length.
646 *(UInt32 *)_piImage = length;
647
648 if (_nvramController != 0) {
649 _nvramController->write(0, _nvramImage, kIODTNVRAMImageSize);
650 }
651 /*
652 * This prevents OF variables from being committed if the system has panicked
653 */
654 _systemPaniced = true;
655 /* The call to sync() forces the NVRAM controller to write the panic info
656 * partition to NVRAM.
657 */
658 sync();
659
660 return length;
661 }
662
663 // Private methods
664
665 UInt8 IODTNVRAM::calculatePartitionChecksum(UInt8 *partitionHeader)
666 {
667 UInt8 cnt, isum, csum = 0;
668
669 for (cnt = 0; cnt < 0x10; cnt++) {
670 isum = csum + partitionHeader[cnt];
671 if (isum < csum) isum++;
672 csum = isum;
673 }
674
675 return csum;
676 }
677
678 IOReturn IODTNVRAM::initOFVariables(void)
679 {
680 UInt32 cnt;
681 UInt8 *propName, *propData;
682 UInt32 propNameLength, propDataLength;
683 const OSSymbol *propSymbol;
684 OSObject *propObject;
685
686 if (_ofImage == 0) return kIOReturnNotReady;
687
688 _ofDict = OSDictionary::withCapacity(1);
689 _ofLock = IOLockAlloc();
690 if (!_ofDict || !_ofLock) return kIOReturnNoMemory;
691
692 cnt = 0;
693 while (cnt < _ofPartitionSize) {
694 // Break if there is no name.
695 if (_ofImage[cnt] == '\0') break;
696
697 // Find the length of the name.
698 propName = _ofImage + cnt;
699 for (propNameLength = 0; (cnt + propNameLength) < _ofPartitionSize;
700 propNameLength++) {
701 if (_ofImage[cnt + propNameLength] == '=') break;
702 }
703
704 // Break if the name goes past the end of the partition.
705 if ((cnt + propNameLength) >= _ofPartitionSize) break;
706 cnt += propNameLength + 1;
707
708 propData = _ofImage + cnt;
709 for (propDataLength = 0; (cnt + propDataLength) < _ofPartitionSize;
710 propDataLength++) {
711 if (_ofImage[cnt + propDataLength] == '\0') break;
712 }
713
714 // Break if the data goes past the end of the partition.
715 if ((cnt + propDataLength) >= _ofPartitionSize) break;
716 cnt += propDataLength + 1;
717
718 if (convertPropToObject(propName, propNameLength,
719 propData, propDataLength,
720 &propSymbol, &propObject)) {
721 _ofDict->setObject(propSymbol, propObject);
722 propSymbol->release();
723 propObject->release();
724 }
725 }
726
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();
733 }
734 }
735
736 // Create the 'aapl,panic-info' property if needed.
737 if (_piImage != 0) {
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();
743
744 // Clear the length from _piImage and mark dirty.
745 *(UInt32 *)_piImage = 0;
746 if (_nvramController != 0) {
747 _nvramController->write(0, _nvramImage, kIODTNVRAMImageSize);
748 }
749 }
750 }
751
752 return kIOReturnSuccess;
753 }
754
755 IOReturn IODTNVRAM::syncOFVariables(void)
756 {
757 bool ok;
758 UInt32 length, maxLength;
759 UInt8 *buffer, *tmpBuffer;
760 const OSSymbol *tmpSymbol;
761 OSObject *tmpObject;
762 OSCollectionIterator *iter;
763
764 if ((_ofImage == 0) || (_ofDict == 0) || _systemPaniced) return kIOReturnNotReady;
765
766 buffer = tmpBuffer = IONew(UInt8, _ofPartitionSize);
767 if (buffer == 0) return kIOReturnNoMemory;
768 bzero(buffer, _ofPartitionSize);
769
770 ok = true;
771 maxLength = _ofPartitionSize;
772
773 IOLockLock(_ofLock);
774 iter = OSCollectionIterator::withCollection(_ofDict);
775 if (iter == 0) ok = false;
776
777 while (ok) {
778 tmpSymbol = OSDynamicCast(OSSymbol, iter->getNextObject());
779 if (tmpSymbol == 0) break;
780
781 // Don't save 'aapl,panic-info'.
782 if (tmpSymbol->isEqualTo(kIODTNVRAMPanicInfoKey)) continue;
783
784 tmpObject = _ofDict->getObject(tmpSymbol);
785
786 length = maxLength;
787 ok = convertObjectToProp(tmpBuffer, &length, tmpSymbol, tmpObject);
788 if (ok) {
789 tmpBuffer += length;
790 maxLength -= length;
791 }
792 }
793 iter->release();
794 IOLockUnlock(_ofLock);
795
796 if (ok) {
797 bcopy(buffer, _ofImage, _ofPartitionSize);
798 }
799
800 IODelete(buffer, UInt8, _ofPartitionSize);
801
802 if (!ok) return kIOReturnBadArgument;
803
804 if (_nvramController != 0) {
805 _nvramController->write(0, _nvramImage, kIODTNVRAMImageSize);
806 }
807
808 return kIOReturnSuccess;
809 }
810
811 struct OFVariable {
812 const char *variableName;
813 UInt32 variableType;
814 UInt32 variablePerm;
815 SInt32 variableOffset;
816 };
817 typedef struct OFVariable OFVariable;
818
819 enum {
820 kOWVariableOffsetNumber = 8,
821 kOWVariableOffsetString = 17
822 };
823
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}
873 };
874
875 UInt32 IODTNVRAM::getOFVariableType(const OSSymbol *propSymbol) const
876 {
877 OFVariable *ofVar;
878
879 ofVar = gOFVariables;
880 while (1) {
881 if ((ofVar->variableName == 0) ||
882 propSymbol->isEqualTo(ofVar->variableName)) break;
883 ofVar++;
884 }
885
886 return ofVar->variableType;
887 }
888
889 UInt32 IODTNVRAM::getOFVariablePerm(const OSSymbol *propSymbol) const
890 {
891 OFVariable *ofVar;
892
893 ofVar = gOFVariables;
894 while (1) {
895 if ((ofVar->variableName == 0) ||
896 propSymbol->isEqualTo(ofVar->variableName)) break;
897 ofVar++;
898 }
899
900 return ofVar->variablePerm;
901 }
902
903 bool IODTNVRAM::getOWVariableInfo(UInt32 variableNumber, const OSSymbol **propSymbol,
904 UInt32 *propType, UInt32 *propOffset)
905 {
906 OFVariable *ofVar;
907
908 ofVar = gOFVariables;
909 while (1) {
910 if (ofVar->variableName == 0) return false;
911
912 if (ofVar->variableOffset == (SInt32) variableNumber) break;
913
914 ofVar++;
915 }
916
917 *propSymbol = OSSymbol::withCStringNoCopy(ofVar->variableName);
918 *propType = ofVar->variableType;
919
920 switch (*propType) {
921 case kOFVariableTypeBoolean :
922 *propOffset = 1 << (31 - variableNumber);
923 break;
924
925 case kOFVariableTypeNumber :
926 *propOffset = variableNumber - kOWVariableOffsetNumber;
927 break;
928
929 case kOFVariableTypeString :
930 *propOffset = variableNumber - kOWVariableOffsetString;
931 break;
932 }
933
934 return true;
935 }
936
937 bool IODTNVRAM::convertPropToObject(UInt8 *propName, UInt32 propNameLength,
938 UInt8 *propData, UInt32 propDataLength,
939 const OSSymbol **propSymbol,
940 OSObject **propObject)
941 {
942 UInt32 propType;
943 const OSSymbol *tmpSymbol;
944 OSObject *tmpObject;
945 OSNumber *tmpNumber;
946 OSString *tmpString;
947
948 // Create the symbol.
949 propName[propNameLength] = '\0';
950 tmpSymbol = OSSymbol::withCString((const char *)propName);
951 propName[propNameLength] = '=';
952 if (tmpSymbol == 0) {
953 return false;
954 }
955
956 propType = getOFVariableType(tmpSymbol);
957
958 // Create the object.
959 tmpObject = 0;
960 switch (propType) {
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;
966 }
967 break;
968
969 case kOFVariableTypeNumber :
970 tmpNumber = OSNumber::withNumber(strtol((const char *)propData, 0, 0), 32);
971 if (tmpNumber != 0) tmpObject = tmpNumber;
972 break;
973
974 case kOFVariableTypeString :
975 tmpString = OSString::withCString((const char *)propData);
976 if (tmpString != 0) tmpObject = tmpString;
977 break;
978
979 case kOFVariableTypeData :
980 tmpObject = unescapeBytesToData(propData, propDataLength);
981 break;
982 }
983
984 if (tmpObject == 0) {
985 tmpSymbol->release();
986 return false;
987 }
988
989 *propSymbol = tmpSymbol;
990 *propObject = tmpObject;
991
992 return true;
993 }
994
995 bool IODTNVRAM::convertObjectToProp(UInt8 *buffer, UInt32 *length,
996 const OSSymbol *propSymbol, OSObject *propObject)
997 {
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;
1005
1006 propName = (const UInt8 *)propSymbol->getCStringNoCopy();
1007 propNameLength = propSymbol->getLength();
1008 propType = getOFVariableType(propSymbol);
1009
1010 // Get the size of the data.
1011 propDataLength = 0xFFFFFFFF;
1012 switch (propType) {
1013 case kOFVariableTypeBoolean :
1014 tmpBoolean = OSDynamicCast(OSBoolean, propObject);
1015 if (tmpBoolean != 0) propDataLength = 5;
1016 break;
1017
1018 case kOFVariableTypeNumber :
1019 tmpNumber = OSDynamicCast(OSNumber, propObject);
1020 if (tmpNumber != 0) propDataLength = 10;
1021 break;
1022
1023 case kOFVariableTypeString :
1024 tmpString = OSDynamicCast(OSString, propObject);
1025 if (tmpString != 0) propDataLength = tmpString->getLength();
1026 break;
1027
1028 case kOFVariableTypeData :
1029 tmpData = OSDynamicCast(OSData, propObject);
1030 if (tmpData != 0) {
1031 tmpData = escapeDataToData(tmpData);
1032 propDataLength = tmpData->getLength();
1033 }
1034 break;
1035 }
1036
1037 // Make sure the propertySize is known and will fit.
1038 if (propDataLength == 0xFFFFFFFF) return false;
1039 if ((propNameLength + propDataLength + 2) > *length) return false;
1040
1041 // Copy the property name equal sign.
1042 buffer += snprintf((char *)buffer, *length, "%s=", propName);
1043
1044 switch (propType) {
1045 case kOFVariableTypeBoolean :
1046 if (tmpBoolean->getValue()) {
1047 strlcpy((char *)buffer, "true", *length - propNameLength);
1048 } else {
1049 strlcpy((char *)buffer, "false", *length - propNameLength);
1050 }
1051 break;
1052
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);
1059 } else {
1060 snprintf((char *)buffer, *length - propNameLength, "0x%x", (uint32_t)tmpValue);
1061 }
1062 break;
1063
1064 case kOFVariableTypeString :
1065 strlcpy((char *)buffer, tmpString->getCStringNoCopy(), *length - propNameLength);
1066 break;
1067
1068 case kOFVariableTypeData :
1069 bcopy(tmpData->getBytesNoCopy(), buffer, propDataLength);
1070 tmpData->release();
1071 break;
1072 }
1073
1074 propDataLength = strlen((const char *)buffer);
1075
1076 *length = propNameLength + propDataLength + 2;
1077
1078 return true;
1079 }
1080
1081
1082 UInt16 IODTNVRAM::generateOWChecksum(UInt8 *buffer)
1083 {
1084 UInt32 cnt, checksum = 0;
1085 UInt16 *tmpBuffer = (UInt16 *)buffer;
1086
1087 for (cnt = 0; cnt < _ofPartitionSize / 2; cnt++)
1088 checksum += tmpBuffer[cnt];
1089
1090 return checksum % 0x0000FFFF;
1091 }
1092
1093 bool IODTNVRAM::validateOWChecksum(UInt8 *buffer)
1094 {
1095 UInt32 cnt, checksum, sum = 0;
1096 UInt16 *tmpBuffer = (UInt16 *)buffer;
1097
1098 for (cnt = 0; cnt < _ofPartitionSize / 2; cnt++)
1099 sum += tmpBuffer[cnt];
1100
1101 checksum = (sum >> 16) + (sum & 0x0000FFFF);
1102 if (checksum == 0x10000) checksum--;
1103 checksum = (checksum ^ 0x0000FFFF) & 0x0000FFFF;
1104
1105 return checksum == 0;
1106 }
1107
1108 void IODTNVRAM::updateOWBootArgs(const OSSymbol *key, OSObject *value)
1109 {
1110 bool wasBootArgs, bootr = false;
1111 UInt32 cnt;
1112 OSString *tmpString, *bootCommand, *bootArgs = 0;
1113 const UInt8 *bootCommandData, *bootArgsData;
1114 UInt8 *tmpData;
1115 UInt32 bootCommandDataLength, bootArgsDataLength, tmpDataLength;
1116
1117 tmpString = OSDynamicCast(OSString, value);
1118 if (tmpString == 0) return;
1119
1120 if (key->isEqualTo("boot-command")) {
1121 wasBootArgs = false;
1122 bootCommand = tmpString;
1123 } else if (key->isEqualTo("boot-args")) {
1124 wasBootArgs = true;
1125 bootArgs = tmpString;
1126 bootCommand = OSDynamicCast(OSString, _ofDict->getObject("boot-command"));
1127 if (bootCommand == 0) return;
1128 } else return;
1129
1130 bootCommandData = (const UInt8 *)bootCommand->getCStringNoCopy();
1131 bootCommandDataLength = bootCommand->getLength();
1132
1133 if (bootCommandData == 0) return;
1134
1135 for (cnt = 0; cnt < bootCommandDataLength; cnt++) {
1136 if ((bootCommandData[cnt] == 'b') &&
1137 !strncmp("bootr", (const char *)bootCommandData + cnt, 5)) {
1138 cnt += 5;
1139 while (bootCommandData[cnt] == ' ') cnt++;
1140 bootr = true;
1141 break;
1142 }
1143 }
1144 if (!bootr) {
1145 _ofDict->removeObject("boot-args");
1146 return;
1147 }
1148
1149 if (wasBootArgs) {
1150 bootArgsData = (const UInt8 *)bootArgs->getCStringNoCopy();
1151 bootArgsDataLength = bootArgs->getLength();
1152 if (bootArgsData == 0) return;
1153
1154 tmpDataLength = cnt + bootArgsDataLength;
1155 tmpData = IONew(UInt8, tmpDataLength + 1);
1156 if (tmpData == 0) return;
1157
1158 cnt -= strlcpy((char *)tmpData, (const char *)bootCommandData, cnt);
1159 strlcat((char *)tmpData, (const char *)bootArgsData, cnt);
1160
1161 bootCommand = OSString::withCString((const char *)tmpData);
1162 if (bootCommand != 0) {
1163 _ofDict->setObject("boot-command", bootCommand);
1164 bootCommand->release();
1165 }
1166
1167 IODelete(tmpData, UInt8, tmpDataLength + 1);
1168 } else {
1169 bootArgs = OSString::withCString((const char *)(bootCommandData + cnt));
1170 if (bootArgs != 0) {
1171 _ofDict->setObject("boot-args", bootArgs);
1172 bootArgs->release();
1173 }
1174 }
1175 }
1176
1177 bool IODTNVRAM::searchNVRAMProperty(IONVRAMDescriptor *hdr, UInt32 *where)
1178 {
1179 return false;
1180 }
1181
1182 IOReturn IODTNVRAM::readNVRAMPropertyType0(IORegistryEntry *entry,
1183 const OSSymbol **name,
1184 OSData **value)
1185 {
1186 return kIOReturnUnsupported;
1187 }
1188
1189
1190 IOReturn IODTNVRAM::writeNVRAMPropertyType0(IORegistryEntry *entry,
1191 const OSSymbol *name,
1192 OSData *value)
1193 {
1194 return kIOReturnUnsupported;
1195 }
1196
1197
1198 OSData *IODTNVRAM::unescapeBytesToData(const UInt8 *bytes, UInt32 length)
1199 {
1200 OSData *data = 0;
1201 UInt32 totalLength = 0;
1202 UInt32 cnt, cnt2;
1203 UInt8 byte;
1204 bool ok;
1205
1206 // Calculate the actual length of the data.
1207 ok = true;
1208 totalLength = 0;
1209 for (cnt = 0; cnt < length;) {
1210 byte = bytes[cnt++];
1211 if (byte == 0xFF) {
1212 byte = bytes[cnt++];
1213 if (byte == 0x00) {
1214 ok = false;
1215 break;
1216 }
1217 cnt2 = byte & 0x7F;
1218 } else
1219 cnt2 = 1;
1220 totalLength += cnt2;
1221 }
1222
1223 if (ok) {
1224 // Create an empty OSData of the correct size.
1225 data = OSData::withCapacity(totalLength);
1226 if (data != 0) {
1227 for (cnt = 0; cnt < length;) {
1228 byte = bytes[cnt++];
1229 if (byte == 0xFF) {
1230 byte = bytes[cnt++];
1231 cnt2 = byte & 0x7F;
1232 byte = (byte & 0x80) ? 0xFF : 0x00;
1233 } else
1234 cnt2 = 1;
1235 data->appendByte(byte, cnt2);
1236 }
1237 }
1238 }
1239
1240 return data;
1241 }
1242
1243 OSData * IODTNVRAM::escapeDataToData(OSData * value)
1244 {
1245 OSData * result;
1246 const UInt8 * startPtr;
1247 const UInt8 * endPtr;
1248 const UInt8 * wherePtr;
1249 UInt8 byte;
1250 bool ok = true;
1251
1252 wherePtr = (const UInt8 *) value->getBytesNoCopy();
1253 endPtr = wherePtr + value->getLength();
1254
1255 result = OSData::withCapacity(endPtr - wherePtr);
1256 if (!result)
1257 return result;
1258
1259 while (wherePtr < endPtr) {
1260 startPtr = wherePtr;
1261 byte = *wherePtr++;
1262 if ((byte == 0x00) || (byte == 0xFF)) {
1263 for (;
1264 ((wherePtr - startPtr) < 0x80) && (wherePtr < endPtr) && (byte == *wherePtr);
1265 wherePtr++) {}
1266 ok &= result->appendByte(0xff, 1);
1267 byte = (byte & 0x80) | (wherePtr - startPtr);
1268 }
1269 ok &= result->appendByte(byte, 1);
1270 }
1271 ok &= result->appendByte(0, 1);
1272
1273 if (!ok) {
1274 result->release();
1275 result = 0;
1276 }
1277
1278 return result;
1279 }
1280
1281 static bool IsApplePropertyName(const char * propName)
1282 {
1283 char c;
1284 while ((c = *propName++)) {
1285 if ((c >= 'A') && (c <= 'Z'))
1286 break;
1287 }
1288
1289 return (c == 0);
1290 }
1291
1292 IOReturn IODTNVRAM::readNVRAMPropertyType1(IORegistryEntry *entry,
1293 const OSSymbol **name,
1294 OSData **value)
1295 {
1296 IOReturn err = kIOReturnNoResources;
1297 OSData *data;
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;
1306 UInt8 byte;
1307
1308 if (_ofDict == 0) return err;
1309
1310 IOLockLock(_ofLock);
1311 data = OSDynamicCast(OSData, _ofDict->getObject(_registryPropertiesKey));
1312 IOLockUnlock(_ofLock);
1313
1314 if (data == 0) return err;
1315
1316 startPtr = (const UInt8 *) data->getBytesNoCopy();
1317 endPtr = startPtr + data->getLength();
1318
1319 wherePtr = startPtr;
1320 while (wherePtr < endPtr) {
1321 byte = *(wherePtr++);
1322 if (byte)
1323 continue;
1324
1325 if (nvPath == 0)
1326 nvPath = startPtr;
1327 else if (nvName == 0)
1328 nvName = (const char *) startPtr;
1329 else {
1330 IORegistryEntry * compareEntry = IORegistryEntry::fromPath((const char *) nvPath, gIODTPlane);
1331 if (compareEntry)
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;
1339 }
1340 if (!appleProp)
1341 break;
1342 }
1343 nvPath = 0;
1344 nvName = 0;
1345 }
1346 startPtr = wherePtr;
1347 }
1348 if (resultName) {
1349 *name = OSSymbol::withCString(resultName);
1350 *value = unescapeBytesToData(resultValue, resultValueLen);
1351 if ((*name != 0) && (*value != 0))
1352 err = kIOReturnSuccess;
1353 else
1354 err = kIOReturnNoMemory;
1355 }
1356 return err;
1357 }
1358
1359 IOReturn IODTNVRAM::writeNVRAMPropertyType1(IORegistryEntry *entry,
1360 const OSSymbol *propName,
1361 OSData *value)
1362 {
1363 OSData *oldData;
1364 OSData *data = 0;
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;
1371 const char * comp;
1372 const char * name;
1373 UInt8 byte;
1374 bool ok = true;
1375 bool settingAppleProp;
1376
1377 if (_ofDict == 0) return kIOReturnNoResources;
1378
1379 settingAppleProp = IsApplePropertyName(propName->getCStringNoCopy());
1380
1381 // copy over existing properties for other entries
1382
1383 IOLockLock(_ofLock);
1384
1385 oldData = OSDynamicCast(OSData, _ofDict->getObject(_registryPropertiesKey));
1386 if (oldData) {
1387 startPtr = (const UInt8 *) oldData->getBytesNoCopy();
1388 endPtr = startPtr + oldData->getLength();
1389
1390 propStart = startPtr;
1391 wherePtr = startPtr;
1392 while (wherePtr < endPtr) {
1393 byte = *(wherePtr++);
1394 if (byte)
1395 continue;
1396 if (nvPath == 0)
1397 nvPath = startPtr;
1398 else if (nvName == 0)
1399 nvName = (const char *) startPtr;
1400 else {
1401 IORegistryEntry * compareEntry = IORegistryEntry::fromPath((const char *) nvPath, gIODTPlane);
1402 if (compareEntry)
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);
1409 if (data)
1410 ok &= data->appendBytes(wherePtr, endPtr - wherePtr);
1411 break;
1412 }
1413 }
1414 nvPath = 0;
1415 nvName = 0;
1416 }
1417
1418 startPtr = wherePtr;
1419 }
1420 }
1421
1422 // make the new property
1423
1424 if (!data) {
1425 if (oldData)
1426 data = OSData::withData(oldData);
1427 else
1428 data = OSData::withCapacity(16);
1429 if (!data) ok = false;
1430 }
1431
1432 if (ok && value && value->getLength()) do {
1433 // get entries in path
1434 OSArray *array = OSArray::withCapacity(5);
1435 if (!array) {
1436 ok = false;
1437 break;
1438 }
1439 do
1440 array->setObject(entry);
1441 while ((entry = entry->getParentEntry(gIODTPlane)));
1442
1443 // append path
1444 for (int i = array->getCount() - 3;
1445 (entry = (IORegistryEntry *) array->getObject(i));
1446 i--) {
1447
1448 name = entry->getName(gIODTPlane);
1449 comp = entry->getLocation(gIODTPlane);
1450 if (comp) ok &= data->appendBytes("/@", 2);
1451 else {
1452 if (!name) continue;
1453 ok &= data->appendByte('/', 1);
1454 comp = name;
1455 }
1456 ok &= data->appendBytes(comp, strlen(comp));
1457 }
1458 ok &= data->appendByte(0, 1);
1459 array->release();
1460
1461 // append prop name
1462 ok &= data->appendBytes(propName->getCStringNoCopy(), propName->getLength() + 1);
1463
1464 // append escaped data
1465 oldData = escapeDataToData(value);
1466 ok &= (oldData != 0);
1467 if (ok) ok &= data->appendBytes(oldData);
1468
1469 } while (false);
1470
1471 if (ok) {
1472 ok = _ofDict->setObject(_registryPropertiesKey, data);
1473 }
1474
1475 IOLockUnlock(_ofLock);
1476 if (data) data->release();
1477
1478 if (ok) syncOFVariables();
1479
1480 return ok ? kIOReturnSuccess : kIOReturnNoMemory;
1481 }
1482
1483 bool IODTNVRAM::safeToSync(void)
1484 {
1485 AbsoluteTime delta;
1486 UInt64 delta_ns;
1487 SInt32 delta_secs;
1488
1489 // delta interval went by
1490 clock_get_uptime(&delta);
1491
1492 // Figure it in seconds.
1493 absolutetime_to_nanoseconds(delta, &delta_ns);
1494 delta_secs = (SInt32)(delta_ns / NSEC_PER_SEC);
1495
1496 if ((delta_secs > (_lastDeviceSync + MIN_SYNC_NOW_INTERVAL)) || _freshInterval)
1497 {
1498 _lastDeviceSync = delta_secs;
1499 _freshInterval = FALSE;
1500 return TRUE;
1501 }
1502
1503 return FALSE;
1504 }