1*492c086fSandvar /* $NetBSD: mt.c,v 1.49 2022/01/24 09:14:36 andvar Exp $ */
2df247e9bScgd
323a6037aScgd /*
4df247e9bScgd * Copyright (c) 1980, 1993
5df247e9bScgd * The Regents of the University of California. All rights reserved.
623a6037aScgd *
723a6037aScgd * Redistribution and use in source and binary forms, with or without
823a6037aScgd * modification, are permitted provided that the following conditions
923a6037aScgd * are met:
1023a6037aScgd * 1. Redistributions of source code must retain the above copyright
1123a6037aScgd * notice, this list of conditions and the following disclaimer.
1223a6037aScgd * 2. Redistributions in binary form must reproduce the above copyright
1323a6037aScgd * notice, this list of conditions and the following disclaimer in the
1423a6037aScgd * documentation and/or other materials provided with the distribution.
15b5b29542Sagc * 3. Neither the name of the University nor the names of its contributors
1623a6037aScgd * may be used to endorse or promote products derived from this software
1723a6037aScgd * without specific prior written permission.
1823a6037aScgd *
1923a6037aScgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2023a6037aScgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2123a6037aScgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2223a6037aScgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2323a6037aScgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2423a6037aScgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2523a6037aScgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2623a6037aScgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2723a6037aScgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2823a6037aScgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2923a6037aScgd * SUCH DAMAGE.
3023a6037aScgd */
3123a6037aScgd
322e55bdafSchristos #include <sys/cdefs.h>
3323a6037aScgd #ifndef lint
342fe2731dSlukem __COPYRIGHT("@(#) Copyright (c) 1980, 1993\
352fe2731dSlukem The Regents of the University of California. All rights reserved.");
3623a6037aScgd #endif /* not lint */
3723a6037aScgd
3823a6037aScgd #ifndef lint
39df247e9bScgd #if 0
40546f6b17Stls static char sccsid[] = "@(#)mt.c 8.2 (Berkeley) 6/6/93";
41df247e9bScgd #else
42*492c086fSandvar __RCSID("$NetBSD: mt.c,v 1.49 2022/01/24 09:14:36 andvar Exp $");
43df247e9bScgd #endif
4423a6037aScgd #endif /* not lint */
4523a6037aScgd
4623a6037aScgd /*
4723a6037aScgd * mt --
4823a6037aScgd * magnetic tape manipulation program
4923a6037aScgd */
5023a6037aScgd #include <sys/types.h>
51cff5b51cSchristos #include <sys/param.h>
5223a6037aScgd #include <sys/ioctl.h>
5323a6037aScgd #include <sys/mtio.h>
54c23158c0Sscottr #include <sys/stat.h>
55c23158c0Sscottr
56df247e9bScgd #include <ctype.h>
57c23158c0Sscottr #include <err.h>
58c23158c0Sscottr #include <fcntl.h>
5931bf4d0eSlukem #include <paths.h>
6012fd1274Swiz #include <rmt.h>
61c23158c0Sscottr #include <stdio.h>
62c23158c0Sscottr #include <stdlib.h>
63df247e9bScgd #include <string.h>
643a2ca195Sscottr #include <unistd.h>
6523a6037aScgd
6633849ebeSjtc /* pseudo ioctl constants */
6733849ebeSjtc #define MTASF 100
6833849ebeSjtc
6923a6037aScgd struct commands {
700b34aaa0Shannken const char *c_name; /* command */
71796d7ce2Schristos size_t c_namelen; /* command len */
72c6470d37Sgmcgarry u_long c_spcl; /* ioctl request */
7360d6809bShannken int c_code; /* ioctl code for MTIOCTOP command */
740b34aaa0Shannken int c_ronly; /* open tape read-only */
750b34aaa0Shannken int c_mincount; /* min allowed count value */
7633849ebeSjtc };
7733849ebeSjtc
78796d7ce2Schristos #define CMD(a) a, sizeof(a) - 1
797aae24bcSjoerg static const struct commands com[] = {
80796d7ce2Schristos { CMD("asf"), MTIOCTOP, MTASF, 1, 0 },
81796d7ce2Schristos { CMD("blocksize"), MTIOCTOP, MTSETBSIZ, 1, 0 },
82796d7ce2Schristos { CMD("bsf"), MTIOCTOP, MTBSF, 1, 1 },
83796d7ce2Schristos { CMD("bsr"), MTIOCTOP, MTBSR, 1, 1 },
84796d7ce2Schristos { CMD("compress"), MTIOCTOP, MTCMPRESS, 1, 0 },
85796d7ce2Schristos { CMD("density"), MTIOCTOP, MTSETDNSTY, 1, 0 },
86796d7ce2Schristos { CMD("eof"), MTIOCTOP, MTWEOF, 0, 1 },
87796d7ce2Schristos { CMD("eom"), MTIOCTOP, MTEOM, 1, 0 },
88796d7ce2Schristos { CMD("erase"), MTIOCTOP, MTERASE, 0, 0 },
89796d7ce2Schristos { CMD("fsf"), MTIOCTOP, MTFSF, 1, 1 },
90796d7ce2Schristos { CMD("fsr"), MTIOCTOP, MTFSR, 1, 1 },
91796d7ce2Schristos { CMD("offline"), MTIOCTOP, MTOFFL, 1, 0 },
92796d7ce2Schristos { CMD("rdhpos"), MTIOCRDHPOS, 0, 1, 0 },
93796d7ce2Schristos { CMD("rdspos"), MTIOCRDSPOS, 0, 1, 0 },
94796d7ce2Schristos { CMD("retension"), MTIOCTOP, MTRETEN, 1, 0 },
95796d7ce2Schristos { CMD("rewind"), MTIOCTOP, MTREW, 1, 0 },
96796d7ce2Schristos { CMD("rewoffl"), MTIOCTOP, MTOFFL, 1, 0 },
97796d7ce2Schristos { CMD("setblk"), MTIOCTOP, MTSETBSIZ, 1, 0 },
98796d7ce2Schristos { CMD("setdensity"), MTIOCTOP, MTSETDNSTY, 1, 0 },
99796d7ce2Schristos { CMD("sethpos"), MTIOCHLOCATE, 0, 1, 0 },
100796d7ce2Schristos { CMD("setspos"), MTIOCSLOCATE, 0, 1, 0 },
101796d7ce2Schristos { CMD("status"), MTIOCGET, MTNOP, 1, 0 },
102796d7ce2Schristos { CMD("weof"), MTIOCTOP, MTWEOF, 0, 1 },
103796d7ce2Schristos { CMD("eew"), MTIOCTOP, MTEWARN, 1, 0 },
104c2909ab6Smlelstv { CMD("cache"), MTIOCTOP, MTCACHE, 1, 0 },
105c2909ab6Smlelstv { CMD("nocache"), MTIOCTOP, MTNOCACHE, 1, 0 },
106d2bca380Schristos { .c_name = NULL }
10723a6037aScgd };
10823a6037aScgd
1097aae24bcSjoerg static void printreg(const char *, u_int, const char *);
1107aae24bcSjoerg static void status(struct mtget *);
1117aae24bcSjoerg __dead static void usage(void);
11223a6037aScgd
113468f4334Sjtc int
main(int argc,char * argv[])11412fd1274Swiz main(int argc, char *argv[])
11523a6037aScgd {
116cff5b51cSchristos const struct commands *cp, *comp;
117df247e9bScgd struct mtget mt_status;
118df247e9bScgd struct mtop mt_com;
119796d7ce2Schristos int ch, mtfd, flags;
1200e2f9ea9Smycroft char *p;
1210e2f9ea9Smycroft const char *tape;
122a1b6da92Sveego int count;
123cff5b51cSchristos size_t len;
12423a6037aScgd
12512fd1274Swiz setprogname(argv[0]);
126df247e9bScgd if ((tape = getenv("TAPE")) == NULL)
12731bf4d0eSlukem tape = _PATH_DEFTAPE;
128df247e9bScgd
129df247e9bScgd while ((ch = getopt(argc, argv, "f:t:")) != -1)
130df247e9bScgd switch (ch) {
131468f4334Sjtc case 'f':
132468f4334Sjtc case 't':
133468f4334Sjtc tape = optarg;
134468f4334Sjtc break;
135df247e9bScgd case '?':
136468f4334Sjtc default:
137468f4334Sjtc usage();
138468f4334Sjtc }
139468f4334Sjtc argc -= optind;
140468f4334Sjtc argv += optind;
141468f4334Sjtc
142df247e9bScgd if (argc < 1 || argc > 2)
143468f4334Sjtc usage();
144468f4334Sjtc
145cff5b51cSchristos len = strlen(p = *argv++);
146cff5b51cSchristos for (comp = NULL, cp = com; cp->c_name != NULL; cp++) {
147cff5b51cSchristos size_t clen = MIN(len, cp->c_namelen);
148cff5b51cSchristos if (strncmp(p, cp->c_name, clen) == 0) {
149cff5b51cSchristos if (comp != NULL)
150cff5b51cSchristos errx(1, "%s: Ambiguous command `%s' or `%s'?",
151cff5b51cSchristos p, cp->c_name, comp->c_name);
152cff5b51cSchristos else
153cff5b51cSchristos comp = cp;
15423a6037aScgd }
155cff5b51cSchristos }
156cff5b51cSchristos if (comp == NULL)
157cff5b51cSchristos errx(1, "%s: unknown command", p);
158c23158c0Sscottr
1590b34aaa0Shannken if (*argv) {
1600b34aaa0Shannken count = strtol(*argv, &p, 10);
1610b34aaa0Shannken if (count < comp->c_mincount || *p)
1620b34aaa0Shannken errx(2, "%s: illegal count", *argv);
16392aec668Smjacob } else
1640b34aaa0Shannken count = 1;
16560d6809bShannken
166813b4a4fSjtc flags = comp->c_ronly ? O_RDONLY : O_WRONLY;
16792aec668Smjacob
168813b4a4fSjtc if ((mtfd = open(tape, flags)) < 0)
1696c1353c6Spk err(2, "%s", tape);
17033849ebeSjtc
17160d6809bShannken switch (comp->c_spcl) {
17260d6809bShannken case MTIOCTOP:
17360d6809bShannken if (comp->c_code == MTASF) {
174885787d2Smason
17533849ebeSjtc /* If mtget.mt_fileno was implemented, We could
17633849ebeSjtc compute the minimal seek needed to position
17733849ebeSjtc the tape. Until then, rewind and seek from
178*492c086fSandvar beginning-of-tape */
17933849ebeSjtc
18033849ebeSjtc mt_com.mt_op = MTREW;
181df247e9bScgd mt_com.mt_count = 1;
182813b4a4fSjtc if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
18333849ebeSjtc err(2, "%s", tape);
18433849ebeSjtc
1855b0331e6Schristos if (count > 0) {
18633849ebeSjtc mt_com.mt_op = MTFSF;
18733849ebeSjtc mt_com.mt_count = count;
188813b4a4fSjtc if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
18933849ebeSjtc err(2, "%s", tape);
1905b0331e6Schristos }
1913b04543dSmason
19260d6809bShannken } else {
19333849ebeSjtc mt_com.mt_op = comp->c_code;
19433849ebeSjtc mt_com.mt_count = count;
19533849ebeSjtc
196813b4a4fSjtc if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
1976c1353c6Spk err(2, "%s: %s", tape, comp->c_name);
19833849ebeSjtc
19960d6809bShannken }
20060d6809bShannken break;
20160d6809bShannken
20260d6809bShannken case MTIOCGET:
203df247e9bScgd if (ioctl(mtfd, MTIOCGET, &mt_status) < 0)
204813b4a4fSjtc err(2, "%s: %s", tape, comp->c_name);
20523a6037aScgd status(&mt_status);
20660d6809bShannken break;
20760d6809bShannken
20860d6809bShannken case MTIOCRDSPOS:
20960d6809bShannken case MTIOCRDHPOS:
21060d6809bShannken if (ioctl(mtfd, comp->c_spcl, (caddr_t) &count) < 0)
21160d6809bShannken err(2, "%s", tape);
21260d6809bShannken printf("%s: block location %u\n", tape, (unsigned int) count);
21360d6809bShannken break;
21460d6809bShannken
21560d6809bShannken case MTIOCSLOCATE:
21660d6809bShannken case MTIOCHLOCATE:
21760d6809bShannken if (ioctl(mtfd, comp->c_spcl, (caddr_t) &count) < 0)
21860d6809bShannken err(2, "%s", tape);
21960d6809bShannken break;
22060d6809bShannken
22160d6809bShannken default:
222c3b638beSdogcow errx(1, "internal error: unknown request %ld", comp->c_spcl);
22323a6037aScgd }
22441c2cff5Sscottr
225813b4a4fSjtc exit(0);
226df247e9bScgd /* NOTREACHED */
22723a6037aScgd }
22823a6037aScgd
229fb7b7a24Schristos #if defined(sun) && !defined(__SVR4)
23023a6037aScgd #include <sundev/tmreg.h>
23123a6037aScgd #include <sundev/arreg.h>
23223a6037aScgd #endif
23323a6037aScgd
23423a6037aScgd #ifdef tahoe
23523a6037aScgd #include <tahoe/vba/cyreg.h>
23623a6037aScgd #endif
23723a6037aScgd
2387aae24bcSjoerg static const struct tape_desc {
23923a6037aScgd short t_type; /* type of magtape device */
2400e2f9ea9Smycroft const char *t_name; /* printing name */
2410e2f9ea9Smycroft const char *t_dsbits; /* "drive status" register */
2420e2f9ea9Smycroft const char *t_erbits; /* "error" register */
24323a6037aScgd } tapes[] = {
244fb7b7a24Schristos #if defined(sun) && !defined(__SVR4)
24523a6037aScgd { MT_ISCPC, "TapeMaster", TMS_BITS, 0 },
24623a6037aScgd { MT_ISAR, "Archive", ARCH_CTRL_BITS, ARCH_BITS },
24723a6037aScgd #endif
24823a6037aScgd #ifdef tahoe
24923a6037aScgd { MT_ISCY, "cipher", CYS_BITS, CYCW_BITS },
25023a6037aScgd #endif
2515433a0b6Smjacob #define SCSI_DS_BITS "\20\5WriteProtect\2Mounted"
2525433a0b6Smjacob { 0x7, "SCSI", SCSI_DS_BITS, "76543210" },
253d2bca380Schristos { .t_type = 0 }
25423a6037aScgd };
25523a6037aScgd
2565433a0b6Smjacob
25723a6037aScgd /*
25823a6037aScgd * Interpret the status buffer returned
25923a6037aScgd */
2607aae24bcSjoerg static void
status(struct mtget * bp)26112fd1274Swiz status(struct mtget *bp)
26223a6037aScgd {
2630e2f9ea9Smycroft const struct tape_desc *mt;
26423a6037aScgd
265df247e9bScgd for (mt = tapes;; mt++) {
26623a6037aScgd if (mt->t_type == 0) {
267df247e9bScgd (void)printf("%d: unknown tape drive type\n",
268df247e9bScgd bp->mt_type);
26923a6037aScgd return;
27023a6037aScgd }
271df247e9bScgd if (mt->t_type == bp->mt_type)
272df247e9bScgd break;
273df247e9bScgd }
274df247e9bScgd (void)printf("%s tape drive, residual=%d\n", mt->t_name, bp->mt_resid);
27523a6037aScgd printreg("ds", bp->mt_dsreg, mt->t_dsbits);
27623a6037aScgd printreg("\ner", bp->mt_erreg, mt->t_erbits);
277df247e9bScgd (void)putchar('\n');
2782e55bdafSchristos (void)printf("blocksize: %d (%d, %d, %d, %d)\n",
2795bdb157aSmrg bp->mt_blksiz, bp->mt_mblksiz[0], bp->mt_mblksiz[1],
2805bdb157aSmrg bp->mt_mblksiz[2], bp->mt_mblksiz[3]);
2812e55bdafSchristos (void)printf("density: %d (%d, %d, %d, %d)\n",
2825bdb157aSmrg bp->mt_density, bp->mt_mdensity[0], bp->mt_mdensity[1],
2835bdb157aSmrg bp->mt_mdensity[2], bp->mt_mdensity[3]);
2847c249bc0Ssimonb (void)printf("current file number: %d\n", bp->mt_fileno);
2857c249bc0Ssimonb (void)printf("current block number: %d\n", bp->mt_blkno);
28623a6037aScgd }
28723a6037aScgd
28823a6037aScgd /*
289df247e9bScgd * Print a register a la the %b format of the kernel's printf.
29023a6037aScgd */
2917aae24bcSjoerg static void
printreg(const char * s,u_int v,const char * bits)29212fd1274Swiz printreg(const char *s, u_int v, const char *bits)
29323a6037aScgd {
29412fd1274Swiz int any, i;
2953811362cStls char c;
29623a6037aScgd
29712fd1274Swiz any = 0;
29823a6037aScgd if (bits && *bits == 8)
29923a6037aScgd printf("%s=%o", s, v);
30023a6037aScgd else
30123a6037aScgd printf("%s=%x", s, v);
3024bd79d53Schristos if (v && bits && *++bits) {
30323a6037aScgd putchar('<');
3043a2ca195Sscottr while ((i = *bits++)) {
30523a6037aScgd if (v & (1 << (i-1))) {
30623a6037aScgd if (any)
30723a6037aScgd putchar(',');
30823a6037aScgd any = 1;
30923a6037aScgd for (; (c = *bits) > 32; bits++)
31023a6037aScgd putchar(c);
31123a6037aScgd } else
31223a6037aScgd for (; *bits > 32; bits++)
31323a6037aScgd ;
31423a6037aScgd }
31523a6037aScgd putchar('>');
31623a6037aScgd }
31723a6037aScgd }
318468f4334Sjtc
3197aae24bcSjoerg static void
usage(void)32012fd1274Swiz usage(void)
321468f4334Sjtc {
322dfa6df8eShira (void)fprintf(stderr, "usage: %s [-f device] command [count]\n",
323dfa6df8eShira getprogname());
324813b4a4fSjtc exit(1);
3259dc385beSmycroft /* NOTREACHED */
326468f4334Sjtc }
327