]>
Commit | Line | Data |
---|---|---|
6dbe3af9 KZ |
1 | /* fdformat.c - Low-level formats a floppy disk. */ |
2 | ||
3 | #include <stdio.h> | |
4 | #include <string.h> | |
5 | #include <fcntl.h> | |
6 | #include <errno.h> | |
7 | #include <unistd.h> | |
8 | #include <stdlib.h> | |
9 | #include <sys/stat.h> | |
fd6b7a7f | 10 | #include <sys/ioctl.h> |
6dbe3af9 KZ |
11 | #include <linux/fd.h> |
12 | #include <linux/fs.h> | |
13 | ||
14 | static int ctrl; | |
15 | struct floppy_struct param; | |
16 | ||
17 | #define FLOPPY_MAJOR 2 | |
18 | #define SECTOR_SIZE 512 | |
19 | #define PERROR(msg) { perror(msg); exit(1); } | |
20 | ||
21 | static void format_disk(char *name) | |
22 | { | |
23 | struct format_descr descr; | |
24 | int track; | |
25 | ||
26 | printf("Formatting ... "); | |
27 | fflush(stdout); | |
28 | if (ioctl(ctrl,FDFMTBEG,NULL) < 0) PERROR("\nioctl(FDFMTBEG)"); | |
29 | for (track = 0; track < param.track; track++) { | |
30 | descr.track = track; | |
31 | descr.head = 0; | |
fd6b7a7f KZ |
32 | if (ioctl(ctrl,FDFMTTRK,(long) &descr) < 0) |
33 | PERROR("\nioctl(FDFMTTRK)"); | |
34 | ||
6dbe3af9 KZ |
35 | printf("%3d\b\b\b",track); |
36 | fflush(stdout); | |
37 | if (param.head == 2) { | |
38 | descr.head = 1; | |
fd6b7a7f KZ |
39 | if (ioctl(ctrl,FDFMTTRK,(long) &descr) < 0) |
40 | PERROR("\nioctl(FDFMTTRK)"); | |
6dbe3af9 KZ |
41 | } |
42 | } | |
43 | if (ioctl(ctrl,FDFMTEND,NULL) < 0) PERROR("\nioctl(FDFMTEND)"); | |
44 | printf("done\n"); | |
45 | } | |
46 | ||
47 | ||
48 | static void verify_disk(char *name) | |
49 | { | |
50 | unsigned char *data; | |
51 | int fd,cyl_size,cyl,count; | |
52 | ||
53 | cyl_size = param.sect*param.head*512; | |
54 | if ((data = (unsigned char *) malloc(cyl_size)) == NULL) PERROR("malloc"); | |
55 | printf("Verifying ... "); | |
56 | fflush(stdout); | |
57 | if ((fd = open(name,O_RDONLY)) < 0) PERROR(name); | |
58 | for (cyl = 0; cyl < param.track; cyl++) { | |
59 | printf("%3d\b\b\b",cyl); | |
60 | fflush(stdout); | |
61 | if (read(fd,data,cyl_size) != cyl_size) PERROR("read"); | |
62 | for (count = 0; count < cyl_size; count++) | |
63 | if (data[count] != FD_FILL_BYTE) { | |
64 | printf("bad data in cyl %d\nContinuing ... ",cyl); | |
65 | fflush(stdout); | |
66 | break; | |
67 | } | |
68 | } | |
69 | printf("done\n"); | |
70 | if (close(fd) < 0) PERROR("close"); | |
71 | } | |
72 | ||
73 | ||
74 | static void usage(char *name) | |
75 | { | |
76 | char *this; | |
77 | ||
fd6b7a7f | 78 | if ((this = strrchr(name,'/')) != NULL) name = this+1; |
6dbe3af9 KZ |
79 | fprintf(stderr,"usage: %s [ -n ] device\n",name); |
80 | exit(1); | |
81 | } | |
82 | ||
83 | ||
84 | int main(int argc,char **argv) | |
85 | { | |
86 | int verify; | |
87 | char *name; | |
88 | struct stat st; | |
89 | ||
90 | name = argv[0]; | |
91 | verify = 1; | |
92 | if (argc > 1 && argv[1][0] == '-') { | |
93 | if (argv[1][1] != 'n') usage(name); | |
94 | verify = 0; | |
95 | argc--; | |
96 | argv++; | |
97 | } | |
98 | if (argc != 2) usage(name); | |
726f69e2 | 99 | if (stat(argv[1],&st) < 0) PERROR(argv[1]); |
6dbe3af9 KZ |
100 | if (!S_ISBLK(st.st_mode) || MAJOR(st.st_rdev) != FLOPPY_MAJOR) { |
101 | fprintf(stderr,"%s: not a floppy device\n",argv[1]); | |
102 | exit(1); | |
103 | } | |
104 | if (access(argv[1],W_OK) < 0) PERROR(argv[1]); | |
105 | if ((ctrl = open(argv[1],3)) < 0) PERROR(argv[1]); | |
fd6b7a7f KZ |
106 | if (ioctl(ctrl,FDGETPRM,(long) ¶m) < 0) |
107 | PERROR("Could not determine current format type"); | |
6dbe3af9 KZ |
108 | printf("%sle-sided, %d tracks, %d sec/track. Total capacity %d kB.\n", |
109 | param.head ? "Doub" : "Sing",param.track,param.sect,param.size >> 1); | |
110 | format_disk(argv[1]); | |
111 | if (verify) verify_disk(argv[1]); | |
112 | return 0; | |
113 | } |