xref: /openbmc/linux/drivers/mtd/ubi/io.c (revision 92d124f5)
1801c135cSArtem B. Bityutskiy /*
2801c135cSArtem B. Bityutskiy  * Copyright (c) International Business Machines Corp., 2006
3801c135cSArtem B. Bityutskiy  * Copyright (c) Nokia Corporation, 2006, 2007
4801c135cSArtem B. Bityutskiy  *
5801c135cSArtem B. Bityutskiy  * This program is free software; you can redistribute it and/or modify
6801c135cSArtem B. Bityutskiy  * it under the terms of the GNU General Public License as published by
7801c135cSArtem B. Bityutskiy  * the Free Software Foundation; either version 2 of the License, or
8801c135cSArtem B. Bityutskiy  * (at your option) any later version.
9801c135cSArtem B. Bityutskiy  *
10801c135cSArtem B. Bityutskiy  * This program is distributed in the hope that it will be useful,
11801c135cSArtem B. Bityutskiy  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12801c135cSArtem B. Bityutskiy  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13801c135cSArtem B. Bityutskiy  * the GNU General Public License for more details.
14801c135cSArtem B. Bityutskiy  *
15801c135cSArtem B. Bityutskiy  * You should have received a copy of the GNU General Public License
16801c135cSArtem B. Bityutskiy  * along with this program; if not, write to the Free Software
17801c135cSArtem B. Bityutskiy  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18801c135cSArtem B. Bityutskiy  *
19801c135cSArtem B. Bityutskiy  * Author: Artem Bityutskiy (Битюцкий Артём)
20801c135cSArtem B. Bityutskiy  */
21801c135cSArtem B. Bityutskiy 
22801c135cSArtem B. Bityutskiy /*
2385c6e6e2SArtem Bityutskiy  * UBI input/output sub-system.
24801c135cSArtem B. Bityutskiy  *
2585c6e6e2SArtem Bityutskiy  * This sub-system provides a uniform way to work with all kinds of the
2685c6e6e2SArtem Bityutskiy  * underlying MTD devices. It also implements handy functions for reading and
2785c6e6e2SArtem Bityutskiy  * writing UBI headers.
28801c135cSArtem B. Bityutskiy  *
29801c135cSArtem B. Bityutskiy  * We are trying to have a paranoid mindset and not to trust to what we read
3085c6e6e2SArtem Bityutskiy  * from the flash media in order to be more secure and robust. So this
3185c6e6e2SArtem Bityutskiy  * sub-system validates every single header it reads from the flash media.
32801c135cSArtem B. Bityutskiy  *
33801c135cSArtem B. Bityutskiy  * Some words about how the eraseblock headers are stored.
34801c135cSArtem B. Bityutskiy  *
35801c135cSArtem B. Bityutskiy  * The erase counter header is always stored at offset zero. By default, the
36801c135cSArtem B. Bityutskiy  * VID header is stored after the EC header at the closest aligned offset
37801c135cSArtem B. Bityutskiy  * (i.e. aligned to the minimum I/O unit size). Data starts next to the VID
38801c135cSArtem B. Bityutskiy  * header at the closest aligned offset. But this default layout may be
39801c135cSArtem B. Bityutskiy  * changed. For example, for different reasons (e.g., optimization) UBI may be
40801c135cSArtem B. Bityutskiy  * asked to put the VID header at further offset, and even at an unaligned
41801c135cSArtem B. Bityutskiy  * offset. Of course, if the offset of the VID header is unaligned, UBI adds
42801c135cSArtem B. Bityutskiy  * proper padding in front of it. Data offset may also be changed but it has to
43801c135cSArtem B. Bityutskiy  * be aligned.
44801c135cSArtem B. Bityutskiy  *
45801c135cSArtem B. Bityutskiy  * About minimal I/O units. In general, UBI assumes flash device model where
46801c135cSArtem B. Bityutskiy  * there is only one minimal I/O unit size. E.g., in case of NOR flash it is 1,
47801c135cSArtem B. Bityutskiy  * in case of NAND flash it is a NAND page, etc. This is reported by MTD in the
48801c135cSArtem B. Bityutskiy  * @ubi->mtd->writesize field. But as an exception, UBI admits of using another
49801c135cSArtem B. Bityutskiy  * (smaller) minimal I/O unit size for EC and VID headers to make it possible
50801c135cSArtem B. Bityutskiy  * to do different optimizations.
51801c135cSArtem B. Bityutskiy  *
52801c135cSArtem B. Bityutskiy  * This is extremely useful in case of NAND flashes which admit of several
53801c135cSArtem B. Bityutskiy  * write operations to one NAND page. In this case UBI can fit EC and VID
54801c135cSArtem B. Bityutskiy  * headers at one NAND page. Thus, UBI may use "sub-page" size as the minimal
55801c135cSArtem B. Bityutskiy  * I/O unit for the headers (the @ubi->hdrs_min_io_size field). But it still
56801c135cSArtem B. Bityutskiy  * reports NAND page size (@ubi->min_io_size) as a minimal I/O unit for the UBI
57801c135cSArtem B. Bityutskiy  * users.
58801c135cSArtem B. Bityutskiy  *
59801c135cSArtem B. Bityutskiy  * Example: some Samsung NANDs with 2KiB pages allow 4x 512-byte writes, so
60801c135cSArtem B. Bityutskiy  * although the minimal I/O unit is 2K, UBI uses 512 bytes for EC and VID
61801c135cSArtem B. Bityutskiy  * headers.
62801c135cSArtem B. Bityutskiy  *
63801c135cSArtem B. Bityutskiy  * Q: why not just to treat sub-page as a minimal I/O unit of this flash
64801c135cSArtem B. Bityutskiy  * device, e.g., make @ubi->min_io_size = 512 in the example above?
65801c135cSArtem B. Bityutskiy  *
66801c135cSArtem B. Bityutskiy  * A: because when writing a sub-page, MTD still writes a full 2K page but the
67be436f62SShinya Kuribayashi  * bytes which are not relevant to the sub-page are 0xFF. So, basically,
68be436f62SShinya Kuribayashi  * writing 4x512 sub-pages is 4 times slower than writing one 2KiB NAND page.
69be436f62SShinya Kuribayashi  * Thus, we prefer to use sub-pages only for EC and VID headers.
70801c135cSArtem B. Bityutskiy  *
71801c135cSArtem B. Bityutskiy  * As it was noted above, the VID header may start at a non-aligned offset.
72801c135cSArtem B. Bityutskiy  * For example, in case of a 2KiB page NAND flash with a 512 bytes sub-page,
73801c135cSArtem B. Bityutskiy  * the VID header may reside at offset 1984 which is the last 64 bytes of the
74801c135cSArtem B. Bityutskiy  * last sub-page (EC header is always at offset zero). This causes some
75801c135cSArtem B. Bityutskiy  * difficulties when reading and writing VID headers.
76801c135cSArtem B. Bityutskiy  *
77801c135cSArtem B. Bityutskiy  * Suppose we have a 64-byte buffer and we read a VID header at it. We change
78801c135cSArtem B. Bityutskiy  * the data and want to write this VID header out. As we can only write in
79801c135cSArtem B. Bityutskiy  * 512-byte chunks, we have to allocate one more buffer and copy our VID header
80801c135cSArtem B. Bityutskiy  * to offset 448 of this buffer.
81801c135cSArtem B. Bityutskiy  *
8285c6e6e2SArtem Bityutskiy  * The I/O sub-system does the following trick in order to avoid this extra
8385c6e6e2SArtem Bityutskiy  * copy. It always allocates a @ubi->vid_hdr_alsize bytes buffer for the VID
8485c6e6e2SArtem Bityutskiy  * header and returns a pointer to offset @ubi->vid_hdr_shift of this buffer.
8585c6e6e2SArtem Bityutskiy  * When the VID header is being written out, it shifts the VID header pointer
8685c6e6e2SArtem Bityutskiy  * back and writes the whole sub-page.
87801c135cSArtem B. Bityutskiy  */
88801c135cSArtem B. Bityutskiy 
89801c135cSArtem B. Bityutskiy #include <linux/crc32.h>
90801c135cSArtem B. Bityutskiy #include <linux/err.h>
915a0e3ad6STejun Heo #include <linux/slab.h>
92801c135cSArtem B. Bityutskiy #include "ubi.h"
93801c135cSArtem B. Bityutskiy 
9492d124f5SArtem Bityutskiy #ifdef CONFIG_MTD_UBI_DEBUG
95801c135cSArtem B. Bityutskiy static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum);
96801c135cSArtem B. Bityutskiy static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum);
97801c135cSArtem B. Bityutskiy static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum,
98801c135cSArtem B. Bityutskiy 				 const struct ubi_ec_hdr *ec_hdr);
99801c135cSArtem B. Bityutskiy static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum);
100801c135cSArtem B. Bityutskiy static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum,
101801c135cSArtem B. Bityutskiy 				  const struct ubi_vid_hdr *vid_hdr);
102801c135cSArtem B. Bityutskiy #else
103801c135cSArtem B. Bityutskiy #define paranoid_check_not_bad(ubi, pnum) 0
104801c135cSArtem B. Bityutskiy #define paranoid_check_peb_ec_hdr(ubi, pnum)  0
105801c135cSArtem B. Bityutskiy #define paranoid_check_ec_hdr(ubi, pnum, ec_hdr)  0
106801c135cSArtem B. Bityutskiy #define paranoid_check_peb_vid_hdr(ubi, pnum) 0
107801c135cSArtem B. Bityutskiy #define paranoid_check_vid_hdr(ubi, pnum, vid_hdr) 0
108801c135cSArtem B. Bityutskiy #endif
109801c135cSArtem B. Bityutskiy 
110801c135cSArtem B. Bityutskiy /**
111801c135cSArtem B. Bityutskiy  * ubi_io_read - read data from a physical eraseblock.
112801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
113801c135cSArtem B. Bityutskiy  * @buf: buffer where to store the read data
114801c135cSArtem B. Bityutskiy  * @pnum: physical eraseblock number to read from
115801c135cSArtem B. Bityutskiy  * @offset: offset within the physical eraseblock from where to read
116801c135cSArtem B. Bityutskiy  * @len: how many bytes to read
117801c135cSArtem B. Bityutskiy  *
118801c135cSArtem B. Bityutskiy  * This function reads data from offset @offset of physical eraseblock @pnum
119801c135cSArtem B. Bityutskiy  * and stores the read data in the @buf buffer. The following return codes are
120801c135cSArtem B. Bityutskiy  * possible:
121801c135cSArtem B. Bityutskiy  *
122801c135cSArtem B. Bityutskiy  * o %0 if all the requested data were successfully read;
123801c135cSArtem B. Bityutskiy  * o %UBI_IO_BITFLIPS if all the requested data were successfully read, but
124801c135cSArtem B. Bityutskiy  *   correctable bit-flips were detected; this is harmless but may indicate
125801c135cSArtem B. Bityutskiy  *   that this eraseblock may become bad soon (but do not have to);
12663b6c1edSArtem Bityutskiy  * o %-EBADMSG if the MTD subsystem reported about data integrity problems, for
12763b6c1edSArtem Bityutskiy  *   example it can be an ECC error in case of NAND; this most probably means
12863b6c1edSArtem Bityutskiy  *   that the data is corrupted;
129801c135cSArtem B. Bityutskiy  * o %-EIO if some I/O error occurred;
130801c135cSArtem B. Bityutskiy  * o other negative error codes in case of other errors.
131801c135cSArtem B. Bityutskiy  */
132801c135cSArtem B. Bityutskiy int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset,
133801c135cSArtem B. Bityutskiy 		int len)
134801c135cSArtem B. Bityutskiy {
135801c135cSArtem B. Bityutskiy 	int err, retries = 0;
136801c135cSArtem B. Bityutskiy 	size_t read;
137801c135cSArtem B. Bityutskiy 	loff_t addr;
138801c135cSArtem B. Bityutskiy 
139801c135cSArtem B. Bityutskiy 	dbg_io("read %d bytes from PEB %d:%d", len, pnum, offset);
140801c135cSArtem B. Bityutskiy 
141801c135cSArtem B. Bityutskiy 	ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
142801c135cSArtem B. Bityutskiy 	ubi_assert(offset >= 0 && offset + len <= ubi->peb_size);
143801c135cSArtem B. Bityutskiy 	ubi_assert(len > 0);
144801c135cSArtem B. Bityutskiy 
145801c135cSArtem B. Bityutskiy 	err = paranoid_check_not_bad(ubi, pnum);
146801c135cSArtem B. Bityutskiy 	if (err)
147adbf05e3SArtem Bityutskiy 		return err;
148801c135cSArtem B. Bityutskiy 
149276832d8SArtem Bityutskiy 	/*
150276832d8SArtem Bityutskiy 	 * Deliberately corrupt the buffer to improve robustness. Indeed, if we
151276832d8SArtem Bityutskiy 	 * do not do this, the following may happen:
152276832d8SArtem Bityutskiy 	 * 1. The buffer contains data from previous operation, e.g., read from
153276832d8SArtem Bityutskiy 	 *    another PEB previously. The data looks like expected, e.g., if we
154276832d8SArtem Bityutskiy 	 *    just do not read anything and return - the caller would not
155276832d8SArtem Bityutskiy 	 *    notice this. E.g., if we are reading a VID header, the buffer may
156276832d8SArtem Bityutskiy 	 *    contain a valid VID header from another PEB.
157276832d8SArtem Bityutskiy 	 * 2. The driver is buggy and returns us success or -EBADMSG or
158276832d8SArtem Bityutskiy 	 *    -EUCLEAN, but it does not actually put any data to the buffer.
159276832d8SArtem Bityutskiy 	 *
160276832d8SArtem Bityutskiy 	 * This may confuse UBI or upper layers - they may think the buffer
161276832d8SArtem Bityutskiy 	 * contains valid data while in fact it is just old data. This is
162276832d8SArtem Bityutskiy 	 * especially possible because UBI (and UBIFS) relies on CRC, and
163276832d8SArtem Bityutskiy 	 * treats data as correct even in case of ECC errors if the CRC is
164276832d8SArtem Bityutskiy 	 * correct.
165276832d8SArtem Bityutskiy 	 *
166276832d8SArtem Bityutskiy 	 * Try to prevent this situation by changing the first byte of the
167276832d8SArtem Bityutskiy 	 * buffer.
168276832d8SArtem Bityutskiy 	 */
169276832d8SArtem Bityutskiy 	*((uint8_t *)buf) ^= 0xFF;
170276832d8SArtem Bityutskiy 
171801c135cSArtem B. Bityutskiy 	addr = (loff_t)pnum * ubi->peb_size + offset;
172801c135cSArtem B. Bityutskiy retry:
173801c135cSArtem B. Bityutskiy 	err = ubi->mtd->read(ubi->mtd, addr, len, &read, buf);
174801c135cSArtem B. Bityutskiy 	if (err) {
175f5d5b1f8SArtem Bityutskiy 		const char *errstr = (err == -EBADMSG) ? " (ECC error)" : "";
1761a49af2cSArtem Bityutskiy 
177801c135cSArtem B. Bityutskiy 		if (err == -EUCLEAN) {
178801c135cSArtem B. Bityutskiy 			/*
179801c135cSArtem B. Bityutskiy 			 * -EUCLEAN is reported if there was a bit-flip which
180801c135cSArtem B. Bityutskiy 			 * was corrected, so this is harmless.
1818c1e6ee1SArtem Bityutskiy 			 *
1828c1e6ee1SArtem Bityutskiy 			 * We do not report about it here unless debugging is
1838c1e6ee1SArtem Bityutskiy 			 * enabled. A corresponding message will be printed
1848c1e6ee1SArtem Bityutskiy 			 * later, when it is has been scrubbed.
185801c135cSArtem B. Bityutskiy 			 */
1868c1e6ee1SArtem Bityutskiy 			dbg_msg("fixable bit-flip detected at PEB %d", pnum);
187801c135cSArtem B. Bityutskiy 			ubi_assert(len == read);
188801c135cSArtem B. Bityutskiy 			return UBI_IO_BITFLIPS;
189801c135cSArtem B. Bityutskiy 		}
190801c135cSArtem B. Bityutskiy 
191a87f29cbSArtem Bityutskiy 		if (retries++ < UBI_IO_RETRIES) {
1921a49af2cSArtem Bityutskiy 			dbg_io("error %d%s while reading %d bytes from PEB %d:%d,"
193801c135cSArtem B. Bityutskiy 			       " read only %zd bytes, retry",
1941a49af2cSArtem Bityutskiy 			       err, errstr, len, pnum, offset, read);
195801c135cSArtem B. Bityutskiy 			yield();
196801c135cSArtem B. Bityutskiy 			goto retry;
197801c135cSArtem B. Bityutskiy 		}
198801c135cSArtem B. Bityutskiy 
199f5d5b1f8SArtem Bityutskiy 		ubi_err("error %d%s while reading %d bytes from PEB %d:%d, "
2001a49af2cSArtem Bityutskiy 			"read %zd bytes", err, errstr, len, pnum, offset, read);
201801c135cSArtem B. Bityutskiy 		ubi_dbg_dump_stack();
2022362a53eSArtem Bityutskiy 
2032362a53eSArtem Bityutskiy 		/*
2042362a53eSArtem Bityutskiy 		 * The driver should never return -EBADMSG if it failed to read
2052362a53eSArtem Bityutskiy 		 * all the requested data. But some buggy drivers might do
2062362a53eSArtem Bityutskiy 		 * this, so we change it to -EIO.
2072362a53eSArtem Bityutskiy 		 */
2082362a53eSArtem Bityutskiy 		if (read != len && err == -EBADMSG) {
2092362a53eSArtem Bityutskiy 			ubi_assert(0);
2102362a53eSArtem Bityutskiy 			err = -EIO;
2112362a53eSArtem Bityutskiy 		}
212801c135cSArtem B. Bityutskiy 	} else {
213801c135cSArtem B. Bityutskiy 		ubi_assert(len == read);
214801c135cSArtem B. Bityutskiy 
215801c135cSArtem B. Bityutskiy 		if (ubi_dbg_is_bitflip()) {
216c8566350SArtem Bityutskiy 			dbg_gen("bit-flip (emulated)");
217801c135cSArtem B. Bityutskiy 			err = UBI_IO_BITFLIPS;
218801c135cSArtem B. Bityutskiy 		}
219801c135cSArtem B. Bityutskiy 	}
220801c135cSArtem B. Bityutskiy 
221801c135cSArtem B. Bityutskiy 	return err;
222801c135cSArtem B. Bityutskiy }
223801c135cSArtem B. Bityutskiy 
224801c135cSArtem B. Bityutskiy /**
225801c135cSArtem B. Bityutskiy  * ubi_io_write - write data to a physical eraseblock.
226801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
227801c135cSArtem B. Bityutskiy  * @buf: buffer with the data to write
228801c135cSArtem B. Bityutskiy  * @pnum: physical eraseblock number to write to
229801c135cSArtem B. Bityutskiy  * @offset: offset within the physical eraseblock where to write
230801c135cSArtem B. Bityutskiy  * @len: how many bytes to write
231801c135cSArtem B. Bityutskiy  *
232801c135cSArtem B. Bityutskiy  * This function writes @len bytes of data from buffer @buf to offset @offset
233801c135cSArtem B. Bityutskiy  * of physical eraseblock @pnum. If all the data were successfully written,
234801c135cSArtem B. Bityutskiy  * zero is returned. If an error occurred, this function returns a negative
235801c135cSArtem B. Bityutskiy  * error code. If %-EIO is returned, the physical eraseblock most probably went
236801c135cSArtem B. Bityutskiy  * bad.
237801c135cSArtem B. Bityutskiy  *
238801c135cSArtem B. Bityutskiy  * Note, in case of an error, it is possible that something was still written
239801c135cSArtem B. Bityutskiy  * to the flash media, but may be some garbage.
240801c135cSArtem B. Bityutskiy  */
241e88d6e10SArtem Bityutskiy int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
242e88d6e10SArtem Bityutskiy 		 int len)
243801c135cSArtem B. Bityutskiy {
244801c135cSArtem B. Bityutskiy 	int err;
245801c135cSArtem B. Bityutskiy 	size_t written;
246801c135cSArtem B. Bityutskiy 	loff_t addr;
247801c135cSArtem B. Bityutskiy 
248801c135cSArtem B. Bityutskiy 	dbg_io("write %d bytes to PEB %d:%d", len, pnum, offset);
249801c135cSArtem B. Bityutskiy 
250801c135cSArtem B. Bityutskiy 	ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
251801c135cSArtem B. Bityutskiy 	ubi_assert(offset >= 0 && offset + len <= ubi->peb_size);
252801c135cSArtem B. Bityutskiy 	ubi_assert(offset % ubi->hdrs_min_io_size == 0);
253801c135cSArtem B. Bityutskiy 	ubi_assert(len > 0 && len % ubi->hdrs_min_io_size == 0);
254801c135cSArtem B. Bityutskiy 
255801c135cSArtem B. Bityutskiy 	if (ubi->ro_mode) {
256801c135cSArtem B. Bityutskiy 		ubi_err("read-only mode");
257801c135cSArtem B. Bityutskiy 		return -EROFS;
258801c135cSArtem B. Bityutskiy 	}
259801c135cSArtem B. Bityutskiy 
260801c135cSArtem B. Bityutskiy 	/* The below has to be compiled out if paranoid checks are disabled */
261801c135cSArtem B. Bityutskiy 
262801c135cSArtem B. Bityutskiy 	err = paranoid_check_not_bad(ubi, pnum);
263801c135cSArtem B. Bityutskiy 	if (err)
264adbf05e3SArtem Bityutskiy 		return err;
265801c135cSArtem B. Bityutskiy 
266801c135cSArtem B. Bityutskiy 	/* The area we are writing to has to contain all 0xFF bytes */
26740a71a87SArtem Bityutskiy 	err = ubi_dbg_check_all_ff(ubi, pnum, offset, len);
268801c135cSArtem B. Bityutskiy 	if (err)
269adbf05e3SArtem Bityutskiy 		return err;
270801c135cSArtem B. Bityutskiy 
271801c135cSArtem B. Bityutskiy 	if (offset >= ubi->leb_start) {
272801c135cSArtem B. Bityutskiy 		/*
273801c135cSArtem B. Bityutskiy 		 * We write to the data area of the physical eraseblock. Make
274801c135cSArtem B. Bityutskiy 		 * sure it has valid EC and VID headers.
275801c135cSArtem B. Bityutskiy 		 */
276801c135cSArtem B. Bityutskiy 		err = paranoid_check_peb_ec_hdr(ubi, pnum);
277801c135cSArtem B. Bityutskiy 		if (err)
278adbf05e3SArtem Bityutskiy 			return err;
279801c135cSArtem B. Bityutskiy 		err = paranoid_check_peb_vid_hdr(ubi, pnum);
280801c135cSArtem B. Bityutskiy 		if (err)
281adbf05e3SArtem Bityutskiy 			return err;
282801c135cSArtem B. Bityutskiy 	}
283801c135cSArtem B. Bityutskiy 
284801c135cSArtem B. Bityutskiy 	if (ubi_dbg_is_write_failure()) {
285801c135cSArtem B. Bityutskiy 		dbg_err("cannot write %d bytes to PEB %d:%d "
286801c135cSArtem B. Bityutskiy 			"(emulated)", len, pnum, offset);
287801c135cSArtem B. Bityutskiy 		ubi_dbg_dump_stack();
288801c135cSArtem B. Bityutskiy 		return -EIO;
289801c135cSArtem B. Bityutskiy 	}
290801c135cSArtem B. Bityutskiy 
291801c135cSArtem B. Bityutskiy 	addr = (loff_t)pnum * ubi->peb_size + offset;
292801c135cSArtem B. Bityutskiy 	err = ubi->mtd->write(ubi->mtd, addr, len, &written, buf);
293801c135cSArtem B. Bityutskiy 	if (err) {
294801c135cSArtem B. Bityutskiy 		ubi_err("error %d while writing %d bytes to PEB %d:%d, written "
295801c135cSArtem B. Bityutskiy 			"%zd bytes", err, len, pnum, offset, written);
296801c135cSArtem B. Bityutskiy 		ubi_dbg_dump_stack();
297867996b1SArtem Bityutskiy 		ubi_dbg_dump_flash(ubi, pnum, offset, len);
298801c135cSArtem B. Bityutskiy 	} else
299801c135cSArtem B. Bityutskiy 		ubi_assert(written == len);
300801c135cSArtem B. Bityutskiy 
3016e9065d7SArtem Bityutskiy 	if (!err) {
3026e9065d7SArtem Bityutskiy 		err = ubi_dbg_check_write(ubi, buf, pnum, offset, len);
3036e9065d7SArtem Bityutskiy 		if (err)
3046e9065d7SArtem Bityutskiy 			return err;
3056e9065d7SArtem Bityutskiy 
3066e9065d7SArtem Bityutskiy 		/*
3076e9065d7SArtem Bityutskiy 		 * Since we always write sequentially, the rest of the PEB has
3086e9065d7SArtem Bityutskiy 		 * to contain only 0xFF bytes.
3096e9065d7SArtem Bityutskiy 		 */
3106e9065d7SArtem Bityutskiy 		offset += len;
3116e9065d7SArtem Bityutskiy 		len = ubi->peb_size - offset;
3126e9065d7SArtem Bityutskiy 		if (len)
3136e9065d7SArtem Bityutskiy 			err = ubi_dbg_check_all_ff(ubi, pnum, offset, len);
3146e9065d7SArtem Bityutskiy 	}
3156e9065d7SArtem Bityutskiy 
316801c135cSArtem B. Bityutskiy 	return err;
317801c135cSArtem B. Bityutskiy }
318801c135cSArtem B. Bityutskiy 
319801c135cSArtem B. Bityutskiy /**
320801c135cSArtem B. Bityutskiy  * erase_callback - MTD erasure call-back.
321801c135cSArtem B. Bityutskiy  * @ei: MTD erase information object.
322801c135cSArtem B. Bityutskiy  *
323801c135cSArtem B. Bityutskiy  * Note, even though MTD erase interface is asynchronous, all the current
324801c135cSArtem B. Bityutskiy  * implementations are synchronous anyway.
325801c135cSArtem B. Bityutskiy  */
326801c135cSArtem B. Bityutskiy static void erase_callback(struct erase_info *ei)
327801c135cSArtem B. Bityutskiy {
328801c135cSArtem B. Bityutskiy 	wake_up_interruptible((wait_queue_head_t *)ei->priv);
329801c135cSArtem B. Bityutskiy }
330801c135cSArtem B. Bityutskiy 
331801c135cSArtem B. Bityutskiy /**
332801c135cSArtem B. Bityutskiy  * do_sync_erase - synchronously erase a physical eraseblock.
333801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
334801c135cSArtem B. Bityutskiy  * @pnum: the physical eraseblock number to erase
335801c135cSArtem B. Bityutskiy  *
336801c135cSArtem B. Bityutskiy  * This function synchronously erases physical eraseblock @pnum and returns
337801c135cSArtem B. Bityutskiy  * zero in case of success and a negative error code in case of failure. If
338801c135cSArtem B. Bityutskiy  * %-EIO is returned, the physical eraseblock most probably went bad.
339801c135cSArtem B. Bityutskiy  */
340e88d6e10SArtem Bityutskiy static int do_sync_erase(struct ubi_device *ubi, int pnum)
341801c135cSArtem B. Bityutskiy {
342801c135cSArtem B. Bityutskiy 	int err, retries = 0;
343801c135cSArtem B. Bityutskiy 	struct erase_info ei;
344801c135cSArtem B. Bityutskiy 	wait_queue_head_t wq;
345801c135cSArtem B. Bityutskiy 
346801c135cSArtem B. Bityutskiy 	dbg_io("erase PEB %d", pnum);
347801c135cSArtem B. Bityutskiy 
348801c135cSArtem B. Bityutskiy retry:
349801c135cSArtem B. Bityutskiy 	init_waitqueue_head(&wq);
350801c135cSArtem B. Bityutskiy 	memset(&ei, 0, sizeof(struct erase_info));
351801c135cSArtem B. Bityutskiy 
352801c135cSArtem B. Bityutskiy 	ei.mtd      = ubi->mtd;
3532f176f79SBrijesh Singh 	ei.addr     = (loff_t)pnum * ubi->peb_size;
354801c135cSArtem B. Bityutskiy 	ei.len      = ubi->peb_size;
355801c135cSArtem B. Bityutskiy 	ei.callback = erase_callback;
356801c135cSArtem B. Bityutskiy 	ei.priv     = (unsigned long)&wq;
357801c135cSArtem B. Bityutskiy 
358801c135cSArtem B. Bityutskiy 	err = ubi->mtd->erase(ubi->mtd, &ei);
359801c135cSArtem B. Bityutskiy 	if (err) {
360801c135cSArtem B. Bityutskiy 		if (retries++ < UBI_IO_RETRIES) {
361801c135cSArtem B. Bityutskiy 			dbg_io("error %d while erasing PEB %d, retry",
362801c135cSArtem B. Bityutskiy 			       err, pnum);
363801c135cSArtem B. Bityutskiy 			yield();
364801c135cSArtem B. Bityutskiy 			goto retry;
365801c135cSArtem B. Bityutskiy 		}
366801c135cSArtem B. Bityutskiy 		ubi_err("cannot erase PEB %d, error %d", pnum, err);
367801c135cSArtem B. Bityutskiy 		ubi_dbg_dump_stack();
368801c135cSArtem B. Bityutskiy 		return err;
369801c135cSArtem B. Bityutskiy 	}
370801c135cSArtem B. Bityutskiy 
371801c135cSArtem B. Bityutskiy 	err = wait_event_interruptible(wq, ei.state == MTD_ERASE_DONE ||
372801c135cSArtem B. Bityutskiy 					   ei.state == MTD_ERASE_FAILED);
373801c135cSArtem B. Bityutskiy 	if (err) {
374801c135cSArtem B. Bityutskiy 		ubi_err("interrupted PEB %d erasure", pnum);
375801c135cSArtem B. Bityutskiy 		return -EINTR;
376801c135cSArtem B. Bityutskiy 	}
377801c135cSArtem B. Bityutskiy 
378801c135cSArtem B. Bityutskiy 	if (ei.state == MTD_ERASE_FAILED) {
379801c135cSArtem B. Bityutskiy 		if (retries++ < UBI_IO_RETRIES) {
380801c135cSArtem B. Bityutskiy 			dbg_io("error while erasing PEB %d, retry", pnum);
381801c135cSArtem B. Bityutskiy 			yield();
382801c135cSArtem B. Bityutskiy 			goto retry;
383801c135cSArtem B. Bityutskiy 		}
384801c135cSArtem B. Bityutskiy 		ubi_err("cannot erase PEB %d", pnum);
385801c135cSArtem B. Bityutskiy 		ubi_dbg_dump_stack();
386801c135cSArtem B. Bityutskiy 		return -EIO;
387801c135cSArtem B. Bityutskiy 	}
388801c135cSArtem B. Bityutskiy 
38940a71a87SArtem Bityutskiy 	err = ubi_dbg_check_all_ff(ubi, pnum, 0, ubi->peb_size);
390801c135cSArtem B. Bityutskiy 	if (err)
391adbf05e3SArtem Bityutskiy 		return err;
392801c135cSArtem B. Bityutskiy 
393801c135cSArtem B. Bityutskiy 	if (ubi_dbg_is_erase_failure() && !err) {
394801c135cSArtem B. Bityutskiy 		dbg_err("cannot erase PEB %d (emulated)", pnum);
395801c135cSArtem B. Bityutskiy 		return -EIO;
396801c135cSArtem B. Bityutskiy 	}
397801c135cSArtem B. Bityutskiy 
398801c135cSArtem B. Bityutskiy 	return 0;
399801c135cSArtem B. Bityutskiy }
400801c135cSArtem B. Bityutskiy 
401801c135cSArtem B. Bityutskiy /* Patterns to write to a physical eraseblock when torturing it */
402801c135cSArtem B. Bityutskiy static uint8_t patterns[] = {0xa5, 0x5a, 0x0};
403801c135cSArtem B. Bityutskiy 
404801c135cSArtem B. Bityutskiy /**
405801c135cSArtem B. Bityutskiy  * torture_peb - test a supposedly bad physical eraseblock.
406801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
407801c135cSArtem B. Bityutskiy  * @pnum: the physical eraseblock number to test
408801c135cSArtem B. Bityutskiy  *
409801c135cSArtem B. Bityutskiy  * This function returns %-EIO if the physical eraseblock did not pass the
410801c135cSArtem B. Bityutskiy  * test, a positive number of erase operations done if the test was
411801c135cSArtem B. Bityutskiy  * successfully passed, and other negative error codes in case of other errors.
412801c135cSArtem B. Bityutskiy  */
413e88d6e10SArtem Bityutskiy static int torture_peb(struct ubi_device *ubi, int pnum)
414801c135cSArtem B. Bityutskiy {
415801c135cSArtem B. Bityutskiy 	int err, i, patt_count;
416801c135cSArtem B. Bityutskiy 
4178c1e6ee1SArtem Bityutskiy 	ubi_msg("run torture test for PEB %d", pnum);
418801c135cSArtem B. Bityutskiy 	patt_count = ARRAY_SIZE(patterns);
419801c135cSArtem B. Bityutskiy 	ubi_assert(patt_count > 0);
420801c135cSArtem B. Bityutskiy 
421e88d6e10SArtem Bityutskiy 	mutex_lock(&ubi->buf_mutex);
422801c135cSArtem B. Bityutskiy 	for (i = 0; i < patt_count; i++) {
423801c135cSArtem B. Bityutskiy 		err = do_sync_erase(ubi, pnum);
424801c135cSArtem B. Bityutskiy 		if (err)
425801c135cSArtem B. Bityutskiy 			goto out;
426801c135cSArtem B. Bityutskiy 
427801c135cSArtem B. Bityutskiy 		/* Make sure the PEB contains only 0xFF bytes */
428e88d6e10SArtem Bityutskiy 		err = ubi_io_read(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size);
429801c135cSArtem B. Bityutskiy 		if (err)
430801c135cSArtem B. Bityutskiy 			goto out;
431801c135cSArtem B. Bityutskiy 
432bb00e180SArtem Bityutskiy 		err = ubi_check_pattern(ubi->peb_buf1, 0xFF, ubi->peb_size);
433801c135cSArtem B. Bityutskiy 		if (err == 0) {
434801c135cSArtem B. Bityutskiy 			ubi_err("erased PEB %d, but a non-0xFF byte found",
435801c135cSArtem B. Bityutskiy 				pnum);
436801c135cSArtem B. Bityutskiy 			err = -EIO;
437801c135cSArtem B. Bityutskiy 			goto out;
438801c135cSArtem B. Bityutskiy 		}
439801c135cSArtem B. Bityutskiy 
440801c135cSArtem B. Bityutskiy 		/* Write a pattern and check it */
441e88d6e10SArtem Bityutskiy 		memset(ubi->peb_buf1, patterns[i], ubi->peb_size);
442e88d6e10SArtem Bityutskiy 		err = ubi_io_write(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size);
443801c135cSArtem B. Bityutskiy 		if (err)
444801c135cSArtem B. Bityutskiy 			goto out;
445801c135cSArtem B. Bityutskiy 
446e88d6e10SArtem Bityutskiy 		memset(ubi->peb_buf1, ~patterns[i], ubi->peb_size);
447e88d6e10SArtem Bityutskiy 		err = ubi_io_read(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size);
448801c135cSArtem B. Bityutskiy 		if (err)
449801c135cSArtem B. Bityutskiy 			goto out;
450801c135cSArtem B. Bityutskiy 
451bb00e180SArtem Bityutskiy 		err = ubi_check_pattern(ubi->peb_buf1, patterns[i],
452bb00e180SArtem Bityutskiy 					ubi->peb_size);
453801c135cSArtem B. Bityutskiy 		if (err == 0) {
454801c135cSArtem B. Bityutskiy 			ubi_err("pattern %x checking failed for PEB %d",
455801c135cSArtem B. Bityutskiy 				patterns[i], pnum);
456801c135cSArtem B. Bityutskiy 			err = -EIO;
457801c135cSArtem B. Bityutskiy 			goto out;
458801c135cSArtem B. Bityutskiy 		}
459801c135cSArtem B. Bityutskiy 	}
460801c135cSArtem B. Bityutskiy 
461801c135cSArtem B. Bityutskiy 	err = patt_count;
4628c1e6ee1SArtem Bityutskiy 	ubi_msg("PEB %d passed torture test, do not mark it a bad", pnum);
463801c135cSArtem B. Bityutskiy 
464801c135cSArtem B. Bityutskiy out:
465e88d6e10SArtem Bityutskiy 	mutex_unlock(&ubi->buf_mutex);
4668d2d4011SArtem Bityutskiy 	if (err == UBI_IO_BITFLIPS || err == -EBADMSG) {
467801c135cSArtem B. Bityutskiy 		/*
468801c135cSArtem B. Bityutskiy 		 * If a bit-flip or data integrity error was detected, the test
469801c135cSArtem B. Bityutskiy 		 * has not passed because it happened on a freshly erased
470801c135cSArtem B. Bityutskiy 		 * physical eraseblock which means something is wrong with it.
471801c135cSArtem B. Bityutskiy 		 */
4728d2d4011SArtem Bityutskiy 		ubi_err("read problems on freshly erased PEB %d, must be bad",
4738d2d4011SArtem Bityutskiy 			pnum);
474801c135cSArtem B. Bityutskiy 		err = -EIO;
4758d2d4011SArtem Bityutskiy 	}
476801c135cSArtem B. Bityutskiy 	return err;
477801c135cSArtem B. Bityutskiy }
478801c135cSArtem B. Bityutskiy 
479801c135cSArtem B. Bityutskiy /**
480ebf53f42SArtem Bityutskiy  * nor_erase_prepare - prepare a NOR flash PEB for erasure.
481ebf53f42SArtem Bityutskiy  * @ubi: UBI device description object
482ebf53f42SArtem Bityutskiy  * @pnum: physical eraseblock number to prepare
483ebf53f42SArtem Bityutskiy  *
484ebf53f42SArtem Bityutskiy  * NOR flash, or at least some of them, have peculiar embedded PEB erasure
485ebf53f42SArtem Bityutskiy  * algorithm: the PEB is first filled with zeroes, then it is erased. And
486ebf53f42SArtem Bityutskiy  * filling with zeroes starts from the end of the PEB. This was observed with
487ebf53f42SArtem Bityutskiy  * Spansion S29GL512N NOR flash.
488ebf53f42SArtem Bityutskiy  *
489ebf53f42SArtem Bityutskiy  * This means that in case of a power cut we may end up with intact data at the
490ebf53f42SArtem Bityutskiy  * beginning of the PEB, and all zeroes at the end of PEB. In other words, the
491ebf53f42SArtem Bityutskiy  * EC and VID headers are OK, but a large chunk of data at the end of PEB is
492ebf53f42SArtem Bityutskiy  * zeroed. This makes UBI mistakenly treat this PEB as used and associate it
493ebf53f42SArtem Bityutskiy  * with an LEB, which leads to subsequent failures (e.g., UBIFS fails).
494ebf53f42SArtem Bityutskiy  *
495ebf53f42SArtem Bityutskiy  * This function is called before erasing NOR PEBs and it zeroes out EC and VID
496ebf53f42SArtem Bityutskiy  * magic numbers in order to invalidate them and prevent the failures. Returns
497ebf53f42SArtem Bityutskiy  * zero in case of success and a negative error code in case of failure.
498ebf53f42SArtem Bityutskiy  */
499ebf53f42SArtem Bityutskiy static int nor_erase_prepare(struct ubi_device *ubi, int pnum)
500ebf53f42SArtem Bityutskiy {
501de75c771SArtem Bityutskiy 	int err, err1;
502ebf53f42SArtem Bityutskiy 	size_t written;
503ebf53f42SArtem Bityutskiy 	loff_t addr;
504ebf53f42SArtem Bityutskiy 	uint32_t data = 0;
5052fff570eSArtem Bityutskiy 	/*
5062fff570eSArtem Bityutskiy 	 * Note, we cannot generally define VID header buffers on stack,
5072fff570eSArtem Bityutskiy 	 * because of the way we deal with these buffers (see the header
5082fff570eSArtem Bityutskiy 	 * comment in this file). But we know this is a NOR-specific piece of
5092fff570eSArtem Bityutskiy 	 * code, so we can do this. But yes, this is error-prone and we should
5102fff570eSArtem Bityutskiy 	 * (pre-)allocate VID header buffer instead.
5112fff570eSArtem Bityutskiy 	 */
512de75c771SArtem Bityutskiy 	struct ubi_vid_hdr vid_hdr;
513ebf53f42SArtem Bityutskiy 
5147ac760c2SArtem Bityutskiy 	/*
5157ac760c2SArtem Bityutskiy 	 * It is important to first invalidate the EC header, and then the VID
5167ac760c2SArtem Bityutskiy 	 * header. Otherwise a power cut may lead to valid EC header and
5177ac760c2SArtem Bityutskiy 	 * invalid VID header, in which case UBI will treat this PEB as
5187ac760c2SArtem Bityutskiy 	 * corrupted and will try to preserve it, and print scary warnings (see
5197ac760c2SArtem Bityutskiy 	 * the header comment in scan.c for more information).
5207ac760c2SArtem Bityutskiy 	 */
5217ac760c2SArtem Bityutskiy 	addr = (loff_t)pnum * ubi->peb_size;
52283c2099fSArtem Bityutskiy 	err = ubi->mtd->write(ubi->mtd, addr, 4, &written, (void *)&data);
523de75c771SArtem Bityutskiy 	if (!err) {
5247ac760c2SArtem Bityutskiy 		addr += ubi->vid_hdr_aloffset;
525de75c771SArtem Bityutskiy 		err = ubi->mtd->write(ubi->mtd, addr, 4, &written,
526de75c771SArtem Bityutskiy 				      (void *)&data);
527de75c771SArtem Bityutskiy 		if (!err)
528de75c771SArtem Bityutskiy 			return 0;
5295b289b56SArtem Bityutskiy 	}
5305b289b56SArtem Bityutskiy 
531de75c771SArtem Bityutskiy 	/*
532de75c771SArtem Bityutskiy 	 * We failed to write to the media. This was observed with Spansion
5337ac760c2SArtem Bityutskiy 	 * S29GL512N NOR flash. Most probably the previously eraseblock erasure
5347ac760c2SArtem Bityutskiy 	 * was interrupted at a very inappropriate moment, so it became
5357ac760c2SArtem Bityutskiy 	 * unwritable. In this case we probably anyway have garbage in this
5367ac760c2SArtem Bityutskiy 	 * PEB.
537de75c771SArtem Bityutskiy 	 */
538de75c771SArtem Bityutskiy 	err1 = ubi_io_read_vid_hdr(ubi, pnum, &vid_hdr, 0);
539d4c63813SHolger Brunck 	if (err1 == UBI_IO_BAD_HDR_EBADMSG || err1 == UBI_IO_BAD_HDR ||
540d4c63813SHolger Brunck 	    err1 == UBI_IO_FF) {
5417ac760c2SArtem Bityutskiy 		struct ubi_ec_hdr ec_hdr;
5427ac760c2SArtem Bityutskiy 
5437ac760c2SArtem Bityutskiy 		err1 = ubi_io_read_ec_hdr(ubi, pnum, &ec_hdr, 0);
544d4c63813SHolger Brunck 		if (err1 == UBI_IO_BAD_HDR_EBADMSG || err1 == UBI_IO_BAD_HDR ||
545d4c63813SHolger Brunck 		    err1 == UBI_IO_FF)
546de75c771SArtem Bityutskiy 			/*
5477ac760c2SArtem Bityutskiy 			 * Both VID and EC headers are corrupted, so we can
5487ac760c2SArtem Bityutskiy 			 * safely erase this PEB and not afraid that it will be
5497ac760c2SArtem Bityutskiy 			 * treated as a valid PEB in case of an unclean reboot.
550de75c771SArtem Bityutskiy 			 */
551ebf53f42SArtem Bityutskiy 			return 0;
5527ac760c2SArtem Bityutskiy 	}
553de75c771SArtem Bityutskiy 
554de75c771SArtem Bityutskiy 	/*
555de75c771SArtem Bityutskiy 	 * The PEB contains a valid VID header, but we cannot invalidate it.
556de75c771SArtem Bityutskiy 	 * Supposedly the flash media or the driver is screwed up, so return an
557de75c771SArtem Bityutskiy 	 * error.
558de75c771SArtem Bityutskiy 	 */
559de75c771SArtem Bityutskiy 	ubi_err("cannot invalidate PEB %d, write returned %d read returned %d",
560de75c771SArtem Bityutskiy 		pnum, err, err1);
561de75c771SArtem Bityutskiy 	ubi_dbg_dump_flash(ubi, pnum, 0, ubi->peb_size);
562de75c771SArtem Bityutskiy 	return -EIO;
563ebf53f42SArtem Bityutskiy }
564ebf53f42SArtem Bityutskiy 
565ebf53f42SArtem Bityutskiy /**
566801c135cSArtem B. Bityutskiy  * ubi_io_sync_erase - synchronously erase a physical eraseblock.
567801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
568801c135cSArtem B. Bityutskiy  * @pnum: physical eraseblock number to erase
569801c135cSArtem B. Bityutskiy  * @torture: if this physical eraseblock has to be tortured
570801c135cSArtem B. Bityutskiy  *
571801c135cSArtem B. Bityutskiy  * This function synchronously erases physical eraseblock @pnum. If @torture
572801c135cSArtem B. Bityutskiy  * flag is not zero, the physical eraseblock is checked by means of writing
573801c135cSArtem B. Bityutskiy  * different patterns to it and reading them back. If the torturing is enabled,
574025dfdafSFrederik Schwarzer  * the physical eraseblock is erased more than once.
575801c135cSArtem B. Bityutskiy  *
576801c135cSArtem B. Bityutskiy  * This function returns the number of erasures made in case of success, %-EIO
577801c135cSArtem B. Bityutskiy  * if the erasure failed or the torturing test failed, and other negative error
578801c135cSArtem B. Bityutskiy  * codes in case of other errors. Note, %-EIO means that the physical
579801c135cSArtem B. Bityutskiy  * eraseblock is bad.
580801c135cSArtem B. Bityutskiy  */
581e88d6e10SArtem Bityutskiy int ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture)
582801c135cSArtem B. Bityutskiy {
583801c135cSArtem B. Bityutskiy 	int err, ret = 0;
584801c135cSArtem B. Bityutskiy 
585801c135cSArtem B. Bityutskiy 	ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
586801c135cSArtem B. Bityutskiy 
587801c135cSArtem B. Bityutskiy 	err = paranoid_check_not_bad(ubi, pnum);
588801c135cSArtem B. Bityutskiy 	if (err != 0)
589adbf05e3SArtem Bityutskiy 		return err;
590801c135cSArtem B. Bityutskiy 
591801c135cSArtem B. Bityutskiy 	if (ubi->ro_mode) {
592801c135cSArtem B. Bityutskiy 		ubi_err("read-only mode");
593801c135cSArtem B. Bityutskiy 		return -EROFS;
594801c135cSArtem B. Bityutskiy 	}
595801c135cSArtem B. Bityutskiy 
596ebf53f42SArtem Bityutskiy 	if (ubi->nor_flash) {
597ebf53f42SArtem Bityutskiy 		err = nor_erase_prepare(ubi, pnum);
598ebf53f42SArtem Bityutskiy 		if (err)
599ebf53f42SArtem Bityutskiy 			return err;
600ebf53f42SArtem Bityutskiy 	}
601ebf53f42SArtem Bityutskiy 
602801c135cSArtem B. Bityutskiy 	if (torture) {
603801c135cSArtem B. Bityutskiy 		ret = torture_peb(ubi, pnum);
604801c135cSArtem B. Bityutskiy 		if (ret < 0)
605801c135cSArtem B. Bityutskiy 			return ret;
606801c135cSArtem B. Bityutskiy 	}
607801c135cSArtem B. Bityutskiy 
608801c135cSArtem B. Bityutskiy 	err = do_sync_erase(ubi, pnum);
609801c135cSArtem B. Bityutskiy 	if (err)
610801c135cSArtem B. Bityutskiy 		return err;
611801c135cSArtem B. Bityutskiy 
612801c135cSArtem B. Bityutskiy 	return ret + 1;
613801c135cSArtem B. Bityutskiy }
614801c135cSArtem B. Bityutskiy 
615801c135cSArtem B. Bityutskiy /**
616801c135cSArtem B. Bityutskiy  * ubi_io_is_bad - check if a physical eraseblock is bad.
617801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
618801c135cSArtem B. Bityutskiy  * @pnum: the physical eraseblock number to check
619801c135cSArtem B. Bityutskiy  *
620801c135cSArtem B. Bityutskiy  * This function returns a positive number if the physical eraseblock is bad,
621801c135cSArtem B. Bityutskiy  * zero if not, and a negative error code if an error occurred.
622801c135cSArtem B. Bityutskiy  */
623801c135cSArtem B. Bityutskiy int ubi_io_is_bad(const struct ubi_device *ubi, int pnum)
624801c135cSArtem B. Bityutskiy {
625801c135cSArtem B. Bityutskiy 	struct mtd_info *mtd = ubi->mtd;
626801c135cSArtem B. Bityutskiy 
627801c135cSArtem B. Bityutskiy 	ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
628801c135cSArtem B. Bityutskiy 
629801c135cSArtem B. Bityutskiy 	if (ubi->bad_allowed) {
630801c135cSArtem B. Bityutskiy 		int ret;
631801c135cSArtem B. Bityutskiy 
632801c135cSArtem B. Bityutskiy 		ret = mtd->block_isbad(mtd, (loff_t)pnum * ubi->peb_size);
633801c135cSArtem B. Bityutskiy 		if (ret < 0)
634801c135cSArtem B. Bityutskiy 			ubi_err("error %d while checking if PEB %d is bad",
635801c135cSArtem B. Bityutskiy 				ret, pnum);
636801c135cSArtem B. Bityutskiy 		else if (ret)
637801c135cSArtem B. Bityutskiy 			dbg_io("PEB %d is bad", pnum);
638801c135cSArtem B. Bityutskiy 		return ret;
639801c135cSArtem B. Bityutskiy 	}
640801c135cSArtem B. Bityutskiy 
641801c135cSArtem B. Bityutskiy 	return 0;
642801c135cSArtem B. Bityutskiy }
643801c135cSArtem B. Bityutskiy 
644801c135cSArtem B. Bityutskiy /**
645801c135cSArtem B. Bityutskiy  * ubi_io_mark_bad - mark a physical eraseblock as bad.
646801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
647801c135cSArtem B. Bityutskiy  * @pnum: the physical eraseblock number to mark
648801c135cSArtem B. Bityutskiy  *
649801c135cSArtem B. Bityutskiy  * This function returns zero in case of success and a negative error code in
650801c135cSArtem B. Bityutskiy  * case of failure.
651801c135cSArtem B. Bityutskiy  */
652801c135cSArtem B. Bityutskiy int ubi_io_mark_bad(const struct ubi_device *ubi, int pnum)
653801c135cSArtem B. Bityutskiy {
654801c135cSArtem B. Bityutskiy 	int err;
655801c135cSArtem B. Bityutskiy 	struct mtd_info *mtd = ubi->mtd;
656801c135cSArtem B. Bityutskiy 
657801c135cSArtem B. Bityutskiy 	ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
658801c135cSArtem B. Bityutskiy 
659801c135cSArtem B. Bityutskiy 	if (ubi->ro_mode) {
660801c135cSArtem B. Bityutskiy 		ubi_err("read-only mode");
661801c135cSArtem B. Bityutskiy 		return -EROFS;
662801c135cSArtem B. Bityutskiy 	}
663801c135cSArtem B. Bityutskiy 
664801c135cSArtem B. Bityutskiy 	if (!ubi->bad_allowed)
665801c135cSArtem B. Bityutskiy 		return 0;
666801c135cSArtem B. Bityutskiy 
667801c135cSArtem B. Bityutskiy 	err = mtd->block_markbad(mtd, (loff_t)pnum * ubi->peb_size);
668801c135cSArtem B. Bityutskiy 	if (err)
669801c135cSArtem B. Bityutskiy 		ubi_err("cannot mark PEB %d bad, error %d", pnum, err);
670801c135cSArtem B. Bityutskiy 	return err;
671801c135cSArtem B. Bityutskiy }
672801c135cSArtem B. Bityutskiy 
673801c135cSArtem B. Bityutskiy /**
674801c135cSArtem B. Bityutskiy  * validate_ec_hdr - validate an erase counter header.
675801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
676801c135cSArtem B. Bityutskiy  * @ec_hdr: the erase counter header to check
677801c135cSArtem B. Bityutskiy  *
678801c135cSArtem B. Bityutskiy  * This function returns zero if the erase counter header is OK, and %1 if
679801c135cSArtem B. Bityutskiy  * not.
680801c135cSArtem B. Bityutskiy  */
681fe96efc1SArtem Bityutskiy static int validate_ec_hdr(const struct ubi_device *ubi,
682801c135cSArtem B. Bityutskiy 			   const struct ubi_ec_hdr *ec_hdr)
683801c135cSArtem B. Bityutskiy {
684801c135cSArtem B. Bityutskiy 	long long ec;
685fe96efc1SArtem Bityutskiy 	int vid_hdr_offset, leb_start;
686801c135cSArtem B. Bityutskiy 
6873261ebd7SChristoph Hellwig 	ec = be64_to_cpu(ec_hdr->ec);
6883261ebd7SChristoph Hellwig 	vid_hdr_offset = be32_to_cpu(ec_hdr->vid_hdr_offset);
6893261ebd7SChristoph Hellwig 	leb_start = be32_to_cpu(ec_hdr->data_offset);
690801c135cSArtem B. Bityutskiy 
691801c135cSArtem B. Bityutskiy 	if (ec_hdr->version != UBI_VERSION) {
692801c135cSArtem B. Bityutskiy 		ubi_err("node with incompatible UBI version found: "
693801c135cSArtem B. Bityutskiy 			"this UBI version is %d, image version is %d",
694801c135cSArtem B. Bityutskiy 			UBI_VERSION, (int)ec_hdr->version);
695801c135cSArtem B. Bityutskiy 		goto bad;
696801c135cSArtem B. Bityutskiy 	}
697801c135cSArtem B. Bityutskiy 
698801c135cSArtem B. Bityutskiy 	if (vid_hdr_offset != ubi->vid_hdr_offset) {
699801c135cSArtem B. Bityutskiy 		ubi_err("bad VID header offset %d, expected %d",
700801c135cSArtem B. Bityutskiy 			vid_hdr_offset, ubi->vid_hdr_offset);
701801c135cSArtem B. Bityutskiy 		goto bad;
702801c135cSArtem B. Bityutskiy 	}
703801c135cSArtem B. Bityutskiy 
704801c135cSArtem B. Bityutskiy 	if (leb_start != ubi->leb_start) {
705801c135cSArtem B. Bityutskiy 		ubi_err("bad data offset %d, expected %d",
706801c135cSArtem B. Bityutskiy 			leb_start, ubi->leb_start);
707801c135cSArtem B. Bityutskiy 		goto bad;
708801c135cSArtem B. Bityutskiy 	}
709801c135cSArtem B. Bityutskiy 
710801c135cSArtem B. Bityutskiy 	if (ec < 0 || ec > UBI_MAX_ERASECOUNTER) {
711801c135cSArtem B. Bityutskiy 		ubi_err("bad erase counter %lld", ec);
712801c135cSArtem B. Bityutskiy 		goto bad;
713801c135cSArtem B. Bityutskiy 	}
714801c135cSArtem B. Bityutskiy 
715801c135cSArtem B. Bityutskiy 	return 0;
716801c135cSArtem B. Bityutskiy 
717801c135cSArtem B. Bityutskiy bad:
718801c135cSArtem B. Bityutskiy 	ubi_err("bad EC header");
719801c135cSArtem B. Bityutskiy 	ubi_dbg_dump_ec_hdr(ec_hdr);
720801c135cSArtem B. Bityutskiy 	ubi_dbg_dump_stack();
721801c135cSArtem B. Bityutskiy 	return 1;
722801c135cSArtem B. Bityutskiy }
723801c135cSArtem B. Bityutskiy 
724801c135cSArtem B. Bityutskiy /**
725801c135cSArtem B. Bityutskiy  * ubi_io_read_ec_hdr - read and check an erase counter header.
726801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
727801c135cSArtem B. Bityutskiy  * @pnum: physical eraseblock to read from
728801c135cSArtem B. Bityutskiy  * @ec_hdr: a &struct ubi_ec_hdr object where to store the read erase counter
729801c135cSArtem B. Bityutskiy  * header
730801c135cSArtem B. Bityutskiy  * @verbose: be verbose if the header is corrupted or was not found
731801c135cSArtem B. Bityutskiy  *
732801c135cSArtem B. Bityutskiy  * This function reads erase counter header from physical eraseblock @pnum and
733801c135cSArtem B. Bityutskiy  * stores it in @ec_hdr. This function also checks CRC checksum of the read
734801c135cSArtem B. Bityutskiy  * erase counter header. The following codes may be returned:
735801c135cSArtem B. Bityutskiy  *
736801c135cSArtem B. Bityutskiy  * o %0 if the CRC checksum is correct and the header was successfully read;
737801c135cSArtem B. Bityutskiy  * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected
738801c135cSArtem B. Bityutskiy  *   and corrected by the flash driver; this is harmless but may indicate that
739801c135cSArtem B. Bityutskiy  *   this eraseblock may become bad soon (but may be not);
740786d7831SArtem Bityutskiy  * o %UBI_IO_BAD_HDR if the erase counter header is corrupted (a CRC error);
741756e1df1SArtem Bityutskiy  * o %UBI_IO_BAD_HDR_EBADMSG is the same as %UBI_IO_BAD_HDR, but there also was
742756e1df1SArtem Bityutskiy  *   a data integrity error (uncorrectable ECC error in case of NAND);
74374d82d26SArtem Bityutskiy  * o %UBI_IO_FF if only 0xFF bytes were read (the PEB is supposedly empty)
744801c135cSArtem B. Bityutskiy  * o a negative error code in case of failure.
745801c135cSArtem B. Bityutskiy  */
746e88d6e10SArtem Bityutskiy int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
747801c135cSArtem B. Bityutskiy 		       struct ubi_ec_hdr *ec_hdr, int verbose)
748801c135cSArtem B. Bityutskiy {
74992e1a7d9SArtem Bityutskiy 	int err, read_err;
750801c135cSArtem B. Bityutskiy 	uint32_t crc, magic, hdr_crc;
751801c135cSArtem B. Bityutskiy 
752801c135cSArtem B. Bityutskiy 	dbg_io("read EC header from PEB %d", pnum);
753801c135cSArtem B. Bityutskiy 	ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
754801c135cSArtem B. Bityutskiy 
75592e1a7d9SArtem Bityutskiy 	read_err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE);
75692e1a7d9SArtem Bityutskiy 	if (read_err) {
75792e1a7d9SArtem Bityutskiy 		if (read_err != UBI_IO_BITFLIPS && read_err != -EBADMSG)
75892e1a7d9SArtem Bityutskiy 			return read_err;
759801c135cSArtem B. Bityutskiy 
760801c135cSArtem B. Bityutskiy 		/*
761801c135cSArtem B. Bityutskiy 		 * We read all the data, but either a correctable bit-flip
762756e1df1SArtem Bityutskiy 		 * occurred, or MTD reported a data integrity error
763756e1df1SArtem Bityutskiy 		 * (uncorrectable ECC error in case of NAND). The former is
764756e1df1SArtem Bityutskiy 		 * harmless, the later may mean that the read data is
765756e1df1SArtem Bityutskiy 		 * corrupted. But we have a CRC check-sum and we will detect
766756e1df1SArtem Bityutskiy 		 * this. If the EC header is still OK, we just report this as
767756e1df1SArtem Bityutskiy 		 * there was a bit-flip, to force scrubbing.
768801c135cSArtem B. Bityutskiy 		 */
769801c135cSArtem B. Bityutskiy 	}
770801c135cSArtem B. Bityutskiy 
7713261ebd7SChristoph Hellwig 	magic = be32_to_cpu(ec_hdr->magic);
772801c135cSArtem B. Bityutskiy 	if (magic != UBI_EC_HDR_MAGIC) {
77392e1a7d9SArtem Bityutskiy 		if (read_err == -EBADMSG)
77492e1a7d9SArtem Bityutskiy 			return UBI_IO_BAD_HDR_EBADMSG;
775eb89580eSArtem Bityutskiy 
776801c135cSArtem B. Bityutskiy 		/*
777801c135cSArtem B. Bityutskiy 		 * The magic field is wrong. Let's check if we have read all
778801c135cSArtem B. Bityutskiy 		 * 0xFF. If yes, this physical eraseblock is assumed to be
779801c135cSArtem B. Bityutskiy 		 * empty.
780801c135cSArtem B. Bityutskiy 		 */
781bb00e180SArtem Bityutskiy 		if (ubi_check_pattern(ec_hdr, 0xFF, UBI_EC_HDR_SIZE)) {
782801c135cSArtem B. Bityutskiy 			/* The physical eraseblock is supposedly empty */
783801c135cSArtem B. Bityutskiy 			if (verbose)
784801c135cSArtem B. Bityutskiy 				ubi_warn("no EC header found at PEB %d, "
785801c135cSArtem B. Bityutskiy 					 "only 0xFF bytes", pnum);
7866f9fdf62SArtem Bityutskiy 			dbg_bld("no EC header found at PEB %d, "
787ed45819fSArtem Bityutskiy 				"only 0xFF bytes", pnum);
78892e1a7d9SArtem Bityutskiy 			if (!read_err)
78974d82d26SArtem Bityutskiy 				return UBI_IO_FF;
79092e1a7d9SArtem Bityutskiy 			else
79192e1a7d9SArtem Bityutskiy 				return UBI_IO_FF_BITFLIPS;
792801c135cSArtem B. Bityutskiy 		}
793801c135cSArtem B. Bityutskiy 
794801c135cSArtem B. Bityutskiy 		/*
795801c135cSArtem B. Bityutskiy 		 * This is not a valid erase counter header, and these are not
796801c135cSArtem B. Bityutskiy 		 * 0xFF bytes. Report that the header is corrupted.
797801c135cSArtem B. Bityutskiy 		 */
798801c135cSArtem B. Bityutskiy 		if (verbose) {
799801c135cSArtem B. Bityutskiy 			ubi_warn("bad magic number at PEB %d: %08x instead of "
800801c135cSArtem B. Bityutskiy 				 "%08x", pnum, magic, UBI_EC_HDR_MAGIC);
801801c135cSArtem B. Bityutskiy 			ubi_dbg_dump_ec_hdr(ec_hdr);
8026f9fdf62SArtem Bityutskiy 		}
8036f9fdf62SArtem Bityutskiy 		dbg_bld("bad magic number at PEB %d: %08x instead of "
804ed45819fSArtem Bityutskiy 			"%08x", pnum, magic, UBI_EC_HDR_MAGIC);
805786d7831SArtem Bityutskiy 		return UBI_IO_BAD_HDR;
806801c135cSArtem B. Bityutskiy 	}
807801c135cSArtem B. Bityutskiy 
808801c135cSArtem B. Bityutskiy 	crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
8093261ebd7SChristoph Hellwig 	hdr_crc = be32_to_cpu(ec_hdr->hdr_crc);
810801c135cSArtem B. Bityutskiy 
811801c135cSArtem B. Bityutskiy 	if (hdr_crc != crc) {
812801c135cSArtem B. Bityutskiy 		if (verbose) {
8139c9ec147SArtem Bityutskiy 			ubi_warn("bad EC header CRC at PEB %d, calculated "
8149c9ec147SArtem Bityutskiy 				 "%#08x, read %#08x", pnum, crc, hdr_crc);
815801c135cSArtem B. Bityutskiy 			ubi_dbg_dump_ec_hdr(ec_hdr);
8166f9fdf62SArtem Bityutskiy 		}
8176f9fdf62SArtem Bityutskiy 		dbg_bld("bad EC header CRC at PEB %d, calculated "
818ed45819fSArtem Bityutskiy 			"%#08x, read %#08x", pnum, crc, hdr_crc);
81992e1a7d9SArtem Bityutskiy 
82092e1a7d9SArtem Bityutskiy 		if (!read_err)
82192e1a7d9SArtem Bityutskiy 			return UBI_IO_BAD_HDR;
82292e1a7d9SArtem Bityutskiy 		else
82392e1a7d9SArtem Bityutskiy 			return UBI_IO_BAD_HDR_EBADMSG;
824801c135cSArtem B. Bityutskiy 	}
825801c135cSArtem B. Bityutskiy 
826801c135cSArtem B. Bityutskiy 	/* And of course validate what has just been read from the media */
827801c135cSArtem B. Bityutskiy 	err = validate_ec_hdr(ubi, ec_hdr);
828801c135cSArtem B. Bityutskiy 	if (err) {
829801c135cSArtem B. Bityutskiy 		ubi_err("validation failed for PEB %d", pnum);
830801c135cSArtem B. Bityutskiy 		return -EINVAL;
831801c135cSArtem B. Bityutskiy 	}
832801c135cSArtem B. Bityutskiy 
833eb89580eSArtem Bityutskiy 	/*
834eb89580eSArtem Bityutskiy 	 * If there was %-EBADMSG, but the header CRC is still OK, report about
835eb89580eSArtem Bityutskiy 	 * a bit-flip to force scrubbing on this PEB.
836eb89580eSArtem Bityutskiy 	 */
837801c135cSArtem B. Bityutskiy 	return read_err ? UBI_IO_BITFLIPS : 0;
838801c135cSArtem B. Bityutskiy }
839801c135cSArtem B. Bityutskiy 
840801c135cSArtem B. Bityutskiy /**
841801c135cSArtem B. Bityutskiy  * ubi_io_write_ec_hdr - write an erase counter header.
842801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
843801c135cSArtem B. Bityutskiy  * @pnum: physical eraseblock to write to
844801c135cSArtem B. Bityutskiy  * @ec_hdr: the erase counter header to write
845801c135cSArtem B. Bityutskiy  *
846801c135cSArtem B. Bityutskiy  * This function writes erase counter header described by @ec_hdr to physical
847801c135cSArtem B. Bityutskiy  * eraseblock @pnum. It also fills most fields of @ec_hdr before writing, so
848801c135cSArtem B. Bityutskiy  * the caller do not have to fill them. Callers must only fill the @ec_hdr->ec
849801c135cSArtem B. Bityutskiy  * field.
850801c135cSArtem B. Bityutskiy  *
851801c135cSArtem B. Bityutskiy  * This function returns zero in case of success and a negative error code in
852801c135cSArtem B. Bityutskiy  * case of failure. If %-EIO is returned, the physical eraseblock most probably
853801c135cSArtem B. Bityutskiy  * went bad.
854801c135cSArtem B. Bityutskiy  */
855e88d6e10SArtem Bityutskiy int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum,
856801c135cSArtem B. Bityutskiy 			struct ubi_ec_hdr *ec_hdr)
857801c135cSArtem B. Bityutskiy {
858801c135cSArtem B. Bityutskiy 	int err;
859801c135cSArtem B. Bityutskiy 	uint32_t crc;
860801c135cSArtem B. Bityutskiy 
861801c135cSArtem B. Bityutskiy 	dbg_io("write EC header to PEB %d", pnum);
862801c135cSArtem B. Bityutskiy 	ubi_assert(pnum >= 0 &&  pnum < ubi->peb_count);
863801c135cSArtem B. Bityutskiy 
8643261ebd7SChristoph Hellwig 	ec_hdr->magic = cpu_to_be32(UBI_EC_HDR_MAGIC);
865801c135cSArtem B. Bityutskiy 	ec_hdr->version = UBI_VERSION;
8663261ebd7SChristoph Hellwig 	ec_hdr->vid_hdr_offset = cpu_to_be32(ubi->vid_hdr_offset);
8673261ebd7SChristoph Hellwig 	ec_hdr->data_offset = cpu_to_be32(ubi->leb_start);
8680c6c7fa1SAdrian Hunter 	ec_hdr->image_seq = cpu_to_be32(ubi->image_seq);
869801c135cSArtem B. Bityutskiy 	crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
8703261ebd7SChristoph Hellwig 	ec_hdr->hdr_crc = cpu_to_be32(crc);
871801c135cSArtem B. Bityutskiy 
872801c135cSArtem B. Bityutskiy 	err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr);
873801c135cSArtem B. Bityutskiy 	if (err)
874adbf05e3SArtem Bityutskiy 		return err;
875801c135cSArtem B. Bityutskiy 
876801c135cSArtem B. Bityutskiy 	err = ubi_io_write(ubi, ec_hdr, pnum, 0, ubi->ec_hdr_alsize);
877801c135cSArtem B. Bityutskiy 	return err;
878801c135cSArtem B. Bityutskiy }
879801c135cSArtem B. Bityutskiy 
880801c135cSArtem B. Bityutskiy /**
881801c135cSArtem B. Bityutskiy  * validate_vid_hdr - validate a volume identifier header.
882801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
883801c135cSArtem B. Bityutskiy  * @vid_hdr: the volume identifier header to check
884801c135cSArtem B. Bityutskiy  *
885801c135cSArtem B. Bityutskiy  * This function checks that data stored in the volume identifier header
886801c135cSArtem B. Bityutskiy  * @vid_hdr. Returns zero if the VID header is OK and %1 if not.
887801c135cSArtem B. Bityutskiy  */
888801c135cSArtem B. Bityutskiy static int validate_vid_hdr(const struct ubi_device *ubi,
889801c135cSArtem B. Bityutskiy 			    const struct ubi_vid_hdr *vid_hdr)
890801c135cSArtem B. Bityutskiy {
891801c135cSArtem B. Bityutskiy 	int vol_type = vid_hdr->vol_type;
892801c135cSArtem B. Bityutskiy 	int copy_flag = vid_hdr->copy_flag;
8933261ebd7SChristoph Hellwig 	int vol_id = be32_to_cpu(vid_hdr->vol_id);
8943261ebd7SChristoph Hellwig 	int lnum = be32_to_cpu(vid_hdr->lnum);
895801c135cSArtem B. Bityutskiy 	int compat = vid_hdr->compat;
8963261ebd7SChristoph Hellwig 	int data_size = be32_to_cpu(vid_hdr->data_size);
8973261ebd7SChristoph Hellwig 	int used_ebs = be32_to_cpu(vid_hdr->used_ebs);
8983261ebd7SChristoph Hellwig 	int data_pad = be32_to_cpu(vid_hdr->data_pad);
8993261ebd7SChristoph Hellwig 	int data_crc = be32_to_cpu(vid_hdr->data_crc);
900801c135cSArtem B. Bityutskiy 	int usable_leb_size = ubi->leb_size - data_pad;
901801c135cSArtem B. Bityutskiy 
902801c135cSArtem B. Bityutskiy 	if (copy_flag != 0 && copy_flag != 1) {
903801c135cSArtem B. Bityutskiy 		dbg_err("bad copy_flag");
904801c135cSArtem B. Bityutskiy 		goto bad;
905801c135cSArtem B. Bityutskiy 	}
906801c135cSArtem B. Bityutskiy 
907801c135cSArtem B. Bityutskiy 	if (vol_id < 0 || lnum < 0 || data_size < 0 || used_ebs < 0 ||
908801c135cSArtem B. Bityutskiy 	    data_pad < 0) {
909801c135cSArtem B. Bityutskiy 		dbg_err("negative values");
910801c135cSArtem B. Bityutskiy 		goto bad;
911801c135cSArtem B. Bityutskiy 	}
912801c135cSArtem B. Bityutskiy 
913801c135cSArtem B. Bityutskiy 	if (vol_id >= UBI_MAX_VOLUMES && vol_id < UBI_INTERNAL_VOL_START) {
914801c135cSArtem B. Bityutskiy 		dbg_err("bad vol_id");
915801c135cSArtem B. Bityutskiy 		goto bad;
916801c135cSArtem B. Bityutskiy 	}
917801c135cSArtem B. Bityutskiy 
918801c135cSArtem B. Bityutskiy 	if (vol_id < UBI_INTERNAL_VOL_START && compat != 0) {
919801c135cSArtem B. Bityutskiy 		dbg_err("bad compat");
920801c135cSArtem B. Bityutskiy 		goto bad;
921801c135cSArtem B. Bityutskiy 	}
922801c135cSArtem B. Bityutskiy 
923801c135cSArtem B. Bityutskiy 	if (vol_id >= UBI_INTERNAL_VOL_START && compat != UBI_COMPAT_DELETE &&
924801c135cSArtem B. Bityutskiy 	    compat != UBI_COMPAT_RO && compat != UBI_COMPAT_PRESERVE &&
925801c135cSArtem B. Bityutskiy 	    compat != UBI_COMPAT_REJECT) {
926801c135cSArtem B. Bityutskiy 		dbg_err("bad compat");
927801c135cSArtem B. Bityutskiy 		goto bad;
928801c135cSArtem B. Bityutskiy 	}
929801c135cSArtem B. Bityutskiy 
930801c135cSArtem B. Bityutskiy 	if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) {
931801c135cSArtem B. Bityutskiy 		dbg_err("bad vol_type");
932801c135cSArtem B. Bityutskiy 		goto bad;
933801c135cSArtem B. Bityutskiy 	}
934801c135cSArtem B. Bityutskiy 
935801c135cSArtem B. Bityutskiy 	if (data_pad >= ubi->leb_size / 2) {
936801c135cSArtem B. Bityutskiy 		dbg_err("bad data_pad");
937801c135cSArtem B. Bityutskiy 		goto bad;
938801c135cSArtem B. Bityutskiy 	}
939801c135cSArtem B. Bityutskiy 
940801c135cSArtem B. Bityutskiy 	if (vol_type == UBI_VID_STATIC) {
941801c135cSArtem B. Bityutskiy 		/*
942801c135cSArtem B. Bityutskiy 		 * Although from high-level point of view static volumes may
943801c135cSArtem B. Bityutskiy 		 * contain zero bytes of data, but no VID headers can contain
944801c135cSArtem B. Bityutskiy 		 * zero at these fields, because they empty volumes do not have
945801c135cSArtem B. Bityutskiy 		 * mapped logical eraseblocks.
946801c135cSArtem B. Bityutskiy 		 */
947801c135cSArtem B. Bityutskiy 		if (used_ebs == 0) {
948801c135cSArtem B. Bityutskiy 			dbg_err("zero used_ebs");
949801c135cSArtem B. Bityutskiy 			goto bad;
950801c135cSArtem B. Bityutskiy 		}
951801c135cSArtem B. Bityutskiy 		if (data_size == 0) {
952801c135cSArtem B. Bityutskiy 			dbg_err("zero data_size");
953801c135cSArtem B. Bityutskiy 			goto bad;
954801c135cSArtem B. Bityutskiy 		}
955801c135cSArtem B. Bityutskiy 		if (lnum < used_ebs - 1) {
956801c135cSArtem B. Bityutskiy 			if (data_size != usable_leb_size) {
957801c135cSArtem B. Bityutskiy 				dbg_err("bad data_size");
958801c135cSArtem B. Bityutskiy 				goto bad;
959801c135cSArtem B. Bityutskiy 			}
960801c135cSArtem B. Bityutskiy 		} else if (lnum == used_ebs - 1) {
961801c135cSArtem B. Bityutskiy 			if (data_size == 0) {
962801c135cSArtem B. Bityutskiy 				dbg_err("bad data_size at last LEB");
963801c135cSArtem B. Bityutskiy 				goto bad;
964801c135cSArtem B. Bityutskiy 			}
965801c135cSArtem B. Bityutskiy 		} else {
966801c135cSArtem B. Bityutskiy 			dbg_err("too high lnum");
967801c135cSArtem B. Bityutskiy 			goto bad;
968801c135cSArtem B. Bityutskiy 		}
969801c135cSArtem B. Bityutskiy 	} else {
970801c135cSArtem B. Bityutskiy 		if (copy_flag == 0) {
971801c135cSArtem B. Bityutskiy 			if (data_crc != 0) {
972801c135cSArtem B. Bityutskiy 				dbg_err("non-zero data CRC");
973801c135cSArtem B. Bityutskiy 				goto bad;
974801c135cSArtem B. Bityutskiy 			}
975801c135cSArtem B. Bityutskiy 			if (data_size != 0) {
976801c135cSArtem B. Bityutskiy 				dbg_err("non-zero data_size");
977801c135cSArtem B. Bityutskiy 				goto bad;
978801c135cSArtem B. Bityutskiy 			}
979801c135cSArtem B. Bityutskiy 		} else {
980801c135cSArtem B. Bityutskiy 			if (data_size == 0) {
981801c135cSArtem B. Bityutskiy 				dbg_err("zero data_size of copy");
982801c135cSArtem B. Bityutskiy 				goto bad;
983801c135cSArtem B. Bityutskiy 			}
984801c135cSArtem B. Bityutskiy 		}
985801c135cSArtem B. Bityutskiy 		if (used_ebs != 0) {
986801c135cSArtem B. Bityutskiy 			dbg_err("bad used_ebs");
987801c135cSArtem B. Bityutskiy 			goto bad;
988801c135cSArtem B. Bityutskiy 		}
989801c135cSArtem B. Bityutskiy 	}
990801c135cSArtem B. Bityutskiy 
991801c135cSArtem B. Bityutskiy 	return 0;
992801c135cSArtem B. Bityutskiy 
993801c135cSArtem B. Bityutskiy bad:
994801c135cSArtem B. Bityutskiy 	ubi_err("bad VID header");
995801c135cSArtem B. Bityutskiy 	ubi_dbg_dump_vid_hdr(vid_hdr);
996801c135cSArtem B. Bityutskiy 	ubi_dbg_dump_stack();
997801c135cSArtem B. Bityutskiy 	return 1;
998801c135cSArtem B. Bityutskiy }
999801c135cSArtem B. Bityutskiy 
1000801c135cSArtem B. Bityutskiy /**
1001801c135cSArtem B. Bityutskiy  * ubi_io_read_vid_hdr - read and check a volume identifier header.
1002801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
1003801c135cSArtem B. Bityutskiy  * @pnum: physical eraseblock number to read from
1004801c135cSArtem B. Bityutskiy  * @vid_hdr: &struct ubi_vid_hdr object where to store the read volume
1005801c135cSArtem B. Bityutskiy  * identifier header
1006801c135cSArtem B. Bityutskiy  * @verbose: be verbose if the header is corrupted or wasn't found
1007801c135cSArtem B. Bityutskiy  *
1008801c135cSArtem B. Bityutskiy  * This function reads the volume identifier header from physical eraseblock
1009801c135cSArtem B. Bityutskiy  * @pnum and stores it in @vid_hdr. It also checks CRC checksum of the read
101074d82d26SArtem Bityutskiy  * volume identifier header. The error codes are the same as in
101174d82d26SArtem Bityutskiy  * 'ubi_io_read_ec_hdr()'.
1012801c135cSArtem B. Bityutskiy  *
101374d82d26SArtem Bityutskiy  * Note, the implementation of this function is also very similar to
101474d82d26SArtem Bityutskiy  * 'ubi_io_read_ec_hdr()', so refer commentaries in 'ubi_io_read_ec_hdr()'.
1015801c135cSArtem B. Bityutskiy  */
1016e88d6e10SArtem Bityutskiy int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
1017801c135cSArtem B. Bityutskiy 			struct ubi_vid_hdr *vid_hdr, int verbose)
1018801c135cSArtem B. Bityutskiy {
101992e1a7d9SArtem Bityutskiy 	int err, read_err;
1020801c135cSArtem B. Bityutskiy 	uint32_t crc, magic, hdr_crc;
1021801c135cSArtem B. Bityutskiy 	void *p;
1022801c135cSArtem B. Bityutskiy 
1023801c135cSArtem B. Bityutskiy 	dbg_io("read VID header from PEB %d", pnum);
1024801c135cSArtem B. Bityutskiy 	ubi_assert(pnum >= 0 &&  pnum < ubi->peb_count);
1025801c135cSArtem B. Bityutskiy 
1026801c135cSArtem B. Bityutskiy 	p = (char *)vid_hdr - ubi->vid_hdr_shift;
102792e1a7d9SArtem Bityutskiy 	read_err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset,
1028801c135cSArtem B. Bityutskiy 			  ubi->vid_hdr_alsize);
102992e1a7d9SArtem Bityutskiy 	if (read_err && read_err != UBI_IO_BITFLIPS && read_err != -EBADMSG)
103092e1a7d9SArtem Bityutskiy 		return read_err;
1031801c135cSArtem B. Bityutskiy 
10323261ebd7SChristoph Hellwig 	magic = be32_to_cpu(vid_hdr->magic);
1033801c135cSArtem B. Bityutskiy 	if (magic != UBI_VID_HDR_MAGIC) {
103492e1a7d9SArtem Bityutskiy 		if (read_err == -EBADMSG)
103592e1a7d9SArtem Bityutskiy 			return UBI_IO_BAD_HDR_EBADMSG;
1036eb89580eSArtem Bityutskiy 
1037bb00e180SArtem Bityutskiy 		if (ubi_check_pattern(vid_hdr, 0xFF, UBI_VID_HDR_SIZE)) {
1038801c135cSArtem B. Bityutskiy 			if (verbose)
1039801c135cSArtem B. Bityutskiy 				ubi_warn("no VID header found at PEB %d, "
1040801c135cSArtem B. Bityutskiy 					 "only 0xFF bytes", pnum);
10416f9fdf62SArtem Bityutskiy 			dbg_bld("no VID header found at PEB %d, "
1042ed45819fSArtem Bityutskiy 				"only 0xFF bytes", pnum);
104392e1a7d9SArtem Bityutskiy 			if (!read_err)
104474d82d26SArtem Bityutskiy 				return UBI_IO_FF;
104592e1a7d9SArtem Bityutskiy 			else
104692e1a7d9SArtem Bityutskiy 				return UBI_IO_FF_BITFLIPS;
1047801c135cSArtem B. Bityutskiy 		}
1048801c135cSArtem B. Bityutskiy 
1049801c135cSArtem B. Bityutskiy 		if (verbose) {
1050801c135cSArtem B. Bityutskiy 			ubi_warn("bad magic number at PEB %d: %08x instead of "
1051801c135cSArtem B. Bityutskiy 				 "%08x", pnum, magic, UBI_VID_HDR_MAGIC);
1052801c135cSArtem B. Bityutskiy 			ubi_dbg_dump_vid_hdr(vid_hdr);
10536f9fdf62SArtem Bityutskiy 		}
10546f9fdf62SArtem Bityutskiy 		dbg_bld("bad magic number at PEB %d: %08x instead of "
1055ed45819fSArtem Bityutskiy 			"%08x", pnum, magic, UBI_VID_HDR_MAGIC);
1056786d7831SArtem Bityutskiy 		return UBI_IO_BAD_HDR;
1057801c135cSArtem B. Bityutskiy 	}
1058801c135cSArtem B. Bityutskiy 
1059801c135cSArtem B. Bityutskiy 	crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC);
10603261ebd7SChristoph Hellwig 	hdr_crc = be32_to_cpu(vid_hdr->hdr_crc);
1061801c135cSArtem B. Bityutskiy 
1062801c135cSArtem B. Bityutskiy 	if (hdr_crc != crc) {
1063801c135cSArtem B. Bityutskiy 		if (verbose) {
1064801c135cSArtem B. Bityutskiy 			ubi_warn("bad CRC at PEB %d, calculated %#08x, "
1065801c135cSArtem B. Bityutskiy 				 "read %#08x", pnum, crc, hdr_crc);
1066801c135cSArtem B. Bityutskiy 			ubi_dbg_dump_vid_hdr(vid_hdr);
10676f9fdf62SArtem Bityutskiy 		}
10686f9fdf62SArtem Bityutskiy 		dbg_bld("bad CRC at PEB %d, calculated %#08x, "
1069ed45819fSArtem Bityutskiy 			"read %#08x", pnum, crc, hdr_crc);
107092e1a7d9SArtem Bityutskiy 		if (!read_err)
107192e1a7d9SArtem Bityutskiy 			return UBI_IO_BAD_HDR;
107292e1a7d9SArtem Bityutskiy 		else
107392e1a7d9SArtem Bityutskiy 			return UBI_IO_BAD_HDR_EBADMSG;
1074801c135cSArtem B. Bityutskiy 	}
1075801c135cSArtem B. Bityutskiy 
1076801c135cSArtem B. Bityutskiy 	err = validate_vid_hdr(ubi, vid_hdr);
1077801c135cSArtem B. Bityutskiy 	if (err) {
1078801c135cSArtem B. Bityutskiy 		ubi_err("validation failed for PEB %d", pnum);
1079801c135cSArtem B. Bityutskiy 		return -EINVAL;
1080801c135cSArtem B. Bityutskiy 	}
1081801c135cSArtem B. Bityutskiy 
1082801c135cSArtem B. Bityutskiy 	return read_err ? UBI_IO_BITFLIPS : 0;
1083801c135cSArtem B. Bityutskiy }
1084801c135cSArtem B. Bityutskiy 
1085801c135cSArtem B. Bityutskiy /**
1086801c135cSArtem B. Bityutskiy  * ubi_io_write_vid_hdr - write a volume identifier header.
1087801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
1088801c135cSArtem B. Bityutskiy  * @pnum: the physical eraseblock number to write to
1089801c135cSArtem B. Bityutskiy  * @vid_hdr: the volume identifier header to write
1090801c135cSArtem B. Bityutskiy  *
1091801c135cSArtem B. Bityutskiy  * This function writes the volume identifier header described by @vid_hdr to
1092801c135cSArtem B. Bityutskiy  * physical eraseblock @pnum. This function automatically fills the
1093801c135cSArtem B. Bityutskiy  * @vid_hdr->magic and the @vid_hdr->version fields, as well as calculates
1094801c135cSArtem B. Bityutskiy  * header CRC checksum and stores it at vid_hdr->hdr_crc.
1095801c135cSArtem B. Bityutskiy  *
1096801c135cSArtem B. Bityutskiy  * This function returns zero in case of success and a negative error code in
1097801c135cSArtem B. Bityutskiy  * case of failure. If %-EIO is returned, the physical eraseblock probably went
1098801c135cSArtem B. Bityutskiy  * bad.
1099801c135cSArtem B. Bityutskiy  */
1100e88d6e10SArtem Bityutskiy int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
1101801c135cSArtem B. Bityutskiy 			 struct ubi_vid_hdr *vid_hdr)
1102801c135cSArtem B. Bityutskiy {
1103801c135cSArtem B. Bityutskiy 	int err;
1104801c135cSArtem B. Bityutskiy 	uint32_t crc;
1105801c135cSArtem B. Bityutskiy 	void *p;
1106801c135cSArtem B. Bityutskiy 
1107801c135cSArtem B. Bityutskiy 	dbg_io("write VID header to PEB %d", pnum);
1108801c135cSArtem B. Bityutskiy 	ubi_assert(pnum >= 0 &&  pnum < ubi->peb_count);
1109801c135cSArtem B. Bityutskiy 
1110801c135cSArtem B. Bityutskiy 	err = paranoid_check_peb_ec_hdr(ubi, pnum);
1111801c135cSArtem B. Bityutskiy 	if (err)
1112adbf05e3SArtem Bityutskiy 		return err;
1113801c135cSArtem B. Bityutskiy 
11143261ebd7SChristoph Hellwig 	vid_hdr->magic = cpu_to_be32(UBI_VID_HDR_MAGIC);
1115801c135cSArtem B. Bityutskiy 	vid_hdr->version = UBI_VERSION;
1116801c135cSArtem B. Bityutskiy 	crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC);
11173261ebd7SChristoph Hellwig 	vid_hdr->hdr_crc = cpu_to_be32(crc);
1118801c135cSArtem B. Bityutskiy 
1119801c135cSArtem B. Bityutskiy 	err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr);
1120801c135cSArtem B. Bityutskiy 	if (err)
1121adbf05e3SArtem Bityutskiy 		return err;
1122801c135cSArtem B. Bityutskiy 
1123801c135cSArtem B. Bityutskiy 	p = (char *)vid_hdr - ubi->vid_hdr_shift;
1124801c135cSArtem B. Bityutskiy 	err = ubi_io_write(ubi, p, pnum, ubi->vid_hdr_aloffset,
1125801c135cSArtem B. Bityutskiy 			   ubi->vid_hdr_alsize);
1126801c135cSArtem B. Bityutskiy 	return err;
1127801c135cSArtem B. Bityutskiy }
1128801c135cSArtem B. Bityutskiy 
112992d124f5SArtem Bityutskiy #ifdef CONFIG_MTD_UBI_DEBUG
1130801c135cSArtem B. Bityutskiy 
1131801c135cSArtem B. Bityutskiy /**
1132801c135cSArtem B. Bityutskiy  * paranoid_check_not_bad - ensure that a physical eraseblock is not bad.
1133801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
1134801c135cSArtem B. Bityutskiy  * @pnum: physical eraseblock number to check
1135801c135cSArtem B. Bityutskiy  *
1136adbf05e3SArtem Bityutskiy  * This function returns zero if the physical eraseblock is good, %-EINVAL if
1137adbf05e3SArtem Bityutskiy  * it is bad and a negative error code if an error occurred.
1138801c135cSArtem B. Bityutskiy  */
1139801c135cSArtem B. Bityutskiy static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum)
1140801c135cSArtem B. Bityutskiy {
1141801c135cSArtem B. Bityutskiy 	int err;
1142801c135cSArtem B. Bityutskiy 
114392d124f5SArtem Bityutskiy 	if (!(ubi_chk_flags & UBI_CHK_IO))
114492d124f5SArtem Bityutskiy 		return 0;
114592d124f5SArtem Bityutskiy 
1146801c135cSArtem B. Bityutskiy 	err = ubi_io_is_bad(ubi, pnum);
1147801c135cSArtem B. Bityutskiy 	if (!err)
1148801c135cSArtem B. Bityutskiy 		return err;
1149801c135cSArtem B. Bityutskiy 
1150801c135cSArtem B. Bityutskiy 	ubi_err("paranoid check failed for PEB %d", pnum);
1151801c135cSArtem B. Bityutskiy 	ubi_dbg_dump_stack();
1152adbf05e3SArtem Bityutskiy 	return err > 0 ? -EINVAL : err;
1153801c135cSArtem B. Bityutskiy }
1154801c135cSArtem B. Bityutskiy 
1155801c135cSArtem B. Bityutskiy /**
1156801c135cSArtem B. Bityutskiy  * paranoid_check_ec_hdr - check if an erase counter header is all right.
1157801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
1158801c135cSArtem B. Bityutskiy  * @pnum: physical eraseblock number the erase counter header belongs to
1159801c135cSArtem B. Bityutskiy  * @ec_hdr: the erase counter header to check
1160801c135cSArtem B. Bityutskiy  *
1161801c135cSArtem B. Bityutskiy  * This function returns zero if the erase counter header contains valid
1162adbf05e3SArtem Bityutskiy  * values, and %-EINVAL if not.
1163801c135cSArtem B. Bityutskiy  */
1164801c135cSArtem B. Bityutskiy static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum,
1165801c135cSArtem B. Bityutskiy 				 const struct ubi_ec_hdr *ec_hdr)
1166801c135cSArtem B. Bityutskiy {
1167801c135cSArtem B. Bityutskiy 	int err;
1168801c135cSArtem B. Bityutskiy 	uint32_t magic;
1169801c135cSArtem B. Bityutskiy 
117092d124f5SArtem Bityutskiy 	if (!(ubi_chk_flags & UBI_CHK_IO))
117192d124f5SArtem Bityutskiy 		return 0;
117292d124f5SArtem Bityutskiy 
11733261ebd7SChristoph Hellwig 	magic = be32_to_cpu(ec_hdr->magic);
1174801c135cSArtem B. Bityutskiy 	if (magic != UBI_EC_HDR_MAGIC) {
1175801c135cSArtem B. Bityutskiy 		ubi_err("bad magic %#08x, must be %#08x",
1176801c135cSArtem B. Bityutskiy 			magic, UBI_EC_HDR_MAGIC);
1177801c135cSArtem B. Bityutskiy 		goto fail;
1178801c135cSArtem B. Bityutskiy 	}
1179801c135cSArtem B. Bityutskiy 
1180801c135cSArtem B. Bityutskiy 	err = validate_ec_hdr(ubi, ec_hdr);
1181801c135cSArtem B. Bityutskiy 	if (err) {
1182801c135cSArtem B. Bityutskiy 		ubi_err("paranoid check failed for PEB %d", pnum);
1183801c135cSArtem B. Bityutskiy 		goto fail;
1184801c135cSArtem B. Bityutskiy 	}
1185801c135cSArtem B. Bityutskiy 
1186801c135cSArtem B. Bityutskiy 	return 0;
1187801c135cSArtem B. Bityutskiy 
1188801c135cSArtem B. Bityutskiy fail:
1189801c135cSArtem B. Bityutskiy 	ubi_dbg_dump_ec_hdr(ec_hdr);
1190801c135cSArtem B. Bityutskiy 	ubi_dbg_dump_stack();
1191adbf05e3SArtem Bityutskiy 	return -EINVAL;
1192801c135cSArtem B. Bityutskiy }
1193801c135cSArtem B. Bityutskiy 
1194801c135cSArtem B. Bityutskiy /**
1195ebaaf1afSArtem Bityutskiy  * paranoid_check_peb_ec_hdr - check erase counter header.
1196801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
1197801c135cSArtem B. Bityutskiy  * @pnum: the physical eraseblock number to check
1198801c135cSArtem B. Bityutskiy  *
1199adbf05e3SArtem Bityutskiy  * This function returns zero if the erase counter header is all right and and
1200adbf05e3SArtem Bityutskiy  * a negative error code if not or if an error occurred.
1201801c135cSArtem B. Bityutskiy  */
1202801c135cSArtem B. Bityutskiy static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum)
1203801c135cSArtem B. Bityutskiy {
1204801c135cSArtem B. Bityutskiy 	int err;
1205801c135cSArtem B. Bityutskiy 	uint32_t crc, hdr_crc;
1206801c135cSArtem B. Bityutskiy 	struct ubi_ec_hdr *ec_hdr;
1207801c135cSArtem B. Bityutskiy 
120892d124f5SArtem Bityutskiy 	if (!(ubi_chk_flags & UBI_CHK_IO))
120992d124f5SArtem Bityutskiy 		return 0;
121092d124f5SArtem Bityutskiy 
121133818bbbSArtem Bityutskiy 	ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
1212801c135cSArtem B. Bityutskiy 	if (!ec_hdr)
1213801c135cSArtem B. Bityutskiy 		return -ENOMEM;
1214801c135cSArtem B. Bityutskiy 
1215801c135cSArtem B. Bityutskiy 	err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE);
1216801c135cSArtem B. Bityutskiy 	if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG)
1217801c135cSArtem B. Bityutskiy 		goto exit;
1218801c135cSArtem B. Bityutskiy 
1219801c135cSArtem B. Bityutskiy 	crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
12203261ebd7SChristoph Hellwig 	hdr_crc = be32_to_cpu(ec_hdr->hdr_crc);
1221801c135cSArtem B. Bityutskiy 	if (hdr_crc != crc) {
1222801c135cSArtem B. Bityutskiy 		ubi_err("bad CRC, calculated %#08x, read %#08x", crc, hdr_crc);
1223801c135cSArtem B. Bityutskiy 		ubi_err("paranoid check failed for PEB %d", pnum);
1224801c135cSArtem B. Bityutskiy 		ubi_dbg_dump_ec_hdr(ec_hdr);
1225801c135cSArtem B. Bityutskiy 		ubi_dbg_dump_stack();
1226adbf05e3SArtem Bityutskiy 		err = -EINVAL;
1227801c135cSArtem B. Bityutskiy 		goto exit;
1228801c135cSArtem B. Bityutskiy 	}
1229801c135cSArtem B. Bityutskiy 
1230801c135cSArtem B. Bityutskiy 	err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr);
1231801c135cSArtem B. Bityutskiy 
1232801c135cSArtem B. Bityutskiy exit:
1233801c135cSArtem B. Bityutskiy 	kfree(ec_hdr);
1234801c135cSArtem B. Bityutskiy 	return err;
1235801c135cSArtem B. Bityutskiy }
1236801c135cSArtem B. Bityutskiy 
1237801c135cSArtem B. Bityutskiy /**
1238801c135cSArtem B. Bityutskiy  * paranoid_check_vid_hdr - check that a volume identifier header is all right.
1239801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
1240801c135cSArtem B. Bityutskiy  * @pnum: physical eraseblock number the volume identifier header belongs to
1241801c135cSArtem B. Bityutskiy  * @vid_hdr: the volume identifier header to check
1242801c135cSArtem B. Bityutskiy  *
1243801c135cSArtem B. Bityutskiy  * This function returns zero if the volume identifier header is all right, and
1244adbf05e3SArtem Bityutskiy  * %-EINVAL if not.
1245801c135cSArtem B. Bityutskiy  */
1246801c135cSArtem B. Bityutskiy static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum,
1247801c135cSArtem B. Bityutskiy 				  const struct ubi_vid_hdr *vid_hdr)
1248801c135cSArtem B. Bityutskiy {
1249801c135cSArtem B. Bityutskiy 	int err;
1250801c135cSArtem B. Bityutskiy 	uint32_t magic;
1251801c135cSArtem B. Bityutskiy 
125292d124f5SArtem Bityutskiy 	if (!(ubi_chk_flags & UBI_CHK_IO))
125392d124f5SArtem Bityutskiy 		return 0;
125492d124f5SArtem Bityutskiy 
12553261ebd7SChristoph Hellwig 	magic = be32_to_cpu(vid_hdr->magic);
1256801c135cSArtem B. Bityutskiy 	if (magic != UBI_VID_HDR_MAGIC) {
1257801c135cSArtem B. Bityutskiy 		ubi_err("bad VID header magic %#08x at PEB %d, must be %#08x",
1258801c135cSArtem B. Bityutskiy 			magic, pnum, UBI_VID_HDR_MAGIC);
1259801c135cSArtem B. Bityutskiy 		goto fail;
1260801c135cSArtem B. Bityutskiy 	}
1261801c135cSArtem B. Bityutskiy 
1262801c135cSArtem B. Bityutskiy 	err = validate_vid_hdr(ubi, vid_hdr);
1263801c135cSArtem B. Bityutskiy 	if (err) {
1264801c135cSArtem B. Bityutskiy 		ubi_err("paranoid check failed for PEB %d", pnum);
1265801c135cSArtem B. Bityutskiy 		goto fail;
1266801c135cSArtem B. Bityutskiy 	}
1267801c135cSArtem B. Bityutskiy 
1268801c135cSArtem B. Bityutskiy 	return err;
1269801c135cSArtem B. Bityutskiy 
1270801c135cSArtem B. Bityutskiy fail:
1271801c135cSArtem B. Bityutskiy 	ubi_err("paranoid check failed for PEB %d", pnum);
1272801c135cSArtem B. Bityutskiy 	ubi_dbg_dump_vid_hdr(vid_hdr);
1273801c135cSArtem B. Bityutskiy 	ubi_dbg_dump_stack();
1274adbf05e3SArtem Bityutskiy 	return -EINVAL;
1275801c135cSArtem B. Bityutskiy 
1276801c135cSArtem B. Bityutskiy }
1277801c135cSArtem B. Bityutskiy 
1278801c135cSArtem B. Bityutskiy /**
1279ebaaf1afSArtem Bityutskiy  * paranoid_check_peb_vid_hdr - check volume identifier header.
1280801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
1281801c135cSArtem B. Bityutskiy  * @pnum: the physical eraseblock number to check
1282801c135cSArtem B. Bityutskiy  *
1283801c135cSArtem B. Bityutskiy  * This function returns zero if the volume identifier header is all right,
1284adbf05e3SArtem Bityutskiy  * and a negative error code if not or if an error occurred.
1285801c135cSArtem B. Bityutskiy  */
1286801c135cSArtem B. Bityutskiy static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum)
1287801c135cSArtem B. Bityutskiy {
1288801c135cSArtem B. Bityutskiy 	int err;
1289801c135cSArtem B. Bityutskiy 	uint32_t crc, hdr_crc;
1290801c135cSArtem B. Bityutskiy 	struct ubi_vid_hdr *vid_hdr;
1291801c135cSArtem B. Bityutskiy 	void *p;
1292801c135cSArtem B. Bityutskiy 
129392d124f5SArtem Bityutskiy 	if (!(ubi_chk_flags & UBI_CHK_IO))
129492d124f5SArtem Bityutskiy 		return 0;
129592d124f5SArtem Bityutskiy 
129633818bbbSArtem Bityutskiy 	vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
1297801c135cSArtem B. Bityutskiy 	if (!vid_hdr)
1298801c135cSArtem B. Bityutskiy 		return -ENOMEM;
1299801c135cSArtem B. Bityutskiy 
1300801c135cSArtem B. Bityutskiy 	p = (char *)vid_hdr - ubi->vid_hdr_shift;
1301801c135cSArtem B. Bityutskiy 	err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset,
1302801c135cSArtem B. Bityutskiy 			  ubi->vid_hdr_alsize);
1303801c135cSArtem B. Bityutskiy 	if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG)
1304801c135cSArtem B. Bityutskiy 		goto exit;
1305801c135cSArtem B. Bityutskiy 
1306801c135cSArtem B. Bityutskiy 	crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_EC_HDR_SIZE_CRC);
13073261ebd7SChristoph Hellwig 	hdr_crc = be32_to_cpu(vid_hdr->hdr_crc);
1308801c135cSArtem B. Bityutskiy 	if (hdr_crc != crc) {
1309801c135cSArtem B. Bityutskiy 		ubi_err("bad VID header CRC at PEB %d, calculated %#08x, "
1310801c135cSArtem B. Bityutskiy 			"read %#08x", pnum, crc, hdr_crc);
1311801c135cSArtem B. Bityutskiy 		ubi_err("paranoid check failed for PEB %d", pnum);
1312801c135cSArtem B. Bityutskiy 		ubi_dbg_dump_vid_hdr(vid_hdr);
1313801c135cSArtem B. Bityutskiy 		ubi_dbg_dump_stack();
1314adbf05e3SArtem Bityutskiy 		err = -EINVAL;
1315801c135cSArtem B. Bityutskiy 		goto exit;
1316801c135cSArtem B. Bityutskiy 	}
1317801c135cSArtem B. Bityutskiy 
1318801c135cSArtem B. Bityutskiy 	err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr);
1319801c135cSArtem B. Bityutskiy 
1320801c135cSArtem B. Bityutskiy exit:
1321801c135cSArtem B. Bityutskiy 	ubi_free_vid_hdr(ubi, vid_hdr);
1322801c135cSArtem B. Bityutskiy 	return err;
1323801c135cSArtem B. Bityutskiy }
1324801c135cSArtem B. Bityutskiy 
1325801c135cSArtem B. Bityutskiy /**
13266e9065d7SArtem Bityutskiy  * ubi_dbg_check_write - make sure write succeeded.
13276e9065d7SArtem Bityutskiy  * @ubi: UBI device description object
13286e9065d7SArtem Bityutskiy  * @buf: buffer with data which were written
13296e9065d7SArtem Bityutskiy  * @pnum: physical eraseblock number the data were written to
13306e9065d7SArtem Bityutskiy  * @offset: offset within the physical eraseblock the data were written to
13316e9065d7SArtem Bityutskiy  * @len: how many bytes were written
13326e9065d7SArtem Bityutskiy  *
13336e9065d7SArtem Bityutskiy  * This functions reads data which were recently written and compares it with
13346e9065d7SArtem Bityutskiy  * the original data buffer - the data have to match. Returns zero if the data
13356e9065d7SArtem Bityutskiy  * match and a negative error code if not or in case of failure.
13366e9065d7SArtem Bityutskiy  */
13376e9065d7SArtem Bityutskiy int ubi_dbg_check_write(struct ubi_device *ubi, const void *buf, int pnum,
13386e9065d7SArtem Bityutskiy 			int offset, int len)
13396e9065d7SArtem Bityutskiy {
13406e9065d7SArtem Bityutskiy 	int err, i;
13417950d023SArtem Bityutskiy 	size_t read;
1342a7586743SArtem Bityutskiy 	void *buf1;
13437950d023SArtem Bityutskiy 	loff_t addr = (loff_t)pnum * ubi->peb_size + offset;
13446e9065d7SArtem Bityutskiy 
134592d124f5SArtem Bityutskiy 	if (!(ubi_chk_flags & UBI_CHK_IO))
134692d124f5SArtem Bityutskiy 		return 0;
134792d124f5SArtem Bityutskiy 
1348a7586743SArtem Bityutskiy 	buf1 = __vmalloc(len, GFP_KERNEL | GFP_NOFS, PAGE_KERNEL);
1349a7586743SArtem Bityutskiy 	if (!buf1) {
1350a7586743SArtem Bityutskiy 		ubi_err("cannot allocate memory to check writes");
1351a7586743SArtem Bityutskiy 		return 0;
1352a7586743SArtem Bityutskiy 	}
1353a7586743SArtem Bityutskiy 
1354a7586743SArtem Bityutskiy 	err = ubi->mtd->read(ubi->mtd, addr, len, &read, buf1);
13557950d023SArtem Bityutskiy 	if (err && err != -EUCLEAN)
1356a7586743SArtem Bityutskiy 		goto out_free;
13576e9065d7SArtem Bityutskiy 
13586e9065d7SArtem Bityutskiy 	for (i = 0; i < len; i++) {
13596e9065d7SArtem Bityutskiy 		uint8_t c = ((uint8_t *)buf)[i];
1360a7586743SArtem Bityutskiy 		uint8_t c1 = ((uint8_t *)buf1)[i];
13616e9065d7SArtem Bityutskiy 		int dump_len;
13626e9065d7SArtem Bityutskiy 
13636e9065d7SArtem Bityutskiy 		if (c == c1)
13646e9065d7SArtem Bityutskiy 			continue;
13656e9065d7SArtem Bityutskiy 
13666e9065d7SArtem Bityutskiy 		ubi_err("paranoid check failed for PEB %d:%d, len %d",
13676e9065d7SArtem Bityutskiy 			pnum, offset, len);
13686e9065d7SArtem Bityutskiy 		ubi_msg("data differ at position %d", i);
13696e9065d7SArtem Bityutskiy 		dump_len = max_t(int, 128, len - i);
13706e9065d7SArtem Bityutskiy 		ubi_msg("hex dump of the original buffer from %d to %d",
13716e9065d7SArtem Bityutskiy 			i, i + dump_len);
13726e9065d7SArtem Bityutskiy 		print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
13736e9065d7SArtem Bityutskiy 			       buf + i, dump_len, 1);
13746e9065d7SArtem Bityutskiy 		ubi_msg("hex dump of the read buffer from %d to %d",
13756e9065d7SArtem Bityutskiy 			i, i + dump_len);
13766e9065d7SArtem Bityutskiy 		print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
1377a7586743SArtem Bityutskiy 			       buf1 + i, dump_len, 1);
13786e9065d7SArtem Bityutskiy 		ubi_dbg_dump_stack();
13796e9065d7SArtem Bityutskiy 		err = -EINVAL;
1380a7586743SArtem Bityutskiy 		goto out_free;
13816e9065d7SArtem Bityutskiy 	}
13826e9065d7SArtem Bityutskiy 
1383a7586743SArtem Bityutskiy 	vfree(buf1);
13846e9065d7SArtem Bityutskiy 	return 0;
13856e9065d7SArtem Bityutskiy 
1386a7586743SArtem Bityutskiy out_free:
1387a7586743SArtem Bityutskiy 	vfree(buf1);
13886e9065d7SArtem Bityutskiy 	return err;
13896e9065d7SArtem Bityutskiy }
13906e9065d7SArtem Bityutskiy 
13916e9065d7SArtem Bityutskiy /**
139240a71a87SArtem Bityutskiy  * ubi_dbg_check_all_ff - check that a region of flash is empty.
1393801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
1394801c135cSArtem B. Bityutskiy  * @pnum: the physical eraseblock number to check
1395801c135cSArtem B. Bityutskiy  * @offset: the starting offset within the physical eraseblock to check
1396801c135cSArtem B. Bityutskiy  * @len: the length of the region to check
1397801c135cSArtem B. Bityutskiy  *
1398801c135cSArtem B. Bityutskiy  * This function returns zero if only 0xFF bytes are present at offset
1399adbf05e3SArtem Bityutskiy  * @offset of the physical eraseblock @pnum, and a negative error code if not
1400adbf05e3SArtem Bityutskiy  * or if an error occurred.
1401801c135cSArtem B. Bityutskiy  */
140240a71a87SArtem Bityutskiy int ubi_dbg_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len)
1403801c135cSArtem B. Bityutskiy {
1404801c135cSArtem B. Bityutskiy 	size_t read;
1405801c135cSArtem B. Bityutskiy 	int err;
1406332873d6SArtem Bityutskiy 	void *buf;
1407801c135cSArtem B. Bityutskiy 	loff_t addr = (loff_t)pnum * ubi->peb_size + offset;
1408801c135cSArtem B. Bityutskiy 
140992d124f5SArtem Bityutskiy 	if (!(ubi_chk_flags & UBI_CHK_IO))
141092d124f5SArtem Bityutskiy 		return 0;
141192d124f5SArtem Bityutskiy 
1412332873d6SArtem Bityutskiy 	buf = __vmalloc(len, GFP_KERNEL | GFP_NOFS, PAGE_KERNEL);
1413332873d6SArtem Bityutskiy 	if (!buf) {
1414332873d6SArtem Bityutskiy 		ubi_err("cannot allocate memory to check for 0xFFs");
1415332873d6SArtem Bityutskiy 		return 0;
1416332873d6SArtem Bityutskiy 	}
1417332873d6SArtem Bityutskiy 
1418332873d6SArtem Bityutskiy 	err = ubi->mtd->read(ubi->mtd, addr, len, &read, buf);
1419801c135cSArtem B. Bityutskiy 	if (err && err != -EUCLEAN) {
1420801c135cSArtem B. Bityutskiy 		ubi_err("error %d while reading %d bytes from PEB %d:%d, "
1421801c135cSArtem B. Bityutskiy 			"read %zd bytes", err, len, pnum, offset, read);
1422801c135cSArtem B. Bityutskiy 		goto error;
1423801c135cSArtem B. Bityutskiy 	}
1424801c135cSArtem B. Bityutskiy 
1425332873d6SArtem Bityutskiy 	err = ubi_check_pattern(buf, 0xFF, len);
1426801c135cSArtem B. Bityutskiy 	if (err == 0) {
1427801c135cSArtem B. Bityutskiy 		ubi_err("flash region at PEB %d:%d, length %d does not "
1428801c135cSArtem B. Bityutskiy 			"contain all 0xFF bytes", pnum, offset, len);
1429801c135cSArtem B. Bityutskiy 		goto fail;
1430801c135cSArtem B. Bityutskiy 	}
1431801c135cSArtem B. Bityutskiy 
1432332873d6SArtem Bityutskiy 	vfree(buf);
1433801c135cSArtem B. Bityutskiy 	return 0;
1434801c135cSArtem B. Bityutskiy 
1435801c135cSArtem B. Bityutskiy fail:
1436801c135cSArtem B. Bityutskiy 	ubi_err("paranoid check failed for PEB %d", pnum);
1437c8566350SArtem Bityutskiy 	ubi_msg("hex dump of the %d-%d region", offset, offset + len);
1438332873d6SArtem Bityutskiy 	print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, buf, len, 1);
1439adbf05e3SArtem Bityutskiy 	err = -EINVAL;
1440801c135cSArtem B. Bityutskiy error:
1441801c135cSArtem B. Bityutskiy 	ubi_dbg_dump_stack();
1442332873d6SArtem Bityutskiy 	vfree(buf);
1443801c135cSArtem B. Bityutskiy 	return err;
1444801c135cSArtem B. Bityutskiy }
1445801c135cSArtem B. Bityutskiy 
144692d124f5SArtem Bityutskiy #endif /* CONFIG_MTD_UBI_DEBUG */
1447