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 <u-boot/crc.h>
15 #include <linux/crc32.h>
20 * ubi_calc_fm_size - calculates the fastmap size in bytes for an UBI device.
21 * @ubi: UBI device description object
23 static size_t ubi_calc_fm_size(struct ubi_scan_info *ubi)
27 size = sizeof(struct ubi_fm_sb) +
28 sizeof(struct ubi_fm_hdr) +
29 sizeof(struct ubi_fm_scan_pool) +
30 sizeof(struct ubi_fm_scan_pool) +
31 (ubi->peb_count * sizeof(struct ubi_fm_ec)) +
32 (sizeof(struct ubi_fm_eba) +
33 (ubi->peb_count * sizeof(__be32))) +
34 sizeof(struct ubi_fm_volhdr) * UBI_MAX_VOLUMES;
35 return roundup(size, ubi->leb_size);
38 static int ubi_io_read(struct ubi_scan_info *ubi, void *buf, int pnum,
39 unsigned long from, unsigned long len)
41 return ubi->read(pnum + ubi->peb_offset, from, len, buf);
44 static int ubi_io_is_bad(struct ubi_scan_info *ubi, int peb)
46 return peb >= ubi->peb_count || peb < 0;
49 #ifdef CONFIG_SPL_UBI_LOAD_BY_VOLNAME
52 * ubi_dump_vtbl_record - dump a &struct ubi_vtbl_record object.
53 * @r: the object to dump
54 * @idx: volume table index
56 void ubi_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx)
58 int name_len = be16_to_cpu(r->name_len);
60 ubi_dbg("Volume table record %d dump: size: %d",
61 idx, sizeof(struct ubi_vtbl_record));
62 ubi_dbg("\treserved_pebs %d", be32_to_cpu(r->reserved_pebs));
63 ubi_dbg("\talignment %d", be32_to_cpu(r->alignment));
64 ubi_dbg("\tdata_pad %d", be32_to_cpu(r->data_pad));
65 ubi_dbg("\tvol_type %d", (int)r->vol_type);
66 ubi_dbg("\tupd_marker %d", (int)r->upd_marker);
67 ubi_dbg("\tname_len %d", name_len);
69 if (r->name[0] == '\0') {
70 ubi_dbg("\tname NULL");
74 if (name_len <= UBI_VOL_NAME_MAX &&
75 strnlen(&r->name[0], name_len + 1) == name_len) {
76 ubi_dbg("\tname %s", &r->name[0]);
78 ubi_dbg("\t1st 5 characters of name: %c%c%c%c%c",
79 r->name[0], r->name[1], r->name[2], r->name[3],
82 ubi_dbg("\tcrc %#08x", be32_to_cpu(r->crc));
85 /* Empty volume table record */
86 static struct ubi_vtbl_record empty_vtbl_record;
89 * vtbl_check - check if volume table is not corrupted and sensible.
90 * @ubi: UBI device description object
93 * This function returns zero if @vtbl is all right, %1 if CRC is incorrect,
94 * and %-EINVAL if it contains inconsistent data.
96 static int vtbl_check(struct ubi_scan_info *ubi,
97 struct ubi_vtbl_record *vtbl)
99 int i, n, reserved_pebs, alignment, data_pad, vol_type, name_len;
104 for (i = 0; i < UBI_SPL_VOL_IDS; i++) {
105 reserved_pebs = be32_to_cpu(vtbl[i].reserved_pebs);
106 alignment = be32_to_cpu(vtbl[i].alignment);
107 data_pad = be32_to_cpu(vtbl[i].data_pad);
108 upd_marker = vtbl[i].upd_marker;
109 vol_type = vtbl[i].vol_type;
110 name_len = be16_to_cpu(vtbl[i].name_len);
111 name = &vtbl[i].name[0];
113 crc = crc32(UBI_CRC32_INIT, &vtbl[i], UBI_VTBL_RECORD_SIZE_CRC);
114 if (be32_to_cpu(vtbl[i].crc) != crc) {
115 ubi_err("bad CRC at record %u: %#08x, not %#08x",
116 i, crc, be32_to_cpu(vtbl[i].crc));
117 ubi_dump_vtbl_record(&vtbl[i], i);
121 if (reserved_pebs == 0) {
122 if (memcmp(&vtbl[i], &empty_vtbl_record,
123 UBI_VTBL_RECORD_SIZE)) {
130 if (reserved_pebs < 0 || alignment < 0 || data_pad < 0 ||
136 if (alignment > ubi->leb_size || alignment == 0) {
141 n = alignment & (CONFIG_SPL_UBI_VID_OFFSET - 1);
142 if (alignment != 1 && n) {
147 n = ubi->leb_size % alignment;
149 ubi_err("bad data_pad, has to be %d", n);
154 if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) {
159 if (upd_marker != 0 && upd_marker != 1) {
164 if (name_len > UBI_VOL_NAME_MAX) {
169 if (name[0] == '\0') {
174 if (name_len != strnlen(name, name_len + 1)) {
179 ubi_dump_vtbl_record(&vtbl[i], i);
182 /* Checks that all names are unique */
183 for (i = 0; i < UBI_SPL_VOL_IDS - 1; i++) {
184 for (n = i + 1; n < UBI_SPL_VOL_IDS; n++) {
185 int len1 = be16_to_cpu(vtbl[i].name_len);
186 int len2 = be16_to_cpu(vtbl[n].name_len);
188 if (len1 > 0 && len1 == len2 &&
189 !strncmp(vtbl[i].name, vtbl[n].name, len1)) {
190 ubi_err("volumes %d and %d have the same name \"%s\"",
192 ubi_dump_vtbl_record(&vtbl[i], i);
193 ubi_dump_vtbl_record(&vtbl[n], n);
202 ubi_err("volume table check failed: record %d, error %d", i, err);
203 ubi_dump_vtbl_record(&vtbl[i], i);
207 static int ubi_read_volume_table(struct ubi_scan_info *ubi, u32 pnum)
211 empty_vtbl_record.crc = cpu_to_be32(0xf116c36b);
213 err = ubi_io_read(ubi, &ubi->vtbl, pnum, ubi->leb_start,
214 sizeof(struct ubi_vtbl_record) * UBI_SPL_VOL_IDS);
215 if (err && err != UBI_IO_BITFLIPS) {
216 ubi_err("unable to read volume table");
220 if (!vtbl_check(ubi, ubi->vtbl)) {
228 #endif /* CONFIG_SPL_UBI_LOAD_BY_VOLNAME */
230 static int ubi_io_read_vid_hdr(struct ubi_scan_info *ubi, int pnum,
231 struct ubi_vid_hdr *vh, int unused)
236 /* No point in rescanning a corrupt block */
237 if (test_bit(pnum, ubi->corrupt))
238 return UBI_IO_BAD_HDR;
240 * If the block has been scanned already, no need to rescan
242 if (test_and_set_bit(pnum, ubi->scanned))
245 res = ubi_io_read(ubi, vh, pnum, ubi->vid_offset, sizeof(*vh));
248 * Bad block, unrecoverable ECC error, skip the block
251 ubi_dbg("Skipping bad or unreadable block %d", pnum);
253 generic_set_bit(pnum, ubi->corrupt);
257 /* Magic number available ? */
258 magic = be32_to_cpu(vh->magic);
259 if (magic != UBI_VID_HDR_MAGIC) {
260 generic_set_bit(pnum, ubi->corrupt);
261 if (magic == 0xffffffff)
263 ubi_msg("Bad magic in block 0%d %08x", pnum, magic);
264 return UBI_IO_BAD_HDR;
267 /* Header CRC correct ? */
268 if (crc32(UBI_CRC32_INIT, vh, UBI_VID_HDR_SIZE_CRC) !=
269 be32_to_cpu(vh->hdr_crc)) {
270 ubi_msg("Bad CRC in block 0%d", pnum);
271 generic_set_bit(pnum, ubi->corrupt);
272 return UBI_IO_BAD_HDR;
275 ubi_dbg("RV: pnum: %i sqnum %llu", pnum, be64_to_cpu(vh->sqnum));
280 static int ubi_rescan_fm_vid_hdr(struct ubi_scan_info *ubi,
281 struct ubi_vid_hdr *vh,
282 u32 fm_pnum, u32 fm_vol_id, u32 fm_lnum)
286 if (ubi_io_is_bad(ubi, fm_pnum))
289 res = ubi_io_read_vid_hdr(ubi, fm_pnum, vh, 0);
291 /* Check volume id, volume type and lnum */
292 if (be32_to_cpu(vh->vol_id) == fm_vol_id &&
293 vh->vol_type == UBI_VID_STATIC &&
294 be32_to_cpu(vh->lnum) == fm_lnum)
296 ubi_dbg("RS: PEB %u vol: %u : %u typ %u lnum %u %u",
297 fm_pnum, fm_vol_id, vh->vol_type,
298 be32_to_cpu(vh->vol_id),
299 fm_lnum, be32_to_cpu(vh->lnum));
304 /* Insert the logic block into the volume info */
305 static int ubi_add_peb_to_vol(struct ubi_scan_info *ubi,
306 struct ubi_vid_hdr *vh, u32 vol_id,
309 struct ubi_vol_info *vi = ubi->volinfo + vol_id;
313 * If the volume is larger than expected, yell and give up :(
315 if (lnum >= UBI_MAX_VOL_LEBS) {
316 ubi_warn("Vol: %u LEB %d > %d", vol_id, lnum, UBI_MAX_VOL_LEBS);
320 ubi_dbg("SC: Add PEB %u to Vol %u as LEB %u fnd %d sc %d",
321 pnum, vol_id, lnum, !!test_bit(lnum, vi->found),
322 !!test_bit(pnum, ubi->scanned));
324 /* Points to the translation entry */
325 ltp = vi->lebs_to_pebs + lnum;
327 /* If the block is already assigned, check sqnum */
328 if (__test_and_set_bit(lnum, vi->found)) {
330 struct ubi_vid_hdr *cur = ubi->blockinfo + cur_pnum;
333 * If the current block hase not yet been scanned, we
334 * need to do that. The other block might be stale or
335 * the current block corrupted and the FM not yet
338 if (!test_bit(cur_pnum, ubi->scanned)) {
340 * If the scan fails, we use the valid block
342 if (ubi_rescan_fm_vid_hdr(ubi, cur, cur_pnum, vol_id,
350 * Should not happen ....
352 if (test_bit(cur_pnum, ubi->corrupt)) {
357 ubi_dbg("Vol %u LEB %u PEB %u->sqnum %llu NPEB %u->sqnum %llu",
358 vol_id, lnum, cur_pnum, be64_to_cpu(cur->sqnum), pnum,
359 be64_to_cpu(vh->sqnum));
362 * Compare sqnum and take the newer one
364 if (be64_to_cpu(cur->sqnum) < be64_to_cpu(vh->sqnum))
368 if (lnum > vi->last_block)
369 vi->last_block = lnum;
375 static int ubi_scan_vid_hdr(struct ubi_scan_info *ubi, struct ubi_vid_hdr *vh,
381 if (ubi_io_is_bad(ubi, pnum))
384 res = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
389 vol_id = be32_to_cpu(vh->vol_id);
391 /* If this is the fastmap anchor, return right away */
392 if (vol_id == UBI_FM_SB_VOLUME_ID)
393 return ubi->fm_enabled ? UBI_FASTMAP_ANCHOR : 0;
395 #ifdef CONFIG_SPL_UBI_LOAD_BY_VOLNAME
396 /* If this is a UBI volume table, read it and return */
397 if (vol_id == UBI_LAYOUT_VOLUME_ID && !ubi->vtbl_valid) {
398 res = ubi_read_volume_table(ubi, pnum);
403 /* We only care about static volumes with an id < UBI_SPL_VOL_IDS */
404 if (vol_id >= UBI_SPL_VOL_IDS || vh->vol_type != UBI_VID_STATIC)
407 #ifndef CONFIG_SPL_UBI_LOAD_BY_VOLNAME
408 /* We are only interested in the volumes to load */
409 if (!test_bit(vol_id, ubi->toload))
412 lnum = be32_to_cpu(vh->lnum);
413 return ubi_add_peb_to_vol(ubi, vh, vol_id, pnum, lnum);
416 static int assign_aeb_to_av(struct ubi_scan_info *ubi, u32 pnum, u32 lnum,
417 u32 vol_id, u32 vol_type, u32 used)
419 struct ubi_vid_hdr *vh;
421 if (ubi_io_is_bad(ubi, pnum))
426 #ifndef CONFIG_SPL_UBI_LOAD_BY_VOLNAME
427 if (vol_id >= UBI_SPL_VOL_IDS || vol_type != UBI_STATIC_VOLUME)
430 /* We are only interested in the volumes to load */
431 if (!test_bit(vol_id, ubi->toload))
434 vh = ubi->blockinfo + pnum;
436 return ubi_scan_vid_hdr(ubi, vh, pnum);
439 static int scan_pool(struct ubi_scan_info *ubi, __be32 *pebs, int pool_size)
441 struct ubi_vid_hdr *vh;
445 ubi_dbg("Scanning pool size: %d", pool_size);
447 for (i = 0; i < pool_size; i++) {
448 pnum = be32_to_cpu(pebs[i]);
450 if (ubi_io_is_bad(ubi, pnum)) {
451 ubi_err("FM: Bad PEB in fastmap pool! %u", pnum);
452 return UBI_BAD_FASTMAP;
455 vh = ubi->blockinfo + pnum;
457 * We allow the scan to fail here. The loader will notice
458 * and look for a replacement.
460 ubi_scan_vid_hdr(ubi, vh, pnum);
466 * Fastmap code is stolen from Linux kernel and this stub structure is used
469 struct ubi_attach_info {
473 static int ubi_attach_fastmap(struct ubi_scan_info *ubi,
474 struct ubi_attach_info *ai,
475 struct ubi_fastmap_layout *fm)
477 struct ubi_fm_hdr *fmhdr;
478 struct ubi_fm_scan_pool *fmpl1, *fmpl2;
479 struct ubi_fm_ec *fmec;
480 struct ubi_fm_volhdr *fmvhdr;
481 struct ubi_fm_eba *fm_eba;
482 int ret, i, j, pool_size, wl_pool_size;
483 size_t fm_pos = 0, fm_size = ubi->fm_size;
484 void *fm_raw = ubi->fm_buf;
486 memset(ubi->fm_used, 0, sizeof(ubi->fm_used));
488 fm_pos += sizeof(struct ubi_fm_sb);
489 if (fm_pos >= fm_size)
492 fmhdr = (struct ubi_fm_hdr *)(fm_raw + fm_pos);
493 fm_pos += sizeof(*fmhdr);
494 if (fm_pos >= fm_size)
497 if (be32_to_cpu(fmhdr->magic) != UBI_FM_HDR_MAGIC) {
498 ubi_err("bad fastmap header magic: 0x%x, expected: 0x%x",
499 be32_to_cpu(fmhdr->magic), UBI_FM_HDR_MAGIC);
503 fmpl1 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
504 fm_pos += sizeof(*fmpl1);
505 if (fm_pos >= fm_size)
507 if (be32_to_cpu(fmpl1->magic) != UBI_FM_POOL_MAGIC) {
508 ubi_err("bad fastmap pool magic: 0x%x, expected: 0x%x",
509 be32_to_cpu(fmpl1->magic), UBI_FM_POOL_MAGIC);
513 fmpl2 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
514 fm_pos += sizeof(*fmpl2);
515 if (fm_pos >= fm_size)
517 if (be32_to_cpu(fmpl2->magic) != UBI_FM_POOL_MAGIC) {
518 ubi_err("bad fastmap pool magic: 0x%x, expected: 0x%x",
519 be32_to_cpu(fmpl2->magic), UBI_FM_POOL_MAGIC);
523 pool_size = be16_to_cpu(fmpl1->size);
524 wl_pool_size = be16_to_cpu(fmpl2->size);
525 fm->max_pool_size = be16_to_cpu(fmpl1->max_size);
526 fm->max_wl_pool_size = be16_to_cpu(fmpl2->max_size);
528 if (pool_size > UBI_FM_MAX_POOL_SIZE || pool_size < 0) {
529 ubi_err("bad pool size: %i", pool_size);
533 if (wl_pool_size > UBI_FM_MAX_POOL_SIZE || wl_pool_size < 0) {
534 ubi_err("bad WL pool size: %i", wl_pool_size);
538 if (fm->max_pool_size > UBI_FM_MAX_POOL_SIZE ||
539 fm->max_pool_size < 0) {
540 ubi_err("bad maximal pool size: %i", fm->max_pool_size);
544 if (fm->max_wl_pool_size > UBI_FM_MAX_POOL_SIZE ||
545 fm->max_wl_pool_size < 0) {
546 ubi_err("bad maximal WL pool size: %i", fm->max_wl_pool_size);
550 /* read EC values from free list */
551 for (i = 0; i < be32_to_cpu(fmhdr->free_peb_count); i++) {
552 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
553 fm_pos += sizeof(*fmec);
554 if (fm_pos >= fm_size)
558 /* read EC values from used list */
559 for (i = 0; i < be32_to_cpu(fmhdr->used_peb_count); i++) {
560 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
561 fm_pos += sizeof(*fmec);
562 if (fm_pos >= fm_size)
565 generic_set_bit(be32_to_cpu(fmec->pnum), ubi->fm_used);
568 /* read EC values from scrub list */
569 for (i = 0; i < be32_to_cpu(fmhdr->scrub_peb_count); i++) {
570 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
571 fm_pos += sizeof(*fmec);
572 if (fm_pos >= fm_size)
576 /* read EC values from erase list */
577 for (i = 0; i < be32_to_cpu(fmhdr->erase_peb_count); i++) {
578 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
579 fm_pos += sizeof(*fmec);
580 if (fm_pos >= fm_size)
584 /* Iterate over all volumes and read their EBA table */
585 for (i = 0; i < be32_to_cpu(fmhdr->vol_count); i++) {
586 u32 vol_id, vol_type, used, reserved;
588 fmvhdr = (struct ubi_fm_volhdr *)(fm_raw + fm_pos);
589 fm_pos += sizeof(*fmvhdr);
590 if (fm_pos >= fm_size)
593 if (be32_to_cpu(fmvhdr->magic) != UBI_FM_VHDR_MAGIC) {
594 ubi_err("bad fastmap vol header magic: 0x%x, " \
596 be32_to_cpu(fmvhdr->magic), UBI_FM_VHDR_MAGIC);
600 vol_id = be32_to_cpu(fmvhdr->vol_id);
601 vol_type = fmvhdr->vol_type;
602 used = be32_to_cpu(fmvhdr->used_ebs);
604 fm_eba = (struct ubi_fm_eba *)(fm_raw + fm_pos);
605 fm_pos += sizeof(*fm_eba);
606 fm_pos += (sizeof(__be32) * be32_to_cpu(fm_eba->reserved_pebs));
607 if (fm_pos >= fm_size)
610 if (be32_to_cpu(fm_eba->magic) != UBI_FM_EBA_MAGIC) {
611 ubi_err("bad fastmap EBA header magic: 0x%x, " \
613 be32_to_cpu(fm_eba->magic), UBI_FM_EBA_MAGIC);
617 reserved = be32_to_cpu(fm_eba->reserved_pebs);
618 ubi_dbg("FA: vol %u used %u res: %u", vol_id, used, reserved);
619 for (j = 0; j < reserved; j++) {
620 int pnum = be32_to_cpu(fm_eba->pnum[j]);
622 if ((int)be32_to_cpu(fm_eba->pnum[j]) < 0)
625 if (!__test_and_clear_bit(pnum, ubi->fm_used))
629 * We only handle static volumes so used_ebs
630 * needs to be handed in. And we do not assign
631 * the reserved blocks
636 ret = assign_aeb_to_av(ubi, pnum, j, vol_id,
642 * Nasty: The fastmap claims that the volume
643 * has one block more than it, but that block
644 * is always empty and the other blocks have
645 * the correct number of total LEBs in the
646 * headers. Deal with it.
648 if (ret != UBI_IO_FF && j != used - 1)
650 ubi_dbg("FA: Vol: %u Ignoring empty LEB %d of %d",
655 ret = scan_pool(ubi, fmpl1->pebs, pool_size);
659 ret = scan_pool(ubi, fmpl2->pebs, wl_pool_size);
665 * If fastmap is leaking PEBs (must not happen), raise a
666 * fat warning and fall back to scanning mode.
667 * We do this here because in ubi_wl_init() it's too late
668 * and we cannot fall back to scanning.
670 if (WARN_ON(count_fastmap_pebs(ai) != ubi->peb_count -
671 ai->bad_peb_count - fm->used_blocks))
678 ret = UBI_BAD_FASTMAP;
683 static int ubi_scan_fastmap(struct ubi_scan_info *ubi,
684 struct ubi_attach_info *ai,
687 struct ubi_fm_sb *fmsb, *fmsb2;
688 struct ubi_vid_hdr *vh;
689 struct ubi_fastmap_layout *fm;
690 int i, used_blocks, pnum, ret = 0;
693 unsigned long long sqnum = 0;
696 fm = &ubi->fm_layout;
698 ret = ubi_io_read(ubi, fmsb, fm_anchor, ubi->leb_start, sizeof(*fmsb));
699 if (ret && ret != UBI_IO_BITFLIPS)
701 else if (ret == UBI_IO_BITFLIPS)
702 fm->to_be_tortured[0] = 1;
704 if (be32_to_cpu(fmsb->magic) != UBI_FM_SB_MAGIC) {
705 ubi_err("bad super block magic: 0x%x, expected: 0x%x",
706 be32_to_cpu(fmsb->magic), UBI_FM_SB_MAGIC);
707 ret = UBI_BAD_FASTMAP;
711 if (fmsb->version != UBI_FM_FMT_VERSION) {
712 ubi_err("bad fastmap version: %i, expected: %i",
713 fmsb->version, UBI_FM_FMT_VERSION);
714 ret = UBI_BAD_FASTMAP;
718 used_blocks = be32_to_cpu(fmsb->used_blocks);
719 if (used_blocks > UBI_FM_MAX_BLOCKS || used_blocks < 1) {
720 ubi_err("number of fastmap blocks is invalid: %i", used_blocks);
721 ret = UBI_BAD_FASTMAP;
725 fm_size = ubi->leb_size * used_blocks;
726 if (fm_size != ubi->fm_size) {
727 ubi_err("bad fastmap size: %zi, expected: %zi", fm_size,
729 ret = UBI_BAD_FASTMAP;
735 for (i = 0; i < used_blocks; i++) {
736 pnum = be32_to_cpu(fmsb->block_loc[i]);
738 if (ubi_io_is_bad(ubi, pnum)) {
739 ret = UBI_BAD_FASTMAP;
745 ret = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
746 if (ret && ret != UBI_IO_BITFLIPS) {
747 ubi_err("unable to read fastmap block# %i EC (PEB: %i)",
750 ret = UBI_BAD_FASTMAP;
752 } else if (ret == UBI_IO_BITFLIPS)
753 fm->to_be_tortured[i] = 1;
755 image_seq = be32_to_cpu(ech->image_seq);
757 ubi->image_seq = image_seq;
759 * Older UBI implementations have image_seq set to zero, so
760 * we shouldn't fail if image_seq == 0.
762 if (image_seq && (image_seq != ubi->image_seq)) {
763 ubi_err("wrong image seq:%d instead of %d",
764 be32_to_cpu(ech->image_seq), ubi->image_seq);
765 ret = UBI_BAD_FASTMAP;
769 ret = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
770 if (ret && ret != UBI_IO_BITFLIPS) {
771 ubi_err("unable to read fastmap block# %i (PEB: %i)",
777 * Mainline code rescans the anchor header. We've done
778 * that already so we merily copy it over.
780 if (pnum == fm_anchor)
781 memcpy(vh, ubi->blockinfo + pnum, sizeof(*fm));
784 if (be32_to_cpu(vh->vol_id) != UBI_FM_SB_VOLUME_ID) {
785 ubi_err("bad fastmap anchor vol_id: 0x%x," \
787 be32_to_cpu(vh->vol_id),
788 UBI_FM_SB_VOLUME_ID);
789 ret = UBI_BAD_FASTMAP;
793 if (be32_to_cpu(vh->vol_id) != UBI_FM_DATA_VOLUME_ID) {
794 ubi_err("bad fastmap data vol_id: 0x%x," \
796 be32_to_cpu(vh->vol_id),
797 UBI_FM_DATA_VOLUME_ID);
798 ret = UBI_BAD_FASTMAP;
803 if (sqnum < be64_to_cpu(vh->sqnum))
804 sqnum = be64_to_cpu(vh->sqnum);
806 ret = ubi_io_read(ubi, ubi->fm_buf + (ubi->leb_size * i), pnum,
807 ubi->leb_start, ubi->leb_size);
808 if (ret && ret != UBI_IO_BITFLIPS) {
809 ubi_err("unable to read fastmap block# %i (PEB: %i, " \
810 "err: %i)", i, pnum, ret);
815 fmsb2 = (struct ubi_fm_sb *)(ubi->fm_buf);
816 tmp_crc = be32_to_cpu(fmsb2->data_crc);
818 crc = crc32(UBI_CRC32_INIT, ubi->fm_buf, fm_size);
819 if (crc != tmp_crc) {
820 ubi_err("fastmap data CRC is invalid");
821 ubi_err("CRC should be: 0x%x, calc: 0x%x", tmp_crc, crc);
822 ret = UBI_BAD_FASTMAP;
826 fmsb2->sqnum = sqnum;
828 fm->used_blocks = used_blocks;
830 ret = ubi_attach_fastmap(ubi, ai, fm);
833 ret = UBI_BAD_FASTMAP;
838 ubi->fm_pool.max_size = ubi->fm->max_pool_size;
839 ubi->fm_wl_pool.max_size = ubi->fm->max_wl_pool_size;
840 ubi_msg("attached by fastmap %uMB %u blocks",
841 ubi->fsize_mb, ubi->peb_count);
842 ubi_dbg("fastmap pool size: %d", ubi->fm_pool.max_size);
843 ubi_dbg("fastmap WL pool size: %d", ubi->fm_wl_pool.max_size);
847 ubi_err("Attach by fastmap failed, doing a full scan!");
856 * Scan the flash and attempt to attach via fastmap
858 static void ipl_scan(struct ubi_scan_info *ubi)
864 * Scan first for the fastmap super block
866 for (pnum = 0; pnum < UBI_FM_MAX_START; pnum++) {
867 res = ubi_scan_vid_hdr(ubi, ubi->blockinfo + pnum, pnum);
869 * We ignore errors here as we are meriliy scanning
872 if (res != UBI_FASTMAP_ANCHOR)
876 * If fastmap is disabled, continue scanning. This
877 * might happen because the previous attempt failed or
878 * the caller disabled it right away.
880 if (!ubi->fm_enabled)
884 * Try to attach the fastmap, if that fails continue
887 if (!ubi_scan_fastmap(ubi, NULL, pnum))
890 * Fastmap failed. Clear everything we have and start
891 * over. We are paranoid and do not trust anything.
893 memset(ubi->volinfo, 0, sizeof(ubi->volinfo));
899 * Continue scanning, ignore errors, we might find what we are
902 for (; pnum < ubi->peb_count; pnum++)
903 ubi_scan_vid_hdr(ubi, ubi->blockinfo + pnum, pnum);
907 * Load a logical block of a volume into memory
909 static int ubi_load_block(struct ubi_scan_info *ubi, uint8_t *laddr,
910 struct ubi_vol_info *vi, u32 vol_id, u32 lnum,
913 struct ubi_vid_hdr *vh, *vrepl;
918 * If this is a fastmap run, we try to rescan full, otherwise
921 if (!test_bit(lnum, vi->found)) {
922 ubi_warn("LEB %d of %d is missing", lnum, last);
926 pnum = vi->lebs_to_pebs[lnum];
928 ubi_dbg("Load vol %u LEB %u PEB %u", vol_id, lnum, pnum);
930 if (ubi_io_is_bad(ubi, pnum)) {
931 ubi_warn("Corrupted mapping block %d PB %d\n", lnum, pnum);
935 if (test_bit(pnum, ubi->corrupt))
939 * Lets try to read that block
941 vh = ubi->blockinfo + pnum;
943 if (!test_bit(pnum, ubi->scanned)) {
944 ubi_warn("Vol: %u LEB %u PEB %u not yet scanned", vol_id,
946 if (ubi_rescan_fm_vid_hdr(ubi, vh, pnum, vol_id, lnum))
951 * Check, if the total number of blocks is correct
953 if (be32_to_cpu(vh->used_ebs) != last) {
954 ubi_dbg("Block count missmatch.");
955 ubi_dbg("vh->used_ebs: %d nrblocks: %d",
956 be32_to_cpu(vh->used_ebs), last);
957 generic_set_bit(pnum, ubi->corrupt);
962 * Get the data length of this block.
964 dlen = be32_to_cpu(vh->data_size);
967 * Read the data into RAM. We ignore the return value
968 * here as the only thing which might go wrong are
969 * bitflips. Try nevertheless.
971 ubi_io_read(ubi, laddr, pnum, ubi->leb_start, dlen);
973 /* Calculate CRC over the data */
974 crc = crc32(UBI_CRC32_INIT, laddr, dlen);
976 if (crc != be32_to_cpu(vh->data_crc)) {
977 ubi_warn("Vol: %u LEB %u PEB %u data CRC failure", vol_id,
979 generic_set_bit(pnum, ubi->corrupt);
983 /* We are good. Return the data length we read */
987 ubi_dbg("Find replacement for LEB %u PEB %u", lnum, pnum);
988 generic_clear_bit(lnum, vi->found);
991 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
992 struct ubi_vid_hdr *tmp = ubi->blockinfo + pnum;
993 u32 t_vol_id = be32_to_cpu(tmp->vol_id);
994 u32 t_lnum = be32_to_cpu(tmp->lnum);
996 if (test_bit(pnum, ubi->corrupt))
999 if (t_vol_id != vol_id || t_lnum != lnum)
1002 if (!test_bit(pnum, ubi->scanned)) {
1003 ubi_warn("Vol: %u LEB %u PEB %u not yet scanned",
1004 vol_id, lnum, pnum);
1005 if (ubi_rescan_fm_vid_hdr(ubi, tmp, pnum, vol_id, lnum))
1010 * We found one. If its the first, assign it otherwise
1013 generic_set_bit(lnum, vi->found);
1020 if (be64_to_cpu(vrepl->sqnum) < be64_to_cpu(tmp->sqnum))
1025 /* Update the vi table */
1026 pnum = vrepl - ubi->blockinfo;
1027 vi->lebs_to_pebs[lnum] = pnum;
1028 ubi_dbg("Trying PEB %u for LEB %u", pnum, lnum);
1035 * Load a volume into RAM
1037 static int ipl_load(struct ubi_scan_info *ubi, const u32 vol_id, uint8_t *laddr)
1039 struct ubi_vol_info *vi;
1040 u32 lnum, last, len;
1042 if (vol_id >= UBI_SPL_VOL_IDS)
1046 vi = ubi->volinfo + vol_id;
1047 last = vi->last_block + 1;
1049 /* Read the blocks to RAM, check CRC */
1050 for (lnum = 0 ; lnum < last; lnum++) {
1051 int res = ubi_load_block(ubi, laddr, vi, vol_id, lnum, last);
1054 ubi_warn("Failed to load volume %u", vol_id);
1057 /* res is the data length of the read block */
1064 int ubispl_load_volumes(struct ubispl_info *info, struct ubispl_load *lvols,
1067 struct ubi_scan_info *ubi = info->ubi;
1068 int res, i, fastmap = info->fastmap;
1073 * We do a partial initializiation of @ubi. Cleaning fm_buf is
1076 memset(ubi, 0, offsetof(struct ubi_scan_info, fm_buf));
1078 ubi->read = info->read;
1080 /* Precalculate the offsets */
1081 ubi->vid_offset = info->vid_offset;
1082 ubi->leb_start = info->leb_start;
1083 ubi->leb_size = info->peb_size - ubi->leb_start;
1084 ubi->peb_count = info->peb_count;
1085 ubi->peb_offset = info->peb_offset;
1087 #ifdef CONFIG_SPL_UBI_LOAD_BY_VOLNAME
1088 ubi->vtbl_valid = 0;
1091 fsize = info->peb_size * info->peb_count;
1092 ubi->fsize_mb = fsize >> 20;
1095 ubi->fm_size = ubi_calc_fm_size(ubi);
1096 ubi->fm_enabled = fastmap;
1098 for (i = 0; i < nrvols; i++) {
1099 struct ubispl_load *lv = lvols + i;
1101 generic_set_bit(lv->vol_id, ubi->toload);
1106 for (i = 0; i < nrvols; i++) {
1107 struct ubispl_load *lv = lvols + i;
1109 #ifdef CONFIG_SPL_UBI_LOAD_BY_VOLNAME
1110 if (lv->vol_id == -1) {
1111 for (int j = 0; j < UBI_SPL_VOL_IDS; j++) {
1112 int len = be16_to_cpu(ubi->vtbl[j].name_len);
1114 if (strncmp(lv->name,
1122 ubi_msg("Loading VolName %s (VolId #%d)", lv->name, lv->vol_id);
1124 ubi_msg("Loading VolId #%d", lv->vol_id);
1126 res = ipl_load(ubi, lv->vol_id, lv->load_addr);