11de7b4b8SPedro F. Giffuni /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro 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
29b97809a7SJoerg Wunsch #include <sys/types.h>
30b97809a7SJoerg Wunsch #include <sys/stat.h>
31d137c337SJoerg Wunsch #include <sys/fdcio.h>
32b97809a7SJoerg Wunsch
33b97809a7SJoerg Wunsch #include <err.h>
34b97809a7SJoerg Wunsch #include <errno.h>
35b97809a7SJoerg Wunsch #include <fcntl.h>
36b97809a7SJoerg Wunsch #include <paths.h>
37b97809a7SJoerg Wunsch #include <stdio.h>
38b97809a7SJoerg Wunsch #include <stdlib.h>
39b97809a7SJoerg Wunsch #include <string.h>
40b97809a7SJoerg Wunsch #include <sysexits.h>
41b97809a7SJoerg Wunsch #include <unistd.h>
42b97809a7SJoerg Wunsch
43ba44060dSJoerg Wunsch #include <dev/ic/nec765.h>
44b97809a7SJoerg Wunsch
4565217f13SJoerg Wunsch #include "fdutil.h"
4665217f13SJoerg Wunsch
47d938340aSEd Schouten static int quiet, recover;
48d938340aSEd Schouten static unsigned char fillbyte = 0xf0; /* "foo" */
49b97809a7SJoerg Wunsch
50d938340aSEd Schouten static int doread(int fd, FILE *of, const char *_devname);
51d938340aSEd Schouten static int doreadid(int fd, unsigned int numids, unsigned int trackno);
52d938340aSEd Schouten static void usage(void);
53b97809a7SJoerg Wunsch
54d938340aSEd Schouten static void
usage(void)55b97809a7SJoerg Wunsch usage(void)
56b97809a7SJoerg Wunsch {
57b97809a7SJoerg Wunsch
58b97809a7SJoerg Wunsch errx(EX_USAGE,
598a5f4c12SJoerg Wunsch "usage: fdread [-qr] [-d device] [-f fillbyte]\n"
608a5f4c12SJoerg Wunsch " fdread [-d device] -I numids [-t trackno]");
61b97809a7SJoerg Wunsch }
62b97809a7SJoerg Wunsch
63b97809a7SJoerg Wunsch
64b97809a7SJoerg Wunsch int
main(int argc,char ** argv)65b97809a7SJoerg Wunsch main(int argc, char **argv)
66b97809a7SJoerg Wunsch {
67b97809a7SJoerg Wunsch int c, errs = 0;
688a5f4c12SJoerg Wunsch unsigned int numids = 0, trackno = 0;
694c1f1c62SXin LI const char *fname = 0, *_devname = "/dev/fd0";
70b97809a7SJoerg Wunsch char *cp;
71b97809a7SJoerg Wunsch FILE *of = stdout;
72b97809a7SJoerg Wunsch int fd;
73b97809a7SJoerg Wunsch unsigned long ul;
74b97809a7SJoerg Wunsch
758a5f4c12SJoerg Wunsch while ((c = getopt(argc, argv, "d:f:I:o:qrt:")) != -1)
76b97809a7SJoerg Wunsch switch (c) {
77b97809a7SJoerg Wunsch case 'd':
784c1f1c62SXin LI _devname = optarg;
79b97809a7SJoerg Wunsch break;
80b97809a7SJoerg Wunsch
81b97809a7SJoerg Wunsch case 'f':
82b97809a7SJoerg Wunsch ul = strtoul(optarg, &cp, 0);
83b97809a7SJoerg Wunsch if (*cp != '\0') {
84b97809a7SJoerg Wunsch fprintf(stderr,
85b97809a7SJoerg Wunsch "Bad argument %s to -f option; must be numeric\n",
86b97809a7SJoerg Wunsch optarg);
87b97809a7SJoerg Wunsch usage();
88b97809a7SJoerg Wunsch }
89b97809a7SJoerg Wunsch if (ul > 0xff)
90b97809a7SJoerg Wunsch warnx(
91b97809a7SJoerg Wunsch "Warning: fillbyte %#lx too large, truncating\n",
92b97809a7SJoerg Wunsch ul);
93b97809a7SJoerg Wunsch fillbyte = ul & 0xff;
94b97809a7SJoerg Wunsch break;
95b97809a7SJoerg Wunsch
968a5f4c12SJoerg Wunsch case 'I':
978a5f4c12SJoerg Wunsch ul = strtoul(optarg, &cp, 0);
988a5f4c12SJoerg Wunsch if (*cp != '\0') {
998a5f4c12SJoerg Wunsch fprintf(stderr,
1008a5f4c12SJoerg Wunsch "Bad argument %s to -I option; must be numeric\n",
1018a5f4c12SJoerg Wunsch optarg);
1028a5f4c12SJoerg Wunsch usage();
1038a5f4c12SJoerg Wunsch }
1048a5f4c12SJoerg Wunsch numids = ul;
1058a5f4c12SJoerg Wunsch break;
1068a5f4c12SJoerg Wunsch
107b97809a7SJoerg Wunsch case 'o':
108b97809a7SJoerg Wunsch fname = optarg;
109b97809a7SJoerg Wunsch break;
110b97809a7SJoerg Wunsch
111b97809a7SJoerg Wunsch case 'q':
112b97809a7SJoerg Wunsch quiet++;
113b97809a7SJoerg Wunsch break;
114b97809a7SJoerg Wunsch
115b97809a7SJoerg Wunsch case 'r':
116b97809a7SJoerg Wunsch recover++;
117b97809a7SJoerg Wunsch break;
118b97809a7SJoerg Wunsch
1198a5f4c12SJoerg Wunsch case 't':
1208a5f4c12SJoerg Wunsch ul = strtoul(optarg, &cp, 0);
1218a5f4c12SJoerg Wunsch if (*cp != '\0') {
1228a5f4c12SJoerg Wunsch fprintf(stderr,
1238a5f4c12SJoerg Wunsch "Bad argument %s to -t option; must be numeric\n",
1248a5f4c12SJoerg Wunsch optarg);
1258a5f4c12SJoerg Wunsch usage();
1268a5f4c12SJoerg Wunsch }
1278a5f4c12SJoerg Wunsch trackno = ul;
1288a5f4c12SJoerg Wunsch break;
1298a5f4c12SJoerg Wunsch
130b97809a7SJoerg Wunsch default:
131b97809a7SJoerg Wunsch errs++;
132b97809a7SJoerg Wunsch }
133b97809a7SJoerg Wunsch argc -= optind;
134b97809a7SJoerg Wunsch argv += optind;
135b97809a7SJoerg Wunsch
136b97809a7SJoerg Wunsch if (argc != 0 || errs)
137b97809a7SJoerg Wunsch usage();
1388a5f4c12SJoerg Wunsch /* check for mutually exclusive options */
1398a5f4c12SJoerg Wunsch if (numids) {
1408a5f4c12SJoerg Wunsch if (fname || quiet || recover)
1418a5f4c12SJoerg Wunsch usage();
1428a5f4c12SJoerg Wunsch } else {
1438a5f4c12SJoerg Wunsch if (trackno)
1448a5f4c12SJoerg Wunsch usage();
1458a5f4c12SJoerg Wunsch }
146b97809a7SJoerg Wunsch
147b97809a7SJoerg Wunsch if (fname) {
148b97809a7SJoerg Wunsch if ((of = fopen(fname, "w")) == NULL)
149b97809a7SJoerg Wunsch err(EX_OSERR, "cannot create output file %s", fname);
150b97809a7SJoerg Wunsch }
151b97809a7SJoerg Wunsch
152beee0062SJoerg Wunsch if ((fd = open(_devname, O_RDONLY)) == -1)
1534c1f1c62SXin LI err(EX_OSERR, "cannot open device %s", _devname);
154b97809a7SJoerg Wunsch
1554c1f1c62SXin LI return (numids? doreadid(fd, numids, trackno): doread(fd, of, _devname));
156b97809a7SJoerg Wunsch }
157b97809a7SJoerg Wunsch
158d938340aSEd Schouten static int
doread(int fd,FILE * of,const char * _devname)1594c1f1c62SXin LI doread(int fd, FILE *of, const char *_devname)
160b97809a7SJoerg Wunsch {
161b97809a7SJoerg Wunsch char *trackbuf;
162b97809a7SJoerg Wunsch int rv, fdopts, recoverable, nerrs = 0;
163b97809a7SJoerg Wunsch unsigned int nbytes, tracksize, mediasize, secsize, n;
164b97809a7SJoerg Wunsch struct fdc_status fdcs;
165b97809a7SJoerg Wunsch struct fd_type fdt;
166b97809a7SJoerg Wunsch
167b97809a7SJoerg Wunsch if (ioctl(fd, FD_GTYPE, &fdt) == -1)
168b97809a7SJoerg Wunsch err(EX_OSERR, "ioctl(FD_GTYPE) failed -- not a floppy?");
169b97809a7SJoerg Wunsch
170b97809a7SJoerg Wunsch secsize = 128 << fdt.secsize;
171b97809a7SJoerg Wunsch tracksize = fdt.sectrac * secsize;
172b97809a7SJoerg Wunsch mediasize = tracksize * fdt.tracks * fdt.heads;
17345be165fSMarcelo Araujo if ((trackbuf = malloc(tracksize)) == NULL)
174b97809a7SJoerg Wunsch errx(EX_TEMPFAIL, "out of memory");
175b97809a7SJoerg Wunsch
176b97809a7SJoerg Wunsch if (!quiet)
177b97809a7SJoerg Wunsch fprintf(stderr, "Reading %d * %d * %d * %d medium at %s\n",
1784c1f1c62SXin LI fdt.tracks, fdt.heads, fdt.sectrac, secsize, _devname);
179b97809a7SJoerg Wunsch
180b97809a7SJoerg Wunsch for (nbytes = 0; nbytes < mediasize;) {
181b97809a7SJoerg Wunsch if (lseek(fd, nbytes, SEEK_SET) != nbytes)
182b97809a7SJoerg Wunsch err(EX_OSERR, "cannot lseek()");
183b97809a7SJoerg Wunsch rv = read(fd, trackbuf, tracksize);
184b97809a7SJoerg Wunsch if (rv == 0) {
185b97809a7SJoerg Wunsch /* EOF? */
186b97809a7SJoerg Wunsch warnx("premature EOF after %u bytes", nbytes);
187049e1954SMarcelo Araujo free(trackbuf);
188b97809a7SJoerg Wunsch return (EX_OK);
189b97809a7SJoerg Wunsch }
1904c1f1c62SXin LI if ((unsigned)rv == tracksize) {
191b97809a7SJoerg Wunsch nbytes += rv;
192b97809a7SJoerg Wunsch if (!quiet)
193b97809a7SJoerg Wunsch fprintf(stderr, "%5d KB\r", nbytes / 1024);
194b97809a7SJoerg Wunsch fwrite(trackbuf, sizeof(unsigned char), rv, of);
195b97809a7SJoerg Wunsch fflush(of);
196b97809a7SJoerg Wunsch continue;
197b97809a7SJoerg Wunsch }
198b97809a7SJoerg Wunsch if (rv == -1) {
199b97809a7SJoerg Wunsch /* fall back reading one sector at a time */
200b97809a7SJoerg Wunsch for (n = 0; n < tracksize; n += secsize) {
201b97809a7SJoerg Wunsch if (lseek(fd, nbytes, SEEK_SET) != nbytes)
202b97809a7SJoerg Wunsch err(EX_OSERR, "cannot lseek()");
203b97809a7SJoerg Wunsch rv = read(fd, trackbuf, secsize);
2044c1f1c62SXin LI if ((unsigned) rv == secsize) {
205b97809a7SJoerg Wunsch nbytes += rv;
206b97809a7SJoerg Wunsch if (!quiet)
207b97809a7SJoerg Wunsch fprintf(stderr, "%5d KB\r",
208b97809a7SJoerg Wunsch nbytes / 1024);
209b97809a7SJoerg Wunsch fwrite(trackbuf, sizeof(unsigned char),
210b97809a7SJoerg Wunsch rv, of);
211b97809a7SJoerg Wunsch fflush(of);
212b97809a7SJoerg Wunsch continue;
213b97809a7SJoerg Wunsch }
214b97809a7SJoerg Wunsch if (rv == -1) {
215b97809a7SJoerg Wunsch if (errno != EIO) {
216b97809a7SJoerg Wunsch if (!quiet)
217b97809a7SJoerg Wunsch putc('\n', stderr);
218b97809a7SJoerg Wunsch perror("non-IO error");
219049e1954SMarcelo Araujo free(trackbuf);
220b97809a7SJoerg Wunsch return (EX_OSERR);
221b97809a7SJoerg Wunsch }
222b97809a7SJoerg Wunsch if (ioctl(fd, FD_GSTAT, &fdcs) == -1)
223b97809a7SJoerg Wunsch errx(EX_IOERR,
224b97809a7SJoerg Wunsch "floppy IO error, but no FDC status");
225b97809a7SJoerg Wunsch nerrs++;
226b97809a7SJoerg Wunsch recoverable = fdcs.status[2] &
227b97809a7SJoerg Wunsch NE7_ST2_DD;
228b97809a7SJoerg Wunsch if (!quiet) {
22965217f13SJoerg Wunsch printstatus(&fdcs, 0);
230b97809a7SJoerg Wunsch fputs(" (", stderr);
231b97809a7SJoerg Wunsch if (!recoverable)
232b97809a7SJoerg Wunsch fputs("not ", stderr);
233b97809a7SJoerg Wunsch fputs("recoverable)", stderr);
234b97809a7SJoerg Wunsch }
235b97809a7SJoerg Wunsch if (!recover) {
236b97809a7SJoerg Wunsch if (!quiet)
237b97809a7SJoerg Wunsch putc('\n', stderr);
238049e1954SMarcelo Araujo free(trackbuf);
239b97809a7SJoerg Wunsch return (EX_IOERR);
240b97809a7SJoerg Wunsch }
241b97809a7SJoerg Wunsch memset(trackbuf, fillbyte, secsize);
242b97809a7SJoerg Wunsch if (recoverable) {
243b97809a7SJoerg Wunsch fdopts |= FDOPT_NOERROR;
244b97809a7SJoerg Wunsch if (ioctl(fd, FD_SOPTS,
245b97809a7SJoerg Wunsch &fdopts) == -1)
246b97809a7SJoerg Wunsch err(EX_OSERR,
247b97809a7SJoerg Wunsch "ioctl(fd, FD_SOPTS, FDOPT_NOERROR)");
248b97809a7SJoerg Wunsch rv = read(fd, trackbuf,
249b97809a7SJoerg Wunsch secsize);
2504c1f1c62SXin LI if ((unsigned)rv != secsize)
251b97809a7SJoerg Wunsch err(EX_IOERR,
252b97809a7SJoerg Wunsch "read() with FDOPT_NOERROR still fails");
253b97809a7SJoerg Wunsch fdopts &= ~FDOPT_NOERROR;
254b97809a7SJoerg Wunsch (void)ioctl(fd, FD_SOPTS,
255b97809a7SJoerg Wunsch &fdopts);
256b97809a7SJoerg Wunsch }
257b97809a7SJoerg Wunsch if (!quiet) {
258b97809a7SJoerg Wunsch if (recoverable)
259b97809a7SJoerg Wunsch fprintf(stderr,
260b97809a7SJoerg Wunsch ": recovered");
261b97809a7SJoerg Wunsch else
262b97809a7SJoerg Wunsch fprintf(stderr,
263b97809a7SJoerg Wunsch ": dummy");
264b97809a7SJoerg Wunsch fprintf(stderr,
265b97809a7SJoerg Wunsch " data @ %#x ... %#x\n",
266b97809a7SJoerg Wunsch nbytes,
267b97809a7SJoerg Wunsch nbytes + secsize - 1);
268b97809a7SJoerg Wunsch }
269b97809a7SJoerg Wunsch nbytes += secsize;
270b97809a7SJoerg Wunsch fwrite(trackbuf, sizeof(unsigned char),
271b97809a7SJoerg Wunsch secsize, of);
272b97809a7SJoerg Wunsch fflush(of);
273b97809a7SJoerg Wunsch continue;
274b97809a7SJoerg Wunsch }
275b97809a7SJoerg Wunsch errx(EX_OSERR, "unexpected read() result: %d",
276b97809a7SJoerg Wunsch rv);
277b97809a7SJoerg Wunsch }
278b97809a7SJoerg Wunsch }
2794c1f1c62SXin LI if ((unsigned)rv < tracksize) {
2804c1f1c62SXin LI /* should not happen */
2814c1f1c62SXin LI nbytes += rv;
2824c1f1c62SXin LI if (!quiet)
2834c1f1c62SXin LI fprintf(stderr, "\nshort after %5d KB\r",
2844c1f1c62SXin LI nbytes / 1024);
2854c1f1c62SXin LI fwrite(trackbuf, sizeof(unsigned char), rv, of);
2864c1f1c62SXin LI fflush(of);
2874c1f1c62SXin LI continue;
2884c1f1c62SXin LI }
289b97809a7SJoerg Wunsch }
290049e1954SMarcelo Araujo free(trackbuf);
291b97809a7SJoerg Wunsch if (!quiet) {
292b97809a7SJoerg Wunsch putc('\n', stderr);
293b97809a7SJoerg Wunsch if (nerrs)
294b97809a7SJoerg Wunsch fprintf(stderr, "%d error%s\n",
295b97809a7SJoerg Wunsch nerrs, nerrs > 1? "s": "");
296b97809a7SJoerg Wunsch }
297b97809a7SJoerg Wunsch
298b97809a7SJoerg Wunsch return (nerrs? EX_IOERR: EX_OK);
299b97809a7SJoerg Wunsch }
300b97809a7SJoerg Wunsch
301d938340aSEd Schouten static int
doreadid(int fd,unsigned int numids,unsigned int trackno)3028a5f4c12SJoerg Wunsch doreadid(int fd, unsigned int numids, unsigned int trackno)
3038a5f4c12SJoerg Wunsch {
30481f5cd99SJoerg Wunsch int rv = 0;
3058a5f4c12SJoerg Wunsch unsigned int i;
3068a5f4c12SJoerg Wunsch struct fdc_readid info;
3078a5f4c12SJoerg Wunsch struct fdc_status fdcs;
3088a5f4c12SJoerg Wunsch struct fd_type fdt;
3098a5f4c12SJoerg Wunsch
3108a5f4c12SJoerg Wunsch if (ioctl(fd, FD_GTYPE, &fdt) == -1)
3118a5f4c12SJoerg Wunsch err(EX_OSERR, "ioctl(FD_GTYPE) failed -- not a floppy?");
3128a5f4c12SJoerg Wunsch
3138a5f4c12SJoerg Wunsch for (i = 0; i < numids; i++) {
3148a5f4c12SJoerg Wunsch info.cyl = trackno / fdt.heads;
3158a5f4c12SJoerg Wunsch info.head = fdt.heads > 1? trackno % fdt.heads: 0;
316347a5ee7SStefan Farfeleder if (ioctl(fd, FD_READID, &info) == 0) {
3178a5f4c12SJoerg Wunsch printf("C = %d, H = %d, R = %d, N = %d\n",
3188a5f4c12SJoerg Wunsch info.cyl, info.head, info.sec, info.secshift);
3198a5f4c12SJoerg Wunsch } else {
3208a5f4c12SJoerg Wunsch if (errno != EIO) {
3218a5f4c12SJoerg Wunsch perror("non-IO error");
3228a5f4c12SJoerg Wunsch return (EX_OSERR);
3238a5f4c12SJoerg Wunsch }
3248a5f4c12SJoerg Wunsch if (ioctl(fd, FD_GSTAT, &fdcs) == -1)
3258a5f4c12SJoerg Wunsch errx(EX_IOERR,
3268a5f4c12SJoerg Wunsch "floppy IO error, but no FDC status");
32765217f13SJoerg Wunsch printstatus(&fdcs, 0);
3288a5f4c12SJoerg Wunsch putc('\n', stderr);
3298a5f4c12SJoerg Wunsch rv = EX_IOERR;
3308a5f4c12SJoerg Wunsch }
3318a5f4c12SJoerg Wunsch }
3328a5f4c12SJoerg Wunsch
3338a5f4c12SJoerg Wunsch return (rv);
3348a5f4c12SJoerg Wunsch }
335