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 // 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");
225 printf("\t-m set nvram variables on macOS from bridgeOS\n");
227 printf("\tname=value set named variable\n");
228 printf("\tname print variable\n");
229 printf("Note that arguments and options are executed in order.\n");
235 // States for ParseFile.
246 kMaxStringSize
= 0x800,
251 // ParseFile(fileName)
253 // Open and parse the specified file.
255 static void ParseFile(char *fileName
)
257 long state
, ni
= 0, vi
= 0;
259 char name
[kMaxNameSize
];
260 char value
[kMaxStringSize
];
265 ParseXMLFile(fileName
);
269 patches
= fopen(fileName
, "r");
271 err(1, "Couldn't open patch file - '%s'", fileName
);
274 state
= kFirstColumn
;
275 while ((tc
= getc(patches
)) != EOF
) {
276 if(ni
==(kMaxNameSize
-1))
277 errx(1, "Name exceeded max length of %d", kMaxNameSize
);
278 if(vi
==(kMaxStringSize
-1))
279 errx(1, "Value exceeded max length of %d", kMaxStringSize
);
285 state
= kScanComment
;
286 } else if (tc
== '\n') {
287 // state stays kFirstColumn.
288 } else if (isspace(tc
)) {
291 state
= kCollectName
;
298 state
= kFirstColumn
;
300 // state stays kScanComment.
306 state
= kFirstColumn
;
307 } else if (isspace(tc
)) {
308 // state stays kFindName.
310 state
= kCollectName
;
318 warnx("Name must be followed by white space - '%s'", name
);
319 state
= kFirstColumn
;
320 } else if (isspace(tc
)) {
324 // state staus kCollectName.
329 case kContinueValue
:
332 } else if (isspace(tc
)) {
333 // state stays kFindValue or kContinueValue.
335 state
= kCollectValue
;
342 if (value
[vi
-1] == '\\') {
344 state
= kContinueValue
;
349 // state stays kCollectValue.
355 if (state
== kSetenv
) {
358 if ((kret
= SetOFVariable(name
, value
)) != KERN_SUCCESS
) {
359 errx(1, "Error setting variable - '%s': %s", name
,
360 mach_error_string(kret
));
362 state
= kFirstColumn
;
366 if (state
!= kFirstColumn
) {
367 errx(1, "Last line ended abruptly");
371 // ParseXMLFile(fileName)
373 // Open and parse the specified file in XML format,
374 // and set variables appropriately.
376 static void ParseXMLFile(char *fileName
)
378 CFPropertyListRef plist
;
382 CFReadStreamRef stream
;
383 CFPropertyListFormat format
= kCFPropertyListBinaryFormat_v1_0
;
385 fd
= open(fileName
, O_RDONLY
| O_NOFOLLOW
, S_IFREG
);
387 errx(1, "Could not open %s: %s", fileName
, strerror(errno
));
390 if (fstat(fd
, &sb
) == -1) {
391 errx(1, "Could not fstat %s: %s", fileName
, strerror(errno
));
394 if (sb
.st_size
> UINT32_MAX
) {
395 errx(1, "too big for our purposes");
398 buffer
= malloc((size_t)sb
.st_size
);
399 if (buffer
== NULL
) {
400 errx(1, "Could not allocate buffer");
403 if (read(fd
, buffer
, (size_t)sb
.st_size
) != sb
.st_size
) {
404 errx(1, "Could not read %s: %s", fileName
, strerror(errno
));
409 stream
= CFReadStreamCreateWithBytesNoCopy(kCFAllocatorDefault
,
410 (const UInt8
*)buffer
,
413 if (stream
== NULL
) {
414 errx(1, "Could not create stream from serialized data");
417 if (!CFReadStreamOpen(stream
)) {
418 errx(1, "Could not open the stream");
421 plist
= CFPropertyListCreateWithStream(kCFAllocatorDefault
,
424 kCFPropertyListImmutable
,
429 errx(1, "Error parsing XML file");
432 CFReadStreamClose(stream
);
438 CFDictionaryApplyFunction(plist
, &SetOFVariableFromFile
, 0);
443 // SetOrGetOFVariable(str)
445 // Parse the input string, then set, append or get
446 // the specified firmware variable.
448 static void SetOrGetOFVariable(char *str
)
454 CFStringRef nameRef
= NULL
;
455 CFTypeRef valueRef
= NULL
;
456 CFMutableStringRef appended
= NULL
;
457 kern_return_t result
;
459 // OF variable name is first.
462 // Find the equal sign for set or += for append
464 if (*str
== '+' && *(str
+1) == '=') {
479 // Read the current value if appending or if no =/+=
480 if (append
== 1 || (set
== 0 && append
== 0)) {
482 if (gBridgeToIntel
) {
483 result
= GetMacOFVariable(name
, &value
);
484 if (result
!= KERN_SUCCESS
) {
485 errx(1, "Error getting variable - '%s': %s", name
,
486 mach_error_string(result
));
488 nameRef
= CFStringCreateWithCString(kCFAllocatorDefault
, name
, kCFStringEncodingUTF8
);
489 valueRef
= CFStringCreateWithCString(kCFAllocatorDefault
, value
, kCFStringEncodingUTF8
);
495 result
= GetOFVariable(name
, &nameRef
, &valueRef
);
496 if (result
!= KERN_SUCCESS
) {
497 errx(1, "Error getting variable - '%s': %s", name
,
498 mach_error_string(result
));
504 // On sets, the OF variable's value follows the equal sign.
509 // On append, the value to append follows the += substring
510 appended
= CFStringCreateMutableCopy(NULL
, 0, valueRef
);
511 CFStringAppendCString(appended
, str
, kCFStringEncodingUTF8
);
512 value
= (char*)CFStringGetCStringPtr(appended
, kCFStringEncodingUTF8
);
515 if (set
== 1 || append
== 1) {
517 if (gBridgeToIntel
) {
518 result
= SetMacOFVariable(name
, value
);
523 result
= SetOFVariable(name
, value
);
524 NVRamSyncNow(name
); /* Try syncing the new data to device, best effort! */
526 if (result
!= KERN_SUCCESS
) {
527 errx(1, "Error setting variable - '%s': %s", name
,
528 mach_error_string(result
));
531 PrintOFVariable(nameRef
, valueRef
, 0);
533 if ( nameRef
) CFRelease(nameRef
);
534 if ( valueRef
) CFRelease(valueRef
);
535 if ( appended
) CFRelease(appended
);
539 static kern_return_t
LinkMacNVRAMSymbols()
541 gDL_handle
= dlopen("libMacEFIHostInterface.dylib", RTLD_LAZY
);
542 if (gDL_handle
== NULL
) {
543 errx(errno
, "Failed to dlopen libMacEFIHostInterface.dylib");
544 return KERN_FAILURE
; /* NOTREACHED */
547 hostInterfaceInitialize_fptr
= dlsym(gDL_handle
, "hostInterfaceInitialize");
548 if (hostInterfaceInitialize_fptr
== NULL
) {
549 errx(errno
, "failed to link hostInterfaceInitialize");
551 createNvramHostInterface_fptr
= dlsym(gDL_handle
, "createNvramHostInterface");
552 if (createNvramHostInterface_fptr
== NULL
) {
553 errx(errno
, "failed to link createNvramHostInterface");
555 destroyNvramHostInterface_fptr
= dlsym(gDL_handle
, "destroyNvramHostInterface");
556 if (destroyNvramHostInterface_fptr
== NULL
) {
557 errx(errno
, "failed to link destroyNvramHostInterface");
559 getNVRAMVariable_fptr
= dlsym(gDL_handle
, "getNVRAMVariable");
560 if (getNVRAMVariable_fptr
== NULL
) {
561 errx(errno
, "failed to link getNVRAMVariable");
563 setNVRAMVariable_fptr
= dlsym(gDL_handle
, "setNVRAMVariable");
564 if (setNVRAMVariable_fptr
== NULL
) {
565 errx(errno
, "failed to link setNVRAMVariable");
567 deleteNVRAMVariable_fptr
= dlsym(gDL_handle
, "deleteNVRAMVariable");
568 if (deleteNVRAMVariable_fptr
== NULL
) {
569 errx(errno
, "failed to link deleteNVRAMVariable");
571 hostInterfaceDeinitialize_fptr
= dlsym(gDL_handle
, "hostInterfaceDeinitialize");
572 if (hostInterfaceDeinitialize_fptr
== NULL
) {
573 errx(errno
, "failed to link hostInterfaceDeinitialize");
576 /* also do the initialization */
577 hostInterfaceInitialize_fptr();
578 gNvramInterface
= createNvramHostInterface_fptr(NULL
);
584 // GetOFVariable(name, nameRef, valueRef)
586 // Get the named firmware variable.
587 // Return it and it's symbol in valueRef and nameRef.
589 static kern_return_t
GetOFVariable(char *name
, CFStringRef
*nameRef
,
592 *nameRef
= CFStringCreateWithCString(kCFAllocatorDefault
, name
,
593 kCFStringEncodingUTF8
);
595 errx(1, "Error creating CFString for key %s", name
);
598 *valueRef
= IORegistryEntryCreateCFProperty(gOptionsRef
, *nameRef
, 0, 0);
599 if (*valueRef
== 0) return kIOReturnNotFound
;
605 // GetMacOFVariable(name, value)
607 // Get the named firmware variable from the Intel side.
608 // Return the value in value
610 static kern_return_t
GetMacOFVariable(char *name
, char **value
)
614 return getNVRAMVariable_fptr(gNvramInterface
, name
, value
, &value_size
);
618 // SetOFVariable(name, value)
620 // Set or create an firmware variable with name and value.
622 static kern_return_t
SetOFVariable(char *name
, char *value
)
627 kern_return_t result
= KERN_SUCCESS
;
629 nameRef
= CFStringCreateWithCString(kCFAllocatorDefault
, name
,
630 kCFStringEncodingUTF8
);
632 errx(1, "Error creating CFString for key %s", name
);
635 valueRef
= IORegistryEntryCreateCFProperty(gOptionsRef
, nameRef
, 0, 0);
637 typeID
= CFGetTypeID(valueRef
);
640 valueRef
= ConvertValueToCFTypeRef(typeID
, value
);
642 errx(1, "Error creating CFTypeRef for value %s", value
);
643 } result
= IORegistryEntrySetCFProperty(gOptionsRef
, nameRef
, valueRef
);
646 // In the default case, try data, string, number, then boolean.
648 valueRef
= ConvertValueToCFTypeRef(CFDataGetTypeID(), value
);
650 result
= IORegistryEntrySetCFProperty(gOptionsRef
, nameRef
, valueRef
);
651 if (result
== KERN_SUCCESS
) break;
654 valueRef
= ConvertValueToCFTypeRef(CFStringGetTypeID(), value
);
656 result
= IORegistryEntrySetCFProperty(gOptionsRef
, nameRef
, valueRef
);
657 if (result
== KERN_SUCCESS
) break;
660 valueRef
= ConvertValueToCFTypeRef(CFNumberGetTypeID(), value
);
662 result
= IORegistryEntrySetCFProperty(gOptionsRef
, nameRef
, valueRef
);
663 if (result
== KERN_SUCCESS
) break;
666 valueRef
= ConvertValueToCFTypeRef(CFBooleanGetTypeID(), value
);
668 result
= IORegistryEntrySetCFProperty(gOptionsRef
, nameRef
, valueRef
);
669 if (result
== KERN_SUCCESS
) break;
682 static kern_return_t
SetMacOFVariable(char *name
, char *value
)
684 return setNVRAMVariable_fptr(gNvramInterface
, name
, value
);
688 // DeleteOFVariable(name)
690 // Delete the named firmware variable.
693 static void DeleteOFVariable(char *name
)
695 SetOFVariable(kIONVRAMDeletePropertyKey
, name
);
699 static kern_return_t
DeleteMacOFVariable(char *name
)
701 return deleteNVRAMVariable_fptr(gNvramInterface
, name
);
705 static void NVRamSyncNow(char *name
)
707 if (!gUseForceSync
) {
708 SetOFVariable(kIONVRAMSyncNowPropertyKey
, name
);
710 SetOFVariable(kIONVRAMForceSyncNowPropertyKey
, name
);
714 // PrintOFVariables()
716 // Print all of the firmware variables.
718 static void PrintOFVariables(void)
720 kern_return_t result
;
721 CFMutableDictionaryRef dict
;
723 result
= IORegistryEntryCreateCFProperties(gOptionsRef
, &dict
, 0, 0);
724 if (result
!= KERN_SUCCESS
) {
725 errx(1, "Error getting the firmware variables: %s", mach_error_string(result
));
731 data
= CFPropertyListCreateData( kCFAllocatorDefault
, dict
, kCFPropertyListXMLFormat_v1_0
, 0, NULL
);
733 errx(1, "Error converting variables to xml");
736 fwrite(CFDataGetBytePtr(data
), sizeof(UInt8
), CFDataGetLength(data
), stdout
);
742 CFDictionaryApplyFunction(dict
, &PrintOFVariable
, 0);
749 // PrintOFVariable(key, value, context)
751 // Print the given firmware variable.
753 static void PrintOFVariable(const void *key
, const void *value
, void *context
)
757 char *nameBuffer
= 0;
758 const char *nameString
;
759 char numberBuffer
[10];
760 const uint8_t *dataPtr
;
762 char *dataBuffer
= 0;
764 char *valueBuffer
= 0;
765 const char *valueString
= 0;
772 CFDictionaryRef dict
= CFDictionaryCreate(kCFAllocatorDefault
, &key
, &value
, 1,
773 &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
775 errx(1, "Error creating dictionary for variable value");
778 data
= CFPropertyListCreateData( kCFAllocatorDefault
, dict
, kCFPropertyListXMLFormat_v1_0
, 0, NULL
);
780 errx(1, "Error creating xml plist for variable");
783 fwrite(CFDataGetBytePtr(data
), sizeof(UInt8
), CFDataGetLength(data
), stdout
);
790 // Get the OF variable's name.
791 nameLen
= CFStringGetMaximumSizeForEncoding(CFStringGetLength(key
),
792 kCFStringEncodingUTF8
) + 1;
793 nameBuffer
= malloc(nameLen
);
794 if( nameBuffer
&& CFStringGetCString(key
, nameBuffer
, nameLen
, kCFStringEncodingUTF8
) )
795 nameString
= nameBuffer
;
797 warnx("Unable to convert property name to C string");
798 nameString
= "<UNPRINTABLE>";
801 // Get the OF variable's type.
802 typeID
= CFGetTypeID(value
);
804 if (typeID
== CFBooleanGetTypeID()) {
805 if (CFBooleanGetValue(value
)) valueString
= "true";
806 else valueString
= "false";
807 } else if (typeID
== CFNumberGetTypeID()) {
808 CFNumberGetValue(value
, kCFNumberSInt32Type
, &number
);
809 if (number
== 0xFFFFFFFF) sprintf(numberBuffer
, "-1");
810 else if (number
< 1000) sprintf(numberBuffer
, "%d", number
);
811 else sprintf(numberBuffer
, "0x%x", number
);
812 valueString
= numberBuffer
;
813 } else if (typeID
== CFStringGetTypeID()) {
814 valueLen
= CFStringGetMaximumSizeForEncoding(CFStringGetLength(value
),
815 kCFStringEncodingUTF8
) + 1;
816 valueBuffer
= malloc(valueLen
+ 1);
817 if ( valueBuffer
&& CFStringGetCString(value
, valueBuffer
, valueLen
, kCFStringEncodingUTF8
) )
818 valueString
= valueBuffer
;
820 warnx("Unable to convert value to C string");
821 valueString
= "<UNPRINTABLE>";
823 } else if (typeID
== CFDataGetTypeID()) {
824 length
= CFDataGetLength(value
);
825 if (length
== 0) valueString
= "";
827 dataBuffer
= malloc(length
* 3 + 1);
828 if (dataBuffer
!= 0) {
829 dataPtr
= CFDataGetBytePtr(value
);
830 for (cnt
= cnt2
= 0; cnt
< length
; cnt
++) {
831 dataChar
= dataPtr
[cnt
];
832 if (isprint(dataChar
) && dataChar
!= '%') {
833 dataBuffer
[cnt2
++] = dataChar
;
835 sprintf(dataBuffer
+ cnt2
, "%%%02x", dataChar
);
839 dataBuffer
[cnt2
] = '\0';
840 valueString
= dataBuffer
;
844 valueString
="<INVALID>";
847 if ((nameString
!= 0) && (valueString
!= 0))
848 printf("%s\t%s\n", nameString
, valueString
);
850 if (dataBuffer
!= 0) free(dataBuffer
);
851 if (nameBuffer
!= 0) free(nameBuffer
);
852 if (valueBuffer
!= 0) free(valueBuffer
);
855 // ClearOFVariables()
857 // Deletes all OF variables
859 static void ClearOFVariables(void)
861 kern_return_t result
;
862 CFMutableDictionaryRef dict
;
864 result
= IORegistryEntryCreateCFProperties(gOptionsRef
, &dict
, 0, 0);
865 if (result
!= KERN_SUCCESS
) {
866 errx(1, "Error getting the firmware variables: %s", mach_error_string(result
));
868 CFDictionaryApplyFunction(dict
, &ClearOFVariable
, 0);
873 static void ClearOFVariable(const void *key
, const void *value
, void *context
)
875 kern_return_t result
;
876 result
= IORegistryEntrySetCFProperty(gOptionsRef
,
877 CFSTR(kIONVRAMDeletePropertyKey
), key
);
878 if (result
!= KERN_SUCCESS
) {
879 assert(CFGetTypeID(key
) == CFStringGetTypeID());
880 const char *keyStr
= CFStringGetCStringPtr(key
, kCFStringEncodingUTF8
);
881 char *keyBuffer
= NULL
;
882 size_t keyBufferLen
= 0;
884 keyBufferLen
= CFStringGetMaximumSizeForEncoding(CFStringGetLength(key
), kCFStringEncodingUTF8
) + 1;
885 keyBuffer
= (char *)malloc(keyBufferLen
);
886 if (keyBuffer
!= NULL
&& CFStringGetCString(key
, keyBuffer
, keyBufferLen
, kCFStringEncodingUTF8
)) {
889 warnx("Unable to convert property name to C string");
890 keyStr
= "<UNPRINTABLE>";
894 warnx("Error clearing firmware variable %s: %s", keyStr
, mach_error_string(result
));
901 // ConvertValueToCFTypeRef(typeID, value)
903 // Convert the value into a CFType given the typeID.
905 static CFTypeRef
ConvertValueToCFTypeRef(CFTypeID typeID
, char *value
)
907 CFTypeRef valueRef
= 0;
908 long cnt
, cnt2
, length
;
909 unsigned long number
, tmp
;
911 if (typeID
== CFBooleanGetTypeID()) {
912 if (!strcmp("true", value
)) valueRef
= kCFBooleanTrue
;
913 else if (!strcmp("false", value
)) valueRef
= kCFBooleanFalse
;
914 } else if (typeID
== CFNumberGetTypeID()) {
915 number
= strtol(value
, 0, 0);
916 valueRef
= CFNumberCreate(kCFAllocatorDefault
, kCFNumberSInt32Type
,
918 } else if (typeID
== CFStringGetTypeID()) {
919 valueRef
= CFStringCreateWithCString(kCFAllocatorDefault
, value
,
920 kCFStringEncodingUTF8
);
921 } else if (typeID
== CFDataGetTypeID()) {
922 length
= strlen(value
);
923 for (cnt
= cnt2
= 0; cnt
< length
; cnt
++, cnt2
++) {
924 if (value
[cnt
] == '%') {
925 if (!ishexnumber(value
[cnt
+ 1]) ||
926 !ishexnumber(value
[cnt
+ 2])) return 0;
927 number
= toupper(value
[++cnt
]) - '0';
928 if (number
> 9) number
-= 7;
929 tmp
= toupper(value
[++cnt
]) - '0';
930 if (tmp
> 9) tmp
-= 7;
931 number
= (number
<< 4) + tmp
;
932 value
[cnt2
] = number
;
933 } else value
[cnt2
] = value
[cnt
];
935 valueRef
= CFDataCreateWithBytesNoCopy(kCFAllocatorDefault
, (const UInt8
*)value
,
936 cnt2
, kCFAllocatorDefault
);
942 static void SetOFVariableFromFile(const void *key
, const void *value
, void *context
)
944 kern_return_t result
;
946 result
= IORegistryEntrySetCFProperty(gOptionsRef
, key
, value
);
947 if ( result
!= KERN_SUCCESS
) {
952 // Get the variable's name.
953 nameLen
= CFStringGetMaximumSizeForEncoding(CFStringGetLength(key
),
954 kCFStringEncodingUTF8
) + 1;
955 nameBuffer
= malloc(nameLen
);
956 if( nameBuffer
&& CFStringGetCString(key
, nameBuffer
, nameLen
, kCFStringEncodingUTF8
) )
957 nameString
= nameBuffer
;
959 warnx("Unable to convert property name to C string");
960 nameString
= "<UNPRINTABLE>";
962 errx(1, "Error setting variable - '%s': %s", nameString
,
963 mach_error_string(result
));