pci: sandbox: Probe PCI emulation devices when used
[oweals/u-boot.git] / drivers / mtd / ubispl / ubispl.c
1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2 /*
3  * Copyright (c) Thomas Gleixner <tglx@linutronix.de>
4  *
5  * The parts taken from the kernel implementation are:
6  *
7  * Copyright (c) International Business Machines Corp., 2006
8  */
9
10 #include <common.h>
11 #include <errno.h>
12 #include <ubispl.h>
13
14 #include <linux/crc32.h>
15
16 #include "ubispl.h"
17
18 /**
19  * ubi_calc_fm_size - calculates the fastmap size in bytes for an UBI device.
20  * @ubi: UBI device description object
21  */
22 static size_t ubi_calc_fm_size(struct ubi_scan_info *ubi)
23 {
24         size_t size;
25
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);
35 }
36
37 static int ubi_io_read(struct ubi_scan_info *ubi, void *buf, int pnum,
38                        unsigned long from, unsigned long len)
39 {
40         return ubi->read(pnum + ubi->peb_offset, from, len, buf);
41 }
42
43 static int ubi_io_is_bad(struct ubi_scan_info *ubi, int peb)
44 {
45         return peb >= ubi->peb_count || peb < 0;
46 }
47
48 #ifdef CONFIG_SPL_UBI_LOAD_BY_VOLNAME
49
50 /**
51  * ubi_dump_vtbl_record - dump a &struct ubi_vtbl_record object.
52  * @r: the object to dump
53  * @idx: volume table index
54  */
55 void ubi_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx)
56 {
57         int name_len = be16_to_cpu(r->name_len);
58
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);
67
68         if (r->name[0] == '\0') {
69                 ubi_dbg("\tname            NULL");
70                 return;
71         }
72
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]);
76         } else {
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],
79                         r->name[4]);
80         }
81         ubi_dbg("\tcrc             %#08x", be32_to_cpu(r->crc));
82 }
83
84 /* Empty volume table record */
85 static struct ubi_vtbl_record empty_vtbl_record;
86
87 /**
88  * vtbl_check - check if volume table is not corrupted and sensible.
89  * @ubi: UBI device description object
90  * @vtbl: volume table
91  *
92  * This function returns zero if @vtbl is all right, %1 if CRC is incorrect,
93  * and %-EINVAL if it contains inconsistent data.
94  */
95 static int vtbl_check(struct ubi_scan_info *ubi,
96                       struct ubi_vtbl_record *vtbl)
97 {
98         int i, n, reserved_pebs, alignment, data_pad, vol_type, name_len;
99         int upd_marker, err;
100         uint32_t crc;
101         const char *name;
102
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];
111
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);
117                         return 1;
118                 }
119
120                 if (reserved_pebs == 0) {
121                         if (memcmp(&vtbl[i], &empty_vtbl_record,
122                                    UBI_VTBL_RECORD_SIZE)) {
123                                 err = 2;
124                                 goto bad;
125                         }
126                         continue;
127                 }
128
129                 if (reserved_pebs < 0 || alignment < 0 || data_pad < 0 ||
130                     name_len < 0) {
131                         err = 3;
132                         goto bad;
133                 }
134
135                 if (alignment > ubi->leb_size || alignment == 0) {
136                         err = 4;
137                         goto bad;
138                 }
139
140                 n = alignment & (CONFIG_SPL_UBI_VID_OFFSET - 1);
141                 if (alignment != 1 && n) {
142                         err = 5;
143                         goto bad;
144                 }
145
146                 n = ubi->leb_size % alignment;
147                 if (data_pad != n) {
148                         ubi_err("bad data_pad, has to be %d", n);
149                         err = 6;
150                         goto bad;
151                 }
152
153                 if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) {
154                         err = 7;
155                         goto bad;
156                 }
157
158                 if (upd_marker != 0 && upd_marker != 1) {
159                         err = 8;
160                         goto bad;
161                 }
162
163                 if (name_len > UBI_VOL_NAME_MAX) {
164                         err = 10;
165                         goto bad;
166                 }
167
168                 if (name[0] == '\0') {
169                         err = 11;
170                         goto bad;
171                 }
172
173                 if (name_len != strnlen(name, name_len + 1)) {
174                         err = 12;
175                         goto bad;
176                 }
177
178                 ubi_dump_vtbl_record(&vtbl[i], i);
179         }
180
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);
186
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\"",
190                                         i, n, vtbl[i].name);
191                                 ubi_dump_vtbl_record(&vtbl[i], i);
192                                 ubi_dump_vtbl_record(&vtbl[n], n);
193                                 return -EINVAL;
194                         }
195                 }
196         }
197
198         return 0;
199
200 bad:
201         ubi_err("volume table check failed: record %d, error %d", i, err);
202         ubi_dump_vtbl_record(&vtbl[i], i);
203         return -EINVAL;
204 }
205
206 static int ubi_read_volume_table(struct ubi_scan_info *ubi, u32 pnum)
207 {
208         int err = -EINVAL;
209
210         empty_vtbl_record.crc = cpu_to_be32(0xf116c36b);
211
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");
216                 goto out;
217         }
218
219         if (!vtbl_check(ubi, ubi->vtbl)) {
220                 ubi->vtbl_valid = 1;
221                 err = 0;
222         }
223 out:
224         return err;
225 }
226
227 #endif /* CONFIG_SPL_UBI_LOAD_BY_VOLNAME */
228
229 static int ubi_io_read_vid_hdr(struct ubi_scan_info *ubi, int pnum,
230                                struct ubi_vid_hdr *vh, int unused)
231 {
232         u32 magic;
233         int res;
234
235         /* No point in rescanning a corrupt block */
236         if (test_bit(pnum, ubi->corrupt))
237                 return UBI_IO_BAD_HDR;
238         /*
239          * If the block has been scanned already, no need to rescan
240          */
241         if (test_and_set_bit(pnum, ubi->scanned))
242                 return 0;
243
244         res = ubi_io_read(ubi, vh, pnum, ubi->vid_offset, sizeof(*vh));
245
246         /*
247          * Bad block, unrecoverable ECC error, skip the block
248          */
249         if (res) {
250                 ubi_dbg("Skipping bad or unreadable block %d", pnum);
251                 vh->magic = 0;
252                 generic_set_bit(pnum, ubi->corrupt);
253                 return res;
254         }
255
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)
261                         return UBI_IO_FF;
262                 ubi_msg("Bad magic in block 0%d %08x", pnum, magic);
263                 return UBI_IO_BAD_HDR;
264         }
265
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;
272         }
273
274         ubi_dbg("RV: pnum: %i sqnum %llu", pnum, be64_to_cpu(vh->sqnum));
275
276         return 0;
277 }
278
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)
282 {
283         int res;
284
285         if (ubi_io_is_bad(ubi, fm_pnum))
286                 return -EINVAL;
287
288         res = ubi_io_read_vid_hdr(ubi, fm_pnum, vh, 0);
289         if (!res) {
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)
294                         return 0;
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));
299         }
300         return res;
301 }
302
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,
306                               u32 pnum, u32 lnum)
307 {
308         struct ubi_vol_info *vi = ubi->volinfo + vol_id;
309         u32 *ltp;
310
311         /*
312          * If the volume is larger than expected, yell and give up :(
313          */
314         if (lnum >= UBI_MAX_VOL_LEBS) {
315                 ubi_warn("Vol: %u LEB %d > %d", vol_id, lnum, UBI_MAX_VOL_LEBS);
316                 return -EINVAL;
317         }
318
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));
322
323         /* Points to the translation entry */
324         ltp = vi->lebs_to_pebs + lnum;
325
326         /* If the block is already assigned, check sqnum */
327         if (__test_and_set_bit(lnum, vi->found)) {
328                 u32 cur_pnum = *ltp;
329                 struct ubi_vid_hdr *cur = ubi->blockinfo + cur_pnum;
330
331                 /*
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
335                  * updated.
336                  */
337                 if (!test_bit(cur_pnum, ubi->scanned)) {
338                         /*
339                          * If the scan fails, we use the valid block
340                          */
341                         if (ubi_rescan_fm_vid_hdr(ubi, cur, cur_pnum, vol_id,
342                                                   lnum)) {
343                                 *ltp = pnum;
344                                 return 0;
345                         }
346                 }
347
348                 /*
349                  * Should not happen ....
350                  */
351                 if (test_bit(cur_pnum, ubi->corrupt)) {
352                         *ltp = pnum;
353                         return 0;
354                 }
355
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));
359
360                 /*
361                  * Compare sqnum and take the newer one
362                  */
363                 if (be64_to_cpu(cur->sqnum) < be64_to_cpu(vh->sqnum))
364                         *ltp = pnum;
365         } else {
366                 *ltp = pnum;
367                 if (lnum > vi->last_block)
368                         vi->last_block = lnum;
369         }
370
371         return 0;
372 }
373
374 static int ubi_scan_vid_hdr(struct ubi_scan_info *ubi, struct ubi_vid_hdr *vh,
375                             u32 pnum)
376 {
377         u32 vol_id, lnum;
378         int res;
379
380         if (ubi_io_is_bad(ubi, pnum))
381                 return -EINVAL;
382
383         res = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
384         if (res)
385                 return res;
386
387         /* Get volume id */
388         vol_id = be32_to_cpu(vh->vol_id);
389
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;
393
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);
398                 return res;
399         }
400 #endif
401
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)
404                 return 0;
405
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))
409                 return 0;
410 #endif
411         lnum = be32_to_cpu(vh->lnum);
412         return ubi_add_peb_to_vol(ubi, vh, vol_id, pnum, lnum);
413 }
414
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)
417 {
418         struct ubi_vid_hdr *vh;
419
420         if (ubi_io_is_bad(ubi, pnum))
421                 return -EINVAL;
422
423         ubi->fastmap_pebs++;
424
425 #ifndef CONFIG_SPL_UBI_LOAD_BY_VOLNAME
426         if (vol_id >= UBI_SPL_VOL_IDS || vol_type != UBI_STATIC_VOLUME)
427                 return 0;
428
429         /* We are only interested in the volumes to load */
430         if (!test_bit(vol_id, ubi->toload))
431                 return 0;
432 #endif
433         vh = ubi->blockinfo + pnum;
434
435         return ubi_scan_vid_hdr(ubi, vh, pnum);
436 }
437
438 static int scan_pool(struct ubi_scan_info *ubi, __be32 *pebs, int pool_size)
439 {
440         struct ubi_vid_hdr *vh;
441         u32 pnum;
442         int i;
443
444         ubi_dbg("Scanning pool size: %d", pool_size);
445
446         for (i = 0; i < pool_size; i++) {
447                 pnum = be32_to_cpu(pebs[i]);
448
449                 if (ubi_io_is_bad(ubi, pnum)) {
450                         ubi_err("FM: Bad PEB in fastmap pool! %u", pnum);
451                         return UBI_BAD_FASTMAP;
452                 }
453
454                 vh = ubi->blockinfo + pnum;
455                 /*
456                  * We allow the scan to fail here. The loader will notice
457                  * and look for a replacement.
458                  */
459                 ubi_scan_vid_hdr(ubi, vh, pnum);
460         }
461         return 0;
462 }
463
464 /*
465  * Fastmap code is stolen from Linux kernel and this stub structure is used
466  * to make it happy.
467  */
468 struct ubi_attach_info {
469         int i;
470 };
471
472 static int ubi_attach_fastmap(struct ubi_scan_info *ubi,
473                               struct ubi_attach_info *ai,
474                               struct ubi_fastmap_layout *fm)
475 {
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;
484
485         memset(ubi->fm_used, 0, sizeof(ubi->fm_used));
486
487         fm_pos += sizeof(struct ubi_fm_sb);
488         if (fm_pos >= fm_size)
489                 goto fail_bad;
490
491         fmhdr = (struct ubi_fm_hdr *)(fm_raw + fm_pos);
492         fm_pos += sizeof(*fmhdr);
493         if (fm_pos >= fm_size)
494                 goto fail_bad;
495
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);
499                 goto fail_bad;
500         }
501
502         fmpl1 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
503         fm_pos += sizeof(*fmpl1);
504         if (fm_pos >= fm_size)
505                 goto fail_bad;
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);
509                 goto fail_bad;
510         }
511
512         fmpl2 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
513         fm_pos += sizeof(*fmpl2);
514         if (fm_pos >= fm_size)
515                 goto fail_bad;
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);
519                 goto fail_bad;
520         }
521
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);
526
527         if (pool_size > UBI_FM_MAX_POOL_SIZE || pool_size < 0) {
528                 ubi_err("bad pool size: %i", pool_size);
529                 goto fail_bad;
530         }
531
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);
534                 goto fail_bad;
535         }
536
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);
540                 goto fail_bad;
541         }
542
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);
546                 goto fail_bad;
547         }
548
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)
554                         goto fail_bad;
555         }
556
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)
562                         goto fail_bad;
563
564                 generic_set_bit(be32_to_cpu(fmec->pnum), ubi->fm_used);
565         }
566
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)
572                         goto fail_bad;
573         }
574
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)
580                         goto fail_bad;
581         }
582
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;
586
587                 fmvhdr = (struct ubi_fm_volhdr *)(fm_raw + fm_pos);
588                 fm_pos += sizeof(*fmvhdr);
589                 if (fm_pos >= fm_size)
590                         goto fail_bad;
591
592                 if (be32_to_cpu(fmvhdr->magic) != UBI_FM_VHDR_MAGIC) {
593                         ubi_err("bad fastmap vol header magic: 0x%x, " \
594                                 "expected: 0x%x",
595                                 be32_to_cpu(fmvhdr->magic), UBI_FM_VHDR_MAGIC);
596                         goto fail_bad;
597                 }
598
599                 vol_id = be32_to_cpu(fmvhdr->vol_id);
600                 vol_type = fmvhdr->vol_type;
601                 used = be32_to_cpu(fmvhdr->used_ebs);
602
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)
607                         goto fail_bad;
608
609                 if (be32_to_cpu(fm_eba->magic) != UBI_FM_EBA_MAGIC) {
610                         ubi_err("bad fastmap EBA header magic: 0x%x, " \
611                                 "expected: 0x%x",
612                                 be32_to_cpu(fm_eba->magic), UBI_FM_EBA_MAGIC);
613                         goto fail_bad;
614                 }
615
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]);
620
621                         if ((int)be32_to_cpu(fm_eba->pnum[j]) < 0)
622                                 continue;
623
624                         if (!__test_and_clear_bit(pnum, ubi->fm_used))
625                                 continue;
626
627                         /*
628                          * We only handle static volumes so used_ebs
629                          * needs to be handed in. And we do not assign
630                          * the reserved blocks
631                          */
632                         if (j >= used)
633                                 continue;
634
635                         ret = assign_aeb_to_av(ubi, pnum, j, vol_id,
636                                                vol_type, used);
637                         if (!ret)
638                                 continue;
639
640                         /*
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.
646                          */
647                         if (ret != UBI_IO_FF && j != used - 1)
648                                 goto fail_bad;
649                         ubi_dbg("FA: Vol: %u Ignoring empty LEB %d of %d",
650                                 vol_id, j, used);
651                 }
652         }
653
654         ret = scan_pool(ubi, fmpl1->pebs, pool_size);
655         if (ret)
656                 goto fail;
657
658         ret = scan_pool(ubi, fmpl2->pebs, wl_pool_size);
659         if (ret)
660                 goto fail;
661
662 #ifdef CHECKME
663         /*
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.
668          */
669         if (WARN_ON(count_fastmap_pebs(ai) != ubi->peb_count -
670                     ai->bad_peb_count - fm->used_blocks))
671                 goto fail_bad;
672 #endif
673
674         return 0;
675
676 fail_bad:
677         ret = UBI_BAD_FASTMAP;
678 fail:
679         return ret;
680 }
681
682 static int ubi_scan_fastmap(struct ubi_scan_info *ubi,
683                             struct ubi_attach_info *ai,
684                             int fm_anchor)
685 {
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;
690         size_t fm_size;
691         __be32 crc, tmp_crc;
692         unsigned long long sqnum = 0;
693
694         fmsb = &ubi->fm_sb;
695         fm = &ubi->fm_layout;
696
697         ret = ubi_io_read(ubi, fmsb, fm_anchor, ubi->leb_start, sizeof(*fmsb));
698         if (ret && ret != UBI_IO_BITFLIPS)
699                 goto free_fm_sb;
700         else if (ret == UBI_IO_BITFLIPS)
701                 fm->to_be_tortured[0] = 1;
702
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;
707                 goto free_fm_sb;
708         }
709
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;
714                 goto free_fm_sb;
715         }
716
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;
721                 goto free_fm_sb;
722         }
723
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,
727                         ubi->fm_size);
728                 ret = UBI_BAD_FASTMAP;
729                 goto free_fm_sb;
730         }
731
732         vh = &ubi->fm_vh;
733
734         for (i = 0; i < used_blocks; i++) {
735                 pnum = be32_to_cpu(fmsb->block_loc[i]);
736
737                 if (ubi_io_is_bad(ubi, pnum)) {
738                         ret = UBI_BAD_FASTMAP;
739                         goto free_hdr;
740                 }
741
742 #ifdef LATER
743                 int image_seq;
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)",
747                                 i, pnum);
748                         if (ret > 0)
749                                 ret = UBI_BAD_FASTMAP;
750                         goto free_hdr;
751                 } else if (ret == UBI_IO_BITFLIPS)
752                         fm->to_be_tortured[i] = 1;
753
754                 image_seq = be32_to_cpu(ech->image_seq);
755                 if (!ubi->image_seq)
756                         ubi->image_seq = image_seq;
757                 /*
758                  * Older UBI implementations have image_seq set to zero, so
759                  * we shouldn't fail if image_seq == 0.
760                  */
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;
765                         goto free_hdr;
766                 }
767 #endif
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)",
771                                 i, pnum);
772                         goto free_hdr;
773                 }
774
775                 /*
776                  * Mainline code rescans the anchor header. We've done
777                  * that already so we merily copy it over.
778                  */
779                 if (pnum == fm_anchor)
780                         memcpy(vh, ubi->blockinfo + pnum, sizeof(*fm));
781
782                 if (i == 0) {
783                         if (be32_to_cpu(vh->vol_id) != UBI_FM_SB_VOLUME_ID) {
784                                 ubi_err("bad fastmap anchor vol_id: 0x%x," \
785                                         " expected: 0x%x",
786                                         be32_to_cpu(vh->vol_id),
787                                         UBI_FM_SB_VOLUME_ID);
788                                 ret = UBI_BAD_FASTMAP;
789                                 goto free_hdr;
790                         }
791                 } else {
792                         if (be32_to_cpu(vh->vol_id) != UBI_FM_DATA_VOLUME_ID) {
793                                 ubi_err("bad fastmap data vol_id: 0x%x," \
794                                         " expected: 0x%x",
795                                         be32_to_cpu(vh->vol_id),
796                                         UBI_FM_DATA_VOLUME_ID);
797                                 ret = UBI_BAD_FASTMAP;
798                                 goto free_hdr;
799                         }
800                 }
801
802                 if (sqnum < be64_to_cpu(vh->sqnum))
803                         sqnum = be64_to_cpu(vh->sqnum);
804
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);
810                         goto free_hdr;
811                 }
812         }
813
814         fmsb2 = (struct ubi_fm_sb *)(ubi->fm_buf);
815         tmp_crc = be32_to_cpu(fmsb2->data_crc);
816         fmsb2->data_crc = 0;
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;
822                 goto free_hdr;
823         }
824
825         fmsb2->sqnum = sqnum;
826
827         fm->used_blocks = used_blocks;
828
829         ret = ubi_attach_fastmap(ubi, ai, fm);
830         if (ret) {
831                 if (ret > 0)
832                         ret = UBI_BAD_FASTMAP;
833                 goto free_hdr;
834         }
835
836         ubi->fm = fm;
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);
843
844 out:
845         if (ret)
846                 ubi_err("Attach by fastmap failed, doing a full scan!");
847         return ret;
848
849 free_hdr:
850 free_fm_sb:
851         goto out;
852 }
853
854 /*
855  * Scan the flash and attempt to attach via fastmap
856  */
857 static void ipl_scan(struct ubi_scan_info *ubi)
858 {
859         unsigned int pnum;
860         int res;
861
862         /*
863          * Scan first for the fastmap super block
864          */
865         for (pnum = 0; pnum < UBI_FM_MAX_START; pnum++) {
866                 res = ubi_scan_vid_hdr(ubi, ubi->blockinfo + pnum, pnum);
867                 /*
868                  * We ignore errors here as we are meriliy scanning
869                  * the headers.
870                  */
871                 if (res != UBI_FASTMAP_ANCHOR)
872                         continue;
873
874                 /*
875                  * If fastmap is disabled, continue scanning. This
876                  * might happen because the previous attempt failed or
877                  * the caller disabled it right away.
878                  */
879                 if (!ubi->fm_enabled)
880                         continue;
881
882                 /*
883                  * Try to attach the fastmap, if that fails continue
884                  * scanning.
885                  */
886                 if (!ubi_scan_fastmap(ubi, NULL, pnum))
887                         return;
888                 /*
889                  * Fastmap failed. Clear everything we have and start
890                  * over. We are paranoid and do not trust anything.
891                  */
892                 memset(ubi->volinfo, 0, sizeof(ubi->volinfo));
893                 pnum = 0;
894                 break;
895         }
896
897         /*
898          * Continue scanning, ignore errors, we might find what we are
899          * looking for,
900          */
901         for (; pnum < ubi->peb_count; pnum++)
902                 ubi_scan_vid_hdr(ubi, ubi->blockinfo + pnum, pnum);
903 }
904
905 /*
906  * Load a logical block of a volume into memory
907  */
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,
910                           u32 last)
911 {
912         struct ubi_vid_hdr *vh, *vrepl;
913         u32 pnum, crc, dlen;
914
915 retry:
916         /*
917          * If this is a fastmap run, we try to rescan full, otherwise
918          * we simply give up.
919          */
920         if (!test_bit(lnum, vi->found)) {
921                 ubi_warn("LEB %d of %d is missing", lnum, last);
922                 return -EINVAL;
923         }
924
925         pnum = vi->lebs_to_pebs[lnum];
926
927         ubi_dbg("Load vol %u LEB %u PEB %u", vol_id, lnum, pnum);
928
929         if (ubi_io_is_bad(ubi, pnum)) {
930                 ubi_warn("Corrupted mapping block %d PB %d\n", lnum, pnum);
931                 return -EINVAL;
932         }
933
934         if (test_bit(pnum, ubi->corrupt))
935                 goto find_other;
936
937         /*
938          * Lets try to read that block
939          */
940         vh = ubi->blockinfo + pnum;
941
942         if (!test_bit(pnum, ubi->scanned)) {
943                 ubi_warn("Vol: %u LEB %u PEB %u not yet scanned", vol_id,
944                          lnum, pnum);
945                 if (ubi_rescan_fm_vid_hdr(ubi, vh, pnum, vol_id, lnum))
946                         goto find_other;
947         }
948
949         /*
950          * Check, if the total number of blocks is correct
951          */
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);
957                 goto find_other;
958         }
959
960         /*
961          * Get the data length of this block.
962          */
963         dlen = be32_to_cpu(vh->data_size);
964
965         /*
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.
969          */
970         ubi_io_read(ubi, laddr, pnum, ubi->leb_start, dlen);
971
972         /* Calculate CRC over the data */
973         crc = crc32(UBI_CRC32_INIT, laddr, dlen);
974
975         if (crc != be32_to_cpu(vh->data_crc)) {
976                 ubi_warn("Vol: %u LEB %u PEB %u data CRC failure", vol_id,
977                          lnum, pnum);
978                 generic_set_bit(pnum, ubi->corrupt);
979                 goto find_other;
980         }
981
982         /* We are good. Return the data length we read */
983         return dlen;
984
985 find_other:
986         ubi_dbg("Find replacement for LEB %u PEB %u", lnum, pnum);
987         generic_clear_bit(lnum, vi->found);
988         vrepl = NULL;
989
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);
994
995                 if (test_bit(pnum, ubi->corrupt))
996                         continue;
997
998                 if (t_vol_id != vol_id || t_lnum != lnum)
999                         continue;
1000
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))
1005                                 continue;
1006                 }
1007
1008                 /*
1009                  * We found one. If its the first, assign it otherwise
1010                  * compare the sqnum
1011                  */
1012                 generic_set_bit(lnum, vi->found);
1013
1014                 if (!vrepl) {
1015                         vrepl = tmp;
1016                         continue;
1017                 }
1018
1019                 if (be64_to_cpu(vrepl->sqnum) < be64_to_cpu(tmp->sqnum))
1020                         vrepl = tmp;
1021         }
1022
1023         if (vrepl) {
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);
1028                 vh = vrepl;
1029         }
1030         goto retry;
1031 }
1032
1033 /*
1034  * Load a volume into RAM
1035  */
1036 static int ipl_load(struct ubi_scan_info *ubi, const u32 vol_id, uint8_t *laddr)
1037 {
1038         struct ubi_vol_info *vi;
1039         u32 lnum, last, len;
1040
1041         if (vol_id >= UBI_SPL_VOL_IDS)
1042                 return -EINVAL;
1043
1044         len = 0;
1045         vi = ubi->volinfo + vol_id;
1046         last = vi->last_block + 1;
1047
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);
1051
1052                 if (res < 0) {
1053                         ubi_warn("Failed to load volume %u", vol_id);
1054                         return res;
1055                 }
1056                 /* res is the data length of the read block */
1057                 laddr += res;
1058                 len += res;
1059         }
1060         return len;
1061 }
1062
1063 int ubispl_load_volumes(struct ubispl_info *info, struct ubispl_load *lvols,
1064                         int nrvols)
1065 {
1066         struct ubi_scan_info *ubi = info->ubi;
1067         int res, i, fastmap = info->fastmap;
1068         u32 fsize;
1069
1070 retry:
1071         /*
1072          * We do a partial initializiation of @ubi. Cleaning fm_buf is
1073          * not necessary.
1074          */
1075         memset(ubi, 0, offsetof(struct ubi_scan_info, fm_buf));
1076
1077         ubi->read = info->read;
1078
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;
1085
1086 #ifdef CONFIG_SPL_UBI_LOAD_BY_VOLNAME
1087         ubi->vtbl_valid = 0;
1088 #endif
1089
1090         fsize = info->peb_size * info->peb_count;
1091         ubi->fsize_mb = fsize >> 20;
1092
1093         /* Fastmap init */
1094         ubi->fm_size = ubi_calc_fm_size(ubi);
1095         ubi->fm_enabled = fastmap;
1096
1097         for (i = 0; i < nrvols; i++) {
1098                 struct ubispl_load *lv = lvols + i;
1099
1100                 generic_set_bit(lv->vol_id, ubi->toload);
1101         }
1102
1103         ipl_scan(ubi);
1104
1105         for (i = 0; i < nrvols; i++) {
1106                 struct ubispl_load *lv = lvols + i;
1107
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);
1112
1113                                 if (strncmp(lv->name,
1114                                             ubi->vtbl[j].name,
1115                                             len) == 0) {
1116                                         lv->vol_id = j;
1117                                         break;
1118                                 }
1119                         }
1120                 }
1121                 ubi_msg("Loading VolName %s (VolId #%d)", lv->name, lv->vol_id);
1122 #else
1123                 ubi_msg("Loading VolId #%d", lv->vol_id);
1124 #endif
1125                 res = ipl_load(ubi, lv->vol_id, lv->load_addr);
1126                 if (res < 0) {
1127                         if (fastmap) {
1128                                 fastmap = 0;
1129                                 goto retry;
1130                         }
1131                         ubi_warn("Failed");
1132                         return res;
1133                 }
1134         }
1135         return 0;
1136 }