2
* Copyright (c) International Business Machines Corp., 2006
3
* Copyright (c) Nokia Corporation, 2006, 2007
5
* SPDX-License-Identifier: GPL-2.0+
7
* Author: Artem Bityutskiy (Битюцкий Артём)
11
* UBI input/output unit.
13
* This unit provides a uniform way to work with all kinds of the underlying
14
* MTD devices. It also implements handy functions for reading and writing UBI
17
* We are trying to have a paranoid mindset and not to trust to what we read
18
* from the flash media in order to be more secure and robust. So this unit
19
* validates every single header it reads from the flash media.
21
* Some words about how the eraseblock headers are stored.
23
* The erase counter header is always stored at offset zero. By default, the
24
* VID header is stored after the EC header at the closest aligned offset
25
* (i.e. aligned to the minimum I/O unit size). Data starts next to the VID
26
* header at the closest aligned offset. But this default layout may be
27
* changed. For example, for different reasons (e.g., optimization) UBI may be
28
* asked to put the VID header at further offset, and even at an unaligned
29
* offset. Of course, if the offset of the VID header is unaligned, UBI adds
30
* proper padding in front of it. Data offset may also be changed but it has to
33
* About minimal I/O units. In general, UBI assumes flash device model where
34
* there is only one minimal I/O unit size. E.g., in case of NOR flash it is 1,
35
* in case of NAND flash it is a NAND page, etc. This is reported by MTD in the
36
* @ubi->mtd->writesize field. But as an exception, UBI admits of using another
37
* (smaller) minimal I/O unit size for EC and VID headers to make it possible
38
* to do different optimizations.
40
* This is extremely useful in case of NAND flashes which admit of several
41
* write operations to one NAND page. In this case UBI can fit EC and VID
42
* headers at one NAND page. Thus, UBI may use "sub-page" size as the minimal
43
* I/O unit for the headers (the @ubi->hdrs_min_io_size field). But it still
44
* reports NAND page size (@ubi->min_io_size) as a minimal I/O unit for the UBI
47
* Example: some Samsung NANDs with 2KiB pages allow 4x 512-byte writes, so
48
* although the minimal I/O unit is 2K, UBI uses 512 bytes for EC and VID
51
* Q: why not just to treat sub-page as a minimal I/O unit of this flash
52
* device, e.g., make @ubi->min_io_size = 512 in the example above?
54
* A: because when writing a sub-page, MTD still writes a full 2K page but the
55
* bytes which are no relevant to the sub-page are 0xFF. So, basically, writing
56
* 4x512 sub-pages is 4 times slower then writing one 2KiB NAND page. Thus, we
57
* prefer to use sub-pages only for EV and VID headers.
59
* As it was noted above, the VID header may start at a non-aligned offset.
60
* For example, in case of a 2KiB page NAND flash with a 512 bytes sub-page,
61
* the VID header may reside at offset 1984 which is the last 64 bytes of the
62
* last sub-page (EC header is always at offset zero). This causes some
63
* difficulties when reading and writing VID headers.
65
* Suppose we have a 64-byte buffer and we read a VID header at it. We change
66
* the data and want to write this VID header out. As we can only write in
67
* 512-byte chunks, we have to allocate one more buffer and copy our VID header
68
* to offset 448 of this buffer.
70
* The I/O unit does the following trick in order to avoid this extra copy.
71
* It always allocates a @ubi->vid_hdr_alsize bytes buffer for the VID header
72
* and returns a pointer to offset @ubi->vid_hdr_shift of this buffer. When the
73
* VID header is being written out, it shifts the VID header pointer back and
74
* writes the whole sub-page.
78
#include <linux/crc32.h>
79
#include <linux/err.h>
82
#include <ubi_uboot.h>
85
#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
86
static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum);
87
static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum);
88
static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum,
89
const struct ubi_ec_hdr *ec_hdr);
90
static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum);
91
static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum,
92
const struct ubi_vid_hdr *vid_hdr);
93
static int paranoid_check_all_ff(struct ubi_device *ubi, int pnum, int offset,
96
#define paranoid_check_not_bad(ubi, pnum) 0
97
#define paranoid_check_peb_ec_hdr(ubi, pnum) 0
98
#define paranoid_check_ec_hdr(ubi, pnum, ec_hdr) 0
99
#define paranoid_check_peb_vid_hdr(ubi, pnum) 0
100
#define paranoid_check_vid_hdr(ubi, pnum, vid_hdr) 0
101
#define paranoid_check_all_ff(ubi, pnum, offset, len) 0
105
* ubi_io_read - read data from a physical eraseblock.
106
* @ubi: UBI device description object
107
* @buf: buffer where to store the read data
108
* @pnum: physical eraseblock number to read from
109
* @offset: offset within the physical eraseblock from where to read
110
* @len: how many bytes to read
112
* This function reads data from offset @offset of physical eraseblock @pnum
113
* and stores the read data in the @buf buffer. The following return codes are
116
* o %0 if all the requested data were successfully read;
117
* o %UBI_IO_BITFLIPS if all the requested data were successfully read, but
118
* correctable bit-flips were detected; this is harmless but may indicate
119
* that this eraseblock may become bad soon (but do not have to);
120
* o %-EBADMSG if the MTD subsystem reported about data integrity problems, for
121
* example it can be an ECC error in case of NAND; this most probably means
122
* that the data is corrupted;
123
* o %-EIO if some I/O error occurred;
124
* o other negative error codes in case of other errors.
126
int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset,
129
int err, retries = 0;
133
dbg_io("read %d bytes from PEB %d:%d", len, pnum, offset);
135
ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
136
ubi_assert(offset >= 0 && offset + len <= ubi->peb_size);
139
err = paranoid_check_not_bad(ubi, pnum);
141
return err > 0 ? -EINVAL : err;
143
addr = (loff_t)pnum * ubi->peb_size + offset;
145
err = mtd_read(ubi->mtd, addr, len, &read, buf);
147
if (err == -EUCLEAN) {
149
* -EUCLEAN is reported if there was a bit-flip which
150
* was corrected, so this is harmless.
152
ubi_msg("fixable bit-flip detected at PEB %d", pnum);
153
ubi_assert(len == read);
154
return UBI_IO_BITFLIPS;
157
if (read != len && retries++ < UBI_IO_RETRIES) {
158
dbg_io("error %d while reading %d bytes from PEB %d:%d, "
159
"read only %zd bytes, retry",
160
err, len, pnum, offset, read);
165
ubi_err("error %d while reading %d bytes from PEB %d:%d, "
166
"read %zd bytes", err, len, pnum, offset, read);
167
ubi_dbg_dump_stack();
170
* The driver should never return -EBADMSG if it failed to read
171
* all the requested data. But some buggy drivers might do
172
* this, so we change it to -EIO.
174
if (read != len && err == -EBADMSG) {
176
printk("%s[%d] not here\n", __func__, __LINE__);
180
ubi_assert(len == read);
182
if (ubi_dbg_is_bitflip()) {
183
dbg_msg("bit-flip (emulated)");
184
err = UBI_IO_BITFLIPS;
192
* ubi_io_write - write data to a physical eraseblock.
193
* @ubi: UBI device description object
194
* @buf: buffer with the data to write
195
* @pnum: physical eraseblock number to write to
196
* @offset: offset within the physical eraseblock where to write
197
* @len: how many bytes to write
199
* This function writes @len bytes of data from buffer @buf to offset @offset
200
* of physical eraseblock @pnum. If all the data were successfully written,
201
* zero is returned. If an error occurred, this function returns a negative
202
* error code. If %-EIO is returned, the physical eraseblock most probably went
205
* Note, in case of an error, it is possible that something was still written
206
* to the flash media, but may be some garbage.
208
int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
215
dbg_io("write %d bytes to PEB %d:%d", len, pnum, offset);
217
ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
218
ubi_assert(offset >= 0 && offset + len <= ubi->peb_size);
219
ubi_assert(offset % ubi->hdrs_min_io_size == 0);
220
ubi_assert(len > 0 && len % ubi->hdrs_min_io_size == 0);
223
ubi_err("read-only mode");
227
/* The below has to be compiled out if paranoid checks are disabled */
229
err = paranoid_check_not_bad(ubi, pnum);
231
return err > 0 ? -EINVAL : err;
233
/* The area we are writing to has to contain all 0xFF bytes */
234
err = paranoid_check_all_ff(ubi, pnum, offset, len);
236
return err > 0 ? -EINVAL : err;
238
if (offset >= ubi->leb_start) {
240
* We write to the data area of the physical eraseblock. Make
241
* sure it has valid EC and VID headers.
243
err = paranoid_check_peb_ec_hdr(ubi, pnum);
245
return err > 0 ? -EINVAL : err;
246
err = paranoid_check_peb_vid_hdr(ubi, pnum);
248
return err > 0 ? -EINVAL : err;
251
if (ubi_dbg_is_write_failure()) {
252
dbg_err("cannot write %d bytes to PEB %d:%d "
253
"(emulated)", len, pnum, offset);
254
ubi_dbg_dump_stack();
258
addr = (loff_t)pnum * ubi->peb_size + offset;
259
err = mtd_write(ubi->mtd, addr, len, &written, buf);
261
ubi_err("error %d while writing %d bytes to PEB %d:%d, written"
262
" %zd bytes", err, len, pnum, offset, written);
263
ubi_dbg_dump_stack();
265
ubi_assert(written == len);
271
* erase_callback - MTD erasure call-back.
272
* @ei: MTD erase information object.
274
* Note, even though MTD erase interface is asynchronous, all the current
275
* implementations are synchronous anyway.
277
static void erase_callback(struct erase_info *ei)
279
wake_up_interruptible((wait_queue_head_t *)ei->priv);
283
* do_sync_erase - synchronously erase a physical eraseblock.
284
* @ubi: UBI device description object
285
* @pnum: the physical eraseblock number to erase
287
* This function synchronously erases physical eraseblock @pnum and returns
288
* zero in case of success and a negative error code in case of failure. If
289
* %-EIO is returned, the physical eraseblock most probably went bad.
291
static int do_sync_erase(struct ubi_device *ubi, int pnum)
293
int err, retries = 0;
294
struct erase_info ei;
295
wait_queue_head_t wq;
297
dbg_io("erase PEB %d", pnum);
300
init_waitqueue_head(&wq);
301
memset(&ei, 0, sizeof(struct erase_info));
304
ei.addr = (loff_t)pnum * ubi->peb_size;
305
ei.len = ubi->peb_size;
306
ei.callback = erase_callback;
307
ei.priv = (unsigned long)&wq;
309
err = mtd_erase(ubi->mtd, &ei);
311
if (retries++ < UBI_IO_RETRIES) {
312
dbg_io("error %d while erasing PEB %d, retry",
317
ubi_err("cannot erase PEB %d, error %d", pnum, err);
318
ubi_dbg_dump_stack();
322
err = wait_event_interruptible(wq, ei.state == MTD_ERASE_DONE ||
323
ei.state == MTD_ERASE_FAILED);
325
ubi_err("interrupted PEB %d erasure", pnum);
329
if (ei.state == MTD_ERASE_FAILED) {
330
if (retries++ < UBI_IO_RETRIES) {
331
dbg_io("error while erasing PEB %d, retry", pnum);
335
ubi_err("cannot erase PEB %d", pnum);
336
ubi_dbg_dump_stack();
340
err = paranoid_check_all_ff(ubi, pnum, 0, ubi->peb_size);
342
return err > 0 ? -EINVAL : err;
344
if (ubi_dbg_is_erase_failure() && !err) {
345
dbg_err("cannot erase PEB %d (emulated)", pnum);
353
* check_pattern - check if buffer contains only a certain byte pattern.
354
* @buf: buffer to check
355
* @patt: the pattern to check
356
* @size: buffer size in bytes
358
* This function returns %1 in there are only @patt bytes in @buf, and %0 if
359
* something else was also found.
361
static int check_pattern(const void *buf, uint8_t patt, int size)
365
for (i = 0; i < size; i++)
366
if (((const uint8_t *)buf)[i] != patt)
371
/* Patterns to write to a physical eraseblock when torturing it */
372
static uint8_t patterns[] = {0xa5, 0x5a, 0x0};
375
* torture_peb - test a supposedly bad physical eraseblock.
376
* @ubi: UBI device description object
377
* @pnum: the physical eraseblock number to test
379
* This function returns %-EIO if the physical eraseblock did not pass the
380
* test, a positive number of erase operations done if the test was
381
* successfully passed, and other negative error codes in case of other errors.
383
static int torture_peb(struct ubi_device *ubi, int pnum)
385
int err, i, patt_count;
387
patt_count = ARRAY_SIZE(patterns);
388
ubi_assert(patt_count > 0);
390
mutex_lock(&ubi->buf_mutex);
391
for (i = 0; i < patt_count; i++) {
392
err = do_sync_erase(ubi, pnum);
396
/* Make sure the PEB contains only 0xFF bytes */
397
err = ubi_io_read(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size);
401
err = check_pattern(ubi->peb_buf1, 0xFF, ubi->peb_size);
403
ubi_err("erased PEB %d, but a non-0xFF byte found",
409
/* Write a pattern and check it */
410
memset(ubi->peb_buf1, patterns[i], ubi->peb_size);
411
err = ubi_io_write(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size);
415
memset(ubi->peb_buf1, ~patterns[i], ubi->peb_size);
416
err = ubi_io_read(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size);
420
err = check_pattern(ubi->peb_buf1, patterns[i], ubi->peb_size);
422
ubi_err("pattern %x checking failed for PEB %d",
432
mutex_unlock(&ubi->buf_mutex);
433
if (err == UBI_IO_BITFLIPS || err == -EBADMSG) {
435
* If a bit-flip or data integrity error was detected, the test
436
* has not passed because it happened on a freshly erased
437
* physical eraseblock which means something is wrong with it.
439
ubi_err("read problems on freshly erased PEB %d, must be bad",
447
* ubi_io_sync_erase - synchronously erase a physical eraseblock.
448
* @ubi: UBI device description object
449
* @pnum: physical eraseblock number to erase
450
* @torture: if this physical eraseblock has to be tortured
452
* This function synchronously erases physical eraseblock @pnum. If @torture
453
* flag is not zero, the physical eraseblock is checked by means of writing
454
* different patterns to it and reading them back. If the torturing is enabled,
455
* the physical eraseblock is erased more then once.
457
* This function returns the number of erasures made in case of success, %-EIO
458
* if the erasure failed or the torturing test failed, and other negative error
459
* codes in case of other errors. Note, %-EIO means that the physical
462
int ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture)
466
ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
468
err = paranoid_check_not_bad(ubi, pnum);
470
return err > 0 ? -EINVAL : err;
473
ubi_err("read-only mode");
478
ret = torture_peb(ubi, pnum);
483
err = do_sync_erase(ubi, pnum);
491
* ubi_io_is_bad - check if a physical eraseblock is bad.
492
* @ubi: UBI device description object
493
* @pnum: the physical eraseblock number to check
495
* This function returns a positive number if the physical eraseblock is bad,
496
* zero if not, and a negative error code if an error occurred.
498
int ubi_io_is_bad(const struct ubi_device *ubi, int pnum)
500
struct mtd_info *mtd = ubi->mtd;
502
ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
504
if (ubi->bad_allowed) {
507
ret = mtd_block_isbad(mtd, (loff_t)pnum * ubi->peb_size);
509
ubi_err("error %d while checking if PEB %d is bad",
512
dbg_io("PEB %d is bad", pnum);
520
* ubi_io_mark_bad - mark a physical eraseblock as bad.
521
* @ubi: UBI device description object
522
* @pnum: the physical eraseblock number to mark
524
* This function returns zero in case of success and a negative error code in
527
int ubi_io_mark_bad(const struct ubi_device *ubi, int pnum)
530
struct mtd_info *mtd = ubi->mtd;
532
ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
535
ubi_err("read-only mode");
539
if (!ubi->bad_allowed)
542
err = mtd_block_markbad(mtd, (loff_t)pnum * ubi->peb_size);
544
ubi_err("cannot mark PEB %d bad, error %d", pnum, err);
549
* validate_ec_hdr - validate an erase counter header.
550
* @ubi: UBI device description object
551
* @ec_hdr: the erase counter header to check
553
* This function returns zero if the erase counter header is OK, and %1 if
556
static int validate_ec_hdr(const struct ubi_device *ubi,
557
const struct ubi_ec_hdr *ec_hdr)
560
int vid_hdr_offset, leb_start;
562
ec = be64_to_cpu(ec_hdr->ec);
563
vid_hdr_offset = be32_to_cpu(ec_hdr->vid_hdr_offset);
564
leb_start = be32_to_cpu(ec_hdr->data_offset);
566
if (ec_hdr->version != UBI_VERSION) {
567
ubi_err("node with incompatible UBI version found: "
568
"this UBI version is %d, image version is %d",
569
UBI_VERSION, (int)ec_hdr->version);
573
if (vid_hdr_offset != ubi->vid_hdr_offset) {
574
ubi_err("bad VID header offset %d, expected %d",
575
vid_hdr_offset, ubi->vid_hdr_offset);
579
if (leb_start != ubi->leb_start) {
580
ubi_err("bad data offset %d, expected %d",
581
leb_start, ubi->leb_start);
585
if (ec < 0 || ec > UBI_MAX_ERASECOUNTER) {
586
ubi_err("bad erase counter %lld", ec);
593
ubi_err("bad EC header");
594
ubi_dbg_dump_ec_hdr(ec_hdr);
595
ubi_dbg_dump_stack();
600
* ubi_io_read_ec_hdr - read and check an erase counter header.
601
* @ubi: UBI device description object
602
* @pnum: physical eraseblock to read from
603
* @ec_hdr: a &struct ubi_ec_hdr object where to store the read erase counter
605
* @verbose: be verbose if the header is corrupted or was not found
607
* This function reads erase counter header from physical eraseblock @pnum and
608
* stores it in @ec_hdr. This function also checks CRC checksum of the read
609
* erase counter header. The following codes may be returned:
611
* o %0 if the CRC checksum is correct and the header was successfully read;
612
* o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected
613
* and corrected by the flash driver; this is harmless but may indicate that
614
* this eraseblock may become bad soon (but may be not);
615
* o %UBI_IO_BAD_EC_HDR if the erase counter header is corrupted (a CRC error);
616
* o %UBI_IO_PEB_EMPTY if the physical eraseblock is empty;
617
* o a negative error code in case of failure.
619
int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
620
struct ubi_ec_hdr *ec_hdr, int verbose)
622
int err, read_err = 0;
623
uint32_t crc, magic, hdr_crc;
625
dbg_io("read EC header from PEB %d", pnum);
626
ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
630
err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE);
632
if (err != UBI_IO_BITFLIPS && err != -EBADMSG)
636
* We read all the data, but either a correctable bit-flip
637
* occurred, or MTD reported about some data integrity error,
638
* like an ECC error in case of NAND. The former is harmless,
639
* the later may mean that the read data is corrupted. But we
640
* have a CRC check-sum and we will detect this. If the EC
641
* header is still OK, we just report this as there was a
647
magic = be32_to_cpu(ec_hdr->magic);
648
if (magic != UBI_EC_HDR_MAGIC) {
650
* The magic field is wrong. Let's check if we have read all
651
* 0xFF. If yes, this physical eraseblock is assumed to be
654
* But if there was a read error, we do not test it for all
655
* 0xFFs. Even if it does contain all 0xFFs, this error
656
* indicates that something is still wrong with this physical
657
* eraseblock and we anyway cannot treat it as empty.
659
if (read_err != -EBADMSG &&
660
check_pattern(ec_hdr, 0xFF, UBI_EC_HDR_SIZE)) {
661
/* The physical eraseblock is supposedly empty */
664
* The below is just a paranoid check, it has to be
665
* compiled out if paranoid checks are disabled.
667
err = paranoid_check_all_ff(ubi, pnum, 0,
670
return err > 0 ? UBI_IO_BAD_EC_HDR : err;
673
ubi_warn("no EC header found at PEB %d, "
674
"only 0xFF bytes", pnum);
675
return UBI_IO_PEB_EMPTY;
679
* This is not a valid erase counter header, and these are not
680
* 0xFF bytes. Report that the header is corrupted.
683
ubi_warn("bad magic number at PEB %d: %08x instead of "
684
"%08x", pnum, magic, UBI_EC_HDR_MAGIC);
685
ubi_dbg_dump_ec_hdr(ec_hdr);
687
return UBI_IO_BAD_EC_HDR;
690
crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
691
hdr_crc = be32_to_cpu(ec_hdr->hdr_crc);
693
if (hdr_crc != crc) {
695
ubi_warn("bad EC header CRC at PEB %d, calculated %#08x,"
696
" read %#08x", pnum, crc, hdr_crc);
697
ubi_dbg_dump_ec_hdr(ec_hdr);
699
return UBI_IO_BAD_EC_HDR;
702
/* And of course validate what has just been read from the media */
703
err = validate_ec_hdr(ubi, ec_hdr);
705
ubi_err("validation failed for PEB %d", pnum);
709
return read_err ? UBI_IO_BITFLIPS : 0;
713
* ubi_io_write_ec_hdr - write an erase counter header.
714
* @ubi: UBI device description object
715
* @pnum: physical eraseblock to write to
716
* @ec_hdr: the erase counter header to write
718
* This function writes erase counter header described by @ec_hdr to physical
719
* eraseblock @pnum. It also fills most fields of @ec_hdr before writing, so
720
* the caller do not have to fill them. Callers must only fill the @ec_hdr->ec
723
* This function returns zero in case of success and a negative error code in
724
* case of failure. If %-EIO is returned, the physical eraseblock most probably
727
int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum,
728
struct ubi_ec_hdr *ec_hdr)
733
dbg_io("write EC header to PEB %d", pnum);
734
ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
736
ec_hdr->magic = cpu_to_be32(UBI_EC_HDR_MAGIC);
737
ec_hdr->version = UBI_VERSION;
738
ec_hdr->vid_hdr_offset = cpu_to_be32(ubi->vid_hdr_offset);
739
ec_hdr->data_offset = cpu_to_be32(ubi->leb_start);
740
crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
741
ec_hdr->hdr_crc = cpu_to_be32(crc);
743
err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr);
747
err = ubi_io_write(ubi, ec_hdr, pnum, 0, ubi->ec_hdr_alsize);
752
* validate_vid_hdr - validate a volume identifier header.
753
* @ubi: UBI device description object
754
* @vid_hdr: the volume identifier header to check
756
* This function checks that data stored in the volume identifier header
757
* @vid_hdr. Returns zero if the VID header is OK and %1 if not.
759
static int validate_vid_hdr(const struct ubi_device *ubi,
760
const struct ubi_vid_hdr *vid_hdr)
762
int vol_type = vid_hdr->vol_type;
763
int copy_flag = vid_hdr->copy_flag;
764
int vol_id = be32_to_cpu(vid_hdr->vol_id);
765
int lnum = be32_to_cpu(vid_hdr->lnum);
766
int compat = vid_hdr->compat;
767
int data_size = be32_to_cpu(vid_hdr->data_size);
768
int used_ebs = be32_to_cpu(vid_hdr->used_ebs);
769
int data_pad = be32_to_cpu(vid_hdr->data_pad);
770
int data_crc = be32_to_cpu(vid_hdr->data_crc);
771
int usable_leb_size = ubi->leb_size - data_pad;
773
if (copy_flag != 0 && copy_flag != 1) {
774
dbg_err("bad copy_flag");
778
if (vol_id < 0 || lnum < 0 || data_size < 0 || used_ebs < 0 ||
780
dbg_err("negative values");
784
if (vol_id >= UBI_MAX_VOLUMES && vol_id < UBI_INTERNAL_VOL_START) {
785
dbg_err("bad vol_id");
789
if (vol_id < UBI_INTERNAL_VOL_START && compat != 0) {
790
dbg_err("bad compat");
794
if (vol_id >= UBI_INTERNAL_VOL_START && compat != UBI_COMPAT_DELETE &&
795
compat != UBI_COMPAT_RO && compat != UBI_COMPAT_PRESERVE &&
796
compat != UBI_COMPAT_REJECT) {
797
dbg_err("bad compat");
801
if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) {
802
dbg_err("bad vol_type");
806
if (data_pad >= ubi->leb_size / 2) {
807
dbg_err("bad data_pad");
811
if (vol_type == UBI_VID_STATIC) {
813
* Although from high-level point of view static volumes may
814
* contain zero bytes of data, but no VID headers can contain
815
* zero at these fields, because they empty volumes do not have
816
* mapped logical eraseblocks.
819
dbg_err("zero used_ebs");
822
if (data_size == 0) {
823
dbg_err("zero data_size");
826
if (lnum < used_ebs - 1) {
827
if (data_size != usable_leb_size) {
828
dbg_err("bad data_size");
831
} else if (lnum == used_ebs - 1) {
832
if (data_size == 0) {
833
dbg_err("bad data_size at last LEB");
837
dbg_err("too high lnum");
841
if (copy_flag == 0) {
843
dbg_err("non-zero data CRC");
846
if (data_size != 0) {
847
dbg_err("non-zero data_size");
851
if (data_size == 0) {
852
dbg_err("zero data_size of copy");
857
dbg_err("bad used_ebs");
865
ubi_err("bad VID header");
866
ubi_dbg_dump_vid_hdr(vid_hdr);
867
ubi_dbg_dump_stack();
872
* ubi_io_read_vid_hdr - read and check a volume identifier header.
873
* @ubi: UBI device description object
874
* @pnum: physical eraseblock number to read from
875
* @vid_hdr: &struct ubi_vid_hdr object where to store the read volume
877
* @verbose: be verbose if the header is corrupted or wasn't found
879
* This function reads the volume identifier header from physical eraseblock
880
* @pnum and stores it in @vid_hdr. It also checks CRC checksum of the read
881
* volume identifier header. The following codes may be returned:
883
* o %0 if the CRC checksum is correct and the header was successfully read;
884
* o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected
885
* and corrected by the flash driver; this is harmless but may indicate that
886
* this eraseblock may become bad soon;
887
* o %UBI_IO_BAD_VID_HRD if the volume identifier header is corrupted (a CRC
889
* o %UBI_IO_PEB_FREE if the physical eraseblock is free (i.e., there is no VID
891
* o a negative error code in case of failure.
893
int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
894
struct ubi_vid_hdr *vid_hdr, int verbose)
896
int err, read_err = 0;
897
uint32_t crc, magic, hdr_crc;
900
dbg_io("read VID header from PEB %d", pnum);
901
ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
905
p = (char *)vid_hdr - ubi->vid_hdr_shift;
906
err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset,
907
ubi->vid_hdr_alsize);
909
if (err != UBI_IO_BITFLIPS && err != -EBADMSG)
913
* We read all the data, but either a correctable bit-flip
914
* occurred, or MTD reported about some data integrity error,
915
* like an ECC error in case of NAND. The former is harmless,
916
* the later may mean the read data is corrupted. But we have a
917
* CRC check-sum and we will identify this. If the VID header is
918
* still OK, we just report this as there was a bit-flip.
923
magic = be32_to_cpu(vid_hdr->magic);
924
if (magic != UBI_VID_HDR_MAGIC) {
926
* If we have read all 0xFF bytes, the VID header probably does
927
* not exist and the physical eraseblock is assumed to be free.
929
* But if there was a read error, we do not test the data for
930
* 0xFFs. Even if it does contain all 0xFFs, this error
931
* indicates that something is still wrong with this physical
932
* eraseblock and it cannot be regarded as free.
934
if (read_err != -EBADMSG &&
935
check_pattern(vid_hdr, 0xFF, UBI_VID_HDR_SIZE)) {
936
/* The physical eraseblock is supposedly free */
939
* The below is just a paranoid check, it has to be
940
* compiled out if paranoid checks are disabled.
942
err = paranoid_check_all_ff(ubi, pnum, ubi->leb_start,
945
return err > 0 ? UBI_IO_BAD_VID_HDR : err;
948
ubi_warn("no VID header found at PEB %d, "
949
"only 0xFF bytes", pnum);
950
return UBI_IO_PEB_FREE;
954
* This is not a valid VID header, and these are not 0xFF
955
* bytes. Report that the header is corrupted.
958
ubi_warn("bad magic number at PEB %d: %08x instead of "
959
"%08x", pnum, magic, UBI_VID_HDR_MAGIC);
960
ubi_dbg_dump_vid_hdr(vid_hdr);
962
return UBI_IO_BAD_VID_HDR;
965
crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC);
966
hdr_crc = be32_to_cpu(vid_hdr->hdr_crc);
968
if (hdr_crc != crc) {
970
ubi_warn("bad CRC at PEB %d, calculated %#08x, "
971
"read %#08x", pnum, crc, hdr_crc);
972
ubi_dbg_dump_vid_hdr(vid_hdr);
974
return UBI_IO_BAD_VID_HDR;
977
/* Validate the VID header that we have just read */
978
err = validate_vid_hdr(ubi, vid_hdr);
980
ubi_err("validation failed for PEB %d", pnum);
984
return read_err ? UBI_IO_BITFLIPS : 0;
988
* ubi_io_write_vid_hdr - write a volume identifier header.
989
* @ubi: UBI device description object
990
* @pnum: the physical eraseblock number to write to
991
* @vid_hdr: the volume identifier header to write
993
* This function writes the volume identifier header described by @vid_hdr to
994
* physical eraseblock @pnum. This function automatically fills the
995
* @vid_hdr->magic and the @vid_hdr->version fields, as well as calculates
996
* header CRC checksum and stores it at vid_hdr->hdr_crc.
998
* This function returns zero in case of success and a negative error code in
999
* case of failure. If %-EIO is returned, the physical eraseblock probably went
1002
int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
1003
struct ubi_vid_hdr *vid_hdr)
1009
dbg_io("write VID header to PEB %d", pnum);
1010
ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
1012
err = paranoid_check_peb_ec_hdr(ubi, pnum);
1014
return err > 0 ? -EINVAL: err;
1016
vid_hdr->magic = cpu_to_be32(UBI_VID_HDR_MAGIC);
1017
vid_hdr->version = UBI_VERSION;
1018
crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC);
1019
vid_hdr->hdr_crc = cpu_to_be32(crc);
1021
err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr);
1025
p = (char *)vid_hdr - ubi->vid_hdr_shift;
1026
err = ubi_io_write(ubi, p, pnum, ubi->vid_hdr_aloffset,
1027
ubi->vid_hdr_alsize);
1031
#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
1034
* paranoid_check_not_bad - ensure that a physical eraseblock is not bad.
1035
* @ubi: UBI device description object
1036
* @pnum: physical eraseblock number to check
1038
* This function returns zero if the physical eraseblock is good, a positive
1039
* number if it is bad and a negative error code if an error occurred.
1041
static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum)
1045
err = ubi_io_is_bad(ubi, pnum);
1049
ubi_err("paranoid check failed for PEB %d", pnum);
1050
ubi_dbg_dump_stack();
1055
* paranoid_check_ec_hdr - check if an erase counter header is all right.
1056
* @ubi: UBI device description object
1057
* @pnum: physical eraseblock number the erase counter header belongs to
1058
* @ec_hdr: the erase counter header to check
1060
* This function returns zero if the erase counter header contains valid
1061
* values, and %1 if not.
1063
static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum,
1064
const struct ubi_ec_hdr *ec_hdr)
1069
magic = be32_to_cpu(ec_hdr->magic);
1070
if (magic != UBI_EC_HDR_MAGIC) {
1071
ubi_err("bad magic %#08x, must be %#08x",
1072
magic, UBI_EC_HDR_MAGIC);
1076
err = validate_ec_hdr(ubi, ec_hdr);
1078
ubi_err("paranoid check failed for PEB %d", pnum);
1085
ubi_dbg_dump_ec_hdr(ec_hdr);
1086
ubi_dbg_dump_stack();
1091
* paranoid_check_peb_ec_hdr - check that the erase counter header of a
1092
* physical eraseblock is in-place and is all right.
1093
* @ubi: UBI device description object
1094
* @pnum: the physical eraseblock number to check
1096
* This function returns zero if the erase counter header is all right, %1 if
1097
* not, and a negative error code if an error occurred.
1099
static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum)
1102
uint32_t crc, hdr_crc;
1103
struct ubi_ec_hdr *ec_hdr;
1105
ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
1109
err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE);
1110
if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG)
1113
crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
1114
hdr_crc = be32_to_cpu(ec_hdr->hdr_crc);
1115
if (hdr_crc != crc) {
1116
ubi_err("bad CRC, calculated %#08x, read %#08x", crc, hdr_crc);
1117
ubi_err("paranoid check failed for PEB %d", pnum);
1118
ubi_dbg_dump_ec_hdr(ec_hdr);
1119
ubi_dbg_dump_stack();
1124
err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr);
1132
* paranoid_check_vid_hdr - check that a volume identifier header is all right.
1133
* @ubi: UBI device description object
1134
* @pnum: physical eraseblock number the volume identifier header belongs to
1135
* @vid_hdr: the volume identifier header to check
1137
* This function returns zero if the volume identifier header is all right, and
1140
static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum,
1141
const struct ubi_vid_hdr *vid_hdr)
1146
magic = be32_to_cpu(vid_hdr->magic);
1147
if (magic != UBI_VID_HDR_MAGIC) {
1148
ubi_err("bad VID header magic %#08x at PEB %d, must be %#08x",
1149
magic, pnum, UBI_VID_HDR_MAGIC);
1153
err = validate_vid_hdr(ubi, vid_hdr);
1155
ubi_err("paranoid check failed for PEB %d", pnum);
1162
ubi_err("paranoid check failed for PEB %d", pnum);
1163
ubi_dbg_dump_vid_hdr(vid_hdr);
1164
ubi_dbg_dump_stack();
1170
* paranoid_check_peb_vid_hdr - check that the volume identifier header of a
1171
* physical eraseblock is in-place and is all right.
1172
* @ubi: UBI device description object
1173
* @pnum: the physical eraseblock number to check
1175
* This function returns zero if the volume identifier header is all right,
1176
* %1 if not, and a negative error code if an error occurred.
1178
static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum)
1181
uint32_t crc, hdr_crc;
1182
struct ubi_vid_hdr *vid_hdr;
1185
vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
1189
p = (char *)vid_hdr - ubi->vid_hdr_shift;
1190
err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset,
1191
ubi->vid_hdr_alsize);
1192
if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG)
1195
crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_EC_HDR_SIZE_CRC);
1196
hdr_crc = be32_to_cpu(vid_hdr->hdr_crc);
1197
if (hdr_crc != crc) {
1198
ubi_err("bad VID header CRC at PEB %d, calculated %#08x, "
1199
"read %#08x", pnum, crc, hdr_crc);
1200
ubi_err("paranoid check failed for PEB %d", pnum);
1201
ubi_dbg_dump_vid_hdr(vid_hdr);
1202
ubi_dbg_dump_stack();
1207
err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr);
1210
ubi_free_vid_hdr(ubi, vid_hdr);
1215
* paranoid_check_all_ff - check that a region of flash is empty.
1216
* @ubi: UBI device description object
1217
* @pnum: the physical eraseblock number to check
1218
* @offset: the starting offset within the physical eraseblock to check
1219
* @len: the length of the region to check
1221
* This function returns zero if only 0xFF bytes are present at offset
1222
* @offset of the physical eraseblock @pnum, %1 if not, and a negative error
1223
* code if an error occurred.
1225
static int paranoid_check_all_ff(struct ubi_device *ubi, int pnum, int offset,
1230
loff_t addr = (loff_t)pnum * ubi->peb_size + offset;
1232
mutex_lock(&ubi->dbg_buf_mutex);
1233
err = mtd_read(ubi->mtd, addr, len, &read, ubi->dbg_peb_buf);
1234
if (err && err != -EUCLEAN) {
1235
ubi_err("error %d while reading %d bytes from PEB %d:%d, "
1236
"read %zd bytes", err, len, pnum, offset, read);
1240
err = check_pattern(ubi->dbg_peb_buf, 0xFF, len);
1242
ubi_err("flash region at PEB %d:%d, length %d does not "
1243
"contain all 0xFF bytes", pnum, offset, len);
1246
mutex_unlock(&ubi->dbg_buf_mutex);
1251
ubi_err("paranoid check failed for PEB %d", pnum);
1252
dbg_msg("hex dump of the %d-%d region", offset, offset + len);
1253
print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
1254
ubi->dbg_peb_buf, len, 1);
1257
ubi_dbg_dump_stack();
1258
mutex_unlock(&ubi->dbg_buf_mutex);
1262
#endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */