mt.c revision 1.38 1 1.38 christos /* $NetBSD: mt.c,v 1.38 2005/02/03 00:03:02 christos 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.38 christos __RCSID("$NetBSD: mt.c,v 1.38 2005/02/03 00:03:02 christos 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.1 cgd #include <sys/ioctl.h>
52 1.1 cgd #include <sys/mtio.h>
53 1.12 scottr #include <sys/stat.h>
54 1.12 scottr
55 1.12 scottr #include <ctype.h>
56 1.12 scottr #include <err.h>
57 1.1 cgd #include <fcntl.h>
58 1.19 lukem #include <paths.h>
59 1.36 wiz #include <rmt.h>
60 1.12 scottr #include <stdio.h>
61 1.5 cgd #include <stdlib.h>
62 1.3 jtc #include <string.h>
63 1.11 scottr #include <unistd.h>
64 1.25 thorpej
65 1.16 jtc /* pseudo ioctl constants */
66 1.16 jtc #define MTASF 100
67 1.16 jtc
68 1.1 cgd struct commands {
69 1.20 hannken const char *c_name; /* command */
70 1.38 christos size_t c_namelen; /* command len */
71 1.23 hannken int c_spcl; /* ioctl request */
72 1.23 hannken int c_code; /* ioctl code for MTIOCTOP command */
73 1.20 hannken int c_ronly; /* open tape read-only */
74 1.20 hannken int c_mincount; /* min allowed count value */
75 1.16 jtc };
76 1.16 jtc
77 1.38 christos #define CMD(a) a, sizeof(a) - 1
78 1.16 jtc const struct commands com[] = {
79 1.38 christos { CMD("asf"), MTIOCTOP, MTASF, 1, 0 },
80 1.38 christos { CMD("blocksize"), MTIOCTOP, MTSETBSIZ, 1, 0 },
81 1.38 christos { CMD("bsf"), MTIOCTOP, MTBSF, 1, 1 },
82 1.38 christos { CMD("bsr"), MTIOCTOP, MTBSR, 1, 1 },
83 1.38 christos { CMD("compress"), MTIOCTOP, MTCMPRESS, 1, 0 },
84 1.38 christos { CMD("density"), MTIOCTOP, MTSETDNSTY, 1, 0 },
85 1.38 christos { CMD("eof"), MTIOCTOP, MTWEOF, 0, 1 },
86 1.38 christos { CMD("eom"), MTIOCTOP, MTEOM, 1, 0 },
87 1.38 christos { CMD("erase"), MTIOCTOP, MTERASE, 0, 0 },
88 1.38 christos { CMD("fsf"), MTIOCTOP, MTFSF, 1, 1 },
89 1.38 christos { CMD("fsr"), MTIOCTOP, MTFSR, 1, 1 },
90 1.38 christos { CMD("offline"), MTIOCTOP, MTOFFL, 1, 0 },
91 1.38 christos { CMD("rdhpos"), MTIOCRDHPOS, 0, 1, 0 },
92 1.38 christos { CMD("rdspos"), MTIOCRDSPOS, 0, 1, 0 },
93 1.38 christos { CMD("retension"), MTIOCTOP, MTRETEN, 1, 0 },
94 1.38 christos { CMD("rewind"), MTIOCTOP, MTREW, 1, 0 },
95 1.38 christos { CMD("rewoffl"), MTIOCTOP, MTOFFL, 1, 0 },
96 1.38 christos { CMD("setblk"), MTIOCTOP, MTSETBSIZ, 1, 0 },
97 1.38 christos { CMD("setdensity"), MTIOCTOP, MTSETDNSTY, 1, 0 },
98 1.38 christos { CMD("sethpos"), MTIOCHLOCATE, 0, 1, 0 },
99 1.38 christos { CMD("setspos"), MTIOCSLOCATE, 0, 1, 0 },
100 1.38 christos { CMD("status"), MTIOCGET, MTNOP, 1, 0 },
101 1.38 christos { CMD("weof"), MTIOCTOP, MTWEOF, 0, 1 },
102 1.38 christos { CMD("eew"), MTIOCTOP, MTEWARN, 1, 0 },
103 1.5 cgd { NULL }
104 1.1 cgd };
105 1.1 cgd
106 1.36 wiz void printreg(const char *, u_int, const char *);
107 1.36 wiz void status(struct mtget *);
108 1.36 wiz void usage(void);
109 1.36 wiz int main(int, char *[]);
110 1.1 cgd
111 1.3 jtc int
112 1.36 wiz main(int argc, char *argv[])
113 1.1 cgd {
114 1.22 mjacob const struct commands *comp = (const struct commands *) NULL;
115 1.5 cgd struct mtget mt_status;
116 1.5 cgd struct mtop mt_com;
117 1.38 christos int ch, mtfd, flags;
118 1.27 mycroft char *p;
119 1.27 mycroft const char *tape;
120 1.24 veego int count;
121 1.1 cgd
122 1.36 wiz setprogname(argv[0]);
123 1.5 cgd if ((tape = getenv("TAPE")) == NULL)
124 1.19 lukem tape = _PATH_DEFTAPE;
125 1.5 cgd
126 1.5 cgd while ((ch = getopt(argc, argv, "f:t:")) != -1)
127 1.5 cgd switch (ch) {
128 1.3 jtc case 'f':
129 1.3 jtc case 't':
130 1.3 jtc tape = optarg;
131 1.3 jtc break;
132 1.5 cgd case '?':
133 1.3 jtc default:
134 1.3 jtc usage();
135 1.3 jtc }
136 1.3 jtc argc -= optind;
137 1.3 jtc argv += optind;
138 1.3 jtc
139 1.5 cgd if (argc < 1 || argc > 2)
140 1.3 jtc usage();
141 1.3 jtc
142 1.38 christos p = *argv++;
143 1.23 hannken for (comp = com;; comp++) {
144 1.23 hannken if (comp->c_name == NULL)
145 1.23 hannken errx(1, "%s: unknown command", p);
146 1.38 christos if (strncmp(p, comp->c_name, comp->c_namelen) == 0)
147 1.23 hannken break;
148 1.23 hannken }
149 1.22 mjacob
150 1.23 hannken if (*argv) {
151 1.23 hannken count = strtol(*argv, &p, 10);
152 1.23 hannken if (count < comp->c_mincount || *p)
153 1.23 hannken errx(2, "%s: illegal count", *argv);
154 1.23 hannken } else
155 1.23 hannken count = 1;
156 1.12 scottr
157 1.23 hannken flags = comp->c_ronly ? O_RDONLY : O_WRONLY;
158 1.20 hannken
159 1.17 jtc if ((mtfd = open(tape, flags)) < 0)
160 1.7 pk err(2, "%s", tape);
161 1.16 jtc
162 1.23 hannken switch (comp->c_spcl) {
163 1.23 hannken case MTIOCTOP:
164 1.23 hannken if (comp->c_code == MTASF) {
165 1.35 mason
166 1.23 hannken /* If mtget.mt_fileno was implemented, We could
167 1.23 hannken compute the minimal seek needed to position
168 1.23 hannken the tape. Until then, rewind and seek from
169 1.23 hannken begining-of-tape */
170 1.23 hannken
171 1.23 hannken mt_com.mt_op = MTREW;
172 1.23 hannken mt_com.mt_count = 1;
173 1.23 hannken if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
174 1.23 hannken err(2, "%s", tape);
175 1.23 hannken
176 1.33 christos if (count > 0) {
177 1.33 christos mt_com.mt_op = MTFSF;
178 1.33 christos mt_com.mt_count = count;
179 1.33 christos if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
180 1.33 christos err(2, "%s", tape);
181 1.33 christos }
182 1.34 mason
183 1.22 mjacob } else {
184 1.23 hannken mt_com.mt_op = comp->c_code;
185 1.23 hannken mt_com.mt_count = count;
186 1.16 jtc
187 1.23 hannken if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0)
188 1.23 hannken err(2, "%s: %s", tape, comp->c_name);
189 1.16 jtc
190 1.23 hannken }
191 1.23 hannken break;
192 1.16 jtc
193 1.23 hannken case MTIOCGET:
194 1.17 jtc if (ioctl(mtfd, MTIOCGET, &mt_status) < 0)
195 1.17 jtc err(2, "%s: %s", tape, comp->c_name);
196 1.17 jtc status(&mt_status);
197 1.23 hannken break;
198 1.23 hannken
199 1.23 hannken case MTIOCRDSPOS:
200 1.23 hannken case MTIOCRDHPOS:
201 1.23 hannken if (ioctl(mtfd, comp->c_spcl, (caddr_t) &count) < 0)
202 1.23 hannken err(2, "%s", tape);
203 1.23 hannken printf("%s: block location %u\n", tape, (unsigned int) count);
204 1.23 hannken break;
205 1.23 hannken
206 1.23 hannken case MTIOCSLOCATE:
207 1.23 hannken case MTIOCHLOCATE:
208 1.23 hannken if (ioctl(mtfd, comp->c_spcl, (caddr_t) &count) < 0)
209 1.23 hannken err(2, "%s", tape);
210 1.23 hannken break;
211 1.23 hannken
212 1.23 hannken default:
213 1.23 hannken errx(1, "internal error: unknown request %d", comp->c_spcl);
214 1.1 cgd }
215 1.9 scottr
216 1.17 jtc exit(0);
217 1.5 cgd /* NOTREACHED */
218 1.1 cgd }
219 1.1 cgd
220 1.30 christos #if defined(sun) && !defined(__SVR4)
221 1.1 cgd #include <sundev/tmreg.h>
222 1.1 cgd #include <sundev/arreg.h>
223 1.1 cgd #endif
224 1.1 cgd
225 1.1 cgd #ifdef tahoe
226 1.1 cgd #include <tahoe/vba/cyreg.h>
227 1.1 cgd #endif
228 1.1 cgd
229 1.27 mycroft const struct tape_desc {
230 1.1 cgd short t_type; /* type of magtape device */
231 1.27 mycroft const char *t_name; /* printing name */
232 1.27 mycroft const char *t_dsbits; /* "drive status" register */
233 1.27 mycroft const char *t_erbits; /* "error" register */
234 1.1 cgd } tapes[] = {
235 1.30 christos #if defined(sun) && !defined(__SVR4)
236 1.1 cgd { MT_ISCPC, "TapeMaster", TMS_BITS, 0 },
237 1.1 cgd { MT_ISAR, "Archive", ARCH_CTRL_BITS, ARCH_BITS },
238 1.1 cgd #endif
239 1.1 cgd #ifdef tahoe
240 1.1 cgd { MT_ISCY, "cipher", CYS_BITS, CYCW_BITS },
241 1.1 cgd #endif
242 1.26 mjacob #define SCSI_DS_BITS "\20\5WriteProtect\2Mounted"
243 1.26 mjacob { 0x7, "SCSI", SCSI_DS_BITS, "76543210" },
244 1.1 cgd { 0 }
245 1.1 cgd };
246 1.1 cgd
247 1.26 mjacob
248 1.1 cgd /*
249 1.1 cgd * Interpret the status buffer returned
250 1.1 cgd */
251 1.5 cgd void
252 1.36 wiz status(struct mtget *bp)
253 1.1 cgd {
254 1.27 mycroft const struct tape_desc *mt;
255 1.1 cgd
256 1.5 cgd for (mt = tapes;; mt++) {
257 1.5 cgd if (mt->t_type == 0) {
258 1.5 cgd (void)printf("%d: unknown tape drive type\n",
259 1.5 cgd bp->mt_type);
260 1.5 cgd return;
261 1.5 cgd }
262 1.1 cgd if (mt->t_type == bp->mt_type)
263 1.1 cgd break;
264 1.1 cgd }
265 1.5 cgd (void)printf("%s tape drive, residual=%d\n", mt->t_name, bp->mt_resid);
266 1.1 cgd printreg("ds", bp->mt_dsreg, mt->t_dsbits);
267 1.1 cgd printreg("\ner", bp->mt_erreg, mt->t_erbits);
268 1.5 cgd (void)putchar('\n');
269 1.21 christos (void)printf("blocksize: %d (%d, %d, %d, %d)\n",
270 1.14 mrg bp->mt_blksiz, bp->mt_mblksiz[0], bp->mt_mblksiz[1],
271 1.14 mrg bp->mt_mblksiz[2], bp->mt_mblksiz[3]);
272 1.21 christos (void)printf("density: %d (%d, %d, %d, %d)\n",
273 1.14 mrg bp->mt_density, bp->mt_mdensity[0], bp->mt_mdensity[1],
274 1.14 mrg bp->mt_mdensity[2], bp->mt_mdensity[3]);
275 1.32 simonb (void)printf("current file number: %d\n", bp->mt_fileno);
276 1.32 simonb (void)printf("current block number: %d\n", bp->mt_blkno);
277 1.1 cgd }
278 1.1 cgd
279 1.1 cgd /*
280 1.5 cgd * Print a register a la the %b format of the kernel's printf.
281 1.1 cgd */
282 1.5 cgd void
283 1.36 wiz printreg(const char *s, u_int v, const char *bits)
284 1.1 cgd {
285 1.36 wiz int any, i;
286 1.18 tls char c;
287 1.1 cgd
288 1.36 wiz any = 0;
289 1.1 cgd if (bits && *bits == 8)
290 1.1 cgd printf("%s=%o", s, v);
291 1.1 cgd else
292 1.1 cgd printf("%s=%x", s, v);
293 1.1 cgd bits++;
294 1.26 mjacob if (v && *bits) {
295 1.1 cgd putchar('<');
296 1.11 scottr while ((i = *bits++)) {
297 1.1 cgd if (v & (1 << (i-1))) {
298 1.1 cgd if (any)
299 1.1 cgd putchar(',');
300 1.1 cgd any = 1;
301 1.1 cgd for (; (c = *bits) > 32; bits++)
302 1.1 cgd putchar(c);
303 1.1 cgd } else
304 1.1 cgd for (; *bits > 32; bits++)
305 1.1 cgd ;
306 1.1 cgd }
307 1.1 cgd putchar('>');
308 1.1 cgd }
309 1.3 jtc }
310 1.3 jtc
311 1.5 cgd void
312 1.36 wiz usage(void)
313 1.3 jtc {
314 1.36 wiz (void)fprintf(stderr, "usage: %s [-f device] command [ count ]\n", getprogname());
315 1.17 jtc exit(1);
316 1.28 mycroft /* NOTREACHED */
317 1.1 cgd }
318