xref: /openbmc/u-boot/drivers/mtd/ubi/io.c (revision 1a459660)
12d262c48SKyungmin Park /*
22d262c48SKyungmin Park  * Copyright (c) International Business Machines Corp., 2006
32d262c48SKyungmin Park  * Copyright (c) Nokia Corporation, 2006, 2007
42d262c48SKyungmin Park  *
5*1a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
62d262c48SKyungmin Park  *
72d262c48SKyungmin Park  * Author: Artem Bityutskiy (Битюцкий Артём)
82d262c48SKyungmin Park  */
92d262c48SKyungmin Park 
102d262c48SKyungmin Park /*
112d262c48SKyungmin Park  * UBI input/output unit.
122d262c48SKyungmin Park  *
132d262c48SKyungmin Park  * This unit provides a uniform way to work with all kinds of the underlying
142d262c48SKyungmin Park  * MTD devices. It also implements handy functions for reading and writing UBI
152d262c48SKyungmin Park  * headers.
162d262c48SKyungmin Park  *
172d262c48SKyungmin Park  * We are trying to have a paranoid mindset and not to trust to what we read
182d262c48SKyungmin Park  * from the flash media in order to be more secure and robust. So this unit
192d262c48SKyungmin Park  * validates every single header it reads from the flash media.
202d262c48SKyungmin Park  *
212d262c48SKyungmin Park  * Some words about how the eraseblock headers are stored.
222d262c48SKyungmin Park  *
232d262c48SKyungmin Park  * The erase counter header is always stored at offset zero. By default, the
242d262c48SKyungmin Park  * VID header is stored after the EC header at the closest aligned offset
252d262c48SKyungmin Park  * (i.e. aligned to the minimum I/O unit size). Data starts next to the VID
262d262c48SKyungmin Park  * header at the closest aligned offset. But this default layout may be
272d262c48SKyungmin Park  * changed. For example, for different reasons (e.g., optimization) UBI may be
282d262c48SKyungmin Park  * asked to put the VID header at further offset, and even at an unaligned
292d262c48SKyungmin Park  * offset. Of course, if the offset of the VID header is unaligned, UBI adds
302d262c48SKyungmin Park  * proper padding in front of it. Data offset may also be changed but it has to
312d262c48SKyungmin Park  * be aligned.
322d262c48SKyungmin Park  *
332d262c48SKyungmin Park  * About minimal I/O units. In general, UBI assumes flash device model where
342d262c48SKyungmin Park  * there is only one minimal I/O unit size. E.g., in case of NOR flash it is 1,
352d262c48SKyungmin Park  * in case of NAND flash it is a NAND page, etc. This is reported by MTD in the
362d262c48SKyungmin Park  * @ubi->mtd->writesize field. But as an exception, UBI admits of using another
372d262c48SKyungmin Park  * (smaller) minimal I/O unit size for EC and VID headers to make it possible
382d262c48SKyungmin Park  * to do different optimizations.
392d262c48SKyungmin Park  *
402d262c48SKyungmin Park  * This is extremely useful in case of NAND flashes which admit of several
412d262c48SKyungmin Park  * write operations to one NAND page. In this case UBI can fit EC and VID
422d262c48SKyungmin Park  * headers at one NAND page. Thus, UBI may use "sub-page" size as the minimal
432d262c48SKyungmin Park  * I/O unit for the headers (the @ubi->hdrs_min_io_size field). But it still
442d262c48SKyungmin Park  * reports NAND page size (@ubi->min_io_size) as a minimal I/O unit for the UBI
452d262c48SKyungmin Park  * users.
462d262c48SKyungmin Park  *
472d262c48SKyungmin Park  * Example: some Samsung NANDs with 2KiB pages allow 4x 512-byte writes, so
482d262c48SKyungmin Park  * although the minimal I/O unit is 2K, UBI uses 512 bytes for EC and VID
492d262c48SKyungmin Park  * headers.
502d262c48SKyungmin Park  *
512d262c48SKyungmin Park  * Q: why not just to treat sub-page as a minimal I/O unit of this flash
522d262c48SKyungmin Park  * device, e.g., make @ubi->min_io_size = 512 in the example above?
532d262c48SKyungmin Park  *
542d262c48SKyungmin Park  * A: because when writing a sub-page, MTD still writes a full 2K page but the
552d262c48SKyungmin Park  * bytes which are no relevant to the sub-page are 0xFF. So, basically, writing
562d262c48SKyungmin Park  * 4x512 sub-pages is 4 times slower then writing one 2KiB NAND page. Thus, we
572d262c48SKyungmin Park  * prefer to use sub-pages only for EV and VID headers.
582d262c48SKyungmin Park  *
592d262c48SKyungmin Park  * As it was noted above, the VID header may start at a non-aligned offset.
602d262c48SKyungmin Park  * For example, in case of a 2KiB page NAND flash with a 512 bytes sub-page,
612d262c48SKyungmin Park  * the VID header may reside at offset 1984 which is the last 64 bytes of the
622d262c48SKyungmin Park  * last sub-page (EC header is always at offset zero). This causes some
632d262c48SKyungmin Park  * difficulties when reading and writing VID headers.
642d262c48SKyungmin Park  *
652d262c48SKyungmin Park  * Suppose we have a 64-byte buffer and we read a VID header at it. We change
662d262c48SKyungmin Park  * the data and want to write this VID header out. As we can only write in
672d262c48SKyungmin Park  * 512-byte chunks, we have to allocate one more buffer and copy our VID header
682d262c48SKyungmin Park  * to offset 448 of this buffer.
692d262c48SKyungmin Park  *
702d262c48SKyungmin Park  * The I/O unit does the following trick in order to avoid this extra copy.
712d262c48SKyungmin Park  * It always allocates a @ubi->vid_hdr_alsize bytes buffer for the VID header
722d262c48SKyungmin Park  * and returns a pointer to offset @ubi->vid_hdr_shift of this buffer. When the
732d262c48SKyungmin Park  * VID header is being written out, it shifts the VID header pointer back and
742d262c48SKyungmin Park  * writes the whole sub-page.
752d262c48SKyungmin Park  */
762d262c48SKyungmin Park 
772d262c48SKyungmin Park #ifdef UBI_LINUX
782d262c48SKyungmin Park #include <linux/crc32.h>
792d262c48SKyungmin Park #include <linux/err.h>
802d262c48SKyungmin Park #endif
812d262c48SKyungmin Park 
822d262c48SKyungmin Park #include <ubi_uboot.h>
832d262c48SKyungmin Park #include "ubi.h"
842d262c48SKyungmin Park 
852d262c48SKyungmin Park #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
862d262c48SKyungmin Park static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum);
872d262c48SKyungmin Park static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum);
882d262c48SKyungmin Park static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum,
892d262c48SKyungmin Park 				 const struct ubi_ec_hdr *ec_hdr);
902d262c48SKyungmin Park static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum);
912d262c48SKyungmin Park static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum,
922d262c48SKyungmin Park 				  const struct ubi_vid_hdr *vid_hdr);
932d262c48SKyungmin Park static int paranoid_check_all_ff(struct ubi_device *ubi, int pnum, int offset,
942d262c48SKyungmin Park 				 int len);
952d262c48SKyungmin Park #else
962d262c48SKyungmin Park #define paranoid_check_not_bad(ubi, pnum) 0
972d262c48SKyungmin Park #define paranoid_check_peb_ec_hdr(ubi, pnum)  0
982d262c48SKyungmin Park #define paranoid_check_ec_hdr(ubi, pnum, ec_hdr)  0
992d262c48SKyungmin Park #define paranoid_check_peb_vid_hdr(ubi, pnum) 0
1002d262c48SKyungmin Park #define paranoid_check_vid_hdr(ubi, pnum, vid_hdr) 0
1012d262c48SKyungmin Park #define paranoid_check_all_ff(ubi, pnum, offset, len) 0
1022d262c48SKyungmin Park #endif
1032d262c48SKyungmin Park 
1042d262c48SKyungmin Park /**
1052d262c48SKyungmin Park  * ubi_io_read - read data from a physical eraseblock.
1062d262c48SKyungmin Park  * @ubi: UBI device description object
1072d262c48SKyungmin Park  * @buf: buffer where to store the read data
1082d262c48SKyungmin Park  * @pnum: physical eraseblock number to read from
1092d262c48SKyungmin Park  * @offset: offset within the physical eraseblock from where to read
1102d262c48SKyungmin Park  * @len: how many bytes to read
1112d262c48SKyungmin Park  *
1122d262c48SKyungmin Park  * This function reads data from offset @offset of physical eraseblock @pnum
1132d262c48SKyungmin Park  * and stores the read data in the @buf buffer. The following return codes are
1142d262c48SKyungmin Park  * possible:
1152d262c48SKyungmin Park  *
1162d262c48SKyungmin Park  * o %0 if all the requested data were successfully read;
1172d262c48SKyungmin Park  * o %UBI_IO_BITFLIPS if all the requested data were successfully read, but
1182d262c48SKyungmin Park  *   correctable bit-flips were detected; this is harmless but may indicate
1192d262c48SKyungmin Park  *   that this eraseblock may become bad soon (but do not have to);
1202d262c48SKyungmin Park  * o %-EBADMSG if the MTD subsystem reported about data integrity problems, for
1212d262c48SKyungmin Park  *   example it can be an ECC error in case of NAND; this most probably means
1222d262c48SKyungmin Park  *   that the data is corrupted;
1232d262c48SKyungmin Park  * o %-EIO if some I/O error occurred;
1242d262c48SKyungmin Park  * o other negative error codes in case of other errors.
1252d262c48SKyungmin Park  */
1262d262c48SKyungmin Park int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset,
1272d262c48SKyungmin Park 		int len)
1282d262c48SKyungmin Park {
1292d262c48SKyungmin Park 	int err, retries = 0;
1302d262c48SKyungmin Park 	size_t read;
1312d262c48SKyungmin Park 	loff_t addr;
1322d262c48SKyungmin Park 
1332d262c48SKyungmin Park 	dbg_io("read %d bytes from PEB %d:%d", len, pnum, offset);
1342d262c48SKyungmin Park 
1352d262c48SKyungmin Park 	ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
1362d262c48SKyungmin Park 	ubi_assert(offset >= 0 && offset + len <= ubi->peb_size);
1372d262c48SKyungmin Park 	ubi_assert(len > 0);
1382d262c48SKyungmin Park 
1392d262c48SKyungmin Park 	err = paranoid_check_not_bad(ubi, pnum);
1402d262c48SKyungmin Park 	if (err)
1412d262c48SKyungmin Park 		return err > 0 ? -EINVAL : err;
1422d262c48SKyungmin Park 
1432d262c48SKyungmin Park 	addr = (loff_t)pnum * ubi->peb_size + offset;
1442d262c48SKyungmin Park retry:
145dfe64e2cSSergey Lapin 	err = mtd_read(ubi->mtd, addr, len, &read, buf);
1462d262c48SKyungmin Park 	if (err) {
1472d262c48SKyungmin Park 		if (err == -EUCLEAN) {
1482d262c48SKyungmin Park 			/*
1492d262c48SKyungmin Park 			 * -EUCLEAN is reported if there was a bit-flip which
1502d262c48SKyungmin Park 			 * was corrected, so this is harmless.
1512d262c48SKyungmin Park 			 */
1522d262c48SKyungmin Park 			ubi_msg("fixable bit-flip detected at PEB %d", pnum);
1532d262c48SKyungmin Park 			ubi_assert(len == read);
1542d262c48SKyungmin Park 			return UBI_IO_BITFLIPS;
1552d262c48SKyungmin Park 		}
1562d262c48SKyungmin Park 
1572d262c48SKyungmin Park 		if (read != len && retries++ < UBI_IO_RETRIES) {
1582d262c48SKyungmin Park 			dbg_io("error %d while reading %d bytes from PEB %d:%d, "
1592d262c48SKyungmin Park 			       "read only %zd bytes, retry",
1602d262c48SKyungmin Park 			       err, len, pnum, offset, read);
1612d262c48SKyungmin Park 			yield();
1622d262c48SKyungmin Park 			goto retry;
1632d262c48SKyungmin Park 		}
1642d262c48SKyungmin Park 
1652d262c48SKyungmin Park 		ubi_err("error %d while reading %d bytes from PEB %d:%d, "
1662d262c48SKyungmin Park 			"read %zd bytes", err, len, pnum, offset, read);
1672d262c48SKyungmin Park 		ubi_dbg_dump_stack();
1682d262c48SKyungmin Park 
1692d262c48SKyungmin Park 		/*
1702d262c48SKyungmin Park 		 * The driver should never return -EBADMSG if it failed to read
1712d262c48SKyungmin Park 		 * all the requested data. But some buggy drivers might do
1722d262c48SKyungmin Park 		 * this, so we change it to -EIO.
1732d262c48SKyungmin Park 		 */
1742d262c48SKyungmin Park 		if (read != len && err == -EBADMSG) {
1752d262c48SKyungmin Park 			ubi_assert(0);
1762d262c48SKyungmin Park 			printk("%s[%d] not here\n", __func__, __LINE__);
177455ae7e8SWolfgang Denk /*			err = -EIO; */
1782d262c48SKyungmin Park 		}
1792d262c48SKyungmin Park 	} else {
1802d262c48SKyungmin Park 		ubi_assert(len == read);
1812d262c48SKyungmin Park 
1822d262c48SKyungmin Park 		if (ubi_dbg_is_bitflip()) {
1832d262c48SKyungmin Park 			dbg_msg("bit-flip (emulated)");
1842d262c48SKyungmin Park 			err = UBI_IO_BITFLIPS;
1852d262c48SKyungmin Park 		}
1862d262c48SKyungmin Park 	}
1872d262c48SKyungmin Park 
1882d262c48SKyungmin Park 	return err;
1892d262c48SKyungmin Park }
1902d262c48SKyungmin Park 
1912d262c48SKyungmin Park /**
1922d262c48SKyungmin Park  * ubi_io_write - write data to a physical eraseblock.
1932d262c48SKyungmin Park  * @ubi: UBI device description object
1942d262c48SKyungmin Park  * @buf: buffer with the data to write
1952d262c48SKyungmin Park  * @pnum: physical eraseblock number to write to
1962d262c48SKyungmin Park  * @offset: offset within the physical eraseblock where to write
1972d262c48SKyungmin Park  * @len: how many bytes to write
1982d262c48SKyungmin Park  *
1992d262c48SKyungmin Park  * This function writes @len bytes of data from buffer @buf to offset @offset
2002d262c48SKyungmin Park  * of physical eraseblock @pnum. If all the data were successfully written,
2012d262c48SKyungmin Park  * zero is returned. If an error occurred, this function returns a negative
2022d262c48SKyungmin Park  * error code. If %-EIO is returned, the physical eraseblock most probably went
2032d262c48SKyungmin Park  * bad.
2042d262c48SKyungmin Park  *
2052d262c48SKyungmin Park  * Note, in case of an error, it is possible that something was still written
2062d262c48SKyungmin Park  * to the flash media, but may be some garbage.
2072d262c48SKyungmin Park  */
2082d262c48SKyungmin Park int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
2092d262c48SKyungmin Park 		 int len)
2102d262c48SKyungmin Park {
2112d262c48SKyungmin Park 	int err;
2122d262c48SKyungmin Park 	size_t written;
2132d262c48SKyungmin Park 	loff_t addr;
2142d262c48SKyungmin Park 
2152d262c48SKyungmin Park 	dbg_io("write %d bytes to PEB %d:%d", len, pnum, offset);
2162d262c48SKyungmin Park 
2172d262c48SKyungmin Park 	ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
2182d262c48SKyungmin Park 	ubi_assert(offset >= 0 && offset + len <= ubi->peb_size);
2192d262c48SKyungmin Park 	ubi_assert(offset % ubi->hdrs_min_io_size == 0);
2202d262c48SKyungmin Park 	ubi_assert(len > 0 && len % ubi->hdrs_min_io_size == 0);
2212d262c48SKyungmin Park 
2222d262c48SKyungmin Park 	if (ubi->ro_mode) {
2232d262c48SKyungmin Park 		ubi_err("read-only mode");
2242d262c48SKyungmin Park 		return -EROFS;
2252d262c48SKyungmin Park 	}
2262d262c48SKyungmin Park 
2272d262c48SKyungmin Park 	/* The below has to be compiled out if paranoid checks are disabled */
2282d262c48SKyungmin Park 
2292d262c48SKyungmin Park 	err = paranoid_check_not_bad(ubi, pnum);
2302d262c48SKyungmin Park 	if (err)
2312d262c48SKyungmin Park 		return err > 0 ? -EINVAL : err;
2322d262c48SKyungmin Park 
2332d262c48SKyungmin Park 	/* The area we are writing to has to contain all 0xFF bytes */
2342d262c48SKyungmin Park 	err = paranoid_check_all_ff(ubi, pnum, offset, len);
2352d262c48SKyungmin Park 	if (err)
2362d262c48SKyungmin Park 		return err > 0 ? -EINVAL : err;
2372d262c48SKyungmin Park 
2382d262c48SKyungmin Park 	if (offset >= ubi->leb_start) {
2392d262c48SKyungmin Park 		/*
2402d262c48SKyungmin Park 		 * We write to the data area of the physical eraseblock. Make
2412d262c48SKyungmin Park 		 * sure it has valid EC and VID headers.
2422d262c48SKyungmin Park 		 */
2432d262c48SKyungmin Park 		err = paranoid_check_peb_ec_hdr(ubi, pnum);
2442d262c48SKyungmin Park 		if (err)
2452d262c48SKyungmin Park 			return err > 0 ? -EINVAL : err;
2462d262c48SKyungmin Park 		err = paranoid_check_peb_vid_hdr(ubi, pnum);
2472d262c48SKyungmin Park 		if (err)
2482d262c48SKyungmin Park 			return err > 0 ? -EINVAL : err;
2492d262c48SKyungmin Park 	}
2502d262c48SKyungmin Park 
2512d262c48SKyungmin Park 	if (ubi_dbg_is_write_failure()) {
2522d262c48SKyungmin Park 		dbg_err("cannot write %d bytes to PEB %d:%d "
2532d262c48SKyungmin Park 			"(emulated)", len, pnum, offset);
2542d262c48SKyungmin Park 		ubi_dbg_dump_stack();
2552d262c48SKyungmin Park 		return -EIO;
2562d262c48SKyungmin Park 	}
2572d262c48SKyungmin Park 
2582d262c48SKyungmin Park 	addr = (loff_t)pnum * ubi->peb_size + offset;
259dfe64e2cSSergey Lapin 	err = mtd_write(ubi->mtd, addr, len, &written, buf);
2602d262c48SKyungmin Park 	if (err) {
2612d262c48SKyungmin Park 		ubi_err("error %d while writing %d bytes to PEB %d:%d, written"
2622d262c48SKyungmin Park 			" %zd bytes", err, len, pnum, offset, written);
2632d262c48SKyungmin Park 		ubi_dbg_dump_stack();
2642d262c48SKyungmin Park 	} else
2652d262c48SKyungmin Park 		ubi_assert(written == len);
2662d262c48SKyungmin Park 
2672d262c48SKyungmin Park 	return err;
2682d262c48SKyungmin Park }
2692d262c48SKyungmin Park 
2702d262c48SKyungmin Park /**
2712d262c48SKyungmin Park  * erase_callback - MTD erasure call-back.
2722d262c48SKyungmin Park  * @ei: MTD erase information object.
2732d262c48SKyungmin Park  *
2742d262c48SKyungmin Park  * Note, even though MTD erase interface is asynchronous, all the current
2752d262c48SKyungmin Park  * implementations are synchronous anyway.
2762d262c48SKyungmin Park  */
2772d262c48SKyungmin Park static void erase_callback(struct erase_info *ei)
2782d262c48SKyungmin Park {
2792d262c48SKyungmin Park 	wake_up_interruptible((wait_queue_head_t *)ei->priv);
2802d262c48SKyungmin Park }
2812d262c48SKyungmin Park 
2822d262c48SKyungmin Park /**
2832d262c48SKyungmin Park  * do_sync_erase - synchronously erase a physical eraseblock.
2842d262c48SKyungmin Park  * @ubi: UBI device description object
2852d262c48SKyungmin Park  * @pnum: the physical eraseblock number to erase
2862d262c48SKyungmin Park  *
2872d262c48SKyungmin Park  * This function synchronously erases physical eraseblock @pnum and returns
2882d262c48SKyungmin Park  * zero in case of success and a negative error code in case of failure. If
2892d262c48SKyungmin Park  * %-EIO is returned, the physical eraseblock most probably went bad.
2902d262c48SKyungmin Park  */
2912d262c48SKyungmin Park static int do_sync_erase(struct ubi_device *ubi, int pnum)
2922d262c48SKyungmin Park {
2932d262c48SKyungmin Park 	int err, retries = 0;
2942d262c48SKyungmin Park 	struct erase_info ei;
2952d262c48SKyungmin Park 	wait_queue_head_t wq;
2962d262c48SKyungmin Park 
2972d262c48SKyungmin Park 	dbg_io("erase PEB %d", pnum);
2982d262c48SKyungmin Park 
2992d262c48SKyungmin Park retry:
3002d262c48SKyungmin Park 	init_waitqueue_head(&wq);
3012d262c48SKyungmin Park 	memset(&ei, 0, sizeof(struct erase_info));
3022d262c48SKyungmin Park 
3032d262c48SKyungmin Park 	ei.mtd      = ubi->mtd;
3042d262c48SKyungmin Park 	ei.addr     = (loff_t)pnum * ubi->peb_size;
3052d262c48SKyungmin Park 	ei.len      = ubi->peb_size;
3062d262c48SKyungmin Park 	ei.callback = erase_callback;
3072d262c48SKyungmin Park 	ei.priv     = (unsigned long)&wq;
3082d262c48SKyungmin Park 
309dfe64e2cSSergey Lapin 	err = mtd_erase(ubi->mtd, &ei);
3102d262c48SKyungmin Park 	if (err) {
3112d262c48SKyungmin Park 		if (retries++ < UBI_IO_RETRIES) {
3122d262c48SKyungmin Park 			dbg_io("error %d while erasing PEB %d, retry",
3132d262c48SKyungmin Park 			       err, pnum);
3142d262c48SKyungmin Park 			yield();
3152d262c48SKyungmin Park 			goto retry;
3162d262c48SKyungmin Park 		}
3172d262c48SKyungmin Park 		ubi_err("cannot erase PEB %d, error %d", pnum, err);
3182d262c48SKyungmin Park 		ubi_dbg_dump_stack();
3192d262c48SKyungmin Park 		return err;
3202d262c48SKyungmin Park 	}
3212d262c48SKyungmin Park 
3222d262c48SKyungmin Park 	err = wait_event_interruptible(wq, ei.state == MTD_ERASE_DONE ||
3232d262c48SKyungmin Park 					   ei.state == MTD_ERASE_FAILED);
3242d262c48SKyungmin Park 	if (err) {
3252d262c48SKyungmin Park 		ubi_err("interrupted PEB %d erasure", pnum);
3262d262c48SKyungmin Park 		return -EINTR;
3272d262c48SKyungmin Park 	}
3282d262c48SKyungmin Park 
3292d262c48SKyungmin Park 	if (ei.state == MTD_ERASE_FAILED) {
3302d262c48SKyungmin Park 		if (retries++ < UBI_IO_RETRIES) {
3312d262c48SKyungmin Park 			dbg_io("error while erasing PEB %d, retry", pnum);
3322d262c48SKyungmin Park 			yield();
3332d262c48SKyungmin Park 			goto retry;
3342d262c48SKyungmin Park 		}
3352d262c48SKyungmin Park 		ubi_err("cannot erase PEB %d", pnum);
3362d262c48SKyungmin Park 		ubi_dbg_dump_stack();
3372d262c48SKyungmin Park 		return -EIO;
3382d262c48SKyungmin Park 	}
3392d262c48SKyungmin Park 
3402d262c48SKyungmin Park 	err = paranoid_check_all_ff(ubi, pnum, 0, ubi->peb_size);
3412d262c48SKyungmin Park 	if (err)
3422d262c48SKyungmin Park 		return err > 0 ? -EINVAL : err;
3432d262c48SKyungmin Park 
3442d262c48SKyungmin Park 	if (ubi_dbg_is_erase_failure() && !err) {
3452d262c48SKyungmin Park 		dbg_err("cannot erase PEB %d (emulated)", pnum);
3462d262c48SKyungmin Park 		return -EIO;
3472d262c48SKyungmin Park 	}
3482d262c48SKyungmin Park 
3492d262c48SKyungmin Park 	return 0;
3502d262c48SKyungmin Park }
3512d262c48SKyungmin Park 
3522d262c48SKyungmin Park /**
3532d262c48SKyungmin Park  * check_pattern - check if buffer contains only a certain byte pattern.
3542d262c48SKyungmin Park  * @buf: buffer to check
3552d262c48SKyungmin Park  * @patt: the pattern to check
3562d262c48SKyungmin Park  * @size: buffer size in bytes
3572d262c48SKyungmin Park  *
3582d262c48SKyungmin Park  * This function returns %1 in there are only @patt bytes in @buf, and %0 if
3592d262c48SKyungmin Park  * something else was also found.
3602d262c48SKyungmin Park  */
3612d262c48SKyungmin Park static int check_pattern(const void *buf, uint8_t patt, int size)
3622d262c48SKyungmin Park {
3632d262c48SKyungmin Park 	int i;
3642d262c48SKyungmin Park 
3652d262c48SKyungmin Park 	for (i = 0; i < size; i++)
3662d262c48SKyungmin Park 		if (((const uint8_t *)buf)[i] != patt)
3672d262c48SKyungmin Park 			return 0;
3682d262c48SKyungmin Park 	return 1;
3692d262c48SKyungmin Park }
3702d262c48SKyungmin Park 
3712d262c48SKyungmin Park /* Patterns to write to a physical eraseblock when torturing it */
3722d262c48SKyungmin Park static uint8_t patterns[] = {0xa5, 0x5a, 0x0};
3732d262c48SKyungmin Park 
3742d262c48SKyungmin Park /**
3752d262c48SKyungmin Park  * torture_peb - test a supposedly bad physical eraseblock.
3762d262c48SKyungmin Park  * @ubi: UBI device description object
3772d262c48SKyungmin Park  * @pnum: the physical eraseblock number to test
3782d262c48SKyungmin Park  *
3792d262c48SKyungmin Park  * This function returns %-EIO if the physical eraseblock did not pass the
3802d262c48SKyungmin Park  * test, a positive number of erase operations done if the test was
3812d262c48SKyungmin Park  * successfully passed, and other negative error codes in case of other errors.
3822d262c48SKyungmin Park  */
3832d262c48SKyungmin Park static int torture_peb(struct ubi_device *ubi, int pnum)
3842d262c48SKyungmin Park {
3852d262c48SKyungmin Park 	int err, i, patt_count;
3862d262c48SKyungmin Park 
3872d262c48SKyungmin Park 	patt_count = ARRAY_SIZE(patterns);
3882d262c48SKyungmin Park 	ubi_assert(patt_count > 0);
3892d262c48SKyungmin Park 
3902d262c48SKyungmin Park 	mutex_lock(&ubi->buf_mutex);
3912d262c48SKyungmin Park 	for (i = 0; i < patt_count; i++) {
3922d262c48SKyungmin Park 		err = do_sync_erase(ubi, pnum);
3932d262c48SKyungmin Park 		if (err)
3942d262c48SKyungmin Park 			goto out;
3952d262c48SKyungmin Park 
3962d262c48SKyungmin Park 		/* Make sure the PEB contains only 0xFF bytes */
3972d262c48SKyungmin Park 		err = ubi_io_read(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size);
3982d262c48SKyungmin Park 		if (err)
3992d262c48SKyungmin Park 			goto out;
4002d262c48SKyungmin Park 
4012d262c48SKyungmin Park 		err = check_pattern(ubi->peb_buf1, 0xFF, ubi->peb_size);
4022d262c48SKyungmin Park 		if (err == 0) {
4032d262c48SKyungmin Park 			ubi_err("erased PEB %d, but a non-0xFF byte found",
4042d262c48SKyungmin Park 				pnum);
4052d262c48SKyungmin Park 			err = -EIO;
4062d262c48SKyungmin Park 			goto out;
4072d262c48SKyungmin Park 		}
4082d262c48SKyungmin Park 
4092d262c48SKyungmin Park 		/* Write a pattern and check it */
4102d262c48SKyungmin Park 		memset(ubi->peb_buf1, patterns[i], ubi->peb_size);
4112d262c48SKyungmin Park 		err = ubi_io_write(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size);
4122d262c48SKyungmin Park 		if (err)
4132d262c48SKyungmin Park 			goto out;
4142d262c48SKyungmin Park 
4152d262c48SKyungmin Park 		memset(ubi->peb_buf1, ~patterns[i], ubi->peb_size);
4162d262c48SKyungmin Park 		err = ubi_io_read(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size);
4172d262c48SKyungmin Park 		if (err)
4182d262c48SKyungmin Park 			goto out;
4192d262c48SKyungmin Park 
4202d262c48SKyungmin Park 		err = check_pattern(ubi->peb_buf1, patterns[i], ubi->peb_size);
4212d262c48SKyungmin Park 		if (err == 0) {
4222d262c48SKyungmin Park 			ubi_err("pattern %x checking failed for PEB %d",
4232d262c48SKyungmin Park 				patterns[i], pnum);
4242d262c48SKyungmin Park 			err = -EIO;
4252d262c48SKyungmin Park 			goto out;
4262d262c48SKyungmin Park 		}
4272d262c48SKyungmin Park 	}
4282d262c48SKyungmin Park 
4292d262c48SKyungmin Park 	err = patt_count;
4302d262c48SKyungmin Park 
4312d262c48SKyungmin Park out:
4322d262c48SKyungmin Park 	mutex_unlock(&ubi->buf_mutex);
4332d262c48SKyungmin Park 	if (err == UBI_IO_BITFLIPS || err == -EBADMSG) {
4342d262c48SKyungmin Park 		/*
4352d262c48SKyungmin Park 		 * If a bit-flip or data integrity error was detected, the test
4362d262c48SKyungmin Park 		 * has not passed because it happened on a freshly erased
4372d262c48SKyungmin Park 		 * physical eraseblock which means something is wrong with it.
4382d262c48SKyungmin Park 		 */
4392d262c48SKyungmin Park 		ubi_err("read problems on freshly erased PEB %d, must be bad",
4402d262c48SKyungmin Park 			pnum);
4412d262c48SKyungmin Park 		err = -EIO;
4422d262c48SKyungmin Park 	}
4432d262c48SKyungmin Park 	return err;
4442d262c48SKyungmin Park }
4452d262c48SKyungmin Park 
4462d262c48SKyungmin Park /**
4472d262c48SKyungmin Park  * ubi_io_sync_erase - synchronously erase a physical eraseblock.
4482d262c48SKyungmin Park  * @ubi: UBI device description object
4492d262c48SKyungmin Park  * @pnum: physical eraseblock number to erase
4502d262c48SKyungmin Park  * @torture: if this physical eraseblock has to be tortured
4512d262c48SKyungmin Park  *
4522d262c48SKyungmin Park  * This function synchronously erases physical eraseblock @pnum. If @torture
4532d262c48SKyungmin Park  * flag is not zero, the physical eraseblock is checked by means of writing
4542d262c48SKyungmin Park  * different patterns to it and reading them back. If the torturing is enabled,
4552d262c48SKyungmin Park  * the physical eraseblock is erased more then once.
4562d262c48SKyungmin Park  *
4572d262c48SKyungmin Park  * This function returns the number of erasures made in case of success, %-EIO
4582d262c48SKyungmin Park  * if the erasure failed or the torturing test failed, and other negative error
4592d262c48SKyungmin Park  * codes in case of other errors. Note, %-EIO means that the physical
4602d262c48SKyungmin Park  * eraseblock is bad.
4612d262c48SKyungmin Park  */
4622d262c48SKyungmin Park int ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture)
4632d262c48SKyungmin Park {
4642d262c48SKyungmin Park 	int err, ret = 0;
4652d262c48SKyungmin Park 
4662d262c48SKyungmin Park 	ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
4672d262c48SKyungmin Park 
4682d262c48SKyungmin Park 	err = paranoid_check_not_bad(ubi, pnum);
4692d262c48SKyungmin Park 	if (err != 0)
4702d262c48SKyungmin Park 		return err > 0 ? -EINVAL : err;
4712d262c48SKyungmin Park 
4722d262c48SKyungmin Park 	if (ubi->ro_mode) {
4732d262c48SKyungmin Park 		ubi_err("read-only mode");
4742d262c48SKyungmin Park 		return -EROFS;
4752d262c48SKyungmin Park 	}
4762d262c48SKyungmin Park 
4772d262c48SKyungmin Park 	if (torture) {
4782d262c48SKyungmin Park 		ret = torture_peb(ubi, pnum);
4792d262c48SKyungmin Park 		if (ret < 0)
4802d262c48SKyungmin Park 			return ret;
4812d262c48SKyungmin Park 	}
4822d262c48SKyungmin Park 
4832d262c48SKyungmin Park 	err = do_sync_erase(ubi, pnum);
4842d262c48SKyungmin Park 	if (err)
4852d262c48SKyungmin Park 		return err;
4862d262c48SKyungmin Park 
4872d262c48SKyungmin Park 	return ret + 1;
4882d262c48SKyungmin Park }
4892d262c48SKyungmin Park 
4902d262c48SKyungmin Park /**
4912d262c48SKyungmin Park  * ubi_io_is_bad - check if a physical eraseblock is bad.
4922d262c48SKyungmin Park  * @ubi: UBI device description object
4932d262c48SKyungmin Park  * @pnum: the physical eraseblock number to check
4942d262c48SKyungmin Park  *
4952d262c48SKyungmin Park  * This function returns a positive number if the physical eraseblock is bad,
4962d262c48SKyungmin Park  * zero if not, and a negative error code if an error occurred.
4972d262c48SKyungmin Park  */
4982d262c48SKyungmin Park int ubi_io_is_bad(const struct ubi_device *ubi, int pnum)
4992d262c48SKyungmin Park {
5002d262c48SKyungmin Park 	struct mtd_info *mtd = ubi->mtd;
5012d262c48SKyungmin Park 
5022d262c48SKyungmin Park 	ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
5032d262c48SKyungmin Park 
5042d262c48SKyungmin Park 	if (ubi->bad_allowed) {
5052d262c48SKyungmin Park 		int ret;
5062d262c48SKyungmin Park 
507dfe64e2cSSergey Lapin 		ret = mtd_block_isbad(mtd, (loff_t)pnum * ubi->peb_size);
5082d262c48SKyungmin Park 		if (ret < 0)
5092d262c48SKyungmin Park 			ubi_err("error %d while checking if PEB %d is bad",
5102d262c48SKyungmin Park 				ret, pnum);
5112d262c48SKyungmin Park 		else if (ret)
5122d262c48SKyungmin Park 			dbg_io("PEB %d is bad", pnum);
5132d262c48SKyungmin Park 		return ret;
5142d262c48SKyungmin Park 	}
5152d262c48SKyungmin Park 
5162d262c48SKyungmin Park 	return 0;
5172d262c48SKyungmin Park }
5182d262c48SKyungmin Park 
5192d262c48SKyungmin Park /**
5202d262c48SKyungmin Park  * ubi_io_mark_bad - mark a physical eraseblock as bad.
5212d262c48SKyungmin Park  * @ubi: UBI device description object
5222d262c48SKyungmin Park  * @pnum: the physical eraseblock number to mark
5232d262c48SKyungmin Park  *
5242d262c48SKyungmin Park  * This function returns zero in case of success and a negative error code in
5252d262c48SKyungmin Park  * case of failure.
5262d262c48SKyungmin Park  */
5272d262c48SKyungmin Park int ubi_io_mark_bad(const struct ubi_device *ubi, int pnum)
5282d262c48SKyungmin Park {
5292d262c48SKyungmin Park 	int err;
5302d262c48SKyungmin Park 	struct mtd_info *mtd = ubi->mtd;
5312d262c48SKyungmin Park 
5322d262c48SKyungmin Park 	ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
5332d262c48SKyungmin Park 
5342d262c48SKyungmin Park 	if (ubi->ro_mode) {
5352d262c48SKyungmin Park 		ubi_err("read-only mode");
5362d262c48SKyungmin Park 		return -EROFS;
5372d262c48SKyungmin Park 	}
5382d262c48SKyungmin Park 
5392d262c48SKyungmin Park 	if (!ubi->bad_allowed)
5402d262c48SKyungmin Park 		return 0;
5412d262c48SKyungmin Park 
542dfe64e2cSSergey Lapin 	err = mtd_block_markbad(mtd, (loff_t)pnum * ubi->peb_size);
5432d262c48SKyungmin Park 	if (err)
5442d262c48SKyungmin Park 		ubi_err("cannot mark PEB %d bad, error %d", pnum, err);
5452d262c48SKyungmin Park 	return err;
5462d262c48SKyungmin Park }
5472d262c48SKyungmin Park 
5482d262c48SKyungmin Park /**
5492d262c48SKyungmin Park  * validate_ec_hdr - validate an erase counter header.
5502d262c48SKyungmin Park  * @ubi: UBI device description object
5512d262c48SKyungmin Park  * @ec_hdr: the erase counter header to check
5522d262c48SKyungmin Park  *
5532d262c48SKyungmin Park  * This function returns zero if the erase counter header is OK, and %1 if
5542d262c48SKyungmin Park  * not.
5552d262c48SKyungmin Park  */
5562d262c48SKyungmin Park static int validate_ec_hdr(const struct ubi_device *ubi,
5572d262c48SKyungmin Park 			   const struct ubi_ec_hdr *ec_hdr)
5582d262c48SKyungmin Park {
5592d262c48SKyungmin Park 	long long ec;
5602d262c48SKyungmin Park 	int vid_hdr_offset, leb_start;
5612d262c48SKyungmin Park 
5622d262c48SKyungmin Park 	ec = be64_to_cpu(ec_hdr->ec);
5632d262c48SKyungmin Park 	vid_hdr_offset = be32_to_cpu(ec_hdr->vid_hdr_offset);
5642d262c48SKyungmin Park 	leb_start = be32_to_cpu(ec_hdr->data_offset);
5652d262c48SKyungmin Park 
5662d262c48SKyungmin Park 	if (ec_hdr->version != UBI_VERSION) {
5672d262c48SKyungmin Park 		ubi_err("node with incompatible UBI version found: "
5682d262c48SKyungmin Park 			"this UBI version is %d, image version is %d",
5692d262c48SKyungmin Park 			UBI_VERSION, (int)ec_hdr->version);
5702d262c48SKyungmin Park 		goto bad;
5712d262c48SKyungmin Park 	}
5722d262c48SKyungmin Park 
5732d262c48SKyungmin Park 	if (vid_hdr_offset != ubi->vid_hdr_offset) {
5742d262c48SKyungmin Park 		ubi_err("bad VID header offset %d, expected %d",
5752d262c48SKyungmin Park 			vid_hdr_offset, ubi->vid_hdr_offset);
5762d262c48SKyungmin Park 		goto bad;
5772d262c48SKyungmin Park 	}
5782d262c48SKyungmin Park 
5792d262c48SKyungmin Park 	if (leb_start != ubi->leb_start) {
5802d262c48SKyungmin Park 		ubi_err("bad data offset %d, expected %d",
5812d262c48SKyungmin Park 			leb_start, ubi->leb_start);
5822d262c48SKyungmin Park 		goto bad;
5832d262c48SKyungmin Park 	}
5842d262c48SKyungmin Park 
5852d262c48SKyungmin Park 	if (ec < 0 || ec > UBI_MAX_ERASECOUNTER) {
5862d262c48SKyungmin Park 		ubi_err("bad erase counter %lld", ec);
5872d262c48SKyungmin Park 		goto bad;
5882d262c48SKyungmin Park 	}
5892d262c48SKyungmin Park 
5902d262c48SKyungmin Park 	return 0;
5912d262c48SKyungmin Park 
5922d262c48SKyungmin Park bad:
5932d262c48SKyungmin Park 	ubi_err("bad EC header");
5942d262c48SKyungmin Park 	ubi_dbg_dump_ec_hdr(ec_hdr);
5952d262c48SKyungmin Park 	ubi_dbg_dump_stack();
5962d262c48SKyungmin Park 	return 1;
5972d262c48SKyungmin Park }
5982d262c48SKyungmin Park 
5992d262c48SKyungmin Park /**
6002d262c48SKyungmin Park  * ubi_io_read_ec_hdr - read and check an erase counter header.
6012d262c48SKyungmin Park  * @ubi: UBI device description object
6022d262c48SKyungmin Park  * @pnum: physical eraseblock to read from
6032d262c48SKyungmin Park  * @ec_hdr: a &struct ubi_ec_hdr object where to store the read erase counter
6042d262c48SKyungmin Park  * header
6052d262c48SKyungmin Park  * @verbose: be verbose if the header is corrupted or was not found
6062d262c48SKyungmin Park  *
6072d262c48SKyungmin Park  * This function reads erase counter header from physical eraseblock @pnum and
6082d262c48SKyungmin Park  * stores it in @ec_hdr. This function also checks CRC checksum of the read
6092d262c48SKyungmin Park  * erase counter header. The following codes may be returned:
6102d262c48SKyungmin Park  *
6112d262c48SKyungmin Park  * o %0 if the CRC checksum is correct and the header was successfully read;
6122d262c48SKyungmin Park  * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected
6132d262c48SKyungmin Park  *   and corrected by the flash driver; this is harmless but may indicate that
6142d262c48SKyungmin Park  *   this eraseblock may become bad soon (but may be not);
6152d262c48SKyungmin Park  * o %UBI_IO_BAD_EC_HDR if the erase counter header is corrupted (a CRC error);
6162d262c48SKyungmin Park  * o %UBI_IO_PEB_EMPTY if the physical eraseblock is empty;
6172d262c48SKyungmin Park  * o a negative error code in case of failure.
6182d262c48SKyungmin Park  */
6192d262c48SKyungmin Park int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
6202d262c48SKyungmin Park 		       struct ubi_ec_hdr *ec_hdr, int verbose)
6212d262c48SKyungmin Park {
6222d262c48SKyungmin Park 	int err, read_err = 0;
6232d262c48SKyungmin Park 	uint32_t crc, magic, hdr_crc;
6242d262c48SKyungmin Park 
6252d262c48SKyungmin Park 	dbg_io("read EC header from PEB %d", pnum);
6262d262c48SKyungmin Park 	ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
6272d262c48SKyungmin Park 	if (UBI_IO_DEBUG)
6282d262c48SKyungmin Park 		verbose = 1;
6292d262c48SKyungmin Park 
6302d262c48SKyungmin Park 	err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE);
6312d262c48SKyungmin Park 	if (err) {
6322d262c48SKyungmin Park 		if (err != UBI_IO_BITFLIPS && err != -EBADMSG)
6332d262c48SKyungmin Park 			return err;
6342d262c48SKyungmin Park 
6352d262c48SKyungmin Park 		/*
6362d262c48SKyungmin Park 		 * We read all the data, but either a correctable bit-flip
6372d262c48SKyungmin Park 		 * occurred, or MTD reported about some data integrity error,
6382d262c48SKyungmin Park 		 * like an ECC error in case of NAND. The former is harmless,
6392d262c48SKyungmin Park 		 * the later may mean that the read data is corrupted. But we
6402d262c48SKyungmin Park 		 * have a CRC check-sum and we will detect this. If the EC
6412d262c48SKyungmin Park 		 * header is still OK, we just report this as there was a
6422d262c48SKyungmin Park 		 * bit-flip.
6432d262c48SKyungmin Park 		 */
6442d262c48SKyungmin Park 		read_err = err;
6452d262c48SKyungmin Park 	}
6462d262c48SKyungmin Park 
6472d262c48SKyungmin Park 	magic = be32_to_cpu(ec_hdr->magic);
6482d262c48SKyungmin Park 	if (magic != UBI_EC_HDR_MAGIC) {
6492d262c48SKyungmin Park 		/*
6502d262c48SKyungmin Park 		 * The magic field is wrong. Let's check if we have read all
6512d262c48SKyungmin Park 		 * 0xFF. If yes, this physical eraseblock is assumed to be
6522d262c48SKyungmin Park 		 * empty.
6532d262c48SKyungmin Park 		 *
6542d262c48SKyungmin Park 		 * But if there was a read error, we do not test it for all
6552d262c48SKyungmin Park 		 * 0xFFs. Even if it does contain all 0xFFs, this error
6562d262c48SKyungmin Park 		 * indicates that something is still wrong with this physical
6572d262c48SKyungmin Park 		 * eraseblock and we anyway cannot treat it as empty.
6582d262c48SKyungmin Park 		 */
6592d262c48SKyungmin Park 		if (read_err != -EBADMSG &&
6602d262c48SKyungmin Park 		    check_pattern(ec_hdr, 0xFF, UBI_EC_HDR_SIZE)) {
6612d262c48SKyungmin Park 			/* The physical eraseblock is supposedly empty */
6622d262c48SKyungmin Park 
6632d262c48SKyungmin Park 			/*
6642d262c48SKyungmin Park 			 * The below is just a paranoid check, it has to be
6652d262c48SKyungmin Park 			 * compiled out if paranoid checks are disabled.
6662d262c48SKyungmin Park 			 */
6672d262c48SKyungmin Park 			err = paranoid_check_all_ff(ubi, pnum, 0,
6682d262c48SKyungmin Park 						    ubi->peb_size);
6692d262c48SKyungmin Park 			if (err)
6702d262c48SKyungmin Park 				return err > 0 ? UBI_IO_BAD_EC_HDR : err;
6712d262c48SKyungmin Park 
6722d262c48SKyungmin Park 			if (verbose)
6732d262c48SKyungmin Park 				ubi_warn("no EC header found at PEB %d, "
6742d262c48SKyungmin Park 					 "only 0xFF bytes", pnum);
6752d262c48SKyungmin Park 			return UBI_IO_PEB_EMPTY;
6762d262c48SKyungmin Park 		}
6772d262c48SKyungmin Park 
6782d262c48SKyungmin Park 		/*
6792d262c48SKyungmin Park 		 * This is not a valid erase counter header, and these are not
6802d262c48SKyungmin Park 		 * 0xFF bytes. Report that the header is corrupted.
6812d262c48SKyungmin Park 		 */
6822d262c48SKyungmin Park 		if (verbose) {
6832d262c48SKyungmin Park 			ubi_warn("bad magic number at PEB %d: %08x instead of "
6842d262c48SKyungmin Park 				 "%08x", pnum, magic, UBI_EC_HDR_MAGIC);
6852d262c48SKyungmin Park 			ubi_dbg_dump_ec_hdr(ec_hdr);
6862d262c48SKyungmin Park 		}
6872d262c48SKyungmin Park 		return UBI_IO_BAD_EC_HDR;
6882d262c48SKyungmin Park 	}
6892d262c48SKyungmin Park 
6902d262c48SKyungmin Park 	crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
6912d262c48SKyungmin Park 	hdr_crc = be32_to_cpu(ec_hdr->hdr_crc);
6922d262c48SKyungmin Park 
6932d262c48SKyungmin Park 	if (hdr_crc != crc) {
6942d262c48SKyungmin Park 		if (verbose) {
6952d262c48SKyungmin Park 			ubi_warn("bad EC header CRC at PEB %d, calculated %#08x,"
6962d262c48SKyungmin Park 				 " read %#08x", pnum, crc, hdr_crc);
6972d262c48SKyungmin Park 			ubi_dbg_dump_ec_hdr(ec_hdr);
6982d262c48SKyungmin Park 		}
6992d262c48SKyungmin Park 		return UBI_IO_BAD_EC_HDR;
7002d262c48SKyungmin Park 	}
7012d262c48SKyungmin Park 
7022d262c48SKyungmin Park 	/* And of course validate what has just been read from the media */
7032d262c48SKyungmin Park 	err = validate_ec_hdr(ubi, ec_hdr);
7042d262c48SKyungmin Park 	if (err) {
7052d262c48SKyungmin Park 		ubi_err("validation failed for PEB %d", pnum);
7062d262c48SKyungmin Park 		return -EINVAL;
7072d262c48SKyungmin Park 	}
7082d262c48SKyungmin Park 
7092d262c48SKyungmin Park 	return read_err ? UBI_IO_BITFLIPS : 0;
7102d262c48SKyungmin Park }
7112d262c48SKyungmin Park 
7122d262c48SKyungmin Park /**
7132d262c48SKyungmin Park  * ubi_io_write_ec_hdr - write an erase counter header.
7142d262c48SKyungmin Park  * @ubi: UBI device description object
7152d262c48SKyungmin Park  * @pnum: physical eraseblock to write to
7162d262c48SKyungmin Park  * @ec_hdr: the erase counter header to write
7172d262c48SKyungmin Park  *
7182d262c48SKyungmin Park  * This function writes erase counter header described by @ec_hdr to physical
7192d262c48SKyungmin Park  * eraseblock @pnum. It also fills most fields of @ec_hdr before writing, so
7202d262c48SKyungmin Park  * the caller do not have to fill them. Callers must only fill the @ec_hdr->ec
7212d262c48SKyungmin Park  * field.
7222d262c48SKyungmin Park  *
7232d262c48SKyungmin Park  * This function returns zero in case of success and a negative error code in
7242d262c48SKyungmin Park  * case of failure. If %-EIO is returned, the physical eraseblock most probably
7252d262c48SKyungmin Park  * went bad.
7262d262c48SKyungmin Park  */
7272d262c48SKyungmin Park int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum,
7282d262c48SKyungmin Park 			struct ubi_ec_hdr *ec_hdr)
7292d262c48SKyungmin Park {
7302d262c48SKyungmin Park 	int err;
7312d262c48SKyungmin Park 	uint32_t crc;
7322d262c48SKyungmin Park 
7332d262c48SKyungmin Park 	dbg_io("write EC header to PEB %d", pnum);
7342d262c48SKyungmin Park 	ubi_assert(pnum >= 0 &&  pnum < ubi->peb_count);
7352d262c48SKyungmin Park 
7362d262c48SKyungmin Park 	ec_hdr->magic = cpu_to_be32(UBI_EC_HDR_MAGIC);
7372d262c48SKyungmin Park 	ec_hdr->version = UBI_VERSION;
7382d262c48SKyungmin Park 	ec_hdr->vid_hdr_offset = cpu_to_be32(ubi->vid_hdr_offset);
7392d262c48SKyungmin Park 	ec_hdr->data_offset = cpu_to_be32(ubi->leb_start);
7402d262c48SKyungmin Park 	crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
7412d262c48SKyungmin Park 	ec_hdr->hdr_crc = cpu_to_be32(crc);
7422d262c48SKyungmin Park 
7432d262c48SKyungmin Park 	err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr);
7442d262c48SKyungmin Park 	if (err)
7452d262c48SKyungmin Park 		return -EINVAL;
7462d262c48SKyungmin Park 
7472d262c48SKyungmin Park 	err = ubi_io_write(ubi, ec_hdr, pnum, 0, ubi->ec_hdr_alsize);
7482d262c48SKyungmin Park 	return err;
7492d262c48SKyungmin Park }
7502d262c48SKyungmin Park 
7512d262c48SKyungmin Park /**
7522d262c48SKyungmin Park  * validate_vid_hdr - validate a volume identifier header.
7532d262c48SKyungmin Park  * @ubi: UBI device description object
7542d262c48SKyungmin Park  * @vid_hdr: the volume identifier header to check
7552d262c48SKyungmin Park  *
7562d262c48SKyungmin Park  * This function checks that data stored in the volume identifier header
7572d262c48SKyungmin Park  * @vid_hdr. Returns zero if the VID header is OK and %1 if not.
7582d262c48SKyungmin Park  */
7592d262c48SKyungmin Park static int validate_vid_hdr(const struct ubi_device *ubi,
7602d262c48SKyungmin Park 			    const struct ubi_vid_hdr *vid_hdr)
7612d262c48SKyungmin Park {
7622d262c48SKyungmin Park 	int vol_type = vid_hdr->vol_type;
7632d262c48SKyungmin Park 	int copy_flag = vid_hdr->copy_flag;
7642d262c48SKyungmin Park 	int vol_id = be32_to_cpu(vid_hdr->vol_id);
7652d262c48SKyungmin Park 	int lnum = be32_to_cpu(vid_hdr->lnum);
7662d262c48SKyungmin Park 	int compat = vid_hdr->compat;
7672d262c48SKyungmin Park 	int data_size = be32_to_cpu(vid_hdr->data_size);
7682d262c48SKyungmin Park 	int used_ebs = be32_to_cpu(vid_hdr->used_ebs);
7692d262c48SKyungmin Park 	int data_pad = be32_to_cpu(vid_hdr->data_pad);
7702d262c48SKyungmin Park 	int data_crc = be32_to_cpu(vid_hdr->data_crc);
7712d262c48SKyungmin Park 	int usable_leb_size = ubi->leb_size - data_pad;
7722d262c48SKyungmin Park 
7732d262c48SKyungmin Park 	if (copy_flag != 0 && copy_flag != 1) {
7742d262c48SKyungmin Park 		dbg_err("bad copy_flag");
7752d262c48SKyungmin Park 		goto bad;
7762d262c48SKyungmin Park 	}
7772d262c48SKyungmin Park 
7782d262c48SKyungmin Park 	if (vol_id < 0 || lnum < 0 || data_size < 0 || used_ebs < 0 ||
7792d262c48SKyungmin Park 	    data_pad < 0) {
7802d262c48SKyungmin Park 		dbg_err("negative values");
7812d262c48SKyungmin Park 		goto bad;
7822d262c48SKyungmin Park 	}
7832d262c48SKyungmin Park 
7842d262c48SKyungmin Park 	if (vol_id >= UBI_MAX_VOLUMES && vol_id < UBI_INTERNAL_VOL_START) {
7852d262c48SKyungmin Park 		dbg_err("bad vol_id");
7862d262c48SKyungmin Park 		goto bad;
7872d262c48SKyungmin Park 	}
7882d262c48SKyungmin Park 
7892d262c48SKyungmin Park 	if (vol_id < UBI_INTERNAL_VOL_START && compat != 0) {
7902d262c48SKyungmin Park 		dbg_err("bad compat");
7912d262c48SKyungmin Park 		goto bad;
7922d262c48SKyungmin Park 	}
7932d262c48SKyungmin Park 
7942d262c48SKyungmin Park 	if (vol_id >= UBI_INTERNAL_VOL_START && compat != UBI_COMPAT_DELETE &&
7952d262c48SKyungmin Park 	    compat != UBI_COMPAT_RO && compat != UBI_COMPAT_PRESERVE &&
7962d262c48SKyungmin Park 	    compat != UBI_COMPAT_REJECT) {
7972d262c48SKyungmin Park 		dbg_err("bad compat");
7982d262c48SKyungmin Park 		goto bad;
7992d262c48SKyungmin Park 	}
8002d262c48SKyungmin Park 
8012d262c48SKyungmin Park 	if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) {
8022d262c48SKyungmin Park 		dbg_err("bad vol_type");
8032d262c48SKyungmin Park 		goto bad;
8042d262c48SKyungmin Park 	}
8052d262c48SKyungmin Park 
8062d262c48SKyungmin Park 	if (data_pad >= ubi->leb_size / 2) {
8072d262c48SKyungmin Park 		dbg_err("bad data_pad");
8082d262c48SKyungmin Park 		goto bad;
8092d262c48SKyungmin Park 	}
8102d262c48SKyungmin Park 
8112d262c48SKyungmin Park 	if (vol_type == UBI_VID_STATIC) {
8122d262c48SKyungmin Park 		/*
8132d262c48SKyungmin Park 		 * Although from high-level point of view static volumes may
8142d262c48SKyungmin Park 		 * contain zero bytes of data, but no VID headers can contain
8152d262c48SKyungmin Park 		 * zero at these fields, because they empty volumes do not have
8162d262c48SKyungmin Park 		 * mapped logical eraseblocks.
8172d262c48SKyungmin Park 		 */
8182d262c48SKyungmin Park 		if (used_ebs == 0) {
8192d262c48SKyungmin Park 			dbg_err("zero used_ebs");
8202d262c48SKyungmin Park 			goto bad;
8212d262c48SKyungmin Park 		}
8222d262c48SKyungmin Park 		if (data_size == 0) {
8232d262c48SKyungmin Park 			dbg_err("zero data_size");
8242d262c48SKyungmin Park 			goto bad;
8252d262c48SKyungmin Park 		}
8262d262c48SKyungmin Park 		if (lnum < used_ebs - 1) {
8272d262c48SKyungmin Park 			if (data_size != usable_leb_size) {
8282d262c48SKyungmin Park 				dbg_err("bad data_size");
8292d262c48SKyungmin Park 				goto bad;
8302d262c48SKyungmin Park 			}
8312d262c48SKyungmin Park 		} else if (lnum == used_ebs - 1) {
8322d262c48SKyungmin Park 			if (data_size == 0) {
8332d262c48SKyungmin Park 				dbg_err("bad data_size at last LEB");
8342d262c48SKyungmin Park 				goto bad;
8352d262c48SKyungmin Park 			}
8362d262c48SKyungmin Park 		} else {
8372d262c48SKyungmin Park 			dbg_err("too high lnum");
8382d262c48SKyungmin Park 			goto bad;
8392d262c48SKyungmin Park 		}
8402d262c48SKyungmin Park 	} else {
8412d262c48SKyungmin Park 		if (copy_flag == 0) {
8422d262c48SKyungmin Park 			if (data_crc != 0) {
8432d262c48SKyungmin Park 				dbg_err("non-zero data CRC");
8442d262c48SKyungmin Park 				goto bad;
8452d262c48SKyungmin Park 			}
8462d262c48SKyungmin Park 			if (data_size != 0) {
8472d262c48SKyungmin Park 				dbg_err("non-zero data_size");
8482d262c48SKyungmin Park 				goto bad;
8492d262c48SKyungmin Park 			}
8502d262c48SKyungmin Park 		} else {
8512d262c48SKyungmin Park 			if (data_size == 0) {
8522d262c48SKyungmin Park 				dbg_err("zero data_size of copy");
8532d262c48SKyungmin Park 				goto bad;
8542d262c48SKyungmin Park 			}
8552d262c48SKyungmin Park 		}
8562d262c48SKyungmin Park 		if (used_ebs != 0) {
8572d262c48SKyungmin Park 			dbg_err("bad used_ebs");
8582d262c48SKyungmin Park 			goto bad;
8592d262c48SKyungmin Park 		}
8602d262c48SKyungmin Park 	}
8612d262c48SKyungmin Park 
8622d262c48SKyungmin Park 	return 0;
8632d262c48SKyungmin Park 
8642d262c48SKyungmin Park bad:
8652d262c48SKyungmin Park 	ubi_err("bad VID header");
8662d262c48SKyungmin Park 	ubi_dbg_dump_vid_hdr(vid_hdr);
8672d262c48SKyungmin Park 	ubi_dbg_dump_stack();
8682d262c48SKyungmin Park 	return 1;
8692d262c48SKyungmin Park }
8702d262c48SKyungmin Park 
8712d262c48SKyungmin Park /**
8722d262c48SKyungmin Park  * ubi_io_read_vid_hdr - read and check a volume identifier header.
8732d262c48SKyungmin Park  * @ubi: UBI device description object
8742d262c48SKyungmin Park  * @pnum: physical eraseblock number to read from
8752d262c48SKyungmin Park  * @vid_hdr: &struct ubi_vid_hdr object where to store the read volume
8762d262c48SKyungmin Park  * identifier header
8772d262c48SKyungmin Park  * @verbose: be verbose if the header is corrupted or wasn't found
8782d262c48SKyungmin Park  *
8792d262c48SKyungmin Park  * This function reads the volume identifier header from physical eraseblock
8802d262c48SKyungmin Park  * @pnum and stores it in @vid_hdr. It also checks CRC checksum of the read
8812d262c48SKyungmin Park  * volume identifier header. The following codes may be returned:
8822d262c48SKyungmin Park  *
8832d262c48SKyungmin Park  * o %0 if the CRC checksum is correct and the header was successfully read;
8842d262c48SKyungmin Park  * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected
8852d262c48SKyungmin Park  *   and corrected by the flash driver; this is harmless but may indicate that
8862d262c48SKyungmin Park  *   this eraseblock may become bad soon;
8872d262c48SKyungmin Park  * o %UBI_IO_BAD_VID_HRD if the volume identifier header is corrupted (a CRC
8882d262c48SKyungmin Park  *   error detected);
8892d262c48SKyungmin Park  * o %UBI_IO_PEB_FREE if the physical eraseblock is free (i.e., there is no VID
8902d262c48SKyungmin Park  *   header there);
8912d262c48SKyungmin Park  * o a negative error code in case of failure.
8922d262c48SKyungmin Park  */
8932d262c48SKyungmin Park int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
8942d262c48SKyungmin Park 			struct ubi_vid_hdr *vid_hdr, int verbose)
8952d262c48SKyungmin Park {
8962d262c48SKyungmin Park 	int err, read_err = 0;
8972d262c48SKyungmin Park 	uint32_t crc, magic, hdr_crc;
8982d262c48SKyungmin Park 	void *p;
8992d262c48SKyungmin Park 
9002d262c48SKyungmin Park 	dbg_io("read VID header from PEB %d", pnum);
9012d262c48SKyungmin Park 	ubi_assert(pnum >= 0 &&  pnum < ubi->peb_count);
9022d262c48SKyungmin Park 	if (UBI_IO_DEBUG)
9032d262c48SKyungmin Park 		verbose = 1;
9042d262c48SKyungmin Park 
9052d262c48SKyungmin Park 	p = (char *)vid_hdr - ubi->vid_hdr_shift;
9062d262c48SKyungmin Park 	err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset,
9072d262c48SKyungmin Park 			  ubi->vid_hdr_alsize);
9082d262c48SKyungmin Park 	if (err) {
9092d262c48SKyungmin Park 		if (err != UBI_IO_BITFLIPS && err != -EBADMSG)
9102d262c48SKyungmin Park 			return err;
9112d262c48SKyungmin Park 
9122d262c48SKyungmin Park 		/*
9132d262c48SKyungmin Park 		 * We read all the data, but either a correctable bit-flip
9142d262c48SKyungmin Park 		 * occurred, or MTD reported about some data integrity error,
9152d262c48SKyungmin Park 		 * like an ECC error in case of NAND. The former is harmless,
9162d262c48SKyungmin Park 		 * the later may mean the read data is corrupted. But we have a
9172d262c48SKyungmin Park 		 * CRC check-sum and we will identify this. If the VID header is
9182d262c48SKyungmin Park 		 * still OK, we just report this as there was a bit-flip.
9192d262c48SKyungmin Park 		 */
9202d262c48SKyungmin Park 		read_err = err;
9212d262c48SKyungmin Park 	}
9222d262c48SKyungmin Park 
9232d262c48SKyungmin Park 	magic = be32_to_cpu(vid_hdr->magic);
9242d262c48SKyungmin Park 	if (magic != UBI_VID_HDR_MAGIC) {
9252d262c48SKyungmin Park 		/*
9262d262c48SKyungmin Park 		 * If we have read all 0xFF bytes, the VID header probably does
9272d262c48SKyungmin Park 		 * not exist and the physical eraseblock is assumed to be free.
9282d262c48SKyungmin Park 		 *
9292d262c48SKyungmin Park 		 * But if there was a read error, we do not test the data for
9302d262c48SKyungmin Park 		 * 0xFFs. Even if it does contain all 0xFFs, this error
9312d262c48SKyungmin Park 		 * indicates that something is still wrong with this physical
9322d262c48SKyungmin Park 		 * eraseblock and it cannot be regarded as free.
9332d262c48SKyungmin Park 		 */
9342d262c48SKyungmin Park 		if (read_err != -EBADMSG &&
9352d262c48SKyungmin Park 		    check_pattern(vid_hdr, 0xFF, UBI_VID_HDR_SIZE)) {
9362d262c48SKyungmin Park 			/* The physical eraseblock is supposedly free */
9372d262c48SKyungmin Park 
9382d262c48SKyungmin Park 			/*
9392d262c48SKyungmin Park 			 * The below is just a paranoid check, it has to be
9402d262c48SKyungmin Park 			 * compiled out if paranoid checks are disabled.
9412d262c48SKyungmin Park 			 */
9422d262c48SKyungmin Park 			err = paranoid_check_all_ff(ubi, pnum, ubi->leb_start,
9432d262c48SKyungmin Park 						    ubi->leb_size);
9442d262c48SKyungmin Park 			if (err)
9452d262c48SKyungmin Park 				return err > 0 ? UBI_IO_BAD_VID_HDR : err;
9462d262c48SKyungmin Park 
9472d262c48SKyungmin Park 			if (verbose)
9482d262c48SKyungmin Park 				ubi_warn("no VID header found at PEB %d, "
9492d262c48SKyungmin Park 					 "only 0xFF bytes", pnum);
9502d262c48SKyungmin Park 			return UBI_IO_PEB_FREE;
9512d262c48SKyungmin Park 		}
9522d262c48SKyungmin Park 
9532d262c48SKyungmin Park 		/*
9542d262c48SKyungmin Park 		 * This is not a valid VID header, and these are not 0xFF
9552d262c48SKyungmin Park 		 * bytes. Report that the header is corrupted.
9562d262c48SKyungmin Park 		 */
9572d262c48SKyungmin Park 		if (verbose) {
9582d262c48SKyungmin Park 			ubi_warn("bad magic number at PEB %d: %08x instead of "
9592d262c48SKyungmin Park 				 "%08x", pnum, magic, UBI_VID_HDR_MAGIC);
9602d262c48SKyungmin Park 			ubi_dbg_dump_vid_hdr(vid_hdr);
9612d262c48SKyungmin Park 		}
9622d262c48SKyungmin Park 		return UBI_IO_BAD_VID_HDR;
9632d262c48SKyungmin Park 	}
9642d262c48SKyungmin Park 
9652d262c48SKyungmin Park 	crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC);
9662d262c48SKyungmin Park 	hdr_crc = be32_to_cpu(vid_hdr->hdr_crc);
9672d262c48SKyungmin Park 
9682d262c48SKyungmin Park 	if (hdr_crc != crc) {
9692d262c48SKyungmin Park 		if (verbose) {
9702d262c48SKyungmin Park 			ubi_warn("bad CRC at PEB %d, calculated %#08x, "
9712d262c48SKyungmin Park 				 "read %#08x", pnum, crc, hdr_crc);
9722d262c48SKyungmin Park 			ubi_dbg_dump_vid_hdr(vid_hdr);
9732d262c48SKyungmin Park 		}
9742d262c48SKyungmin Park 		return UBI_IO_BAD_VID_HDR;
9752d262c48SKyungmin Park 	}
9762d262c48SKyungmin Park 
9772d262c48SKyungmin Park 	/* Validate the VID header that we have just read */
9782d262c48SKyungmin Park 	err = validate_vid_hdr(ubi, vid_hdr);
9792d262c48SKyungmin Park 	if (err) {
9802d262c48SKyungmin Park 		ubi_err("validation failed for PEB %d", pnum);
9812d262c48SKyungmin Park 		return -EINVAL;
9822d262c48SKyungmin Park 	}
9832d262c48SKyungmin Park 
9842d262c48SKyungmin Park 	return read_err ? UBI_IO_BITFLIPS : 0;
9852d262c48SKyungmin Park }
9862d262c48SKyungmin Park 
9872d262c48SKyungmin Park /**
9882d262c48SKyungmin Park  * ubi_io_write_vid_hdr - write a volume identifier header.
9892d262c48SKyungmin Park  * @ubi: UBI device description object
9902d262c48SKyungmin Park  * @pnum: the physical eraseblock number to write to
9912d262c48SKyungmin Park  * @vid_hdr: the volume identifier header to write
9922d262c48SKyungmin Park  *
9932d262c48SKyungmin Park  * This function writes the volume identifier header described by @vid_hdr to
9942d262c48SKyungmin Park  * physical eraseblock @pnum. This function automatically fills the
9952d262c48SKyungmin Park  * @vid_hdr->magic and the @vid_hdr->version fields, as well as calculates
9962d262c48SKyungmin Park  * header CRC checksum and stores it at vid_hdr->hdr_crc.
9972d262c48SKyungmin Park  *
9982d262c48SKyungmin Park  * This function returns zero in case of success and a negative error code in
9992d262c48SKyungmin Park  * case of failure. If %-EIO is returned, the physical eraseblock probably went
10002d262c48SKyungmin Park  * bad.
10012d262c48SKyungmin Park  */
10022d262c48SKyungmin Park int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
10032d262c48SKyungmin Park 			 struct ubi_vid_hdr *vid_hdr)
10042d262c48SKyungmin Park {
10052d262c48SKyungmin Park 	int err;
10062d262c48SKyungmin Park 	uint32_t crc;
10072d262c48SKyungmin Park 	void *p;
10082d262c48SKyungmin Park 
10092d262c48SKyungmin Park 	dbg_io("write VID header to PEB %d", pnum);
10102d262c48SKyungmin Park 	ubi_assert(pnum >= 0 &&  pnum < ubi->peb_count);
10112d262c48SKyungmin Park 
10122d262c48SKyungmin Park 	err = paranoid_check_peb_ec_hdr(ubi, pnum);
10132d262c48SKyungmin Park 	if (err)
10142d262c48SKyungmin Park 		return err > 0 ? -EINVAL: err;
10152d262c48SKyungmin Park 
10162d262c48SKyungmin Park 	vid_hdr->magic = cpu_to_be32(UBI_VID_HDR_MAGIC);
10172d262c48SKyungmin Park 	vid_hdr->version = UBI_VERSION;
10182d262c48SKyungmin Park 	crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC);
10192d262c48SKyungmin Park 	vid_hdr->hdr_crc = cpu_to_be32(crc);
10202d262c48SKyungmin Park 
10212d262c48SKyungmin Park 	err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr);
10222d262c48SKyungmin Park 	if (err)
10232d262c48SKyungmin Park 		return -EINVAL;
10242d262c48SKyungmin Park 
10252d262c48SKyungmin Park 	p = (char *)vid_hdr - ubi->vid_hdr_shift;
10262d262c48SKyungmin Park 	err = ubi_io_write(ubi, p, pnum, ubi->vid_hdr_aloffset,
10272d262c48SKyungmin Park 			   ubi->vid_hdr_alsize);
10282d262c48SKyungmin Park 	return err;
10292d262c48SKyungmin Park }
10302d262c48SKyungmin Park 
10312d262c48SKyungmin Park #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
10322d262c48SKyungmin Park 
10332d262c48SKyungmin Park /**
10342d262c48SKyungmin Park  * paranoid_check_not_bad - ensure that a physical eraseblock is not bad.
10352d262c48SKyungmin Park  * @ubi: UBI device description object
10362d262c48SKyungmin Park  * @pnum: physical eraseblock number to check
10372d262c48SKyungmin Park  *
10382d262c48SKyungmin Park  * This function returns zero if the physical eraseblock is good, a positive
10392d262c48SKyungmin Park  * number if it is bad and a negative error code if an error occurred.
10402d262c48SKyungmin Park  */
10412d262c48SKyungmin Park static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum)
10422d262c48SKyungmin Park {
10432d262c48SKyungmin Park 	int err;
10442d262c48SKyungmin Park 
10452d262c48SKyungmin Park 	err = ubi_io_is_bad(ubi, pnum);
10462d262c48SKyungmin Park 	if (!err)
10472d262c48SKyungmin Park 		return err;
10482d262c48SKyungmin Park 
10492d262c48SKyungmin Park 	ubi_err("paranoid check failed for PEB %d", pnum);
10502d262c48SKyungmin Park 	ubi_dbg_dump_stack();
10512d262c48SKyungmin Park 	return err;
10522d262c48SKyungmin Park }
10532d262c48SKyungmin Park 
10542d262c48SKyungmin Park /**
10552d262c48SKyungmin Park  * paranoid_check_ec_hdr - check if an erase counter header is all right.
10562d262c48SKyungmin Park  * @ubi: UBI device description object
10572d262c48SKyungmin Park  * @pnum: physical eraseblock number the erase counter header belongs to
10582d262c48SKyungmin Park  * @ec_hdr: the erase counter header to check
10592d262c48SKyungmin Park  *
10602d262c48SKyungmin Park  * This function returns zero if the erase counter header contains valid
10612d262c48SKyungmin Park  * values, and %1 if not.
10622d262c48SKyungmin Park  */
10632d262c48SKyungmin Park static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum,
10642d262c48SKyungmin Park 				 const struct ubi_ec_hdr *ec_hdr)
10652d262c48SKyungmin Park {
10662d262c48SKyungmin Park 	int err;
10672d262c48SKyungmin Park 	uint32_t magic;
10682d262c48SKyungmin Park 
10692d262c48SKyungmin Park 	magic = be32_to_cpu(ec_hdr->magic);
10702d262c48SKyungmin Park 	if (magic != UBI_EC_HDR_MAGIC) {
10712d262c48SKyungmin Park 		ubi_err("bad magic %#08x, must be %#08x",
10722d262c48SKyungmin Park 			magic, UBI_EC_HDR_MAGIC);
10732d262c48SKyungmin Park 		goto fail;
10742d262c48SKyungmin Park 	}
10752d262c48SKyungmin Park 
10762d262c48SKyungmin Park 	err = validate_ec_hdr(ubi, ec_hdr);
10772d262c48SKyungmin Park 	if (err) {
10782d262c48SKyungmin Park 		ubi_err("paranoid check failed for PEB %d", pnum);
10792d262c48SKyungmin Park 		goto fail;
10802d262c48SKyungmin Park 	}
10812d262c48SKyungmin Park 
10822d262c48SKyungmin Park 	return 0;
10832d262c48SKyungmin Park 
10842d262c48SKyungmin Park fail:
10852d262c48SKyungmin Park 	ubi_dbg_dump_ec_hdr(ec_hdr);
10862d262c48SKyungmin Park 	ubi_dbg_dump_stack();
10872d262c48SKyungmin Park 	return 1;
10882d262c48SKyungmin Park }
10892d262c48SKyungmin Park 
10902d262c48SKyungmin Park /**
10912d262c48SKyungmin Park  * paranoid_check_peb_ec_hdr - check that the erase counter header of a
10922d262c48SKyungmin Park  * physical eraseblock is in-place and is all right.
10932d262c48SKyungmin Park  * @ubi: UBI device description object
10942d262c48SKyungmin Park  * @pnum: the physical eraseblock number to check
10952d262c48SKyungmin Park  *
10962d262c48SKyungmin Park  * This function returns zero if the erase counter header is all right, %1 if
10972d262c48SKyungmin Park  * not, and a negative error code if an error occurred.
10982d262c48SKyungmin Park  */
10992d262c48SKyungmin Park static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum)
11002d262c48SKyungmin Park {
11012d262c48SKyungmin Park 	int err;
11022d262c48SKyungmin Park 	uint32_t crc, hdr_crc;
11032d262c48SKyungmin Park 	struct ubi_ec_hdr *ec_hdr;
11042d262c48SKyungmin Park 
11052d262c48SKyungmin Park 	ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
11062d262c48SKyungmin Park 	if (!ec_hdr)
11072d262c48SKyungmin Park 		return -ENOMEM;
11082d262c48SKyungmin Park 
11092d262c48SKyungmin Park 	err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE);
11102d262c48SKyungmin Park 	if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG)
11112d262c48SKyungmin Park 		goto exit;
11122d262c48SKyungmin Park 
11132d262c48SKyungmin Park 	crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
11142d262c48SKyungmin Park 	hdr_crc = be32_to_cpu(ec_hdr->hdr_crc);
11152d262c48SKyungmin Park 	if (hdr_crc != crc) {
11162d262c48SKyungmin Park 		ubi_err("bad CRC, calculated %#08x, read %#08x", crc, hdr_crc);
11172d262c48SKyungmin Park 		ubi_err("paranoid check failed for PEB %d", pnum);
11182d262c48SKyungmin Park 		ubi_dbg_dump_ec_hdr(ec_hdr);
11192d262c48SKyungmin Park 		ubi_dbg_dump_stack();
11202d262c48SKyungmin Park 		err = 1;
11212d262c48SKyungmin Park 		goto exit;
11222d262c48SKyungmin Park 	}
11232d262c48SKyungmin Park 
11242d262c48SKyungmin Park 	err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr);
11252d262c48SKyungmin Park 
11262d262c48SKyungmin Park exit:
11272d262c48SKyungmin Park 	kfree(ec_hdr);
11282d262c48SKyungmin Park 	return err;
11292d262c48SKyungmin Park }
11302d262c48SKyungmin Park 
11312d262c48SKyungmin Park /**
11322d262c48SKyungmin Park  * paranoid_check_vid_hdr - check that a volume identifier header is all right.
11332d262c48SKyungmin Park  * @ubi: UBI device description object
11342d262c48SKyungmin Park  * @pnum: physical eraseblock number the volume identifier header belongs to
11352d262c48SKyungmin Park  * @vid_hdr: the volume identifier header to check
11362d262c48SKyungmin Park  *
11372d262c48SKyungmin Park  * This function returns zero if the volume identifier header is all right, and
11382d262c48SKyungmin Park  * %1 if not.
11392d262c48SKyungmin Park  */
11402d262c48SKyungmin Park static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum,
11412d262c48SKyungmin Park 				  const struct ubi_vid_hdr *vid_hdr)
11422d262c48SKyungmin Park {
11432d262c48SKyungmin Park 	int err;
11442d262c48SKyungmin Park 	uint32_t magic;
11452d262c48SKyungmin Park 
11462d262c48SKyungmin Park 	magic = be32_to_cpu(vid_hdr->magic);
11472d262c48SKyungmin Park 	if (magic != UBI_VID_HDR_MAGIC) {
11482d262c48SKyungmin Park 		ubi_err("bad VID header magic %#08x at PEB %d, must be %#08x",
11492d262c48SKyungmin Park 			magic, pnum, UBI_VID_HDR_MAGIC);
11502d262c48SKyungmin Park 		goto fail;
11512d262c48SKyungmin Park 	}
11522d262c48SKyungmin Park 
11532d262c48SKyungmin Park 	err = validate_vid_hdr(ubi, vid_hdr);
11542d262c48SKyungmin Park 	if (err) {
11552d262c48SKyungmin Park 		ubi_err("paranoid check failed for PEB %d", pnum);
11562d262c48SKyungmin Park 		goto fail;
11572d262c48SKyungmin Park 	}
11582d262c48SKyungmin Park 
11592d262c48SKyungmin Park 	return err;
11602d262c48SKyungmin Park 
11612d262c48SKyungmin Park fail:
11622d262c48SKyungmin Park 	ubi_err("paranoid check failed for PEB %d", pnum);
11632d262c48SKyungmin Park 	ubi_dbg_dump_vid_hdr(vid_hdr);
11642d262c48SKyungmin Park 	ubi_dbg_dump_stack();
11652d262c48SKyungmin Park 	return 1;
11662d262c48SKyungmin Park 
11672d262c48SKyungmin Park }
11682d262c48SKyungmin Park 
11692d262c48SKyungmin Park /**
11702d262c48SKyungmin Park  * paranoid_check_peb_vid_hdr - check that the volume identifier header of a
11712d262c48SKyungmin Park  * physical eraseblock is in-place and is all right.
11722d262c48SKyungmin Park  * @ubi: UBI device description object
11732d262c48SKyungmin Park  * @pnum: the physical eraseblock number to check
11742d262c48SKyungmin Park  *
11752d262c48SKyungmin Park  * This function returns zero if the volume identifier header is all right,
11762d262c48SKyungmin Park  * %1 if not, and a negative error code if an error occurred.
11772d262c48SKyungmin Park  */
11782d262c48SKyungmin Park static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum)
11792d262c48SKyungmin Park {
11802d262c48SKyungmin Park 	int err;
11812d262c48SKyungmin Park 	uint32_t crc, hdr_crc;
11822d262c48SKyungmin Park 	struct ubi_vid_hdr *vid_hdr;
11832d262c48SKyungmin Park 	void *p;
11842d262c48SKyungmin Park 
11852d262c48SKyungmin Park 	vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
11862d262c48SKyungmin Park 	if (!vid_hdr)
11872d262c48SKyungmin Park 		return -ENOMEM;
11882d262c48SKyungmin Park 
11892d262c48SKyungmin Park 	p = (char *)vid_hdr - ubi->vid_hdr_shift;
11902d262c48SKyungmin Park 	err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset,
11912d262c48SKyungmin Park 			  ubi->vid_hdr_alsize);
11922d262c48SKyungmin Park 	if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG)
11932d262c48SKyungmin Park 		goto exit;
11942d262c48SKyungmin Park 
11952d262c48SKyungmin Park 	crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_EC_HDR_SIZE_CRC);
11962d262c48SKyungmin Park 	hdr_crc = be32_to_cpu(vid_hdr->hdr_crc);
11972d262c48SKyungmin Park 	if (hdr_crc != crc) {
11982d262c48SKyungmin Park 		ubi_err("bad VID header CRC at PEB %d, calculated %#08x, "
11992d262c48SKyungmin Park 			"read %#08x", pnum, crc, hdr_crc);
12002d262c48SKyungmin Park 		ubi_err("paranoid check failed for PEB %d", pnum);
12012d262c48SKyungmin Park 		ubi_dbg_dump_vid_hdr(vid_hdr);
12022d262c48SKyungmin Park 		ubi_dbg_dump_stack();
12032d262c48SKyungmin Park 		err = 1;
12042d262c48SKyungmin Park 		goto exit;
12052d262c48SKyungmin Park 	}
12062d262c48SKyungmin Park 
12072d262c48SKyungmin Park 	err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr);
12082d262c48SKyungmin Park 
12092d262c48SKyungmin Park exit:
12102d262c48SKyungmin Park 	ubi_free_vid_hdr(ubi, vid_hdr);
12112d262c48SKyungmin Park 	return err;
12122d262c48SKyungmin Park }
12132d262c48SKyungmin Park 
12142d262c48SKyungmin Park /**
12152d262c48SKyungmin Park  * paranoid_check_all_ff - check that a region of flash is empty.
12162d262c48SKyungmin Park  * @ubi: UBI device description object
12172d262c48SKyungmin Park  * @pnum: the physical eraseblock number to check
12182d262c48SKyungmin Park  * @offset: the starting offset within the physical eraseblock to check
12192d262c48SKyungmin Park  * @len: the length of the region to check
12202d262c48SKyungmin Park  *
12212d262c48SKyungmin Park  * This function returns zero if only 0xFF bytes are present at offset
12222d262c48SKyungmin Park  * @offset of the physical eraseblock @pnum, %1 if not, and a negative error
12232d262c48SKyungmin Park  * code if an error occurred.
12242d262c48SKyungmin Park  */
12252d262c48SKyungmin Park static int paranoid_check_all_ff(struct ubi_device *ubi, int pnum, int offset,
12262d262c48SKyungmin Park 				 int len)
12272d262c48SKyungmin Park {
12282d262c48SKyungmin Park 	size_t read;
12292d262c48SKyungmin Park 	int err;
12302d262c48SKyungmin Park 	loff_t addr = (loff_t)pnum * ubi->peb_size + offset;
12312d262c48SKyungmin Park 
12322d262c48SKyungmin Park 	mutex_lock(&ubi->dbg_buf_mutex);
1233dfe64e2cSSergey Lapin 	err = mtd_read(ubi->mtd, addr, len, &read, ubi->dbg_peb_buf);
12342d262c48SKyungmin Park 	if (err && err != -EUCLEAN) {
12352d262c48SKyungmin Park 		ubi_err("error %d while reading %d bytes from PEB %d:%d, "
12362d262c48SKyungmin Park 			"read %zd bytes", err, len, pnum, offset, read);
12372d262c48SKyungmin Park 		goto error;
12382d262c48SKyungmin Park 	}
12392d262c48SKyungmin Park 
12402d262c48SKyungmin Park 	err = check_pattern(ubi->dbg_peb_buf, 0xFF, len);
12412d262c48SKyungmin Park 	if (err == 0) {
12422d262c48SKyungmin Park 		ubi_err("flash region at PEB %d:%d, length %d does not "
12432d262c48SKyungmin Park 			"contain all 0xFF bytes", pnum, offset, len);
12442d262c48SKyungmin Park 		goto fail;
12452d262c48SKyungmin Park 	}
12462d262c48SKyungmin Park 	mutex_unlock(&ubi->dbg_buf_mutex);
12472d262c48SKyungmin Park 
12482d262c48SKyungmin Park 	return 0;
12492d262c48SKyungmin Park 
12502d262c48SKyungmin Park fail:
12512d262c48SKyungmin Park 	ubi_err("paranoid check failed for PEB %d", pnum);
12522d262c48SKyungmin Park 	dbg_msg("hex dump of the %d-%d region", offset, offset + len);
12532d262c48SKyungmin Park 	print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
12542d262c48SKyungmin Park 		       ubi->dbg_peb_buf, len, 1);
12552d262c48SKyungmin Park 	err = 1;
12562d262c48SKyungmin Park error:
12572d262c48SKyungmin Park 	ubi_dbg_dump_stack();
12582d262c48SKyungmin Park 	mutex_unlock(&ubi->dbg_buf_mutex);
12592d262c48SKyungmin Park 	return err;
12602d262c48SKyungmin Park }
12612d262c48SKyungmin Park 
12622d262c48SKyungmin Park #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */
1263