1 /*
2  * This file has been modified for the cdrkit suite.
3  *
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).
6  *
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.
10  *
11  */
12 
13 /* @(#)scsi_scan.c	1.19 04/04/16 Copyright 1997-2004 J. Schilling */
14 /*
15  *	Scan SCSI Bus.
16  *	Stolen from sformat. Need a more general form to
17  *	re-use it in sformat too.
18  *
19  *	Copyright (c) 1997-2004 J. Schilling
20  */
21 /*
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.
25  *
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.
30  *
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.
34  */
35 
36 #include <mconfig.h>
37 #include <stdio.h>
38 #include <errno.h>
39 #include <schily.h>
40 
41 #include <usal/usalcmd.h>
42 #include <usal/scsidefs.h>
43 #include <usal/scsireg.h>
44 #include <usal/scsitransp.h>
45 
46 #include "scsi_scan.h"
47 #include "wodim.h"
48 
49 #include <sys/types.h>
50 #include <sys/stat.h>
51 #include <unistd.h>
52 #include <string.h>
53 #include <ctype.h>
54 #include <stdlib.h>
55 
56 
57 static	void	print_product(FILE *f, struct scsi_inquiry *ip);
58 int	select_target(SCSI *usalp, FILE *f);
59 
60 #define MAXDEVCOUNT (256+26)
61 
62 extern BOOL check_linux_26();
63 
print_product(FILE * f,struct scsi_inquiry * ip)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 ");
70 	}
71 	usal_fprintdev(f, ip);
72 }
73 
open_auto(int64_t need_size,int debug,int lverbose)74 SCSI * open_auto(int64_t need_size, int debug, int lverbose) {
75 	int res;
76 	SCSI * usalp = NULL;
77 	char  errstr[80];
78 
79 #ifdef __linux__
80 	/* quick-and-dirty code but should do what is supposed to, more quickly */
81 
82 		/*
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.
88 
89      An example for procfs file contents, beware of the TABs
90 
91 ---
92 CD-ROM information, Id: cdrom.c 3.20 2003/12/17
93 
94 drive name:		hdc     hda
95 drive speed:		40      40
96 drive # of slots:       1       1
97 Can close tray:         1       1
98 Can open tray:          1       1
99 Can lock tray:          1       1
100 Can change speed:       1       1
101 Can select disk:        0       0
102 Can read multisession:  1       1
103 Can read MCN:           1       1
104 Reports media changed:  1       1
105 Can play audio:         1       1
106 Can write CD-R:		0       1
107 Can write CD-RW:        0       1
108 Can read DVD:           1       1
109 Can write DVD-R:        0       1
110 Can write DVD-RAM:      0       1
111 Can read MRW:           0       1
112 Can write MRW:          0       1
113 Can write RAM:          0       1
114 
115 ---
116 */
117 	struct stat statbuf;
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;
120 	FILE *fh;
121 
122 	if( need_size > 360000*2048 ) {
123 		type="DVD-R";
124 		guessdev="/dev/dvdrw";
125 		key="Can write DVD-R:";
126 	}
127 
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))
131 		result=guessdev;
132 	else if(0!= (fh = fopen("/proc/sys/dev/cdrom/info", "r")) ) {
133 		/* ok, going the hard way */
134 		char *nameline=NULL;
135 		static char buf[256];
136 		int kn = strlen(key);
137 
138 		buf[255]='\0';
139 
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)) {
144 				int p=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++)
150 							;
151 					}
152 					if(buf[p]=='1') {
153 						result=descptr-5;
154 						/* terminate on space/newline and stop there */
155 						for(;*descptr;descptr++) {
156 							if(isspace((Uchar) *descptr))
157 								*(descptr--)='\0';
158 						}
159 						strncpy(result, "/dev/", 5);
160 						break;
161 					}
162 					else { /* no hit, move to after word ending */
163 						for(; *descptr && ! isspace((Uchar) *descptr); descptr++)
164 							;
165 					}
166 					p++;
167 				}
168 			}
169 
170 		}
171 		fclose(fh);
172 	}
173 
174 	if(result)
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");
179 	}
180 	if(result)
181 		return usal_open(result, errstr, sizeof(errstr), debug, lverbose);
182 #endif /* __linux__ */
183 
184 	usalp = usal_open(NULL, errstr, sizeof(errstr), debug, lverbose);
185 	if(!usalp)
186 		return NULL;
187 	res = list_devices(usalp, stdout, 1);
188 	if(res>0)
189 		return usalp;
190 	else
191 		usal_close(usalp);
192 	return NULL;
193 }
194 
list_devices(SCSI * usalp,FILE * f,int pickup_first)195 int list_devices(SCSI *usalp, FILE *f, int pickup_first) {
196 	int	initiator;
197 	int	i;
198 	int	bus;
199 	int	tgt;
200 	int	lun = 0;
201 	BOOL	have_tgt;
202 
203 	int fd, ndevs=0;
204   	struct stat statbuf;
205 	char *lines[MAXDEVCOUNT];
206 	char buf[256], perms[8];
207 
208 
209 	usalp->silent++;
210 
211 	/* XXX should be done before opening usal fprintf(stderr, "Beginning native device scan. This may take a while if devices are busy...\n"); */
212 
213 	for (bus = 0; bus < 1256; bus++) {
214 		usal_settarget(usalp, bus, 0, 0);
215 
216 		if (!usal_havebus(usalp, bus))
217 			continue;
218 
219 		initiator = usal_initiator_id(usalp);
220 		//fprintf(f, "scsibus%d:\n", bus);
221 
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;
225 
226 			if (!have_tgt && tgt > 7) {
227 				if (usalp->scmd->ux_errno == EINVAL)
228 					break;
229 				continue;
230 			}
231 
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';
241 			}
242 			getdev(usalp, FALSE);
243 			if(usalp->inq->type == INQ_ROMD || usalp->inq->type == INQ_WORM) {
244 				char *p;
245 
246 				for(p=usalp->inq->vendor_info + 7 ; p >= usalp->inq->vendor_info; p--) {
247 					if(isspace((unsigned char)*p))
248 						*p='\0';
249 					else
250 						break;
251 				}
252 				for(p=usalp->inq->prod_ident + 15 ; p >= usalp->inq->prod_ident; p--) {
253 					if(isspace((unsigned char)*p))
254 						*p='\0';
255 					else
256 						break;
257 				}
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 */
260 				if(pickup_first) {
261 					printf("Using drive: %s\n", usal_natname(usalp, bus, tgt, lun));
262 					return 1;
263 				}
264 				lines[ndevs++]=strdup(buf);
265 			}
266 
267 		}
268 	}
269 	usalp->silent--;
270 
271 	/* should have been returned before if there was a recorder */
272 	if(pickup_first)
273 		return 0;
274 
275 	/* now start the output */
276 
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]);
282 		free(lines[i]);
283 	}
284 	fprintf(stdout,	"-------------------------------------------------------------------------\n");
285 
286 	return ndevs;
287 }
288 
select_target(SCSI * usalp,FILE * f)289 int select_target(SCSI *usalp, FILE *f) {
290 	int	initiator;
291 #ifdef	FMT
292 	int	cscsibus = usal_scsibus(usalp);
293 	int	ctarget  = usal_target(usalp);
294 	int	clun	 = usal_lun(usalp);
295 #endif
296 	int	n;
297 	int	low	= -1;
298 	int	high	= -1;
299 	int	amt	= 0;
300 	int	bus;
301 	int	tgt;
302 	int	lun = 0;
303 	BOOL	have_tgt;
304 
305 	usalp->silent++;
306 
307 	for (bus = 0; bus < 1256; bus++) {
308 		usal_settarget(usalp, bus, 0, 0);
309 
310 		if (!usal_havebus(usalp, bus))
311 			continue;
312 
313 		initiator = usal_initiator_id(usalp);
314 		fprintf(f, "scsibus%d:\n", bus);
315 
316 		for (tgt = 0; tgt < 16; tgt++) {
317 			n = bus*100 + tgt;
318 
319 			usal_settarget(usalp, bus, tgt, lun);
320 			have_tgt = unit_ready(usalp) || usalp->scmd->error != SCG_FATAL;
321 
322 			if (!have_tgt && tgt > 7) {
323 				if (usalp->scmd->ux_errno == EINVAL)
324 					break;
325 				continue;
326 			}
327 
328 #ifdef	FMT
329 			if (print_disknames(bus, tgt, -1) < 8)
330 				fprintf(f, "\t");
331 			else
332 				fprintf(f, " ");
333 #else
334 			fprintf(f, "\t");
335 #endif
336 			if (fprintf(f, "%d,%d,%d", bus, tgt, lun) < 8)
337 				fprintf(f, "\t");
338 			else
339 				fprintf(f, " ");
340 			fprintf(f, "%3d) ", n);
341 			if (tgt == initiator) {
342 				fprintf(f, "HOST ADAPTOR\n");
343 				continue;
344 			}
345 			if (!have_tgt) {
346 				/*
347 				 * Hack: fd -> -2 means no access
348 				 */
349 				fprintf(f, "%c\n", usalp->fd == -2 ? '?':'*');
350 				continue;
351 			}
352 			amt++;
353 			if (low < 0)
354 				low = n;
355 			high = n;
356 
357 			getdev(usalp, FALSE);
358 			print_product(f, usalp->inq);
359 		}
360 	}
361 	usalp->silent--;
362 
363 	if (low < 0) {
364 		errmsgno(EX_BAD, "No target found.\n");
365 		return (0);
366 	}
367 	n = -1;
368 #ifdef	FMT
369 	getint("Select target", &n, low, high);
370 	bus = n/100;
371 	tgt = n%100;
372 	usal_settarget(usalp, bus, tgt, lun);
373 	return (select_unit(usalp));
374 
375 	usal_settarget(usalp, cscsibus, ctarget, clun);
376 #endif
377 	return (amt);
378 }
379 
380