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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)mt.c 8.2 (Berkeley) 5/4/95
31 * $FreeBSD: src/usr.bin/mt/mt.c,v 1.26.2.3 2002/11/08 11:35:57 joerg Exp $
36 * magnetic tape manipulation program
38 #include <sys/types.h>
39 #include <sys/ioctl.h>
50 /* the appropriate sections of <sys/mtio.h> are also #ifdef'd for FreeBSD */
51 #if defined(__DragonFly__)
53 #define NEED_2ARGS 0x01
54 #define ZERO_ALLOWED 0x02
55 #define IS_DENSITY 0x04
56 #define DISABLE_THIS 0x08
58 #endif /* defined(__DragonFly__) */
67 static struct commands
{
71 #if defined(__DragonFly__)
73 #endif /* defined(__DragonFly__) */
77 #if defined(__DragonFly__)
78 /* XXX FreeBSD considered "eof" dangerous, since it's being
79 confused with "eom" (and is an alias for "weof" anyway) */
80 { "eof", MTWEOF
, 0, DISABLE_THIS
},
86 { "offline", MTOFFL
, 1 },
87 { "rewind", MTREW
, 1 },
88 { "rewoffl", MTOFFL
, 1 },
89 { "status", MTNOP
, 1 },
90 #if defined(__DragonFly__)
91 { "weof", MTWEOF
, 0, ZERO_ALLOWED
},
93 { "weof", MTWEOF
, 0 },
95 #if defined(__DragonFly__)
96 { "erase", MTERASE
, 0, ZERO_ALLOWED
},
97 { "blocksize", MTSETBSIZ
, 0, NEED_2ARGS
|ZERO_ALLOWED
},
98 { "density", MTSETDNSTY
, 0, NEED_2ARGS
|ZERO_ALLOWED
|IS_DENSITY
},
105 { "comp", MTCOMP
, 0, NEED_2ARGS
|ZERO_ALLOWED
|IS_COMP
},
106 { "retension", MTRETENS
, 1 },
107 { "rdhpos", MTIOCRDHPOS
, 0 },
108 { "rdspos", MTIOCRDSPOS
, 0 },
109 { "sethpos", MTIOCHLOCATE
, 0, NEED_2ARGS
|ZERO_ALLOWED
},
110 { "setspos", MTIOCSLOCATE
, 0, NEED_2ARGS
|ZERO_ALLOWED
},
111 { "errstat", MTIOCERRSTAT
, 0 },
112 { "setmodel", MTIOCSETEOTMODEL
, 0, NEED_2ARGS
|ZERO_ALLOWED
},
113 { "seteotmodel", MTIOCSETEOTMODEL
, 0, NEED_2ARGS
|ZERO_ALLOWED
},
114 { "getmodel", MTIOCGETEOTMODEL
},
115 { "geteotmodel", MTIOCGETEOTMODEL
},
116 #endif /* defined(__DragonFly__) */
120 static void printreg(char *, u_int
, char *);
121 static void status(struct mtget
*);
122 static void usage(void) __dead2
;
123 #if defined (__DragonFly__)
124 static void st_status(struct mtget
*);
125 static int stringtodens(const char *s
);
126 static const char *denstostring(int d
);
127 static int denstobp(int d
, int bpi
);
128 static u_int32_t
stringtocomp(const char *s
);
129 static const char *comptostring(u_int32_t comp
);
130 static void warn_eof(void) __dead2
;
131 #endif /* defined (__DragonFly__) */
134 main(int argc
, char **argv
)
136 struct commands
*comp
;
137 struct mtget mt_status
;
142 if ((tape
= getenv("TAPE")) == NULL
)
145 while ((ch
= getopt(argc
, argv
, "f:t:")) != -1)
158 if (argc
< 1 || argc
> 2)
161 len
= strlen(p
= *argv
++);
162 for (comp
= com
;; comp
++) {
163 if (comp
->c_name
== NULL
)
164 errx(1, "%s: unknown command", p
);
165 if (strncmp(p
, comp
->c_name
, len
) == 0)
168 #if defined(__DragonFly__)
169 if((comp
->c_flags
& NEED_2ARGS
) && argc
!= 2)
171 if(comp
->c_flags
& DISABLE_THIS
) {
174 #endif /* defined(__DragonFly__) */
175 if ((mtfd
= open(tape
, comp
->c_ronly
? O_RDONLY
: O_RDWR
)) < 0)
177 if (comp
->c_code
!= MTNOP
) {
178 mt_com
.mt_op
= comp
->c_code
;
180 #if defined (__DragonFly__)
181 if (!isdigit(**argv
) &&
182 (comp
->c_flags
& IS_DENSITY
)) {
184 mt_com
.mt_count
= stringtodens(*argv
);
185 if (mt_com
.mt_count
== 0)
186 errx(1, "%s: unknown density", *argv
);
187 dcanon
= denstostring(mt_com
.mt_count
);
188 if (strcmp(dcanon
, *argv
) != 0)
190 "Using \"%s\" as an alias for %s\n",
193 } else if (!isdigit(**argv
) &&
194 (comp
->c_flags
& IS_COMP
)) {
196 mt_com
.mt_count
= stringtocomp(*argv
);
197 if ((u_int32_t
)mt_com
.mt_count
== 0xf0f0f0f0)
198 errx(1, "%s: unknown compression",
202 /* allow for hex numbers; useful for density */
203 mt_com
.mt_count
= strtol(*argv
, &p
, 0);
205 mt_com
.mt_count
= strtol(*argv
, &p
, 10);
206 #endif /* defined(__DragonFly__) */
207 if ((mt_com
.mt_count
<=
208 #if defined (__DragonFly__)
209 ((comp
->c_flags
& ZERO_ALLOWED
)? -1: 0)
210 && ((comp
->c_flags
& IS_COMP
) == 0)
213 #endif /* defined (__DragonFly__) */
215 errx(1, "%s: illegal count", *argv
);
219 #if defined(__DragonFly__)
220 switch (comp
->c_code
) {
225 struct scsi_tape_errors
*s
= &umn
.scsi_errstat
;
227 if (ioctl(mtfd
, comp
->c_code
, (caddr_t
)&umn
) < 0)
229 (void)printf("Last I/O Residual: %u\n", s
->io_resid
);
230 (void)printf(" Last I/O Command:");
231 for (i
= 0; i
< sizeof (s
->io_cdb
); i
++)
232 (void)printf(" %02X", s
->io_cdb
[i
]);
234 (void)printf(" Last I/O Sense:\n\n\t");
235 for (i
= 0; i
< sizeof (s
->io_sense
); i
++) {
236 (void)printf(" %02X", s
->io_sense
[i
]);
237 if (((i
+ 1) & 0xf) == 0) {
238 (void)printf("\n\t");
242 (void)printf("Last Control Residual: %u\n",
244 (void)printf(" Last Control Command:");
245 for (i
= 0; i
< sizeof (s
->ctl_cdb
); i
++)
246 (void)printf(" %02X", s
->ctl_cdb
[i
]);
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");
255 (void)printf("\n\n");
263 if (ioctl(mtfd
, comp
->c_code
, (caddr_t
)&block
) < 0)
265 (void)printf("%s: %s block location %u\n", tape
,
266 (comp
->c_code
== MTIOCRDHPOS
)? "hardware" :
274 u_int32_t block
= (u_int32_t
)mt_com
.mt_count
;
275 if (ioctl(mtfd
, comp
->c_code
, (caddr_t
)&block
) < 0)
280 case MTIOCGETEOTMODEL
:
283 if (ioctl(mtfd
, MTIOCGETEOTMODEL
, (caddr_t
)&om
) < 0)
285 (void)printf("%s: the model is %u filemar%s at EOT\n",
286 tape
, om
, (om
> 1)? "ks" : "k");
290 case MTIOCSETEOTMODEL
:
292 u_int32_t om
, nm
= (u_int32_t
)mt_com
.mt_count
;
293 if (ioctl(mtfd
, MTIOCGETEOTMODEL
, (caddr_t
)&om
) < 0)
295 if (ioctl(mtfd
, comp
->c_code
, (caddr_t
)&nm
) < 0)
297 (void)printf("%s: old model was %u filemar%s at EOT\n",
298 tape
, om
, (om
> 1)? "ks" : "k");
299 (void)printf("%s: new model is %u filemar%s at EOT\n",
300 tape
, nm
, (nm
> 1)? "ks" : "k");
308 if (ioctl(mtfd
, MTIOCTOP
, &mt_com
) < 0)
309 err(1, "%s: %s", tape
, comp
->c_name
);
311 if (ioctl(mtfd
, MTIOCGET
, &mt_status
) < 0)
320 #include <sundev/tmreg.h>
321 #include <sundev/arreg.h>
324 static struct tape_desc
{
325 short t_type
; /* type of magtape device */
326 char *t_name
; /* printing name */
327 char *t_dsbits
; /* "drive status" register */
328 char *t_erbits
; /* "error" register */
331 { MT_ISCPC
, "TapeMaster", TMS_BITS
, 0 },
332 { MT_ISAR
, "Archive", ARCH_CTRL_BITS
, ARCH_BITS
},
334 #if defined (__DragonFly__)
336 * XXX This is weird. The st driver reports the tape drive
337 * as 0x7 (MT_ISAR - Sun/Archive compatible); the wt driver
338 * either reports MT_ISVIPER1 for an Archive tape, or 0x11
339 * (MT_ISMFOUR) for other tapes.
340 * XXX for the wt driver, rely on it behaving like a "standard"
343 { MT_ISAR
, "SCSI tape drive", 0, 0 },
344 #endif /* defined (__DragonFly__) */
349 * Interpret the status buffer returned
352 status(struct mtget
*bp
)
354 struct tape_desc
*mt
;
356 for (mt
= tapes
;; mt
++) {
357 if (mt
->t_type
== 0) {
358 (void)printf("%d: unknown tape drive type\n",
362 if (mt
->t_type
== bp
->mt_type
)
365 #if defined (__DragonFly__)
366 if(mt
->t_type
== MT_ISAR
)
369 #endif /* defined (__DragonFly__) */
370 (void)printf("%s tape drive, residual=%d\n", mt
->t_name
, bp
->mt_resid
);
371 printreg("ds", (unsigned short)bp
->mt_dsreg
, mt
->t_dsbits
);
372 printreg("\ner", (unsigned short)bp
->mt_erreg
, mt
->t_erbits
);
374 #if defined (__DragonFly__)
376 #endif /* defined (__DragonFly__) */
380 * Print a register a la the %b format of the kernel's printf.
383 printreg(char *s
, u_int v
, char *bits
)
388 if (bits
&& *bits
== 8)
389 printf("%s=%o", s
, v
);
391 printf("%s=%x", s
, v
);
397 while ((i
= *bits
++)) {
398 if (v
& (1 << (i
-1))) {
402 for (; (c
= *bits
) > 32; bits
++)
405 for (; *bits
> 32; bits
++)
415 (void)fprintf(stderr
, "usage: mt [-f device] command [ count ]\n");
419 #if defined (__DragonFly__)
421 static struct densities
{
428 * Taken from T10 Project 997D
429 * SCSI-3 Stream Device Commands (SSC)
430 * Revision 11, 4-Nov-97
432 /*Num. bpmm bpi Reference */
433 { 0x1, 32, 800, "X3.22-1983" },
434 { 0x2, 63, 1600, "X3.39-1986" },
435 { 0x3, 246, 6250, "X3.54-1986" },
436 { 0x5, 315, 8000, "X3.136-1986" },
437 { 0x6, 126, 3200, "X3.157-1987" },
438 { 0x7, 252, 6400, "X3.116-1986" },
439 { 0x8, 315, 8000, "X3.158-1987" },
440 { 0x9, 491, 37871, "X3.180" },
441 { 0xA, 262, 6667, "X3B5/86-199" },
442 { 0xB, 63, 1600, "X3.56-1986" },
443 { 0xC, 500, 12690, "HI-TC1" },
444 { 0xD, 999, 25380, "HI-TC2" },
445 { 0xF, 394, 10000, "QIC-120" },
446 { 0x10, 394, 10000, "QIC-150" },
447 { 0x11, 630, 16000, "QIC-320" },
448 { 0x12, 2034, 51667, "QIC-1350" },
449 { 0x13, 2400, 61000, "X3B5/88-185A" },
450 { 0x14, 1703, 43245, "X3.202-1991" },
451 { 0x15, 1789, 45434, "ECMA TC17" },
452 { 0x16, 394, 10000, "X3.193-1990" },
453 { 0x17, 1673, 42500, "X3B5/91-174" },
454 { 0x18, 1673, 42500, "X3B5/92-50" },
455 { 0x19, 2460, 62500, "DLTapeIII" },
456 { 0x1A, 3214, 81633, "DLTapeIV(20GB)" },
457 { 0x1B, 3383, 85937, "DLTapeIV(35GB)" },
458 { 0x1C, 1654, 42000, "QIC-385M" },
459 { 0x1D, 1512, 38400, "QIC-410M" },
460 { 0x1E, 1385, 36000, "QIC-1000C" },
461 { 0x1F, 2666, 67733, "QIC-2100C" },
462 { 0x20, 2666, 67733, "QIC-6GB(M)" },
463 { 0x21, 2666, 67733, "QIC-20GB(C)" },
464 { 0x22, 1600, 40640, "QIC-2GB(C)" },
465 { 0x23, 2666, 67733, "QIC-875M" },
466 { 0x24, 2400, 61000, "DDS-2" },
467 { 0x25, 3816, 97000, "DDS-3" },
468 { 0x26, 3816, 97000, "DDS-4" },
469 { 0x27, 3056, 77611, "Mammoth" },
470 { 0x28, 1491, 37871, "X3.224" },
471 { 0x41, 3868, 98250, "DLTapeIV(40GB)" },
472 { 0x48, 5236, 133000, "SDLTapeI(110)" },
473 { 0x49, 7598, 193000, "SDLTapeI(160)" },
477 static struct compression_types
{
478 u_int32_t comp_number
;
485 { 0xffffffff, "enable" },
486 { 0xffffffff, "on" },
494 struct densities
*sd
;
496 /* densities 0 and 0x7f are handled as special cases */
501 for (sd
= dens
; sd
->dens
; sd
++)
505 sprintf(buf
, "0x%02x", d
);
507 sprintf(buf
, "0x%02x:%s", d
, sd
->name
);
512 * Given a specific density number, return either the bits per inch or bits
513 * per millimeter for the given density.
516 denstobp(int d
, int bpi
)
518 struct densities
*sd
;
520 for (sd
= dens
; sd
->dens
; sd
++)
534 stringtodens(const char *s
)
536 struct densities
*sd
;
537 size_t l
= strlen(s
);
539 for (sd
= dens
; sd
->dens
; sd
++)
540 if (strncasecmp(sd
->name
, s
, l
) == 0)
553 sprintf(buf
, "%d bytes", bs
);
559 comptostring(u_int32_t comp
)
562 struct compression_types
*ct
;
564 if (comp
== MT_COMP_DISABLED
)
566 else if (comp
== MT_COMP_UNSUPP
)
567 return "unsupported";
569 for (ct
= comp_types
; ct
->name
; ct
++)
570 if (ct
->comp_number
== comp
)
573 if (ct
->comp_number
== 0xf0f0f0f0) {
574 sprintf(buf
, "0x%x", comp
);
581 stringtocomp(const char *s
)
583 struct compression_types
*ct
;
584 size_t l
= strlen(s
);
586 for (ct
= comp_types
; ct
->name
; ct
++)
587 if (strncasecmp(ct
->name
, s
, l
) == 0)
590 return(ct
->comp_number
);
594 st_status(struct mtget
*bp
)
596 printf("Mode Density Blocksize bpi "
598 "Current: %-17s %-12s %-7d %s\n"
599 "---------available modes---------\n"
600 "0: %-17s %-12s %-7d %s\n"
601 "1: %-17s %-12s %-7d %s\n"
602 "2: %-17s %-12s %-7d %s\n"
603 "3: %-17s %-12s %-7d %s\n",
604 denstostring(bp
->mt_density
), getblksiz(bp
->mt_blksiz
),
605 denstobp(bp
->mt_density
, TRUE
), comptostring(bp
->mt_comp
),
606 denstostring(bp
->mt_density0
), getblksiz(bp
->mt_blksiz0
),
607 denstobp(bp
->mt_density0
, TRUE
), comptostring(bp
->mt_comp0
),
608 denstostring(bp
->mt_density1
), getblksiz(bp
->mt_blksiz1
),
609 denstobp(bp
->mt_density1
, TRUE
), comptostring(bp
->mt_comp1
),
610 denstostring(bp
->mt_density2
), getblksiz(bp
->mt_blksiz2
),
611 denstobp(bp
->mt_density2
, TRUE
), comptostring(bp
->mt_comp2
),
612 denstostring(bp
->mt_density3
), getblksiz(bp
->mt_blksiz3
),
613 denstobp(bp
->mt_density3
, TRUE
), comptostring(bp
->mt_comp3
));
615 if (bp
->mt_dsreg
!= MTIO_DSREG_NIL
) {
617 const char *sfmt
= "Current Driver State: %s.\n";
618 printf("---------------------------------\n");
619 switch (bp
->mt_dsreg
) {
620 case MTIO_DSREG_REST
:
621 printf(sfmt
, "at rest");
623 case MTIO_DSREG_RBSY
:
624 printf(sfmt
, "Communicating with drive");
627 printf(sfmt
, "Writing");
630 printf(sfmt
, "Writing Filemarks");
633 printf(sfmt
, "Erasing");
636 printf(sfmt
, "Reading");
639 printf(sfmt
, "Spacing Forward");
642 printf(sfmt
, "Spacing Reverse");
646 "Hardware Positioning (direction unknown)");
649 printf(sfmt
, "Rewinding");
652 printf(sfmt
, "Retensioning");
655 printf(sfmt
, "Unloading");
658 printf(sfmt
, "Loading");
661 (void) sprintf(foo
, "Unknown state 0x%x", bp
->mt_dsreg
);
666 if (bp
->mt_resid
== 0 && bp
->mt_fileno
== (daddr_t
) -1 &&
667 bp
->mt_blkno
== (daddr_t
) -1)
669 printf("---------------------------------\n");
670 printf("File Number: %u\tRecord Number: %u\tResidual Count %d\n",
671 bp
->mt_fileno
, bp
->mt_blkno
, bp
->mt_resid
);
678 "The \"eof\" command has been disabled.\n"
679 "Use \"weof\" if you really want to write end-of-file marks,\n"
680 "or \"eom\" if you rather want to skip to the end of "
681 "recorded medium.\n");
685 #endif /* defined (__DragonFly__) */