2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
33 * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)mt.c 8.2 (Berkeley) 5/4/95
35 * $FreeBSD: src/usr.bin/mt/mt.c,v 1.26.2.3 2002/11/08 11:35:57 joerg Exp $
40 * magnetic tape manipulation program
42 #include <sys/types.h>
43 #include <sys/ioctl.h>
54 /* the appropriate sections of <sys/mtio.h> are also #ifdef'd for FreeBSD */
55 #if defined(__DragonFly__)
57 #define NEED_2ARGS 0x01
58 #define ZERO_ALLOWED 0x02
59 #define IS_DENSITY 0x04
60 #define DISABLE_THIS 0x08
62 #endif /* defined(__DragonFly__) */
75 #if defined(__DragonFly__)
77 #endif /* defined(__DragonFly__) */
81 #if defined(__DragonFly__)
82 /* XXX FreeBSD considered "eof" dangerous, since it's being
83 confused with "eom" (and is an alias for "weof" anyway) */
84 { "eof", MTWEOF, 0, DISABLE_THIS },
90 { "offline", MTOFFL, 1 },
91 { "rewind", MTREW, 1 },
92 { "rewoffl", MTOFFL, 1 },
93 { "status", MTNOP, 1 },
94 #if defined(__DragonFly__)
95 { "weof", MTWEOF, 0, ZERO_ALLOWED },
97 { "weof", MTWEOF, 0 },
99 #if defined(__DragonFly__)
100 { "erase", MTERASE, 0, ZERO_ALLOWED},
101 { "blocksize", MTSETBSIZ, 0, NEED_2ARGS|ZERO_ALLOWED },
102 { "density", MTSETDNSTY, 0, NEED_2ARGS|ZERO_ALLOWED|IS_DENSITY },
109 { "comp", MTCOMP, 0, NEED_2ARGS|ZERO_ALLOWED|IS_COMP },
110 { "retension", MTRETENS, 1 },
111 { "rdhpos", MTIOCRDHPOS, 0 },
112 { "rdspos", MTIOCRDSPOS, 0 },
113 { "sethpos", MTIOCHLOCATE, 0, NEED_2ARGS|ZERO_ALLOWED },
114 { "setspos", MTIOCSLOCATE, 0, NEED_2ARGS|ZERO_ALLOWED },
115 { "errstat", MTIOCERRSTAT, 0 },
116 { "setmodel", MTIOCSETEOTMODEL, 0, NEED_2ARGS|ZERO_ALLOWED },
117 { "seteotmodel", MTIOCSETEOTMODEL, 0, NEED_2ARGS|ZERO_ALLOWED },
118 { "getmodel", MTIOCGETEOTMODEL },
119 { "geteotmodel", MTIOCGETEOTMODEL },
120 #endif /* defined(__DragonFly__) */
124 void printreg(char *, u_int, char *);
125 void status(struct mtget *);
127 #if defined (__DragonFly__)
128 void st_status (struct mtget *);
129 int stringtodens (const char *s);
130 const char *denstostring (int d);
131 int denstobp(int d, int bpi);
132 u_int32_t stringtocomp(const char *s);
133 const char * comptostring(u_int32_t comp);
135 #endif /* defined (__DragonFly__) */
138 main(int argc, char **argv)
140 struct commands *comp;
141 struct mtget mt_status;
146 if ((tape = getenv("TAPE")) == NULL)
149 while ((ch = getopt(argc, argv, "f:t:")) != -1)
162 if (argc < 1 || argc > 2)
165 len = strlen(p = *argv++);
166 for (comp = com;; comp++) {
167 if (comp->c_name == NULL)
168 errx(1, "%s: unknown command", p);
169 if (strncmp(p, comp->c_name, len) == 0)
172 #if defined(__DragonFly__)
173 if((comp->c_flags & NEED_2ARGS) && argc != 2)
175 if(comp->c_flags & DISABLE_THIS) {
178 #endif /* defined(__DragonFly__) */
179 if ((mtfd = open(tape, comp->c_ronly ? O_RDONLY : O_RDWR)) < 0)
181 if (comp->c_code != MTNOP) {
182 mt_com.mt_op = comp->c_code;
184 #if defined (__DragonFly__)
185 if (!isdigit(**argv) &&
186 (comp->c_flags & IS_DENSITY)) {
188 mt_com.mt_count = stringtodens(*argv);
189 if (mt_com.mt_count == 0)
190 errx(1, "%s: unknown density", *argv);
191 dcanon = denstostring(mt_com.mt_count);
192 if (strcmp(dcanon, *argv) != 0)
194 "Using \"%s\" as an alias for %s\n",
197 } else if (!isdigit(**argv) &&
198 (comp->c_flags & IS_COMP)) {
200 mt_com.mt_count = stringtocomp(*argv);
201 if ((u_int32_t)mt_com.mt_count == 0xf0f0f0f0)
202 errx(1, "%s: unknown compression",
206 /* allow for hex numbers; useful for density */
207 mt_com.mt_count = strtol(*argv, &p, 0);
209 mt_com.mt_count = strtol(*argv, &p, 10);
210 #endif /* defined(__DragonFly__) */
211 if ((mt_com.mt_count <=
212 #if defined (__DragonFly__)
213 ((comp->c_flags & ZERO_ALLOWED)? -1: 0)
214 && ((comp->c_flags & IS_COMP) == 0)
217 #endif /* defined (__DragonFly__) */
219 errx(1, "%s: illegal count", *argv);
223 #if defined(__DragonFly__)
224 switch (comp->c_code) {
229 struct scsi_tape_errors *s = &umn.scsi_errstat;
231 if (ioctl(mtfd, comp->c_code, (caddr_t)&umn) < 0)
233 (void)printf("Last I/O Residual: %u\n", s->io_resid);
234 (void)printf(" Last I/O Command:");
235 for (i = 0; i < sizeof (s->io_cdb); i++)
236 (void)printf(" %02X", s->io_cdb[i]);
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");
246 (void)printf("Last Control Residual: %u\n",
248 (void)printf(" Last Control Command:");
249 for (i = 0; i < sizeof (s->ctl_cdb); i++)
250 (void)printf(" %02X", s->ctl_cdb[i]);
252 (void)printf(" Last Control Sense:\n\n\t");
253 for (i = 0; i < sizeof (s->ctl_sense); i++) {
254 (void)printf(" %02X", s->ctl_sense[i]);
255 if (((i + 1) & 0xf) == 0) {
256 (void)printf("\n\t");
259 (void)printf("\n\n");
267 if (ioctl(mtfd, comp->c_code, (caddr_t)&block) < 0)
269 (void)printf("%s: %s block location %u\n", tape,
270 (comp->c_code == MTIOCRDHPOS)? "hardware" :
278 u_int32_t block = (u_int32_t)mt_com.mt_count;
279 if (ioctl(mtfd, comp->c_code, (caddr_t)&block) < 0)
284 case MTIOCGETEOTMODEL:
287 if (ioctl(mtfd, MTIOCGETEOTMODEL, (caddr_t)&om) < 0)
289 (void)printf("%s: the model is %u filemar%s at EOT\n",
290 tape, om, (om > 1)? "ks" : "k");
294 case MTIOCSETEOTMODEL:
296 u_int32_t om, nm = (u_int32_t)mt_com.mt_count;
297 if (ioctl(mtfd, MTIOCGETEOTMODEL, (caddr_t)&om) < 0)
299 if (ioctl(mtfd, comp->c_code, (caddr_t)&nm) < 0)
301 (void)printf("%s: old model was %u filemar%s at EOT\n",
302 tape, om, (om > 1)? "ks" : "k");
303 (void)printf("%s: new model is %u filemar%s at EOT\n",
304 tape, nm, (nm > 1)? "ks" : "k");
312 if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
313 err(1, "%s: %s", tape, comp->c_name);
315 if (ioctl(mtfd, MTIOCGET, &mt_status) < 0)
324 #include <sundev/tmreg.h>
325 #include <sundev/arreg.h>
329 short t_type; /* type of magtape device */
330 char *t_name; /* printing name */
331 char *t_dsbits; /* "drive status" register */
332 char *t_erbits; /* "error" register */
335 { MT_ISCPC, "TapeMaster", TMS_BITS, 0 },
336 { MT_ISAR, "Archive", ARCH_CTRL_BITS, ARCH_BITS },
338 #if defined (__DragonFly__)
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"
347 { MT_ISAR, "SCSI tape drive", 0, 0 },
348 #endif /* defined (__DragonFly__) */
353 * Interpret the status buffer returned
356 status(struct mtget *bp)
358 struct tape_desc *mt;
360 for (mt = tapes;; mt++) {
361 if (mt->t_type == 0) {
362 (void)printf("%d: unknown tape drive type\n",
366 if (mt->t_type == bp->mt_type)
369 #if defined (__DragonFly__)
370 if(mt->t_type == MT_ISAR)
373 #endif /* defined (__DragonFly__) */
374 (void)printf("%s tape drive, residual=%d\n", mt->t_name, bp->mt_resid);
375 printreg("ds", (unsigned short)bp->mt_dsreg, mt->t_dsbits);
376 printreg("\ner", (unsigned short)bp->mt_erreg, mt->t_erbits);
378 #if defined (__DragonFly__)
380 #endif /* defined (__DragonFly__) */
384 * Print a register a la the %b format of the kernel's printf.
387 printreg(char *s, u_int v, char *bits)
392 if (bits && *bits == 8)
393 printf("%s=%o", s, v);
395 printf("%s=%x", s, v);
401 while ((i = *bits++)) {
402 if (v & (1 << (i-1))) {
406 for (; (c = *bits) > 32; bits++)
409 for (; *bits > 32; bits++)
419 (void)fprintf(stderr, "usage: mt [-f device] command [ count ]\n");
423 #if defined (__DragonFly__)
432 * Taken from T10 Project 997D
433 * SCSI-3 Stream Device Commands (SSC)
434 * Revision 11, 4-Nov-97
436 /*Num. bpmm bpi Reference */
437 { 0x1, 32, 800, "X3.22-1983" },
438 { 0x2, 63, 1600, "X3.39-1986" },
439 { 0x3, 246, 6250, "X3.54-1986" },
440 { 0x5, 315, 8000, "X3.136-1986" },
441 { 0x6, 126, 3200, "X3.157-1987" },
442 { 0x7, 252, 6400, "X3.116-1986" },
443 { 0x8, 315, 8000, "X3.158-1987" },
444 { 0x9, 491, 37871, "X3.180" },
445 { 0xA, 262, 6667, "X3B5/86-199" },
446 { 0xB, 63, 1600, "X3.56-1986" },
447 { 0xC, 500, 12690, "HI-TC1" },
448 { 0xD, 999, 25380, "HI-TC2" },
449 { 0xF, 394, 10000, "QIC-120" },
450 { 0x10, 394, 10000, "QIC-150" },
451 { 0x11, 630, 16000, "QIC-320" },
452 { 0x12, 2034, 51667, "QIC-1350" },
453 { 0x13, 2400, 61000, "X3B5/88-185A" },
454 { 0x14, 1703, 43245, "X3.202-1991" },
455 { 0x15, 1789, 45434, "ECMA TC17" },
456 { 0x16, 394, 10000, "X3.193-1990" },
457 { 0x17, 1673, 42500, "X3B5/91-174" },
458 { 0x18, 1673, 42500, "X3B5/92-50" },
459 { 0x19, 2460, 62500, "DLTapeIII" },
460 { 0x1A, 3214, 81633, "DLTapeIV(20GB)" },
461 { 0x1B, 3383, 85937, "DLTapeIV(35GB)" },
462 { 0x1C, 1654, 42000, "QIC-385M" },
463 { 0x1D, 1512, 38400, "QIC-410M" },
464 { 0x1E, 1385, 36000, "QIC-1000C" },
465 { 0x1F, 2666, 67733, "QIC-2100C" },
466 { 0x20, 2666, 67733, "QIC-6GB(M)" },
467 { 0x21, 2666, 67733, "QIC-20GB(C)" },
468 { 0x22, 1600, 40640, "QIC-2GB(C)" },
469 { 0x23, 2666, 67733, "QIC-875M" },
470 { 0x24, 2400, 61000, "DDS-2" },
471 { 0x25, 3816, 97000, "DDS-3" },
472 { 0x26, 3816, 97000, "DDS-4" },
473 { 0x27, 3056, 77611, "Mammoth" },
474 { 0x28, 1491, 37871, "X3.224" },
475 { 0x41, 3868, 98250, "DLTapeIV(40GB)" },
476 { 0x48, 5236, 133000, "SDLTapeI(110)" },
477 { 0x49, 7598, 193000, "SDLTapeI(160)" },
481 struct compression_types {
482 u_int32_t comp_number;
489 { 0xffffffff, "enable" },
490 { 0xffffffff, "on" },
498 struct densities *sd;
500 /* densities 0 and 0x7f are handled as special cases */
505 for (sd = dens; sd->dens; sd++)
509 sprintf(buf, "0x%02x", d);
511 sprintf(buf, "0x%02x:%s", d, sd->name);
516 * Given a specific density number, return either the bits per inch or bits
517 * per millimeter for the given density.
520 denstobp(int d, int bpi)
522 struct densities *sd;
524 for (sd = dens; sd->dens; sd++)
538 stringtodens(const char *s)
540 struct densities *sd;
541 size_t l = strlen(s);
543 for (sd = dens; sd->dens; sd++)
544 if (strncasecmp(sd->name, s, l) == 0)
557 sprintf(buf, "%d bytes", bs);
563 comptostring(u_int32_t comp)
566 struct compression_types *ct;
568 if (comp == MT_COMP_DISABLED)
570 else if (comp == MT_COMP_UNSUPP)
571 return "unsupported";
573 for (ct = comp_types; ct->name; ct++)
574 if (ct->comp_number == comp)
577 if (ct->comp_number == 0xf0f0f0f0) {
578 sprintf(buf, "0x%x", comp);
585 stringtocomp(const char *s)
587 struct compression_types *ct;
588 size_t l = strlen(s);
590 for (ct = comp_types; ct->name; ct++)
591 if (strncasecmp(ct->name, s, l) == 0)
594 return(ct->comp_number);
598 st_status(struct mtget *bp)
600 printf("Mode Density Blocksize bpi "
602 "Current: %-17s %-12s %-7d %s\n"
603 "---------available modes---------\n"
604 "0: %-17s %-12s %-7d %s\n"
605 "1: %-17s %-12s %-7d %s\n"
606 "2: %-17s %-12s %-7d %s\n"
607 "3: %-17s %-12s %-7d %s\n",
608 denstostring(bp->mt_density), getblksiz(bp->mt_blksiz),
609 denstobp(bp->mt_density, TRUE), comptostring(bp->mt_comp),
610 denstostring(bp->mt_density0), getblksiz(bp->mt_blksiz0),
611 denstobp(bp->mt_density0, TRUE), comptostring(bp->mt_comp0),
612 denstostring(bp->mt_density1), getblksiz(bp->mt_blksiz1),
613 denstobp(bp->mt_density1, TRUE), comptostring(bp->mt_comp1),
614 denstostring(bp->mt_density2), getblksiz(bp->mt_blksiz2),
615 denstobp(bp->mt_density2, TRUE), comptostring(bp->mt_comp2),
616 denstostring(bp->mt_density3), getblksiz(bp->mt_blksiz3),
617 denstobp(bp->mt_density3, TRUE), comptostring(bp->mt_comp3));
619 if (bp->mt_dsreg != MTIO_DSREG_NIL) {
621 const char *sfmt = "Current Driver State: %s.\n";
622 printf("---------------------------------\n");
623 switch (bp->mt_dsreg) {
624 case MTIO_DSREG_REST:
625 printf(sfmt, "at rest");
627 case MTIO_DSREG_RBSY:
628 printf(sfmt, "Communicating with drive");
631 printf(sfmt, "Writing");
634 printf(sfmt, "Writing Filemarks");
637 printf(sfmt, "Erasing");
640 printf(sfmt, "Reading");
643 printf(sfmt, "Spacing Forward");
646 printf(sfmt, "Spacing Reverse");
650 "Hardware Positioning (direction unknown)");
653 printf(sfmt, "Rewinding");
656 printf(sfmt, "Retensioning");
659 printf(sfmt, "Unloading");
662 printf(sfmt, "Loading");
665 (void) sprintf(foo, "Unknown state 0x%x", bp->mt_dsreg);
670 if (bp->mt_resid == 0 && bp->mt_fileno == (daddr_t) -1 &&
671 bp->mt_blkno == (daddr_t) -1)
673 printf("---------------------------------\n");
674 printf("File Number: %ld\tRecord Number: %ld\tResidual Count %d\n",
675 bp->mt_fileno, bp->mt_blkno, bp->mt_resid);
682 "The \"eof\" command has been disabled.\n"
683 "Use \"weof\" if you really want to write end-of-file marks,\n"
684 "or \"eom\" if you rather want to skip to the end of "
685 "recorded medium.\n");
689 #endif /* defined (__DragonFly__) */