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