Lines Matching +full:ubi +full:- +full:volume +full:-

1 // SPDX-License-Identifier: GPL-2.0-or-later
10 * UBI input/output sub-system.
12 * This sub-system provides a uniform way to work with all kinds of the
14 * writing UBI headers.
18 * sub-system validates every single header it reads from the flash media.
26 * changed. For example, for different reasons (e.g., optimization) UBI may be
28 * offset. Of course, if the offset of the VID header is unaligned, UBI adds
32 * About minimal I/O units. In general, UBI assumes flash device model where
35 * @ubi->mtd->writesize field. But as an exception, UBI admits use of another
40 * write operations to one NAND page. In this case UBI can fit EC and VID
41 * headers at one NAND page. Thus, UBI may use "sub-page" size as the minimal
42 * I/O unit for the headers (the @ubi->hdrs_min_io_size field). But it still
43 * reports NAND page size (@ubi->min_io_size) as a minimal I/O unit for the UBI
46 * Example: some Samsung NANDs with 2KiB pages allow 4x 512-byte writes, so
47 * although the minimal I/O unit is 2K, UBI uses 512 bytes for EC and VID
50 * Q: why not just to treat sub-page as a minimal I/O unit of this flash
51 * device, e.g., make @ubi->min_io_size = 512 in the example above?
53 * A: because when writing a sub-page, MTD still writes a full 2K page but the
54 * bytes which are not relevant to the sub-page are 0xFF. So, basically,
55 * writing 4x512 sub-pages is 4 times slower than writing one 2KiB NAND page.
56 * Thus, we prefer to use sub-pages only for EC and VID headers.
58 * As it was noted above, the VID header may start at a non-aligned offset.
59 * For example, in case of a 2KiB page NAND flash with a 512 bytes sub-page,
61 * last sub-page (EC header is always at offset zero). This causes some
64 * Suppose we have a 64-byte buffer and we read a VID header at it. We change
66 * 512-byte chunks, we have to allocate one more buffer and copy our VID header
69 * The I/O sub-system does the following trick in order to avoid this extra
70 * copy. It always allocates a @ubi->vid_hdr_alsize bytes buffer for the VID
71 * header and returns a pointer to offset @ubi->vid_hdr_shift of this buffer.
73 * back and writes the whole sub-page.
79 #include "ubi.h"
81 static int self_check_not_bad(const struct ubi_device *ubi, int pnum);
82 static int self_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum);
83 static int self_check_ec_hdr(const struct ubi_device *ubi, int pnum,
85 static int self_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum);
86 static int self_check_vid_hdr(const struct ubi_device *ubi, int pnum,
88 static int self_check_write(struct ubi_device *ubi, const void *buf, int pnum,
92 * ubi_io_read - read data from a physical eraseblock.
93 * @ubi: UBI device description object
105 * correctable bit-flips were detected; this is harmless but may indicate
107 * o %-EBADMSG if the MTD subsystem reported about data integrity problems, for
110 * o %-EIO if some I/O error occurred;
113 int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset, in ubi_io_read() argument
122 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); in ubi_io_read()
123 ubi_assert(offset >= 0 && offset + len <= ubi->peb_size); in ubi_io_read()
126 err = self_check_not_bad(ubi, pnum); in ubi_io_read()
135 * just do not read anything and return - the caller would not in ubi_io_read()
138 * 2. The driver is buggy and returns us success or -EBADMSG or in ubi_io_read()
139 * -EUCLEAN, but it does not actually put any data to the buffer. in ubi_io_read()
141 * This may confuse UBI or upper layers - they may think the buffer in ubi_io_read()
143 * especially possible because UBI (and UBIFS) relies on CRC, and in ubi_io_read()
152 addr = (loff_t)pnum * ubi->peb_size + offset; in ubi_io_read()
154 err = mtd_read(ubi->mtd, addr, len, &read, buf); in ubi_io_read()
160 * -EUCLEAN is reported if there was a bit-flip which in ubi_io_read()
167 ubi_msg(ubi, "fixable bit-flip detected at PEB %d", in ubi_io_read()
174 ubi_warn(ubi, "error %d%s while reading %d bytes from PEB %d:%d, read only %zd bytes, retry", in ubi_io_read()
180 ubi_err(ubi, "error %d%s while reading %d bytes from PEB %d:%d, read %zd bytes", in ubi_io_read()
185 * The driver should never return -EBADMSG if it failed to read in ubi_io_read()
187 * this, so we change it to -EIO. in ubi_io_read()
191 err = -EIO; in ubi_io_read()
196 if (ubi_dbg_is_bitflip(ubi)) { in ubi_io_read()
197 dbg_gen("bit-flip (emulated)"); in ubi_io_read()
201 if (ubi_dbg_is_read_failure(ubi, MASK_READ_FAILURE)) { in ubi_io_read()
202 ubi_warn(ubi, "cannot read %d bytes from PEB %d:%d (emulated)", in ubi_io_read()
204 return -EIO; in ubi_io_read()
207 if (ubi_dbg_is_eccerr(ubi)) { in ubi_io_read()
208 ubi_warn(ubi, "ECC error (emulated) while reading %d bytes from PEB %d:%d, read %zd bytes", in ubi_io_read()
210 return -EBADMSG; in ubi_io_read()
218 * ubi_io_write - write data to a physical eraseblock.
219 * @ubi: UBI device description object
228 * error code. If %-EIO is returned, the physical eraseblock most probably went
234 int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset, in ubi_io_write() argument
243 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); in ubi_io_write()
244 ubi_assert(offset >= 0 && offset + len <= ubi->peb_size); in ubi_io_write()
245 ubi_assert(offset % ubi->hdrs_min_io_size == 0); in ubi_io_write()
246 ubi_assert(len > 0 && len % ubi->hdrs_min_io_size == 0); in ubi_io_write()
248 if (ubi->ro_mode) { in ubi_io_write()
249 ubi_err(ubi, "read-only mode"); in ubi_io_write()
250 return -EROFS; in ubi_io_write()
253 err = self_check_not_bad(ubi, pnum); in ubi_io_write()
258 err = ubi_self_check_all_ff(ubi, pnum, offset, len); in ubi_io_write()
262 if (offset >= ubi->leb_start) { in ubi_io_write()
267 err = self_check_peb_ec_hdr(ubi, pnum); in ubi_io_write()
270 err = self_check_peb_vid_hdr(ubi, pnum); in ubi_io_write()
275 if (ubi_dbg_is_write_failure(ubi)) { in ubi_io_write()
276 ubi_err(ubi, "cannot write %d bytes to PEB %d:%d (emulated)", in ubi_io_write()
279 return -EIO; in ubi_io_write()
282 addr = (loff_t)pnum * ubi->peb_size + offset; in ubi_io_write()
283 err = mtd_write(ubi->mtd, addr, len, &written, buf); in ubi_io_write()
285 ubi_err(ubi, "error %d while writing %d bytes to PEB %d:%d, written %zd bytes", in ubi_io_write()
288 ubi_dump_flash(ubi, pnum, offset, len); in ubi_io_write()
293 err = self_check_write(ubi, buf, pnum, offset, len); in ubi_io_write()
302 len = ubi->peb_size - offset; in ubi_io_write()
304 err = ubi_self_check_all_ff(ubi, pnum, offset, len); in ubi_io_write()
311 * do_sync_erase - synchronously erase a physical eraseblock.
312 * @ubi: UBI device description object
317 * %-EIO is returned, the physical eraseblock most probably went bad.
319 static int do_sync_erase(struct ubi_device *ubi, int pnum) in do_sync_erase() argument
325 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); in do_sync_erase()
327 if (ubi->ro_mode) { in do_sync_erase()
328 ubi_err(ubi, "read-only mode"); in do_sync_erase()
329 return -EROFS; in do_sync_erase()
335 ei.addr = (loff_t)pnum * ubi->peb_size; in do_sync_erase()
336 ei.len = ubi->peb_size; in do_sync_erase()
338 err = mtd_erase(ubi->mtd, &ei); in do_sync_erase()
341 ubi_warn(ubi, "error %d while erasing PEB %d, retry", in do_sync_erase()
346 ubi_err(ubi, "cannot erase PEB %d, error %d", pnum, err); in do_sync_erase()
351 err = ubi_self_check_all_ff(ubi, pnum, 0, ubi->peb_size); in do_sync_erase()
355 if (ubi_dbg_is_erase_failure(ubi)) { in do_sync_erase()
356 ubi_err(ubi, "cannot erase PEB %d (emulated)", pnum); in do_sync_erase()
357 return -EIO; in do_sync_erase()
367 * torture_peb - test a supposedly bad physical eraseblock.
368 * @ubi: UBI device description object
371 * This function returns %-EIO if the physical eraseblock did not pass the
375 static int torture_peb(struct ubi_device *ubi, int pnum) in torture_peb() argument
379 ubi_msg(ubi, "run torture test for PEB %d", pnum); in torture_peb()
383 mutex_lock(&ubi->buf_mutex); in torture_peb()
385 err = do_sync_erase(ubi, pnum); in torture_peb()
390 err = ubi_io_read(ubi, ubi->peb_buf, pnum, 0, ubi->peb_size); in torture_peb()
394 err = ubi_check_pattern(ubi->peb_buf, 0xFF, ubi->peb_size); in torture_peb()
396 ubi_err(ubi, "erased PEB %d, but a non-0xFF byte found", in torture_peb()
398 err = -EIO; in torture_peb()
403 memset(ubi->peb_buf, patterns[i], ubi->peb_size); in torture_peb()
404 err = ubi_io_write(ubi, ubi->peb_buf, pnum, 0, ubi->peb_size); in torture_peb()
408 memset(ubi->peb_buf, ~patterns[i], ubi->peb_size); in torture_peb()
409 err = ubi_io_read(ubi, ubi->peb_buf, pnum, 0, ubi->peb_size); in torture_peb()
413 err = ubi_check_pattern(ubi->peb_buf, patterns[i], in torture_peb()
414 ubi->peb_size); in torture_peb()
416 ubi_err(ubi, "pattern %x checking failed for PEB %d", in torture_peb()
418 err = -EIO; in torture_peb()
424 ubi_msg(ubi, "PEB %d passed torture test, do not mark it as bad", pnum); in torture_peb()
427 mutex_unlock(&ubi->buf_mutex); in torture_peb()
430 * If a bit-flip or data integrity error was detected, the test in torture_peb()
434 ubi_err(ubi, "read problems on freshly erased PEB %d, must be bad", in torture_peb()
436 err = -EIO; in torture_peb()
442 * nor_erase_prepare - prepare a NOR flash PEB for erasure.
443 * @ubi: UBI device description object
454 * zeroed. This makes UBI mistakenly treat this PEB as used and associate it
461 static int nor_erase_prepare(struct ubi_device *ubi, int pnum) in nor_erase_prepare() argument
473 * comment in this file). But we know this is a NOR-specific piece of in nor_erase_prepare()
474 * code, so we can do this. But yes, this is error-prone and we should in nor_erase_prepare()
475 * (pre-)allocate VID header buffer instead. in nor_erase_prepare()
483 * invalid VID header, in which case UBI will treat this PEB as in nor_erase_prepare()
486 addr = (loff_t)pnum * ubi->peb_size; in nor_erase_prepare()
487 err = ubi_io_read_ec_hdr(ubi, pnum, &ec_hdr, 0); in nor_erase_prepare()
490 err = mtd_write(ubi->mtd, addr, 4, &written, (void *)&data); in nor_erase_prepare()
495 ubi_init_vid_buf(ubi, &vidb, &vid_hdr); in nor_erase_prepare()
498 err = ubi_io_read_vid_hdr(ubi, pnum, &vidb, 0); in nor_erase_prepare()
501 addr += ubi->vid_hdr_aloffset; in nor_erase_prepare()
502 err = mtd_write(ubi->mtd, addr, 4, &written, (void *)&data); in nor_erase_prepare()
514 ubi_err(ubi, "cannot invalidate PEB %d, write returned %d", pnum, err); in nor_erase_prepare()
515 ubi_dump_flash(ubi, pnum, 0, ubi->peb_size); in nor_erase_prepare()
516 return -EIO; in nor_erase_prepare()
520 * ubi_io_sync_erase - synchronously erase a physical eraseblock.
521 * @ubi: UBI device description object
530 * This function returns the number of erasures made in case of success, %-EIO
532 * codes in case of other errors. Note, %-EIO means that the physical
535 int ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture) in ubi_io_sync_erase() argument
539 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); in ubi_io_sync_erase()
541 err = self_check_not_bad(ubi, pnum); in ubi_io_sync_erase()
545 if (ubi->ro_mode) { in ubi_io_sync_erase()
546 ubi_err(ubi, "read-only mode"); in ubi_io_sync_erase()
547 return -EROFS; in ubi_io_sync_erase()
551 * If the flash is ECC-ed then we have to erase the ECC block before we in ubi_io_sync_erase()
557 if (ubi->nor_flash && ubi->mtd->writesize == 1) { in ubi_io_sync_erase()
558 err = nor_erase_prepare(ubi, pnum); in ubi_io_sync_erase()
564 ret = torture_peb(ubi, pnum); in ubi_io_sync_erase()
569 err = do_sync_erase(ubi, pnum); in ubi_io_sync_erase()
577 * ubi_io_is_bad - check if a physical eraseblock is bad.
578 * @ubi: UBI device description object
584 int ubi_io_is_bad(const struct ubi_device *ubi, int pnum) in ubi_io_is_bad() argument
586 struct mtd_info *mtd = ubi->mtd; in ubi_io_is_bad()
588 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); in ubi_io_is_bad()
590 if (ubi->bad_allowed) { in ubi_io_is_bad()
593 ret = mtd_block_isbad(mtd, (loff_t)pnum * ubi->peb_size); in ubi_io_is_bad()
595 ubi_err(ubi, "error %d while checking if PEB %d is bad", in ubi_io_is_bad()
606 * ubi_io_mark_bad - mark a physical eraseblock as bad.
607 * @ubi: UBI device description object
613 int ubi_io_mark_bad(const struct ubi_device *ubi, int pnum) in ubi_io_mark_bad() argument
616 struct mtd_info *mtd = ubi->mtd; in ubi_io_mark_bad()
618 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); in ubi_io_mark_bad()
620 if (ubi->ro_mode) { in ubi_io_mark_bad()
621 ubi_err(ubi, "read-only mode"); in ubi_io_mark_bad()
622 return -EROFS; in ubi_io_mark_bad()
625 if (!ubi->bad_allowed) in ubi_io_mark_bad()
628 err = mtd_block_markbad(mtd, (loff_t)pnum * ubi->peb_size); in ubi_io_mark_bad()
630 ubi_err(ubi, "cannot mark PEB %d bad, error %d", pnum, err); in ubi_io_mark_bad()
635 * validate_ec_hdr - validate an erase counter header.
636 * @ubi: UBI device description object
642 static int validate_ec_hdr(const struct ubi_device *ubi, in validate_ec_hdr() argument
648 ec = be64_to_cpu(ec_hdr->ec); in validate_ec_hdr()
649 vid_hdr_offset = be32_to_cpu(ec_hdr->vid_hdr_offset); in validate_ec_hdr()
650 leb_start = be32_to_cpu(ec_hdr->data_offset); in validate_ec_hdr()
652 if (ec_hdr->version != UBI_VERSION) { in validate_ec_hdr()
653 …ubi_err(ubi, "node with incompatible UBI version found: this UBI version is %d, image version is %… in validate_ec_hdr()
654 UBI_VERSION, (int)ec_hdr->version); in validate_ec_hdr()
658 if (vid_hdr_offset != ubi->vid_hdr_offset) { in validate_ec_hdr()
659 ubi_err(ubi, "bad VID header offset %d, expected %d", in validate_ec_hdr()
660 vid_hdr_offset, ubi->vid_hdr_offset); in validate_ec_hdr()
664 if (leb_start != ubi->leb_start) { in validate_ec_hdr()
665 ubi_err(ubi, "bad data offset %d, expected %d", in validate_ec_hdr()
666 leb_start, ubi->leb_start); in validate_ec_hdr()
671 ubi_err(ubi, "bad erase counter %lld", ec); in validate_ec_hdr()
678 ubi_err(ubi, "bad EC header"); in validate_ec_hdr()
685 * ubi_io_read_ec_hdr - read and check an erase counter header.
686 * @ubi: UBI device description object
697 * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected
706 int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum, in ubi_io_read_ec_hdr() argument
713 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); in ubi_io_read_ec_hdr()
715 read_err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE); in ubi_io_read_ec_hdr()
721 * We read all the data, but either a correctable bit-flip in ubi_io_read_ec_hdr()
725 * corrupted. But we have a CRC check-sum and we will detect in ubi_io_read_ec_hdr()
727 * there was a bit-flip, to force scrubbing. in ubi_io_read_ec_hdr()
731 magic = be32_to_cpu(ec_hdr->magic); in ubi_io_read_ec_hdr()
744 ubi_warn(ubi, "no EC header found at PEB %d, only 0xFF bytes", in ubi_io_read_ec_hdr()
759 ubi_warn(ubi, "bad magic number at PEB %d: %08x instead of %08x", in ubi_io_read_ec_hdr()
769 hdr_crc = be32_to_cpu(ec_hdr->hdr_crc); in ubi_io_read_ec_hdr()
773 ubi_warn(ubi, "bad EC header CRC at PEB %d, calculated %#08x, read %#08x", in ubi_io_read_ec_hdr()
787 err = validate_ec_hdr(ubi, ec_hdr); in ubi_io_read_ec_hdr()
789 ubi_err(ubi, "validation failed for PEB %d", pnum); in ubi_io_read_ec_hdr()
790 return -EINVAL; in ubi_io_read_ec_hdr()
794 * If there was %-EBADMSG, but the header CRC is still OK, report about in ubi_io_read_ec_hdr()
795 * a bit-flip to force scrubbing on this PEB. in ubi_io_read_ec_hdr()
800 if (ubi_dbg_is_read_failure(ubi, MASK_READ_FAILURE_EC)) { in ubi_io_read_ec_hdr()
801 ubi_warn(ubi, "cannot read EC header from PEB %d (emulated)", in ubi_io_read_ec_hdr()
803 return -EIO; in ubi_io_read_ec_hdr()
806 if (ubi_dbg_is_ff(ubi, MASK_IO_FF_EC)) { in ubi_io_read_ec_hdr()
807 ubi_warn(ubi, "bit-all-ff (emulated)"); in ubi_io_read_ec_hdr()
811 if (ubi_dbg_is_ff_bitflips(ubi, MASK_IO_FF_BITFLIPS_EC)) { in ubi_io_read_ec_hdr()
812 ubi_warn(ubi, "bit-all-ff with error reported by MTD driver (emulated)"); in ubi_io_read_ec_hdr()
816 if (ubi_dbg_is_bad_hdr(ubi, MASK_BAD_HDR_EC)) { in ubi_io_read_ec_hdr()
817 ubi_warn(ubi, "bad_hdr (emulated)"); in ubi_io_read_ec_hdr()
821 if (ubi_dbg_is_bad_hdr_ebadmsg(ubi, MASK_BAD_HDR_EBADMSG_EC)) { in ubi_io_read_ec_hdr()
822 ubi_warn(ubi, "bad_hdr with ECC error (emulated)"); in ubi_io_read_ec_hdr()
830 * ubi_io_write_ec_hdr - write an erase counter header.
831 * @ubi: UBI device description object
837 * the caller do not have to fill them. Callers must only fill the @ec_hdr->ec
841 * case of failure. If %-EIO is returned, the physical eraseblock most probably
844 int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum, in ubi_io_write_ec_hdr() argument
851 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); in ubi_io_write_ec_hdr()
853 ec_hdr->magic = cpu_to_be32(UBI_EC_HDR_MAGIC); in ubi_io_write_ec_hdr()
854 ec_hdr->version = UBI_VERSION; in ubi_io_write_ec_hdr()
855 ec_hdr->vid_hdr_offset = cpu_to_be32(ubi->vid_hdr_offset); in ubi_io_write_ec_hdr()
856 ec_hdr->data_offset = cpu_to_be32(ubi->leb_start); in ubi_io_write_ec_hdr()
857 ec_hdr->image_seq = cpu_to_be32(ubi->image_seq); in ubi_io_write_ec_hdr()
859 ec_hdr->hdr_crc = cpu_to_be32(crc); in ubi_io_write_ec_hdr()
861 err = self_check_ec_hdr(ubi, pnum, ec_hdr); in ubi_io_write_ec_hdr()
865 if (ubi_dbg_is_power_cut(ubi, MASK_POWER_CUT_EC)) { in ubi_io_write_ec_hdr()
866 ubi_warn(ubi, "emulating a power cut when writing EC header"); in ubi_io_write_ec_hdr()
867 ubi_ro_mode(ubi); in ubi_io_write_ec_hdr()
868 return -EROFS; in ubi_io_write_ec_hdr()
871 err = ubi_io_write(ubi, ec_hdr, pnum, 0, ubi->ec_hdr_alsize); in ubi_io_write_ec_hdr()
876 * validate_vid_hdr - validate a volume identifier header.
877 * @ubi: UBI device description object
878 * @vid_hdr: the volume identifier header to check
880 * This function checks that data stored in the volume identifier header
883 static int validate_vid_hdr(const struct ubi_device *ubi, in validate_vid_hdr() argument
886 int vol_type = vid_hdr->vol_type; in validate_vid_hdr()
887 int copy_flag = vid_hdr->copy_flag; in validate_vid_hdr()
888 int vol_id = be32_to_cpu(vid_hdr->vol_id); in validate_vid_hdr()
889 int lnum = be32_to_cpu(vid_hdr->lnum); in validate_vid_hdr()
890 int compat = vid_hdr->compat; in validate_vid_hdr()
891 int data_size = be32_to_cpu(vid_hdr->data_size); in validate_vid_hdr()
892 int used_ebs = be32_to_cpu(vid_hdr->used_ebs); in validate_vid_hdr()
893 int data_pad = be32_to_cpu(vid_hdr->data_pad); in validate_vid_hdr()
894 int data_crc = be32_to_cpu(vid_hdr->data_crc); in validate_vid_hdr()
895 int usable_leb_size = ubi->leb_size - data_pad; in validate_vid_hdr()
898 ubi_err(ubi, "bad copy_flag"); in validate_vid_hdr()
904 ubi_err(ubi, "negative values"); in validate_vid_hdr()
909 ubi_err(ubi, "bad vol_id"); in validate_vid_hdr()
914 ubi_err(ubi, "bad compat"); in validate_vid_hdr()
921 ubi_err(ubi, "bad compat"); in validate_vid_hdr()
926 ubi_err(ubi, "bad vol_type"); in validate_vid_hdr()
930 if (data_pad >= ubi->leb_size / 2) { in validate_vid_hdr()
931 ubi_err(ubi, "bad data_pad"); in validate_vid_hdr()
935 if (data_size > ubi->leb_size) { in validate_vid_hdr()
936 ubi_err(ubi, "bad data_size"); in validate_vid_hdr()
942 * Although from high-level point of view static volumes may in validate_vid_hdr()
948 ubi_err(ubi, "zero used_ebs"); in validate_vid_hdr()
952 ubi_err(ubi, "zero data_size"); in validate_vid_hdr()
955 if (lnum < used_ebs - 1) { in validate_vid_hdr()
957 ubi_err(ubi, "bad data_size"); in validate_vid_hdr()
960 } else if (lnum > used_ebs - 1) { in validate_vid_hdr()
961 ubi_err(ubi, "too high lnum"); in validate_vid_hdr()
967 ubi_err(ubi, "non-zero data CRC"); in validate_vid_hdr()
971 ubi_err(ubi, "non-zero data_size"); in validate_vid_hdr()
976 ubi_err(ubi, "zero data_size of copy"); in validate_vid_hdr()
981 ubi_err(ubi, "bad used_ebs"); in validate_vid_hdr()
989 ubi_err(ubi, "bad VID header"); in validate_vid_hdr()
996 * ubi_io_read_vid_hdr - read and check a volume identifier header.
997 * @ubi: UBI device description object
999 * @vidb: the volume identifier buffer to store data in
1002 * This function reads the volume identifier header from physical eraseblock
1004 * volume identifier header. The error codes are the same as in
1010 int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum, in ubi_io_read_vid_hdr() argument
1016 void *p = vidb->buffer; in ubi_io_read_vid_hdr()
1019 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); in ubi_io_read_vid_hdr()
1021 read_err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset, in ubi_io_read_vid_hdr()
1022 ubi->vid_hdr_shift + UBI_VID_HDR_SIZE); in ubi_io_read_vid_hdr()
1026 magic = be32_to_cpu(vid_hdr->magic); in ubi_io_read_vid_hdr()
1033 ubi_warn(ubi, "no VID header found at PEB %d, only 0xFF bytes", in ubi_io_read_vid_hdr()
1044 ubi_warn(ubi, "bad magic number at PEB %d: %08x instead of %08x", in ubi_io_read_vid_hdr()
1054 hdr_crc = be32_to_cpu(vid_hdr->hdr_crc); in ubi_io_read_vid_hdr()
1058 ubi_warn(ubi, "bad CRC at PEB %d, calculated %#08x, read %#08x", in ubi_io_read_vid_hdr()
1070 err = validate_vid_hdr(ubi, vid_hdr); in ubi_io_read_vid_hdr()
1072 ubi_err(ubi, "validation failed for PEB %d", pnum); in ubi_io_read_vid_hdr()
1073 return -EINVAL; in ubi_io_read_vid_hdr()
1079 if (ubi_dbg_is_read_failure(ubi, MASK_READ_FAILURE_VID)) { in ubi_io_read_vid_hdr()
1080 ubi_warn(ubi, "cannot read VID header from PEB %d (emulated)", in ubi_io_read_vid_hdr()
1082 return -EIO; in ubi_io_read_vid_hdr()
1085 if (ubi_dbg_is_ff(ubi, MASK_IO_FF_VID)) { in ubi_io_read_vid_hdr()
1086 ubi_warn(ubi, "bit-all-ff (emulated)"); in ubi_io_read_vid_hdr()
1090 if (ubi_dbg_is_ff_bitflips(ubi, MASK_IO_FF_BITFLIPS_VID)) { in ubi_io_read_vid_hdr()
1091 ubi_warn(ubi, "bit-all-ff with error reported by MTD driver (emulated)"); in ubi_io_read_vid_hdr()
1095 if (ubi_dbg_is_bad_hdr(ubi, MASK_BAD_HDR_VID)) { in ubi_io_read_vid_hdr()
1096 ubi_warn(ubi, "bad_hdr (emulated)"); in ubi_io_read_vid_hdr()
1100 if (ubi_dbg_is_bad_hdr_ebadmsg(ubi, MASK_BAD_HDR_EBADMSG_VID)) { in ubi_io_read_vid_hdr()
1101 ubi_warn(ubi, "bad_hdr with ECC error (emulated)"); in ubi_io_read_vid_hdr()
1109 * ubi_io_write_vid_hdr - write a volume identifier header.
1110 * @ubi: UBI device description object
1112 * @vidb: the volume identifier buffer to write
1114 * This function writes the volume identifier header described by @vid_hdr to
1116 * @vidb->hdr->magic and the @vidb->hdr->version fields, as well as calculates
1117 * header CRC checksum and stores it at vidb->hdr->hdr_crc.
1120 * case of failure. If %-EIO is returned, the physical eraseblock probably went
1123 int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum, in ubi_io_write_vid_hdr() argument
1129 void *p = vidb->buffer; in ubi_io_write_vid_hdr()
1132 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); in ubi_io_write_vid_hdr()
1134 err = self_check_peb_ec_hdr(ubi, pnum); in ubi_io_write_vid_hdr()
1138 vid_hdr->magic = cpu_to_be32(UBI_VID_HDR_MAGIC); in ubi_io_write_vid_hdr()
1139 vid_hdr->version = UBI_VERSION; in ubi_io_write_vid_hdr()
1141 vid_hdr->hdr_crc = cpu_to_be32(crc); in ubi_io_write_vid_hdr()
1143 err = self_check_vid_hdr(ubi, pnum, vid_hdr); in ubi_io_write_vid_hdr()
1147 if (ubi_dbg_is_power_cut(ubi, MASK_POWER_CUT_VID)) { in ubi_io_write_vid_hdr()
1148 ubi_warn(ubi, "emulating a power cut when writing VID header"); in ubi_io_write_vid_hdr()
1149 ubi_ro_mode(ubi); in ubi_io_write_vid_hdr()
1150 return -EROFS; in ubi_io_write_vid_hdr()
1153 err = ubi_io_write(ubi, p, pnum, ubi->vid_hdr_aloffset, in ubi_io_write_vid_hdr()
1154 ubi->vid_hdr_alsize); in ubi_io_write_vid_hdr()
1159 * self_check_not_bad - ensure that a physical eraseblock is not bad.
1160 * @ubi: UBI device description object
1163 * This function returns zero if the physical eraseblock is good, %-EINVAL if
1166 static int self_check_not_bad(const struct ubi_device *ubi, int pnum) in self_check_not_bad() argument
1170 if (!ubi_dbg_chk_io(ubi)) in self_check_not_bad()
1173 err = ubi_io_is_bad(ubi, pnum); in self_check_not_bad()
1177 ubi_err(ubi, "self-check failed for PEB %d", pnum); in self_check_not_bad()
1179 return err > 0 ? -EINVAL : err; in self_check_not_bad()
1183 * self_check_ec_hdr - check if an erase counter header is all right.
1184 * @ubi: UBI device description object
1189 * values, and %-EINVAL if not.
1191 static int self_check_ec_hdr(const struct ubi_device *ubi, int pnum, in self_check_ec_hdr() argument
1197 if (!ubi_dbg_chk_io(ubi)) in self_check_ec_hdr()
1200 magic = be32_to_cpu(ec_hdr->magic); in self_check_ec_hdr()
1202 ubi_err(ubi, "bad magic %#08x, must be %#08x", in self_check_ec_hdr()
1207 err = validate_ec_hdr(ubi, ec_hdr); in self_check_ec_hdr()
1209 ubi_err(ubi, "self-check failed for PEB %d", pnum); in self_check_ec_hdr()
1218 return -EINVAL; in self_check_ec_hdr()
1222 * self_check_peb_ec_hdr - check erase counter header.
1223 * @ubi: UBI device description object
1229 static int self_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum) in self_check_peb_ec_hdr() argument
1235 if (!ubi_dbg_chk_io(ubi)) in self_check_peb_ec_hdr()
1238 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS); in self_check_peb_ec_hdr()
1240 return -ENOMEM; in self_check_peb_ec_hdr()
1242 err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE); in self_check_peb_ec_hdr()
1247 hdr_crc = be32_to_cpu(ec_hdr->hdr_crc); in self_check_peb_ec_hdr()
1249 ubi_err(ubi, "bad CRC, calculated %#08x, read %#08x", in self_check_peb_ec_hdr()
1251 ubi_err(ubi, "self-check failed for PEB %d", pnum); in self_check_peb_ec_hdr()
1254 err = -EINVAL; in self_check_peb_ec_hdr()
1258 err = self_check_ec_hdr(ubi, pnum, ec_hdr); in self_check_peb_ec_hdr()
1266 * self_check_vid_hdr - check that a volume identifier header is all right.
1267 * @ubi: UBI device description object
1268 * @pnum: physical eraseblock number the volume identifier header belongs to
1269 * @vid_hdr: the volume identifier header to check
1271 * This function returns zero if the volume identifier header is all right, and
1272 * %-EINVAL if not.
1274 static int self_check_vid_hdr(const struct ubi_device *ubi, int pnum, in self_check_vid_hdr() argument
1280 if (!ubi_dbg_chk_io(ubi)) in self_check_vid_hdr()
1283 magic = be32_to_cpu(vid_hdr->magic); in self_check_vid_hdr()
1285 ubi_err(ubi, "bad VID header magic %#08x at PEB %d, must be %#08x", in self_check_vid_hdr()
1290 err = validate_vid_hdr(ubi, vid_hdr); in self_check_vid_hdr()
1292 ubi_err(ubi, "self-check failed for PEB %d", pnum); in self_check_vid_hdr()
1299 ubi_err(ubi, "self-check failed for PEB %d", pnum); in self_check_vid_hdr()
1302 return -EINVAL; in self_check_vid_hdr()
1307 * self_check_peb_vid_hdr - check volume identifier header.
1308 * @ubi: UBI device description object
1311 * This function returns zero if the volume identifier header is all right,
1314 static int self_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum) in self_check_peb_vid_hdr() argument
1322 if (!ubi_dbg_chk_io(ubi)) in self_check_peb_vid_hdr()
1325 vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS); in self_check_peb_vid_hdr()
1327 return -ENOMEM; in self_check_peb_vid_hdr()
1330 p = vidb->buffer; in self_check_peb_vid_hdr()
1331 err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset, in self_check_peb_vid_hdr()
1332 ubi->vid_hdr_alsize); in self_check_peb_vid_hdr()
1337 hdr_crc = be32_to_cpu(vid_hdr->hdr_crc); in self_check_peb_vid_hdr()
1339 ubi_err(ubi, "bad VID header CRC at PEB %d, calculated %#08x, read %#08x", in self_check_peb_vid_hdr()
1341 ubi_err(ubi, "self-check failed for PEB %d", pnum); in self_check_peb_vid_hdr()
1344 err = -EINVAL; in self_check_peb_vid_hdr()
1348 err = self_check_vid_hdr(ubi, pnum, vid_hdr); in self_check_peb_vid_hdr()
1356 * self_check_write - make sure write succeeded.
1357 * @ubi: UBI device description object
1364 * the original data buffer - the data have to match. Returns zero if the data
1367 static int self_check_write(struct ubi_device *ubi, const void *buf, int pnum, in self_check_write() argument
1373 loff_t addr = (loff_t)pnum * ubi->peb_size + offset; in self_check_write()
1375 if (!ubi_dbg_chk_io(ubi)) in self_check_write()
1380 ubi_err(ubi, "cannot allocate memory to check writes"); in self_check_write()
1384 err = mtd_read(ubi->mtd, addr, len, &read, buf1); in self_check_write()
1396 ubi_err(ubi, "self-check failed for PEB %d:%d, len %d", in self_check_write()
1398 ubi_msg(ubi, "data differ at position %d", i); in self_check_write()
1399 dump_len = max_t(int, 128, len - i); in self_check_write()
1400 ubi_msg(ubi, "hex dump of the original buffer from %d to %d", in self_check_write()
1404 ubi_msg(ubi, "hex dump of the read buffer from %d to %d", in self_check_write()
1409 err = -EINVAL; in self_check_write()
1422 * ubi_self_check_all_ff - check that a region of flash is empty.
1423 * @ubi: UBI device description object
1432 int ubi_self_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len) in ubi_self_check_all_ff() argument
1437 loff_t addr = (loff_t)pnum * ubi->peb_size + offset; in ubi_self_check_all_ff()
1439 if (!ubi_dbg_chk_io(ubi)) in ubi_self_check_all_ff()
1444 ubi_err(ubi, "cannot allocate memory to check for 0xFFs"); in ubi_self_check_all_ff()
1448 err = mtd_read(ubi->mtd, addr, len, &read, buf); in ubi_self_check_all_ff()
1450 ubi_err(ubi, "err %d while reading %d bytes from PEB %d:%d, read %zd bytes", in ubi_self_check_all_ff()
1457 ubi_err(ubi, "flash region at PEB %d:%d, length %d does not contain all 0xFF bytes", in ubi_self_check_all_ff()
1466 ubi_err(ubi, "self-check failed for PEB %d", pnum); in ubi_self_check_all_ff()
1467 ubi_msg(ubi, "hex dump of the %d-%d region", offset, offset + len); in ubi_self_check_all_ff()
1469 err = -EINVAL; in ubi_self_check_all_ff()