blob: 2e9e2d11f204aa8db90d775743778ce5719443e4 [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
Akinobu Mita4bf527a2013-08-03 18:52:09 +090034#include "mtd_test.h"
35
Wolfram Sang74060602011-10-30 00:11:53 +020036static int dev = -EINVAL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +020037module_param(dev, int, S_IRUGO);
38MODULE_PARM_DESC(dev, "MTD device number to use");
39
40static struct mtd_info *mtd;
41static unsigned char *readbuf;
42static unsigned char *writebuf;
43static unsigned char *bbt;
44
45static int ebcnt;
46static int pgcnt;
47static int errcnt;
48static int use_offset;
49static int use_len;
50static int use_len_max;
51static int vary_offset;
Akinobu Mita8dad0492013-02-27 17:05:33 -080052static struct rnd_state rnd_state;
Artem Bityutskiye3644da2008-12-08 13:33:29 +020053
Artem Bityutskiye3644da2008-12-08 13:33:29 +020054static void do_vary_offset(void)
55{
56 use_len -= 1;
57 if (use_len < 1) {
58 use_offset += 1;
59 if (use_offset >= use_len_max)
60 use_offset = 0;
61 use_len = use_len_max - use_offset;
62 }
63}
64
65static int write_eraseblock(int ebnum)
66{
67 int i;
68 struct mtd_oob_ops ops;
69 int err = 0;
70 loff_t addr = ebnum * mtd->erasesize;
71
72 for (i = 0; i < pgcnt; ++i, addr += mtd->writesize) {
Akinobu Mita8dad0492013-02-27 17:05:33 -080073 prandom_bytes_state(&rnd_state, writebuf, use_len);
Brian Norris0612b9d2011-08-30 18:45:40 -070074 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +020075 ops.len = 0;
76 ops.retlen = 0;
77 ops.ooblen = use_len;
78 ops.oobretlen = 0;
79 ops.ooboffs = use_offset;
Hannes Eder23d42492009-03-05 20:15:01 +010080 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +020081 ops.oobbuf = writebuf;
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +020082 err = mtd_write_oob(mtd, addr, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +020083 if (err || ops.oobretlen != use_len) {
Vikram Narayanan04810272012-10-10 23:12:02 +053084 pr_err("error: writeoob failed at %#llx\n",
Artem Bityutskiye3644da2008-12-08 13:33:29 +020085 (long long)addr);
Vikram Narayanan04810272012-10-10 23:12:02 +053086 pr_err("error: use_len %d, use_offset %d\n",
Artem Bityutskiye3644da2008-12-08 13:33:29 +020087 use_len, use_offset);
88 errcnt += 1;
89 return err ? err : -1;
90 }
91 if (vary_offset)
92 do_vary_offset();
93 }
94
95 return err;
96}
97
98static int write_whole_device(void)
99{
100 int err;
101 unsigned int i;
102
Vikram Narayanan04810272012-10-10 23:12:02 +0530103 pr_info("writing OOBs of whole device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200104 for (i = 0; i < ebcnt; ++i) {
105 if (bbt[i])
106 continue;
107 err = write_eraseblock(i);
108 if (err)
109 return err;
110 if (i % 256 == 0)
Vikram Narayanan04810272012-10-10 23:12:02 +0530111 pr_info("written up to eraseblock %u\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200112 cond_resched();
113 }
Vikram Narayanan04810272012-10-10 23:12:02 +0530114 pr_info("written %u eraseblocks\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200115 return 0;
116}
117
118static int verify_eraseblock(int ebnum)
119{
120 int i;
121 struct mtd_oob_ops ops;
122 int err = 0;
123 loff_t addr = ebnum * mtd->erasesize;
124
125 for (i = 0; i < pgcnt; ++i, addr += mtd->writesize) {
Akinobu Mita8dad0492013-02-27 17:05:33 -0800126 prandom_bytes_state(&rnd_state, writebuf, use_len);
Brian Norris0612b9d2011-08-30 18:45:40 -0700127 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200128 ops.len = 0;
129 ops.retlen = 0;
130 ops.ooblen = use_len;
131 ops.oobretlen = 0;
132 ops.ooboffs = use_offset;
Hannes Eder23d42492009-03-05 20:15:01 +0100133 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200134 ops.oobbuf = readbuf;
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200135 err = mtd_read_oob(mtd, addr, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200136 if (err || ops.oobretlen != use_len) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530137 pr_err("error: readoob failed at %#llx\n",
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200138 (long long)addr);
139 errcnt += 1;
140 return err ? err : -1;
141 }
142 if (memcmp(readbuf, writebuf, use_len)) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530143 pr_err("error: verify failed at %#llx\n",
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200144 (long long)addr);
145 errcnt += 1;
146 if (errcnt > 1000) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530147 pr_err("error: too many errors\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200148 return -1;
149 }
150 }
151 if (use_offset != 0 || use_len < mtd->ecclayout->oobavail) {
152 int k;
153
Brian Norris0612b9d2011-08-30 18:45:40 -0700154 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200155 ops.len = 0;
156 ops.retlen = 0;
157 ops.ooblen = mtd->ecclayout->oobavail;
158 ops.oobretlen = 0;
159 ops.ooboffs = 0;
Hannes Eder23d42492009-03-05 20:15:01 +0100160 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200161 ops.oobbuf = readbuf;
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200162 err = mtd_read_oob(mtd, addr, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200163 if (err || ops.oobretlen != mtd->ecclayout->oobavail) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530164 pr_err("error: readoob failed at %#llx\n",
165 (long long)addr);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200166 errcnt += 1;
167 return err ? err : -1;
168 }
169 if (memcmp(readbuf + use_offset, writebuf, use_len)) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530170 pr_err("error: verify failed at %#llx\n",
171 (long long)addr);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200172 errcnt += 1;
173 if (errcnt > 1000) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530174 pr_err("error: too many errors\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200175 return -1;
176 }
177 }
178 for (k = 0; k < use_offset; ++k)
179 if (readbuf[k] != 0xff) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530180 pr_err("error: verify 0xff "
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200181 "failed at %#llx\n",
182 (long long)addr);
183 errcnt += 1;
184 if (errcnt > 1000) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530185 pr_err("error: too "
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200186 "many errors\n");
187 return -1;
188 }
189 }
190 for (k = use_offset + use_len;
191 k < mtd->ecclayout->oobavail; ++k)
192 if (readbuf[k] != 0xff) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530193 pr_err("error: verify 0xff "
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200194 "failed at %#llx\n",
195 (long long)addr);
196 errcnt += 1;
197 if (errcnt > 1000) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530198 pr_err("error: too "
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200199 "many errors\n");
200 return -1;
201 }
202 }
203 }
204 if (vary_offset)
205 do_vary_offset();
206 }
207 return err;
208}
209
210static int verify_eraseblock_in_one_go(int ebnum)
211{
212 struct mtd_oob_ops ops;
213 int err = 0;
214 loff_t addr = ebnum * mtd->erasesize;
215 size_t len = mtd->ecclayout->oobavail * pgcnt;
216
Akinobu Mita8dad0492013-02-27 17:05:33 -0800217 prandom_bytes_state(&rnd_state, writebuf, len);
Brian Norris0612b9d2011-08-30 18:45:40 -0700218 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200219 ops.len = 0;
220 ops.retlen = 0;
221 ops.ooblen = len;
222 ops.oobretlen = 0;
223 ops.ooboffs = 0;
Hannes Eder23d42492009-03-05 20:15:01 +0100224 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200225 ops.oobbuf = readbuf;
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200226 err = mtd_read_oob(mtd, addr, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200227 if (err || ops.oobretlen != len) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530228 pr_err("error: readoob failed at %#llx\n",
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200229 (long long)addr);
230 errcnt += 1;
231 return err ? err : -1;
232 }
233 if (memcmp(readbuf, writebuf, len)) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530234 pr_err("error: verify failed at %#llx\n",
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200235 (long long)addr);
236 errcnt += 1;
237 if (errcnt > 1000) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530238 pr_err("error: too many errors\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200239 return -1;
240 }
241 }
242
243 return err;
244}
245
246static int verify_all_eraseblocks(void)
247{
248 int err;
249 unsigned int i;
250
Vikram Narayanan04810272012-10-10 23:12:02 +0530251 pr_info("verifying all eraseblocks\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200252 for (i = 0; i < ebcnt; ++i) {
253 if (bbt[i])
254 continue;
255 err = verify_eraseblock(i);
256 if (err)
257 return err;
258 if (i % 256 == 0)
Vikram Narayanan04810272012-10-10 23:12:02 +0530259 pr_info("verified up to eraseblock %u\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200260 cond_resched();
261 }
Vikram Narayanan04810272012-10-10 23:12:02 +0530262 pr_info("verified %u eraseblocks\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200263 return 0;
264}
265
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200266static int __init mtd_oobtest_init(void)
267{
268 int err = 0;
269 unsigned int i;
270 uint64_t tmp;
271 struct mtd_oob_ops ops;
272 loff_t addr = 0, addr0;
273
274 printk(KERN_INFO "\n");
275 printk(KERN_INFO "=================================================\n");
Wolfram Sang74060602011-10-30 00:11:53 +0200276
277 if (dev < 0) {
Masanari Iida064a7692012-11-09 23:20:58 +0900278 pr_info("Please specify a valid mtd-device via module parameter\n");
279 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
Wolfram Sang74060602011-10-30 00:11:53 +0200280 return -EINVAL;
281 }
282
Vikram Narayanan04810272012-10-10 23:12:02 +0530283 pr_info("MTD device: %d\n", dev);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200284
285 mtd = get_mtd_device(NULL, dev);
286 if (IS_ERR(mtd)) {
287 err = PTR_ERR(mtd);
Vikram Narayanan04810272012-10-10 23:12:02 +0530288 pr_err("error: cannot get MTD device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200289 return err;
290 }
291
Huang Shijie818b9732013-09-25 14:58:17 +0800292 if (!mtd_type_is_nand(mtd)) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530293 pr_info("this test requires NAND flash\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200294 goto out;
295 }
296
297 tmp = mtd->size;
298 do_div(tmp, mtd->erasesize);
299 ebcnt = tmp;
300 pgcnt = mtd->erasesize / mtd->writesize;
301
Vikram Narayanan04810272012-10-10 23:12:02 +0530302 pr_info("MTD device size %llu, eraseblock size %u, "
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200303 "page size %u, count of eraseblocks %u, pages per "
304 "eraseblock %u, OOB size %u\n",
305 (unsigned long long)mtd->size, mtd->erasesize,
306 mtd->writesize, ebcnt, pgcnt, mtd->oobsize);
307
308 err = -ENOMEM;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200309 readbuf = kmalloc(mtd->erasesize, GFP_KERNEL);
Brian Norris33777e62013-05-02 14:18:51 -0700310 if (!readbuf)
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200311 goto out;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200312 writebuf = kmalloc(mtd->erasesize, GFP_KERNEL);
Brian Norris33777e62013-05-02 14:18:51 -0700313 if (!writebuf)
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200314 goto out;
Akinobu Mita4bf527a2013-08-03 18:52:09 +0900315 bbt = kzalloc(ebcnt, GFP_KERNEL);
316 if (!bbt)
317 goto out;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200318
Akinobu Mita4bf527a2013-08-03 18:52:09 +0900319 err = mtdtest_scan_for_bad_eraseblocks(mtd, bbt, 0, ebcnt);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200320 if (err)
321 goto out;
322
323 use_offset = 0;
324 use_len = mtd->ecclayout->oobavail;
325 use_len_max = mtd->ecclayout->oobavail;
326 vary_offset = 0;
327
328 /* First test: write all OOB, read it back and verify */
Vikram Narayanan04810272012-10-10 23:12:02 +0530329 pr_info("test 1 of 5\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200330
Akinobu Mita4bf527a2013-08-03 18:52:09 +0900331 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200332 if (err)
333 goto out;
334
Akinobu Mita8dad0492013-02-27 17:05:33 -0800335 prandom_seed_state(&rnd_state, 1);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200336 err = write_whole_device();
337 if (err)
338 goto out;
339
Akinobu Mita8dad0492013-02-27 17:05:33 -0800340 prandom_seed_state(&rnd_state, 1);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200341 err = verify_all_eraseblocks();
342 if (err)
343 goto out;
344
345 /*
346 * Second test: write all OOB, a block at a time, read it back and
347 * verify.
348 */
Vikram Narayanan04810272012-10-10 23:12:02 +0530349 pr_info("test 2 of 5\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200350
Akinobu Mita4bf527a2013-08-03 18:52:09 +0900351 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200352 if (err)
353 goto out;
354
Akinobu Mita8dad0492013-02-27 17:05:33 -0800355 prandom_seed_state(&rnd_state, 3);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200356 err = write_whole_device();
357 if (err)
358 goto out;
359
360 /* Check all eraseblocks */
Akinobu Mita8dad0492013-02-27 17:05:33 -0800361 prandom_seed_state(&rnd_state, 3);
Vikram Narayanan04810272012-10-10 23:12:02 +0530362 pr_info("verifying all eraseblocks\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200363 for (i = 0; i < ebcnt; ++i) {
364 if (bbt[i])
365 continue;
366 err = verify_eraseblock_in_one_go(i);
367 if (err)
368 goto out;
369 if (i % 256 == 0)
Vikram Narayanan04810272012-10-10 23:12:02 +0530370 pr_info("verified up to eraseblock %u\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200371 cond_resched();
372 }
Vikram Narayanan04810272012-10-10 23:12:02 +0530373 pr_info("verified %u eraseblocks\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200374
375 /*
376 * Third test: write OOB at varying offsets and lengths, read it back
377 * and verify.
378 */
Vikram Narayanan04810272012-10-10 23:12:02 +0530379 pr_info("test 3 of 5\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200380
Akinobu Mita4bf527a2013-08-03 18:52:09 +0900381 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200382 if (err)
383 goto out;
384
385 /* Write all eraseblocks */
386 use_offset = 0;
387 use_len = mtd->ecclayout->oobavail;
388 use_len_max = mtd->ecclayout->oobavail;
389 vary_offset = 1;
Akinobu Mita8dad0492013-02-27 17:05:33 -0800390 prandom_seed_state(&rnd_state, 5);
Akinobu Mitaf54d6332009-10-09 18:43:52 +0900391
392 err = write_whole_device();
393 if (err)
394 goto out;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200395
396 /* Check all eraseblocks */
397 use_offset = 0;
398 use_len = mtd->ecclayout->oobavail;
399 use_len_max = mtd->ecclayout->oobavail;
400 vary_offset = 1;
Akinobu Mita8dad0492013-02-27 17:05:33 -0800401 prandom_seed_state(&rnd_state, 5);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200402 err = verify_all_eraseblocks();
403 if (err)
404 goto out;
405
406 use_offset = 0;
407 use_len = mtd->ecclayout->oobavail;
408 use_len_max = mtd->ecclayout->oobavail;
409 vary_offset = 0;
410
411 /* Fourth test: try to write off end of device */
Vikram Narayanan04810272012-10-10 23:12:02 +0530412 pr_info("test 4 of 5\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200413
Akinobu Mita4bf527a2013-08-03 18:52:09 +0900414 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200415 if (err)
416 goto out;
417
418 addr0 = 0;
Roel Kluinc6f7e7b2009-07-31 16:21:01 +0200419 for (i = 0; i < ebcnt && bbt[i]; ++i)
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200420 addr0 += mtd->erasesize;
421
422 /* Attempt to write off end of OOB */
Brian Norris0612b9d2011-08-30 18:45:40 -0700423 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200424 ops.len = 0;
425 ops.retlen = 0;
426 ops.ooblen = 1;
427 ops.oobretlen = 0;
428 ops.ooboffs = mtd->ecclayout->oobavail;
Hannes Eder23d42492009-03-05 20:15:01 +0100429 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200430 ops.oobbuf = writebuf;
Vikram Narayanan04810272012-10-10 23:12:02 +0530431 pr_info("attempting to start write past end of OOB\n");
432 pr_info("an error is expected...\n");
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200433 err = mtd_write_oob(mtd, addr0, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200434 if (err) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530435 pr_info("error occurred as expected\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200436 err = 0;
437 } else {
Vikram Narayanan04810272012-10-10 23:12:02 +0530438 pr_err("error: can write past end of OOB\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200439 errcnt += 1;
440 }
441
442 /* Attempt to read off end of OOB */
Brian Norris0612b9d2011-08-30 18:45:40 -0700443 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200444 ops.len = 0;
445 ops.retlen = 0;
446 ops.ooblen = 1;
447 ops.oobretlen = 0;
448 ops.ooboffs = mtd->ecclayout->oobavail;
Hannes Eder23d42492009-03-05 20:15:01 +0100449 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200450 ops.oobbuf = readbuf;
Vikram Narayanan04810272012-10-10 23:12:02 +0530451 pr_info("attempting to start read past end of OOB\n");
452 pr_info("an error is expected...\n");
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200453 err = mtd_read_oob(mtd, addr0, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200454 if (err) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530455 pr_info("error occurred as expected\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200456 err = 0;
457 } else {
Vikram Narayanan04810272012-10-10 23:12:02 +0530458 pr_err("error: can read past end of OOB\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200459 errcnt += 1;
460 }
461
462 if (bbt[ebcnt - 1])
Vikram Narayanan04810272012-10-10 23:12:02 +0530463 pr_info("skipping end of device tests because last "
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200464 "block is bad\n");
465 else {
466 /* Attempt to write off end of device */
Brian Norris0612b9d2011-08-30 18:45:40 -0700467 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200468 ops.len = 0;
469 ops.retlen = 0;
470 ops.ooblen = mtd->ecclayout->oobavail + 1;
471 ops.oobretlen = 0;
472 ops.ooboffs = 0;
Hannes Eder23d42492009-03-05 20:15:01 +0100473 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200474 ops.oobbuf = writebuf;
Vikram Narayanan04810272012-10-10 23:12:02 +0530475 pr_info("attempting to write past end of device\n");
476 pr_info("an error is expected...\n");
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200477 err = mtd_write_oob(mtd, mtd->size - mtd->writesize, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200478 if (err) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530479 pr_info("error occurred as expected\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200480 err = 0;
481 } else {
Vikram Narayanan04810272012-10-10 23:12:02 +0530482 pr_err("error: wrote past end of device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200483 errcnt += 1;
484 }
485
486 /* Attempt to read off end of device */
Brian Norris0612b9d2011-08-30 18:45:40 -0700487 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200488 ops.len = 0;
489 ops.retlen = 0;
490 ops.ooblen = mtd->ecclayout->oobavail + 1;
491 ops.oobretlen = 0;
492 ops.ooboffs = 0;
Hannes Eder23d42492009-03-05 20:15:01 +0100493 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200494 ops.oobbuf = readbuf;
Vikram Narayanan04810272012-10-10 23:12:02 +0530495 pr_info("attempting to read past end of device\n");
496 pr_info("an error is expected...\n");
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200497 err = mtd_read_oob(mtd, mtd->size - mtd->writesize, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200498 if (err) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530499 pr_info("error occurred as expected\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200500 err = 0;
501 } else {
Vikram Narayanan04810272012-10-10 23:12:02 +0530502 pr_err("error: read past end of device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200503 errcnt += 1;
504 }
505
Akinobu Mita4bf527a2013-08-03 18:52:09 +0900506 err = mtdtest_erase_eraseblock(mtd, ebcnt - 1);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200507 if (err)
508 goto out;
509
510 /* Attempt to write off end of device */
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 = mtd->ecclayout->oobavail;
515 ops.oobretlen = 0;
516 ops.ooboffs = 1;
Hannes Eder23d42492009-03-05 20:15:01 +0100517 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200518 ops.oobbuf = writebuf;
Vikram Narayanan04810272012-10-10 23:12:02 +0530519 pr_info("attempting to write past end of device\n");
520 pr_info("an error is expected...\n");
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200521 err = mtd_write_oob(mtd, mtd->size - mtd->writesize, &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: wrote past end of device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200527 errcnt += 1;
528 }
529
530 /* Attempt to read off end of device */
Brian Norris0612b9d2011-08-30 18:45:40 -0700531 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200532 ops.len = 0;
533 ops.retlen = 0;
534 ops.ooblen = mtd->ecclayout->oobavail;
535 ops.oobretlen = 0;
536 ops.ooboffs = 1;
Hannes Eder23d42492009-03-05 20:15:01 +0100537 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200538 ops.oobbuf = readbuf;
Vikram Narayanan04810272012-10-10 23:12:02 +0530539 pr_info("attempting to read past end of device\n");
540 pr_info("an error is expected...\n");
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200541 err = mtd_read_oob(mtd, mtd->size - mtd->writesize, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200542 if (err) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530543 pr_info("error occurred as expected\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200544 err = 0;
545 } else {
Vikram Narayanan04810272012-10-10 23:12:02 +0530546 pr_err("error: read past end of device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200547 errcnt += 1;
548 }
549 }
550
551 /* Fifth test: write / read across block boundaries */
Vikram Narayanan04810272012-10-10 23:12:02 +0530552 pr_info("test 5 of 5\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200553
554 /* Erase all eraseblocks */
Akinobu Mita4bf527a2013-08-03 18:52:09 +0900555 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200556 if (err)
557 goto out;
558
559 /* Write all eraseblocks */
Akinobu Mita8dad0492013-02-27 17:05:33 -0800560 prandom_seed_state(&rnd_state, 11);
Vikram Narayanan04810272012-10-10 23:12:02 +0530561 pr_info("writing OOBs of whole device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200562 for (i = 0; i < ebcnt - 1; ++i) {
563 int cnt = 2;
564 int pg;
565 size_t sz = mtd->ecclayout->oobavail;
566 if (bbt[i] || bbt[i + 1])
567 continue;
568 addr = (i + 1) * mtd->erasesize - mtd->writesize;
569 for (pg = 0; pg < cnt; ++pg) {
Akinobu Mita8dad0492013-02-27 17:05:33 -0800570 prandom_bytes_state(&rnd_state, writebuf, sz);
Brian Norris0612b9d2011-08-30 18:45:40 -0700571 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200572 ops.len = 0;
573 ops.retlen = 0;
574 ops.ooblen = sz;
575 ops.oobretlen = 0;
576 ops.ooboffs = 0;
Hannes Eder23d42492009-03-05 20:15:01 +0100577 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200578 ops.oobbuf = writebuf;
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200579 err = mtd_write_oob(mtd, addr, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200580 if (err)
581 goto out;
582 if (i % 256 == 0)
Vikram Narayanan04810272012-10-10 23:12:02 +0530583 pr_info("written up to eraseblock %u\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200584 cond_resched();
585 addr += mtd->writesize;
586 }
587 }
Vikram Narayanan04810272012-10-10 23:12:02 +0530588 pr_info("written %u eraseblocks\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200589
590 /* Check all eraseblocks */
Akinobu Mita8dad0492013-02-27 17:05:33 -0800591 prandom_seed_state(&rnd_state, 11);
Vikram Narayanan04810272012-10-10 23:12:02 +0530592 pr_info("verifying all eraseblocks\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200593 for (i = 0; i < ebcnt - 1; ++i) {
594 if (bbt[i] || bbt[i + 1])
595 continue;
Akinobu Mita8dad0492013-02-27 17:05:33 -0800596 prandom_bytes_state(&rnd_state, writebuf,
597 mtd->ecclayout->oobavail * 2);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200598 addr = (i + 1) * mtd->erasesize - mtd->writesize;
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 * 2;
603 ops.oobretlen = 0;
604 ops.ooboffs = 0;
Hannes Eder23d42492009-03-05 20:15:01 +0100605 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200606 ops.oobbuf = readbuf;
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200607 err = mtd_read_oob(mtd, addr, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200608 if (err)
609 goto out;
610 if (memcmp(readbuf, writebuf, mtd->ecclayout->oobavail * 2)) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530611 pr_err("error: verify failed at %#llx\n",
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200612 (long long)addr);
613 errcnt += 1;
614 if (errcnt > 1000) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530615 pr_err("error: too many errors\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200616 goto out;
617 }
618 }
619 if (i % 256 == 0)
Vikram Narayanan04810272012-10-10 23:12:02 +0530620 pr_info("verified up to eraseblock %u\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200621 cond_resched();
622 }
Vikram Narayanan04810272012-10-10 23:12:02 +0530623 pr_info("verified %u eraseblocks\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200624
Vikram Narayanan04810272012-10-10 23:12:02 +0530625 pr_info("finished with %d errors\n", errcnt);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200626out:
627 kfree(bbt);
628 kfree(writebuf);
629 kfree(readbuf);
630 put_mtd_device(mtd);
631 if (err)
Vikram Narayanan04810272012-10-10 23:12:02 +0530632 pr_info("error %d occurred\n", err);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200633 printk(KERN_INFO "=================================================\n");
634 return err;
635}
636module_init(mtd_oobtest_init);
637
638static void __exit mtd_oobtest_exit(void)
639{
640 return;
641}
642module_exit(mtd_oobtest_exit);
643
644MODULE_DESCRIPTION("Out-of-band test module");
645MODULE_AUTHOR("Adrian Hunter");
646MODULE_LICENSE("GPL");