2 * Copyright (c) 2000-2016 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 cc -o nvram nvram.c -framework CoreFoundation -framework IOKit -Wall
27 #include <IOKit/IOKitLib.h>
28 #include <IOKit/IOKitKeys.h>
29 #include <IOKit/IOKitKeysPrivate.h>
30 #include <CoreFoundation/CoreFoundation.h>
32 #include <mach/mach_error.h>
36 static void UsageMessage(char *message
);
37 static void ParseFile(char *fileName
);
38 static void ParseXMLFile(char *fileName
);
39 static void SetOrGetOFVariable(char *str
);
40 static kern_return_t
GetOFVariable(char *name
, CFStringRef
*nameRef
,
42 static kern_return_t
SetOFVariable(char *name
, char *value
);
43 static void DeleteOFVariable(char *name
);
44 static void PrintOFVariables(void);
45 static void PrintOFVariable(const void *key
,const void *value
,void *context
);
46 static void SetOFVariableFromFile(const void *key
, const void *value
, void *context
);
47 static void ClearOFVariables(void);
48 static void ClearOFVariable(const void *key
,const void *value
,void *context
);
49 static CFTypeRef
ConvertValueToCFTypeRef(CFTypeID typeID
, char *value
);
51 static void NVRamSyncNow(char *name
);
54 static char *gToolName
;
55 static io_registry_entry_t gOptionsRef
;
57 static bool gUseForceSync
;
59 #if TARGET_OS_BRIDGE /* Stuff for nvram bridge -> intel */
61 #include <libMacEFIManager/MacEFIHostInterfaceAPI.h>
63 static kern_return_t
LinkMacNVRAMSymbols(void);
64 static kern_return_t
GetMacOFVariable(char *name
, char **value
);
65 static kern_return_t
SetMacOFVariable(char *name
, char *value
);
66 static kern_return_t
DeleteMacOFVariable(char *name
);
68 static bool gBridgeToIntel
;
69 static void *gDL_handle
;
70 static void *gNvramInterface
;
72 static void (*hostInterfaceInitialize_fptr
)(void);
73 static void *(*createNvramHostInterface_fptr
)(const char *handle
);
74 static kern_return_t (*destroyNvramHostInterface_fptr
)(void *interface
);
75 static kern_return_t (*getNVRAMVariable_fptr
)(void *interface
, char *name
, char **buffer
, uint32_t *size
);
76 static kern_return_t (*setNVRAMVariable_fptr
)(void *interface
, char *name
, char *buffer
);
77 static kern_return_t (*deleteNVRAMVariable_fptr
)(void *interface
, char *name
);
78 static void (*hostInterfaceDeinitialize_fptr
)(void); /* may not need? */
80 #endif /* TARGET_OS_BRIDGE */
82 int main(int argc
, char **argv
)
85 char *str
, errorMessage
[256];
87 mach_port_t masterPort
;
90 // Get the name of the command.
91 gToolName
= strrchr(argv
[0], '/');
92 if (gToolName
!= 0) gToolName
++;
93 else gToolName
= argv
[0];
95 result
= IOMasterPort(bootstrap_port
, &masterPort
);
96 if (result
!= KERN_SUCCESS
) {
97 errx(1, "Error getting the IOMaster port: %s",
98 mach_error_string(result
));
101 gOptionsRef
= IORegistryEntryFromPath(masterPort
, "IODeviceTree:/options");
102 if (gOptionsRef
== 0) {
103 errx(1, "nvram is not supported on this system");
106 for (cnt
= 1; cnt
< argc
; cnt
++) {
108 if (str
[0] == '-' && str
[1] != 0) {
109 // Parse the options.
110 for (str
+= 1 ; *str
; str
++) {
114 if (gBridgeToIntel
) {
115 fprintf(stderr
, "-p not supported for Mac NVRAM store.\n");
128 if (gBridgeToIntel
) {
129 fprintf(stderr
, "-f not supported for Mac NVRAM store.\n");
134 if (cnt
< argc
&& *argv
[cnt
] != '-') {
135 ParseFile(argv
[cnt
]);
137 UsageMessage("missing filename");
143 if (cnt
< argc
&& *argv
[cnt
] != '-') {
145 if (gBridgeToIntel
) {
146 if ((result
= DeleteMacOFVariable(argv
[cnt
])) != KERN_SUCCESS
) {
147 errx(1, "Error deleting variable - '%s': %s (0x%08x)", argv
[cnt
],
148 mach_error_string(result
), result
);
154 DeleteOFVariable(argv
[cnt
]);
157 UsageMessage("missing name");
163 if (gBridgeToIntel
) {
164 fprintf(stderr
, "-c not supported for Mac NVRAM store.\n");
171 // -s option is unadvertised -- advises the kernel more forcibly to
172 // commit the variable to nonvolatile storage
173 gUseForceSync
= true;
177 // -m option is unadvertised -- used to set nvram variables on the Intel side
178 // from the ARM side (Bridge -> Mac)
179 fprintf(stdout
, "Using Mac NVRAM store.\n");
181 LinkMacNVRAMSymbols();
182 gBridgeToIntel
= true;
187 strcpy(errorMessage
, "no such option as --");
188 errorMessage
[strlen(errorMessage
)-1] = *str
;
189 UsageMessage(errorMessage
);
193 // Other arguments will be firmware variable requests.
195 SetOrGetOFVariable(str
);
200 if (argcount
== 0 && gUseForceSync
== true) {
204 IOObjectRelease(gOptionsRef
);
209 // UsageMessage(message)
211 // Print the usage information and exit.
213 static void UsageMessage(char *message
)
215 warnx("(usage: %s)", message
);
217 printf("%s [-x] [-p] [-f filename] [-d name] [-c] name[=value] ...\n", gToolName
);
218 printf("\t-x use XML format for printing or reading variables\n");
219 printf("\t (must appear before -p or -f)\n");
220 printf("\t-p print all firmware variables\n");
221 printf("\t-f set firmware variables from a text file\n");
222 printf("\t-d delete the named variable\n");
223 printf("\t-c delete all variables\n");
224 printf("\tname=value set named variable\n");
225 printf("\tname print variable\n");
226 printf("Note that arguments and options are executed in order.\n");
232 // States for ParseFile.
243 kMaxStringSize
= 0x800,
248 // ParseFile(fileName)
250 // Open and parse the specified file.
252 static void ParseFile(char *fileName
)
254 long state
, ni
= 0, vi
= 0;
256 char name
[kMaxNameSize
];
257 char value
[kMaxStringSize
];
262 ParseXMLFile(fileName
);
266 patches
= fopen(fileName
, "r");
268 err(1, "Couldn't open patch file - '%s'", fileName
);
271 state
= kFirstColumn
;
272 while ((tc
= getc(patches
)) != EOF
) {
273 if(ni
==(kMaxNameSize
-1))
274 errx(1, "Name exceeded max length of %d", kMaxNameSize
);
275 if(vi
==(kMaxStringSize
-1))
276 errx(1, "Value exceeded max length of %d", kMaxStringSize
);
282 state
= kScanComment
;
283 } else if (tc
== '\n') {
284 // state stays kFirstColumn.
285 } else if (isspace(tc
)) {
288 state
= kCollectName
;
295 state
= kFirstColumn
;
297 // state stays kScanComment.
303 state
= kFirstColumn
;
304 } else if (isspace(tc
)) {
305 // state stays kFindName.
307 state
= kCollectName
;
315 warnx("Name must be followed by white space - '%s'", name
);
316 state
= kFirstColumn
;
317 } else if (isspace(tc
)) {
321 // state staus kCollectName.
326 case kContinueValue
:
329 } else if (isspace(tc
)) {
330 // state stays kFindValue or kContinueValue.
332 state
= kCollectValue
;
339 if (value
[vi
-1] == '\\') {
341 state
= kContinueValue
;
346 // state stays kCollectValue.
352 if (state
== kSetenv
) {
355 if ((kret
= SetOFVariable(name
, value
)) != KERN_SUCCESS
) {
356 errx(1, "Error setting variable - '%s': %s", name
,
357 mach_error_string(kret
));
359 state
= kFirstColumn
;
363 if (state
!= kFirstColumn
) {
364 errx(1, "Last line ended abruptly");
368 // ParseXMLFile(fileName)
370 // Open and parse the specified file in XML format,
371 // and set variables appropriately.
373 static void ParseXMLFile(char *fileName
)
375 CFPropertyListRef plist
;
379 CFReadStreamRef stream
;
380 CFPropertyListFormat format
= kCFPropertyListBinaryFormat_v1_0
;
382 fd
= open(fileName
, O_RDONLY
| O_NOFOLLOW
, S_IFREG
);
384 errx(1, "Could not open %s: %s", fileName
, strerror(errno
));
387 if (fstat(fd
, &sb
) == -1) {
388 errx(1, "Could not fstat %s: %s", fileName
, strerror(errno
));
391 if (sb
.st_size
> UINT32_MAX
) {
392 errx(1, "too big for our purposes");
395 buffer
= malloc((size_t)sb
.st_size
);
396 if (buffer
== NULL
) {
397 errx(1, "Could not allocate buffer");
400 if (read(fd
, buffer
, (size_t)sb
.st_size
) != sb
.st_size
) {
401 errx(1, "Could not read %s: %s", fileName
, strerror(errno
));
406 stream
= CFReadStreamCreateWithBytesNoCopy(kCFAllocatorDefault
,
407 (const UInt8
*)buffer
,
410 if (stream
== NULL
) {
411 errx(1, "Could not create stream from serialized data");
414 if (!CFReadStreamOpen(stream
)) {
415 errx(1, "Could not open the stream");
418 plist
= CFPropertyListCreateWithStream(kCFAllocatorDefault
,
421 kCFPropertyListImmutable
,
426 errx(1, "Error parsing XML file");
429 CFReadStreamClose(stream
);
435 CFDictionaryApplyFunction(plist
, &SetOFVariableFromFile
, 0);
440 // SetOrGetOFVariable(str)
442 // Parse the input string, then set or get the specified
443 // firmware variable.
445 static void SetOrGetOFVariable(char *str
)
452 kern_return_t result
;
454 // OF variable name is first.
457 // Find the equal sign for set
468 // On sets, the OF variable's value follows the equal sign.
471 if (gBridgeToIntel
) {
472 result
= SetMacOFVariable(name
, value
);
477 result
= SetOFVariable(name
, value
);
478 NVRamSyncNow(name
); /* Try syncing the new data to device, best effort! */
480 if (result
!= KERN_SUCCESS
) {
481 errx(1, "Error setting variable - '%s': %s", name
,
482 mach_error_string(result
));
486 if (gBridgeToIntel
) {
487 result
= GetMacOFVariable(name
, &value
);
488 if (result
!= KERN_SUCCESS
) {
489 errx(1, "Error getting variable - '%s': %s", name
,
490 mach_error_string(result
));
492 nameRef
= CFStringCreateWithCString(kCFAllocatorDefault
, name
, kCFStringEncodingUTF8
);
493 valueRef
= CFStringCreateWithCString(kCFAllocatorDefault
, value
, kCFStringEncodingUTF8
);
499 result
= GetOFVariable(name
, &nameRef
, &valueRef
);
500 if (result
!= KERN_SUCCESS
) {
501 errx(1, "Error getting variable - '%s': %s", name
,
502 mach_error_string(result
));
506 PrintOFVariable(nameRef
, valueRef
, 0);
513 static kern_return_t
LinkMacNVRAMSymbols()
515 gDL_handle
= dlopen("libMacEFIHostInterface.dylib", RTLD_LAZY
);
516 if (gDL_handle
== NULL
) {
517 errx(errno
, "Failed to dlopen libMacEFIHostInterface.dylib");
518 return KERN_FAILURE
; /* NOTREACHED */
521 hostInterfaceInitialize_fptr
= dlsym(gDL_handle
, "hostInterfaceInitialize");
522 if (hostInterfaceInitialize_fptr
== NULL
) {
523 errx(errno
, "failed to link hostInterfaceInitialize");
525 createNvramHostInterface_fptr
= dlsym(gDL_handle
, "createNvramHostInterface");
526 if (createNvramHostInterface_fptr
== NULL
) {
527 errx(errno
, "failed to link createNvramHostInterface");
529 destroyNvramHostInterface_fptr
= dlsym(gDL_handle
, "destroyNvramHostInterface");
530 if (destroyNvramHostInterface_fptr
== NULL
) {
531 errx(errno
, "failed to link destroyNvramHostInterface");
533 getNVRAMVariable_fptr
= dlsym(gDL_handle
, "getNVRAMVariable");
534 if (getNVRAMVariable_fptr
== NULL
) {
535 errx(errno
, "failed to link getNVRAMVariable");
537 setNVRAMVariable_fptr
= dlsym(gDL_handle
, "setNVRAMVariable");
538 if (setNVRAMVariable_fptr
== NULL
) {
539 errx(errno
, "failed to link setNVRAMVariable");
541 deleteNVRAMVariable_fptr
= dlsym(gDL_handle
, "deleteNVRAMVariable");
542 if (deleteNVRAMVariable_fptr
== NULL
) {
543 errx(errno
, "failed to link deleteNVRAMVariable");
545 hostInterfaceDeinitialize_fptr
= dlsym(gDL_handle
, "hostInterfaceDeinitialize");
546 if (hostInterfaceDeinitialize_fptr
== NULL
) {
547 errx(errno
, "failed to link hostInterfaceDeinitialize");
550 /* also do the initialization */
551 hostInterfaceInitialize_fptr();
552 gNvramInterface
= createNvramHostInterface_fptr(NULL
);
558 // GetOFVariable(name, nameRef, valueRef)
560 // Get the named firmware variable.
561 // Return it and it's symbol in valueRef and nameRef.
563 static kern_return_t
GetOFVariable(char *name
, CFStringRef
*nameRef
,
566 *nameRef
= CFStringCreateWithCString(kCFAllocatorDefault
, name
,
567 kCFStringEncodingUTF8
);
569 errx(1, "Error creating CFString for key %s", name
);
572 *valueRef
= IORegistryEntryCreateCFProperty(gOptionsRef
, *nameRef
, 0, 0);
573 if (*valueRef
== 0) return kIOReturnNotFound
;
579 // GetMacOFVariable(name, value)
581 // Get the named firmware variable from the Intel side.
582 // Return the value in value
584 static kern_return_t
GetMacOFVariable(char *name
, char **value
)
588 return getNVRAMVariable_fptr(gNvramInterface
, name
, value
, &value_size
);
592 // SetOFVariable(name, value)
594 // Set or create an firmware variable with name and value.
596 static kern_return_t
SetOFVariable(char *name
, char *value
)
601 kern_return_t result
= KERN_SUCCESS
;
603 nameRef
= CFStringCreateWithCString(kCFAllocatorDefault
, name
,
604 kCFStringEncodingUTF8
);
606 errx(1, "Error creating CFString for key %s", name
);
609 valueRef
= IORegistryEntryCreateCFProperty(gOptionsRef
, nameRef
, 0, 0);
611 typeID
= CFGetTypeID(valueRef
);
614 valueRef
= ConvertValueToCFTypeRef(typeID
, value
);
616 errx(1, "Error creating CFTypeRef for value %s", value
);
617 } result
= IORegistryEntrySetCFProperty(gOptionsRef
, nameRef
, valueRef
);
620 // In the default case, try data, string, number, then boolean.
622 valueRef
= ConvertValueToCFTypeRef(CFDataGetTypeID(), value
);
624 result
= IORegistryEntrySetCFProperty(gOptionsRef
, nameRef
, valueRef
);
625 if (result
== KERN_SUCCESS
) break;
628 valueRef
= ConvertValueToCFTypeRef(CFStringGetTypeID(), value
);
630 result
= IORegistryEntrySetCFProperty(gOptionsRef
, nameRef
, valueRef
);
631 if (result
== KERN_SUCCESS
) break;
634 valueRef
= ConvertValueToCFTypeRef(CFNumberGetTypeID(), value
);
636 result
= IORegistryEntrySetCFProperty(gOptionsRef
, nameRef
, valueRef
);
637 if (result
== KERN_SUCCESS
) break;
640 valueRef
= ConvertValueToCFTypeRef(CFBooleanGetTypeID(), value
);
642 result
= IORegistryEntrySetCFProperty(gOptionsRef
, nameRef
, valueRef
);
643 if (result
== KERN_SUCCESS
) break;
656 static kern_return_t
SetMacOFVariable(char *name
, char *value
)
658 return setNVRAMVariable_fptr(gNvramInterface
, name
, value
);
662 // DeleteOFVariable(name)
664 // Delete the named firmware variable.
667 static void DeleteOFVariable(char *name
)
669 SetOFVariable(kIONVRAMDeletePropertyKey
, name
);
673 static kern_return_t
DeleteMacOFVariable(char *name
)
675 return deleteNVRAMVariable_fptr(gNvramInterface
, name
);
679 static void NVRamSyncNow(char *name
)
681 if (!gUseForceSync
) {
682 SetOFVariable(kIONVRAMSyncNowPropertyKey
, name
);
684 SetOFVariable(kIONVRAMForceSyncNowPropertyKey
, name
);
688 // PrintOFVariables()
690 // Print all of the firmware variables.
692 static void PrintOFVariables(void)
694 kern_return_t result
;
695 CFMutableDictionaryRef dict
;
697 result
= IORegistryEntryCreateCFProperties(gOptionsRef
, &dict
, 0, 0);
698 if (result
!= KERN_SUCCESS
) {
699 errx(1, "Error getting the firmware variables: %s", mach_error_string(result
));
705 data
= CFPropertyListCreateData( kCFAllocatorDefault
, dict
, kCFPropertyListXMLFormat_v1_0
, 0, NULL
);
707 errx(1, "Error converting variables to xml");
710 fwrite(CFDataGetBytePtr(data
), sizeof(UInt8
), CFDataGetLength(data
), stdout
);
716 CFDictionaryApplyFunction(dict
, &PrintOFVariable
, 0);
723 // PrintOFVariable(key, value, context)
725 // Print the given firmware variable.
727 static void PrintOFVariable(const void *key
, const void *value
, void *context
)
731 char *nameBuffer
= 0;
732 const char *nameString
;
733 char numberBuffer
[10];
734 const uint8_t *dataPtr
;
736 char *dataBuffer
= 0;
738 char *valueBuffer
= 0;
739 const char *valueString
= 0;
746 CFDictionaryRef dict
= CFDictionaryCreate(kCFAllocatorDefault
, &key
, &value
, 1,
747 &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
749 errx(1, "Error creating dictionary for variable value");
752 data
= CFPropertyListCreateData( kCFAllocatorDefault
, dict
, kCFPropertyListXMLFormat_v1_0
, 0, NULL
);
754 errx(1, "Error creating xml plist for variable");
757 fwrite(CFDataGetBytePtr(data
), sizeof(UInt8
), CFDataGetLength(data
), stdout
);
764 // Get the OF variable's name.
765 nameLen
= CFStringGetLength(key
) + 1;
766 nameBuffer
= malloc(nameLen
);
767 if( nameBuffer
&& CFStringGetCString(key
, nameBuffer
, nameLen
, kCFStringEncodingUTF8
) )
768 nameString
= nameBuffer
;
770 warnx("Unable to convert property name to C string");
771 nameString
= "<UNPRINTABLE>";
774 // Get the OF variable's type.
775 typeID
= CFGetTypeID(value
);
777 if (typeID
== CFBooleanGetTypeID()) {
778 if (CFBooleanGetValue(value
)) valueString
= "true";
779 else valueString
= "false";
780 } else if (typeID
== CFNumberGetTypeID()) {
781 CFNumberGetValue(value
, kCFNumberSInt32Type
, &number
);
782 if (number
== 0xFFFFFFFF) sprintf(numberBuffer
, "-1");
783 else if (number
< 1000) sprintf(numberBuffer
, "%d", number
);
784 else sprintf(numberBuffer
, "0x%x", number
);
785 valueString
= numberBuffer
;
786 } else if (typeID
== CFStringGetTypeID()) {
787 valueLen
= CFStringGetLength(value
) + 1;
788 valueBuffer
= malloc(valueLen
+ 1);
789 if ( valueBuffer
&& CFStringGetCString(value
, valueBuffer
, valueLen
, kCFStringEncodingUTF8
) )
790 valueString
= valueBuffer
;
792 warnx("Unable to convert value to C string");
793 valueString
= "<UNPRINTABLE>";
795 } else if (typeID
== CFDataGetTypeID()) {
796 length
= CFDataGetLength(value
);
797 if (length
== 0) valueString
= "";
799 dataBuffer
= malloc(length
* 3 + 1);
800 if (dataBuffer
!= 0) {
801 dataPtr
= CFDataGetBytePtr(value
);
802 for (cnt
= cnt2
= 0; cnt
< length
; cnt
++) {
803 dataChar
= dataPtr
[cnt
];
804 if (isprint(dataChar
) && dataChar
!= '%') {
805 dataBuffer
[cnt2
++] = dataChar
;
807 sprintf(dataBuffer
+ cnt2
, "%%%02x", dataChar
);
811 dataBuffer
[cnt2
] = '\0';
812 valueString
= dataBuffer
;
816 valueString
="<INVALID>";
819 if ((nameString
!= 0) && (valueString
!= 0))
820 printf("%s\t%s\n", nameString
, valueString
);
822 if (dataBuffer
!= 0) free(dataBuffer
);
823 if (nameBuffer
!= 0) free(nameBuffer
);
824 if (valueBuffer
!= 0) free(valueBuffer
);
827 // ClearOFVariables()
829 // Deletes all OF variables
831 static void ClearOFVariables(void)
833 kern_return_t result
;
834 CFMutableDictionaryRef dict
;
836 result
= IORegistryEntryCreateCFProperties(gOptionsRef
, &dict
, 0, 0);
837 if (result
!= KERN_SUCCESS
) {
838 errx(1, "Error getting the firmware variables: %s", mach_error_string(result
));
840 CFDictionaryApplyFunction(dict
, &ClearOFVariable
, 0);
845 static void ClearOFVariable(const void *key
, const void *value
, void *context
)
847 kern_return_t result
;
848 result
= IORegistryEntrySetCFProperty(gOptionsRef
,
849 CFSTR(kIONVRAMDeletePropertyKey
), key
);
850 if (result
!= KERN_SUCCESS
) {
851 errx(1, "Error clearing firmware variables: %s", mach_error_string(result
));
855 // ConvertValueToCFTypeRef(typeID, value)
857 // Convert the value into a CFType given the typeID.
859 static CFTypeRef
ConvertValueToCFTypeRef(CFTypeID typeID
, char *value
)
861 CFTypeRef valueRef
= 0;
862 long cnt
, cnt2
, length
;
863 unsigned long number
, tmp
;
865 if (typeID
== CFBooleanGetTypeID()) {
866 if (!strcmp("true", value
)) valueRef
= kCFBooleanTrue
;
867 else if (!strcmp("false", value
)) valueRef
= kCFBooleanFalse
;
868 } else if (typeID
== CFNumberGetTypeID()) {
869 number
= strtol(value
, 0, 0);
870 valueRef
= CFNumberCreate(kCFAllocatorDefault
, kCFNumberSInt32Type
,
872 } else if (typeID
== CFStringGetTypeID()) {
873 valueRef
= CFStringCreateWithCString(kCFAllocatorDefault
, value
,
874 kCFStringEncodingUTF8
);
875 } else if (typeID
== CFDataGetTypeID()) {
876 length
= strlen(value
);
877 for (cnt
= cnt2
= 0; cnt
< length
; cnt
++, cnt2
++) {
878 if (value
[cnt
] == '%') {
879 if (!ishexnumber(value
[cnt
+ 1]) ||
880 !ishexnumber(value
[cnt
+ 2])) return 0;
881 number
= toupper(value
[++cnt
]) - '0';
882 if (number
> 9) number
-= 7;
883 tmp
= toupper(value
[++cnt
]) - '0';
884 if (tmp
> 9) tmp
-= 7;
885 number
= (number
<< 4) + tmp
;
886 value
[cnt2
] = number
;
887 } else value
[cnt2
] = value
[cnt
];
889 valueRef
= CFDataCreateWithBytesNoCopy(kCFAllocatorDefault
, (const UInt8
*)value
,
890 cnt2
, kCFAllocatorDefault
);
896 static void SetOFVariableFromFile(const void *key
, const void *value
, void *context
)
898 kern_return_t result
;
900 result
= IORegistryEntrySetCFProperty(gOptionsRef
, key
, value
);
901 if ( result
!= KERN_SUCCESS
) {
906 // Get the variable's name.
907 nameLen
= CFStringGetLength(key
) + 1;
908 nameBuffer
= malloc(nameLen
);
909 if( nameBuffer
&& CFStringGetCString(key
, nameBuffer
, nameLen
, kCFStringEncodingUTF8
) )
910 nameString
= nameBuffer
;
912 warnx("Unable to convert property name to C string");
913 nameString
= "<UNPRINTABLE>";
915 errx(1, "Error setting variable - '%s': %s", nameString
,
916 mach_error_string(result
));