blob: ab81e9a5947ebe9e5fbb6540bd1cf20cb22d6537 [file] [log] [blame]
Artem Bityutskiye3644da2008-12-08 13:33:29 +02001/*
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 Narayanan04810272012-10-10 23:12:02 +053022#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
Artem Bityutskiye3644da2008-12-08 13:33:29 +020024#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 Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Artem Bityutskiye3644da2008-12-08 13:33:29 +020031#include <linux/sched.h>
Akinobu Mita8dad0492013-02-27 17:05:33 -080032#include <linux/random.h>
Artem Bityutskiye3644da2008-12-08 13:33:29 +020033
Wolfram Sang74060602011-10-30 00:11:53 +020034static int dev = -EINVAL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +020035module_param(dev, int, S_IRUGO);
36MODULE_PARM_DESC(dev, "MTD device number to use");
37
38static struct mtd_info *mtd;
39static unsigned char *readbuf;
40static unsigned char *writebuf;
41static unsigned char *bbt;
42
43static int ebcnt;
44static int pgcnt;
45static int errcnt;
46static int use_offset;
47static int use_len;
48static int use_len_max;
49static int vary_offset;
Akinobu Mita8dad0492013-02-27 17:05:33 -080050static struct rnd_state rnd_state;
Artem Bityutskiye3644da2008-12-08 13:33:29 +020051
52static int erase_eraseblock(int ebnum)
53{
54 int err;
55 struct erase_info ei;
56 loff_t addr = ebnum * mtd->erasesize;
57
58 memset(&ei, 0, sizeof(struct erase_info));
59 ei.mtd = mtd;
60 ei.addr = addr;
61 ei.len = mtd->erasesize;
62
Artem Bityutskiy7e1f0dc2011-12-23 15:25:39 +020063 err = mtd_erase(mtd, &ei);
Artem Bityutskiye3644da2008-12-08 13:33:29 +020064 if (err) {
Vikram Narayanan04810272012-10-10 23:12:02 +053065 pr_err("error %d while erasing EB %d\n", err, ebnum);
Artem Bityutskiye3644da2008-12-08 13:33:29 +020066 return err;
67 }
68
69 if (ei.state == MTD_ERASE_FAILED) {
Vikram Narayanan04810272012-10-10 23:12:02 +053070 pr_err("some erase error occurred at EB %d\n", ebnum);
Artem Bityutskiye3644da2008-12-08 13:33:29 +020071 return -EIO;
72 }
73
74 return 0;
75}
76
77static int erase_whole_device(void)
78{
79 int err;
80 unsigned int i;
81
Vikram Narayanan04810272012-10-10 23:12:02 +053082 pr_info("erasing whole device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +020083 for (i = 0; i < ebcnt; ++i) {
84 if (bbt[i])
85 continue;
86 err = erase_eraseblock(i);
87 if (err)
88 return err;
89 cond_resched();
90 }
Vikram Narayanan04810272012-10-10 23:12:02 +053091 pr_info("erased %u eraseblocks\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +020092 return 0;
93}
94
95static void do_vary_offset(void)
96{
97 use_len -= 1;
98 if (use_len < 1) {
99 use_offset += 1;
100 if (use_offset >= use_len_max)
101 use_offset = 0;
102 use_len = use_len_max - use_offset;
103 }
104}
105
106static int write_eraseblock(int ebnum)
107{
108 int i;
109 struct mtd_oob_ops ops;
110 int err = 0;
111 loff_t addr = ebnum * mtd->erasesize;
112
113 for (i = 0; i < pgcnt; ++i, addr += mtd->writesize) {
Akinobu Mita8dad0492013-02-27 17:05:33 -0800114 prandom_bytes_state(&rnd_state, writebuf, use_len);
Brian Norris0612b9d2011-08-30 18:45:40 -0700115 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200116 ops.len = 0;
117 ops.retlen = 0;
118 ops.ooblen = use_len;
119 ops.oobretlen = 0;
120 ops.ooboffs = use_offset;
Hannes Eder23d42492009-03-05 20:15:01 +0100121 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200122 ops.oobbuf = writebuf;
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200123 err = mtd_write_oob(mtd, addr, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200124 if (err || ops.oobretlen != use_len) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530125 pr_err("error: writeoob failed at %#llx\n",
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200126 (long long)addr);
Vikram Narayanan04810272012-10-10 23:12:02 +0530127 pr_err("error: use_len %d, use_offset %d\n",
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200128 use_len, use_offset);
129 errcnt += 1;
130 return err ? err : -1;
131 }
132 if (vary_offset)
133 do_vary_offset();
134 }
135
136 return err;
137}
138
139static int write_whole_device(void)
140{
141 int err;
142 unsigned int i;
143
Vikram Narayanan04810272012-10-10 23:12:02 +0530144 pr_info("writing OOBs of whole device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200145 for (i = 0; i < ebcnt; ++i) {
146 if (bbt[i])
147 continue;
148 err = write_eraseblock(i);
149 if (err)
150 return err;
151 if (i % 256 == 0)
Vikram Narayanan04810272012-10-10 23:12:02 +0530152 pr_info("written up to eraseblock %u\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200153 cond_resched();
154 }
Vikram Narayanan04810272012-10-10 23:12:02 +0530155 pr_info("written %u eraseblocks\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200156 return 0;
157}
158
159static int verify_eraseblock(int ebnum)
160{
161 int i;
162 struct mtd_oob_ops ops;
163 int err = 0;
164 loff_t addr = ebnum * mtd->erasesize;
165
166 for (i = 0; i < pgcnt; ++i, addr += mtd->writesize) {
Akinobu Mita8dad0492013-02-27 17:05:33 -0800167 prandom_bytes_state(&rnd_state, writebuf, use_len);
Brian Norris0612b9d2011-08-30 18:45:40 -0700168 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200169 ops.len = 0;
170 ops.retlen = 0;
171 ops.ooblen = use_len;
172 ops.oobretlen = 0;
173 ops.ooboffs = use_offset;
Hannes Eder23d42492009-03-05 20:15:01 +0100174 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200175 ops.oobbuf = readbuf;
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200176 err = mtd_read_oob(mtd, addr, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200177 if (err || ops.oobretlen != use_len) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530178 pr_err("error: readoob failed at %#llx\n",
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200179 (long long)addr);
180 errcnt += 1;
181 return err ? err : -1;
182 }
183 if (memcmp(readbuf, writebuf, use_len)) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530184 pr_err("error: verify failed at %#llx\n",
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200185 (long long)addr);
186 errcnt += 1;
187 if (errcnt > 1000) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530188 pr_err("error: too many errors\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200189 return -1;
190 }
191 }
192 if (use_offset != 0 || use_len < mtd->ecclayout->oobavail) {
193 int k;
194
Brian Norris0612b9d2011-08-30 18:45:40 -0700195 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200196 ops.len = 0;
197 ops.retlen = 0;
198 ops.ooblen = mtd->ecclayout->oobavail;
199 ops.oobretlen = 0;
200 ops.ooboffs = 0;
Hannes Eder23d42492009-03-05 20:15:01 +0100201 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200202 ops.oobbuf = readbuf;
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200203 err = mtd_read_oob(mtd, addr, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200204 if (err || ops.oobretlen != mtd->ecclayout->oobavail) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530205 pr_err("error: readoob failed at %#llx\n",
206 (long long)addr);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200207 errcnt += 1;
208 return err ? err : -1;
209 }
210 if (memcmp(readbuf + use_offset, writebuf, use_len)) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530211 pr_err("error: verify failed at %#llx\n",
212 (long long)addr);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200213 errcnt += 1;
214 if (errcnt > 1000) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530215 pr_err("error: too many errors\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200216 return -1;
217 }
218 }
219 for (k = 0; k < use_offset; ++k)
220 if (readbuf[k] != 0xff) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530221 pr_err("error: verify 0xff "
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200222 "failed at %#llx\n",
223 (long long)addr);
224 errcnt += 1;
225 if (errcnt > 1000) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530226 pr_err("error: too "
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200227 "many errors\n");
228 return -1;
229 }
230 }
231 for (k = use_offset + use_len;
232 k < mtd->ecclayout->oobavail; ++k)
233 if (readbuf[k] != 0xff) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530234 pr_err("error: verify 0xff "
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200235 "failed at %#llx\n",
236 (long long)addr);
237 errcnt += 1;
238 if (errcnt > 1000) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530239 pr_err("error: too "
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200240 "many errors\n");
241 return -1;
242 }
243 }
244 }
245 if (vary_offset)
246 do_vary_offset();
247 }
248 return err;
249}
250
251static int verify_eraseblock_in_one_go(int ebnum)
252{
253 struct mtd_oob_ops ops;
254 int err = 0;
255 loff_t addr = ebnum * mtd->erasesize;
256 size_t len = mtd->ecclayout->oobavail * pgcnt;
257
Akinobu Mita8dad0492013-02-27 17:05:33 -0800258 prandom_bytes_state(&rnd_state, writebuf, len);
Brian Norris0612b9d2011-08-30 18:45:40 -0700259 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200260 ops.len = 0;
261 ops.retlen = 0;
262 ops.ooblen = len;
263 ops.oobretlen = 0;
264 ops.ooboffs = 0;
Hannes Eder23d42492009-03-05 20:15:01 +0100265 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200266 ops.oobbuf = readbuf;
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200267 err = mtd_read_oob(mtd, addr, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200268 if (err || ops.oobretlen != len) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530269 pr_err("error: readoob failed at %#llx\n",
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200270 (long long)addr);
271 errcnt += 1;
272 return err ? err : -1;
273 }
274 if (memcmp(readbuf, writebuf, len)) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530275 pr_err("error: verify failed at %#llx\n",
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200276 (long long)addr);
277 errcnt += 1;
278 if (errcnt > 1000) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530279 pr_err("error: too many errors\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200280 return -1;
281 }
282 }
283
284 return err;
285}
286
287static int verify_all_eraseblocks(void)
288{
289 int err;
290 unsigned int i;
291
Vikram Narayanan04810272012-10-10 23:12:02 +0530292 pr_info("verifying all eraseblocks\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200293 for (i = 0; i < ebcnt; ++i) {
294 if (bbt[i])
295 continue;
296 err = verify_eraseblock(i);
297 if (err)
298 return err;
299 if (i % 256 == 0)
Vikram Narayanan04810272012-10-10 23:12:02 +0530300 pr_info("verified up to eraseblock %u\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200301 cond_resched();
302 }
Vikram Narayanan04810272012-10-10 23:12:02 +0530303 pr_info("verified %u eraseblocks\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200304 return 0;
305}
306
307static int is_block_bad(int ebnum)
308{
309 int ret;
310 loff_t addr = ebnum * mtd->erasesize;
311
Artem Bityutskiy7086c192011-12-23 19:35:30 +0200312 ret = mtd_block_isbad(mtd, addr);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200313 if (ret)
Vikram Narayanan04810272012-10-10 23:12:02 +0530314 pr_info("block %d is bad\n", ebnum);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200315 return ret;
316}
317
318static int scan_for_bad_eraseblocks(void)
319{
320 int i, bad = 0;
321
322 bbt = kmalloc(ebcnt, GFP_KERNEL);
Brian Norris33777e62013-05-02 14:18:51 -0700323 if (!bbt)
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200324 return -ENOMEM;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200325
Vikram Narayanan04810272012-10-10 23:12:02 +0530326 pr_info("scanning for bad eraseblocks\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200327 for (i = 0; i < ebcnt; ++i) {
328 bbt[i] = is_block_bad(i) ? 1 : 0;
329 if (bbt[i])
330 bad += 1;
331 cond_resched();
332 }
Vikram Narayanan04810272012-10-10 23:12:02 +0530333 pr_info("scanned %d eraseblocks, %d are bad\n", i, bad);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200334 return 0;
335}
336
337static int __init mtd_oobtest_init(void)
338{
339 int err = 0;
340 unsigned int i;
341 uint64_t tmp;
342 struct mtd_oob_ops ops;
343 loff_t addr = 0, addr0;
344
345 printk(KERN_INFO "\n");
346 printk(KERN_INFO "=================================================\n");
Wolfram Sang74060602011-10-30 00:11:53 +0200347
348 if (dev < 0) {
Masanari Iida064a7692012-11-09 23:20:58 +0900349 pr_info("Please specify a valid mtd-device via module parameter\n");
350 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
Wolfram Sang74060602011-10-30 00:11:53 +0200351 return -EINVAL;
352 }
353
Vikram Narayanan04810272012-10-10 23:12:02 +0530354 pr_info("MTD device: %d\n", dev);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200355
356 mtd = get_mtd_device(NULL, dev);
357 if (IS_ERR(mtd)) {
358 err = PTR_ERR(mtd);
Vikram Narayanan04810272012-10-10 23:12:02 +0530359 pr_err("error: cannot get MTD device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200360 return err;
361 }
362
363 if (mtd->type != MTD_NANDFLASH) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530364 pr_info("this test requires NAND flash\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200365 goto out;
366 }
367
368 tmp = mtd->size;
369 do_div(tmp, mtd->erasesize);
370 ebcnt = tmp;
371 pgcnt = mtd->erasesize / mtd->writesize;
372
Vikram Narayanan04810272012-10-10 23:12:02 +0530373 pr_info("MTD device size %llu, eraseblock size %u, "
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200374 "page size %u, count of eraseblocks %u, pages per "
375 "eraseblock %u, OOB size %u\n",
376 (unsigned long long)mtd->size, mtd->erasesize,
377 mtd->writesize, ebcnt, pgcnt, mtd->oobsize);
378
379 err = -ENOMEM;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200380 readbuf = kmalloc(mtd->erasesize, GFP_KERNEL);
Brian Norris33777e62013-05-02 14:18:51 -0700381 if (!readbuf)
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200382 goto out;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200383 writebuf = kmalloc(mtd->erasesize, GFP_KERNEL);
Brian Norris33777e62013-05-02 14:18:51 -0700384 if (!writebuf)
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200385 goto out;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200386
387 err = scan_for_bad_eraseblocks();
388 if (err)
389 goto out;
390
391 use_offset = 0;
392 use_len = mtd->ecclayout->oobavail;
393 use_len_max = mtd->ecclayout->oobavail;
394 vary_offset = 0;
395
396 /* First test: write all OOB, read it back and verify */
Vikram Narayanan04810272012-10-10 23:12:02 +0530397 pr_info("test 1 of 5\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200398
399 err = erase_whole_device();
400 if (err)
401 goto out;
402
Akinobu Mita8dad0492013-02-27 17:05:33 -0800403 prandom_seed_state(&rnd_state, 1);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200404 err = write_whole_device();
405 if (err)
406 goto out;
407
Akinobu Mita8dad0492013-02-27 17:05:33 -0800408 prandom_seed_state(&rnd_state, 1);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200409 err = verify_all_eraseblocks();
410 if (err)
411 goto out;
412
413 /*
414 * Second test: write all OOB, a block at a time, read it back and
415 * verify.
416 */
Vikram Narayanan04810272012-10-10 23:12:02 +0530417 pr_info("test 2 of 5\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200418
419 err = erase_whole_device();
420 if (err)
421 goto out;
422
Akinobu Mita8dad0492013-02-27 17:05:33 -0800423 prandom_seed_state(&rnd_state, 3);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200424 err = write_whole_device();
425 if (err)
426 goto out;
427
428 /* Check all eraseblocks */
Akinobu Mita8dad0492013-02-27 17:05:33 -0800429 prandom_seed_state(&rnd_state, 3);
Vikram Narayanan04810272012-10-10 23:12:02 +0530430 pr_info("verifying all eraseblocks\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200431 for (i = 0; i < ebcnt; ++i) {
432 if (bbt[i])
433 continue;
434 err = verify_eraseblock_in_one_go(i);
435 if (err)
436 goto out;
437 if (i % 256 == 0)
Vikram Narayanan04810272012-10-10 23:12:02 +0530438 pr_info("verified up to eraseblock %u\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200439 cond_resched();
440 }
Vikram Narayanan04810272012-10-10 23:12:02 +0530441 pr_info("verified %u eraseblocks\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200442
443 /*
444 * Third test: write OOB at varying offsets and lengths, read it back
445 * and verify.
446 */
Vikram Narayanan04810272012-10-10 23:12:02 +0530447 pr_info("test 3 of 5\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200448
449 err = erase_whole_device();
450 if (err)
451 goto out;
452
453 /* Write all eraseblocks */
454 use_offset = 0;
455 use_len = mtd->ecclayout->oobavail;
456 use_len_max = mtd->ecclayout->oobavail;
457 vary_offset = 1;
Akinobu Mita8dad0492013-02-27 17:05:33 -0800458 prandom_seed_state(&rnd_state, 5);
Akinobu Mitaf54d6332009-10-09 18:43:52 +0900459
460 err = write_whole_device();
461 if (err)
462 goto out;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200463
464 /* Check all eraseblocks */
465 use_offset = 0;
466 use_len = mtd->ecclayout->oobavail;
467 use_len_max = mtd->ecclayout->oobavail;
468 vary_offset = 1;
Akinobu Mita8dad0492013-02-27 17:05:33 -0800469 prandom_seed_state(&rnd_state, 5);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200470 err = verify_all_eraseblocks();
471 if (err)
472 goto out;
473
474 use_offset = 0;
475 use_len = mtd->ecclayout->oobavail;
476 use_len_max = mtd->ecclayout->oobavail;
477 vary_offset = 0;
478
479 /* Fourth test: try to write off end of device */
Vikram Narayanan04810272012-10-10 23:12:02 +0530480 pr_info("test 4 of 5\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200481
482 err = erase_whole_device();
483 if (err)
484 goto out;
485
486 addr0 = 0;
Roel Kluinc6f7e7b2009-07-31 16:21:01 +0200487 for (i = 0; i < ebcnt && bbt[i]; ++i)
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200488 addr0 += mtd->erasesize;
489
490 /* Attempt to write off end of OOB */
Brian Norris0612b9d2011-08-30 18:45:40 -0700491 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200492 ops.len = 0;
493 ops.retlen = 0;
494 ops.ooblen = 1;
495 ops.oobretlen = 0;
496 ops.ooboffs = mtd->ecclayout->oobavail;
Hannes Eder23d42492009-03-05 20:15:01 +0100497 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200498 ops.oobbuf = writebuf;
Vikram Narayanan04810272012-10-10 23:12:02 +0530499 pr_info("attempting to start write past end of OOB\n");
500 pr_info("an error is expected...\n");
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200501 err = mtd_write_oob(mtd, addr0, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200502 if (err) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530503 pr_info("error occurred as expected\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200504 err = 0;
505 } else {
Vikram Narayanan04810272012-10-10 23:12:02 +0530506 pr_err("error: can write past end of OOB\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200507 errcnt += 1;
508 }
509
510 /* Attempt to read off end of OOB */
Brian Norris0612b9d2011-08-30 18:45:40 -0700511 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200512 ops.len = 0;
513 ops.retlen = 0;
514 ops.ooblen = 1;
515 ops.oobretlen = 0;
516 ops.ooboffs = mtd->ecclayout->oobavail;
Hannes Eder23d42492009-03-05 20:15:01 +0100517 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200518 ops.oobbuf = readbuf;
Vikram Narayanan04810272012-10-10 23:12:02 +0530519 pr_info("attempting to start read past end of OOB\n");
520 pr_info("an error is expected...\n");
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200521 err = mtd_read_oob(mtd, addr0, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200522 if (err) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530523 pr_info("error occurred as expected\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200524 err = 0;
525 } else {
Vikram Narayanan04810272012-10-10 23:12:02 +0530526 pr_err("error: can read past end of OOB\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200527 errcnt += 1;
528 }
529
530 if (bbt[ebcnt - 1])
Vikram Narayanan04810272012-10-10 23:12:02 +0530531 pr_info("skipping end of device tests because last "
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200532 "block is bad\n");
533 else {
534 /* Attempt to write off end of device */
Brian Norris0612b9d2011-08-30 18:45:40 -0700535 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200536 ops.len = 0;
537 ops.retlen = 0;
538 ops.ooblen = mtd->ecclayout->oobavail + 1;
539 ops.oobretlen = 0;
540 ops.ooboffs = 0;
Hannes Eder23d42492009-03-05 20:15:01 +0100541 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200542 ops.oobbuf = writebuf;
Vikram Narayanan04810272012-10-10 23:12:02 +0530543 pr_info("attempting to write past end of device\n");
544 pr_info("an error is expected...\n");
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200545 err = mtd_write_oob(mtd, mtd->size - mtd->writesize, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200546 if (err) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530547 pr_info("error occurred as expected\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200548 err = 0;
549 } else {
Vikram Narayanan04810272012-10-10 23:12:02 +0530550 pr_err("error: wrote past end of device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200551 errcnt += 1;
552 }
553
554 /* Attempt to read off end of device */
Brian Norris0612b9d2011-08-30 18:45:40 -0700555 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200556 ops.len = 0;
557 ops.retlen = 0;
558 ops.ooblen = mtd->ecclayout->oobavail + 1;
559 ops.oobretlen = 0;
560 ops.ooboffs = 0;
Hannes Eder23d42492009-03-05 20:15:01 +0100561 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200562 ops.oobbuf = readbuf;
Vikram Narayanan04810272012-10-10 23:12:02 +0530563 pr_info("attempting to read past end of device\n");
564 pr_info("an error is expected...\n");
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200565 err = mtd_read_oob(mtd, mtd->size - mtd->writesize, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200566 if (err) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530567 pr_info("error occurred as expected\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200568 err = 0;
569 } else {
Vikram Narayanan04810272012-10-10 23:12:02 +0530570 pr_err("error: read past end of device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200571 errcnt += 1;
572 }
573
574 err = erase_eraseblock(ebcnt - 1);
575 if (err)
576 goto out;
577
578 /* Attempt to write off end of device */
Brian Norris0612b9d2011-08-30 18:45:40 -0700579 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200580 ops.len = 0;
581 ops.retlen = 0;
582 ops.ooblen = mtd->ecclayout->oobavail;
583 ops.oobretlen = 0;
584 ops.ooboffs = 1;
Hannes Eder23d42492009-03-05 20:15:01 +0100585 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200586 ops.oobbuf = writebuf;
Vikram Narayanan04810272012-10-10 23:12:02 +0530587 pr_info("attempting to write past end of device\n");
588 pr_info("an error is expected...\n");
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200589 err = mtd_write_oob(mtd, mtd->size - mtd->writesize, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200590 if (err) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530591 pr_info("error occurred as expected\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200592 err = 0;
593 } else {
Vikram Narayanan04810272012-10-10 23:12:02 +0530594 pr_err("error: wrote past end of device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200595 errcnt += 1;
596 }
597
598 /* Attempt to read off end of device */
Brian Norris0612b9d2011-08-30 18:45:40 -0700599 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200600 ops.len = 0;
601 ops.retlen = 0;
602 ops.ooblen = mtd->ecclayout->oobavail;
603 ops.oobretlen = 0;
604 ops.ooboffs = 1;
Hannes Eder23d42492009-03-05 20:15:01 +0100605 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200606 ops.oobbuf = readbuf;
Vikram Narayanan04810272012-10-10 23:12:02 +0530607 pr_info("attempting to read past end of device\n");
608 pr_info("an error is expected...\n");
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200609 err = mtd_read_oob(mtd, mtd->size - mtd->writesize, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200610 if (err) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530611 pr_info("error occurred as expected\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200612 err = 0;
613 } else {
Vikram Narayanan04810272012-10-10 23:12:02 +0530614 pr_err("error: read past end of device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200615 errcnt += 1;
616 }
617 }
618
619 /* Fifth test: write / read across block boundaries */
Vikram Narayanan04810272012-10-10 23:12:02 +0530620 pr_info("test 5 of 5\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200621
622 /* Erase all eraseblocks */
623 err = erase_whole_device();
624 if (err)
625 goto out;
626
627 /* Write all eraseblocks */
Akinobu Mita8dad0492013-02-27 17:05:33 -0800628 prandom_seed_state(&rnd_state, 11);
Vikram Narayanan04810272012-10-10 23:12:02 +0530629 pr_info("writing OOBs of whole device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200630 for (i = 0; i < ebcnt - 1; ++i) {
631 int cnt = 2;
632 int pg;
633 size_t sz = mtd->ecclayout->oobavail;
634 if (bbt[i] || bbt[i + 1])
635 continue;
636 addr = (i + 1) * mtd->erasesize - mtd->writesize;
637 for (pg = 0; pg < cnt; ++pg) {
Akinobu Mita8dad0492013-02-27 17:05:33 -0800638 prandom_bytes_state(&rnd_state, writebuf, sz);
Brian Norris0612b9d2011-08-30 18:45:40 -0700639 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200640 ops.len = 0;
641 ops.retlen = 0;
642 ops.ooblen = sz;
643 ops.oobretlen = 0;
644 ops.ooboffs = 0;
Hannes Eder23d42492009-03-05 20:15:01 +0100645 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200646 ops.oobbuf = writebuf;
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200647 err = mtd_write_oob(mtd, addr, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200648 if (err)
649 goto out;
650 if (i % 256 == 0)
Vikram Narayanan04810272012-10-10 23:12:02 +0530651 pr_info("written up to eraseblock %u\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200652 cond_resched();
653 addr += mtd->writesize;
654 }
655 }
Vikram Narayanan04810272012-10-10 23:12:02 +0530656 pr_info("written %u eraseblocks\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200657
658 /* Check all eraseblocks */
Akinobu Mita8dad0492013-02-27 17:05:33 -0800659 prandom_seed_state(&rnd_state, 11);
Vikram Narayanan04810272012-10-10 23:12:02 +0530660 pr_info("verifying all eraseblocks\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200661 for (i = 0; i < ebcnt - 1; ++i) {
662 if (bbt[i] || bbt[i + 1])
663 continue;
Akinobu Mita8dad0492013-02-27 17:05:33 -0800664 prandom_bytes_state(&rnd_state, writebuf,
665 mtd->ecclayout->oobavail * 2);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200666 addr = (i + 1) * mtd->erasesize - mtd->writesize;
Brian Norris0612b9d2011-08-30 18:45:40 -0700667 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200668 ops.len = 0;
669 ops.retlen = 0;
670 ops.ooblen = mtd->ecclayout->oobavail * 2;
671 ops.oobretlen = 0;
672 ops.ooboffs = 0;
Hannes Eder23d42492009-03-05 20:15:01 +0100673 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200674 ops.oobbuf = readbuf;
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200675 err = mtd_read_oob(mtd, addr, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200676 if (err)
677 goto out;
678 if (memcmp(readbuf, writebuf, mtd->ecclayout->oobavail * 2)) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530679 pr_err("error: verify failed at %#llx\n",
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200680 (long long)addr);
681 errcnt += 1;
682 if (errcnt > 1000) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530683 pr_err("error: too many errors\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200684 goto out;
685 }
686 }
687 if (i % 256 == 0)
Vikram Narayanan04810272012-10-10 23:12:02 +0530688 pr_info("verified up to eraseblock %u\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200689 cond_resched();
690 }
Vikram Narayanan04810272012-10-10 23:12:02 +0530691 pr_info("verified %u eraseblocks\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200692
Vikram Narayanan04810272012-10-10 23:12:02 +0530693 pr_info("finished with %d errors\n", errcnt);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200694out:
695 kfree(bbt);
696 kfree(writebuf);
697 kfree(readbuf);
698 put_mtd_device(mtd);
699 if (err)
Vikram Narayanan04810272012-10-10 23:12:02 +0530700 pr_info("error %d occurred\n", err);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200701 printk(KERN_INFO "=================================================\n");
702 return err;
703}
704module_init(mtd_oobtest_init);
705
706static void __exit mtd_oobtest_exit(void)
707{
708 return;
709}
710module_exit(mtd_oobtest_exit);
711
712MODULE_DESCRIPTION("Out-of-band test module");
713MODULE_AUTHOR("Adrian Hunter");
714MODULE_LICENSE("GPL");