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. */
57 static void format_disk(int ctrl, char *name, struct floppy_struct *param)
59 struct format_descr descr;
62 printf("Formatting ... ");
64 if (ioctl(ctrl,FDFMTBEG,NULL) < 0) {
65 bb_perror_msg_and_die("FDFMTBEG");
67 for (track = 0; track < param->track; track++)
71 if (ioctl(ctrl,FDFMTTRK,(long) &descr) < 0) {
72 bb_perror_msg_and_die("FDFMTTRK");
75 printf("%3d\b\b\b",track);
77 if (param->head == 2) {
79 if (ioctl(ctrl,FDFMTTRK,(long) &descr) < 0) {
80 bb_perror_msg_and_die("FDFMTTRK");
84 if (ioctl(ctrl,FDFMTEND,NULL) < 0) {
85 bb_perror_msg_and_die("FDFMTEND");
90 static void verify_disk(char *name, struct floppy_struct *param)
93 int fd,cyl_size,cyl,count,read_bytes;
95 cyl_size = param->sect*param->head*512;
96 data = xmalloc(cyl_size);
97 printf("Verifying ... ");
99 fd = bb_xopen(name,O_RDONLY);
100 for (cyl = 0; cyl < param->track; cyl++)
102 printf("%3d\b\b\b",cyl);
104 read_bytes = safe_read(fd,data,cyl_size);
105 if(read_bytes != cyl_size) {
107 bb_perror_msg("Read: ");
109 bb_error_msg_and_die("Problem reading cylinder %d, "
110 "expected %d, read %d", cyl, cyl_size, read_bytes);
112 for (count = 0; count < cyl_size; count++)
113 if (data[count] != FD_FILL_BYTE) {
114 printf("bad data in cyl %d\nContinuing ... ",cyl);
123 int fdformat_main(int argc,char **argv)
128 struct floppy_struct param;
133 verify = !bb_getopt_ulflags(argc, argv, "n");
136 if (stat(*argv,&st) < 0 || access(*argv,W_OK) < 0) {
137 bb_perror_msg_and_die(*argv);
139 if (!S_ISBLK(st.st_mode)) {
140 bb_error_msg_and_die("%s: not a block device",*argv);
141 /* do not test major - perhaps this was an USB floppy */
144 ctrl = bb_xopen(*argv,O_WRONLY);
145 if (ioctl(ctrl,FDGETPRM,(long) ¶m) < 0) {
146 bb_perror_msg_and_die("Could not determine current format type");
148 printf("%s-sided, %d tracks, %d sec/track. Total capacity %d kB.\n",
149 (param.head == 2) ? "Double" : "Single",
150 param.track, param.sect,param.size >> 1);
151 format_disk(ctrl, *argv, ¶m);
155 verify_disk(*argv, ¶m);