Lines Matching +full:use +full:- +full:minimum +full:- +full:ecc
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
18 * sub-system validates every single header it reads from the flash media.
24 * (i.e. aligned to the minimum I/O unit size). Data starts next to the VID
35 * @ubi->mtd->writesize field. But as an exception, UBI admits use of another
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
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.
92 * ubi_io_read - read data from a physical eraseblock.
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
108 * example it can be an ECC error in case of NAND; this most probably means
110 * o %-EIO if some I/O error occurred;
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()
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()
144 * treats data as correct even in case of ECC errors if the CRC is 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()
156 const char *errstr = mtd_is_eccerr(err) ? " (ECC error)" : ""; 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()
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()
197 dbg_gen("bit-flip (emulated)"); in ubi_io_read()
204 return -EIO; 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.
228 * error code. If %-EIO is returned, the physical eraseblock most probably went
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()
262 if (offset >= ubi->leb_start) { 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()
302 len = ubi->peb_size - offset; in ubi_io_write()
311 * do_sync_erase - synchronously erase a physical eraseblock.
317 * %-EIO is returned, the physical eraseblock most probably went bad.
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()
351 err = ubi_self_check_all_ff(ubi, pnum, 0, ubi->peb_size); in do_sync_erase()
357 return -EIO; in do_sync_erase()
367 * torture_peb - test a supposedly bad physical eraseblock.
371 * This function returns %-EIO if the physical eraseblock did not pass the
383 mutex_lock(&ubi->buf_mutex); 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()
418 err = -EIO; 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()
436 err = -EIO; in torture_peb()
442 * nor_erase_prepare - prepare a NOR flash PEB for erasure.
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()
486 addr = (loff_t)pnum * ubi->peb_size; in nor_erase_prepare()
490 err = mtd_write(ubi->mtd, addr, 4, &written, (void *)&data); 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()
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.
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
539 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 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()
577 * ubi_io_is_bad - check if a physical eraseblock is bad.
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()
606 * ubi_io_mark_bad - mark a physical eraseblock as bad.
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()
635 * validate_ec_hdr - validate an erase counter header.
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()
654 UBI_VERSION, (int)ec_hdr->version); in validate_ec_hdr()
658 if (vid_hdr_offset != ubi->vid_hdr_offset) { 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()
666 leb_start, ubi->leb_start); in validate_ec_hdr()
685 * ubi_io_read_ec_hdr - read and check an erase counter header.
697 * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected
702 * a data integrity error (uncorrectable ECC error in case of NAND);
713 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); in ubi_io_read_ec_hdr()
721 * We read all the data, but either a correctable bit-flip in ubi_io_read_ec_hdr()
723 * (uncorrectable ECC error in case of NAND). The former is 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()
769 hdr_crc = be32_to_cpu(ec_hdr->hdr_crc); 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()
803 return -EIO; in ubi_io_read_ec_hdr()
807 ubi_warn(ubi, "bit-all-ff (emulated)"); 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()
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.
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
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()
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.
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()
930 if (data_pad >= ubi->leb_size / 2) { in validate_vid_hdr()
935 if (data_size > ubi->leb_size) { in validate_vid_hdr()
942 * Although from high-level point of view static volumes may in validate_vid_hdr()
955 if (lnum < used_ebs - 1) { in validate_vid_hdr()
960 } else if (lnum > used_ebs - 1) { 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()
996 * ubi_io_read_vid_hdr - read and check a volume identifier header.
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()
1054 hdr_crc = be32_to_cpu(vid_hdr->hdr_crc); in ubi_io_read_vid_hdr()
1073 return -EINVAL; in ubi_io_read_vid_hdr()
1082 return -EIO; in ubi_io_read_vid_hdr()
1086 ubi_warn(ubi, "bit-all-ff (emulated)"); 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()
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.
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
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()
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()
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.
1163 * This function returns zero if the physical eraseblock is good, %-EINVAL if
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.
1189 * values, and %-EINVAL if not.
1200 magic = be32_to_cpu(ec_hdr->magic); 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.
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()
1247 hdr_crc = be32_to_cpu(ec_hdr->hdr_crc); 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()
1266 * self_check_vid_hdr - check that a volume identifier header is all right.
1272 * %-EINVAL if not.
1283 magic = be32_to_cpu(vid_hdr->magic); 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.
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()
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()
1356 * self_check_write - make sure write succeeded.
1364 * the original data buffer - the data have to match. Returns zero if the data
1373 loff_t addr = (loff_t)pnum * ubi->peb_size + offset; 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()
1399 dump_len = max_t(int, 128, len - i); 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.
1437 loff_t addr = (loff_t)pnum * ubi->peb_size + offset; in ubi_self_check_all_ff()
1448 err = mtd_read(ubi->mtd, addr, len, &read, buf); 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()