mt.c revision 1.45 1 1.45 dogcow /* $NetBSD: mt.c,v 1.45 2008/06/27 08:17:43 dogcow Exp $ */
2 1.5 cgd
3 1.1 cgd /*
4 1.5 cgd * Copyright (c) 1980, 1993
5 1.5 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.37 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.21 christos #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.21 christos __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
35 1.21 christos The Regents of the University of California. All rights reserved.\n");
36 1.1 cgd #endif /* not lint */
37 1.1 cgd
38 1.1 cgd #ifndef lint
39 1.5 cgd #if 0
40 1.8 tls static char sccsid[] = "@(#)mt.c 8.2 (Berkeley) 6/6/93";
41 1.5 cgd #else
42 1.45 dogcow __RCSID("$NetBSD: mt.c,v 1.45 2008/06/27 08:17:43 dogcow Exp $");
43 1.5 cgd #endif
44 1.1 cgd #endif /* not lint */
45 1.1 cgd
46 1.1 cgd /*
47 1.1 cgd * mt --
48 1.1 cgd * magnetic tape manipulation program
49 1.1 cgd */
50 1.1 cgd #include <sys/types.h>
51 1.39 christos #include <sys/param.h>
52 1.1 cgd #include <sys/ioctl.h>
53 1.1 cgd #include <sys/mtio.h>
54 1.12 scottr #include <sys/stat.h>
55 1.12 scottr
56 1.12 scottr #include <ctype.h>
57 1.12 scottr #include <err.h>
58 1.1 cgd #include <fcntl.h>
59 1.19 lukem #include <paths.h>
60 1.36 wiz #include <rmt.h>
61 1.12 scottr #include <stdio.h>
62 1.5 cgd #include <stdlib.h>
63 1.3 jtc #include <string.h>
64 1.11 scottr #include <unistd.h>
65 1.25 thorpej
66 1.16 jtc /* pseudo ioctl constants */
67 1.16 jtc #define MTASF 100
68 1.16 jtc
69 1.1 cgd struct commands {
70 1.20 hannken const char *c_name; /* command */
71 1.38 christos size_t c_namelen; /* command len */
72 1.44 gmcgarry u_long c_spcl; /* ioctl request */
73 1.23 hannken int c_code; /* ioctl code for MTIOCTOP command */
74 1.20 hannken int c_ronly; /* open tape read-only */
75 1.20 hannken int c_mincount; /* min allowed count value */
76 1.16 jtc };
77 1.16 jtc
78 1.38 christos #define CMD(a) a, sizeof(a) - 1
79 1.16 jtc const struct commands com[] = {
80 1.38 christos { CMD("asf"), MTIOCTOP, MTASF, 1, 0 },
81 1.38 christos { CMD("blocksize"), MTIOCTOP, MTSETBSIZ, 1, 0 },
82 1.38 christos { CMD("bsf"), MTIOCTOP, MTBSF, 1, 1 },
83 1.38 christos { CMD("bsr"), MTIOCTOP, MTBSR, 1, 1 },
84 1.38 christos { CMD("compress"), MTIOCTOP, MTCMPRESS, 1, 0 },
85 1.38 christos { CMD("density"), MTIOCTOP, MTSETDNSTY, 1, 0 },
86 1.38 christos { CMD("eof"), MTIOCTOP, MTWEOF, 0, 1 },
87 1.38 christos { CMD("eom"), MTIOCTOP, MTEOM, 1, 0 },
88 1.38 christos { CMD("erase"), MTIOCTOP, MTERASE, 0, 0 },
89 1.38 christos { CMD("fsf"), MTIOCTOP, MTFSF, 1, 1 },
90 1.38 christos { CMD("fsr"), MTIOCTOP, MTFSR, 1, 1 },
91 1.38 christos { CMD("offline"), MTIOCTOP, MTOFFL, 1, 0 },
92 1.38 christos { CMD("rdhpos"), MTIOCRDHPOS, 0, 1, 0 },
93 1.38 christos { CMD("rdspos"), MTIOCRDSPOS, 0, 1, 0 },
94 1.38 christos { CMD("retension"), MTIOCTOP, MTRETEN, 1, 0 },
95 1.38 christos { CMD("rewind"), MTIOCTOP, MTREW, 1, 0 },
96 1.38 christos { CMD("rewoffl"), MTIOCTOP, MTOFFL, 1, 0 },
97 1.38 christos { CMD("setblk"), MTIOCTOP, MTSETBSIZ, 1, 0 },
98 1.38 christos { CMD("setdensity"), MTIOCTOP, MTSETDNSTY, 1, 0 },
99 1.38 christos { CMD("sethpos"), MTIOCHLOCATE, 0, 1, 0 },
100 1.38 christos { CMD("setspos"), MTIOCSLOCATE, 0, 1, 0 },
101 1.38 christos { CMD("status"), MTIOCGET, MTNOP, 1, 0 },
102 1.38 christos { CMD("weof"), MTIOCTOP, MTWEOF, 0, 1 },
103 1.38 christos { CMD("eew"), MTIOCTOP, MTEWARN, 1, 0 },
104 1.43 christos { .c_name = NULL }
105 1.1 cgd };
106 1.1 cgd
107 1.36 wiz void printreg(const char *, u_int, const char *);
108 1.36 wiz void status(struct mtget *);
109 1.36 wiz void usage(void);
110 1.36 wiz int main(int, char *[]);
111 1.1 cgd
112 1.3 jtc int
113 1.36 wiz main(int argc, char *argv[])
114 1.1 cgd {
115 1.39 christos const struct commands *cp, *comp;
116 1.5 cgd struct mtget mt_status;
117 1.5 cgd struct mtop mt_com;
118 1.38 christos int ch, mtfd, flags;
119 1.27 mycroft char *p;
120 1.27 mycroft const char *tape;
121 1.24 veego int count;
122 1.39 christos size_t len;
123 1.1 cgd
124 1.36 wiz setprogname(argv[0]);
125 1.5 cgd if ((tape = getenv("TAPE")) == NULL)
126 1.19 lukem tape = _PATH_DEFTAPE;
127 1.5 cgd
128 1.5 cgd while ((ch = getopt(argc, argv, "f:t:")) != -1)
129 1.5 cgd switch (ch) {
130 1.3 jtc case 'f':
131 1.3 jtc case 't':
132 1.3 jtc tape = optarg;
133 1.3 jtc break;
134 1.5 cgd case '?':
135 1.3 jtc default:
136 1.3 jtc usage();
137 1.3 jtc }
138 1.3 jtc argc -= optind;
139 1.3 jtc argv += optind;
140 1.3 jtc
141 1.5 cgd if (argc < 1 || argc > 2)
142 1.3 jtc usage();
143 1.3 jtc
144 1.39 christos len = strlen(p = *argv++);
145 1.39 christos for (comp = NULL, cp = com; cp->c_name != NULL; cp++) {
146 1.39 christos size_t clen = MIN(len, cp->c_namelen);
147 1.39 christos if (strncmp(p, cp->c_name, clen) == 0) {
148 1.39 christos if (comp != NULL)
149 1.39 christos errx(1, "%s: Ambiguous command `%s' or `%s'?",
150 1.39 christos p, cp->c_name, comp->c_name);
151 1.39 christos else
152 1.39 christos comp = cp;
153 1.39 christos }
154 1.23 hannken }
155 1.39 christos if (comp == NULL)
156 1.39 christos errx(1, "%s: unknown command", p);
157 1.22 mjacob
158 1.23 hannken if (*argv) {
159 1.23 hannken count = strtol(*argv, &p, 10);
160 1.23 hannken if (count < comp->c_mincount || *p)
161 1.23 hannken errx(2, "%s: illegal count", *argv);
162 1.23 hannken } else
163 1.23 hannken count = 1;
164 1.12 scottr
165 1.23 hannken flags = comp->c_ronly ? O_RDONLY : O_WRONLY;
166 1.20 hannken
167 1.17 jtc if ((mtfd = open(tape, flags)) < 0)
168 1.7 pk err(2, "%s", tape);
169 1.16 jtc
170 1.23 hannken switch (comp->c_spcl) {
171 1.23 hannken case MTIOCTOP:
172 1.23 hannken if (comp->c_code == MTASF) {
173 1.35 mason
174 1.23 hannken /* If mtget.mt_fileno was implemented, We could
175 1.23 hannken compute the minimal seek needed to position
176 1.23 hannken the tape. Until then, rewind and seek from
177 1.23 hannken begining-of-tape */
178 1.23 hannken
179 1.23 hannken mt_com.mt_op = MTREW;
180 1.23 hannken mt_com.mt_count = 1;
181 1.23 hannken if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
182 1.23 hannken err(2, "%s", tape);
183 1.23 hannken
184 1.33 christos if (count > 0) {
185 1.33 christos mt_com.mt_op = MTFSF;
186 1.33 christos mt_com.mt_count = count;
187 1.33 christos if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
188 1.33 christos err(2, "%s", tape);
189 1.33 christos }
190 1.34 mason
191 1.22 mjacob } else {
192 1.23 hannken mt_com.mt_op = comp->c_code;
193 1.23 hannken mt_com.mt_count = count;
194 1.16 jtc
195 1.23 hannken if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
196 1.23 hannken err(2, "%s: %s", tape, comp->c_name);
197 1.16 jtc
198 1.23 hannken }
199 1.23 hannken break;
200 1.16 jtc
201 1.23 hannken case MTIOCGET:
202 1.17 jtc if (ioctl(mtfd, MTIOCGET, &mt_status) < 0)
203 1.17 jtc err(2, "%s: %s", tape, comp->c_name);
204 1.17 jtc status(&mt_status);
205 1.23 hannken break;
206 1.23 hannken
207 1.23 hannken case MTIOCRDSPOS:
208 1.23 hannken case MTIOCRDHPOS:
209 1.23 hannken if (ioctl(mtfd, comp->c_spcl, (caddr_t) &count) < 0)
210 1.23 hannken err(2, "%s", tape);
211 1.23 hannken printf("%s: block location %u\n", tape, (unsigned int) count);
212 1.23 hannken break;
213 1.23 hannken
214 1.23 hannken case MTIOCSLOCATE:
215 1.23 hannken case MTIOCHLOCATE:
216 1.23 hannken if (ioctl(mtfd, comp->c_spcl, (caddr_t) &count) < 0)
217 1.23 hannken err(2, "%s", tape);
218 1.23 hannken break;
219 1.23 hannken
220 1.23 hannken default:
221 1.45 dogcow errx(1, "internal error: unknown request %ld", comp->c_spcl);
222 1.1 cgd }
223 1.9 scottr
224 1.17 jtc exit(0);
225 1.5 cgd /* NOTREACHED */
226 1.1 cgd }
227 1.1 cgd
228 1.30 christos #if defined(sun) && !defined(__SVR4)
229 1.1 cgd #include <sundev/tmreg.h>
230 1.1 cgd #include <sundev/arreg.h>
231 1.1 cgd #endif
232 1.1 cgd
233 1.1 cgd #ifdef tahoe
234 1.1 cgd #include <tahoe/vba/cyreg.h>
235 1.1 cgd #endif
236 1.1 cgd
237 1.27 mycroft const struct tape_desc {
238 1.1 cgd short t_type; /* type of magtape device */
239 1.27 mycroft const char *t_name; /* printing name */
240 1.27 mycroft const char *t_dsbits; /* "drive status" register */
241 1.27 mycroft const char *t_erbits; /* "error" register */
242 1.1 cgd } tapes[] = {
243 1.30 christos #if defined(sun) && !defined(__SVR4)
244 1.1 cgd { MT_ISCPC, "TapeMaster", TMS_BITS, 0 },
245 1.1 cgd { MT_ISAR, "Archive", ARCH_CTRL_BITS, ARCH_BITS },
246 1.1 cgd #endif
247 1.1 cgd #ifdef tahoe
248 1.1 cgd { MT_ISCY, "cipher", CYS_BITS, CYCW_BITS },
249 1.1 cgd #endif
250 1.26 mjacob #define SCSI_DS_BITS "\20\5WriteProtect\2Mounted"
251 1.26 mjacob { 0x7, "SCSI", SCSI_DS_BITS, "76543210" },
252 1.43 christos { .t_type = 0 }
253 1.1 cgd };
254 1.1 cgd
255 1.26 mjacob
256 1.1 cgd /*
257 1.1 cgd * Interpret the status buffer returned
258 1.1 cgd */
259 1.5 cgd void
260 1.36 wiz status(struct mtget *bp)
261 1.1 cgd {
262 1.27 mycroft const struct tape_desc *mt;
263 1.1 cgd
264 1.5 cgd for (mt = tapes;; mt++) {
265 1.5 cgd if (mt->t_type == 0) {
266 1.5 cgd (void)printf("%d: unknown tape drive type\n",
267 1.5 cgd bp->mt_type);
268 1.5 cgd return;
269 1.5 cgd }
270 1.1 cgd if (mt->t_type == bp->mt_type)
271 1.1 cgd break;
272 1.1 cgd }
273 1.5 cgd (void)printf("%s tape drive, residual=%d\n", mt->t_name, bp->mt_resid);
274 1.1 cgd printreg("ds", bp->mt_dsreg, mt->t_dsbits);
275 1.1 cgd printreg("\ner", bp->mt_erreg, mt->t_erbits);
276 1.5 cgd (void)putchar('\n');
277 1.21 christos (void)printf("blocksize: %d (%d, %d, %d, %d)\n",
278 1.14 mrg bp->mt_blksiz, bp->mt_mblksiz[0], bp->mt_mblksiz[1],
279 1.14 mrg bp->mt_mblksiz[2], bp->mt_mblksiz[3]);
280 1.21 christos (void)printf("density: %d (%d, %d, %d, %d)\n",
281 1.14 mrg bp->mt_density, bp->mt_mdensity[0], bp->mt_mdensity[1],
282 1.14 mrg bp->mt_mdensity[2], bp->mt_mdensity[3]);
283 1.32 simonb (void)printf("current file number: %d\n", bp->mt_fileno);
284 1.32 simonb (void)printf("current block number: %d\n", bp->mt_blkno);
285 1.1 cgd }
286 1.1 cgd
287 1.1 cgd /*
288 1.5 cgd * Print a register a la the %b format of the kernel's printf.
289 1.1 cgd */
290 1.5 cgd void
291 1.36 wiz printreg(const char *s, u_int v, const char *bits)
292 1.1 cgd {
293 1.36 wiz int any, i;
294 1.18 tls char c;
295 1.1 cgd
296 1.36 wiz any = 0;
297 1.1 cgd if (bits && *bits == 8)
298 1.1 cgd printf("%s=%o", s, v);
299 1.1 cgd else
300 1.1 cgd printf("%s=%x", s, v);
301 1.42 christos if (v && bits && *++bits) {
302 1.1 cgd putchar('<');
303 1.11 scottr while ((i = *bits++)) {
304 1.1 cgd if (v & (1 << (i-1))) {
305 1.1 cgd if (any)
306 1.1 cgd putchar(',');
307 1.1 cgd any = 1;
308 1.1 cgd for (; (c = *bits) > 32; bits++)
309 1.1 cgd putchar(c);
310 1.1 cgd } else
311 1.1 cgd for (; *bits > 32; bits++)
312 1.1 cgd ;
313 1.1 cgd }
314 1.1 cgd putchar('>');
315 1.1 cgd }
316 1.3 jtc }
317 1.3 jtc
318 1.5 cgd void
319 1.36 wiz usage(void)
320 1.3 jtc {
321 1.40 hira (void)fprintf(stderr, "usage: %s [-f device] command [count]\n",
322 1.40 hira getprogname());
323 1.17 jtc exit(1);
324 1.28 mycroft /* NOTREACHED */
325 1.1 cgd }
326