File: scsiopen.c

package info (click to toggle)
cdrkit 9:1.1.11-3
  • links: PTS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 7,464 kB
  • sloc: ansic: 107,182; perl: 968; sh: 481; makefile: 229; sed: 4
file content (464 lines) | stat: -rw-r--r-- 11,387 bytes parent folder | download | duplicates (4)
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*
 * 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.
 *
 */

/* @(#)scsiopen.c	1.95 04/01/14 Copyright 1995,2000 J. Schilling */
/*
 *	SCSI command functions for cdrecord
 *
 *	Copyright (c) 1995,2000 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.
 */

/*
 * NOTICE:	The Philips CDD 521 has several firmware bugs.
 *		One of them is not to respond to a SCSI selection
 *		within 200ms if the general load on the
 *		SCSI bus is high. To deal with this problem
 *		most of the SCSI commands are send with the
 *		SCG_CMD_RETRY flag enabled.
 *
 *		Note that the only legal place to assign
 *		values to usal_scsibus() usal_target() and usal_lun()
 *		is usal_settarget().
 */
#include <mconfig.h>

#include <stdio.h>
#include <standard.h>
#include <stdxlib.h>
#include <unixstd.h>
#include <fctldefs.h>
#include <errno.h>
#include <strdefs.h>
#include <timedefs.h>

#include <utypes.h>
#include <btorder.h>
#include <schily.h>

#include <usal/usalcmd.h>
#include <usal/scsidefs.h>
#include <usal/scsireg.h>
#include <usal/scsitransp.h>

#if    defined(linux) || defined(__linux) || defined(__linux__)
extern BOOL check_linux_26();
#endif

#define	strbeg(s1, s2)	(strstr((s2), (s1)) == (s2))

extern	int	lverbose;

SCSI	*usal_open(char *scsidev, char *errs, int slen, int debug, int be_verbose);
int	usal_help(FILE *f);
static	int	usal_scandev(char *devp, char *errs, int slen, int *busp, 
									int *tgtp, int *lunp);
int	usal_close(SCSI * usalp);

void	usal_settimeout(SCSI * usalp, int timeout);

SCSI	*usal_smalloc(void);
void	usal_sfree(SCSI *usalp);

/*
 * Open a SCSI device.
 *
 * Possible syntax is:
 *
 * Preferred:
 *	dev=target,lun / dev=scsibus,target,lun
 *
 * Needed on some systems:
 *	dev=devicename:target,lun / dev=devicename:scsibus,target,lun
 *
 * On systems that don't support SCSI Bus scanning this syntax helps:
 *	dev=devicename:@ / dev=devicename:@,lun
 * or	dev=devicename (undocumented)
 *
 * NOTE: As the 'lun' is part of the SCSI command descriptor block, it
 *	 must always be known. If the OS cannot map it, it must be
 *	 specified on command line.
 */
