diff options
author | Eric Andersen <andersen@codepoet.org> | 2003-07-05 07:51:31 +0000 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 2003-07-05 07:51:31 +0000 |
commit | 01c3d40f3c022abc08c14b96fe586a33fce081e8 (patch) | |
tree | 44343baf5a55f3f623770a1097a54eaad112c7f4 /util-linux/fdformat.c | |
parent | a21f4e018a39c08e09d69fae63383000a327716a (diff) | |
download | busybox-w32-01c3d40f3c022abc08c14b96fe586a33fce081e8.tar.gz busybox-w32-01c3d40f3c022abc08c14b96fe586a33fce081e8.tar.bz2 busybox-w32-01c3d40f3c022abc08c14b96fe586a33fce081e8.zip |
Add the fdformat utility, based on a patch from Kent Robotti,
but I then completely reworked the fdformat utility to comply
with the current busybox way of doing things.
Diffstat (limited to 'util-linux/fdformat.c')
-rw-r--r-- | util-linux/fdformat.c | 158 |
1 files changed, 158 insertions, 0 deletions
diff --git a/util-linux/fdformat.c b/util-linux/fdformat.c new file mode 100644 index 000000000..5b926467f --- /dev/null +++ b/util-linux/fdformat.c | |||
@@ -0,0 +1,158 @@ | |||
1 | /* fdformat.c - Low-level formats a floppy disk - Werner Almesberger */ | ||
2 | |||
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 | ||
7 | * | ||
8 | * 5 July 2003 -- modified for Busybox by Erik Andersen | ||
9 | */ | ||
10 | |||
11 | #include <stdio.h> | ||
12 | #include <string.h> | ||
13 | #include <fcntl.h> | ||
14 | #include <errno.h> | ||
15 | #include <unistd.h> | ||
16 | #include <stdlib.h> | ||
17 | #include <sys/stat.h> | ||
18 | #include <sys/ioctl.h> | ||
19 | #include "busybox.h" | ||
20 | |||
21 | |||
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 */ | ||
29 | #define FD_STRETCH 1 | ||
30 | #define FD_SWAPSIDES 2 | ||
31 | |||
32 | unsigned char gap, /* gap1 size */ | ||
33 | |||
34 | rate, /* data rate. |= 0x40 for perpendicular */ | ||
35 | #define FD_2M 0x4 | ||
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) ) | ||
40 | #define FD_PERP 0x40 | ||
41 | |||
42 | spec1, /* stepping rate, head unload time */ | ||
43 | fmt_gap; /* gap2 size */ | ||
44 | const char * name; /* used only for predefined formats */ | ||
45 | }; | ||
46 | struct format_descr { | ||
47 | unsigned int device,head,track; | ||
48 | }; | ||
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. */ | ||
54 | |||
55 | |||
56 | |||
57 | static void format_disk(int ctrl, char *name, struct floppy_struct *param) | ||
58 | { | ||
59 | struct format_descr descr; | ||
60 | int track; | ||
61 | |||
62 | printf("Formatting ... "); | ||
63 | fflush(stdout); | ||
64 | if (ioctl(ctrl,FDFMTBEG,NULL) < 0) { | ||
65 | bb_perror_msg_and_die("FDFMTBEG"); | ||
66 | } | ||
67 | for (track = 0; track < param->track; track++) | ||
68 | { | ||
69 | descr.track = track; | ||
70 | descr.head = 0; | ||
71 | if (ioctl(ctrl,FDFMTTRK,(long) &descr) < 0) { | ||
72 | bb_perror_msg_and_die("FDFMTTRK"); | ||
73 | } | ||
74 | |||
75 | printf("%3d\b\b\b",track); | ||
76 | fflush(stdout); | ||
77 | if (param->head == 2) { | ||
78 | descr.head = 1; | ||
79 | if (ioctl(ctrl,FDFMTTRK,(long) &descr) < 0) { | ||
80 | bb_perror_msg_and_die("FDFMTTRK"); | ||
81 | } | ||
82 | } | ||
83 | } | ||
84 | if (ioctl(ctrl,FDFMTEND,NULL) < 0) { | ||
85 | bb_perror_msg_and_die("FDFMTEND"); | ||
86 | } | ||
87 | printf("done\n"); | ||
88 | } | ||
89 | |||
90 | static void verify_disk(char *name, struct floppy_struct *param) | ||
91 | { | ||
92 | unsigned char *data; | ||
93 | int fd,cyl_size,cyl,count,read_bytes; | ||
94 | |||
95 | cyl_size = param->sect*param->head*512; | ||
96 | data = xmalloc(cyl_size); | ||
97 | printf("Verifying ... "); | ||
98 | fflush(stdout); | ||
99 | fd = bb_xopen(name,O_RDONLY); | ||
100 | for (cyl = 0; cyl < param->track; cyl++) | ||
101 | { | ||
102 | printf("%3d\b\b\b",cyl); | ||
103 | fflush(stdout); | ||
104 | read_bytes = safe_read(fd,data,cyl_size); | ||
105 | if(read_bytes != cyl_size) { | ||
106 | if(read_bytes < 0) { | ||
107 | bb_perror_msg("Read: "); | ||
108 | } | ||
109 | bb_error_msg_and_die("Problem reading cylinder %d, " | ||
110 | "expected %d, read %d", cyl, cyl_size, read_bytes); | ||
111 | } | ||
112 | for (count = 0; count < cyl_size; count++) | ||
113 | if (data[count] != FD_FILL_BYTE) { | ||
114 | printf("bad data in cyl %d\nContinuing ... ",cyl); | ||
115 | fflush(stdout); | ||
116 | break; | ||
117 | } | ||
118 | } | ||
119 | printf("done\n"); | ||
120 | close(fd); | ||
121 | } | ||
122 | |||
123 | int fdformat_main(int argc,char **argv) | ||
124 | { | ||
125 | int ctrl; | ||
126 | int verify; | ||
127 | struct stat st; | ||
128 | struct floppy_struct param; | ||
129 | |||
130 | if (argc < 2) { | ||
131 | bb_show_usage(); | ||
132 | } | ||
133 | verify != bb_getopt_ulflags(argc, argv, "n"); | ||
134 | argv += optind; | ||
135 | |||
136 | if (stat(*argv,&st) < 0 || access(*argv,W_OK) < 0) { | ||
137 | bb_perror_msg_and_die(*argv); | ||
138 | } | ||
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 */ | ||
142 | } | ||
143 | |||
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"); | ||
147 | } | ||
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); | ||
152 | close(ctrl); | ||
153 | |||
154 | if (verify) { | ||
155 | verify_disk(*argv, ¶m); | ||
156 | } | ||
157 | return EXIT_SUCCESS; | ||
158 | } | ||