3
0

fdformat.c 3.5 KB

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