2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2001 Joerg Wunsch
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/types.h>
33 #include <sys/fdcio.h>
45 #include <dev/ic/nec765.h>
49 static int quiet, recover;
50 static unsigned char fillbyte = 0xf0; /* "foo" */
52 static int doread(int fd, FILE *of, const char *_devname);
53 static int doreadid(int fd, unsigned int numids, unsigned int trackno);
54 static void usage(void);
61 "usage: fdread [-qr] [-d device] [-f fillbyte]\n"
62 " fdread [-d device] -I numids [-t trackno]");
67 main(int argc, char **argv)
70 unsigned int numids = 0, trackno = 0;
71 const char *fname = 0, *_devname = "/dev/fd0";
77 while ((c = getopt(argc, argv, "d:f:I:o:qrt:")) != -1)
84 ul = strtoul(optarg, &cp, 0);
87 "Bad argument %s to -f option; must be numeric\n",
93 "Warning: fillbyte %#lx too large, truncating\n",
99 ul = strtoul(optarg, &cp, 0);
102 "Bad argument %s to -I option; must be numeric\n",
122 ul = strtoul(optarg, &cp, 0);
125 "Bad argument %s to -t option; must be numeric\n",
138 if (argc != 0 || errs)
140 /* check for mutually exclusive options */
142 if (fname || quiet || recover)
150 if ((of = fopen(fname, "w")) == NULL)
151 err(EX_OSERR, "cannot create output file %s", fname);
154 if ((fd = open(_devname, O_RDONLY)) == -1)
155 err(EX_OSERR, "cannot open device %s", _devname);
157 return (numids? doreadid(fd, numids, trackno): doread(fd, of, _devname));
161 doread(int fd, FILE *of, const char *_devname)
164 int rv, fdopts, recoverable, nerrs = 0;
165 unsigned int nbytes, tracksize, mediasize, secsize, n;
166 struct fdc_status fdcs;
169 if (ioctl(fd, FD_GTYPE, &fdt) == -1)
170 err(EX_OSERR, "ioctl(FD_GTYPE) failed -- not a floppy?");
172 secsize = 128 << fdt.secsize;
173 tracksize = fdt.sectrac * secsize;
174 mediasize = tracksize * fdt.tracks * fdt.heads;
175 if ((trackbuf = malloc(tracksize)) == NULL)
176 errx(EX_TEMPFAIL, "out of memory");
179 fprintf(stderr, "Reading %d * %d * %d * %d medium at %s\n",
180 fdt.tracks, fdt.heads, fdt.sectrac, secsize, _devname);
182 for (nbytes = 0; nbytes < mediasize;) {
183 if (lseek(fd, nbytes, SEEK_SET) != nbytes)
184 err(EX_OSERR, "cannot lseek()");
185 rv = read(fd, trackbuf, tracksize);
188 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");
224 if (ioctl(fd, FD_GSTAT, &fdcs) == -1)
226 "floppy IO error, but no FDC status");
228 recoverable = fdcs.status[2] &
231 printstatus(&fdcs, 0);
234 fputs("not ", stderr);
235 fputs("recoverable)", stderr);
243 memset(trackbuf, fillbyte, secsize);
245 fdopts |= FDOPT_NOERROR;
246 if (ioctl(fd, FD_SOPTS,
249 "ioctl(fd, FD_SOPTS, FDOPT_NOERROR)");
250 rv = read(fd, trackbuf,
252 if ((unsigned)rv != secsize)
254 "read() with FDOPT_NOERROR still fails");
255 fdopts &= ~FDOPT_NOERROR;
256 (void)ioctl(fd, FD_SOPTS,
267 " data @ %#x ... %#x\n",
269 nbytes + secsize - 1);
272 fwrite(trackbuf, sizeof(unsigned char),
277 errx(EX_OSERR, "unexpected read() result: %d",
281 if ((unsigned)rv < tracksize) {
282 /* should not happen */
285 fprintf(stderr, "\nshort after %5d KB\r",
287 fwrite(trackbuf, sizeof(unsigned char), rv, of);
296 fprintf(stderr, "%d error%s\n",
297 nerrs, nerrs > 1? "s": "");
300 return (nerrs? EX_IOERR: EX_OK);
304 doreadid(int fd, unsigned int numids, unsigned int trackno)
308 struct fdc_readid info;
309 struct fdc_status fdcs;
312 if (ioctl(fd, FD_GTYPE, &fdt) == -1)
313 err(EX_OSERR, "ioctl(FD_GTYPE) failed -- not a floppy?");
315 for (i = 0; i < numids; i++) {
316 info.cyl = trackno / fdt.heads;
317 info.head = fdt.heads > 1? trackno % fdt.heads: 0;
318 if (ioctl(fd, FD_READID, &info) == 0) {
319 printf("C = %d, H = %d, R = %d, N = %d\n",
320 info.cyl, info.head, info.sec, info.secshift);
323 perror("non-IO error");
326 if (ioctl(fd, FD_GSTAT, &fdcs) == -1)
328 "floppy IO error, but no FDC status");
329 printstatus(&fdcs, 0);