forked from tedigh/ufiformat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ufi_detect.c
354 lines (318 loc) · 7.67 KB
/
ufi_detect.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
/*
ufiformat Version 0.9.9 2011/01/29
Copyright (C) 2005-2011 Kazuhiro Hayashi <[email protected]>
Copyright (C) 2005 John Floyd <[email protected]>
The method of formatting a floppy on USB-FDD used in this program
is introduced by Bruce M Simpson.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <linux/major.h>
#include <fcntl.h>
#include <scsi/scsi.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include "ufi_detect.h"
#ifndef SCSI_DISK_MAJOR
#define SCSI_DISK_MAJOR(M) ((M) == SCSI_DISK0_MAJOR || \
((M) >= SCSI_DISK1_MAJOR && (M) <= SCSI_DISK7_MAJOR) || \
((M) >= SCSI_DISK8_MAJOR && (M) <= SCSI_DISK15_MAJOR))
#endif
#define HOST_ID(idlun) ((idlun >> 24) & 0xff)
static char *pathcpy(char *dst, const char *src, size_t len)
{
if (strlen(src) >= len) {
errno = ENAMETOOLONG;
return NULL;
}
return strcpy(dst, src);
}
static int is_sd_device(const char *dev)
{
struct stat st;
if (stat(dev, &st) < 0) {
return -1;
}
return S_ISBLK(st.st_mode) && SCSI_DISK_MAJOR(major(st.st_rdev)) && (minor(st.st_rdev) & 0xf) == 0 ? 1 : 0;
}
static int is_sg_device(const char *dev)
{
struct stat st;
if (stat(dev, &st) < 0) {
return -1;
}
return S_ISCHR(st.st_mode) && major(st.st_rdev) == SCSI_GENERIC_MAJOR ? 1 : 0;
}
static int get_id_lun(const char *dev)
{
int fd;
int idlun[2];
if ((fd = open(dev, O_RDONLY | O_NONBLOCK)) < 0) {
return -1;
}
if (ioctl(fd, SCSI_IOCTL_GET_IDLUN, idlun) < 0) {
close(fd);
return -1;
}
close(fd);
return idlun[0];
}
static int get_sd_path(int idlun, char *path, size_t len, int host_id_only)
{
char tmp[PATH_MAX];
int i;
int eacces;
int idlun2;
eacces = 0;
/* BUG: only single usb fdd is supported for one host_id */
if (host_id_only) {
idlun = (idlun & 0xff) << 24;
}
for (i = 0; i < 256; i++) {
if (i < 26) {
sprintf(tmp, "/dev/sd%c", 'a' + i);
} else {
sprintf(tmp, "/dev/sd%c%c", 'a' + (i / 26), 'a' + (i % 26));
}
if ((idlun2 = get_id_lun(tmp)) == -1) {
if (errno == EACCES) {
eacces = 1;
}
} else if (idlun == idlun2) {
if (pathcpy(path, tmp, len) == NULL) {
return -1;
}
return 0;
}
}
errno = eacces ? EACCES : ENOENT;
return -1;
}
static int get_sg_path(int idlun, char *path, size_t len, int host_id_only)
{
char tmp[PATH_MAX];
int i;
int eacces;
int idlun2;
eacces = 0;
/* BUG: only single usb fdd is supported for one host_id */
if (host_id_only) {
idlun = (idlun & 0xff) << 24;
}
for (i = 0; i < 256; i++) {
sprintf(tmp, "/dev/sg%d", i);
if ((idlun2 = get_id_lun(tmp)) == -1) {
if (errno == EACCES) {
eacces = 1;
}
} else if (idlun == idlun2) {
if (pathcpy(path, tmp, len) == NULL) {
return -1;
}
return 0;
}
}
errno = eacces ? EACCES : ENOENT;
return -1;
}
int convert_to_sg_path(const char *sd, char *sg, size_t len)
{
int idlun;
int flag;
if ((flag = is_sg_device(sd)) < 0) {
return -1;
}
if (flag) {
if (pathcpy(sg, sd, len) == NULL) {
return -1;
}
return 0;
}
if ((flag = is_sd_device(sd)) < 0) {
return -1;
}
if (!flag) {
errno = ENODEV;
return -1;
}
if ((idlun = get_id_lun(sd)) == -1) {
return -1;
}
if (get_sg_path(idlun, sg, len, 0) < 0) {
errno = -errno;
return -1;
}
return 0;
}
int convert_to_sd_path(const char *sg, char *sd, size_t len)
{
int idlun;
int flag;
if ((flag = is_sd_device(sg)) < 0) {
return -1;
}
if (flag) {
if (pathcpy(sd, sg, len) == NULL) {
return -1;
}
return 0;
}
if ((flag = is_sg_device(sg)) < 0) {
return -1;
}
if (!flag) {
errno = ENODEV;
return -1;
}
if ((idlun = get_id_lun(sg)) == -1) {
return -1;
}
if (get_sd_path(idlun, sd, len, 0) < 0) {
errno = -errno;
return -1;
}
return 0;
}
static int is_usb_fdd_for_host(int host_id)
{
int i;
char path[PATH_MAX];
char buf[1024];
FILE *fp;
struct stat st;
sprintf(path, "/sys/class/scsi_host/host%d/device/../driver", host_id);
if (stat(path, &st) == 0) {
int n;
if ((n = readlink(path, buf, sizeof(buf))) < 12 || strncmp(buf + n - 12, "/usb-storage", 12) != 0) {
return 0;
}
sprintf(path, "/sys/class/scsi_host/host%d/device/../bInterfaceClass", host_id);
if ((fp = fopen(path, "r")) == NULL) {
return 0;
}
if (fgets(buf, sizeof(buf), fp) == NULL || strcmp(buf, "08\n") != 0) {
fclose(fp);
return 0;
}
fclose(fp);
sprintf(path, "/sys/class/scsi_host/host%d/device/../bInterfaceSubClass", host_id);
if ((fp = fopen(path, "r")) == NULL) {
return 0;
}
if (fgets(buf, sizeof(buf), fp) == NULL || strcmp(buf, "04\n") != 0) {
fclose(fp);
return 0;
}
fclose(fp);
return 1;
}
sprintf(path, "/proc/scsi/usb-storage/%d", host_id);
if ((fp = fopen(path, "r")) == NULL) {
for (i = 0; i < 256; i++) {
sprintf(path, "/proc/scsi/usb-storage-%d/%d", i, host_id);
if ((fp = fopen(path, "r")) != NULL) {
break;
}
}
if (fp == NULL) {
return 0;
}
}
while (fgets(buf, sizeof(buf), fp) != NULL) {
if (strcmp(buf, " Protocol: Uniform Floppy Interface (UFI)\n") == 0) {
fclose(fp);
return 1;
}
}
fclose(fp);
return 0;
}
int is_usb_fdd(const char *dev)
{
int idlun;
int is_sd;
int is_sg;
if ((is_sd = is_sd_device(dev)) < 0 || (is_sg = is_sg_device(dev)) < 0) {
return -1;
}
if (!is_sd && !is_sg) {
return 0;
}
if ((idlun = get_id_lun(dev)) == -1) {
return -1;
}
return is_usb_fdd_for_host(HOST_ID(idlun));
}
int get_usb_fdds(struct ufi_path_info **info)
{
int i;
struct ufi_path_info *top = NULL;
struct ufi_path_info *last = NULL;
struct ufi_path_info *current;
char sg_path[PATH_MAX];
char sd_path[PATH_MAX];
for (i = 0; i < 256; i++) {
if (is_usb_fdd_for_host(i)) {
if (get_sg_path(i, sg_path, sizeof(sg_path), 1) < 0) {
if (errno != ENOENT) {
free_ufi_path_info(top);
return -1;
}
sg_path[0] = '\0';
}
if (get_sd_path(i, sd_path, sizeof(sd_path), 1) < 0) {
if (errno != ENOENT) {
free_ufi_path_info(top);
return -1;
}
sd_path[0] = '\0';
}
current = malloc(sizeof(struct ufi_path_info));
current->host_id = i;
current->sg_path = sg_path[0] != '\0' ? strdup(sg_path) : NULL;
current->sd_path = sd_path[0] != '\0' ? strdup(sd_path) : NULL;
current->next = NULL;
if (last != NULL) {
last->next = current;
}
if (top == NULL) {
top = current;
}
last = current;
}
}
*info = top;
return 0;
}
void free_ufi_path_info(struct ufi_path_info *info)
{
while (info != NULL) {
if (info->sg_path != NULL) {
free(info->sg_path);
}
if (info->sd_path != NULL) {
free(info->sd_path);
}
info = info->next;
}
}