1b97809a7SJoerg Wunsch /* 2b97809a7SJoerg Wunsch * Copyright (c) 2001 Joerg Wunsch 3b97809a7SJoerg Wunsch * 4b97809a7SJoerg Wunsch * All rights reserved. 5b97809a7SJoerg Wunsch * 6b97809a7SJoerg Wunsch * Redistribution and use in source and binary forms, with or without 7b97809a7SJoerg Wunsch * modification, are permitted provided that the following conditions 8b97809a7SJoerg Wunsch * are met: 9b97809a7SJoerg Wunsch * 1. Redistributions of source code must retain the above copyright 10b97809a7SJoerg Wunsch * notice, this list of conditions and the following disclaimer. 11b97809a7SJoerg Wunsch * 2. Redistributions in binary form must reproduce the above copyright 12b97809a7SJoerg Wunsch * notice, this list of conditions and the following disclaimer in the 13b97809a7SJoerg Wunsch * documentation and/or other materials provided with the distribution. 14b97809a7SJoerg Wunsch * 15b97809a7SJoerg Wunsch * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR 16b97809a7SJoerg Wunsch * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17b97809a7SJoerg Wunsch * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18b97809a7SJoerg Wunsch * IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT, 19b97809a7SJoerg Wunsch * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20b97809a7SJoerg Wunsch * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21b97809a7SJoerg Wunsch * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22b97809a7SJoerg Wunsch * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23b97809a7SJoerg Wunsch * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24b97809a7SJoerg Wunsch * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25b97809a7SJoerg Wunsch * 26b97809a7SJoerg Wunsch * $FreeBSD$ 27b97809a7SJoerg Wunsch */ 28b97809a7SJoerg Wunsch 29b97809a7SJoerg Wunsch #include <sys/types.h> 30b97809a7SJoerg Wunsch #include <sys/stat.h> 31b97809a7SJoerg Wunsch 32b97809a7SJoerg Wunsch #include <machine/ioctl_fd.h> 33b97809a7SJoerg Wunsch 34b97809a7SJoerg Wunsch #include <err.h> 35b97809a7SJoerg Wunsch #include <errno.h> 36b97809a7SJoerg Wunsch #include <fcntl.h> 37b97809a7SJoerg Wunsch #include <paths.h> 38b97809a7SJoerg Wunsch #include <stdio.h> 39b97809a7SJoerg Wunsch #include <stdlib.h> 40b97809a7SJoerg Wunsch #include <string.h> 41b97809a7SJoerg Wunsch #include <sysexits.h> 42b97809a7SJoerg Wunsch #include <unistd.h> 43b97809a7SJoerg Wunsch 44b97809a7SJoerg Wunsch /* XXX -- header file should probably be installed somewhere */ 45b97809a7SJoerg Wunsch #include "/sys/isa/ic/nec765.h" 46b97809a7SJoerg Wunsch 47b97809a7SJoerg Wunsch int quiet, recover; 48b97809a7SJoerg Wunsch unsigned char fillbyte = 0xf0; /* "foo" */ 49b97809a7SJoerg Wunsch 50b97809a7SJoerg Wunsch int doread(int fd, FILE *of, const char *devname); 51b97809a7SJoerg Wunsch void printstatus(struct fdc_status *fdcsp); 52b97809a7SJoerg Wunsch void usage(void); 53b97809a7SJoerg Wunsch 54b97809a7SJoerg Wunsch void 55b97809a7SJoerg Wunsch usage(void) 56b97809a7SJoerg Wunsch { 57b97809a7SJoerg Wunsch 58b97809a7SJoerg Wunsch errx(EX_USAGE, 59b97809a7SJoerg Wunsch "usage: fdread [-qr] [-d device] [-f fillbyte] [-o file]"); 60b97809a7SJoerg Wunsch } 61b97809a7SJoerg Wunsch 62b97809a7SJoerg Wunsch 63b97809a7SJoerg Wunsch int 64b97809a7SJoerg Wunsch main(int argc, char **argv) 65b97809a7SJoerg Wunsch { 66b97809a7SJoerg Wunsch int c, errs = 0; 67b97809a7SJoerg Wunsch const char *fname = 0, *devname = "/dev/fd0"; 68b97809a7SJoerg Wunsch char *cp; 69b97809a7SJoerg Wunsch FILE *of = stdout; 70b97809a7SJoerg Wunsch int fd; 71b97809a7SJoerg Wunsch unsigned long ul; 72b97809a7SJoerg Wunsch 73b97809a7SJoerg Wunsch while ((c = getopt(argc, argv, "d:f:o:qr")) != -1) 74b97809a7SJoerg Wunsch switch (c) { 75b97809a7SJoerg Wunsch case 'd': 76b97809a7SJoerg Wunsch devname = optarg; 77b97809a7SJoerg Wunsch break; 78b97809a7SJoerg Wunsch 79b97809a7SJoerg Wunsch case 'f': 80b97809a7SJoerg Wunsch ul = strtoul(optarg, &cp, 0); 81b97809a7SJoerg Wunsch if (*cp != '\0') { 82b97809a7SJoerg Wunsch fprintf(stderr, 83b97809a7SJoerg Wunsch "Bad argument %s to -f option; must be numeric\n", 84b97809a7SJoerg Wunsch optarg); 85b97809a7SJoerg Wunsch usage(); 86b97809a7SJoerg Wunsch } 87b97809a7SJoerg Wunsch if (ul > 0xff) 88b97809a7SJoerg Wunsch warnx( 89b97809a7SJoerg Wunsch "Warning: fillbyte %#lx too large, truncating\n", 90b97809a7SJoerg Wunsch ul); 91b97809a7SJoerg Wunsch fillbyte = ul & 0xff; 92b97809a7SJoerg Wunsch break; 93b97809a7SJoerg Wunsch 94b97809a7SJoerg Wunsch case 'o': 95b97809a7SJoerg Wunsch fname = optarg; 96b97809a7SJoerg Wunsch break; 97b97809a7SJoerg Wunsch 98b97809a7SJoerg Wunsch case 'q': 99b97809a7SJoerg Wunsch quiet++; 100b97809a7SJoerg Wunsch break; 101b97809a7SJoerg Wunsch 102b97809a7SJoerg Wunsch case 'r': 103b97809a7SJoerg Wunsch recover++; 104b97809a7SJoerg Wunsch break; 105b97809a7SJoerg Wunsch 106b97809a7SJoerg Wunsch default: 107b97809a7SJoerg Wunsch errs++; 108b97809a7SJoerg Wunsch } 109b97809a7SJoerg Wunsch argc -= optind; 110b97809a7SJoerg Wunsch argv += optind; 111b97809a7SJoerg Wunsch 112b97809a7SJoerg Wunsch if (argc != 0 || errs) 113b97809a7SJoerg Wunsch usage(); 114b97809a7SJoerg Wunsch 115b97809a7SJoerg Wunsch if (fname) { 116b97809a7SJoerg Wunsch if ((of = fopen(fname, "w")) == NULL) 117b97809a7SJoerg Wunsch err(EX_OSERR, "cannot create output file %s", fname); 118b97809a7SJoerg Wunsch } 119b97809a7SJoerg Wunsch 120b97809a7SJoerg Wunsch if ((fd = open(devname, O_RDONLY)) == -1) 121b97809a7SJoerg Wunsch err(EX_OSERR, "cannot open device %s", devname); 122b97809a7SJoerg Wunsch 123b97809a7SJoerg Wunsch return (doread(fd, of, devname)); 124b97809a7SJoerg Wunsch } 125b97809a7SJoerg Wunsch 126b97809a7SJoerg Wunsch int 127b97809a7SJoerg Wunsch doread(int fd, FILE *of, const char *devname) 128b97809a7SJoerg Wunsch { 129b97809a7SJoerg Wunsch char *trackbuf; 130b97809a7SJoerg Wunsch int rv, fdopts, recoverable, nerrs = 0; 131b97809a7SJoerg Wunsch unsigned int nbytes, tracksize, mediasize, secsize, n; 132b97809a7SJoerg Wunsch struct fdc_status fdcs; 133b97809a7SJoerg Wunsch struct fd_type fdt; 134b97809a7SJoerg Wunsch 135b97809a7SJoerg Wunsch if (ioctl(fd, FD_GTYPE, &fdt) == -1) 136b97809a7SJoerg Wunsch err(EX_OSERR, "ioctl(FD_GTYPE) failed -- not a floppy?"); 137b97809a7SJoerg Wunsch fdopts = FDOPT_NOERRLOG; 138b97809a7SJoerg Wunsch if (ioctl(fd, FD_SOPTS, &fdopts) == -1) 139b97809a7SJoerg Wunsch err(EX_OSERR, "ioctl(FD_SOPTS, FDOPT_NOERRLOG)"); 140b97809a7SJoerg Wunsch 141b97809a7SJoerg Wunsch secsize = 128 << fdt.secsize; 142b97809a7SJoerg Wunsch tracksize = fdt.sectrac * secsize; 143b97809a7SJoerg Wunsch mediasize = tracksize * fdt.tracks * fdt.heads; 144b97809a7SJoerg Wunsch if ((trackbuf = malloc(tracksize)) == 0) 145b97809a7SJoerg Wunsch errx(EX_TEMPFAIL, "out of memory"); 146b97809a7SJoerg Wunsch 147b97809a7SJoerg Wunsch if (!quiet) 148b97809a7SJoerg Wunsch fprintf(stderr, "Reading %d * %d * %d * %d medium at %s\n", 149b97809a7SJoerg Wunsch fdt.tracks, fdt.heads, fdt.sectrac, secsize, devname); 150b97809a7SJoerg Wunsch 151b97809a7SJoerg Wunsch for (nbytes = 0; nbytes < mediasize;) { 152b97809a7SJoerg Wunsch if (lseek(fd, nbytes, SEEK_SET) != nbytes) 153b97809a7SJoerg Wunsch err(EX_OSERR, "cannot lseek()"); 154b97809a7SJoerg Wunsch rv = read(fd, trackbuf, tracksize); 155b97809a7SJoerg Wunsch if (rv == 0) { 156b97809a7SJoerg Wunsch /* EOF? */ 157b97809a7SJoerg Wunsch warnx("premature EOF after %u bytes", nbytes); 158b97809a7SJoerg Wunsch return (EX_OK); 159b97809a7SJoerg Wunsch } 160b97809a7SJoerg Wunsch if (rv == tracksize) { 161b97809a7SJoerg Wunsch nbytes += rv; 162b97809a7SJoerg Wunsch if (!quiet) 163b97809a7SJoerg Wunsch fprintf(stderr, "%5d KB\r", nbytes / 1024); 164b97809a7SJoerg Wunsch fwrite(trackbuf, sizeof(unsigned char), rv, of); 165b97809a7SJoerg Wunsch fflush(of); 166b97809a7SJoerg Wunsch continue; 167b97809a7SJoerg Wunsch } 168b97809a7SJoerg Wunsch if (rv < tracksize) { 169b97809a7SJoerg Wunsch /* should not happen */ 170b97809a7SJoerg Wunsch nbytes += rv; 171b97809a7SJoerg Wunsch if (!quiet) 172b97809a7SJoerg Wunsch fprintf(stderr, "\nshort after %5d KB\r", 173b97809a7SJoerg Wunsch nbytes / 1024); 174b97809a7SJoerg Wunsch fwrite(trackbuf, sizeof(unsigned char), rv, of); 175b97809a7SJoerg Wunsch fflush(of); 176b97809a7SJoerg Wunsch continue; 177b97809a7SJoerg Wunsch } 178b97809a7SJoerg Wunsch if (rv == -1) { 179b97809a7SJoerg Wunsch /* fall back reading one sector at a time */ 180b97809a7SJoerg Wunsch for (n = 0; n < tracksize; n += secsize) { 181b97809a7SJoerg Wunsch if (lseek(fd, nbytes, SEEK_SET) != nbytes) 182b97809a7SJoerg Wunsch err(EX_OSERR, "cannot lseek()"); 183b97809a7SJoerg Wunsch rv = read(fd, trackbuf, secsize); 184b97809a7SJoerg Wunsch if (rv == secsize) { 185b97809a7SJoerg Wunsch nbytes += rv; 186b97809a7SJoerg Wunsch if (!quiet) 187b97809a7SJoerg Wunsch fprintf(stderr, "%5d KB\r", 188b97809a7SJoerg Wunsch nbytes / 1024); 189b97809a7SJoerg Wunsch fwrite(trackbuf, sizeof(unsigned char), 190b97809a7SJoerg Wunsch rv, of); 191b97809a7SJoerg Wunsch fflush(of); 192b97809a7SJoerg Wunsch continue; 193b97809a7SJoerg Wunsch } 194b97809a7SJoerg Wunsch if (rv == -1) { 195b97809a7SJoerg Wunsch if (errno != EIO) { 196b97809a7SJoerg Wunsch if (!quiet) 197b97809a7SJoerg Wunsch putc('\n', stderr); 198b97809a7SJoerg Wunsch perror("non-IO error"); 199b97809a7SJoerg Wunsch return (EX_OSERR); 200b97809a7SJoerg Wunsch } 201b97809a7SJoerg Wunsch if (ioctl(fd, FD_GSTAT, &fdcs) == -1) 202b97809a7SJoerg Wunsch errx(EX_IOERR, 203b97809a7SJoerg Wunsch "floppy IO error, but no FDC status"); 204b97809a7SJoerg Wunsch nerrs++; 205b97809a7SJoerg Wunsch recoverable = fdcs.status[2] & 206b97809a7SJoerg Wunsch NE7_ST2_DD; 207b97809a7SJoerg Wunsch if (!quiet) { 208b97809a7SJoerg Wunsch printstatus(&fdcs); 209b97809a7SJoerg Wunsch fputs(" (", stderr); 210b97809a7SJoerg Wunsch if (!recoverable) 211b97809a7SJoerg Wunsch fputs("not ", stderr); 212b97809a7SJoerg Wunsch fputs("recoverable)", stderr); 213b97809a7SJoerg Wunsch } 214b97809a7SJoerg Wunsch if (!recover) { 215b97809a7SJoerg Wunsch if (!quiet) 216b97809a7SJoerg Wunsch putc('\n', stderr); 217b97809a7SJoerg Wunsch return (EX_IOERR); 218b97809a7SJoerg Wunsch } 219b97809a7SJoerg Wunsch memset(trackbuf, fillbyte, secsize); 220b97809a7SJoerg Wunsch if (recoverable) { 221b97809a7SJoerg Wunsch fdopts |= FDOPT_NOERROR; 222b97809a7SJoerg Wunsch if (ioctl(fd, FD_SOPTS, 223b97809a7SJoerg Wunsch &fdopts) == -1) 224b97809a7SJoerg Wunsch err(EX_OSERR, 225b97809a7SJoerg Wunsch "ioctl(fd, FD_SOPTS, FDOPT_NOERROR)"); 226b97809a7SJoerg Wunsch rv = read(fd, trackbuf, 227b97809a7SJoerg Wunsch secsize); 228b97809a7SJoerg Wunsch if (rv != secsize) 229b97809a7SJoerg Wunsch err(EX_IOERR, 230b97809a7SJoerg Wunsch "read() with FDOPT_NOERROR still fails"); 231b97809a7SJoerg Wunsch fdopts &= ~FDOPT_NOERROR; 232b97809a7SJoerg Wunsch (void)ioctl(fd, FD_SOPTS, 233b97809a7SJoerg Wunsch &fdopts); 234b97809a7SJoerg Wunsch } 235b97809a7SJoerg Wunsch if (!quiet) { 236b97809a7SJoerg Wunsch if (recoverable) 237b97809a7SJoerg Wunsch fprintf(stderr, 238b97809a7SJoerg Wunsch ": recovered"); 239b97809a7SJoerg Wunsch else 240b97809a7SJoerg Wunsch fprintf(stderr, 241b97809a7SJoerg Wunsch ": dummy"); 242b97809a7SJoerg Wunsch fprintf(stderr, 243b97809a7SJoerg Wunsch " data @ %#x ... %#x\n", 244b97809a7SJoerg Wunsch nbytes, 245b97809a7SJoerg Wunsch nbytes + secsize - 1); 246b97809a7SJoerg Wunsch } 247b97809a7SJoerg Wunsch nbytes += secsize; 248b97809a7SJoerg Wunsch fwrite(trackbuf, sizeof(unsigned char), 249b97809a7SJoerg Wunsch secsize, of); 250b97809a7SJoerg Wunsch fflush(of); 251b97809a7SJoerg Wunsch continue; 252b97809a7SJoerg Wunsch } 253b97809a7SJoerg Wunsch errx(EX_OSERR, "unexpected read() result: %d", 254b97809a7SJoerg Wunsch rv); 255b97809a7SJoerg Wunsch } 256b97809a7SJoerg Wunsch } 257b97809a7SJoerg Wunsch } 258b97809a7SJoerg Wunsch if (!quiet) { 259b97809a7SJoerg Wunsch putc('\n', stderr); 260b97809a7SJoerg Wunsch if (nerrs) 261b97809a7SJoerg Wunsch fprintf(stderr, "%d error%s\n", 262b97809a7SJoerg Wunsch nerrs, nerrs > 1? "s": ""); 263b97809a7SJoerg Wunsch } 264b97809a7SJoerg Wunsch 265b97809a7SJoerg Wunsch return (nerrs? EX_IOERR: EX_OK); 266b97809a7SJoerg Wunsch } 267b97809a7SJoerg Wunsch 268b97809a7SJoerg Wunsch void 269b97809a7SJoerg Wunsch printstatus(struct fdc_status *fdcsp) 270b97809a7SJoerg Wunsch { 271b97809a7SJoerg Wunsch char msgbuf[100]; 272b97809a7SJoerg Wunsch 273b97809a7SJoerg Wunsch fprintf(stderr, 274b97809a7SJoerg Wunsch "\nFDC status ST0=%#x ST1=%#x ST2=%#x C=%u H=%u R=%u N=%u:\n", 275b97809a7SJoerg Wunsch fdcsp->status[0] & 0xff, 276b97809a7SJoerg Wunsch fdcsp->status[1] & 0xff, 277b97809a7SJoerg Wunsch fdcsp->status[2] & 0xff, 278b97809a7SJoerg Wunsch fdcsp->status[3] & 0xff, 279b97809a7SJoerg Wunsch fdcsp->status[4] & 0xff, 280b97809a7SJoerg Wunsch fdcsp->status[5] & 0xff, 281b97809a7SJoerg Wunsch fdcsp->status[6] & 0xff); 282b97809a7SJoerg Wunsch 283b97809a7SJoerg Wunsch if ((fdcsp->status[0] & NE7_ST0_IC_RC) != NE7_ST0_IC_AT) { 284b97809a7SJoerg Wunsch sprintf(msgbuf, "unexcpted interrupt code %#x", 285b97809a7SJoerg Wunsch fdcsp->status[0] & NE7_ST0_IC_RC); 286b97809a7SJoerg Wunsch } else { 287b97809a7SJoerg Wunsch strcpy(msgbuf, "unexpected error code in ST1/ST2"); 288b97809a7SJoerg Wunsch 289b97809a7SJoerg Wunsch if (fdcsp->status[1] & NE7_ST1_EN) 290b97809a7SJoerg Wunsch strcpy(msgbuf, "end of cylinder (wrong format)"); 291b97809a7SJoerg Wunsch else if (fdcsp->status[1] & NE7_ST1_DE) { 292b97809a7SJoerg Wunsch if (fdcsp->status[2] & NE7_ST2_DD) 293b97809a7SJoerg Wunsch strcpy(msgbuf, "CRC error in data field"); 294b97809a7SJoerg Wunsch else 295b97809a7SJoerg Wunsch strcpy(msgbuf, "CRC error in ID field"); 296b97809a7SJoerg Wunsch } else if (fdcsp->status[1] & NE7_ST1_MA) { 297b97809a7SJoerg Wunsch if (fdcsp->status[2] & NE7_ST2_MD) 298b97809a7SJoerg Wunsch strcpy(msgbuf, "no address mark in data field"); 299b97809a7SJoerg Wunsch else 300b97809a7SJoerg Wunsch strcpy(msgbuf, "no address mark in ID field"); 301b97809a7SJoerg Wunsch } else if (fdcsp->status[2] & NE7_ST2_WC) 302b97809a7SJoerg Wunsch strcpy(msgbuf, "wrong cylinder (format mismatch)"); 303b97809a7SJoerg Wunsch else if (fdcsp->status[1] & NE7_ST1_ND) 304b97809a7SJoerg Wunsch strcpy(msgbuf, "no data (sector not found)"); 305b97809a7SJoerg Wunsch } 306b97809a7SJoerg Wunsch fputs(msgbuf, stderr); 307b97809a7SJoerg Wunsch } 308