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