nandwrite.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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_NANDDUMP(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 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]" IF_LONG_OPTS(" [--bb=padbad|skipbad]") " [-s ADDR] [-l LEN] [-f FILE] MTD_DEVICE"
  35. //usage:#define nanddump_full_usage "\n\n"
  36. //usage: "Dump MTD_DEVICE\n"
  37. //usage: "\n -o Dump oob data"
  38. //usage: "\n -s ADDR Start address"
  39. //usage: "\n -l LEN Length"
  40. //usage: "\n -f FILE Dump to file ('-' for stdout)"
  41. //usage: IF_LONG_OPTS(
  42. //usage: "\n --bb=METHOD:"
  43. //usage: "\n skipbad: skip bad blocks"
  44. //usage: "\n padbad: substitute bad blocks by 0xff (default)"
  45. //usage: )
  46. #include "libbb.h"
  47. #include <mtd/mtd-user.h>
  48. #define IS_NANDDUMP (ENABLE_NANDDUMP && (!ENABLE_NANDWRITE || (applet_name[4] == 'd')))
  49. #define IS_NANDWRITE (ENABLE_NANDWRITE && (!ENABLE_NANDDUMP || (applet_name[4] != 'd')))
  50. #define OPT_p (1 << 0) /* nandwrite only */
  51. #define OPT_o (1 << 0) /* nanddump only */
  52. #define OPT_s (1 << 1)
  53. #define OPT_f (1 << 2)
  54. #define OPT_l (1 << 3)
  55. #define OPT_bb (1 << 4) /* must be the last one in the list */
  56. #define BB_PADBAD (1 << 0)
  57. #define BB_SKIPBAD (1 << 1)
  58. /* helper for writing out 0xff for bad blocks pad */
  59. static void dump_bad(struct mtd_info_user *meminfo, unsigned len, int oob)
  60. {
  61. unsigned char buf[meminfo->writesize];
  62. unsigned count;
  63. /* round len to the next page only if len is not already on a page */
  64. len = ((len - 1) | (meminfo->writesize - 1)) + 1;
  65. memset(buf, 0xff, sizeof(buf));
  66. for (count = 0; count < len; count += meminfo->writesize) {
  67. xwrite(STDOUT_FILENO, buf, meminfo->writesize);
  68. if (oob)
  69. xwrite(STDOUT_FILENO, buf, meminfo->oobsize);
  70. }
  71. }
  72. static unsigned next_good_eraseblock(int fd, struct mtd_info_user *meminfo,
  73. unsigned block_offset)
  74. {
  75. while (1) {
  76. loff_t offs;
  77. if (block_offset >= meminfo->size) {
  78. if (IS_NANDWRITE)
  79. bb_error_msg_and_die("not enough space in MTD device");
  80. return block_offset; /* let the caller exit */
  81. }
  82. offs = block_offset;
  83. if (xioctl(fd, MEMGETBADBLOCK, &offs) == 0)
  84. return block_offset;
  85. /* ioctl returned 1 => "bad block" */
  86. if (IS_NANDWRITE)
  87. printf("Skipping bad block at 0x%08x\n", block_offset);
  88. block_offset += meminfo->erasesize;
  89. }
  90. }
  91. int nandwrite_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  92. int nandwrite_main(int argc UNUSED_PARAM, char **argv)
  93. {
  94. /* Buffer for OOB data */
  95. unsigned char *oobbuf;
  96. unsigned opts;
  97. unsigned bb_method = BB_SKIPBAD;
  98. int fd;
  99. ssize_t cnt;
  100. unsigned mtdoffset, meminfo_writesize, blockstart, limit;
  101. unsigned end_addr = ~0;
  102. struct mtd_info_user meminfo;
  103. struct mtd_oob_buf oob;
  104. unsigned char *filebuf;
  105. const char *opt_s = "0", *opt_f = "-", *opt_l, *opt_bb;
  106. if (IS_NANDDUMP) {
  107. opt_complementary = "=1";
  108. #if ENABLE_LONG_OPTS
  109. applet_long_options =
  110. "bb\0" Required_argument "\xff"; /* no short equivalent */
  111. #endif
  112. opts = getopt32(argv, "os:f:l:", &opt_s, &opt_f, &opt_l, &opt_bb);
  113. } else { /* nandwrite */
  114. opt_complementary = "-1:?2";
  115. opts = getopt32(argv, "ps:", &opt_s);
  116. }
  117. argv += optind;
  118. if (IS_NANDWRITE && argv[1])
  119. opt_f = argv[1];
  120. if (!LONE_DASH(opt_f)) {
  121. int tmp_fd = xopen(opt_f,
  122. IS_NANDDUMP ? O_WRONLY | O_TRUNC | O_CREAT : O_RDONLY
  123. );
  124. xmove_fd(tmp_fd, IS_NANDDUMP ? STDOUT_FILENO : STDIN_FILENO);
  125. }
  126. fd = xopen(argv[0], IS_NANDWRITE ? O_RDWR : O_RDONLY);
  127. xioctl(fd, MEMGETINFO, &meminfo);
  128. mtdoffset = xstrtou(opt_s, 0);
  129. if (IS_NANDDUMP && (opts & OPT_l)) {
  130. unsigned length = xstrtou(opt_l, 0);
  131. if (length < meminfo.size - mtdoffset)
  132. end_addr = mtdoffset + length;
  133. }
  134. if (IS_NANDDUMP && (opts & OPT_bb)) {
  135. if (strcmp("skipbad", opt_bb) == 0)
  136. bb_method = BB_SKIPBAD;
  137. else if (strcmp("padbad", opt_bb) == 0)
  138. bb_method = BB_PADBAD;
  139. else
  140. bb_show_usage();
  141. }
  142. /* Pull it into a CPU register (hopefully) - smaller code that way */
  143. meminfo_writesize = meminfo.writesize;
  144. if (mtdoffset & (meminfo_writesize - 1))
  145. bb_error_msg_and_die("start address is not page aligned");
  146. filebuf = xmalloc(meminfo_writesize);
  147. oobbuf = xmalloc(meminfo.oobsize);
  148. oob.start = 0;
  149. oob.length = meminfo.oobsize;
  150. oob.ptr = oobbuf;
  151. blockstart = mtdoffset & ~(meminfo.erasesize - 1);
  152. if (blockstart != mtdoffset) {
  153. unsigned tmp;
  154. /* mtdoffset is in the middle of an erase block, verify that
  155. * this block is OK. Advance mtdoffset only if this block is
  156. * bad.
  157. */
  158. tmp = next_good_eraseblock(fd, &meminfo, blockstart);
  159. if (tmp != blockstart) {
  160. /* bad block(s), advance mtdoffset */
  161. if (IS_NANDDUMP) {
  162. if (bb_method == BB_PADBAD) {
  163. int bad_len = MIN(tmp, end_addr) - mtdoffset;
  164. dump_bad(&meminfo, bad_len, opts & OPT_o);
  165. }
  166. /* with option skipbad, increase the total length */
  167. if (bb_method == BB_SKIPBAD) {
  168. end_addr += (tmp - blockstart);
  169. }
  170. }
  171. mtdoffset = tmp;
  172. }
  173. }
  174. cnt = -1;
  175. limit = MIN(meminfo.size, end_addr);
  176. while (mtdoffset < limit) {
  177. int input_fd = IS_NANDWRITE ? STDIN_FILENO : fd;
  178. int output_fd = IS_NANDWRITE ? fd : STDOUT_FILENO;
  179. blockstart = mtdoffset & ~(meminfo.erasesize - 1);
  180. if (blockstart == mtdoffset) {
  181. /* starting a new eraseblock */
  182. mtdoffset = next_good_eraseblock(fd, &meminfo, blockstart);
  183. if (IS_NANDWRITE)
  184. printf("Writing at 0x%08x\n", mtdoffset);
  185. else if (mtdoffset > blockstart) {
  186. if (bb_method == BB_PADBAD) {
  187. /* dump FF padded bad block */
  188. int bad_len = MIN(mtdoffset, limit) - blockstart;
  189. dump_bad(&meminfo, bad_len, opts & OPT_o);
  190. } else if (bb_method == BB_SKIPBAD) {
  191. /* for skipbad, increase the length */
  192. if ((end_addr + mtdoffset - blockstart) > end_addr)
  193. end_addr += (mtdoffset - blockstart);
  194. else
  195. end_addr = ~0;
  196. limit = MIN(meminfo.size, end_addr);
  197. }
  198. }
  199. if (mtdoffset >= limit)
  200. break;
  201. }
  202. xlseek(fd, mtdoffset, SEEK_SET);
  203. /* get some more data from input */
  204. cnt = full_read(input_fd, filebuf, meminfo_writesize);
  205. if (cnt == 0) {
  206. /* even with -p, we do not pad past the end of input
  207. * (-p only zero-pads last incomplete page)
  208. */
  209. break;
  210. }
  211. if (cnt < meminfo_writesize) {
  212. if (IS_NANDDUMP)
  213. bb_error_msg_and_die("short read");
  214. if (!(opts & OPT_p))
  215. bb_error_msg_and_die("input size is not rounded up to page size, "
  216. "use -p to zero pad");
  217. /* zero pad to end of write block */
  218. memset(filebuf + cnt, 0, meminfo_writesize - cnt);
  219. }
  220. xwrite(output_fd, filebuf, meminfo_writesize);
  221. if (IS_NANDDUMP && (opts & OPT_o)) {
  222. /* Dump OOB data */
  223. oob.start = mtdoffset;
  224. xioctl(fd, MEMREADOOB, &oob);
  225. xwrite(output_fd, oobbuf, meminfo.oobsize);
  226. }
  227. mtdoffset += meminfo_writesize;
  228. if (cnt < meminfo_writesize)
  229. break;
  230. }
  231. if (IS_NANDWRITE && cnt != 0) {
  232. /* We filled entire MTD, but did we reach EOF on input? */
  233. if (full_read(STDIN_FILENO, filebuf, meminfo_writesize) != 0) {
  234. /* no */
  235. bb_error_msg_and_die("not enough space in MTD device");
  236. }
  237. }
  238. if (ENABLE_FEATURE_CLEAN_UP) {
  239. free(filebuf);
  240. close(fd);
  241. }
  242. return EXIT_SUCCESS;
  243. }