flash_eraseall.c 5.2 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 tarball for details.
  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 uint32_t crc32(uint32_t val, const void *ss, int len,
  37. uint32_t *crc32_table)
  38. {
  39. const unsigned char *s = ss;
  40. while (--len >= 0)
  41. val = crc32_table[(val ^ *s++) & 0xff] ^ (val >> 8);
  42. return val;
  43. }
  44. static void show_progress(mtd_info_t *meminfo, erase_info_t *erase)
  45. {
  46. printf("\rErasing %u Kibyte @ %x - %2u%% complete.",
  47. (unsigned)meminfo->erasesize / 1024,
  48. erase->start,
  49. (unsigned) ((unsigned long long) erase->start * 100 / meminfo->size)
  50. );
  51. fflush_all();
  52. }
  53. int flash_eraseall_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  54. int flash_eraseall_main(int argc UNUSED_PARAM, char **argv)
  55. {
  56. struct jffs2_unknown_node cleanmarker;
  57. mtd_info_t meminfo;
  58. int fd, clmpos, clmlen;
  59. erase_info_t erase;
  60. struct stat st;
  61. unsigned int flags;
  62. char *mtd_name;
  63. opt_complementary = "=1";
  64. flags = BBTEST | getopt32(argv, "jq");
  65. mtd_name = argv[optind];
  66. fd = xopen(mtd_name, O_RDWR);
  67. fstat(fd, &st);
  68. if (!S_ISCHR(st.st_mode))
  69. bb_error_msg_and_die("%s: not a char device", mtd_name);
  70. xioctl(fd, MEMGETINFO, &meminfo);
  71. erase.length = meminfo.erasesize;
  72. if (meminfo.type == MTD_NANDFLASH)
  73. flags |= IS_NAND;
  74. clmpos = 0;
  75. clmlen = 8;
  76. if (flags & OPTION_J) {
  77. uint32_t *crc32_table;
  78. crc32_table = crc32_filltable(NULL, 0);
  79. cleanmarker.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  80. cleanmarker.nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER);
  81. if (!(flags & IS_NAND))
  82. cleanmarker.totlen = cpu_to_je32(sizeof(struct jffs2_unknown_node));
  83. else {
  84. struct nand_oobinfo oobinfo;
  85. xioctl(fd, MEMGETOOBSEL, &oobinfo);
  86. /* Check for autoplacement */
  87. if (oobinfo.useecc == MTD_NANDECC_AUTOPLACE) {
  88. /* Get the position of the free bytes */
  89. clmpos = oobinfo.oobfree[0][0];
  90. clmlen = oobinfo.oobfree[0][1];
  91. if (clmlen > 8)
  92. clmlen = 8;
  93. if (clmlen == 0)
  94. bb_error_msg_and_die("autoplacement selected and no empty space in oob");
  95. } else {
  96. /* Legacy mode */
  97. switch (meminfo.oobsize) {
  98. case 8:
  99. clmpos = 6;
  100. clmlen = 2;
  101. break;
  102. case 16:
  103. clmpos = 8;
  104. /*clmlen = 8;*/
  105. break;
  106. case 64:
  107. clmpos = 16;
  108. /*clmlen = 8;*/
  109. break;
  110. }
  111. }
  112. cleanmarker.totlen = cpu_to_je32(8);
  113. }
  114. cleanmarker.hdr_crc = cpu_to_je32(crc32(0, &cleanmarker, sizeof(struct jffs2_unknown_node) - 4,
  115. crc32_table));
  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 & BBTEST) {
  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. bb_info_msg("\nSkipping bad block at 0x%08x", 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 &= ~BBTEST;
  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. }