fdformat.c 4.2 KB

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