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 *  BLCreateEFIXMLRepresentationForDevice.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#include <TargetConditionals.h>
32
33#import <IOKit/IOKitLib.h>
34#import <IOKit/IOCFSerialize.h>
35#import <IOKit/IOBSD.h>
36#import <IOKit/IOKitKeys.h>
37#import <IOKit/storage/IOMedia.h>
38
39#import <CoreFoundation/CoreFoundation.h>
40
41#include <string.h>
42#include <sys/param.h>
43#include <sys/stat.h>
44
45#include "bless.h"
46#include "bless_private.h"
47
48extern int addMatchingInfoForBSDName(BLContextPtr context,
49                              mach_port_t masterPort,
50                              CFMutableDictionaryRef dict,
51                              const char *bsdName,
52                              bool shortForm);
53
54int BLCreateEFIXMLRepresentationForDevice(BLContextPtr context,
55                                        const char *bsdName,
56                                        const char *optionalData,
57                                        CFStringRef *xmlString,
58                                        bool shortForm)
59{
60    int ret;
61    mach_port_t masterPort;
62    kern_return_t kret;
63
64    CFDataRef xmlData;
65    CFMutableDictionaryRef dict;
66    CFMutableArrayRef array;
67
68    const UInt8 *xmlBuffer;
69    UInt8 *outBuffer;
70    CFIndex count;
71
72    kret = IOMasterPort(MACH_PORT_NULL, &masterPort);
73    if(kret) return 1;
74
75    array = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
76
77    dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks,
78                                     &kCFTypeDictionaryValueCallBacks);
79
80    ret = addMatchingInfoForBSDName(context, masterPort, dict, bsdName, shortForm);
81    if(ret) {
82        CFRelease(dict);
83        CFRelease(array);
84        return 2;
85    }
86    CFArrayAppendValue(array, dict);
87    CFRelease(dict);
88
89    if(optionalData) {
90        CFStringRef optString = CFStringCreateWithCString(kCFAllocatorDefault, optionalData, kCFStringEncodingUTF8);
91
92        dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
93                                         &kCFTypeDictionaryKeyCallBacks,
94                                         &kCFTypeDictionaryValueCallBacks);
95        CFDictionaryAddValue(dict, CFSTR("IOEFIBootOption"),
96                             optString);
97        CFArrayAppendValue(array, dict);
98        CFRelease(dict);
99
100        CFRelease(optString);
101    }
102
103    xmlData = IOCFSerialize(array, 0);
104    CFRelease(array);
105
106    if(xmlData == NULL) {
107        contextprintf(context, kBLLogLevelError, "Can't create XML representation\n");
108        return 2;
109    }
110
111    count = CFDataGetLength(xmlData);
112    xmlBuffer = CFDataGetBytePtr(xmlData);
113    outBuffer = calloc(count+1, sizeof(char)); // terminate
114
115    memcpy(outBuffer, xmlBuffer, count);
116    CFRelease(xmlData);
117
118    *xmlString = CFStringCreateWithCString(kCFAllocatorDefault, (const char *)outBuffer, kCFStringEncodingUTF8);
119
120    free(outBuffer);
121
122    return 0;
123}
124
125