3
0

flash_eraseall.c 5.8 KB

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