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