blob: 90a7c4c6f9e08552d68a54afbe6d423a4a345de0 [file] [log] [blame]
Tom Rini8b0c8a12018-05-06 18:27:01 -04001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
Thomas Gleixner820d24d2016-07-12 20:28:12 +02002/*
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
Thomas Gleixner820d24d2016-07-12 20:28:12 +02008 */
9
Thomas Gleixner820d24d2016-07-12 20:28:12 +020010#include <errno.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060011#include <linux/bug.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070012#include <u-boot/crc.h>
Thomas Gleixner820d24d2016-07-12 20:28:12 +020013#include <ubispl.h>
14
Simon Glass4dcacfc2020-05-10 11:40:13 -060015#include <linux/bitops.h>
Thomas Gleixner820d24d2016-07-12 20:28:12 +020016#include <linux/crc32.h>
17
18#include "ubispl.h"
19
20/**
21 * ubi_calc_fm_size - calculates the fastmap size in bytes for an UBI device.
22 * @ubi: UBI device description object
23 */
24static size_t ubi_calc_fm_size(struct ubi_scan_info *ubi)
25{
26 size_t size;
27
28 size = sizeof(struct ubi_fm_sb) +
29 sizeof(struct ubi_fm_hdr) +
30 sizeof(struct ubi_fm_scan_pool) +
31 sizeof(struct ubi_fm_scan_pool) +
32 (ubi->peb_count * sizeof(struct ubi_fm_ec)) +
33 (sizeof(struct ubi_fm_eba) +
34 (ubi->peb_count * sizeof(__be32))) +
35 sizeof(struct ubi_fm_volhdr) * UBI_MAX_VOLUMES;
36 return roundup(size, ubi->leb_size);
37}
38
39static int ubi_io_read(struct ubi_scan_info *ubi, void *buf, int pnum,
40 unsigned long from, unsigned long len)
41{
42 return ubi->read(pnum + ubi->peb_offset, from, len, buf);
43}
44
45static int ubi_io_is_bad(struct ubi_scan_info *ubi, int peb)
46{
47 return peb >= ubi->peb_count || peb < 0;
48}
49
Hamish Guthrie3ce740d2019-05-15 15:15:59 +020050#ifdef CONFIG_SPL_UBI_LOAD_BY_VOLNAME
51
52/**
53 * ubi_dump_vtbl_record - dump a &struct ubi_vtbl_record object.
54 * @r: the object to dump
55 * @idx: volume table index
56 */
57void ubi_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx)
58{
59 int name_len = be16_to_cpu(r->name_len);
60
61 ubi_dbg("Volume table record %d dump: size: %d",
62 idx, sizeof(struct ubi_vtbl_record));
63 ubi_dbg("\treserved_pebs %d", be32_to_cpu(r->reserved_pebs));
64 ubi_dbg("\talignment %d", be32_to_cpu(r->alignment));
65 ubi_dbg("\tdata_pad %d", be32_to_cpu(r->data_pad));
66 ubi_dbg("\tvol_type %d", (int)r->vol_type);
67 ubi_dbg("\tupd_marker %d", (int)r->upd_marker);
68 ubi_dbg("\tname_len %d", name_len);
69
70 if (r->name[0] == '\0') {
71 ubi_dbg("\tname NULL");
72 return;
73 }
74
75 if (name_len <= UBI_VOL_NAME_MAX &&
76 strnlen(&r->name[0], name_len + 1) == name_len) {
77 ubi_dbg("\tname %s", &r->name[0]);
78 } else {
79 ubi_dbg("\t1st 5 characters of name: %c%c%c%c%c",
80 r->name[0], r->name[1], r->name[2], r->name[3],
81 r->name[4]);
82 }
83 ubi_dbg("\tcrc %#08x", be32_to_cpu(r->crc));
84}
85
86/* Empty volume table record */
87static struct ubi_vtbl_record empty_vtbl_record;
88
89/**
90 * vtbl_check - check if volume table is not corrupted and sensible.
91 * @ubi: UBI device description object
92 * @vtbl: volume table
93 *
94 * This function returns zero if @vtbl is all right, %1 if CRC is incorrect,
95 * and %-EINVAL if it contains inconsistent data.
96 */
97static int vtbl_check(struct ubi_scan_info *ubi,
98 struct ubi_vtbl_record *vtbl)
99{
100 int i, n, reserved_pebs, alignment, data_pad, vol_type, name_len;
101 int upd_marker, err;
102 uint32_t crc;
103 const char *name;
104
105 for (i = 0; i < UBI_SPL_VOL_IDS; i++) {
106 reserved_pebs = be32_to_cpu(vtbl[i].reserved_pebs);
107 alignment = be32_to_cpu(vtbl[i].alignment);
108 data_pad = be32_to_cpu(vtbl[i].data_pad);
109 upd_marker = vtbl[i].upd_marker;
110 vol_type = vtbl[i].vol_type;
111 name_len = be16_to_cpu(vtbl[i].name_len);
112 name = &vtbl[i].name[0];
113
114 crc = crc32(UBI_CRC32_INIT, &vtbl[i], UBI_VTBL_RECORD_SIZE_CRC);
115 if (be32_to_cpu(vtbl[i].crc) != crc) {
116 ubi_err("bad CRC at record %u: %#08x, not %#08x",
117 i, crc, be32_to_cpu(vtbl[i].crc));
118 ubi_dump_vtbl_record(&vtbl[i], i);
119 return 1;
120 }
121
122 if (reserved_pebs == 0) {
123 if (memcmp(&vtbl[i], &empty_vtbl_record,
124 UBI_VTBL_RECORD_SIZE)) {
125 err = 2;
126 goto bad;
127 }
128 continue;
129 }
130
131 if (reserved_pebs < 0 || alignment < 0 || data_pad < 0 ||
132 name_len < 0) {
133 err = 3;
134 goto bad;
135 }
136
137 if (alignment > ubi->leb_size || alignment == 0) {
138 err = 4;
139 goto bad;
140 }
141
142 n = alignment & (CONFIG_SPL_UBI_VID_OFFSET - 1);
143 if (alignment != 1 && n) {
144 err = 5;
145 goto bad;
146 }
147
148 n = ubi->leb_size % alignment;
149 if (data_pad != n) {
150 ubi_err("bad data_pad, has to be %d", n);
151 err = 6;
152 goto bad;
153 }
154
155 if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) {
156 err = 7;
157 goto bad;
158 }
159
160 if (upd_marker != 0 && upd_marker != 1) {
161 err = 8;
162 goto bad;
163 }
164
165 if (name_len > UBI_VOL_NAME_MAX) {
166 err = 10;
167 goto bad;
168 }
169
170 if (name[0] == '\0') {
171 err = 11;
172 goto bad;
173 }
174
175 if (name_len != strnlen(name, name_len + 1)) {
176 err = 12;
177 goto bad;
178 }
179
180 ubi_dump_vtbl_record(&vtbl[i], i);
181 }
182
183 /* Checks that all names are unique */
184 for (i = 0; i < UBI_SPL_VOL_IDS - 1; i++) {
185 for (n = i + 1; n < UBI_SPL_VOL_IDS; n++) {
186 int len1 = be16_to_cpu(vtbl[i].name_len);
187 int len2 = be16_to_cpu(vtbl[n].name_len);
188
189 if (len1 > 0 && len1 == len2 &&
190 !strncmp(vtbl[i].name, vtbl[n].name, len1)) {
191 ubi_err("volumes %d and %d have the same name \"%s\"",
192 i, n, vtbl[i].name);
193 ubi_dump_vtbl_record(&vtbl[i], i);
194 ubi_dump_vtbl_record(&vtbl[n], n);
195 return -EINVAL;
196 }
197 }
198 }
199
200 return 0;
201
202bad:
203 ubi_err("volume table check failed: record %d, error %d", i, err);
204 ubi_dump_vtbl_record(&vtbl[i], i);
205 return -EINVAL;
206}
207
208static int ubi_read_volume_table(struct ubi_scan_info *ubi, u32 pnum)
209{
210 int err = -EINVAL;
211
212 empty_vtbl_record.crc = cpu_to_be32(0xf116c36b);
213
214 err = ubi_io_read(ubi, &ubi->vtbl, pnum, ubi->leb_start,
215 sizeof(struct ubi_vtbl_record) * UBI_SPL_VOL_IDS);
216 if (err && err != UBI_IO_BITFLIPS) {
217 ubi_err("unable to read volume table");
218 goto out;
219 }
220
221 if (!vtbl_check(ubi, ubi->vtbl)) {
222 ubi->vtbl_valid = 1;
223 err = 0;
224 }
225out:
226 return err;
227}
228
229#endif /* CONFIG_SPL_UBI_LOAD_BY_VOLNAME */
230
Thomas Gleixner820d24d2016-07-12 20:28:12 +0200231static int ubi_io_read_vid_hdr(struct ubi_scan_info *ubi, int pnum,
232 struct ubi_vid_hdr *vh, int unused)
233{
234 u32 magic;
235 int res;
236
237 /* No point in rescanning a corrupt block */
238 if (test_bit(pnum, ubi->corrupt))
239 return UBI_IO_BAD_HDR;
240 /*
241 * If the block has been scanned already, no need to rescan
242 */
243 if (test_and_set_bit(pnum, ubi->scanned))
244 return 0;
245
246 res = ubi_io_read(ubi, vh, pnum, ubi->vid_offset, sizeof(*vh));
247
248 /*
249 * Bad block, unrecoverable ECC error, skip the block
250 */
251 if (res) {
252 ubi_dbg("Skipping bad or unreadable block %d", pnum);
253 vh->magic = 0;
254 generic_set_bit(pnum, ubi->corrupt);
255 return res;
256 }
257
258 /* Magic number available ? */
259 magic = be32_to_cpu(vh->magic);
260 if (magic != UBI_VID_HDR_MAGIC) {
261 generic_set_bit(pnum, ubi->corrupt);
262 if (magic == 0xffffffff)
263 return UBI_IO_FF;
264 ubi_msg("Bad magic in block 0%d %08x", pnum, magic);
265 return UBI_IO_BAD_HDR;
266 }
267
268 /* Header CRC correct ? */
269 if (crc32(UBI_CRC32_INIT, vh, UBI_VID_HDR_SIZE_CRC) !=
270 be32_to_cpu(vh->hdr_crc)) {
271 ubi_msg("Bad CRC in block 0%d", pnum);
272 generic_set_bit(pnum, ubi->corrupt);
273 return UBI_IO_BAD_HDR;
274 }
275
276 ubi_dbg("RV: pnum: %i sqnum %llu", pnum, be64_to_cpu(vh->sqnum));
277
278 return 0;
279}
280
281static int ubi_rescan_fm_vid_hdr(struct ubi_scan_info *ubi,
282 struct ubi_vid_hdr *vh,
283 u32 fm_pnum, u32 fm_vol_id, u32 fm_lnum)
284{
285 int res;
286
287 if (ubi_io_is_bad(ubi, fm_pnum))
288 return -EINVAL;
289
290 res = ubi_io_read_vid_hdr(ubi, fm_pnum, vh, 0);
291 if (!res) {
292 /* Check volume id, volume type and lnum */
293 if (be32_to_cpu(vh->vol_id) == fm_vol_id &&
294 vh->vol_type == UBI_VID_STATIC &&
295 be32_to_cpu(vh->lnum) == fm_lnum)
296 return 0;
297 ubi_dbg("RS: PEB %u vol: %u : %u typ %u lnum %u %u",
298 fm_pnum, fm_vol_id, vh->vol_type,
299 be32_to_cpu(vh->vol_id),
300 fm_lnum, be32_to_cpu(vh->lnum));
301 }
302 return res;
303}
304
305/* Insert the logic block into the volume info */
306static int ubi_add_peb_to_vol(struct ubi_scan_info *ubi,
307 struct ubi_vid_hdr *vh, u32 vol_id,
308 u32 pnum, u32 lnum)
309{
310 struct ubi_vol_info *vi = ubi->volinfo + vol_id;
311 u32 *ltp;
312
313 /*
314 * If the volume is larger than expected, yell and give up :(
315 */
316 if (lnum >= UBI_MAX_VOL_LEBS) {
317 ubi_warn("Vol: %u LEB %d > %d", vol_id, lnum, UBI_MAX_VOL_LEBS);
318 return -EINVAL;
319 }
320
321 ubi_dbg("SC: Add PEB %u to Vol %u as LEB %u fnd %d sc %d",
322 pnum, vol_id, lnum, !!test_bit(lnum, vi->found),
323 !!test_bit(pnum, ubi->scanned));
324
325 /* Points to the translation entry */
326 ltp = vi->lebs_to_pebs + lnum;
327
328 /* If the block is already assigned, check sqnum */
329 if (__test_and_set_bit(lnum, vi->found)) {
330 u32 cur_pnum = *ltp;
331 struct ubi_vid_hdr *cur = ubi->blockinfo + cur_pnum;
332
333 /*
334 * If the current block hase not yet been scanned, we
335 * need to do that. The other block might be stale or
336 * the current block corrupted and the FM not yet
337 * updated.
338 */
339 if (!test_bit(cur_pnum, ubi->scanned)) {
340 /*
341 * If the scan fails, we use the valid block
342 */
343 if (ubi_rescan_fm_vid_hdr(ubi, cur, cur_pnum, vol_id,
344 lnum)) {
345 *ltp = pnum;
346 return 0;
347 }
348 }
349
350 /*
351 * Should not happen ....
352 */
353 if (test_bit(cur_pnum, ubi->corrupt)) {
354 *ltp = pnum;
355 return 0;
356 }
357
358 ubi_dbg("Vol %u LEB %u PEB %u->sqnum %llu NPEB %u->sqnum %llu",
359 vol_id, lnum, cur_pnum, be64_to_cpu(cur->sqnum), pnum,
360 be64_to_cpu(vh->sqnum));
361
362 /*
363 * Compare sqnum and take the newer one
364 */
365 if (be64_to_cpu(cur->sqnum) < be64_to_cpu(vh->sqnum))
366 *ltp = pnum;
367 } else {
368 *ltp = pnum;
369 if (lnum > vi->last_block)
370 vi->last_block = lnum;
371 }
372
373 return 0;
374}
375
376static int ubi_scan_vid_hdr(struct ubi_scan_info *ubi, struct ubi_vid_hdr *vh,
377 u32 pnum)
378{
379 u32 vol_id, lnum;
380 int res;
381
382 if (ubi_io_is_bad(ubi, pnum))
383 return -EINVAL;
384
385 res = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
386 if (res)
387 return res;
388
389 /* Get volume id */
390 vol_id = be32_to_cpu(vh->vol_id);
391
392 /* If this is the fastmap anchor, return right away */
393 if (vol_id == UBI_FM_SB_VOLUME_ID)
394 return ubi->fm_enabled ? UBI_FASTMAP_ANCHOR : 0;
395
Hamish Guthrie3ce740d2019-05-15 15:15:59 +0200396#ifdef CONFIG_SPL_UBI_LOAD_BY_VOLNAME
397 /* If this is a UBI volume table, read it and return */
398 if (vol_id == UBI_LAYOUT_VOLUME_ID && !ubi->vtbl_valid) {
399 res = ubi_read_volume_table(ubi, pnum);
400 return res;
401 }
402#endif
403
Thomas Gleixner820d24d2016-07-12 20:28:12 +0200404 /* We only care about static volumes with an id < UBI_SPL_VOL_IDS */
405 if (vol_id >= UBI_SPL_VOL_IDS || vh->vol_type != UBI_VID_STATIC)
406 return 0;
407
Hamish Guthrie3ce740d2019-05-15 15:15:59 +0200408#ifndef CONFIG_SPL_UBI_LOAD_BY_VOLNAME
Thomas Gleixner820d24d2016-07-12 20:28:12 +0200409 /* We are only interested in the volumes to load */
410 if (!test_bit(vol_id, ubi->toload))
411 return 0;
Hamish Guthrie3ce740d2019-05-15 15:15:59 +0200412#endif
Thomas Gleixner820d24d2016-07-12 20:28:12 +0200413 lnum = be32_to_cpu(vh->lnum);
414 return ubi_add_peb_to_vol(ubi, vh, vol_id, pnum, lnum);
415}
416
417static int assign_aeb_to_av(struct ubi_scan_info *ubi, u32 pnum, u32 lnum,
418 u32 vol_id, u32 vol_type, u32 used)
419{
420 struct ubi_vid_hdr *vh;
421
422 if (ubi_io_is_bad(ubi, pnum))
423 return -EINVAL;
424
425 ubi->fastmap_pebs++;
426
Hamish Guthrie3ce740d2019-05-15 15:15:59 +0200427#ifndef CONFIG_SPL_UBI_LOAD_BY_VOLNAME
Thomas Gleixner820d24d2016-07-12 20:28:12 +0200428 if (vol_id >= UBI_SPL_VOL_IDS || vol_type != UBI_STATIC_VOLUME)
429 return 0;
430
431 /* We are only interested in the volumes to load */
432 if (!test_bit(vol_id, ubi->toload))
433 return 0;
Hamish Guthrie3ce740d2019-05-15 15:15:59 +0200434#endif
Thomas Gleixner820d24d2016-07-12 20:28:12 +0200435 vh = ubi->blockinfo + pnum;
436
437 return ubi_scan_vid_hdr(ubi, vh, pnum);
438}
439
440static int scan_pool(struct ubi_scan_info *ubi, __be32 *pebs, int pool_size)
441{
442 struct ubi_vid_hdr *vh;
443 u32 pnum;
444 int i;
445
446 ubi_dbg("Scanning pool size: %d", pool_size);
447
448 for (i = 0; i < pool_size; i++) {
449 pnum = be32_to_cpu(pebs[i]);
450
451 if (ubi_io_is_bad(ubi, pnum)) {
452 ubi_err("FM: Bad PEB in fastmap pool! %u", pnum);
453 return UBI_BAD_FASTMAP;
454 }
455
456 vh = ubi->blockinfo + pnum;
457 /*
458 * We allow the scan to fail here. The loader will notice
459 * and look for a replacement.
460 */
461 ubi_scan_vid_hdr(ubi, vh, pnum);
462 }
463 return 0;
464}
465
466/*
467 * Fastmap code is stolen from Linux kernel and this stub structure is used
468 * to make it happy.
469 */
470struct ubi_attach_info {
471 int i;
472};
473
474static int ubi_attach_fastmap(struct ubi_scan_info *ubi,
475 struct ubi_attach_info *ai,
476 struct ubi_fastmap_layout *fm)
477{
478 struct ubi_fm_hdr *fmhdr;
479 struct ubi_fm_scan_pool *fmpl1, *fmpl2;
480 struct ubi_fm_ec *fmec;
481 struct ubi_fm_volhdr *fmvhdr;
482 struct ubi_fm_eba *fm_eba;
483 int ret, i, j, pool_size, wl_pool_size;
484 size_t fm_pos = 0, fm_size = ubi->fm_size;
485 void *fm_raw = ubi->fm_buf;
486
487 memset(ubi->fm_used, 0, sizeof(ubi->fm_used));
488
489 fm_pos += sizeof(struct ubi_fm_sb);
490 if (fm_pos >= fm_size)
491 goto fail_bad;
492
493 fmhdr = (struct ubi_fm_hdr *)(fm_raw + fm_pos);
494 fm_pos += sizeof(*fmhdr);
495 if (fm_pos >= fm_size)
496 goto fail_bad;
497
498 if (be32_to_cpu(fmhdr->magic) != UBI_FM_HDR_MAGIC) {
499 ubi_err("bad fastmap header magic: 0x%x, expected: 0x%x",
500 be32_to_cpu(fmhdr->magic), UBI_FM_HDR_MAGIC);
501 goto fail_bad;
502 }
503
504 fmpl1 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
505 fm_pos += sizeof(*fmpl1);
506 if (fm_pos >= fm_size)
507 goto fail_bad;
508 if (be32_to_cpu(fmpl1->magic) != UBI_FM_POOL_MAGIC) {
509 ubi_err("bad fastmap pool magic: 0x%x, expected: 0x%x",
510 be32_to_cpu(fmpl1->magic), UBI_FM_POOL_MAGIC);
511 goto fail_bad;
512 }
513
514 fmpl2 = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos);
515 fm_pos += sizeof(*fmpl2);
516 if (fm_pos >= fm_size)
517 goto fail_bad;
518 if (be32_to_cpu(fmpl2->magic) != UBI_FM_POOL_MAGIC) {
519 ubi_err("bad fastmap pool magic: 0x%x, expected: 0x%x",
520 be32_to_cpu(fmpl2->magic), UBI_FM_POOL_MAGIC);
521 goto fail_bad;
522 }
523
524 pool_size = be16_to_cpu(fmpl1->size);
525 wl_pool_size = be16_to_cpu(fmpl2->size);
526 fm->max_pool_size = be16_to_cpu(fmpl1->max_size);
527 fm->max_wl_pool_size = be16_to_cpu(fmpl2->max_size);
528
529 if (pool_size > UBI_FM_MAX_POOL_SIZE || pool_size < 0) {
530 ubi_err("bad pool size: %i", pool_size);
531 goto fail_bad;
532 }
533
534 if (wl_pool_size > UBI_FM_MAX_POOL_SIZE || wl_pool_size < 0) {
535 ubi_err("bad WL pool size: %i", wl_pool_size);
536 goto fail_bad;
537 }
538
539 if (fm->max_pool_size > UBI_FM_MAX_POOL_SIZE ||
540 fm->max_pool_size < 0) {
541 ubi_err("bad maximal pool size: %i", fm->max_pool_size);
542 goto fail_bad;
543 }
544
545 if (fm->max_wl_pool_size > UBI_FM_MAX_POOL_SIZE ||
546 fm->max_wl_pool_size < 0) {
547 ubi_err("bad maximal WL pool size: %i", fm->max_wl_pool_size);
548 goto fail_bad;
549 }
550
551 /* read EC values from free list */
552 for (i = 0; i < be32_to_cpu(fmhdr->free_peb_count); i++) {
553 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
554 fm_pos += sizeof(*fmec);
555 if (fm_pos >= fm_size)
556 goto fail_bad;
557 }
558
559 /* read EC values from used list */
560 for (i = 0; i < be32_to_cpu(fmhdr->used_peb_count); i++) {
561 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
562 fm_pos += sizeof(*fmec);
563 if (fm_pos >= fm_size)
564 goto fail_bad;
565
566 generic_set_bit(be32_to_cpu(fmec->pnum), ubi->fm_used);
567 }
568
569 /* read EC values from scrub list */
570 for (i = 0; i < be32_to_cpu(fmhdr->scrub_peb_count); i++) {
571 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
572 fm_pos += sizeof(*fmec);
573 if (fm_pos >= fm_size)
574 goto fail_bad;
575 }
576
577 /* read EC values from erase list */
578 for (i = 0; i < be32_to_cpu(fmhdr->erase_peb_count); i++) {
579 fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos);
580 fm_pos += sizeof(*fmec);
581 if (fm_pos >= fm_size)
582 goto fail_bad;
583 }
584
585 /* Iterate over all volumes and read their EBA table */
586 for (i = 0; i < be32_to_cpu(fmhdr->vol_count); i++) {
587 u32 vol_id, vol_type, used, reserved;
588
589 fmvhdr = (struct ubi_fm_volhdr *)(fm_raw + fm_pos);
590 fm_pos += sizeof(*fmvhdr);
591 if (fm_pos >= fm_size)
592 goto fail_bad;
593
594 if (be32_to_cpu(fmvhdr->magic) != UBI_FM_VHDR_MAGIC) {
595 ubi_err("bad fastmap vol header magic: 0x%x, " \
596 "expected: 0x%x",
597 be32_to_cpu(fmvhdr->magic), UBI_FM_VHDR_MAGIC);
598 goto fail_bad;
599 }
600
601 vol_id = be32_to_cpu(fmvhdr->vol_id);
602 vol_type = fmvhdr->vol_type;
603 used = be32_to_cpu(fmvhdr->used_ebs);
604
605 fm_eba = (struct ubi_fm_eba *)(fm_raw + fm_pos);
606 fm_pos += sizeof(*fm_eba);
607 fm_pos += (sizeof(__be32) * be32_to_cpu(fm_eba->reserved_pebs));
608 if (fm_pos >= fm_size)
609 goto fail_bad;
610
611 if (be32_to_cpu(fm_eba->magic) != UBI_FM_EBA_MAGIC) {
612 ubi_err("bad fastmap EBA header magic: 0x%x, " \
613 "expected: 0x%x",
614 be32_to_cpu(fm_eba->magic), UBI_FM_EBA_MAGIC);
615 goto fail_bad;
616 }
617
618 reserved = be32_to_cpu(fm_eba->reserved_pebs);
619 ubi_dbg("FA: vol %u used %u res: %u", vol_id, used, reserved);
620 for (j = 0; j < reserved; j++) {
621 int pnum = be32_to_cpu(fm_eba->pnum[j]);
622
623 if ((int)be32_to_cpu(fm_eba->pnum[j]) < 0)
624 continue;
625
626 if (!__test_and_clear_bit(pnum, ubi->fm_used))
627 continue;
628
629 /*
630 * We only handle static volumes so used_ebs
631 * needs to be handed in. And we do not assign
632 * the reserved blocks
633 */
634 if (j >= used)
635 continue;
636
637 ret = assign_aeb_to_av(ubi, pnum, j, vol_id,
638 vol_type, used);
639 if (!ret)
640 continue;
641
642 /*
643 * Nasty: The fastmap claims that the volume
644 * has one block more than it, but that block
645 * is always empty and the other blocks have
646 * the correct number of total LEBs in the
647 * headers. Deal with it.
648 */
649 if (ret != UBI_IO_FF && j != used - 1)
650 goto fail_bad;
651 ubi_dbg("FA: Vol: %u Ignoring empty LEB %d of %d",
652 vol_id, j, used);
653 }
654 }
655
656 ret = scan_pool(ubi, fmpl1->pebs, pool_size);
657 if (ret)
658 goto fail;
659
660 ret = scan_pool(ubi, fmpl2->pebs, wl_pool_size);
661 if (ret)
662 goto fail;
663
664#ifdef CHECKME
665 /*
666 * If fastmap is leaking PEBs (must not happen), raise a
667 * fat warning and fall back to scanning mode.
668 * We do this here because in ubi_wl_init() it's too late
669 * and we cannot fall back to scanning.
670 */
671 if (WARN_ON(count_fastmap_pebs(ai) != ubi->peb_count -
672 ai->bad_peb_count - fm->used_blocks))
673 goto fail_bad;
674#endif
675
676 return 0;
677
678fail_bad:
679 ret = UBI_BAD_FASTMAP;
680fail:
681 return ret;
682}
683
684static int ubi_scan_fastmap(struct ubi_scan_info *ubi,
685 struct ubi_attach_info *ai,
686 int fm_anchor)
687{
688 struct ubi_fm_sb *fmsb, *fmsb2;
689 struct ubi_vid_hdr *vh;
690 struct ubi_fastmap_layout *fm;
691 int i, used_blocks, pnum, ret = 0;
692 size_t fm_size;
693 __be32 crc, tmp_crc;
694 unsigned long long sqnum = 0;
695
696 fmsb = &ubi->fm_sb;
697 fm = &ubi->fm_layout;
698
699 ret = ubi_io_read(ubi, fmsb, fm_anchor, ubi->leb_start, sizeof(*fmsb));
700 if (ret && ret != UBI_IO_BITFLIPS)
701 goto free_fm_sb;
702 else if (ret == UBI_IO_BITFLIPS)
703 fm->to_be_tortured[0] = 1;
704
705 if (be32_to_cpu(fmsb->magic) != UBI_FM_SB_MAGIC) {
706 ubi_err("bad super block magic: 0x%x, expected: 0x%x",
707 be32_to_cpu(fmsb->magic), UBI_FM_SB_MAGIC);
708 ret = UBI_BAD_FASTMAP;
709 goto free_fm_sb;
710 }
711
712 if (fmsb->version != UBI_FM_FMT_VERSION) {
713 ubi_err("bad fastmap version: %i, expected: %i",
714 fmsb->version, UBI_FM_FMT_VERSION);
715 ret = UBI_BAD_FASTMAP;
716 goto free_fm_sb;
717 }
718
719 used_blocks = be32_to_cpu(fmsb->used_blocks);
720 if (used_blocks > UBI_FM_MAX_BLOCKS || used_blocks < 1) {
721 ubi_err("number of fastmap blocks is invalid: %i", used_blocks);
722 ret = UBI_BAD_FASTMAP;
723 goto free_fm_sb;
724 }
725
726 fm_size = ubi->leb_size * used_blocks;
727 if (fm_size != ubi->fm_size) {
728 ubi_err("bad fastmap size: %zi, expected: %zi", fm_size,
729 ubi->fm_size);
730 ret = UBI_BAD_FASTMAP;
731 goto free_fm_sb;
732 }
733
734 vh = &ubi->fm_vh;
735
736 for (i = 0; i < used_blocks; i++) {
737 pnum = be32_to_cpu(fmsb->block_loc[i]);
738
739 if (ubi_io_is_bad(ubi, pnum)) {
740 ret = UBI_BAD_FASTMAP;
741 goto free_hdr;
742 }
743
744#ifdef LATER
745 int image_seq;
746 ret = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
747 if (ret && ret != UBI_IO_BITFLIPS) {
748 ubi_err("unable to read fastmap block# %i EC (PEB: %i)",
749 i, pnum);
750 if (ret > 0)
751 ret = UBI_BAD_FASTMAP;
752 goto free_hdr;
753 } else if (ret == UBI_IO_BITFLIPS)
754 fm->to_be_tortured[i] = 1;
755
756 image_seq = be32_to_cpu(ech->image_seq);
757 if (!ubi->image_seq)
758 ubi->image_seq = image_seq;
759 /*
760 * Older UBI implementations have image_seq set to zero, so
761 * we shouldn't fail if image_seq == 0.
762 */
763 if (image_seq && (image_seq != ubi->image_seq)) {
764 ubi_err("wrong image seq:%d instead of %d",
765 be32_to_cpu(ech->image_seq), ubi->image_seq);
766 ret = UBI_BAD_FASTMAP;
767 goto free_hdr;
768 }
769#endif
770 ret = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
771 if (ret && ret != UBI_IO_BITFLIPS) {
772 ubi_err("unable to read fastmap block# %i (PEB: %i)",
773 i, pnum);
774 goto free_hdr;
775 }
776
777 /*
778 * Mainline code rescans the anchor header. We've done
779 * that already so we merily copy it over.
780 */
781 if (pnum == fm_anchor)
782 memcpy(vh, ubi->blockinfo + pnum, sizeof(*fm));
783
784 if (i == 0) {
785 if (be32_to_cpu(vh->vol_id) != UBI_FM_SB_VOLUME_ID) {
786 ubi_err("bad fastmap anchor vol_id: 0x%x," \
787 " expected: 0x%x",
788 be32_to_cpu(vh->vol_id),
789 UBI_FM_SB_VOLUME_ID);
790 ret = UBI_BAD_FASTMAP;
791 goto free_hdr;
792 }
793 } else {
794 if (be32_to_cpu(vh->vol_id) != UBI_FM_DATA_VOLUME_ID) {
795 ubi_err("bad fastmap data vol_id: 0x%x," \
796 " expected: 0x%x",
797 be32_to_cpu(vh->vol_id),
798 UBI_FM_DATA_VOLUME_ID);
799 ret = UBI_BAD_FASTMAP;
800 goto free_hdr;
801 }
802 }
803
804 if (sqnum < be64_to_cpu(vh->sqnum))
805 sqnum = be64_to_cpu(vh->sqnum);
806
807 ret = ubi_io_read(ubi, ubi->fm_buf + (ubi->leb_size * i), pnum,
808 ubi->leb_start, ubi->leb_size);
809 if (ret && ret != UBI_IO_BITFLIPS) {
810 ubi_err("unable to read fastmap block# %i (PEB: %i, " \
811 "err: %i)", i, pnum, ret);
812 goto free_hdr;
813 }
814 }
815
816 fmsb2 = (struct ubi_fm_sb *)(ubi->fm_buf);
817 tmp_crc = be32_to_cpu(fmsb2->data_crc);
818 fmsb2->data_crc = 0;
819 crc = crc32(UBI_CRC32_INIT, ubi->fm_buf, fm_size);
820 if (crc != tmp_crc) {
821 ubi_err("fastmap data CRC is invalid");
822 ubi_err("CRC should be: 0x%x, calc: 0x%x", tmp_crc, crc);
823 ret = UBI_BAD_FASTMAP;
824 goto free_hdr;
825 }
826
827 fmsb2->sqnum = sqnum;
828
829 fm->used_blocks = used_blocks;
830
831 ret = ubi_attach_fastmap(ubi, ai, fm);
832 if (ret) {
833 if (ret > 0)
834 ret = UBI_BAD_FASTMAP;
835 goto free_hdr;
836 }
837
838 ubi->fm = fm;
839 ubi->fm_pool.max_size = ubi->fm->max_pool_size;
840 ubi->fm_wl_pool.max_size = ubi->fm->max_wl_pool_size;
841 ubi_msg("attached by fastmap %uMB %u blocks",
842 ubi->fsize_mb, ubi->peb_count);
843 ubi_dbg("fastmap pool size: %d", ubi->fm_pool.max_size);
844 ubi_dbg("fastmap WL pool size: %d", ubi->fm_wl_pool.max_size);
845
846out:
847 if (ret)
848 ubi_err("Attach by fastmap failed, doing a full scan!");
849 return ret;
850
851free_hdr:
852free_fm_sb:
853 goto out;
854}
855
856/*
857 * Scan the flash and attempt to attach via fastmap
858 */
859static void ipl_scan(struct ubi_scan_info *ubi)
860{
861 unsigned int pnum;
862 int res;
863
864 /*
865 * Scan first for the fastmap super block
866 */
867 for (pnum = 0; pnum < UBI_FM_MAX_START; pnum++) {
868 res = ubi_scan_vid_hdr(ubi, ubi->blockinfo + pnum, pnum);
869 /*
870 * We ignore errors here as we are meriliy scanning
871 * the headers.
872 */
873 if (res != UBI_FASTMAP_ANCHOR)
874 continue;
875
876 /*
877 * If fastmap is disabled, continue scanning. This
878 * might happen because the previous attempt failed or
879 * the caller disabled it right away.
880 */
881 if (!ubi->fm_enabled)
882 continue;
883
884 /*
885 * Try to attach the fastmap, if that fails continue
886 * scanning.
887 */
888 if (!ubi_scan_fastmap(ubi, NULL, pnum))
889 return;
890 /*
891 * Fastmap failed. Clear everything we have and start
892 * over. We are paranoid and do not trust anything.
893 */
894 memset(ubi->volinfo, 0, sizeof(ubi->volinfo));
895 pnum = 0;
896 break;
897 }
898
899 /*
900 * Continue scanning, ignore errors, we might find what we are
901 * looking for,
902 */
903 for (; pnum < ubi->peb_count; pnum++)
904 ubi_scan_vid_hdr(ubi, ubi->blockinfo + pnum, pnum);
905}
906
907/*
908 * Load a logical block of a volume into memory
909 */
910static int ubi_load_block(struct ubi_scan_info *ubi, uint8_t *laddr,
911 struct ubi_vol_info *vi, u32 vol_id, u32 lnum,
912 u32 last)
913{
914 struct ubi_vid_hdr *vh, *vrepl;
915 u32 pnum, crc, dlen;
916
917retry:
918 /*
919 * If this is a fastmap run, we try to rescan full, otherwise
920 * we simply give up.
921 */
922 if (!test_bit(lnum, vi->found)) {
923 ubi_warn("LEB %d of %d is missing", lnum, last);
924 return -EINVAL;
925 }
926
927 pnum = vi->lebs_to_pebs[lnum];
928
929 ubi_dbg("Load vol %u LEB %u PEB %u", vol_id, lnum, pnum);
930
931 if (ubi_io_is_bad(ubi, pnum)) {
932 ubi_warn("Corrupted mapping block %d PB %d\n", lnum, pnum);
933 return -EINVAL;
934 }
935
936 if (test_bit(pnum, ubi->corrupt))
937 goto find_other;
938
939 /*
940 * Lets try to read that block
941 */
942 vh = ubi->blockinfo + pnum;
943
944 if (!test_bit(pnum, ubi->scanned)) {
945 ubi_warn("Vol: %u LEB %u PEB %u not yet scanned", vol_id,
946 lnum, pnum);
947 if (ubi_rescan_fm_vid_hdr(ubi, vh, pnum, vol_id, lnum))
948 goto find_other;
949 }
950
951 /*
952 * Check, if the total number of blocks is correct
953 */
954 if (be32_to_cpu(vh->used_ebs) != last) {
Vagrant Cascadianadee3992021-12-21 13:06:53 -0800955 ubi_dbg("Block count mismatch.");
Thomas Gleixner820d24d2016-07-12 20:28:12 +0200956 ubi_dbg("vh->used_ebs: %d nrblocks: %d",
957 be32_to_cpu(vh->used_ebs), last);
958 generic_set_bit(pnum, ubi->corrupt);
959 goto find_other;
960 }
961
962 /*
963 * Get the data length of this block.
964 */
965 dlen = be32_to_cpu(vh->data_size);
966
967 /*
968 * Read the data into RAM. We ignore the return value
969 * here as the only thing which might go wrong are
970 * bitflips. Try nevertheless.
971 */
972 ubi_io_read(ubi, laddr, pnum, ubi->leb_start, dlen);
973
974 /* Calculate CRC over the data */
975 crc = crc32(UBI_CRC32_INIT, laddr, dlen);
976
977 if (crc != be32_to_cpu(vh->data_crc)) {
978 ubi_warn("Vol: %u LEB %u PEB %u data CRC failure", vol_id,
979 lnum, pnum);
980 generic_set_bit(pnum, ubi->corrupt);
981 goto find_other;
982 }
983
984 /* We are good. Return the data length we read */
985 return dlen;
986
987find_other:
988 ubi_dbg("Find replacement for LEB %u PEB %u", lnum, pnum);
989 generic_clear_bit(lnum, vi->found);
990 vrepl = NULL;
991
992 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
993 struct ubi_vid_hdr *tmp = ubi->blockinfo + pnum;
994 u32 t_vol_id = be32_to_cpu(tmp->vol_id);
995 u32 t_lnum = be32_to_cpu(tmp->lnum);
996
997 if (test_bit(pnum, ubi->corrupt))
998 continue;
999
1000 if (t_vol_id != vol_id || t_lnum != lnum)
1001 continue;
1002
1003 if (!test_bit(pnum, ubi->scanned)) {
1004 ubi_warn("Vol: %u LEB %u PEB %u not yet scanned",
1005 vol_id, lnum, pnum);
1006 if (ubi_rescan_fm_vid_hdr(ubi, tmp, pnum, vol_id, lnum))
1007 continue;
1008 }
1009
1010 /*
1011 * We found one. If its the first, assign it otherwise
1012 * compare the sqnum
1013 */
1014 generic_set_bit(lnum, vi->found);
1015
1016 if (!vrepl) {
1017 vrepl = tmp;
1018 continue;
1019 }
1020
1021 if (be64_to_cpu(vrepl->sqnum) < be64_to_cpu(tmp->sqnum))
1022 vrepl = tmp;
1023 }
1024
1025 if (vrepl) {
1026 /* Update the vi table */
1027 pnum = vrepl - ubi->blockinfo;
1028 vi->lebs_to_pebs[lnum] = pnum;
1029 ubi_dbg("Trying PEB %u for LEB %u", pnum, lnum);
1030 vh = vrepl;
1031 }
1032 goto retry;
1033}
1034
1035/*
1036 * Load a volume into RAM
1037 */
1038static int ipl_load(struct ubi_scan_info *ubi, const u32 vol_id, uint8_t *laddr)
1039{
1040 struct ubi_vol_info *vi;
1041 u32 lnum, last, len;
1042
1043 if (vol_id >= UBI_SPL_VOL_IDS)
1044 return -EINVAL;
1045
1046 len = 0;
1047 vi = ubi->volinfo + vol_id;
1048 last = vi->last_block + 1;
1049
1050 /* Read the blocks to RAM, check CRC */
1051 for (lnum = 0 ; lnum < last; lnum++) {
1052 int res = ubi_load_block(ubi, laddr, vi, vol_id, lnum, last);
1053
1054 if (res < 0) {
1055 ubi_warn("Failed to load volume %u", vol_id);
1056 return res;
1057 }
1058 /* res is the data length of the read block */
1059 laddr += res;
1060 len += res;
1061 }
1062 return len;
1063}
1064
1065int ubispl_load_volumes(struct ubispl_info *info, struct ubispl_load *lvols,
1066 int nrvols)
1067{
1068 struct ubi_scan_info *ubi = info->ubi;
1069 int res, i, fastmap = info->fastmap;
1070 u32 fsize;
1071
1072retry:
1073 /*
1074 * We do a partial initializiation of @ubi. Cleaning fm_buf is
1075 * not necessary.
1076 */
1077 memset(ubi, 0, offsetof(struct ubi_scan_info, fm_buf));
1078
1079 ubi->read = info->read;
1080
1081 /* Precalculate the offsets */
1082 ubi->vid_offset = info->vid_offset;
1083 ubi->leb_start = info->leb_start;
1084 ubi->leb_size = info->peb_size - ubi->leb_start;
1085 ubi->peb_count = info->peb_count;
1086 ubi->peb_offset = info->peb_offset;
1087
Hamish Guthrie3ce740d2019-05-15 15:15:59 +02001088#ifdef CONFIG_SPL_UBI_LOAD_BY_VOLNAME
1089 ubi->vtbl_valid = 0;
1090#endif
1091
Thomas Gleixner820d24d2016-07-12 20:28:12 +02001092 fsize = info->peb_size * info->peb_count;
1093 ubi->fsize_mb = fsize >> 20;
1094
1095 /* Fastmap init */
1096 ubi->fm_size = ubi_calc_fm_size(ubi);
1097 ubi->fm_enabled = fastmap;
1098
1099 for (i = 0; i < nrvols; i++) {
1100 struct ubispl_load *lv = lvols + i;
1101
1102 generic_set_bit(lv->vol_id, ubi->toload);
1103 }
1104
1105 ipl_scan(ubi);
1106
1107 for (i = 0; i < nrvols; i++) {
1108 struct ubispl_load *lv = lvols + i;
1109
Hamish Guthrie3ce740d2019-05-15 15:15:59 +02001110#ifdef CONFIG_SPL_UBI_LOAD_BY_VOLNAME
1111 if (lv->vol_id == -1) {
1112 for (int j = 0; j < UBI_SPL_VOL_IDS; j++) {
1113 int len = be16_to_cpu(ubi->vtbl[j].name_len);
1114
1115 if (strncmp(lv->name,
1116 ubi->vtbl[j].name,
1117 len) == 0) {
1118 lv->vol_id = j;
1119 break;
1120 }
1121 }
1122 }
1123 ubi_msg("Loading VolName %s (VolId #%d)", lv->name, lv->vol_id);
1124#else
Thomas Gleixner820d24d2016-07-12 20:28:12 +02001125 ubi_msg("Loading VolId #%d", lv->vol_id);
Hamish Guthrie3ce740d2019-05-15 15:15:59 +02001126#endif
Thomas Gleixner820d24d2016-07-12 20:28:12 +02001127 res = ipl_load(ubi, lv->vol_id, lv->load_addr);
1128 if (res < 0) {
1129 if (fastmap) {
1130 fastmap = 0;
1131 goto retry;
1132 }
1133 ubi_warn("Failed");
1134 return res;
1135 }
1136 }
1137 return 0;
1138}