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.13 1997/08/21 05:49:29 joerg 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 #ifdef __FreeBSD__ 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 { MT_ISVIPER1, "Archive Viper", WTDS_BITS, WTER_BITS }, 283 { MT_ISMFOUR, "Wangtek", WTDS_BITS, WTER_BITS }, 284 #endif /* defined (__FreeBSD__) */ 285 { 0 } 286 }; 287 288 /* 289 * Interpret the status buffer returned 290 */ 291 void 292 status(bp) 293 register struct mtget *bp; 294 { 295 register struct tape_desc *mt; 296 297 for (mt = tapes;; mt++) { 298 if (mt->t_type == 0) { 299 (void)printf("%d: unknown tape drive type\n", 300 bp->mt_type); 301 return; 302 } 303 if (mt->t_type == bp->mt_type) 304 break; 305 } 306 #if defined (__FreeBSD__) 307 if(mt->t_type == MT_ISAR) 308 st_status(bp); 309 else { 310 #endif /* defined (__FreeBSD__) */ 311 (void)printf("%s tape drive, residual=%d\n", mt->t_name, bp->mt_resid); 312 printreg("ds", (unsigned short)bp->mt_dsreg, mt->t_dsbits); 313 printreg("\ner", (unsigned short)bp->mt_erreg, mt->t_erbits); 314 (void)putchar('\n'); 315 #if defined (__FreeBSD__) 316 } 317 #endif /* defined (__FreeBSD__) */ 318 } 319 320 /* 321 * Print a register a la the %b format of the kernel's printf. 322 */ 323 void 324 printreg(s, v, bits) 325 char *s; 326 register u_int v; 327 register char *bits; 328 { 329 register int i, any = 0; 330 register char c; 331 332 if (bits && *bits == 8) 333 printf("%s=%o", s, v); 334 else 335 printf("%s=%x", s, v); 336 if (!bits) 337 return; 338 bits++; 339 if (v && bits) { 340 putchar('<'); 341 while ((i = *bits++)) { 342 if (v & (1 << (i-1))) { 343 if (any) 344 putchar(','); 345 any = 1; 346 for (; (c = *bits) > 32; bits++) 347 putchar(c); 348 } else 349 for (; *bits > 32; bits++) 350 ; 351 } 352 putchar('>'); 353 } 354 } 355 356 void 357 usage() 358 { 359 (void)fprintf(stderr, "usage: mt [-f device] command [ count ]\n"); 360 exit(1); 361 } 362 363 #if defined (__FreeBSD__) 364 365 struct densities { 366 int dens; 367 int bpmm; 368 int bpi; 369 const char *name; 370 } dens[] = { 371 /* 372 * Taken from T10 Project 997D 373 * SCSI-3 Stream Device Commands (SSC) 374 * Revision 11, 4-Nov-97 375 */ 376 /*Num. bpmm bpi Reference */ 377 { 0x1, 32, 800, "X3.22-1983" }, 378 { 0x2, 63, 1600, "X3.39-1986" }, 379 { 0x3, 246, 6250, "X3.54-1986" }, 380 { 0x5, 315, 8000, "X3.136-1986" }, 381 { 0x6, 126, 3200, "X3.157-1987" }, 382 { 0x7, 252, 6400, "X3.116-1986" }, 383 { 0x8, 315, 8000, "X3.158-1987" }, 384 { 0x9, 491, 37871, "X3.180" }, 385 { 0xA, 262, 6667, "X3B5/86-199" }, 386 { 0xB, 63, 1600, "X3.56-1986" }, 387 { 0xC, 500, 12690, "HI-TC1" }, 388 { 0xD, 999, 25380, "HI-TC2" }, 389 { 0xF, 394, 10000, "QIC-120" }, 390 { 0x10, 394, 10000, "QIC-150" }, 391 { 0x11, 630, 16000, "QIC-320" }, 392 { 0x12, 2034, 51667, "QIC-1350" }, 393 { 0x13, 2400, 61000, "X3B5/88-185A" }, 394 { 0x14, 1703, 43245, "X3.202-1991" }, 395 { 0x15, 1789, 45434, "ECMA TC17" }, 396 { 0x16, 394, 10000, "X3.193-1990" }, 397 { 0x17, 1673, 42500, "X3B5/91-174" }, 398 { 0x18, 1673, 42500, "X3B5/92-50" }, 399 { 0x1C, 1654, 42000, "QIC-385M" }, 400 { 0x1D, 1512, 38400, "QIC-410M" }, 401 { 0x1E, 1385, 36000, "QIC-1000C" }, 402 { 0x1F, 2666, 67733, "QIC-2100C" }, 403 { 0x20, 2666, 67733, "QIC-6GB(M)" }, 404 { 0x21, 2666, 67733, "QIC-20GB(C)" }, 405 { 0x22, 1600, 40640, "QIC-2GB(C)" }, 406 { 0x23, 2666, 67733, "QIC-875M" }, 407 { 0x24, 2400, 61000, "DDS-2" }, 408 { 0x25, 3816, 97000, "DDS-3" }, 409 { 0x26, 3816, 97000, "DDS-4" }, 410 { 0x27, 3056, 77611, "Mammoth" }, 411 { 0x28, 1491, 37871, "X3.224" }, 412 { 0, 0, 0, NULL } 413 }; 414 415 struct compression_types { 416 u_int32_t comp_number; 417 const char *name; 418 } comp_types[] = { 419 { 0x00, "none" }, 420 { 0x00, "off" }, 421 { 0x10, "IDRC" }, 422 { 0x20, "DCLZ" }, 423 { 0xffffffff, "enable" }, 424 { 0xffffffff, "on" }, 425 { 0xf0f0f0f0, NULL} 426 }; 427 428 const char * 429 denstostring(int d) 430 { 431 static char buf[20]; 432 struct densities *sd; 433 434 for (sd = dens; sd->dens; sd++) 435 if (sd->dens == d) 436 break; 437 if (sd->dens == 0) { 438 sprintf(buf, "0x%02x", d); 439 return buf; 440 } else 441 return sd->name; 442 } 443 444 /* 445 * Given a specific density number, return either the bits per inch or bits 446 * per millimeter for the given density. 447 */ 448 int 449 denstobp(int d, int bpi) 450 { 451 struct densities *sd; 452 453 for (sd = dens; sd->dens; sd++) 454 if (sd->dens == d) 455 break; 456 if (sd->dens == 0) 457 return(0); 458 else { 459 if (bpi) 460 return(sd->bpi); 461 else 462 return(sd->bpmm); 463 } 464 } 465 466 int 467 stringtodens(const char *s) 468 { 469 struct densities *sd; 470 size_t l = strlen(s); 471 472 for (sd = dens; sd->dens; sd++) 473 if (strncasecmp(sd->name, s, l) == 0) 474 break; 475 return sd->dens; 476 } 477 478 479 const char * 480 getblksiz(int bs) 481 { 482 static char buf[25]; 483 if (bs == 0) 484 return "variable"; 485 else { 486 sprintf(buf, "%d bytes", bs); 487 return buf; 488 } 489 } 490 491 const char * 492 comptostring(u_int32_t comp) 493 { 494 static char buf[20]; 495 struct compression_types *ct; 496 497 if (comp == MT_COMP_DISABLED) 498 return "disabled"; 499 else if (comp == MT_COMP_UNSUPP) 500 return "unsupported"; 501 502 for (ct = comp_types; ct->name; ct++) 503 if (ct->comp_number == comp) 504 break; 505 506 if (ct->comp_number == 0xf0f0f0f0) { 507 sprintf(buf, "0x%2x", comp); 508 return(buf); 509 } else 510 return(ct->name); 511 } 512 513 u_int32_t 514 stringtocomp(const char *s) 515 { 516 struct compression_types *ct; 517 size_t l = strlen(s); 518 519 for (ct = comp_types; ct->name; ct++) 520 if (strncasecmp(ct->name, s, l) == 0) 521 break; 522 523 return(ct->comp_number); 524 } 525 526 void 527 st_status(struct mtget *bp) 528 { 529 printf("Mode Density Blocksize bpi " 530 "Compression\n" 531 "Current: %-12s %-12s %-7d %s\n" 532 "---------available modes---------\n" 533 "0: %-12s %-12s %-7d %s\n" 534 "1: %-12s %-12s %-7d %s\n" 535 "2: %-12s %-12s %-7d %s\n" 536 "3: %-12s %-12s %-7d %s\n", 537 denstostring(bp->mt_density), getblksiz(bp->mt_blksiz), 538 denstobp(bp->mt_density, TRUE), comptostring(bp->mt_comp), 539 denstostring(bp->mt_density0), getblksiz(bp->mt_blksiz0), 540 denstobp(bp->mt_density0, TRUE), comptostring(bp->mt_comp0), 541 denstostring(bp->mt_density1), getblksiz(bp->mt_blksiz1), 542 denstobp(bp->mt_density1, TRUE), comptostring(bp->mt_comp1), 543 denstostring(bp->mt_density2), getblksiz(bp->mt_blksiz2), 544 denstobp(bp->mt_density2, TRUE), comptostring(bp->mt_comp2), 545 denstostring(bp->mt_density3), getblksiz(bp->mt_blksiz3), 546 denstobp(bp->mt_density3, TRUE), comptostring(bp->mt_comp3)); 547 } 548 549 void 550 warn_eof(void) 551 { 552 fprintf(stderr, 553 "The \"eof\" command has been disabled.\n" 554 "Use \"weof\" if you really want to write end-of-file marks,\n" 555 "or \"eom\" if you rather want to skip to the end of " 556 "recorded medium.\n"); 557 exit(1); 558 } 559 560 #endif /* defined (__FreeBSD__) */ 561