1/*
2 * Copyright (c) 2001-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 *  BLSetOFLabelForDevice.c
25 *  bless
26 *
27 *  Created by Shantonu Sen on Wed Apr 16 2003.
28 *  Copyright (c) 2003-2007 Apple Inc. All Rights Reserved.
29 *
30 *  $Id: BLSetOFLabelForDevice.c,v 1.18 2006/02/20 22:49:55 ssen Exp $
31 *
32 */
33
34#include <sys/types.h>
35
36#include <CoreFoundation/CoreFoundation.h>
37#include <MediaKit/MKMedia.h>
38#include <MediaKit/MKHFSUtilities.h>
39
40#include "bless.h"
41#include "bless_private.h"
42
43
44static int GetBlessedFolder(BLContextPtr context, const char *device, uint32_t *blessedFolderID);
45
46
47int BLSetDiskLabelForDevice(BLContextPtr context,
48                            const char *device,
49                            const CFDataRef label,
50                            int scale)
51{
52
53	int				status;
54	uint32_t        blessedID;
55	BLUpdateBooterFileSpec	spec;
56
57	status = GetBlessedFolder(context, device, &blessedID);
58	if (status) {
59		return 1;
60	}
61
62	bzero(&spec, sizeof(spec));
63
64	spec.version = 0;
65	spec.reqType = 0;
66	spec.reqCreator = 0;
67	spec.reqParentDir = blessedID;
68	spec.reqFilename = (scale == 1) ? ".disk_label" : ".disk_label_2x";
69	spec.payloadData = label;
70	spec.postType = 0; // no type
71	spec.postCreator = 0; // no type
72	spec.foundFile = 0;
73	spec.updatedFile = 0;
74
75	status = BLUpdateBooter(context, device, &spec, 1);
76	if(status) {
77		contextprintf(context, kBLLogLevelError,  "Error enumerating HFS+ volume\n");
78		return 1;
79	}
80
81	if(!spec.foundFile) {
82		contextprintf(context, kBLLogLevelError,  "No pre-existing disk label found in HFS+ volume\n");
83		return 1;
84	}
85	if(!spec.updatedFile) {
86		contextprintf(context, kBLLogLevelError,  "disk label was not updated\n");
87		return 2;
88	}
89
90	return 0;
91}
92
93
94int BLSetOFLabelForDevice(BLContextPtr context,
95			  const char * device,
96			  const CFDataRef label)
97{
98
99    int				status;
100	BLUpdateBooterFileSpec	array[2];
101
102	bzero(array, sizeof(array));
103
104	array[0].version = 0;
105	array[0].reqType = kBL_OSTYPE_PPC_TYPE_OFLABEL;
106	array[0].reqCreator = kBL_OSTYPE_PPC_CREATOR_CHRP;
107	array[0].reqFilename = NULL;
108	array[0].payloadData = label;
109	array[0].postType = 0; // no type
110	array[0].postCreator = 0; // no type
111	array[0].foundFile = 0;
112	array[0].updatedFile = 0;
113
114	array[1].version = 0;
115	array[1].reqType = kBL_OSTYPE_PPC_TYPE_OFLABEL_PLACEHOLDER;
116	array[1].reqCreator = kBL_OSTYPE_PPC_CREATOR_CHRP;
117	array[1].reqFilename = NULL;
118	array[1].payloadData = label;
119	array[1].postType = kBL_OSTYPE_PPC_TYPE_OFLABEL;
120	array[1].postCreator = 0; // no type
121	array[1].foundFile = 0;
122	array[1].updatedFile = 0;
123
124	status = BLUpdateBooter(context, device, array, 2);
125	if(status) {
126		contextprintf(context, kBLLogLevelError,  "Error enumerating HFS+ volume\n");
127		return 1;
128	}
129
130    if(!(array[0].foundFile || array[1].foundFile)) {
131		contextprintf(context, kBLLogLevelError,  "No pre-existing OF label found in HFS+ volume\n");
132		return 1;
133    }
134    if(!(array[0].updatedFile || array[1].updatedFile)) {
135		contextprintf(context, kBLLogLevelError,  "OF label was not updated\n");
136		return 2;
137    }
138
139    return 0;
140}
141
142
143
144static int GetBlessedFolder(BLContextPtr context, const char *device, uint32_t *blessedFolderID)
145{
146	int32_t             i32;
147	MKMediaRef          media;
148	HFSTypes            hfsType;
149	HFSPlusVolumeHeader vh;
150
151	media = MKMediaCreateWithPath(kCFAllocatorDefault, device, NULL, &i32);
152	if (!media) {
153		contextprintf(context, kBLLogLevelError,  "Can't open device %s: %d\n", device, i32);
154		return 1;
155	}
156
157	i32 = MKReadHFSMDBVHB(media, &hfsType, &vh, NULL, NULL);
158	if (i32) {
159		CFRelease(media);
160		contextprintf(context, kBLLogLevelError,  "Can't read HFS+ VH for device %s: %d\n", device, i32);
161		return 1;
162	}
163
164	CFRelease(media);
165
166	if (hfsType < kHFSPlusFormat) {
167		CFRelease(media);
168		contextprintf(context, kBLLogLevelError, "Device %s does not have an HFS+ volume\n", device);
169		return 1;
170	}
171
172	*blessedFolderID = vh.finderInfo[0];
173	return 0;
174}
175