xref: /freebsd/usr.sbin/fdread/fdread.c (revision 1b6c76a2fe091c74f08427e6c870851025a9cf67)
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 int	quiet, recover;
46 unsigned char fillbyte = 0xf0;	/* "foo" */
47 
48 int	doread(int fd, FILE *of, const char *devname);
49 int	doreadid(int fd, unsigned int numids, unsigned int trackno);
50 void	printstatus(struct fdc_status *fdcsp);
51 void	usage(void);
52 
53 void
54 usage(void)
55 {
56 
57 	errx(EX_USAGE,
58 	     "usage: fdread [-qr] [-d device] [-f fillbyte]\n"
59 	     "       fdread [-d device] -I numids [-t trackno]");
60 }
61 
62 
63 int
64 main(int argc, char **argv)
65 {
66 	int c, errs = 0;
67 	unsigned int numids = 0, trackno = 0;
68 	const char *fname = 0, *devname = "/dev/fd0";
69 	char *cp;
70 	FILE *of = stdout;
71 	int fd;
72 	unsigned long ul;
73 
74 	while ((c = getopt(argc, argv, "d:f:I:o:qrt:")) != -1)
75 		switch (c) {
76 		case 'd':
77 			devname = optarg;
78 			break;
79 
80 		case 'f':
81 			ul = strtoul(optarg, &cp, 0);
82 			if (*cp != '\0') {
83 				fprintf(stderr,
84 			"Bad argument %s to -f option; must be numeric\n",
85 					optarg);
86 				usage();
87 			}
88 			if (ul > 0xff)
89 				warnx(
90 			"Warning: fillbyte %#lx too large, truncating\n",
91 				      ul);
92 			fillbyte = ul & 0xff;
93 			break;
94 
95 		case 'I':
96 			ul = strtoul(optarg, &cp, 0);
97 			if (*cp != '\0') {
98 				fprintf(stderr,
99 			"Bad argument %s to -I option; must be numeric\n",
100 					optarg);
101 				usage();
102 			}
103 			numids = ul;
104 			break;
105 
106 		case 'o':
107 			fname = optarg;
108 			break;
109 
110 		case 'q':
111 			quiet++;
112 			break;
113 
114 		case 'r':
115 			recover++;
116 			break;
117 
118 		case 't':
119 			ul = strtoul(optarg, &cp, 0);
120 			if (*cp != '\0') {
121 				fprintf(stderr,
122 			"Bad argument %s to -t option; must be numeric\n",
123 					optarg);
124 				usage();
125 			}
126 			trackno = ul;
127 			break;
128 
129 		default:
130 			errs++;
131 		}
132 	argc -= optind;
133 	argv += optind;
134 
135 	if (argc != 0 || errs)
136 		usage();
137 	/* check for mutually exclusive options */
138 	if (numids) {
139 		if (fname || quiet || recover)
140 			usage();
141 	} else {
142 		if (trackno)
143 			usage();
144 	}
145 
146 	if (fname) {
147 		if ((of = fopen(fname, "w")) == NULL)
148 			err(EX_OSERR, "cannot create output file %s", fname);
149 	}
150 
151 	if ((fd = open(devname, O_RDONLY)) == -1)
152 		err(EX_OSERR, "cannot open device %s", devname);
153 
154 	return (numids? doreadid(fd, numids, trackno): doread(fd, of, devname));
155 }
156 
157 int
158 doread(int fd, FILE *of, const char *devname)
159 {
160 	char *trackbuf;
161 	int rv, fdopts, recoverable, nerrs = 0;
162 	unsigned int nbytes, tracksize, mediasize, secsize, n;
163 	struct fdc_status fdcs;
164 	struct fd_type fdt;
165 
166 	if (ioctl(fd, FD_GTYPE, &fdt) == -1)
167 		err(EX_OSERR, "ioctl(FD_GTYPE) failed -- not a floppy?");
168 	fdopts = FDOPT_NOERRLOG;
169 	if (ioctl(fd, FD_SOPTS, &fdopts) == -1)
170 		err(EX_OSERR, "ioctl(FD_SOPTS, FDOPT_NOERRLOG)");
171 
172 	secsize = 128 << fdt.secsize;
173 	tracksize = fdt.sectrac * secsize;
174 	mediasize = tracksize * fdt.tracks * fdt.heads;
175 	if ((trackbuf = malloc(tracksize)) == 0)
176 		errx(EX_TEMPFAIL, "out of memory");
177 
178 	if (!quiet)
179 		fprintf(stderr, "Reading %d * %d * %d * %d medium at %s\n",
180 			fdt.tracks, fdt.heads, fdt.sectrac, secsize, devname);
181 
182 	for (nbytes = 0; nbytes < mediasize;) {
183 		if (lseek(fd, nbytes, SEEK_SET) != nbytes)
184 			err(EX_OSERR, "cannot lseek()");
185 		rv = read(fd, trackbuf, tracksize);
186 		if (rv == 0) {
187 			/* EOF? */
188 			warnx("premature EOF after %u bytes", nbytes);
189 			return (EX_OK);
190 		}
191 		if (rv == tracksize) {
192 			nbytes += rv;
193 			if (!quiet)
194 				fprintf(stderr, "%5d KB\r", nbytes / 1024);
195 			fwrite(trackbuf, sizeof(unsigned char), rv, of);
196 			fflush(of);
197 			continue;
198 		}
199 		if (rv < tracksize) {
200 			/* should not happen */
201 			nbytes += rv;
202 			if (!quiet)
203 				fprintf(stderr, "\nshort after %5d KB\r",
204 					nbytes / 1024);
205 			fwrite(trackbuf, sizeof(unsigned char), rv, of);
206 			fflush(of);
207 			continue;
208 		}
209 		if (rv == -1) {
210 			/* fall back reading one sector at a time */
211 			for (n = 0; n < tracksize; n += secsize) {
212 				if (lseek(fd, nbytes, SEEK_SET) != nbytes)
213 					err(EX_OSERR, "cannot lseek()");
214 				rv = read(fd, trackbuf, secsize);
215 				if (rv == secsize) {
216 					nbytes += rv;
217 					if (!quiet)
218 						fprintf(stderr, "%5d KB\r",
219 							nbytes / 1024);
220 					fwrite(trackbuf, sizeof(unsigned char),
221 					       rv, of);
222 					fflush(of);
223 					continue;
224 				}
225 				if (rv == -1) {
226 					if (errno != EIO) {
227 						if (!quiet)
228 							putc('\n', stderr);
229 						perror("non-IO error");
230 						return (EX_OSERR);
231 					}
232 					if (ioctl(fd, FD_GSTAT, &fdcs) == -1)
233 						errx(EX_IOERR,
234 				     "floppy IO error, but no FDC status");
235 					nerrs++;
236 					recoverable = fdcs.status[2] &
237 						NE7_ST2_DD;
238 					if (!quiet) {
239 						printstatus(&fdcs);
240 						fputs(" (", stderr);
241 						if (!recoverable)
242 							fputs("not ", stderr);
243 						fputs("recoverable)", stderr);
244 					}
245 					if (!recover) {
246 						if (!quiet)
247 							putc('\n', stderr);
248 						return (EX_IOERR);
249 					}
250 					memset(trackbuf, fillbyte, secsize);
251 					if (recoverable) {
252 						fdopts |= FDOPT_NOERROR;
253 						if (ioctl(fd, FD_SOPTS,
254 							  &fdopts) == -1)
255 							err(EX_OSERR,
256 				    "ioctl(fd, FD_SOPTS, FDOPT_NOERROR)");
257 						rv = read(fd, trackbuf,
258 							  secsize);
259 						if (rv != secsize)
260 							err(EX_IOERR,
261 				    "read() with FDOPT_NOERROR still fails");
262 						fdopts &= ~FDOPT_NOERROR;
263 						(void)ioctl(fd, FD_SOPTS,
264 							    &fdopts);
265 					}
266 					if (!quiet) {
267 						if (recoverable)
268 							fprintf(stderr,
269 								": recovered");
270 						else
271 							fprintf(stderr,
272 								": dummy");
273 						fprintf(stderr,
274 							" data @ %#x ... %#x\n",
275 							nbytes,
276 							nbytes + secsize - 1);
277 					}
278 					nbytes += secsize;
279 					fwrite(trackbuf, sizeof(unsigned char),
280 					       secsize, of);
281 					fflush(of);
282 					continue;
283 				}
284 				errx(EX_OSERR, "unexpected read() result: %d",
285 				     rv);
286 			}
287 		}
288 	}
289 	if (!quiet) {
290 		putc('\n', stderr);
291 		if (nerrs)
292 			fprintf(stderr, "%d error%s\n",
293 				nerrs, nerrs > 1? "s": "");
294 	}
295 
296 	return (nerrs? EX_IOERR: EX_OK);
297 }
298 
299 void
300 printstatus(struct fdc_status *fdcsp)
301 {
302 	char msgbuf[100];
303 
304 	fprintf(stderr,
305 		"\nFDC status ST0=%#x ST1=%#x ST2=%#x C=%u H=%u R=%u N=%u:\n",
306 		fdcsp->status[0] & 0xff,
307 		fdcsp->status[1] & 0xff,
308 		fdcsp->status[2] & 0xff,
309 		fdcsp->status[3] & 0xff,
310 		fdcsp->status[4] & 0xff,
311 		fdcsp->status[5] & 0xff,
312 		fdcsp->status[6] & 0xff);
313 
314 	if ((fdcsp->status[0] & NE7_ST0_IC_RC) != NE7_ST0_IC_AT) {
315 		sprintf(msgbuf, "unexcpted interrupt code %#x",
316 			fdcsp->status[0] & NE7_ST0_IC_RC);
317 	} else {
318 		strcpy(msgbuf, "unexpected error code in ST1/ST2");
319 
320 		if (fdcsp->status[1] & NE7_ST1_EN)
321 			strcpy(msgbuf, "end of cylinder (wrong format)");
322 		else if (fdcsp->status[1] & NE7_ST1_DE) {
323 			if (fdcsp->status[2] & NE7_ST2_DD)
324 				strcpy(msgbuf, "CRC error in data field");
325 			else
326 				strcpy(msgbuf, "CRC error in ID field");
327 		} else if (fdcsp->status[1] & NE7_ST1_MA) {
328 			if (fdcsp->status[2] & NE7_ST2_MD)
329 				strcpy(msgbuf, "no address mark in data field");
330 			else
331 				strcpy(msgbuf, "no address mark in ID field");
332 		} else if (fdcsp->status[2] & NE7_ST2_WC)
333 			strcpy(msgbuf, "wrong cylinder (format mismatch)");
334 		else if (fdcsp->status[1] & NE7_ST1_ND)
335 			strcpy(msgbuf, "no data (sector not found)");
336 	}
337 	fputs(msgbuf, stderr);
338 }
339 
340 int
341 doreadid(int fd, unsigned int numids, unsigned int trackno)
342 {
343 	int rv = 0, status, fdopts;
344 	unsigned int i;
345 	struct fdc_readid info;
346 	struct fdc_status fdcs;
347 	struct fd_type fdt;
348 
349 	if (ioctl(fd, FD_GTYPE, &fdt) == -1)
350 		err(EX_OSERR, "ioctl(FD_GTYPE) failed -- not a floppy?");
351 
352 	fdopts = FDOPT_NOERRLOG;
353 	if (ioctl(fd, FD_SOPTS, &fdopts) == -1)
354 		err(EX_OSERR, "ioctl(FD_SOPTS, FDOPT_NOERRLOG)");
355 
356 	for (i = 0; i < numids; i++) {
357 		info.cyl = trackno / fdt.heads;
358 		info.head = fdt.heads > 1? trackno % fdt.heads: 0;
359 		if ((status = ioctl(fd, FD_READID, &info)) == 0) {
360 			printf("C = %d, H = %d, R = %d, N = %d\n",
361 			       info.cyl, info.head, info.sec, info.secshift);
362 		} else {
363 			if (errno != EIO) {
364 				perror("non-IO error");
365 				return (EX_OSERR);
366 			}
367 			if (ioctl(fd, FD_GSTAT, &fdcs) == -1)
368 				errx(EX_IOERR,
369 				     "floppy IO error, but no FDC status");
370 			printstatus(&fdcs);
371 			putc('\n', stderr);
372 			rv = EX_IOERR;
373 		}
374 	}
375 
376 	return (rv);
377 }
378