1/*
2 * Copyright (c) 2005-2007 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23/*
24 *  BLCopyEFINVRAMVariableAsString.c
25 *  bless
26 *
27 *  Created by Shantonu Sen on 12/2/05.
28 *  Copyright 2005-2007 Apple Inc. All Rights Reserved.
29 *
30 */
31
32#include <IOKit/IOKitLib.h>
33#include <IOKit/IOKitKeys.h>
34
35#include "bless.h"
36#include "bless_private.h"
37
38int BLCopyEFINVRAMVariableAsString(BLContextPtr context,
39                                   CFStringRef  name,
40                                   CFStringRef *value)
41{
42
43    io_registry_entry_t optionsNode = 0;
44    char            cStr[1024];
45    CFTypeRef       valRef;
46    CFStringRef     stringRef;
47
48    *value = NULL;
49
50    optionsNode = IORegistryEntryFromPath(kIOMasterPortDefault, kIODeviceTreePlane ":/options");
51
52    if(IO_OBJECT_NULL == optionsNode) {
53        contextprintf(context, kBLLogLevelError,  "Could not find " kIODeviceTreePlane ":/options\n");
54        return 1;
55    }
56
57    valRef = IORegistryEntryCreateCFProperty(optionsNode, name, kCFAllocatorDefault, 0);
58    IOObjectRelease(optionsNode);
59
60    if(valRef == NULL)
61        return 0;
62
63    if(CFGetTypeID(valRef) == CFStringGetTypeID()) {
64        if(!CFStringGetCString(valRef, cStr, sizeof(cStr), kCFStringEncodingUTF8)) {
65            contextprintf(context, kBLLogLevelVerbose,
66                               "Could not interpret NVRAM variable as UTF-8 string. Ignoring...\n");
67            cStr[0] = '\0';
68        }
69    } else if(CFGetTypeID(valRef) == CFDataGetTypeID()) {
70        const UInt8 *ptr = CFDataGetBytePtr(valRef);
71        CFIndex len = CFDataGetLength(valRef);
72
73        if(len > sizeof(cStr)-1)
74            len = sizeof(cStr)-1;
75
76        memcpy(cStr, (char *)ptr, len);
77        cStr[len] = '\0';
78
79    } else {
80        contextprintf(context, kBLLogLevelError,  "Could not interpret NVRAM variable. Ignoring...\n");
81        cStr[0] = '\0';
82    }
83
84    CFRelease(valRef);
85
86    stringRef = CFStringCreateWithCString(kCFAllocatorDefault, cStr, kCFStringEncodingUTF8);
87    if(stringRef == NULL) {
88        return 2;
89    }
90
91    *value = stringRef;
92
93    return 0;
94}
95