"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 | |
| 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 | |
Eric Andersen | 01c3d40 | 2003-07-05 07:51:31 +0000 | [diff] [blame] | 12 | #include "busybox.h" |
| 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 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 48 | static void xioctl(int fd, int request, void *argp, const char *string) |
Eric Andersen | 01c3d40 | 2003-07-05 07:51:31 +0000 | [diff] [blame] | 49 | { |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 50 | if (ioctl (fd, request, argp) < 0) { |
| 51 | bb_perror_msg_and_die(string); |
Eric Andersen | 01c3d40 | 2003-07-05 07:51:31 +0000 | [diff] [blame] | 52 | } |
Eric Andersen | 01c3d40 | 2003-07-05 07:51:31 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | int fdformat_main(int argc,char **argv) |
| 56 | { |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 57 | int fd, n, cyl, read_bytes, verify; |
| 58 | unsigned char *data; |
| 59 | struct stat st; |
| 60 | struct floppy_struct param; |
| 61 | struct format_descr descr; |
Eric Andersen | 01c3d40 | 2003-07-05 07:51:31 +0000 | [diff] [blame] | 62 | |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 63 | if (argc < 2) { |
| 64 | bb_show_usage(); |
| 65 | } |
| 66 | verify = !bb_getopt_ulflags(argc, argv, "n"); |
| 67 | argv += optind; |
Eric Andersen | 01c3d40 | 2003-07-05 07:51:31 +0000 | [diff] [blame] | 68 | |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 69 | /* R_OK is needed for verifying */ |
| 70 | if (stat(*argv,&st) < 0 || access(*argv,W_OK | R_OK ) < 0) { |
Mike Frysinger | 948a09d | 2006-03-23 02:07:20 +0000 | [diff] [blame] | 71 | bb_perror_msg_and_die("%s",*argv); |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 72 | } |
| 73 | if (!S_ISBLK(st.st_mode)) { |
| 74 | bb_error_msg_and_die("%s: not a block device",*argv); |
| 75 | /* do not test major - perhaps this was an USB floppy */ |
| 76 | } |
Eric Andersen | 01c3d40 | 2003-07-05 07:51:31 +0000 | [diff] [blame] | 77 | |
Eric Andersen | 01c3d40 | 2003-07-05 07:51:31 +0000 | [diff] [blame] | 78 | |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 79 | /* O_RDWR for formatting and verifying */ |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 80 | fd = xopen(*argv,O_RDWR ); |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 81 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 82 | xioctl(fd, FDGETPRM, ¶m, "FDGETPRM");/*original message was: "Could not determine current format type" */ |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 83 | |
Rob Landley | 59fe8b9 | 2006-08-04 22:02:55 +0000 | [diff] [blame] | 84 | 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] | 85 | (param.head == 2) ? "Double" : "Single", |
| 86 | param.track, param.sect, param.size >> 1); |
| 87 | |
| 88 | /* FORMAT */ |
Rob Landley | 59fe8b9 | 2006-08-04 22:02:55 +0000 | [diff] [blame] | 89 | printf("Formatting ... "); |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 90 | xioctl(fd, FDFMTBEG,NULL,"FDFMTBEG"); |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 91 | |
| 92 | /* n == track */ |
Eric Andersen | 522a2f3 | 2004-04-03 12:36:03 +0000 | [diff] [blame] | 93 | for (n = 0; n < param.track; n++) |
| 94 | { |
| 95 | descr.head = 0; |
| 96 | descr.track = n; |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 97 | xioctl(fd, FDFMTTRK,&descr,"FDFMTTRK"); |
Rob Landley | 59fe8b9 | 2006-08-04 22:02:55 +0000 | [diff] [blame] | 98 | printf("%3d\b\b\b", n); |
Eric Andersen | 522a2f3 | 2004-04-03 12:36:03 +0000 | [diff] [blame] | 99 | if (param.head == 2) { |
| 100 | descr.head = 1; |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 101 | xioctl(fd, FDFMTTRK,&descr,"FDFMTTRK"); |
Eric Andersen | 522a2f3 | 2004-04-03 12:36:03 +0000 | [diff] [blame] | 102 | } |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 103 | } |
Eric Andersen | 522a2f3 | 2004-04-03 12:36:03 +0000 | [diff] [blame] | 104 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 105 | xioctl(fd,FDFMTEND,NULL,"FDFMTEND"); |
Rob Landley | 59fe8b9 | 2006-08-04 22:02:55 +0000 | [diff] [blame] | 106 | printf("done\n"); |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 107 | |
| 108 | /* VERIFY */ |
| 109 | if(verify) { |
| 110 | /* n == cyl_size */ |
| 111 | n = param.sect*param.head*512; |
| 112 | |
| 113 | data = xmalloc(n); |
Rob Landley | 59fe8b9 | 2006-08-04 22:02:55 +0000 | [diff] [blame] | 114 | printf("Verifying ... "); |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 115 | for (cyl = 0; cyl < param.track; cyl++) { |
Rob Landley | 59fe8b9 | 2006-08-04 22:02:55 +0000 | [diff] [blame] | 116 | printf("%3d\b\b\b", cyl); |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 117 | if((read_bytes = safe_read(fd,data,n))!= n ) { |
| 118 | if(read_bytes < 0) { |
Bernhard Reutner-Fischer | 1b9d7c9 | 2006-06-03 22:45:37 +0000 | [diff] [blame] | 119 | bb_perror_msg(bb_msg_read_error); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 120 | } |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 121 | bb_error_msg_and_die("Problem reading cylinder %d, expected %d, read %d", cyl, n, read_bytes); |
| 122 | } |
| 123 | /* Check backwards so we don't need a counter */ |
| 124 | while(--read_bytes>=0) { |
| 125 | if( data[read_bytes] != FD_FILL_BYTE) { |
Rob Landley | 59fe8b9 | 2006-08-04 22:02:55 +0000 | [diff] [blame] | 126 | printf("bad data in cyl %d\nContinuing ... ",cyl); |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | } |
| 130 | /* There is no point in freeing blocks at the end of a program, because |
| 131 | all of the program's space is given back to the system when the process |
| 132 | terminates.*/ |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 133 | |
Rob Landley | 658d2cf | 2005-09-08 03:11:58 +0000 | [diff] [blame] | 134 | if (ENABLE_FEATURE_CLEAN_UP) free(data); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 135 | |
Rob Landley | 59fe8b9 | 2006-08-04 22:02:55 +0000 | [diff] [blame] | 136 | printf("done\n"); |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 137 | } |
Rob Landley | 658d2cf | 2005-09-08 03:11:58 +0000 | [diff] [blame] | 138 | |
| 139 | if (ENABLE_FEATURE_CLEAN_UP) close(fd); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 140 | |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 141 | /* Don't bother closing. Exit does |
| 142 | * that, so we can save a few bytes */ |
| 143 | return EXIT_SUCCESS; |
Eric Andersen | 01c3d40 | 2003-07-05 07:51:31 +0000 | [diff] [blame] | 144 | } |