"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Denys Vlasenko | 2ab9403 | 2017-10-05 15:33:28 +0200 | [diff] [blame] | 2 | /* |
| 3 | * fdformat.c - Low-level formats a floppy disk - Werner Almesberger |
Denis Vlasenko | db12d1d | 2008-12-07 00:52:58 +0000 | [diff] [blame] | 4 | * 5 July 2003 -- modified for Busybox by Erik Andersen |
| 5 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 6 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Eric Andersen | 01c3d40 | 2003-07-05 07:51:31 +0000 | [diff] [blame] | 7 | */ |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 8 | //config:config FDFORMAT |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 9 | //config: bool "fdformat (4.4 kb)" |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 10 | //config: default y |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 11 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 12 | //config: fdformat is used to low-level format a floppy disk. |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 13 | |
| 14 | //applet:IF_FDFORMAT(APPLET(fdformat, BB_DIR_USR_SBIN, BB_SUID_DROP)) |
| 15 | |
| 16 | //kbuild:lib-$(CONFIG_FDFORMAT) += fdformat.o |
Eric Andersen | 01c3d40 | 2003-07-05 07:51:31 +0000 | [diff] [blame] | 17 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 18 | //usage:#define fdformat_trivial_usage |
| 19 | //usage: "[-n] DEVICE" |
| 20 | //usage:#define fdformat_full_usage "\n\n" |
| 21 | //usage: "Format floppy disk\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 22 | //usage: "\n -n Don't verify after format" |
| 23 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 24 | #include "libbb.h" |
Eric Andersen | 01c3d40 | 2003-07-05 07:51:31 +0000 | [diff] [blame] | 25 | |
| 26 | |
| 27 | /* Stuff extracted from linux/fd.h */ |
| 28 | struct floppy_struct { |
| 29 | unsigned int size, /* nr of sectors total */ |
| 30 | sect, /* sectors per track */ |
| 31 | head, /* nr of heads */ |
| 32 | track, /* nr of tracks */ |
| 33 | stretch; /* !=0 means double track steps */ |
| 34 | #define FD_STRETCH 1 |
| 35 | #define FD_SWAPSIDES 2 |
| 36 | |
| 37 | unsigned char gap, /* gap1 size */ |
| 38 | |
| 39 | rate, /* data rate. |= 0x40 for perpendicular */ |
| 40 | #define FD_2M 0x4 |
| 41 | #define FD_SIZECODEMASK 0x38 |
| 42 | #define FD_SIZECODE(floppy) (((((floppy)->rate&FD_SIZECODEMASK)>> 3)+ 2) %8) |
| 43 | #define FD_SECTSIZE(floppy) ( (floppy)->rate & FD_2M ? \ |
| 44 | 512 : 128 << FD_SIZECODE(floppy) ) |
| 45 | #define FD_PERP 0x40 |
| 46 | |
| 47 | spec1, /* stepping rate, head unload time */ |
| 48 | fmt_gap; /* gap2 size */ |
| 49 | const char * name; /* used only for predefined formats */ |
| 50 | }; |
| 51 | struct format_descr { |
| 52 | unsigned int device,head,track; |
| 53 | }; |
| 54 | #define FDFMTBEG _IO(2,0x47) |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 55 | #define FDFMTTRK _IOW(2,0x48, struct format_descr) |
Eric Andersen | 01c3d40 | 2003-07-05 07:51:31 +0000 | [diff] [blame] | 56 | #define FDFMTEND _IO(2,0x49) |
| 57 | #define FDGETPRM _IOR(2, 0x04, struct floppy_struct) |
| 58 | #define FD_FILL_BYTE 0xF6 /* format fill byte. */ |
| 59 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 60 | int fdformat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 61 | int fdformat_main(int argc UNUSED_PARAM, char **argv) |
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 | int fd, n, cyl, read_bytes, verify; |
| 64 | unsigned char *data; |
| 65 | struct stat st; |
| 66 | struct floppy_struct param; |
| 67 | struct format_descr descr; |
Eric Andersen | 01c3d40 | 2003-07-05 07:51:31 +0000 | [diff] [blame] | 68 | |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 69 | verify = !getopt32(argv, "^" "n" "\0" "=1"); |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 70 | argv += optind; |
Eric Andersen | 01c3d40 | 2003-07-05 07:51:31 +0000 | [diff] [blame] | 71 | |
Denis Vlasenko | b633224 | 2006-10-03 19:57:50 +0000 | [diff] [blame] | 72 | xstat(*argv, &st); |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 73 | if (!S_ISBLK(st.st_mode)) { |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 74 | bb_error_msg_and_die("%s: not a block device", *argv); |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 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 | |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 78 | /* O_RDWR for formatting and verifying */ |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 79 | fd = xopen(*argv, O_RDWR); |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 80 | |
Denis Vlasenko | b633224 | 2006-10-03 19:57:50 +0000 | [diff] [blame] | 81 | /* original message was: "Could not determine current format type" */ |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 82 | xioctl(fd, FDGETPRM, ¶m); |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 83 | |
Denys Vlasenko | 327f550 | 2013-11-29 16:45:45 +0100 | [diff] [blame] | 84 | printf("%s-sided, %u tracks, %u 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 */ |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 89 | printf("Formatting... "); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 90 | xioctl(fd, FDFMTBEG, NULL); |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 91 | |
| 92 | /* n == track */ |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 93 | for (n = 0; n < param.track; n++) { |
| 94 | descr.head = 0; |
| 95 | descr.track = n; |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 96 | xioctl(fd, FDFMTTRK, &descr); |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 97 | printf("%3d\b\b\b", n); |
| 98 | if (param.head == 2) { |
| 99 | descr.head = 1; |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 100 | xioctl(fd, FDFMTTRK, &descr); |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 101 | } |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 102 | } |
Eric Andersen | 522a2f3 | 2004-04-03 12:36:03 +0000 | [diff] [blame] | 103 | |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 104 | xioctl(fd, FDFMTEND, NULL); |
Denys Vlasenko | d60752f | 2015-10-07 22:42:45 +0200 | [diff] [blame] | 105 | puts("Done"); |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 106 | |
| 107 | /* VERIFY */ |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 108 | if (verify) { |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 109 | /* n == cyl_size */ |
| 110 | n = param.sect*param.head*512; |
| 111 | |
| 112 | data = xmalloc(n); |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 113 | printf("Verifying... "); |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 114 | for (cyl = 0; cyl < param.track; cyl++) { |
Rob Landley | 59fe8b9 | 2006-08-04 22:02:55 +0000 | [diff] [blame] | 115 | printf("%3d\b\b\b", cyl); |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 116 | read_bytes = safe_read(fd, data, n); |
| 117 | if (read_bytes != n) { |
| 118 | if (read_bytes < 0) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 119 | bb_simple_perror_msg(bb_msg_read_error); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 120 | } |
Denis Vlasenko | b633224 | 2006-10-03 19:57:50 +0000 | [diff] [blame] | 121 | bb_error_msg_and_die("problem reading cylinder %d, " |
| 122 | "expected %d, read %d", cyl, n, read_bytes); |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 123 | // FIXME: maybe better seek & continue?? |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 124 | } |
| 125 | /* Check backwards so we don't need a counter */ |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 126 | while (--read_bytes >= 0) { |
| 127 | if (data[read_bytes] != FD_FILL_BYTE) { |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 128 | printf("bad data in cyl %d\nContinuing... ", cyl); |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | } |
| 132 | /* There is no point in freeing blocks at the end of a program, because |
| 133 | all of the program's space is given back to the system when the process |
| 134 | terminates.*/ |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 135 | |
Rob Landley | 658d2cf | 2005-09-08 03:11:58 +0000 | [diff] [blame] | 136 | if (ENABLE_FEATURE_CLEAN_UP) free(data); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 137 | |
Denys Vlasenko | d60752f | 2015-10-07 22:42:45 +0200 | [diff] [blame] | 138 | puts("Done"); |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 139 | } |
Rob Landley | 658d2cf | 2005-09-08 03:11:58 +0000 | [diff] [blame] | 140 | |
| 141 | if (ENABLE_FEATURE_CLEAN_UP) close(fd); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 142 | |
Glenn L McGrath | 348672d | 2004-01-20 12:57:18 +0000 | [diff] [blame] | 143 | /* Don't bother closing. Exit does |
| 144 | * that, so we can save a few bytes */ |
| 145 | return EXIT_SUCCESS; |
Eric Andersen | 01c3d40 | 2003-07-05 07:51:31 +0000 | [diff] [blame] | 146 | } |