"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | 01c3d40 | 2003-07-05 07:51:31 +0000 | [diff] [blame] | 2 | /* fdformat.c - Low-level formats a floppy disk - Werner Almesberger */ |
| 3 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 4 | /* 5 July 2003 -- modified for Busybox by Erik Andersen |
Eric Andersen | 01c3d40 | 2003-07-05 07:51:31 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 7 | #include "libbb.h" |
Eric Andersen | 01c3d40 | 2003-07-05 07:51:31 +0000 | [diff] [blame] | 8 | |
| 9 | |
| 10 | /* Stuff extracted from linux/fd.h */ |
| 11 | struct floppy_struct { |
| 12 | unsigned int size, /* nr of sectors total */ |
| 13 | sect, /* sectors per track */ |
| 14 | head, /* nr of heads */ |
| 15 | track, /* nr of tracks */ |
| 16 | stretch; /* !=0 means double track steps */ |
| 17 | #define FD_STRETCH 1 |
| 18 | #define FD_SWAPSIDES 2 |
| 19 | |
| 20 | unsigned char gap, /* gap1 size */ |
| 21 | |
| 22 | rate, /* data rate. |= 0x40 for perpendicular */ |
| 23 | #define FD_2M 0x4 |
| 24 | #define FD_SIZECODEMASK 0x38 |
| 25 | #define FD_SIZECODE(floppy) (((((floppy)->rate&FD_SIZECODEMASK)>> 3)+ 2) %8) |
| 26 | #define FD_SECTSIZE(floppy) ( (floppy)->rate & FD_2M ? \ |
| 27 | 512 : 128 << FD_SIZECODE(floppy) ) |
| 28 | #define FD_PERP 0x40 |
| 29 | |
| 30 | spec1, /* stepping rate, head unload time */ |
| 31 | fmt_gap; /* gap2 size */ |
| 32 | const char * name; /* used only for predefined formats */ |
| 33 | }; |
| 34 | struct format_descr { |
| 35 | unsigned int device,head,track; |
| 36 | }; |
| 37 | #define FDFMTBEG _IO(2,0x47) |
| 38 | #define FDFMTTRK _IOW(2,0x48, struct format_descr) |
| 39 | #define FDFMTEND _IO(2,0x49) |
| 40 | #define FDGETPRM _IOR(2, 0x04, struct floppy_struct) |
| 41 | #define FD_FILL_BYTE 0xF6 /* format fill byte. */ |
| 42 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 43 | int fdformat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 44 | int fdformat_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 01c3d40 | 2003-07-05 07:51:31 +0000 | [diff] [blame] | 45 | { |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 46 | int fd, n, cyl, read_bytes, verify; |
| 47 | unsigned char *data; |
| 48 | struct stat st; |
| 49 | struct floppy_struct param; |
| 50 | struct format_descr descr; |
Eric Andersen | 01c3d40 | 2003-07-05 07:51:31 +0000 | [diff] [blame] | 51 | |
Denis Vlasenko | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 52 | opt_complementary = "=1"; /* must have 1 param */ |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 53 | verify = !getopt32(argv, "n"); |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 54 | argv += optind; |
Eric Andersen | 01c3d40 | 2003-07-05 07:51:31 +0000 | [diff] [blame] | 55 | |
Denis Vlasenko | b633224 | 2006-10-03 19:57:50 +0000 | [diff] [blame] | 56 | xstat(*argv, &st); |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 57 | if (!S_ISBLK(st.st_mode)) { |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 58 | bb_error_msg_and_die("%s: not a block device", *argv); |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 59 | /* do not test major - perhaps this was an USB floppy */ |
| 60 | } |
Eric Andersen | 01c3d40 | 2003-07-05 07:51:31 +0000 | [diff] [blame] | 61 | |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 62 | /* O_RDWR for formatting and verifying */ |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 63 | fd = xopen(*argv, O_RDWR); |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 64 | |
Denis Vlasenko | b633224 | 2006-10-03 19:57:50 +0000 | [diff] [blame] | 65 | /* original message was: "Could not determine current format type" */ |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 66 | xioctl(fd, FDGETPRM, ¶m); |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 67 | |
Denis Vlasenko | b633224 | 2006-10-03 19:57:50 +0000 | [diff] [blame] | 68 | printf("%s-sided, %d tracks, %d sec/track. Total capacity %d kB\n", |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 69 | (param.head == 2) ? "Double" : "Single", |
| 70 | param.track, param.sect, param.size >> 1); |
| 71 | |
| 72 | /* FORMAT */ |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 73 | printf("Formatting... "); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 74 | xioctl(fd, FDFMTBEG, NULL); |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 75 | |
| 76 | /* n == track */ |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 77 | for (n = 0; n < param.track; n++) { |
| 78 | descr.head = 0; |
| 79 | descr.track = n; |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 80 | xioctl(fd, FDFMTTRK, &descr); |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 81 | printf("%3d\b\b\b", n); |
| 82 | if (param.head == 2) { |
| 83 | descr.head = 1; |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 84 | xioctl(fd, FDFMTTRK, &descr); |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 85 | } |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 86 | } |
Eric Andersen | 522a2f3 | 2004-04-03 12:36:03 +0000 | [diff] [blame] | 87 | |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 88 | xioctl(fd, FDFMTEND, NULL); |
Rob Landley | 59fe8b9 | 2006-08-04 22:02:55 +0000 | [diff] [blame] | 89 | printf("done\n"); |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 90 | |
| 91 | /* VERIFY */ |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 92 | if (verify) { |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 93 | /* n == cyl_size */ |
| 94 | n = param.sect*param.head*512; |
| 95 | |
| 96 | data = xmalloc(n); |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 97 | printf("Verifying... "); |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 98 | for (cyl = 0; cyl < param.track; cyl++) { |
Rob Landley | 59fe8b9 | 2006-08-04 22:02:55 +0000 | [diff] [blame] | 99 | printf("%3d\b\b\b", cyl); |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 100 | read_bytes = safe_read(fd, data, n); |
| 101 | if (read_bytes != n) { |
| 102 | if (read_bytes < 0) { |
Bernhard Reutner-Fischer | 1b9d7c9 | 2006-06-03 22:45:37 +0000 | [diff] [blame] | 103 | bb_perror_msg(bb_msg_read_error); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 104 | } |
Denis Vlasenko | b633224 | 2006-10-03 19:57:50 +0000 | [diff] [blame] | 105 | bb_error_msg_and_die("problem reading cylinder %d, " |
| 106 | "expected %d, read %d", cyl, n, read_bytes); |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 107 | // FIXME: maybe better seek & continue?? |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 108 | } |
| 109 | /* Check backwards so we don't need a counter */ |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 110 | while (--read_bytes >= 0) { |
| 111 | if (data[read_bytes] != FD_FILL_BYTE) { |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 112 | printf("bad data in cyl %d\nContinuing... ", cyl); |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | } |
| 116 | /* There is no point in freeing blocks at the end of a program, because |
| 117 | all of the program's space is given back to the system when the process |
| 118 | terminates.*/ |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 119 | |
Rob Landley | 658d2cf | 2005-09-08 03:11:58 +0000 | [diff] [blame] | 120 | if (ENABLE_FEATURE_CLEAN_UP) free(data); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 121 | |
Rob Landley | 59fe8b9 | 2006-08-04 22:02:55 +0000 | [diff] [blame] | 122 | printf("done\n"); |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 123 | } |
Rob Landley | 658d2cf | 2005-09-08 03:11:58 +0000 | [diff] [blame] | 124 | |
| 125 | if (ENABLE_FEATURE_CLEAN_UP) close(fd); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 126 | |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 127 | /* Don't bother closing. Exit does |
| 128 | * that, so we can save a few bytes */ |
| 129 | return EXIT_SUCCESS; |
Eric Andersen | 01c3d40 | 2003-07-05 07:51:31 +0000 | [diff] [blame] | 130 | } |