flash_eraseall.c 5.3 KB

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