86 #include <linux/slab.h>
89 #include <linux/random.h>
124 if (list == &ai->
free) {
125 dbg_bld(
"add to free: PEB %d, EC %d", pnum, ec);
126 }
else if (list == &ai->
erase) {
127 dbg_bld(
"add to erase: PEB %d, EC %d", pnum, ec);
128 }
else if (list == &ai->
alien) {
129 dbg_bld(
"add to alien: PEB %d, EC %d", pnum, ec);
143 list_add(&aeb->
u.
list, list);
164 dbg_bld(
"add to corrupted: PEB %d, EC %d", pnum, ec);
191 static int validate_vid_hdr(
const struct ubi_vid_hdr *vid_hdr,
208 if (vol_id != av->
vol_id) {
209 ubi_err(
"inconsistent vol_id");
218 if (vol_type != av_vol_type) {
219 ubi_err(
"inconsistent vol_type");
224 ubi_err(
"inconsistent used_ebs");
229 ubi_err(
"inconsistent data_pad");
237 ubi_err(
"inconsistent VID header at PEB %d", pnum);
257 int vol_id,
int pnum,
295 rb_link_node(&av->
rb, parent, p);
298 dbg_bld(
"added volume %d", vol_id);
326 int len,
err, second_is_newer, bitflips = 0, corrupted = 0;
331 if (sqnum2 == aeb->
sqnum) {
340 ubi_err(
"unsupported on-flash UBI format");
345 second_is_newer = (sqnum2 > aeb->
sqnum);
356 if (second_is_newer) {
359 dbg_bld(
"second PEB %d is newer, copy_flag is unset",
366 dbg_bld(
"first PEB %d is newer, copy_flag is unset",
368 return bitflips << 1;
381 ubi_err(
"VID of PEB %d header is bad, but it was OK earlier, err %d",
402 err = ubi_io_read_data(ubi, buf, pnum, 0, len);
408 if (crc != data_crc) {
409 dbg_bld(
"PEB %d CRC error: calculated %#08x, must be %#08x",
410 pnum, crc, data_crc);
413 second_is_newer = !second_is_newer;
415 dbg_bld(
"PEB %d CRC is OK", pnum);
420 ubi_free_vid_hdr(ubi, vh);
423 dbg_bld(
"second PEB %d is newer, copy_flag is set", pnum);
425 dbg_bld(
"first PEB %d is newer, copy_flag is set", pnum);
427 return second_is_newer | (bitflips << 1) | (corrupted << 2);
432 ubi_free_vid_hdr(ubi, vh);
453 int ec,
const struct ubi_vid_hdr *vid_hdr,
int bitflips)
456 unsigned long long sqnum;
465 dbg_bld(
"PEB %d, LEB %d:%d, EC %d, sqnum %llu, bitflips %d",
466 pnum, vol_id, lnum, ec, sqnum, bitflips);
468 av = add_volume(ai, vol_id, pnum, vid_hdr);
479 p = &av->
root.rb_node;
485 if (lnum != aeb->
lnum) {
486 if (lnum < aeb->lnum)
498 dbg_bld(
"this LEB already exists: PEB %d, sqnum %llu, EC %d",
514 if (aeb->
sqnum == sqnum && sqnum != 0) {
515 ubi_err(
"two LEBs with same sequence number %llu",
535 err = validate_vid_hdr(vid_hdr, av, pnum);
540 aeb->
lnum, aeb->
ec, cmp_res & 4,
549 aeb->
scrub = ((cmp_res & 2) || bitflips);
563 return add_to_list(ai, pnum, vol_id, lnum, ec,
564 cmp_res & 4, &ai->
erase);
573 err = validate_vid_hdr(vid_hdr, av, pnum);
585 aeb->
scrub = bitflips;
595 rb_link_node(&aeb->
u.
rb, parent, p);
639 dbg_bld(
"remove attaching information about volume %d", av->
vol_id);
665 static int early_erase_peb(
struct ubi_device *ubi,
676 ubi_err(
"erase counter overflow at PEB %d, EC %d", pnum, ec);
717 if (!list_empty(&ai->
free)) {
734 err = early_erase_peb(ubi, ai, aeb->
pnum, aeb->
ec+1);
744 ubi_err(
"no free eraseblocks");
793 ubi_err(
"PEB %d contains corrupted VID header, and the data does not contain all 0xFF",
795 ubi_err(
"this may be a non-UBI PEB or a severe VID header corruption which requires manual inspection");
797 pr_err(
"hexdump of PEB %d offset %d, length %d",
822 int pnum,
int *
vid,
unsigned long long *
sqnum)
825 int err, bitflips = 0, vol_id = -1, ec_err = 0;
867 ubi_err(
"'ubi_io_read_ec_hdr()' returned unknown code %d", err);
876 ubi_err(
"this UBI version is %d, image version is %d",
890 ubi_err(
"erase counter overflow, max is %d",
912 ubi_err(
"bad image sequence number %d in PEB %d, expected %d",
959 err = check_corruption(ubi, vidh, pnum);
969 err = add_corrupted(ai, pnum, ec);
980 if (ec_err || bitflips)
990 ubi_err(
"'ubi_io_read_vid_hdr()' returned unknown code %d",
1008 ubi_msg(
"\"delete\" compatible internal volume %d:%d found, will remove it",
1011 err = add_to_list(ai, pnum, vol_id, lnum,
1018 ubi_msg(
"read-only compatible internal volume %d:%d found, switch to read-only mode",
1024 ubi_msg(
"\"preserve\" compatible internal volume %d:%d found",
1026 err = add_to_list(ai, pnum, vol_id, lnum,
1033 ubi_err(
"incompatible internal volume %d:%d found",
1040 ubi_warn(
"valid VID header but corrupted EC header at PEB %d",
1052 if (ec < ai->min_ec)
1073 int max_corr, peb_count;
1076 max_corr = peb_count / 20 ?: 8;
1084 ubi_err(
"%d PEBs are corrupted and preserved",
1086 pr_err(
"Corrupted PEBs are:");
1095 if (ai->corr_peb_count >= max_corr) {
1096 ubi_err(
"too many corrupted PEBs, refusing");
1119 ubi_msg(
"empty MTD device detected");
1123 ubi_err(
"MTD device is not UBI-formatted and possibly contains non-UBI data - refusing it");
1248 for (pnum = start; pnum < ubi->
peb_count; pnum++) {
1251 dbg_gen(
"process PEB %d", pnum);
1252 err = scan_peb(ubi, ai, pnum,
NULL,
NULL);
1257 ubi_msg(
"scanning is finished");
1263 err = late_analysis(ubi, ai);
1274 aeb->ec = ai->mean_ec;
1284 aeb->ec = ai->mean_ec;
1288 aeb->ec = ai->mean_ec;
1290 err = self_check_ai(ubi, ai);
1294 ubi_free_vid_hdr(ubi, vidh);
1300 ubi_free_vid_hdr(ubi, vidh);
1306 #ifdef CONFIG_MTD_UBI_FASTMAP
1320 int err,
pnum, fm_anchor = -1;
1321 unsigned long long max_sqnum = 0;
1335 unsigned long long sqnum = -1;
1338 dbg_gen(
"process PEB %d", pnum);
1339 err = scan_peb(ubi, ai, pnum, &vol_id, &sqnum);
1349 ubi_free_vid_hdr(ubi, vidh);
1358 ubi_free_vid_hdr(ubi, vidh);
1375 INIT_LIST_HEAD(&ai->
corr);
1376 INIT_LIST_HEAD(&ai->
free);
1377 INIT_LIST_HEAD(&ai->
erase);
1378 INIT_LIST_HEAD(&ai->
alien);
1404 ai = alloc_ai(
"ubi_aeb_slab_cache");
1408 #ifdef CONFIG_MTD_UBI_FASTMAP
1410 if ((
int)mtd_div_by_eb(ubi->
mtd->size, ubi->
mtd) <= UBI_FM_MAX_START) {
1416 err = scan_all(ubi, ai, 0);
1418 err = scan_fast(ubi, ai);
1422 ai = alloc_ai(
"ubi_aeb_slab_cache2");
1427 err = scan_all(ubi, ai, UBI_FM_MAX_START);
1431 err = scan_all(ubi, ai, 0);
1455 #ifdef CONFIG_MTD_UBI_FASTMAP
1456 if (ubi->
fm && ubi->
dbg->chk_gen) {
1459 scan_ai = alloc_ai(
"ubi_ckh_aeb_slab_cache");
1463 err = scan_all(ubi, scan_ai, 0);
1465 destroy_ai(scan_ai);
1470 destroy_ai(scan_ai);
1506 if (!ubi->
dbg->chk_gen)
1538 ubi_err(
"highest_vol_id is %d, but vol_id %d is there",
1561 if (aeb->
pnum < 0 || aeb->
ec < 0) {
1567 ubi_err(
"bad ai->min_ec (%d), %d found",
1573 ubi_err(
"bad ai->max_ec (%d), %d found",
1579 ubi_err(
"too high PEB number %d, total PEBs %d",
1586 ubi_err(
"bad lnum or used_ebs");
1597 ubi_err(
"incorrect highest_lnum or lnum");
1603 ubi_err(
"bad leb_count, %d objects in the tree",
1620 ubi_err(
"bad ai->vols_found %d, should be %d",
1637 ubi_err(
"VID header is not OK (%d)", err);
1660 if (av->
compat != vidh->compat) {
1661 ubi_err(
"bad compat %d", vidh->compat);
1703 for (pnum = 0; pnum < ubi->
peb_count; pnum++) {
1729 for (pnum = 0; pnum < ubi->peb_count; pnum++)
1731 ubi_err(
"PEB %d is not referred", pnum);
1741 ubi_err(
"bad attaching information about LEB %d", aeb->
lnum);
1747 ubi_err(
"bad attaching information about volume %d", av->
vol_id);
1752 ubi_err(
"bad attaching information about volume %d", av->
vol_id);