2 * Copyright (C) 1992-1994,2001 by Joerg Wunsch, Dresden
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
23 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/types.h>
30 #include <sys/fdcio.h>
48 format_track(int fd, int cyl, int secs, int head, int rate,
49 int gaplen, int secsize, int fill, int interleave,
53 int i, j, il[FD_MAX_NSEC + 1];
55 memset(il, 0, sizeof il);
56 for(j = 0, i = 1 + offset; i <= secs + offset; i++) {
57 while(il[(j % secs) + 1])
59 il[(j % secs) + 1] = i;
63 f.format_version = FD_FORMAT_VERSION;
66 f.transfer_rate = rate;
68 f.fd_formb_secshift = secsize;
69 f.fd_formb_nsecs = secs;
70 f.fd_formb_gaplen = gaplen;
71 f.fd_formb_fillbyte = fill;
72 for(i = 0; i < secs; i++) {
73 f.fd_formb_cylno(i) = cyl;
74 f.fd_formb_headno(i) = head;
75 f.fd_formb_secno(i) = il[i+1];
76 f.fd_formb_secsize(i) = secsize;
78 (void)ioctl(fd, FD_FORM, (caddr_t)&f);
82 verify_track(int fd, int track, int tracksize)
86 int fdopts = -1, ofdopts, rv = 0;
88 if (ioctl(fd, FD_GOPTS, &fdopts) < 0)
89 warn("warning: ioctl(FD_GOPTS)");
92 fdopts |= FDOPT_NORETRY;
93 (void)ioctl(fd, FD_SOPTS, &fdopts);
96 if (bufsz < tracksize)
97 buf = realloc(buf, bufsz = tracksize);
99 errx(EX_UNAVAILABLE, "out of memory");
100 if (lseek (fd, (long) track * tracksize, 0) < 0)
102 /* try twice reading it, without using the normal retrier */
103 else if (read (fd, buf, tracksize) != tracksize
104 && read (fd, buf, tracksize) != tracksize)
107 (void)ioctl(fd, FD_SOPTS, &ofdopts);
115 "usage: fdformat [-F fill] [-f fmt] [-s fmtstr] [-nqvy] device");
123 reply[sizeof(reply) - 1] = 0;
126 if (!fgets (reply, sizeof(reply) - 1, stdin))
128 for (p=reply; *p==' ' || *p=='\t'; ++p)
130 if (*p=='y' || *p=='Y')
132 if (*p=='n' || *p=='N' || *p=='\n' || *p=='\r')
134 printf("Answer `yes' or `no': ");
139 main(int argc, char **argv)
141 enum fd_drivetype type;
142 struct fd_type fdt, newft, *fdtp;
144 #define MAXPRINTERRS 10
145 struct fdc_status fdcs[MAXPRINTERRS];
146 int format, fill, quiet, verify, verify_only, confirm;
147 int fd, c, i, track, error, tracks_per_dot, bytes_per_track, errs;
149 char *fmtstring, *device;
150 const char *name, *descr;
152 format = quiet = verify_only = confirm = 0;
157 while((c = getopt(argc, argv, "F:f:nqs:vy")) != -1)
159 case 'F': /* fill byte */
160 if (getnum(optarg, &fill)) {
162 "Bad argument %s to -F option; must be numeric\n",
168 case 'f': /* format in kilobytes */
169 if (getnum(optarg, &format)) {
171 "Bad argument %s to -f option; must be numeric\n",
177 case 'n': /* don't verify */
181 case 'q': /* quiet */
185 case 's': /* format string with detailed options */
189 case 'v': /* verify only */
194 case 'y': /* confirm */
202 if(optind != argc - 1)
205 if (stat(argv[optind], &sb) == -1 && errno == ENOENT) {
206 /* try prepending _PATH_DEV */
207 device = malloc(strlen(argv[optind]) + sizeof(_PATH_DEV) + 1);
209 errx(EX_UNAVAILABLE, "out of memory");
210 strcpy(device, _PATH_DEV);
211 strcat(device, argv[optind]);
212 if (stat(device, &sb) == -1) {
214 device = argv[optind]; /* let it fail below */
217 device = argv[optind];
220 if ((fd = open(device, O_RDWR | O_NONBLOCK)) < 0)
221 err(EX_OSERR, "open(%s)", device);
224 * Device initialization.
226 * First, get the device type descriptor. This tells us about
227 * the media geometry data we need to format a medium. It also
228 * lets us know quickly whether the device name actually points
229 * to a floppy disk drive.
231 * Then, obtain any drive options. We're mainly interested to
232 * see whether we're currently working on a device with media
233 * density autoselection (FDOPT_AUTOSEL). Then, we add the
234 * device option to tell the kernel not to log media errors,
235 * since we can handle them ourselves. If the device does
236 * media density autoselection, we then need to set the device
237 * type appropriately, since by opening with O_NONBLOCK we
238 * told the driver to bypass media autoselection (otherwise we
239 * wouldn't stand a chance to format an unformatted or damaged
240 * medium). We do not attempt to set the media type on any
241 * other devices since this is a privileged operation. For the
242 * same reason, specifying -f and -s options is only possible
243 * for autoselecting devices.
245 * Finally, we are ready to turn off O_NONBLOCK, and start to
246 * actually format something.
248 if(ioctl(fd, FD_GTYPE, &fdt) < 0)
249 errx(EX_OSERR, "not a floppy disk: %s", device);
250 if (ioctl(fd, FD_GDTYPE, &type) == -1)
251 err(EX_OSERR, "ioctl(FD_GDTYPE)");
253 getname(type, &name, &descr);
254 fdtp = get_fmt(format, type);
257 "unknown format %d KB for drive type %s",
262 parse_fmt(fmtstring, type, fdt, &newft);
265 if (ioctl(fd, FD_STYPE, &fdt) < 0)
266 err(EX_OSERR, "ioctl(FD_STYPE)");
267 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
268 err(EX_OSERR, "fcntl(F_GETFL)");
269 flags &= ~O_NONBLOCK;
270 if (fcntl(fd, F_SETFL, flags) == -1)
271 err(EX_OSERR, "fcntl(F_SETFL)");
273 bytes_per_track = fdt.sectrac * (128 << fdt.secsize);
275 /* XXX 20/40 = 0.5 */
276 tracks_per_dot = (fdt.tracks * fdt.heads + 20) / 40;
280 printf("Verify %dK floppy `%s'.\n",
281 fdt.tracks * fdt.heads * bytes_per_track / 1024,
284 else if(!quiet && !confirm) {
285 printf("Format %dK floppy `%s'? (y/n): ",
286 fdt.tracks * fdt.heads * bytes_per_track / 1024,
289 printf("Not confirmed.\n");
290 return (EX_UNAVAILABLE);
298 printf("Processing ");
299 for (i = 0; i < (fdt.tracks * fdt.heads) / tracks_per_dot; i++)
301 printf("\rProcessing ");
307 for (track = 0; track < fdt.tracks * fdt.heads; track++) {
309 format_track(fd, track / fdt.heads, fdt.sectrac,
310 track % fdt.heads, fdt.trans, fdt.f_gap,
311 fdt.secsize, fill, fdt.f_inter,
312 track % fdt.heads? fdt.offset_side2: 0);
313 if(!quiet && !((track + 1) % tracks_per_dot)) {
319 if (verify_track(fd, track, bytes_per_track) < 0) {
321 if (errs < MAXPRINTERRS && errno == EIO) {
322 if (ioctl(fd, FD_GSTAT, fdcs + errs) ==
325 "floppy IO error, but no FDC status");
329 if(!quiet && !((track + 1) % tracks_per_dot)) {
345 if (!quiet && errs) {
347 fprintf(stderr, "Errors encountered:\nCyl Head Sect Error\n");
348 for (i = 0; i < errs && i < MAXPRINTERRS; i++) {
349 fprintf(stderr, " %2d %2d %2d ",
350 fdcs[i].status[3], fdcs[i].status[4],
352 printstatus(fdcs + i, 1);
355 if (errs >= MAXPRINTERRS)
356 fprintf(stderr, "(Further errors not printed.)\n");