]>
Commit | Line | Data |
---|---|---|
3160fcf3 KZ |
1 | /* |
2 | * fdformat.c - Low-level formats a floppy disk - Werner Almesberger | |
7eda085c | 3 | */ |
6dbe3af9 | 4 | #include <errno.h> |
8d18d353 SK |
5 | #include <fcntl.h> |
6 | #include <getopt.h> | |
7 | #include <linux/fd.h> | |
8 | #include <stdio.h> | |
6dbe3af9 | 9 | #include <stdlib.h> |
fd6b7a7f | 10 | #include <sys/ioctl.h> |
8d18d353 SK |
11 | #include <sys/stat.h> |
12 | #include <unistd.h> | |
22853e4a | 13 | |
b7889ba8 | 14 | #include "c.h" |
45ca68ec | 15 | #include "closestream.h" |
7eda085c | 16 | #include "nls.h" |
a53b31d0 | 17 | #include "xalloc.h" |
6dbe3af9 | 18 | |
6dbe3af9 KZ |
19 | struct floppy_struct param; |
20 | ||
6dbe3af9 | 21 | #define SECTOR_SIZE 512 |
6dbe3af9 | 22 | |
40c077a5 | 23 | static void format_disk(int ctrl) |
6dbe3af9 | 24 | { |
8d18d353 SK |
25 | struct format_descr descr; |
26 | unsigned int track; | |
27 | ||
28 | printf(_("Formatting ... ")); | |
6dbe3af9 | 29 | fflush(stdout); |
8d18d353 | 30 | if (ioctl(ctrl, FDFMTBEG, NULL) < 0) |
3160fcf3 | 31 | err(EXIT_FAILURE, "ioctl: FDFMTBEG"); |
8d18d353 SK |
32 | for (track = 0; track < param.track; track++) { |
33 | descr.track = track; | |
34 | descr.head = 0; | |
3160fcf3 KZ |
35 | if (ioctl(ctrl, FDFMTTRK, (long) &descr) < 0) |
36 | err(EXIT_FAILURE, "ioctl: FDFMTTRK"); | |
8d18d353 SK |
37 | |
38 | printf("%3d\b\b\b", track); | |
39 | fflush(stdout); | |
40 | if (param.head == 2) { | |
41 | descr.head = 1; | |
42 | if (ioctl(ctrl, FDFMTTRK, (long)&descr) < 0) | |
3160fcf3 | 43 | err(EXIT_FAILURE, "ioctl: FDFMTTRK"); |
8d18d353 | 44 | } |
6dbe3af9 | 45 | } |
8d18d353 | 46 | if (ioctl(ctrl, FDFMTEND, NULL) < 0) |
3160fcf3 | 47 | err(EXIT_FAILURE, "ioctl: FDFMTEND"); |
8d18d353 | 48 | printf(_("done\n")); |
6dbe3af9 KZ |
49 | } |
50 | ||
6dbe3af9 KZ |
51 | static void verify_disk(char *name) |
52 | { | |
8d18d353 SK |
53 | unsigned char *data; |
54 | unsigned int cyl; | |
55 | int fd, cyl_size, count; | |
56 | ||
57 | cyl_size = param.sect * param.head * 512; | |
58 | data = xmalloc(cyl_size); | |
59 | printf(_("Verifying ... ")); | |
6dbe3af9 | 60 | fflush(stdout); |
8d18d353 SK |
61 | if ((fd = open(name, O_RDONLY)) < 0) |
62 | err(EXIT_FAILURE, _("cannot open file %s"), name); | |
63 | for (cyl = 0; cyl < param.track; cyl++) { | |
64 | int read_bytes; | |
65 | ||
66 | printf("%3d\b\b\b", cyl); | |
6dbe3af9 | 67 | fflush(stdout); |
8d18d353 SK |
68 | read_bytes = read(fd, data, cyl_size); |
69 | if (read_bytes != cyl_size) { | |
70 | if (read_bytes < 0) | |
71 | perror(_("Read: ")); | |
72 | fprintf(stderr, | |
73 | _("Problem reading cylinder %d," | |
74 | " expected %d, read %d\n"), | |
75 | cyl, cyl_size, read_bytes); | |
76 | free(data); | |
77 | exit(EXIT_FAILURE); | |
78 | } | |
79 | for (count = 0; count < cyl_size; count++) | |
80 | if (data[count] != FD_FILL_BYTE) { | |
81 | printf(_("bad data in cyl %d\n" | |
82 | "Continuing ... "), cyl); | |
83 | fflush(stdout); | |
84 | break; | |
85 | } | |
86 | } | |
87 | free(data); | |
88 | printf(_("done\n")); | |
89 | if (close(fd) < 0) | |
90 | err(EXIT_FAILURE, "close"); | |
6dbe3af9 KZ |
91 | } |
92 | ||
a8f36480 | 93 | static void __attribute__ ((__noreturn__)) usage(FILE * out) |
6dbe3af9 | 94 | { |
a8f36480 SK |
95 | fprintf(out, _("Usage: %s [options] device\n"), |
96 | program_invocation_short_name); | |
6dbe3af9 | 97 | |
a8f36480 SK |
98 | fprintf(out, _("\nOptions:\n" |
99 | " -n, --no-verify disable the verification after the format\n" | |
100 | " -V, --version output version information and exit\n" | |
101 | " -h, --help display this help and exit\n\n")); | |
6dbe3af9 | 102 | |
a8f36480 SK |
103 | exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); |
104 | } | |
6dbe3af9 | 105 | |
8d18d353 | 106 | int main(int argc, char **argv) |
6dbe3af9 | 107 | { |
8d18d353 SK |
108 | int ch; |
109 | int ctrl; | |
8c40f481 | 110 | int verify = 1; |
8d18d353 SK |
111 | struct stat st; |
112 | ||
113 | static const struct option longopts[] = { | |
114 | {"no-verify", no_argument, NULL, 'n'}, | |
115 | {"version", no_argument, NULL, 'V'}, | |
116 | {"help", no_argument, NULL, 'h'}, | |
117 | {NULL, 0, NULL, 0} | |
118 | }; | |
119 | ||
120 | setlocale(LC_ALL, ""); | |
121 | bindtextdomain(PACKAGE, LOCALEDIR); | |
122 | textdomain(PACKAGE); | |
45ca68ec | 123 | atexit(close_stdout); |
8d18d353 SK |
124 | |
125 | while ((ch = getopt_long(argc, argv, "nVh", longopts, NULL)) != -1) | |
126 | switch (ch) { | |
127 | case 'n': | |
128 | verify = 0; | |
129 | break; | |
130 | case 'V': | |
131 | printf(_("%s from %s\n"), program_invocation_short_name, | |
132 | PACKAGE_STRING); | |
133 | exit(EXIT_SUCCESS); | |
134 | case 'h': | |
135 | usage(stdout); | |
136 | default: | |
137 | usage(stderr); | |
138 | } | |
139 | ||
140 | argc -= optind; | |
141 | argv += optind; | |
142 | ||
143 | if (argc < 1) | |
144 | usage(stderr); | |
145 | if (stat(argv[0], &st) < 0) | |
146 | err(EXIT_FAILURE, _("cannot stat file %s"), argv[0]); | |
147 | if (!S_ISBLK(st.st_mode)) | |
148 | /* do not test major - perhaps this was an USB floppy */ | |
149 | errx(EXIT_FAILURE, _("%s: not a block device"), argv[0]); | |
150 | if (access(argv[0], W_OK) < 0) | |
151 | err(EXIT_FAILURE, _("cannot access file %s"), argv[0]); | |
152 | ||
153 | ctrl = open(argv[0], O_WRONLY); | |
154 | if (ctrl < 0) | |
155 | err(EXIT_FAILURE, _("cannot open file %s"), argv[0]); | |
156 | if (ioctl(ctrl, FDGETPRM, (long)¶m) < 0) | |
157 | err(EXIT_FAILURE, _("Could not determine current format type")); | |
158 | ||
159 | printf(_("%s-sided, %d tracks, %d sec/track. Total capacity %d kB.\n"), | |
160 | (param.head == 2) ? _("Double") : _("Single"), | |
161 | param.track, param.sect, param.size >> 1); | |
162 | format_disk(ctrl); | |
163 | close(ctrl); | |
164 | ||
165 | if (verify) | |
166 | verify_disk(argv[0]); | |
167 | return EXIT_SUCCESS; | |
6dbe3af9 | 168 | } |