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