-
Notifications
You must be signed in to change notification settings - Fork 0
/
FSFormatName.c
662 lines (550 loc) · 20.2 KB
/
FSFormatName.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
/*
* Copyright (c) 1999-2004 Apple Computer, Inc. All Rights Reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
#include <libkern/OSByteOrder.h>
#include <stdio.h>
#include <unistd.h>
#include <paths.h>
#include <fsproperties.h>
#include <IOKit/storage/IOMedia.h>
#include <IOKit/storage/CoreStorage/CoreStorageUserLib.h>
#include "FSFormatName.h"
static CFMutableDictionaryRef __FSLocalizedNameTable = NULL;
static OSSpinLock __FSLocalizedNameTableLock = 0;
static bool IsEncrypted(const char *bsdname);
CFStringRef FSCopyFormatNameForFSType(CFStringRef fsType, int16_t fsSubtype, bool localized, bool encrypted)
{
CFTypeRef formatName;
CFStringRef formatNameTableKey;
CFIndex indx;
if (NULL == fsType) return NULL;
// Create a key for cache localized name table (i.e. "0hfs0")
formatNameTableKey = CFStringCreateWithFormat(NULL, NULL, CFSTR("%d%@%d"), (localized ? 1 : 0), fsType, fsSubtype);
// Use OSSpinLock to protect the table accessed from multiple threads
OSSpinLockLock(&__FSLocalizedNameTableLock);
formatName = (void*)((NULL == __FSLocalizedNameTable) ? NULL : CFDictionaryGetValue(__FSLocalizedNameTable, (const void *)formatNameTableKey));
OSSpinLockUnlock(&__FSLocalizedNameTableLock);
if (NULL == formatName) { // not in the cache
CFBundleRef bundle = NULL;
CFURLRef bundleURL;
CFStringRef fsTypeName;
static CFArrayRef searchPaths = NULL;
/* Construct a bundle path URL from the fsType argument and create a CFBundle. We search (using CFCopySearchPathForDirectoriesInDomains) /Network/Library/Filesystems, /Library/Filesystems, and /System/Library/Filesystems. */
// Create CFURL for /System/Library/Filesystems and cache it
if (NULL == searchPaths) {
CFArrayRef tmpPaths = CFCopySearchPathForDirectoriesInDomains(kCFLibraryDirectory, kCFSystemDomainMask | kCFNetworkDomainMask | kCFLocalDomainMask, true);
CFMutableArrayRef tmpStrings;
CFIndex i;
if (NULL == tmpPaths)
return NULL; // No directories to search?!?!
tmpStrings = CFArrayCreateMutable(NULL, CFArrayGetCount(tmpPaths), NULL);
if (tmpStrings == NULL)
goto done;
for (i = 0; i < CFArrayGetCount(tmpPaths); i++) {
CFStringRef tStr;
CFURLRef tURL;
char path[PATH_MAX + 1];
CFTypeRef tobject = CFArrayGetValueAtIndex(tmpPaths, i);
if (CFGetTypeID(tobject) == CFURLGetTypeID()) {
if (false ==
CFURLGetFileSystemRepresentation(
tobject,
false,
(UInt8*)path,
sizeof(path))) {
goto done;
}
} else if (CFGetTypeID(tobject) == CFStringGetTypeID()) {
CFStringGetCString(tobject, path, sizeof(path), kCFStringEncodingUTF8);
} else {
goto done;
}
strlcat(path, "/Filesystems", sizeof(path));
tStr = CFStringCreateWithCString(NULL, path, kCFStringEncodingUTF8);
if (tStr == NULL)
goto done;
tURL = CFURLCreateWithFileSystemPath(NULL, tStr, kCFURLPOSIXPathStyle, true);
if (tURL) {
CFArrayAppendValue(tmpStrings, tURL);
}
CFRelease(tStr);
}
searchPaths = CFArrayCreateCopy(NULL, tmpStrings);
done:
CFRelease(tmpStrings);
CFRelease(tmpPaths);
if (searchPaths == NULL)
return NULL;
}
for (indx = 0; indx < CFArrayGetCount(searchPaths); indx++) {
CFURLRef libRef = CFArrayGetValueAtIndex(searchPaths, indx);
fsTypeName = CFStringCreateWithFormat(NULL, NULL, CFSTR("%@.fs"), fsType);
bundleURL = CFURLCreateWithFileSystemPathRelativeToBase(NULL, fsTypeName, kCFURLPOSIXPathStyle, true, libRef);
bundle = CFBundleCreate(NULL, bundleURL);
CFRelease(fsTypeName);
CFRelease(bundleURL);
if (NULL != bundle) {
break;
}
}
if (NULL != bundle) { // the bundle exists at path
CFDictionaryRef localPersonalities = NULL;
// Access the Info dictionary in the bundle
CFDictionaryRef bundleDict = CFBundleGetInfoDictionary(bundle);
// Get localized FSPersonalities only if we want localized name
if (localized == true) {
localPersonalities = CFBundleGetValueForInfoDictionaryKey(bundle, KEY_FS_PERSONALITIES);
//NSLog(CFSTR("localPersonalities = %@\n"), localPersonalities);
}
/* Get global FSPersonalities. We need to access this since FSSubType exists only
* in global FSPersonalities
*/
CFDictionaryRef globalPersonalities = CFDictionaryGetValue(bundleDict, (const void *) KEY_FS_PERSONALITIES);
//NSLog(CFSTR("globalPersonalities = %@\n"), globalPersonalities);
CFIndex numPersonalities;
if (((NULL != localPersonalities) || (localized == false)) && // localPersonalities or we don't want localizations
(NULL != globalPersonalities) &&
((numPersonalities = CFDictionaryGetCount(globalPersonalities)) > 0)) {
// read all FSPersonalities keys and values
CFDictionaryRef valuesBuffer[MAX_FS_SUBTYPES];
CFStringRef keysBuffer[MAX_FS_SUBTYPES];
CFDictionaryRef *values = ((numPersonalities > MAX_FS_SUBTYPES) ? (CFDictionaryRef *)malloc(sizeof(CFDictionaryRef) * numPersonalities) : valuesBuffer);
CFStringRef *keys = ((numPersonalities > MAX_FS_SUBTYPES) ? (CFStringRef *)malloc(sizeof(CFStringRef) * numPersonalities) : keysBuffer);
CFDictionaryGetKeysAndValues(globalPersonalities, (const void **)keys, (const void **)values);
// create CFNumberRef for the FSSubType
CFNumberRef subTypeID = CFNumberCreate(NULL, kCFNumberSInt16Type, (const void *)&fsSubtype);
CFStringRef FSNameKey = NULL;
// search for valid FSSubType - we will use its key from global FSPersonalties to
// access FSName from localized FSPersonalities
CFIndex index;
CFNumberRef readSubTypeID;
for (index = 0; index < numPersonalities; index++) {
if ((true == CFDictionaryGetValueIfPresent(values[index], (const void *)KEY_FS_SUBTYPE, (const void **)&readSubTypeID)) &&
(CFNumberCompare(subTypeID, readSubTypeID, NULL) == 0)) {
FSNameKey = keys[index];
break;
}
}
CFRelease(subTypeID);
/* If a personality hasn't been found, use the last value in the dictionary (note the content of CFDictionary is unordered so choosing the last doesn't produce consistent result) */
if (NULL == FSNameKey) {
FSNameKey = keys[numPersonalities - 1]; // is selecting the last entry right ?
}
// Get FSName from the FSPersonalities entry
CFDictionaryRef FSNameDict;
if (localized == true) {
FSNameDict = CFDictionaryGetValue(localPersonalities, FSNameKey);
} else {
FSNameDict = CFDictionaryGetValue(globalPersonalities, FSNameKey);
}
if (NULL != FSNameDict) {
CFStringRef tempName = CFDictionaryGetValue(FSNameDict, (const void *)KEY_FS_NAME);
CFStringRef encrName = CFDictionaryGetValue(FSNameDict, CFSTR(kFSCoreStorageEncryptNameKey));
if (tempName) {
if (encrName) {
formatName = (void*)CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
if (formatName != NULL) {
(void)CFDictionarySetValue((void*)formatName, tempName, encrName);
}
} else {
formatName = tempName;
}
}
}
if (values != valuesBuffer) free(values);
if (keys != keysBuffer) free(keys);
}
}
// If all failed, return "Unknown format (f_fstypename)" as the last resort
if (NULL == formatName) {
static CFStringRef unknownTypeString = NULL;
CFStringRef unknownFSNameString = NULL;
// This should use the framework bundle this code resides. CarbonCore ??? */
if (NULL == unknownTypeString) unknownTypeString = CFCopyLocalizedString(UNKNOWN_FS_NAME, "This string is displayed when localized file system name cannot be determined.");
unknownFSNameString = CFStringCreateWithFormat(NULL, NULL, CFSTR("%@ (%@)"), unknownTypeString, fsType);
formatName = (void*)unknownFSNameString;
}
// Cache the result
OSSpinLockLock(&__FSLocalizedNameTableLock);
if (NULL == __FSLocalizedNameTable) __FSLocalizedNameTable = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
// NSLog(CFSTR("Setting value %@ for key %@\n"), formatName, formatNameTableKey);
CFDictionarySetValue(__FSLocalizedNameTable, (const void *)formatNameTableKey, (const void *)formatName);
OSSpinLockUnlock(&__FSLocalizedNameTableLock);
// NSLog(CFSTR("Localized Name Table = %@\n"), __FSLocalizedNameTable);
if (NULL != bundle) CFRelease(bundle); // it has to be released here since formatName might be owned by the bundle
}
CFRelease(formatNameTableKey);
if (CFGetTypeID(formatName) == CFStringGetTypeID()) {
return CFRetain(formatName);
} else if (CFGetTypeID(formatName) == CFDictionaryGetTypeID()) {
// Dictionary with the (possibly localized) name as the key, and the encrypted name as the value
// If we want the encrypted name, we return the value, else we return the key
size_t numEntries = CFDictionaryGetCount((void*)formatName);
void *keyNames[numEntries];
void *values[numEntries];
CFDictionaryGetKeysAndValues((void*)formatName, (const void**)&keyNames, (const void**)&values);
if (encrypted)
return CFRetain(values[0]);
else
return CFRetain(keyNames[0]);
}
return CFRetain(formatName);
}
CFStringRef _FSCopyLocalizedNameForVolumeFormatAtURL(CFURLRef url)
{
CFStringRef formatName = NULL;
uint8_t buffer[MAXPATHLEN + 1];
if ((NULL != url) && CFURLGetFileSystemRepresentation(url, true, buffer, MAXPATHLEN)) {
struct statfs fsInfo;
bool encrypted = false;
if (statfs((char *)buffer, &fsInfo) == 0) {
CFStringRef fsType = CFStringCreateWithCString(NULL, fsInfo.f_fstypename, kCFStringEncodingASCII);
encrypted = IsEncrypted(fsInfo.f_mntfromname);
#ifdef _DARWIN_FEATURE_64_BIT_INODE
formatName = FSCopyFormatNameForFSType(fsType, fsInfo.f_fssubtype, true, encrypted);
#else
formatName = FSCopyFormatNameForFSType(fsType, fsInfo.f_reserved1, true, encrypted);
#endif
CFRelease(fsType);
}
}
return formatName;
}
CFStringRef _FSCopyNameForVolumeFormatAtURL(CFURLRef url)
{
CFStringRef formatName = NULL;
uint8_t buffer[MAXPATHLEN + 1];
if ((NULL != url) && CFURLGetFileSystemRepresentation(url, true, buffer, MAXPATHLEN)) {
struct statfs fsInfo;
if (statfs((char *)buffer, &fsInfo) == 0) {
CFStringRef fsType = CFStringCreateWithCString(NULL, fsInfo.f_fstypename, kCFStringEncodingASCII);
bool encrypted = false;
encrypted = IsEncrypted(fsInfo.f_mntfromname);
#ifdef _DARWIN_FEATURE_64_BIT_INODE
formatName = FSCopyFormatNameForFSType(fsType, fsInfo.f_fssubtype, false, encrypted);
#else
formatName = FSCopyFormatNameForFSType(fsType, fsInfo.f_reserved1, false, encrypted);
#endif
CFRelease(fsType);
}
}
return formatName;
}
CFStringRef _FSCopyLocalizedNameForVolumeFormatAtNode(CFStringRef devnode)
{
CFStringRef formatName = NULL;
char devnodename[MAXPATHLEN + 1];
char fsname[MAX_FSNAME];
int fssubtype = 0;
/* convert CFStringRef devnode to CSTR */
if (true == CFStringGetCString(devnode, devnodename, MAXPATHLEN + 1, kCFStringEncodingUTF8)) {
bool encrypted = false;
encrypted = IsEncrypted(devnodename);
/* get fsname and fssubtype */
memset(fsname, MAX_FSNAME, 0);
if (getfstype(devnodename, fsname, &fssubtype) == true) {
/* get unlocalized string */
CFStringRef fsType = CFStringCreateWithCString(NULL, fsname, kCFStringEncodingASCII);
formatName = FSCopyFormatNameForFSType(fsType, fssubtype, true, encrypted);
CFRelease(fsType);
}
}
return formatName;
}
CFStringRef _FSCopyNameForVolumeFormatAtNode(CFStringRef devnode)
{
CFStringRef formatName = NULL;
char devnodename[MAXPATHLEN + 1];
char fsname[MAX_FSNAME];
int fssubtype = 0;
/* convert CFStringRef devnode to CSTR */
if (true == CFStringGetCString(devnode, devnodename, MAXPATHLEN + 1, kCFStringEncodingUTF8)) {
bool encrypted;
encrypted = IsEncrypted(devnodename);
/* get fsname and fssubtype */
memset(fsname, MAX_FSNAME, 0);
if (getfstype(devnodename, fsname, &fssubtype) == true) {
/* get unlocalized string */
CFStringRef fsType = CFStringCreateWithCString(NULL, fsname, kCFStringEncodingASCII);
formatName = FSCopyFormatNameForFSType(fsType, fssubtype, false, encrypted);
CFRelease(fsType);
}
}
return formatName;
}
/* Return the fsname and subtype number for devnode */
bool getfstype(char *devnode, char *fsname, int *fssubtype)
{
/* Check if HFS */
if (is_hfs(devnode, fssubtype) == true) {
strcpy(fsname, HFS_NAME);
return true;
}
/* Check if MSDOS */
if (is_msdos(devnode, fssubtype) == true) {
strcpy(fsname, MSDOS_NAME);
return true;
}
return false;
}
#define SW16(x) OSSwapBigToHostInt16(x)
#define SW32(x) OSSwapBigToHostInt32(x)
/* Check if the disk is HFS disk and return its subtypes */
bool is_hfs(char *devnode, int *fssubtype)
{
HFSPlusVolumeHeader *vhp;
off_t offset = 0;
char *buffer = NULL;
int fd = 0;
bool retval = false;
/* default fssubtype to non-existing value */
*fssubtype = -1;
buffer = (char *)malloc(MAX_HFS_BLOCK_READ);
if (!buffer) {
goto out;
}
/* open the device */
fd = open(devnode, O_RDONLY | O_NDELAY, 0);
if (fd <= 0) {
goto out;
}
/* read volume header (512 bytes, block 2) from the device */
if (getblk(fd, 2, MAX_HFS_BLOCK_READ, buffer) < MAX_HFS_BLOCK_READ) {
goto out;
}
/* Check if it is a HFS volume */
if (getwrapper((HFSMasterDirectoryBlock *)buffer, &offset)) {
if (getblk(fd, 2 + (offset/MAX_HFS_BLOCK_READ), MAX_HFS_BLOCK_READ, buffer) < MAX_HFS_BLOCK_READ) {
goto out;
}
}
vhp = (HFSPlusVolumeHeader *)buffer;
/* Validate signature */
switch (SW16(vhp->signature)) {
case kHFSPlusSigWord: {
if (SW16(vhp->version) != kHFSPlusVersion) {
goto out;
}
break;
}
case kHFSXSigWord: {
if (SW16(vhp->version) != kHFSXVersion) {
goto out;
}
break;
}
case kHFSSigWord: {
/* HFS */
*fssubtype = kHFSSubType;
retval = true;
goto out;
}
default: {
goto out;
}
};
if ((vhp->journalInfoBlock != 0) && (SW32(vhp->attributes) & kHFSVolumeJournaledMask)) {
/* Journaled */
*fssubtype = kHFSJSubType;
}
if (SW16(vhp->signature) == kHFSXSigWord) {
BTHeaderRec * bthp;
off_t foffset;
foffset = (off_t)SW32(vhp->catalogFile.extents[0].startBlock) * (off_t)SW32(vhp->blockSize);
if (getblk(fd, (offset/MAX_HFS_BLOCK_READ) + (foffset/MAX_HFS_BLOCK_READ) , MAX_HFS_BLOCK_READ, buffer) < MAX_HFS_BLOCK_READ) {
goto out;
}
bthp = (BTHeaderRec *)&buffer[sizeof(BTNodeDescriptor)];
if ((SW16(bthp->maxKeyLength) == kHFSPlusCatalogKeyMaximumLength) &&
(bthp->keyCompareType == kHFSBinaryCompare)) {
/* HFSX */
if (*fssubtype == kHFSJSubType) {
/* Journaled HFSX */
*fssubtype = kHFSXJSubType;
} else {
/* HFSX */
*fssubtype = kHFSXSubType;
}
}
}
if (*fssubtype < 0) {
/* default HFS Plus */
*fssubtype = kHFSPlusSubType;
}
retval = true;
out:
if (buffer) {
free(buffer);
}
if (fd > 0) {
close(fd);
}
return retval;
}
/* Check if the disk is MSDOS disk and return its subtypes */
bool is_msdos(char *devnode, int *fssubtype)
{
union bootsector *bsp;
struct byte_bpb710 *b710;
u_int32_t FATSectors;
u_int32_t TotalSectors;
u_int32_t countOfClusters;
u_int32_t DataSectors;
u_int32_t RootDirSectors;
u_int16_t BytesPerSector;
u_int8_t SectorsPerCluster;
char *buffer = NULL;
int fd = 0;
bool retval = false;
/* default fssubtype to non-existing value */
*fssubtype = -1;
buffer = (char *)malloc(MAX_DOS_BLOCKSIZE);
if (!buffer) {
goto out;
}
/* open the device */
fd = open(devnode, O_RDONLY | O_NDELAY, 0);
if (fd <= 0) {
goto out;
}
/* read the block */
if (getblk(fd, 0, MAX_DOS_BLOCKSIZE, buffer) < MAX_DOS_BLOCKSIZE) {
goto out;
}
bsp = (union bootsector *)buffer;
b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB;
/* The first three bytes are an Intel x86 jump instruction. It should be one
* of the following forms:
* 0xE9 0x?? 0x??
* 0xEC 0x?? 0x90
* where 0x?? means any byte value is OK.
*/
if (bsp->bs50.bsJump[0] != 0xE9
&& (bsp->bs50.bsJump[0] != 0xEB || bsp->bs50.bsJump[2] != 0x90)) {
goto out;
}
/* We only work with 512, 1024, and 2048 byte sectors */
BytesPerSector = getushort(b710->bpbBytesPerSec);
if ((BytesPerSector < 0x200) || (BytesPerSector & (BytesPerSector - 1)) || (BytesPerSector > 0x800)) {
goto out;
}
/* Check to make sure valid sectors per cluster */
SectorsPerCluster = b710->bpbSecPerClust;
if ((SectorsPerCluster == 0 ) || (SectorsPerCluster & (SectorsPerCluster - 1))) {
goto out;
}
RootDirSectors = ((getushort(b710->bpbRootDirEnts) * 32) + (BytesPerSector - 1)) / BytesPerSector;
if (getushort(b710->bpbFATsecs)) {
FATSectors = getushort(b710->bpbFATsecs);
} else {
FATSectors = getulong(b710->bpbBigFATsecs);
}
if (getushort(b710->bpbSectors)) {
TotalSectors = getushort(b710->bpbSectors);
} else {
TotalSectors = getulong(b710->bpbHugeSectors);
}
DataSectors = TotalSectors - (getushort(b710->bpbResSectors) + (b710->bpbFATs * FATSectors) + RootDirSectors);
countOfClusters = DataSectors/(b710->bpbSecPerClust);
if (countOfClusters < 4085) {
/* FAT12 */
*fssubtype = 0;
} else if (countOfClusters < 65525) {
/* FAT16 */
*fssubtype = 1;
} else {
/* FAT32 */
*fssubtype = 2;
}
retval = true;
out:
if (buffer) {
free(buffer);
}
if (fd > 0) {
close(fd);
}
return retval;
}
/* read raw block from disk */
static int getblk(int fd, unsigned long blknum, int blksize, char* buf)
{
off_t offset;
int bytes_read;
offset = (off_t)blknum * (off_t)blksize;
if ((bytes_read = pread(fd, buf, blksize, offset)) != blksize) {
return (-1);
}
return (bytes_read);
}
/* get HFS wrapper information */
static int getwrapper(const HFSMasterDirectoryBlock *mdbp, off_t *offset)
{
if ((SW16(mdbp->drSigWord) != kHFSSigWord) ||
(SW16(mdbp->drEmbedSigWord) != kHFSPlusSigWord)) {
return(0);
}
*offset = SW16(mdbp->drAlBlSt) * 512;
*offset += (u_int64_t)SW16(mdbp->drEmbedExtent.startBlock) * (u_int64_t)SW32(mdbp->drAlBlkSiz);
return (1);
}
static bool
IsEncrypted(const char *bsdname)
{
bool retval = false;
const char *diskname = NULL;
CFMutableDictionaryRef ioMatch; //IOServiceGetMatchingService() releases:!
io_object_t ioObj = IO_OBJECT_NULL;
CFBooleanRef lvfIsEncr = NULL;
if (strncmp(bsdname, _PATH_DEV, strlen(_PATH_DEV)) == 0) {
diskname = bsdname + strlen(_PATH_DEV);
}
if (diskname == NULL)
goto finish;
if (strncmp(diskname, "rdisk", 5) == 0)
diskname++;
//look up the IOMedia object
ioMatch = IOBSDNameMatching(kIOMasterPortDefault, 0, diskname);
if (!ioMatch)
goto finish;
// Setting this allows a fast-path lookup to happen
// see 10248763
CFDictionarySetValue(ioMatch, CFSTR(kIOProviderClassKey), CFSTR(kIOMediaClass));
ioObj = IOServiceGetMatchingService(kIOMasterPortDefault, ioMatch);
ioMatch = NULL;
//IOServiceGetMatching() released ioMatch
if (ioObj == IO_OBJECT_NULL) {
goto finish;
}
lvfIsEncr = IORegistryEntryCreateCFProperty(ioObj, CFSTR(kCoreStorageIsEncryptedKey), nil, 0);
if (lvfIsEncr == NULL)
retval = false;
else
retval = CFBooleanGetValue(lvfIsEncr);
finish:
if (lvfIsEncr)
CFRelease(lvfIsEncr);
if (ioObj != IO_OBJECT_NULL) {
IOObjectRelease(ioObj);
}
return retval;
}