xref: /openbmc/linux/drivers/mtd/ubi/io.c (revision ffb6b7e4)
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
67801c135cSArtem B. Bityutskiy  * bytes which are no relevant to the sub-page are 0xFF. So, basically, writing
68801c135cSArtem B. Bityutskiy  * 4x512 sub-pages is 4 times slower then writing one 2KiB NAND page. Thus, we
69801c135cSArtem B. Bityutskiy  * prefer to use sub-pages only for EV 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>
91801c135cSArtem B. Bityutskiy #include "ubi.h"
92801c135cSArtem B. Bityutskiy 
93801c135cSArtem B. Bityutskiy #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
94801c135cSArtem B. Bityutskiy static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum);
95801c135cSArtem B. Bityutskiy static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum);
96801c135cSArtem B. Bityutskiy static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum,
97801c135cSArtem B. Bityutskiy 				 const struct ubi_ec_hdr *ec_hdr);
98801c135cSArtem B. Bityutskiy static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum);
99801c135cSArtem B. Bityutskiy static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum,
100801c135cSArtem B. Bityutskiy 				  const struct ubi_vid_hdr *vid_hdr);
101e88d6e10SArtem Bityutskiy static int paranoid_check_all_ff(struct ubi_device *ubi, int pnum, int offset,
102e88d6e10SArtem Bityutskiy 				 int len);
103ffb6b7e4SArtem Bityutskiy static int paranoid_check_empty(struct ubi_device *ubi, int pnum);
104801c135cSArtem B. Bityutskiy #else
105801c135cSArtem B. Bityutskiy #define paranoid_check_not_bad(ubi, pnum) 0
106801c135cSArtem B. Bityutskiy #define paranoid_check_peb_ec_hdr(ubi, pnum)  0
107801c135cSArtem B. Bityutskiy #define paranoid_check_ec_hdr(ubi, pnum, ec_hdr)  0
108801c135cSArtem B. Bityutskiy #define paranoid_check_peb_vid_hdr(ubi, pnum) 0
109801c135cSArtem B. Bityutskiy #define paranoid_check_vid_hdr(ubi, pnum, vid_hdr) 0
110801c135cSArtem B. Bityutskiy #define paranoid_check_all_ff(ubi, pnum, offset, len) 0
111ffb6b7e4SArtem Bityutskiy #define paranoid_check_empty(ubi, pnum) 0
112801c135cSArtem B. Bityutskiy #endif
113801c135cSArtem B. Bityutskiy 
114801c135cSArtem B. Bityutskiy /**
115801c135cSArtem B. Bityutskiy  * ubi_io_read - read data from a physical eraseblock.
116801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
117801c135cSArtem B. Bityutskiy  * @buf: buffer where to store the read data
118801c135cSArtem B. Bityutskiy  * @pnum: physical eraseblock number to read from
119801c135cSArtem B. Bityutskiy  * @offset: offset within the physical eraseblock from where to read
120801c135cSArtem B. Bityutskiy  * @len: how many bytes to read
121801c135cSArtem B. Bityutskiy  *
122801c135cSArtem B. Bityutskiy  * This function reads data from offset @offset of physical eraseblock @pnum
123801c135cSArtem B. Bityutskiy  * and stores the read data in the @buf buffer. The following return codes are
124801c135cSArtem B. Bityutskiy  * possible:
125801c135cSArtem B. Bityutskiy  *
126801c135cSArtem B. Bityutskiy  * o %0 if all the requested data were successfully read;
127801c135cSArtem B. Bityutskiy  * o %UBI_IO_BITFLIPS if all the requested data were successfully read, but
128801c135cSArtem B. Bityutskiy  *   correctable bit-flips were detected; this is harmless but may indicate
129801c135cSArtem B. Bityutskiy  *   that this eraseblock may become bad soon (but do not have to);
13063b6c1edSArtem Bityutskiy  * o %-EBADMSG if the MTD subsystem reported about data integrity problems, for
13163b6c1edSArtem Bityutskiy  *   example it can be an ECC error in case of NAND; this most probably means
13263b6c1edSArtem Bityutskiy  *   that the data is corrupted;
133801c135cSArtem B. Bityutskiy  * o %-EIO if some I/O error occurred;
134801c135cSArtem B. Bityutskiy  * o other negative error codes in case of other errors.
135801c135cSArtem B. Bityutskiy  */
136801c135cSArtem B. Bityutskiy int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset,
137801c135cSArtem B. Bityutskiy 		int len)
138801c135cSArtem B. Bityutskiy {
139801c135cSArtem B. Bityutskiy 	int err, retries = 0;
140801c135cSArtem B. Bityutskiy 	size_t read;
141801c135cSArtem B. Bityutskiy 	loff_t addr;
142801c135cSArtem B. Bityutskiy 
143801c135cSArtem B. Bityutskiy 	dbg_io("read %d bytes from PEB %d:%d", len, pnum, offset);
144801c135cSArtem B. Bityutskiy 
145801c135cSArtem B. Bityutskiy 	ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
146801c135cSArtem B. Bityutskiy 	ubi_assert(offset >= 0 && offset + len <= ubi->peb_size);
147801c135cSArtem B. Bityutskiy 	ubi_assert(len > 0);
148801c135cSArtem B. Bityutskiy 
149801c135cSArtem B. Bityutskiy 	err = paranoid_check_not_bad(ubi, pnum);
150801c135cSArtem B. Bityutskiy 	if (err)
151801c135cSArtem B. Bityutskiy 		return err > 0 ? -EINVAL : err;
152801c135cSArtem B. Bityutskiy 
153801c135cSArtem B. Bityutskiy 	addr = (loff_t)pnum * ubi->peb_size + offset;
154801c135cSArtem B. Bityutskiy retry:
155801c135cSArtem B. Bityutskiy 	err = ubi->mtd->read(ubi->mtd, addr, len, &read, buf);
156801c135cSArtem B. Bityutskiy 	if (err) {
157801c135cSArtem B. Bityutskiy 		if (err == -EUCLEAN) {
158801c135cSArtem B. Bityutskiy 			/*
159801c135cSArtem B. Bityutskiy 			 * -EUCLEAN is reported if there was a bit-flip which
160801c135cSArtem B. Bityutskiy 			 * was corrected, so this is harmless.
1618c1e6ee1SArtem Bityutskiy 			 *
1628c1e6ee1SArtem Bityutskiy 			 * We do not report about it here unless debugging is
1638c1e6ee1SArtem Bityutskiy 			 * enabled. A corresponding message will be printed
1648c1e6ee1SArtem Bityutskiy 			 * later, when it is has been scrubbed.
165801c135cSArtem B. Bityutskiy 			 */
1668c1e6ee1SArtem Bityutskiy 			dbg_msg("fixable bit-flip detected at PEB %d", pnum);
167801c135cSArtem B. Bityutskiy 			ubi_assert(len == read);
168801c135cSArtem B. Bityutskiy 			return UBI_IO_BITFLIPS;
169801c135cSArtem B. Bityutskiy 		}
170801c135cSArtem B. Bityutskiy 
171801c135cSArtem B. Bityutskiy 		if (read != len && retries++ < UBI_IO_RETRIES) {
172801c135cSArtem B. Bityutskiy 			dbg_io("error %d while reading %d bytes from PEB %d:%d,"
173801c135cSArtem B. Bityutskiy 			       " read only %zd bytes, retry",
174801c135cSArtem B. Bityutskiy 			       err, len, pnum, offset, read);
175801c135cSArtem B. Bityutskiy 			yield();
176801c135cSArtem B. Bityutskiy 			goto retry;
177801c135cSArtem B. Bityutskiy 		}
178801c135cSArtem B. Bityutskiy 
179801c135cSArtem B. Bityutskiy 		ubi_err("error %d while reading %d bytes from PEB %d:%d, "
180801c135cSArtem B. Bityutskiy 			"read %zd bytes", err, len, pnum, offset, read);
181801c135cSArtem B. Bityutskiy 		ubi_dbg_dump_stack();
1822362a53eSArtem Bityutskiy 
1832362a53eSArtem Bityutskiy 		/*
1842362a53eSArtem Bityutskiy 		 * The driver should never return -EBADMSG if it failed to read
1852362a53eSArtem Bityutskiy 		 * all the requested data. But some buggy drivers might do
1862362a53eSArtem Bityutskiy 		 * this, so we change it to -EIO.
1872362a53eSArtem Bityutskiy 		 */
1882362a53eSArtem Bityutskiy 		if (read != len && err == -EBADMSG) {
1892362a53eSArtem Bityutskiy 			ubi_assert(0);
1902362a53eSArtem Bityutskiy 			err = -EIO;
1912362a53eSArtem Bityutskiy 		}
192801c135cSArtem B. Bityutskiy 	} else {
193801c135cSArtem B. Bityutskiy 		ubi_assert(len == read);
194801c135cSArtem B. Bityutskiy 
195801c135cSArtem B. Bityutskiy 		if (ubi_dbg_is_bitflip()) {
196c8566350SArtem Bityutskiy 			dbg_gen("bit-flip (emulated)");
197801c135cSArtem B. Bityutskiy 			err = UBI_IO_BITFLIPS;
198801c135cSArtem B. Bityutskiy 		}
199801c135cSArtem B. Bityutskiy 	}
200801c135cSArtem B. Bityutskiy 
201801c135cSArtem B. Bityutskiy 	return err;
202801c135cSArtem B. Bityutskiy }
203801c135cSArtem B. Bityutskiy 
204801c135cSArtem B. Bityutskiy /**
205801c135cSArtem B. Bityutskiy  * ubi_io_write - write data to a physical eraseblock.
206801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
207801c135cSArtem B. Bityutskiy  * @buf: buffer with the data to write
208801c135cSArtem B. Bityutskiy  * @pnum: physical eraseblock number to write to
209801c135cSArtem B. Bityutskiy  * @offset: offset within the physical eraseblock where to write
210801c135cSArtem B. Bityutskiy  * @len: how many bytes to write
211801c135cSArtem B. Bityutskiy  *
212801c135cSArtem B. Bityutskiy  * This function writes @len bytes of data from buffer @buf to offset @offset
213801c135cSArtem B. Bityutskiy  * of physical eraseblock @pnum. If all the data were successfully written,
214801c135cSArtem B. Bityutskiy  * zero is returned. If an error occurred, this function returns a negative
215801c135cSArtem B. Bityutskiy  * error code. If %-EIO is returned, the physical eraseblock most probably went
216801c135cSArtem B. Bityutskiy  * bad.
217801c135cSArtem B. Bityutskiy  *
218801c135cSArtem B. Bityutskiy  * Note, in case of an error, it is possible that something was still written
219801c135cSArtem B. Bityutskiy  * to the flash media, but may be some garbage.
220801c135cSArtem B. Bityutskiy  */
221e88d6e10SArtem Bityutskiy int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
222e88d6e10SArtem Bityutskiy 		 int len)
223801c135cSArtem B. Bityutskiy {
224801c135cSArtem B. Bityutskiy 	int err;
225801c135cSArtem B. Bityutskiy 	size_t written;
226801c135cSArtem B. Bityutskiy 	loff_t addr;
227801c135cSArtem B. Bityutskiy 
228801c135cSArtem B. Bityutskiy 	dbg_io("write %d bytes to PEB %d:%d", len, pnum, offset);
229801c135cSArtem B. Bityutskiy 
230801c135cSArtem B. Bityutskiy 	ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
231801c135cSArtem B. Bityutskiy 	ubi_assert(offset >= 0 && offset + len <= ubi->peb_size);
232801c135cSArtem B. Bityutskiy 	ubi_assert(offset % ubi->hdrs_min_io_size == 0);
233801c135cSArtem B. Bityutskiy 	ubi_assert(len > 0 && len % ubi->hdrs_min_io_size == 0);
234801c135cSArtem B. Bityutskiy 
235801c135cSArtem B. Bityutskiy 	if (ubi->ro_mode) {
236801c135cSArtem B. Bityutskiy 		ubi_err("read-only mode");
237801c135cSArtem B. Bityutskiy 		return -EROFS;
238801c135cSArtem B. Bityutskiy 	}
239801c135cSArtem B. Bityutskiy 
240801c135cSArtem B. Bityutskiy 	/* The below has to be compiled out if paranoid checks are disabled */
241801c135cSArtem B. Bityutskiy 
242801c135cSArtem B. Bityutskiy 	err = paranoid_check_not_bad(ubi, pnum);
243801c135cSArtem B. Bityutskiy 	if (err)
244801c135cSArtem B. Bityutskiy 		return err > 0 ? -EINVAL : err;
245801c135cSArtem B. Bityutskiy 
246801c135cSArtem B. Bityutskiy 	/* The area we are writing to has to contain all 0xFF bytes */
247801c135cSArtem B. Bityutskiy 	err = paranoid_check_all_ff(ubi, pnum, offset, len);
248801c135cSArtem B. Bityutskiy 	if (err)
249801c135cSArtem B. Bityutskiy 		return err > 0 ? -EINVAL : err;
250801c135cSArtem B. Bityutskiy 
251801c135cSArtem B. Bityutskiy 	if (offset >= ubi->leb_start) {
252801c135cSArtem B. Bityutskiy 		/*
253801c135cSArtem B. Bityutskiy 		 * We write to the data area of the physical eraseblock. Make
254801c135cSArtem B. Bityutskiy 		 * sure it has valid EC and VID headers.
255801c135cSArtem B. Bityutskiy 		 */
256801c135cSArtem B. Bityutskiy 		err = paranoid_check_peb_ec_hdr(ubi, pnum);
257801c135cSArtem B. Bityutskiy 		if (err)
258801c135cSArtem B. Bityutskiy 			return err > 0 ? -EINVAL : err;
259801c135cSArtem B. Bityutskiy 		err = paranoid_check_peb_vid_hdr(ubi, pnum);
260801c135cSArtem B. Bityutskiy 		if (err)
261801c135cSArtem B. Bityutskiy 			return err > 0 ? -EINVAL : err;
262801c135cSArtem B. Bityutskiy 	}
263801c135cSArtem B. Bityutskiy 
264801c135cSArtem B. Bityutskiy 	if (ubi_dbg_is_write_failure()) {
265801c135cSArtem B. Bityutskiy 		dbg_err("cannot write %d bytes to PEB %d:%d "
266801c135cSArtem B. Bityutskiy 			"(emulated)", len, pnum, offset);
267801c135cSArtem B. Bityutskiy 		ubi_dbg_dump_stack();
268801c135cSArtem B. Bityutskiy 		return -EIO;
269801c135cSArtem B. Bityutskiy 	}
270801c135cSArtem B. Bityutskiy 
271801c135cSArtem B. Bityutskiy 	addr = (loff_t)pnum * ubi->peb_size + offset;
272801c135cSArtem B. Bityutskiy 	err = ubi->mtd->write(ubi->mtd, addr, len, &written, buf);
273801c135cSArtem B. Bityutskiy 	if (err) {
274801c135cSArtem B. Bityutskiy 		ubi_err("error %d while writing %d bytes to PEB %d:%d, written"
275801c135cSArtem B. Bityutskiy 			" %zd bytes", err, len, pnum, offset, written);
276801c135cSArtem B. Bityutskiy 		ubi_dbg_dump_stack();
277801c135cSArtem B. Bityutskiy 	} else
278801c135cSArtem B. Bityutskiy 		ubi_assert(written == len);
279801c135cSArtem B. Bityutskiy 
280801c135cSArtem B. Bityutskiy 	return err;
281801c135cSArtem B. Bityutskiy }
282801c135cSArtem B. Bityutskiy 
283801c135cSArtem B. Bityutskiy /**
284801c135cSArtem B. Bityutskiy  * erase_callback - MTD erasure call-back.
285801c135cSArtem B. Bityutskiy  * @ei: MTD erase information object.
286801c135cSArtem B. Bityutskiy  *
287801c135cSArtem B. Bityutskiy  * Note, even though MTD erase interface is asynchronous, all the current
288801c135cSArtem B. Bityutskiy  * implementations are synchronous anyway.
289801c135cSArtem B. Bityutskiy  */
290801c135cSArtem B. Bityutskiy static void erase_callback(struct erase_info *ei)
291801c135cSArtem B. Bityutskiy {
292801c135cSArtem B. Bityutskiy 	wake_up_interruptible((wait_queue_head_t *)ei->priv);
293801c135cSArtem B. Bityutskiy }
294801c135cSArtem B. Bityutskiy 
295801c135cSArtem B. Bityutskiy /**
296801c135cSArtem B. Bityutskiy  * do_sync_erase - synchronously erase a physical eraseblock.
297801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
298801c135cSArtem B. Bityutskiy  * @pnum: the physical eraseblock number to erase
299801c135cSArtem B. Bityutskiy  *
300801c135cSArtem B. Bityutskiy  * This function synchronously erases physical eraseblock @pnum and returns
301801c135cSArtem B. Bityutskiy  * zero in case of success and a negative error code in case of failure. If
302801c135cSArtem B. Bityutskiy  * %-EIO is returned, the physical eraseblock most probably went bad.
303801c135cSArtem B. Bityutskiy  */
304e88d6e10SArtem Bityutskiy static int do_sync_erase(struct ubi_device *ubi, int pnum)
305801c135cSArtem B. Bityutskiy {
306801c135cSArtem B. Bityutskiy 	int err, retries = 0;
307801c135cSArtem B. Bityutskiy 	struct erase_info ei;
308801c135cSArtem B. Bityutskiy 	wait_queue_head_t wq;
309801c135cSArtem B. Bityutskiy 
310801c135cSArtem B. Bityutskiy 	dbg_io("erase PEB %d", pnum);
311801c135cSArtem B. Bityutskiy 
312801c135cSArtem B. Bityutskiy retry:
313801c135cSArtem B. Bityutskiy 	init_waitqueue_head(&wq);
314801c135cSArtem B. Bityutskiy 	memset(&ei, 0, sizeof(struct erase_info));
315801c135cSArtem B. Bityutskiy 
316801c135cSArtem B. Bityutskiy 	ei.mtd      = ubi->mtd;
3172f176f79SBrijesh Singh 	ei.addr     = (loff_t)pnum * ubi->peb_size;
318801c135cSArtem B. Bityutskiy 	ei.len      = ubi->peb_size;
319801c135cSArtem B. Bityutskiy 	ei.callback = erase_callback;
320801c135cSArtem B. Bityutskiy 	ei.priv     = (unsigned long)&wq;
321801c135cSArtem B. Bityutskiy 
322801c135cSArtem B. Bityutskiy 	err = ubi->mtd->erase(ubi->mtd, &ei);
323801c135cSArtem B. Bityutskiy 	if (err) {
324801c135cSArtem B. Bityutskiy 		if (retries++ < UBI_IO_RETRIES) {
325801c135cSArtem B. Bityutskiy 			dbg_io("error %d while erasing PEB %d, retry",
326801c135cSArtem B. Bityutskiy 			       err, pnum);
327801c135cSArtem B. Bityutskiy 			yield();
328801c135cSArtem B. Bityutskiy 			goto retry;
329801c135cSArtem B. Bityutskiy 		}
330801c135cSArtem B. Bityutskiy 		ubi_err("cannot erase PEB %d, error %d", pnum, err);
331801c135cSArtem B. Bityutskiy 		ubi_dbg_dump_stack();
332801c135cSArtem B. Bityutskiy 		return err;
333801c135cSArtem B. Bityutskiy 	}
334801c135cSArtem B. Bityutskiy 
335801c135cSArtem B. Bityutskiy 	err = wait_event_interruptible(wq, ei.state == MTD_ERASE_DONE ||
336801c135cSArtem B. Bityutskiy 					   ei.state == MTD_ERASE_FAILED);
337801c135cSArtem B. Bityutskiy 	if (err) {
338801c135cSArtem B. Bityutskiy 		ubi_err("interrupted PEB %d erasure", pnum);
339801c135cSArtem B. Bityutskiy 		return -EINTR;
340801c135cSArtem B. Bityutskiy 	}
341801c135cSArtem B. Bityutskiy 
342801c135cSArtem B. Bityutskiy 	if (ei.state == MTD_ERASE_FAILED) {
343801c135cSArtem B. Bityutskiy 		if (retries++ < UBI_IO_RETRIES) {
344801c135cSArtem B. Bityutskiy 			dbg_io("error while erasing PEB %d, retry", pnum);
345801c135cSArtem B. Bityutskiy 			yield();
346801c135cSArtem B. Bityutskiy 			goto retry;
347801c135cSArtem B. Bityutskiy 		}
348801c135cSArtem B. Bityutskiy 		ubi_err("cannot erase PEB %d", pnum);
349801c135cSArtem B. Bityutskiy 		ubi_dbg_dump_stack();
350801c135cSArtem B. Bityutskiy 		return -EIO;
351801c135cSArtem B. Bityutskiy 	}
352801c135cSArtem B. Bityutskiy 
353801c135cSArtem B. Bityutskiy 	err = paranoid_check_all_ff(ubi, pnum, 0, ubi->peb_size);
354801c135cSArtem B. Bityutskiy 	if (err)
355801c135cSArtem B. Bityutskiy 		return err > 0 ? -EINVAL : err;
356801c135cSArtem B. Bityutskiy 
357801c135cSArtem B. Bityutskiy 	if (ubi_dbg_is_erase_failure() && !err) {
358801c135cSArtem B. Bityutskiy 		dbg_err("cannot erase PEB %d (emulated)", pnum);
359801c135cSArtem B. Bityutskiy 		return -EIO;
360801c135cSArtem B. Bityutskiy 	}
361801c135cSArtem B. Bityutskiy 
362801c135cSArtem B. Bityutskiy 	return 0;
363801c135cSArtem B. Bityutskiy }
364801c135cSArtem B. Bityutskiy 
365801c135cSArtem B. Bityutskiy /**
366801c135cSArtem B. Bityutskiy  * check_pattern - check if buffer contains only a certain byte pattern.
367801c135cSArtem B. Bityutskiy  * @buf: buffer to check
368801c135cSArtem B. Bityutskiy  * @patt: the pattern to check
369801c135cSArtem B. Bityutskiy  * @size: buffer size in bytes
370801c135cSArtem B. Bityutskiy  *
371801c135cSArtem B. Bityutskiy  * This function returns %1 in there are only @patt bytes in @buf, and %0 if
372801c135cSArtem B. Bityutskiy  * something else was also found.
373801c135cSArtem B. Bityutskiy  */
374801c135cSArtem B. Bityutskiy static int check_pattern(const void *buf, uint8_t patt, int size)
375801c135cSArtem B. Bityutskiy {
376801c135cSArtem B. Bityutskiy 	int i;
377801c135cSArtem B. Bityutskiy 
378801c135cSArtem B. Bityutskiy 	for (i = 0; i < size; i++)
379801c135cSArtem B. Bityutskiy 		if (((const uint8_t *)buf)[i] != patt)
380801c135cSArtem B. Bityutskiy 			return 0;
381801c135cSArtem B. Bityutskiy 	return 1;
382801c135cSArtem B. Bityutskiy }
383801c135cSArtem B. Bityutskiy 
384801c135cSArtem B. Bityutskiy /* Patterns to write to a physical eraseblock when torturing it */
385801c135cSArtem B. Bityutskiy static uint8_t patterns[] = {0xa5, 0x5a, 0x0};
386801c135cSArtem B. Bityutskiy 
387801c135cSArtem B. Bityutskiy /**
388801c135cSArtem B. Bityutskiy  * torture_peb - test a supposedly bad physical eraseblock.
389801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
390801c135cSArtem B. Bityutskiy  * @pnum: the physical eraseblock number to test
391801c135cSArtem B. Bityutskiy  *
392801c135cSArtem B. Bityutskiy  * This function returns %-EIO if the physical eraseblock did not pass the
393801c135cSArtem B. Bityutskiy  * test, a positive number of erase operations done if the test was
394801c135cSArtem B. Bityutskiy  * successfully passed, and other negative error codes in case of other errors.
395801c135cSArtem B. Bityutskiy  */
396e88d6e10SArtem Bityutskiy static int torture_peb(struct ubi_device *ubi, int pnum)
397801c135cSArtem B. Bityutskiy {
398801c135cSArtem B. Bityutskiy 	int err, i, patt_count;
399801c135cSArtem B. Bityutskiy 
4008c1e6ee1SArtem Bityutskiy 	ubi_msg("run torture test for PEB %d", pnum);
401801c135cSArtem B. Bityutskiy 	patt_count = ARRAY_SIZE(patterns);
402801c135cSArtem B. Bityutskiy 	ubi_assert(patt_count > 0);
403801c135cSArtem B. Bityutskiy 
404e88d6e10SArtem Bityutskiy 	mutex_lock(&ubi->buf_mutex);
405801c135cSArtem B. Bityutskiy 	for (i = 0; i < patt_count; i++) {
406801c135cSArtem B. Bityutskiy 		err = do_sync_erase(ubi, pnum);
407801c135cSArtem B. Bityutskiy 		if (err)
408801c135cSArtem B. Bityutskiy 			goto out;
409801c135cSArtem B. Bityutskiy 
410801c135cSArtem B. Bityutskiy 		/* Make sure the PEB contains only 0xFF bytes */
411e88d6e10SArtem Bityutskiy 		err = ubi_io_read(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size);
412801c135cSArtem B. Bityutskiy 		if (err)
413801c135cSArtem B. Bityutskiy 			goto out;
414801c135cSArtem B. Bityutskiy 
415e88d6e10SArtem Bityutskiy 		err = check_pattern(ubi->peb_buf1, 0xFF, ubi->peb_size);
416801c135cSArtem B. Bityutskiy 		if (err == 0) {
417801c135cSArtem B. Bityutskiy 			ubi_err("erased PEB %d, but a non-0xFF byte found",
418801c135cSArtem B. Bityutskiy 				pnum);
419801c135cSArtem B. Bityutskiy 			err = -EIO;
420801c135cSArtem B. Bityutskiy 			goto out;
421801c135cSArtem B. Bityutskiy 		}
422801c135cSArtem B. Bityutskiy 
423801c135cSArtem B. Bityutskiy 		/* Write a pattern and check it */
424e88d6e10SArtem Bityutskiy 		memset(ubi->peb_buf1, patterns[i], ubi->peb_size);
425e88d6e10SArtem Bityutskiy 		err = ubi_io_write(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size);
426801c135cSArtem B. Bityutskiy 		if (err)
427801c135cSArtem B. Bityutskiy 			goto out;
428801c135cSArtem B. Bityutskiy 
429e88d6e10SArtem Bityutskiy 		memset(ubi->peb_buf1, ~patterns[i], ubi->peb_size);
430e88d6e10SArtem Bityutskiy 		err = ubi_io_read(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size);
431801c135cSArtem B. Bityutskiy 		if (err)
432801c135cSArtem B. Bityutskiy 			goto out;
433801c135cSArtem B. Bityutskiy 
434e88d6e10SArtem Bityutskiy 		err = check_pattern(ubi->peb_buf1, patterns[i], ubi->peb_size);
435801c135cSArtem B. Bityutskiy 		if (err == 0) {
436801c135cSArtem B. Bityutskiy 			ubi_err("pattern %x checking failed for PEB %d",
437801c135cSArtem B. Bityutskiy 				patterns[i], pnum);
438801c135cSArtem B. Bityutskiy 			err = -EIO;
439801c135cSArtem B. Bityutskiy 			goto out;
440801c135cSArtem B. Bityutskiy 		}
441801c135cSArtem B. Bityutskiy 	}
442801c135cSArtem B. Bityutskiy 
443801c135cSArtem B. Bityutskiy 	err = patt_count;
4448c1e6ee1SArtem Bityutskiy 	ubi_msg("PEB %d passed torture test, do not mark it a bad", pnum);
445801c135cSArtem B. Bityutskiy 
446801c135cSArtem B. Bityutskiy out:
447e88d6e10SArtem Bityutskiy 	mutex_unlock(&ubi->buf_mutex);
4488d2d4011SArtem Bityutskiy 	if (err == UBI_IO_BITFLIPS || err == -EBADMSG) {
449801c135cSArtem B. Bityutskiy 		/*
450801c135cSArtem B. Bityutskiy 		 * If a bit-flip or data integrity error was detected, the test
451801c135cSArtem B. Bityutskiy 		 * has not passed because it happened on a freshly erased
452801c135cSArtem B. Bityutskiy 		 * physical eraseblock which means something is wrong with it.
453801c135cSArtem B. Bityutskiy 		 */
4548d2d4011SArtem Bityutskiy 		ubi_err("read problems on freshly erased PEB %d, must be bad",
4558d2d4011SArtem Bityutskiy 			pnum);
456801c135cSArtem B. Bityutskiy 		err = -EIO;
4578d2d4011SArtem Bityutskiy 	}
458801c135cSArtem B. Bityutskiy 	return err;
459801c135cSArtem B. Bityutskiy }
460801c135cSArtem B. Bityutskiy 
461801c135cSArtem B. Bityutskiy /**
462801c135cSArtem B. Bityutskiy  * ubi_io_sync_erase - synchronously erase a physical eraseblock.
463801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
464801c135cSArtem B. Bityutskiy  * @pnum: physical eraseblock number to erase
465801c135cSArtem B. Bityutskiy  * @torture: if this physical eraseblock has to be tortured
466801c135cSArtem B. Bityutskiy  *
467801c135cSArtem B. Bityutskiy  * This function synchronously erases physical eraseblock @pnum. If @torture
468801c135cSArtem B. Bityutskiy  * flag is not zero, the physical eraseblock is checked by means of writing
469801c135cSArtem B. Bityutskiy  * different patterns to it and reading them back. If the torturing is enabled,
470025dfdafSFrederik Schwarzer  * the physical eraseblock is erased more than once.
471801c135cSArtem B. Bityutskiy  *
472801c135cSArtem B. Bityutskiy  * This function returns the number of erasures made in case of success, %-EIO
473801c135cSArtem B. Bityutskiy  * if the erasure failed or the torturing test failed, and other negative error
474801c135cSArtem B. Bityutskiy  * codes in case of other errors. Note, %-EIO means that the physical
475801c135cSArtem B. Bityutskiy  * eraseblock is bad.
476801c135cSArtem B. Bityutskiy  */
477e88d6e10SArtem Bityutskiy int ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture)
478801c135cSArtem B. Bityutskiy {
479801c135cSArtem B. Bityutskiy 	int err, ret = 0;
480801c135cSArtem B. Bityutskiy 
481801c135cSArtem B. Bityutskiy 	ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
482801c135cSArtem B. Bityutskiy 
483801c135cSArtem B. Bityutskiy 	err = paranoid_check_not_bad(ubi, pnum);
484801c135cSArtem B. Bityutskiy 	if (err != 0)
485801c135cSArtem B. Bityutskiy 		return err > 0 ? -EINVAL : err;
486801c135cSArtem B. Bityutskiy 
487801c135cSArtem B. Bityutskiy 	if (ubi->ro_mode) {
488801c135cSArtem B. Bityutskiy 		ubi_err("read-only mode");
489801c135cSArtem B. Bityutskiy 		return -EROFS;
490801c135cSArtem B. Bityutskiy 	}
491801c135cSArtem B. Bityutskiy 
492801c135cSArtem B. Bityutskiy 	if (torture) {
493801c135cSArtem B. Bityutskiy 		ret = torture_peb(ubi, pnum);
494801c135cSArtem B. Bityutskiy 		if (ret < 0)
495801c135cSArtem B. Bityutskiy 			return ret;
496801c135cSArtem B. Bityutskiy 	}
497801c135cSArtem B. Bityutskiy 
498801c135cSArtem B. Bityutskiy 	err = do_sync_erase(ubi, pnum);
499801c135cSArtem B. Bityutskiy 	if (err)
500801c135cSArtem B. Bityutskiy 		return err;
501801c135cSArtem B. Bityutskiy 
502801c135cSArtem B. Bityutskiy 	return ret + 1;
503801c135cSArtem B. Bityutskiy }
504801c135cSArtem B. Bityutskiy 
505801c135cSArtem B. Bityutskiy /**
506801c135cSArtem B. Bityutskiy  * ubi_io_is_bad - check if a physical eraseblock is bad.
507801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
508801c135cSArtem B. Bityutskiy  * @pnum: the physical eraseblock number to check
509801c135cSArtem B. Bityutskiy  *
510801c135cSArtem B. Bityutskiy  * This function returns a positive number if the physical eraseblock is bad,
511801c135cSArtem B. Bityutskiy  * zero if not, and a negative error code if an error occurred.
512801c135cSArtem B. Bityutskiy  */
513801c135cSArtem B. Bityutskiy int ubi_io_is_bad(const struct ubi_device *ubi, int pnum)
514801c135cSArtem B. Bityutskiy {
515801c135cSArtem B. Bityutskiy 	struct mtd_info *mtd = ubi->mtd;
516801c135cSArtem B. Bityutskiy 
517801c135cSArtem B. Bityutskiy 	ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
518801c135cSArtem B. Bityutskiy 
519801c135cSArtem B. Bityutskiy 	if (ubi->bad_allowed) {
520801c135cSArtem B. Bityutskiy 		int ret;
521801c135cSArtem B. Bityutskiy 
522801c135cSArtem B. Bityutskiy 		ret = mtd->block_isbad(mtd, (loff_t)pnum * ubi->peb_size);
523801c135cSArtem B. Bityutskiy 		if (ret < 0)
524801c135cSArtem B. Bityutskiy 			ubi_err("error %d while checking if PEB %d is bad",
525801c135cSArtem B. Bityutskiy 				ret, pnum);
526801c135cSArtem B. Bityutskiy 		else if (ret)
527801c135cSArtem B. Bityutskiy 			dbg_io("PEB %d is bad", pnum);
528801c135cSArtem B. Bityutskiy 		return ret;
529801c135cSArtem B. Bityutskiy 	}
530801c135cSArtem B. Bityutskiy 
531801c135cSArtem B. Bityutskiy 	return 0;
532801c135cSArtem B. Bityutskiy }
533801c135cSArtem B. Bityutskiy 
534801c135cSArtem B. Bityutskiy /**
535801c135cSArtem B. Bityutskiy  * ubi_io_mark_bad - mark a physical eraseblock as bad.
536801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
537801c135cSArtem B. Bityutskiy  * @pnum: the physical eraseblock number to mark
538801c135cSArtem B. Bityutskiy  *
539801c135cSArtem B. Bityutskiy  * This function returns zero in case of success and a negative error code in
540801c135cSArtem B. Bityutskiy  * case of failure.
541801c135cSArtem B. Bityutskiy  */
542801c135cSArtem B. Bityutskiy int ubi_io_mark_bad(const struct ubi_device *ubi, int pnum)
543801c135cSArtem B. Bityutskiy {
544801c135cSArtem B. Bityutskiy 	int err;
545801c135cSArtem B. Bityutskiy 	struct mtd_info *mtd = ubi->mtd;
546801c135cSArtem B. Bityutskiy 
547801c135cSArtem B. Bityutskiy 	ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
548801c135cSArtem B. Bityutskiy 
549801c135cSArtem B. Bityutskiy 	if (ubi->ro_mode) {
550801c135cSArtem B. Bityutskiy 		ubi_err("read-only mode");
551801c135cSArtem B. Bityutskiy 		return -EROFS;
552801c135cSArtem B. Bityutskiy 	}
553801c135cSArtem B. Bityutskiy 
554801c135cSArtem B. Bityutskiy 	if (!ubi->bad_allowed)
555801c135cSArtem B. Bityutskiy 		return 0;
556801c135cSArtem B. Bityutskiy 
557801c135cSArtem B. Bityutskiy 	err = mtd->block_markbad(mtd, (loff_t)pnum * ubi->peb_size);
558801c135cSArtem B. Bityutskiy 	if (err)
559801c135cSArtem B. Bityutskiy 		ubi_err("cannot mark PEB %d bad, error %d", pnum, err);
560801c135cSArtem B. Bityutskiy 	return err;
561801c135cSArtem B. Bityutskiy }
562801c135cSArtem B. Bityutskiy 
563801c135cSArtem B. Bityutskiy /**
564801c135cSArtem B. Bityutskiy  * validate_ec_hdr - validate an erase counter header.
565801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
566801c135cSArtem B. Bityutskiy  * @ec_hdr: the erase counter header to check
567801c135cSArtem B. Bityutskiy  *
568801c135cSArtem B. Bityutskiy  * This function returns zero if the erase counter header is OK, and %1 if
569801c135cSArtem B. Bityutskiy  * not.
570801c135cSArtem B. Bityutskiy  */
571801c135cSArtem B. Bityutskiy static int validate_ec_hdr(const struct ubi_device *ubi,
572801c135cSArtem B. Bityutskiy 			   const struct ubi_ec_hdr *ec_hdr)
573801c135cSArtem B. Bityutskiy {
574801c135cSArtem B. Bityutskiy 	long long ec;
575801c135cSArtem B. Bityutskiy 	int vid_hdr_offset, leb_start;
576801c135cSArtem B. Bityutskiy 
5773261ebd7SChristoph Hellwig 	ec = be64_to_cpu(ec_hdr->ec);
5783261ebd7SChristoph Hellwig 	vid_hdr_offset = be32_to_cpu(ec_hdr->vid_hdr_offset);
5793261ebd7SChristoph Hellwig 	leb_start = be32_to_cpu(ec_hdr->data_offset);
580801c135cSArtem B. Bityutskiy 
581801c135cSArtem B. Bityutskiy 	if (ec_hdr->version != UBI_VERSION) {
582801c135cSArtem B. Bityutskiy 		ubi_err("node with incompatible UBI version found: "
583801c135cSArtem B. Bityutskiy 			"this UBI version is %d, image version is %d",
584801c135cSArtem B. Bityutskiy 			UBI_VERSION, (int)ec_hdr->version);
585801c135cSArtem B. Bityutskiy 		goto bad;
586801c135cSArtem B. Bityutskiy 	}
587801c135cSArtem B. Bityutskiy 
588801c135cSArtem B. Bityutskiy 	if (vid_hdr_offset != ubi->vid_hdr_offset) {
589801c135cSArtem B. Bityutskiy 		ubi_err("bad VID header offset %d, expected %d",
590801c135cSArtem B. Bityutskiy 			vid_hdr_offset, ubi->vid_hdr_offset);
591801c135cSArtem B. Bityutskiy 		goto bad;
592801c135cSArtem B. Bityutskiy 	}
593801c135cSArtem B. Bityutskiy 
594801c135cSArtem B. Bityutskiy 	if (leb_start != ubi->leb_start) {
595801c135cSArtem B. Bityutskiy 		ubi_err("bad data offset %d, expected %d",
596801c135cSArtem B. Bityutskiy 			leb_start, ubi->leb_start);
597801c135cSArtem B. Bityutskiy 		goto bad;
598801c135cSArtem B. Bityutskiy 	}
599801c135cSArtem B. Bityutskiy 
600801c135cSArtem B. Bityutskiy 	if (ec < 0 || ec > UBI_MAX_ERASECOUNTER) {
601801c135cSArtem B. Bityutskiy 		ubi_err("bad erase counter %lld", ec);
602801c135cSArtem B. Bityutskiy 		goto bad;
603801c135cSArtem B. Bityutskiy 	}
604801c135cSArtem B. Bityutskiy 
605801c135cSArtem B. Bityutskiy 	return 0;
606801c135cSArtem B. Bityutskiy 
607801c135cSArtem B. Bityutskiy bad:
608801c135cSArtem B. Bityutskiy 	ubi_err("bad EC header");
609801c135cSArtem B. Bityutskiy 	ubi_dbg_dump_ec_hdr(ec_hdr);
610801c135cSArtem B. Bityutskiy 	ubi_dbg_dump_stack();
611801c135cSArtem B. Bityutskiy 	return 1;
612801c135cSArtem B. Bityutskiy }
613801c135cSArtem B. Bityutskiy 
614801c135cSArtem B. Bityutskiy /**
615801c135cSArtem B. Bityutskiy  * ubi_io_read_ec_hdr - read and check an erase counter header.
616801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
617801c135cSArtem B. Bityutskiy  * @pnum: physical eraseblock to read from
618801c135cSArtem B. Bityutskiy  * @ec_hdr: a &struct ubi_ec_hdr object where to store the read erase counter
619801c135cSArtem B. Bityutskiy  * header
620801c135cSArtem B. Bityutskiy  * @verbose: be verbose if the header is corrupted or was not found
621801c135cSArtem B. Bityutskiy  *
622801c135cSArtem B. Bityutskiy  * This function reads erase counter header from physical eraseblock @pnum and
623801c135cSArtem B. Bityutskiy  * stores it in @ec_hdr. This function also checks CRC checksum of the read
624801c135cSArtem B. Bityutskiy  * erase counter header. The following codes may be returned:
625801c135cSArtem B. Bityutskiy  *
626801c135cSArtem B. Bityutskiy  * o %0 if the CRC checksum is correct and the header was successfully read;
627801c135cSArtem B. Bityutskiy  * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected
628801c135cSArtem B. Bityutskiy  *   and corrected by the flash driver; this is harmless but may indicate that
629801c135cSArtem B. Bityutskiy  *   this eraseblock may become bad soon (but may be not);
630801c135cSArtem B. Bityutskiy  * o %UBI_IO_BAD_EC_HDR if the erase counter header is corrupted (a CRC error);
631801c135cSArtem B. Bityutskiy  * o %UBI_IO_PEB_EMPTY if the physical eraseblock is empty;
632801c135cSArtem B. Bityutskiy  * o a negative error code in case of failure.
633801c135cSArtem B. Bityutskiy  */
634e88d6e10SArtem Bityutskiy int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
635801c135cSArtem B. Bityutskiy 		       struct ubi_ec_hdr *ec_hdr, int verbose)
636801c135cSArtem B. Bityutskiy {
637801c135cSArtem B. Bityutskiy 	int err, read_err = 0;
638801c135cSArtem B. Bityutskiy 	uint32_t crc, magic, hdr_crc;
639801c135cSArtem B. Bityutskiy 
640801c135cSArtem B. Bityutskiy 	dbg_io("read EC header from PEB %d", pnum);
641801c135cSArtem B. Bityutskiy 	ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
642801c135cSArtem B. Bityutskiy 
643801c135cSArtem B. Bityutskiy 	err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE);
644801c135cSArtem B. Bityutskiy 	if (err) {
645801c135cSArtem B. Bityutskiy 		if (err != UBI_IO_BITFLIPS && err != -EBADMSG)
646801c135cSArtem B. Bityutskiy 			return err;
647801c135cSArtem B. Bityutskiy 
648801c135cSArtem B. Bityutskiy 		/*
649801c135cSArtem B. Bityutskiy 		 * We read all the data, but either a correctable bit-flip
650801c135cSArtem B. Bityutskiy 		 * occurred, or MTD reported about some data integrity error,
651801c135cSArtem B. Bityutskiy 		 * like an ECC error in case of NAND. The former is harmless,
652801c135cSArtem B. Bityutskiy 		 * the later may mean that the read data is corrupted. But we
653801c135cSArtem B. Bityutskiy 		 * have a CRC check-sum and we will detect this. If the EC
654801c135cSArtem B. Bityutskiy 		 * header is still OK, we just report this as there was a
655801c135cSArtem B. Bityutskiy 		 * bit-flip.
656801c135cSArtem B. Bityutskiy 		 */
657801c135cSArtem B. Bityutskiy 		read_err = err;
658801c135cSArtem B. Bityutskiy 	}
659801c135cSArtem B. Bityutskiy 
6603261ebd7SChristoph Hellwig 	magic = be32_to_cpu(ec_hdr->magic);
661801c135cSArtem B. Bityutskiy 	if (magic != UBI_EC_HDR_MAGIC) {
662801c135cSArtem B. Bityutskiy 		/*
663801c135cSArtem B. Bityutskiy 		 * The magic field is wrong. Let's check if we have read all
664801c135cSArtem B. Bityutskiy 		 * 0xFF. If yes, this physical eraseblock is assumed to be
665801c135cSArtem B. Bityutskiy 		 * empty.
666801c135cSArtem B. Bityutskiy 		 *
667801c135cSArtem B. Bityutskiy 		 * But if there was a read error, we do not test it for all
668801c135cSArtem B. Bityutskiy 		 * 0xFFs. Even if it does contain all 0xFFs, this error
669801c135cSArtem B. Bityutskiy 		 * indicates that something is still wrong with this physical
670801c135cSArtem B. Bityutskiy 		 * eraseblock and we anyway cannot treat it as empty.
671801c135cSArtem B. Bityutskiy 		 */
672801c135cSArtem B. Bityutskiy 		if (read_err != -EBADMSG &&
673801c135cSArtem B. Bityutskiy 		    check_pattern(ec_hdr, 0xFF, UBI_EC_HDR_SIZE)) {
674801c135cSArtem B. Bityutskiy 			/* The physical eraseblock is supposedly empty */
675801c135cSArtem B. Bityutskiy 			err = paranoid_check_all_ff(ubi, pnum, 0,
676801c135cSArtem B. Bityutskiy 						    ubi->peb_size);
677801c135cSArtem B. Bityutskiy 			if (err)
678801c135cSArtem B. Bityutskiy 				return err > 0 ? UBI_IO_BAD_EC_HDR : err;
679801c135cSArtem B. Bityutskiy 
680801c135cSArtem B. Bityutskiy 			if (verbose)
681801c135cSArtem B. Bityutskiy 				ubi_warn("no EC header found at PEB %d, "
682801c135cSArtem B. Bityutskiy 					 "only 0xFF bytes", pnum);
683ed45819fSArtem Bityutskiy 			else if (UBI_IO_DEBUG)
684ed45819fSArtem Bityutskiy 				dbg_msg("no EC header found at PEB %d, "
685ed45819fSArtem Bityutskiy 					"only 0xFF bytes", pnum);
686801c135cSArtem B. Bityutskiy 			return UBI_IO_PEB_EMPTY;
687801c135cSArtem B. Bityutskiy 		}
688801c135cSArtem B. Bityutskiy 
689801c135cSArtem B. Bityutskiy 		/*
690801c135cSArtem B. Bityutskiy 		 * This is not a valid erase counter header, and these are not
691801c135cSArtem B. Bityutskiy 		 * 0xFF bytes. Report that the header is corrupted.
692801c135cSArtem B. Bityutskiy 		 */
693801c135cSArtem B. Bityutskiy 		if (verbose) {
694801c135cSArtem B. Bityutskiy 			ubi_warn("bad magic number at PEB %d: %08x instead of "
695801c135cSArtem B. Bityutskiy 				 "%08x", pnum, magic, UBI_EC_HDR_MAGIC);
696801c135cSArtem B. Bityutskiy 			ubi_dbg_dump_ec_hdr(ec_hdr);
697ed45819fSArtem Bityutskiy 		} else if (UBI_IO_DEBUG)
698ed45819fSArtem Bityutskiy 			dbg_msg("bad magic number at PEB %d: %08x instead of "
699ed45819fSArtem Bityutskiy 				"%08x", pnum, magic, UBI_EC_HDR_MAGIC);
700801c135cSArtem B. Bityutskiy 		return UBI_IO_BAD_EC_HDR;
701801c135cSArtem B. Bityutskiy 	}
702801c135cSArtem B. Bityutskiy 
703801c135cSArtem B. Bityutskiy 	crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
7043261ebd7SChristoph Hellwig 	hdr_crc = be32_to_cpu(ec_hdr->hdr_crc);
705801c135cSArtem B. Bityutskiy 
706801c135cSArtem B. Bityutskiy 	if (hdr_crc != crc) {
707801c135cSArtem B. Bityutskiy 		if (verbose) {
7089c9ec147SArtem Bityutskiy 			ubi_warn("bad EC header CRC at PEB %d, calculated "
7099c9ec147SArtem Bityutskiy 				 "%#08x, read %#08x", pnum, crc, hdr_crc);
710801c135cSArtem B. Bityutskiy 			ubi_dbg_dump_ec_hdr(ec_hdr);
711ed45819fSArtem Bityutskiy 		} else if (UBI_IO_DEBUG)
712ed45819fSArtem Bityutskiy 			dbg_msg("bad EC header CRC at PEB %d, calculated "
713ed45819fSArtem Bityutskiy 				"%#08x, read %#08x", pnum, crc, hdr_crc);
714801c135cSArtem B. Bityutskiy 		return UBI_IO_BAD_EC_HDR;
715801c135cSArtem B. Bityutskiy 	}
716801c135cSArtem B. Bityutskiy 
717801c135cSArtem B. Bityutskiy 	/* And of course validate what has just been read from the media */
718801c135cSArtem B. Bityutskiy 	err = validate_ec_hdr(ubi, ec_hdr);
719801c135cSArtem B. Bityutskiy 	if (err) {
720801c135cSArtem B. Bityutskiy 		ubi_err("validation failed for PEB %d", pnum);
721801c135cSArtem B. Bityutskiy 		return -EINVAL;
722801c135cSArtem B. Bityutskiy 	}
723801c135cSArtem B. Bityutskiy 
724801c135cSArtem B. Bityutskiy 	return read_err ? UBI_IO_BITFLIPS : 0;
725801c135cSArtem B. Bityutskiy }
726801c135cSArtem B. Bityutskiy 
727801c135cSArtem B. Bityutskiy /**
728801c135cSArtem B. Bityutskiy  * ubi_io_write_ec_hdr - write an erase counter header.
729801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
730801c135cSArtem B. Bityutskiy  * @pnum: physical eraseblock to write to
731801c135cSArtem B. Bityutskiy  * @ec_hdr: the erase counter header to write
732801c135cSArtem B. Bityutskiy  *
733801c135cSArtem B. Bityutskiy  * This function writes erase counter header described by @ec_hdr to physical
734801c135cSArtem B. Bityutskiy  * eraseblock @pnum. It also fills most fields of @ec_hdr before writing, so
735801c135cSArtem B. Bityutskiy  * the caller do not have to fill them. Callers must only fill the @ec_hdr->ec
736801c135cSArtem B. Bityutskiy  * field.
737801c135cSArtem B. Bityutskiy  *
738801c135cSArtem B. Bityutskiy  * This function returns zero in case of success and a negative error code in
739801c135cSArtem B. Bityutskiy  * case of failure. If %-EIO is returned, the physical eraseblock most probably
740801c135cSArtem B. Bityutskiy  * went bad.
741801c135cSArtem B. Bityutskiy  */
742e88d6e10SArtem Bityutskiy int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum,
743801c135cSArtem B. Bityutskiy 			struct ubi_ec_hdr *ec_hdr)
744801c135cSArtem B. Bityutskiy {
745801c135cSArtem B. Bityutskiy 	int err;
746801c135cSArtem B. Bityutskiy 	uint32_t crc;
747801c135cSArtem B. Bityutskiy 
748801c135cSArtem B. Bityutskiy 	dbg_io("write EC header to PEB %d", pnum);
749801c135cSArtem B. Bityutskiy 	ubi_assert(pnum >= 0 &&  pnum < ubi->peb_count);
750801c135cSArtem B. Bityutskiy 
7513261ebd7SChristoph Hellwig 	ec_hdr->magic = cpu_to_be32(UBI_EC_HDR_MAGIC);
752801c135cSArtem B. Bityutskiy 	ec_hdr->version = UBI_VERSION;
7533261ebd7SChristoph Hellwig 	ec_hdr->vid_hdr_offset = cpu_to_be32(ubi->vid_hdr_offset);
7543261ebd7SChristoph Hellwig 	ec_hdr->data_offset = cpu_to_be32(ubi->leb_start);
755801c135cSArtem B. Bityutskiy 	crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
7563261ebd7SChristoph Hellwig 	ec_hdr->hdr_crc = cpu_to_be32(crc);
757801c135cSArtem B. Bityutskiy 
758801c135cSArtem B. Bityutskiy 	err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr);
759801c135cSArtem B. Bityutskiy 	if (err)
760801c135cSArtem B. Bityutskiy 		return -EINVAL;
761801c135cSArtem B. Bityutskiy 
762801c135cSArtem B. Bityutskiy 	err = ubi_io_write(ubi, ec_hdr, pnum, 0, ubi->ec_hdr_alsize);
763801c135cSArtem B. Bityutskiy 	return err;
764801c135cSArtem B. Bityutskiy }
765801c135cSArtem B. Bityutskiy 
766801c135cSArtem B. Bityutskiy /**
767801c135cSArtem B. Bityutskiy  * validate_vid_hdr - validate a volume identifier header.
768801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
769801c135cSArtem B. Bityutskiy  * @vid_hdr: the volume identifier header to check
770801c135cSArtem B. Bityutskiy  *
771801c135cSArtem B. Bityutskiy  * This function checks that data stored in the volume identifier header
772801c135cSArtem B. Bityutskiy  * @vid_hdr. Returns zero if the VID header is OK and %1 if not.
773801c135cSArtem B. Bityutskiy  */
774801c135cSArtem B. Bityutskiy static int validate_vid_hdr(const struct ubi_device *ubi,
775801c135cSArtem B. Bityutskiy 			    const struct ubi_vid_hdr *vid_hdr)
776801c135cSArtem B. Bityutskiy {
777801c135cSArtem B. Bityutskiy 	int vol_type = vid_hdr->vol_type;
778801c135cSArtem B. Bityutskiy 	int copy_flag = vid_hdr->copy_flag;
7793261ebd7SChristoph Hellwig 	int vol_id = be32_to_cpu(vid_hdr->vol_id);
7803261ebd7SChristoph Hellwig 	int lnum = be32_to_cpu(vid_hdr->lnum);
781801c135cSArtem B. Bityutskiy 	int compat = vid_hdr->compat;
7823261ebd7SChristoph Hellwig 	int data_size = be32_to_cpu(vid_hdr->data_size);
7833261ebd7SChristoph Hellwig 	int used_ebs = be32_to_cpu(vid_hdr->used_ebs);
7843261ebd7SChristoph Hellwig 	int data_pad = be32_to_cpu(vid_hdr->data_pad);
7853261ebd7SChristoph Hellwig 	int data_crc = be32_to_cpu(vid_hdr->data_crc);
786801c135cSArtem B. Bityutskiy 	int usable_leb_size = ubi->leb_size - data_pad;
787801c135cSArtem B. Bityutskiy 
788801c135cSArtem B. Bityutskiy 	if (copy_flag != 0 && copy_flag != 1) {
789801c135cSArtem B. Bityutskiy 		dbg_err("bad copy_flag");
790801c135cSArtem B. Bityutskiy 		goto bad;
791801c135cSArtem B. Bityutskiy 	}
792801c135cSArtem B. Bityutskiy 
793801c135cSArtem B. Bityutskiy 	if (vol_id < 0 || lnum < 0 || data_size < 0 || used_ebs < 0 ||
794801c135cSArtem B. Bityutskiy 	    data_pad < 0) {
795801c135cSArtem B. Bityutskiy 		dbg_err("negative values");
796801c135cSArtem B. Bityutskiy 		goto bad;
797801c135cSArtem B. Bityutskiy 	}
798801c135cSArtem B. Bityutskiy 
799801c135cSArtem B. Bityutskiy 	if (vol_id >= UBI_MAX_VOLUMES && vol_id < UBI_INTERNAL_VOL_START) {
800801c135cSArtem B. Bityutskiy 		dbg_err("bad vol_id");
801801c135cSArtem B. Bityutskiy 		goto bad;
802801c135cSArtem B. Bityutskiy 	}
803801c135cSArtem B. Bityutskiy 
804801c135cSArtem B. Bityutskiy 	if (vol_id < UBI_INTERNAL_VOL_START && compat != 0) {
805801c135cSArtem B. Bityutskiy 		dbg_err("bad compat");
806801c135cSArtem B. Bityutskiy 		goto bad;
807801c135cSArtem B. Bityutskiy 	}
808801c135cSArtem B. Bityutskiy 
809801c135cSArtem B. Bityutskiy 	if (vol_id >= UBI_INTERNAL_VOL_START && compat != UBI_COMPAT_DELETE &&
810801c135cSArtem B. Bityutskiy 	    compat != UBI_COMPAT_RO && compat != UBI_COMPAT_PRESERVE &&
811801c135cSArtem B. Bityutskiy 	    compat != UBI_COMPAT_REJECT) {
812801c135cSArtem B. Bityutskiy 		dbg_err("bad compat");
813801c135cSArtem B. Bityutskiy 		goto bad;
814801c135cSArtem B. Bityutskiy 	}
815801c135cSArtem B. Bityutskiy 
816801c135cSArtem B. Bityutskiy 	if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) {
817801c135cSArtem B. Bityutskiy 		dbg_err("bad vol_type");
818801c135cSArtem B. Bityutskiy 		goto bad;
819801c135cSArtem B. Bityutskiy 	}
820801c135cSArtem B. Bityutskiy 
821801c135cSArtem B. Bityutskiy 	if (data_pad >= ubi->leb_size / 2) {
822801c135cSArtem B. Bityutskiy 		dbg_err("bad data_pad");
823801c135cSArtem B. Bityutskiy 		goto bad;
824801c135cSArtem B. Bityutskiy 	}
825801c135cSArtem B. Bityutskiy 
826801c135cSArtem B. Bityutskiy 	if (vol_type == UBI_VID_STATIC) {
827801c135cSArtem B. Bityutskiy 		/*
828801c135cSArtem B. Bityutskiy 		 * Although from high-level point of view static volumes may
829801c135cSArtem B. Bityutskiy 		 * contain zero bytes of data, but no VID headers can contain
830801c135cSArtem B. Bityutskiy 		 * zero at these fields, because they empty volumes do not have
831801c135cSArtem B. Bityutskiy 		 * mapped logical eraseblocks.
832801c135cSArtem B. Bityutskiy 		 */
833801c135cSArtem B. Bityutskiy 		if (used_ebs == 0) {
834801c135cSArtem B. Bityutskiy 			dbg_err("zero used_ebs");
835801c135cSArtem B. Bityutskiy 			goto bad;
836801c135cSArtem B. Bityutskiy 		}
837801c135cSArtem B. Bityutskiy 		if (data_size == 0) {
838801c135cSArtem B. Bityutskiy 			dbg_err("zero data_size");
839801c135cSArtem B. Bityutskiy 			goto bad;
840801c135cSArtem B. Bityutskiy 		}
841801c135cSArtem B. Bityutskiy 		if (lnum < used_ebs - 1) {
842801c135cSArtem B. Bityutskiy 			if (data_size != usable_leb_size) {
843801c135cSArtem B. Bityutskiy 				dbg_err("bad data_size");
844801c135cSArtem B. Bityutskiy 				goto bad;
845801c135cSArtem B. Bityutskiy 			}
846801c135cSArtem B. Bityutskiy 		} else if (lnum == used_ebs - 1) {
847801c135cSArtem B. Bityutskiy 			if (data_size == 0) {
848801c135cSArtem B. Bityutskiy 				dbg_err("bad data_size at last LEB");
849801c135cSArtem B. Bityutskiy 				goto bad;
850801c135cSArtem B. Bityutskiy 			}
851801c135cSArtem B. Bityutskiy 		} else {
852801c135cSArtem B. Bityutskiy 			dbg_err("too high lnum");
853801c135cSArtem B. Bityutskiy 			goto bad;
854801c135cSArtem B. Bityutskiy 		}
855801c135cSArtem B. Bityutskiy 	} else {
856801c135cSArtem B. Bityutskiy 		if (copy_flag == 0) {
857801c135cSArtem B. Bityutskiy 			if (data_crc != 0) {
858801c135cSArtem B. Bityutskiy 				dbg_err("non-zero data CRC");
859801c135cSArtem B. Bityutskiy 				goto bad;
860801c135cSArtem B. Bityutskiy 			}
861801c135cSArtem B. Bityutskiy 			if (data_size != 0) {
862801c135cSArtem B. Bityutskiy 				dbg_err("non-zero data_size");
863801c135cSArtem B. Bityutskiy 				goto bad;
864801c135cSArtem B. Bityutskiy 			}
865801c135cSArtem B. Bityutskiy 		} else {
866801c135cSArtem B. Bityutskiy 			if (data_size == 0) {
867801c135cSArtem B. Bityutskiy 				dbg_err("zero data_size of copy");
868801c135cSArtem B. Bityutskiy 				goto bad;
869801c135cSArtem B. Bityutskiy 			}
870801c135cSArtem B. Bityutskiy 		}
871801c135cSArtem B. Bityutskiy 		if (used_ebs != 0) {
872801c135cSArtem B. Bityutskiy 			dbg_err("bad used_ebs");
873801c135cSArtem B. Bityutskiy 			goto bad;
874801c135cSArtem B. Bityutskiy 		}
875801c135cSArtem B. Bityutskiy 	}
876801c135cSArtem B. Bityutskiy 
877801c135cSArtem B. Bityutskiy 	return 0;
878801c135cSArtem B. Bityutskiy 
879801c135cSArtem B. Bityutskiy bad:
880801c135cSArtem B. Bityutskiy 	ubi_err("bad VID header");
881801c135cSArtem B. Bityutskiy 	ubi_dbg_dump_vid_hdr(vid_hdr);
882801c135cSArtem B. Bityutskiy 	ubi_dbg_dump_stack();
883801c135cSArtem B. Bityutskiy 	return 1;
884801c135cSArtem B. Bityutskiy }
885801c135cSArtem B. Bityutskiy 
886801c135cSArtem B. Bityutskiy /**
887801c135cSArtem B. Bityutskiy  * ubi_io_read_vid_hdr - read and check a volume identifier header.
888801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
889801c135cSArtem B. Bityutskiy  * @pnum: physical eraseblock number to read from
890801c135cSArtem B. Bityutskiy  * @vid_hdr: &struct ubi_vid_hdr object where to store the read volume
891801c135cSArtem B. Bityutskiy  * identifier header
892801c135cSArtem B. Bityutskiy  * @verbose: be verbose if the header is corrupted or wasn't found
893801c135cSArtem B. Bityutskiy  *
894801c135cSArtem B. Bityutskiy  * This function reads the volume identifier header from physical eraseblock
895801c135cSArtem B. Bityutskiy  * @pnum and stores it in @vid_hdr. It also checks CRC checksum of the read
896801c135cSArtem B. Bityutskiy  * volume identifier header. The following codes may be returned:
897801c135cSArtem B. Bityutskiy  *
898801c135cSArtem B. Bityutskiy  * o %0 if the CRC checksum is correct and the header was successfully read;
899801c135cSArtem B. Bityutskiy  * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected
900801c135cSArtem B. Bityutskiy  *   and corrected by the flash driver; this is harmless but may indicate that
901801c135cSArtem B. Bityutskiy  *   this eraseblock may become bad soon;
902801c135cSArtem B. Bityutskiy  * o %UBI_IO_BAD_VID_HRD if the volume identifier header is corrupted (a CRC
903801c135cSArtem B. Bityutskiy  *   error detected);
904801c135cSArtem B. Bityutskiy  * o %UBI_IO_PEB_FREE if the physical eraseblock is free (i.e., there is no VID
905801c135cSArtem B. Bityutskiy  *   header there);
906801c135cSArtem B. Bityutskiy  * o a negative error code in case of failure.
907801c135cSArtem B. Bityutskiy  */
908e88d6e10SArtem Bityutskiy int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
909801c135cSArtem B. Bityutskiy 			struct ubi_vid_hdr *vid_hdr, int verbose)
910801c135cSArtem B. Bityutskiy {
911801c135cSArtem B. Bityutskiy 	int err, read_err = 0;
912801c135cSArtem B. Bityutskiy 	uint32_t crc, magic, hdr_crc;
913801c135cSArtem B. Bityutskiy 	void *p;
914801c135cSArtem B. Bityutskiy 
915801c135cSArtem B. Bityutskiy 	dbg_io("read VID header from PEB %d", pnum);
916801c135cSArtem B. Bityutskiy 	ubi_assert(pnum >= 0 &&  pnum < ubi->peb_count);
917801c135cSArtem B. Bityutskiy 
918801c135cSArtem B. Bityutskiy 	p = (char *)vid_hdr - ubi->vid_hdr_shift;
919801c135cSArtem B. Bityutskiy 	err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset,
920801c135cSArtem B. Bityutskiy 			  ubi->vid_hdr_alsize);
921801c135cSArtem B. Bityutskiy 	if (err) {
922801c135cSArtem B. Bityutskiy 		if (err != UBI_IO_BITFLIPS && err != -EBADMSG)
923801c135cSArtem B. Bityutskiy 			return err;
924801c135cSArtem B. Bityutskiy 
925801c135cSArtem B. Bityutskiy 		/*
926801c135cSArtem B. Bityutskiy 		 * We read all the data, but either a correctable bit-flip
927801c135cSArtem B. Bityutskiy 		 * occurred, or MTD reported about some data integrity error,
928801c135cSArtem B. Bityutskiy 		 * like an ECC error in case of NAND. The former is harmless,
929801c135cSArtem B. Bityutskiy 		 * the later may mean the read data is corrupted. But we have a
930801c135cSArtem B. Bityutskiy 		 * CRC check-sum and we will identify this. If the VID header is
931801c135cSArtem B. Bityutskiy 		 * still OK, we just report this as there was a bit-flip.
932801c135cSArtem B. Bityutskiy 		 */
933801c135cSArtem B. Bityutskiy 		read_err = err;
934801c135cSArtem B. Bityutskiy 	}
935801c135cSArtem B. Bityutskiy 
9363261ebd7SChristoph Hellwig 	magic = be32_to_cpu(vid_hdr->magic);
937801c135cSArtem B. Bityutskiy 	if (magic != UBI_VID_HDR_MAGIC) {
938801c135cSArtem B. Bityutskiy 		/*
939801c135cSArtem B. Bityutskiy 		 * If we have read all 0xFF bytes, the VID header probably does
940801c135cSArtem B. Bityutskiy 		 * not exist and the physical eraseblock is assumed to be free.
941801c135cSArtem B. Bityutskiy 		 *
942801c135cSArtem B. Bityutskiy 		 * But if there was a read error, we do not test the data for
943801c135cSArtem B. Bityutskiy 		 * 0xFFs. Even if it does contain all 0xFFs, this error
944801c135cSArtem B. Bityutskiy 		 * indicates that something is still wrong with this physical
945801c135cSArtem B. Bityutskiy 		 * eraseblock and it cannot be regarded as free.
946801c135cSArtem B. Bityutskiy 		 */
947801c135cSArtem B. Bityutskiy 		if (read_err != -EBADMSG &&
948801c135cSArtem B. Bityutskiy 		    check_pattern(vid_hdr, 0xFF, UBI_VID_HDR_SIZE)) {
949801c135cSArtem B. Bityutskiy 			/* The physical eraseblock is supposedly free */
950801c135cSArtem B. Bityutskiy 
951801c135cSArtem B. Bityutskiy 			/*
952801c135cSArtem B. Bityutskiy 			 * The below is just a paranoid check, it has to be
953801c135cSArtem B. Bityutskiy 			 * compiled out if paranoid checks are disabled.
954801c135cSArtem B. Bityutskiy 			 */
955ffb6b7e4SArtem Bityutskiy 			err = paranoid_check_empty(ubi, pnum);
956801c135cSArtem B. Bityutskiy 			if (err)
957801c135cSArtem B. Bityutskiy 				return err > 0 ? UBI_IO_BAD_VID_HDR : err;
958801c135cSArtem B. Bityutskiy 
959801c135cSArtem B. Bityutskiy 			if (verbose)
960801c135cSArtem B. Bityutskiy 				ubi_warn("no VID header found at PEB %d, "
961801c135cSArtem B. Bityutskiy 					 "only 0xFF bytes", pnum);
962ed45819fSArtem Bityutskiy 			else if (UBI_IO_DEBUG)
963ed45819fSArtem Bityutskiy 				dbg_msg("no VID header found at PEB %d, "
964ed45819fSArtem Bityutskiy 					"only 0xFF bytes", pnum);
965801c135cSArtem B. Bityutskiy 			return UBI_IO_PEB_FREE;
966801c135cSArtem B. Bityutskiy 		}
967801c135cSArtem B. Bityutskiy 
968801c135cSArtem B. Bityutskiy 		/*
969801c135cSArtem B. Bityutskiy 		 * This is not a valid VID header, and these are not 0xFF
970801c135cSArtem B. Bityutskiy 		 * bytes. Report that the header is corrupted.
971801c135cSArtem B. Bityutskiy 		 */
972801c135cSArtem B. Bityutskiy 		if (verbose) {
973801c135cSArtem B. Bityutskiy 			ubi_warn("bad magic number at PEB %d: %08x instead of "
974801c135cSArtem B. Bityutskiy 				 "%08x", pnum, magic, UBI_VID_HDR_MAGIC);
975801c135cSArtem B. Bityutskiy 			ubi_dbg_dump_vid_hdr(vid_hdr);
976ed45819fSArtem Bityutskiy 		} else if (UBI_IO_DEBUG)
977ed45819fSArtem Bityutskiy 			dbg_msg("bad magic number at PEB %d: %08x instead of "
978ed45819fSArtem Bityutskiy 				"%08x", pnum, magic, UBI_VID_HDR_MAGIC);
979801c135cSArtem B. Bityutskiy 		return UBI_IO_BAD_VID_HDR;
980801c135cSArtem B. Bityutskiy 	}
981801c135cSArtem B. Bityutskiy 
982801c135cSArtem B. Bityutskiy 	crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC);
9833261ebd7SChristoph Hellwig 	hdr_crc = be32_to_cpu(vid_hdr->hdr_crc);
984801c135cSArtem B. Bityutskiy 
985801c135cSArtem B. Bityutskiy 	if (hdr_crc != crc) {
986801c135cSArtem B. Bityutskiy 		if (verbose) {
987801c135cSArtem B. Bityutskiy 			ubi_warn("bad CRC at PEB %d, calculated %#08x, "
988801c135cSArtem B. Bityutskiy 				 "read %#08x", pnum, crc, hdr_crc);
989801c135cSArtem B. Bityutskiy 			ubi_dbg_dump_vid_hdr(vid_hdr);
990ed45819fSArtem Bityutskiy 		} else if (UBI_IO_DEBUG)
991ed45819fSArtem Bityutskiy 			dbg_msg("bad CRC at PEB %d, calculated %#08x, "
992ed45819fSArtem Bityutskiy 				"read %#08x", pnum, crc, hdr_crc);
993801c135cSArtem B. Bityutskiy 		return UBI_IO_BAD_VID_HDR;
994801c135cSArtem B. Bityutskiy 	}
995801c135cSArtem B. Bityutskiy 
996801c135cSArtem B. Bityutskiy 	/* Validate the VID header that we have just read */
997801c135cSArtem B. Bityutskiy 	err = validate_vid_hdr(ubi, vid_hdr);
998801c135cSArtem B. Bityutskiy 	if (err) {
999801c135cSArtem B. Bityutskiy 		ubi_err("validation failed for PEB %d", pnum);
1000801c135cSArtem B. Bityutskiy 		return -EINVAL;
1001801c135cSArtem B. Bityutskiy 	}
1002801c135cSArtem B. Bityutskiy 
1003801c135cSArtem B. Bityutskiy 	return read_err ? UBI_IO_BITFLIPS : 0;
1004801c135cSArtem B. Bityutskiy }
1005801c135cSArtem B. Bityutskiy 
1006801c135cSArtem B. Bityutskiy /**
1007801c135cSArtem B. Bityutskiy  * ubi_io_write_vid_hdr - write a volume identifier header.
1008801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
1009801c135cSArtem B. Bityutskiy  * @pnum: the physical eraseblock number to write to
1010801c135cSArtem B. Bityutskiy  * @vid_hdr: the volume identifier header to write
1011801c135cSArtem B. Bityutskiy  *
1012801c135cSArtem B. Bityutskiy  * This function writes the volume identifier header described by @vid_hdr to
1013801c135cSArtem B. Bityutskiy  * physical eraseblock @pnum. This function automatically fills the
1014801c135cSArtem B. Bityutskiy  * @vid_hdr->magic and the @vid_hdr->version fields, as well as calculates
1015801c135cSArtem B. Bityutskiy  * header CRC checksum and stores it at vid_hdr->hdr_crc.
1016801c135cSArtem B. Bityutskiy  *
1017801c135cSArtem B. Bityutskiy  * This function returns zero in case of success and a negative error code in
1018801c135cSArtem B. Bityutskiy  * case of failure. If %-EIO is returned, the physical eraseblock probably went
1019801c135cSArtem B. Bityutskiy  * bad.
1020801c135cSArtem B. Bityutskiy  */
1021e88d6e10SArtem Bityutskiy int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
1022801c135cSArtem B. Bityutskiy 			 struct ubi_vid_hdr *vid_hdr)
1023801c135cSArtem B. Bityutskiy {
1024801c135cSArtem B. Bityutskiy 	int err;
1025801c135cSArtem B. Bityutskiy 	uint32_t crc;
1026801c135cSArtem B. Bityutskiy 	void *p;
1027801c135cSArtem B. Bityutskiy 
1028801c135cSArtem B. Bityutskiy 	dbg_io("write VID header to PEB %d", pnum);
1029801c135cSArtem B. Bityutskiy 	ubi_assert(pnum >= 0 &&  pnum < ubi->peb_count);
1030801c135cSArtem B. Bityutskiy 
1031801c135cSArtem B. Bityutskiy 	err = paranoid_check_peb_ec_hdr(ubi, pnum);
1032801c135cSArtem B. Bityutskiy 	if (err)
1033801c135cSArtem B. Bityutskiy 		return err > 0 ? -EINVAL : err;
1034801c135cSArtem B. Bityutskiy 
10353261ebd7SChristoph Hellwig 	vid_hdr->magic = cpu_to_be32(UBI_VID_HDR_MAGIC);
1036801c135cSArtem B. Bityutskiy 	vid_hdr->version = UBI_VERSION;
1037801c135cSArtem B. Bityutskiy 	crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC);
10383261ebd7SChristoph Hellwig 	vid_hdr->hdr_crc = cpu_to_be32(crc);
1039801c135cSArtem B. Bityutskiy 
1040801c135cSArtem B. Bityutskiy 	err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr);
1041801c135cSArtem B. Bityutskiy 	if (err)
1042801c135cSArtem B. Bityutskiy 		return -EINVAL;
1043801c135cSArtem B. Bityutskiy 
1044801c135cSArtem B. Bityutskiy 	p = (char *)vid_hdr - ubi->vid_hdr_shift;
1045801c135cSArtem B. Bityutskiy 	err = ubi_io_write(ubi, p, pnum, ubi->vid_hdr_aloffset,
1046801c135cSArtem B. Bityutskiy 			   ubi->vid_hdr_alsize);
1047801c135cSArtem B. Bityutskiy 	return err;
1048801c135cSArtem B. Bityutskiy }
1049801c135cSArtem B. Bityutskiy 
1050801c135cSArtem B. Bityutskiy #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
1051801c135cSArtem B. Bityutskiy 
1052801c135cSArtem B. Bityutskiy /**
1053801c135cSArtem B. Bityutskiy  * paranoid_check_not_bad - ensure that a physical eraseblock is not bad.
1054801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
1055801c135cSArtem B. Bityutskiy  * @pnum: physical eraseblock number to check
1056801c135cSArtem B. Bityutskiy  *
1057801c135cSArtem B. Bityutskiy  * This function returns zero if the physical eraseblock is good, a positive
1058801c135cSArtem B. Bityutskiy  * number if it is bad and a negative error code if an error occurred.
1059801c135cSArtem B. Bityutskiy  */
1060801c135cSArtem B. Bityutskiy static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum)
1061801c135cSArtem B. Bityutskiy {
1062801c135cSArtem B. Bityutskiy 	int err;
1063801c135cSArtem B. Bityutskiy 
1064801c135cSArtem B. Bityutskiy 	err = ubi_io_is_bad(ubi, pnum);
1065801c135cSArtem B. Bityutskiy 	if (!err)
1066801c135cSArtem B. Bityutskiy 		return err;
1067801c135cSArtem B. Bityutskiy 
1068801c135cSArtem B. Bityutskiy 	ubi_err("paranoid check failed for PEB %d", pnum);
1069801c135cSArtem B. Bityutskiy 	ubi_dbg_dump_stack();
1070801c135cSArtem B. Bityutskiy 	return err;
1071801c135cSArtem B. Bityutskiy }
1072801c135cSArtem B. Bityutskiy 
1073801c135cSArtem B. Bityutskiy /**
1074801c135cSArtem B. Bityutskiy  * paranoid_check_ec_hdr - check if an erase counter header is all right.
1075801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
1076801c135cSArtem B. Bityutskiy  * @pnum: physical eraseblock number the erase counter header belongs to
1077801c135cSArtem B. Bityutskiy  * @ec_hdr: the erase counter header to check
1078801c135cSArtem B. Bityutskiy  *
1079801c135cSArtem B. Bityutskiy  * This function returns zero if the erase counter header contains valid
1080801c135cSArtem B. Bityutskiy  * values, and %1 if not.
1081801c135cSArtem B. Bityutskiy  */
1082801c135cSArtem B. Bityutskiy static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum,
1083801c135cSArtem B. Bityutskiy 				 const struct ubi_ec_hdr *ec_hdr)
1084801c135cSArtem B. Bityutskiy {
1085801c135cSArtem B. Bityutskiy 	int err;
1086801c135cSArtem B. Bityutskiy 	uint32_t magic;
1087801c135cSArtem B. Bityutskiy 
10883261ebd7SChristoph Hellwig 	magic = be32_to_cpu(ec_hdr->magic);
1089801c135cSArtem B. Bityutskiy 	if (magic != UBI_EC_HDR_MAGIC) {
1090801c135cSArtem B. Bityutskiy 		ubi_err("bad magic %#08x, must be %#08x",
1091801c135cSArtem B. Bityutskiy 			magic, UBI_EC_HDR_MAGIC);
1092801c135cSArtem B. Bityutskiy 		goto fail;
1093801c135cSArtem B. Bityutskiy 	}
1094801c135cSArtem B. Bityutskiy 
1095801c135cSArtem B. Bityutskiy 	err = validate_ec_hdr(ubi, ec_hdr);
1096801c135cSArtem B. Bityutskiy 	if (err) {
1097801c135cSArtem B. Bityutskiy 		ubi_err("paranoid check failed for PEB %d", pnum);
1098801c135cSArtem B. Bityutskiy 		goto fail;
1099801c135cSArtem B. Bityutskiy 	}
1100801c135cSArtem B. Bityutskiy 
1101801c135cSArtem B. Bityutskiy 	return 0;
1102801c135cSArtem B. Bityutskiy 
1103801c135cSArtem B. Bityutskiy fail:
1104801c135cSArtem B. Bityutskiy 	ubi_dbg_dump_ec_hdr(ec_hdr);
1105801c135cSArtem B. Bityutskiy 	ubi_dbg_dump_stack();
1106801c135cSArtem B. Bityutskiy 	return 1;
1107801c135cSArtem B. Bityutskiy }
1108801c135cSArtem B. Bityutskiy 
1109801c135cSArtem B. Bityutskiy /**
1110ebaaf1afSArtem Bityutskiy  * paranoid_check_peb_ec_hdr - check erase counter header.
1111801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
1112801c135cSArtem B. Bityutskiy  * @pnum: the physical eraseblock number to check
1113801c135cSArtem B. Bityutskiy  *
1114801c135cSArtem B. Bityutskiy  * This function returns zero if the erase counter header is all right, %1 if
1115801c135cSArtem B. Bityutskiy  * not, and a negative error code if an error occurred.
1116801c135cSArtem B. Bityutskiy  */
1117801c135cSArtem B. Bityutskiy static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum)
1118801c135cSArtem B. Bityutskiy {
1119801c135cSArtem B. Bityutskiy 	int err;
1120801c135cSArtem B. Bityutskiy 	uint32_t crc, hdr_crc;
1121801c135cSArtem B. Bityutskiy 	struct ubi_ec_hdr *ec_hdr;
1122801c135cSArtem B. Bityutskiy 
112333818bbbSArtem Bityutskiy 	ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
1124801c135cSArtem B. Bityutskiy 	if (!ec_hdr)
1125801c135cSArtem B. Bityutskiy 		return -ENOMEM;
1126801c135cSArtem B. Bityutskiy 
1127801c135cSArtem B. Bityutskiy 	err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE);
1128801c135cSArtem B. Bityutskiy 	if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG)
1129801c135cSArtem B. Bityutskiy 		goto exit;
1130801c135cSArtem B. Bityutskiy 
1131801c135cSArtem B. Bityutskiy 	crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
11323261ebd7SChristoph Hellwig 	hdr_crc = be32_to_cpu(ec_hdr->hdr_crc);
1133801c135cSArtem B. Bityutskiy 	if (hdr_crc != crc) {
1134801c135cSArtem B. Bityutskiy 		ubi_err("bad CRC, calculated %#08x, read %#08x", crc, hdr_crc);
1135801c135cSArtem B. Bityutskiy 		ubi_err("paranoid check failed for PEB %d", pnum);
1136801c135cSArtem B. Bityutskiy 		ubi_dbg_dump_ec_hdr(ec_hdr);
1137801c135cSArtem B. Bityutskiy 		ubi_dbg_dump_stack();
1138801c135cSArtem B. Bityutskiy 		err = 1;
1139801c135cSArtem B. Bityutskiy 		goto exit;
1140801c135cSArtem B. Bityutskiy 	}
1141801c135cSArtem B. Bityutskiy 
1142801c135cSArtem B. Bityutskiy 	err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr);
1143801c135cSArtem B. Bityutskiy 
1144801c135cSArtem B. Bityutskiy exit:
1145801c135cSArtem B. Bityutskiy 	kfree(ec_hdr);
1146801c135cSArtem B. Bityutskiy 	return err;
1147801c135cSArtem B. Bityutskiy }
1148801c135cSArtem B. Bityutskiy 
1149801c135cSArtem B. Bityutskiy /**
1150801c135cSArtem B. Bityutskiy  * paranoid_check_vid_hdr - check that a volume identifier header is all right.
1151801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
1152801c135cSArtem B. Bityutskiy  * @pnum: physical eraseblock number the volume identifier header belongs to
1153801c135cSArtem B. Bityutskiy  * @vid_hdr: the volume identifier header to check
1154801c135cSArtem B. Bityutskiy  *
1155801c135cSArtem B. Bityutskiy  * This function returns zero if the volume identifier header is all right, and
1156801c135cSArtem B. Bityutskiy  * %1 if not.
1157801c135cSArtem B. Bityutskiy  */
1158801c135cSArtem B. Bityutskiy static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum,
1159801c135cSArtem B. Bityutskiy 				  const struct ubi_vid_hdr *vid_hdr)
1160801c135cSArtem B. Bityutskiy {
1161801c135cSArtem B. Bityutskiy 	int err;
1162801c135cSArtem B. Bityutskiy 	uint32_t magic;
1163801c135cSArtem B. Bityutskiy 
11643261ebd7SChristoph Hellwig 	magic = be32_to_cpu(vid_hdr->magic);
1165801c135cSArtem B. Bityutskiy 	if (magic != UBI_VID_HDR_MAGIC) {
1166801c135cSArtem B. Bityutskiy 		ubi_err("bad VID header magic %#08x at PEB %d, must be %#08x",
1167801c135cSArtem B. Bityutskiy 			magic, pnum, UBI_VID_HDR_MAGIC);
1168801c135cSArtem B. Bityutskiy 		goto fail;
1169801c135cSArtem B. Bityutskiy 	}
1170801c135cSArtem B. Bityutskiy 
1171801c135cSArtem B. Bityutskiy 	err = validate_vid_hdr(ubi, vid_hdr);
1172801c135cSArtem B. Bityutskiy 	if (err) {
1173801c135cSArtem B. Bityutskiy 		ubi_err("paranoid check failed for PEB %d", pnum);
1174801c135cSArtem B. Bityutskiy 		goto fail;
1175801c135cSArtem B. Bityutskiy 	}
1176801c135cSArtem B. Bityutskiy 
1177801c135cSArtem B. Bityutskiy 	return err;
1178801c135cSArtem B. Bityutskiy 
1179801c135cSArtem B. Bityutskiy fail:
1180801c135cSArtem B. Bityutskiy 	ubi_err("paranoid check failed for PEB %d", pnum);
1181801c135cSArtem B. Bityutskiy 	ubi_dbg_dump_vid_hdr(vid_hdr);
1182801c135cSArtem B. Bityutskiy 	ubi_dbg_dump_stack();
1183801c135cSArtem B. Bityutskiy 	return 1;
1184801c135cSArtem B. Bityutskiy 
1185801c135cSArtem B. Bityutskiy }
1186801c135cSArtem B. Bityutskiy 
1187801c135cSArtem B. Bityutskiy /**
1188ebaaf1afSArtem Bityutskiy  * paranoid_check_peb_vid_hdr - check volume identifier header.
1189801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
1190801c135cSArtem B. Bityutskiy  * @pnum: the physical eraseblock number to check
1191801c135cSArtem B. Bityutskiy  *
1192801c135cSArtem B. Bityutskiy  * This function returns zero if the volume identifier header is all right,
1193801c135cSArtem B. Bityutskiy  * %1 if not, and a negative error code if an error occurred.
1194801c135cSArtem B. Bityutskiy  */
1195801c135cSArtem B. Bityutskiy static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum)
1196801c135cSArtem B. Bityutskiy {
1197801c135cSArtem B. Bityutskiy 	int err;
1198801c135cSArtem B. Bityutskiy 	uint32_t crc, hdr_crc;
1199801c135cSArtem B. Bityutskiy 	struct ubi_vid_hdr *vid_hdr;
1200801c135cSArtem B. Bityutskiy 	void *p;
1201801c135cSArtem B. Bityutskiy 
120233818bbbSArtem Bityutskiy 	vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
1203801c135cSArtem B. Bityutskiy 	if (!vid_hdr)
1204801c135cSArtem B. Bityutskiy 		return -ENOMEM;
1205801c135cSArtem B. Bityutskiy 
1206801c135cSArtem B. Bityutskiy 	p = (char *)vid_hdr - ubi->vid_hdr_shift;
1207801c135cSArtem B. Bityutskiy 	err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset,
1208801c135cSArtem B. Bityutskiy 			  ubi->vid_hdr_alsize);
1209801c135cSArtem B. Bityutskiy 	if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG)
1210801c135cSArtem B. Bityutskiy 		goto exit;
1211801c135cSArtem B. Bityutskiy 
1212801c135cSArtem B. Bityutskiy 	crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_EC_HDR_SIZE_CRC);
12133261ebd7SChristoph Hellwig 	hdr_crc = be32_to_cpu(vid_hdr->hdr_crc);
1214801c135cSArtem B. Bityutskiy 	if (hdr_crc != crc) {
1215801c135cSArtem B. Bityutskiy 		ubi_err("bad VID header CRC at PEB %d, calculated %#08x, "
1216801c135cSArtem B. Bityutskiy 			"read %#08x", pnum, crc, hdr_crc);
1217801c135cSArtem B. Bityutskiy 		ubi_err("paranoid check failed for PEB %d", pnum);
1218801c135cSArtem B. Bityutskiy 		ubi_dbg_dump_vid_hdr(vid_hdr);
1219801c135cSArtem B. Bityutskiy 		ubi_dbg_dump_stack();
1220801c135cSArtem B. Bityutskiy 		err = 1;
1221801c135cSArtem B. Bityutskiy 		goto exit;
1222801c135cSArtem B. Bityutskiy 	}
1223801c135cSArtem B. Bityutskiy 
1224801c135cSArtem B. Bityutskiy 	err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr);
1225801c135cSArtem B. Bityutskiy 
1226801c135cSArtem B. Bityutskiy exit:
1227801c135cSArtem B. Bityutskiy 	ubi_free_vid_hdr(ubi, vid_hdr);
1228801c135cSArtem B. Bityutskiy 	return err;
1229801c135cSArtem B. Bityutskiy }
1230801c135cSArtem B. Bityutskiy 
1231801c135cSArtem B. Bityutskiy /**
1232801c135cSArtem B. Bityutskiy  * paranoid_check_all_ff - check that a region of flash is empty.
1233801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
1234801c135cSArtem B. Bityutskiy  * @pnum: the physical eraseblock number to check
1235801c135cSArtem B. Bityutskiy  * @offset: the starting offset within the physical eraseblock to check
1236801c135cSArtem B. Bityutskiy  * @len: the length of the region to check
1237801c135cSArtem B. Bityutskiy  *
1238801c135cSArtem B. Bityutskiy  * This function returns zero if only 0xFF bytes are present at offset
1239801c135cSArtem B. Bityutskiy  * @offset of the physical eraseblock @pnum, %1 if not, and a negative error
1240801c135cSArtem B. Bityutskiy  * code if an error occurred.
1241801c135cSArtem B. Bityutskiy  */
1242e88d6e10SArtem Bityutskiy static int paranoid_check_all_ff(struct ubi_device *ubi, int pnum, int offset,
1243e88d6e10SArtem Bityutskiy 				 int len)
1244801c135cSArtem B. Bityutskiy {
1245801c135cSArtem B. Bityutskiy 	size_t read;
1246801c135cSArtem B. Bityutskiy 	int err;
1247801c135cSArtem B. Bityutskiy 	loff_t addr = (loff_t)pnum * ubi->peb_size + offset;
1248801c135cSArtem B. Bityutskiy 
1249e88d6e10SArtem Bityutskiy 	mutex_lock(&ubi->dbg_buf_mutex);
1250e88d6e10SArtem Bityutskiy 	err = ubi->mtd->read(ubi->mtd, addr, len, &read, ubi->dbg_peb_buf);
1251801c135cSArtem B. Bityutskiy 	if (err && err != -EUCLEAN) {
1252801c135cSArtem B. Bityutskiy 		ubi_err("error %d while reading %d bytes from PEB %d:%d, "
1253801c135cSArtem B. Bityutskiy 			"read %zd bytes", err, len, pnum, offset, read);
1254801c135cSArtem B. Bityutskiy 		goto error;
1255801c135cSArtem B. Bityutskiy 	}
1256801c135cSArtem B. Bityutskiy 
1257e88d6e10SArtem Bityutskiy 	err = check_pattern(ubi->dbg_peb_buf, 0xFF, len);
1258801c135cSArtem B. Bityutskiy 	if (err == 0) {
1259801c135cSArtem B. Bityutskiy 		ubi_err("flash region at PEB %d:%d, length %d does not "
1260801c135cSArtem B. Bityutskiy 			"contain all 0xFF bytes", pnum, offset, len);
1261801c135cSArtem B. Bityutskiy 		goto fail;
1262801c135cSArtem B. Bityutskiy 	}
1263e88d6e10SArtem Bityutskiy 	mutex_unlock(&ubi->dbg_buf_mutex);
1264801c135cSArtem B. Bityutskiy 
1265801c135cSArtem B. Bityutskiy 	return 0;
1266801c135cSArtem B. Bityutskiy 
1267801c135cSArtem B. Bityutskiy fail:
1268801c135cSArtem B. Bityutskiy 	ubi_err("paranoid check failed for PEB %d", pnum);
1269c8566350SArtem Bityutskiy 	ubi_msg("hex dump of the %d-%d region", offset, offset + len);
12706986646bSArtem Bityutskiy 	print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
1271e88d6e10SArtem Bityutskiy 		       ubi->dbg_peb_buf, len, 1);
1272801c135cSArtem B. Bityutskiy 	err = 1;
1273801c135cSArtem B. Bityutskiy error:
1274801c135cSArtem B. Bityutskiy 	ubi_dbg_dump_stack();
1275e88d6e10SArtem Bityutskiy 	mutex_unlock(&ubi->dbg_buf_mutex);
1276801c135cSArtem B. Bityutskiy 	return err;
1277801c135cSArtem B. Bityutskiy }
1278801c135cSArtem B. Bityutskiy 
1279ffb6b7e4SArtem Bityutskiy /**
1280ffb6b7e4SArtem Bityutskiy  * paranoid_check_empty - whether a PEB is empty.
1281ffb6b7e4SArtem Bityutskiy  * @ubi: UBI device description object
1282ffb6b7e4SArtem Bityutskiy  * @pnum: the physical eraseblock number to check
1283ffb6b7e4SArtem Bityutskiy  *
1284ffb6b7e4SArtem Bityutskiy  * This function makes sure PEB @pnum is empty, which means it contains only
1285ffb6b7e4SArtem Bityutskiy  * %0xFF data bytes. Returns zero if the PEB is empty, %1 if not, and a
1286ffb6b7e4SArtem Bityutskiy  * negative error code in case of failure.
1287ffb6b7e4SArtem Bityutskiy  *
1288ffb6b7e4SArtem Bityutskiy  * Empty PEBs have the EC header, and do not have the VID header. The caller of
1289ffb6b7e4SArtem Bityutskiy  * this function should have already made sure the PEB does not have the VID
1290ffb6b7e4SArtem Bityutskiy  * header. However, this function re-checks that, because it is possible that
1291ffb6b7e4SArtem Bityutskiy  * the header and data has already been written to the PEB.
1292ffb6b7e4SArtem Bityutskiy  *
1293ffb6b7e4SArtem Bityutskiy  * Let's consider a possible scenario. Suppose there are 2 tasks - A and B.
1294ffb6b7e4SArtem Bityutskiy  * Task A is in 'wear_leveling_worker()'. It is reading VID header of PEB X to
1295ffb6b7e4SArtem Bityutskiy  * find which LEB it corresponds to. PEB X is currently unmapped, and has no
1296ffb6b7e4SArtem Bityutskiy  * VID header. Task B is trying to write to PEB X.
1297ffb6b7e4SArtem Bityutskiy  *
1298ffb6b7e4SArtem Bityutskiy  * Task A: in 'ubi_io_read_vid_hdr()': reads the VID header from PEB X. The
1299ffb6b7e4SArtem Bityutskiy  *         read data contain all 0xFF bytes;
1300ffb6b7e4SArtem Bityutskiy  * Task B: writes VID header and some data to PEB X;
1301ffb6b7e4SArtem Bityutskiy  * Task A: assumes PEB X is empty, calls 'paranoid_check_empty()'. And if we
1302ffb6b7e4SArtem Bityutskiy  *         do not re-read the VID header, and do not cancel the checking if it
1303ffb6b7e4SArtem Bityutskiy  *         is there, we fail.
1304ffb6b7e4SArtem Bityutskiy  */
1305ffb6b7e4SArtem Bityutskiy static int paranoid_check_empty(struct ubi_device *ubi, int pnum)
1306ffb6b7e4SArtem Bityutskiy {
1307ffb6b7e4SArtem Bityutskiy 	int err, offs = ubi->vid_hdr_aloffset, len = ubi->vid_hdr_alsize;
1308ffb6b7e4SArtem Bityutskiy 	size_t read;
1309ffb6b7e4SArtem Bityutskiy 	uint32_t magic;
1310ffb6b7e4SArtem Bityutskiy 	const struct ubi_vid_hdr *vid_hdr;
1311ffb6b7e4SArtem Bityutskiy 
1312ffb6b7e4SArtem Bityutskiy 	mutex_lock(&ubi->dbg_buf_mutex);
1313ffb6b7e4SArtem Bityutskiy 	err = ubi->mtd->read(ubi->mtd, offs, len, &read, ubi->dbg_peb_buf);
1314ffb6b7e4SArtem Bityutskiy 	if (err && err != -EUCLEAN) {
1315ffb6b7e4SArtem Bityutskiy 		ubi_err("error %d while reading %d bytes from PEB %d:%d, "
1316ffb6b7e4SArtem Bityutskiy 			"read %zd bytes", err, len, pnum, offs, read);
1317ffb6b7e4SArtem Bityutskiy 		goto error;
1318ffb6b7e4SArtem Bityutskiy 	}
1319ffb6b7e4SArtem Bityutskiy 
1320ffb6b7e4SArtem Bityutskiy 	vid_hdr = ubi->dbg_peb_buf;
1321ffb6b7e4SArtem Bityutskiy 	magic = be32_to_cpu(vid_hdr->magic);
1322ffb6b7e4SArtem Bityutskiy 	if (magic == UBI_VID_HDR_MAGIC)
1323ffb6b7e4SArtem Bityutskiy 		/* The PEB contains VID header, so it is not empty */
1324ffb6b7e4SArtem Bityutskiy 		goto out;
1325ffb6b7e4SArtem Bityutskiy 
1326ffb6b7e4SArtem Bityutskiy 	err = check_pattern(ubi->dbg_peb_buf, 0xFF, len);
1327ffb6b7e4SArtem Bityutskiy 	if (err == 0) {
1328ffb6b7e4SArtem Bityutskiy 		ubi_err("flash region at PEB %d:%d, length %d does not "
1329ffb6b7e4SArtem Bityutskiy 			"contain all 0xFF bytes", pnum, offs, len);
1330ffb6b7e4SArtem Bityutskiy 		goto fail;
1331ffb6b7e4SArtem Bityutskiy 	}
1332ffb6b7e4SArtem Bityutskiy 
1333ffb6b7e4SArtem Bityutskiy out:
1334ffb6b7e4SArtem Bityutskiy 	mutex_unlock(&ubi->dbg_buf_mutex);
1335ffb6b7e4SArtem Bityutskiy 	return 0;
1336ffb6b7e4SArtem Bityutskiy 
1337ffb6b7e4SArtem Bityutskiy fail:
1338ffb6b7e4SArtem Bityutskiy 	ubi_err("paranoid check failed for PEB %d", pnum);
1339ffb6b7e4SArtem Bityutskiy 	ubi_msg("hex dump of the %d-%d region", offs, offs + len);
1340ffb6b7e4SArtem Bityutskiy 	print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
1341ffb6b7e4SArtem Bityutskiy 		       ubi->dbg_peb_buf, len, 1);
1342ffb6b7e4SArtem Bityutskiy 	err = 1;
1343ffb6b7e4SArtem Bityutskiy error:
1344ffb6b7e4SArtem Bityutskiy 	ubi_dbg_dump_stack();
1345ffb6b7e4SArtem Bityutskiy 	mutex_unlock(&ubi->dbg_buf_mutex);
1346ffb6b7e4SArtem Bityutskiy 	return err;
1347ffb6b7e4SArtem Bityutskiy }
1348ffb6b7e4SArtem Bityutskiy 
1349801c135cSArtem B. Bityutskiy #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */
1350