xref: /freebsd/usr.sbin/fdread/fdread.c (revision 02e9120893770924227138ba49df1edb3896112a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 
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 static int	quiet, recover;
48 static unsigned char fillbyte = 0xf0;	/* "foo" */
49 
50 static int	doread(int fd, FILE *of, const char *_devname);
51 static int	doreadid(int fd, unsigned int numids, unsigned int trackno);
52 static void	usage(void);
53 
54 static 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 static 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 
170 	secsize = 128 << fdt.secsize;
171 	tracksize = fdt.sectrac * secsize;
172 	mediasize = tracksize * fdt.tracks * fdt.heads;
173 	if ((trackbuf = malloc(tracksize)) == NULL)
174 		errx(EX_TEMPFAIL, "out of memory");
175 
176 	if (!quiet)
177 		fprintf(stderr, "Reading %d * %d * %d * %d medium at %s\n",
178 			fdt.tracks, fdt.heads, fdt.sectrac, secsize, _devname);
179 
180 	for (nbytes = 0; nbytes < mediasize;) {
181 		if (lseek(fd, nbytes, SEEK_SET) != nbytes)
182 			err(EX_OSERR, "cannot lseek()");
183 		rv = read(fd, trackbuf, tracksize);
184 		if (rv == 0) {
185 			/* EOF? */
186 			warnx("premature EOF after %u bytes", nbytes);
187 			free(trackbuf);
188 			return (EX_OK);
189 		}
190 		if ((unsigned)rv == tracksize) {
191 			nbytes += rv;
192 			if (!quiet)
193 				fprintf(stderr, "%5d KB\r", nbytes / 1024);
194 			fwrite(trackbuf, sizeof(unsigned char), rv, of);
195 			fflush(of);
196 			continue;
197 		}
198 		if (rv == -1) {
199 			/* fall back reading one sector at a time */
200 			for (n = 0; n < tracksize; n += secsize) {
201 				if (lseek(fd, nbytes, SEEK_SET) != nbytes)
202 					err(EX_OSERR, "cannot lseek()");
203 				rv = read(fd, trackbuf, secsize);
204 				if ((unsigned) rv == secsize) {
205 					nbytes += rv;
206 					if (!quiet)
207 						fprintf(stderr, "%5d KB\r",
208 							nbytes / 1024);
209 					fwrite(trackbuf, sizeof(unsigned char),
210 					       rv, of);
211 					fflush(of);
212 					continue;
213 				}
214 				if (rv == -1) {
215 					if (errno != EIO) {
216 						if (!quiet)
217 							putc('\n', stderr);
218 						perror("non-IO error");
219 						free(trackbuf);
220 						return (EX_OSERR);
221 					}
222 					if (ioctl(fd, FD_GSTAT, &fdcs) == -1)
223 						errx(EX_IOERR,
224 				     "floppy IO error, but no FDC status");
225 					nerrs++;
226 					recoverable = fdcs.status[2] &
227 						NE7_ST2_DD;
228 					if (!quiet) {
229 						printstatus(&fdcs, 0);
230 						fputs(" (", stderr);
231 						if (!recoverable)
232 							fputs("not ", stderr);
233 						fputs("recoverable)", stderr);
234 					}
235 					if (!recover) {
236 						if (!quiet)
237 							putc('\n', stderr);
238 						free(trackbuf);
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 	free(trackbuf);
291 	if (!quiet) {
292 		putc('\n', stderr);
293 		if (nerrs)
294 			fprintf(stderr, "%d error%s\n",
295 				nerrs, nerrs > 1? "s": "");
296 	}
297 
298 	return (nerrs? EX_IOERR: EX_OK);
299 }
300 
301 static int
302 doreadid(int fd, unsigned int numids, unsigned int trackno)
303 {
304 	int rv = 0;
305 	unsigned int i;
306 	struct fdc_readid info;
307 	struct fdc_status fdcs;
308 	struct fd_type fdt;
309 
310 	if (ioctl(fd, FD_GTYPE, &fdt) == -1)
311 		err(EX_OSERR, "ioctl(FD_GTYPE) failed -- not a floppy?");
312 
313 	for (i = 0; i < numids; i++) {
314 		info.cyl = trackno / fdt.heads;
315 		info.head = fdt.heads > 1? trackno % fdt.heads: 0;
316 		if (ioctl(fd, FD_READID, &info) == 0) {
317 			printf("C = %d, H = %d, R = %d, N = %d\n",
318 			       info.cyl, info.head, info.sec, info.secshift);
319 		} else {
320 			if (errno != EIO) {
321 				perror("non-IO error");
322 				return (EX_OSERR);
323 			}
324 			if (ioctl(fd, FD_GSTAT, &fdcs) == -1)
325 				errx(EX_IOERR,
326 				     "floppy IO error, but no FDC status");
327 			printstatus(&fdcs, 0);
328 			putc('\n', stderr);
329 			rv = EX_IOERR;
330 		}
331 	}
332 
333 	return (rv);
334 }
335