xref: /freebsd/usr.sbin/fdread/fdread.c (revision c17d43407fe04133a94055b0dbc7ea8965654a9f)
1 /*
2  * Copyright (c) 2001 Joerg Wunsch
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <sys/fdcio.h>
32 
33 #include <err.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <paths.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <sysexits.h>
41 #include <unistd.h>
42 
43 #include <dev/ic/nec765.h>
44 
45 #include "fdutil.h"
46 
47 int	quiet, recover;
48 unsigned char fillbyte = 0xf0;	/* "foo" */
49 
50 int	doread(int fd, FILE *of, const char *devname);
51 int	doreadid(int fd, unsigned int numids, unsigned int trackno);
52 void	usage(void);
53 
54 void
55 usage(void)
56 {
57 
58 	errx(EX_USAGE,
59 	     "usage: fdread [-qr] [-d device] [-f fillbyte]\n"
60 	     "       fdread [-d device] -I numids [-t trackno]");
61 }
62 
63 
64 int
65 main(int argc, char **argv)
66 {
67 	int c, errs = 0;
68 	unsigned int numids = 0, trackno = 0;
69 	const char *fname = 0, *devname = "/dev/fd0";
70 	char *cp;
71 	FILE *of = stdout;
72 	int fd;
73 	unsigned long ul;
74 
75 	while ((c = getopt(argc, argv, "d:f:I:o:qrt:")) != -1)
76 		switch (c) {
77 		case 'd':
78 			devname = optarg;
79 			break;
80 
81 		case 'f':
82 			ul = strtoul(optarg, &cp, 0);
83 			if (*cp != '\0') {
84 				fprintf(stderr,
85 			"Bad argument %s to -f option; must be numeric\n",
86 					optarg);
87 				usage();
88 			}
89 			if (ul > 0xff)
90 				warnx(
91 			"Warning: fillbyte %#lx too large, truncating\n",
92 				      ul);
93 			fillbyte = ul & 0xff;
94 			break;
95 
96 		case 'I':
97 			ul = strtoul(optarg, &cp, 0);
98 			if (*cp != '\0') {
99 				fprintf(stderr,
100 			"Bad argument %s to -I option; must be numeric\n",
101 					optarg);
102 				usage();
103 			}
104 			numids = ul;
105 			break;
106 
107 		case 'o':
108 			fname = optarg;
109 			break;
110 
111 		case 'q':
112 			quiet++;
113 			break;
114 
115 		case 'r':
116 			recover++;
117 			break;
118 
119 		case 't':
120 			ul = strtoul(optarg, &cp, 0);
121 			if (*cp != '\0') {
122 				fprintf(stderr,
123 			"Bad argument %s to -t option; must be numeric\n",
124 					optarg);
125 				usage();
126 			}
127 			trackno = ul;
128 			break;
129 
130 		default:
131 			errs++;
132 		}
133 	argc -= optind;
134 	argv += optind;
135 
136 	if (argc != 0 || errs)
137 		usage();
138 	/* check for mutually exclusive options */
139 	if (numids) {
140 		if (fname || quiet || recover)
141 			usage();
142 	} else {
143 		if (trackno)
144 			usage();
145 	}
146 
147 	if (fname) {
148 		if ((of = fopen(fname, "w")) == NULL)
149 			err(EX_OSERR, "cannot create output file %s", fname);
150 	}
151 
152 	if ((fd = open(devname, O_RDONLY)) == -1)
153 		err(EX_OSERR, "cannot open device %s", devname);
154 
155 	return (numids? doreadid(fd, numids, trackno): doread(fd, of, devname));
156 }
157 
158 int
159 doread(int fd, FILE *of, const char *devname)
160 {
161 	char *trackbuf;
162 	int rv, fdopts, recoverable, nerrs = 0;
163 	unsigned int nbytes, tracksize, mediasize, secsize, n;
164 	struct fdc_status fdcs;
165 	struct fd_type fdt;
166 
167 	if (ioctl(fd, FD_GTYPE, &fdt) == -1)
168 		err(EX_OSERR, "ioctl(FD_GTYPE) failed -- not a floppy?");
169 	fdopts = FDOPT_NOERRLOG;
170 	if (ioctl(fd, FD_SOPTS, &fdopts) == -1)
171 		err(EX_OSERR, "ioctl(FD_SOPTS, FDOPT_NOERRLOG)");
172 
173 	secsize = 128 << fdt.secsize;
174 	tracksize = fdt.sectrac * secsize;
175 	mediasize = tracksize * fdt.tracks * fdt.heads;
176 	if ((trackbuf = malloc(tracksize)) == 0)
177 		errx(EX_TEMPFAIL, "out of memory");
178 
179 	if (!quiet)
180 		fprintf(stderr, "Reading %d * %d * %d * %d medium at %s\n",
181 			fdt.tracks, fdt.heads, fdt.sectrac, secsize, devname);
182 
183 	for (nbytes = 0; nbytes < mediasize;) {
184 		if (lseek(fd, nbytes, SEEK_SET) != nbytes)
185 			err(EX_OSERR, "cannot lseek()");
186 		rv = read(fd, trackbuf, tracksize);
187 		if (rv == 0) {
188 			/* EOF? */
189 			warnx("premature EOF after %u bytes", nbytes);
190 			return (EX_OK);
191 		}
192 		if (rv == tracksize) {
193 			nbytes += rv;
194 			if (!quiet)
195 				fprintf(stderr, "%5d KB\r", nbytes / 1024);
196 			fwrite(trackbuf, sizeof(unsigned char), rv, of);
197 			fflush(of);
198 			continue;
199 		}
200 		if (rv < tracksize) {
201 			/* should not happen */
202 			nbytes += rv;
203 			if (!quiet)
204 				fprintf(stderr, "\nshort after %5d KB\r",
205 					nbytes / 1024);
206 			fwrite(trackbuf, sizeof(unsigned char), rv, of);
207 			fflush(of);
208 			continue;
209 		}
210 		if (rv == -1) {
211 			/* fall back reading one sector at a time */
212 			for (n = 0; n < tracksize; n += secsize) {
213 				if (lseek(fd, nbytes, SEEK_SET) != nbytes)
214 					err(EX_OSERR, "cannot lseek()");
215 				rv = read(fd, trackbuf, secsize);
216 				if (rv == secsize) {
217 					nbytes += rv;
218 					if (!quiet)
219 						fprintf(stderr, "%5d KB\r",
220 							nbytes / 1024);
221 					fwrite(trackbuf, sizeof(unsigned char),
222 					       rv, of);
223 					fflush(of);
224 					continue;
225 				}
226 				if (rv == -1) {
227 					if (errno != EIO) {
228 						if (!quiet)
229 							putc('\n', stderr);
230 						perror("non-IO error");
231 						return (EX_OSERR);
232 					}
233 					if (ioctl(fd, FD_GSTAT, &fdcs) == -1)
234 						errx(EX_IOERR,
235 				     "floppy IO error, but no FDC status");
236 					nerrs++;
237 					recoverable = fdcs.status[2] &
238 						NE7_ST2_DD;
239 					if (!quiet) {
240 						printstatus(&fdcs, 0);
241 						fputs(" (", stderr);
242 						if (!recoverable)
243 							fputs("not ", stderr);
244 						fputs("recoverable)", stderr);
245 					}
246 					if (!recover) {
247 						if (!quiet)
248 							putc('\n', stderr);
249 						return (EX_IOERR);
250 					}
251 					memset(trackbuf, fillbyte, secsize);
252 					if (recoverable) {
253 						fdopts |= FDOPT_NOERROR;
254 						if (ioctl(fd, FD_SOPTS,
255 							  &fdopts) == -1)
256 							err(EX_OSERR,
257 				    "ioctl(fd, FD_SOPTS, FDOPT_NOERROR)");
258 						rv = read(fd, trackbuf,
259 							  secsize);
260 						if (rv != secsize)
261 							err(EX_IOERR,
262 				    "read() with FDOPT_NOERROR still fails");
263 						fdopts &= ~FDOPT_NOERROR;
264 						(void)ioctl(fd, FD_SOPTS,
265 							    &fdopts);
266 					}
267 					if (!quiet) {
268 						if (recoverable)
269 							fprintf(stderr,
270 								": recovered");
271 						else
272 							fprintf(stderr,
273 								": dummy");
274 						fprintf(stderr,
275 							" data @ %#x ... %#x\n",
276 							nbytes,
277 							nbytes + secsize - 1);
278 					}
279 					nbytes += secsize;
280 					fwrite(trackbuf, sizeof(unsigned char),
281 					       secsize, of);
282 					fflush(of);
283 					continue;
284 				}
285 				errx(EX_OSERR, "unexpected read() result: %d",
286 				     rv);
287 			}
288 		}
289 	}
290 	if (!quiet) {
291 		putc('\n', stderr);
292 		if (nerrs)
293 			fprintf(stderr, "%d error%s\n",
294 				nerrs, nerrs > 1? "s": "");
295 	}
296 
297 	return (nerrs? EX_IOERR: EX_OK);
298 }
299 
300 int
301 doreadid(int fd, unsigned int numids, unsigned int trackno)
302 {
303 	int rv = 0, status, fdopts;
304 	unsigned int i;
305 	struct fdc_readid info;
306 	struct fdc_status fdcs;
307 	struct fd_type fdt;
308 
309 	if (ioctl(fd, FD_GTYPE, &fdt) == -1)
310 		err(EX_OSERR, "ioctl(FD_GTYPE) failed -- not a floppy?");
311 
312 	fdopts = FDOPT_NOERRLOG;
313 	if (ioctl(fd, FD_SOPTS, &fdopts) == -1)
314 		err(EX_OSERR, "ioctl(FD_SOPTS, FDOPT_NOERRLOG)");
315 
316 	for (i = 0; i < numids; i++) {
317 		info.cyl = trackno / fdt.heads;
318 		info.head = fdt.heads > 1? trackno % fdt.heads: 0;
319 		if ((status = ioctl(fd, FD_READID, &info)) == 0) {
320 			printf("C = %d, H = %d, R = %d, N = %d\n",
321 			       info.cyl, info.head, info.sec, info.secshift);
322 		} else {
323 			if (errno != EIO) {
324 				perror("non-IO error");
325 				return (EX_OSERR);
326 			}
327 			if (ioctl(fd, FD_GSTAT, &fdcs) == -1)
328 				errx(EX_IOERR,
329 				     "floppy IO error, but no FDC status");
330 			printstatus(&fdcs, 0);
331 			putc('\n', stderr);
332 			rv = EX_IOERR;
333 		}
334 	}
335 
336 	return (rv);
337 }
338