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); |
| 323 | if (!bbt) { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 324 | pr_err("error: cannot allocate memory\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 325 | return -ENOMEM; |
| 326 | } |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 327 | |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 328 | pr_info("scanning for bad eraseblocks\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 329 | for (i = 0; i < ebcnt; ++i) { |
| 330 | bbt[i] = is_block_bad(i) ? 1 : 0; |
| 331 | if (bbt[i]) |
| 332 | bad += 1; |
| 333 | cond_resched(); |
| 334 | } |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 335 | pr_info("scanned %d eraseblocks, %d are bad\n", i, bad); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | static int __init mtd_oobtest_init(void) |
| 340 | { |
| 341 | int err = 0; |
| 342 | unsigned int i; |
| 343 | uint64_t tmp; |
| 344 | struct mtd_oob_ops ops; |
| 345 | loff_t addr = 0, addr0; |
| 346 | |
| 347 | printk(KERN_INFO "\n"); |
| 348 | printk(KERN_INFO "=================================================\n"); |
Wolfram Sang | 7406060 | 2011-10-30 00:11:53 +0200 | [diff] [blame] | 349 | |
| 350 | if (dev < 0) { |
Masanari Iida | 064a769 | 2012-11-09 23:20:58 +0900 | [diff] [blame] | 351 | pr_info("Please specify a valid mtd-device via module parameter\n"); |
| 352 | 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] | 353 | return -EINVAL; |
| 354 | } |
| 355 | |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 356 | pr_info("MTD device: %d\n", dev); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 357 | |
| 358 | mtd = get_mtd_device(NULL, dev); |
| 359 | if (IS_ERR(mtd)) { |
| 360 | err = PTR_ERR(mtd); |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 361 | pr_err("error: cannot get MTD device\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 362 | return err; |
| 363 | } |
| 364 | |
| 365 | if (mtd->type != MTD_NANDFLASH) { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 366 | pr_info("this test requires NAND flash\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 367 | goto out; |
| 368 | } |
| 369 | |
| 370 | tmp = mtd->size; |
| 371 | do_div(tmp, mtd->erasesize); |
| 372 | ebcnt = tmp; |
| 373 | pgcnt = mtd->erasesize / mtd->writesize; |
| 374 | |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 375 | pr_info("MTD device size %llu, eraseblock size %u, " |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 376 | "page size %u, count of eraseblocks %u, pages per " |
| 377 | "eraseblock %u, OOB size %u\n", |
| 378 | (unsigned long long)mtd->size, mtd->erasesize, |
| 379 | mtd->writesize, ebcnt, pgcnt, mtd->oobsize); |
| 380 | |
| 381 | err = -ENOMEM; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 382 | readbuf = kmalloc(mtd->erasesize, GFP_KERNEL); |
| 383 | if (!readbuf) { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 384 | pr_err("error: cannot allocate memory\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 385 | goto out; |
| 386 | } |
| 387 | writebuf = kmalloc(mtd->erasesize, GFP_KERNEL); |
| 388 | if (!writebuf) { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 389 | pr_err("error: cannot allocate memory\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 390 | goto out; |
| 391 | } |
| 392 | |
| 393 | err = scan_for_bad_eraseblocks(); |
| 394 | if (err) |
| 395 | goto out; |
| 396 | |
| 397 | use_offset = 0; |
| 398 | use_len = mtd->ecclayout->oobavail; |
| 399 | use_len_max = mtd->ecclayout->oobavail; |
| 400 | vary_offset = 0; |
| 401 | |
| 402 | /* First test: write all OOB, read it back and verify */ |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 403 | pr_info("test 1 of 5\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 404 | |
| 405 | err = erase_whole_device(); |
| 406 | if (err) |
| 407 | goto out; |
| 408 | |
Akinobu Mita | 8dad049 | 2013-02-27 17:05:33 -0800 | [diff] [blame^] | 409 | prandom_seed_state(&rnd_state, 1); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 410 | err = write_whole_device(); |
| 411 | if (err) |
| 412 | goto out; |
| 413 | |
Akinobu Mita | 8dad049 | 2013-02-27 17:05:33 -0800 | [diff] [blame^] | 414 | prandom_seed_state(&rnd_state, 1); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 415 | err = verify_all_eraseblocks(); |
| 416 | if (err) |
| 417 | goto out; |
| 418 | |
| 419 | /* |
| 420 | * Second test: write all OOB, a block at a time, read it back and |
| 421 | * verify. |
| 422 | */ |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 423 | pr_info("test 2 of 5\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 424 | |
| 425 | err = erase_whole_device(); |
| 426 | if (err) |
| 427 | goto out; |
| 428 | |
Akinobu Mita | 8dad049 | 2013-02-27 17:05:33 -0800 | [diff] [blame^] | 429 | prandom_seed_state(&rnd_state, 3); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 430 | err = write_whole_device(); |
| 431 | if (err) |
| 432 | goto out; |
| 433 | |
| 434 | /* Check all eraseblocks */ |
Akinobu Mita | 8dad049 | 2013-02-27 17:05:33 -0800 | [diff] [blame^] | 435 | prandom_seed_state(&rnd_state, 3); |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 436 | pr_info("verifying all eraseblocks\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 437 | for (i = 0; i < ebcnt; ++i) { |
| 438 | if (bbt[i]) |
| 439 | continue; |
| 440 | err = verify_eraseblock_in_one_go(i); |
| 441 | if (err) |
| 442 | goto out; |
| 443 | if (i % 256 == 0) |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 444 | pr_info("verified up to eraseblock %u\n", i); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 445 | cond_resched(); |
| 446 | } |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 447 | pr_info("verified %u eraseblocks\n", i); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 448 | |
| 449 | /* |
| 450 | * Third test: write OOB at varying offsets and lengths, read it back |
| 451 | * and verify. |
| 452 | */ |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 453 | pr_info("test 3 of 5\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 454 | |
| 455 | err = erase_whole_device(); |
| 456 | if (err) |
| 457 | goto out; |
| 458 | |
| 459 | /* Write all eraseblocks */ |
| 460 | use_offset = 0; |
| 461 | use_len = mtd->ecclayout->oobavail; |
| 462 | use_len_max = mtd->ecclayout->oobavail; |
| 463 | vary_offset = 1; |
Akinobu Mita | 8dad049 | 2013-02-27 17:05:33 -0800 | [diff] [blame^] | 464 | prandom_seed_state(&rnd_state, 5); |
Akinobu Mita | f54d633 | 2009-10-09 18:43:52 +0900 | [diff] [blame] | 465 | |
| 466 | err = write_whole_device(); |
| 467 | if (err) |
| 468 | goto out; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 469 | |
| 470 | /* Check all eraseblocks */ |
| 471 | use_offset = 0; |
| 472 | use_len = mtd->ecclayout->oobavail; |
| 473 | use_len_max = mtd->ecclayout->oobavail; |
| 474 | vary_offset = 1; |
Akinobu Mita | 8dad049 | 2013-02-27 17:05:33 -0800 | [diff] [blame^] | 475 | prandom_seed_state(&rnd_state, 5); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 476 | err = verify_all_eraseblocks(); |
| 477 | if (err) |
| 478 | goto out; |
| 479 | |
| 480 | use_offset = 0; |
| 481 | use_len = mtd->ecclayout->oobavail; |
| 482 | use_len_max = mtd->ecclayout->oobavail; |
| 483 | vary_offset = 0; |
| 484 | |
| 485 | /* Fourth test: try to write off end of device */ |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 486 | pr_info("test 4 of 5\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 487 | |
| 488 | err = erase_whole_device(); |
| 489 | if (err) |
| 490 | goto out; |
| 491 | |
| 492 | addr0 = 0; |
Roel Kluin | c6f7e7b | 2009-07-31 16:21:01 +0200 | [diff] [blame] | 493 | for (i = 0; i < ebcnt && bbt[i]; ++i) |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 494 | addr0 += mtd->erasesize; |
| 495 | |
| 496 | /* Attempt to write off end of OOB */ |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 497 | ops.mode = MTD_OPS_AUTO_OOB; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 498 | ops.len = 0; |
| 499 | ops.retlen = 0; |
| 500 | ops.ooblen = 1; |
| 501 | ops.oobretlen = 0; |
| 502 | ops.ooboffs = mtd->ecclayout->oobavail; |
Hannes Eder | 23d4249 | 2009-03-05 20:15:01 +0100 | [diff] [blame] | 503 | ops.datbuf = NULL; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 504 | ops.oobbuf = writebuf; |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 505 | pr_info("attempting to start write past end of OOB\n"); |
| 506 | pr_info("an error is expected...\n"); |
Artem Bityutskiy | a2cc5ba | 2011-12-23 18:29:55 +0200 | [diff] [blame] | 507 | err = mtd_write_oob(mtd, addr0, &ops); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 508 | if (err) { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 509 | pr_info("error occurred as expected\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 510 | err = 0; |
| 511 | } else { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 512 | pr_err("error: can write past end of OOB\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 513 | errcnt += 1; |
| 514 | } |
| 515 | |
| 516 | /* Attempt to read off end of OOB */ |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 517 | ops.mode = MTD_OPS_AUTO_OOB; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 518 | ops.len = 0; |
| 519 | ops.retlen = 0; |
| 520 | ops.ooblen = 1; |
| 521 | ops.oobretlen = 0; |
| 522 | ops.ooboffs = mtd->ecclayout->oobavail; |
Hannes Eder | 23d4249 | 2009-03-05 20:15:01 +0100 | [diff] [blame] | 523 | ops.datbuf = NULL; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 524 | ops.oobbuf = readbuf; |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 525 | pr_info("attempting to start read past end of OOB\n"); |
| 526 | pr_info("an error is expected...\n"); |
Artem Bityutskiy | fd2819b | 2011-12-23 18:27:05 +0200 | [diff] [blame] | 527 | err = mtd_read_oob(mtd, addr0, &ops); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 528 | if (err) { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 529 | pr_info("error occurred as expected\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 530 | err = 0; |
| 531 | } else { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 532 | pr_err("error: can read past end of OOB\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 533 | errcnt += 1; |
| 534 | } |
| 535 | |
| 536 | if (bbt[ebcnt - 1]) |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 537 | pr_info("skipping end of device tests because last " |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 538 | "block is bad\n"); |
| 539 | else { |
| 540 | /* Attempt to write off end of device */ |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 541 | ops.mode = MTD_OPS_AUTO_OOB; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 542 | ops.len = 0; |
| 543 | ops.retlen = 0; |
| 544 | ops.ooblen = mtd->ecclayout->oobavail + 1; |
| 545 | ops.oobretlen = 0; |
| 546 | ops.ooboffs = 0; |
Hannes Eder | 23d4249 | 2009-03-05 20:15:01 +0100 | [diff] [blame] | 547 | ops.datbuf = NULL; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 548 | ops.oobbuf = writebuf; |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 549 | pr_info("attempting to write past end of device\n"); |
| 550 | pr_info("an error is expected...\n"); |
Artem Bityutskiy | a2cc5ba | 2011-12-23 18:29:55 +0200 | [diff] [blame] | 551 | err = mtd_write_oob(mtd, mtd->size - mtd->writesize, &ops); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 552 | if (err) { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 553 | pr_info("error occurred as expected\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 554 | err = 0; |
| 555 | } else { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 556 | pr_err("error: wrote past end of device\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 557 | errcnt += 1; |
| 558 | } |
| 559 | |
| 560 | /* Attempt to read off end of device */ |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 561 | ops.mode = MTD_OPS_AUTO_OOB; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 562 | ops.len = 0; |
| 563 | ops.retlen = 0; |
| 564 | ops.ooblen = mtd->ecclayout->oobavail + 1; |
| 565 | ops.oobretlen = 0; |
| 566 | ops.ooboffs = 0; |
Hannes Eder | 23d4249 | 2009-03-05 20:15:01 +0100 | [diff] [blame] | 567 | ops.datbuf = NULL; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 568 | ops.oobbuf = readbuf; |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 569 | pr_info("attempting to read past end of device\n"); |
| 570 | pr_info("an error is expected...\n"); |
Artem Bityutskiy | fd2819b | 2011-12-23 18:27:05 +0200 | [diff] [blame] | 571 | err = mtd_read_oob(mtd, mtd->size - mtd->writesize, &ops); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 572 | if (err) { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 573 | pr_info("error occurred as expected\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 574 | err = 0; |
| 575 | } else { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 576 | pr_err("error: read past end of device\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 577 | errcnt += 1; |
| 578 | } |
| 579 | |
| 580 | err = erase_eraseblock(ebcnt - 1); |
| 581 | if (err) |
| 582 | goto out; |
| 583 | |
| 584 | /* Attempt to write off end of device */ |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 585 | ops.mode = MTD_OPS_AUTO_OOB; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 586 | ops.len = 0; |
| 587 | ops.retlen = 0; |
| 588 | ops.ooblen = mtd->ecclayout->oobavail; |
| 589 | ops.oobretlen = 0; |
| 590 | ops.ooboffs = 1; |
Hannes Eder | 23d4249 | 2009-03-05 20:15:01 +0100 | [diff] [blame] | 591 | ops.datbuf = NULL; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 592 | ops.oobbuf = writebuf; |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 593 | pr_info("attempting to write past end of device\n"); |
| 594 | pr_info("an error is expected...\n"); |
Artem Bityutskiy | a2cc5ba | 2011-12-23 18:29:55 +0200 | [diff] [blame] | 595 | err = mtd_write_oob(mtd, mtd->size - mtd->writesize, &ops); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 596 | if (err) { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 597 | pr_info("error occurred as expected\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 598 | err = 0; |
| 599 | } else { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 600 | pr_err("error: wrote past end of device\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 601 | errcnt += 1; |
| 602 | } |
| 603 | |
| 604 | /* Attempt to read off end of device */ |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 605 | ops.mode = MTD_OPS_AUTO_OOB; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 606 | ops.len = 0; |
| 607 | ops.retlen = 0; |
| 608 | ops.ooblen = mtd->ecclayout->oobavail; |
| 609 | ops.oobretlen = 0; |
| 610 | ops.ooboffs = 1; |
Hannes Eder | 23d4249 | 2009-03-05 20:15:01 +0100 | [diff] [blame] | 611 | ops.datbuf = NULL; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 612 | ops.oobbuf = readbuf; |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 613 | pr_info("attempting to read past end of device\n"); |
| 614 | pr_info("an error is expected...\n"); |
Artem Bityutskiy | fd2819b | 2011-12-23 18:27:05 +0200 | [diff] [blame] | 615 | err = mtd_read_oob(mtd, mtd->size - mtd->writesize, &ops); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 616 | if (err) { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 617 | pr_info("error occurred as expected\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 618 | err = 0; |
| 619 | } else { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 620 | pr_err("error: read past end of device\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 621 | errcnt += 1; |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | /* Fifth test: write / read across block boundaries */ |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 626 | pr_info("test 5 of 5\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 627 | |
| 628 | /* Erase all eraseblocks */ |
| 629 | err = erase_whole_device(); |
| 630 | if (err) |
| 631 | goto out; |
| 632 | |
| 633 | /* Write all eraseblocks */ |
Akinobu Mita | 8dad049 | 2013-02-27 17:05:33 -0800 | [diff] [blame^] | 634 | prandom_seed_state(&rnd_state, 11); |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 635 | pr_info("writing OOBs of whole device\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 636 | for (i = 0; i < ebcnt - 1; ++i) { |
| 637 | int cnt = 2; |
| 638 | int pg; |
| 639 | size_t sz = mtd->ecclayout->oobavail; |
| 640 | if (bbt[i] || bbt[i + 1]) |
| 641 | continue; |
| 642 | addr = (i + 1) * mtd->erasesize - mtd->writesize; |
| 643 | for (pg = 0; pg < cnt; ++pg) { |
Akinobu Mita | 8dad049 | 2013-02-27 17:05:33 -0800 | [diff] [blame^] | 644 | prandom_bytes_state(&rnd_state, writebuf, sz); |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 645 | ops.mode = MTD_OPS_AUTO_OOB; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 646 | ops.len = 0; |
| 647 | ops.retlen = 0; |
| 648 | ops.ooblen = sz; |
| 649 | ops.oobretlen = 0; |
| 650 | ops.ooboffs = 0; |
Hannes Eder | 23d4249 | 2009-03-05 20:15:01 +0100 | [diff] [blame] | 651 | ops.datbuf = NULL; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 652 | ops.oobbuf = writebuf; |
Artem Bityutskiy | a2cc5ba | 2011-12-23 18:29:55 +0200 | [diff] [blame] | 653 | err = mtd_write_oob(mtd, addr, &ops); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 654 | if (err) |
| 655 | goto out; |
| 656 | if (i % 256 == 0) |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 657 | pr_info("written up to eraseblock %u\n", i); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 658 | cond_resched(); |
| 659 | addr += mtd->writesize; |
| 660 | } |
| 661 | } |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 662 | pr_info("written %u eraseblocks\n", i); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 663 | |
| 664 | /* Check all eraseblocks */ |
Akinobu Mita | 8dad049 | 2013-02-27 17:05:33 -0800 | [diff] [blame^] | 665 | prandom_seed_state(&rnd_state, 11); |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 666 | pr_info("verifying all eraseblocks\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 667 | for (i = 0; i < ebcnt - 1; ++i) { |
| 668 | if (bbt[i] || bbt[i + 1]) |
| 669 | continue; |
Akinobu Mita | 8dad049 | 2013-02-27 17:05:33 -0800 | [diff] [blame^] | 670 | prandom_bytes_state(&rnd_state, writebuf, |
| 671 | mtd->ecclayout->oobavail * 2); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 672 | addr = (i + 1) * mtd->erasesize - mtd->writesize; |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 673 | ops.mode = MTD_OPS_AUTO_OOB; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 674 | ops.len = 0; |
| 675 | ops.retlen = 0; |
| 676 | ops.ooblen = mtd->ecclayout->oobavail * 2; |
| 677 | ops.oobretlen = 0; |
| 678 | ops.ooboffs = 0; |
Hannes Eder | 23d4249 | 2009-03-05 20:15:01 +0100 | [diff] [blame] | 679 | ops.datbuf = NULL; |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 680 | ops.oobbuf = readbuf; |
Artem Bityutskiy | fd2819b | 2011-12-23 18:27:05 +0200 | [diff] [blame] | 681 | err = mtd_read_oob(mtd, addr, &ops); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 682 | if (err) |
| 683 | goto out; |
| 684 | if (memcmp(readbuf, writebuf, mtd->ecclayout->oobavail * 2)) { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 685 | pr_err("error: verify failed at %#llx\n", |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 686 | (long long)addr); |
| 687 | errcnt += 1; |
| 688 | if (errcnt > 1000) { |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 689 | pr_err("error: too many errors\n"); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 690 | goto out; |
| 691 | } |
| 692 | } |
| 693 | if (i % 256 == 0) |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 694 | pr_info("verified up to eraseblock %u\n", i); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 695 | cond_resched(); |
| 696 | } |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 697 | pr_info("verified %u eraseblocks\n", i); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 698 | |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 699 | pr_info("finished with %d errors\n", errcnt); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 700 | out: |
| 701 | kfree(bbt); |
| 702 | kfree(writebuf); |
| 703 | kfree(readbuf); |
| 704 | put_mtd_device(mtd); |
| 705 | if (err) |
Vikram Narayanan | 0481027 | 2012-10-10 23:12:02 +0530 | [diff] [blame] | 706 | pr_info("error %d occurred\n", err); |
Artem Bityutskiy | e3644da | 2008-12-08 13:33:29 +0200 | [diff] [blame] | 707 | printk(KERN_INFO "=================================================\n"); |
| 708 | return err; |
| 709 | } |
| 710 | module_init(mtd_oobtest_init); |
| 711 | |
| 712 | static void __exit mtd_oobtest_exit(void) |
| 713 | { |
| 714 | return; |
| 715 | } |
| 716 | module_exit(mtd_oobtest_exit); |
| 717 | |
| 718 | MODULE_DESCRIPTION("Out-of-band test module"); |
| 719 | MODULE_AUTHOR("Adrian Hunter"); |
| 720 | MODULE_LICENSE("GPL"); |