fdformat.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. #include "libbb.h"
  6. /* Stuff extracted from linux/fd.h */
  7. struct floppy_struct {
  8. unsigned int size, /* nr of sectors total */
  9. sect, /* sectors per track */
  10. head, /* nr of heads */
  11. track, /* nr of tracks */
  12. stretch; /* !=0 means double track steps */
  13. #define FD_STRETCH 1
  14. #define FD_SWAPSIDES 2
  15. unsigned char gap, /* gap1 size */
  16. rate, /* data rate. |= 0x40 for perpendicular */
  17. #define FD_2M 0x4
  18. #define FD_SIZECODEMASK 0x38
  19. #define FD_SIZECODE(floppy) (((((floppy)->rate&FD_SIZECODEMASK)>> 3)+ 2) %8)
  20. #define FD_SECTSIZE(floppy) ( (floppy)->rate & FD_2M ? \
  21. 512 : 128 << FD_SIZECODE(floppy) )
  22. #define FD_PERP 0x40
  23. spec1, /* stepping rate, head unload time */
  24. fmt_gap; /* gap2 size */
  25. const char * name; /* used only for predefined formats */
  26. };
  27. struct format_descr {
  28. unsigned int device,head,track;
  29. };
  30. #define FDFMTBEG _IO(2,0x47)
  31. #define FDFMTTRK _IOW(2,0x48, struct format_descr)
  32. #define FDFMTEND _IO(2,0x49)
  33. #define FDGETPRM _IOR(2, 0x04, struct floppy_struct)
  34. #define FD_FILL_BYTE 0xF6 /* format fill byte. */
  35. int fdformat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  36. int fdformat_main(int argc ATTRIBUTE_UNUSED, char **argv)
  37. {
  38. int fd, n, cyl, read_bytes, verify;
  39. unsigned char *data;
  40. struct stat st;
  41. struct floppy_struct param;
  42. struct format_descr descr;
  43. opt_complementary = "=1"; /* must have 1 param */
  44. verify = !getopt32(argv, "n");
  45. argv += optind;
  46. xstat(*argv, &st);
  47. if (!S_ISBLK(st.st_mode)) {
  48. bb_error_msg_and_die("%s: not a block device", *argv);
  49. /* do not test major - perhaps this was an USB floppy */
  50. }
  51. /* O_RDWR for formatting and verifying */
  52. fd = xopen(*argv, O_RDWR);
  53. /* original message was: "Could not determine current format type" */
  54. xioctl(fd, FDGETPRM, &param);
  55. printf("%s-sided, %d tracks, %d sec/track. Total capacity %d kB\n",
  56. (param.head == 2) ? "Double" : "Single",
  57. param.track, param.sect, param.size >> 1);
  58. /* FORMAT */
  59. printf("Formatting... ");
  60. xioctl(fd, FDFMTBEG, NULL);
  61. /* n == track */
  62. for (n = 0; n < param.track; n++) {
  63. descr.head = 0;
  64. descr.track = n;
  65. xioctl(fd, FDFMTTRK, &descr);
  66. printf("%3d\b\b\b", n);
  67. if (param.head == 2) {
  68. descr.head = 1;
  69. xioctl(fd, FDFMTTRK, &descr);
  70. }
  71. }
  72. xioctl(fd, FDFMTEND, NULL);
  73. printf("done\n");
  74. /* VERIFY */
  75. if (verify) {
  76. /* n == cyl_size */
  77. n = param.sect*param.head*512;
  78. data = xmalloc(n);
  79. printf("Verifying... ");
  80. for (cyl = 0; cyl < param.track; cyl++) {
  81. printf("%3d\b\b\b", cyl);
  82. read_bytes = safe_read(fd, data, n);
  83. if (read_bytes != n) {
  84. if (read_bytes < 0) {
  85. bb_perror_msg(bb_msg_read_error);
  86. }
  87. bb_error_msg_and_die("problem reading cylinder %d, "
  88. "expected %d, read %d", cyl, n, read_bytes);
  89. // FIXME: maybe better seek & continue??
  90. }
  91. /* Check backwards so we don't need a counter */
  92. while (--read_bytes >= 0) {
  93. if (data[read_bytes] != FD_FILL_BYTE) {
  94. printf("bad data in cyl %d\nContinuing... ", cyl);
  95. }
  96. }
  97. }
  98. /* There is no point in freeing blocks at the end of a program, because
  99. all of the program's space is given back to the system when the process
  100. terminates.*/
  101. if (ENABLE_FEATURE_CLEAN_UP) free(data);
  102. printf("done\n");
  103. }
  104. if (ENABLE_FEATURE_CLEAN_UP) close(fd);
  105. /* Don't bother closing. Exit does
  106. * that, so we can save a few bytes */
  107. return EXIT_SUCCESS;
  108. }