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
|
/*
* This file has been modified for the cdrkit suite.
*
* The behaviour and appearence of the program code below can differ to a major
* extent from the version distributed by the original author(s).
*
* For details, see Changelog file distributed with the cdrkit package. If you
* received this file from another source then ask the distributing person for
* a log of modifications.
*
*/
/* @(#)scsi_scan.c 1.19 04/04/16 Copyright 1997-2004 J. Schilling */
/*
* Scan SCSI Bus.
* Stolen from sformat. Need a more general form to
* re-use it in sformat too.
*
* Copyright (c) 1997-2004 J. Schilling
*/
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* 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; see the file COPYING. If not, write to the Free Software
* Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <mconfig.h>
#include <stdio.h>
#include <errno.h>
#include <schily.h>
#include <usal/usalcmd.h>
#include <usal/scsidefs.h>
#include <usal/scsireg.h>
#include <usal/scsitransp.h>
#include "scsi_scan.h"
#include "wodim.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
static void print_product(FILE *f, struct scsi_inquiry *ip);
int select_target(SCSI *usalp, FILE *f);
#define MAXDEVCOUNT (256+26)
extern BOOL check_linux_26();
static void print_product(FILE *f, struct scsi_inquiry *ip) {
fprintf(f, "'%.8s' ", ip->vendor_info);
fprintf(f, "'%.16s' ", ip->prod_ident);
fprintf(f, "'%.4s' ", ip->prod_revision);
if (ip->add_len < 31) {
fprintf(f, "NON CCS ");
}
usal_fprintdev(f, ip);
}
SCSI * open_auto(int64_t need_size, int debug, int lverbose) {
int res;
SCSI * usalp = NULL;
char errstr[80];
#ifdef __linux__
/* quick-and-dirty code but should do what is supposed to, more quickly */
/*
* For Linux, try these strategies, in order:
* 1. stat /dev/cdrw or /dev/dvdrw, depending on size we need.
* 2. Read /proc/sys/dev/cdrom/info, look for a CD-R/DVD-R.
* Will fail for kernel 2.4 or if cdrom module not loaded.
* 3. stat /dev/cdrom, just assume that it can write media.
An example for procfs file contents, beware of the TABs
---
CD-ROM information, Id: cdrom.c 3.20 2003/12/17
drive name: hdc hda
drive speed: 40 40
drive # of slots: 1 1
Can close tray: 1 1
Can open tray: 1 1
Can lock tray: 1 1
Can change speed: 1 1
Can select disk: 0 0
Can read multisession: 1 1
Can read MCN: 1 1
Reports media changed: 1 1
Can play audio: 1 1
Can write CD-R: 0 1
Can write CD-RW: 0 1
Can read DVD: 1 1
Can write DVD-R: 0 1
Can write DVD-RAM: 0 1
Can read MRW: 0 1
Can write MRW: 0 1
Can write RAM: 0 1
---
*/
struct stat statbuf;
/* XXX Good guess? BD-RE recorders may not support CDRW anymore... */
char *type="CD-R", *key="Can write CD-R:", *guessdev="/dev/cdrw", *result=NULL;
FILE *fh;
if( need_size > 360000*2048 ) {
type="DVD-R";
guessdev="/dev/dvdrw";
key="Can write DVD-R:";
}
if(need_size>10240) /* don't bother with weird numbers */
fprintf(stderr, "Looking for a %s drive to store %.2f MiB...\n", type, (float)need_size/1048576.0);
if(0==stat(guessdev, &statbuf))
result=guessdev;
else if(0!= (fh = fopen("/proc/sys/dev/cdrom/info", "r")) ) {
/* ok, going the hard way */
char *nameline=NULL;
static char buf[256];
int kn = strlen(key);
buf[255]='\0';
while(fgets(buf, sizeof(buf), fh)) {
if(0==strncmp(buf, "drive name:", 11))
nameline=strdup(buf);
if(nameline && 0==strncmp(buf, key, kn)) {
int p=kn;
char *descptr=nameline+11; /* start at the known whitespace */
while(p<sizeof(buf) && buf[p]) {
if(buf[p]=='1' || buf[p]=='0') {
/* find the beginning of the descriptor */
for(;isspace((Uchar) *descptr);descptr++)
;
}
if(buf[p]=='1') {
result=descptr-5;
/* terminate on space/newline and stop there */
for(;*descptr;descptr++) {
if(isspace((Uchar) *descptr))
*(descptr--)='\0';
}
strncpy(result, "/dev/", 5);
break;
}
else { /* no hit, move to after word ending */
for(; *descptr && ! isspace((Uchar) *descptr); descptr++)
;
}
p++;
}
}
}
fclose(fh);
}
if(result)
fprintf(stderr, "Detected %s drive: %s\n", type, result);
if (0==stat("/dev/cdrom", &statbuf)) {
result = "/dev/cdrom";
fprintf(stderr, "Using /dev/cdrom of unknown capabilities\n");
}
if(result)
return usal_open(result, errstr, sizeof(errstr), debug, lverbose);
#endif /* __linux__ */
usalp = usal_open(NULL, errstr, sizeof(errstr), debug, lverbose);
if(!usalp)
return NULL;
res = list_devices(usalp, stdout, 1);
if(res>0)
return usalp;
else
usal_close(usalp);
return NULL;
}
int list_devices(SCSI *usalp, FILE *f, int pickup_first) {
int initiator;
int i;
int bus;
int tgt;
int lun = 0;
BOOL have_tgt;
int fd, ndevs=0;
struct stat statbuf;
char *lines[MAXDEVCOUNT];
char buf[256], perms[8];
usalp->silent++;
/* XXX should be done before opening usal fprintf(stderr, "Beginning native device scan. This may take a while if devices are busy...\n"); */
for (bus = 0; bus < 1256; bus++) {
usal_settarget(usalp, bus, 0, 0);
if (!usal_havebus(usalp, bus))
continue;
initiator = usal_initiator_id(usalp);
//fprintf(f, "scsibus%d:\n", bus);
for (tgt = 0; tgt < 16; tgt++) {
usal_settarget(usalp, bus, tgt, lun);
have_tgt = unit_ready(usalp) || usalp->scmd->error != SCG_FATAL;
if (!have_tgt && tgt > 7) {
if (usalp->scmd->ux_errno == EINVAL)
break;
continue;
}
fd=usal_fileno(usalp, bus, tgt, lun);
strcpy(perms,"------");
if(fd>=0 && 0==fstat(fd, &statbuf)) {
if(statbuf.st_mode&S_IRUSR) perms[0]= 'r';
if(statbuf.st_mode&S_IWUSR) perms[1]= 'w';
if(statbuf.st_mode&S_IRGRP) perms[2]= 'r';
if(statbuf.st_mode&S_IWGRP) perms[3]= 'w';
if(statbuf.st_mode&S_IROTH) perms[4]= 'r';
if(statbuf.st_mode&S_IWOTH) perms[5]= 'w';
}
getdev(usalp, FALSE);
if(usalp->inq->type == INQ_ROMD || usalp->inq->type == INQ_WORM) {
char *p;
for(p=usalp->inq->vendor_info + 7 ; p >= usalp->inq->vendor_info; p--) {
if(isspace((unsigned char)*p))
*p='\0';
else
break;
}
for(p=usalp->inq->prod_ident + 15 ; p >= usalp->inq->prod_ident; p--) {
if(isspace((unsigned char)*p))
*p='\0';
else
break;
}
snprintf(buf, sizeof(buf), "%2d dev='%s'\t%s : '%.8s' '%.16s'\n", ndevs, usal_natname(usalp, bus, tgt, lun), perms, usalp->inq->vendor_info, usalp->inq->prod_ident);
/* alternative use, only select the first device */
if(pickup_first) {
printf("Using drive: %s\n", usal_natname(usalp, bus, tgt, lun));
return 1;
}
lines[ndevs++]=strdup(buf);
}
}
}
usalp->silent--;
/* should have been returned before if there was a recorder */
if(pickup_first)
return 0;
/* now start the output */
fprintf(stdout, "%s: Overview of accessible drives (%d found) :\n"
"-------------------------------------------------------------------------\n",
get_progname(), ndevs);
for(i=0;i<ndevs;i++) {
fprintf(stdout, "%s", lines[i]);
free(lines[i]);
}
fprintf(stdout, "-------------------------------------------------------------------------\n");
return ndevs;
}
int select_target(SCSI *usalp, FILE *f) {
int initiator;
#ifdef FMT
int cscsibus = usal_scsibus(usalp);
int ctarget = usal_target(usalp);
int clun = usal_lun(usalp);
#endif
int n;
int low = -1;
int high = -1;
int amt = 0;
int bus;
int tgt;
int lun = 0;
BOOL have_tgt;
usalp->silent++;
for (bus = 0; bus < 1256; bus++) {
usal_settarget(usalp, bus, 0, 0);
if (!usal_havebus(usalp, bus))
continue;
initiator = usal_initiator_id(usalp);
fprintf(f, "scsibus%d:\n", bus);
for (tgt = 0; tgt < 16; tgt++) {
n = bus*100 + tgt;
usal_settarget(usalp, bus, tgt, lun);
have_tgt = unit_ready(usalp) || usalp->scmd->error != SCG_FATAL;
if (!have_tgt && tgt > 7) {
if (usalp->scmd->ux_errno == EINVAL)
break;
continue;
}
#ifdef FMT
if (print_disknames(bus, tgt, -1) < 8)
fprintf(f, "\t");
else
fprintf(f, " ");
#else
fprintf(f, "\t");
#endif
if (fprintf(f, "%d,%d,%d", bus, tgt, lun) < 8)
fprintf(f, "\t");
else
fprintf(f, " ");
fprintf(f, "%3d) ", n);
if (tgt == initiator) {
fprintf(f, "HOST ADAPTOR\n");
continue;
}
if (!have_tgt) {
/*
* Hack: fd -> -2 means no access
*/
fprintf(f, "%c\n", usalp->fd == -2 ? '?':'*');
continue;
}
amt++;
if (low < 0)
low = n;
high = n;
getdev(usalp, FALSE);
print_product(f, usalp->inq);
}
}
usalp->silent--;
if (low < 0) {
errmsgno(EX_BAD, "No target found.\n");
return (0);
}
n = -1;
#ifdef FMT
getint("Select target", &n, low, high);
bus = n/100;
tgt = n%100;
usal_settarget(usalp, bus, tgt, lun);
return (select_unit(usalp));
usal_settarget(usalp, cscsibus, ctarget, clun);
#endif
return (amt);
}
|