This repository has been archived by the owner on Oct 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
EFIDiskUtil.m
164 lines (130 loc) · 4.4 KB
/
EFIDiskUtil.m
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
//
// EFIDiskUtil.m
// Boot2EFI
//
// Created by Eduard Sionov on 1/16/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "EFIDiskUtil.h"
@implementation EFIDiskUtil
- (id)init {
self = [super init];
assert(self);
return self;
}
- (NSMutableArray*)deviceNodes {
// run task
NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/usr/sbin/diskutil"];
NSArray *arguments;
arguments = [NSArray arrayWithObject:@"list"];
[task setArguments: arguments];
NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
NSFileHandle *file;
file = [pipe fileHandleForReading];
[task launch];
[task waitUntilExit];
NSData *data;
data = [file readDataToEndOfFile];
NSString *string;
string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
// deviceID array
NSMutableArray * deviceNodes = [NSMutableArray array];
//***********************************************************************************
// searching strings with substring "/dev/disk". it means that its disk identifiers
int _beg = 0;//begin of ich string
int _end = 0;//end of ich string
int diskCount = 0;
NSString * stringToSearch = @"/dev/disk";
for (int i = 0; i < [string length]; i++) {
if ([string characterAtIndex:i] == '\n') {
_end = i;
NSString * subStr = [[NSString alloc] initWithString:[string substringWithRange:NSMakeRange(_beg,_end - _beg)]];
if ([subStr rangeOfString:stringToSearch].location == 0) {
diskCount++;
[deviceNodes addObject:subStr];
NSLog(@"Disk detected: %d with name : %@",diskCount,subStr);
}
_beg = _end+1;
}
}
return deviceNodes;
}
- (DeviceInformation*)deviceInfoWithNode:(NSString*)deviceNode {
DeviceInformation * deviceInfo = [[DeviceInformation alloc] init];
deviceInfo.deviceNode = deviceNode;
// execute task
NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/usr/sbin/diskutil"];
NSArray *arguments;
arguments = [NSArray arrayWithObjects: @"info", deviceNode, nil, nil];
[task setArguments: arguments];
NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
NSFileHandle *file;
file = [pipe fileHandleForReading];
[task launch];
[task waitUntilExit];
NSData *data;
data = [file readDataToEndOfFile];
NSString *string;
string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(@"%@",string);
//***********************************************************************************
// searching strings with substring "Media Name:". it is name of device
int _beg = 0;//begin of ich string
int _end = 0;//end of ich string
NSString * stringToSearch = @"Device / Media Name:";
for (int i = 0; i < [string length]; i++)
{
if ([string characterAtIndex:i] == '\n')
{
_end = i;
NSString * subStr = [[NSString alloc] initWithString:[string substringWithRange:
NSMakeRange(_beg,_end - _beg)]];
if ([subStr rangeOfString:stringToSearch].location == 3)
{
NSString * devName = [[NSString alloc] initWithString:[subStr substringWithRange:
NSMakeRange(29,[subStr length] - 29)]];
deviceInfo.deviceName = devName;
}
_beg = _end+1;
}
}
//***********************************************************************************
// searching strings with substring "Media Name:". it is name of device
_beg = 0;//begin of ich string
_end = 0;//end of ich string
stringToSearch = @"Partition Type:";
for (int i = 0; i < [string length]; i++)
{
if ([string characterAtIndex:i] == '\n')
{
_end = i;
NSString * subStr = [[NSString alloc] initWithString:[string substringWithRange:
NSMakeRange(_beg,_end - _beg)]];
if ([subStr rangeOfString:stringToSearch].location == 3)
{
NSString * partType = [[NSString alloc] initWithString:[subStr substringWithRange:
NSMakeRange(29,[subStr length] - 29)]];
if ([partType isEqualToString:@"GUID_partition_scheme"]) {
deviceInfo.deviceType = GUID_partition_scheme;
}
if ([partType isEqualToString:@"Apple_partition_scheme"]) {
deviceInfo.deviceType = Apple_partition_scheme;
}
if ([partType isEqualToString:@"FDisk_partition_scheme"]) {
deviceInfo.deviceType = FDisk_partition_scheme;
}
}
_beg = _end+1;
}
}
return deviceInfo;
}
@end