flash_eraseall.c 5.0 KB

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