fdformat.c 3.9 KB

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