2 * This file has been modified for the cdrkit suite.
4 * The behaviour and appearence of the program code below can differ to a major
5 * extent from the version distributed by the original author(s).
7 * For details, see Changelog file distributed with the cdrkit package. If you
8 * received this file from another source then ask the distributing person for
9 * a log of modifications.
13 /* @(#)scsi_scan.c 1.19 04/04/16 Copyright 1997-2004 J. Schilling */
16 * Stolen from sformat. Need a more general form to
17 * re-use it in sformat too.
19 * Copyright (c) 1997-2004 J. Schilling
22 * This program is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License version 2
24 * as published by the Free Software Foundation.
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License along with
32 * this program; see the file COPYING. If not, write to the Free Software
33 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
41 #include <usal/usalcmd.h>
42 #include <usal/scsidefs.h>
43 #include <usal/scsireg.h>
44 #include <usal/scsitransp.h>
46 #include "scsi_scan.h"
49 #include <sys/types.h>
57 static void print_product(FILE *f, struct scsi_inquiry *ip);
58 int select_target(SCSI *usalp, FILE *f);
60 #define MAXDEVCOUNT (256+26)
62 extern BOOL check_linux_26();
64 static void print_product(FILE *f, struct scsi_inquiry *ip) {
65 fprintf(f, "'%.8s' ", ip->vendor_info);
66 fprintf(f, "'%.16s' ", ip->prod_ident);
67 fprintf(f, "'%.4s' ", ip->prod_revision);
68 if (ip->add_len < 31) {
69 fprintf(f, "NON CCS ");
71 usal_fprintdev(f, ip);
74 SCSI * open_auto(int64_t need_size, int debug, int lverbose) {
80 /* quick-and-dirty code but should do what is supposed to, more quickly */
83 * For Linux, try these strategies, in order:
84 * 1. stat /dev/cdrw or /dev/dvdrw, depending on size we need.
85 * 2. Read /proc/sys/dev/cdrom/info, look for a CD-R/DVD-R.
86 * Will fail for kernel 2.4 or if cdrom module not loaded.
87 * 3. stat /dev/cdrom, just assume that it can write media.
89 An example for procfs file contents, beware of the TABs
92 CD-ROM information, Id: cdrom.c 3.20 2003/12/17
100 Can change speed: 1 1
102 Can read multisession: 1 1
104 Reports media changed: 1 1
110 Can write DVD-RAM: 0 1
118 /* XXX Good guess? BD-RE recorders may not support CDRW anymore... */
119 char *type="CD-R", *key="Can write CD-R:", *guessdev="/dev/cdrw", *result=NULL;
122 if( need_size > 360000*2048 ) {
124 guessdev="/dev/dvdrw";
125 key="Can write DVD-R:";
128 if(need_size>10240) /* don't bother with weird numbers */
129 fprintf(stderr, "Looking for a %s drive to store %.2f MiB...\n", type, (float)need_size/1048576.0);
130 if(0==stat(guessdev, &statbuf))
132 else if(0!= (fh = fopen("/proc/sys/dev/cdrom/info", "r")) ) {
133 /* ok, going the hard way */
135 static char buf[256];
136 int kn = strlen(key);
140 while(fgets(buf, sizeof(buf), fh)) {
141 if(0==strncmp(buf, "drive name:", 11))
142 nameline=strdup(buf);
143 if(nameline && 0==strncmp(buf, key, kn)) {
145 char *descptr=nameline+11; /* start at the known whitespace */
146 while(p<sizeof(buf) && buf[p]) {
147 if(buf[p]=='1' || buf[p]=='0') {
148 /* find the beginning of the descriptor */
149 for(;isspace((Uchar) *descptr);descptr++)
154 /* terminate on space/newline and stop there */
155 for(;*descptr;descptr++) {
156 if(isspace((Uchar) *descptr))
159 strncpy(result, "/dev/", 5);
162 else { /* no hit, move to after word ending */
163 for(; *descptr && ! isspace((Uchar) *descptr); descptr++)
175 fprintf(stderr, "Detected %s drive: %s\n", type, result);
176 if (0==stat("/dev/cdrom", &statbuf)) {
177 result = "/dev/cdrom";
178 fprintf(stderr, "Using /dev/cdrom of unknown capabilities\n");
181 return usal_open(result, errstr, sizeof(errstr), debug, lverbose);
182 #endif /* __linux__ */
184 usalp = usal_open(NULL, errstr, sizeof(errstr), debug, lverbose);
187 res = list_devices(usalp, stdout, 1);
195 int list_devices(SCSI *usalp, FILE *f, int pickup_first) {
205 char *lines[MAXDEVCOUNT];
206 char buf[256], perms[8];
211 /* XXX should be done before opening usal fprintf(stderr, "Beginning native device scan. This may take a while if devices are busy...\n"); */
213 for (bus = 0; bus < 1256; bus++) {
214 usal_settarget(usalp, bus, 0, 0);
216 if (!usal_havebus(usalp, bus))
219 initiator = usal_initiator_id(usalp);
220 //fprintf(f, "scsibus%d:\n", bus);
222 for (tgt = 0; tgt < 16; tgt++) {
223 usal_settarget(usalp, bus, tgt, lun);
224 have_tgt = unit_ready(usalp) || usalp->scmd->error != SCG_FATAL;
226 if (!have_tgt && tgt > 7) {
227 if (usalp->scmd->ux_errno == EINVAL)
232 fd=usal_fileno(usalp, bus, tgt, lun);
233 strcpy(perms,"------");
234 if(fd>=0 && 0==fstat(fd, &statbuf)) {
235 if(statbuf.st_mode&S_IRUSR) perms[0]= 'r';
236 if(statbuf.st_mode&S_IWUSR) perms[1]= 'w';
237 if(statbuf.st_mode&S_IRGRP) perms[2]= 'r';
238 if(statbuf.st_mode&S_IWGRP) perms[3]= 'w';
239 if(statbuf.st_mode&S_IROTH) perms[4]= 'r';
240 if(statbuf.st_mode&S_IWOTH) perms[5]= 'w';
242 getdev(usalp, FALSE);
243 if(usalp->inq->type == INQ_ROMD || usalp->inq->type == INQ_WORM) {
246 for(p=usalp->inq->vendor_info + 7 ; p >= usalp->inq->vendor_info; p--) {
247 if(isspace((unsigned char)*p))
252 for(p=usalp->inq->prod_ident + 15 ; p >= usalp->inq->prod_ident; p--) {
253 if(isspace((unsigned char)*p))
258 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);
259 /* alternative use, only select the first device */
261 printf("Using drive: %s\n", usal_natname(usalp, bus, tgt, lun));
264 lines[ndevs++]=strdup(buf);
271 /* should have been returned before if there was a recorder */
275 /* now start the output */
277 fprintf(stdout, "%s: Overview of accessible drives (%d found) :\n"
278 "-------------------------------------------------------------------------\n",
279 get_progname(), ndevs);
280 for(i=0;i<ndevs;i++) {
281 fprintf(stdout, "%s", lines[i]);
284 fprintf(stdout, "-------------------------------------------------------------------------\n");
289 int select_target(SCSI *usalp, FILE *f) {
292 int cscsibus = usal_scsibus(usalp);
293 int ctarget = usal_target(usalp);
294 int clun = usal_lun(usalp);
307 for (bus = 0; bus < 1256; bus++) {
308 usal_settarget(usalp, bus, 0, 0);
310 if (!usal_havebus(usalp, bus))
313 initiator = usal_initiator_id(usalp);
314 fprintf(f, "scsibus%d:\n", bus);
316 for (tgt = 0; tgt < 16; tgt++) {
319 usal_settarget(usalp, bus, tgt, lun);
320 have_tgt = unit_ready(usalp) || usalp->scmd->error != SCG_FATAL;
322 if (!have_tgt && tgt > 7) {
323 if (usalp->scmd->ux_errno == EINVAL)
329 if (print_disknames(bus, tgt, -1) < 8)
336 if (fprintf(f, "%d,%d,%d", bus, tgt, lun) < 8)
340 fprintf(f, "%3d) ", n);
341 if (tgt == initiator) {
342 fprintf(f, "HOST ADAPTOR\n");
347 * Hack: fd -> -2 means no access
349 fprintf(f, "%c\n", usalp->fd == -2 ? '?':'*');
357 getdev(usalp, FALSE);
358 print_product(f, usalp->inq);
364 errmsgno(EX_BAD, "No target found.\n");
369 getint("Select target", &n, low, high);
372 usal_settarget(usalp, bus, tgt, lun);
373 return (select_unit(usalp));
375 usal_settarget(usalp, cscsibus, ctarget, clun);