nandwrite.c 8.2 KB

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