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)) == 0)
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);
189 if ((unsigned)rv == tracksize) {
192 fprintf(stderr, "%5d KB\r", nbytes / 1024);
193 fwrite(trackbuf, sizeof(unsigned char), rv, of);
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) {
206 fprintf(stderr, "%5d KB\r",
208 fwrite(trackbuf, sizeof(unsigned char),
217 perror("non-IO error");
220 if (ioctl(fd, FD_GSTAT, &fdcs) == -1)
222 "floppy IO error, but no FDC status");
224 recoverable = fdcs.status[2] &
227 printstatus(&fdcs, 0);
230 fputs("not ", stderr);
231 fputs("recoverable)", stderr);
238 memset(trackbuf, fillbyte, secsize);
240 fdopts |= FDOPT_NOERROR;
241 if (ioctl(fd, FD_SOPTS,
244 "ioctl(fd, FD_SOPTS, FDOPT_NOERROR)");
245 rv = read(fd, trackbuf,
247 if ((unsigned)rv != secsize)
249 "read() with FDOPT_NOERROR still fails");
250 fdopts &= ~FDOPT_NOERROR;
251 (void)ioctl(fd, FD_SOPTS,
262 " data @ %#x ... %#x\n",
264 nbytes + secsize - 1);
267 fwrite(trackbuf, sizeof(unsigned char),
272 errx(EX_OSERR, "unexpected read() result: %d",
276 if ((unsigned)rv < tracksize) {
277 /* should not happen */
280 fprintf(stderr, "\nshort after %5d KB\r",
282 fwrite(trackbuf, sizeof(unsigned char), rv, of);
290 fprintf(stderr, "%d error%s\n",
291 nerrs, nerrs > 1? "s": "");
294 return (nerrs? EX_IOERR: EX_OK);
298 doreadid(int fd, unsigned int numids, unsigned int trackno)
302 struct fdc_readid info;
303 struct fdc_status fdcs;
306 if (ioctl(fd, FD_GTYPE, &fdt) == -1)
307 err(EX_OSERR, "ioctl(FD_GTYPE) failed -- not a floppy?");
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);
317 perror("non-IO error");
320 if (ioctl(fd, FD_GSTAT, &fdcs) == -1)
322 "floppy IO error, but no FDC status");
323 printstatus(&fdcs, 0);