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