2001-05-14 20:22:49 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2001 Joerg Wunsch
|
|
|
|
*
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2001-06-06 06:16:19 +00:00
|
|
|
#include <sys/fdcio.h>
|
2001-05-14 20:22:49 +00:00
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <paths.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sysexits.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2001-06-04 21:16:28 +00:00
|
|
|
#include <dev/ic/nec765.h>
|
2001-05-14 20:22:49 +00:00
|
|
|
|
2001-07-02 21:21:58 +00:00
|
|
|
#include "fdutil.h"
|
|
|
|
|
2001-05-14 20:22:49 +00:00
|
|
|
int quiet, recover;
|
|
|
|
unsigned char fillbyte = 0xf0; /* "foo" */
|
|
|
|
|
2005-01-08 15:46:06 +00:00
|
|
|
int doread(int fd, FILE *of, const char *_devname);
|
2001-06-26 22:17:21 +00:00
|
|
|
int doreadid(int fd, unsigned int numids, unsigned int trackno);
|
2001-05-14 20:22:49 +00:00
|
|
|
void usage(void);
|
|
|
|
|
|
|
|
void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
errx(EX_USAGE,
|
2001-06-26 22:17:21 +00:00
|
|
|
"usage: fdread [-qr] [-d device] [-f fillbyte]\n"
|
|
|
|
" fdread [-d device] -I numids [-t trackno]");
|
2001-05-14 20:22:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int c, errs = 0;
|
2001-06-26 22:17:21 +00:00
|
|
|
unsigned int numids = 0, trackno = 0;
|
2005-01-08 15:46:06 +00:00
|
|
|
const char *fname = 0, *_devname = "/dev/fd0";
|
2001-05-14 20:22:49 +00:00
|
|
|
char *cp;
|
|
|
|
FILE *of = stdout;
|
|
|
|
int fd;
|
|
|
|
unsigned long ul;
|
|
|
|
|
2001-06-26 22:17:21 +00:00
|
|
|
while ((c = getopt(argc, argv, "d:f:I:o:qrt:")) != -1)
|
2001-05-14 20:22:49 +00:00
|
|
|
switch (c) {
|
|
|
|
case 'd':
|
2005-01-08 15:46:06 +00:00
|
|
|
_devname = optarg;
|
2001-05-14 20:22:49 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
|
|
|
ul = strtoul(optarg, &cp, 0);
|
|
|
|
if (*cp != '\0') {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Bad argument %s to -f option; must be numeric\n",
|
|
|
|
optarg);
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
if (ul > 0xff)
|
|
|
|
warnx(
|
|
|
|
"Warning: fillbyte %#lx too large, truncating\n",
|
|
|
|
ul);
|
|
|
|
fillbyte = ul & 0xff;
|
|
|
|
break;
|
|
|
|
|
2001-06-26 22:17:21 +00:00
|
|
|
case 'I':
|
|
|
|
ul = strtoul(optarg, &cp, 0);
|
|
|
|
if (*cp != '\0') {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Bad argument %s to -I option; must be numeric\n",
|
|
|
|
optarg);
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
numids = ul;
|
|
|
|
break;
|
|
|
|
|
2001-05-14 20:22:49 +00:00
|
|
|
case 'o':
|
|
|
|
fname = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'q':
|
|
|
|
quiet++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'r':
|
|
|
|
recover++;
|
|
|
|
break;
|
|
|
|
|
2001-06-26 22:17:21 +00:00
|
|
|
case 't':
|
|
|
|
ul = strtoul(optarg, &cp, 0);
|
|
|
|
if (*cp != '\0') {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Bad argument %s to -t option; must be numeric\n",
|
|
|
|
optarg);
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
trackno = ul;
|
|
|
|
break;
|
|
|
|
|
2001-05-14 20:22:49 +00:00
|
|
|
default:
|
|
|
|
errs++;
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (argc != 0 || errs)
|
|
|
|
usage();
|
2001-06-26 22:17:21 +00:00
|
|
|
/* check for mutually exclusive options */
|
|
|
|
if (numids) {
|
|
|
|
if (fname || quiet || recover)
|
|
|
|
usage();
|
|
|
|
} else {
|
|
|
|
if (trackno)
|
|
|
|
usage();
|
|
|
|
}
|
2001-05-14 20:22:49 +00:00
|
|
|
|
|
|
|
if (fname) {
|
|
|
|
if ((of = fopen(fname, "w")) == NULL)
|
|
|
|
err(EX_OSERR, "cannot create output file %s", fname);
|
|
|
|
}
|
|
|
|
|
2005-01-08 15:46:06 +00:00
|
|
|
if ((fd = open(_devname, O_RDONLY)) == -1)
|
|
|
|
err(EX_OSERR, "cannot open device %s", _devname);
|
2001-05-14 20:22:49 +00:00
|
|
|
|
2005-01-08 15:46:06 +00:00
|
|
|
return (numids? doreadid(fd, numids, trackno): doread(fd, of, _devname));
|
2001-05-14 20:22:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2005-01-08 15:46:06 +00:00
|
|
|
doread(int fd, FILE *of, const char *_devname)
|
2001-05-14 20:22:49 +00:00
|
|
|
{
|
|
|
|
char *trackbuf;
|
|
|
|
int rv, fdopts, recoverable, nerrs = 0;
|
|
|
|
unsigned int nbytes, tracksize, mediasize, secsize, n;
|
|
|
|
struct fdc_status fdcs;
|
|
|
|
struct fd_type fdt;
|
|
|
|
|
|
|
|
if (ioctl(fd, FD_GTYPE, &fdt) == -1)
|
|
|
|
err(EX_OSERR, "ioctl(FD_GTYPE) failed -- not a floppy?");
|
|
|
|
fdopts = FDOPT_NOERRLOG;
|
|
|
|
if (ioctl(fd, FD_SOPTS, &fdopts) == -1)
|
|
|
|
err(EX_OSERR, "ioctl(FD_SOPTS, FDOPT_NOERRLOG)");
|
|
|
|
|
|
|
|
secsize = 128 << fdt.secsize;
|
|
|
|
tracksize = fdt.sectrac * secsize;
|
|
|
|
mediasize = tracksize * fdt.tracks * fdt.heads;
|
|
|
|
if ((trackbuf = malloc(tracksize)) == 0)
|
|
|
|
errx(EX_TEMPFAIL, "out of memory");
|
|
|
|
|
|
|
|
if (!quiet)
|
|
|
|
fprintf(stderr, "Reading %d * %d * %d * %d medium at %s\n",
|
2005-01-08 15:46:06 +00:00
|
|
|
fdt.tracks, fdt.heads, fdt.sectrac, secsize, _devname);
|
2001-05-14 20:22:49 +00:00
|
|
|
|
|
|
|
for (nbytes = 0; nbytes < mediasize;) {
|
|
|
|
if (lseek(fd, nbytes, SEEK_SET) != nbytes)
|
|
|
|
err(EX_OSERR, "cannot lseek()");
|
|
|
|
rv = read(fd, trackbuf, tracksize);
|
|
|
|
if (rv == 0) {
|
|
|
|
/* EOF? */
|
|
|
|
warnx("premature EOF after %u bytes", nbytes);
|
|
|
|
return (EX_OK);
|
|
|
|
}
|
2005-01-08 15:46:06 +00:00
|
|
|
if ((unsigned)rv == tracksize) {
|
2001-05-14 20:22:49 +00:00
|
|
|
nbytes += rv;
|
|
|
|
if (!quiet)
|
|
|
|
fprintf(stderr, "%5d KB\r", nbytes / 1024);
|
|
|
|
fwrite(trackbuf, sizeof(unsigned char), rv, of);
|
|
|
|
fflush(of);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (rv == -1) {
|
|
|
|
/* fall back reading one sector at a time */
|
|
|
|
for (n = 0; n < tracksize; n += secsize) {
|
|
|
|
if (lseek(fd, nbytes, SEEK_SET) != nbytes)
|
|
|
|
err(EX_OSERR, "cannot lseek()");
|
|
|
|
rv = read(fd, trackbuf, secsize);
|
2005-01-08 15:46:06 +00:00
|
|
|
if ((unsigned) rv == secsize) {
|
2001-05-14 20:22:49 +00:00
|
|
|
nbytes += rv;
|
|
|
|
if (!quiet)
|
|
|
|
fprintf(stderr, "%5d KB\r",
|
|
|
|
nbytes / 1024);
|
|
|
|
fwrite(trackbuf, sizeof(unsigned char),
|
|
|
|
rv, of);
|
|
|
|
fflush(of);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (rv == -1) {
|
|
|
|
if (errno != EIO) {
|
|
|
|
if (!quiet)
|
|
|
|
putc('\n', stderr);
|
|
|
|
perror("non-IO error");
|
|
|
|
return (EX_OSERR);
|
|
|
|
}
|
|
|
|
if (ioctl(fd, FD_GSTAT, &fdcs) == -1)
|
|
|
|
errx(EX_IOERR,
|
|
|
|
"floppy IO error, but no FDC status");
|
|
|
|
nerrs++;
|
|
|
|
recoverable = fdcs.status[2] &
|
|
|
|
NE7_ST2_DD;
|
|
|
|
if (!quiet) {
|
2001-07-02 21:21:58 +00:00
|
|
|
printstatus(&fdcs, 0);
|
2001-05-14 20:22:49 +00:00
|
|
|
fputs(" (", stderr);
|
|
|
|
if (!recoverable)
|
|
|
|
fputs("not ", stderr);
|
|
|
|
fputs("recoverable)", stderr);
|
|
|
|
}
|
|
|
|
if (!recover) {
|
|
|
|
if (!quiet)
|
|
|
|
putc('\n', stderr);
|
|
|
|
return (EX_IOERR);
|
|
|
|
}
|
|
|
|
memset(trackbuf, fillbyte, secsize);
|
|
|
|
if (recoverable) {
|
|
|
|
fdopts |= FDOPT_NOERROR;
|
|
|
|
if (ioctl(fd, FD_SOPTS,
|
|
|
|
&fdopts) == -1)
|
|
|
|
err(EX_OSERR,
|
|
|
|
"ioctl(fd, FD_SOPTS, FDOPT_NOERROR)");
|
|
|
|
rv = read(fd, trackbuf,
|
|
|
|
secsize);
|
2005-01-08 15:46:06 +00:00
|
|
|
if ((unsigned)rv != secsize)
|
2001-05-14 20:22:49 +00:00
|
|
|
err(EX_IOERR,
|
|
|
|
"read() with FDOPT_NOERROR still fails");
|
|
|
|
fdopts &= ~FDOPT_NOERROR;
|
|
|
|
(void)ioctl(fd, FD_SOPTS,
|
|
|
|
&fdopts);
|
|
|
|
}
|
|
|
|
if (!quiet) {
|
|
|
|
if (recoverable)
|
|
|
|
fprintf(stderr,
|
|
|
|
": recovered");
|
|
|
|
else
|
|
|
|
fprintf(stderr,
|
|
|
|
": dummy");
|
|
|
|
fprintf(stderr,
|
|
|
|
" data @ %#x ... %#x\n",
|
|
|
|
nbytes,
|
|
|
|
nbytes + secsize - 1);
|
|
|
|
}
|
|
|
|
nbytes += secsize;
|
|
|
|
fwrite(trackbuf, sizeof(unsigned char),
|
|
|
|
secsize, of);
|
|
|
|
fflush(of);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
errx(EX_OSERR, "unexpected read() result: %d",
|
|
|
|
rv);
|
|
|
|
}
|
|
|
|
}
|
2005-01-08 15:46:06 +00:00
|
|
|
if ((unsigned)rv < tracksize) {
|
|
|
|
/* should not happen */
|
|
|
|
nbytes += rv;
|
|
|
|
if (!quiet)
|
|
|
|
fprintf(stderr, "\nshort after %5d KB\r",
|
|
|
|
nbytes / 1024);
|
|
|
|
fwrite(trackbuf, sizeof(unsigned char), rv, of);
|
|
|
|
fflush(of);
|
|
|
|
continue;
|
|
|
|
}
|
2001-05-14 20:22:49 +00:00
|
|
|
}
|
|
|
|
if (!quiet) {
|
|
|
|
putc('\n', stderr);
|
|
|
|
if (nerrs)
|
|
|
|
fprintf(stderr, "%d error%s\n",
|
|
|
|
nerrs, nerrs > 1? "s": "");
|
|
|
|
}
|
|
|
|
|
|
|
|
return (nerrs? EX_IOERR: EX_OK);
|
|
|
|
}
|
|
|
|
|
2001-06-26 22:17:21 +00:00
|
|
|
int
|
|
|
|
doreadid(int fd, unsigned int numids, unsigned int trackno)
|
|
|
|
{
|
|
|
|
int rv = 0, status, fdopts;
|
|
|
|
unsigned int i;
|
|
|
|
struct fdc_readid info;
|
|
|
|
struct fdc_status fdcs;
|
|
|
|
struct fd_type fdt;
|
|
|
|
|
|
|
|
if (ioctl(fd, FD_GTYPE, &fdt) == -1)
|
|
|
|
err(EX_OSERR, "ioctl(FD_GTYPE) failed -- not a floppy?");
|
|
|
|
|
|
|
|
fdopts = FDOPT_NOERRLOG;
|
|
|
|
if (ioctl(fd, FD_SOPTS, &fdopts) == -1)
|
|
|
|
err(EX_OSERR, "ioctl(FD_SOPTS, FDOPT_NOERRLOG)");
|
|
|
|
|
|
|
|
for (i = 0; i < numids; i++) {
|
|
|
|
info.cyl = trackno / fdt.heads;
|
|
|
|
info.head = fdt.heads > 1? trackno % fdt.heads: 0;
|
|
|
|
if ((status = ioctl(fd, FD_READID, &info)) == 0) {
|
|
|
|
printf("C = %d, H = %d, R = %d, N = %d\n",
|
|
|
|
info.cyl, info.head, info.sec, info.secshift);
|
|
|
|
} else {
|
|
|
|
if (errno != EIO) {
|
|
|
|
perror("non-IO error");
|
|
|
|
return (EX_OSERR);
|
|
|
|
}
|
|
|
|
if (ioctl(fd, FD_GSTAT, &fdcs) == -1)
|
|
|
|
errx(EX_IOERR,
|
|
|
|
"floppy IO error, but no FDC status");
|
2001-07-02 21:21:58 +00:00
|
|
|
printstatus(&fdcs, 0);
|
2001-06-26 22:17:21 +00:00
|
|
|
putc('\n', stderr);
|
|
|
|
rv = EX_IOERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (rv);
|
|
|
|
}
|