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