3
0

fdformat.c 3.7 KB

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