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