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()
206 * ubi_io_write - write data to a physical eraseblock.
216 * error code. If %-EIO is returned, the physical eraseblock most probably went
231 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); in ubi_io_write()
232 ubi_assert(offset >= 0 && offset + len <= ubi->peb_size); in ubi_io_write()
233 ubi_assert(offset % ubi->hdrs_min_io_size == 0); in ubi_io_write()
234 ubi_assert(len > 0 && len % ubi->hdrs_min_io_size == 0); in ubi_io_write()
236 if (ubi->ro_mode) { in ubi_io_write()
237 ubi_err(ubi, "read-only mode"); in ubi_io_write()
238 return -EROFS; in ubi_io_write()
250 if (offset >= ubi->leb_start) { in ubi_io_write()
267 return -EIO; in ubi_io_write()
270 addr = (loff_t)pnum * ubi->peb_size + offset; in ubi_io_write()
271 err = mtd_write(ubi->mtd, addr, len, &written, buf); in ubi_io_write()
290 len = ubi->peb_size - offset; in ubi_io_write()
299 * do_sync_erase - synchronously erase a physical eraseblock.
305 * %-EIO is returned, the physical eraseblock most probably went bad.
313 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); in do_sync_erase()
315 if (ubi->ro_mode) { in do_sync_erase()
316 ubi_err(ubi, "read-only mode"); in do_sync_erase()
317 return -EROFS; in do_sync_erase()
323 ei.addr = (loff_t)pnum * ubi->peb_size; in do_sync_erase()
324 ei.len = ubi->peb_size; in do_sync_erase()
326 err = mtd_erase(ubi->mtd, &ei); in do_sync_erase()
339 err = ubi_self_check_all_ff(ubi, pnum, 0, ubi->peb_size); in do_sync_erase()
345 return -EIO; in do_sync_erase()
355 * torture_peb - test a supposedly bad physical eraseblock.
359 * This function returns %-EIO if the physical eraseblock did not pass the
371 mutex_lock(&ubi->buf_mutex); in torture_peb()
378 err = ubi_io_read(ubi, ubi->peb_buf, pnum, 0, ubi->peb_size); in torture_peb()
382 err = ubi_check_pattern(ubi->peb_buf, 0xFF, ubi->peb_size); in torture_peb()
384 ubi_err(ubi, "erased PEB %d, but a non-0xFF byte found", in torture_peb()
386 err = -EIO; in torture_peb()
391 memset(ubi->peb_buf, patterns[i], ubi->peb_size); in torture_peb()
392 err = ubi_io_write(ubi, ubi->peb_buf, pnum, 0, ubi->peb_size); in torture_peb()
396 memset(ubi->peb_buf, ~patterns[i], ubi->peb_size); in torture_peb()
397 err = ubi_io_read(ubi, ubi->peb_buf, pnum, 0, ubi->peb_size); in torture_peb()
401 err = ubi_check_pattern(ubi->peb_buf, patterns[i], in torture_peb()
402 ubi->peb_size); in torture_peb()
406 err = -EIO; in torture_peb()
415 mutex_unlock(&ubi->buf_mutex); in torture_peb()
418 * If a bit-flip or data integrity error was detected, the test in torture_peb()
424 err = -EIO; in torture_peb()
430 * nor_erase_prepare - prepare a NOR flash PEB for erasure.
461 * comment in this file). But we know this is a NOR-specific piece of in nor_erase_prepare()
462 * code, so we can do this. But yes, this is error-prone and we should in nor_erase_prepare()
463 * (pre-)allocate VID header buffer instead. in nor_erase_prepare()
474 addr = (loff_t)pnum * ubi->peb_size; in nor_erase_prepare()
478 err = mtd_write(ubi->mtd, addr, 4, &written, (void *)&data); in nor_erase_prepare()
489 addr += ubi->vid_hdr_aloffset; in nor_erase_prepare()
490 err = mtd_write(ubi->mtd, addr, 4, &written, (void *)&data); in nor_erase_prepare()
503 ubi_dump_flash(ubi, pnum, 0, ubi->peb_size); in nor_erase_prepare()
504 return -EIO; in nor_erase_prepare()
508 * ubi_io_sync_erase - synchronously erase a physical eraseblock.
518 * This function returns the number of erasures made in case of success, %-EIO
520 * codes in case of other errors. Note, %-EIO means that the physical
527 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); in ubi_io_sync_erase()
533 if (ubi->ro_mode) { in ubi_io_sync_erase()
534 ubi_err(ubi, "read-only mode"); in ubi_io_sync_erase()
535 return -EROFS; in ubi_io_sync_erase()
539 * If the flash is ECC-ed then we have to erase the ECC block before we in ubi_io_sync_erase()
545 if (ubi->nor_flash && ubi->mtd->writesize == 1) { in ubi_io_sync_erase()
565 * ubi_io_is_bad - check if a physical eraseblock is bad.
574 struct mtd_info *mtd = ubi->mtd; in ubi_io_is_bad()
576 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); in ubi_io_is_bad()
578 if (ubi->bad_allowed) { in ubi_io_is_bad()
581 ret = mtd_block_isbad(mtd, (loff_t)pnum * ubi->peb_size); in ubi_io_is_bad()
594 * ubi_io_mark_bad - mark a physical eraseblock as bad.
604 struct mtd_info *mtd = ubi->mtd; in ubi_io_mark_bad()
606 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); in ubi_io_mark_bad()
608 if (ubi->ro_mode) { in ubi_io_mark_bad()
609 ubi_err(ubi, "read-only mode"); in ubi_io_mark_bad()
610 return -EROFS; in ubi_io_mark_bad()
613 if (!ubi->bad_allowed) in ubi_io_mark_bad()
616 err = mtd_block_markbad(mtd, (loff_t)pnum * ubi->peb_size); in ubi_io_mark_bad()
623 * validate_ec_hdr - validate an erase counter header.
636 ec = be64_to_cpu(ec_hdr->ec); in validate_ec_hdr()
637 vid_hdr_offset = be32_to_cpu(ec_hdr->vid_hdr_offset); in validate_ec_hdr()
638 leb_start = be32_to_cpu(ec_hdr->data_offset); in validate_ec_hdr()
640 if (ec_hdr->version != UBI_VERSION) { in validate_ec_hdr()
642 UBI_VERSION, (int)ec_hdr->version); in validate_ec_hdr()
646 if (vid_hdr_offset != ubi->vid_hdr_offset) { in validate_ec_hdr()
648 vid_hdr_offset, ubi->vid_hdr_offset); in validate_ec_hdr()
652 if (leb_start != ubi->leb_start) { in validate_ec_hdr()
654 leb_start, ubi->leb_start); in validate_ec_hdr()
673 * ubi_io_read_ec_hdr - read and check an erase counter header.
685 * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected
690 * a data integrity error (uncorrectable ECC error in case of NAND);
701 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); in ubi_io_read_ec_hdr()
709 * We read all the data, but either a correctable bit-flip in ubi_io_read_ec_hdr()
711 * (uncorrectable ECC error in case of NAND). The former is in ubi_io_read_ec_hdr()
713 * corrupted. But we have a CRC check-sum and we will detect in ubi_io_read_ec_hdr()
715 * there was a bit-flip, to force scrubbing. in ubi_io_read_ec_hdr()
719 magic = be32_to_cpu(ec_hdr->magic); in ubi_io_read_ec_hdr()
757 hdr_crc = be32_to_cpu(ec_hdr->hdr_crc); in ubi_io_read_ec_hdr()
778 return -EINVAL; in ubi_io_read_ec_hdr()
782 * If there was %-EBADMSG, but the header CRC is still OK, report about in ubi_io_read_ec_hdr()
783 * a bit-flip to force scrubbing on this PEB. in ubi_io_read_ec_hdr()
789 * ubi_io_write_ec_hdr - write an erase counter header.
796 * the caller do not have to fill them. Callers must only fill the @ec_hdr->ec
800 * case of failure. If %-EIO is returned, the physical eraseblock most probably
810 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); in ubi_io_write_ec_hdr()
812 ec_hdr->magic = cpu_to_be32(UBI_EC_HDR_MAGIC); in ubi_io_write_ec_hdr()
813 ec_hdr->version = UBI_VERSION; in ubi_io_write_ec_hdr()
814 ec_hdr->vid_hdr_offset = cpu_to_be32(ubi->vid_hdr_offset); in ubi_io_write_ec_hdr()
815 ec_hdr->data_offset = cpu_to_be32(ubi->leb_start); in ubi_io_write_ec_hdr()
816 ec_hdr->image_seq = cpu_to_be32(ubi->image_seq); in ubi_io_write_ec_hdr()
818 ec_hdr->hdr_crc = cpu_to_be32(crc); in ubi_io_write_ec_hdr()
825 return -EROFS; in ubi_io_write_ec_hdr()
827 err = ubi_io_write(ubi, ec_hdr, pnum, 0, ubi->ec_hdr_alsize); in ubi_io_write_ec_hdr()
832 * validate_vid_hdr - validate a volume identifier header.
842 int vol_type = vid_hdr->vol_type; in validate_vid_hdr()
843 int copy_flag = vid_hdr->copy_flag; in validate_vid_hdr()
844 int vol_id = be32_to_cpu(vid_hdr->vol_id); in validate_vid_hdr()
845 int lnum = be32_to_cpu(vid_hdr->lnum); in validate_vid_hdr()
846 int compat = vid_hdr->compat; in validate_vid_hdr()
847 int data_size = be32_to_cpu(vid_hdr->data_size); in validate_vid_hdr()
848 int used_ebs = be32_to_cpu(vid_hdr->used_ebs); in validate_vid_hdr()
849 int data_pad = be32_to_cpu(vid_hdr->data_pad); in validate_vid_hdr()
850 int data_crc = be32_to_cpu(vid_hdr->data_crc); in validate_vid_hdr()
851 int usable_leb_size = ubi->leb_size - data_pad; in validate_vid_hdr()
886 if (data_pad >= ubi->leb_size / 2) { in validate_vid_hdr()
891 if (data_size > ubi->leb_size) { in validate_vid_hdr()
898 * Although from high-level point of view static volumes may in validate_vid_hdr()
911 if (lnum < used_ebs - 1) { in validate_vid_hdr()
916 } else if (lnum > used_ebs - 1) { in validate_vid_hdr()
923 ubi_err(ubi, "non-zero data CRC"); in validate_vid_hdr()
927 ubi_err(ubi, "non-zero data_size"); in validate_vid_hdr()
952 * ubi_io_read_vid_hdr - read and check a volume identifier header.
972 void *p = vidb->buffer; in ubi_io_read_vid_hdr()
975 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); in ubi_io_read_vid_hdr()
977 read_err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset, in ubi_io_read_vid_hdr()
978 ubi->vid_hdr_shift + UBI_VID_HDR_SIZE); in ubi_io_read_vid_hdr()
982 magic = be32_to_cpu(vid_hdr->magic); in ubi_io_read_vid_hdr()
1010 hdr_crc = be32_to_cpu(vid_hdr->hdr_crc); in ubi_io_read_vid_hdr()
1029 return -EINVAL; in ubi_io_read_vid_hdr()
1036 * ubi_io_write_vid_hdr - write a volume identifier header.
1043 * @vidb->hdr->magic and the @vidb->hdr->version fields, as well as calculates
1044 * header CRC checksum and stores it at vidb->hdr->hdr_crc.
1047 * case of failure. If %-EIO is returned, the physical eraseblock probably went
1056 void *p = vidb->buffer; in ubi_io_write_vid_hdr()
1059 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); in ubi_io_write_vid_hdr()
1065 vid_hdr->magic = cpu_to_be32(UBI_VID_HDR_MAGIC); in ubi_io_write_vid_hdr()
1066 vid_hdr->version = UBI_VERSION; in ubi_io_write_vid_hdr()
1068 vid_hdr->hdr_crc = cpu_to_be32(crc); in ubi_io_write_vid_hdr()
1075 return -EROFS; in ubi_io_write_vid_hdr()
1077 err = ubi_io_write(ubi, p, pnum, ubi->vid_hdr_aloffset, in ubi_io_write_vid_hdr()
1078 ubi->vid_hdr_alsize); in ubi_io_write_vid_hdr()
1083 * self_check_not_bad - ensure that a physical eraseblock is not bad.
1087 * This function returns zero if the physical eraseblock is good, %-EINVAL if
1101 ubi_err(ubi, "self-check failed for PEB %d", pnum); in self_check_not_bad()
1103 return err > 0 ? -EINVAL : err; in self_check_not_bad()
1107 * self_check_ec_hdr - check if an erase counter header is all right.
1113 * values, and %-EINVAL if not.
1124 magic = be32_to_cpu(ec_hdr->magic); in self_check_ec_hdr()
1133 ubi_err(ubi, "self-check failed for PEB %d", pnum); in self_check_ec_hdr()
1142 return -EINVAL; in self_check_ec_hdr()
1146 * self_check_peb_ec_hdr - check erase counter header.
1162 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS); in self_check_peb_ec_hdr()
1164 return -ENOMEM; in self_check_peb_ec_hdr()
1171 hdr_crc = be32_to_cpu(ec_hdr->hdr_crc); in self_check_peb_ec_hdr()
1175 ubi_err(ubi, "self-check failed for PEB %d", pnum); in self_check_peb_ec_hdr()
1178 err = -EINVAL; in self_check_peb_ec_hdr()
1190 * self_check_vid_hdr - check that a volume identifier header is all right.
1196 * %-EINVAL if not.
1207 magic = be32_to_cpu(vid_hdr->magic); in self_check_vid_hdr()
1216 ubi_err(ubi, "self-check failed for PEB %d", pnum); in self_check_vid_hdr()
1223 ubi_err(ubi, "self-check failed for PEB %d", pnum); in self_check_vid_hdr()
1226 return -EINVAL; in self_check_vid_hdr()
1231 * self_check_peb_vid_hdr - check volume identifier header.
1251 return -ENOMEM; in self_check_peb_vid_hdr()
1254 p = vidb->buffer; in self_check_peb_vid_hdr()
1255 err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset, in self_check_peb_vid_hdr()
1256 ubi->vid_hdr_alsize); in self_check_peb_vid_hdr()
1261 hdr_crc = be32_to_cpu(vid_hdr->hdr_crc); in self_check_peb_vid_hdr()
1265 ubi_err(ubi, "self-check failed for PEB %d", pnum); in self_check_peb_vid_hdr()
1268 err = -EINVAL; in self_check_peb_vid_hdr()
1280 * self_check_write - make sure write succeeded.
1288 * the original data buffer - the data have to match. Returns zero if the data
1297 loff_t addr = (loff_t)pnum * ubi->peb_size + offset; in self_check_write()
1308 err = mtd_read(ubi->mtd, addr, len, &read, buf1); in self_check_write()
1320 ubi_err(ubi, "self-check failed for PEB %d:%d, len %d", in self_check_write()
1323 dump_len = max_t(int, 128, len - i); in self_check_write()
1333 err = -EINVAL; in self_check_write()
1346 * ubi_self_check_all_ff - check that a region of flash is empty.
1361 loff_t addr = (loff_t)pnum * ubi->peb_size + offset; in ubi_self_check_all_ff()
1372 err = mtd_read(ubi->mtd, addr, len, &read, buf); in ubi_self_check_all_ff()
1390 ubi_err(ubi, "self-check failed for PEB %d", pnum); in ubi_self_check_all_ff()
1391 ubi_msg(ubi, "hex dump of the %d-%d region", offset, offset + len); in ubi_self_check_all_ff()
1393 err = -EINVAL; in ubi_self_check_all_ff()