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