1 /* fdformat.c - Low-level formats a floppy disk - Werner Almesberger */
3 /* 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
4 * - added Native Language Support
5 * 1999-03-20 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
6 * - more i18n/nls translatable strings marked
8 * 5 July 2003 -- modified for Busybox by Erik Andersen
18 #include <sys/ioctl.h>
22 /* Stuff extracted from linux/fd.h */
23 struct floppy_struct {
24 unsigned int size, /* nr of sectors total */
25 sect, /* sectors per track */
26 head, /* nr of heads */
27 track, /* nr of tracks */
28 stretch; /* !=0 means double track steps */
30 #define FD_SWAPSIDES 2
32 unsigned char gap, /* gap1 size */
34 rate, /* data rate. |= 0x40 for perpendicular */
36 #define FD_SIZECODEMASK 0x38
37 #define FD_SIZECODE(floppy) (((((floppy)->rate&FD_SIZECODEMASK)>> 3)+ 2) %8)
38 #define FD_SECTSIZE(floppy) ( (floppy)->rate & FD_2M ? \
39 512 : 128 << FD_SIZECODE(floppy) )
42 spec1, /* stepping rate, head unload time */
43 fmt_gap; /* gap2 size */
44 const char * name; /* used only for predefined formats */
47 unsigned int device,head,track;
49 #define FDFMTBEG _IO(2,0x47)
50 #define FDFMTTRK _IOW(2,0x48, struct format_descr)
51 #define FDFMTEND _IO(2,0x49)
52 #define FDGETPRM _IOR(2, 0x04, struct floppy_struct)
53 #define FD_FILL_BYTE 0xF6 /* format fill byte. */
55 static void print_and_flush(const char * __restrict format, ...)
59 va_start(arg, format);
60 bb_vfprintf(stdout, format, arg);
65 static void bb_xioctl(int fd, int request, void *argp, const char *string)
67 if (ioctl (fd, request, argp) < 0) {
68 bb_perror_msg_and_die(string);
72 int fdformat_main(int argc,char **argv)
74 int fd, n, cyl, read_bytes, verify;
77 struct floppy_struct param;
78 struct format_descr descr;
83 verify = !bb_getopt_ulflags(argc, argv, "n");
86 /* R_OK is needed for verifying */
87 if (stat(*argv,&st) < 0 || access(*argv,W_OK | R_OK ) < 0) {
88 bb_perror_msg_and_die(*argv);
90 if (!S_ISBLK(st.st_mode)) {
91 bb_error_msg_and_die("%s: not a block device",*argv);
92 /* do not test major - perhaps this was an USB floppy */
96 /* O_RDWR for formatting and verifying */
97 fd = bb_xopen(*argv,O_RDWR );
99 bb_xioctl(fd, FDGETPRM, ¶m, "FDGETPRM");/*original message was: "Could not determine current format type" */
101 print_and_flush("%s-sided, %d tracks, %d sec/track. Total capacity %d kB.\n",
102 (param.head == 2) ? "Double" : "Single",
103 param.track, param.sect, param.size >> 1);
106 print_and_flush("Formatting ... ", NULL);
107 bb_xioctl(fd, FDFMTBEG,NULL,"FDFMTBEG");
110 for (n = 0; n < param.track; n++)
114 bb_xioctl(fd, FDFMTTRK,&descr,"FDFMTTRK");
115 print_and_flush("%3d\b\b\b", n);
116 if (param.head == 2) {
118 bb_xioctl(fd, FDFMTTRK,&descr,"FDFMTTRK");
122 bb_xioctl(fd,FDFMTEND,NULL,"FDFMTEND");
123 print_and_flush("done\n", NULL);
128 n = param.sect*param.head*512;
131 print_and_flush("Verifying ... ", NULL);
132 for (cyl = 0; cyl < param.track; cyl++) {
133 print_and_flush("%3d\b\b\b", cyl);
134 if((read_bytes = safe_read(fd,data,n))!= n ) {
136 bb_perror_msg("Read: ");
138 bb_error_msg_and_die("Problem reading cylinder %d, expected %d, read %d", cyl, n, read_bytes);
140 /* Check backwards so we don't need a counter */
141 while(--read_bytes>=0) {
142 if( data[read_bytes] != FD_FILL_BYTE) {
143 print_and_flush("bad data in cyl %d\nContinuing ... ",cyl);
147 /* There is no point in freeing blocks at the end of a program, because
148 all of the program's space is given back to the system when the process
150 #ifdef CONFIG_FEATURE_CLEAN_UP
153 print_and_flush("done\n", NULL);
155 #ifdef CONFIG_FEATURE_CLEAN_UP
158 /* Don't bother closing. Exit does
159 * that, so we can save a few bytes */