diff options
Diffstat (limited to 'util-linux')
| -rw-r--r-- | util-linux/fdformat.c | 183 |
1 files changed, 90 insertions, 93 deletions
diff --git a/util-linux/fdformat.c b/util-linux/fdformat.c index d94c6a62b..930525848 100644 --- a/util-linux/fdformat.c +++ b/util-linux/fdformat.c | |||
| @@ -52,107 +52,104 @@ struct format_descr { | |||
| 52 | #define FDGETPRM _IOR(2, 0x04, struct floppy_struct) | 52 | #define FDGETPRM _IOR(2, 0x04, struct floppy_struct) |
| 53 | #define FD_FILL_BYTE 0xF6 /* format fill byte. */ | 53 | #define FD_FILL_BYTE 0xF6 /* format fill byte. */ |
| 54 | 54 | ||
| 55 | 55 | static void print_and_flush(const char * __restrict format, ...) | |
| 56 | |||
| 57 | static void format_disk(int ctrl, char *name, struct floppy_struct *param) | ||
| 58 | { | 56 | { |
| 59 | struct format_descr descr; | 57 | va_list arg; |
| 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 | 58 | ||
| 75 | printf("%3d\b\b\b",track); | 59 | va_start(arg, format); |
| 76 | fflush(stdout); | 60 | bb_vfprintf(stdout, format, arg); |
| 77 | if (param->head == 2) { | 61 | va_end(arg); |
| 78 | descr.head = 1; | 62 | bb_xfflush_stdout(); |
| 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 | } | 63 | } |
| 89 | 64 | ||
| 90 | static void verify_disk(char *name, struct floppy_struct *param) | 65 | static void bb_xioctl(int fd, int request, void *argp, const char *string) |
| 91 | { | 66 | { |
| 92 | unsigned char *data; | 67 | if (ioctl (fd, request, argp) < 0) { |
| 93 | int fd,cyl_size,cyl,count,read_bytes; | 68 | bb_perror_msg_and_die(string); |
| 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 | } | 69 | } |
| 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 | } | 70 | } |
| 122 | 71 | ||
| 123 | int fdformat_main(int argc,char **argv) | 72 | int fdformat_main(int argc,char **argv) |
| 124 | { | 73 | { |
| 125 | int ctrl; | 74 | int fd, n, cyl, read_bytes, verify; |
| 126 | int verify; | 75 | unsigned char *data; |
| 127 | struct stat st; | 76 | struct stat st; |
| 128 | struct floppy_struct param; | 77 | struct floppy_struct param; |
| 129 | 78 | struct format_descr descr; | |
| 130 | if (argc < 2) { | 79 | |
| 131 | bb_show_usage(); | 80 | if (argc < 2) { |
| 132 | } | 81 | bb_show_usage(); |
| 133 | verify = !bb_getopt_ulflags(argc, argv, "n"); | 82 | } |
| 134 | argv += optind; | 83 | verify = !bb_getopt_ulflags(argc, argv, "n"); |
| 135 | 84 | argv += optind; | |
| 136 | if (stat(*argv,&st) < 0 || access(*argv,W_OK) < 0) { | 85 | |
| 137 | bb_perror_msg_and_die(*argv); | 86 | /* R_OK is needed for verifying */ |
| 138 | } | 87 | if (stat(*argv,&st) < 0 || access(*argv,W_OK | R_OK ) < 0) { |
| 139 | if (!S_ISBLK(st.st_mode)) { | 88 | bb_perror_msg_and_die(*argv); |
| 140 | bb_error_msg_and_die("%s: not a block device",*argv); | 89 | } |
| 141 | /* do not test major - perhaps this was an USB floppy */ | 90 | if (!S_ISBLK(st.st_mode)) { |
| 142 | } | 91 | bb_error_msg_and_die("%s: not a block device",*argv); |
| 143 | 92 | /* do not test major - perhaps this was an USB floppy */ | |
| 144 | ctrl = bb_xopen(*argv,O_WRONLY); | 93 | } |
| 145 | if (ioctl(ctrl,FDGETPRM,(long) ¶m) < 0) { | 94 | |
| 146 | bb_perror_msg_and_die("Could not determine current format type"); | 95 | |
| 147 | } | 96 | /* O_RDWR for formatting and verifying */ |
| 148 | printf("%s-sided, %d tracks, %d sec/track. Total capacity %d kB.\n", | 97 | fd = bb_xopen(*argv,O_RDWR ); |
| 149 | (param.head == 2) ? "Double" : "Single", | 98 | |
| 150 | param.track, param.sect,param.size >> 1); | 99 | bb_xioctl(fd, FDGETPRM, ¶m, "FDGETPRM");/*original message was: "Could not determine current format type" */ |
| 151 | format_disk(ctrl, *argv, ¶m); | 100 | |
| 152 | close(ctrl); | 101 | print_and_flush("%s-sided, %d tracks, %d sec/track. Total capacity %d kB.\n", |
| 153 | 102 | (param.head == 2) ? "Double" : "Single", | |
| 154 | if (verify) { | 103 | param.track, param.sect, param.size >> 1); |
| 155 | verify_disk(*argv, ¶m); | 104 | |
| 156 | } | 105 | /* FORMAT */ |
| 157 | return EXIT_SUCCESS; | 106 | print_and_flush("Formatting ... ", NULL); |
| 107 | bb_xioctl(fd, FDFMTBEG,NULL,"FDFMTBEG"); | ||
| 108 | |||
| 109 | /* n == track */ | ||
| 110 | for (n = 0; n < param.track; n++) { | ||
| 111 | descr.track = n; | ||
| 112 | for(descr.head=0, print_and_flush("%3d\b\b\b", n) ; descr.head < param.head; descr.head++){ | ||
| 113 | bb_xioctl(fd,FDFMTTRK, &descr,"FDFMTTRK"); | ||
| 114 | } | ||
| 115 | } | ||
| 116 | bb_xioctl(fd,FDFMTEND,NULL,"FDFMTEND"); | ||
| 117 | print_and_flush("done\n", NULL); | ||
| 118 | |||
| 119 | /* VERIFY */ | ||
| 120 | if(verify) { | ||
| 121 | /* n == cyl_size */ | ||
| 122 | n = param.sect*param.head*512; | ||
| 123 | |||
| 124 | data = xmalloc(n); | ||
| 125 | print_and_flush("Verifying ... ", NULL); | ||
| 126 | for (cyl = 0; cyl < param.track; cyl++) { | ||
| 127 | print_and_flush("%3d\b\b\b", cyl); | ||
| 128 | if((read_bytes = safe_read(fd,data,n))!= n ) { | ||
| 129 | if(read_bytes < 0) { | ||
| 130 | bb_perror_msg("Read: "); | ||
| 131 | } | ||
| 132 | bb_error_msg_and_die("Problem reading cylinder %d, expected %d, read %d", cyl, n, read_bytes); | ||
| 133 | } | ||
| 134 | /* Check backwards so we don't need a counter */ | ||
| 135 | while(--read_bytes>=0) { | ||
| 136 | if( data[read_bytes] != FD_FILL_BYTE) { | ||
| 137 | print_and_flush("bad data in cyl %d\nContinuing ... ",cyl); | ||
| 138 | } | ||
| 139 | } | ||
| 140 | } | ||
| 141 | /* There is no point in freeing blocks at the end of a program, because | ||
| 142 | all of the program's space is given back to the system when the process | ||
| 143 | terminates.*/ | ||
| 144 | #ifdef CONFIG_FEATURE_CLEAN_UP | ||
| 145 | free(data); | ||
| 146 | #endif | ||
| 147 | print_and_flush("done\n", NULL); | ||
| 148 | } | ||
| 149 | #ifdef CONFIG_FEATURE_CLEAN_UP | ||
| 150 | close(fd); | ||
| 151 | #endif | ||
| 152 | /* Don't bother closing. Exit does | ||
| 153 | * that, so we can save a few bytes */ | ||
| 154 | return EXIT_SUCCESS; | ||
| 158 | } | 155 | } |
