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