mt.c revision 41945
1/*
2 * Copyright (c) 1980, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static const char copyright[] =
36"@(#) Copyright (c) 1980, 1993\n\
37	The Regents of the University of California.  All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41#if 0
42static char sccsid[] = "@(#)mt.c	8.2 (Berkeley) 5/4/95";
43#endif
44static const char rcsid[] =
45	"$Id: mt.c,v 1.18 1998/12/18 18:16:35 mjacob Exp $";
46#endif /* not lint */
47
48/*
49 * mt --
50 *   magnetic tape manipulation program
51 */
52#include <sys/types.h>
53#include <sys/ioctl.h>
54#include <sys/mtio.h>
55
56#include <ctype.h>
57#include <err.h>
58#include <fcntl.h>
59#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
62#include <unistd.h>
63
64/* the appropriate sections of <sys/mtio.h> are also #ifdef'd for FreeBSD */
65#if defined(__FreeBSD__)
66/* c_flags */
67#define NEED_2ARGS	0x01
68#define ZERO_ALLOWED	0x02
69#define IS_DENSITY	0x04
70#define DISABLE_THIS	0x08
71#define IS_COMP		0x10
72#endif /* defined(__FreeBSD__) */
73
74#ifndef TRUE
75#define TRUE 1
76#endif
77#ifndef FALSE
78#define FALSE 0
79#endif
80
81struct commands {
82	char *c_name;
83	int c_code;
84	int c_ronly;
85#if defined(__FreeBSD__)
86	int c_flags;
87#endif /* defined(__FreeBSD__) */
88} com[] = {
89	{ "bsf",	MTBSF,	1 },
90	{ "bsr",	MTBSR,	1 },
91#if defined(__FreeBSD__)
92	/* XXX FreeBSD considered "eof" dangerous, since it's being
93	   confused with "eom" (and is an alias for "weof" anyway) */
94	{ "eof",	MTWEOF, 0, DISABLE_THIS },
95#else
96	{ "eof",	MTWEOF, 0 },
97#endif
98	{ "fsf",	MTFSF,	1 },
99	{ "fsr",	MTFSR,	1 },
100	{ "offline",	MTOFFL,	1 },
101	{ "rewind",	MTREW,	1 },
102	{ "rewoffl",	MTOFFL,	1 },
103	{ "status",	MTNOP,	1 },
104#if defined(__FreeBSD__)
105	{ "weof",	MTWEOF,	0, ZERO_ALLOWED },
106#else
107	{ "weof",	MTWEOF,	0 },
108#endif
109#if defined(__FreeBSD__)
110	{ "erase",	MTERASE, 0, ZERO_ALLOWED},
111	{ "blocksize",	MTSETBSIZ, 0, NEED_2ARGS|ZERO_ALLOWED },
112	{ "density",	MTSETDNSTY, 0, NEED_2ARGS|ZERO_ALLOWED|IS_DENSITY },
113	{ "eom",	MTEOD, 1 },
114	{ "eod",	MTEOD, 1 },
115	{ "comp",	MTCOMP, 0, NEED_2ARGS|ZERO_ALLOWED|IS_COMP },
116	{ "retension",	MTRETENS, 1 },
117	{ "rdhpos",     MTIOCRDHPOS,  0 },
118	{ "rdspos",     MTIOCRDSPOS,  0 },
119	{ "sethpos",    MTIOCHLOCATE, 0, NEED_2ARGS|ZERO_ALLOWED },
120	{ "setspos",    MTIOCSLOCATE, 0, NEED_2ARGS|ZERO_ALLOWED },
121	{ "errstat",	MTIOCERRSTAT, 0 },
122#endif /* defined(__FreeBSD__) */
123	{ NULL }
124};
125
126void printreg __P((char *, u_int, char *));
127void status __P((struct mtget *));
128void usage __P((void));
129#if defined (__FreeBSD__)
130void st_status (struct mtget *);
131int stringtodens (const char *s);
132const char *denstostring (int d);
133int denstobp(int d, int bpi);
134u_int32_t stringtocomp(const char *s);
135const char * comptostring(u_int32_t comp);
136void warn_eof __P((void));
137#endif /* defined (__FreeBSD__) */
138
139int
140main(argc, argv)
141	int argc;
142	char *argv[];
143{
144	register struct commands *comp;
145	struct mtget mt_status;
146	struct mtop mt_com;
147	int ch, len, mtfd;
148	char *p, *tape;
149
150	if ((tape = getenv("TAPE")) == NULL)
151		tape = DEFTAPE;
152
153	while ((ch = getopt(argc, argv, "f:t:")) != -1)
154		switch(ch) {
155		case 'f':
156		case 't':
157			tape = optarg;
158			break;
159		case '?':
160		default:
161			usage();
162		}
163	argc -= optind;
164	argv += optind;
165
166	if (argc < 1 || argc > 2)
167		usage();
168
169	len = strlen(p = *argv++);
170	for (comp = com;; comp++) {
171		if (comp->c_name == NULL)
172			errx(1, "%s: unknown command", p);
173		if (strncmp(p, comp->c_name, len) == 0)
174			break;
175	}
176#if defined(__FreeBSD__)
177	if((comp->c_flags & NEED_2ARGS) && argc != 2)
178		usage();
179	if(comp->c_flags & DISABLE_THIS) {
180		warn_eof();
181	}
182#endif /* defined(__FreeBSD__) */
183	if ((mtfd = open(tape, comp->c_ronly ? O_RDONLY : O_RDWR)) < 0)
184		err(1, "%s", tape);
185	if (comp->c_code != MTNOP) {
186		mt_com.mt_op = comp->c_code;
187		if (*argv) {
188#if defined (__FreeBSD__)
189			if (!isdigit(**argv) &&
190			    (comp->c_flags & IS_DENSITY)) {
191				const char *dcanon;
192				mt_com.mt_count = stringtodens(*argv);
193				if (mt_com.mt_count == 0)
194					errx(1, "%s: unknown density", *argv);
195				dcanon = denstostring(mt_com.mt_count);
196				if (strcmp(dcanon, *argv) != 0)
197					printf(
198					"Using \"%s\" as an alias for %s\n",
199					       *argv, dcanon);
200				p = "";
201			} else if (!isdigit(**argv) &&
202				   (comp->c_flags & IS_COMP)) {
203
204				mt_com.mt_count = stringtocomp(*argv);
205				if ((u_int32_t)mt_com.mt_count == 0xf0f0f0f0)
206					errx(1, "%s: unknown compression",
207					     *argv);
208				p = "";
209			} else
210				/* allow for hex numbers; useful for density */
211				mt_com.mt_count = strtol(*argv, &p, 0);
212#else
213			mt_com.mt_count = strtol(*argv, &p, 10);
214#endif /* defined(__FreeBSD__) */
215			if ((mt_com.mt_count <=
216#if defined (__FreeBSD__)
217			    ((comp->c_flags & ZERO_ALLOWED)? -1: 0)
218			    && ((comp->c_flags & IS_COMP) == 0)
219#else
220			    0
221#endif /* defined (__FreeBSD__) */
222			    ) || *p)
223				errx(1, "%s: illegal count", *argv);
224		}
225		else
226			mt_com.mt_count = 1;
227#if	defined(__FreeBSD__)
228		switch (comp->c_code) {
229		case MTIOCERRSTAT:
230		{
231			int i;
232			union mterrstat umn;
233			struct scsi_tape_errors *s = &umn.scsi_errstat;
234
235			if (ioctl(mtfd, comp->c_code, (caddr_t)&umn) < 0)
236				err(2, "%s", tape);
237			(void)printf("Last I/O Residual: %u\n", s->io_resid);
238			(void)printf("   Last I/O Sense:\n\n\t");
239			for (i = 0; i < sizeof (s->io_sense); i++) {
240				(void)printf(" %02X", s->io_sense[i]);
241				if (((i + 1) & 0xf) == 0) {
242					(void)printf("\n\t");
243				}
244			}
245			(void)printf("\n");
246			(void)printf("Last Control Residual: %u\n",
247			    s->ctl_resid);
248			(void)printf("   Last Control Sense:\n\n\t");
249			for (i = 0; i < sizeof (s->ctl_sense); i++) {
250				(void)printf(" %02X", s->ctl_sense[i]);
251				if (((i + 1) & 0xf) == 0) {
252					(void)printf("\n\t");
253				}
254			}
255			(void)printf("\n\n");
256			exit(0);
257			/* NOTREACHED */
258		}
259		case MTIOCRDHPOS:
260		case MTIOCRDSPOS:
261		{
262			u_int32_t block;
263			if (ioctl(mtfd, comp->c_code, (caddr_t)&block) < 0)
264				err(2, "%s", tape);
265			(void)printf("%s: %s block location %u\n", tape,
266			    (comp->c_code == MTIOCRDHPOS)? "hardware" :
267			    "logical", block);
268			exit(0);
269			/* NOTREACHED */
270		}
271		case MTIOCSLOCATE:
272		case MTIOCHLOCATE:
273		{
274			u_int32_t block = (u_int32_t)mt_com.mt_count;
275			if (ioctl(mtfd, comp->c_code, (caddr_t)&block) < 0)
276				err(2, "%s", tape);
277			exit(0);
278			/* NOTREACHED */
279		}
280		default:
281			break;
282		}
283#endif
284		if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
285			err(1, "%s: %s", tape, comp->c_name);
286	} else {
287		if (ioctl(mtfd, MTIOCGET, &mt_status) < 0)
288			err(1, NULL);
289		status(&mt_status);
290	}
291	exit(0);
292	/* NOTREACHED */
293}
294
295#ifdef vax
296#include <vax/mba/mtreg.h>
297#include <vax/mba/htreg.h>
298
299#include <vax/uba/utreg.h>
300#include <vax/uba/tmreg.h>
301#undef b_repcnt		/* argh */
302#include <vax/uba/tsreg.h>
303#endif
304
305#ifdef sun
306#include <sundev/tmreg.h>
307#include <sundev/arreg.h>
308#endif
309
310#ifdef tahoe
311#include <tahoe/vba/cyreg.h>
312#endif
313
314#if defined(__FreeBSD__) && defined(__i386__)
315#include <machine/wtio.h>
316#endif
317
318struct tape_desc {
319	short	t_type;		/* type of magtape device */
320	char	*t_name;	/* printing name */
321	char	*t_dsbits;	/* "drive status" register */
322	char	*t_erbits;	/* "error" register */
323} tapes[] = {
324#ifdef vax
325	{ MT_ISTS,	"ts11",		0,		TSXS0_BITS },
326	{ MT_ISHT,	"tm03",		HTDS_BITS,	HTER_BITS },
327	{ MT_ISTM,	"tm11",		0,		TMER_BITS },
328	{ MT_ISMT,	"tu78",		MTDS_BITS,	0 },
329	{ MT_ISUT,	"tu45",		UTDS_BITS,	UTER_BITS },
330#endif
331#ifdef sun
332	{ MT_ISCPC,	"TapeMaster",	TMS_BITS,	0 },
333	{ MT_ISAR,	"Archive",	ARCH_CTRL_BITS,	ARCH_BITS },
334#endif
335#ifdef tahoe
336	{ MT_ISCY,	"cipher",	CYS_BITS,	CYCW_BITS },
337#endif
338#if defined (__FreeBSD__)
339	/*
340	 * XXX This is weird.  The st driver reports the tape drive
341	 * as 0x7 (MT_ISAR - Sun/Archive compatible); the wt driver
342	 * either reports MT_ISVIPER1 for an Archive tape, or 0x11
343	 * (MT_ISMFOUR) for other tapes.
344	 * XXX for the wt driver, rely on it behaving like a "standard"
345	 * magtape driver.
346	 */
347	{ MT_ISAR,	"SCSI tape drive", 0,		0 },
348#if defined (__i386__)
349	{ MT_ISVIPER1,	"Archive Viper", WTDS_BITS, WTER_BITS },
350	{ MT_ISMFOUR,	"Wangtek",	 WTDS_BITS, WTER_BITS },
351#endif
352#endif /* defined (__FreeBSD__) */
353	{ 0 }
354};
355
356/*
357 * Interpret the status buffer returned
358 */
359void
360status(bp)
361	register struct mtget *bp;
362{
363	register struct tape_desc *mt;
364
365	for (mt = tapes;; mt++) {
366		if (mt->t_type == 0) {
367			(void)printf("%d: unknown tape drive type\n",
368			    bp->mt_type);
369			return;
370		}
371		if (mt->t_type == bp->mt_type)
372			break;
373	}
374#if defined (__FreeBSD__)
375	if(mt->t_type == MT_ISAR)
376		st_status(bp);
377	else {
378#endif /* defined (__FreeBSD__) */
379	(void)printf("%s tape drive, residual=%d\n", mt->t_name, bp->mt_resid);
380	printreg("ds", (unsigned short)bp->mt_dsreg, mt->t_dsbits);
381	printreg("\ner", (unsigned short)bp->mt_erreg, mt->t_erbits);
382	(void)putchar('\n');
383#if defined (__FreeBSD__)
384	}
385#endif /* defined (__FreeBSD__) */
386}
387
388/*
389 * Print a register a la the %b format of the kernel's printf.
390 */
391void
392printreg(s, v, bits)
393	char *s;
394	register u_int v;
395	register char *bits;
396{
397	register int i, any = 0;
398	register char c;
399
400	if (bits && *bits == 8)
401		printf("%s=%o", s, v);
402	else
403		printf("%s=%x", s, v);
404	if (!bits)
405		return;
406	bits++;
407	if (v && bits) {
408		putchar('<');
409		while ((i = *bits++)) {
410			if (v & (1 << (i-1))) {
411				if (any)
412					putchar(',');
413				any = 1;
414				for (; (c = *bits) > 32; bits++)
415					putchar(c);
416			} else
417				for (; *bits > 32; bits++)
418					;
419		}
420		putchar('>');
421	}
422}
423
424void
425usage()
426{
427	(void)fprintf(stderr, "usage: mt [-f device] command [ count ]\n");
428	exit(1);
429}
430
431#if defined (__FreeBSD__)
432
433struct densities {
434	int dens;
435	int bpmm;
436	int bpi;
437	const char *name;
438} dens[] = {
439	/*
440	 * Taken from T10 Project 997D
441	 * SCSI-3 Stream Device Commands (SSC)
442	 * Revision 11, 4-Nov-97
443	 */
444	/*Num.  bpmm    bpi     Reference     */
445	{ 0x1,	32,	800,	"X3.22-1983" },
446	{ 0x2,	63,	1600,	"X3.39-1986" },
447	{ 0x3,	246,	6250,	"X3.54-1986" },
448	{ 0x5,	315,	8000,	"X3.136-1986" },
449	{ 0x6,	126,	3200,	"X3.157-1987" },
450	{ 0x7,	252,	6400,	"X3.116-1986" },
451	{ 0x8,	315,	8000,	"X3.158-1987" },
452	{ 0x9,	491,	37871,	"X3.180" },
453	{ 0xA,	262,	6667,	"X3B5/86-199" },
454	{ 0xB,	63,	1600,	"X3.56-1986" },
455	{ 0xC,	500,	12690,	"HI-TC1" },
456	{ 0xD,	999,	25380,	"HI-TC2" },
457	{ 0xF,	394,	10000,	"QIC-120" },
458	{ 0x10,	394,	10000,	"QIC-150" },
459	{ 0x11,	630,	16000,	"QIC-320" },
460	{ 0x12,	2034,	51667,	"QIC-1350" },
461	{ 0x13,	2400,	61000,	"X3B5/88-185A" },
462	{ 0x14,	1703,	43245,	"X3.202-1991" },
463	{ 0x15,	1789,	45434,	"ECMA TC17" },
464	{ 0x16,	394,	10000,	"X3.193-1990" },
465	{ 0x17,	1673,	42500,	"X3B5/91-174" },
466	{ 0x18,	1673,	42500,	"X3B5/92-50" },
467	{ 0x1C, 1654,	42000,	"QIC-385M" },
468	{ 0x1D,	1512,	38400,	"QIC-410M" },
469	{ 0x1E, 1385,	36000,	"QIC-1000C" },
470	{ 0x1F,	2666,	67733,	"QIC-2100C" },
471	{ 0x20, 2666,	67733,	"QIC-6GB(M)" },
472	{ 0x21,	2666,	67733,	"QIC-20GB(C)" },
473	{ 0x22,	1600,	40640,	"QIC-2GB(C)" },
474	{ 0x23, 2666,	67733,	"QIC-875M" },
475	{ 0x24,	2400,	61000,	"DDS-2" },
476	{ 0x25,	3816,	97000,	"DDS-3" },
477	{ 0x26,	3816,	97000,	"DDS-4" },
478	{ 0x27,	3056,	77611,	"Mammoth" },
479	{ 0x28,	1491,	37871,	"X3.224" },
480	{ 0, 0, 0, NULL }
481};
482
483struct compression_types {
484	u_int32_t	comp_number;
485	const char 	*name;
486} comp_types[] = {
487	{ 0x00, "none" },
488	{ 0x00, "off" },
489	{ 0x10, "IDRC" },
490	{ 0x20, "DCLZ" },
491	{ 0xffffffff, "enable" },
492	{ 0xffffffff, "on" },
493	{ 0xf0f0f0f0, NULL}
494};
495
496const char *
497denstostring(int d)
498{
499	static char buf[20];
500	struct densities *sd;
501
502	for (sd = dens; sd->dens; sd++)
503		if (sd->dens == d)
504			break;
505	if (sd->dens == 0) {
506		sprintf(buf, "0x%02x", d);
507		return buf;
508	} else
509		return sd->name;
510}
511
512/*
513 * Given a specific density number, return either the bits per inch or bits
514 * per millimeter for the given density.
515 */
516int
517denstobp(int d, int bpi)
518{
519	struct densities *sd;
520
521	for (sd = dens; sd->dens; sd++)
522		if (sd->dens == d)
523			break;
524	if (sd->dens == 0)
525		return(0);
526	else {
527		if (bpi)
528			return(sd->bpi);
529		else
530			return(sd->bpmm);
531	}
532}
533
534int
535stringtodens(const char *s)
536{
537	struct densities *sd;
538	size_t l = strlen(s);
539
540	for (sd = dens; sd->dens; sd++)
541		if (strncasecmp(sd->name, s, l) == 0)
542			break;
543	return sd->dens;
544}
545
546
547const char *
548getblksiz(int bs)
549{
550	static char buf[25];
551	if (bs == 0)
552		return "variable";
553	else {
554		sprintf(buf, "%d bytes", bs);
555		return buf;
556	}
557}
558
559const char *
560comptostring(u_int32_t comp)
561{
562	static char buf[20];
563	struct compression_types *ct;
564
565	if (comp == MT_COMP_DISABLED)
566		return "disabled";
567	else if (comp == MT_COMP_UNSUPP)
568		return "unsupported";
569
570	for (ct = comp_types; ct->name; ct++)
571		if (ct->comp_number == comp)
572			break;
573
574	if (ct->comp_number == 0xf0f0f0f0) {
575		sprintf(buf, "0x%2x", comp);
576		return(buf);
577	} else
578		return(ct->name);
579}
580
581u_int32_t
582stringtocomp(const char *s)
583{
584	struct compression_types *ct;
585	size_t l = strlen(s);
586
587	for (ct = comp_types; ct->name; ct++)
588		if (strncasecmp(ct->name, s, l) == 0)
589			break;
590
591	return(ct->comp_number);
592}
593
594void
595st_status(struct mtget *bp)
596{
597	printf("Mode      Density         Blocksize      bpi      "
598	       "Compression\n"
599	       "Current:  %-12s    %-12s   %-7d  %s\n"
600	       "---------available modes---------\n"
601	       "0:        %-12s    %-12s   %-7d  %s\n"
602	       "1:        %-12s    %-12s   %-7d  %s\n"
603	       "2:        %-12s    %-12s   %-7d  %s\n"
604	       "3:        %-12s    %-12s   %-7d  %s\n",
605	       denstostring(bp->mt_density), getblksiz(bp->mt_blksiz),
606	       denstobp(bp->mt_density, TRUE), comptostring(bp->mt_comp),
607	       denstostring(bp->mt_density0), getblksiz(bp->mt_blksiz0),
608	       denstobp(bp->mt_density0, TRUE), comptostring(bp->mt_comp0),
609	       denstostring(bp->mt_density1), getblksiz(bp->mt_blksiz1),
610	       denstobp(bp->mt_density1, TRUE), comptostring(bp->mt_comp1),
611	       denstostring(bp->mt_density2), getblksiz(bp->mt_blksiz2),
612	       denstobp(bp->mt_density2, TRUE), comptostring(bp->mt_comp2),
613	       denstostring(bp->mt_density3), getblksiz(bp->mt_blksiz3),
614	       denstobp(bp->mt_density3, TRUE), comptostring(bp->mt_comp3));
615}
616
617void
618warn_eof(void)
619{
620	fprintf(stderr,
621		"The \"eof\" command has been disabled.\n"
622		"Use \"weof\" if you really want to write end-of-file marks,\n"
623		"or \"eom\" if you rather want to skip to the end of "
624		"recorded medium.\n");
625	exit(1);
626}
627
628#endif /* defined (__FreeBSD__) */
629