Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 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 | * Test OOB read and write on MTD device. |
| 18 | * |
| 19 | * Author: Adrian Hunter <ext-adrian.hunter@nokia.com> |
| 20 | */ |
| 21 | |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 22 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 23 | |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 24 | #include <asm/div64.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/moduleparam.h> |
| 28 | #include <linux/err.h> |
| 29 | #include <linux/mtd/mtd.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 30 | #include <linux/slab.h> |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 31 | #include <linux/sched.h> |
Akinobu Mita | 8dad049 | 2013-02-27 17:05:33 -0800 | [diff] [blame] | 32 | #include <linux/random.h> |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 33 | |
Akinobu Mita | 4bf527a | 2013-08-03 18:52:09 +0900 | [diff] [blame] | 34 | #include "mtd_test.h" |
| 35 | |
Wolfram Sang | 7406060 | 2011-10-30 00:11:53 +0200 | [diff] [blame] | 36 | static int dev = -EINVAL; |
Roger Quadros | afc0ea1 | 2014-10-21 16:53:28 +0300 | [diff] [blame] | 37 | static int bitflip_limit; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 38 | module_param(dev, int, S_IRUGO); |
| 39 | MODULE_PARM_DESC(dev, "MTD device number to use"); |
Roger Quadros | afc0ea1 | 2014-10-21 16:53:28 +0300 | [diff] [blame] | 40 | module_param(bitflip_limit, int, S_IRUGO); |
| 41 | MODULE_PARM_DESC(bitflip_limit, "Max. allowed bitflips per page"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 42 | |
| 43 | static struct mtd_info *mtd; |
| 44 | static unsigned char *readbuf; |
| 45 | static unsigned char *writebuf; |
| 46 | static unsigned char *bbt; |
| 47 | |
| 48 | static int ebcnt; |
| 49 | static int pgcnt; |
| 50 | static int errcnt; |
| 51 | static int use_offset; |
| 52 | static int use_len; |
| 53 | static int use_len_max; |
| 54 | static int vary_offset; |
Akinobu Mita | 8dad049 | 2013-02-27 17:05:33 -0800 | [diff] [blame] | 55 | static struct rnd_state rnd_state; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 56 | |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 57 | static void do_vary_offset(void) |
| 58 | { |
| 59 | use_len -= 1; |
| 60 | if (use_len < 1) { |
| 61 | use_offset += 1; |
| 62 | if (use_offset >= use_len_max) |
| 63 | use_offset = 0; |
| 64 | use_len = use_len_max - use_offset; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | static int write_eraseblock(int ebnum) |
| 69 | { |
| 70 | int i; |
| 71 | struct mtd_oob_ops ops; |
| 72 | int err = 0; |
| 73 | loff_t addr = ebnum * mtd->erasesize; |
| 74 | |
Akinobu Mita | be54f8f | 2014-03-08 00:24:10 +0900 | [diff] [blame] | 75 | prandom_bytes_state(&rnd_state, writebuf, use_len_max * pgcnt); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 76 | for (i = 0; i < pgcnt; ++i, addr += mtd->writesize) { |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 77 | ops.mode = MTD_OPS_AUTO_OOB; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 78 | ops.len = 0; |
| 79 | ops.retlen = 0; |
| 80 | ops.ooblen = use_len; |
| 81 | ops.oobretlen = 0; |
| 82 | ops.ooboffs = use_offset; |
Hannes Eder | 23d4249 | 2009-03-05 20:15:01 +0100 | [diff] [blame] | 83 | ops.datbuf = NULL; |
Akinobu Mita | be54f8f | 2014-03-08 00:24:10 +0900 | [diff] [blame] | 84 | ops.oobbuf = writebuf + (use_len_max * i) + use_offset; |
Artem Bityutskiy | a2cc5ba | 2011-12-23 18:29:55 +0200 | [diff] [blame] | 85 | err = mtd_write_oob(mtd, addr, &ops); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 86 | if (err || ops.oobretlen != use_len) { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 87 | pr_err("error: writeoob failed at %#llx\n", |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 88 | (long long)addr); |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 89 | pr_err("error: use_len %d, use_offset %d\n", |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 90 | use_len, use_offset); |
| 91 | errcnt += 1; |
| 92 | return err ? err : -1; |
| 93 | } |
| 94 | if (vary_offset) |
| 95 | do_vary_offset(); |
| 96 | } |
| 97 | |
| 98 | return err; |
| 99 | } |
| 100 | |
| 101 | static int write_whole_device(void) |
| 102 | { |
| 103 | int err; |
| 104 | unsigned int i; |
| 105 | |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 106 | pr_info("writing OOBs of whole device\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 107 | for (i = 0; i < ebcnt; ++i) { |
| 108 | if (bbt[i]) |
| 109 | continue; |
| 110 | err = write_eraseblock(i); |
| 111 | if (err) |
| 112 | return err; |
| 113 | if (i % 256 == 0) |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 114 | pr_info("written up to eraseblock %u\n", i); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 115 | cond_resched(); |
| 116 | } |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 117 | pr_info("written %u eraseblocks\n", i); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 118 | return 0; |
| 119 | } |
| 120 | |
Roger Quadros | afc0ea1 | 2014-10-21 16:53:28 +0300 | [diff] [blame] | 121 | /* |
| 122 | * Display the address, offset and data bytes at comparison failure. |
| 123 | * Return number of bitflips encountered. |
| 124 | */ |
| 125 | static size_t memcmpshow(loff_t addr, const void *cs, const void *ct, size_t count) |
Roger Quadros | 5a66088 | 2014-10-21 16:53:27 +0300 | [diff] [blame] | 126 | { |
| 127 | const unsigned char *su1, *su2; |
| 128 | int res; |
Roger Quadros | 5a66088 | 2014-10-21 16:53:27 +0300 | [diff] [blame] | 129 | size_t i = 0; |
Roger Quadros | afc0ea1 | 2014-10-21 16:53:28 +0300 | [diff] [blame] | 130 | size_t bitflips = 0; |
Roger Quadros | 5a66088 | 2014-10-21 16:53:27 +0300 | [diff] [blame] | 131 | |
| 132 | for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--, i++) { |
| 133 | res = *su1 ^ *su2; |
| 134 | if (res) { |
Brian Norris | 806b6ef | 2014-11-20 01:26:51 -0800 | [diff] [blame] | 135 | pr_info("error @addr[0x%lx:0x%zx] 0x%x -> 0x%x diff 0x%x\n", |
Roger Quadros | 5a66088 | 2014-10-21 16:53:27 +0300 | [diff] [blame] | 136 | (unsigned long)addr, i, *su1, *su2, res); |
Roger Quadros | afc0ea1 | 2014-10-21 16:53:28 +0300 | [diff] [blame] | 137 | bitflips += hweight8(res); |
Roger Quadros | 5a66088 | 2014-10-21 16:53:27 +0300 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
Roger Quadros | afc0ea1 | 2014-10-21 16:53:28 +0300 | [diff] [blame] | 141 | return bitflips; |
Roger Quadros | 5a66088 | 2014-10-21 16:53:27 +0300 | [diff] [blame] | 142 | } |
| 143 | |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 144 | static int verify_eraseblock(int ebnum) |
| 145 | { |
| 146 | int i; |
| 147 | struct mtd_oob_ops ops; |
| 148 | int err = 0; |
Brian Norris | 1001ff7 | 2014-07-21 19:07:12 -0700 | [diff] [blame] | 149 | loff_t addr = (loff_t)ebnum * mtd->erasesize; |
Roger Quadros | afc0ea1 | 2014-10-21 16:53:28 +0300 | [diff] [blame] | 150 | size_t bitflips; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 151 | |
Akinobu Mita | be54f8f | 2014-03-08 00:24:10 +0900 | [diff] [blame] | 152 | prandom_bytes_state(&rnd_state, writebuf, use_len_max * pgcnt); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 153 | for (i = 0; i < pgcnt; ++i, addr += mtd->writesize) { |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 154 | ops.mode = MTD_OPS_AUTO_OOB; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 155 | ops.len = 0; |
| 156 | ops.retlen = 0; |
| 157 | ops.ooblen = use_len; |
| 158 | ops.oobretlen = 0; |
| 159 | ops.ooboffs = use_offset; |
Hannes Eder | 23d4249 | 2009-03-05 20:15:01 +0100 | [diff] [blame] | 160 | ops.datbuf = NULL; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 161 | ops.oobbuf = readbuf; |
Artem Bityutskiy | fd2819b | 2011-12-23 18:27:05 +0200 | [diff] [blame] | 162 | err = mtd_read_oob(mtd, addr, &ops); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 163 | if (err || ops.oobretlen != use_len) { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 164 | pr_err("error: readoob failed at %#llx\n", |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 165 | (long long)addr); |
| 166 | errcnt += 1; |
| 167 | return err ? err : -1; |
| 168 | } |
Roger Quadros | afc0ea1 | 2014-10-21 16:53:28 +0300 | [diff] [blame] | 169 | |
| 170 | bitflips = memcmpshow(addr, readbuf, |
| 171 | writebuf + (use_len_max * i) + use_offset, |
| 172 | use_len); |
| 173 | if (bitflips > bitflip_limit) { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 174 | pr_err("error: verify failed at %#llx\n", |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 175 | (long long)addr); |
| 176 | errcnt += 1; |
| 177 | if (errcnt > 1000) { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 178 | pr_err("error: too many errors\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 179 | return -1; |
| 180 | } |
Roger Quadros | afc0ea1 | 2014-10-21 16:53:28 +0300 | [diff] [blame] | 181 | } else if (bitflips) { |
| 182 | pr_info("ignoring error as within bitflip_limit\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 183 | } |
Roger Quadros | afc0ea1 | 2014-10-21 16:53:28 +0300 | [diff] [blame] | 184 | |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 185 | if (use_offset != 0 || use_len < mtd->ecclayout->oobavail) { |
| 186 | int k; |
| 187 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 188 | ops.mode = MTD_OPS_AUTO_OOB; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 189 | ops.len = 0; |
| 190 | ops.retlen = 0; |
| 191 | ops.ooblen = mtd->ecclayout->oobavail; |
| 192 | ops.oobretlen = 0; |
| 193 | ops.ooboffs = 0; |
Hannes Eder | 23d4249 | 2009-03-05 20:15:01 +0100 | [diff] [blame] | 194 | ops.datbuf = NULL; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 195 | ops.oobbuf = readbuf; |
Artem Bityutskiy | fd2819b | 2011-12-23 18:27:05 +0200 | [diff] [blame] | 196 | err = mtd_read_oob(mtd, addr, &ops); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 197 | if (err || ops.oobretlen != mtd->ecclayout->oobavail) { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 198 | pr_err("error: readoob failed at %#llx\n", |
| 199 | (long long)addr); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 200 | errcnt += 1; |
| 201 | return err ? err : -1; |
| 202 | } |
Roger Quadros | afc0ea1 | 2014-10-21 16:53:28 +0300 | [diff] [blame] | 203 | bitflips = memcmpshow(addr, readbuf + use_offset, |
| 204 | writebuf + (use_len_max * i) + use_offset, |
| 205 | use_len); |
| 206 | if (bitflips > bitflip_limit) { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 207 | pr_err("error: verify failed at %#llx\n", |
| 208 | (long long)addr); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 209 | errcnt += 1; |
| 210 | if (errcnt > 1000) { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 211 | pr_err("error: too many errors\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 212 | return -1; |
| 213 | } |
Roger Quadros | afc0ea1 | 2014-10-21 16:53:28 +0300 | [diff] [blame] | 214 | } else if (bitflips) { |
| 215 | pr_info("ignoring error as within bitflip_limit\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 216 | } |
Roger Quadros | afc0ea1 | 2014-10-21 16:53:28 +0300 | [diff] [blame] | 217 | |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 218 | for (k = 0; k < use_offset; ++k) |
| 219 | if (readbuf[k] != 0xff) { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 220 | pr_err("error: verify 0xff " |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 221 | "failed at %#llx\n", |
| 222 | (long long)addr); |
| 223 | errcnt += 1; |
| 224 | if (errcnt > 1000) { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 225 | pr_err("error: too " |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 226 | "many errors\n"); |
| 227 | return -1; |
| 228 | } |
| 229 | } |
| 230 | for (k = use_offset + use_len; |
| 231 | k < mtd->ecclayout->oobavail; ++k) |
| 232 | if (readbuf[k] != 0xff) { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 233 | pr_err("error: verify 0xff " |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 234 | "failed at %#llx\n", |
| 235 | (long long)addr); |
| 236 | errcnt += 1; |
| 237 | if (errcnt > 1000) { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 238 | pr_err("error: too " |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 239 | "many errors\n"); |
| 240 | return -1; |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | if (vary_offset) |
| 245 | do_vary_offset(); |
| 246 | } |
| 247 | return err; |
| 248 | } |
| 249 | |
| 250 | static int verify_eraseblock_in_one_go(int ebnum) |
| 251 | { |
| 252 | struct mtd_oob_ops ops; |
| 253 | int err = 0; |
Brian Norris | 1001ff7 | 2014-07-21 19:07:12 -0700 | [diff] [blame] | 254 | loff_t addr = (loff_t)ebnum * mtd->erasesize; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 255 | size_t len = mtd->ecclayout->oobavail * pgcnt; |
Roger Quadros | afc0ea1 | 2014-10-21 16:53:28 +0300 | [diff] [blame] | 256 | size_t oobavail = mtd->ecclayout->oobavail; |
| 257 | size_t bitflips; |
| 258 | int i; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 259 | |
Akinobu Mita | 8dad049 | 2013-02-27 17:05:33 -0800 | [diff] [blame] | 260 | prandom_bytes_state(&rnd_state, writebuf, len); |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 261 | ops.mode = MTD_OPS_AUTO_OOB; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 262 | ops.len = 0; |
| 263 | ops.retlen = 0; |
| 264 | ops.ooblen = len; |
| 265 | ops.oobretlen = 0; |
| 266 | ops.ooboffs = 0; |
Hannes Eder | 23d4249 | 2009-03-05 20:15:01 +0100 | [diff] [blame] | 267 | ops.datbuf = NULL; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 268 | ops.oobbuf = readbuf; |
Roger Quadros | afc0ea1 | 2014-10-21 16:53:28 +0300 | [diff] [blame] | 269 | |
| 270 | /* read entire block's OOB at one go */ |
Artem Bityutskiy | fd2819b | 2011-12-23 18:27:05 +0200 | [diff] [blame] | 271 | err = mtd_read_oob(mtd, addr, &ops); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 272 | if (err || ops.oobretlen != len) { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 273 | pr_err("error: readoob failed at %#llx\n", |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 274 | (long long)addr); |
| 275 | errcnt += 1; |
| 276 | return err ? err : -1; |
| 277 | } |
Roger Quadros | afc0ea1 | 2014-10-21 16:53:28 +0300 | [diff] [blame] | 278 | |
| 279 | /* verify one page OOB at a time for bitflip per page limit check */ |
| 280 | for (i = 0; i < pgcnt; ++i, addr += mtd->writesize) { |
| 281 | bitflips = memcmpshow(addr, readbuf + (i * oobavail), |
| 282 | writebuf + (i * oobavail), oobavail); |
| 283 | if (bitflips > bitflip_limit) { |
| 284 | pr_err("error: verify failed at %#llx\n", |
| 285 | (long long)addr); |
| 286 | errcnt += 1; |
| 287 | if (errcnt > 1000) { |
| 288 | pr_err("error: too many errors\n"); |
| 289 | return -1; |
| 290 | } |
| 291 | } else if (bitflips) { |
| 292 | pr_info("ignoring error as within bitflip_limit\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
| 296 | return err; |
| 297 | } |
| 298 | |
| 299 | static int verify_all_eraseblocks(void) |
| 300 | { |
| 301 | int err; |
| 302 | unsigned int i; |
| 303 | |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 304 | pr_info("verifying all eraseblocks\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 305 | for (i = 0; i < ebcnt; ++i) { |
| 306 | if (bbt[i]) |
| 307 | continue; |
| 308 | err = verify_eraseblock(i); |
| 309 | if (err) |
| 310 | return err; |
| 311 | if (i % 256 == 0) |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 312 | pr_info("verified up to eraseblock %u\n", i); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 313 | cond_resched(); |
| 314 | } |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 315 | pr_info("verified %u eraseblocks\n", i); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 316 | return 0; |
| 317 | } |
| 318 | |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 319 | static int __init mtd_oobtest_init(void) |
| 320 | { |
| 321 | int err = 0; |
| 322 | unsigned int i; |
| 323 | uint64_t tmp; |
| 324 | struct mtd_oob_ops ops; |
| 325 | loff_t addr = 0, addr0; |
| 326 | |
| 327 | printk(KERN_INFO "\n"); |
| 328 | printk(KERN_INFO "=================================================\n"); |
Wolfram Sang | 7406060 | 2011-10-30 00:11:53 +0200 | [diff] [blame] | 329 | |
| 330 | if (dev < 0) { |
Masanari Iida | 064a769 | 2012-11-09 23:20:58 +0900 | [diff] [blame] | 331 | pr_info("Please specify a valid mtd-device via module parameter\n"); |
| 332 | pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n"); |
Wolfram Sang | 7406060 | 2011-10-30 00:11:53 +0200 | [diff] [blame] | 333 | return -EINVAL; |
| 334 | } |
| 335 | |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 336 | pr_info("MTD device: %d\n", dev); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 337 | |
| 338 | mtd = get_mtd_device(NULL, dev); |
| 339 | if (IS_ERR(mtd)) { |
| 340 | err = PTR_ERR(mtd); |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 341 | pr_err("error: cannot get MTD device\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 342 | return err; |
| 343 | } |
| 344 | |
Huang Shijie | 818b973 | 2013-09-25 14:58:17 +0800 | [diff] [blame] | 345 | if (!mtd_type_is_nand(mtd)) { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 346 | pr_info("this test requires NAND flash\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 347 | goto out; |
| 348 | } |
| 349 | |
| 350 | tmp = mtd->size; |
| 351 | do_div(tmp, mtd->erasesize); |
| 352 | ebcnt = tmp; |
| 353 | pgcnt = mtd->erasesize / mtd->writesize; |
| 354 | |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 355 | pr_info("MTD device size %llu, eraseblock size %u, " |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 356 | "page size %u, count of eraseblocks %u, pages per " |
| 357 | "eraseblock %u, OOB size %u\n", |
| 358 | (unsigned long long)mtd->size, mtd->erasesize, |
| 359 | mtd->writesize, ebcnt, pgcnt, mtd->oobsize); |
| 360 | |
| 361 | err = -ENOMEM; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 362 | readbuf = kmalloc(mtd->erasesize, GFP_KERNEL); |
Brian Norris | 33777e6 | 2013-05-02 14:18:51 -0700 | [diff] [blame] | 363 | if (!readbuf) |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 364 | goto out; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 365 | writebuf = kmalloc(mtd->erasesize, GFP_KERNEL); |
Brian Norris | 33777e6 | 2013-05-02 14:18:51 -0700 | [diff] [blame] | 366 | if (!writebuf) |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 367 | goto out; |
Akinobu Mita | 4bf527a | 2013-08-03 18:52:09 +0900 | [diff] [blame] | 368 | bbt = kzalloc(ebcnt, GFP_KERNEL); |
| 369 | if (!bbt) |
| 370 | goto out; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 371 | |
Akinobu Mita | 4bf527a | 2013-08-03 18:52:09 +0900 | [diff] [blame] | 372 | err = mtdtest_scan_for_bad_eraseblocks(mtd, bbt, 0, ebcnt); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 373 | if (err) |
| 374 | goto out; |
| 375 | |
| 376 | use_offset = 0; |
| 377 | use_len = mtd->ecclayout->oobavail; |
| 378 | use_len_max = mtd->ecclayout->oobavail; |
| 379 | vary_offset = 0; |
| 380 | |
| 381 | /* First test: write all OOB, read it back and verify */ |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 382 | pr_info("test 1 of 5\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 383 | |
Akinobu Mita | 4bf527a | 2013-08-03 18:52:09 +0900 | [diff] [blame] | 384 | err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 385 | if (err) |
| 386 | goto out; |
| 387 | |
Akinobu Mita | 8dad049 | 2013-02-27 17:05:33 -0800 | [diff] [blame] | 388 | prandom_seed_state(&rnd_state, 1); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 389 | err = write_whole_device(); |
| 390 | if (err) |
| 391 | goto out; |
| 392 | |
Akinobu Mita | 8dad049 | 2013-02-27 17:05:33 -0800 | [diff] [blame] | 393 | prandom_seed_state(&rnd_state, 1); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 394 | err = verify_all_eraseblocks(); |
| 395 | if (err) |
| 396 | goto out; |
| 397 | |
| 398 | /* |
| 399 | * Second test: write all OOB, a block at a time, read it back and |
| 400 | * verify. |
| 401 | */ |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 402 | pr_info("test 2 of 5\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 403 | |
Akinobu Mita | 4bf527a | 2013-08-03 18:52:09 +0900 | [diff] [blame] | 404 | err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 405 | if (err) |
| 406 | goto out; |
| 407 | |
Akinobu Mita | 8dad049 | 2013-02-27 17:05:33 -0800 | [diff] [blame] | 408 | prandom_seed_state(&rnd_state, 3); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 409 | err = write_whole_device(); |
| 410 | if (err) |
| 411 | goto out; |
| 412 | |
| 413 | /* Check all eraseblocks */ |
Akinobu Mita | 8dad049 | 2013-02-27 17:05:33 -0800 | [diff] [blame] | 414 | prandom_seed_state(&rnd_state, 3); |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 415 | pr_info("verifying all eraseblocks\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 416 | for (i = 0; i < ebcnt; ++i) { |
| 417 | if (bbt[i]) |
| 418 | continue; |
| 419 | err = verify_eraseblock_in_one_go(i); |
| 420 | if (err) |
| 421 | goto out; |
| 422 | if (i % 256 == 0) |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 423 | pr_info("verified up to eraseblock %u\n", i); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 424 | cond_resched(); |
| 425 | } |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 426 | pr_info("verified %u eraseblocks\n", i); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 427 | |
| 428 | /* |
| 429 | * Third test: write OOB at varying offsets and lengths, read it back |
| 430 | * and verify. |
| 431 | */ |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 432 | pr_info("test 3 of 5\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 433 | |
Akinobu Mita | 4bf527a | 2013-08-03 18:52:09 +0900 | [diff] [blame] | 434 | err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 435 | if (err) |
| 436 | goto out; |
| 437 | |
| 438 | /* Write all eraseblocks */ |
| 439 | use_offset = 0; |
| 440 | use_len = mtd->ecclayout->oobavail; |
| 441 | use_len_max = mtd->ecclayout->oobavail; |
| 442 | vary_offset = 1; |
Akinobu Mita | 8dad049 | 2013-02-27 17:05:33 -0800 | [diff] [blame] | 443 | prandom_seed_state(&rnd_state, 5); |
Akinobu Mita | f54d633 | 2009-10-09 18:43:52 +0900 | [diff] [blame] | 444 | |
| 445 | err = write_whole_device(); |
| 446 | if (err) |
| 447 | goto out; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 448 | |
| 449 | /* Check all eraseblocks */ |
| 450 | use_offset = 0; |
| 451 | use_len = mtd->ecclayout->oobavail; |
| 452 | use_len_max = mtd->ecclayout->oobavail; |
| 453 | vary_offset = 1; |
Akinobu Mita | 8dad049 | 2013-02-27 17:05:33 -0800 | [diff] [blame] | 454 | prandom_seed_state(&rnd_state, 5); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 455 | err = verify_all_eraseblocks(); |
| 456 | if (err) |
| 457 | goto out; |
| 458 | |
| 459 | use_offset = 0; |
| 460 | use_len = mtd->ecclayout->oobavail; |
| 461 | use_len_max = mtd->ecclayout->oobavail; |
| 462 | vary_offset = 0; |
| 463 | |
| 464 | /* Fourth test: try to write off end of device */ |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 465 | pr_info("test 4 of 5\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 466 | |
Akinobu Mita | 4bf527a | 2013-08-03 18:52:09 +0900 | [diff] [blame] | 467 | err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 468 | if (err) |
| 469 | goto out; |
| 470 | |
| 471 | addr0 = 0; |
Roel Kluin | c6f7e7b | 2009-07-31 16:21:01 +0200 | [diff] [blame] | 472 | for (i = 0; i < ebcnt && bbt[i]; ++i) |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 473 | addr0 += mtd->erasesize; |
| 474 | |
| 475 | /* Attempt to write off end of OOB */ |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 476 | ops.mode = MTD_OPS_AUTO_OOB; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 477 | ops.len = 0; |
| 478 | ops.retlen = 0; |
| 479 | ops.ooblen = 1; |
| 480 | ops.oobretlen = 0; |
| 481 | ops.ooboffs = mtd->ecclayout->oobavail; |
Hannes Eder | 23d4249 | 2009-03-05 20:15:01 +0100 | [diff] [blame] | 482 | ops.datbuf = NULL; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 483 | ops.oobbuf = writebuf; |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 484 | pr_info("attempting to start write past end of OOB\n"); |
| 485 | pr_info("an error is expected...\n"); |
Artem Bityutskiy | a2cc5ba | 2011-12-23 18:29:55 +0200 | [diff] [blame] | 486 | err = mtd_write_oob(mtd, addr0, &ops); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 487 | if (err) { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 488 | pr_info("error occurred as expected\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 489 | err = 0; |
| 490 | } else { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 491 | pr_err("error: can write past end of OOB\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 492 | errcnt += 1; |
| 493 | } |
| 494 | |
| 495 | /* Attempt to read off end of OOB */ |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 496 | ops.mode = MTD_OPS_AUTO_OOB; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 497 | ops.len = 0; |
| 498 | ops.retlen = 0; |
| 499 | ops.ooblen = 1; |
| 500 | ops.oobretlen = 0; |
| 501 | ops.ooboffs = mtd->ecclayout->oobavail; |
Hannes Eder | 23d4249 | 2009-03-05 20:15:01 +0100 | [diff] [blame] | 502 | ops.datbuf = NULL; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 503 | ops.oobbuf = readbuf; |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 504 | pr_info("attempting to start read past end of OOB\n"); |
| 505 | pr_info("an error is expected...\n"); |
Artem Bityutskiy | fd2819b | 2011-12-23 18:27:05 +0200 | [diff] [blame] | 506 | err = mtd_read_oob(mtd, addr0, &ops); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 507 | if (err) { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 508 | pr_info("error occurred as expected\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 509 | err = 0; |
| 510 | } else { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 511 | pr_err("error: can read past end of OOB\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 512 | errcnt += 1; |
| 513 | } |
| 514 | |
| 515 | if (bbt[ebcnt - 1]) |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 516 | pr_info("skipping end of device tests because last " |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 517 | "block is bad\n"); |
| 518 | else { |
| 519 | /* Attempt to write off end of device */ |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 520 | ops.mode = MTD_OPS_AUTO_OOB; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 521 | ops.len = 0; |
| 522 | ops.retlen = 0; |
| 523 | ops.ooblen = mtd->ecclayout->oobavail + 1; |
| 524 | ops.oobretlen = 0; |
| 525 | ops.ooboffs = 0; |
Hannes Eder | 23d4249 | 2009-03-05 20:15:01 +0100 | [diff] [blame] | 526 | ops.datbuf = NULL; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 527 | ops.oobbuf = writebuf; |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 528 | pr_info("attempting to write past end of device\n"); |
| 529 | pr_info("an error is expected...\n"); |
Artem Bityutskiy | a2cc5ba | 2011-12-23 18:29:55 +0200 | [diff] [blame] | 530 | err = mtd_write_oob(mtd, mtd->size - mtd->writesize, &ops); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 531 | if (err) { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 532 | pr_info("error occurred as expected\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 533 | err = 0; |
| 534 | } else { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 535 | pr_err("error: wrote past end of device\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 536 | errcnt += 1; |
| 537 | } |
| 538 | |
| 539 | /* Attempt to read off end of device */ |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 540 | ops.mode = MTD_OPS_AUTO_OOB; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 541 | ops.len = 0; |
| 542 | ops.retlen = 0; |
| 543 | ops.ooblen = mtd->ecclayout->oobavail + 1; |
| 544 | ops.oobretlen = 0; |
| 545 | ops.ooboffs = 0; |
Hannes Eder | 23d4249 | 2009-03-05 20:15:01 +0100 | [diff] [blame] | 546 | ops.datbuf = NULL; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 547 | ops.oobbuf = readbuf; |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 548 | pr_info("attempting to read past end of device\n"); |
| 549 | pr_info("an error is expected...\n"); |
Artem Bityutskiy | fd2819b | 2011-12-23 18:27:05 +0200 | [diff] [blame] | 550 | err = mtd_read_oob(mtd, mtd->size - mtd->writesize, &ops); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 551 | if (err) { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 552 | pr_info("error occurred as expected\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 553 | err = 0; |
| 554 | } else { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 555 | pr_err("error: read past end of device\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 556 | errcnt += 1; |
| 557 | } |
| 558 | |
Akinobu Mita | 4bf527a | 2013-08-03 18:52:09 +0900 | [diff] [blame] | 559 | err = mtdtest_erase_eraseblock(mtd, ebcnt - 1); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 560 | if (err) |
| 561 | goto out; |
| 562 | |
| 563 | /* Attempt to write off end of device */ |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 564 | ops.mode = MTD_OPS_AUTO_OOB; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 565 | ops.len = 0; |
| 566 | ops.retlen = 0; |
| 567 | ops.ooblen = mtd->ecclayout->oobavail; |
| 568 | ops.oobretlen = 0; |
| 569 | ops.ooboffs = 1; |
Hannes Eder | 23d4249 | 2009-03-05 20:15:01 +0100 | [diff] [blame] | 570 | ops.datbuf = NULL; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 571 | ops.oobbuf = writebuf; |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 572 | pr_info("attempting to write past end of device\n"); |
| 573 | pr_info("an error is expected...\n"); |
Artem Bityutskiy | a2cc5ba | 2011-12-23 18:29:55 +0200 | [diff] [blame] | 574 | err = mtd_write_oob(mtd, mtd->size - mtd->writesize, &ops); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 575 | if (err) { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 576 | pr_info("error occurred as expected\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 577 | err = 0; |
| 578 | } else { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 579 | pr_err("error: wrote past end of device\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 580 | errcnt += 1; |
| 581 | } |
| 582 | |
| 583 | /* Attempt to read off end of device */ |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 584 | ops.mode = MTD_OPS_AUTO_OOB; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 585 | ops.len = 0; |
| 586 | ops.retlen = 0; |
| 587 | ops.ooblen = mtd->ecclayout->oobavail; |
| 588 | ops.oobretlen = 0; |
| 589 | ops.ooboffs = 1; |
Hannes Eder | 23d4249 | 2009-03-05 20:15:01 +0100 | [diff] [blame] | 590 | ops.datbuf = NULL; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 591 | ops.oobbuf = readbuf; |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 592 | pr_info("attempting to read past end of device\n"); |
| 593 | pr_info("an error is expected...\n"); |
Artem Bityutskiy | fd2819b | 2011-12-23 18:27:05 +0200 | [diff] [blame] | 594 | err = mtd_read_oob(mtd, mtd->size - mtd->writesize, &ops); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 595 | if (err) { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 596 | pr_info("error occurred as expected\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 597 | err = 0; |
| 598 | } else { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 599 | pr_err("error: read past end of device\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 600 | errcnt += 1; |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | /* Fifth test: write / read across block boundaries */ |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 605 | pr_info("test 5 of 5\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 606 | |
| 607 | /* Erase all eraseblocks */ |
Akinobu Mita | 4bf527a | 2013-08-03 18:52:09 +0900 | [diff] [blame] | 608 | err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 609 | if (err) |
| 610 | goto out; |
| 611 | |
| 612 | /* Write all eraseblocks */ |
Akinobu Mita | 8dad049 | 2013-02-27 17:05:33 -0800 | [diff] [blame] | 613 | prandom_seed_state(&rnd_state, 11); |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 614 | pr_info("writing OOBs of whole device\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 615 | for (i = 0; i < ebcnt - 1; ++i) { |
| 616 | int cnt = 2; |
| 617 | int pg; |
| 618 | size_t sz = mtd->ecclayout->oobavail; |
| 619 | if (bbt[i] || bbt[i + 1]) |
| 620 | continue; |
Brian Norris | 1001ff7 | 2014-07-21 19:07:12 -0700 | [diff] [blame] | 621 | addr = (loff_t)(i + 1) * mtd->erasesize - mtd->writesize; |
Akinobu Mita | be54f8f | 2014-03-08 00:24:10 +0900 | [diff] [blame] | 622 | prandom_bytes_state(&rnd_state, writebuf, sz * cnt); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 623 | for (pg = 0; pg < cnt; ++pg) { |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 624 | ops.mode = MTD_OPS_AUTO_OOB; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 625 | ops.len = 0; |
| 626 | ops.retlen = 0; |
| 627 | ops.ooblen = sz; |
| 628 | ops.oobretlen = 0; |
| 629 | ops.ooboffs = 0; |
Hannes Eder | 23d4249 | 2009-03-05 20:15:01 +0100 | [diff] [blame] | 630 | ops.datbuf = NULL; |
Akinobu Mita | be54f8f | 2014-03-08 00:24:10 +0900 | [diff] [blame] | 631 | ops.oobbuf = writebuf + pg * sz; |
Artem Bityutskiy | a2cc5ba | 2011-12-23 18:29:55 +0200 | [diff] [blame] | 632 | err = mtd_write_oob(mtd, addr, &ops); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 633 | if (err) |
| 634 | goto out; |
| 635 | if (i % 256 == 0) |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 636 | pr_info("written up to eraseblock %u\n", i); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 637 | cond_resched(); |
| 638 | addr += mtd->writesize; |
| 639 | } |
| 640 | } |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 641 | pr_info("written %u eraseblocks\n", i); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 642 | |
| 643 | /* Check all eraseblocks */ |
Akinobu Mita | 8dad049 | 2013-02-27 17:05:33 -0800 | [diff] [blame] | 644 | prandom_seed_state(&rnd_state, 11); |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 645 | pr_info("verifying all eraseblocks\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 646 | for (i = 0; i < ebcnt - 1; ++i) { |
| 647 | if (bbt[i] || bbt[i + 1]) |
| 648 | continue; |
Akinobu Mita | 8dad049 | 2013-02-27 17:05:33 -0800 | [diff] [blame] | 649 | prandom_bytes_state(&rnd_state, writebuf, |
| 650 | mtd->ecclayout->oobavail * 2); |
Brian Norris | 1001ff7 | 2014-07-21 19:07:12 -0700 | [diff] [blame] | 651 | addr = (loff_t)(i + 1) * mtd->erasesize - mtd->writesize; |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 652 | ops.mode = MTD_OPS_AUTO_OOB; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 653 | ops.len = 0; |
| 654 | ops.retlen = 0; |
| 655 | ops.ooblen = mtd->ecclayout->oobavail * 2; |
| 656 | ops.oobretlen = 0; |
| 657 | ops.ooboffs = 0; |
Hannes Eder | 23d4249 | 2009-03-05 20:15:01 +0100 | [diff] [blame] | 658 | ops.datbuf = NULL; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 659 | ops.oobbuf = readbuf; |
Artem Bityutskiy | fd2819b | 2011-12-23 18:27:05 +0200 | [diff] [blame] | 660 | err = mtd_read_oob(mtd, addr, &ops); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 661 | if (err) |
| 662 | goto out; |
Roger Quadros | 5a66088 | 2014-10-21 16:53:27 +0300 | [diff] [blame] | 663 | if (memcmpshow(addr, readbuf, writebuf, |
| 664 | mtd->ecclayout->oobavail * 2)) { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 665 | pr_err("error: verify failed at %#llx\n", |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 666 | (long long)addr); |
| 667 | errcnt += 1; |
| 668 | if (errcnt > 1000) { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 669 | pr_err("error: too many errors\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 670 | goto out; |
| 671 | } |
| 672 | } |
| 673 | if (i % 256 == 0) |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 674 | pr_info("verified up to eraseblock %u\n", i); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 675 | cond_resched(); |
| 676 | } |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 677 | pr_info("verified %u eraseblocks\n", i); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 678 | |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 679 | pr_info("finished with %d errors\n", errcnt); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 680 | out: |
| 681 | kfree(bbt); |
| 682 | kfree(writebuf); |
| 683 | kfree(readbuf); |
| 684 | put_mtd_device(mtd); |
| 685 | if (err) |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 686 | pr_info("error %d occurred\n", err); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 687 | printk(KERN_INFO "=================================================\n"); |
| 688 | return err; |
| 689 | } |
| 690 | module_init(mtd_oobtest_init); |
| 691 | |
| 692 | static void __exit mtd_oobtest_exit(void) |
| 693 | { |
| 694 | return; |
| 695 | } |
| 696 | module_exit(mtd_oobtest_exit); |
| 697 | |
| 698 | MODULE_DESCRIPTION("Out-of-band test module"); |
| 699 | MODULE_AUTHOR("Adrian Hunter"); |
| 700 | MODULE_LICENSE("GPL"); |