xref: /freebsd/usr.sbin/fdread/fdread.c (revision 1de7b4b805ddbf2429da511c053686ac4591ed89)
1*1de7b4b8SPedro F. Giffuni /*-
2*1de7b4b8SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*1de7b4b8SPedro F. Giffuni  *
4b97809a7SJoerg Wunsch  * Copyright (c) 2001 Joerg Wunsch
5b97809a7SJoerg Wunsch  *
6b97809a7SJoerg Wunsch  * All rights reserved.
7b97809a7SJoerg Wunsch  *
8b97809a7SJoerg Wunsch  * Redistribution and use in source and binary forms, with or without
9b97809a7SJoerg Wunsch  * modification, are permitted provided that the following conditions
10b97809a7SJoerg Wunsch  * are met:
11b97809a7SJoerg Wunsch  * 1. Redistributions of source code must retain the above copyright
12b97809a7SJoerg Wunsch  *    notice, this list of conditions and the following disclaimer.
13b97809a7SJoerg Wunsch  * 2. Redistributions in binary form must reproduce the above copyright
14b97809a7SJoerg Wunsch  *    notice, this list of conditions and the following disclaimer in the
15b97809a7SJoerg Wunsch  *    documentation and/or other materials provided with the distribution.
16b97809a7SJoerg Wunsch  *
17b97809a7SJoerg Wunsch  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
18b97809a7SJoerg Wunsch  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19b97809a7SJoerg Wunsch  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20b97809a7SJoerg Wunsch  * IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
21b97809a7SJoerg Wunsch  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22b97809a7SJoerg Wunsch  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23b97809a7SJoerg Wunsch  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24b97809a7SJoerg Wunsch  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25b97809a7SJoerg Wunsch  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26b97809a7SJoerg Wunsch  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27b97809a7SJoerg Wunsch  *
28b97809a7SJoerg Wunsch  * $FreeBSD$
29b97809a7SJoerg Wunsch  */
30b97809a7SJoerg Wunsch 
31b97809a7SJoerg Wunsch #include <sys/types.h>
32b97809a7SJoerg Wunsch #include <sys/stat.h>
33d137c337SJoerg Wunsch #include <sys/fdcio.h>
34b97809a7SJoerg Wunsch 
35b97809a7SJoerg Wunsch #include <err.h>
36b97809a7SJoerg Wunsch #include <errno.h>
37b97809a7SJoerg Wunsch #include <fcntl.h>
38b97809a7SJoerg Wunsch #include <paths.h>
39b97809a7SJoerg Wunsch #include <stdio.h>
40b97809a7SJoerg Wunsch #include <stdlib.h>
41b97809a7SJoerg Wunsch #include <string.h>
42b97809a7SJoerg Wunsch #include <sysexits.h>
43b97809a7SJoerg Wunsch #include <unistd.h>
44b97809a7SJoerg Wunsch 
45ba44060dSJoerg Wunsch #include <dev/ic/nec765.h>
46b97809a7SJoerg Wunsch 
4765217f13SJoerg Wunsch #include "fdutil.h"
4865217f13SJoerg Wunsch 
49d938340aSEd Schouten static int	quiet, recover;
50d938340aSEd Schouten static unsigned char fillbyte = 0xf0;	/* "foo" */
51b97809a7SJoerg Wunsch 
52d938340aSEd Schouten static int	doread(int fd, FILE *of, const char *_devname);
53d938340aSEd Schouten static int	doreadid(int fd, unsigned int numids, unsigned int trackno);
54d938340aSEd Schouten static void	usage(void);
55b97809a7SJoerg Wunsch 
56d938340aSEd Schouten static void
57b97809a7SJoerg Wunsch usage(void)
58b97809a7SJoerg Wunsch {
59b97809a7SJoerg Wunsch 
60b97809a7SJoerg Wunsch 	errx(EX_USAGE,
618a5f4c12SJoerg Wunsch 	     "usage: fdread [-qr] [-d device] [-f fillbyte]\n"
628a5f4c12SJoerg Wunsch 	     "       fdread [-d device] -I numids [-t trackno]");
63b97809a7SJoerg Wunsch }
64b97809a7SJoerg Wunsch 
65b97809a7SJoerg Wunsch 
66b97809a7SJoerg Wunsch int
67b97809a7SJoerg Wunsch main(int argc, char **argv)
68b97809a7SJoerg Wunsch {
69b97809a7SJoerg Wunsch 	int c, errs = 0;
708a5f4c12SJoerg Wunsch 	unsigned int numids = 0, trackno = 0;
714c1f1c62SXin LI 	const char *fname = 0, *_devname = "/dev/fd0";
72b97809a7SJoerg Wunsch 	char *cp;
73b97809a7SJoerg Wunsch 	FILE *of = stdout;
74b97809a7SJoerg Wunsch 	int fd;
75b97809a7SJoerg Wunsch 	unsigned long ul;
76b97809a7SJoerg Wunsch 
778a5f4c12SJoerg Wunsch 	while ((c = getopt(argc, argv, "d:f:I:o:qrt:")) != -1)
78b97809a7SJoerg Wunsch 		switch (c) {
79b97809a7SJoerg Wunsch 		case 'd':
804c1f1c62SXin LI 			_devname = optarg;
81b97809a7SJoerg Wunsch 			break;
82b97809a7SJoerg Wunsch 
83b97809a7SJoerg Wunsch 		case 'f':
84b97809a7SJoerg Wunsch 			ul = strtoul(optarg, &cp, 0);
85b97809a7SJoerg Wunsch 			if (*cp != '\0') {
86b97809a7SJoerg Wunsch 				fprintf(stderr,
87b97809a7SJoerg Wunsch 			"Bad argument %s to -f option; must be numeric\n",
88b97809a7SJoerg Wunsch 					optarg);
89b97809a7SJoerg Wunsch 				usage();
90b97809a7SJoerg Wunsch 			}
91b97809a7SJoerg Wunsch 			if (ul > 0xff)
92b97809a7SJoerg Wunsch 				warnx(
93b97809a7SJoerg Wunsch 			"Warning: fillbyte %#lx too large, truncating\n",
94b97809a7SJoerg Wunsch 				      ul);
95b97809a7SJoerg Wunsch 			fillbyte = ul & 0xff;
96b97809a7SJoerg Wunsch 			break;
97b97809a7SJoerg Wunsch 
988a5f4c12SJoerg Wunsch 		case 'I':
998a5f4c12SJoerg Wunsch 			ul = strtoul(optarg, &cp, 0);
1008a5f4c12SJoerg Wunsch 			if (*cp != '\0') {
1018a5f4c12SJoerg Wunsch 				fprintf(stderr,
1028a5f4c12SJoerg Wunsch 			"Bad argument %s to -I option; must be numeric\n",
1038a5f4c12SJoerg Wunsch 					optarg);
1048a5f4c12SJoerg Wunsch 				usage();
1058a5f4c12SJoerg Wunsch 			}
1068a5f4c12SJoerg Wunsch 			numids = ul;
1078a5f4c12SJoerg Wunsch 			break;
1088a5f4c12SJoerg Wunsch 
109b97809a7SJoerg Wunsch 		case 'o':
110b97809a7SJoerg Wunsch 			fname = optarg;
111b97809a7SJoerg Wunsch 			break;
112b97809a7SJoerg Wunsch 
113b97809a7SJoerg Wunsch 		case 'q':
114b97809a7SJoerg Wunsch 			quiet++;
115b97809a7SJoerg Wunsch 			break;
116b97809a7SJoerg Wunsch 
117b97809a7SJoerg Wunsch 		case 'r':
118b97809a7SJoerg Wunsch 			recover++;
119b97809a7SJoerg Wunsch 			break;
120b97809a7SJoerg Wunsch 
1218a5f4c12SJoerg Wunsch 		case 't':
1228a5f4c12SJoerg Wunsch 			ul = strtoul(optarg, &cp, 0);
1238a5f4c12SJoerg Wunsch 			if (*cp != '\0') {
1248a5f4c12SJoerg Wunsch 				fprintf(stderr,
1258a5f4c12SJoerg Wunsch 			"Bad argument %s to -t option; must be numeric\n",
1268a5f4c12SJoerg Wunsch 					optarg);
1278a5f4c12SJoerg Wunsch 				usage();
1288a5f4c12SJoerg Wunsch 			}
1298a5f4c12SJoerg Wunsch 			trackno = ul;
1308a5f4c12SJoerg Wunsch 			break;
1318a5f4c12SJoerg Wunsch 
132b97809a7SJoerg Wunsch 		default:
133b97809a7SJoerg Wunsch 			errs++;
134b97809a7SJoerg Wunsch 		}
135b97809a7SJoerg Wunsch 	argc -= optind;
136b97809a7SJoerg Wunsch 	argv += optind;
137b97809a7SJoerg Wunsch 
138b97809a7SJoerg Wunsch 	if (argc != 0 || errs)
139b97809a7SJoerg Wunsch 		usage();
1408a5f4c12SJoerg Wunsch 	/* check for mutually exclusive options */
1418a5f4c12SJoerg Wunsch 	if (numids) {
1428a5f4c12SJoerg Wunsch 		if (fname || quiet || recover)
1438a5f4c12SJoerg Wunsch 			usage();
1448a5f4c12SJoerg Wunsch 	} else {
1458a5f4c12SJoerg Wunsch 		if (trackno)
1468a5f4c12SJoerg Wunsch 			usage();
1478a5f4c12SJoerg Wunsch 	}
148b97809a7SJoerg Wunsch 
149b97809a7SJoerg Wunsch 	if (fname) {
150b97809a7SJoerg Wunsch 		if ((of = fopen(fname, "w")) == NULL)
151b97809a7SJoerg Wunsch 			err(EX_OSERR, "cannot create output file %s", fname);
152b97809a7SJoerg Wunsch 	}
153b97809a7SJoerg Wunsch 
154beee0062SJoerg Wunsch 	if ((fd = open(_devname, O_RDONLY)) == -1)
1554c1f1c62SXin LI 		err(EX_OSERR, "cannot open device %s", _devname);
156b97809a7SJoerg Wunsch 
1574c1f1c62SXin LI 	return (numids? doreadid(fd, numids, trackno): doread(fd, of, _devname));
158b97809a7SJoerg Wunsch }
159b97809a7SJoerg Wunsch 
160d938340aSEd Schouten static int
1614c1f1c62SXin LI doread(int fd, FILE *of, const char *_devname)
162b97809a7SJoerg Wunsch {
163b97809a7SJoerg Wunsch 	char *trackbuf;
164b97809a7SJoerg Wunsch 	int rv, fdopts, recoverable, nerrs = 0;
165b97809a7SJoerg Wunsch 	unsigned int nbytes, tracksize, mediasize, secsize, n;
166b97809a7SJoerg Wunsch 	struct fdc_status fdcs;
167b97809a7SJoerg Wunsch 	struct fd_type fdt;
168b97809a7SJoerg Wunsch 
169b97809a7SJoerg Wunsch 	if (ioctl(fd, FD_GTYPE, &fdt) == -1)
170b97809a7SJoerg Wunsch 		err(EX_OSERR, "ioctl(FD_GTYPE) failed -- not a floppy?");
171b97809a7SJoerg Wunsch 
172b97809a7SJoerg Wunsch 	secsize = 128 << fdt.secsize;
173b97809a7SJoerg Wunsch 	tracksize = fdt.sectrac * secsize;
174b97809a7SJoerg Wunsch 	mediasize = tracksize * fdt.tracks * fdt.heads;
17545be165fSMarcelo Araujo 	if ((trackbuf = malloc(tracksize)) == NULL)
176b97809a7SJoerg Wunsch 		errx(EX_TEMPFAIL, "out of memory");
177b97809a7SJoerg Wunsch 
178b97809a7SJoerg Wunsch 	if (!quiet)
179b97809a7SJoerg Wunsch 		fprintf(stderr, "Reading %d * %d * %d * %d medium at %s\n",
1804c1f1c62SXin LI 			fdt.tracks, fdt.heads, fdt.sectrac, secsize, _devname);
181b97809a7SJoerg Wunsch 
182b97809a7SJoerg Wunsch 	for (nbytes = 0; nbytes < mediasize;) {
183b97809a7SJoerg Wunsch 		if (lseek(fd, nbytes, SEEK_SET) != nbytes)
184b97809a7SJoerg Wunsch 			err(EX_OSERR, "cannot lseek()");
185b97809a7SJoerg Wunsch 		rv = read(fd, trackbuf, tracksize);
186b97809a7SJoerg Wunsch 		if (rv == 0) {
187b97809a7SJoerg Wunsch 			/* EOF? */
188b97809a7SJoerg Wunsch 			warnx("premature EOF after %u bytes", nbytes);
189049e1954SMarcelo Araujo 			free(trackbuf);
190b97809a7SJoerg Wunsch 			return (EX_OK);
191b97809a7SJoerg Wunsch 		}
1924c1f1c62SXin LI 		if ((unsigned)rv == tracksize) {
193b97809a7SJoerg Wunsch 			nbytes += rv;
194b97809a7SJoerg Wunsch 			if (!quiet)
195b97809a7SJoerg Wunsch 				fprintf(stderr, "%5d KB\r", nbytes / 1024);
196b97809a7SJoerg Wunsch 			fwrite(trackbuf, sizeof(unsigned char), rv, of);
197b97809a7SJoerg Wunsch 			fflush(of);
198b97809a7SJoerg Wunsch 			continue;
199b97809a7SJoerg Wunsch 		}
200b97809a7SJoerg Wunsch 		if (rv == -1) {
201b97809a7SJoerg Wunsch 			/* fall back reading one sector at a time */
202b97809a7SJoerg Wunsch 			for (n = 0; n < tracksize; n += secsize) {
203b97809a7SJoerg Wunsch 				if (lseek(fd, nbytes, SEEK_SET) != nbytes)
204b97809a7SJoerg Wunsch 					err(EX_OSERR, "cannot lseek()");
205b97809a7SJoerg Wunsch 				rv = read(fd, trackbuf, secsize);
2064c1f1c62SXin LI 				if ((unsigned) rv == secsize) {
207b97809a7SJoerg Wunsch 					nbytes += rv;
208b97809a7SJoerg Wunsch 					if (!quiet)
209b97809a7SJoerg Wunsch 						fprintf(stderr, "%5d KB\r",
210b97809a7SJoerg Wunsch 							nbytes / 1024);
211b97809a7SJoerg Wunsch 					fwrite(trackbuf, sizeof(unsigned char),
212b97809a7SJoerg Wunsch 					       rv, of);
213b97809a7SJoerg Wunsch 					fflush(of);
214b97809a7SJoerg Wunsch 					continue;
215b97809a7SJoerg Wunsch 				}
216b97809a7SJoerg Wunsch 				if (rv == -1) {
217b97809a7SJoerg Wunsch 					if (errno != EIO) {
218b97809a7SJoerg Wunsch 						if (!quiet)
219b97809a7SJoerg Wunsch 							putc('\n', stderr);
220b97809a7SJoerg Wunsch 						perror("non-IO error");
221049e1954SMarcelo Araujo 						free(trackbuf);
222b97809a7SJoerg Wunsch 						return (EX_OSERR);
223b97809a7SJoerg Wunsch 					}
224b97809a7SJoerg Wunsch 					if (ioctl(fd, FD_GSTAT, &fdcs) == -1)
225b97809a7SJoerg Wunsch 						errx(EX_IOERR,
226b97809a7SJoerg Wunsch 				     "floppy IO error, but no FDC status");
227b97809a7SJoerg Wunsch 					nerrs++;
228b97809a7SJoerg Wunsch 					recoverable = fdcs.status[2] &
229b97809a7SJoerg Wunsch 						NE7_ST2_DD;
230b97809a7SJoerg Wunsch 					if (!quiet) {
23165217f13SJoerg Wunsch 						printstatus(&fdcs, 0);
232b97809a7SJoerg Wunsch 						fputs(" (", stderr);
233b97809a7SJoerg Wunsch 						if (!recoverable)
234b97809a7SJoerg Wunsch 							fputs("not ", stderr);
235b97809a7SJoerg Wunsch 						fputs("recoverable)", stderr);
236b97809a7SJoerg Wunsch 					}
237b97809a7SJoerg Wunsch 					if (!recover) {
238b97809a7SJoerg Wunsch 						if (!quiet)
239b97809a7SJoerg Wunsch 							putc('\n', stderr);
240049e1954SMarcelo Araujo 						free(trackbuf);
241b97809a7SJoerg Wunsch 						return (EX_IOERR);
242b97809a7SJoerg Wunsch 					}
243b97809a7SJoerg Wunsch 					memset(trackbuf, fillbyte, secsize);
244b97809a7SJoerg Wunsch 					if (recoverable) {
245b97809a7SJoerg Wunsch 						fdopts |= FDOPT_NOERROR;
246b97809a7SJoerg Wunsch 						if (ioctl(fd, FD_SOPTS,
247b97809a7SJoerg Wunsch 							  &fdopts) == -1)
248b97809a7SJoerg Wunsch 							err(EX_OSERR,
249b97809a7SJoerg Wunsch 				    "ioctl(fd, FD_SOPTS, FDOPT_NOERROR)");
250b97809a7SJoerg Wunsch 						rv = read(fd, trackbuf,
251b97809a7SJoerg Wunsch 							  secsize);
2524c1f1c62SXin LI 						if ((unsigned)rv != secsize)
253b97809a7SJoerg Wunsch 							err(EX_IOERR,
254b97809a7SJoerg Wunsch 				    "read() with FDOPT_NOERROR still fails");
255b97809a7SJoerg Wunsch 						fdopts &= ~FDOPT_NOERROR;
256b97809a7SJoerg Wunsch 						(void)ioctl(fd, FD_SOPTS,
257b97809a7SJoerg Wunsch 							    &fdopts);
258b97809a7SJoerg Wunsch 					}
259b97809a7SJoerg Wunsch 					if (!quiet) {
260b97809a7SJoerg Wunsch 						if (recoverable)
261b97809a7SJoerg Wunsch 							fprintf(stderr,
262b97809a7SJoerg Wunsch 								": recovered");
263b97809a7SJoerg Wunsch 						else
264b97809a7SJoerg Wunsch 							fprintf(stderr,
265b97809a7SJoerg Wunsch 								": dummy");
266b97809a7SJoerg Wunsch 						fprintf(stderr,
267b97809a7SJoerg Wunsch 							" data @ %#x ... %#x\n",
268b97809a7SJoerg Wunsch 							nbytes,
269b97809a7SJoerg Wunsch 							nbytes + secsize - 1);
270b97809a7SJoerg Wunsch 					}
271b97809a7SJoerg Wunsch 					nbytes += secsize;
272b97809a7SJoerg Wunsch 					fwrite(trackbuf, sizeof(unsigned char),
273b97809a7SJoerg Wunsch 					       secsize, of);
274b97809a7SJoerg Wunsch 					fflush(of);
275b97809a7SJoerg Wunsch 					continue;
276b97809a7SJoerg Wunsch 				}
277b97809a7SJoerg Wunsch 				errx(EX_OSERR, "unexpected read() result: %d",
278b97809a7SJoerg Wunsch 				     rv);
279b97809a7SJoerg Wunsch 			}
280b97809a7SJoerg Wunsch 		}
2814c1f1c62SXin LI 		if ((unsigned)rv < tracksize) {
2824c1f1c62SXin LI 			/* should not happen */
2834c1f1c62SXin LI 			nbytes += rv;
2844c1f1c62SXin LI 			if (!quiet)
2854c1f1c62SXin LI 				fprintf(stderr, "\nshort after %5d KB\r",
2864c1f1c62SXin LI 					nbytes / 1024);
2874c1f1c62SXin LI 			fwrite(trackbuf, sizeof(unsigned char), rv, of);
2884c1f1c62SXin LI 			fflush(of);
2894c1f1c62SXin LI 			continue;
2904c1f1c62SXin LI 		}
291b97809a7SJoerg Wunsch 	}
292049e1954SMarcelo Araujo 	free(trackbuf);
293b97809a7SJoerg Wunsch 	if (!quiet) {
294b97809a7SJoerg Wunsch 		putc('\n', stderr);
295b97809a7SJoerg Wunsch 		if (nerrs)
296b97809a7SJoerg Wunsch 			fprintf(stderr, "%d error%s\n",
297b97809a7SJoerg Wunsch 				nerrs, nerrs > 1? "s": "");
298b97809a7SJoerg Wunsch 	}
299b97809a7SJoerg Wunsch 
300b97809a7SJoerg Wunsch 	return (nerrs? EX_IOERR: EX_OK);
301b97809a7SJoerg Wunsch }
302b97809a7SJoerg Wunsch 
303d938340aSEd Schouten static int
3048a5f4c12SJoerg Wunsch doreadid(int fd, unsigned int numids, unsigned int trackno)
3058a5f4c12SJoerg Wunsch {
30681f5cd99SJoerg Wunsch 	int rv = 0;
3078a5f4c12SJoerg Wunsch 	unsigned int i;
3088a5f4c12SJoerg Wunsch 	struct fdc_readid info;
3098a5f4c12SJoerg Wunsch 	struct fdc_status fdcs;
3108a5f4c12SJoerg Wunsch 	struct fd_type fdt;
3118a5f4c12SJoerg Wunsch 
3128a5f4c12SJoerg Wunsch 	if (ioctl(fd, FD_GTYPE, &fdt) == -1)
3138a5f4c12SJoerg Wunsch 		err(EX_OSERR, "ioctl(FD_GTYPE) failed -- not a floppy?");
3148a5f4c12SJoerg Wunsch 
3158a5f4c12SJoerg Wunsch 	for (i = 0; i < numids; i++) {
3168a5f4c12SJoerg Wunsch 		info.cyl = trackno / fdt.heads;
3178a5f4c12SJoerg Wunsch 		info.head = fdt.heads > 1? trackno % fdt.heads: 0;
318347a5ee7SStefan Farfeleder 		if (ioctl(fd, FD_READID, &info) == 0) {
3198a5f4c12SJoerg Wunsch 			printf("C = %d, H = %d, R = %d, N = %d\n",
3208a5f4c12SJoerg Wunsch 			       info.cyl, info.head, info.sec, info.secshift);
3218a5f4c12SJoerg Wunsch 		} else {
3228a5f4c12SJoerg Wunsch 			if (errno != EIO) {
3238a5f4c12SJoerg Wunsch 				perror("non-IO error");
3248a5f4c12SJoerg Wunsch 				return (EX_OSERR);
3258a5f4c12SJoerg Wunsch 			}
3268a5f4c12SJoerg Wunsch 			if (ioctl(fd, FD_GSTAT, &fdcs) == -1)
3278a5f4c12SJoerg Wunsch 				errx(EX_IOERR,
3288a5f4c12SJoerg Wunsch 				     "floppy IO error, but no FDC status");
32965217f13SJoerg Wunsch 			printstatus(&fdcs, 0);
3308a5f4c12SJoerg Wunsch 			putc('\n', stderr);
3318a5f4c12SJoerg Wunsch 			rv = EX_IOERR;
3328a5f4c12SJoerg Wunsch 		}
3338a5f4c12SJoerg Wunsch 	}
3348a5f4c12SJoerg Wunsch 
3358a5f4c12SJoerg Wunsch 	return (rv);
3368a5f4c12SJoerg Wunsch }
337