nandwrite.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * nandwrite and nanddump ported to busybox from mtd-utils
  3. *
  4. * Author: Baruch Siach <baruch@tkos.co.il>, Orex Computed Radiography
  5. *
  6. * Licensed under GPLv2, see file LICENSE in this source tree.
  7. *
  8. * TODO: add support for large (>4GB) MTD devices
  9. */
  10. //config:config NANDWRITE
  11. //config: bool "nandwrite"
  12. //config: default y
  13. //config: select PLATFORM_LINUX
  14. //config: help
  15. //config: Write to the specified MTD device, with bad blocks awareness
  16. //config:
  17. //config:config NANDDUMP
  18. //config: bool "nanddump"
  19. //config: default y
  20. //config: select PLATFORM_LINUX
  21. //config: help
  22. //config: Dump the content of raw NAND chip
  23. //applet:IF_NANDWRITE(APPLET(nandwrite, BB_DIR_USR_SBIN, BB_SUID_DROP))
  24. //applet:IF_NANDWRITE(APPLET_ODDNAME(nanddump, nandwrite, BB_DIR_USR_SBIN, BB_SUID_DROP, nanddump))
  25. //kbuild:lib-$(CONFIG_NANDWRITE) += nandwrite.o
  26. //kbuild:lib-$(CONFIG_NANDDUMP) += nandwrite.o
  27. //usage:#define nandwrite_trivial_usage
  28. //usage: "[-p] [-s ADDR] MTD_DEVICE [FILE]"
  29. //usage:#define nandwrite_full_usage "\n\n"
  30. //usage: "Write to the specified MTD device\n"
  31. //usage: "\n -p Pad to page size"
  32. //usage: "\n -s ADDR Start address"
  33. //usage:#define nanddump_trivial_usage
  34. //usage: "[-o] [-b] [-s ADDR] [-f FILE] MTD_DEVICE"
  35. //usage:#define nanddump_full_usage "\n\n"
  36. //usage: "Dump the specified MTD device\n"
  37. //usage: "\n -o Dump oob data"
  38. //usage: "\n -b Omit bad block from the dump"
  39. //usage: "\n -s ADDR Start address"
  40. //usage: "\n -l LEN Length"
  41. //usage: "\n -f FILE Dump to file ('-' for stdout)"
  42. #include "libbb.h"
  43. #include <mtd/mtd-user.h>
  44. #define IS_NANDDUMP (ENABLE_NANDDUMP && (!ENABLE_NANDWRITE || (applet_name[4] == 'd')))
  45. #define IS_NANDWRITE (ENABLE_NANDWRITE && (!ENABLE_NANDDUMP || (applet_name[4] != 'd')))
  46. #define OPT_p (1 << 0) /* nandwrite only */
  47. #define OPT_o (1 << 0) /* nanddump only */
  48. #define OPT_s (1 << 1)
  49. #define OPT_b (1 << 2)
  50. #define OPT_f (1 << 3)
  51. #define OPT_l (1 << 4)
  52. /* helper for writing out 0xff for bad blocks pad */
  53. static void dump_bad(struct mtd_info_user *meminfo, unsigned len, int oob)
  54. {
  55. unsigned char buf[meminfo->writesize];
  56. unsigned count;
  57. /* round len to the next page */
  58. len = (len | ~(meminfo->writesize - 1)) + 1;
  59. memset(buf, 0xff, sizeof(buf));
  60. for (count = 0; count < len; count += meminfo->writesize) {
  61. xwrite(STDOUT_FILENO, buf, meminfo->writesize);
  62. if (oob)
  63. xwrite(STDOUT_FILENO, buf, meminfo->oobsize);
  64. }
  65. }
  66. static unsigned next_good_eraseblock(int fd, struct mtd_info_user *meminfo,
  67. unsigned block_offset)
  68. {
  69. while (1) {
  70. loff_t offs;
  71. if (block_offset >= meminfo->size) {
  72. if (IS_NANDWRITE)
  73. bb_error_msg_and_die("not enough space in MTD device");
  74. return block_offset; /* let the caller exit */
  75. }
  76. offs = block_offset;
  77. if (xioctl(fd, MEMGETBADBLOCK, &offs) == 0)
  78. return block_offset;
  79. /* ioctl returned 1 => "bad block" */
  80. if (IS_NANDWRITE)
  81. printf("Skipping bad block at 0x%08x\n", block_offset);
  82. block_offset += meminfo->erasesize;
  83. }
  84. }
  85. int nandwrite_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  86. int nandwrite_main(int argc UNUSED_PARAM, char **argv)
  87. {
  88. /* Buffer for OOB data */
  89. unsigned char *oobbuf;
  90. unsigned opts;
  91. int fd;
  92. ssize_t cnt;
  93. unsigned mtdoffset, meminfo_writesize, blockstart, limit;
  94. unsigned end_addr = ~0;
  95. struct mtd_info_user meminfo;
  96. struct mtd_oob_buf oob;
  97. unsigned char *filebuf;
  98. const char *opt_s = "0", *opt_f = "-", *opt_l;
  99. if (IS_NANDDUMP) {
  100. opt_complementary = "=1";
  101. opts = getopt32(argv, "os:bf:l:", &opt_s, &opt_f, &opt_l);
  102. } else { /* nandwrite */
  103. opt_complementary = "-1:?2";
  104. opts = getopt32(argv, "ps:", &opt_s);
  105. }
  106. argv += optind;
  107. if (IS_NANDWRITE && argv[1])
  108. opt_f = argv[1];
  109. if (!LONE_DASH(opt_f)) {
  110. int tmp_fd = xopen(opt_f,
  111. IS_NANDDUMP ? O_WRONLY | O_TRUNC | O_CREAT : O_RDONLY
  112. );
  113. xmove_fd(tmp_fd, IS_NANDDUMP ? STDOUT_FILENO : STDIN_FILENO);
  114. }
  115. fd = xopen(argv[0], IS_NANDWRITE ? O_RDWR : O_RDONLY);
  116. xioctl(fd, MEMGETINFO, &meminfo);
  117. mtdoffset = xstrtou(opt_s, 0);
  118. if (IS_NANDDUMP && (opts & OPT_l)) {
  119. unsigned length = xstrtou(opt_l, 0);
  120. if (length < meminfo.size - mtdoffset)
  121. end_addr = mtdoffset + length;
  122. }
  123. /* Pull it into a CPU register (hopefully) - smaller code that way */
  124. meminfo_writesize = meminfo.writesize;
  125. if (mtdoffset & (meminfo_writesize - 1))
  126. bb_error_msg_and_die("start address is not page aligned");
  127. filebuf = xmalloc(meminfo_writesize);
  128. oobbuf = xmalloc(meminfo.oobsize);
  129. oob.start = 0;
  130. oob.length = meminfo.oobsize;
  131. oob.ptr = oobbuf;
  132. blockstart = mtdoffset & ~(meminfo.erasesize - 1);
  133. if (blockstart != mtdoffset) {
  134. unsigned tmp;
  135. /* mtdoffset is in the middle of an erase block, verify that
  136. * this block is OK. Advance mtdoffset only if this block is
  137. * bad.
  138. */
  139. tmp = next_good_eraseblock(fd, &meminfo, blockstart);
  140. if (tmp != blockstart) {
  141. /* bad block(s), advance mtdoffset */
  142. if (IS_NANDDUMP && !(opts & OPT_b)) {
  143. int bad_len = MIN(tmp, end_addr) - mtdoffset;
  144. dump_bad(&meminfo, bad_len, opts & OPT_o);
  145. }
  146. mtdoffset = tmp;
  147. }
  148. }
  149. cnt = -1;
  150. limit = MIN(meminfo.size, end_addr);
  151. while (mtdoffset < limit) {
  152. int input_fd = IS_NANDWRITE ? STDIN_FILENO : fd;
  153. int output_fd = IS_NANDWRITE ? fd : STDOUT_FILENO;
  154. blockstart = mtdoffset & ~(meminfo.erasesize - 1);
  155. if (blockstart == mtdoffset) {
  156. /* starting a new eraseblock */
  157. mtdoffset = next_good_eraseblock(fd, &meminfo, blockstart);
  158. if (IS_NANDWRITE)
  159. printf("Writing at 0x%08x\n", mtdoffset);
  160. else if (mtdoffset > blockstart && !(opts & OPT_b)) {
  161. int bad_len = MIN(mtdoffset, limit) - blockstart;
  162. dump_bad(&meminfo, bad_len, opts & OPT_o);
  163. }
  164. if (mtdoffset >= limit)
  165. break;
  166. }
  167. xlseek(fd, mtdoffset, SEEK_SET);
  168. /* get some more data from input */
  169. cnt = full_read(input_fd, filebuf, meminfo_writesize);
  170. if (cnt == 0) {
  171. /* even with -p, we do not pad past the end of input
  172. * (-p only zero-pads last incomplete page)
  173. */
  174. break;
  175. }
  176. if (cnt < meminfo_writesize) {
  177. if (IS_NANDDUMP)
  178. bb_error_msg_and_die("short read");
  179. if (!(opts & OPT_p))
  180. bb_error_msg_and_die("input size is not rounded up to page size, "
  181. "use -p to zero pad");
  182. /* zero pad to end of write block */
  183. memset(filebuf + cnt, 0, meminfo_writesize - cnt);
  184. }
  185. xwrite(output_fd, filebuf, meminfo_writesize);
  186. if (IS_NANDDUMP && (opts & OPT_o)) {
  187. /* Dump OOB data */
  188. oob.start = mtdoffset;
  189. xioctl(fd, MEMREADOOB, &oob);
  190. xwrite(output_fd, oobbuf, meminfo.oobsize);
  191. }
  192. mtdoffset += meminfo_writesize;
  193. if (cnt < meminfo_writesize)
  194. break;
  195. }
  196. if (IS_NANDWRITE && cnt != 0) {
  197. /* We filled entire MTD, but did we reach EOF on input? */
  198. if (full_read(STDIN_FILENO, filebuf, meminfo_writesize) != 0) {
  199. /* no */
  200. bb_error_msg_and_die("not enough space in MTD device");
  201. }
  202. }
  203. if (ENABLE_FEATURE_CLEAN_UP) {
  204. free(filebuf);
  205. close(fd);
  206. }
  207. return EXIT_SUCCESS;
  208. }