SCSI *
usal_open(char *scsidev, char *errs, int slen, int debug, int be_verbose)
{
	char	devname[256];
	char	*devp = NULL;
	char	*sdev = NULL;
	int	x1;
	int	bus = 0;
	int	tgt = 0;
	int	lun = 0;
	int	n = 0;
	SCSI	*usalp;

	if (errs)
		errs[0] = '\0';
	usalp = usal_smalloc();
	if (usalp == NULL) {
		if (errs)
			snprintf(errs, slen, "No memory for SCSI structure");
		return ((SCSI *)0);
	}
	usalp->debug = debug;
	usalp->overbose = be_verbose;
	devname[0] = '\0';
	if (scsidev != NULL && scsidev[0] != '\0') {
		sdev = scsidev;
		if ((strncmp(scsidev, "HELP", 4) == 0) ||
		    (strncmp(scsidev, "help", 4) == 0)) {

			return ((SCSI *)0);
		}
		if (strncmp(scsidev, "REMOTE", 6) == 0) {
			/*
			 * REMOTE:user@host:scsidev or
			 * REMOTE(transp):user@host:scsidev
			 * e.g.: REMOTE(/usr/bin/ssh):user@host:scsidev
			 *
			 * We must send the complete device spec to the remote
			 * site to allow parsing on both sites.
			 */
			strncpy(devname, scsidev, sizeof (devname)-1);
			devname[sizeof (devname)-1] = '\0';
			if (sdev[6] == '(' || sdev[6] == ':')
				sdev = strchr(sdev, ':');
			else
				sdev = NULL;

			if (sdev == NULL) {
				/*
				 * This seems to be an illegal remote dev spec.
				 * Give it a chance with a standard parsing.
				 */
				sdev = scsidev;
				devname[0] = '\0';
			} else {
				/*
				 * Now try to go past user@host spec.
				 */
				if (sdev)
					sdev = strchr(&sdev[1], ':');
				if (sdev)
					sdev++;	/* Device name follows ... */
				else
					goto nulldevice;
			}
		}
		if ((devp = strchr(sdev, ':')) == NULL) {
			if (strchr(sdev, ',') == NULL) {
				/* Notation form: 'devname' (undocumented)  */
				/* Forward complete name to usal__open()	    */
				/* Fetch bus/tgt/lun values from OS	    */
				/* We may come here too with 'USCSI'	    */
				n = -1;
				lun  = -2;	/* Lun must be known	    */
				if (devname[0] == '\0') {
					strncpy(devname, scsidev,
							sizeof (devname)-1);
					devname[sizeof (devname)-1] = '\0';
				}
			} else {
				/* Basic notation form: 'bus,tgt,lun'	    */
				devp = sdev;
			}
		} else {
			/* Notation form: 'devname:bus,tgt,lun'/'devname:@' */
			/* We may come here too with 'USCSI:'		    */
			if (devname[0] == '\0') {
				/*
				 * Copy over the part before the ':'
				 */
				x1 = devp - scsidev;
				if (x1 >= (int)sizeof (devname))
					x1 = sizeof (devname)-1;
				strncpy(devname, scsidev, x1);
				devname[x1] = '\0';
			}
			devp++;
			/* Check for a notation in the form 'devname:@'	    */
			if (devp[0] == '@') {
				if (devp[1] == '\0') {
					lun = -2;
				} else if (devp[1] == ',') {
					if (*astoi(&devp[2], &lun) != '\0') {
						errno = EINVAL;
						if (errs)
							snprintf(errs, slen,
								"Invalid lun specifier '%s'",
										&devp[2]);
						return ((SCSI *)0);
					}
				}
				n = -1;
				/*
				 * Got device:@ or device:@,lun
				 * Make sure not to call usal_scandev()
				 */
				devp = NULL;
			} else if (devp[0] == '\0') {
				/*
				 * Got USCSI: or ATAPI:
				 * Make sure not to call usal_scandev()
				 */
				devp = NULL;
			} else if (strchr(sdev, ',') == NULL) {
				/* We may come here with 'ATAPI:/dev/hdc'   */
				strncpy(devname, scsidev,
						sizeof (devname)-1);
				devname[sizeof (devname)-1] = '\0';
				n = -1;
				lun  = -2;	/* Lun must be known	    */
				/*
				 * Make sure not to call usal_scandev()
				 */
				devp = NULL;
			}
		}
	}
nulldevice:

/*fprintf(stderr, "10 scsidev '%s' sdev '%s' devp '%s' b: %d t: %d l: %d\n", scsidev, sdev, devp, bus, tgt, lun);*/

	if (devp != NULL) {
		n = usal_scandev(devp, errs, slen, &bus, &tgt, &lun);
		if (n < 0) {
			errno = EINVAL;
			return ((SCSI *)0);
		}
	}
	if (n >= 1 && n <= 3) {	/* Got bus,target,lun or target,lun or tgt*/
		usal_settarget(usalp, bus, tgt, lun);
	} else if (n == -1) {	/* Got device:@, fetch bus/lun from OS	*/
		usal_settarget(usalp, -2, -2, lun);
	} else if (devp != NULL) {
		/*
		 * XXX May this happen after we allow tgt to repesent tgt,0 ?
		 */
		fprintf(stderr, "WARNING: device not valid, trying to use default target...\n");
		usal_settarget(usalp, 0, 6, 0);
	}
	if (be_verbose && scsidev != NULL) {
		fprintf(stderr, "scsidev: '%s'\n", scsidev);
		if (devname[0] != '\0')
			fprintf(stderr, "devname: '%s'\n", devname);
		fprintf(stderr, "scsibus: %d target: %d lun: %d\n",
					usal_scsibus(usalp), usal_target(usalp), usal_lun(usalp));
	}
	if (debug > 0) {
		fprintf(stderr, "usal__open(%s) %d,%d,%d\n",
			devname,
			usal_scsibus(usalp), usal_target(usalp), usal_lun(usalp));
	}
	if (usal__open(usalp, devname) <= 0) {
		if (errs && usalp->errstr)
			snprintf(errs, slen, "%s", usalp->errstr);
		usal_sfree(usalp);
		return ((SCSI *)0);
	}
	return (usalp);
}

int
usal_help(FILE *f)
{
	SCSI	*usalp;

	usalp = usal_smalloc();
	if (usalp != NULL) {
extern	usal_ops_t usal_std_ops;

		usalp->ops = &usal_std_ops;

		printf("Supported SCSI transports for this platform:\n");
		SCGO_HELP(usalp, f);
		usal_remote()->usalo_help(usalp, f);
		usal_sfree(usalp);
	}
	return (0);
}

