コード例 #1
0
void format_device(unsigned drive, unsigned type, int verify)
{
	int ffd, vfd;
	char *fmt_dev, *ver_dev;
	struct stat st;
	unsigned cyl, head;
	unsigned nr_cyls;
	type_parameters_t *tparams= &parameters[type - 1];
	int verbose= isatty(1);

	fmt_dev= tmpnam(nil);

	if (mknod(fmt_dev, S_IFBLK | 0700, fl_makedev(drive, type, 1)) < 0) {
		fprintf(stderr, "format: making format device failed: %s\n",
			strerror(errno));
		exit(1);
	}

	if ((ffd= open(fmt_dev, O_WRONLY)) < 0 || fstat(ffd, &st) < 0) {
		report(fmt_dev);
		(void) unlink(fmt_dev);
		exit(1);
	}

	(void) unlink(fmt_dev);

	if (st.st_rdev != fl_makedev(drive, type, 1)) {
		/* Someone is trying to trick me. */
		exit(1);
	}

	if (verify) {
		ver_dev= tmpnam(nil);

		if (mknod(ver_dev, S_IFBLK | 0700, fl_makedev(drive, type, 0))
									< 0) {
			fprintf(stderr,
				"format: making verify device failed: %s\n",
				strerror(errno));
			exit(1);
		}

		if ((vfd= open(ver_dev, O_RDONLY)) < 0) {
			report(ver_dev);
			(void) unlink(ver_dev);
			exit(1);
		}

		(void) unlink(ver_dev);
	}

	nr_cyls= tparams->media_size * (1024 / SECTOR_SIZE) / NR_HEADS
				/ tparams->fmt_params.sectors_per_track;

	if (verbose) {
		printf("Formatting a %uk diskette in a %uk drive\n",
			tparams->media_size, tparams->drive_size);
	}

	for (cyl= 0; cyl < nr_cyls; cyl++) {
		for (head= 0; head < NR_HEADS; head++) {
			if (verbose) {
				printf(" Cyl. %2u, Head %u\r", cyl, head);
				fflush(stdout);
			}
			/* After formatting a track we are too late to format
			 * the next track.  So we can sleep at most 1/6 sec to
			 * allow the above printf to get displayed before we
			 * lock Minix into the floppy driver again.
			 */
			if (verbose) usleep(50000);	/* 1/20 sec will do. */
			format_track(ffd, type, cyl, head);
			if (verify) verify_track(vfd, type, cyl, head);
		}
	}
	if (verbose) fputc('\n', stdout);
}
コード例 #2
0
int
main(int argc, char **argv)
{
	enum fd_drivetype type;
	struct fd_type fdt, newft, *fdtp;
	struct stat sb;
#define MAXPRINTERRS 10
	struct fdc_status fdcs[MAXPRINTERRS];
	int format, fill, quiet, verify, verify_only, confirm;
	int fd, c, i, track, error, tracks_per_dot, bytes_per_track, errs;
	int flags;
	char *fmtstring, *device;
	const char *name, *descr;

	format = quiet = verify_only = confirm = 0;
	verify = 1;
	fill = 0xf6;
	fmtstring = 0;

	while((c = getopt(argc, argv, "F:f:nqs:vy")) != -1)
		switch(c) {
		case 'F':	/* fill byte */
			if (getnum(optarg, &fill)) {
				fprintf(stderr,
			"Bad argument %s to -F option; must be numeric\n",
					optarg);
				usage();
			}
			break;

		case 'f':	/* format in kilobytes */
			if (getnum(optarg, &format)) {
				fprintf(stderr,
			"Bad argument %s to -f option; must be numeric\n",
					optarg);
				usage();
			}
			break;

		case 'n':	/* don't verify */
			verify = 0;
			break;

		case 'q':	/* quiet */
			quiet = 1;
			break;

		case 's':	/* format string with detailed options */
			fmtstring = optarg;
			break;

		case 'v':	/* verify only */
			verify = 1;
			verify_only = 1;
			break;

		case 'y':	/* confirm */
			confirm = 1;
			break;

		default:
			usage();
		}

	if(optind != argc - 1)
		usage();

	if (stat(argv[optind], &sb) == -1 && errno == ENOENT) {
		/* try prepending _PATH_DEV */
		device = malloc(strlen(argv[optind]) + sizeof(_PATH_DEV) + 1);
		if (device == 0)
			errx(EX_UNAVAILABLE, "out of memory");
		strcpy(device, _PATH_DEV);
		strcat(device, argv[optind]);
		if (stat(device, &sb) == -1) {
			free(device);
			device = argv[optind]; /* let it fail below */
		}
	} else {
		device = argv[optind];
	}

	if ((fd = open(device, O_RDWR | O_NONBLOCK)) < 0)
		err(EX_OSERR, "open(%s)", device);

	/*
	 * Device initialization.
	 *
	 * First, get the device type descriptor.  This tells us about
	 * the media geometry data we need to format a medium.  It also
	 * lets us know quickly whether the device name actually points
	 * to a floppy disk drive.
	 *
	 * Then, obtain any drive options.  We're mainly interested to
	 * see whether we're currently working on a device with media
	 * density autoselection (FDOPT_AUTOSEL).  Then, we add the
	 * device option to tell the kernel not to log media errors,
	 * since we can handle them ourselves.  If the device does
	 * media density autoselection, we then need to set the device
	 * type appropriately, since by opening with O_NONBLOCK we
	 * told the driver to bypass media autoselection (otherwise we
	 * wouldn't stand a chance to format an unformatted or damaged
	 * medium).  We do not attempt to set the media type on any
	 * other devices since this is a privileged operation.  For the
	 * same reason, specifying -f and -s options is only possible
	 * for autoselecting devices.
	 *
	 * Finally, we are ready to turn off O_NONBLOCK, and start to
	 * actually format something.
	 */
	if(ioctl(fd, FD_GTYPE, &fdt) < 0)
		errx(EX_OSERR, "not a floppy disk: %s", device);
	if (ioctl(fd, FD_GDTYPE, &type) == -1)
		err(EX_OSERR, "ioctl(FD_GDTYPE)");
	if (format) {
		getname(type, &name, &descr);
		fdtp = get_fmt(format, type);
		if (fdtp == 0)
			errx(EX_USAGE,
			    "unknown format %d KB for drive type %s",
			     format, name);
		fdt = *fdtp;
	}
	if (fmtstring) {
		parse_fmt(fmtstring, type, fdt, &newft);
		fdt = newft;
	}
	if (ioctl(fd, FD_STYPE, &fdt) < 0)
		err(EX_OSERR, "ioctl(FD_STYPE)");
	if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
		err(EX_OSERR, "fcntl(F_GETFL)");
	flags &= ~O_NONBLOCK;
	if (fcntl(fd, F_SETFL, flags) == -1)
		err(EX_OSERR, "fcntl(F_SETFL)");

	bytes_per_track = fdt.sectrac * (128 << fdt.secsize);

	/* XXX  20/40 = 0.5 */
	tracks_per_dot = (fdt.tracks * fdt.heads + 20) / 40;

	if (verify_only) {
		if(!quiet)
			printf("Verify %dK floppy `%s'.\n",
				fdt.tracks * fdt.heads * bytes_per_track / 1024,
				device);
	}
	else if(!quiet && !confirm) {
		printf("Format %dK floppy `%s'? (y/n): ",
			fdt.tracks * fdt.heads * bytes_per_track / 1024,
			device);
		if(!yes()) {
			printf("Not confirmed.\n");
			return (EX_UNAVAILABLE);
		}
	}

	/*
	 * Formatting.
	 */
	if(!quiet) {
		printf("Processing ");
		for (i = 0; i < (fdt.tracks * fdt.heads) / tracks_per_dot; i++)
			putchar('-');
		printf("\rProcessing ");
		fflush(stdout);
	}

	error = errs = 0;

	for (track = 0; track < fdt.tracks * fdt.heads; track++) {
		if (!verify_only) {
			format_track(fd, track / fdt.heads, fdt.sectrac,
				track % fdt.heads, fdt.trans, fdt.f_gap,
				fdt.secsize, fill, fdt.f_inter,
				track % fdt.heads? fdt.offset_side2: 0);
			if(!quiet && !((track + 1) % tracks_per_dot)) {
				putchar('F');
				fflush(stdout);
			}
		}
		if (verify) {
			if (verify_track(fd, track, bytes_per_track) < 0) {
				error = 1;
				if (errs < MAXPRINTERRS && errno == EIO) {
					if (ioctl(fd, FD_GSTAT, fdcs + errs) ==
					    -1)
						errx(EX_IOERR,
					"floppy IO error, but no FDC status");
					errs++;
				}
			}
			if(!quiet && !((track + 1) % tracks_per_dot)) {
				if (!verify_only)
					putchar('\b');
				if (error) {
					putchar('E');
					error = 0;
				}
				else
					putchar('V');
				fflush(stdout);
			}
		}
	}
	if(!quiet)
		printf(" done.\n");

	if (!quiet && errs) {
		fflush(stdout);
		fprintf(stderr, "Errors encountered:\nCyl Head Sect   Error\n");
		for (i = 0; i < errs && i < MAXPRINTERRS; i++) {
			fprintf(stderr, " %2d   %2d   %2d   ",
				fdcs[i].status[3], fdcs[i].status[4],
				fdcs[i].status[5]);
			printstatus(fdcs + i, 1);
			putc('\n', stderr);
		}
		if (errs >= MAXPRINTERRS)
			fprintf(stderr, "(Further errors not printed.)\n");
	}

	return errs != 0;
}