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
14 #include <linux/crc32.h>
19 * ubi_calc_fm_size - calculates the fastmap size in bytes for an UBI device.
20 * @ubi: UBI device description object
22 static size_t ubi_calc_fm_size(struct ubi_scan_info *ubi)
26 size = sizeof(struct ubi_fm_sb) +
27 sizeof(struct ubi_fm_hdr) +
28 sizeof(struct ubi_fm_scan_pool) +
29 sizeof(struct ubi_fm_scan_pool) +
30 (ubi->peb_count * sizeof(struct ubi_fm_ec)) +
31 (sizeof(struct ubi_fm_eba) +
32 (ubi->peb_count * sizeof(__be32))) +
33 sizeof(struct ubi_fm_volhdr) * UBI_MAX_VOLUMES;
34 return roundup(size, ubi->leb_size);
37 static int ubi_io_read(struct ubi_scan_info *ubi, void *buf, int pnum,
38 unsigned long from, unsigned long len)
40 return ubi->read(pnum + ubi->peb_offset, from, len, buf);
43 static int ubi_io_is_bad(struct ubi_scan_info *ubi, int peb)
45 return peb >= ubi->peb_count || peb < 0;
48 #ifdef CONFIG_SPL_UBI_LOAD_BY_VOLNAME
51 * ubi_dump_vtbl_record - dump a &struct ubi_vtbl_record object.
52 * @r: the object to dump
53 * @idx: volume table index
55 void ubi_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx)
57 int name_len = be16_to_cpu(r->name_len);
59 ubi_dbg("Volume table record %d dump: size: %d",
60 idx, sizeof(struct ubi_vtbl_record));
61 ubi_dbg("\treserved_pebs %d", be32_to_cpu(r->reserved_pebs));
62 ubi_dbg("\talignment %d", be32_to_cpu(r->alignment));
63 ubi_dbg("\tdata_pad %d", be32_to_cpu(r->data_pad));
64 ubi_dbg("\tvol_type %d", (int)r->vol_type);
65 ubi_dbg("\tupd_marker %d", (int)r->upd_marker);
66 ubi_dbg("\tname_len %d", name_len);
68 if (r->name[0] == '\0') {
69 ubi_dbg("\tname NULL");
73 if (name_len <= UBI_VOL_NAME_MAX &&
74 strnlen(&r->name[0], name_len + 1) == name_len) {
75 ubi_dbg("\tname %s", &r->name[0]);
77 ubi_dbg("\t1st 5 characters of name: %c%c%c%c%c",
78 r->name[0], r->name[1], r->name[2], r->name[3],
81 ubi_dbg("\tcrc %#08x", be32_to_cpu(r->crc));
84 /* Empty volume table record */
85 static struct ubi_vtbl_record empty_vtbl_record;
88 * vtbl_check - check if volume table is not corrupted and sensible.
89 * @ubi: UBI device description object
92 * This function returns zero if @vtbl is all right, %1 if CRC is incorrect,
93 * and %-EINVAL if it contains inconsistent data.
95 static int vtbl_check(struct ubi_scan_info *ubi,
96 struct ubi_vtbl_record *vtbl)
98 int i, n, reserved_pebs, alignment, data_pad, vol_type, name_len;
103 for (i = 0; i < UBI_SPL_VOL_IDS; i++) {
104 reserved_pebs = be32_to_cpu(vtbl[i].reserved_pebs);
105 alignment = be32_to_cpu(vtbl[i].alignment);
106 data_pad = be32_to_cpu(vtbl[i].data_pad);
107 upd_marker = vtbl[i].upd_marker;
108 vol_type = vtbl[i].vol_type;
109 name_len = be16_to_cpu(vtbl[i].name_len);
110 name = &vtbl[i].name[0];
112 crc = crc32(UBI_CRC32_INIT, &vtbl[i], UBI_VTBL_RECORD_SIZE_CRC);
113 if (be32_to_cpu(vtbl[i].crc) != crc) {
114 ubi_err("bad CRC at record %u: %#08x, not %#08x",
115 i, crc, be32_to_cpu(vtbl[i].crc));
116 ubi_dump_vtbl_record(&vtbl[i], i);
120 if (reserved_pebs == 0) {
121 if (memcmp(&vtbl[i], &empty_vtbl_record,
122 UBI_VTBL_RECORD_SIZE)) {
129 if (reserved_pebs < 0 || alignment < 0 || data_pad < 0 ||
135 if (alignment > ubi->leb_size || alignment == 0) {
140 n = alignment & (CONFIG_SPL_UBI_VID_OFFSET - 1);
141 if (alignment != 1 && n) {
146 n = ubi->leb_size % alignment;
148 ubi_err("bad data_pad, has to be %d", n);
153 if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) {
158 if (upd_marker != 0 && upd_marker != 1) {
163 if (name_len > UBI_VOL_NAME_MAX) {
168 if (name[0] == '\0') {
173 if (name_len != strnlen(name, name_len + 1)) {
178 ubi_dump_vtbl_record(&vtbl[i], i);
181 /* Checks that all names are unique */
182 for (i = 0; i < UBI_SPL_VOL_IDS - 1; i++) {
183 for (n = i + 1; n < UBI_SPL_VOL_IDS; n++) {
184 int len1 = be16_to_cpu(vtbl[i].name_len);
185 int len2 = be16_to_cpu(vtbl[n].name_len);
187 if (len1 > 0 && len1 == len2 &&
188 !strncmp(vtbl[i].name, vtbl[n].name, len1)) {
189 ubi_err("volumes %d and %d have the same name \"%s\"",
191 ubi_dump_vtbl_record(&vtbl[i], i);
192 ubi_dump_vtbl_record(&vtbl[n], n);
201 ubi_err("volume table check failed: record %d, error %d", i, err);
202 ubi_dump_vtbl_record(&vtbl[i], i);
206 static int ubi_read_volume_table(struct ubi_scan_info *ubi, u32 pnum)
210 empty_vtbl_record.crc = cpu_to_be32(0xf116c36b);
212 err = ubi_io_read(ubi, &ubi->vtbl, pnum, ubi->leb_start,
213 sizeof(struct ubi_vtbl_record) * UBI_SPL_VOL_IDS);
214 if (err && err != UBI_IO_BITFLIPS) {
215 ubi_err("unable to read volume table");
219 if (!vtbl_check(ubi, ubi->vtbl)) {
227 #endif /* CONFIG_SPL_UBI_LOAD_BY_VOLNAME */
229 static int ubi_io_read_vid_hdr(struct ubi_scan_info *ubi, int pnum,
230 struct ubi_vid_hdr *vh, int unused)
235 /* No point in rescanning a corrupt block */
236 if (test_bit(pnum, ubi->corrupt))
237 return UBI_IO_BAD_HDR;
239 * If the block has been scanned already, no need to rescan
241 if (test_and_set_bit(pnum, ubi->scanned))
244 res = ubi_io_read(ubi, vh, pnum, ubi->vid_offset, sizeof(*vh));
247 * Bad block, unrecoverable ECC error, skip the block
250 ubi_dbg("Skipping bad or unreadable block %d", pnum);
252 generic_set_bit(pnum, ubi->corrupt);
256 /* Magic number available ? */
257 magic = be32_to_cpu(vh->magic);
258 if (magic != UBI_VID_HDR_MAGIC) {
259 generic_set_bit(pnum, ubi->corrupt);
260 if (magic == 0xffffffff)
262 ubi_msg("Bad magic in block 0%d %08x", pnum, magic);
263 return UBI_IO_BAD_HDR;
266 /* Header CRC correct ? */
267 if (crc32(UBI_CRC32_INIT, vh, UBI_VID_HDR_SIZE_CRC) !=
268 be32_to_cpu(vh->hdr_crc)) {
269 ubi_msg("Bad CRC in block 0%d", pnum);
270 generic_set_bit(pnum, ubi->corrupt);
271 return UBI_IO_BAD_HDR;
274 ubi_dbg("RV: pnum: %i sqnum %llu", pnum, be64_to_cpu(vh->sqnum));
279 static int ubi_rescan_fm_vid_hdr(struct ubi_scan_info *ubi,
280 struct ubi_vid_hdr *vh,
281 u32 fm_pnum, u32 fm_vol_id, u32 fm_lnum)
285 if (ubi_io_is_bad(ubi, fm_pnum))
288 res = ubi_io_read_vid_hdr(ubi, fm_pnum, vh, 0);
290 /* Check volume id, volume type and lnum */
291 if (be32_to_cpu(vh->vol_id) == fm_vol_id &&
292 vh->vol_type == UBI_VID_STATIC &&
293 be32_to_cpu(vh->lnum) == fm_lnum)
295 ubi_dbg("RS: PEB %u vol: %u : %u typ %u lnum %u %u",
296 fm_pnum, fm_vol_id, vh->vol_type,
297 be32_to_cpu(vh->vol_id),
298 fm_lnum, be32_to_cpu(vh->lnum));
303 /* Insert the logic block into the volume info */
304 static int ubi_add_peb_to_vol(struct ubi_scan_info *ubi,
305 struct ubi_vid_hdr *vh, u32 vol_id,
308 struct ubi_vol_info *vi = ubi->volinfo + vol_id;
312 * If the volume is larger than expected, yell and give up :(
314 if (lnum >= UBI_MAX_VOL_LEBS) {
315 ubi_warn("Vol: %u LEB %d > %d", vol_id, lnum, UBI_MAX_VOL_LEBS);
319 ubi_dbg("SC: Add PEB %u to Vol %u as LEB %u fnd %d sc %d",
320 pnum, vol_id, lnum, !!test_bit(lnum, vi->found),
321 !!test_bit(pnum, ubi->scanned));
323 /* Points to the translation entry */
324 ltp = vi->lebs_to_pebs + lnum;
326 /* If the block is already assigned, check sqnum */
327 if (__test_and_set_bit(lnum, vi->found)) {
329 struct ubi_vid_hdr *cur = ubi->blockinfo + cur_pnum;
332 * If the current block hase not yet been scanned, we
333 * need to do that. The other block might be stale or
334 * the current block corrupted and the FM not yet
337 if (!test_bit(cur_pnum, ubi->scanned)) {
339 * If the scan fails, we use the valid block
341 if (ubi_rescan_fm_vid_hdr(ubi, cur, cur_pnum, vol_id,
349 * Should not happen ....
351 if (test_bit(cur_pnum, ubi->corrupt)) {
356 ubi_dbg("Vol %u LEB %u PEB %u->sqnum %llu NPEB %u->sqnum %llu",
357 vol_id, lnum, cur_pnum, be64_to_cpu(cur->sqnum), pnum,
358 be64_to_cpu(vh->sqnum));
361 * Compare sqnum and take the newer one
363 if (be64_to_cpu(cur->sqnum) < be64_to_cpu(vh->sqnum))
367 if (lnum > vi->last_block)
368 vi->last_block = lnum;
374 static int ubi_scan_vid_hdr(struct ubi_scan_info *ubi, struct ubi_vid_hdr *vh,
380 if (ubi_io_is_bad(ubi, pnum))
383 res = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
388 vol_id = be32_to_cpu(vh->vol_id);
390 /* If this is the fastmap anchor, return right away */
391 if (vol_id == UBI_FM_SB_VOLUME_ID)
392 return ubi->fm_enabled ? UBI_FASTMAP_ANCHOR : 0;
394 #ifdef CONFIG_SPL_UBI_LOAD_BY_VOLNAME
395 /* If this is a UBI volume table, read it and return */
396 if (vol_id == UBI_LAYOUT_VOLUME_ID && !ubi->vtbl_valid) {
397 res = ubi_read_volume_table(ubi, pnum);
402 /* We only care about static volumes with an id < UBI_SPL_VOL_IDS */
403 if (vol_id >= UBI_SPL_VOL_IDS || vh->vol_type != UBI_VID_STATIC)
406 #ifndef CONFIG_SPL_UBI_LOAD_BY_VOLNAME
407 /* We are only interested in the volumes to load */
408 if (!test_bit(vol_id, ubi->toload))
411 lnum = be32_to_cpu(vh->lnum);
412 return ubi_add_peb_to_vol(ubi, vh, vol_id, pnum, lnum);
415 static int assign_aeb_to_av(struct ubi_scan_info *ubi, u32 pnum, u32 lnum,
416 u32 vol_id, u32 vol_type, u32 used)
418 struct ubi_vid_hdr *vh;
420 if (ubi_io_is_bad(ubi, pnum))
425 #ifndef CONFIG_SPL_UBI_LOAD_BY_VOLNAME
426 if (vol_id >= UBI_SPL_VOL_IDS || vol_type != UBI_STATIC_VOLUME)
429 /* We are only interested in the volumes to load */
430 if (!test_bit(vol_id, ubi->toload))
433 vh = ubi->blockinfo + pnum;
435 return ubi_scan_vid_hdr(ubi, vh, pnum);
438 static int scan_pool(struct ubi_scan_info *ubi, __be32 *pebs, int pool_size)
440 struct ubi_vid_hdr *vh;
444 ubi_dbg("Scanning pool size: %d", pool_size);
446 for (i = 0; i < pool_size; i++) {
447 pnum = be32_to_cpu(pebs[i]);
449 if (ubi_io_is_bad(ubi, pnum)) {
450 ubi_err("FM: Bad PEB in fastmap pool! %u", pnum);
451 return UBI_BAD_FASTMAP;
454 vh = ubi->blockinfo + pnum;
456 * We allow the scan to fail here. The loader will notice
457 * and look for a replacement.
459 ubi_scan_vid_hdr(ubi, vh, pnum);
465 * Fastmap code is stolen from Linux kernel and this stub structure is used
468 struct ubi_attach_info {
472 static int ubi_attach_fastmap(struct ubi_scan_info *ubi,
473 struct ubi_attach_info *ai,
474 struct ubi_fastmap_layout *fm)
476 struct ubi_fm_hdr *fmhdr;
477 struct ubi_fm_scan_pool *fmpl1, *fmpl2;
478 struct ubi_fm_ec *fmec;
479 struct ubi_fm_volhdr *fmvhdr;
480 struct ubi_fm_eba *fm_eba;
481 int ret, i, j, pool_size, wl_pool_size;
482 size_t fm_pos = 0, fm_size = ubi->fm_size;
483 void *fm_raw = ubi->fm_buf;
485 memset(ubi->fm_used, 0, sizeof(ubi->fm_used));
487 fm_pos += sizeof(struct ubi_fm_sb);
488 if (fm_pos >= fm_size)
491 fmhdr = (struct ubi_fm_hdr *)(fm_raw + fm_pos);
492 fm_pos += sizeof(*fmhdr);
493 if (fm_pos >= fm_size)
496 if (be32_to_cpu(fmhdr->magic) != UBI_FM_HDR_MAGIC) {
497 ubi_err("bad fastmap header magic: 0x%x, expected: 0x%x",
498 be32_to_cpu(fmhdr->magic), UBI_FM_HDR_MAGIC);
502 fmpl1 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
503 fm_pos += sizeof(*fmpl1);
504 if (fm_pos >= fm_size)
506 if (be32_to_cpu(fmpl1->magic) != UBI_FM_POOL_MAGIC) {
507 ubi_err("bad fastmap pool magic: 0x%x, expected: 0x%x",
508 be32_to_cpu(fmpl1->magic), UBI_FM_POOL_MAGIC);
512 fmpl2 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
513 fm_pos += sizeof(*fmpl2);
514 if (fm_pos >= fm_size)
516 if (be32_to_cpu(fmpl2->magic) != UBI_FM_POOL_MAGIC) {
517 ubi_err("bad fastmap pool magic: 0x%x, expected: 0x%x",
518 be32_to_cpu(fmpl2->magic), UBI_FM_POOL_MAGIC);
522 pool_size = be16_to_cpu(fmpl1->size);
523 wl_pool_size = be16_to_cpu(fmpl2->size);
524 fm->max_pool_size = be16_to_cpu(fmpl1->max_size);
525 fm->max_wl_pool_size = be16_to_cpu(fmpl2->max_size);
527 if (pool_size > UBI_FM_MAX_POOL_SIZE || pool_size < 0) {
528 ubi_err("bad pool size: %i", pool_size);
532 if (wl_pool_size > UBI_FM_MAX_POOL_SIZE || wl_pool_size < 0) {
533 ubi_err("bad WL pool size: %i", wl_pool_size);
537 if (fm->max_pool_size > UBI_FM_MAX_POOL_SIZE ||
538 fm->max_pool_size < 0) {
539 ubi_err("bad maximal pool size: %i", fm->max_pool_size);
543 if (fm->max_wl_pool_size > UBI_FM_MAX_POOL_SIZE ||
544 fm->max_wl_pool_size < 0) {
545 ubi_err("bad maximal WL pool size: %i", fm->max_wl_pool_size);
549 /* read EC values from free list */
550 for (i = 0; i < be32_to_cpu(fmhdr->free_peb_count); i++) {
551 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
552 fm_pos += sizeof(*fmec);
553 if (fm_pos >= fm_size)
557 /* read EC values from used list */
558 for (i = 0; i < be32_to_cpu(fmhdr->used_peb_count); i++) {
559 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
560 fm_pos += sizeof(*fmec);
561 if (fm_pos >= fm_size)
564 generic_set_bit(be32_to_cpu(fmec->pnum), ubi->fm_used);
567 /* read EC values from scrub list */
568 for (i = 0; i < be32_to_cpu(fmhdr->scrub_peb_count); i++) {
569 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
570 fm_pos += sizeof(*fmec);
571 if (fm_pos >= fm_size)
575 /* read EC values from erase list */
576 for (i = 0; i < be32_to_cpu(fmhdr->erase_peb_count); i++) {
577 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
578 fm_pos += sizeof(*fmec);
579 if (fm_pos >= fm_size)
583 /* Iterate over all volumes and read their EBA table */
584 for (i = 0; i < be32_to_cpu(fmhdr->vol_count); i++) {
585 u32 vol_id, vol_type, used, reserved;
587 fmvhdr = (struct ubi_fm_volhdr *)(fm_raw + fm_pos);
588 fm_pos += sizeof(*fmvhdr);
589 if (fm_pos >= fm_size)
592 if (be32_to_cpu(fmvhdr->magic) != UBI_FM_VHDR_MAGIC) {
593 ubi_err("bad fastmap vol header magic: 0x%x, " \
595 be32_to_cpu(fmvhdr->magic), UBI_FM_VHDR_MAGIC);
599 vol_id = be32_to_cpu(fmvhdr->vol_id);
600 vol_type = fmvhdr->vol_type;
601 used = be32_to_cpu(fmvhdr->used_ebs);
603 fm_eba = (struct ubi_fm_eba *)(fm_raw + fm_pos);
604 fm_pos += sizeof(*fm_eba);
605 fm_pos += (sizeof(__be32) * be32_to_cpu(fm_eba->reserved_pebs));
606 if (fm_pos >= fm_size)
609 if (be32_to_cpu(fm_eba->magic) != UBI_FM_EBA_MAGIC) {
610 ubi_err("bad fastmap EBA header magic: 0x%x, " \
612 be32_to_cpu(fm_eba->magic), UBI_FM_EBA_MAGIC);
616 reserved = be32_to_cpu(fm_eba->reserved_pebs);
617 ubi_dbg("FA: vol %u used %u res: %u", vol_id, used, reserved);
618 for (j = 0; j < reserved; j++) {
619 int pnum = be32_to_cpu(fm_eba->pnum[j]);
621 if ((int)be32_to_cpu(fm_eba->pnum[j]) < 0)
624 if (!__test_and_clear_bit(pnum, ubi->fm_used))
628 * We only handle static volumes so used_ebs
629 * needs to be handed in. And we do not assign
630 * the reserved blocks
635 ret = assign_aeb_to_av(ubi, pnum, j, vol_id,
641 * Nasty: The fastmap claims that the volume
642 * has one block more than it, but that block
643 * is always empty and the other blocks have
644 * the correct number of total LEBs in the
645 * headers. Deal with it.
647 if (ret != UBI_IO_FF && j != used - 1)
649 ubi_dbg("FA: Vol: %u Ignoring empty LEB %d of %d",
654 ret = scan_pool(ubi, fmpl1->pebs, pool_size);
658 ret = scan_pool(ubi, fmpl2->pebs, wl_pool_size);
664 * If fastmap is leaking PEBs (must not happen), raise a
665 * fat warning and fall back to scanning mode.
666 * We do this here because in ubi_wl_init() it's too late
667 * and we cannot fall back to scanning.
669 if (WARN_ON(count_fastmap_pebs(ai) != ubi->peb_count -
670 ai->bad_peb_count - fm->used_blocks))
677 ret = UBI_BAD_FASTMAP;
682 static int ubi_scan_fastmap(struct ubi_scan_info *ubi,
683 struct ubi_attach_info *ai,
686 struct ubi_fm_sb *fmsb, *fmsb2;
687 struct ubi_vid_hdr *vh;
688 struct ubi_fastmap_layout *fm;
689 int i, used_blocks, pnum, ret = 0;
692 unsigned long long sqnum = 0;
695 fm = &ubi->fm_layout;
697 ret = ubi_io_read(ubi, fmsb, fm_anchor, ubi->leb_start, sizeof(*fmsb));
698 if (ret && ret != UBI_IO_BITFLIPS)
700 else if (ret == UBI_IO_BITFLIPS)
701 fm->to_be_tortured[0] = 1;
703 if (be32_to_cpu(fmsb->magic) != UBI_FM_SB_MAGIC) {
704 ubi_err("bad super block magic: 0x%x, expected: 0x%x",
705 be32_to_cpu(fmsb->magic), UBI_FM_SB_MAGIC);
706 ret = UBI_BAD_FASTMAP;
710 if (fmsb->version != UBI_FM_FMT_VERSION) {
711 ubi_err("bad fastmap version: %i, expected: %i",
712 fmsb->version, UBI_FM_FMT_VERSION);
713 ret = UBI_BAD_FASTMAP;
717 used_blocks = be32_to_cpu(fmsb->used_blocks);
718 if (used_blocks > UBI_FM_MAX_BLOCKS || used_blocks < 1) {
719 ubi_err("number of fastmap blocks is invalid: %i", used_blocks);
720 ret = UBI_BAD_FASTMAP;
724 fm_size = ubi->leb_size * used_blocks;
725 if (fm_size != ubi->fm_size) {
726 ubi_err("bad fastmap size: %zi, expected: %zi", fm_size,
728 ret = UBI_BAD_FASTMAP;
734 for (i = 0; i < used_blocks; i++) {
735 pnum = be32_to_cpu(fmsb->block_loc[i]);
737 if (ubi_io_is_bad(ubi, pnum)) {
738 ret = UBI_BAD_FASTMAP;
744 ret = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
745 if (ret && ret != UBI_IO_BITFLIPS) {
746 ubi_err("unable to read fastmap block# %i EC (PEB: %i)",
749 ret = UBI_BAD_FASTMAP;
751 } else if (ret == UBI_IO_BITFLIPS)
752 fm->to_be_tortured[i] = 1;
754 image_seq = be32_to_cpu(ech->image_seq);
756 ubi->image_seq = image_seq;
758 * Older UBI implementations have image_seq set to zero, so
759 * we shouldn't fail if image_seq == 0.
761 if (image_seq && (image_seq != ubi->image_seq)) {
762 ubi_err("wrong image seq:%d instead of %d",
763 be32_to_cpu(ech->image_seq), ubi->image_seq);
764 ret = UBI_BAD_FASTMAP;
768 ret = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
769 if (ret && ret != UBI_IO_BITFLIPS) {
770 ubi_err("unable to read fastmap block# %i (PEB: %i)",
776 * Mainline code rescans the anchor header. We've done
777 * that already so we merily copy it over.
779 if (pnum == fm_anchor)
780 memcpy(vh, ubi->blockinfo + pnum, sizeof(*fm));
783 if (be32_to_cpu(vh->vol_id) != UBI_FM_SB_VOLUME_ID) {
784 ubi_err("bad fastmap anchor vol_id: 0x%x," \
786 be32_to_cpu(vh->vol_id),
787 UBI_FM_SB_VOLUME_ID);
788 ret = UBI_BAD_FASTMAP;
792 if (be32_to_cpu(vh->vol_id) != UBI_FM_DATA_VOLUME_ID) {
793 ubi_err("bad fastmap data vol_id: 0x%x," \
795 be32_to_cpu(vh->vol_id),
796 UBI_FM_DATA_VOLUME_ID);
797 ret = UBI_BAD_FASTMAP;
802 if (sqnum < be64_to_cpu(vh->sqnum))
803 sqnum = be64_to_cpu(vh->sqnum);
805 ret = ubi_io_read(ubi, ubi->fm_buf + (ubi->leb_size * i), pnum,
806 ubi->leb_start, ubi->leb_size);
807 if (ret && ret != UBI_IO_BITFLIPS) {
808 ubi_err("unable to read fastmap block# %i (PEB: %i, " \
809 "err: %i)", i, pnum, ret);
814 fmsb2 = (struct ubi_fm_sb *)(ubi->fm_buf);
815 tmp_crc = be32_to_cpu(fmsb2->data_crc);
817 crc = crc32(UBI_CRC32_INIT, ubi->fm_buf, fm_size);
818 if (crc != tmp_crc) {
819 ubi_err("fastmap data CRC is invalid");
820 ubi_err("CRC should be: 0x%x, calc: 0x%x", tmp_crc, crc);
821 ret = UBI_BAD_FASTMAP;
825 fmsb2->sqnum = sqnum;
827 fm->used_blocks = used_blocks;
829 ret = ubi_attach_fastmap(ubi, ai, fm);
832 ret = UBI_BAD_FASTMAP;
837 ubi->fm_pool.max_size = ubi->fm->max_pool_size;
838 ubi->fm_wl_pool.max_size = ubi->fm->max_wl_pool_size;
839 ubi_msg("attached by fastmap %uMB %u blocks",
840 ubi->fsize_mb, ubi->peb_count);
841 ubi_dbg("fastmap pool size: %d", ubi->fm_pool.max_size);
842 ubi_dbg("fastmap WL pool size: %d", ubi->fm_wl_pool.max_size);
846 ubi_err("Attach by fastmap failed, doing a full scan!");
855 * Scan the flash and attempt to attach via fastmap
857 static void ipl_scan(struct ubi_scan_info *ubi)
863 * Scan first for the fastmap super block
865 for (pnum = 0; pnum < UBI_FM_MAX_START; pnum++) {
866 res = ubi_scan_vid_hdr(ubi, ubi->blockinfo + pnum, pnum);
868 * We ignore errors here as we are meriliy scanning
871 if (res != UBI_FASTMAP_ANCHOR)
875 * If fastmap is disabled, continue scanning. This
876 * might happen because the previous attempt failed or
877 * the caller disabled it right away.
879 if (!ubi->fm_enabled)
883 * Try to attach the fastmap, if that fails continue
886 if (!ubi_scan_fastmap(ubi, NULL, pnum))
889 * Fastmap failed. Clear everything we have and start
890 * over. We are paranoid and do not trust anything.
892 memset(ubi->volinfo, 0, sizeof(ubi->volinfo));
898 * Continue scanning, ignore errors, we might find what we are
901 for (; pnum < ubi->peb_count; pnum++)
902 ubi_scan_vid_hdr(ubi, ubi->blockinfo + pnum, pnum);
906 * Load a logical block of a volume into memory
908 static int ubi_load_block(struct ubi_scan_info *ubi, uint8_t *laddr,
909 struct ubi_vol_info *vi, u32 vol_id, u32 lnum,
912 struct ubi_vid_hdr *vh, *vrepl;
917 * If this is a fastmap run, we try to rescan full, otherwise
920 if (!test_bit(lnum, vi->found)) {
921 ubi_warn("LEB %d of %d is missing", lnum, last);
925 pnum = vi->lebs_to_pebs[lnum];
927 ubi_dbg("Load vol %u LEB %u PEB %u", vol_id, lnum, pnum);
929 if (ubi_io_is_bad(ubi, pnum)) {
930 ubi_warn("Corrupted mapping block %d PB %d\n", lnum, pnum);
934 if (test_bit(pnum, ubi->corrupt))
938 * Lets try to read that block
940 vh = ubi->blockinfo + pnum;
942 if (!test_bit(pnum, ubi->scanned)) {
943 ubi_warn("Vol: %u LEB %u PEB %u not yet scanned", vol_id,
945 if (ubi_rescan_fm_vid_hdr(ubi, vh, pnum, vol_id, lnum))
950 * Check, if the total number of blocks is correct
952 if (be32_to_cpu(vh->used_ebs) != last) {
953 ubi_dbg("Block count missmatch.");
954 ubi_dbg("vh->used_ebs: %d nrblocks: %d",
955 be32_to_cpu(vh->used_ebs), last);
956 generic_set_bit(pnum, ubi->corrupt);
961 * Get the data length of this block.
963 dlen = be32_to_cpu(vh->data_size);
966 * Read the data into RAM. We ignore the return value
967 * here as the only thing which might go wrong are
968 * bitflips. Try nevertheless.
970 ubi_io_read(ubi, laddr, pnum, ubi->leb_start, dlen);
972 /* Calculate CRC over the data */
973 crc = crc32(UBI_CRC32_INIT, laddr, dlen);
975 if (crc != be32_to_cpu(vh->data_crc)) {
976 ubi_warn("Vol: %u LEB %u PEB %u data CRC failure", vol_id,
978 generic_set_bit(pnum, ubi->corrupt);
982 /* We are good. Return the data length we read */
986 ubi_dbg("Find replacement for LEB %u PEB %u", lnum, pnum);
987 generic_clear_bit(lnum, vi->found);
990 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
991 struct ubi_vid_hdr *tmp = ubi->blockinfo + pnum;
992 u32 t_vol_id = be32_to_cpu(tmp->vol_id);
993 u32 t_lnum = be32_to_cpu(tmp->lnum);
995 if (test_bit(pnum, ubi->corrupt))
998 if (t_vol_id != vol_id || t_lnum != lnum)
1001 if (!test_bit(pnum, ubi->scanned)) {
1002 ubi_warn("Vol: %u LEB %u PEB %u not yet scanned",
1003 vol_id, lnum, pnum);
1004 if (ubi_rescan_fm_vid_hdr(ubi, tmp, pnum, vol_id, lnum))
1009 * We found one. If its the first, assign it otherwise
1012 generic_set_bit(lnum, vi->found);
1019 if (be64_to_cpu(vrepl->sqnum) < be64_to_cpu(tmp->sqnum))
1024 /* Update the vi table */
1025 pnum = vrepl - ubi->blockinfo;
1026 vi->lebs_to_pebs[lnum] = pnum;
1027 ubi_dbg("Trying PEB %u for LEB %u", pnum, lnum);
1034 * Load a volume into RAM
1036 static int ipl_load(struct ubi_scan_info *ubi, const u32 vol_id, uint8_t *laddr)
1038 struct ubi_vol_info *vi;
1039 u32 lnum, last, len;
1041 if (vol_id >= UBI_SPL_VOL_IDS)
1045 vi = ubi->volinfo + vol_id;
1046 last = vi->last_block + 1;
1048 /* Read the blocks to RAM, check CRC */
1049 for (lnum = 0 ; lnum < last; lnum++) {
1050 int res = ubi_load_block(ubi, laddr, vi, vol_id, lnum, last);
1053 ubi_warn("Failed to load volume %u", vol_id);
1056 /* res is the data length of the read block */
1063 int ubispl_load_volumes(struct ubispl_info *info, struct ubispl_load *lvols,
1066 struct ubi_scan_info *ubi = info->ubi;
1067 int res, i, fastmap = info->fastmap;
1072 * We do a partial initializiation of @ubi. Cleaning fm_buf is
1075 memset(ubi, 0, offsetof(struct ubi_scan_info, fm_buf));
1077 ubi->read = info->read;
1079 /* Precalculate the offsets */
1080 ubi->vid_offset = info->vid_offset;
1081 ubi->leb_start = info->leb_start;
1082 ubi->leb_size = info->peb_size - ubi->leb_start;
1083 ubi->peb_count = info->peb_count;
1084 ubi->peb_offset = info->peb_offset;
1086 #ifdef CONFIG_SPL_UBI_LOAD_BY_VOLNAME
1087 ubi->vtbl_valid = 0;
1090 fsize = info->peb_size * info->peb_count;
1091 ubi->fsize_mb = fsize >> 20;
1094 ubi->fm_size = ubi_calc_fm_size(ubi);
1095 ubi->fm_enabled = fastmap;
1097 for (i = 0; i < nrvols; i++) {
1098 struct ubispl_load *lv = lvols + i;
1100 generic_set_bit(lv->vol_id, ubi->toload);
1105 for (i = 0; i < nrvols; i++) {
1106 struct ubispl_load *lv = lvols + i;
1108 #ifdef CONFIG_SPL_UBI_LOAD_BY_VOLNAME
1109 if (lv->vol_id == -1) {
1110 for (int j = 0; j < UBI_SPL_VOL_IDS; j++) {
1111 int len = be16_to_cpu(ubi->vtbl[j].name_len);
1113 if (strncmp(lv->name,
1121 ubi_msg("Loading VolName %s (VolId #%d)", lv->name, lv->vol_id);
1123 ubi_msg("Loading VolId #%d", lv->vol_id);
1125 res = ipl_load(ubi, lv->vol_id, lv->load_addr);