3
0

fdformat.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * fdformat.c - Low-level formats a floppy disk - Werner Almesberger
  4. * 5 July 2003 -- modified for Busybox by Erik Andersen
  5. *
  6. * Licensed under GPLv2, see file LICENSE in this source tree.
  7. */
  8. //config:config FDFORMAT
  9. //config: bool "fdformat (4.7 kb)"
  10. //config: default y
  11. //config: help
  12. //config: fdformat is used to low-level format a floppy disk.
  13. //applet:IF_FDFORMAT(APPLET(fdformat, BB_DIR_USR_SBIN, BB_SUID_DROP))
  14. //kbuild:lib-$(CONFIG_FDFORMAT) += fdformat.o
  15. //usage:#define fdformat_trivial_usage
  16. //usage: "[-n] DEVICE"
  17. //usage:#define fdformat_full_usage "\n\n"
  18. //usage: "Format floppy disk\n"
  19. //usage: "\n -n Don't verify after format"
  20. #include "libbb.h"
  21. /* Stuff extracted from linux/fd.h */
  22. struct floppy_struct {
  23. unsigned int size, /* nr of sectors total */
  24. sect, /* sectors per track */
  25. head, /* nr of heads */
  26. track, /* nr of tracks */
  27. stretch; /* !=0 means double track steps */
  28. #define FD_STRETCH 1
  29. #define FD_SWAPSIDES 2
  30. unsigned char gap, /* gap1 size */
  31. rate, /* data rate. |= 0x40 for perpendicular */
  32. #define FD_2M 0x4
  33. #define FD_SIZECODEMASK 0x38
  34. #define FD_SIZECODE(floppy) (((((floppy)->rate&FD_SIZECODEMASK)>> 3)+ 2) %8)
  35. #define FD_SECTSIZE(floppy) ( (floppy)->rate & FD_2M ? \
  36. 512 : 128 << FD_SIZECODE(floppy) )
  37. #define FD_PERP 0x40
  38. spec1, /* stepping rate, head unload time */
  39. fmt_gap; /* gap2 size */
  40. const char * name; /* used only for predefined formats */
  41. };
  42. struct format_descr {
  43. unsigned int device,head,track;
  44. };
  45. #define FDFMTBEG _IO(2,0x47)
  46. #define FDFMTTRK _IOW(2,0x48, struct format_descr)
  47. #define FDFMTEND _IO(2,0x49)
  48. #define FDGETPRM _IOR(2, 0x04, struct floppy_struct)
  49. #define FD_FILL_BYTE 0xF6 /* format fill byte. */
  50. int fdformat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  51. int fdformat_main(int argc UNUSED_PARAM, char **argv)
  52. {
  53. int fd, n, cyl, read_bytes, verify;
  54. unsigned char *data;
  55. struct stat st;
  56. struct floppy_struct param;
  57. struct format_descr descr;
  58. verify = !getopt32(argv, "^" "n" "\0" "=1");
  59. argv += optind;
  60. xstat(*argv, &st);
  61. if (!S_ISBLK(st.st_mode)) {
  62. bb_error_msg_and_die("%s: not a block device", *argv);
  63. /* do not test major - perhaps this was an USB floppy */
  64. }
  65. /* O_RDWR for formatting and verifying */
  66. fd = xopen(*argv, O_RDWR);
  67. /* original message was: "Could not determine current format type" */
  68. xioctl(fd, FDGETPRM, &param);
  69. printf("%s-sided, %u tracks, %u sec/track. Total capacity %d kB\n",
  70. (param.head == 2) ? "Double" : "Single",
  71. param.track, param.sect, param.size >> 1);
  72. /* FORMAT */
  73. printf("Formatting... ");
  74. xioctl(fd, FDFMTBEG, NULL);
  75. /* n == track */
  76. for (n = 0; n < param.track; n++) {
  77. descr.head = 0;
  78. descr.track = n;
  79. xioctl(fd, FDFMTTRK, &descr);
  80. printf("%3d\b\b\b", n);
  81. if (param.head == 2) {
  82. descr.head = 1;
  83. xioctl(fd, FDFMTTRK, &descr);
  84. }
  85. }
  86. xioctl(fd, FDFMTEND, NULL);
  87. puts("Done");
  88. /* VERIFY */
  89. if (verify) {
  90. /* n == cyl_size */
  91. n = param.sect*param.head*512;
  92. data = xmalloc(n);
  93. printf("Verifying... ");
  94. for (cyl = 0; cyl < param.track; cyl++) {
  95. printf("%3d\b\b\b", cyl);
  96. read_bytes = safe_read(fd, data, n);
  97. if (read_bytes != n) {
  98. if (read_bytes < 0) {
  99. bb_simple_perror_msg(bb_msg_read_error);
  100. }
  101. bb_error_msg_and_die("problem reading cylinder %d, "
  102. "expected %d, read %d", cyl, n, read_bytes);
  103. // FIXME: maybe better seek & continue??
  104. }
  105. /* Check backwards so we don't need a counter */
  106. while (--read_bytes >= 0) {
  107. if (data[read_bytes] != FD_FILL_BYTE) {
  108. printf("bad data in cyl %d\nContinuing... ", cyl);
  109. }
  110. }
  111. }
  112. /* There is no point in freeing blocks at the end of a program, because
  113. all of the program's space is given back to the system when the process
  114. terminates.*/
  115. if (ENABLE_FEATURE_CLEAN_UP) free(data);
  116. puts("Done");
  117. }
  118. if (ENABLE_FEATURE_CLEAN_UP) close(fd);
  119. /* Don't bother closing. Exit does
  120. * that, so we can save a few bytes */
  121. return EXIT_SUCCESS;
  122. }