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