nandwrite.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. //applet:IF_NANDWRITE(APPLET(nandwrite, _BB_DIR_USR_SBIN, _BB_SUID_DROP))
  11. //applet:IF_NANDWRITE(APPLET_ODDNAME(nanddump, nandwrite, _BB_DIR_USR_SBIN, _BB_SUID_DROP, nanddump))
  12. //kbuild:lib-$(CONFIG_NANDWRITE) += nandwrite.o
  13. //kbuild:lib-$(CONFIG_NANDDUMP) += nandwrite.o
  14. //config:config NANDWRITE
  15. //config: bool "nandwrite"
  16. //config: default n
  17. //config: depends on PLATFORM_LINUX
  18. //config: help
  19. //config: Write to the specified MTD device, with bad blocks awareness
  20. //config:
  21. //config:config NANDDUMP
  22. //config: bool "nanddump"
  23. //config: default n
  24. //config: depends on PLATFORM_LINUX
  25. //config: help
  26. //config: Dump the content of raw NAND chip
  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 the specified MTD device\n"
  31. //usage: "\nOptions:"
  32. //usage: "\n -p Pad to page size"
  33. //usage: "\n -s ADDR Start address"
  34. //usage:#define nanddump_trivial_usage
  35. //usage: "[-o] [-b] [-s ADDR] [-f FILE] MTD_DEVICE"
  36. //usage:#define nanddump_full_usage "\n\n"
  37. //usage: "Dump the sepcified MTD device\n"
  38. //usage: "\nOptions:"
  39. //usage: "\n -o Omit oob data"
  40. //usage: "\n -b Omit bad block from the dump"
  41. //usage: "\n -s ADDR Start address"
  42. //usage: "\n -l LEN Length"
  43. //usage: "\n -f FILE Dump to file ('-' for stdout)"
  44. #include "libbb.h"
  45. #include <mtd/mtd-user.h>
  46. #define IS_NANDDUMP (ENABLE_NANDDUMP && (!ENABLE_NANDWRITE || (applet_name[4] == 'd')))
  47. #define IS_NANDWRITE (ENABLE_NANDWRITE && (!ENABLE_NANDDUMP || (applet_name[4] != 'd')))
  48. #define OPT_p (1 << 0) /* nandwrite only */
  49. #define OPT_o (1 << 0) /* nanddump only */
  50. #define OPT_s (1 << 1)
  51. #define OPT_b (1 << 2)
  52. #define OPT_f (1 << 3)
  53. #define OPT_l (1 << 4)
  54. /* helper for writing out 0xff for bad blocks pad */
  55. static void dump_bad(struct mtd_info_user *meminfo, unsigned len, int oob)
  56. {
  57. unsigned char buf[meminfo->writesize];
  58. unsigned count;
  59. /* round len to the next page */
  60. len = (len | ~(meminfo->writesize - 1)) + 1;
  61. memset(buf, 0xff, sizeof(buf));
  62. for (count = 0; count < len; count += meminfo->writesize) {
  63. xwrite(STDOUT_FILENO, buf, meminfo->writesize);
  64. if (oob)
  65. xwrite(STDOUT_FILENO, buf, meminfo->oobsize);
  66. }
  67. }
  68. static unsigned next_good_eraseblock(int fd, struct mtd_info_user *meminfo,
  69. unsigned block_offset)
  70. {
  71. while (1) {
  72. loff_t offs;
  73. if (block_offset >= meminfo->size) {
  74. if (IS_NANDWRITE)
  75. bb_error_msg_and_die("not enough space in MTD device");
  76. return block_offset; /* let the caller exit */
  77. }
  78. offs = block_offset;
  79. if (xioctl(fd, MEMGETBADBLOCK, &offs) == 0)
  80. return block_offset;
  81. /* ioctl returned 1 => "bad block" */
  82. if (IS_NANDWRITE)
  83. printf("Skipping bad block at 0x%08x\n", block_offset);
  84. block_offset += meminfo->erasesize;
  85. }
  86. }
  87. int nandwrite_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  88. int nandwrite_main(int argc UNUSED_PARAM, char **argv)
  89. {
  90. /* Buffer for OOB data */
  91. unsigned char *oobbuf;
  92. unsigned opts;
  93. int fd;
  94. ssize_t cnt;
  95. unsigned mtdoffset, meminfo_writesize, blockstart, limit;
  96. unsigned end_addr = ~0;
  97. struct mtd_info_user meminfo;
  98. struct mtd_oob_buf oob;
  99. unsigned char *filebuf;
  100. const char *opt_s = "0", *opt_f = "-", *opt_l;
  101. if (IS_NANDDUMP) {
  102. opt_complementary = "=1";
  103. opts = getopt32(argv, "os:bf:l:", &opt_s, &opt_f, &opt_l);
  104. } else { /* nandwrite */
  105. opt_complementary = "-1:?2";
  106. opts = getopt32(argv, "ps:", &opt_s);
  107. }
  108. argv += optind;
  109. if (IS_NANDWRITE && argv[1])
  110. opt_f = argv[1];
  111. if (!LONE_DASH(opt_f)) {
  112. int tmp_fd = xopen(opt_f,
  113. IS_NANDDUMP ? O_WRONLY | O_TRUNC | O_CREAT : O_RDONLY
  114. );
  115. xmove_fd(tmp_fd, IS_NANDDUMP ? STDOUT_FILENO : STDIN_FILENO);
  116. }
  117. fd = xopen(argv[0], O_RDWR);
  118. xioctl(fd, MEMGETINFO, &meminfo);
  119. mtdoffset = xstrtou(opt_s, 0);
  120. if (IS_NANDDUMP && (opts & OPT_l)) {
  121. unsigned length = xstrtou(opt_l, 0);
  122. if (length < meminfo.size - mtdoffset)
  123. end_addr = mtdoffset + length;
  124. }
  125. /* Pull it into a CPU register (hopefully) - smaller code that way */
  126. meminfo_writesize = meminfo.writesize;
  127. if (mtdoffset & (meminfo_writesize - 1))
  128. bb_error_msg_and_die("start address is not page aligned");
  129. filebuf = xmalloc(meminfo_writesize);
  130. oobbuf = xmalloc(meminfo.oobsize);
  131. oob.start = 0;
  132. oob.length = meminfo.oobsize;
  133. oob.ptr = oobbuf;
  134. blockstart = mtdoffset & ~(meminfo.erasesize - 1);
  135. if (blockstart != mtdoffset) {
  136. unsigned tmp;
  137. /* mtdoffset is in the middle of an erase block, verify that
  138. * this block is OK. Advance mtdoffset only if this block is
  139. * bad.
  140. */
  141. tmp = next_good_eraseblock(fd, &meminfo, blockstart);
  142. if (tmp != blockstart) {
  143. /* bad block(s), advance mtdoffset */
  144. if (IS_NANDDUMP & !(opts & OPT_b)) {
  145. int bad_len = MIN(tmp, end_addr) - mtdoffset;
  146. dump_bad(&meminfo, bad_len, !(opts & OPT_o));
  147. }
  148. mtdoffset = tmp;
  149. }
  150. }
  151. cnt = -1;
  152. limit = MIN(meminfo.size, end_addr);
  153. while (mtdoffset < limit) {
  154. int input_fd = IS_NANDWRITE ? STDIN_FILENO : fd;
  155. int output_fd = IS_NANDWRITE ? fd : STDOUT_FILENO;
  156. blockstart = mtdoffset & ~(meminfo.erasesize - 1);
  157. if (blockstart == mtdoffset) {
  158. /* starting a new eraseblock */
  159. mtdoffset = next_good_eraseblock(fd, &meminfo, blockstart);
  160. if (IS_NANDWRITE)
  161. printf("Writing at 0x%08x\n", mtdoffset);
  162. else if (mtdoffset > blockstart) {
  163. int bad_len = MIN(mtdoffset, limit) - blockstart;
  164. dump_bad(&meminfo, bad_len, !(opts & OPT_o));
  165. }
  166. if (mtdoffset >= limit)
  167. break;
  168. }
  169. xlseek(fd, mtdoffset, SEEK_SET);
  170. /* get some more data from input */
  171. cnt = full_read(input_fd, filebuf, meminfo_writesize);
  172. if (cnt == 0) {
  173. /* even with -p, we do not pad past the end of input
  174. * (-p only zero-pads last incomplete page)
  175. */
  176. break;
  177. }
  178. if (cnt < meminfo_writesize) {
  179. if (IS_NANDDUMP)
  180. bb_error_msg_and_die("short read");
  181. if (!(opts & OPT_p))
  182. bb_error_msg_and_die("input size is not rounded up to page size, "
  183. "use -p to zero pad");
  184. /* zero pad to end of write block */
  185. memset(filebuf + cnt, 0, meminfo_writesize - cnt);
  186. }
  187. xwrite(output_fd, filebuf, meminfo_writesize);
  188. if (IS_NANDDUMP && !(opts & OPT_o)) {
  189. /* Dump OOB data */
  190. oob.start = mtdoffset;
  191. xioctl(fd, MEMREADOOB, &oob);
  192. xwrite(output_fd, oobbuf, meminfo.oobsize);
  193. }
  194. mtdoffset += meminfo_writesize;
  195. if (cnt < meminfo_writesize)
  196. break;
  197. }
  198. if (IS_NANDWRITE && cnt != 0) {
  199. /* We filled entire MTD, but did we reach EOF on input? */
  200. if (full_read(STDIN_FILENO, filebuf, meminfo_writesize) != 0) {
  201. /* no */
  202. bb_error_msg_and_die("not enough space in MTD device");
  203. }
  204. }
  205. if (ENABLE_FEATURE_CLEAN_UP) {
  206. free(filebuf);
  207. close(fd);
  208. }
  209. return EXIT_SUCCESS;
  210. }