3
0

nandwrite.c 8.2 KB

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