]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/mtd/ubi/io.c
UBI: fix compilation warnings
[net-next-2.6.git] / drivers / mtd / ubi / io.c
1 /*
2  * Copyright (c) International Business Machines Corp., 2006
3  * Copyright (c) Nokia Corporation, 2006, 2007
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13  * the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Author: Artem Bityutskiy (Битюцкий Артём)
20  */
21
22 /*
23  * UBI input/output sub-system.
24  *
25  * This sub-system provides a uniform way to work with all kinds of the
26  * underlying MTD devices. It also implements handy functions for reading and
27  * writing UBI headers.
28  *
29  * We are trying to have a paranoid mindset and not to trust to what we read
30  * from the flash media in order to be more secure and robust. So this
31  * sub-system validates every single header it reads from the flash media.
32  *
33  * Some words about how the eraseblock headers are stored.
34  *
35  * The erase counter header is always stored at offset zero. By default, the
36  * VID header is stored after the EC header at the closest aligned offset
37  * (i.e. aligned to the minimum I/O unit size). Data starts next to the VID
38  * header at the closest aligned offset. But this default layout may be
39  * changed. For example, for different reasons (e.g., optimization) UBI may be
40  * asked to put the VID header at further offset, and even at an unaligned
41  * offset. Of course, if the offset of the VID header is unaligned, UBI adds
42  * proper padding in front of it. Data offset may also be changed but it has to
43  * be aligned.
44  *
45  * About minimal I/O units. In general, UBI assumes flash device model where
46  * there is only one minimal I/O unit size. E.g., in case of NOR flash it is 1,
47  * in case of NAND flash it is a NAND page, etc. This is reported by MTD in the
48  * @ubi->mtd->writesize field. But as an exception, UBI admits of using another
49  * (smaller) minimal I/O unit size for EC and VID headers to make it possible
50  * to do different optimizations.
51  *
52  * This is extremely useful in case of NAND flashes which admit of several
53  * write operations to one NAND page. In this case UBI can fit EC and VID
54  * headers at one NAND page. Thus, UBI may use "sub-page" size as the minimal
55  * I/O unit for the headers (the @ubi->hdrs_min_io_size field). But it still
56  * reports NAND page size (@ubi->min_io_size) as a minimal I/O unit for the UBI
57  * users.
58  *
59  * Example: some Samsung NANDs with 2KiB pages allow 4x 512-byte writes, so
60  * although the minimal I/O unit is 2K, UBI uses 512 bytes for EC and VID
61  * headers.
62  *
63  * Q: why not just to treat sub-page as a minimal I/O unit of this flash
64  * device, e.g., make @ubi->min_io_size = 512 in the example above?
65  *
66  * A: because when writing a sub-page, MTD still writes a full 2K page but the
67  * bytes which are no relevant to the sub-page are 0xFF. So, basically, writing
68  * 4x512 sub-pages is 4 times slower then writing one 2KiB NAND page. Thus, we
69  * prefer to use sub-pages only for EV and VID headers.
70  *
71  * As it was noted above, the VID header may start at a non-aligned offset.
72  * For example, in case of a 2KiB page NAND flash with a 512 bytes sub-page,
73  * the VID header may reside at offset 1984 which is the last 64 bytes of the
74  * last sub-page (EC header is always at offset zero). This causes some
75  * difficulties when reading and writing VID headers.
76  *
77  * Suppose we have a 64-byte buffer and we read a VID header at it. We change
78  * the data and want to write this VID header out. As we can only write in
79  * 512-byte chunks, we have to allocate one more buffer and copy our VID header
80  * to offset 448 of this buffer.
81  *
82  * The I/O sub-system does the following trick in order to avoid this extra
83  * copy. It always allocates a @ubi->vid_hdr_alsize bytes buffer for the VID
84  * header and returns a pointer to offset @ubi->vid_hdr_shift of this buffer.
85  * When the VID header is being written out, it shifts the VID header pointer
86  * back and writes the whole sub-page.
87  */
88
89 #include <linux/crc32.h>
90 #include <linux/err.h>
91 #include "ubi.h"
92
93 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
94 static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum);
95 static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum);
96 static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum,
97                                  const struct ubi_ec_hdr *ec_hdr);
98 static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum);
99 static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum,
100                                   const struct ubi_vid_hdr *vid_hdr);
101 #else
102 #define paranoid_check_not_bad(ubi, pnum) 0
103 #define paranoid_check_peb_ec_hdr(ubi, pnum)  0
104 #define paranoid_check_ec_hdr(ubi, pnum, ec_hdr)  0
105 #define paranoid_check_peb_vid_hdr(ubi, pnum) 0
106 #define paranoid_check_vid_hdr(ubi, pnum, vid_hdr) 0
107 #endif
108
109 /**
110  * ubi_io_read - read data from a physical eraseblock.
111  * @ubi: UBI device description object
112  * @buf: buffer where to store the read data
113  * @pnum: physical eraseblock number to read from
114  * @offset: offset within the physical eraseblock from where to read
115  * @len: how many bytes to read
116  *
117  * This function reads data from offset @offset of physical eraseblock @pnum
118  * and stores the read data in the @buf buffer. The following return codes are
119  * possible:
120  *
121  * o %0 if all the requested data were successfully read;
122  * o %UBI_IO_BITFLIPS if all the requested data were successfully read, but
123  *   correctable bit-flips were detected; this is harmless but may indicate
124  *   that this eraseblock may become bad soon (but do not have to);
125  * o %-EBADMSG if the MTD subsystem reported about data integrity problems, for
126  *   example it can be an ECC error in case of NAND; this most probably means
127  *   that the data is corrupted;
128  * o %-EIO if some I/O error occurred;
129  * o other negative error codes in case of other errors.
130  */
131 int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset,
132                 int len)
133 {
134         int err, retries = 0;
135         size_t read;
136         loff_t addr;
137
138         dbg_io("read %d bytes from PEB %d:%d", len, pnum, offset);
139
140         ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
141         ubi_assert(offset >= 0 && offset + len <= ubi->peb_size);
142         ubi_assert(len > 0);
143
144         err = paranoid_check_not_bad(ubi, pnum);
145         if (err)
146                 return err > 0 ? -EINVAL : err;
147
148         addr = (loff_t)pnum * ubi->peb_size + offset;
149 retry:
150         err = ubi->mtd->read(ubi->mtd, addr, len, &read, buf);
151         if (err) {
152                 if (err == -EUCLEAN) {
153                         /*
154                          * -EUCLEAN is reported if there was a bit-flip which
155                          * was corrected, so this is harmless.
156                          *
157                          * We do not report about it here unless debugging is
158                          * enabled. A corresponding message will be printed
159                          * later, when it is has been scrubbed.
160                          */
161                         dbg_msg("fixable bit-flip detected at PEB %d", pnum);
162                         ubi_assert(len == read);
163                         return UBI_IO_BITFLIPS;
164                 }
165
166                 if (read != len && retries++ < UBI_IO_RETRIES) {
167                         dbg_io("error %d while reading %d bytes from PEB %d:%d,"
168                                " read only %zd bytes, retry",
169                                err, len, pnum, offset, read);
170                         yield();
171                         goto retry;
172                 }
173
174                 ubi_err("error %d while reading %d bytes from PEB %d:%d, "
175                         "read %zd bytes", err, len, pnum, offset, read);
176                 ubi_dbg_dump_stack();
177
178                 /*
179                  * The driver should never return -EBADMSG if it failed to read
180                  * all the requested data. But some buggy drivers might do
181                  * this, so we change it to -EIO.
182                  */
183                 if (read != len && err == -EBADMSG) {
184                         ubi_assert(0);
185                         err = -EIO;
186                 }
187         } else {
188                 ubi_assert(len == read);
189
190                 if (ubi_dbg_is_bitflip()) {
191                         dbg_gen("bit-flip (emulated)");
192                         err = UBI_IO_BITFLIPS;
193                 }
194         }
195
196         return err;
197 }
198
199 /**
200  * ubi_io_write - write data to a physical eraseblock.
201  * @ubi: UBI device description object
202  * @buf: buffer with the data to write
203  * @pnum: physical eraseblock number to write to
204  * @offset: offset within the physical eraseblock where to write
205  * @len: how many bytes to write
206  *
207  * This function writes @len bytes of data from buffer @buf to offset @offset
208  * of physical eraseblock @pnum. If all the data were successfully written,
209  * zero is returned. If an error occurred, this function returns a negative
210  * error code. If %-EIO is returned, the physical eraseblock most probably went
211  * bad.
212  *
213  * Note, in case of an error, it is possible that something was still written
214  * to the flash media, but may be some garbage.
215  */
216 int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
217                  int len)
218 {
219         int err;
220         size_t written;
221         loff_t addr;
222
223         dbg_io("write %d bytes to PEB %d:%d", len, pnum, offset);
224
225         ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
226         ubi_assert(offset >= 0 && offset + len <= ubi->peb_size);
227         ubi_assert(offset % ubi->hdrs_min_io_size == 0);
228         ubi_assert(len > 0 && len % ubi->hdrs_min_io_size == 0);
229
230         if (ubi->ro_mode) {
231                 ubi_err("read-only mode");
232                 return -EROFS;
233         }
234
235         /* The below has to be compiled out if paranoid checks are disabled */
236
237         err = paranoid_check_not_bad(ubi, pnum);
238         if (err)
239                 return err > 0 ? -EINVAL : err;
240
241         /* The area we are writing to has to contain all 0xFF bytes */
242         err = ubi_dbg_check_all_ff(ubi, pnum, offset, len);
243         if (err)
244                 return err > 0 ? -EINVAL : err;
245
246         if (offset >= ubi->leb_start) {
247                 /*
248                  * We write to the data area of the physical eraseblock. Make
249                  * sure it has valid EC and VID headers.
250                  */
251                 err = paranoid_check_peb_ec_hdr(ubi, pnum);
252                 if (err)
253                         return err > 0 ? -EINVAL : err;
254                 err = paranoid_check_peb_vid_hdr(ubi, pnum);
255                 if (err)
256                         return err > 0 ? -EINVAL : err;
257         }
258
259         if (ubi_dbg_is_write_failure()) {
260                 dbg_err("cannot write %d bytes to PEB %d:%d "
261                         "(emulated)", len, pnum, offset);
262                 ubi_dbg_dump_stack();
263                 return -EIO;
264         }
265
266         addr = (loff_t)pnum * ubi->peb_size + offset;
267         err = ubi->mtd->write(ubi->mtd, addr, len, &written, buf);
268         if (err) {
269                 ubi_err("error %d while writing %d bytes to PEB %d:%d, written "
270                         "%zd bytes", err, len, pnum, offset, written);
271                 ubi_dbg_dump_stack();
272         } else
273                 ubi_assert(written == len);
274
275         return err;
276 }
277
278 /**
279  * erase_callback - MTD erasure call-back.
280  * @ei: MTD erase information object.
281  *
282  * Note, even though MTD erase interface is asynchronous, all the current
283  * implementations are synchronous anyway.
284  */
285 static void erase_callback(struct erase_info *ei)
286 {
287         wake_up_interruptible((wait_queue_head_t *)ei->priv);
288 }
289
290 /**
291  * do_sync_erase - synchronously erase a physical eraseblock.
292  * @ubi: UBI device description object
293  * @pnum: the physical eraseblock number to erase
294  *
295  * This function synchronously erases physical eraseblock @pnum and returns
296  * zero in case of success and a negative error code in case of failure. If
297  * %-EIO is returned, the physical eraseblock most probably went bad.
298  */
299 static int do_sync_erase(struct ubi_device *ubi, int pnum)
300 {
301         int err, retries = 0;
302         struct erase_info ei;
303         wait_queue_head_t wq;
304
305         dbg_io("erase PEB %d", pnum);
306
307 retry:
308         init_waitqueue_head(&wq);
309         memset(&ei, 0, sizeof(struct erase_info));
310
311         ei.mtd      = ubi->mtd;
312         ei.addr     = (loff_t)pnum * ubi->peb_size;
313         ei.len      = ubi->peb_size;
314         ei.callback = erase_callback;
315         ei.priv     = (unsigned long)&wq;
316
317         err = ubi->mtd->erase(ubi->mtd, &ei);
318         if (err) {
319                 if (retries++ < UBI_IO_RETRIES) {
320                         dbg_io("error %d while erasing PEB %d, retry",
321                                err, pnum);
322                         yield();
323                         goto retry;
324                 }
325                 ubi_err("cannot erase PEB %d, error %d", pnum, err);
326                 ubi_dbg_dump_stack();
327                 return err;
328         }
329
330         err = wait_event_interruptible(wq, ei.state == MTD_ERASE_DONE ||
331                                            ei.state == MTD_ERASE_FAILED);
332         if (err) {
333                 ubi_err("interrupted PEB %d erasure", pnum);
334                 return -EINTR;
335         }
336
337         if (ei.state == MTD_ERASE_FAILED) {
338                 if (retries++ < UBI_IO_RETRIES) {
339                         dbg_io("error while erasing PEB %d, retry", pnum);
340                         yield();
341                         goto retry;
342                 }
343                 ubi_err("cannot erase PEB %d", pnum);
344                 ubi_dbg_dump_stack();
345                 return -EIO;
346         }
347
348         err = ubi_dbg_check_all_ff(ubi, pnum, 0, ubi->peb_size);
349         if (err)
350                 return err > 0 ? -EINVAL : err;
351
352         if (ubi_dbg_is_erase_failure() && !err) {
353                 dbg_err("cannot erase PEB %d (emulated)", pnum);
354                 return -EIO;
355         }
356
357         return 0;
358 }
359
360 /**
361  * check_pattern - check if buffer contains only a certain byte pattern.
362  * @buf: buffer to check
363  * @patt: the pattern to check
364  * @size: buffer size in bytes
365  *
366  * This function returns %1 in there are only @patt bytes in @buf, and %0 if
367  * something else was also found.
368  */
369 static int check_pattern(const void *buf, uint8_t patt, int size)
370 {
371         int i;
372
373         for (i = 0; i < size; i++)
374                 if (((const uint8_t *)buf)[i] != patt)
375                         return 0;
376         return 1;
377 }
378
379 /* Patterns to write to a physical eraseblock when torturing it */
380 static uint8_t patterns[] = {0xa5, 0x5a, 0x0};
381
382 /**
383  * torture_peb - test a supposedly bad physical eraseblock.
384  * @ubi: UBI device description object
385  * @pnum: the physical eraseblock number to test
386  *
387  * This function returns %-EIO if the physical eraseblock did not pass the
388  * test, a positive number of erase operations done if the test was
389  * successfully passed, and other negative error codes in case of other errors.
390  */
391 static int torture_peb(struct ubi_device *ubi, int pnum)
392 {
393         int err, i, patt_count;
394
395         ubi_msg("run torture test for PEB %d", pnum);
396         patt_count = ARRAY_SIZE(patterns);
397         ubi_assert(patt_count > 0);
398
399         mutex_lock(&ubi->buf_mutex);
400         for (i = 0; i < patt_count; i++) {
401                 err = do_sync_erase(ubi, pnum);
402                 if (err)
403                         goto out;
404
405                 /* Make sure the PEB contains only 0xFF bytes */
406                 err = ubi_io_read(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size);
407                 if (err)
408                         goto out;
409
410                 err = check_pattern(ubi->peb_buf1, 0xFF, ubi->peb_size);
411                 if (err == 0) {
412                         ubi_err("erased PEB %d, but a non-0xFF byte found",
413                                 pnum);
414                         err = -EIO;
415                         goto out;
416                 }
417
418                 /* Write a pattern and check it */
419                 memset(ubi->peb_buf1, patterns[i], ubi->peb_size);
420                 err = ubi_io_write(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size);
421                 if (err)
422                         goto out;
423
424                 memset(ubi->peb_buf1, ~patterns[i], ubi->peb_size);
425                 err = ubi_io_read(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size);
426                 if (err)
427                         goto out;
428
429                 err = check_pattern(ubi->peb_buf1, patterns[i], ubi->peb_size);
430                 if (err == 0) {
431                         ubi_err("pattern %x checking failed for PEB %d",
432                                 patterns[i], pnum);
433                         err = -EIO;
434                         goto out;
435                 }
436         }
437
438         err = patt_count;
439         ubi_msg("PEB %d passed torture test, do not mark it a bad", pnum);
440
441 out:
442         mutex_unlock(&ubi->buf_mutex);
443         if (err == UBI_IO_BITFLIPS || err == -EBADMSG) {
444                 /*
445                  * If a bit-flip or data integrity error was detected, the test
446                  * has not passed because it happened on a freshly erased
447                  * physical eraseblock which means something is wrong with it.
448                  */
449                 ubi_err("read problems on freshly erased PEB %d, must be bad",
450                         pnum);
451                 err = -EIO;
452         }
453         return err;
454 }
455
456 /**
457  * nor_erase_prepare - prepare a NOR flash PEB for erasure.
458  * @ubi: UBI device description object
459  * @pnum: physical eraseblock number to prepare
460  *
461  * NOR flash, or at least some of them, have peculiar embedded PEB erasure
462  * algorithm: the PEB is first filled with zeroes, then it is erased. And
463  * filling with zeroes starts from the end of the PEB. This was observed with
464  * Spansion S29GL512N NOR flash.
465  *
466  * This means that in case of a power cut we may end up with intact data at the
467  * beginning of the PEB, and all zeroes at the end of PEB. In other words, the
468  * EC and VID headers are OK, but a large chunk of data at the end of PEB is
469  * zeroed. This makes UBI mistakenly treat this PEB as used and associate it
470  * with an LEB, which leads to subsequent failures (e.g., UBIFS fails).
471  *
472  * This function is called before erasing NOR PEBs and it zeroes out EC and VID
473  * magic numbers in order to invalidate them and prevent the failures. Returns
474  * zero in case of success and a negative error code in case of failure.
475  */
476 static int nor_erase_prepare(struct ubi_device *ubi, int pnum)
477 {
478         int err;
479         size_t written;
480         loff_t addr;
481         uint32_t data = 0;
482
483         addr = (loff_t)pnum * ubi->peb_size;
484         err = ubi->mtd->write(ubi->mtd, addr, 4, &written, (void *)&data);
485         if (err) {
486                 ubi_err("error %d while writing 4 bytes to PEB %d:%d, written "
487                         "%zd bytes", err, pnum, 0, written);
488                 ubi_dbg_dump_stack();
489                 return err;
490         }
491
492         addr += ubi->vid_hdr_aloffset;
493         err = ubi->mtd->write(ubi->mtd, addr, 4, &written, (void *)&data);
494         if (err) {
495                 ubi_err("error %d while writing 4 bytes to PEB %d:%d, written "
496                         "%zd bytes", err, pnum, ubi->vid_hdr_aloffset, written);
497                 ubi_dbg_dump_stack();
498                 return err;
499         }
500
501         return 0;
502 }
503
504 /**
505  * ubi_io_sync_erase - synchronously erase a physical eraseblock.
506  * @ubi: UBI device description object
507  * @pnum: physical eraseblock number to erase
508  * @torture: if this physical eraseblock has to be tortured
509  *
510  * This function synchronously erases physical eraseblock @pnum. If @torture
511  * flag is not zero, the physical eraseblock is checked by means of writing
512  * different patterns to it and reading them back. If the torturing is enabled,
513  * the physical eraseblock is erased more than once.
514  *
515  * This function returns the number of erasures made in case of success, %-EIO
516  * if the erasure failed or the torturing test failed, and other negative error
517  * codes in case of other errors. Note, %-EIO means that the physical
518  * eraseblock is bad.
519  */
520 int ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture)
521 {
522         int err, ret = 0;
523
524         ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
525
526         err = paranoid_check_not_bad(ubi, pnum);
527         if (err != 0)
528                 return err > 0 ? -EINVAL : err;
529
530         if (ubi->ro_mode) {
531                 ubi_err("read-only mode");
532                 return -EROFS;
533         }
534
535         if (ubi->nor_flash) {
536                 err = nor_erase_prepare(ubi, pnum);
537                 if (err)
538                         return err;
539         }
540
541         if (torture) {
542                 ret = torture_peb(ubi, pnum);
543                 if (ret < 0)
544                         return ret;
545         }
546
547         err = do_sync_erase(ubi, pnum);
548         if (err)
549                 return err;
550
551         return ret + 1;
552 }
553
554 /**
555  * ubi_io_is_bad - check if a physical eraseblock is bad.
556  * @ubi: UBI device description object
557  * @pnum: the physical eraseblock number to check
558  *
559  * This function returns a positive number if the physical eraseblock is bad,
560  * zero if not, and a negative error code if an error occurred.
561  */
562 int ubi_io_is_bad(const struct ubi_device *ubi, int pnum)
563 {
564         struct mtd_info *mtd = ubi->mtd;
565
566         ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
567
568         if (ubi->bad_allowed) {
569                 int ret;
570
571                 ret = mtd->block_isbad(mtd, (loff_t)pnum * ubi->peb_size);
572                 if (ret < 0)
573                         ubi_err("error %d while checking if PEB %d is bad",
574                                 ret, pnum);
575                 else if (ret)
576                         dbg_io("PEB %d is bad", pnum);
577                 return ret;
578         }
579
580         return 0;
581 }
582
583 /**
584  * ubi_io_mark_bad - mark a physical eraseblock as bad.
585  * @ubi: UBI device description object
586  * @pnum: the physical eraseblock number to mark
587  *
588  * This function returns zero in case of success and a negative error code in
589  * case of failure.
590  */
591 int ubi_io_mark_bad(const struct ubi_device *ubi, int pnum)
592 {
593         int err;
594         struct mtd_info *mtd = ubi->mtd;
595
596         ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
597
598         if (ubi->ro_mode) {
599                 ubi_err("read-only mode");
600                 return -EROFS;
601         }
602
603         if (!ubi->bad_allowed)
604                 return 0;
605
606         err = mtd->block_markbad(mtd, (loff_t)pnum * ubi->peb_size);
607         if (err)
608                 ubi_err("cannot mark PEB %d bad, error %d", pnum, err);
609         return err;
610 }
611
612 /**
613  * validate_ec_hdr - validate an erase counter header.
614  * @ubi: UBI device description object
615  * @ec_hdr: the erase counter header to check
616  *
617  * This function returns zero if the erase counter header is OK, and %1 if
618  * not.
619  */
620 static int validate_ec_hdr(const struct ubi_device *ubi,
621                            const struct ubi_ec_hdr *ec_hdr)
622 {
623         long long ec;
624         int vid_hdr_offset, leb_start;
625
626         ec = be64_to_cpu(ec_hdr->ec);
627         vid_hdr_offset = be32_to_cpu(ec_hdr->vid_hdr_offset);
628         leb_start = be32_to_cpu(ec_hdr->data_offset);
629
630         if (ec_hdr->version != UBI_VERSION) {
631                 ubi_err("node with incompatible UBI version found: "
632                         "this UBI version is %d, image version is %d",
633                         UBI_VERSION, (int)ec_hdr->version);
634                 goto bad;
635         }
636
637         if (vid_hdr_offset != ubi->vid_hdr_offset) {
638                 ubi_err("bad VID header offset %d, expected %d",
639                         vid_hdr_offset, ubi->vid_hdr_offset);
640                 goto bad;
641         }
642
643         if (leb_start != ubi->leb_start) {
644                 ubi_err("bad data offset %d, expected %d",
645                         leb_start, ubi->leb_start);
646                 goto bad;
647         }
648
649         if (ec < 0 || ec > UBI_MAX_ERASECOUNTER) {
650                 ubi_err("bad erase counter %lld", ec);
651                 goto bad;
652         }
653
654         return 0;
655
656 bad:
657         ubi_err("bad EC header");
658         ubi_dbg_dump_ec_hdr(ec_hdr);
659         ubi_dbg_dump_stack();
660         return 1;
661 }
662
663 /**
664  * ubi_io_read_ec_hdr - read and check an erase counter header.
665  * @ubi: UBI device description object
666  * @pnum: physical eraseblock to read from
667  * @ec_hdr: a &struct ubi_ec_hdr object where to store the read erase counter
668  * header
669  * @verbose: be verbose if the header is corrupted or was not found
670  *
671  * This function reads erase counter header from physical eraseblock @pnum and
672  * stores it in @ec_hdr. This function also checks CRC checksum of the read
673  * erase counter header. The following codes may be returned:
674  *
675  * o %0 if the CRC checksum is correct and the header was successfully read;
676  * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected
677  *   and corrected by the flash driver; this is harmless but may indicate that
678  *   this eraseblock may become bad soon (but may be not);
679  * o %UBI_IO_BAD_EC_HDR if the erase counter header is corrupted (a CRC error);
680  * o %UBI_IO_PEB_EMPTY if the physical eraseblock is empty;
681  * o a negative error code in case of failure.
682  */
683 int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
684                        struct ubi_ec_hdr *ec_hdr, int verbose)
685 {
686         int err, read_err = 0;
687         uint32_t crc, magic, hdr_crc;
688
689         dbg_io("read EC header from PEB %d", pnum);
690         ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
691
692         err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE);
693         if (err) {
694                 if (err != UBI_IO_BITFLIPS && err != -EBADMSG)
695                         return err;
696
697                 /*
698                  * We read all the data, but either a correctable bit-flip
699                  * occurred, or MTD reported about some data integrity error,
700                  * like an ECC error in case of NAND. The former is harmless,
701                  * the later may mean that the read data is corrupted. But we
702                  * have a CRC check-sum and we will detect this. If the EC
703                  * header is still OK, we just report this as there was a
704                  * bit-flip.
705                  */
706                 read_err = err;
707         }
708
709         magic = be32_to_cpu(ec_hdr->magic);
710         if (magic != UBI_EC_HDR_MAGIC) {
711                 /*
712                  * The magic field is wrong. Let's check if we have read all
713                  * 0xFF. If yes, this physical eraseblock is assumed to be
714                  * empty.
715                  *
716                  * But if there was a read error, we do not test it for all
717                  * 0xFFs. Even if it does contain all 0xFFs, this error
718                  * indicates that something is still wrong with this physical
719                  * eraseblock and we anyway cannot treat it as empty.
720                  */
721                 if (read_err != -EBADMSG &&
722                     check_pattern(ec_hdr, 0xFF, UBI_EC_HDR_SIZE)) {
723                         /* The physical eraseblock is supposedly empty */
724                         if (verbose)
725                                 ubi_warn("no EC header found at PEB %d, "
726                                          "only 0xFF bytes", pnum);
727                         else if (UBI_IO_DEBUG)
728                                 dbg_msg("no EC header found at PEB %d, "
729                                         "only 0xFF bytes", pnum);
730                         return UBI_IO_PEB_EMPTY;
731                 }
732
733                 /*
734                  * This is not a valid erase counter header, and these are not
735                  * 0xFF bytes. Report that the header is corrupted.
736                  */
737                 if (verbose) {
738                         ubi_warn("bad magic number at PEB %d: %08x instead of "
739                                  "%08x", pnum, magic, UBI_EC_HDR_MAGIC);
740                         ubi_dbg_dump_ec_hdr(ec_hdr);
741                 } else if (UBI_IO_DEBUG)
742                         dbg_msg("bad magic number at PEB %d: %08x instead of "
743                                 "%08x", pnum, magic, UBI_EC_HDR_MAGIC);
744                 return UBI_IO_BAD_EC_HDR;
745         }
746
747         crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
748         hdr_crc = be32_to_cpu(ec_hdr->hdr_crc);
749
750         if (hdr_crc != crc) {
751                 if (verbose) {
752                         ubi_warn("bad EC header CRC at PEB %d, calculated "
753                                  "%#08x, read %#08x", pnum, crc, hdr_crc);
754                         ubi_dbg_dump_ec_hdr(ec_hdr);
755                 } else if (UBI_IO_DEBUG)
756                         dbg_msg("bad EC header CRC at PEB %d, calculated "
757                                 "%#08x, read %#08x", pnum, crc, hdr_crc);
758                 return UBI_IO_BAD_EC_HDR;
759         }
760
761         /* And of course validate what has just been read from the media */
762         err = validate_ec_hdr(ubi, ec_hdr);
763         if (err) {
764                 ubi_err("validation failed for PEB %d", pnum);
765                 return -EINVAL;
766         }
767
768         return read_err ? UBI_IO_BITFLIPS : 0;
769 }
770
771 /**
772  * ubi_io_write_ec_hdr - write an erase counter header.
773  * @ubi: UBI device description object
774  * @pnum: physical eraseblock to write to
775  * @ec_hdr: the erase counter header to write
776  *
777  * This function writes erase counter header described by @ec_hdr to physical
778  * eraseblock @pnum. It also fills most fields of @ec_hdr before writing, so
779  * the caller do not have to fill them. Callers must only fill the @ec_hdr->ec
780  * field.
781  *
782  * This function returns zero in case of success and a negative error code in
783  * case of failure. If %-EIO is returned, the physical eraseblock most probably
784  * went bad.
785  */
786 int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum,
787                         struct ubi_ec_hdr *ec_hdr)
788 {
789         int err;
790         uint32_t crc;
791
792         dbg_io("write EC header to PEB %d", pnum);
793         ubi_assert(pnum >= 0 &&  pnum < ubi->peb_count);
794
795         ec_hdr->magic = cpu_to_be32(UBI_EC_HDR_MAGIC);
796         ec_hdr->version = UBI_VERSION;
797         ec_hdr->vid_hdr_offset = cpu_to_be32(ubi->vid_hdr_offset);
798         ec_hdr->data_offset = cpu_to_be32(ubi->leb_start);
799         ec_hdr->image_seq = cpu_to_be32(ubi->image_seq);
800         crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
801         ec_hdr->hdr_crc = cpu_to_be32(crc);
802
803         err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr);
804         if (err)
805                 return -EINVAL;
806
807         err = ubi_io_write(ubi, ec_hdr, pnum, 0, ubi->ec_hdr_alsize);
808         return err;
809 }
810
811 /**
812  * validate_vid_hdr - validate a volume identifier header.
813  * @ubi: UBI device description object
814  * @vid_hdr: the volume identifier header to check
815  *
816  * This function checks that data stored in the volume identifier header
817  * @vid_hdr. Returns zero if the VID header is OK and %1 if not.
818  */
819 static int validate_vid_hdr(const struct ubi_device *ubi,
820                             const struct ubi_vid_hdr *vid_hdr)
821 {
822         int vol_type = vid_hdr->vol_type;
823         int copy_flag = vid_hdr->copy_flag;
824         int vol_id = be32_to_cpu(vid_hdr->vol_id);
825         int lnum = be32_to_cpu(vid_hdr->lnum);
826         int compat = vid_hdr->compat;
827         int data_size = be32_to_cpu(vid_hdr->data_size);
828         int used_ebs = be32_to_cpu(vid_hdr->used_ebs);
829         int data_pad = be32_to_cpu(vid_hdr->data_pad);
830         int data_crc = be32_to_cpu(vid_hdr->data_crc);
831         int usable_leb_size = ubi->leb_size - data_pad;
832
833         if (copy_flag != 0 && copy_flag != 1) {
834                 dbg_err("bad copy_flag");
835                 goto bad;
836         }
837
838         if (vol_id < 0 || lnum < 0 || data_size < 0 || used_ebs < 0 ||
839             data_pad < 0) {
840                 dbg_err("negative values");
841                 goto bad;
842         }
843
844         if (vol_id >= UBI_MAX_VOLUMES && vol_id < UBI_INTERNAL_VOL_START) {
845                 dbg_err("bad vol_id");
846                 goto bad;
847         }
848
849         if (vol_id < UBI_INTERNAL_VOL_START && compat != 0) {
850                 dbg_err("bad compat");
851                 goto bad;
852         }
853
854         if (vol_id >= UBI_INTERNAL_VOL_START && compat != UBI_COMPAT_DELETE &&
855             compat != UBI_COMPAT_RO && compat != UBI_COMPAT_PRESERVE &&
856             compat != UBI_COMPAT_REJECT) {
857                 dbg_err("bad compat");
858                 goto bad;
859         }
860
861         if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) {
862                 dbg_err("bad vol_type");
863                 goto bad;
864         }
865
866         if (data_pad >= ubi->leb_size / 2) {
867                 dbg_err("bad data_pad");
868                 goto bad;
869         }
870
871         if (vol_type == UBI_VID_STATIC) {
872                 /*
873                  * Although from high-level point of view static volumes may
874                  * contain zero bytes of data, but no VID headers can contain
875                  * zero at these fields, because they empty volumes do not have
876                  * mapped logical eraseblocks.
877                  */
878                 if (used_ebs == 0) {
879                         dbg_err("zero used_ebs");
880                         goto bad;
881                 }
882                 if (data_size == 0) {
883                         dbg_err("zero data_size");
884                         goto bad;
885                 }
886                 if (lnum < used_ebs - 1) {
887                         if (data_size != usable_leb_size) {
888                                 dbg_err("bad data_size");
889                                 goto bad;
890                         }
891                 } else if (lnum == used_ebs - 1) {
892                         if (data_size == 0) {
893                                 dbg_err("bad data_size at last LEB");
894                                 goto bad;
895                         }
896                 } else {
897                         dbg_err("too high lnum");
898                         goto bad;
899                 }
900         } else {
901                 if (copy_flag == 0) {
902                         if (data_crc != 0) {
903                                 dbg_err("non-zero data CRC");
904                                 goto bad;
905                         }
906                         if (data_size != 0) {
907                                 dbg_err("non-zero data_size");
908                                 goto bad;
909                         }
910                 } else {
911                         if (data_size == 0) {
912                                 dbg_err("zero data_size of copy");
913                                 goto bad;
914                         }
915                 }
916                 if (used_ebs != 0) {
917                         dbg_err("bad used_ebs");
918                         goto bad;
919                 }
920         }
921
922         return 0;
923
924 bad:
925         ubi_err("bad VID header");
926         ubi_dbg_dump_vid_hdr(vid_hdr);
927         ubi_dbg_dump_stack();
928         return 1;
929 }
930
931 /**
932  * ubi_io_read_vid_hdr - read and check a volume identifier header.
933  * @ubi: UBI device description object
934  * @pnum: physical eraseblock number to read from
935  * @vid_hdr: &struct ubi_vid_hdr object where to store the read volume
936  * identifier header
937  * @verbose: be verbose if the header is corrupted or wasn't found
938  *
939  * This function reads the volume identifier header from physical eraseblock
940  * @pnum and stores it in @vid_hdr. It also checks CRC checksum of the read
941  * volume identifier header. The following codes may be returned:
942  *
943  * o %0 if the CRC checksum is correct and the header was successfully read;
944  * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected
945  *   and corrected by the flash driver; this is harmless but may indicate that
946  *   this eraseblock may become bad soon;
947  * o %UBI_IO_BAD_VID_HDR if the volume identifier header is corrupted (a CRC
948  *   error detected);
949  * o %UBI_IO_PEB_FREE if the physical eraseblock is free (i.e., there is no VID
950  *   header there);
951  * o a negative error code in case of failure.
952  */
953 int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
954                         struct ubi_vid_hdr *vid_hdr, int verbose)
955 {
956         int err, read_err = 0;
957         uint32_t crc, magic, hdr_crc;
958         void *p;
959
960         dbg_io("read VID header from PEB %d", pnum);
961         ubi_assert(pnum >= 0 &&  pnum < ubi->peb_count);
962
963         p = (char *)vid_hdr - ubi->vid_hdr_shift;
964         err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset,
965                           ubi->vid_hdr_alsize);
966         if (err) {
967                 if (err != UBI_IO_BITFLIPS && err != -EBADMSG)
968                         return err;
969
970                 /*
971                  * We read all the data, but either a correctable bit-flip
972                  * occurred, or MTD reported about some data integrity error,
973                  * like an ECC error in case of NAND. The former is harmless,
974                  * the later may mean the read data is corrupted. But we have a
975                  * CRC check-sum and we will identify this. If the VID header is
976                  * still OK, we just report this as there was a bit-flip.
977                  */
978                 read_err = err;
979         }
980
981         magic = be32_to_cpu(vid_hdr->magic);
982         if (magic != UBI_VID_HDR_MAGIC) {
983                 /*
984                  * If we have read all 0xFF bytes, the VID header probably does
985                  * not exist and the physical eraseblock is assumed to be free.
986                  *
987                  * But if there was a read error, we do not test the data for
988                  * 0xFFs. Even if it does contain all 0xFFs, this error
989                  * indicates that something is still wrong with this physical
990                  * eraseblock and it cannot be regarded as free.
991                  */
992                 if (read_err != -EBADMSG &&
993                     check_pattern(vid_hdr, 0xFF, UBI_VID_HDR_SIZE)) {
994                         /* The physical eraseblock is supposedly free */
995                         if (verbose)
996                                 ubi_warn("no VID header found at PEB %d, "
997                                          "only 0xFF bytes", pnum);
998                         else if (UBI_IO_DEBUG)
999                                 dbg_msg("no VID header found at PEB %d, "
1000                                         "only 0xFF bytes", pnum);
1001                         return UBI_IO_PEB_FREE;
1002                 }
1003
1004                 /*
1005                  * This is not a valid VID header, and these are not 0xFF
1006                  * bytes. Report that the header is corrupted.
1007                  */
1008                 if (verbose) {
1009                         ubi_warn("bad magic number at PEB %d: %08x instead of "
1010                                  "%08x", pnum, magic, UBI_VID_HDR_MAGIC);
1011                         ubi_dbg_dump_vid_hdr(vid_hdr);
1012                 } else if (UBI_IO_DEBUG)
1013                         dbg_msg("bad magic number at PEB %d: %08x instead of "
1014                                 "%08x", pnum, magic, UBI_VID_HDR_MAGIC);
1015                 return UBI_IO_BAD_VID_HDR;
1016         }
1017
1018         crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC);
1019         hdr_crc = be32_to_cpu(vid_hdr->hdr_crc);
1020
1021         if (hdr_crc != crc) {
1022                 if (verbose) {
1023                         ubi_warn("bad CRC at PEB %d, calculated %#08x, "
1024                                  "read %#08x", pnum, crc, hdr_crc);
1025                         ubi_dbg_dump_vid_hdr(vid_hdr);
1026                 } else if (UBI_IO_DEBUG)
1027                         dbg_msg("bad CRC at PEB %d, calculated %#08x, "
1028                                 "read %#08x", pnum, crc, hdr_crc);
1029                 return UBI_IO_BAD_VID_HDR;
1030         }
1031
1032         /* Validate the VID header that we have just read */
1033         err = validate_vid_hdr(ubi, vid_hdr);
1034         if (err) {
1035                 ubi_err("validation failed for PEB %d", pnum);
1036                 return -EINVAL;
1037         }
1038
1039         return read_err ? UBI_IO_BITFLIPS : 0;
1040 }
1041
1042 /**
1043  * ubi_io_write_vid_hdr - write a volume identifier header.
1044  * @ubi: UBI device description object
1045  * @pnum: the physical eraseblock number to write to
1046  * @vid_hdr: the volume identifier header to write
1047  *
1048  * This function writes the volume identifier header described by @vid_hdr to
1049  * physical eraseblock @pnum. This function automatically fills the
1050  * @vid_hdr->magic and the @vid_hdr->version fields, as well as calculates
1051  * header CRC checksum and stores it at vid_hdr->hdr_crc.
1052  *
1053  * This function returns zero in case of success and a negative error code in
1054  * case of failure. If %-EIO is returned, the physical eraseblock probably went
1055  * bad.
1056  */
1057 int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
1058                          struct ubi_vid_hdr *vid_hdr)
1059 {
1060         int err;
1061         uint32_t crc;
1062         void *p;
1063
1064         dbg_io("write VID header to PEB %d", pnum);
1065         ubi_assert(pnum >= 0 &&  pnum < ubi->peb_count);
1066
1067         err = paranoid_check_peb_ec_hdr(ubi, pnum);
1068         if (err)
1069                 return err > 0 ? -EINVAL : err;
1070
1071         vid_hdr->magic = cpu_to_be32(UBI_VID_HDR_MAGIC);
1072         vid_hdr->version = UBI_VERSION;
1073         crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC);
1074         vid_hdr->hdr_crc = cpu_to_be32(crc);
1075
1076         err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr);
1077         if (err)
1078                 return -EINVAL;
1079
1080         p = (char *)vid_hdr - ubi->vid_hdr_shift;
1081         err = ubi_io_write(ubi, p, pnum, ubi->vid_hdr_aloffset,
1082                            ubi->vid_hdr_alsize);
1083         return err;
1084 }
1085
1086 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
1087
1088 /**
1089  * paranoid_check_not_bad - ensure that a physical eraseblock is not bad.
1090  * @ubi: UBI device description object
1091  * @pnum: physical eraseblock number to check
1092  *
1093  * This function returns zero if the physical eraseblock is good, a positive
1094  * number if it is bad and a negative error code if an error occurred.
1095  */
1096 static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum)
1097 {
1098         int err;
1099
1100         err = ubi_io_is_bad(ubi, pnum);
1101         if (!err)
1102                 return err;
1103
1104         ubi_err("paranoid check failed for PEB %d", pnum);
1105         ubi_dbg_dump_stack();
1106         return err;
1107 }
1108
1109 /**
1110  * paranoid_check_ec_hdr - check if an erase counter header is all right.
1111  * @ubi: UBI device description object
1112  * @pnum: physical eraseblock number the erase counter header belongs to
1113  * @ec_hdr: the erase counter header to check
1114  *
1115  * This function returns zero if the erase counter header contains valid
1116  * values, and %1 if not.
1117  */
1118 static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum,
1119                                  const struct ubi_ec_hdr *ec_hdr)
1120 {
1121         int err;
1122         uint32_t magic;
1123
1124         magic = be32_to_cpu(ec_hdr->magic);
1125         if (magic != UBI_EC_HDR_MAGIC) {
1126                 ubi_err("bad magic %#08x, must be %#08x",
1127                         magic, UBI_EC_HDR_MAGIC);
1128                 goto fail;
1129         }
1130
1131         err = validate_ec_hdr(ubi, ec_hdr);
1132         if (err) {
1133                 ubi_err("paranoid check failed for PEB %d", pnum);
1134                 goto fail;
1135         }
1136
1137         return 0;
1138
1139 fail:
1140         ubi_dbg_dump_ec_hdr(ec_hdr);
1141         ubi_dbg_dump_stack();
1142         return 1;
1143 }
1144
1145 /**
1146  * paranoid_check_peb_ec_hdr - check erase counter header.
1147  * @ubi: UBI device description object
1148  * @pnum: the physical eraseblock number to check
1149  *
1150  * This function returns zero if the erase counter header is all right, %1 if
1151  * not, and a negative error code if an error occurred.
1152  */
1153 static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum)
1154 {
1155         int err;
1156         uint32_t crc, hdr_crc;
1157         struct ubi_ec_hdr *ec_hdr;
1158
1159         ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
1160         if (!ec_hdr)
1161                 return -ENOMEM;
1162
1163         err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE);
1164         if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG)
1165                 goto exit;
1166
1167         crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
1168         hdr_crc = be32_to_cpu(ec_hdr->hdr_crc);
1169         if (hdr_crc != crc) {
1170                 ubi_err("bad CRC, calculated %#08x, read %#08x", crc, hdr_crc);
1171                 ubi_err("paranoid check failed for PEB %d", pnum);
1172                 ubi_dbg_dump_ec_hdr(ec_hdr);
1173                 ubi_dbg_dump_stack();
1174                 err = 1;
1175                 goto exit;
1176         }
1177
1178         err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr);
1179
1180 exit:
1181         kfree(ec_hdr);
1182         return err;
1183 }
1184
1185 /**
1186  * paranoid_check_vid_hdr - check that a volume identifier header is all right.
1187  * @ubi: UBI device description object
1188  * @pnum: physical eraseblock number the volume identifier header belongs to
1189  * @vid_hdr: the volume identifier header to check
1190  *
1191  * This function returns zero if the volume identifier header is all right, and
1192  * %1 if not.
1193  */
1194 static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum,
1195                                   const struct ubi_vid_hdr *vid_hdr)
1196 {
1197         int err;
1198         uint32_t magic;
1199
1200         magic = be32_to_cpu(vid_hdr->magic);
1201         if (magic != UBI_VID_HDR_MAGIC) {
1202                 ubi_err("bad VID header magic %#08x at PEB %d, must be %#08x",
1203                         magic, pnum, UBI_VID_HDR_MAGIC);
1204                 goto fail;
1205         }
1206
1207         err = validate_vid_hdr(ubi, vid_hdr);
1208         if (err) {
1209                 ubi_err("paranoid check failed for PEB %d", pnum);
1210                 goto fail;
1211         }
1212
1213         return err;
1214
1215 fail:
1216         ubi_err("paranoid check failed for PEB %d", pnum);
1217         ubi_dbg_dump_vid_hdr(vid_hdr);
1218         ubi_dbg_dump_stack();
1219         return 1;
1220
1221 }
1222
1223 /**
1224  * paranoid_check_peb_vid_hdr - check volume identifier header.
1225  * @ubi: UBI device description object
1226  * @pnum: the physical eraseblock number to check
1227  *
1228  * This function returns zero if the volume identifier header is all right,
1229  * %1 if not, and a negative error code if an error occurred.
1230  */
1231 static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum)
1232 {
1233         int err;
1234         uint32_t crc, hdr_crc;
1235         struct ubi_vid_hdr *vid_hdr;
1236         void *p;
1237
1238         vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
1239         if (!vid_hdr)
1240                 return -ENOMEM;
1241
1242         p = (char *)vid_hdr - ubi->vid_hdr_shift;
1243         err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset,
1244                           ubi->vid_hdr_alsize);
1245         if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG)
1246                 goto exit;
1247
1248         crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_EC_HDR_SIZE_CRC);
1249         hdr_crc = be32_to_cpu(vid_hdr->hdr_crc);
1250         if (hdr_crc != crc) {
1251                 ubi_err("bad VID header CRC at PEB %d, calculated %#08x, "
1252                         "read %#08x", pnum, crc, hdr_crc);
1253                 ubi_err("paranoid check failed for PEB %d", pnum);
1254                 ubi_dbg_dump_vid_hdr(vid_hdr);
1255                 ubi_dbg_dump_stack();
1256                 err = 1;
1257                 goto exit;
1258         }
1259
1260         err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr);
1261
1262 exit:
1263         ubi_free_vid_hdr(ubi, vid_hdr);
1264         return err;
1265 }
1266
1267 /**
1268  * ubi_dbg_check_all_ff - check that a region of flash is empty.
1269  * @ubi: UBI device description object
1270  * @pnum: the physical eraseblock number to check
1271  * @offset: the starting offset within the physical eraseblock to check
1272  * @len: the length of the region to check
1273  *
1274  * This function returns zero if only 0xFF bytes are present at offset
1275  * @offset of the physical eraseblock @pnum, %1 if not, and a negative error
1276  * code if an error occurred.
1277  */
1278 int ubi_dbg_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len)
1279 {
1280         size_t read;
1281         int err;
1282         loff_t addr = (loff_t)pnum * ubi->peb_size + offset;
1283
1284         mutex_lock(&ubi->dbg_buf_mutex);
1285         err = ubi->mtd->read(ubi->mtd, addr, len, &read, ubi->dbg_peb_buf);
1286         if (err && err != -EUCLEAN) {
1287                 ubi_err("error %d while reading %d bytes from PEB %d:%d, "
1288                         "read %zd bytes", err, len, pnum, offset, read);
1289                 goto error;
1290         }
1291
1292         err = check_pattern(ubi->dbg_peb_buf, 0xFF, len);
1293         if (err == 0) {
1294                 ubi_err("flash region at PEB %d:%d, length %d does not "
1295                         "contain all 0xFF bytes", pnum, offset, len);
1296                 goto fail;
1297         }
1298         mutex_unlock(&ubi->dbg_buf_mutex);
1299
1300         return 0;
1301
1302 fail:
1303         ubi_err("paranoid check failed for PEB %d", pnum);
1304         ubi_msg("hex dump of the %d-%d region", offset, offset + len);
1305         print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
1306                        ubi->dbg_peb_buf, len, 1);
1307         err = 1;
1308 error:
1309         ubi_dbg_dump_stack();
1310         mutex_unlock(&ubi->dbg_buf_mutex);
1311         return err;
1312 }
1313
1314 #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */