/drivers/mtd/tests/mtd_oobtest.c
C | 733 lines | 615 code | 75 blank | 43 comment | 110 complexity | 40c4bfc71d2e95950bf5ff4c28220be9 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
- /*
- * Copyright (C) 2006-2008 Nokia Corporation
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 as published by
- * the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
- * more details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program; see the file COPYING. If not, write to the Free Software
- * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- *
- * Test OOB read and write on MTD device.
- *
- * Author: Adrian Hunter <ext-adrian.hunter@nokia.com>
- */
- #include <asm/div64.h>
- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/moduleparam.h>
- #include <linux/err.h>
- #include <linux/mtd/mtd.h>
- #include <linux/slab.h>
- #include <linux/sched.h>
- #define PRINT_PREF KERN_INFO "mtd_oobtest: "
- static int dev;
- module_param(dev, int, S_IRUGO);
- MODULE_PARM_DESC(dev, "MTD device number to use");
- static struct mtd_info *mtd;
- static unsigned char *readbuf;
- static unsigned char *writebuf;
- static unsigned char *bbt;
- static int ebcnt;
- static int pgcnt;
- static int errcnt;
- static int use_offset;
- static int use_len;
- static int use_len_max;
- static int vary_offset;
- static unsigned long next = 1;
- static inline unsigned int simple_rand(void)
- {
- next = next * 1103515245 + 12345;
- return (unsigned int)((next / 65536) % 32768);
- }
- static inline void simple_srand(unsigned long seed)
- {
- next = seed;
- }
- static void set_random_data(unsigned char *buf, size_t len)
- {
- size_t i;
- for (i = 0; i < len; ++i)
- buf[i] = simple_rand();
- }
- static int erase_eraseblock(int ebnum)
- {
- int err;
- struct erase_info ei;
- loff_t addr = ebnum * mtd->erasesize;
- memset(&ei, 0, sizeof(struct erase_info));
- ei.mtd = mtd;
- ei.addr = addr;
- ei.len = mtd->erasesize;
- err = mtd->erase(mtd, &ei);
- if (err) {
- printk(PRINT_PREF "error %d while erasing EB %d\n", err, ebnum);
- return err;
- }
- if (ei.state == MTD_ERASE_FAILED) {
- printk(PRINT_PREF "some erase error occurred at EB %d\n",
- ebnum);
- return -EIO;
- }
- return 0;
- }
- static int erase_whole_device(void)
- {
- int err;
- unsigned int i;
- printk(PRINT_PREF "erasing whole device\n");
- for (i = 0; i < ebcnt; ++i) {
- if (bbt[i])
- continue;
- err = erase_eraseblock(i);
- if (err)
- return err;
- cond_resched();
- }
- printk(PRINT_PREF "erased %u eraseblocks\n", i);
- return 0;
- }
- static void do_vary_offset(void)
- {
- use_len -= 1;
- if (use_len < 1) {
- use_offset += 1;
- if (use_offset >= use_len_max)
- use_offset = 0;
- use_len = use_len_max - use_offset;
- }
- }
- static int write_eraseblock(int ebnum)
- {
- int i;
- struct mtd_oob_ops ops;
- int err = 0;
- loff_t addr = ebnum * mtd->erasesize;
- for (i = 0; i < pgcnt; ++i, addr += mtd->writesize) {
- set_random_data(writebuf, use_len);
- ops.mode = MTD_OOB_AUTO;
- ops.len = 0;
- ops.retlen = 0;
- ops.ooblen = use_len;
- ops.oobretlen = 0;
- ops.ooboffs = use_offset;
- ops.datbuf = NULL;
- ops.oobbuf = writebuf;
- err = mtd->write_oob(mtd, addr, &ops);
- if (err || ops.oobretlen != use_len) {
- printk(PRINT_PREF "error: writeoob failed at %#llx\n",
- (long long)addr);
- printk(PRINT_PREF "error: use_len %d, use_offset %d\n",
- use_len, use_offset);
- errcnt += 1;
- return err ? err : -1;
- }
- if (vary_offset)
- do_vary_offset();
- }
- return err;
- }
- static int write_whole_device(void)
- {
- int err;
- unsigned int i;
- printk(PRINT_PREF "writing OOBs of whole device\n");
- for (i = 0; i < ebcnt; ++i) {
- if (bbt[i])
- continue;
- err = write_eraseblock(i);
- if (err)
- return err;
- if (i % 256 == 0)
- printk(PRINT_PREF "written up to eraseblock %u\n", i);
- cond_resched();
- }
- printk(PRINT_PREF "written %u eraseblocks\n", i);
- return 0;
- }
- static int verify_eraseblock(int ebnum)
- {
- int i;
- struct mtd_oob_ops ops;
- int err = 0;
- loff_t addr = ebnum * mtd->erasesize;
- for (i = 0; i < pgcnt; ++i, addr += mtd->writesize) {
- set_random_data(writebuf, use_len);
- ops.mode = MTD_OOB_AUTO;
- ops.len = 0;
- ops.retlen = 0;
- ops.ooblen = use_len;
- ops.oobretlen = 0;
- ops.ooboffs = use_offset;
- ops.datbuf = NULL;
- ops.oobbuf = readbuf;
- err = mtd->read_oob(mtd, addr, &ops);
- if (err || ops.oobretlen != use_len) {
- printk(PRINT_PREF "error: readoob failed at %#llx\n",
- (long long)addr);
- errcnt += 1;
- return err ? err : -1;
- }
- if (memcmp(readbuf, writebuf, use_len)) {
- printk(PRINT_PREF "error: verify failed at %#llx\n",
- (long long)addr);
- errcnt += 1;
- if (errcnt > 1000) {
- printk(PRINT_PREF "error: too many errors\n");
- return -1;
- }
- }
- if (use_offset != 0 || use_len < mtd->ecclayout->oobavail) {
- int k;
- ops.mode = MTD_OOB_AUTO;
- ops.len = 0;
- ops.retlen = 0;
- ops.ooblen = mtd->ecclayout->oobavail;
- ops.oobretlen = 0;
- ops.ooboffs = 0;
- ops.datbuf = NULL;
- ops.oobbuf = readbuf;
- err = mtd->read_oob(mtd, addr, &ops);
- if (err || ops.oobretlen != mtd->ecclayout->oobavail) {
- printk(PRINT_PREF "error: readoob failed at "
- "%#llx\n", (long long)addr);
- errcnt += 1;
- return err ? err : -1;
- }
- if (memcmp(readbuf + use_offset, writebuf, use_len)) {
- printk(PRINT_PREF "error: verify failed at "
- "%#llx\n", (long long)addr);
- errcnt += 1;
- if (errcnt > 1000) {
- printk(PRINT_PREF "error: too many "
- "errors\n");
- return -1;
- }
- }
- for (k = 0; k < use_offset; ++k)
- if (readbuf[k] != 0xff) {
- printk(PRINT_PREF "error: verify 0xff "
- "failed at %#llx\n",
- (long long)addr);
- errcnt += 1;
- if (errcnt > 1000) {
- printk(PRINT_PREF "error: too "
- "many errors\n");
-