2 * Copyright (c) 2001 Joerg Wunsch
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
29 #include <sys/types.h>
31 #include <sys/fdcio.h>
43 #include <dev/ic/nec765.h>
47 static int quiet, recover;
48 static unsigned char fillbyte = 0xf0; /* "foo" */
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);
59 "usage: fdread [-qr] [-d device] [-f fillbyte]\n"
60 " fdread [-d device] -I numids [-t trackno]");
65 main(int argc, char **argv)
68 unsigned int numids = 0, trackno = 0;
69 const char *fname = 0, *_devname = "/dev/fd0";
75 while ((c = getopt(argc, argv, "d:f:I:o:qrt:")) != -1)
82 ul = strtoul(optarg, &cp, 0);
85 "Bad argument %s to -f option; must be numeric\n",
91 "Warning: fillbyte %#lx too large, truncating\n",
97 ul = strtoul(optarg, &cp, 0);
100 "Bad argument %s to -I option; must be numeric\n",
120 ul = strtoul(optarg, &cp, 0);
123 "Bad argument %s to -t option; must be numeric\n",
136 if (argc != 0 || errs)
138 /* check for mutually exclusive options */
140 if (fname || quiet || recover)
148 if ((of = fopen(fname, "w")) == NULL)
149 err(EX_OSERR, "cannot create output file %s", fname);
152 if ((fd = open(_devname, O_RDONLY)) == -1)
153 err(EX_OSERR, "cannot open device %s", _devname);
155 return (numids? doreadid(fd, numids, trackno): doread(fd, of, _devname));
159 doread(int fd, FILE *of, const char *_devname)
162 int rv, fdopts, recoverable, nerrs = 0;
163 unsigned int nbytes, tracksize, mediasize, secsize, n;
164 struct fdc_status fdcs;
167 if (ioctl(fd, FD_GTYPE, &fdt) == -1)
168 err(EX_OSERR, "ioctl(FD_GTYPE) failed -- not a floppy?");
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");
177 fprintf(stderr, "Reading %d * %d * %d * %d medium at %s\n",
178 fdt.tracks, fdt.heads, fdt.sectrac, secsize, _devname);
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);
186 warnx("premature EOF after %u bytes", nbytes);
190 if ((unsigned)rv == tracksize) {
193 fprintf(stderr, "%5d KB\r", nbytes / 1024);
194 fwrite(trackbuf, sizeof(unsigned char), rv, of);
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) {
207 fprintf(stderr, "%5d KB\r",
209 fwrite(trackbuf, sizeof(unsigned char),
218 perror("non-IO error");
222 if (ioctl(fd, FD_GSTAT, &fdcs) == -1)
224 "floppy IO error, but no FDC status");
226 recoverable = fdcs.status[2] &
229 printstatus(&fdcs, 0);
232 fputs("not ", stderr);
233 fputs("recoverable)", stderr);
241 memset(trackbuf, fillbyte, secsize);
243 fdopts |= FDOPT_NOERROR;
244 if (ioctl(fd, FD_SOPTS,
247 "ioctl(fd, FD_SOPTS, FDOPT_NOERROR)");
248 rv = read(fd, trackbuf,
250 if ((unsigned)rv != secsize)
252 "read() with FDOPT_NOERROR still fails");
253 fdopts &= ~FDOPT_NOERROR;
254 (void)ioctl(fd, FD_SOPTS,
265 " data @ %#x ... %#x\n",
267 nbytes + secsize - 1);
270 fwrite(trackbuf, sizeof(unsigned char),
275 errx(EX_OSERR, "unexpected read() result: %d",
279 if ((unsigned)rv < tracksize) {
280 /* should not happen */
283 fprintf(stderr, "\nshort after %5d KB\r",
285 fwrite(trackbuf, sizeof(unsigned char), rv, of);
294 fprintf(stderr, "%d error%s\n",
295 nerrs, nerrs > 1? "s": "");
298 return (nerrs? EX_IOERR: EX_OK);
302 doreadid(int fd, unsigned int numids, unsigned int trackno)
306 struct fdc_readid info;
307 struct fdc_status fdcs;
310 if (ioctl(fd, FD_GTYPE, &fdt) == -1)
311 err(EX_OSERR, "ioctl(FD_GTYPE) failed -- not a floppy?");
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);
321 perror("non-IO error");
324 if (ioctl(fd, FD_GSTAT, &fdcs) == -1)
326 "floppy IO error, but no FDC status");
327 printstatus(&fdcs, 0);