xref: /freebsd/usr.sbin/fdread/fdread.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
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_RDWR)) == -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 ((unsigned)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 == -1) {
201 			/* fall back reading one sector at a time */
202 			for (n = 0; n < tracksize; n += secsize) {
203 				if (lseek(fd, nbytes, SEEK_SET) != nbytes)
204 					err(EX_OSERR, "cannot lseek()");
205 				rv = read(fd, trackbuf, secsize);
206 				if ((unsigned) rv == secsize) {
207 					nbytes += rv;
208 					if (!quiet)
209 						fprintf(stderr, "%5d KB\r",
210 							nbytes / 1024);
211 					fwrite(trackbuf, sizeof(unsigned char),
212 					       rv, of);
213 					fflush(of);
214 					continue;
215 				}
216 				if (rv == -1) {
217 					if (errno != EIO) {
218 						if (!quiet)
219 							putc('\n', stderr);
220 						perror("non-IO error");
221 						return (EX_OSERR);
222 					}
223 					if (ioctl(fd, FD_GSTAT, &fdcs) == -1)
224 						errx(EX_IOERR,
225 				     "floppy IO error, but no FDC status");
226 					nerrs++;
227 					recoverable = fdcs.status[2] &
228 						NE7_ST2_DD;
229 					if (!quiet) {
230 						printstatus(&fdcs, 0);
231 						fputs(" (", stderr);
232 						if (!recoverable)
233 							fputs("not ", stderr);
234 						fputs("recoverable)", stderr);
235 					}
236 					if (!recover) {
237 						if (!quiet)
238 							putc('\n', stderr);
239 						return (EX_IOERR);
240 					}
241 					memset(trackbuf, fillbyte, secsize);
242 					if (recoverable) {
243 						fdopts |= FDOPT_NOERROR;
244 						if (ioctl(fd, FD_SOPTS,
245 							  &fdopts) == -1)
246 							err(EX_OSERR,
247 				    "ioctl(fd, FD_SOPTS, FDOPT_NOERROR)");
248 						rv = read(fd, trackbuf,
249 							  secsize);
250 						if ((unsigned)rv != secsize)
251 							err(EX_IOERR,
252 				    "read() with FDOPT_NOERROR still fails");
253 						fdopts &= ~FDOPT_NOERROR;
254 						(void)ioctl(fd, FD_SOPTS,
255 							    &fdopts);
256 					}
257 					if (!quiet) {
258 						if (recoverable)
259 							fprintf(stderr,
260 								": recovered");
261 						else
262 							fprintf(stderr,
263 								": dummy");
264 						fprintf(stderr,
265 							" data @ %#x ... %#x\n",
266 							nbytes,
267 							nbytes + secsize - 1);
268 					}
269 					nbytes += secsize;
270 					fwrite(trackbuf, sizeof(unsigned char),
271 					       secsize, of);
272 					fflush(of);
273 					continue;
274 				}
275 				errx(EX_OSERR, "unexpected read() result: %d",
276 				     rv);
277 			}
278 		}
279 		if ((unsigned)rv < tracksize) {
280 			/* should not happen */
281 			nbytes += rv;
282 			if (!quiet)
283 				fprintf(stderr, "\nshort after %5d KB\r",
284 					nbytes / 1024);
285 			fwrite(trackbuf, sizeof(unsigned char), rv, of);
286 			fflush(of);
287 			continue;
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, 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 (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