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 static int ubi_io_read_vid_hdr(struct ubi_scan_info *ubi, int pnum,
49 struct ubi_vid_hdr *vh, int unused)
54 /* No point in rescanning a corrupt block */
55 if (test_bit(pnum, ubi->corrupt))
56 return UBI_IO_BAD_HDR;
58 * If the block has been scanned already, no need to rescan
60 if (test_and_set_bit(pnum, ubi->scanned))
63 res = ubi_io_read(ubi, vh, pnum, ubi->vid_offset, sizeof(*vh));
66 * Bad block, unrecoverable ECC error, skip the block
69 ubi_dbg("Skipping bad or unreadable block %d", pnum);
71 generic_set_bit(pnum, ubi->corrupt);
75 /* Magic number available ? */
76 magic = be32_to_cpu(vh->magic);
77 if (magic != UBI_VID_HDR_MAGIC) {
78 generic_set_bit(pnum, ubi->corrupt);
79 if (magic == 0xffffffff)
81 ubi_msg("Bad magic in block 0%d %08x", pnum, magic);
82 return UBI_IO_BAD_HDR;
85 /* Header CRC correct ? */
86 if (crc32(UBI_CRC32_INIT, vh, UBI_VID_HDR_SIZE_CRC) !=
87 be32_to_cpu(vh->hdr_crc)) {
88 ubi_msg("Bad CRC in block 0%d", pnum);
89 generic_set_bit(pnum, ubi->corrupt);
90 return UBI_IO_BAD_HDR;
93 ubi_dbg("RV: pnum: %i sqnum %llu", pnum, be64_to_cpu(vh->sqnum));
98 static int ubi_rescan_fm_vid_hdr(struct ubi_scan_info *ubi,
99 struct ubi_vid_hdr *vh,
100 u32 fm_pnum, u32 fm_vol_id, u32 fm_lnum)
104 if (ubi_io_is_bad(ubi, fm_pnum))
107 res = ubi_io_read_vid_hdr(ubi, fm_pnum, vh, 0);
109 /* Check volume id, volume type and lnum */
110 if (be32_to_cpu(vh->vol_id) == fm_vol_id &&
111 vh->vol_type == UBI_VID_STATIC &&
112 be32_to_cpu(vh->lnum) == fm_lnum)
114 ubi_dbg("RS: PEB %u vol: %u : %u typ %u lnum %u %u",
115 fm_pnum, fm_vol_id, vh->vol_type,
116 be32_to_cpu(vh->vol_id),
117 fm_lnum, be32_to_cpu(vh->lnum));
122 /* Insert the logic block into the volume info */
123 static int ubi_add_peb_to_vol(struct ubi_scan_info *ubi,
124 struct ubi_vid_hdr *vh, u32 vol_id,
127 struct ubi_vol_info *vi = ubi->volinfo + vol_id;
131 * If the volume is larger than expected, yell and give up :(
133 if (lnum >= UBI_MAX_VOL_LEBS) {
134 ubi_warn("Vol: %u LEB %d > %d", vol_id, lnum, UBI_MAX_VOL_LEBS);
138 ubi_dbg("SC: Add PEB %u to Vol %u as LEB %u fnd %d sc %d",
139 pnum, vol_id, lnum, !!test_bit(lnum, vi->found),
140 !!test_bit(pnum, ubi->scanned));
142 /* Points to the translation entry */
143 ltp = vi->lebs_to_pebs + lnum;
145 /* If the block is already assigned, check sqnum */
146 if (__test_and_set_bit(lnum, vi->found)) {
148 struct ubi_vid_hdr *cur = ubi->blockinfo + cur_pnum;
151 * If the current block hase not yet been scanned, we
152 * need to do that. The other block might be stale or
153 * the current block corrupted and the FM not yet
156 if (!test_bit(cur_pnum, ubi->scanned)) {
158 * If the scan fails, we use the valid block
160 if (ubi_rescan_fm_vid_hdr(ubi, cur, cur_pnum, vol_id,
168 * Should not happen ....
170 if (test_bit(cur_pnum, ubi->corrupt)) {
175 ubi_dbg("Vol %u LEB %u PEB %u->sqnum %llu NPEB %u->sqnum %llu",
176 vol_id, lnum, cur_pnum, be64_to_cpu(cur->sqnum), pnum,
177 be64_to_cpu(vh->sqnum));
180 * Compare sqnum and take the newer one
182 if (be64_to_cpu(cur->sqnum) < be64_to_cpu(vh->sqnum))
186 if (lnum > vi->last_block)
187 vi->last_block = lnum;
193 static int ubi_scan_vid_hdr(struct ubi_scan_info *ubi, struct ubi_vid_hdr *vh,
199 if (ubi_io_is_bad(ubi, pnum))
202 res = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
207 vol_id = be32_to_cpu(vh->vol_id);
209 /* If this is the fastmap anchor, return right away */
210 if (vol_id == UBI_FM_SB_VOLUME_ID)
211 return ubi->fm_enabled ? UBI_FASTMAP_ANCHOR : 0;
213 /* We only care about static volumes with an id < UBI_SPL_VOL_IDS */
214 if (vol_id >= UBI_SPL_VOL_IDS || vh->vol_type != UBI_VID_STATIC)
217 /* We are only interested in the volumes to load */
218 if (!test_bit(vol_id, ubi->toload))
221 lnum = be32_to_cpu(vh->lnum);
222 return ubi_add_peb_to_vol(ubi, vh, vol_id, pnum, lnum);
225 static int assign_aeb_to_av(struct ubi_scan_info *ubi, u32 pnum, u32 lnum,
226 u32 vol_id, u32 vol_type, u32 used)
228 struct ubi_vid_hdr *vh;
230 if (ubi_io_is_bad(ubi, pnum))
235 if (vol_id >= UBI_SPL_VOL_IDS || vol_type != UBI_STATIC_VOLUME)
238 /* We are only interested in the volumes to load */
239 if (!test_bit(vol_id, ubi->toload))
242 vh = ubi->blockinfo + pnum;
244 return ubi_scan_vid_hdr(ubi, vh, pnum);
247 static int scan_pool(struct ubi_scan_info *ubi, __be32 *pebs, int pool_size)
249 struct ubi_vid_hdr *vh;
253 ubi_dbg("Scanning pool size: %d", pool_size);
255 for (i = 0; i < pool_size; i++) {
256 pnum = be32_to_cpu(pebs[i]);
258 if (ubi_io_is_bad(ubi, pnum)) {
259 ubi_err("FM: Bad PEB in fastmap pool! %u", pnum);
260 return UBI_BAD_FASTMAP;
263 vh = ubi->blockinfo + pnum;
265 * We allow the scan to fail here. The loader will notice
266 * and look for a replacement.
268 ubi_scan_vid_hdr(ubi, vh, pnum);
274 * Fastmap code is stolen from Linux kernel and this stub structure is used
277 struct ubi_attach_info {
281 static int ubi_attach_fastmap(struct ubi_scan_info *ubi,
282 struct ubi_attach_info *ai,
283 struct ubi_fastmap_layout *fm)
285 struct ubi_fm_hdr *fmhdr;
286 struct ubi_fm_scan_pool *fmpl1, *fmpl2;
287 struct ubi_fm_ec *fmec;
288 struct ubi_fm_volhdr *fmvhdr;
289 struct ubi_fm_eba *fm_eba;
290 int ret, i, j, pool_size, wl_pool_size;
291 size_t fm_pos = 0, fm_size = ubi->fm_size;
292 void *fm_raw = ubi->fm_buf;
294 memset(ubi->fm_used, 0, sizeof(ubi->fm_used));
296 fm_pos += sizeof(struct ubi_fm_sb);
297 if (fm_pos >= fm_size)
300 fmhdr = (struct ubi_fm_hdr *)(fm_raw + fm_pos);
301 fm_pos += sizeof(*fmhdr);
302 if (fm_pos >= fm_size)
305 if (be32_to_cpu(fmhdr->magic) != UBI_FM_HDR_MAGIC) {
306 ubi_err("bad fastmap header magic: 0x%x, expected: 0x%x",
307 be32_to_cpu(fmhdr->magic), UBI_FM_HDR_MAGIC);
311 fmpl1 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
312 fm_pos += sizeof(*fmpl1);
313 if (fm_pos >= fm_size)
315 if (be32_to_cpu(fmpl1->magic) != UBI_FM_POOL_MAGIC) {
316 ubi_err("bad fastmap pool magic: 0x%x, expected: 0x%x",
317 be32_to_cpu(fmpl1->magic), UBI_FM_POOL_MAGIC);
321 fmpl2 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
322 fm_pos += sizeof(*fmpl2);
323 if (fm_pos >= fm_size)
325 if (be32_to_cpu(fmpl2->magic) != UBI_FM_POOL_MAGIC) {
326 ubi_err("bad fastmap pool magic: 0x%x, expected: 0x%x",
327 be32_to_cpu(fmpl2->magic), UBI_FM_POOL_MAGIC);
331 pool_size = be16_to_cpu(fmpl1->size);
332 wl_pool_size = be16_to_cpu(fmpl2->size);
333 fm->max_pool_size = be16_to_cpu(fmpl1->max_size);
334 fm->max_wl_pool_size = be16_to_cpu(fmpl2->max_size);
336 if (pool_size > UBI_FM_MAX_POOL_SIZE || pool_size < 0) {
337 ubi_err("bad pool size: %i", pool_size);
341 if (wl_pool_size > UBI_FM_MAX_POOL_SIZE || wl_pool_size < 0) {
342 ubi_err("bad WL pool size: %i", wl_pool_size);
346 if (fm->max_pool_size > UBI_FM_MAX_POOL_SIZE ||
347 fm->max_pool_size < 0) {
348 ubi_err("bad maximal pool size: %i", fm->max_pool_size);
352 if (fm->max_wl_pool_size > UBI_FM_MAX_POOL_SIZE ||
353 fm->max_wl_pool_size < 0) {
354 ubi_err("bad maximal WL pool size: %i", fm->max_wl_pool_size);
358 /* read EC values from free list */
359 for (i = 0; i < be32_to_cpu(fmhdr->free_peb_count); i++) {
360 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
361 fm_pos += sizeof(*fmec);
362 if (fm_pos >= fm_size)
366 /* read EC values from used list */
367 for (i = 0; i < be32_to_cpu(fmhdr->used_peb_count); i++) {
368 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
369 fm_pos += sizeof(*fmec);
370 if (fm_pos >= fm_size)
373 generic_set_bit(be32_to_cpu(fmec->pnum), ubi->fm_used);
376 /* read EC values from scrub list */
377 for (i = 0; i < be32_to_cpu(fmhdr->scrub_peb_count); i++) {
378 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
379 fm_pos += sizeof(*fmec);
380 if (fm_pos >= fm_size)
384 /* read EC values from erase list */
385 for (i = 0; i < be32_to_cpu(fmhdr->erase_peb_count); i++) {
386 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
387 fm_pos += sizeof(*fmec);
388 if (fm_pos >= fm_size)
392 /* Iterate over all volumes and read their EBA table */
393 for (i = 0; i < be32_to_cpu(fmhdr->vol_count); i++) {
394 u32 vol_id, vol_type, used, reserved;
396 fmvhdr = (struct ubi_fm_volhdr *)(fm_raw + fm_pos);
397 fm_pos += sizeof(*fmvhdr);
398 if (fm_pos >= fm_size)
401 if (be32_to_cpu(fmvhdr->magic) != UBI_FM_VHDR_MAGIC) {
402 ubi_err("bad fastmap vol header magic: 0x%x, " \
404 be32_to_cpu(fmvhdr->magic), UBI_FM_VHDR_MAGIC);
408 vol_id = be32_to_cpu(fmvhdr->vol_id);
409 vol_type = fmvhdr->vol_type;
410 used = be32_to_cpu(fmvhdr->used_ebs);
412 fm_eba = (struct ubi_fm_eba *)(fm_raw + fm_pos);
413 fm_pos += sizeof(*fm_eba);
414 fm_pos += (sizeof(__be32) * be32_to_cpu(fm_eba->reserved_pebs));
415 if (fm_pos >= fm_size)
418 if (be32_to_cpu(fm_eba->magic) != UBI_FM_EBA_MAGIC) {
419 ubi_err("bad fastmap EBA header magic: 0x%x, " \
421 be32_to_cpu(fm_eba->magic), UBI_FM_EBA_MAGIC);
425 reserved = be32_to_cpu(fm_eba->reserved_pebs);
426 ubi_dbg("FA: vol %u used %u res: %u", vol_id, used, reserved);
427 for (j = 0; j < reserved; j++) {
428 int pnum = be32_to_cpu(fm_eba->pnum[j]);
430 if ((int)be32_to_cpu(fm_eba->pnum[j]) < 0)
433 if (!__test_and_clear_bit(pnum, ubi->fm_used))
437 * We only handle static volumes so used_ebs
438 * needs to be handed in. And we do not assign
439 * the reserved blocks
444 ret = assign_aeb_to_av(ubi, pnum, j, vol_id,
450 * Nasty: The fastmap claims that the volume
451 * has one block more than it, but that block
452 * is always empty and the other blocks have
453 * the correct number of total LEBs in the
454 * headers. Deal with it.
456 if (ret != UBI_IO_FF && j != used - 1)
458 ubi_dbg("FA: Vol: %u Ignoring empty LEB %d of %d",
463 ret = scan_pool(ubi, fmpl1->pebs, pool_size);
467 ret = scan_pool(ubi, fmpl2->pebs, wl_pool_size);
473 * If fastmap is leaking PEBs (must not happen), raise a
474 * fat warning and fall back to scanning mode.
475 * We do this here because in ubi_wl_init() it's too late
476 * and we cannot fall back to scanning.
478 if (WARN_ON(count_fastmap_pebs(ai) != ubi->peb_count -
479 ai->bad_peb_count - fm->used_blocks))
486 ret = UBI_BAD_FASTMAP;
491 static int ubi_scan_fastmap(struct ubi_scan_info *ubi,
492 struct ubi_attach_info *ai,
495 struct ubi_fm_sb *fmsb, *fmsb2;
496 struct ubi_vid_hdr *vh;
497 struct ubi_fastmap_layout *fm;
498 int i, used_blocks, pnum, ret = 0;
501 unsigned long long sqnum = 0;
504 fm = &ubi->fm_layout;
506 ret = ubi_io_read(ubi, fmsb, fm_anchor, ubi->leb_start, sizeof(*fmsb));
507 if (ret && ret != UBI_IO_BITFLIPS)
509 else if (ret == UBI_IO_BITFLIPS)
510 fm->to_be_tortured[0] = 1;
512 if (be32_to_cpu(fmsb->magic) != UBI_FM_SB_MAGIC) {
513 ubi_err("bad super block magic: 0x%x, expected: 0x%x",
514 be32_to_cpu(fmsb->magic), UBI_FM_SB_MAGIC);
515 ret = UBI_BAD_FASTMAP;
519 if (fmsb->version != UBI_FM_FMT_VERSION) {
520 ubi_err("bad fastmap version: %i, expected: %i",
521 fmsb->version, UBI_FM_FMT_VERSION);
522 ret = UBI_BAD_FASTMAP;
526 used_blocks = be32_to_cpu(fmsb->used_blocks);
527 if (used_blocks > UBI_FM_MAX_BLOCKS || used_blocks < 1) {
528 ubi_err("number of fastmap blocks is invalid: %i", used_blocks);
529 ret = UBI_BAD_FASTMAP;
533 fm_size = ubi->leb_size * used_blocks;
534 if (fm_size != ubi->fm_size) {
535 ubi_err("bad fastmap size: %zi, expected: %zi", fm_size,
537 ret = UBI_BAD_FASTMAP;
543 for (i = 0; i < used_blocks; i++) {
544 pnum = be32_to_cpu(fmsb->block_loc[i]);
546 if (ubi_io_is_bad(ubi, pnum)) {
547 ret = UBI_BAD_FASTMAP;
553 ret = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
554 if (ret && ret != UBI_IO_BITFLIPS) {
555 ubi_err("unable to read fastmap block# %i EC (PEB: %i)",
558 ret = UBI_BAD_FASTMAP;
560 } else if (ret == UBI_IO_BITFLIPS)
561 fm->to_be_tortured[i] = 1;
563 image_seq = be32_to_cpu(ech->image_seq);
565 ubi->image_seq = image_seq;
567 * Older UBI implementations have image_seq set to zero, so
568 * we shouldn't fail if image_seq == 0.
570 if (image_seq && (image_seq != ubi->image_seq)) {
571 ubi_err("wrong image seq:%d instead of %d",
572 be32_to_cpu(ech->image_seq), ubi->image_seq);
573 ret = UBI_BAD_FASTMAP;
577 ret = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
578 if (ret && ret != UBI_IO_BITFLIPS) {
579 ubi_err("unable to read fastmap block# %i (PEB: %i)",
585 * Mainline code rescans the anchor header. We've done
586 * that already so we merily copy it over.
588 if (pnum == fm_anchor)
589 memcpy(vh, ubi->blockinfo + pnum, sizeof(*fm));
592 if (be32_to_cpu(vh->vol_id) != UBI_FM_SB_VOLUME_ID) {
593 ubi_err("bad fastmap anchor vol_id: 0x%x," \
595 be32_to_cpu(vh->vol_id),
596 UBI_FM_SB_VOLUME_ID);
597 ret = UBI_BAD_FASTMAP;
601 if (be32_to_cpu(vh->vol_id) != UBI_FM_DATA_VOLUME_ID) {
602 ubi_err("bad fastmap data vol_id: 0x%x," \
604 be32_to_cpu(vh->vol_id),
605 UBI_FM_DATA_VOLUME_ID);
606 ret = UBI_BAD_FASTMAP;
611 if (sqnum < be64_to_cpu(vh->sqnum))
612 sqnum = be64_to_cpu(vh->sqnum);
614 ret = ubi_io_read(ubi, ubi->fm_buf + (ubi->leb_size * i), pnum,
615 ubi->leb_start, ubi->leb_size);
616 if (ret && ret != UBI_IO_BITFLIPS) {
617 ubi_err("unable to read fastmap block# %i (PEB: %i, " \
618 "err: %i)", i, pnum, ret);
623 fmsb2 = (struct ubi_fm_sb *)(ubi->fm_buf);
624 tmp_crc = be32_to_cpu(fmsb2->data_crc);
626 crc = crc32(UBI_CRC32_INIT, ubi->fm_buf, fm_size);
627 if (crc != tmp_crc) {
628 ubi_err("fastmap data CRC is invalid");
629 ubi_err("CRC should be: 0x%x, calc: 0x%x", tmp_crc, crc);
630 ret = UBI_BAD_FASTMAP;
634 fmsb2->sqnum = sqnum;
636 fm->used_blocks = used_blocks;
638 ret = ubi_attach_fastmap(ubi, ai, fm);
641 ret = UBI_BAD_FASTMAP;
646 ubi->fm_pool.max_size = ubi->fm->max_pool_size;
647 ubi->fm_wl_pool.max_size = ubi->fm->max_wl_pool_size;
648 ubi_msg("attached by fastmap %uMB %u blocks",
649 ubi->fsize_mb, ubi->peb_count);
650 ubi_dbg("fastmap pool size: %d", ubi->fm_pool.max_size);
651 ubi_dbg("fastmap WL pool size: %d", ubi->fm_wl_pool.max_size);
655 ubi_err("Attach by fastmap failed, doing a full scan!");
664 * Scan the flash and attempt to attach via fastmap
666 static void ipl_scan(struct ubi_scan_info *ubi)
672 * Scan first for the fastmap super block
674 for (pnum = 0; pnum < UBI_FM_MAX_START; pnum++) {
675 res = ubi_scan_vid_hdr(ubi, ubi->blockinfo + pnum, pnum);
677 * We ignore errors here as we are meriliy scanning
680 if (res != UBI_FASTMAP_ANCHOR)
684 * If fastmap is disabled, continue scanning. This
685 * might happen because the previous attempt failed or
686 * the caller disabled it right away.
688 if (!ubi->fm_enabled)
692 * Try to attach the fastmap, if that fails continue
695 if (!ubi_scan_fastmap(ubi, NULL, pnum))
698 * Fastmap failed. Clear everything we have and start
699 * over. We are paranoid and do not trust anything.
701 memset(ubi->volinfo, 0, sizeof(ubi->volinfo));
707 * Continue scanning, ignore errors, we might find what we are
710 for (; pnum < ubi->peb_count; pnum++)
711 ubi_scan_vid_hdr(ubi, ubi->blockinfo + pnum, pnum);
715 * Load a logical block of a volume into memory
717 static int ubi_load_block(struct ubi_scan_info *ubi, uint8_t *laddr,
718 struct ubi_vol_info *vi, u32 vol_id, u32 lnum,
721 struct ubi_vid_hdr *vh, *vrepl;
726 * If this is a fastmap run, we try to rescan full, otherwise
729 if (!test_bit(lnum, vi->found)) {
730 ubi_warn("LEB %d of %d is missing", lnum, last);
734 pnum = vi->lebs_to_pebs[lnum];
736 ubi_dbg("Load vol %u LEB %u PEB %u", vol_id, lnum, pnum);
738 if (ubi_io_is_bad(ubi, pnum)) {
739 ubi_warn("Corrupted mapping block %d PB %d\n", lnum, pnum);
743 if (test_bit(pnum, ubi->corrupt))
747 * Lets try to read that block
749 vh = ubi->blockinfo + pnum;
751 if (!test_bit(pnum, ubi->scanned)) {
752 ubi_warn("Vol: %u LEB %u PEB %u not yet scanned", vol_id,
754 if (ubi_rescan_fm_vid_hdr(ubi, vh, pnum, vol_id, lnum))
759 * Check, if the total number of blocks is correct
761 if (be32_to_cpu(vh->used_ebs) != last) {
762 ubi_dbg("Block count missmatch.");
763 ubi_dbg("vh->used_ebs: %d nrblocks: %d",
764 be32_to_cpu(vh->used_ebs), last);
765 generic_set_bit(pnum, ubi->corrupt);
770 * Get the data length of this block.
772 dlen = be32_to_cpu(vh->data_size);
775 * Read the data into RAM. We ignore the return value
776 * here as the only thing which might go wrong are
777 * bitflips. Try nevertheless.
779 ubi_io_read(ubi, laddr, pnum, ubi->leb_start, dlen);
781 /* Calculate CRC over the data */
782 crc = crc32(UBI_CRC32_INIT, laddr, dlen);
784 if (crc != be32_to_cpu(vh->data_crc)) {
785 ubi_warn("Vol: %u LEB %u PEB %u data CRC failure", vol_id,
787 generic_set_bit(pnum, ubi->corrupt);
791 /* We are good. Return the data length we read */
795 ubi_dbg("Find replacement for LEB %u PEB %u", lnum, pnum);
796 generic_clear_bit(lnum, vi->found);
799 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
800 struct ubi_vid_hdr *tmp = ubi->blockinfo + pnum;
801 u32 t_vol_id = be32_to_cpu(tmp->vol_id);
802 u32 t_lnum = be32_to_cpu(tmp->lnum);
804 if (test_bit(pnum, ubi->corrupt))
807 if (t_vol_id != vol_id || t_lnum != lnum)
810 if (!test_bit(pnum, ubi->scanned)) {
811 ubi_warn("Vol: %u LEB %u PEB %u not yet scanned",
813 if (ubi_rescan_fm_vid_hdr(ubi, tmp, pnum, vol_id, lnum))
818 * We found one. If its the first, assign it otherwise
821 generic_set_bit(lnum, vi->found);
828 if (be64_to_cpu(vrepl->sqnum) < be64_to_cpu(tmp->sqnum))
833 /* Update the vi table */
834 pnum = vrepl - ubi->blockinfo;
835 vi->lebs_to_pebs[lnum] = pnum;
836 ubi_dbg("Trying PEB %u for LEB %u", pnum, lnum);
843 * Load a volume into RAM
845 static int ipl_load(struct ubi_scan_info *ubi, const u32 vol_id, uint8_t *laddr)
847 struct ubi_vol_info *vi;
850 if (vol_id >= UBI_SPL_VOL_IDS)
854 vi = ubi->volinfo + vol_id;
855 last = vi->last_block + 1;
857 /* Read the blocks to RAM, check CRC */
858 for (lnum = 0 ; lnum < last; lnum++) {
859 int res = ubi_load_block(ubi, laddr, vi, vol_id, lnum, last);
862 ubi_warn("Failed to load volume %u", vol_id);
865 /* res is the data length of the read block */
872 int ubispl_load_volumes(struct ubispl_info *info, struct ubispl_load *lvols,
875 struct ubi_scan_info *ubi = info->ubi;
876 int res, i, fastmap = info->fastmap;
881 * We do a partial initializiation of @ubi. Cleaning fm_buf is
884 memset(ubi, 0, offsetof(struct ubi_scan_info, fm_buf));
886 ubi->read = info->read;
888 /* Precalculate the offsets */
889 ubi->vid_offset = info->vid_offset;
890 ubi->leb_start = info->leb_start;
891 ubi->leb_size = info->peb_size - ubi->leb_start;
892 ubi->peb_count = info->peb_count;
893 ubi->peb_offset = info->peb_offset;
895 fsize = info->peb_size * info->peb_count;
896 ubi->fsize_mb = fsize >> 20;
899 ubi->fm_size = ubi_calc_fm_size(ubi);
900 ubi->fm_enabled = fastmap;
902 for (i = 0; i < nrvols; i++) {
903 struct ubispl_load *lv = lvols + i;
905 generic_set_bit(lv->vol_id, ubi->toload);
910 for (i = 0; i < nrvols; i++) {
911 struct ubispl_load *lv = lvols + i;
913 ubi_msg("Loading VolId #%d", lv->vol_id);
914 res = ipl_load(ubi, lv->vol_id, lv->load_addr);