/*
 * Convert target,lun or scsibus,target,lun syntax.
 * Check for bad syntax and invalid values.
 * This is definitely better than using scanf() as it checks for syntax errors.
 */
static int
usal_scandev(char *devp, char *errs, int slen, int *busp, int *tgtp, int *lunp)
{
	int	x1, x2, x3;
	int	n = 0;
	char	*p = devp;

	x1 = x2 = x3 = 0;
	*busp = *tgtp = *lunp = 0;

	if (*p != '\0') {
		p = astoi(p, &x1);
		if (*p == ',') {
			p++;
			n++;
		} else {
			if (errs)
				snprintf(errs, slen, "Invalid bus or target specifier in '%s'", devp);
			return (-1);
		}
	}
	if (*p != '\0') {
		p = astoi(p, &x2);
		if (*p == ',' || *p == '\0') {
			if (*p != '\0')
				p++;
			n++;
		} else {
			if (errs)
				snprintf(errs, slen, "Invalid target or lun specifier in '%s'", devp);
			return (-1);
		}
	}
	if (*p != '\0') {
		p = astoi(p, &x3);
		if (*p == '\0') {
			n++;
		} else {
			if (errs)
				snprintf(errs, slen, "Invalid lun specifier in '%s'", devp);
			return (-1);
		}
	}
	if (n == 3) {
		*busp = x1;
		*tgtp = x2;
		*lunp = x3;
	}
	if (n == 2) {
		*tgtp = x1;
		*lunp = x2;
	}
	if (n == 1) {
		*tgtp = x1;
	}

	if (x1 < 0 || x2 < 0 || x3 < 0) {
		if (errs)
			snprintf(errs, slen, "Invalid value for bus, target or lun (%d,%d,%d)",
				*busp, *tgtp, *lunp);
		return (-1);
	}
	return (n);
}

int
usal_close(SCSI *usalp)
{
	usal__close(usalp);
	usal_sfree(usalp);
	return (0);
}

char * usal_natname(SCSI *usalp, int busno, int tgt, int tlun) {
	return usalp->ops->usalo_natname(usalp, busno, tgt, tlun);
}

int usal_fileno(SCSI *usalp, int busno, int tgt, int tlun) {
	return usalp->ops->usalo_fileno(usalp, busno, tgt, tlun);
}

void
usal_settimeout(SCSI *usalp, int timeout)
{
#ifdef	nonono
	if (timeout >= 0)
		usalp->deftimeout = timeout;
#else
	usalp->deftimeout = timeout;
#endif
}

SCSI *
usal_smalloc()
{
	SCSI	*usalp;
extern	usal_ops_t usal_dummy_ops;

	usalp = (SCSI *)malloc(sizeof (*usalp));
	if (usalp == NULL)
		return ((SCSI *)0);

	fillbytes(usalp, sizeof (*usalp), 0);
	usalp->ops	= &usal_dummy_ops;
	usal_settarget(usalp, -1, -1, -1);
	usalp->fd	= -1;
	usalp->deftimeout = 20;
	usalp->running	= FALSE;

	usalp->cmdstart = (struct timeval *)malloc(sizeof (struct timeval));
	if (usalp->cmdstart == NULL)
		goto err;
	usalp->cmdstop = (struct timeval *)malloc(sizeof (struct timeval));
	if (usalp->cmdstop == NULL)
		goto err;
	usalp->scmd = (struct usal_cmd *)malloc(sizeof (struct usal_cmd));
	if (usalp->scmd == NULL)
		goto err;
	usalp->errstr = malloc(SCSI_ERRSTR_SIZE);
	if (usalp->errstr == NULL)
		goto err;
	usalp->errptr = usalp->errbeg = usalp->errstr;
	usalp->errstr[0] = '\0';
	usalp->errfile = (void *)stderr;
	usalp->inq = (struct scsi_inquiry *)malloc(sizeof (struct scsi_inquiry));
	if (usalp->inq == NULL)
		goto err;
	usalp->cap = (struct scsi_capacity *)malloc(sizeof (struct scsi_capacity));
	if (usalp->cap == NULL)
		goto err;

	return (usalp);
err:
	usal_sfree(usalp);
	return ((SCSI *)0);
}

void
usal_sfree(SCSI *usalp)
{
	if (usalp->cmdstart)
		free(usalp->cmdstart);
	if (usalp->cmdstop)
		free(usalp->cmdstop);
	if (usalp->scmd)
		free(usalp->scmd);
	if (usalp->inq)
		free(usalp->inq);
	if (usalp->cap)
		free(usalp->cap);
	if (usalp->local)
		free(usalp->local);
	usal_freebuf(usalp);
	if (usalp->errstr)
		free(usalp->errstr);
	free(usalp);
}