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: releng/11.0/usr.sbin/fdread/fdread.c 297962 2016-04-14 12:46:46Z araujo $
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
47static int	quiet, recover;
48static unsigned char fillbyte = 0xf0;	/* "foo" */
49
50static int	doread(int fd, FILE *of, const char *_devname);
51static int	doreadid(int fd, unsigned int numids, unsigned int trackno);
52static void	usage(void);
53
54static void
55usage(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
64int
65main(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
158static int
159doread(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			return (EX_OK);
188		}
189		if ((unsigned)rv == tracksize) {
190			nbytes += rv;
191			if (!quiet)
192				fprintf(stderr, "%5d KB\r", nbytes / 1024);
193			fwrite(trackbuf, sizeof(unsigned char), rv, of);
194			fflush(of);
195			continue;
196		}
197		if (rv == -1) {
198			/* fall back reading one sector at a time */
199			for (n = 0; n < tracksize; n += secsize) {
200				if (lseek(fd, nbytes, SEEK_SET) != nbytes)
201					err(EX_OSERR, "cannot lseek()");
202				rv = read(fd, trackbuf, secsize);
203				if ((unsigned) rv == secsize) {
204					nbytes += rv;
205					if (!quiet)
206						fprintf(stderr, "%5d KB\r",
207							nbytes / 1024);
208					fwrite(trackbuf, sizeof(unsigned char),
209					       rv, of);
210					fflush(of);
211					continue;
212				}
213				if (rv == -1) {
214					if (errno != EIO) {
215						if (!quiet)
216							putc('\n', stderr);
217						perror("non-IO error");
218						return (EX_OSERR);
219					}
220					if (ioctl(fd, FD_GSTAT, &fdcs) == -1)
221						errx(EX_IOERR,
222				     "floppy IO error, but no FDC status");
223					nerrs++;
224					recoverable = fdcs.status[2] &
225						NE7_ST2_DD;
226					if (!quiet) {
227						printstatus(&fdcs, 0);
228						fputs(" (", stderr);
229						if (!recoverable)
230							fputs("not ", stderr);
231						fputs("recoverable)", stderr);
232					}
233					if (!recover) {
234						if (!quiet)
235							putc('\n', stderr);
236						return (EX_IOERR);
237					}
238					memset(trackbuf, fillbyte, secsize);
239					if (recoverable) {
240						fdopts |= FDOPT_NOERROR;
241						if (ioctl(fd, FD_SOPTS,
242							  &fdopts) == -1)
243							err(EX_OSERR,
244				    "ioctl(fd, FD_SOPTS, FDOPT_NOERROR)");
245						rv = read(fd, trackbuf,
246							  secsize);
247						if ((unsigned)rv != secsize)
248							err(EX_IOERR,
249				    "read() with FDOPT_NOERROR still fails");
250						fdopts &= ~FDOPT_NOERROR;
251						(void)ioctl(fd, FD_SOPTS,
252							    &fdopts);
253					}
254					if (!quiet) {
255						if (recoverable)
256							fprintf(stderr,
257								": recovered");
258						else
259							fprintf(stderr,
260								": dummy");
261						fprintf(stderr,
262							" data @ %#x ... %#x\n",
263							nbytes,
264							nbytes + secsize - 1);
265					}
266					nbytes += secsize;
267					fwrite(trackbuf, sizeof(unsigned char),
268					       secsize, of);
269					fflush(of);
270					continue;
271				}
272				errx(EX_OSERR, "unexpected read() result: %d",
273				     rv);
274			}
275		}
276		if ((unsigned)rv < tracksize) {
277			/* should not happen */
278			nbytes += rv;
279			if (!quiet)
280				fprintf(stderr, "\nshort after %5d KB\r",
281					nbytes / 1024);
282			fwrite(trackbuf, sizeof(unsigned char), rv, of);
283			fflush(of);
284			continue;
285		}
286	}
287	if (!quiet) {
288		putc('\n', stderr);
289		if (nerrs)
290			fprintf(stderr, "%d error%s\n",
291				nerrs, nerrs > 1? "s": "");
292	}
293
294	return (nerrs? EX_IOERR: EX_OK);
295}
296
297static int
298doreadid(int fd, unsigned int numids, unsigned int trackno)
299{
300	int rv = 0;
301	unsigned int i;
302	struct fdc_readid info;
303	struct fdc_status fdcs;
304	struct fd_type fdt;
305
306	if (ioctl(fd, FD_GTYPE, &fdt) == -1)
307		err(EX_OSERR, "ioctl(FD_GTYPE) failed -- not a floppy?");
308
309	for (i = 0; i < numids; i++) {
310		info.cyl = trackno / fdt.heads;
311		info.head = fdt.heads > 1? trackno % fdt.heads: 0;
312		if (ioctl(fd, FD_READID, &info) == 0) {
313			printf("C = %d, H = %d, R = %d, N = %d\n",
314			       info.cyl, info.head, info.sec, info.secshift);
315		} else {
316			if (errno != EIO) {
317				perror("non-IO error");
318				return (EX_OSERR);
319			}
320			if (ioctl(fd, FD_GSTAT, &fdcs) == -1)
321				errx(EX_IOERR,
322				     "floppy IO error, but no FDC status");
323			printstatus(&fdcs, 0);
324			putc('\n', stderr);
325			rv = EX_IOERR;
326		}
327	}
328
329	return (rv);
330}
331