blob: f19ab1acde1f22dedaf700e5dd8919bebf33f2c4 [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
Akinobu Mitabe54f8f2014-03-08 00:24:10 +090072 prandom_bytes_state(&rnd_state, writebuf, use_len_max * pgcnt);
Artem Bityutskiye3644da2008-12-08 13:33:29 +020073 for (i = 0; i < pgcnt; ++i, addr += mtd->writesize) {
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;
Akinobu Mitabe54f8f2014-03-08 00:24:10 +090081 ops.oobbuf = writebuf + (use_len_max * i) + use_offset;
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
Akinobu Mitabe54f8f2014-03-08 00:24:10 +0900125 prandom_bytes_state(&rnd_state, writebuf, use_len_max * pgcnt);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200126 for (i = 0; i < pgcnt; ++i, addr += mtd->writesize) {
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 }
Akinobu Mitabe54f8f2014-03-08 00:24:10 +0900142 if (memcmp(readbuf, writebuf + (use_len_max * i) + use_offset,
143 use_len)) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530144 pr_err("error: verify failed at %#llx\n",
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200145 (long long)addr);
146 errcnt += 1;
147 if (errcnt > 1000) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530148 pr_err("error: too many errors\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200149 return -1;
150 }
151 }
152 if (use_offset != 0 || use_len < mtd->ecclayout->oobavail) {
153 int k;
154
Brian Norris0612b9d2011-08-30 18:45:40 -0700155 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200156 ops.len = 0;
157 ops.retlen = 0;
158 ops.ooblen = mtd->ecclayout->oobavail;
159 ops.oobretlen = 0;
160 ops.ooboffs = 0;
Hannes Eder23d42492009-03-05 20:15:01 +0100161 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200162 ops.oobbuf = readbuf;
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200163 err = mtd_read_oob(mtd, addr, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200164 if (err || ops.oobretlen != mtd->ecclayout->oobavail) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530165 pr_err("error: readoob failed at %#llx\n",
166 (long long)addr);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200167 errcnt += 1;
168 return err ? err : -1;
169 }
Akinobu Mitabe54f8f2014-03-08 00:24:10 +0900170 if (memcmp(readbuf + use_offset,
171 writebuf + (use_len_max * i) + use_offset,
172 use_len)) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530173 pr_err("error: verify failed at %#llx\n",
174 (long long)addr);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200175 errcnt += 1;
176 if (errcnt > 1000) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530177 pr_err("error: too many errors\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200178 return -1;
179 }
180 }
181 for (k = 0; k < use_offset; ++k)
182 if (readbuf[k] != 0xff) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530183 pr_err("error: verify 0xff "
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200184 "failed at %#llx\n",
185 (long long)addr);
186 errcnt += 1;
187 if (errcnt > 1000) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530188 pr_err("error: too "
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200189 "many errors\n");
190 return -1;
191 }
192 }
193 for (k = use_offset + use_len;
194 k < mtd->ecclayout->oobavail; ++k)
195 if (readbuf[k] != 0xff) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530196 pr_err("error: verify 0xff "
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200197 "failed at %#llx\n",
198 (long long)addr);
199 errcnt += 1;
200 if (errcnt > 1000) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530201 pr_err("error: too "
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200202 "many errors\n");
203 return -1;
204 }
205 }
206 }
207 if (vary_offset)
208 do_vary_offset();
209 }
210 return err;
211}
212
213static int verify_eraseblock_in_one_go(int ebnum)
214{
215 struct mtd_oob_ops ops;
216 int err = 0;
217 loff_t addr = ebnum * mtd->erasesize;
218 size_t len = mtd->ecclayout->oobavail * pgcnt;
219
Akinobu Mita8dad0492013-02-27 17:05:33 -0800220 prandom_bytes_state(&rnd_state, writebuf, len);
Brian Norris0612b9d2011-08-30 18:45:40 -0700221 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200222 ops.len = 0;
223 ops.retlen = 0;
224 ops.ooblen = len;
225 ops.oobretlen = 0;
226 ops.ooboffs = 0;
Hannes Eder23d42492009-03-05 20:15:01 +0100227 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200228 ops.oobbuf = readbuf;
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200229 err = mtd_read_oob(mtd, addr, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200230 if (err || ops.oobretlen != len) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530231 pr_err("error: readoob failed at %#llx\n",
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200232 (long long)addr);
233 errcnt += 1;
234 return err ? err : -1;
235 }
236 if (memcmp(readbuf, writebuf, len)) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530237 pr_err("error: verify failed at %#llx\n",
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200238 (long long)addr);
239 errcnt += 1;
240 if (errcnt > 1000) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530241 pr_err("error: too many errors\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200242 return -1;
243 }
244 }
245
246 return err;
247}
248
249static int verify_all_eraseblocks(void)
250{
251 int err;
252 unsigned int i;
253
Vikram Narayanan04810272012-10-10 23:12:02 +0530254 pr_info("verifying all eraseblocks\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200255 for (i = 0; i < ebcnt; ++i) {
256 if (bbt[i])
257 continue;
258 err = verify_eraseblock(i);
259 if (err)
260 return err;
261 if (i % 256 == 0)
Vikram Narayanan04810272012-10-10 23:12:02 +0530262 pr_info("verified up to eraseblock %u\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200263 cond_resched();
264 }
Vikram Narayanan04810272012-10-10 23:12:02 +0530265 pr_info("verified %u eraseblocks\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200266 return 0;
267}
268
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200269static int __init mtd_oobtest_init(void)
270{
271 int err = 0;
272 unsigned int i;
273 uint64_t tmp;
274 struct mtd_oob_ops ops;
275 loff_t addr = 0, addr0;
276
277 printk(KERN_INFO "\n");
278 printk(KERN_INFO "=================================================\n");
Wolfram Sang74060602011-10-30 00:11:53 +0200279
280 if (dev < 0) {
Masanari Iida064a7692012-11-09 23:20:58 +0900281 pr_info("Please specify a valid mtd-device via module parameter\n");
282 pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
Wolfram Sang74060602011-10-30 00:11:53 +0200283 return -EINVAL;
284 }
285
Vikram Narayanan04810272012-10-10 23:12:02 +0530286 pr_info("MTD device: %d\n", dev);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200287
288 mtd = get_mtd_device(NULL, dev);
289 if (IS_ERR(mtd)) {
290 err = PTR_ERR(mtd);
Vikram Narayanan04810272012-10-10 23:12:02 +0530291 pr_err("error: cannot get MTD device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200292 return err;
293 }
294
Huang Shijie818b9732013-09-25 14:58:17 +0800295 if (!mtd_type_is_nand(mtd)) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530296 pr_info("this test requires NAND flash\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200297 goto out;
298 }
299
300 tmp = mtd->size;
301 do_div(tmp, mtd->erasesize);
302 ebcnt = tmp;
303 pgcnt = mtd->erasesize / mtd->writesize;
304
Vikram Narayanan04810272012-10-10 23:12:02 +0530305 pr_info("MTD device size %llu, eraseblock size %u, "
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200306 "page size %u, count of eraseblocks %u, pages per "
307 "eraseblock %u, OOB size %u\n",
308 (unsigned long long)mtd->size, mtd->erasesize,
309 mtd->writesize, ebcnt, pgcnt, mtd->oobsize);
310
311 err = -ENOMEM;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200312 readbuf = kmalloc(mtd->erasesize, GFP_KERNEL);
Brian Norris33777e62013-05-02 14:18:51 -0700313 if (!readbuf)
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200314 goto out;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200315 writebuf = kmalloc(mtd->erasesize, GFP_KERNEL);
Brian Norris33777e62013-05-02 14:18:51 -0700316 if (!writebuf)
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200317 goto out;
Akinobu Mita4bf527a2013-08-03 18:52:09 +0900318 bbt = kzalloc(ebcnt, GFP_KERNEL);
319 if (!bbt)
320 goto out;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200321
Akinobu Mita4bf527a2013-08-03 18:52:09 +0900322 err = mtdtest_scan_for_bad_eraseblocks(mtd, bbt, 0, ebcnt);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200323 if (err)
324 goto out;
325
326 use_offset = 0;
327 use_len = mtd->ecclayout->oobavail;
328 use_len_max = mtd->ecclayout->oobavail;
329 vary_offset = 0;
330
331 /* First test: write all OOB, read it back and verify */
Vikram Narayanan04810272012-10-10 23:12:02 +0530332 pr_info("test 1 of 5\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200333
Akinobu Mita4bf527a2013-08-03 18:52:09 +0900334 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200335 if (err)
336 goto out;
337
Akinobu Mita8dad0492013-02-27 17:05:33 -0800338 prandom_seed_state(&rnd_state, 1);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200339 err = write_whole_device();
340 if (err)
341 goto out;
342
Akinobu Mita8dad0492013-02-27 17:05:33 -0800343 prandom_seed_state(&rnd_state, 1);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200344 err = verify_all_eraseblocks();
345 if (err)
346 goto out;
347
348 /*
349 * Second test: write all OOB, a block at a time, read it back and
350 * verify.
351 */
Vikram Narayanan04810272012-10-10 23:12:02 +0530352 pr_info("test 2 of 5\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200353
Akinobu Mita4bf527a2013-08-03 18:52:09 +0900354 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200355 if (err)
356 goto out;
357
Akinobu Mita8dad0492013-02-27 17:05:33 -0800358 prandom_seed_state(&rnd_state, 3);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200359 err = write_whole_device();
360 if (err)
361 goto out;
362
363 /* Check all eraseblocks */
Akinobu Mita8dad0492013-02-27 17:05:33 -0800364 prandom_seed_state(&rnd_state, 3);
Vikram Narayanan04810272012-10-10 23:12:02 +0530365 pr_info("verifying all eraseblocks\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200366 for (i = 0; i < ebcnt; ++i) {
367 if (bbt[i])
368 continue;
369 err = verify_eraseblock_in_one_go(i);
370 if (err)
371 goto out;
372 if (i % 256 == 0)
Vikram Narayanan04810272012-10-10 23:12:02 +0530373 pr_info("verified up to eraseblock %u\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200374 cond_resched();
375 }
Vikram Narayanan04810272012-10-10 23:12:02 +0530376 pr_info("verified %u eraseblocks\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200377
378 /*
379 * Third test: write OOB at varying offsets and lengths, read it back
380 * and verify.
381 */
Vikram Narayanan04810272012-10-10 23:12:02 +0530382 pr_info("test 3 of 5\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200383
Akinobu Mita4bf527a2013-08-03 18:52:09 +0900384 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200385 if (err)
386 goto out;
387
388 /* Write all eraseblocks */
389 use_offset = 0;
390 use_len = mtd->ecclayout->oobavail;
391 use_len_max = mtd->ecclayout->oobavail;
392 vary_offset = 1;
Akinobu Mita8dad0492013-02-27 17:05:33 -0800393 prandom_seed_state(&rnd_state, 5);
Akinobu Mitaf54d6332009-10-09 18:43:52 +0900394
395 err = write_whole_device();
396 if (err)
397 goto out;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200398
399 /* Check all eraseblocks */
400 use_offset = 0;
401 use_len = mtd->ecclayout->oobavail;
402 use_len_max = mtd->ecclayout->oobavail;
403 vary_offset = 1;
Akinobu Mita8dad0492013-02-27 17:05:33 -0800404 prandom_seed_state(&rnd_state, 5);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200405 err = verify_all_eraseblocks();
406 if (err)
407 goto out;
408
409 use_offset = 0;
410 use_len = mtd->ecclayout->oobavail;
411 use_len_max = mtd->ecclayout->oobavail;
412 vary_offset = 0;
413
414 /* Fourth test: try to write off end of device */
Vikram Narayanan04810272012-10-10 23:12:02 +0530415 pr_info("test 4 of 5\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200416
Akinobu Mita4bf527a2013-08-03 18:52:09 +0900417 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200418 if (err)
419 goto out;
420
421 addr0 = 0;
Roel Kluinc6f7e7b2009-07-31 16:21:01 +0200422 for (i = 0; i < ebcnt && bbt[i]; ++i)
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200423 addr0 += mtd->erasesize;
424
425 /* Attempt to write off end of OOB */
Brian Norris0612b9d2011-08-30 18:45:40 -0700426 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200427 ops.len = 0;
428 ops.retlen = 0;
429 ops.ooblen = 1;
430 ops.oobretlen = 0;
431 ops.ooboffs = mtd->ecclayout->oobavail;
Hannes Eder23d42492009-03-05 20:15:01 +0100432 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200433 ops.oobbuf = writebuf;
Vikram Narayanan04810272012-10-10 23:12:02 +0530434 pr_info("attempting to start write past end of OOB\n");
435 pr_info("an error is expected...\n");
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200436 err = mtd_write_oob(mtd, addr0, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200437 if (err) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530438 pr_info("error occurred as expected\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200439 err = 0;
440 } else {
Vikram Narayanan04810272012-10-10 23:12:02 +0530441 pr_err("error: can write past end of OOB\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200442 errcnt += 1;
443 }
444
445 /* Attempt to read off end of OOB */
Brian Norris0612b9d2011-08-30 18:45:40 -0700446 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200447 ops.len = 0;
448 ops.retlen = 0;
449 ops.ooblen = 1;
450 ops.oobretlen = 0;
451 ops.ooboffs = mtd->ecclayout->oobavail;
Hannes Eder23d42492009-03-05 20:15:01 +0100452 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200453 ops.oobbuf = readbuf;
Vikram Narayanan04810272012-10-10 23:12:02 +0530454 pr_info("attempting to start read past end of OOB\n");
455 pr_info("an error is expected...\n");
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200456 err = mtd_read_oob(mtd, addr0, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200457 if (err) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530458 pr_info("error occurred as expected\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200459 err = 0;
460 } else {
Vikram Narayanan04810272012-10-10 23:12:02 +0530461 pr_err("error: can read past end of OOB\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200462 errcnt += 1;
463 }
464
465 if (bbt[ebcnt - 1])
Vikram Narayanan04810272012-10-10 23:12:02 +0530466 pr_info("skipping end of device tests because last "
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200467 "block is bad\n");
468 else {
469 /* Attempt to write off end of device */
Brian Norris0612b9d2011-08-30 18:45:40 -0700470 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200471 ops.len = 0;
472 ops.retlen = 0;
473 ops.ooblen = mtd->ecclayout->oobavail + 1;
474 ops.oobretlen = 0;
475 ops.ooboffs = 0;
Hannes Eder23d42492009-03-05 20:15:01 +0100476 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200477 ops.oobbuf = writebuf;
Vikram Narayanan04810272012-10-10 23:12:02 +0530478 pr_info("attempting to write past end of device\n");
479 pr_info("an error is expected...\n");
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200480 err = mtd_write_oob(mtd, mtd->size - mtd->writesize, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200481 if (err) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530482 pr_info("error occurred as expected\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200483 err = 0;
484 } else {
Vikram Narayanan04810272012-10-10 23:12:02 +0530485 pr_err("error: wrote past end of device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200486 errcnt += 1;
487 }
488
489 /* Attempt to read off end of device */
Brian Norris0612b9d2011-08-30 18:45:40 -0700490 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200491 ops.len = 0;
492 ops.retlen = 0;
493 ops.ooblen = mtd->ecclayout->oobavail + 1;
494 ops.oobretlen = 0;
495 ops.ooboffs = 0;
Hannes Eder23d42492009-03-05 20:15:01 +0100496 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200497 ops.oobbuf = readbuf;
Vikram Narayanan04810272012-10-10 23:12:02 +0530498 pr_info("attempting to read past end of device\n");
499 pr_info("an error is expected...\n");
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200500 err = mtd_read_oob(mtd, mtd->size - mtd->writesize, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200501 if (err) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530502 pr_info("error occurred as expected\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200503 err = 0;
504 } else {
Vikram Narayanan04810272012-10-10 23:12:02 +0530505 pr_err("error: read past end of device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200506 errcnt += 1;
507 }
508
Akinobu Mita4bf527a2013-08-03 18:52:09 +0900509 err = mtdtest_erase_eraseblock(mtd, ebcnt - 1);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200510 if (err)
511 goto out;
512
513 /* Attempt to write off end of device */
Brian Norris0612b9d2011-08-30 18:45:40 -0700514 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200515 ops.len = 0;
516 ops.retlen = 0;
517 ops.ooblen = mtd->ecclayout->oobavail;
518 ops.oobretlen = 0;
519 ops.ooboffs = 1;
Hannes Eder23d42492009-03-05 20:15:01 +0100520 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200521 ops.oobbuf = writebuf;
Vikram Narayanan04810272012-10-10 23:12:02 +0530522 pr_info("attempting to write past end of device\n");
523 pr_info("an error is expected...\n");
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200524 err = mtd_write_oob(mtd, mtd->size - mtd->writesize, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200525 if (err) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530526 pr_info("error occurred as expected\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200527 err = 0;
528 } else {
Vikram Narayanan04810272012-10-10 23:12:02 +0530529 pr_err("error: wrote past end of device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200530 errcnt += 1;
531 }
532
533 /* Attempt to read off end of device */
Brian Norris0612b9d2011-08-30 18:45:40 -0700534 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200535 ops.len = 0;
536 ops.retlen = 0;
537 ops.ooblen = mtd->ecclayout->oobavail;
538 ops.oobretlen = 0;
539 ops.ooboffs = 1;
Hannes Eder23d42492009-03-05 20:15:01 +0100540 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200541 ops.oobbuf = readbuf;
Vikram Narayanan04810272012-10-10 23:12:02 +0530542 pr_info("attempting to read past end of device\n");
543 pr_info("an error is expected...\n");
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200544 err = mtd_read_oob(mtd, mtd->size - mtd->writesize, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200545 if (err) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530546 pr_info("error occurred as expected\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200547 err = 0;
548 } else {
Vikram Narayanan04810272012-10-10 23:12:02 +0530549 pr_err("error: read past end of device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200550 errcnt += 1;
551 }
552 }
553
554 /* Fifth test: write / read across block boundaries */
Vikram Narayanan04810272012-10-10 23:12:02 +0530555 pr_info("test 5 of 5\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200556
557 /* Erase all eraseblocks */
Akinobu Mita4bf527a2013-08-03 18:52:09 +0900558 err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200559 if (err)
560 goto out;
561
562 /* Write all eraseblocks */
Akinobu Mita8dad0492013-02-27 17:05:33 -0800563 prandom_seed_state(&rnd_state, 11);
Vikram Narayanan04810272012-10-10 23:12:02 +0530564 pr_info("writing OOBs of whole device\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200565 for (i = 0; i < ebcnt - 1; ++i) {
566 int cnt = 2;
567 int pg;
568 size_t sz = mtd->ecclayout->oobavail;
569 if (bbt[i] || bbt[i + 1])
570 continue;
571 addr = (i + 1) * mtd->erasesize - mtd->writesize;
Akinobu Mitabe54f8f2014-03-08 00:24:10 +0900572 prandom_bytes_state(&rnd_state, writebuf, sz * cnt);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200573 for (pg = 0; pg < cnt; ++pg) {
Brian Norris0612b9d2011-08-30 18:45:40 -0700574 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200575 ops.len = 0;
576 ops.retlen = 0;
577 ops.ooblen = sz;
578 ops.oobretlen = 0;
579 ops.ooboffs = 0;
Hannes Eder23d42492009-03-05 20:15:01 +0100580 ops.datbuf = NULL;
Akinobu Mitabe54f8f2014-03-08 00:24:10 +0900581 ops.oobbuf = writebuf + pg * sz;
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200582 err = mtd_write_oob(mtd, addr, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200583 if (err)
584 goto out;
585 if (i % 256 == 0)
Vikram Narayanan04810272012-10-10 23:12:02 +0530586 pr_info("written up to eraseblock %u\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200587 cond_resched();
588 addr += mtd->writesize;
589 }
590 }
Vikram Narayanan04810272012-10-10 23:12:02 +0530591 pr_info("written %u eraseblocks\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200592
593 /* Check all eraseblocks */
Akinobu Mita8dad0492013-02-27 17:05:33 -0800594 prandom_seed_state(&rnd_state, 11);
Vikram Narayanan04810272012-10-10 23:12:02 +0530595 pr_info("verifying all eraseblocks\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200596 for (i = 0; i < ebcnt - 1; ++i) {
597 if (bbt[i] || bbt[i + 1])
598 continue;
Akinobu Mita8dad0492013-02-27 17:05:33 -0800599 prandom_bytes_state(&rnd_state, writebuf,
600 mtd->ecclayout->oobavail * 2);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200601 addr = (i + 1) * mtd->erasesize - mtd->writesize;
Brian Norris0612b9d2011-08-30 18:45:40 -0700602 ops.mode = MTD_OPS_AUTO_OOB;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200603 ops.len = 0;
604 ops.retlen = 0;
605 ops.ooblen = mtd->ecclayout->oobavail * 2;
606 ops.oobretlen = 0;
607 ops.ooboffs = 0;
Hannes Eder23d42492009-03-05 20:15:01 +0100608 ops.datbuf = NULL;
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200609 ops.oobbuf = readbuf;
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200610 err = mtd_read_oob(mtd, addr, &ops);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200611 if (err)
612 goto out;
613 if (memcmp(readbuf, writebuf, mtd->ecclayout->oobavail * 2)) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530614 pr_err("error: verify failed at %#llx\n",
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200615 (long long)addr);
616 errcnt += 1;
617 if (errcnt > 1000) {
Vikram Narayanan04810272012-10-10 23:12:02 +0530618 pr_err("error: too many errors\n");
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200619 goto out;
620 }
621 }
622 if (i % 256 == 0)
Vikram Narayanan04810272012-10-10 23:12:02 +0530623 pr_info("verified up to eraseblock %u\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200624 cond_resched();
625 }
Vikram Narayanan04810272012-10-10 23:12:02 +0530626 pr_info("verified %u eraseblocks\n", i);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200627
Vikram Narayanan04810272012-10-10 23:12:02 +0530628 pr_info("finished with %d errors\n", errcnt);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200629out:
630 kfree(bbt);
631 kfree(writebuf);
632 kfree(readbuf);
633 put_mtd_device(mtd);
634 if (err)
Vikram Narayanan04810272012-10-10 23:12:02 +0530635 pr_info("error %d occurred\n", err);
Artem Bityutskiye3644da2008-12-08 13:33:29 +0200636 printk(KERN_INFO "=================================================\n");
637 return err;
638}
639module_init(mtd_oobtest_init);
640
641static void __exit mtd_oobtest_exit(void)
642{
643 return;
644}
645module_exit(mtd_oobtest_exit);
646
647MODULE_DESCRIPTION("Out-of-band test module");
648MODULE_AUTHOR("Adrian Hunter");
649MODULE_LICENSE("GPL");