1 /* vi: set sw=4 ts=4: */
2 /* fdformat.c - Low-level formats a floppy disk - Werner Almesberger
3 * 5 July 2003 -- modified for Busybox by Erik Andersen
5 * Licensed under GPLv2, see file LICENSE in this source tree.
11 /* Stuff extracted from linux/fd.h */
12 struct floppy_struct {
13 unsigned int size, /* nr of sectors total */
14 sect, /* sectors per track */
15 head, /* nr of heads */
16 track, /* nr of tracks */
17 stretch; /* !=0 means double track steps */
19 #define FD_SWAPSIDES 2
21 unsigned char gap, /* gap1 size */
23 rate, /* data rate. |= 0x40 for perpendicular */
25 #define FD_SIZECODEMASK 0x38
26 #define FD_SIZECODE(floppy) (((((floppy)->rate&FD_SIZECODEMASK)>> 3)+ 2) %8)
27 #define FD_SECTSIZE(floppy) ( (floppy)->rate & FD_2M ? \
28 512 : 128 << FD_SIZECODE(floppy) )
31 spec1, /* stepping rate, head unload time */
32 fmt_gap; /* gap2 size */
33 const char * name; /* used only for predefined formats */
36 unsigned int device,head,track;
38 #define FDFMTBEG _IO(2,0x47)
39 #define FDFMTTRK _IOW(2,0x48, struct format_descr)
40 #define FDFMTEND _IO(2,0x49)
41 #define FDGETPRM _IOR(2, 0x04, struct floppy_struct)
42 #define FD_FILL_BYTE 0xF6 /* format fill byte. */
44 int fdformat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
45 int fdformat_main(int argc UNUSED_PARAM, char **argv)
47 int fd, n, cyl, read_bytes, verify;
50 struct floppy_struct param;
51 struct format_descr descr;
53 opt_complementary = "=1"; /* must have 1 param */
54 verify = !getopt32(argv, "n");
58 if (!S_ISBLK(st.st_mode)) {
59 bb_error_msg_and_die("%s: not a block device", *argv);
60 /* do not test major - perhaps this was an USB floppy */
63 /* O_RDWR for formatting and verifying */
64 fd = xopen(*argv, O_RDWR);
66 /* original message was: "Could not determine current format type" */
67 xioctl(fd, FDGETPRM, ¶m);
69 printf("%s-sided, %d tracks, %d sec/track. Total capacity %d kB\n",
70 (param.head == 2) ? "Double" : "Single",
71 param.track, param.sect, param.size >> 1);
74 printf("Formatting... ");
75 xioctl(fd, FDFMTBEG, NULL);
78 for (n = 0; n < param.track; n++) {
81 xioctl(fd, FDFMTTRK, &descr);
82 printf("%3d\b\b\b", n);
83 if (param.head == 2) {
85 xioctl(fd, FDFMTTRK, &descr);
89 xioctl(fd, FDFMTEND, NULL);
95 n = param.sect*param.head*512;
98 printf("Verifying... ");
99 for (cyl = 0; cyl < param.track; cyl++) {
100 printf("%3d\b\b\b", cyl);
101 read_bytes = safe_read(fd, data, n);
102 if (read_bytes != n) {
103 if (read_bytes < 0) {
104 bb_perror_msg(bb_msg_read_error);
106 bb_error_msg_and_die("problem reading cylinder %d, "
107 "expected %d, read %d", cyl, n, read_bytes);
108 // FIXME: maybe better seek & continue??
110 /* Check backwards so we don't need a counter */
111 while (--read_bytes >= 0) {
112 if (data[read_bytes] != FD_FILL_BYTE) {
113 printf("bad data in cyl %d\nContinuing... ", cyl);
117 /* There is no point in freeing blocks at the end of a program, because
118 all of the program's space is given back to the system when the process
121 if (ENABLE_FEATURE_CLEAN_UP) free(data);
126 if (ENABLE_FEATURE_CLEAN_UP) close(fd);
128 /* Don't bother closing. Exit does
129 * that, so we can save a few bytes */