2 * Copyright (c) Thomas Gleixner <tglx@linutronix.de>
4 * The parts taken from the kernel implementation are:
6 * Copyright (c) International Business Machines Corp., 2006
8 * SPDX-License-Identifier: GPL 2.0+ BSD-3-Clause
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 static int ubi_io_read_vid_hdr(struct ubi_scan_info *ubi, int pnum,
50 struct ubi_vid_hdr *vh, int unused)
55 /* No point in rescanning a corrupt block */
56 if (test_bit(pnum, ubi->corrupt))
57 return UBI_IO_BAD_HDR;
59 * If the block has been scanned already, no need to rescan
61 if (test_and_set_bit(pnum, ubi->scanned))
64 res = ubi_io_read(ubi, vh, pnum, ubi->vid_offset, sizeof(*vh));
67 * Bad block, unrecoverable ECC error, skip the block
70 ubi_dbg("Skipping bad or unreadable block %d", pnum);
72 generic_set_bit(pnum, ubi->corrupt);
76 /* Magic number available ? */
77 magic = be32_to_cpu(vh->magic);
78 if (magic != UBI_VID_HDR_MAGIC) {
79 generic_set_bit(pnum, ubi->corrupt);
80 if (magic == 0xffffffff)
82 ubi_msg("Bad magic in block 0%d %08x", pnum, magic);
83 return UBI_IO_BAD_HDR;
86 /* Header CRC correct ? */
87 if (crc32(UBI_CRC32_INIT, vh, UBI_VID_HDR_SIZE_CRC) !=
88 be32_to_cpu(vh->hdr_crc)) {
89 ubi_msg("Bad CRC in block 0%d", pnum);
90 generic_set_bit(pnum, ubi->corrupt);
91 return UBI_IO_BAD_HDR;
94 ubi_dbg("RV: pnum: %i sqnum %llu", pnum, be64_to_cpu(vh->sqnum));
99 static int ubi_rescan_fm_vid_hdr(struct ubi_scan_info *ubi,
100 struct ubi_vid_hdr *vh,
101 u32 fm_pnum, u32 fm_vol_id, u32 fm_lnum)
105 if (ubi_io_is_bad(ubi, fm_pnum))
108 res = ubi_io_read_vid_hdr(ubi, fm_pnum, vh, 0);
110 /* Check volume id, volume type and lnum */
111 if (be32_to_cpu(vh->vol_id) == fm_vol_id &&
112 vh->vol_type == UBI_VID_STATIC &&
113 be32_to_cpu(vh->lnum) == fm_lnum)
115 ubi_dbg("RS: PEB %u vol: %u : %u typ %u lnum %u %u",
116 fm_pnum, fm_vol_id, vh->vol_type,
117 be32_to_cpu(vh->vol_id),
118 fm_lnum, be32_to_cpu(vh->lnum));
123 /* Insert the logic block into the volume info */
124 static int ubi_add_peb_to_vol(struct ubi_scan_info *ubi,
125 struct ubi_vid_hdr *vh, u32 vol_id,
128 struct ubi_vol_info *vi = ubi->volinfo + vol_id;
132 * If the volume is larger than expected, yell and give up :(
134 if (lnum >= UBI_MAX_VOL_LEBS) {
135 ubi_warn("Vol: %u LEB %d > %d", vol_id, lnum, UBI_MAX_VOL_LEBS);
139 ubi_dbg("SC: Add PEB %u to Vol %u as LEB %u fnd %d sc %d",
140 pnum, vol_id, lnum, !!test_bit(lnum, vi->found),
141 !!test_bit(pnum, ubi->scanned));
143 /* Points to the translation entry */
144 ltp = vi->lebs_to_pebs + lnum;
146 /* If the block is already assigned, check sqnum */
147 if (__test_and_set_bit(lnum, vi->found)) {
149 struct ubi_vid_hdr *cur = ubi->blockinfo + cur_pnum;
152 * If the current block hase not yet been scanned, we
153 * need to do that. The other block might be stale or
154 * the current block corrupted and the FM not yet
157 if (!test_bit(cur_pnum, ubi->scanned)) {
159 * If the scan fails, we use the valid block
161 if (ubi_rescan_fm_vid_hdr(ubi, cur, cur_pnum, vol_id,
169 * Should not happen ....
171 if (test_bit(cur_pnum, ubi->corrupt)) {
176 ubi_dbg("Vol %u LEB %u PEB %u->sqnum %llu NPEB %u->sqnum %llu",
177 vol_id, lnum, cur_pnum, be64_to_cpu(cur->sqnum), pnum,
178 be64_to_cpu(vh->sqnum));
181 * Compare sqnum and take the newer one
183 if (be64_to_cpu(cur->sqnum) < be64_to_cpu(vh->sqnum))
187 if (lnum > vi->last_block)
188 vi->last_block = lnum;
194 static int ubi_scan_vid_hdr(struct ubi_scan_info *ubi, struct ubi_vid_hdr *vh,
200 if (ubi_io_is_bad(ubi, pnum))
203 res = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
208 vol_id = be32_to_cpu(vh->vol_id);
210 /* If this is the fastmap anchor, return right away */
211 if (vol_id == UBI_FM_SB_VOLUME_ID)
212 return ubi->fm_enabled ? UBI_FASTMAP_ANCHOR : 0;
214 /* We only care about static volumes with an id < UBI_SPL_VOL_IDS */
215 if (vol_id >= UBI_SPL_VOL_IDS || vh->vol_type != UBI_VID_STATIC)
218 /* We are only interested in the volumes to load */
219 if (!test_bit(vol_id, ubi->toload))
222 lnum = be32_to_cpu(vh->lnum);
223 return ubi_add_peb_to_vol(ubi, vh, vol_id, pnum, lnum);
226 static int assign_aeb_to_av(struct ubi_scan_info *ubi, u32 pnum, u32 lnum,
227 u32 vol_id, u32 vol_type, u32 used)
229 struct ubi_vid_hdr *vh;
231 if (ubi_io_is_bad(ubi, pnum))
236 if (vol_id >= UBI_SPL_VOL_IDS || vol_type != UBI_STATIC_VOLUME)
239 /* We are only interested in the volumes to load */
240 if (!test_bit(vol_id, ubi->toload))
243 vh = ubi->blockinfo + pnum;
245 return ubi_scan_vid_hdr(ubi, vh, pnum);
248 static int scan_pool(struct ubi_scan_info *ubi, __be32 *pebs, int pool_size)
250 struct ubi_vid_hdr *vh;
254 ubi_dbg("Scanning pool size: %d", pool_size);
256 for (i = 0; i < pool_size; i++) {
257 pnum = be32_to_cpu(pebs[i]);
259 if (ubi_io_is_bad(ubi, pnum)) {
260 ubi_err("FM: Bad PEB in fastmap pool! %u", pnum);
261 return UBI_BAD_FASTMAP;
264 vh = ubi->blockinfo + pnum;
266 * We allow the scan to fail here. The loader will notice
267 * and look for a replacement.
269 ubi_scan_vid_hdr(ubi, vh, pnum);
275 * Fastmap code is stolen from Linux kernel and this stub structure is used
278 struct ubi_attach_info {
282 static int ubi_attach_fastmap(struct ubi_scan_info *ubi,
283 struct ubi_attach_info *ai,
284 struct ubi_fastmap_layout *fm)
286 struct ubi_fm_hdr *fmhdr;
287 struct ubi_fm_scan_pool *fmpl1, *fmpl2;
288 struct ubi_fm_ec *fmec;
289 struct ubi_fm_volhdr *fmvhdr;
290 struct ubi_fm_eba *fm_eba;
291 int ret, i, j, pool_size, wl_pool_size;
292 size_t fm_pos = 0, fm_size = ubi->fm_size;
293 void *fm_raw = ubi->fm_buf;
295 memset(ubi->fm_used, 0, sizeof(ubi->fm_used));
297 fm_pos += sizeof(struct ubi_fm_sb);
298 if (fm_pos >= fm_size)
301 fmhdr = (struct ubi_fm_hdr *)(fm_raw + fm_pos);
302 fm_pos += sizeof(*fmhdr);
303 if (fm_pos >= fm_size)
306 if (be32_to_cpu(fmhdr->magic) != UBI_FM_HDR_MAGIC) {
307 ubi_err("bad fastmap header magic: 0x%x, expected: 0x%x",
308 be32_to_cpu(fmhdr->magic), UBI_FM_HDR_MAGIC);
312 fmpl1 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
313 fm_pos += sizeof(*fmpl1);
314 if (fm_pos >= fm_size)
316 if (be32_to_cpu(fmpl1->magic) != UBI_FM_POOL_MAGIC) {
317 ubi_err("bad fastmap pool magic: 0x%x, expected: 0x%x",
318 be32_to_cpu(fmpl1->magic), UBI_FM_POOL_MAGIC);
322 fmpl2 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
323 fm_pos += sizeof(*fmpl2);
324 if (fm_pos >= fm_size)
326 if (be32_to_cpu(fmpl2->magic) != UBI_FM_POOL_MAGIC) {
327 ubi_err("bad fastmap pool magic: 0x%x, expected: 0x%x",
328 be32_to_cpu(fmpl2->magic), UBI_FM_POOL_MAGIC);
332 pool_size = be16_to_cpu(fmpl1->size);
333 wl_pool_size = be16_to_cpu(fmpl2->size);
334 fm->max_pool_size = be16_to_cpu(fmpl1->max_size);
335 fm->max_wl_pool_size = be16_to_cpu(fmpl2->max_size);
337 if (pool_size > UBI_FM_MAX_POOL_SIZE || pool_size < 0) {
338 ubi_err("bad pool size: %i", pool_size);
342 if (wl_pool_size > UBI_FM_MAX_POOL_SIZE || wl_pool_size < 0) {
343 ubi_err("bad WL pool size: %i", wl_pool_size);
347 if (fm->max_pool_size > UBI_FM_MAX_POOL_SIZE ||
348 fm->max_pool_size < 0) {
349 ubi_err("bad maximal pool size: %i", fm->max_pool_size);
353 if (fm->max_wl_pool_size > UBI_FM_MAX_POOL_SIZE ||
354 fm->max_wl_pool_size < 0) {
355 ubi_err("bad maximal WL pool size: %i", fm->max_wl_pool_size);
359 /* read EC values from free list */
360 for (i = 0; i < be32_to_cpu(fmhdr->free_peb_count); i++) {
361 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
362 fm_pos += sizeof(*fmec);
363 if (fm_pos >= fm_size)
367 /* read EC values from used list */
368 for (i = 0; i < be32_to_cpu(fmhdr->used_peb_count); i++) {
369 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
370 fm_pos += sizeof(*fmec);
371 if (fm_pos >= fm_size)
374 generic_set_bit(be32_to_cpu(fmec->pnum), ubi->fm_used);
377 /* read EC values from scrub list */
378 for (i = 0; i < be32_to_cpu(fmhdr->scrub_peb_count); i++) {
379 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
380 fm_pos += sizeof(*fmec);
381 if (fm_pos >= fm_size)
385 /* read EC values from erase list */
386 for (i = 0; i < be32_to_cpu(fmhdr->erase_peb_count); i++) {
387 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
388 fm_pos += sizeof(*fmec);
389 if (fm_pos >= fm_size)
393 /* Iterate over all volumes and read their EBA table */
394 for (i = 0; i < be32_to_cpu(fmhdr->vol_count); i++) {
395 u32 vol_id, vol_type, used, reserved;
397 fmvhdr = (struct ubi_fm_volhdr *)(fm_raw + fm_pos);
398 fm_pos += sizeof(*fmvhdr);
399 if (fm_pos >= fm_size)
402 if (be32_to_cpu(fmvhdr->magic) != UBI_FM_VHDR_MAGIC) {
403 ubi_err("bad fastmap vol header magic: 0x%x, " \
405 be32_to_cpu(fmvhdr->magic), UBI_FM_VHDR_MAGIC);
409 vol_id = be32_to_cpu(fmvhdr->vol_id);
410 vol_type = fmvhdr->vol_type;
411 used = be32_to_cpu(fmvhdr->used_ebs);
413 fm_eba = (struct ubi_fm_eba *)(fm_raw + fm_pos);
414 fm_pos += sizeof(*fm_eba);
415 fm_pos += (sizeof(__be32) * be32_to_cpu(fm_eba->reserved_pebs));
416 if (fm_pos >= fm_size)
419 if (be32_to_cpu(fm_eba->magic) != UBI_FM_EBA_MAGIC) {
420 ubi_err("bad fastmap EBA header magic: 0x%x, " \
422 be32_to_cpu(fm_eba->magic), UBI_FM_EBA_MAGIC);
426 reserved = be32_to_cpu(fm_eba->reserved_pebs);
427 ubi_dbg("FA: vol %u used %u res: %u", vol_id, used, reserved);
428 for (j = 0; j < reserved; j++) {
429 int pnum = be32_to_cpu(fm_eba->pnum[j]);
431 if ((int)be32_to_cpu(fm_eba->pnum[j]) < 0)
434 if (!__test_and_clear_bit(pnum, ubi->fm_used))
438 * We only handle static volumes so used_ebs
439 * needs to be handed in. And we do not assign
440 * the reserved blocks
445 ret = assign_aeb_to_av(ubi, pnum, j, vol_id,
451 * Nasty: The fastmap claims that the volume
452 * has one block more than it, but that block
453 * is always empty and the other blocks have
454 * the correct number of total LEBs in the
455 * headers. Deal with it.
457 if (ret != UBI_IO_FF && j != used - 1)
459 ubi_dbg("FA: Vol: %u Ignoring empty LEB %d of %d",
464 ret = scan_pool(ubi, fmpl1->pebs, pool_size);
468 ret = scan_pool(ubi, fmpl2->pebs, wl_pool_size);
474 * If fastmap is leaking PEBs (must not happen), raise a
475 * fat warning and fall back to scanning mode.
476 * We do this here because in ubi_wl_init() it's too late
477 * and we cannot fall back to scanning.
479 if (WARN_ON(count_fastmap_pebs(ai) != ubi->peb_count -
480 ai->bad_peb_count - fm->used_blocks))
487 ret = UBI_BAD_FASTMAP;
492 static int ubi_scan_fastmap(struct ubi_scan_info *ubi,
493 struct ubi_attach_info *ai,
496 struct ubi_fm_sb *fmsb, *fmsb2;
497 struct ubi_vid_hdr *vh;
498 struct ubi_fastmap_layout *fm;
499 int i, used_blocks, pnum, ret = 0;
502 unsigned long long sqnum = 0;
505 fm = &ubi->fm_layout;
507 ret = ubi_io_read(ubi, fmsb, fm_anchor, ubi->leb_start, sizeof(*fmsb));
508 if (ret && ret != UBI_IO_BITFLIPS)
510 else if (ret == UBI_IO_BITFLIPS)
511 fm->to_be_tortured[0] = 1;
513 if (be32_to_cpu(fmsb->magic) != UBI_FM_SB_MAGIC) {
514 ubi_err("bad super block magic: 0x%x, expected: 0x%x",
515 be32_to_cpu(fmsb->magic), UBI_FM_SB_MAGIC);
516 ret = UBI_BAD_FASTMAP;
520 if (fmsb->version != UBI_FM_FMT_VERSION) {
521 ubi_err("bad fastmap version: %i, expected: %i",
522 fmsb->version, UBI_FM_FMT_VERSION);
523 ret = UBI_BAD_FASTMAP;
527 used_blocks = be32_to_cpu(fmsb->used_blocks);
528 if (used_blocks > UBI_FM_MAX_BLOCKS || used_blocks < 1) {
529 ubi_err("number of fastmap blocks is invalid: %i", used_blocks);
530 ret = UBI_BAD_FASTMAP;
534 fm_size = ubi->leb_size * used_blocks;
535 if (fm_size != ubi->fm_size) {
536 ubi_err("bad fastmap size: %zi, expected: %zi", fm_size,
538 ret = UBI_BAD_FASTMAP;
544 for (i = 0; i < used_blocks; i++) {
545 pnum = be32_to_cpu(fmsb->block_loc[i]);
547 if (ubi_io_is_bad(ubi, pnum)) {
548 ret = UBI_BAD_FASTMAP;
554 ret = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
555 if (ret && ret != UBI_IO_BITFLIPS) {
556 ubi_err("unable to read fastmap block# %i EC (PEB: %i)",
559 ret = UBI_BAD_FASTMAP;
561 } else if (ret == UBI_IO_BITFLIPS)
562 fm->to_be_tortured[i] = 1;
564 image_seq = be32_to_cpu(ech->image_seq);
566 ubi->image_seq = image_seq;
568 * Older UBI implementations have image_seq set to zero, so
569 * we shouldn't fail if image_seq == 0.
571 if (image_seq && (image_seq != ubi->image_seq)) {
572 ubi_err("wrong image seq:%d instead of %d",
573 be32_to_cpu(ech->image_seq), ubi->image_seq);
574 ret = UBI_BAD_FASTMAP;
578 ret = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
579 if (ret && ret != UBI_IO_BITFLIPS) {
580 ubi_err("unable to read fastmap block# %i (PEB: %i)",
586 * Mainline code rescans the anchor header. We've done
587 * that already so we merily copy it over.
589 if (pnum == fm_anchor)
590 memcpy(vh, ubi->blockinfo + pnum, sizeof(*fm));
593 if (be32_to_cpu(vh->vol_id) != UBI_FM_SB_VOLUME_ID) {
594 ubi_err("bad fastmap anchor vol_id: 0x%x," \
596 be32_to_cpu(vh->vol_id),
597 UBI_FM_SB_VOLUME_ID);
598 ret = UBI_BAD_FASTMAP;
602 if (be32_to_cpu(vh->vol_id) != UBI_FM_DATA_VOLUME_ID) {
603 ubi_err("bad fastmap data vol_id: 0x%x," \
605 be32_to_cpu(vh->vol_id),
606 UBI_FM_DATA_VOLUME_ID);
607 ret = UBI_BAD_FASTMAP;
612 if (sqnum < be64_to_cpu(vh->sqnum))
613 sqnum = be64_to_cpu(vh->sqnum);
615 ret = ubi_io_read(ubi, ubi->fm_buf + (ubi->leb_size * i), pnum,
616 ubi->leb_start, ubi->leb_size);
617 if (ret && ret != UBI_IO_BITFLIPS) {
618 ubi_err("unable to read fastmap block# %i (PEB: %i, " \
619 "err: %i)", i, pnum, ret);
624 fmsb2 = (struct ubi_fm_sb *)(ubi->fm_buf);
625 tmp_crc = be32_to_cpu(fmsb2->data_crc);
627 crc = crc32(UBI_CRC32_INIT, ubi->fm_buf, fm_size);
628 if (crc != tmp_crc) {
629 ubi_err("fastmap data CRC is invalid");
630 ubi_err("CRC should be: 0x%x, calc: 0x%x", tmp_crc, crc);
631 ret = UBI_BAD_FASTMAP;
635 fmsb2->sqnum = sqnum;
637 fm->used_blocks = used_blocks;
639 ret = ubi_attach_fastmap(ubi, ai, fm);
642 ret = UBI_BAD_FASTMAP;
647 ubi->fm_pool.max_size = ubi->fm->max_pool_size;
648 ubi->fm_wl_pool.max_size = ubi->fm->max_wl_pool_size;
649 ubi_msg("attached by fastmap %uMB %u blocks",
650 ubi->fsize_mb, ubi->peb_count);
651 ubi_dbg("fastmap pool size: %d", ubi->fm_pool.max_size);
652 ubi_dbg("fastmap WL pool size: %d", ubi->fm_wl_pool.max_size);
656 ubi_err("Attach by fastmap failed, doing a full scan!");
665 * Scan the flash and attempt to attach via fastmap
667 static void ipl_scan(struct ubi_scan_info *ubi)
673 * Scan first for the fastmap super block
675 for (pnum = 0; pnum < UBI_FM_MAX_START; pnum++) {
676 res = ubi_scan_vid_hdr(ubi, ubi->blockinfo + pnum, pnum);
678 * We ignore errors here as we are meriliy scanning
681 if (res != UBI_FASTMAP_ANCHOR)
685 * If fastmap is disabled, continue scanning. This
686 * might happen because the previous attempt failed or
687 * the caller disabled it right away.
689 if (!ubi->fm_enabled)
693 * Try to attach the fastmap, if that fails continue
696 if (!ubi_scan_fastmap(ubi, NULL, pnum))
699 * Fastmap failed. Clear everything we have and start
700 * over. We are paranoid and do not trust anything.
702 memset(ubi->volinfo, 0, sizeof(ubi->volinfo));
708 * Continue scanning, ignore errors, we might find what we are
711 for (; pnum < ubi->peb_count; pnum++)
712 ubi_scan_vid_hdr(ubi, ubi->blockinfo + pnum, pnum);
716 * Load a logical block of a volume into memory
718 static int ubi_load_block(struct ubi_scan_info *ubi, uint8_t *laddr,
719 struct ubi_vol_info *vi, u32 vol_id, u32 lnum,
722 struct ubi_vid_hdr *vh, *vrepl;
727 * If this is a fastmap run, we try to rescan full, otherwise
730 if (!test_bit(lnum, vi->found)) {
731 ubi_warn("LEB %d of %d is missing", lnum, last);
735 pnum = vi->lebs_to_pebs[lnum];
737 ubi_dbg("Load vol %u LEB %u PEB %u", vol_id, lnum, pnum);
739 if (ubi_io_is_bad(ubi, pnum)) {
740 ubi_warn("Corrupted mapping block %d PB %d\n", lnum, pnum);
744 if (test_bit(pnum, ubi->corrupt))
748 * Lets try to read that block
750 vh = ubi->blockinfo + pnum;
752 if (!test_bit(pnum, ubi->scanned)) {
753 ubi_warn("Vol: %u LEB %u PEB %u not yet scanned", vol_id,
755 if (ubi_rescan_fm_vid_hdr(ubi, vh, pnum, vol_id, lnum))
760 * Check, if the total number of blocks is correct
762 if (be32_to_cpu(vh->used_ebs) != last) {
763 ubi_dbg("Block count missmatch.");
764 ubi_dbg("vh->used_ebs: %d nrblocks: %d",
765 be32_to_cpu(vh->used_ebs), last);
766 generic_set_bit(pnum, ubi->corrupt);
771 * Get the data length of this block.
773 dlen = be32_to_cpu(vh->data_size);
776 * Read the data into RAM. We ignore the return value
777 * here as the only thing which might go wrong are
778 * bitflips. Try nevertheless.
780 ubi_io_read(ubi, laddr, pnum, ubi->leb_start, dlen);
782 /* Calculate CRC over the data */
783 crc = crc32(UBI_CRC32_INIT, laddr, dlen);
785 if (crc != be32_to_cpu(vh->data_crc)) {
786 ubi_warn("Vol: %u LEB %u PEB %u data CRC failure", vol_id,
788 generic_set_bit(pnum, ubi->corrupt);
792 /* We are good. Return the data length we read */
796 ubi_dbg("Find replacement for LEB %u PEB %u", lnum, pnum);
797 generic_clear_bit(lnum, vi->found);
800 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
801 struct ubi_vid_hdr *tmp = ubi->blockinfo + pnum;
802 u32 t_vol_id = be32_to_cpu(tmp->vol_id);
803 u32 t_lnum = be32_to_cpu(tmp->lnum);
805 if (test_bit(pnum, ubi->corrupt))
808 if (t_vol_id != vol_id || t_lnum != lnum)
811 if (!test_bit(pnum, ubi->scanned)) {
812 ubi_warn("Vol: %u LEB %u PEB %u not yet scanned",
814 if (ubi_rescan_fm_vid_hdr(ubi, tmp, pnum, vol_id, lnum))
819 * We found one. If its the first, assign it otherwise
822 generic_set_bit(lnum, vi->found);
829 if (be64_to_cpu(vrepl->sqnum) < be64_to_cpu(tmp->sqnum))
834 /* Update the vi table */
835 pnum = vrepl - ubi->blockinfo;
836 vi->lebs_to_pebs[lnum] = pnum;
837 ubi_dbg("Trying PEB %u for LEB %u", pnum, lnum);
844 * Load a volume into RAM
846 static int ipl_load(struct ubi_scan_info *ubi, const u32 vol_id, uint8_t *laddr)
848 struct ubi_vol_info *vi;
851 if (vol_id >= UBI_SPL_VOL_IDS)
855 vi = ubi->volinfo + vol_id;
856 last = vi->last_block + 1;
858 /* Read the blocks to RAM, check CRC */
859 for (lnum = 0 ; lnum < last; lnum++) {
860 int res = ubi_load_block(ubi, laddr, vi, vol_id, lnum, last);
863 ubi_warn("Failed to load volume %u", vol_id);
866 /* res is the data length of the read block */
873 int ubispl_load_volumes(struct ubispl_info *info, struct ubispl_load *lvols,
876 struct ubi_scan_info *ubi = info->ubi;
877 int res, i, fastmap = info->fastmap;
882 * We do a partial initializiation of @ubi. Cleaning fm_buf is
885 memset(ubi, 0, offsetof(struct ubi_scan_info, fm_buf));
887 ubi->read = info->read;
889 /* Precalculate the offsets */
890 ubi->vid_offset = info->vid_offset;
891 ubi->leb_start = info->leb_start;
892 ubi->leb_size = info->peb_size - ubi->leb_start;
893 ubi->peb_count = info->peb_count;
894 ubi->peb_offset = info->peb_offset;
896 fsize = info->peb_size * info->peb_count;
897 ubi->fsize_mb = fsize >> 20;
900 ubi->fm_size = ubi_calc_fm_size(ubi);
901 ubi->fm_enabled = fastmap;
903 for (i = 0; i < nrvols; i++) {
904 struct ubispl_load *lv = lvols + i;
906 generic_set_bit(lv->vol_id, ubi->toload);
911 for (i = 0; i < nrvols; i++) {
912 struct ubispl_load *lv = lvols + i;
914 ubi_msg("Loading VolId #%d", lv->vol_id);
915 res = ipl_load(ubi, lv->vol_id, lv->load_addr);