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

📄 nand_util.c.l

📁 嵌入式试验箱S3C2410的bootloader源代码
💻 L
📖 第 1 页 / 共 2 页
字号:
01 /*02  * drivers/nand/nand_util.c03  *04  * Copyright (C) 2006 by Weiss-Electronic GmbH.05  * All rights reserved.06  *07  * @author: Guido Classen <clagix@gmail.com>08  * @descr:  NAND Flash support09  * @references: borrowed heavily from Linux mtd-utils code:10  *      flash_eraseall.c by Arcom Control System Ltd11  *      nandwrite.c by Steven J. Hill (sjhill@realitydiluted.com)12  *                 and Thomas Gleixner (tglx@linutronix.de)13  *14  * See file CREDITS for list of people who contributed to this15  * project.16  *17  * This program is free software; you can redistribute it and/or18  * modify it under the terms of the GNU General Public License version19  * 2 as published by the Free Software Foundation.20  *21  * This program is distributed in the hope that it will be useful,22  * but WITHOUT ANY WARRANTY; without even the implied warranty of23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the24  * GNU General Public License for more details.25  *26  * You should have received a copy of the GNU General Public License27  * along with this program; if not, write to the Free Software28  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,29  * MA 02111-1307 USA30  *31  */32 33 #include <common.h>34 35 #if (CONFIG_COMMANDS & CFG_CMD_NAND) && !defined(CFG_NAND_LEGACY)36 37 #include <command.h>38 #include <watchdog.h>39 #include <malloc.h>40 41 #include <nand.h>42 #include <jffs2/jffs2.h>43 44 typedef struct erase_info erase_info_t;45 typedef struct mtd_info   mtd_info_t;46 47 /* support only for native endian JFFS2 */48 #define cpu_to_je16(x) (x)49 #define cpu_to_je32(x) (x)50 51 /*****************************************************************************/52 static int nand_block_bad_scrub(struct mtd_info *mtd, loff_t ofs, int getchip)53 {54     return 0;55 }56 57 /**58  * nand_erase_opts: - erase NAND flash with support for various options59  *            (jffs2 formating)60  *61  * @param meminfo   NAND device to erase62  * @param opts      options,  @see struct nand_erase_options63  * @return      0 in case of success64  *65  * This code is ported from flash_eraseall.c from Linux mtd utils by66  * Arcom Control System Ltd.67  */68 int nand_erase_opts(nand_info_t *meminfo, const nand_erase_options_t *opts)69 {70     struct jffs2_unknown_node cleanmarker;71     int clmpos = 0;72     int clmlen = 8;73     erase_info_t erase;74     ulong erase_length;75     int isNAND;76     int bbtest = 1;77     int result;78     int percent_complete = -1;79     int (*nand_block_bad_old)(struct mtd_info *, loff_t, int) = NULL;80     const char *mtd_device = meminfo->name;81 82     memset(&erase, 0, sizeof(erase));83 84     erase.mtd = meminfo;85     erase.len  = meminfo->erasesize;86     erase.addr = opts->offset;87     erase_length = opts->length;88 89     isNAND = meminfo->type == MTD_NANDFLASH ? 1 : 0;90 91     if (opts->jffs2) {92         cleanmarker.magic = cpu_to_je16 (JFFS2_MAGIC_BITMASK);93         cleanmarker.nodetype = cpu_to_je16 (JFFS2_NODETYPE_CLEANMARKER);94         if (isNAND) {95             struct nand_oobinfo *oobinfo = &meminfo->oobinfo;96 97             /* check for autoplacement */98             if (oobinfo->useecc == MTD_NANDECC_AUTOPLACE) {99                 /* get the position of the free bytes */100                 if (!oobinfo->oobfree[0][1]) {101                     printf(" Eeep. Autoplacement selected "102                            "and no empty space in oob\n");103                     return -1;104                 }105                 clmpos = oobinfo->oobfree[0][0];106                 clmlen = oobinfo->oobfree[0][1];107                 if (clmlen > 8)108                     clmlen = 8;109             } else {110                 /* legacy mode */111                 switch (meminfo->oobsize) {112                 case 8:113                     clmpos = 6;114                     clmlen = 2;115                     break;116                 case 16:117                     clmpos = 8;118                     clmlen = 8;119                     break;120                 case 64:121                     clmpos = 16;122                     clmlen = 8;123                     break;124                 }125             }126 127             cleanmarker.totlen = cpu_to_je32(8);128         } else {129             cleanmarker.totlen =130                 cpu_to_je32(sizeof(struct jffs2_unknown_node));131         }132         cleanmarker.hdr_crc =  cpu_to_je32(133             crc32_no_comp(0, (unsigned char *) &cleanmarker,134                       sizeof(struct jffs2_unknown_node) - 4));135     }136 137     /* scrub option allows to erase badblock. To prevent internal138      * check from erase() method, set block check method to dummy139      * and disable bad block table while erasing.140      */141     if (opts->scrub) {142         struct nand_chip *priv_nand = meminfo->priv;143 144         nand_block_bad_old = priv_nand->block_bad;145         priv_nand->block_bad = nand_block_bad_scrub;146         /* we don't need the bad block table anymore...147          * after scrub, there are no bad blocks left!148          */149         if (priv_nand->bbt) {150             kfree(priv_nand->bbt);151         }152         priv_nand->bbt = NULL;153     }154 155     for (;156          erase.addr < opts->offset + erase_length;157          erase.addr += meminfo->erasesize) {158 159         WATCHDOG_RESET ();160 161         if (!opts->scrub && bbtest) {162             int ret = meminfo->block_isbad(meminfo, erase.addr);163             if (ret > 0) {164                 if (!opts->quiet)165                     printf("\rSkipping bad block at  "166                            "0x%08x                   "167                            "                         \n",168                            erase.addr);169                 continue;170 171             } else if (ret < 0) {172                 printf("\n%s: MTD get bad block failed: %d\n",173                        mtd_device,174                        ret);175                 return -1;176             }177         }178 179         result = meminfo->erase(meminfo, &erase);180         if (result != 0) {181             printf("\n%s: MTD Erase failure: %d\n",182                    mtd_device, result);183             continue;184         }185 186         /* format for JFFS2 ? */187         if (opts->jffs2) {188 189             /* write cleanmarker */190             if (isNAND) {191                 size_t written;192                 result = meminfo->write_oob(meminfo,193                                 erase.addr + clmpos,194                                 clmlen,195                                 &written,196                                 (unsigned char *)197                                 &cleanmarker);198                 if (result != 0) {199                     printf("\n%s: MTD writeoob failure: %d\n",200                            mtd_device, result);201                     continue;202                 }203             } else {204                 printf("\n%s: this erase routine only supports"205                        " NAND devices!\n",206                        mtd_device);207             }208         }209 210         if (!opts->quiet) {211             int percent = (int)212                 ((unsigned long long)213                  (erase.addr+meminfo->erasesize-opts->offset)214                  * 100 / erase_length);215 216             /* output progress message only at whole percent217              * steps to reduce the number of messages printed218              * on (slow) serial consoles219              */220             if (percent != percent_complete) {221                 percent_complete = percent;222 223                 printf("\rErasing at 0x%x -- %3d%% complete.",224                        erase.addr, percent);225 226                 if (opts->jffs2 && result == 0)227                     printf(" Cleanmarker written at 0x%x.",228                            erase.addr);229             }230         }231     }232     if (!opts->quiet)233         printf("\n");234 235     if (nand_block_bad_old) {236         struct nand_chip *priv_nand = meminfo->priv;237 238         priv_nand->block_bad = nand_block_bad_old;239         priv_nand->scan_bbt(meminfo);240     }241 242     return 0;243 }244 245 #define MAX_PAGE_SIZE   2048246 #define MAX_OOB_SIZE    64247 248 /*249  * buffer array used for writing data250  */251 static unsigned char data_buf[MAX_PAGE_SIZE];252 static unsigned char oob_buf[MAX_OOB_SIZE];253 254 /* OOB layouts to pass into the kernel as default */255 static struct nand_oobinfo none_oobinfo = {256     .useecc = MTD_NANDECC_OFF,257 };258 259 static struct nand_oobinfo jffs2_oobinfo = {260     .useecc = MTD_NANDECC_PLACE,261     .eccbytes = 6,262     .eccpos = { 0, 1, 2, 3, 6, 7 }263 };264 265 static struct nand_oobinfo yaffs_oobinfo = {266     .useecc = MTD_NANDECC_PLACE,267     .eccbytes = 6,268     .eccpos = { 8, 9, 10, 13, 14, 15}269 };270 271 static struct nand_oobinfo autoplace_oobinfo = {272     .useecc = MTD_NANDECC_AUTOPLACE273 };274 275 /**276  * nand_write_opts: - write image to NAND flash with support for various options277  *278  * @param meminfo   NAND device to erase279  * @param opts      write options (@see nand_write_options)280  * @return      0 in case of success281  *282  * This code is ported from nandwrite.c from Linux mtd utils by283  * Steven J. Hill and Thomas Gleixner.284  */285 int nand_write_opts(nand_info_t *meminfo, const nand_write_options_t *opts)286 {287     int imglen = 0;288     int pagelen;289     int baderaseblock;290     int blockstart = -1;291     loff_t offs;292     int readlen;293     int oobinfochanged = 0;294     int percent_complete = -1;295     struct nand_oobinfo old_oobinfo;296     ulong mtdoffset = opts->offset;297     ulong erasesize_blockalign;298     u_char *buffer = opts->buffer;299     size_t written;300     int result;301     int skipfirstblk = opts->skipfirstblk;302 303     if (opts->pad && opts->writeoob) {304         printf("Can't pad when oob data is present.\n");305         return -1;306     }307 308     /* set erasesize to specified number of blocks - to match309      * jffs2 (virtual) block size */310     if (opts->blockalign == 0) {311         erasesize_blockalign = meminfo->erasesize;312     } else {313         erasesize_blockalign = meminfo->erasesize * opts->blockalign;314     }315 316     /* make sure device page sizes are valid */317     if (!(meminfo->oobsize == 16 && meminfo->oobblock == 512)318         && !(meminfo->oobsize == 8 && meminfo->oobblock == 256)319         && !(meminfo->oobsize == 64 && meminfo->oobblock == 2048)) {320         printf("Unknown flash (not normal NAND)\n");321         return -1;322     }323 324     /* read the current oob info */325     memcpy(&old_oobinfo, &meminfo->oobinfo, sizeof(old_oobinfo));326 327     /* write without ecc? */328     if (opts->noecc) {329         memcpy(&meminfo->oobinfo, &none_oobinfo,330                sizeof(meminfo->oobinfo));331         oobinfochanged = 1;332     }333 334     /* autoplace ECC? */335     if (opts->autoplace && (old_oobinfo.useecc != MTD_NANDECC_AUTOPLACE)) {336 337         memcpy(&meminfo->oobinfo, &autoplace_oobinfo,338                sizeof(meminfo->oobinfo));339         oobinfochanged = 1;340     }341 342     /* force OOB layout for jffs2 or yaffs? */343     if (opts->forcejffs2 || opts->forceyaffs) {344         struct nand_oobinfo *oobsel =345             opts->forcejffs2 ? &jffs2_oobinfo : &yaffs_oobinfo;346 347         if (meminfo->oobsize == 8) {348             if (opts->forceyaffs) {349                 printf("YAFSS cannot operate on "350                        "256 Byte page size\n");351                 goto restoreoob;352             }353             /* Adjust number of ecc bytes */354             jffs2_oobinfo.eccbytes = 3;355         }356 357         memcpy(&meminfo->oobinfo, oobsel, sizeof(meminfo->oobinfo));358     }359 360     /* get image length */361     imglen = opts->length;362     pagelen = meminfo->oobblock363         + ((opts->writeoob != 0) ? meminfo->oobsize : 0);364 365     /* check, if file is pagealigned */366     if ((!opts->pad) && ((imglen % pagelen) != 0)) {367         printf("Input block length is not page aligned\n");368         goto restoreoob;369     }370 371     /* check, if length fits into device */372     if (((imglen / pagelen) * meminfo->oobblock)373          > (meminfo->size - opts->offset)) {374         printf("Image %d bytes, NAND page %d bytes, "375                "OOB area %u bytes, device size %u bytes\n",376                imglen, pagelen, meminfo->oobblock, meminfo->size);377         printf("Input block does not fit into device\n");378         goto restoreoob;379     }380 381     if (!opts->quiet)382         printf("\n");383 384     /* get data from input and write to the device */385     while (imglen && (mtdoffset < meminfo->size)) {386 387         WATCHDOG_RESET ();388 389         /*390          * new eraseblock, check for bad block(s). Stay in the391          * loop to be sure if the offset changes because of392          * a bad block, that the next block that will be393          * written to is also checked. Thus avoiding errors if394          * the block(s) after the skipped block(s) is also bad395          * (number of blocks depending on the blockalign396          */397         while (blockstart != (mtdoffset & (~erasesize_blockalign+1))) {398             blockstart = mtdoffset & (~erasesize_blockalign+1);399             offs = blockstart;400             baderaseblock = 0;401 402             /* check all the blocks in an erase block for403              * bad blocks */404             do {405                 int ret = meminfo->block_isbad(meminfo, offs);406 407                 if (ret < 0) {408                     printf("Bad block check failed\n");409                     goto restoreoob;410                 }411                 if (ret == 1) {412                     baderaseblock = 1;413                     if (!opts->quiet)414                         printf("\rBad block at 0x%lx "415                                "in erase block from "416                                "0x%x will be skipped\n",417                                (long) offs,418                                blockstart);419                 }420 421                 if (baderaseblock) {422                     mtdoffset = blockstart423                         + erasesize_blockalign;424                 }425                 offs +=  erasesize_blockalign426                     / opts->blockalign;427             } while (offs < blockstart + erasesize_blockalign);428         }429 430         /* skip the first good block when wirte yaffs image, by www.arm9.net */431         if (skipfirstblk) {432             mtdoffset += erasesize_blockalign;433             skipfirstblk = 0;434             continue;435         }436 

⌨️ 快捷键说明

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