linux/drivers/mtd/tests/mtd_readtest.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2006-2008 Nokia Corporation
   3 *
   4 * This program is free software; you can redistribute it and/or modify it
   5 * under the terms of the GNU General Public License version 2 as published by
   6 * the Free Software Foundation.
   7 *
   8 * This program is distributed in the hope that it will be useful, but WITHOUT
   9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11 * more details.
  12 *
  13 * You should have received a copy of the GNU General Public License along with
  14 * this program; see the file COPYING. If not, write to the Free Software
  15 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16 *
  17 * Check MTD device read.
  18 *
  19 * Author: Adrian Hunter <ext-adrian.hunter@nokia.com>
  20 */
  21
  22#include <linux/init.h>
  23#include <linux/module.h>
  24#include <linux/moduleparam.h>
  25#include <linux/err.h>
  26#include <linux/mtd/mtd.h>
  27#include <linux/slab.h>
  28#include <linux/sched.h>
  29
  30#define PRINT_PREF KERN_INFO "mtd_readtest: "
  31
  32static int dev;
  33module_param(dev, int, S_IRUGO);
  34MODULE_PARM_DESC(dev, "MTD device number to use");
  35
  36static struct mtd_info *mtd;
  37static unsigned char *iobuf;
  38static unsigned char *iobuf1;
  39static unsigned char *bbt;
  40
  41static int pgsize;
  42static int ebcnt;
  43static int pgcnt;
  44
  45static int read_eraseblock_by_page(int ebnum)
  46{
  47        size_t read = 0;
  48        int i, ret, err = 0;
  49        loff_t addr = ebnum * mtd->erasesize;
  50        void *buf = iobuf;
  51        void *oobbuf = iobuf1;
  52
  53        for (i = 0; i < pgcnt; i++) {
  54                memset(buf, 0 , pgcnt);
  55                ret = mtd->read(mtd, addr, pgsize, &read, buf);
  56                if (ret == -EUCLEAN)
  57                        ret = 0;
  58                if (ret || read != pgsize) {
  59                        printk(PRINT_PREF "error: read failed at %#llx\n",
  60                               (long long)addr);
  61                        if (!err)
  62                                err = ret;
  63                        if (!err)
  64                                err = -EINVAL;
  65                }
  66                if (mtd->oobsize) {
  67                        struct mtd_oob_ops ops;
  68
  69                        ops.mode      = MTD_OOB_PLACE;
  70                        ops.len       = 0;
  71                        ops.retlen    = 0;
  72                        ops.ooblen    = mtd->oobsize;
  73                        ops.oobretlen = 0;
  74                        ops.ooboffs   = 0;
  75                        ops.datbuf    = NULL;
  76                        ops.oobbuf    = oobbuf;
  77                        ret = mtd->read_oob(mtd, addr, &ops);
  78                        if (ret || ops.oobretlen != mtd->oobsize) {
  79                                printk(PRINT_PREF "error: read oob failed at "
  80                                                  "%#llx\n", (long long)addr);
  81                                if (!err)
  82                                        err = ret;
  83                                if (!err)
  84                                        err = -EINVAL;
  85                        }
  86                        oobbuf += mtd->oobsize;
  87                }
  88                addr += pgsize;
  89                buf += pgsize;
  90        }
  91
  92        return err;
  93}
  94
  95static void dump_eraseblock(int ebnum)
  96{
  97        int i, j, n;
  98        char line[128];
  99        int pg, oob;
 100
 101        printk(PRINT_PREF "dumping eraseblock %d\n", ebnum);
 102        n = mtd->erasesize;
 103        for (i = 0; i < n;) {
 104                char *p = line;
 105
 106                p += sprintf(p, "%05x: ", i);
 107                for (j = 0; j < 32 && i < n; j++, i++)
 108                        p += sprintf(p, "%02x", (unsigned int)iobuf[i]);
 109                printk(KERN_CRIT "%s\n", line);
 110                cond_resched();
 111        }
 112        if (!mtd->oobsize)
 113                return;
 114        printk(PRINT_PREF "dumping oob from eraseblock %d\n", ebnum);
 115        n = mtd->oobsize;
 116        for (pg = 0, i = 0; pg < pgcnt; pg++)
 117                for (oob = 0; oob < n;) {
 118                        char *p = line;
 119
 120                        p += sprintf(p, "%05x: ", i);
 121                        for (j = 0; j < 32 && oob < n; j++, oob++, i++)
 122                                p += sprintf(p, "%02x",
 123                                             (unsigned int)iobuf1[i]);
 124                        printk(KERN_CRIT "%s\n", line);
 125                        cond_resched();
 126                }
 127}
 128
 129static int is_block_bad(int ebnum)
 130{
 131        loff_t addr = ebnum * mtd->erasesize;
 132        int ret;
 133
 134        ret = mtd->block_isbad(mtd, addr);
 135        if (ret)
 136                printk(PRINT_PREF "block %d is bad\n", ebnum);
 137        return ret;
 138}
 139
 140static int scan_for_bad_eraseblocks(void)
 141{
 142        int i, bad = 0;
 143
 144        bbt = kzalloc(ebcnt, GFP_KERNEL);
 145        if (!bbt) {
 146                printk(PRINT_PREF "error: cannot allocate memory\n");
 147                return -ENOMEM;
 148        }
 149
 150        /* NOR flash does not implement block_isbad */
 151        if (mtd->block_isbad == NULL)
 152                return 0;
 153
 154        printk(PRINT_PREF "scanning for bad eraseblocks\n");
 155        for (i = 0; i < ebcnt; ++i) {
 156                bbt[i] = is_block_bad(i) ? 1 : 0;
 157                if (bbt[i])
 158                        bad += 1;
 159                cond_resched();
 160        }
 161        printk(PRINT_PREF "scanned %d eraseblocks, %d are bad\n", i, bad);
 162        return 0;
 163}
 164
 165static int __init mtd_readtest_init(void)
 166{
 167        uint64_t tmp;
 168        int err, i;
 169
 170        printk(KERN_INFO "\n");
 171        printk(KERN_INFO "=================================================\n");
 172        printk(PRINT_PREF "MTD device: %d\n", dev);
 173
 174        mtd = get_mtd_device(NULL, dev);
 175        if (IS_ERR(mtd)) {
 176                err = PTR_ERR(mtd);
 177                printk(PRINT_PREF "error: Cannot get MTD device\n");
 178                return err;
 179        }
 180
 181        if (mtd->writesize == 1) {
 182                printk(PRINT_PREF "not NAND flash, assume page size is 512 "
 183                       "bytes.\n");
 184                pgsize = 512;
 185        } else
 186                pgsize = mtd->writesize;
 187
 188        tmp = mtd->size;
 189        do_div(tmp, mtd->erasesize);
 190        ebcnt = tmp;
 191        pgcnt = mtd->erasesize / pgsize;
 192
 193        printk(PRINT_PREF "MTD device size %llu, eraseblock size %u, "
 194               "page size %u, count of eraseblocks %u, pages per "
 195               "eraseblock %u, OOB size %u\n",
 196               (unsigned long long)mtd->size, mtd->erasesize,
 197               pgsize, ebcnt, pgcnt, mtd->oobsize);
 198
 199        err = -ENOMEM;
 200        iobuf = kmalloc(mtd->erasesize, GFP_KERNEL);
 201        if (!iobuf) {
 202                printk(PRINT_PREF "error: cannot allocate memory\n");
 203                goto out;
 204        }
 205        iobuf1 = kmalloc(mtd->erasesize, GFP_KERNEL);
 206        if (!iobuf1) {
 207                printk(PRINT_PREF "error: cannot allocate memory\n");
 208                goto out;
 209        }
 210
 211        err = scan_for_bad_eraseblocks();
 212        if (err)
 213                goto out;
 214
 215        /* Read all eraseblocks 1 page at a time */
 216        printk(PRINT_PREF "testing page read\n");
 217        for (i = 0; i < ebcnt; ++i) {
 218                int ret;
 219
 220                if (bbt[i])
 221                        continue;
 222                ret = read_eraseblock_by_page(i);
 223                if (ret) {
 224                        dump_eraseblock(i);
 225                        if (!err)
 226                                err = ret;
 227                }
 228                cond_resched();
 229        }
 230
 231        if (err)
 232                printk(PRINT_PREF "finished with errors\n");
 233        else
 234                printk(PRINT_PREF "finished\n");
 235
 236out:
 237
 238        kfree(iobuf);
 239        kfree(iobuf1);
 240        kfree(bbt);
 241        put_mtd_device(mtd);
 242        if (err)
 243                printk(PRINT_PREF "error %d occurred\n", err);
 244        printk(KERN_INFO "=================================================\n");
 245        return err;
 246}
 247module_init(mtd_readtest_init);
 248
 249static void __exit mtd_readtest_exit(void)
 250{
 251        return;
 252}
 253module_exit(mtd_readtest_exit);
 254
 255MODULE_DESCRIPTION("Read test module");
 256MODULE_AUTHOR("Adrian Hunter");
 257MODULE_LICENSE("GPL");
 258