3
0

fdformat.c 3.7 KB

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