123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- #include "libbb.h"
- #include <mtd/mtd-user.h>
- #define IS_NANDDUMP (ENABLE_NANDDUMP && (!ENABLE_NANDWRITE || (applet_name[4] == 'd')))
- #define IS_NANDWRITE (ENABLE_NANDWRITE && (!ENABLE_NANDDUMP || (applet_name[4] != 'd')))
- #define OPT_p (1 << 0)
- #define OPT_o (1 << 0)
- #define OPT_n (1 << 1)
- #define OPT_s (1 << 2)
- #define OPT_f (1 << 3)
- #define OPT_l (1 << 4)
- #define OPT_bb (1 << 5)
- #define BB_PADBAD (1 << 0)
- #define BB_SKIPBAD (1 << 1)
- static void dump_bad(struct mtd_info_user *meminfo, unsigned len, int oob)
- {
- unsigned char buf[meminfo->writesize];
- unsigned count;
-
- len = ((len - 1) | (meminfo->writesize - 1)) + 1;
- memset(buf, 0xff, sizeof(buf));
- for (count = 0; count < len; count += meminfo->writesize) {
- xwrite(STDOUT_FILENO, buf, meminfo->writesize);
- if (oob)
- xwrite(STDOUT_FILENO, buf, meminfo->oobsize);
- }
- }
- static unsigned next_good_eraseblock(int fd, struct mtd_info_user *meminfo,
- unsigned block_offset)
- {
- while (1) {
- loff_t offs;
- if (block_offset >= meminfo->size) {
- if (IS_NANDWRITE)
- bb_error_msg_and_die("not enough space in MTD device");
- return block_offset;
- }
- offs = block_offset;
- if (xioctl(fd, MEMGETBADBLOCK, &offs) == 0)
- return block_offset;
-
- if (IS_NANDWRITE)
- printf("Skipping bad block at 0x%08x\n", block_offset);
- block_offset += meminfo->erasesize;
- }
- }
- int nandwrite_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int nandwrite_main(int argc UNUSED_PARAM, char **argv)
- {
-
- unsigned char *oobbuf;
- unsigned opts;
- unsigned bb_method = BB_SKIPBAD;
- int fd;
- ssize_t cnt;
- unsigned mtdoffset, meminfo_writesize, blockstart, limit;
- unsigned end_addr = ~0;
- struct mtd_info_user meminfo;
- struct mtd_oob_buf oob;
- unsigned char *filebuf;
- const char *opt_s = "0", *opt_f = "-", *opt_l, *opt_bb;
- if (IS_NANDDUMP) {
- opts = getopt32long(argv, "^" "ons:f:l:" "\0" "=1",
- "bb\0" Required_argument "\xff",
- &opt_s, &opt_f, &opt_l, &opt_bb
- );
- } else {
- opts = getopt32(argv, "^" "pns:" "\0" "-1:?2", &opt_s);
- }
- argv += optind;
- if (IS_NANDWRITE && argv[1])
- opt_f = argv[1];
- if (!LONE_DASH(opt_f)) {
- int tmp_fd = xopen(opt_f,
- IS_NANDDUMP ? O_WRONLY | O_TRUNC | O_CREAT : O_RDONLY
- );
- xmove_fd(tmp_fd, IS_NANDDUMP ? STDOUT_FILENO : STDIN_FILENO);
- }
- fd = xopen(argv[0], IS_NANDWRITE ? O_RDWR : O_RDONLY);
- xioctl(fd, MEMGETINFO, &meminfo);
- if (opts & OPT_n)
- xioctl(fd, MTDFILEMODE, (void *)MTD_FILE_MODE_RAW);
- mtdoffset = xstrtou(opt_s, 0);
- if (IS_NANDDUMP && (opts & OPT_l)) {
- unsigned length = xstrtou(opt_l, 0);
- if (length < meminfo.size - mtdoffset)
- end_addr = mtdoffset + length;
- }
- if (IS_NANDDUMP && (opts & OPT_bb)) {
- if (strcmp("skipbad", opt_bb) == 0)
- bb_method = BB_SKIPBAD;
- else if (strcmp("padbad", opt_bb) == 0)
- bb_method = BB_PADBAD;
- else
- bb_show_usage();
- }
-
- meminfo_writesize = meminfo.writesize;
- if (mtdoffset & (meminfo_writesize - 1))
- bb_error_msg_and_die("start address is not page aligned");
- filebuf = xmalloc(meminfo_writesize);
- oobbuf = xmalloc(meminfo.oobsize);
- oob.start = 0;
- oob.length = meminfo.oobsize;
- oob.ptr = oobbuf;
- blockstart = mtdoffset & ~(meminfo.erasesize - 1);
- if (blockstart != mtdoffset) {
- unsigned tmp;
-
- tmp = next_good_eraseblock(fd, &meminfo, blockstart);
- if (tmp != blockstart) {
-
- if (IS_NANDDUMP) {
- if (bb_method == BB_PADBAD) {
- int bad_len = MIN(tmp, end_addr) - mtdoffset;
- dump_bad(&meminfo, bad_len, opts & OPT_o);
- }
-
- if (bb_method == BB_SKIPBAD) {
- end_addr += (tmp - blockstart);
- }
- }
- mtdoffset = tmp;
- }
- }
- cnt = -1;
- limit = MIN(meminfo.size, end_addr);
- while (mtdoffset < limit) {
- int input_fd = IS_NANDWRITE ? STDIN_FILENO : fd;
- int output_fd = IS_NANDWRITE ? fd : STDOUT_FILENO;
- blockstart = mtdoffset & ~(meminfo.erasesize - 1);
- if (blockstart == mtdoffset) {
-
- mtdoffset = next_good_eraseblock(fd, &meminfo, blockstart);
- if (IS_NANDWRITE)
- printf("Writing at 0x%08x\n", mtdoffset);
- else if (mtdoffset > blockstart) {
- if (bb_method == BB_PADBAD) {
-
- int bad_len = MIN(mtdoffset, limit) - blockstart;
- dump_bad(&meminfo, bad_len, opts & OPT_o);
- } else if (bb_method == BB_SKIPBAD) {
-
- if ((end_addr + mtdoffset - blockstart) > end_addr)
- end_addr += (mtdoffset - blockstart);
- else
- end_addr = ~0;
- limit = MIN(meminfo.size, end_addr);
- }
- }
- if (mtdoffset >= limit)
- break;
- }
- xlseek(fd, mtdoffset, SEEK_SET);
-
- cnt = full_read(input_fd, filebuf, meminfo_writesize);
- if (cnt == 0) {
-
- break;
- }
- if (cnt < meminfo_writesize) {
- if (IS_NANDDUMP)
- bb_error_msg_and_die("short read");
- if (!(opts & OPT_p))
- bb_error_msg_and_die("input size is not rounded up to page size, "
- "use -p to zero pad");
-
- memset(filebuf + cnt, 0, meminfo_writesize - cnt);
- }
- xwrite(output_fd, filebuf, meminfo_writesize);
- if (IS_NANDDUMP && (opts & OPT_o)) {
-
- oob.start = mtdoffset;
- xioctl(fd, MEMREADOOB, &oob);
- xwrite(output_fd, oobbuf, meminfo.oobsize);
- }
- mtdoffset += meminfo_writesize;
- if (cnt < meminfo_writesize)
- break;
- }
- if (IS_NANDWRITE && cnt != 0) {
-
- if (full_read(STDIN_FILENO, filebuf, meminfo_writesize) != 0) {
-
- bb_error_msg_and_die("not enough space in MTD device");
- }
- }
- if (ENABLE_FEATURE_CLEAN_UP) {
- free(filebuf);
- close(fd);
- }
- return EXIT_SUCCESS;
- }
|