2 * Copyright (c) International Business Machines Corp., 2006
3 * Copyright (c) Nokia Corporation, 2006, 2007
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.
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.
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
19 * Author: Artem Bityutskiy (Битюцкий Артём)
23 * UBI input/output sub-system.
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.
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.
33 * Some words about how the eraseblock headers are stored.
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
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.
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
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
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?
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.
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.
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.
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.
89 #include <linux/crc32.h>
90 #include <linux/err.h>
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
);
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
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
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
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.
131 int ubi_io_read(const struct ubi_device
*ubi
, void *buf
, int pnum
, int offset
,
134 int err
, retries
= 0;
138 dbg_io("read %d bytes from PEB %d:%d", len
, pnum
, offset
);
140 ubi_assert(pnum
>= 0 && pnum
< ubi
->peb_count
);
141 ubi_assert(offset
>= 0 && offset
+ len
<= ubi
->peb_size
);
144 err
= paranoid_check_not_bad(ubi
, pnum
);
146 return err
> 0 ? -EINVAL
: err
;
148 addr
= (loff_t
)pnum
* ubi
->peb_size
+ offset
;
150 err
= ubi
->mtd
->read(ubi
->mtd
, addr
, len
, &read
, buf
);
152 if (err
== -EUCLEAN
) {
154 * -EUCLEAN is reported if there was a bit-flip which
155 * was corrected, so this is harmless.
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.
161 dbg_msg("fixable bit-flip detected at PEB %d", pnum
);
162 ubi_assert(len
== read
);
163 return UBI_IO_BITFLIPS
;
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
);
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();
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.
183 if (read
!= len
&& err
== -EBADMSG
) {
188 ubi_assert(len
== read
);
190 if (ubi_dbg_is_bitflip()) {
191 dbg_gen("bit-flip (emulated)");
192 err
= UBI_IO_BITFLIPS
;
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
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
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.
216 int ubi_io_write(struct ubi_device
*ubi
, const void *buf
, int pnum
, int offset
,
223 dbg_io("write %d bytes to PEB %d:%d", len
, pnum
, offset
);
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);
231 ubi_err("read-only mode");
235 /* The below has to be compiled out if paranoid checks are disabled */
237 err
= paranoid_check_not_bad(ubi
, pnum
);
239 return err
> 0 ? -EINVAL
: err
;
241 /* The area we are writing to has to contain all 0xFF bytes */
242 err
= ubi_dbg_check_all_ff(ubi
, pnum
, offset
, len
);
244 return err
> 0 ? -EINVAL
: err
;
246 if (offset
>= ubi
->leb_start
) {
248 * We write to the data area of the physical eraseblock. Make
249 * sure it has valid EC and VID headers.
251 err
= paranoid_check_peb_ec_hdr(ubi
, pnum
);
253 return err
> 0 ? -EINVAL
: err
;
254 err
= paranoid_check_peb_vid_hdr(ubi
, pnum
);
256 return err
> 0 ? -EINVAL
: err
;
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();
266 addr
= (loff_t
)pnum
* ubi
->peb_size
+ offset
;
267 err
= ubi
->mtd
->write(ubi
->mtd
, addr
, len
, &written
, buf
);
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();
273 ubi_assert(written
== len
);
279 * erase_callback - MTD erasure call-back.
280 * @ei: MTD erase information object.
282 * Note, even though MTD erase interface is asynchronous, all the current
283 * implementations are synchronous anyway.
285 static void erase_callback(struct erase_info
*ei
)
287 wake_up_interruptible((wait_queue_head_t
*)ei
->priv
);
291 * do_sync_erase - synchronously erase a physical eraseblock.
292 * @ubi: UBI device description object
293 * @pnum: the physical eraseblock number to erase
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.
299 static int do_sync_erase(struct ubi_device
*ubi
, int pnum
)
301 int err
, retries
= 0;
302 struct erase_info ei
;
303 wait_queue_head_t wq
;
305 dbg_io("erase PEB %d", pnum
);
308 init_waitqueue_head(&wq
);
309 memset(&ei
, 0, sizeof(struct erase_info
));
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
;
317 err
= ubi
->mtd
->erase(ubi
->mtd
, &ei
);
319 if (retries
++ < UBI_IO_RETRIES
) {
320 dbg_io("error %d while erasing PEB %d, retry",
325 ubi_err("cannot erase PEB %d, error %d", pnum
, err
);
326 ubi_dbg_dump_stack();
330 err
= wait_event_interruptible(wq
, ei
.state
== MTD_ERASE_DONE
||
331 ei
.state
== MTD_ERASE_FAILED
);
333 ubi_err("interrupted PEB %d erasure", pnum
);
337 if (ei
.state
== MTD_ERASE_FAILED
) {
338 if (retries
++ < UBI_IO_RETRIES
) {
339 dbg_io("error while erasing PEB %d, retry", pnum
);
343 ubi_err("cannot erase PEB %d", pnum
);
344 ubi_dbg_dump_stack();
348 err
= ubi_dbg_check_all_ff(ubi
, pnum
, 0, ubi
->peb_size
);
350 return err
> 0 ? -EINVAL
: err
;
352 if (ubi_dbg_is_erase_failure() && !err
) {
353 dbg_err("cannot erase PEB %d (emulated)", pnum
);
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
366 * This function returns %1 in there are only @patt bytes in @buf, and %0 if
367 * something else was also found.
369 static int check_pattern(const void *buf
, uint8_t patt
, int size
)
373 for (i
= 0; i
< size
; i
++)
374 if (((const uint8_t *)buf
)[i
] != patt
)
379 /* Patterns to write to a physical eraseblock when torturing it */
380 static uint8_t patterns
[] = {0xa5, 0x5a, 0x0};
383 * torture_peb - test a supposedly bad physical eraseblock.
384 * @ubi: UBI device description object
385 * @pnum: the physical eraseblock number to test
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.
391 static int torture_peb(struct ubi_device
*ubi
, int pnum
)
393 int err
, i
, patt_count
;
395 ubi_msg("run torture test for PEB %d", pnum
);
396 patt_count
= ARRAY_SIZE(patterns
);
397 ubi_assert(patt_count
> 0);
399 mutex_lock(&ubi
->buf_mutex
);
400 for (i
= 0; i
< patt_count
; i
++) {
401 err
= do_sync_erase(ubi
, pnum
);
405 /* Make sure the PEB contains only 0xFF bytes */
406 err
= ubi_io_read(ubi
, ubi
->peb_buf1
, pnum
, 0, ubi
->peb_size
);
410 err
= check_pattern(ubi
->peb_buf1
, 0xFF, ubi
->peb_size
);
412 ubi_err("erased PEB %d, but a non-0xFF byte found",
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
);
424 memset(ubi
->peb_buf1
, ~patterns
[i
], ubi
->peb_size
);
425 err
= ubi_io_read(ubi
, ubi
->peb_buf1
, pnum
, 0, ubi
->peb_size
);
429 err
= check_pattern(ubi
->peb_buf1
, patterns
[i
], ubi
->peb_size
);
431 ubi_err("pattern %x checking failed for PEB %d",
439 ubi_msg("PEB %d passed torture test, do not mark it a bad", pnum
);
442 mutex_unlock(&ubi
->buf_mutex
);
443 if (err
== UBI_IO_BITFLIPS
|| err
== -EBADMSG
) {
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.
449 ubi_err("read problems on freshly erased PEB %d, must be bad",
457 * nor_erase_prepare - prepare a NOR flash PEB for erasure.
458 * @ubi: UBI device description object
459 * @pnum: physical eraseblock number to prepare
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.
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).
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.
476 static int nor_erase_prepare(struct ubi_device
*ubi
, int pnum
)
483 addr
= (loff_t
)pnum
* ubi
->peb_size
;
484 err
= ubi
->mtd
->write(ubi
->mtd
, addr
, 4, &written
, (void *)&data
);
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();
492 addr
+= ubi
->vid_hdr_aloffset
;
493 err
= ubi
->mtd
->write(ubi
->mtd
, addr
, 4, &written
, (void *)&data
);
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();
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
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.
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
520 int ubi_io_sync_erase(struct ubi_device
*ubi
, int pnum
, int torture
)
524 ubi_assert(pnum
>= 0 && pnum
< ubi
->peb_count
);
526 err
= paranoid_check_not_bad(ubi
, pnum
);
528 return err
> 0 ? -EINVAL
: err
;
531 ubi_err("read-only mode");
535 if (ubi
->nor_flash
) {
536 err
= nor_erase_prepare(ubi
, pnum
);
542 ret
= torture_peb(ubi
, pnum
);
547 err
= do_sync_erase(ubi
, pnum
);
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
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.
562 int ubi_io_is_bad(const struct ubi_device
*ubi
, int pnum
)
564 struct mtd_info
*mtd
= ubi
->mtd
;
566 ubi_assert(pnum
>= 0 && pnum
< ubi
->peb_count
);
568 if (ubi
->bad_allowed
) {
571 ret
= mtd
->block_isbad(mtd
, (loff_t
)pnum
* ubi
->peb_size
);
573 ubi_err("error %d while checking if PEB %d is bad",
576 dbg_io("PEB %d is bad", pnum
);
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
588 * This function returns zero in case of success and a negative error code in
591 int ubi_io_mark_bad(const struct ubi_device
*ubi
, int pnum
)
594 struct mtd_info
*mtd
= ubi
->mtd
;
596 ubi_assert(pnum
>= 0 && pnum
< ubi
->peb_count
);
599 ubi_err("read-only mode");
603 if (!ubi
->bad_allowed
)
606 err
= mtd
->block_markbad(mtd
, (loff_t
)pnum
* ubi
->peb_size
);
608 ubi_err("cannot mark PEB %d bad, error %d", pnum
, err
);
613 * validate_ec_hdr - validate an erase counter header.
614 * @ubi: UBI device description object
615 * @ec_hdr: the erase counter header to check
617 * This function returns zero if the erase counter header is OK, and %1 if
620 static int validate_ec_hdr(const struct ubi_device
*ubi
,
621 const struct ubi_ec_hdr
*ec_hdr
)
624 int vid_hdr_offset
, leb_start
;
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
);
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
);
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
);
643 if (leb_start
!= ubi
->leb_start
) {
644 ubi_err("bad data offset %d, expected %d",
645 leb_start
, ubi
->leb_start
);
649 if (ec
< 0 || ec
> UBI_MAX_ERASECOUNTER
) {
650 ubi_err("bad erase counter %lld", ec
);
657 ubi_err("bad EC header");
658 ubi_dbg_dump_ec_hdr(ec_hdr
);
659 ubi_dbg_dump_stack();
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
669 * @verbose: be verbose if the header is corrupted or was not found
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:
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.
683 int ubi_io_read_ec_hdr(struct ubi_device
*ubi
, int pnum
,
684 struct ubi_ec_hdr
*ec_hdr
, int verbose
)
686 int err
, read_err
= 0;
687 uint32_t crc
, magic
, hdr_crc
;
689 dbg_io("read EC header from PEB %d", pnum
);
690 ubi_assert(pnum
>= 0 && pnum
< ubi
->peb_count
);
692 err
= ubi_io_read(ubi
, ec_hdr
, pnum
, 0, UBI_EC_HDR_SIZE
);
694 if (err
!= UBI_IO_BITFLIPS
&& err
!= -EBADMSG
)
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
709 magic
= be32_to_cpu(ec_hdr
->magic
);
710 if (magic
!= UBI_EC_HDR_MAGIC
) {
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
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.
721 if (read_err
!= -EBADMSG
&&
722 check_pattern(ec_hdr
, 0xFF, UBI_EC_HDR_SIZE
)) {
723 /* The physical eraseblock is supposedly empty */
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
;
734 * This is not a valid erase counter header, and these are not
735 * 0xFF bytes. Report that the header is corrupted.
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
;
747 crc
= crc32(UBI_CRC32_INIT
, ec_hdr
, UBI_EC_HDR_SIZE_CRC
);
748 hdr_crc
= be32_to_cpu(ec_hdr
->hdr_crc
);
750 if (hdr_crc
!= crc
) {
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
;
761 /* And of course validate what has just been read from the media */
762 err
= validate_ec_hdr(ubi
, ec_hdr
);
764 ubi_err("validation failed for PEB %d", pnum
);
768 return read_err
? UBI_IO_BITFLIPS
: 0;
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
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
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
786 int ubi_io_write_ec_hdr(struct ubi_device
*ubi
, int pnum
,
787 struct ubi_ec_hdr
*ec_hdr
)
792 dbg_io("write EC header to PEB %d", pnum
);
793 ubi_assert(pnum
>= 0 && pnum
< ubi
->peb_count
);
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
);
803 err
= paranoid_check_ec_hdr(ubi
, pnum
, ec_hdr
);
807 err
= ubi_io_write(ubi
, ec_hdr
, pnum
, 0, ubi
->ec_hdr_alsize
);
812 * validate_vid_hdr - validate a volume identifier header.
813 * @ubi: UBI device description object
814 * @vid_hdr: the volume identifier header to check
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.
819 static int validate_vid_hdr(const struct ubi_device
*ubi
,
820 const struct ubi_vid_hdr
*vid_hdr
)
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
;
833 if (copy_flag
!= 0 && copy_flag
!= 1) {
834 dbg_err("bad copy_flag");
838 if (vol_id
< 0 || lnum
< 0 || data_size
< 0 || used_ebs
< 0 ||
840 dbg_err("negative values");
844 if (vol_id
>= UBI_MAX_VOLUMES
&& vol_id
< UBI_INTERNAL_VOL_START
) {
845 dbg_err("bad vol_id");
849 if (vol_id
< UBI_INTERNAL_VOL_START
&& compat
!= 0) {
850 dbg_err("bad compat");
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");
861 if (vol_type
!= UBI_VID_DYNAMIC
&& vol_type
!= UBI_VID_STATIC
) {
862 dbg_err("bad vol_type");
866 if (data_pad
>= ubi
->leb_size
/ 2) {
867 dbg_err("bad data_pad");
871 if (vol_type
== UBI_VID_STATIC
) {
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.
879 dbg_err("zero used_ebs");
882 if (data_size
== 0) {
883 dbg_err("zero data_size");
886 if (lnum
< used_ebs
- 1) {
887 if (data_size
!= usable_leb_size
) {
888 dbg_err("bad data_size");
891 } else if (lnum
== used_ebs
- 1) {
892 if (data_size
== 0) {
893 dbg_err("bad data_size at last LEB");
897 dbg_err("too high lnum");
901 if (copy_flag
== 0) {
903 dbg_err("non-zero data CRC");
906 if (data_size
!= 0) {
907 dbg_err("non-zero data_size");
911 if (data_size
== 0) {
912 dbg_err("zero data_size of copy");
917 dbg_err("bad used_ebs");
925 ubi_err("bad VID header");
926 ubi_dbg_dump_vid_hdr(vid_hdr
);
927 ubi_dbg_dump_stack();
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
937 * @verbose: be verbose if the header is corrupted or wasn't found
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:
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
949 * o %UBI_IO_PEB_FREE if the physical eraseblock is free (i.e., there is no VID
951 * o a negative error code in case of failure.
953 int ubi_io_read_vid_hdr(struct ubi_device
*ubi
, int pnum
,
954 struct ubi_vid_hdr
*vid_hdr
, int verbose
)
956 int err
, read_err
= 0;
957 uint32_t crc
, magic
, hdr_crc
;
960 dbg_io("read VID header from PEB %d", pnum
);
961 ubi_assert(pnum
>= 0 && pnum
< ubi
->peb_count
);
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
);
967 if (err
!= UBI_IO_BITFLIPS
&& err
!= -EBADMSG
)
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.
981 magic
= be32_to_cpu(vid_hdr
->magic
);
982 if (magic
!= UBI_VID_HDR_MAGIC
) {
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.
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.
992 if (read_err
!= -EBADMSG
&&
993 check_pattern(vid_hdr
, 0xFF, UBI_VID_HDR_SIZE
)) {
994 /* The physical eraseblock is supposedly free */
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
;
1005 * This is not a valid VID header, and these are not 0xFF
1006 * bytes. Report that the header is corrupted.
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
;
1018 crc
= crc32(UBI_CRC32_INIT
, vid_hdr
, UBI_VID_HDR_SIZE_CRC
);
1019 hdr_crc
= be32_to_cpu(vid_hdr
->hdr_crc
);
1021 if (hdr_crc
!= crc
) {
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
;
1032 /* Validate the VID header that we have just read */
1033 err
= validate_vid_hdr(ubi
, vid_hdr
);
1035 ubi_err("validation failed for PEB %d", pnum
);
1039 return read_err
? UBI_IO_BITFLIPS
: 0;
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
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.
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
1057 int ubi_io_write_vid_hdr(struct ubi_device
*ubi
, int pnum
,
1058 struct ubi_vid_hdr
*vid_hdr
)
1064 dbg_io("write VID header to PEB %d", pnum
);
1065 ubi_assert(pnum
>= 0 && pnum
< ubi
->peb_count
);
1067 err
= paranoid_check_peb_ec_hdr(ubi
, pnum
);
1069 return err
> 0 ? -EINVAL
: err
;
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
);
1076 err
= paranoid_check_vid_hdr(ubi
, pnum
, vid_hdr
);
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
);
1086 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
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
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.
1096 static int paranoid_check_not_bad(const struct ubi_device
*ubi
, int pnum
)
1100 err
= ubi_io_is_bad(ubi
, pnum
);
1104 ubi_err("paranoid check failed for PEB %d", pnum
);
1105 ubi_dbg_dump_stack();
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
1115 * This function returns zero if the erase counter header contains valid
1116 * values, and %1 if not.
1118 static int paranoid_check_ec_hdr(const struct ubi_device
*ubi
, int pnum
,
1119 const struct ubi_ec_hdr
*ec_hdr
)
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
);
1131 err
= validate_ec_hdr(ubi
, ec_hdr
);
1133 ubi_err("paranoid check failed for PEB %d", pnum
);
1140 ubi_dbg_dump_ec_hdr(ec_hdr
);
1141 ubi_dbg_dump_stack();
1146 * paranoid_check_peb_ec_hdr - check erase counter header.
1147 * @ubi: UBI device description object
1148 * @pnum: the physical eraseblock number to check
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.
1153 static int paranoid_check_peb_ec_hdr(const struct ubi_device
*ubi
, int pnum
)
1156 uint32_t crc
, hdr_crc
;
1157 struct ubi_ec_hdr
*ec_hdr
;
1159 ec_hdr
= kzalloc(ubi
->ec_hdr_alsize
, GFP_NOFS
);
1163 err
= ubi_io_read(ubi
, ec_hdr
, pnum
, 0, UBI_EC_HDR_SIZE
);
1164 if (err
&& err
!= UBI_IO_BITFLIPS
&& err
!= -EBADMSG
)
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();
1178 err
= paranoid_check_ec_hdr(ubi
, pnum
, ec_hdr
);
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
1191 * This function returns zero if the volume identifier header is all right, and
1194 static int paranoid_check_vid_hdr(const struct ubi_device
*ubi
, int pnum
,
1195 const struct ubi_vid_hdr
*vid_hdr
)
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
);
1207 err
= validate_vid_hdr(ubi
, vid_hdr
);
1209 ubi_err("paranoid check failed for PEB %d", pnum
);
1216 ubi_err("paranoid check failed for PEB %d", pnum
);
1217 ubi_dbg_dump_vid_hdr(vid_hdr
);
1218 ubi_dbg_dump_stack();
1224 * paranoid_check_peb_vid_hdr - check volume identifier header.
1225 * @ubi: UBI device description object
1226 * @pnum: the physical eraseblock number to check
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.
1231 static int paranoid_check_peb_vid_hdr(const struct ubi_device
*ubi
, int pnum
)
1234 uint32_t crc
, hdr_crc
;
1235 struct ubi_vid_hdr
*vid_hdr
;
1238 vid_hdr
= ubi_zalloc_vid_hdr(ubi
, GFP_NOFS
);
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
)
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();
1260 err
= paranoid_check_vid_hdr(ubi
, pnum
, vid_hdr
);
1263 ubi_free_vid_hdr(ubi
, vid_hdr
);
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
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.
1278 int ubi_dbg_check_all_ff(struct ubi_device
*ubi
, int pnum
, int offset
, int len
)
1282 loff_t addr
= (loff_t
)pnum
* ubi
->peb_size
+ offset
;
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
);
1292 err
= check_pattern(ubi
->dbg_peb_buf
, 0xFF, len
);
1294 ubi_err("flash region at PEB %d:%d, length %d does not "
1295 "contain all 0xFF bytes", pnum
, offset
, len
);
1298 mutex_unlock(&ubi
->dbg_buf_mutex
);
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);
1309 ubi_dbg_dump_stack();
1310 mutex_unlock(&ubi
->dbg_buf_mutex
);
1314 #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */