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>
48 unsigned char fillbyte = 0xf0; /* "foo" */
50 int doread(int fd, FILE *of, const char *_devname);
51 int doreadid(int fd, unsigned int numids, unsigned int trackno);
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_RDWR)) == -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?");
169 fdopts = FDOPT_NOERRLOG;
170 if (ioctl(fd, FD_SOPTS, &fdopts) == -1)
171 err(EX_OSERR, "ioctl(FD_SOPTS, FDOPT_NOERRLOG)");
173 secsize = 128 << fdt.secsize;
174 tracksize = fdt.sectrac * secsize;
175 mediasize = tracksize * fdt.tracks * fdt.heads;
176 if ((trackbuf = malloc(tracksize)) == 0)
177 errx(EX_TEMPFAIL, "out of memory");
180 fprintf(stderr, "Reading %d * %d * %d * %d medium at %s\n",
181 fdt.tracks, fdt.heads, fdt.sectrac, secsize, _devname);
183 for (nbytes = 0; nbytes < mediasize;) {
184 if (lseek(fd, nbytes, SEEK_SET) != nbytes)
185 err(EX_OSERR, "cannot lseek()");
186 rv = read(fd, trackbuf, tracksize);
189 warnx("premature EOF after %u bytes", nbytes);
192 if ((unsigned)rv == tracksize) {
195 fprintf(stderr, "%5d KB\r", nbytes / 1024);
196 fwrite(trackbuf, sizeof(unsigned char), rv, of);
201 /* fall back reading one sector at a time */
202 for (n = 0; n < tracksize; n += secsize) {
203 if (lseek(fd, nbytes, SEEK_SET) != nbytes)
204 err(EX_OSERR, "cannot lseek()");
205 rv = read(fd, trackbuf, secsize);
206 if ((unsigned) rv == secsize) {
209 fprintf(stderr, "%5d KB\r",
211 fwrite(trackbuf, sizeof(unsigned char),
220 perror("non-IO error");
223 if (ioctl(fd, FD_GSTAT, &fdcs) == -1)
225 "floppy IO error, but no FDC status");
227 recoverable = fdcs.status[2] &
230 printstatus(&fdcs, 0);
233 fputs("not ", stderr);
234 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);
293 fprintf(stderr, "%d error%s\n",
294 nerrs, nerrs > 1? "s": "");
297 return (nerrs? EX_IOERR: EX_OK);
301 doreadid(int fd, unsigned int numids, unsigned int trackno)
305 struct fdc_readid info;
306 struct fdc_status fdcs;
309 if (ioctl(fd, FD_GTYPE, &fdt) == -1)
310 err(EX_OSERR, "ioctl(FD_GTYPE) failed -- not a floppy?");
312 fdopts = FDOPT_NOERRLOG;
313 if (ioctl(fd, FD_SOPTS, &fdopts) == -1)
314 err(EX_OSERR, "ioctl(FD_SOPTS, FDOPT_NOERRLOG)");
316 for (i = 0; i < numids; i++) {
317 info.cyl = trackno / fdt.heads;
318 info.head = fdt.heads > 1? trackno % fdt.heads: 0;
319 if (ioctl(fd, FD_READID, &info) == 0) {
320 printf("C = %d, H = %d, R = %d, N = %d\n",
321 info.cyl, info.head, info.sec, info.secshift);
324 perror("non-IO error");
327 if (ioctl(fd, FD_GSTAT, &fdcs) == -1)
329 "floppy IO error, but no FDC status");
330 printstatus(&fdcs, 0);