nandwrite.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 (5.9 kb)"
  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 (6.3 kb)"
  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. opts = getopt32long(argv, "^" "ons:f:l:" "\0" "=1",
  111. "bb\0" Required_argument "\xff", /* no short equivalent */
  112. &opt_s, &opt_f, &opt_l, &opt_bb
  113. );
  114. } else { /* nandwrite */
  115. opts = getopt32(argv, "^" "pns:" "\0" "-1:?2", &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. if (opts & OPT_n)
  129. xioctl(fd, MTDFILEMODE, (void *)MTD_FILE_MODE_RAW);
  130. mtdoffset = xstrtou(opt_s, 0);
  131. if (IS_NANDDUMP && (opts & OPT_l)) {
  132. unsigned length = xstrtou(opt_l, 0);
  133. if (length < meminfo.size - mtdoffset)
  134. end_addr = mtdoffset + length;
  135. }
  136. if (IS_NANDDUMP && (opts & OPT_bb)) {
  137. if (strcmp("skipbad", opt_bb) == 0)
  138. bb_method = BB_SKIPBAD;
  139. else if (strcmp("padbad", opt_bb) == 0)
  140. bb_method = BB_PADBAD;
  141. else
  142. bb_show_usage();
  143. }
  144. /* Pull it into a CPU register (hopefully) - smaller code that way */
  145. meminfo_writesize = meminfo.writesize;
  146. if (mtdoffset & (meminfo_writesize - 1))
  147. bb_error_msg_and_die("start address is not page aligned");
  148. filebuf = xmalloc(meminfo_writesize);
  149. oobbuf = xmalloc(meminfo.oobsize);
  150. oob.start = 0;
  151. oob.length = meminfo.oobsize;
  152. oob.ptr = oobbuf;
  153. blockstart = mtdoffset & ~(meminfo.erasesize - 1);
  154. if (blockstart != mtdoffset) {
  155. unsigned tmp;
  156. /* mtdoffset is in the middle of an erase block, verify that
  157. * this block is OK. Advance mtdoffset only if this block is
  158. * bad.
  159. */
  160. tmp = next_good_eraseblock(fd, &meminfo, blockstart);
  161. if (tmp != blockstart) {
  162. /* bad block(s), advance mtdoffset */
  163. if (IS_NANDDUMP) {
  164. if (bb_method == BB_PADBAD) {
  165. int bad_len = MIN(tmp, end_addr) - mtdoffset;
  166. dump_bad(&meminfo, bad_len, opts & OPT_o);
  167. }
  168. /* with option skipbad, increase the total length */
  169. if (bb_method == BB_SKIPBAD) {
  170. end_addr += (tmp - blockstart);
  171. }
  172. }
  173. mtdoffset = tmp;
  174. }
  175. }
  176. cnt = -1;
  177. limit = MIN(meminfo.size, end_addr);
  178. while (mtdoffset < limit) {
  179. int input_fd = IS_NANDWRITE ? STDIN_FILENO : fd;
  180. int output_fd = IS_NANDWRITE ? fd : STDOUT_FILENO;
  181. blockstart = mtdoffset & ~(meminfo.erasesize - 1);
  182. if (blockstart == mtdoffset) {
  183. /* starting a new eraseblock */
  184. mtdoffset = next_good_eraseblock(fd, &meminfo, blockstart);
  185. if (IS_NANDWRITE)
  186. printf("Writing at 0x%08x\n", mtdoffset);
  187. else if (mtdoffset > blockstart) {
  188. if (bb_method == BB_PADBAD) {
  189. /* dump FF padded bad block */
  190. int bad_len = MIN(mtdoffset, limit) - blockstart;
  191. dump_bad(&meminfo, bad_len, opts & OPT_o);
  192. } else if (bb_method == BB_SKIPBAD) {
  193. /* for skipbad, increase the length */
  194. if ((end_addr + mtdoffset - blockstart) > end_addr)
  195. end_addr += (mtdoffset - blockstart);
  196. else
  197. end_addr = ~0;
  198. limit = MIN(meminfo.size, end_addr);
  199. }
  200. }
  201. if (mtdoffset >= limit)
  202. break;
  203. }
  204. xlseek(fd, mtdoffset, SEEK_SET);
  205. /* get some more data from input */
  206. cnt = full_read(input_fd, filebuf, meminfo_writesize);
  207. if (cnt == 0) {
  208. /* even with -p, we do not pad past the end of input
  209. * (-p only zero-pads last incomplete page)
  210. */
  211. break;
  212. }
  213. if (cnt < meminfo_writesize) {
  214. if (IS_NANDDUMP)
  215. bb_error_msg_and_die("short read");
  216. if (!(opts & OPT_p))
  217. bb_error_msg_and_die("input size is not rounded up to page size, "
  218. "use -p to zero pad");
  219. /* zero pad to end of write block */
  220. memset(filebuf + cnt, 0, meminfo_writesize - cnt);
  221. }
  222. xwrite(output_fd, filebuf, meminfo_writesize);
  223. if (IS_NANDDUMP && (opts & OPT_o)) {
  224. /* Dump OOB data */
  225. oob.start = mtdoffset;
  226. xioctl(fd, MEMREADOOB, &oob);
  227. xwrite(output_fd, oobbuf, meminfo.oobsize);
  228. }
  229. mtdoffset += meminfo_writesize;
  230. if (cnt < meminfo_writesize)
  231. break;
  232. }
  233. if (IS_NANDWRITE && cnt != 0) {
  234. /* We filled entire MTD, but did we reach EOF on input? */
  235. if (full_read(STDIN_FILENO, filebuf, meminfo_writesize) != 0) {
  236. /* no */
  237. bb_error_msg_and_die("not enough space in MTD device");
  238. }
  239. }
  240. if (ENABLE_FEATURE_CLEAN_UP) {
  241. free(filebuf);
  242. close(fd);
  243. }
  244. return EXIT_SUCCESS;
  245. }