3
0

nandwrite.c 8.0 KB

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