⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmd_nand.c.l

📁 嵌入式试验箱S3C2410的bootloader源代码
💻 L
📖 第 1 页 / 共 3 页
字号:
01 /*02  * Driver for NAND support, Rick Bronson03  * borrowed heavily from:04  * (c) 1999 Machine Vision Holdings, Inc.05  * (c) 1999, 2000 David Woodhouse <dwmw2@infradead.org>06  *07  * Added 16-bit nand support08  * (C) 2004 Texas Instruments09  */10 11 #include <common.h>12 13 14 #ifndef CFG_NAND_LEGACY15 /*16  *17  * New NAND support18  *19  */20 #include <common.h>21 22 #if (CONFIG_COMMANDS & CFG_CMD_NAND)23 24 #include <command.h>25 #include <watchdog.h>26 #include <malloc.h>27 #include <asm/byteorder.h>28 29 #ifdef CONFIG_SHOW_BOOT_PROGRESS30 # include <status_led.h>31 # define SHOW_BOOT_PROGRESS(arg)    show_boot_progress(arg)32 #else33 # define SHOW_BOOT_PROGRESS(arg)34 #endif35 36 #include <jffs2/jffs2.h>37 #include <nand.h>38 39 #if (CONFIG_COMMANDS & CFG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)40 41 /* parition handling routines */42 int mtdparts_init(void);43 int id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num);44 int find_dev_and_part(const char *id, struct mtd_device **dev,45         u8 *part_num, struct part_info **part);46 #endif47 48 extern nand_info_t nand_info[];       /* info for NAND chips */49 50 static int nand_dump_oob(nand_info_t *nand, ulong off)51 {52     return 0;53 }54 55 static int nand_dump(nand_info_t *nand, ulong off)56 {57     int i;58     u_char *buf, *p;59 60     buf = malloc(nand->oobblock + nand->oobsize);61     if (!buf) {62         puts("No memory for page buffer\n");63         return 1;64     }65     off &= ~(nand->oobblock - 1);66     i = nand_read_raw(nand, buf, off, nand->oobblock, nand->oobsize);67     if (i < 0) {68         printf("Error (%d) reading page %08x\n", i, off);69         free(buf);70         return 1;71     }72     printf("Page %08x dump:\n", off);73     i = nand->oobblock >> 4; p = buf;74     while (i--) {75         printf( "\t%02x %02x %02x %02x %02x %02x %02x %02x"76             "  %02x %02x %02x %02x %02x %02x %02x %02x\n",77             p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],78             p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);79         p += 16;80     }81     puts("OOB:\n");82     i = nand->oobsize >> 3;83     while (i--) {84         printf( "\t%02x %02x %02x %02x %02x %02x %02x %02x\n",85             p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);86         p += 8;87     }88     free(buf);89 90     return 0;91 }92 93 /* ------------------------------------------------------------------------- */94 95 static inline int str2long(char *p, ulong *num)96 {97     char *endptr;98 99     *num = simple_strtoul(p, &endptr, 16);100     return (*p != '\0' && *endptr == '\0') ? 1 : 0;101 }102 103 static int104 arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off, ulong *size)105 {106     int idx = nand_curr_device;107 #if (CONFIG_COMMANDS & CFG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)108     struct mtd_device *dev;109     struct part_info *part;110     u8 pnum;111 112     if (argc >= 1 && !(str2long(argv[0], off))) {113         if ((mtdparts_init() == 0) &&114             (find_dev_and_part(argv[0], &dev, &pnum, &part) == 0)) {115             if (dev->id->type != MTD_DEV_TYPE_NAND) {116                 puts("not a NAND device\n");117                 return -1;118             }119             *off = part->offset;120             if (argc >= 2) {121                 if (!(str2long(argv[1], size))) {122                     printf("'%s' is not a number\n", argv[1]);123                     return -1;124                 }125                 if (*size > part->size)126                     *size = part->size;127             } else {128                 *size = part->size;129             }130             idx = dev->id->num;131             *nand = nand_info[idx];132             goto out;133         }134     }135 #endif136 137     if (argc >= 1) {138         if (!(str2long(argv[0], off))) {139             printf("'%s' is not a number\n", argv[0]);140             return -1;141         }142     } else {143         *off = 0;144     }145 146     if (argc >= 2) {147         if (!(str2long(argv[1], size))) {148             printf("'%s' is not a number\n", argv[1]);149             return -1;150         }151     } else {152         *size = nand->size - *off;153     }154 155 #if (CONFIG_COMMANDS & CFG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)156 out:157 #endif158     printf("device %d ", idx);159     if (*size == nand->size)160         puts("whole chip\n");161     else162         printf("offset 0x%x, size 0x%x\n", *off, *size);163     return 0;164 }165 166 int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])167 {168     int i, dev, ret;169     ulong addr, off, size;170     char *cmd, *s;171     nand_info_t *nand;172     int quiet = 0;173     const char *quiet_str = getenv("quiet");174 175     /* at least two arguments please */176     if (argc < 2)177         goto usage;178 179     if (quiet_str)180         quiet = simple_strtoul(quiet_str, NULL, 0) != 0;181 182     cmd = argv[1];183 184     if (strcmp(cmd, "info") == 0) {185 186         putc('\n');187         for (i = 0; i < CFG_MAX_NAND_DEVICE; i++) {188             if (nand_info[i].name)189                 printf("Device %d: %s, sector size %lu KiB\n",190                     i, nand_info[i].name,191                     nand_info[i].erasesize >> 10);192         }193         return 0;194     }195 196     if (strcmp(cmd, "device") == 0) {197 198         if (argc < 3) {199             if ((nand_curr_device < 0) ||200                 (nand_curr_device >= CFG_MAX_NAND_DEVICE))201                 puts("\nno devices available\n");202             else203                 printf("\nDevice %d: %s\n", nand_curr_device,204                     nand_info[nand_curr_device].name);205             return 0;206         }207         dev = (int)simple_strtoul(argv[2], NULL, 10);208         if (dev < 0 || dev >= CFG_MAX_NAND_DEVICE || !nand_info[dev].name) {209             puts("No such device\n");210             return 1;211         }212         printf("Device %d: %s", dev, nand_info[dev].name);213         puts("... is now current device\n");214         nand_curr_device = dev;215 216 #ifdef CFG_NAND_SELECT_DEVICE217         /*218          * Select the chip in the board/cpu specific driver219          */220         board_nand_select_device(nand_info[dev].priv, dev);221 #endif222 223         return 0;224     }225 226     if (strcmp(cmd, "bad") != 0 && strcmp(cmd, "erase") != 0 &&227         strncmp(cmd, "dump", 4) != 0 &&228         strncmp(cmd, "read", 4) != 0 && strncmp(cmd, "write", 5) != 0 &&229         strcmp(cmd, "scrub") != 0 && strcmp(cmd, "markbad") != 0 &&230         strcmp(cmd, "biterr") != 0 &&231         strcmp(cmd, "lock") != 0 && strcmp(cmd, "unlock") != 0 )232         goto usage;233 234     /* the following commands operate on the current device */235     if (nand_curr_device < 0 || nand_curr_device >= CFG_MAX_NAND_DEVICE ||236         !nand_info[nand_curr_device].name) {237         puts("\nno devices available\n");238         return 1;239     }240     nand = &nand_info[nand_curr_device];241 242     if (strcmp(cmd, "bad") == 0) {243         printf("\nDevice %d bad blocks:\n", nand_curr_device);244         for (off = 0; off < nand->size; off += nand->erasesize)245             if (nand_block_isbad(nand, off))246                 printf("  %08x\n", off);247         return 0;248     }249 250     /*251      * Syntax is:252      *   0    1     2       3    4253      *   nand erase [clean] [off size]254      */255     if (strcmp(cmd, "erase") == 0 || strcmp(cmd, "scrub") == 0) {256         nand_erase_options_t opts;257         /* "clean" at index 2 means request to write cleanmarker */258         int clean = argc > 2 && !strcmp("clean", argv[2]);259         int o = clean ? 3 : 2;260         int scrub = !strcmp(cmd, "scrub");261 262         printf("\nNAND %s: ", scrub ? "scrub" : "erase");263         /* skip first two or three arguments, look for offset and size */264         if (arg_off_size(argc - o, argv + o, nand, &off, &size) != 0)265             return 1;266 267         memset(&opts, 0, sizeof(opts));268         opts.offset = off;269         opts.length = size;270         opts.jffs2  = clean;271         opts.quiet  = quiet;272 273         if (scrub) {274             puts("Warning: "275                  "scrub option will erase all factory set "276                  "bad blocks!\n"277                  "         "278                  "There is no reliable way to recover them.\n"279                  "         "280                  "Use this command only for testing purposes "281                  "if you\n"282                  "         "283                  "are sure of what you are doing!\n"284                  "\nReally scrub this NAND flash? <y/N>\n");285 286             if (getc() == 'y' && getc() == '\r') {287                 opts.scrub = 1;288             } else {289                 puts("scrub aborted\n");290                 return -1;291             }292         }293         ret = nand_erase_opts(nand, &opts);294         printf("%s\n", ret ? "ERROR" : "OK");295 296         return ret == 0 ? 0 : 1;297     }298 299     if (strncmp(cmd, "dump", 4) == 0) {300         if (argc < 3)301             goto usage;302 303         s = strchr(cmd, '.');304         off = (int)simple_strtoul(argv[2], NULL, 16);305 306         if (s != NULL && strcmp(s, ".oob") == 0)307             ret = nand_dump_oob(nand, off);308         else309             ret = nand_dump(nand, off);310 311         return ret == 0 ? 1 : 0;312 313     }314 315     /* read write */316     if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {317         int read;318 319         if (argc < 4)320             goto usage;321 322         addr = (ulong)simple_strtoul(argv[2], NULL, 16);323 324         read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */325         printf("\nNAND %s: ", read ? "read" : "write");326         if (arg_off_size(argc - 3, argv + 3, nand, &off, &size) != 0)327             return 1;328 329         s = strchr(cmd, '.');330         if (s != NULL &&331             (!strcmp(s, ".jffs2") || !strcmp(s, ".e") || !strcmp(s, ".i"))) {332             if (read) {333                 /* read */334                 nand_read_options_t opts;335                 memset(&opts, 0, sizeof(opts));336                 opts.buffer = (u_char*) addr;337                 opts.length = size;338                 opts.offset = off;339                 opts.quiet      = quiet;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -