mt.c revision 41925
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.17 1998/12/18 02:02:20 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#endif /* defined(__FreeBSD__) */
122	{ NULL }
123};
124
125void printreg __P((char *, u_int, char *));
126void status __P((struct mtget *));
127void usage __P((void));
128#if defined (__FreeBSD__)
129void st_status (struct mtget *);
130int stringtodens (const char *s);
131const char *denstostring (int d);
132int denstobp(int d, int bpi);
133u_int32_t stringtocomp(const char *s);
134const char * comptostring(u_int32_t comp);
135void warn_eof __P((void));
136#endif /* defined (__FreeBSD__) */
137
138int
139main(argc, argv)
140	int argc;
141	char *argv[];
142{
143	register struct commands *comp;
144	struct mtget mt_status;
145	struct mtop mt_com;
146	int ch, len, mtfd;
147	char *p, *tape;
148
149	if ((tape = getenv("TAPE")) == NULL)
150		tape = DEFTAPE;
151
152	while ((ch = getopt(argc, argv, "f:t:")) != -1)
153		switch(ch) {
154		case 'f':
155		case 't':
156			tape = optarg;
157			break;
158		case '?':
159		default:
160			usage();
161		}
162	argc -= optind;
163	argv += optind;
164
165	if (argc < 1 || argc > 2)
166		usage();
167
168	len = strlen(p = *argv++);
169	for (comp = com;; comp++) {
170		if (comp->c_name == NULL)
171			errx(1, "%s: unknown command", p);
172		if (strncmp(p, comp->c_name, len) == 0)
173			break;
174	}
175#if defined(__FreeBSD__)
176	if((comp->c_flags & NEED_2ARGS) && argc != 2)
177		usage();
178	if(comp->c_flags & DISABLE_THIS) {
179		warn_eof();
180	}
181#endif /* defined(__FreeBSD__) */
182	if ((mtfd = open(tape, comp->c_ronly ? O_RDONLY : O_RDWR)) < 0)
183		err(1, "%s", tape);
184	if (comp->c_code != MTNOP) {
185		mt_com.mt_op = comp->c_code;
186		if (*argv) {
187#if defined (__FreeBSD__)
188			if (!isdigit(**argv) &&
189			    (comp->c_flags & IS_DENSITY)) {
190				const char *dcanon;
191				mt_com.mt_count = stringtodens(*argv);
192				if (mt_com.mt_count == 0)
193					errx(1, "%s: unknown density", *argv);
194				dcanon = denstostring(mt_com.mt_count);
195				if (strcmp(dcanon, *argv) != 0)
196					printf(
197					"Using \"%s\" as an alias for %s\n",
198					       *argv, dcanon);
199				p = "";
200			} else if (!isdigit(**argv) &&
201				   (comp->c_flags & IS_COMP)) {
202
203				mt_com.mt_count = stringtocomp(*argv);
204				if ((u_int32_t)mt_com.mt_count == 0xf0f0f0f0)
205					errx(1, "%s: unknown compression",
206					     *argv);
207				p = "";
208			} else
209				/* allow for hex numbers; useful for density */
210				mt_com.mt_count = strtol(*argv, &p, 0);
211#else
212			mt_com.mt_count = strtol(*argv, &p, 10);
213#endif /* defined(__FreeBSD__) */
214			if ((mt_com.mt_count <=
215#if defined (__FreeBSD__)
216			    ((comp->c_flags & ZERO_ALLOWED)? -1: 0)
217			    && ((comp->c_flags & IS_COMP) == 0)
218#else
219			    0
220#endif /* defined (__FreeBSD__) */
221			    ) || *p)
222				errx(1, "%s: illegal count", *argv);
223		}
224		else
225			mt_com.mt_count = 1;
226#if	defined(__FreeBSD__)
227		switch (comp->c_code) {
228		case MTIOCRDHPOS:
229		case MTIOCRDSPOS:
230		{
231			u_int32_t block;
232			if (ioctl(mtfd, comp->c_code, (caddr_t)&block) < 0)
233				err(2, "%s", tape);
234			printf("%s: %s block location %u\n", tape,
235			    (comp->c_code == MTIOCRDHPOS)? "hardware" :
236			    "logical", block);
237			exit(0);
238			/* NOTREACHED */
239		}
240		case MTIOCSLOCATE:
241		case MTIOCHLOCATE:
242		{
243			u_int32_t block = (u_int32_t)mt_com.mt_count;
244			if (ioctl(mtfd, comp->c_code, (caddr_t)&block) < 0)
245				err(2, "%s", tape);
246			exit(0);
247			/* NOTREACHED */
248		}
249		default:
250			break;
251		}
252#endif
253		if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
254			err(1, "%s: %s", tape, comp->c_name);
255	} else {
256		if (ioctl(mtfd, MTIOCGET, &mt_status) < 0)
257			err(1, NULL);
258		status(&mt_status);
259	}
260	exit(0);
261	/* NOTREACHED */
262}
263
264#ifdef vax
265#include <vax/mba/mtreg.h>
266#include <vax/mba/htreg.h>
267
268#include <vax/uba/utreg.h>
269#include <vax/uba/tmreg.h>
270#undef b_repcnt		/* argh */
271#include <vax/uba/tsreg.h>
272#endif
273
274#ifdef sun
275#include <sundev/tmreg.h>
276#include <sundev/arreg.h>
277#endif
278
279#ifdef tahoe
280#include <tahoe/vba/cyreg.h>
281#endif
282
283#if defined(__FreeBSD__) && defined(__i386__)
284#include <machine/wtio.h>
285#endif
286
287struct tape_desc {
288	short	t_type;		/* type of magtape device */
289	char	*t_name;	/* printing name */
290	char	*t_dsbits;	/* "drive status" register */
291	char	*t_erbits;	/* "error" register */
292} tapes[] = {
293#ifdef vax
294	{ MT_ISTS,	"ts11",		0,		TSXS0_BITS },
295	{ MT_ISHT,	"tm03",		HTDS_BITS,	HTER_BITS },
296	{ MT_ISTM,	"tm11",		0,		TMER_BITS },
297	{ MT_ISMT,	"tu78",		MTDS_BITS,	0 },
298	{ MT_ISUT,	"tu45",		UTDS_BITS,	UTER_BITS },
299#endif
300#ifdef sun
301	{ MT_ISCPC,	"TapeMaster",	TMS_BITS,	0 },
302	{ MT_ISAR,	"Archive",	ARCH_CTRL_BITS,	ARCH_BITS },
303#endif
304#ifdef tahoe
305	{ MT_ISCY,	"cipher",	CYS_BITS,	CYCW_BITS },
306#endif
307#if defined (__FreeBSD__)
308	/*
309	 * XXX This is weird.  The st driver reports the tape drive
310	 * as 0x7 (MT_ISAR - Sun/Archive compatible); the wt driver
311	 * either reports MT_ISVIPER1 for an Archive tape, or 0x11
312	 * (MT_ISMFOUR) for other tapes.
313	 * XXX for the wt driver, rely on it behaving like a "standard"
314	 * magtape driver.
315	 */
316	{ MT_ISAR,	"SCSI tape drive", 0,		0 },
317#if defined (__i386__)
318	{ MT_ISVIPER1,	"Archive Viper", WTDS_BITS, WTER_BITS },
319	{ MT_ISMFOUR,	"Wangtek",	 WTDS_BITS, WTER_BITS },
320#endif
321#endif /* defined (__FreeBSD__) */
322	{ 0 }
323};
324
325/*
326 * Interpret the status buffer returned
327 */
328void
329status(bp)
330	register struct mtget *bp;
331{
332	register struct tape_desc *mt;
333
334	for (mt = tapes;; mt++) {
335		if (mt->t_type == 0) {
336			(void)printf("%d: unknown tape drive type\n",
337			    bp->mt_type);
338			return;
339		}
340		if (mt->t_type == bp->mt_type)
341			break;
342	}
343#if defined (__FreeBSD__)
344	if(mt->t_type == MT_ISAR)
345		st_status(bp);
346	else {
347#endif /* defined (__FreeBSD__) */
348	(void)printf("%s tape drive, residual=%d\n", mt->t_name, bp->mt_resid);
349	printreg("ds", (unsigned short)bp->mt_dsreg, mt->t_dsbits);
350	printreg("\ner", (unsigned short)bp->mt_erreg, mt->t_erbits);
351	(void)putchar('\n');
352#if defined (__FreeBSD__)
353	}
354#endif /* defined (__FreeBSD__) */
355}
356
357/*
358 * Print a register a la the %b format of the kernel's printf.
359 */
360void
361printreg(s, v, bits)
362	char *s;
363	register u_int v;
364	register char *bits;
365{
366	register int i, any = 0;
367	register char c;
368
369	if (bits && *bits == 8)
370		printf("%s=%o", s, v);
371	else
372		printf("%s=%x", s, v);
373	if (!bits)
374		return;
375	bits++;
376	if (v && bits) {
377		putchar('<');
378		while ((i = *bits++)) {
379			if (v & (1 << (i-1))) {
380				if (any)
381					putchar(',');
382				any = 1;
383				for (; (c = *bits) > 32; bits++)
384					putchar(c);
385			} else
386				for (; *bits > 32; bits++)
387					;
388		}
389		putchar('>');
390	}
391}
392
393void
394usage()
395{
396	(void)fprintf(stderr, "usage: mt [-f device] command [ count ]\n");
397	exit(1);
398}
399
400#if defined (__FreeBSD__)
401
402struct densities {
403	int dens;
404	int bpmm;
405	int bpi;
406	const char *name;
407} dens[] = {
408	/*
409	 * Taken from T10 Project 997D
410	 * SCSI-3 Stream Device Commands (SSC)
411	 * Revision 11, 4-Nov-97
412	 */
413	/*Num.  bpmm    bpi     Reference     */
414	{ 0x1,	32,	800,	"X3.22-1983" },
415	{ 0x2,	63,	1600,	"X3.39-1986" },
416	{ 0x3,	246,	6250,	"X3.54-1986" },
417	{ 0x5,	315,	8000,	"X3.136-1986" },
418	{ 0x6,	126,	3200,	"X3.157-1987" },
419	{ 0x7,	252,	6400,	"X3.116-1986" },
420	{ 0x8,	315,	8000,	"X3.158-1987" },
421	{ 0x9,	491,	37871,	"X3.180" },
422	{ 0xA,	262,	6667,	"X3B5/86-199" },
423	{ 0xB,	63,	1600,	"X3.56-1986" },
424	{ 0xC,	500,	12690,	"HI-TC1" },
425	{ 0xD,	999,	25380,	"HI-TC2" },
426	{ 0xF,	394,	10000,	"QIC-120" },
427	{ 0x10,	394,	10000,	"QIC-150" },
428	{ 0x11,	630,	16000,	"QIC-320" },
429	{ 0x12,	2034,	51667,	"QIC-1350" },
430	{ 0x13,	2400,	61000,	"X3B5/88-185A" },
431	{ 0x14,	1703,	43245,	"X3.202-1991" },
432	{ 0x15,	1789,	45434,	"ECMA TC17" },
433	{ 0x16,	394,	10000,	"X3.193-1990" },
434	{ 0x17,	1673,	42500,	"X3B5/91-174" },
435	{ 0x18,	1673,	42500,	"X3B5/92-50" },
436	{ 0x1C, 1654,	42000,	"QIC-385M" },
437	{ 0x1D,	1512,	38400,	"QIC-410M" },
438	{ 0x1E, 1385,	36000,	"QIC-1000C" },
439	{ 0x1F,	2666,	67733,	"QIC-2100C" },
440	{ 0x20, 2666,	67733,	"QIC-6GB(M)" },
441	{ 0x21,	2666,	67733,	"QIC-20GB(C)" },
442	{ 0x22,	1600,	40640,	"QIC-2GB(C)" },
443	{ 0x23, 2666,	67733,	"QIC-875M" },
444	{ 0x24,	2400,	61000,	"DDS-2" },
445	{ 0x25,	3816,	97000,	"DDS-3" },
446	{ 0x26,	3816,	97000,	"DDS-4" },
447	{ 0x27,	3056,	77611,	"Mammoth" },
448	{ 0x28,	1491,	37871,	"X3.224" },
449	{ 0, 0, 0, NULL }
450};
451
452struct compression_types {
453	u_int32_t	comp_number;
454	const char 	*name;
455} comp_types[] = {
456	{ 0x00, "none" },
457	{ 0x00, "off" },
458	{ 0x10, "IDRC" },
459	{ 0x20, "DCLZ" },
460	{ 0xffffffff, "enable" },
461	{ 0xffffffff, "on" },
462	{ 0xf0f0f0f0, NULL}
463};
464
465const char *
466denstostring(int d)
467{
468	static char buf[20];
469	struct densities *sd;
470
471	for (sd = dens; sd->dens; sd++)
472		if (sd->dens == d)
473			break;
474	if (sd->dens == 0) {
475		sprintf(buf, "0x%02x", d);
476		return buf;
477	} else
478		return sd->name;
479}
480
481/*
482 * Given a specific density number, return either the bits per inch or bits
483 * per millimeter for the given density.
484 */
485int
486denstobp(int d, int bpi)
487{
488	struct densities *sd;
489
490	for (sd = dens; sd->dens; sd++)
491		if (sd->dens == d)
492			break;
493	if (sd->dens == 0)
494		return(0);
495	else {
496		if (bpi)
497			return(sd->bpi);
498		else
499			return(sd->bpmm);
500	}
501}
502
503int
504stringtodens(const char *s)
505{
506	struct densities *sd;
507	size_t l = strlen(s);
508
509	for (sd = dens; sd->dens; sd++)
510		if (strncasecmp(sd->name, s, l) == 0)
511			break;
512	return sd->dens;
513}
514
515
516const char *
517getblksiz(int bs)
518{
519	static char buf[25];
520	if (bs == 0)
521		return "variable";
522	else {
523		sprintf(buf, "%d bytes", bs);
524		return buf;
525	}
526}
527
528const char *
529comptostring(u_int32_t comp)
530{
531	static char buf[20];
532	struct compression_types *ct;
533
534	if (comp == MT_COMP_DISABLED)
535		return "disabled";
536	else if (comp == MT_COMP_UNSUPP)
537		return "unsupported";
538
539	for (ct = comp_types; ct->name; ct++)
540		if (ct->comp_number == comp)
541			break;
542
543	if (ct->comp_number == 0xf0f0f0f0) {
544		sprintf(buf, "0x%2x", comp);
545		return(buf);
546	} else
547		return(ct->name);
548}
549
550u_int32_t
551stringtocomp(const char *s)
552{
553	struct compression_types *ct;
554	size_t l = strlen(s);
555
556	for (ct = comp_types; ct->name; ct++)
557		if (strncasecmp(ct->name, s, l) == 0)
558			break;
559
560	return(ct->comp_number);
561}
562
563void
564st_status(struct mtget *bp)
565{
566	printf("Mode      Density         Blocksize      bpi      "
567	       "Compression\n"
568	       "Current:  %-12s    %-12s   %-7d  %s\n"
569	       "---------available modes---------\n"
570	       "0:        %-12s    %-12s   %-7d  %s\n"
571	       "1:        %-12s    %-12s   %-7d  %s\n"
572	       "2:        %-12s    %-12s   %-7d  %s\n"
573	       "3:        %-12s    %-12s   %-7d  %s\n",
574	       denstostring(bp->mt_density), getblksiz(bp->mt_blksiz),
575	       denstobp(bp->mt_density, TRUE), comptostring(bp->mt_comp),
576	       denstostring(bp->mt_density0), getblksiz(bp->mt_blksiz0),
577	       denstobp(bp->mt_density0, TRUE), comptostring(bp->mt_comp0),
578	       denstostring(bp->mt_density1), getblksiz(bp->mt_blksiz1),
579	       denstobp(bp->mt_density1, TRUE), comptostring(bp->mt_comp1),
580	       denstostring(bp->mt_density2), getblksiz(bp->mt_blksiz2),
581	       denstobp(bp->mt_density2, TRUE), comptostring(bp->mt_comp2),
582	       denstostring(bp->mt_density3), getblksiz(bp->mt_blksiz3),
583	       denstobp(bp->mt_density3, TRUE), comptostring(bp->mt_comp3));
584}
585
586void
587warn_eof(void)
588{
589	fprintf(stderr,
590		"The \"eof\" command has been disabled.\n"
591		"Use \"weof\" if you really want to write end-of-file marks,\n"
592		"or \"eom\" if you rather want to skip to the end of "
593		"recorded medium.\n");
594	exit(1);
595}
596
597#endif /* defined (__FreeBSD__) */
598