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