flash_eraseall.c 5.7 KB

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