Lines Matching full:ubi
9 * UBI attaching sub-system.
32 * UBI protects EC and VID headers with CRC-32 checksums, so it can detect
33 * whether the headers are corrupted or not. Sometimes UBI also protects the
37 * UBI tries to distinguish between 2 types of corruptions.
39 * 1. Corruptions caused by power cuts. These are expected corruptions and UBI
46 * When UBI detects a corruption (CRC-32 mismatch) in a PEB, and it looks like
47 * the reason is a power cut, UBI puts this PEB to the @erase list, and all
51 * attaching, such PEBs are put to the @corr list and UBI preserves them.
53 * UBI runs out of free PEBs, it switches to R/O mode. UBI also loudly informs
57 * corruptions and UBI's strategy is as follows (in case of attaching by
58 * scanning). UBI assumes corruption type 2 if the VID header is corrupted and
61 * area. Otherwise UBI assumes corruption type 1. So the decision criteria
77 #include "ubi.h"
79 static int self_check_ai(struct ubi_device *ubi, struct ubi_attach_info *ai);
317 * @ubi: UBI device description object
325 * Note, UBI does sanity check of everything it reads from the flash media.
330 static int validate_vid_hdr(const struct ubi_device *ubi, in validate_vid_hdr() argument
349 ubi_err(ubi, "inconsistent vol_id"); in validate_vid_hdr()
359 ubi_err(ubi, "inconsistent vol_type"); in validate_vid_hdr()
364 ubi_err(ubi, "inconsistent used_ebs"); in validate_vid_hdr()
369 ubi_err(ubi, "inconsistent data_pad"); in validate_vid_hdr()
377 ubi_err(ubi, "inconsistent VID header at PEB %d", pnum); in validate_vid_hdr()
420 * @ubi: UBI device description object
438 int ubi_compare_lebs(struct ubi_device *ubi, const struct ubi_ainf_peb *aeb, in ubi_compare_lebs() argument
448 * This must be a really ancient UBI image which has been in ubi_compare_lebs()
451 * eraseblocks. That was before UBI got into mainline. We do not in ubi_compare_lebs()
455 ubi_err(ubi, "unsupported on-flash UBI format"); in ubi_compare_lebs()
486 vidb = ubi_alloc_vid_buf(ubi, GFP_KERNEL); in ubi_compare_lebs()
491 err = ubi_io_read_vid_hdr(ubi, pnum, vidb, 0); in ubi_compare_lebs()
496 ubi_err(ubi, "VID of PEB %d header is bad, but it was OK earlier, err %d", in ubi_compare_lebs()
512 mutex_lock(&ubi->buf_mutex); in ubi_compare_lebs()
513 err = ubi_io_read_data(ubi, ubi->peb_buf, pnum, 0, len); in ubi_compare_lebs()
518 crc = crc32(UBI_CRC32_INIT, ubi->peb_buf, len); in ubi_compare_lebs()
529 mutex_unlock(&ubi->buf_mutex); in ubi_compare_lebs()
541 mutex_unlock(&ubi->buf_mutex); in ubi_compare_lebs()
549 * @ubi: UBI device description object
563 int ubi_add_to_av(struct ubi_device *ubi, struct ubi_attach_info *ai, int pnum, in ubi_add_to_av() argument
617 * be an ancient UBI image from the era when UBI did not have in ubi_add_to_av()
626 ubi_err(ubi, "two LEBs with same sequence number %llu", in ubi_add_to_av()
637 cmp_res = ubi_compare_lebs(ubi, aeb, pnum, vid_hdr); in ubi_add_to_av()
646 err = validate_vid_hdr(ubi, vid_hdr, av, pnum); in ubi_add_to_av()
684 err = validate_vid_hdr(ubi, vid_hdr, av, pnum); in ubi_add_to_av()
760 * @ubi: UBI device description object
766 * counter header to it. This function should only be used on UBI device
771 static int early_erase_peb(struct ubi_device *ubi, in early_erase_peb() argument
779 * Erase counter overflow. Upgrade UBI and use 64-bit in early_erase_peb()
782 ubi_err(ubi, "erase counter overflow at PEB %d, EC %d", in early_erase_peb()
787 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); in early_erase_peb()
793 err = ubi_io_sync_erase(ubi, pnum, 0); in early_erase_peb()
797 err = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr); in early_erase_peb()
806 * @ubi: UBI device description object
810 * called on the UBI initialization stages when the wear-leveling sub-system is
818 struct ubi_ainf_peb *ubi_early_get_peb(struct ubi_device *ubi, in ubi_early_get_peb() argument
841 err = early_erase_peb(ubi, ai, aeb->pnum, aeb->ec+1); in ubi_early_get_peb()
851 ubi_err(ubi, "no free eraseblocks"); in ubi_early_get_peb()
857 * @ubi: UBI device description object
868 * If the corruption reason was a power cut, UBI can safely erase this PEB.
872 static int check_corruption(struct ubi_device *ubi, struct ubi_vid_hdr *vid_hdr, in check_corruption() argument
877 mutex_lock(&ubi->buf_mutex); in check_corruption()
878 memset(ubi->peb_buf, 0x00, ubi->leb_size); in check_corruption()
880 err = ubi_io_read(ubi, ubi->peb_buf, pnum, ubi->leb_start, in check_corruption()
881 ubi->leb_size); in check_corruption()
897 if (ubi_check_pattern(ubi->peb_buf, 0xFF, ubi->leb_size)) in check_corruption()
900 ubi_err(ubi, "PEB %d contains corrupted VID header, and the data does not contain all 0xFF", in check_corruption()
902 …ubi_err(ubi, "this may be a non-UBI PEB or a severe VID header corruption which requires manual in… in check_corruption()
905 pnum, ubi->leb_start, ubi->leb_size); in check_corruption()
907 ubi->peb_buf, ubi->leb_size, 1); in check_corruption()
911 mutex_unlock(&ubi->buf_mutex); in check_corruption()
930 * scan_peb - scan and process UBI headers of a PEB.
931 * @ubi: UBI device description object
936 * This function reads UBI headers of PEB @pnum, checks them, and adds
941 static int scan_peb(struct ubi_device *ubi, struct ubi_attach_info *ai, in scan_peb() argument
953 err = ubi_io_is_bad(ubi, pnum); in scan_peb()
961 err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0); in scan_peb()
990 ubi_err(ubi, "'ubi_io_read_ec_hdr()' returned unknown code %d", in scan_peb()
998 /* Make sure UBI version is OK */ in scan_peb()
1000 ubi_err(ubi, "this UBI version is %d, image version is %d", in scan_peb()
1011 * flash. Upgrade UBI and use 64-bit erase counters in scan_peb()
1014 ubi_err(ubi, "erase counter overflow, max is %d", in scan_peb()
1022 * This allows us to detect situations when users flash UBI in scan_peb()
1023 * images incorrectly, so that the flash has the new UBI image in scan_peb()
1026 * zero, because old UBI implementations always set it to zero. in scan_peb()
1032 if (!ubi->image_seq) in scan_peb()
1033 ubi->image_seq = image_seq; in scan_peb()
1034 if (image_seq && ubi->image_seq != image_seq) { in scan_peb()
1035 ubi_err(ubi, "bad image sequence number %d in PEB %d, expected %d", in scan_peb()
1036 image_seq, pnum, ubi->image_seq); in scan_peb()
1044 err = ubi_io_read_vid_hdr(ubi, pnum, vidb, 0); in scan_peb()
1081 * that this a valid UBI PEB which has corresponding in scan_peb()
1097 err = check_corruption(ubi, vidh, pnum); in scan_peb()
1128 ubi_err(ubi, "'ubi_io_read_vid_hdr()' returned unknown code %d", in scan_peb()
1140 ubi_msg(ubi, "\"delete\" compatible internal volume %d:%d found, will remove it", in scan_peb()
1150 ubi_msg(ubi, "read-only compatible internal volume %d:%d found, switch to read-only mode", in scan_peb()
1152 ubi->ro_mode = 1; in scan_peb()
1156 ubi_msg(ubi, "\"preserve\" compatible internal volume %d:%d found", in scan_peb()
1165 ubi_err(ubi, "incompatible internal volume %d:%d found", in scan_peb()
1172 ubi_warn(ubi, "valid VID header but corrupted EC header at PEB %d", in scan_peb()
1178 err = ubi_add_to_av(ubi, ai, pnum, ec, vidh, bitflips); in scan_peb()
1198 * @ubi: UBI device description object
1207 static int late_analysis(struct ubi_device *ubi, struct ubi_attach_info *ai) in late_analysis() argument
1212 peb_count = ubi->peb_count - ai->bad_peb_count - ai->alien_peb_count; in late_analysis()
1221 ubi_err(ubi, "%d PEBs are corrupted and preserved", in late_analysis()
1233 ubi_err(ubi, "too many corrupted PEBs, refusing"); in late_analysis()
1251 * 2. Flash contains non-UBI data and we do not want to format in late_analysis()
1256 ubi_msg(ubi, "empty MTD device detected"); in late_analysis()
1257 get_random_bytes(&ubi->image_seq, in late_analysis()
1258 sizeof(ubi->image_seq)); in late_analysis()
1260 ubi_err(ubi, "MTD device is not UBI-formatted and possibly contains non-UBI data - refusing it"); in late_analysis()
1366 * @ubi: UBI device description object
1374 static int scan_all(struct ubi_device *ubi, struct ubi_attach_info *ai, in scan_all() argument
1384 ai->ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); in scan_all()
1388 ai->vidb = ubi_alloc_vid_buf(ubi, GFP_KERNEL); in scan_all()
1392 for (pnum = start; pnum < ubi->peb_count; pnum++) { in scan_all()
1396 err = scan_peb(ubi, ai, pnum, false); in scan_all()
1401 ubi_msg(ubi, "scanning is finished"); in scan_all()
1407 err = late_analysis(ubi, ai); in scan_all()
1434 err = self_check_ai(ubi, ai); in scan_all()
1479 * @ubi: UBI device description object
1487 static int scan_fast(struct ubi_device *ubi, struct ubi_attach_info **ai) in scan_fast() argument
1498 scan_ai->ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); in scan_fast()
1502 scan_ai->vidb = ubi_alloc_vid_buf(ubi, GFP_KERNEL); in scan_fast()
1510 err = scan_peb(ubi, scan_ai, pnum, true); in scan_fast()
1521 err = ubi_scan_fastmap(ubi, *ai, scan_ai); in scan_fast()
1549 * @ubi: UBI device descriptor
1555 int ubi_attach(struct ubi_device *ubi, int force_scan) in ubi_attach() argument
1566 if ((int)mtd_div_by_eb(ubi->mtd->size, ubi->mtd) <= UBI_FM_MAX_START) { in ubi_attach()
1567 ubi->fm_disabled = 1; in ubi_attach()
1572 err = scan_all(ubi, ai, 0); in ubi_attach()
1574 err = scan_fast(ubi, &ai); in ubi_attach()
1582 err = scan_all(ubi, ai, 0); in ubi_attach()
1584 err = scan_all(ubi, ai, UBI_FM_MAX_START); in ubi_attach()
1589 err = scan_all(ubi, ai, 0); in ubi_attach()
1594 ubi->bad_peb_count = ai->bad_peb_count; in ubi_attach()
1595 ubi->good_peb_count = ubi->peb_count - ubi->bad_peb_count; in ubi_attach()
1596 ubi->corr_peb_count = ai->corr_peb_count; in ubi_attach()
1597 ubi->max_ec = ai->max_ec; in ubi_attach()
1598 ubi->mean_ec = ai->mean_ec; in ubi_attach()
1601 err = ubi_read_volume_table(ubi, ai); in ubi_attach()
1605 err = ubi_wl_init(ubi, ai); in ubi_attach()
1609 err = ubi_eba_init(ubi, ai); in ubi_attach()
1614 if (ubi->fm && ubi_dbg_chk_fastmap(ubi)) { in ubi_attach()
1623 err = scan_all(ubi, scan_ai, 0); in ubi_attach()
1629 err = self_check_eba(ubi, ai, scan_ai); in ubi_attach()
1641 ubi_wl_close(ubi); in ubi_attach()
1643 ubi_free_all_volumes(ubi); in ubi_attach()
1644 vfree(ubi->vtbl); in ubi_attach()
1652 * @ubi: UBI device description object
1658 static int self_check_ai(struct ubi_device *ubi, struct ubi_attach_info *ai) in self_check_ai() argument
1668 if (!ubi_dbg_chk_gen(ubi)) in self_check_ai()
1682 ubi_err(ubi, "bad is_empty flag"); in self_check_ai()
1689 ubi_err(ubi, "negative values"); in self_check_ai()
1695 ubi_err(ubi, "bad vol_id"); in self_check_ai()
1700 ubi_err(ubi, "highest_vol_id is %d, but vol_id %d is there", in self_check_ai()
1707 ubi_err(ubi, "bad vol_type"); in self_check_ai()
1711 if (av->data_pad > ubi->leb_size / 2) { in self_check_ai()
1712 ubi_err(ubi, "bad data_pad"); in self_check_ai()
1724 ubi_err(ubi, "negative values"); in self_check_ai()
1729 ubi_err(ubi, "bad ai->min_ec (%d), %d found", in self_check_ai()
1735 ubi_err(ubi, "bad ai->max_ec (%d), %d found", in self_check_ai()
1740 if (aeb->pnum >= ubi->peb_count) { in self_check_ai()
1741 ubi_err(ubi, "too high PEB number %d, total PEBs %d", in self_check_ai()
1742 aeb->pnum, ubi->peb_count); in self_check_ai()
1748 ubi_err(ubi, "bad lnum or used_ebs"); in self_check_ai()
1753 ubi_err(ubi, "non-zero used_ebs"); in self_check_ai()
1759 ubi_err(ubi, "incorrect highest_lnum or lnum"); in self_check_ai()
1765 ubi_err(ubi, "bad leb_count, %d objects in the tree", in self_check_ai()
1776 ubi_err(ubi, "bad highest_lnum"); in self_check_ai()
1782 ubi_err(ubi, "bad ai->vols_found %d, should be %d", in self_check_ai()
1797 err = ubi_io_read_vid_hdr(ubi, aeb->pnum, vidb, 1); in self_check_ai()
1799 ubi_err(ubi, "VID header is not OK (%d)", in self_check_ai()
1809 ubi_err(ubi, "bad vol_type"); in self_check_ai()
1814 ubi_err(ubi, "bad sqnum %llu", aeb->sqnum); in self_check_ai()
1819 ubi_err(ubi, "bad vol_id %d", av->vol_id); in self_check_ai()
1824 ubi_err(ubi, "bad compat %d", vidh->compat); in self_check_ai()
1829 ubi_err(ubi, "bad lnum %d", aeb->lnum); in self_check_ai()
1834 ubi_err(ubi, "bad used_ebs %d", av->used_ebs); in self_check_ai()
1839 ubi_err(ubi, "bad data_pad %d", av->data_pad); in self_check_ai()
1848 ubi_err(ubi, "bad highest_lnum %d", av->highest_lnum); in self_check_ai()
1853 ubi_err(ubi, "bad last_data_size %d", in self_check_ai()
1863 buf = kzalloc(ubi->peb_count, GFP_KERNEL); in self_check_ai()
1867 for (pnum = 0; pnum < ubi->peb_count; pnum++) { in self_check_ai()
1868 err = ubi_io_is_bad(ubi, pnum); in self_check_ai()
1893 for (pnum = 0; pnum < ubi->peb_count; pnum++) in self_check_ai()
1895 ubi_err(ubi, "PEB %d is not referred", pnum); in self_check_ai()
1905 ubi_err(ubi, "bad attaching information about LEB %d", aeb->lnum); in self_check_ai()
1911 ubi_err(ubi, "bad attaching information about volume %d", av->vol_id); in self_check_ai()
1916 ubi_err(ubi, "bad attaching information about volume %d", av->vol_id); in self_check_ai()