flash_eraseall.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* vi: set sw=4 ts=4: */
  2. /* eraseall.c -- erase the whole of a MTD device
  3. *
  4. * Ported to busybox from mtd-utils.
  5. *
  6. * Copyright (C) 2000 Arcom Control System Ltd
  7. *
  8. * Renamed to flash_eraseall.c
  9. *
  10. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  11. */
  12. //usage:#define flash_eraseall_trivial_usage
  13. //usage: "[-jNq] MTD_DEVICE"
  14. //usage:#define flash_eraseall_full_usage "\n\n"
  15. //usage: "Erase an MTD device\n"
  16. //usage: "\n -j Format the device for jffs2"
  17. //usage: "\n -N Don't skip bad blocks"
  18. //usage: "\n -q Don't display progress messages"
  19. #include "libbb.h"
  20. #include <mtd/mtd-user.h>
  21. #include <linux/jffs2.h>
  22. #define OPTION_J (1 << 0)
  23. #define OPTION_N (1 << 1)
  24. #define OPTION_Q (1 << 2)
  25. #define IS_NAND (1 << 3)
  26. /* mtd/jffs2-user.h used to have this atrocity:
  27. extern int target_endian;
  28. #define t16(x) ({ __u16 __b = (x); (target_endian==__BYTE_ORDER)?__b:bswap_16(__b); })
  29. #define t32(x) ({ __u32 __b = (x); (target_endian==__BYTE_ORDER)?__b:bswap_32(__b); })
  30. #define cpu_to_je16(x) ((jint16_t){t16(x)})
  31. #define cpu_to_je32(x) ((jint32_t){t32(x)})
  32. #define cpu_to_jemode(x) ((jmode_t){t32(x)})
  33. #define je16_to_cpu(x) (t16((x).v16))
  34. #define je32_to_cpu(x) (t32((x).v32))
  35. #define jemode_to_cpu(x) (t32((x).m))
  36. but mtd/jffs2-user.h is gone now (at least 2.6.31.6 does not have it anymore)
  37. */
  38. /* We always use native endianness */
  39. #undef cpu_to_je16
  40. #undef cpu_to_je32
  41. #define cpu_to_je16(v) ((jint16_t){(v)})
  42. #define cpu_to_je32(v) ((jint32_t){(v)})
  43. static void show_progress(mtd_info_t *meminfo, erase_info_t *erase)
  44. {
  45. printf("\rErasing %u Kibyte @ %x - %2u%% complete.",
  46. (unsigned)meminfo->erasesize / 1024,
  47. erase->start,
  48. (unsigned) ((unsigned long long) erase->start * 100 / meminfo->size)
  49. );
  50. fflush_all();
  51. }
  52. int flash_eraseall_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  53. int flash_eraseall_main(int argc UNUSED_PARAM, char **argv)
  54. {
  55. struct jffs2_unknown_node cleanmarker;
  56. mtd_info_t meminfo;
  57. int fd, clmpos, clmlen;
  58. erase_info_t erase;
  59. struct stat st;
  60. unsigned int flags;
  61. char *mtd_name;
  62. opt_complementary = "=1";
  63. flags = getopt32(argv, "jNq");
  64. mtd_name = argv[optind];
  65. fd = xopen(mtd_name, O_RDWR);
  66. fstat(fd, &st);
  67. if (!S_ISCHR(st.st_mode))
  68. bb_error_msg_and_die("%s: not a char device", mtd_name);
  69. xioctl(fd, MEMGETINFO, &meminfo);
  70. erase.length = meminfo.erasesize;
  71. if (meminfo.type == MTD_NANDFLASH)
  72. flags |= IS_NAND;
  73. clmpos = 0;
  74. clmlen = 8;
  75. if (flags & OPTION_J) {
  76. uint32_t *crc32_table;
  77. crc32_table = crc32_filltable(NULL, 0);
  78. cleanmarker.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  79. cleanmarker.nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER);
  80. if (!(flags & IS_NAND))
  81. cleanmarker.totlen = cpu_to_je32(sizeof(struct jffs2_unknown_node));
  82. else {
  83. struct nand_oobinfo oobinfo;
  84. xioctl(fd, MEMGETOOBSEL, &oobinfo);
  85. /* Check for autoplacement */
  86. if (oobinfo.useecc == MTD_NANDECC_AUTOPLACE) {
  87. /* Get the position of the free bytes */
  88. clmpos = oobinfo.oobfree[0][0];
  89. clmlen = oobinfo.oobfree[0][1];
  90. if (clmlen > 8)
  91. clmlen = 8;
  92. if (clmlen == 0)
  93. bb_error_msg_and_die("autoplacement selected and no empty space in oob");
  94. } else {
  95. /* Legacy mode */
  96. switch (meminfo.oobsize) {
  97. case 8:
  98. clmpos = 6;
  99. clmlen = 2;
  100. break;
  101. case 16:
  102. clmpos = 8;
  103. /*clmlen = 8;*/
  104. break;
  105. case 64:
  106. clmpos = 16;
  107. /*clmlen = 8;*/
  108. break;
  109. }
  110. }
  111. cleanmarker.totlen = cpu_to_je32(8);
  112. }
  113. cleanmarker.hdr_crc = cpu_to_je32(
  114. crc32_block_endian0(0, &cleanmarker, sizeof(struct jffs2_unknown_node) - 4, crc32_table)
  115. );
  116. }
  117. /* Don't want to destroy progress indicator by bb_error_msg's */
  118. applet_name = xasprintf("\n%s: %s", applet_name, mtd_name);
  119. for (erase.start = 0; erase.start < meminfo.size;
  120. erase.start += meminfo.erasesize) {
  121. if (!(flags & OPTION_N)) {
  122. int ret;
  123. loff_t offset = erase.start;
  124. ret = ioctl(fd, MEMGETBADBLOCK, &offset);
  125. if (ret > 0) {
  126. if (!(flags & OPTION_Q))
  127. printf("\nSkipping bad block at 0x%08x\n", erase.start);
  128. continue;
  129. }
  130. if (ret < 0) {
  131. /* Black block table is not available on certain flash
  132. * types e.g. NOR
  133. */
  134. if (errno == EOPNOTSUPP) {
  135. flags |= OPTION_N;
  136. if (flags & IS_NAND)
  137. bb_error_msg_and_die("bad block check not available");
  138. } else {
  139. bb_perror_msg_and_die("MEMGETBADBLOCK error");
  140. }
  141. }
  142. }
  143. if (!(flags & OPTION_Q))
  144. show_progress(&meminfo, &erase);
  145. xioctl(fd, MEMERASE, &erase);
  146. /* format for JFFS2 ? */
  147. if (!(flags & OPTION_J))
  148. continue;
  149. /* write cleanmarker */
  150. if (flags & IS_NAND) {
  151. struct mtd_oob_buf oob;
  152. oob.ptr = (unsigned char *) &cleanmarker;
  153. oob.start = erase.start + clmpos;
  154. oob.length = clmlen;
  155. xioctl(fd, MEMWRITEOOB, &oob);
  156. } else {
  157. xlseek(fd, erase.start, SEEK_SET);
  158. /* if (lseek(fd, erase.start, SEEK_SET) < 0) {
  159. bb_perror_msg("MTD %s failure", "seek");
  160. continue;
  161. } */
  162. xwrite(fd, &cleanmarker, sizeof(cleanmarker));
  163. /* if (write(fd, &cleanmarker, sizeof(cleanmarker)) != sizeof(cleanmarker)) {
  164. bb_perror_msg("MTD %s failure", "write");
  165. continue;
  166. } */
  167. }
  168. if (!(flags & OPTION_Q))
  169. printf(" Cleanmarker written at %x.", erase.start);
  170. }
  171. if (!(flags & OPTION_Q)) {
  172. show_progress(&meminfo, &erase);
  173. bb_putchar('\n');
  174. }
  175. if (ENABLE_FEATURE_CLEAN_UP)
  176. close(fd);
  177. return EXIT_SUCCESS;
  178. }