1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
3 * Copyright (c) Thomas Gleixner <tglx@linutronix.de>
5 * The parts taken from the kernel implementation are:
7 * Copyright (c) International Business Machines Corp., 2006
12 #include <linux/bug.h>
13 #include <u-boot/crc.h>
16 #include <linux/crc32.h>
21 * ubi_calc_fm_size - calculates the fastmap size in bytes for an UBI device.
22 * @ubi: UBI device description object
24 static size_t ubi_calc_fm_size(struct ubi_scan_info *ubi)
28 size = sizeof(struct ubi_fm_sb) +
29 sizeof(struct ubi_fm_hdr) +
30 sizeof(struct ubi_fm_scan_pool) +
31 sizeof(struct ubi_fm_scan_pool) +
32 (ubi->peb_count * sizeof(struct ubi_fm_ec)) +
33 (sizeof(struct ubi_fm_eba) +
34 (ubi->peb_count * sizeof(__be32))) +
35 sizeof(struct ubi_fm_volhdr) * UBI_MAX_VOLUMES;
36 return roundup(size, ubi->leb_size);
39 static int ubi_io_read(struct ubi_scan_info *ubi, void *buf, int pnum,
40 unsigned long from, unsigned long len)
42 return ubi->read(pnum + ubi->peb_offset, from, len, buf);
45 static int ubi_io_is_bad(struct ubi_scan_info *ubi, int peb)
47 return peb >= ubi->peb_count || peb < 0;
50 #ifdef CONFIG_SPL_UBI_LOAD_BY_VOLNAME
53 * ubi_dump_vtbl_record - dump a &struct ubi_vtbl_record object.
54 * @r: the object to dump
55 * @idx: volume table index
57 void ubi_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx)
59 int name_len = be16_to_cpu(r->name_len);
61 ubi_dbg("Volume table record %d dump: size: %d",
62 idx, sizeof(struct ubi_vtbl_record));
63 ubi_dbg("\treserved_pebs %d", be32_to_cpu(r->reserved_pebs));
64 ubi_dbg("\talignment %d", be32_to_cpu(r->alignment));
65 ubi_dbg("\tdata_pad %d", be32_to_cpu(r->data_pad));
66 ubi_dbg("\tvol_type %d", (int)r->vol_type);
67 ubi_dbg("\tupd_marker %d", (int)r->upd_marker);
68 ubi_dbg("\tname_len %d", name_len);
70 if (r->name[0] == '\0') {
71 ubi_dbg("\tname NULL");
75 if (name_len <= UBI_VOL_NAME_MAX &&
76 strnlen(&r->name[0], name_len + 1) == name_len) {
77 ubi_dbg("\tname %s", &r->name[0]);
79 ubi_dbg("\t1st 5 characters of name: %c%c%c%c%c",
80 r->name[0], r->name[1], r->name[2], r->name[3],
83 ubi_dbg("\tcrc %#08x", be32_to_cpu(r->crc));
86 /* Empty volume table record */
87 static struct ubi_vtbl_record empty_vtbl_record;
90 * vtbl_check - check if volume table is not corrupted and sensible.
91 * @ubi: UBI device description object
94 * This function returns zero if @vtbl is all right, %1 if CRC is incorrect,
95 * and %-EINVAL if it contains inconsistent data.
97 static int vtbl_check(struct ubi_scan_info *ubi,
98 struct ubi_vtbl_record *vtbl)
100 int i, n, reserved_pebs, alignment, data_pad, vol_type, name_len;
105 for (i = 0; i < UBI_SPL_VOL_IDS; i++) {
106 reserved_pebs = be32_to_cpu(vtbl[i].reserved_pebs);
107 alignment = be32_to_cpu(vtbl[i].alignment);
108 data_pad = be32_to_cpu(vtbl[i].data_pad);
109 upd_marker = vtbl[i].upd_marker;
110 vol_type = vtbl[i].vol_type;
111 name_len = be16_to_cpu(vtbl[i].name_len);
112 name = &vtbl[i].name[0];
114 crc = crc32(UBI_CRC32_INIT, &vtbl[i], UBI_VTBL_RECORD_SIZE_CRC);
115 if (be32_to_cpu(vtbl[i].crc) != crc) {
116 ubi_err("bad CRC at record %u: %#08x, not %#08x",
117 i, crc, be32_to_cpu(vtbl[i].crc));
118 ubi_dump_vtbl_record(&vtbl[i], i);
122 if (reserved_pebs == 0) {
123 if (memcmp(&vtbl[i], &empty_vtbl_record,
124 UBI_VTBL_RECORD_SIZE)) {
131 if (reserved_pebs < 0 || alignment < 0 || data_pad < 0 ||
137 if (alignment > ubi->leb_size || alignment == 0) {
142 n = alignment & (CONFIG_SPL_UBI_VID_OFFSET - 1);
143 if (alignment != 1 && n) {
148 n = ubi->leb_size % alignment;
150 ubi_err("bad data_pad, has to be %d", n);
155 if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) {
160 if (upd_marker != 0 && upd_marker != 1) {
165 if (name_len > UBI_VOL_NAME_MAX) {
170 if (name[0] == '\0') {
175 if (name_len != strnlen(name, name_len + 1)) {
180 ubi_dump_vtbl_record(&vtbl[i], i);
183 /* Checks that all names are unique */
184 for (i = 0; i < UBI_SPL_VOL_IDS - 1; i++) {
185 for (n = i + 1; n < UBI_SPL_VOL_IDS; n++) {
186 int len1 = be16_to_cpu(vtbl[i].name_len);
187 int len2 = be16_to_cpu(vtbl[n].name_len);
189 if (len1 > 0 && len1 == len2 &&
190 !strncmp(vtbl[i].name, vtbl[n].name, len1)) {
191 ubi_err("volumes %d and %d have the same name \"%s\"",
193 ubi_dump_vtbl_record(&vtbl[i], i);
194 ubi_dump_vtbl_record(&vtbl[n], n);
203 ubi_err("volume table check failed: record %d, error %d", i, err);
204 ubi_dump_vtbl_record(&vtbl[i], i);
208 static int ubi_read_volume_table(struct ubi_scan_info *ubi, u32 pnum)
212 empty_vtbl_record.crc = cpu_to_be32(0xf116c36b);
214 err = ubi_io_read(ubi, &ubi->vtbl, pnum, ubi->leb_start,
215 sizeof(struct ubi_vtbl_record) * UBI_SPL_VOL_IDS);
216 if (err && err != UBI_IO_BITFLIPS) {
217 ubi_err("unable to read volume table");
221 if (!vtbl_check(ubi, ubi->vtbl)) {
229 #endif /* CONFIG_SPL_UBI_LOAD_BY_VOLNAME */
231 static int ubi_io_read_vid_hdr(struct ubi_scan_info *ubi, int pnum,
232 struct ubi_vid_hdr *vh, int unused)
237 /* No point in rescanning a corrupt block */
238 if (test_bit(pnum, ubi->corrupt))
239 return UBI_IO_BAD_HDR;
241 * If the block has been scanned already, no need to rescan
243 if (test_and_set_bit(pnum, ubi->scanned))
246 res = ubi_io_read(ubi, vh, pnum, ubi->vid_offset, sizeof(*vh));
249 * Bad block, unrecoverable ECC error, skip the block
252 ubi_dbg("Skipping bad or unreadable block %d", pnum);
254 generic_set_bit(pnum, ubi->corrupt);
258 /* Magic number available ? */
259 magic = be32_to_cpu(vh->magic);
260 if (magic != UBI_VID_HDR_MAGIC) {
261 generic_set_bit(pnum, ubi->corrupt);
262 if (magic == 0xffffffff)
264 ubi_msg("Bad magic in block 0%d %08x", pnum, magic);
265 return UBI_IO_BAD_HDR;
268 /* Header CRC correct ? */
269 if (crc32(UBI_CRC32_INIT, vh, UBI_VID_HDR_SIZE_CRC) !=
270 be32_to_cpu(vh->hdr_crc)) {
271 ubi_msg("Bad CRC in block 0%d", pnum);
272 generic_set_bit(pnum, ubi->corrupt);
273 return UBI_IO_BAD_HDR;
276 ubi_dbg("RV: pnum: %i sqnum %llu", pnum, be64_to_cpu(vh->sqnum));
281 static int ubi_rescan_fm_vid_hdr(struct ubi_scan_info *ubi,
282 struct ubi_vid_hdr *vh,
283 u32 fm_pnum, u32 fm_vol_id, u32 fm_lnum)
287 if (ubi_io_is_bad(ubi, fm_pnum))
290 res = ubi_io_read_vid_hdr(ubi, fm_pnum, vh, 0);
292 /* Check volume id, volume type and lnum */
293 if (be32_to_cpu(vh->vol_id) == fm_vol_id &&
294 vh->vol_type == UBI_VID_STATIC &&
295 be32_to_cpu(vh->lnum) == fm_lnum)
297 ubi_dbg("RS: PEB %u vol: %u : %u typ %u lnum %u %u",
298 fm_pnum, fm_vol_id, vh->vol_type,
299 be32_to_cpu(vh->vol_id),
300 fm_lnum, be32_to_cpu(vh->lnum));
305 /* Insert the logic block into the volume info */
306 static int ubi_add_peb_to_vol(struct ubi_scan_info *ubi,
307 struct ubi_vid_hdr *vh, u32 vol_id,
310 struct ubi_vol_info *vi = ubi->volinfo + vol_id;
314 * If the volume is larger than expected, yell and give up :(
316 if (lnum >= UBI_MAX_VOL_LEBS) {
317 ubi_warn("Vol: %u LEB %d > %d", vol_id, lnum, UBI_MAX_VOL_LEBS);
321 ubi_dbg("SC: Add PEB %u to Vol %u as LEB %u fnd %d sc %d",
322 pnum, vol_id, lnum, !!test_bit(lnum, vi->found),
323 !!test_bit(pnum, ubi->scanned));
325 /* Points to the translation entry */
326 ltp = vi->lebs_to_pebs + lnum;
328 /* If the block is already assigned, check sqnum */
329 if (__test_and_set_bit(lnum, vi->found)) {
331 struct ubi_vid_hdr *cur = ubi->blockinfo + cur_pnum;
334 * If the current block hase not yet been scanned, we
335 * need to do that. The other block might be stale or
336 * the current block corrupted and the FM not yet
339 if (!test_bit(cur_pnum, ubi->scanned)) {
341 * If the scan fails, we use the valid block
343 if (ubi_rescan_fm_vid_hdr(ubi, cur, cur_pnum, vol_id,
351 * Should not happen ....
353 if (test_bit(cur_pnum, ubi->corrupt)) {
358 ubi_dbg("Vol %u LEB %u PEB %u->sqnum %llu NPEB %u->sqnum %llu",
359 vol_id, lnum, cur_pnum, be64_to_cpu(cur->sqnum), pnum,
360 be64_to_cpu(vh->sqnum));
363 * Compare sqnum and take the newer one
365 if (be64_to_cpu(cur->sqnum) < be64_to_cpu(vh->sqnum))
369 if (lnum > vi->last_block)
370 vi->last_block = lnum;
376 static int ubi_scan_vid_hdr(struct ubi_scan_info *ubi, struct ubi_vid_hdr *vh,
382 if (ubi_io_is_bad(ubi, pnum))
385 res = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
390 vol_id = be32_to_cpu(vh->vol_id);
392 /* If this is the fastmap anchor, return right away */
393 if (vol_id == UBI_FM_SB_VOLUME_ID)
394 return ubi->fm_enabled ? UBI_FASTMAP_ANCHOR : 0;
396 #ifdef CONFIG_SPL_UBI_LOAD_BY_VOLNAME
397 /* If this is a UBI volume table, read it and return */
398 if (vol_id == UBI_LAYOUT_VOLUME_ID && !ubi->vtbl_valid) {
399 res = ubi_read_volume_table(ubi, pnum);
404 /* We only care about static volumes with an id < UBI_SPL_VOL_IDS */
405 if (vol_id >= UBI_SPL_VOL_IDS || vh->vol_type != UBI_VID_STATIC)
408 #ifndef CONFIG_SPL_UBI_LOAD_BY_VOLNAME
409 /* We are only interested in the volumes to load */
410 if (!test_bit(vol_id, ubi->toload))
413 lnum = be32_to_cpu(vh->lnum);
414 return ubi_add_peb_to_vol(ubi, vh, vol_id, pnum, lnum);
417 static int assign_aeb_to_av(struct ubi_scan_info *ubi, u32 pnum, u32 lnum,
418 u32 vol_id, u32 vol_type, u32 used)
420 struct ubi_vid_hdr *vh;
422 if (ubi_io_is_bad(ubi, pnum))
427 #ifndef CONFIG_SPL_UBI_LOAD_BY_VOLNAME
428 if (vol_id >= UBI_SPL_VOL_IDS || vol_type != UBI_STATIC_VOLUME)
431 /* We are only interested in the volumes to load */
432 if (!test_bit(vol_id, ubi->toload))
435 vh = ubi->blockinfo + pnum;
437 return ubi_scan_vid_hdr(ubi, vh, pnum);
440 static int scan_pool(struct ubi_scan_info *ubi, __be32 *pebs, int pool_size)
442 struct ubi_vid_hdr *vh;
446 ubi_dbg("Scanning pool size: %d", pool_size);
448 for (i = 0; i < pool_size; i++) {
449 pnum = be32_to_cpu(pebs[i]);
451 if (ubi_io_is_bad(ubi, pnum)) {
452 ubi_err("FM: Bad PEB in fastmap pool! %u", pnum);
453 return UBI_BAD_FASTMAP;
456 vh = ubi->blockinfo + pnum;
458 * We allow the scan to fail here. The loader will notice
459 * and look for a replacement.
461 ubi_scan_vid_hdr(ubi, vh, pnum);
467 * Fastmap code is stolen from Linux kernel and this stub structure is used
470 struct ubi_attach_info {
474 static int ubi_attach_fastmap(struct ubi_scan_info *ubi,
475 struct ubi_attach_info *ai,
476 struct ubi_fastmap_layout *fm)
478 struct ubi_fm_hdr *fmhdr;
479 struct ubi_fm_scan_pool *fmpl1, *fmpl2;
480 struct ubi_fm_ec *fmec;
481 struct ubi_fm_volhdr *fmvhdr;
482 struct ubi_fm_eba *fm_eba;
483 int ret, i, j, pool_size, wl_pool_size;
484 size_t fm_pos = 0, fm_size = ubi->fm_size;
485 void *fm_raw = ubi->fm_buf;
487 memset(ubi->fm_used, 0, sizeof(ubi->fm_used));
489 fm_pos += sizeof(struct ubi_fm_sb);
490 if (fm_pos >= fm_size)
493 fmhdr = (struct ubi_fm_hdr *)(fm_raw + fm_pos);
494 fm_pos += sizeof(*fmhdr);
495 if (fm_pos >= fm_size)
498 if (be32_to_cpu(fmhdr->magic) != UBI_FM_HDR_MAGIC) {
499 ubi_err("bad fastmap header magic: 0x%x, expected: 0x%x",
500 be32_to_cpu(fmhdr->magic), UBI_FM_HDR_MAGIC);
504 fmpl1 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
505 fm_pos += sizeof(*fmpl1);
506 if (fm_pos >= fm_size)
508 if (be32_to_cpu(fmpl1->magic) != UBI_FM_POOL_MAGIC) {
509 ubi_err("bad fastmap pool magic: 0x%x, expected: 0x%x",
510 be32_to_cpu(fmpl1->magic), UBI_FM_POOL_MAGIC);
514 fmpl2 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
515 fm_pos += sizeof(*fmpl2);
516 if (fm_pos >= fm_size)
518 if (be32_to_cpu(fmpl2->magic) != UBI_FM_POOL_MAGIC) {
519 ubi_err("bad fastmap pool magic: 0x%x, expected: 0x%x",
520 be32_to_cpu(fmpl2->magic), UBI_FM_POOL_MAGIC);
524 pool_size = be16_to_cpu(fmpl1->size);
525 wl_pool_size = be16_to_cpu(fmpl2->size);
526 fm->max_pool_size = be16_to_cpu(fmpl1->max_size);
527 fm->max_wl_pool_size = be16_to_cpu(fmpl2->max_size);
529 if (pool_size > UBI_FM_MAX_POOL_SIZE || pool_size < 0) {
530 ubi_err("bad pool size: %i", pool_size);
534 if (wl_pool_size > UBI_FM_MAX_POOL_SIZE || wl_pool_size < 0) {
535 ubi_err("bad WL pool size: %i", wl_pool_size);
539 if (fm->max_pool_size > UBI_FM_MAX_POOL_SIZE ||
540 fm->max_pool_size < 0) {
541 ubi_err("bad maximal pool size: %i", fm->max_pool_size);
545 if (fm->max_wl_pool_size > UBI_FM_MAX_POOL_SIZE ||
546 fm->max_wl_pool_size < 0) {
547 ubi_err("bad maximal WL pool size: %i", fm->max_wl_pool_size);
551 /* read EC values from free list */
552 for (i = 0; i < be32_to_cpu(fmhdr->free_peb_count); i++) {
553 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
554 fm_pos += sizeof(*fmec);
555 if (fm_pos >= fm_size)
559 /* read EC values from used list */
560 for (i = 0; i < be32_to_cpu(fmhdr->used_peb_count); i++) {
561 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
562 fm_pos += sizeof(*fmec);
563 if (fm_pos >= fm_size)
566 generic_set_bit(be32_to_cpu(fmec->pnum), ubi->fm_used);
569 /* read EC values from scrub list */
570 for (i = 0; i < be32_to_cpu(fmhdr->scrub_peb_count); i++) {
571 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
572 fm_pos += sizeof(*fmec);
573 if (fm_pos >= fm_size)
577 /* read EC values from erase list */
578 for (i = 0; i < be32_to_cpu(fmhdr->erase_peb_count); i++) {
579 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
580 fm_pos += sizeof(*fmec);
581 if (fm_pos >= fm_size)
585 /* Iterate over all volumes and read their EBA table */
586 for (i = 0; i < be32_to_cpu(fmhdr->vol_count); i++) {
587 u32 vol_id, vol_type, used, reserved;
589 fmvhdr = (struct ubi_fm_volhdr *)(fm_raw + fm_pos);
590 fm_pos += sizeof(*fmvhdr);
591 if (fm_pos >= fm_size)
594 if (be32_to_cpu(fmvhdr->magic) != UBI_FM_VHDR_MAGIC) {
595 ubi_err("bad fastmap vol header magic: 0x%x, " \
597 be32_to_cpu(fmvhdr->magic), UBI_FM_VHDR_MAGIC);
601 vol_id = be32_to_cpu(fmvhdr->vol_id);
602 vol_type = fmvhdr->vol_type;
603 used = be32_to_cpu(fmvhdr->used_ebs);
605 fm_eba = (struct ubi_fm_eba *)(fm_raw + fm_pos);
606 fm_pos += sizeof(*fm_eba);
607 fm_pos += (sizeof(__be32) * be32_to_cpu(fm_eba->reserved_pebs));
608 if (fm_pos >= fm_size)
611 if (be32_to_cpu(fm_eba->magic) != UBI_FM_EBA_MAGIC) {
612 ubi_err("bad fastmap EBA header magic: 0x%x, " \
614 be32_to_cpu(fm_eba->magic), UBI_FM_EBA_MAGIC);
618 reserved = be32_to_cpu(fm_eba->reserved_pebs);
619 ubi_dbg("FA: vol %u used %u res: %u", vol_id, used, reserved);
620 for (j = 0; j < reserved; j++) {
621 int pnum = be32_to_cpu(fm_eba->pnum[j]);
623 if ((int)be32_to_cpu(fm_eba->pnum[j]) < 0)
626 if (!__test_and_clear_bit(pnum, ubi->fm_used))
630 * We only handle static volumes so used_ebs
631 * needs to be handed in. And we do not assign
632 * the reserved blocks
637 ret = assign_aeb_to_av(ubi, pnum, j, vol_id,
643 * Nasty: The fastmap claims that the volume
644 * has one block more than it, but that block
645 * is always empty and the other blocks have
646 * the correct number of total LEBs in the
647 * headers. Deal with it.
649 if (ret != UBI_IO_FF && j != used - 1)
651 ubi_dbg("FA: Vol: %u Ignoring empty LEB %d of %d",
656 ret = scan_pool(ubi, fmpl1->pebs, pool_size);
660 ret = scan_pool(ubi, fmpl2->pebs, wl_pool_size);
666 * If fastmap is leaking PEBs (must not happen), raise a
667 * fat warning and fall back to scanning mode.
668 * We do this here because in ubi_wl_init() it's too late
669 * and we cannot fall back to scanning.
671 if (WARN_ON(count_fastmap_pebs(ai) != ubi->peb_count -
672 ai->bad_peb_count - fm->used_blocks))
679 ret = UBI_BAD_FASTMAP;
684 static int ubi_scan_fastmap(struct ubi_scan_info *ubi,
685 struct ubi_attach_info *ai,
688 struct ubi_fm_sb *fmsb, *fmsb2;
689 struct ubi_vid_hdr *vh;
690 struct ubi_fastmap_layout *fm;
691 int i, used_blocks, pnum, ret = 0;
694 unsigned long long sqnum = 0;
697 fm = &ubi->fm_layout;
699 ret = ubi_io_read(ubi, fmsb, fm_anchor, ubi->leb_start, sizeof(*fmsb));
700 if (ret && ret != UBI_IO_BITFLIPS)
702 else if (ret == UBI_IO_BITFLIPS)
703 fm->to_be_tortured[0] = 1;
705 if (be32_to_cpu(fmsb->magic) != UBI_FM_SB_MAGIC) {
706 ubi_err("bad super block magic: 0x%x, expected: 0x%x",
707 be32_to_cpu(fmsb->magic), UBI_FM_SB_MAGIC);
708 ret = UBI_BAD_FASTMAP;
712 if (fmsb->version != UBI_FM_FMT_VERSION) {
713 ubi_err("bad fastmap version: %i, expected: %i",
714 fmsb->version, UBI_FM_FMT_VERSION);
715 ret = UBI_BAD_FASTMAP;
719 used_blocks = be32_to_cpu(fmsb->used_blocks);
720 if (used_blocks > UBI_FM_MAX_BLOCKS || used_blocks < 1) {
721 ubi_err("number of fastmap blocks is invalid: %i", used_blocks);
722 ret = UBI_BAD_FASTMAP;
726 fm_size = ubi->leb_size * used_blocks;
727 if (fm_size != ubi->fm_size) {
728 ubi_err("bad fastmap size: %zi, expected: %zi", fm_size,
730 ret = UBI_BAD_FASTMAP;
736 for (i = 0; i < used_blocks; i++) {
737 pnum = be32_to_cpu(fmsb->block_loc[i]);
739 if (ubi_io_is_bad(ubi, pnum)) {
740 ret = UBI_BAD_FASTMAP;
746 ret = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
747 if (ret && ret != UBI_IO_BITFLIPS) {
748 ubi_err("unable to read fastmap block# %i EC (PEB: %i)",
751 ret = UBI_BAD_FASTMAP;
753 } else if (ret == UBI_IO_BITFLIPS)
754 fm->to_be_tortured[i] = 1;
756 image_seq = be32_to_cpu(ech->image_seq);
758 ubi->image_seq = image_seq;
760 * Older UBI implementations have image_seq set to zero, so
761 * we shouldn't fail if image_seq == 0.
763 if (image_seq && (image_seq != ubi->image_seq)) {
764 ubi_err("wrong image seq:%d instead of %d",
765 be32_to_cpu(ech->image_seq), ubi->image_seq);
766 ret = UBI_BAD_FASTMAP;
770 ret = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
771 if (ret && ret != UBI_IO_BITFLIPS) {
772 ubi_err("unable to read fastmap block# %i (PEB: %i)",
778 * Mainline code rescans the anchor header. We've done
779 * that already so we merily copy it over.
781 if (pnum == fm_anchor)
782 memcpy(vh, ubi->blockinfo + pnum, sizeof(*fm));
785 if (be32_to_cpu(vh->vol_id) != UBI_FM_SB_VOLUME_ID) {
786 ubi_err("bad fastmap anchor vol_id: 0x%x," \
788 be32_to_cpu(vh->vol_id),
789 UBI_FM_SB_VOLUME_ID);
790 ret = UBI_BAD_FASTMAP;
794 if (be32_to_cpu(vh->vol_id) != UBI_FM_DATA_VOLUME_ID) {
795 ubi_err("bad fastmap data vol_id: 0x%x," \
797 be32_to_cpu(vh->vol_id),
798 UBI_FM_DATA_VOLUME_ID);
799 ret = UBI_BAD_FASTMAP;
804 if (sqnum < be64_to_cpu(vh->sqnum))
805 sqnum = be64_to_cpu(vh->sqnum);
807 ret = ubi_io_read(ubi, ubi->fm_buf + (ubi->leb_size * i), pnum,
808 ubi->leb_start, ubi->leb_size);
809 if (ret && ret != UBI_IO_BITFLIPS) {
810 ubi_err("unable to read fastmap block# %i (PEB: %i, " \
811 "err: %i)", i, pnum, ret);
816 fmsb2 = (struct ubi_fm_sb *)(ubi->fm_buf);
817 tmp_crc = be32_to_cpu(fmsb2->data_crc);
819 crc = crc32(UBI_CRC32_INIT, ubi->fm_buf, fm_size);
820 if (crc != tmp_crc) {
821 ubi_err("fastmap data CRC is invalid");
822 ubi_err("CRC should be: 0x%x, calc: 0x%x", tmp_crc, crc);
823 ret = UBI_BAD_FASTMAP;
827 fmsb2->sqnum = sqnum;
829 fm->used_blocks = used_blocks;
831 ret = ubi_attach_fastmap(ubi, ai, fm);
834 ret = UBI_BAD_FASTMAP;
839 ubi->fm_pool.max_size = ubi->fm->max_pool_size;
840 ubi->fm_wl_pool.max_size = ubi->fm->max_wl_pool_size;
841 ubi_msg("attached by fastmap %uMB %u blocks",
842 ubi->fsize_mb, ubi->peb_count);
843 ubi_dbg("fastmap pool size: %d", ubi->fm_pool.max_size);
844 ubi_dbg("fastmap WL pool size: %d", ubi->fm_wl_pool.max_size);
848 ubi_err("Attach by fastmap failed, doing a full scan!");
857 * Scan the flash and attempt to attach via fastmap
859 static void ipl_scan(struct ubi_scan_info *ubi)
865 * Scan first for the fastmap super block
867 for (pnum = 0; pnum < UBI_FM_MAX_START; pnum++) {
868 res = ubi_scan_vid_hdr(ubi, ubi->blockinfo + pnum, pnum);
870 * We ignore errors here as we are meriliy scanning
873 if (res != UBI_FASTMAP_ANCHOR)
877 * If fastmap is disabled, continue scanning. This
878 * might happen because the previous attempt failed or
879 * the caller disabled it right away.
881 if (!ubi->fm_enabled)
885 * Try to attach the fastmap, if that fails continue
888 if (!ubi_scan_fastmap(ubi, NULL, pnum))
891 * Fastmap failed. Clear everything we have and start
892 * over. We are paranoid and do not trust anything.
894 memset(ubi->volinfo, 0, sizeof(ubi->volinfo));
900 * Continue scanning, ignore errors, we might find what we are
903 for (; pnum < ubi->peb_count; pnum++)
904 ubi_scan_vid_hdr(ubi, ubi->blockinfo + pnum, pnum);
908 * Load a logical block of a volume into memory
910 static int ubi_load_block(struct ubi_scan_info *ubi, uint8_t *laddr,
911 struct ubi_vol_info *vi, u32 vol_id, u32 lnum,
914 struct ubi_vid_hdr *vh, *vrepl;
919 * If this is a fastmap run, we try to rescan full, otherwise
922 if (!test_bit(lnum, vi->found)) {
923 ubi_warn("LEB %d of %d is missing", lnum, last);
927 pnum = vi->lebs_to_pebs[lnum];
929 ubi_dbg("Load vol %u LEB %u PEB %u", vol_id, lnum, pnum);
931 if (ubi_io_is_bad(ubi, pnum)) {
932 ubi_warn("Corrupted mapping block %d PB %d\n", lnum, pnum);
936 if (test_bit(pnum, ubi->corrupt))
940 * Lets try to read that block
942 vh = ubi->blockinfo + pnum;
944 if (!test_bit(pnum, ubi->scanned)) {
945 ubi_warn("Vol: %u LEB %u PEB %u not yet scanned", vol_id,
947 if (ubi_rescan_fm_vid_hdr(ubi, vh, pnum, vol_id, lnum))
952 * Check, if the total number of blocks is correct
954 if (be32_to_cpu(vh->used_ebs) != last) {
955 ubi_dbg("Block count missmatch.");
956 ubi_dbg("vh->used_ebs: %d nrblocks: %d",
957 be32_to_cpu(vh->used_ebs), last);
958 generic_set_bit(pnum, ubi->corrupt);
963 * Get the data length of this block.
965 dlen = be32_to_cpu(vh->data_size);
968 * Read the data into RAM. We ignore the return value
969 * here as the only thing which might go wrong are
970 * bitflips. Try nevertheless.
972 ubi_io_read(ubi, laddr, pnum, ubi->leb_start, dlen);
974 /* Calculate CRC over the data */
975 crc = crc32(UBI_CRC32_INIT, laddr, dlen);
977 if (crc != be32_to_cpu(vh->data_crc)) {
978 ubi_warn("Vol: %u LEB %u PEB %u data CRC failure", vol_id,
980 generic_set_bit(pnum, ubi->corrupt);
984 /* We are good. Return the data length we read */
988 ubi_dbg("Find replacement for LEB %u PEB %u", lnum, pnum);
989 generic_clear_bit(lnum, vi->found);
992 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
993 struct ubi_vid_hdr *tmp = ubi->blockinfo + pnum;
994 u32 t_vol_id = be32_to_cpu(tmp->vol_id);
995 u32 t_lnum = be32_to_cpu(tmp->lnum);
997 if (test_bit(pnum, ubi->corrupt))
1000 if (t_vol_id != vol_id || t_lnum != lnum)
1003 if (!test_bit(pnum, ubi->scanned)) {
1004 ubi_warn("Vol: %u LEB %u PEB %u not yet scanned",
1005 vol_id, lnum, pnum);
1006 if (ubi_rescan_fm_vid_hdr(ubi, tmp, pnum, vol_id, lnum))
1011 * We found one. If its the first, assign it otherwise
1014 generic_set_bit(lnum, vi->found);
1021 if (be64_to_cpu(vrepl->sqnum) < be64_to_cpu(tmp->sqnum))
1026 /* Update the vi table */
1027 pnum = vrepl - ubi->blockinfo;
1028 vi->lebs_to_pebs[lnum] = pnum;
1029 ubi_dbg("Trying PEB %u for LEB %u", pnum, lnum);
1036 * Load a volume into RAM
1038 static int ipl_load(struct ubi_scan_info *ubi, const u32 vol_id, uint8_t *laddr)
1040 struct ubi_vol_info *vi;
1041 u32 lnum, last, len;
1043 if (vol_id >= UBI_SPL_VOL_IDS)
1047 vi = ubi->volinfo + vol_id;
1048 last = vi->last_block + 1;
1050 /* Read the blocks to RAM, check CRC */
1051 for (lnum = 0 ; lnum < last; lnum++) {
1052 int res = ubi_load_block(ubi, laddr, vi, vol_id, lnum, last);
1055 ubi_warn("Failed to load volume %u", vol_id);
1058 /* res is the data length of the read block */
1065 int ubispl_load_volumes(struct ubispl_info *info, struct ubispl_load *lvols,
1068 struct ubi_scan_info *ubi = info->ubi;
1069 int res, i, fastmap = info->fastmap;
1074 * We do a partial initializiation of @ubi. Cleaning fm_buf is
1077 memset(ubi, 0, offsetof(struct ubi_scan_info, fm_buf));
1079 ubi->read = info->read;
1081 /* Precalculate the offsets */
1082 ubi->vid_offset = info->vid_offset;
1083 ubi->leb_start = info->leb_start;
1084 ubi->leb_size = info->peb_size - ubi->leb_start;
1085 ubi->peb_count = info->peb_count;
1086 ubi->peb_offset = info->peb_offset;
1088 #ifdef CONFIG_SPL_UBI_LOAD_BY_VOLNAME
1089 ubi->vtbl_valid = 0;
1092 fsize = info->peb_size * info->peb_count;
1093 ubi->fsize_mb = fsize >> 20;
1096 ubi->fm_size = ubi_calc_fm_size(ubi);
1097 ubi->fm_enabled = fastmap;
1099 for (i = 0; i < nrvols; i++) {
1100 struct ubispl_load *lv = lvols + i;
1102 generic_set_bit(lv->vol_id, ubi->toload);
1107 for (i = 0; i < nrvols; i++) {
1108 struct ubispl_load *lv = lvols + i;
1110 #ifdef CONFIG_SPL_UBI_LOAD_BY_VOLNAME
1111 if (lv->vol_id == -1) {
1112 for (int j = 0; j < UBI_SPL_VOL_IDS; j++) {
1113 int len = be16_to_cpu(ubi->vtbl[j].name_len);
1115 if (strncmp(lv->name,
1123 ubi_msg("Loading VolName %s (VolId #%d)", lv->name, lv->vol_id);
1125 ubi_msg("Loading VolId #%d", lv->vol_id);
1127 res = ipl_load(ubi, lv->vol_id, lv->load_addr);