BLCopyEFINVRAMVariableAsString.c [plain text]
#include <IOKit/IOKitLib.h>
#include <IOKit/IOKitKeys.h>
#include "bless.h"
#include "bless_private.h"
int BLCopyEFINVRAMVariableAsString(BLContextPtr context,
CFStringRef name,
CFStringRef *value)
{
io_registry_entry_t optionsNode = 0;
char cStr[1024];
CFTypeRef valRef;
CFStringRef stringRef;
*value = NULL;
optionsNode = IORegistryEntryFromPath(kIOMasterPortDefault, kIODeviceTreePlane ":/options");
if(IO_OBJECT_NULL == optionsNode) {
contextprintf(context, kBLLogLevelError, "Could not find " kIODeviceTreePlane ":/options\n");
return 1;
}
valRef = IORegistryEntryCreateCFProperty(optionsNode, name, kCFAllocatorDefault, 0);
IOObjectRelease(optionsNode);
if(valRef == NULL)
return 0;
if(CFGetTypeID(valRef) == CFStringGetTypeID()) {
if(!CFStringGetCString(valRef, cStr, sizeof(cStr), kCFStringEncodingUTF8)) {
contextprintf(context, kBLLogLevelVerbose,
"Could not interpret NVRAM variable as UTF-8 string. Ignoring...\n");
strcpy(cStr, "");
}
} else if(CFGetTypeID(valRef) == CFDataGetTypeID()) {
const UInt8 *ptr = CFDataGetBytePtr(valRef);
CFIndex len = CFDataGetLength(valRef);
if(len > sizeof(cStr)-1)
len = sizeof(cStr)-1;
memcpy(cStr, (char *)ptr, len);
cStr[len] = '\0';
} else {
contextprintf(context, kBLLogLevelError, "Could not interpret NVRAM variable. Ignoring...\n");
strcpy(cStr, "");
}
CFRelease(valRef);
stringRef = CFStringCreateWithCString(kCFAllocatorDefault, cStr, kCFStringEncodingUTF8);
if(stringRef == NULL) {
return 2;
}
*value = stringRef;
return 0;
}