xref: /openbmc/linux/drivers/mtd/ubi/io.c (revision ec1f97f5)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2801c135cSArtem B. Bityutskiy /*
3801c135cSArtem B. Bityutskiy  * Copyright (c) International Business Machines Corp., 2006
4801c135cSArtem B. Bityutskiy  * Copyright (c) Nokia Corporation, 2006, 2007
5801c135cSArtem B. Bityutskiy  *
6801c135cSArtem B. Bityutskiy  * Author: Artem Bityutskiy (Битюцкий Артём)
7801c135cSArtem B. Bityutskiy  */
8801c135cSArtem B. Bityutskiy 
9801c135cSArtem B. Bityutskiy /*
1085c6e6e2SArtem Bityutskiy  * UBI input/output sub-system.
11801c135cSArtem B. Bityutskiy  *
1285c6e6e2SArtem Bityutskiy  * This sub-system provides a uniform way to work with all kinds of the
1385c6e6e2SArtem Bityutskiy  * underlying MTD devices. It also implements handy functions for reading and
1485c6e6e2SArtem Bityutskiy  * writing UBI headers.
15801c135cSArtem B. Bityutskiy  *
16801c135cSArtem B. Bityutskiy  * We are trying to have a paranoid mindset and not to trust to what we read
1785c6e6e2SArtem Bityutskiy  * from the flash media in order to be more secure and robust. So this
1885c6e6e2SArtem Bityutskiy  * sub-system validates every single header it reads from the flash media.
19801c135cSArtem B. Bityutskiy  *
20801c135cSArtem B. Bityutskiy  * Some words about how the eraseblock headers are stored.
21801c135cSArtem B. Bityutskiy  *
22801c135cSArtem B. Bityutskiy  * The erase counter header is always stored at offset zero. By default, the
23801c135cSArtem B. Bityutskiy  * VID header is stored after the EC header at the closest aligned offset
24801c135cSArtem B. Bityutskiy  * (i.e. aligned to the minimum I/O unit size). Data starts next to the VID
25801c135cSArtem B. Bityutskiy  * header at the closest aligned offset. But this default layout may be
26801c135cSArtem B. Bityutskiy  * changed. For example, for different reasons (e.g., optimization) UBI may be
27801c135cSArtem B. Bityutskiy  * asked to put the VID header at further offset, and even at an unaligned
28801c135cSArtem B. Bityutskiy  * offset. Of course, if the offset of the VID header is unaligned, UBI adds
29801c135cSArtem B. Bityutskiy  * proper padding in front of it. Data offset may also be changed but it has to
30801c135cSArtem B. Bityutskiy  * be aligned.
31801c135cSArtem B. Bityutskiy  *
32801c135cSArtem B. Bityutskiy  * About minimal I/O units. In general, UBI assumes flash device model where
33801c135cSArtem B. Bityutskiy  * there is only one minimal I/O unit size. E.g., in case of NOR flash it is 1,
34801c135cSArtem B. Bityutskiy  * in case of NAND flash it is a NAND page, etc. This is reported by MTD in the
352fae1312SAndrew F. Davis  * @ubi->mtd->writesize field. But as an exception, UBI admits use of another
36801c135cSArtem B. Bityutskiy  * (smaller) minimal I/O unit size for EC and VID headers to make it possible
37801c135cSArtem B. Bityutskiy  * to do different optimizations.
38801c135cSArtem B. Bityutskiy  *
39801c135cSArtem B. Bityutskiy  * This is extremely useful in case of NAND flashes which admit of several
40801c135cSArtem B. Bityutskiy  * write operations to one NAND page. In this case UBI can fit EC and VID
41801c135cSArtem B. Bityutskiy  * headers at one NAND page. Thus, UBI may use "sub-page" size as the minimal
42801c135cSArtem B. Bityutskiy  * I/O unit for the headers (the @ubi->hdrs_min_io_size field). But it still
43801c135cSArtem B. Bityutskiy  * reports NAND page size (@ubi->min_io_size) as a minimal I/O unit for the UBI
44801c135cSArtem B. Bityutskiy  * users.
45801c135cSArtem B. Bityutskiy  *
46801c135cSArtem B. Bityutskiy  * Example: some Samsung NANDs with 2KiB pages allow 4x 512-byte writes, so
47801c135cSArtem B. Bityutskiy  * although the minimal I/O unit is 2K, UBI uses 512 bytes for EC and VID
48801c135cSArtem B. Bityutskiy  * headers.
49801c135cSArtem B. Bityutskiy  *
50801c135cSArtem B. Bityutskiy  * Q: why not just to treat sub-page as a minimal I/O unit of this flash
51801c135cSArtem B. Bityutskiy  * device, e.g., make @ubi->min_io_size = 512 in the example above?
52801c135cSArtem B. Bityutskiy  *
53801c135cSArtem B. Bityutskiy  * A: because when writing a sub-page, MTD still writes a full 2K page but the
54be436f62SShinya Kuribayashi  * bytes which are not relevant to the sub-page are 0xFF. So, basically,
55be436f62SShinya Kuribayashi  * writing 4x512 sub-pages is 4 times slower than writing one 2KiB NAND page.
56be436f62SShinya Kuribayashi  * Thus, we prefer to use sub-pages only for EC and VID headers.
57801c135cSArtem B. Bityutskiy  *
58801c135cSArtem B. Bityutskiy  * As it was noted above, the VID header may start at a non-aligned offset.
59801c135cSArtem B. Bityutskiy  * For example, in case of a 2KiB page NAND flash with a 512 bytes sub-page,
60801c135cSArtem B. Bityutskiy  * the VID header may reside at offset 1984 which is the last 64 bytes of the
61801c135cSArtem B. Bityutskiy  * last sub-page (EC header is always at offset zero). This causes some
62801c135cSArtem B. Bityutskiy  * difficulties when reading and writing VID headers.
63801c135cSArtem B. Bityutskiy  *
64801c135cSArtem B. Bityutskiy  * Suppose we have a 64-byte buffer and we read a VID header at it. We change
65801c135cSArtem B. Bityutskiy  * the data and want to write this VID header out. As we can only write in
66801c135cSArtem B. Bityutskiy  * 512-byte chunks, we have to allocate one more buffer and copy our VID header
67801c135cSArtem B. Bityutskiy  * to offset 448 of this buffer.
68801c135cSArtem B. Bityutskiy  *
6985c6e6e2SArtem Bityutskiy  * The I/O sub-system does the following trick in order to avoid this extra
7085c6e6e2SArtem Bityutskiy  * copy. It always allocates a @ubi->vid_hdr_alsize bytes buffer for the VID
7185c6e6e2SArtem Bityutskiy  * header and returns a pointer to offset @ubi->vid_hdr_shift of this buffer.
7285c6e6e2SArtem Bityutskiy  * When the VID header is being written out, it shifts the VID header pointer
7385c6e6e2SArtem Bityutskiy  * back and writes the whole sub-page.
74801c135cSArtem B. Bityutskiy  */
75801c135cSArtem B. Bityutskiy 
76801c135cSArtem B. Bityutskiy #include <linux/crc32.h>
77801c135cSArtem B. Bityutskiy #include <linux/err.h>
785a0e3ad6STejun Heo #include <linux/slab.h>
79801c135cSArtem B. Bityutskiy #include "ubi.h"
80801c135cSArtem B. Bityutskiy 
818056eb4aSArtem Bityutskiy static int self_check_not_bad(const struct ubi_device *ubi, int pnum);
828056eb4aSArtem Bityutskiy static int self_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum);
838056eb4aSArtem Bityutskiy static int self_check_ec_hdr(const struct ubi_device *ubi, int pnum,
84801c135cSArtem B. Bityutskiy 			     const struct ubi_ec_hdr *ec_hdr);
858056eb4aSArtem Bityutskiy static int self_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum);
868056eb4aSArtem Bityutskiy static int self_check_vid_hdr(const struct ubi_device *ubi, int pnum,
87801c135cSArtem B. Bityutskiy 			      const struct ubi_vid_hdr *vid_hdr);
8897d6104bSArtem Bityutskiy static int self_check_write(struct ubi_device *ubi, const void *buf, int pnum,
8997d6104bSArtem Bityutskiy 			    int offset, int len);
90801c135cSArtem B. Bityutskiy 
91801c135cSArtem B. Bityutskiy /**
92801c135cSArtem B. Bityutskiy  * ubi_io_read - read data from a physical eraseblock.
93801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
94801c135cSArtem B. Bityutskiy  * @buf: buffer where to store the read data
95801c135cSArtem B. Bityutskiy  * @pnum: physical eraseblock number to read from
96801c135cSArtem B. Bityutskiy  * @offset: offset within the physical eraseblock from where to read
97801c135cSArtem B. Bityutskiy  * @len: how many bytes to read
98801c135cSArtem B. Bityutskiy  *
99801c135cSArtem B. Bityutskiy  * This function reads data from offset @offset of physical eraseblock @pnum
100801c135cSArtem B. Bityutskiy  * and stores the read data in the @buf buffer. The following return codes are
101801c135cSArtem B. Bityutskiy  * possible:
102801c135cSArtem B. Bityutskiy  *
103801c135cSArtem B. Bityutskiy  * o %0 if all the requested data were successfully read;
104801c135cSArtem B. Bityutskiy  * o %UBI_IO_BITFLIPS if all the requested data were successfully read, but
105801c135cSArtem B. Bityutskiy  *   correctable bit-flips were detected; this is harmless but may indicate
106801c135cSArtem B. Bityutskiy  *   that this eraseblock may become bad soon (but do not have to);
10763b6c1edSArtem Bityutskiy  * o %-EBADMSG if the MTD subsystem reported about data integrity problems, for
10863b6c1edSArtem Bityutskiy  *   example it can be an ECC error in case of NAND; this most probably means
10963b6c1edSArtem Bityutskiy  *   that the data is corrupted;
110801c135cSArtem B. Bityutskiy  * o %-EIO if some I/O error occurred;
111801c135cSArtem B. Bityutskiy  * o other negative error codes in case of other errors.
112801c135cSArtem B. Bityutskiy  */
ubi_io_read(const struct ubi_device * ubi,void * buf,int pnum,int offset,int len)113801c135cSArtem B. Bityutskiy int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset,
114801c135cSArtem B. Bityutskiy 		int len)
115801c135cSArtem B. Bityutskiy {
116801c135cSArtem B. Bityutskiy 	int err, retries = 0;
117801c135cSArtem B. Bityutskiy 	size_t read;
118801c135cSArtem B. Bityutskiy 	loff_t addr;
119801c135cSArtem B. Bityutskiy 
120801c135cSArtem B. Bityutskiy 	dbg_io("read %d bytes from PEB %d:%d", len, pnum, offset);
121801c135cSArtem B. Bityutskiy 
122801c135cSArtem B. Bityutskiy 	ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
123801c135cSArtem B. Bityutskiy 	ubi_assert(offset >= 0 && offset + len <= ubi->peb_size);
124801c135cSArtem B. Bityutskiy 	ubi_assert(len > 0);
125801c135cSArtem B. Bityutskiy 
1268056eb4aSArtem Bityutskiy 	err = self_check_not_bad(ubi, pnum);
127801c135cSArtem B. Bityutskiy 	if (err)
128adbf05e3SArtem Bityutskiy 		return err;
129801c135cSArtem B. Bityutskiy 
130276832d8SArtem Bityutskiy 	/*
131276832d8SArtem Bityutskiy 	 * Deliberately corrupt the buffer to improve robustness. Indeed, if we
132276832d8SArtem Bityutskiy 	 * do not do this, the following may happen:
133276832d8SArtem Bityutskiy 	 * 1. The buffer contains data from previous operation, e.g., read from
134276832d8SArtem Bityutskiy 	 *    another PEB previously. The data looks like expected, e.g., if we
135276832d8SArtem Bityutskiy 	 *    just do not read anything and return - the caller would not
136276832d8SArtem Bityutskiy 	 *    notice this. E.g., if we are reading a VID header, the buffer may
137276832d8SArtem Bityutskiy 	 *    contain a valid VID header from another PEB.
138276832d8SArtem Bityutskiy 	 * 2. The driver is buggy and returns us success or -EBADMSG or
139276832d8SArtem Bityutskiy 	 *    -EUCLEAN, but it does not actually put any data to the buffer.
140276832d8SArtem Bityutskiy 	 *
141276832d8SArtem Bityutskiy 	 * This may confuse UBI or upper layers - they may think the buffer
142276832d8SArtem Bityutskiy 	 * contains valid data while in fact it is just old data. This is
143276832d8SArtem Bityutskiy 	 * especially possible because UBI (and UBIFS) relies on CRC, and
144276832d8SArtem Bityutskiy 	 * treats data as correct even in case of ECC errors if the CRC is
145276832d8SArtem Bityutskiy 	 * correct.
146276832d8SArtem Bityutskiy 	 *
147276832d8SArtem Bityutskiy 	 * Try to prevent this situation by changing the first byte of the
148276832d8SArtem Bityutskiy 	 * buffer.
149276832d8SArtem Bityutskiy 	 */
150276832d8SArtem Bityutskiy 	*((uint8_t *)buf) ^= 0xFF;
151276832d8SArtem Bityutskiy 
152801c135cSArtem B. Bityutskiy 	addr = (loff_t)pnum * ubi->peb_size + offset;
153801c135cSArtem B. Bityutskiy retry:
154329ad399SArtem Bityutskiy 	err = mtd_read(ubi->mtd, addr, len, &read, buf);
155801c135cSArtem B. Bityutskiy 	if (err) {
156d57f4054SBrian Norris 		const char *errstr = mtd_is_eccerr(err) ? " (ECC error)" : "";
1571a49af2cSArtem Bityutskiy 
158d57f4054SBrian Norris 		if (mtd_is_bitflip(err)) {
159801c135cSArtem B. Bityutskiy 			/*
160801c135cSArtem B. Bityutskiy 			 * -EUCLEAN is reported if there was a bit-flip which
161801c135cSArtem B. Bityutskiy 			 * was corrected, so this is harmless.
1628c1e6ee1SArtem Bityutskiy 			 *
1638c1e6ee1SArtem Bityutskiy 			 * We do not report about it here unless debugging is
1648c1e6ee1SArtem Bityutskiy 			 * enabled. A corresponding message will be printed
1658c1e6ee1SArtem Bityutskiy 			 * later, when it is has been scrubbed.
166801c135cSArtem B. Bityutskiy 			 */
16732608703STanya Brokhman 			ubi_msg(ubi, "fixable bit-flip detected at PEB %d",
16832608703STanya Brokhman 				pnum);
169801c135cSArtem B. Bityutskiy 			ubi_assert(len == read);
170801c135cSArtem B. Bityutskiy 			return UBI_IO_BITFLIPS;
171801c135cSArtem B. Bityutskiy 		}
172801c135cSArtem B. Bityutskiy 
173a87f29cbSArtem Bityutskiy 		if (retries++ < UBI_IO_RETRIES) {
17432608703STanya Brokhman 			ubi_warn(ubi, "error %d%s while reading %d bytes from PEB %d:%d, read only %zd bytes, retry",
1751a49af2cSArtem Bityutskiy 				 err, errstr, len, pnum, offset, read);
176801c135cSArtem B. Bityutskiy 			yield();
177801c135cSArtem B. Bityutskiy 			goto retry;
178801c135cSArtem B. Bityutskiy 		}
179801c135cSArtem B. Bityutskiy 
18032608703STanya Brokhman 		ubi_err(ubi, "error %d%s while reading %d bytes from PEB %d:%d, read %zd bytes",
181049333ceSArtem Bityutskiy 			err, errstr, len, pnum, offset, read);
18225886a36SArtem Bityutskiy 		dump_stack();
1832362a53eSArtem Bityutskiy 
1842362a53eSArtem Bityutskiy 		/*
1852362a53eSArtem Bityutskiy 		 * The driver should never return -EBADMSG if it failed to read
1862362a53eSArtem Bityutskiy 		 * all the requested data. But some buggy drivers might do
1872362a53eSArtem Bityutskiy 		 * this, so we change it to -EIO.
1882362a53eSArtem Bityutskiy 		 */
189d57f4054SBrian Norris 		if (read != len && mtd_is_eccerr(err)) {
1902362a53eSArtem Bityutskiy 			ubi_assert(0);
1912362a53eSArtem Bityutskiy 			err = -EIO;
1922362a53eSArtem Bityutskiy 		}
193801c135cSArtem B. Bityutskiy 	} else {
194801c135cSArtem B. Bityutskiy 		ubi_assert(len == read);
195801c135cSArtem B. Bityutskiy 
19627a0f2a3SArtem Bityutskiy 		if (ubi_dbg_is_bitflip(ubi)) {
197c8566350SArtem Bityutskiy 			dbg_gen("bit-flip (emulated)");
198801c135cSArtem B. Bityutskiy 			err = UBI_IO_BITFLIPS;
199801c135cSArtem B. Bityutskiy 		}
200801c135cSArtem B. Bityutskiy 	}
201801c135cSArtem B. Bityutskiy 
202801c135cSArtem B. Bityutskiy 	return err;
203801c135cSArtem B. Bityutskiy }
204801c135cSArtem B. Bityutskiy 
205801c135cSArtem B. Bityutskiy /**
206801c135cSArtem B. Bityutskiy  * ubi_io_write - write data to a physical eraseblock.
207801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
208801c135cSArtem B. Bityutskiy  * @buf: buffer with the data to write
209801c135cSArtem B. Bityutskiy  * @pnum: physical eraseblock number to write to
210801c135cSArtem B. Bityutskiy  * @offset: offset within the physical eraseblock where to write
211801c135cSArtem B. Bityutskiy  * @len: how many bytes to write
212801c135cSArtem B. Bityutskiy  *
213801c135cSArtem B. Bityutskiy  * This function writes @len bytes of data from buffer @buf to offset @offset
214801c135cSArtem B. Bityutskiy  * of physical eraseblock @pnum. If all the data were successfully written,
215801c135cSArtem B. Bityutskiy  * zero is returned. If an error occurred, this function returns a negative
216801c135cSArtem B. Bityutskiy  * error code. If %-EIO is returned, the physical eraseblock most probably went
217801c135cSArtem B. Bityutskiy  * bad.
218801c135cSArtem B. Bityutskiy  *
219801c135cSArtem B. Bityutskiy  * Note, in case of an error, it is possible that something was still written
220801c135cSArtem B. Bityutskiy  * to the flash media, but may be some garbage.
221801c135cSArtem B. Bityutskiy  */
ubi_io_write(struct ubi_device * ubi,const void * buf,int pnum,int offset,int len)222e88d6e10SArtem Bityutskiy int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
223e88d6e10SArtem Bityutskiy 		 int len)
224801c135cSArtem B. Bityutskiy {
225801c135cSArtem B. Bityutskiy 	int err;
226801c135cSArtem B. Bityutskiy 	size_t written;
227801c135cSArtem B. Bityutskiy 	loff_t addr;
228801c135cSArtem B. Bityutskiy 
229801c135cSArtem B. Bityutskiy 	dbg_io("write %d bytes to PEB %d:%d", len, pnum, offset);
230801c135cSArtem B. Bityutskiy 
231801c135cSArtem B. Bityutskiy 	ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
232801c135cSArtem B. Bityutskiy 	ubi_assert(offset >= 0 && offset + len <= ubi->peb_size);
233801c135cSArtem B. Bityutskiy 	ubi_assert(offset % ubi->hdrs_min_io_size == 0);
234801c135cSArtem B. Bityutskiy 	ubi_assert(len > 0 && len % ubi->hdrs_min_io_size == 0);
235801c135cSArtem B. Bityutskiy 
236801c135cSArtem B. Bityutskiy 	if (ubi->ro_mode) {
23732608703STanya Brokhman 		ubi_err(ubi, "read-only mode");
238801c135cSArtem B. Bityutskiy 		return -EROFS;
239801c135cSArtem B. Bityutskiy 	}
240801c135cSArtem B. Bityutskiy 
2418056eb4aSArtem Bityutskiy 	err = self_check_not_bad(ubi, pnum);
242801c135cSArtem B. Bityutskiy 	if (err)
243adbf05e3SArtem Bityutskiy 		return err;
244801c135cSArtem B. Bityutskiy 
245801c135cSArtem B. Bityutskiy 	/* The area we are writing to has to contain all 0xFF bytes */
24697d6104bSArtem Bityutskiy 	err = ubi_self_check_all_ff(ubi, pnum, offset, len);
247801c135cSArtem B. Bityutskiy 	if (err)
248adbf05e3SArtem Bityutskiy 		return err;
249801c135cSArtem B. Bityutskiy 
250801c135cSArtem B. Bityutskiy 	if (offset >= ubi->leb_start) {
251801c135cSArtem B. Bityutskiy 		/*
252801c135cSArtem B. Bityutskiy 		 * We write to the data area of the physical eraseblock. Make
253801c135cSArtem B. Bityutskiy 		 * sure it has valid EC and VID headers.
254801c135cSArtem B. Bityutskiy 		 */
2558056eb4aSArtem Bityutskiy 		err = self_check_peb_ec_hdr(ubi, pnum);
256801c135cSArtem B. Bityutskiy 		if (err)
257adbf05e3SArtem Bityutskiy 			return err;
2588056eb4aSArtem Bityutskiy 		err = self_check_peb_vid_hdr(ubi, pnum);
259801c135cSArtem B. Bityutskiy 		if (err)
260adbf05e3SArtem Bityutskiy 			return err;
261801c135cSArtem B. Bityutskiy 	}
262801c135cSArtem B. Bityutskiy 
26327a0f2a3SArtem Bityutskiy 	if (ubi_dbg_is_write_failure(ubi)) {
26432608703STanya Brokhman 		ubi_err(ubi, "cannot write %d bytes to PEB %d:%d (emulated)",
265049333ceSArtem Bityutskiy 			len, pnum, offset);
26625886a36SArtem Bityutskiy 		dump_stack();
267801c135cSArtem B. Bityutskiy 		return -EIO;
268801c135cSArtem B. Bityutskiy 	}
269801c135cSArtem B. Bityutskiy 
270801c135cSArtem B. Bityutskiy 	addr = (loff_t)pnum * ubi->peb_size + offset;
271eda95cbfSArtem Bityutskiy 	err = mtd_write(ubi->mtd, addr, len, &written, buf);
272801c135cSArtem B. Bityutskiy 	if (err) {
27332608703STanya Brokhman 		ubi_err(ubi, "error %d while writing %d bytes to PEB %d:%d, written %zd bytes",
274049333ceSArtem Bityutskiy 			err, len, pnum, offset, written);
27525886a36SArtem Bityutskiy 		dump_stack();
276ef7088e7SArtem Bityutskiy 		ubi_dump_flash(ubi, pnum, offset, len);
277801c135cSArtem B. Bityutskiy 	} else
278801c135cSArtem B. Bityutskiy 		ubi_assert(written == len);
279801c135cSArtem B. Bityutskiy 
2806e9065d7SArtem Bityutskiy 	if (!err) {
28197d6104bSArtem Bityutskiy 		err = self_check_write(ubi, buf, pnum, offset, len);
2826e9065d7SArtem Bityutskiy 		if (err)
2836e9065d7SArtem Bityutskiy 			return err;
2846e9065d7SArtem Bityutskiy 
2856e9065d7SArtem Bityutskiy 		/*
2866e9065d7SArtem Bityutskiy 		 * Since we always write sequentially, the rest of the PEB has
2876e9065d7SArtem Bityutskiy 		 * to contain only 0xFF bytes.
2886e9065d7SArtem Bityutskiy 		 */
2896e9065d7SArtem Bityutskiy 		offset += len;
2906e9065d7SArtem Bityutskiy 		len = ubi->peb_size - offset;
2916e9065d7SArtem Bityutskiy 		if (len)
29297d6104bSArtem Bityutskiy 			err = ubi_self_check_all_ff(ubi, pnum, offset, len);
2936e9065d7SArtem Bityutskiy 	}
2946e9065d7SArtem Bityutskiy 
295801c135cSArtem B. Bityutskiy 	return err;
296801c135cSArtem B. Bityutskiy }
297801c135cSArtem B. Bityutskiy 
298801c135cSArtem B. Bityutskiy /**
299801c135cSArtem B. Bityutskiy  * do_sync_erase - synchronously erase a physical eraseblock.
300801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
301801c135cSArtem B. Bityutskiy  * @pnum: the physical eraseblock number to erase
302801c135cSArtem B. Bityutskiy  *
303801c135cSArtem B. Bityutskiy  * This function synchronously erases physical eraseblock @pnum and returns
304801c135cSArtem B. Bityutskiy  * zero in case of success and a negative error code in case of failure. If
305801c135cSArtem B. Bityutskiy  * %-EIO is returned, the physical eraseblock most probably went bad.
306801c135cSArtem B. Bityutskiy  */
do_sync_erase(struct ubi_device * ubi,int pnum)307e88d6e10SArtem Bityutskiy static int do_sync_erase(struct ubi_device *ubi, int pnum)
308801c135cSArtem B. Bityutskiy {
309801c135cSArtem B. Bityutskiy 	int err, retries = 0;
310801c135cSArtem B. Bityutskiy 	struct erase_info ei;
311801c135cSArtem B. Bityutskiy 
312801c135cSArtem B. Bityutskiy 	dbg_io("erase PEB %d", pnum);
3133efe5090SArtem Bityutskiy 	ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
3143efe5090SArtem Bityutskiy 
3153efe5090SArtem Bityutskiy 	if (ubi->ro_mode) {
31632608703STanya Brokhman 		ubi_err(ubi, "read-only mode");
3173efe5090SArtem Bityutskiy 		return -EROFS;
3183efe5090SArtem Bityutskiy 	}
319801c135cSArtem B. Bityutskiy 
320801c135cSArtem B. Bityutskiy retry:
321801c135cSArtem B. Bityutskiy 	memset(&ei, 0, sizeof(struct erase_info));
322801c135cSArtem B. Bityutskiy 
3232f176f79SBrijesh Singh 	ei.addr     = (loff_t)pnum * ubi->peb_size;
324801c135cSArtem B. Bityutskiy 	ei.len      = ubi->peb_size;
325801c135cSArtem B. Bityutskiy 
3267e1f0dc0SArtem Bityutskiy 	err = mtd_erase(ubi->mtd, &ei);
327801c135cSArtem B. Bityutskiy 	if (err) {
328801c135cSArtem B. Bityutskiy 		if (retries++ < UBI_IO_RETRIES) {
32932608703STanya Brokhman 			ubi_warn(ubi, "error %d while erasing PEB %d, retry",
330801c135cSArtem B. Bityutskiy 				 err, pnum);
331801c135cSArtem B. Bityutskiy 			yield();
332801c135cSArtem B. Bityutskiy 			goto retry;
333801c135cSArtem B. Bityutskiy 		}
33432608703STanya Brokhman 		ubi_err(ubi, "cannot erase PEB %d, error %d", pnum, err);
33525886a36SArtem Bityutskiy 		dump_stack();
336801c135cSArtem B. Bityutskiy 		return err;
337801c135cSArtem B. Bityutskiy 	}
338801c135cSArtem B. Bityutskiy 
33997d6104bSArtem Bityutskiy 	err = ubi_self_check_all_ff(ubi, pnum, 0, ubi->peb_size);
340801c135cSArtem B. Bityutskiy 	if (err)
341adbf05e3SArtem Bityutskiy 		return err;
342801c135cSArtem B. Bityutskiy 
34327a0f2a3SArtem Bityutskiy 	if (ubi_dbg_is_erase_failure(ubi)) {
34432608703STanya Brokhman 		ubi_err(ubi, "cannot erase PEB %d (emulated)", pnum);
345801c135cSArtem B. Bityutskiy 		return -EIO;
346801c135cSArtem B. Bityutskiy 	}
347801c135cSArtem B. Bityutskiy 
348801c135cSArtem B. Bityutskiy 	return 0;
349801c135cSArtem B. Bityutskiy }
350801c135cSArtem B. Bityutskiy 
351801c135cSArtem B. Bityutskiy /* Patterns to write to a physical eraseblock when torturing it */
352801c135cSArtem B. Bityutskiy static uint8_t patterns[] = {0xa5, 0x5a, 0x0};
353801c135cSArtem B. Bityutskiy 
354801c135cSArtem B. Bityutskiy /**
355801c135cSArtem B. Bityutskiy  * torture_peb - test a supposedly bad physical eraseblock.
356801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
357801c135cSArtem B. Bityutskiy  * @pnum: the physical eraseblock number to test
358801c135cSArtem B. Bityutskiy  *
359801c135cSArtem B. Bityutskiy  * This function returns %-EIO if the physical eraseblock did not pass the
360801c135cSArtem B. Bityutskiy  * test, a positive number of erase operations done if the test was
361801c135cSArtem B. Bityutskiy  * successfully passed, and other negative error codes in case of other errors.
362801c135cSArtem B. Bityutskiy  */
torture_peb(struct ubi_device * ubi,int pnum)363e88d6e10SArtem Bityutskiy static int torture_peb(struct ubi_device *ubi, int pnum)
364801c135cSArtem B. Bityutskiy {
365801c135cSArtem B. Bityutskiy 	int err, i, patt_count;
366801c135cSArtem B. Bityutskiy 
36732608703STanya Brokhman 	ubi_msg(ubi, "run torture test for PEB %d", pnum);
368801c135cSArtem B. Bityutskiy 	patt_count = ARRAY_SIZE(patterns);
369801c135cSArtem B. Bityutskiy 	ubi_assert(patt_count > 0);
370801c135cSArtem B. Bityutskiy 
371e88d6e10SArtem Bityutskiy 	mutex_lock(&ubi->buf_mutex);
372801c135cSArtem B. Bityutskiy 	for (i = 0; i < patt_count; i++) {
373801c135cSArtem B. Bityutskiy 		err = do_sync_erase(ubi, pnum);
374801c135cSArtem B. Bityutskiy 		if (err)
375801c135cSArtem B. Bityutskiy 			goto out;
376801c135cSArtem B. Bityutskiy 
377801c135cSArtem B. Bityutskiy 		/* Make sure the PEB contains only 0xFF bytes */
3780ca39d74SArtem Bityutskiy 		err = ubi_io_read(ubi, ubi->peb_buf, pnum, 0, ubi->peb_size);
379801c135cSArtem B. Bityutskiy 		if (err)
380801c135cSArtem B. Bityutskiy 			goto out;
381801c135cSArtem B. Bityutskiy 
3820ca39d74SArtem Bityutskiy 		err = ubi_check_pattern(ubi->peb_buf, 0xFF, ubi->peb_size);
383801c135cSArtem B. Bityutskiy 		if (err == 0) {
38432608703STanya Brokhman 			ubi_err(ubi, "erased PEB %d, but a non-0xFF byte found",
385801c135cSArtem B. Bityutskiy 				pnum);
386801c135cSArtem B. Bityutskiy 			err = -EIO;
387801c135cSArtem B. Bityutskiy 			goto out;
388801c135cSArtem B. Bityutskiy 		}
389801c135cSArtem B. Bityutskiy 
390801c135cSArtem B. Bityutskiy 		/* Write a pattern and check it */
3910ca39d74SArtem Bityutskiy 		memset(ubi->peb_buf, patterns[i], ubi->peb_size);
3920ca39d74SArtem Bityutskiy 		err = ubi_io_write(ubi, ubi->peb_buf, pnum, 0, ubi->peb_size);
393801c135cSArtem B. Bityutskiy 		if (err)
394801c135cSArtem B. Bityutskiy 			goto out;
395801c135cSArtem B. Bityutskiy 
3960ca39d74SArtem Bityutskiy 		memset(ubi->peb_buf, ~patterns[i], ubi->peb_size);
3970ca39d74SArtem Bityutskiy 		err = ubi_io_read(ubi, ubi->peb_buf, pnum, 0, ubi->peb_size);
398801c135cSArtem B. Bityutskiy 		if (err)
399801c135cSArtem B. Bityutskiy 			goto out;
400801c135cSArtem B. Bityutskiy 
4010ca39d74SArtem Bityutskiy 		err = ubi_check_pattern(ubi->peb_buf, patterns[i],
402bb00e180SArtem Bityutskiy 					ubi->peb_size);
403801c135cSArtem B. Bityutskiy 		if (err == 0) {
40432608703STanya Brokhman 			ubi_err(ubi, "pattern %x checking failed for PEB %d",
405801c135cSArtem B. Bityutskiy 				patterns[i], pnum);
406801c135cSArtem B. Bityutskiy 			err = -EIO;
407801c135cSArtem B. Bityutskiy 			goto out;
408801c135cSArtem B. Bityutskiy 		}
409801c135cSArtem B. Bityutskiy 	}
410801c135cSArtem B. Bityutskiy 
411801c135cSArtem B. Bityutskiy 	err = patt_count;
41232608703STanya Brokhman 	ubi_msg(ubi, "PEB %d passed torture test, do not mark it as bad", pnum);
413801c135cSArtem B. Bityutskiy 
414801c135cSArtem B. Bityutskiy out:
415e88d6e10SArtem Bityutskiy 	mutex_unlock(&ubi->buf_mutex);
416d57f4054SBrian Norris 	if (err == UBI_IO_BITFLIPS || mtd_is_eccerr(err)) {
417801c135cSArtem B. Bityutskiy 		/*
418801c135cSArtem B. Bityutskiy 		 * If a bit-flip or data integrity error was detected, the test
419801c135cSArtem B. Bityutskiy 		 * has not passed because it happened on a freshly erased
420801c135cSArtem B. Bityutskiy 		 * physical eraseblock which means something is wrong with it.
421801c135cSArtem B. Bityutskiy 		 */
42232608703STanya Brokhman 		ubi_err(ubi, "read problems on freshly erased PEB %d, must be bad",
4238d2d4011SArtem Bityutskiy 			pnum);
424801c135cSArtem B. Bityutskiy 		err = -EIO;
4258d2d4011SArtem Bityutskiy 	}
426801c135cSArtem B. Bityutskiy 	return err;
427801c135cSArtem B. Bityutskiy }
428801c135cSArtem B. Bityutskiy 
429801c135cSArtem B. Bityutskiy /**
430ebf53f42SArtem Bityutskiy  * nor_erase_prepare - prepare a NOR flash PEB for erasure.
431ebf53f42SArtem Bityutskiy  * @ubi: UBI device description object
432ebf53f42SArtem Bityutskiy  * @pnum: physical eraseblock number to prepare
433ebf53f42SArtem Bityutskiy  *
434ebf53f42SArtem Bityutskiy  * NOR flash, or at least some of them, have peculiar embedded PEB erasure
435ebf53f42SArtem Bityutskiy  * algorithm: the PEB is first filled with zeroes, then it is erased. And
436ebf53f42SArtem Bityutskiy  * filling with zeroes starts from the end of the PEB. This was observed with
437ebf53f42SArtem Bityutskiy  * Spansion S29GL512N NOR flash.
438ebf53f42SArtem Bityutskiy  *
439ebf53f42SArtem Bityutskiy  * This means that in case of a power cut we may end up with intact data at the
440ebf53f42SArtem Bityutskiy  * beginning of the PEB, and all zeroes at the end of PEB. In other words, the
441ebf53f42SArtem Bityutskiy  * EC and VID headers are OK, but a large chunk of data at the end of PEB is
442ebf53f42SArtem Bityutskiy  * zeroed. This makes UBI mistakenly treat this PEB as used and associate it
443ebf53f42SArtem Bityutskiy  * with an LEB, which leads to subsequent failures (e.g., UBIFS fails).
444ebf53f42SArtem Bityutskiy  *
445ebf53f42SArtem Bityutskiy  * This function is called before erasing NOR PEBs and it zeroes out EC and VID
446ebf53f42SArtem Bityutskiy  * magic numbers in order to invalidate them and prevent the failures. Returns
447ebf53f42SArtem Bityutskiy  * zero in case of success and a negative error code in case of failure.
448ebf53f42SArtem Bityutskiy  */
nor_erase_prepare(struct ubi_device * ubi,int pnum)449ebf53f42SArtem Bityutskiy static int nor_erase_prepare(struct ubi_device *ubi, int pnum)
450ebf53f42SArtem Bityutskiy {
4512c7ca5ccSQi Wang 王起 (qiwang) 	int err;
452ebf53f42SArtem Bityutskiy 	size_t written;
453ebf53f42SArtem Bityutskiy 	loff_t addr;
454ebf53f42SArtem Bityutskiy 	uint32_t data = 0;
4552c7ca5ccSQi Wang 王起 (qiwang) 	struct ubi_ec_hdr ec_hdr;
4563291b52fSBoris Brezillon 	struct ubi_vid_io_buf vidb;
4572c7ca5ccSQi Wang 王起 (qiwang) 
4582fff570eSArtem Bityutskiy 	/*
4592fff570eSArtem Bityutskiy 	 * Note, we cannot generally define VID header buffers on stack,
4602fff570eSArtem Bityutskiy 	 * because of the way we deal with these buffers (see the header
4612fff570eSArtem Bityutskiy 	 * comment in this file). But we know this is a NOR-specific piece of
4622fff570eSArtem Bityutskiy 	 * code, so we can do this. But yes, this is error-prone and we should
4632fff570eSArtem Bityutskiy 	 * (pre-)allocate VID header buffer instead.
4642fff570eSArtem Bityutskiy 	 */
465de75c771SArtem Bityutskiy 	struct ubi_vid_hdr vid_hdr;
466ebf53f42SArtem Bityutskiy 
4677ac760c2SArtem Bityutskiy 	/*
4682c7ca5ccSQi Wang 王起 (qiwang) 	 * If VID or EC is valid, we have to corrupt them before erasing.
4697ac760c2SArtem Bityutskiy 	 * It is important to first invalidate the EC header, and then the VID
4707ac760c2SArtem Bityutskiy 	 * header. Otherwise a power cut may lead to valid EC header and
4717ac760c2SArtem Bityutskiy 	 * invalid VID header, in which case UBI will treat this PEB as
472fbd0107fSArtem Bityutskiy 	 * corrupted and will try to preserve it, and print scary warnings.
4737ac760c2SArtem Bityutskiy 	 */
4747ac760c2SArtem Bityutskiy 	addr = (loff_t)pnum * ubi->peb_size;
4752c7ca5ccSQi Wang 王起 (qiwang) 	err = ubi_io_read_ec_hdr(ubi, pnum, &ec_hdr, 0);
4762c7ca5ccSQi Wang 王起 (qiwang) 	if (err != UBI_IO_BAD_HDR_EBADMSG && err != UBI_IO_BAD_HDR &&
4772c7ca5ccSQi Wang 王起 (qiwang) 	    err != UBI_IO_FF){
478eda95cbfSArtem Bityutskiy 		err = mtd_write(ubi->mtd, addr, 4, &written, (void *)&data);
4792c7ca5ccSQi Wang 王起 (qiwang) 		if(err)
4802c7ca5ccSQi Wang 王起 (qiwang) 			goto error;
4812c7ca5ccSQi Wang 王起 (qiwang) 	}
4822c7ca5ccSQi Wang 王起 (qiwang) 
4833291b52fSBoris Brezillon 	ubi_init_vid_buf(ubi, &vidb, &vid_hdr);
4843291b52fSBoris Brezillon 	ubi_assert(&vid_hdr == ubi_get_vid_hdr(&vidb));
4853291b52fSBoris Brezillon 
4863291b52fSBoris Brezillon 	err = ubi_io_read_vid_hdr(ubi, pnum, &vidb, 0);
4872c7ca5ccSQi Wang 王起 (qiwang) 	if (err != UBI_IO_BAD_HDR_EBADMSG && err != UBI_IO_BAD_HDR &&
4882c7ca5ccSQi Wang 王起 (qiwang) 	    err != UBI_IO_FF){
4897ac760c2SArtem Bityutskiy 		addr += ubi->vid_hdr_aloffset;
490eda95cbfSArtem Bityutskiy 		err = mtd_write(ubi->mtd, addr, 4, &written, (void *)&data);
4912c7ca5ccSQi Wang 王起 (qiwang) 		if (err)
4922c7ca5ccSQi Wang 王起 (qiwang) 			goto error;
4935b289b56SArtem Bityutskiy 	}
494ebf53f42SArtem Bityutskiy 	return 0;
495de75c771SArtem Bityutskiy 
4962c7ca5ccSQi Wang 王起 (qiwang) error:
497de75c771SArtem Bityutskiy 	/*
4982c7ca5ccSQi Wang 王起 (qiwang) 	 * The PEB contains a valid VID or EC header, but we cannot invalidate
4992c7ca5ccSQi Wang 王起 (qiwang) 	 * it. Supposedly the flash media or the driver is screwed up, so
5002c7ca5ccSQi Wang 王起 (qiwang) 	 * return an error.
501de75c771SArtem Bityutskiy 	 */
50232608703STanya Brokhman 	ubi_err(ubi, "cannot invalidate PEB %d, write returned %d", pnum, err);
503ef7088e7SArtem Bityutskiy 	ubi_dump_flash(ubi, pnum, 0, ubi->peb_size);
504de75c771SArtem Bityutskiy 	return -EIO;
505ebf53f42SArtem Bityutskiy }
506ebf53f42SArtem Bityutskiy 
507ebf53f42SArtem Bityutskiy /**
508801c135cSArtem B. Bityutskiy  * ubi_io_sync_erase - synchronously erase a physical eraseblock.
509801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
510801c135cSArtem B. Bityutskiy  * @pnum: physical eraseblock number to erase
511801c135cSArtem B. Bityutskiy  * @torture: if this physical eraseblock has to be tortured
512801c135cSArtem B. Bityutskiy  *
513801c135cSArtem B. Bityutskiy  * This function synchronously erases physical eraseblock @pnum. If @torture
514801c135cSArtem B. Bityutskiy  * flag is not zero, the physical eraseblock is checked by means of writing
515801c135cSArtem B. Bityutskiy  * different patterns to it and reading them back. If the torturing is enabled,
516025dfdafSFrederik Schwarzer  * the physical eraseblock is erased more than once.
517801c135cSArtem B. Bityutskiy  *
518801c135cSArtem B. Bityutskiy  * This function returns the number of erasures made in case of success, %-EIO
519801c135cSArtem B. Bityutskiy  * if the erasure failed or the torturing test failed, and other negative error
520801c135cSArtem B. Bityutskiy  * codes in case of other errors. Note, %-EIO means that the physical
521801c135cSArtem B. Bityutskiy  * eraseblock is bad.
522801c135cSArtem B. Bityutskiy  */
ubi_io_sync_erase(struct ubi_device * ubi,int pnum,int torture)523e88d6e10SArtem Bityutskiy int ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture)
524801c135cSArtem B. Bityutskiy {
525801c135cSArtem B. Bityutskiy 	int err, ret = 0;
526801c135cSArtem B. Bityutskiy 
527801c135cSArtem B. Bityutskiy 	ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
528801c135cSArtem B. Bityutskiy 
5298056eb4aSArtem Bityutskiy 	err = self_check_not_bad(ubi, pnum);
530801c135cSArtem B. Bityutskiy 	if (err != 0)
531adbf05e3SArtem Bityutskiy 		return err;
532801c135cSArtem B. Bityutskiy 
533801c135cSArtem B. Bityutskiy 	if (ubi->ro_mode) {
53432608703STanya Brokhman 		ubi_err(ubi, "read-only mode");
535801c135cSArtem B. Bityutskiy 		return -EROFS;
536801c135cSArtem B. Bityutskiy 	}
537801c135cSArtem B. Bityutskiy 
538f669e74bSPratyush Yadav 	/*
539f669e74bSPratyush Yadav 	 * If the flash is ECC-ed then we have to erase the ECC block before we
540f669e74bSPratyush Yadav 	 * can write to it. But the write is in preparation to an erase in the
541f669e74bSPratyush Yadav 	 * first place. This means we cannot zero out EC and VID before the
542f669e74bSPratyush Yadav 	 * erase and we just have to hope the flash starts erasing from the
543f669e74bSPratyush Yadav 	 * start of the page.
544f669e74bSPratyush Yadav 	 */
545f669e74bSPratyush Yadav 	if (ubi->nor_flash && ubi->mtd->writesize == 1) {
546ebf53f42SArtem Bityutskiy 		err = nor_erase_prepare(ubi, pnum);
547ebf53f42SArtem Bityutskiy 		if (err)
548ebf53f42SArtem Bityutskiy 			return err;
549ebf53f42SArtem Bityutskiy 	}
550ebf53f42SArtem Bityutskiy 
551801c135cSArtem B. Bityutskiy 	if (torture) {
552801c135cSArtem B. Bityutskiy 		ret = torture_peb(ubi, pnum);
553801c135cSArtem B. Bityutskiy 		if (ret < 0)
554801c135cSArtem B. Bityutskiy 			return ret;
555801c135cSArtem B. Bityutskiy 	}
556801c135cSArtem B. Bityutskiy 
557801c135cSArtem B. Bityutskiy 	err = do_sync_erase(ubi, pnum);
558801c135cSArtem B. Bityutskiy 	if (err)
559801c135cSArtem B. Bityutskiy 		return err;
560801c135cSArtem B. Bityutskiy 
561801c135cSArtem B. Bityutskiy 	return ret + 1;
562801c135cSArtem B. Bityutskiy }
563801c135cSArtem B. Bityutskiy 
564801c135cSArtem B. Bityutskiy /**
565801c135cSArtem B. Bityutskiy  * ubi_io_is_bad - check if a physical eraseblock is bad.
566801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
567801c135cSArtem B. Bityutskiy  * @pnum: the physical eraseblock number to check
568801c135cSArtem B. Bityutskiy  *
569801c135cSArtem B. Bityutskiy  * This function returns a positive number if the physical eraseblock is bad,
570801c135cSArtem B. Bityutskiy  * zero if not, and a negative error code if an error occurred.
571801c135cSArtem B. Bityutskiy  */
ubi_io_is_bad(const struct ubi_device * ubi,int pnum)572801c135cSArtem B. Bityutskiy int ubi_io_is_bad(const struct ubi_device *ubi, int pnum)
573801c135cSArtem B. Bityutskiy {
574801c135cSArtem B. Bityutskiy 	struct mtd_info *mtd = ubi->mtd;
575801c135cSArtem B. Bityutskiy 
576801c135cSArtem B. Bityutskiy 	ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
577801c135cSArtem B. Bityutskiy 
578801c135cSArtem B. Bityutskiy 	if (ubi->bad_allowed) {
579801c135cSArtem B. Bityutskiy 		int ret;
580801c135cSArtem B. Bityutskiy 
5817086c19dSArtem Bityutskiy 		ret = mtd_block_isbad(mtd, (loff_t)pnum * ubi->peb_size);
582801c135cSArtem B. Bityutskiy 		if (ret < 0)
58332608703STanya Brokhman 			ubi_err(ubi, "error %d while checking if PEB %d is bad",
584801c135cSArtem B. Bityutskiy 				ret, pnum);
585801c135cSArtem B. Bityutskiy 		else if (ret)
586801c135cSArtem B. Bityutskiy 			dbg_io("PEB %d is bad", pnum);
587801c135cSArtem B. Bityutskiy 		return ret;
588801c135cSArtem B. Bityutskiy 	}
589801c135cSArtem B. Bityutskiy 
590801c135cSArtem B. Bityutskiy 	return 0;
591801c135cSArtem B. Bityutskiy }
592801c135cSArtem B. Bityutskiy 
593801c135cSArtem B. Bityutskiy /**
594801c135cSArtem B. Bityutskiy  * ubi_io_mark_bad - mark a physical eraseblock as bad.
595801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
596801c135cSArtem B. Bityutskiy  * @pnum: the physical eraseblock number to mark
597801c135cSArtem B. Bityutskiy  *
598801c135cSArtem B. Bityutskiy  * This function returns zero in case of success and a negative error code in
599801c135cSArtem B. Bityutskiy  * case of failure.
600801c135cSArtem B. Bityutskiy  */
ubi_io_mark_bad(const struct ubi_device * ubi,int pnum)601801c135cSArtem B. Bityutskiy int ubi_io_mark_bad(const struct ubi_device *ubi, int pnum)
602801c135cSArtem B. Bityutskiy {
603801c135cSArtem B. Bityutskiy 	int err;
604801c135cSArtem B. Bityutskiy 	struct mtd_info *mtd = ubi->mtd;
605801c135cSArtem B. Bityutskiy 
606801c135cSArtem B. Bityutskiy 	ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
607801c135cSArtem B. Bityutskiy 
608801c135cSArtem B. Bityutskiy 	if (ubi->ro_mode) {
60932608703STanya Brokhman 		ubi_err(ubi, "read-only mode");
610801c135cSArtem B. Bityutskiy 		return -EROFS;
611801c135cSArtem B. Bityutskiy 	}
612801c135cSArtem B. Bityutskiy 
613801c135cSArtem B. Bityutskiy 	if (!ubi->bad_allowed)
614801c135cSArtem B. Bityutskiy 		return 0;
615801c135cSArtem B. Bityutskiy 
6165942ddbcSArtem Bityutskiy 	err = mtd_block_markbad(mtd, (loff_t)pnum * ubi->peb_size);
617801c135cSArtem B. Bityutskiy 	if (err)
61832608703STanya Brokhman 		ubi_err(ubi, "cannot mark PEB %d bad, error %d", pnum, err);
619801c135cSArtem B. Bityutskiy 	return err;
620801c135cSArtem B. Bityutskiy }
621801c135cSArtem B. Bityutskiy 
622801c135cSArtem B. Bityutskiy /**
623801c135cSArtem B. Bityutskiy  * validate_ec_hdr - validate an erase counter header.
624801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
625801c135cSArtem B. Bityutskiy  * @ec_hdr: the erase counter header to check
626801c135cSArtem B. Bityutskiy  *
627801c135cSArtem B. Bityutskiy  * This function returns zero if the erase counter header is OK, and %1 if
628801c135cSArtem B. Bityutskiy  * not.
629801c135cSArtem B. Bityutskiy  */
validate_ec_hdr(const struct ubi_device * ubi,const struct ubi_ec_hdr * ec_hdr)630fe96efc1SArtem Bityutskiy static int validate_ec_hdr(const struct ubi_device *ubi,
631801c135cSArtem B. Bityutskiy 			   const struct ubi_ec_hdr *ec_hdr)
632801c135cSArtem B. Bityutskiy {
633801c135cSArtem B. Bityutskiy 	long long ec;
634fe96efc1SArtem Bityutskiy 	int vid_hdr_offset, leb_start;
635801c135cSArtem B. Bityutskiy 
6363261ebd7SChristoph Hellwig 	ec = be64_to_cpu(ec_hdr->ec);
6373261ebd7SChristoph Hellwig 	vid_hdr_offset = be32_to_cpu(ec_hdr->vid_hdr_offset);
6383261ebd7SChristoph Hellwig 	leb_start = be32_to_cpu(ec_hdr->data_offset);
639801c135cSArtem B. Bityutskiy 
640801c135cSArtem B. Bityutskiy 	if (ec_hdr->version != UBI_VERSION) {
64132608703STanya Brokhman 		ubi_err(ubi, "node with incompatible UBI version found: this UBI version is %d, image version is %d",
642801c135cSArtem B. Bityutskiy 			UBI_VERSION, (int)ec_hdr->version);
643801c135cSArtem B. Bityutskiy 		goto bad;
644801c135cSArtem B. Bityutskiy 	}
645801c135cSArtem B. Bityutskiy 
646801c135cSArtem B. Bityutskiy 	if (vid_hdr_offset != ubi->vid_hdr_offset) {
64732608703STanya Brokhman 		ubi_err(ubi, "bad VID header offset %d, expected %d",
648801c135cSArtem B. Bityutskiy 			vid_hdr_offset, ubi->vid_hdr_offset);
649801c135cSArtem B. Bityutskiy 		goto bad;
650801c135cSArtem B. Bityutskiy 	}
651801c135cSArtem B. Bityutskiy 
652801c135cSArtem B. Bityutskiy 	if (leb_start != ubi->leb_start) {
65332608703STanya Brokhman 		ubi_err(ubi, "bad data offset %d, expected %d",
654801c135cSArtem B. Bityutskiy 			leb_start, ubi->leb_start);
655801c135cSArtem B. Bityutskiy 		goto bad;
656801c135cSArtem B. Bityutskiy 	}
657801c135cSArtem B. Bityutskiy 
658801c135cSArtem B. Bityutskiy 	if (ec < 0 || ec > UBI_MAX_ERASECOUNTER) {
65932608703STanya Brokhman 		ubi_err(ubi, "bad erase counter %lld", ec);
660801c135cSArtem B. Bityutskiy 		goto bad;
661801c135cSArtem B. Bityutskiy 	}
662801c135cSArtem B. Bityutskiy 
663801c135cSArtem B. Bityutskiy 	return 0;
664801c135cSArtem B. Bityutskiy 
665801c135cSArtem B. Bityutskiy bad:
66632608703STanya Brokhman 	ubi_err(ubi, "bad EC header");
667a904e3f1SArtem Bityutskiy 	ubi_dump_ec_hdr(ec_hdr);
66825886a36SArtem Bityutskiy 	dump_stack();
669801c135cSArtem B. Bityutskiy 	return 1;
670801c135cSArtem B. Bityutskiy }
671801c135cSArtem B. Bityutskiy 
672801c135cSArtem B. Bityutskiy /**
673801c135cSArtem B. Bityutskiy  * ubi_io_read_ec_hdr - read and check an erase counter header.
674801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
675801c135cSArtem B. Bityutskiy  * @pnum: physical eraseblock to read from
676801c135cSArtem B. Bityutskiy  * @ec_hdr: a &struct ubi_ec_hdr object where to store the read erase counter
677801c135cSArtem B. Bityutskiy  * header
678801c135cSArtem B. Bityutskiy  * @verbose: be verbose if the header is corrupted or was not found
679801c135cSArtem B. Bityutskiy  *
680801c135cSArtem B. Bityutskiy  * This function reads erase counter header from physical eraseblock @pnum and
681801c135cSArtem B. Bityutskiy  * stores it in @ec_hdr. This function also checks CRC checksum of the read
682801c135cSArtem B. Bityutskiy  * erase counter header. The following codes may be returned:
683801c135cSArtem B. Bityutskiy  *
684801c135cSArtem B. Bityutskiy  * o %0 if the CRC checksum is correct and the header was successfully read;
685801c135cSArtem B. Bityutskiy  * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected
686801c135cSArtem B. Bityutskiy  *   and corrected by the flash driver; this is harmless but may indicate that
687801c135cSArtem B. Bityutskiy  *   this eraseblock may become bad soon (but may be not);
688786d7831SArtem Bityutskiy  * o %UBI_IO_BAD_HDR if the erase counter header is corrupted (a CRC error);
689756e1df1SArtem Bityutskiy  * o %UBI_IO_BAD_HDR_EBADMSG is the same as %UBI_IO_BAD_HDR, but there also was
690756e1df1SArtem Bityutskiy  *   a data integrity error (uncorrectable ECC error in case of NAND);
69174d82d26SArtem Bityutskiy  * o %UBI_IO_FF if only 0xFF bytes were read (the PEB is supposedly empty)
692801c135cSArtem B. Bityutskiy  * o a negative error code in case of failure.
693801c135cSArtem B. Bityutskiy  */
ubi_io_read_ec_hdr(struct ubi_device * ubi,int pnum,struct ubi_ec_hdr * ec_hdr,int verbose)694e88d6e10SArtem Bityutskiy int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
695801c135cSArtem B. Bityutskiy 		       struct ubi_ec_hdr *ec_hdr, int verbose)
696801c135cSArtem B. Bityutskiy {
69792e1a7d9SArtem Bityutskiy 	int err, read_err;
698801c135cSArtem B. Bityutskiy 	uint32_t crc, magic, hdr_crc;
699801c135cSArtem B. Bityutskiy 
700801c135cSArtem B. Bityutskiy 	dbg_io("read EC header from PEB %d", pnum);
701801c135cSArtem B. Bityutskiy 	ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
702801c135cSArtem B. Bityutskiy 
70392e1a7d9SArtem Bityutskiy 	read_err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE);
70492e1a7d9SArtem Bityutskiy 	if (read_err) {
705d57f4054SBrian Norris 		if (read_err != UBI_IO_BITFLIPS && !mtd_is_eccerr(read_err))
70692e1a7d9SArtem Bityutskiy 			return read_err;
707801c135cSArtem B. Bityutskiy 
708801c135cSArtem B. Bityutskiy 		/*
709801c135cSArtem B. Bityutskiy 		 * We read all the data, but either a correctable bit-flip
710756e1df1SArtem Bityutskiy 		 * occurred, or MTD reported a data integrity error
711756e1df1SArtem Bityutskiy 		 * (uncorrectable ECC error in case of NAND). The former is
712756e1df1SArtem Bityutskiy 		 * harmless, the later may mean that the read data is
713756e1df1SArtem Bityutskiy 		 * corrupted. But we have a CRC check-sum and we will detect
714756e1df1SArtem Bityutskiy 		 * this. If the EC header is still OK, we just report this as
715756e1df1SArtem Bityutskiy 		 * there was a bit-flip, to force scrubbing.
716801c135cSArtem B. Bityutskiy 		 */
717801c135cSArtem B. Bityutskiy 	}
718801c135cSArtem B. Bityutskiy 
7193261ebd7SChristoph Hellwig 	magic = be32_to_cpu(ec_hdr->magic);
720801c135cSArtem B. Bityutskiy 	if (magic != UBI_EC_HDR_MAGIC) {
721d57f4054SBrian Norris 		if (mtd_is_eccerr(read_err))
72292e1a7d9SArtem Bityutskiy 			return UBI_IO_BAD_HDR_EBADMSG;
723eb89580eSArtem Bityutskiy 
724801c135cSArtem B. Bityutskiy 		/*
725801c135cSArtem B. Bityutskiy 		 * The magic field is wrong. Let's check if we have read all
726801c135cSArtem B. Bityutskiy 		 * 0xFF. If yes, this physical eraseblock is assumed to be
727801c135cSArtem B. Bityutskiy 		 * empty.
728801c135cSArtem B. Bityutskiy 		 */
729bb00e180SArtem Bityutskiy 		if (ubi_check_pattern(ec_hdr, 0xFF, UBI_EC_HDR_SIZE)) {
730801c135cSArtem B. Bityutskiy 			/* The physical eraseblock is supposedly empty */
731801c135cSArtem B. Bityutskiy 			if (verbose)
73232608703STanya Brokhman 				ubi_warn(ubi, "no EC header found at PEB %d, only 0xFF bytes",
733049333ceSArtem Bityutskiy 					 pnum);
734049333ceSArtem Bityutskiy 			dbg_bld("no EC header found at PEB %d, only 0xFF bytes",
735049333ceSArtem Bityutskiy 				pnum);
73692e1a7d9SArtem Bityutskiy 			if (!read_err)
73774d82d26SArtem Bityutskiy 				return UBI_IO_FF;
73892e1a7d9SArtem Bityutskiy 			else
73992e1a7d9SArtem Bityutskiy 				return UBI_IO_FF_BITFLIPS;
740801c135cSArtem B. Bityutskiy 		}
741801c135cSArtem B. Bityutskiy 
742801c135cSArtem B. Bityutskiy 		/*
743801c135cSArtem B. Bityutskiy 		 * This is not a valid erase counter header, and these are not
744801c135cSArtem B. Bityutskiy 		 * 0xFF bytes. Report that the header is corrupted.
745801c135cSArtem B. Bityutskiy 		 */
746801c135cSArtem B. Bityutskiy 		if (verbose) {
74732608703STanya Brokhman 			ubi_warn(ubi, "bad magic number at PEB %d: %08x instead of %08x",
748049333ceSArtem Bityutskiy 				 pnum, magic, UBI_EC_HDR_MAGIC);
749a904e3f1SArtem Bityutskiy 			ubi_dump_ec_hdr(ec_hdr);
7506f9fdf62SArtem Bityutskiy 		}
751049333ceSArtem Bityutskiy 		dbg_bld("bad magic number at PEB %d: %08x instead of %08x",
752049333ceSArtem Bityutskiy 			pnum, magic, UBI_EC_HDR_MAGIC);
753786d7831SArtem Bityutskiy 		return UBI_IO_BAD_HDR;
754801c135cSArtem B. Bityutskiy 	}
755801c135cSArtem B. Bityutskiy 
756801c135cSArtem B. Bityutskiy 	crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
7573261ebd7SChristoph Hellwig 	hdr_crc = be32_to_cpu(ec_hdr->hdr_crc);
758801c135cSArtem B. Bityutskiy 
759801c135cSArtem B. Bityutskiy 	if (hdr_crc != crc) {
760801c135cSArtem B. Bityutskiy 		if (verbose) {
76132608703STanya Brokhman 			ubi_warn(ubi, "bad EC header CRC at PEB %d, calculated %#08x, read %#08x",
762049333ceSArtem Bityutskiy 				 pnum, crc, hdr_crc);
763a904e3f1SArtem Bityutskiy 			ubi_dump_ec_hdr(ec_hdr);
7646f9fdf62SArtem Bityutskiy 		}
765049333ceSArtem Bityutskiy 		dbg_bld("bad EC header CRC at PEB %d, calculated %#08x, read %#08x",
766049333ceSArtem Bityutskiy 			pnum, crc, hdr_crc);
76792e1a7d9SArtem Bityutskiy 
76892e1a7d9SArtem Bityutskiy 		if (!read_err)
76992e1a7d9SArtem Bityutskiy 			return UBI_IO_BAD_HDR;
77092e1a7d9SArtem Bityutskiy 		else
77192e1a7d9SArtem Bityutskiy 			return UBI_IO_BAD_HDR_EBADMSG;
772801c135cSArtem B. Bityutskiy 	}
773801c135cSArtem B. Bityutskiy 
774801c135cSArtem B. Bityutskiy 	/* And of course validate what has just been read from the media */
775801c135cSArtem B. Bityutskiy 	err = validate_ec_hdr(ubi, ec_hdr);
776801c135cSArtem B. Bityutskiy 	if (err) {
77732608703STanya Brokhman 		ubi_err(ubi, "validation failed for PEB %d", pnum);
778801c135cSArtem B. Bityutskiy 		return -EINVAL;
779801c135cSArtem B. Bityutskiy 	}
780801c135cSArtem B. Bityutskiy 
781eb89580eSArtem Bityutskiy 	/*
782eb89580eSArtem Bityutskiy 	 * If there was %-EBADMSG, but the header CRC is still OK, report about
783eb89580eSArtem Bityutskiy 	 * a bit-flip to force scrubbing on this PEB.
784eb89580eSArtem Bityutskiy 	 */
785801c135cSArtem B. Bityutskiy 	return read_err ? UBI_IO_BITFLIPS : 0;
786801c135cSArtem B. Bityutskiy }
787801c135cSArtem B. Bityutskiy 
788801c135cSArtem B. Bityutskiy /**
789801c135cSArtem B. Bityutskiy  * ubi_io_write_ec_hdr - write an erase counter header.
790801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
791801c135cSArtem B. Bityutskiy  * @pnum: physical eraseblock to write to
792801c135cSArtem B. Bityutskiy  * @ec_hdr: the erase counter header to write
793801c135cSArtem B. Bityutskiy  *
794801c135cSArtem B. Bityutskiy  * This function writes erase counter header described by @ec_hdr to physical
795801c135cSArtem B. Bityutskiy  * eraseblock @pnum. It also fills most fields of @ec_hdr before writing, so
796801c135cSArtem B. Bityutskiy  * the caller do not have to fill them. Callers must only fill the @ec_hdr->ec
797801c135cSArtem B. Bityutskiy  * field.
798801c135cSArtem B. Bityutskiy  *
799801c135cSArtem B. Bityutskiy  * This function returns zero in case of success and a negative error code in
800801c135cSArtem B. Bityutskiy  * case of failure. If %-EIO is returned, the physical eraseblock most probably
801801c135cSArtem B. Bityutskiy  * went bad.
802801c135cSArtem B. Bityutskiy  */
ubi_io_write_ec_hdr(struct ubi_device * ubi,int pnum,struct ubi_ec_hdr * ec_hdr)803e88d6e10SArtem Bityutskiy int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum,
804801c135cSArtem B. Bityutskiy 			struct ubi_ec_hdr *ec_hdr)
805801c135cSArtem B. Bityutskiy {
806801c135cSArtem B. Bityutskiy 	int err;
807801c135cSArtem B. Bityutskiy 	uint32_t crc;
808801c135cSArtem B. Bityutskiy 
809801c135cSArtem B. Bityutskiy 	dbg_io("write EC header to PEB %d", pnum);
810801c135cSArtem B. Bityutskiy 	ubi_assert(pnum >= 0 &&  pnum < ubi->peb_count);
811801c135cSArtem B. Bityutskiy 
8123261ebd7SChristoph Hellwig 	ec_hdr->magic = cpu_to_be32(UBI_EC_HDR_MAGIC);
813801c135cSArtem B. Bityutskiy 	ec_hdr->version = UBI_VERSION;
8143261ebd7SChristoph Hellwig 	ec_hdr->vid_hdr_offset = cpu_to_be32(ubi->vid_hdr_offset);
8153261ebd7SChristoph Hellwig 	ec_hdr->data_offset = cpu_to_be32(ubi->leb_start);
8160c6c7fa1SAdrian Hunter 	ec_hdr->image_seq = cpu_to_be32(ubi->image_seq);
817801c135cSArtem B. Bityutskiy 	crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
8183261ebd7SChristoph Hellwig 	ec_hdr->hdr_crc = cpu_to_be32(crc);
819801c135cSArtem B. Bityutskiy 
8208056eb4aSArtem Bityutskiy 	err = self_check_ec_hdr(ubi, pnum, ec_hdr);
821801c135cSArtem B. Bityutskiy 	if (err)
822adbf05e3SArtem Bityutskiy 		return err;
823801c135cSArtem B. Bityutskiy 
82450269067Sdavid.oberhollenzer@sigma-star.at 	if (ubi_dbg_power_cut(ubi, POWER_CUT_EC_WRITE))
82550269067Sdavid.oberhollenzer@sigma-star.at 		return -EROFS;
82650269067Sdavid.oberhollenzer@sigma-star.at 
827801c135cSArtem B. Bityutskiy 	err = ubi_io_write(ubi, ec_hdr, pnum, 0, ubi->ec_hdr_alsize);
828801c135cSArtem B. Bityutskiy 	return err;
829801c135cSArtem B. Bityutskiy }
830801c135cSArtem B. Bityutskiy 
831801c135cSArtem B. Bityutskiy /**
832801c135cSArtem B. Bityutskiy  * validate_vid_hdr - validate a volume identifier header.
833801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
834801c135cSArtem B. Bityutskiy  * @vid_hdr: the volume identifier header to check
835801c135cSArtem B. Bityutskiy  *
836801c135cSArtem B. Bityutskiy  * This function checks that data stored in the volume identifier header
837801c135cSArtem B. Bityutskiy  * @vid_hdr. Returns zero if the VID header is OK and %1 if not.
838801c135cSArtem B. Bityutskiy  */
validate_vid_hdr(const struct ubi_device * ubi,const struct ubi_vid_hdr * vid_hdr)839801c135cSArtem B. Bityutskiy static int validate_vid_hdr(const struct ubi_device *ubi,
840801c135cSArtem B. Bityutskiy 			    const struct ubi_vid_hdr *vid_hdr)
841801c135cSArtem B. Bityutskiy {
842801c135cSArtem B. Bityutskiy 	int vol_type = vid_hdr->vol_type;
843801c135cSArtem B. Bityutskiy 	int copy_flag = vid_hdr->copy_flag;
8443261ebd7SChristoph Hellwig 	int vol_id = be32_to_cpu(vid_hdr->vol_id);
8453261ebd7SChristoph Hellwig 	int lnum = be32_to_cpu(vid_hdr->lnum);
846801c135cSArtem B. Bityutskiy 	int compat = vid_hdr->compat;
8473261ebd7SChristoph Hellwig 	int data_size = be32_to_cpu(vid_hdr->data_size);
8483261ebd7SChristoph Hellwig 	int used_ebs = be32_to_cpu(vid_hdr->used_ebs);
8493261ebd7SChristoph Hellwig 	int data_pad = be32_to_cpu(vid_hdr->data_pad);
8503261ebd7SChristoph Hellwig 	int data_crc = be32_to_cpu(vid_hdr->data_crc);
851801c135cSArtem B. Bityutskiy 	int usable_leb_size = ubi->leb_size - data_pad;
852801c135cSArtem B. Bityutskiy 
853801c135cSArtem B. Bityutskiy 	if (copy_flag != 0 && copy_flag != 1) {
85432608703STanya Brokhman 		ubi_err(ubi, "bad copy_flag");
855801c135cSArtem B. Bityutskiy 		goto bad;
856801c135cSArtem B. Bityutskiy 	}
857801c135cSArtem B. Bityutskiy 
858801c135cSArtem B. Bityutskiy 	if (vol_id < 0 || lnum < 0 || data_size < 0 || used_ebs < 0 ||
859801c135cSArtem B. Bityutskiy 	    data_pad < 0) {
86032608703STanya Brokhman 		ubi_err(ubi, "negative values");
861801c135cSArtem B. Bityutskiy 		goto bad;
862801c135cSArtem B. Bityutskiy 	}
863801c135cSArtem B. Bityutskiy 
864801c135cSArtem B. Bityutskiy 	if (vol_id >= UBI_MAX_VOLUMES && vol_id < UBI_INTERNAL_VOL_START) {
86532608703STanya Brokhman 		ubi_err(ubi, "bad vol_id");
866801c135cSArtem B. Bityutskiy 		goto bad;
867801c135cSArtem B. Bityutskiy 	}
868801c135cSArtem B. Bityutskiy 
869801c135cSArtem B. Bityutskiy 	if (vol_id < UBI_INTERNAL_VOL_START && compat != 0) {
87032608703STanya Brokhman 		ubi_err(ubi, "bad compat");
871801c135cSArtem B. Bityutskiy 		goto bad;
872801c135cSArtem B. Bityutskiy 	}
873801c135cSArtem B. Bityutskiy 
874801c135cSArtem B. Bityutskiy 	if (vol_id >= UBI_INTERNAL_VOL_START && compat != UBI_COMPAT_DELETE &&
875801c135cSArtem B. Bityutskiy 	    compat != UBI_COMPAT_RO && compat != UBI_COMPAT_PRESERVE &&
876801c135cSArtem B. Bityutskiy 	    compat != UBI_COMPAT_REJECT) {
87732608703STanya Brokhman 		ubi_err(ubi, "bad compat");
878801c135cSArtem B. Bityutskiy 		goto bad;
879801c135cSArtem B. Bityutskiy 	}
880801c135cSArtem B. Bityutskiy 
881801c135cSArtem B. Bityutskiy 	if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) {
88232608703STanya Brokhman 		ubi_err(ubi, "bad vol_type");
883801c135cSArtem B. Bityutskiy 		goto bad;
884801c135cSArtem B. Bityutskiy 	}
885801c135cSArtem B. Bityutskiy 
886801c135cSArtem B. Bityutskiy 	if (data_pad >= ubi->leb_size / 2) {
88732608703STanya Brokhman 		ubi_err(ubi, "bad data_pad");
888801c135cSArtem B. Bityutskiy 		goto bad;
889801c135cSArtem B. Bityutskiy 	}
890801c135cSArtem B. Bityutskiy 
891281fda27SRichard Weinberger 	if (data_size > ubi->leb_size) {
892281fda27SRichard Weinberger 		ubi_err(ubi, "bad data_size");
893281fda27SRichard Weinberger 		goto bad;
894281fda27SRichard Weinberger 	}
895281fda27SRichard Weinberger 
896801c135cSArtem B. Bityutskiy 	if (vol_type == UBI_VID_STATIC) {
897801c135cSArtem B. Bityutskiy 		/*
898801c135cSArtem B. Bityutskiy 		 * Although from high-level point of view static volumes may
899801c135cSArtem B. Bityutskiy 		 * contain zero bytes of data, but no VID headers can contain
900801c135cSArtem B. Bityutskiy 		 * zero at these fields, because they empty volumes do not have
901801c135cSArtem B. Bityutskiy 		 * mapped logical eraseblocks.
902801c135cSArtem B. Bityutskiy 		 */
903801c135cSArtem B. Bityutskiy 		if (used_ebs == 0) {
90432608703STanya Brokhman 			ubi_err(ubi, "zero used_ebs");
905801c135cSArtem B. Bityutskiy 			goto bad;
906801c135cSArtem B. Bityutskiy 		}
907801c135cSArtem B. Bityutskiy 		if (data_size == 0) {
90832608703STanya Brokhman 			ubi_err(ubi, "zero data_size");
909801c135cSArtem B. Bityutskiy 			goto bad;
910801c135cSArtem B. Bityutskiy 		}
911801c135cSArtem B. Bityutskiy 		if (lnum < used_ebs - 1) {
912801c135cSArtem B. Bityutskiy 			if (data_size != usable_leb_size) {
91332608703STanya Brokhman 				ubi_err(ubi, "bad data_size");
914801c135cSArtem B. Bityutskiy 				goto bad;
915801c135cSArtem B. Bityutskiy 			}
916cf0838dfSJubin Zhong 		} else if (lnum > used_ebs - 1) {
91732608703STanya Brokhman 			ubi_err(ubi, "too high lnum");
918801c135cSArtem B. Bityutskiy 			goto bad;
919801c135cSArtem B. Bityutskiy 		}
920801c135cSArtem B. Bityutskiy 	} else {
921801c135cSArtem B. Bityutskiy 		if (copy_flag == 0) {
922801c135cSArtem B. Bityutskiy 			if (data_crc != 0) {
92332608703STanya Brokhman 				ubi_err(ubi, "non-zero data CRC");
924801c135cSArtem B. Bityutskiy 				goto bad;
925801c135cSArtem B. Bityutskiy 			}
926801c135cSArtem B. Bityutskiy 			if (data_size != 0) {
92732608703STanya Brokhman 				ubi_err(ubi, "non-zero data_size");
928801c135cSArtem B. Bityutskiy 				goto bad;
929801c135cSArtem B. Bityutskiy 			}
930801c135cSArtem B. Bityutskiy 		} else {
931801c135cSArtem B. Bityutskiy 			if (data_size == 0) {
93232608703STanya Brokhman 				ubi_err(ubi, "zero data_size of copy");
933801c135cSArtem B. Bityutskiy 				goto bad;
934801c135cSArtem B. Bityutskiy 			}
935801c135cSArtem B. Bityutskiy 		}
936801c135cSArtem B. Bityutskiy 		if (used_ebs != 0) {
93732608703STanya Brokhman 			ubi_err(ubi, "bad used_ebs");
938801c135cSArtem B. Bityutskiy 			goto bad;
939801c135cSArtem B. Bityutskiy 		}
940801c135cSArtem B. Bityutskiy 	}
941801c135cSArtem B. Bityutskiy 
942801c135cSArtem B. Bityutskiy 	return 0;
943801c135cSArtem B. Bityutskiy 
944801c135cSArtem B. Bityutskiy bad:
94532608703STanya Brokhman 	ubi_err(ubi, "bad VID header");
946a904e3f1SArtem Bityutskiy 	ubi_dump_vid_hdr(vid_hdr);
94725886a36SArtem Bityutskiy 	dump_stack();
948801c135cSArtem B. Bityutskiy 	return 1;
949801c135cSArtem B. Bityutskiy }
950801c135cSArtem B. Bityutskiy 
951801c135cSArtem B. Bityutskiy /**
952801c135cSArtem B. Bityutskiy  * ubi_io_read_vid_hdr - read and check a volume identifier header.
953801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
954801c135cSArtem B. Bityutskiy  * @pnum: physical eraseblock number to read from
9553291b52fSBoris Brezillon  * @vidb: the volume identifier buffer to store data in
956801c135cSArtem B. Bityutskiy  * @verbose: be verbose if the header is corrupted or wasn't found
957801c135cSArtem B. Bityutskiy  *
958801c135cSArtem B. Bityutskiy  * This function reads the volume identifier header from physical eraseblock
9593291b52fSBoris Brezillon  * @pnum and stores it in @vidb. It also checks CRC checksum of the read
96074d82d26SArtem Bityutskiy  * volume identifier header. The error codes are the same as in
96174d82d26SArtem Bityutskiy  * 'ubi_io_read_ec_hdr()'.
962801c135cSArtem B. Bityutskiy  *
96374d82d26SArtem Bityutskiy  * Note, the implementation of this function is also very similar to
96474d82d26SArtem Bityutskiy  * 'ubi_io_read_ec_hdr()', so refer commentaries in 'ubi_io_read_ec_hdr()'.
965801c135cSArtem B. Bityutskiy  */
ubi_io_read_vid_hdr(struct ubi_device * ubi,int pnum,struct ubi_vid_io_buf * vidb,int verbose)966e88d6e10SArtem Bityutskiy int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
9673291b52fSBoris Brezillon 			struct ubi_vid_io_buf *vidb, int verbose)
968801c135cSArtem B. Bityutskiy {
96992e1a7d9SArtem Bityutskiy 	int err, read_err;
970801c135cSArtem B. Bityutskiy 	uint32_t crc, magic, hdr_crc;
9713291b52fSBoris Brezillon 	struct ubi_vid_hdr *vid_hdr = ubi_get_vid_hdr(vidb);
9723291b52fSBoris Brezillon 	void *p = vidb->buffer;
973801c135cSArtem B. Bityutskiy 
974801c135cSArtem B. Bityutskiy 	dbg_io("read VID header from PEB %d", pnum);
975801c135cSArtem B. Bityutskiy 	ubi_assert(pnum >= 0 &&  pnum < ubi->peb_count);
976801c135cSArtem B. Bityutskiy 
97792e1a7d9SArtem Bityutskiy 	read_err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset,
9788a8e8d2fSSascha Hauer 			  ubi->vid_hdr_shift + UBI_VID_HDR_SIZE);
979d57f4054SBrian Norris 	if (read_err && read_err != UBI_IO_BITFLIPS && !mtd_is_eccerr(read_err))
98092e1a7d9SArtem Bityutskiy 		return read_err;
981801c135cSArtem B. Bityutskiy 
9823261ebd7SChristoph Hellwig 	magic = be32_to_cpu(vid_hdr->magic);
983801c135cSArtem B. Bityutskiy 	if (magic != UBI_VID_HDR_MAGIC) {
984d57f4054SBrian Norris 		if (mtd_is_eccerr(read_err))
98592e1a7d9SArtem Bityutskiy 			return UBI_IO_BAD_HDR_EBADMSG;
986eb89580eSArtem Bityutskiy 
987bb00e180SArtem Bityutskiy 		if (ubi_check_pattern(vid_hdr, 0xFF, UBI_VID_HDR_SIZE)) {
988801c135cSArtem B. Bityutskiy 			if (verbose)
98932608703STanya Brokhman 				ubi_warn(ubi, "no VID header found at PEB %d, only 0xFF bytes",
990049333ceSArtem Bityutskiy 					 pnum);
991049333ceSArtem Bityutskiy 			dbg_bld("no VID header found at PEB %d, only 0xFF bytes",
992049333ceSArtem Bityutskiy 				pnum);
99392e1a7d9SArtem Bityutskiy 			if (!read_err)
99474d82d26SArtem Bityutskiy 				return UBI_IO_FF;
99592e1a7d9SArtem Bityutskiy 			else
99692e1a7d9SArtem Bityutskiy 				return UBI_IO_FF_BITFLIPS;
997801c135cSArtem B. Bityutskiy 		}
998801c135cSArtem B. Bityutskiy 
999801c135cSArtem B. Bityutskiy 		if (verbose) {
100032608703STanya Brokhman 			ubi_warn(ubi, "bad magic number at PEB %d: %08x instead of %08x",
1001049333ceSArtem Bityutskiy 				 pnum, magic, UBI_VID_HDR_MAGIC);
1002a904e3f1SArtem Bityutskiy 			ubi_dump_vid_hdr(vid_hdr);
10036f9fdf62SArtem Bityutskiy 		}
1004049333ceSArtem Bityutskiy 		dbg_bld("bad magic number at PEB %d: %08x instead of %08x",
1005049333ceSArtem Bityutskiy 			pnum, magic, UBI_VID_HDR_MAGIC);
1006786d7831SArtem Bityutskiy 		return UBI_IO_BAD_HDR;
1007801c135cSArtem B. Bityutskiy 	}
1008801c135cSArtem B. Bityutskiy 
1009801c135cSArtem B. Bityutskiy 	crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC);
10103261ebd7SChristoph Hellwig 	hdr_crc = be32_to_cpu(vid_hdr->hdr_crc);
1011801c135cSArtem B. Bityutskiy 
1012801c135cSArtem B. Bityutskiy 	if (hdr_crc != crc) {
1013801c135cSArtem B. Bityutskiy 		if (verbose) {
101432608703STanya Brokhman 			ubi_warn(ubi, "bad CRC at PEB %d, calculated %#08x, read %#08x",
1015049333ceSArtem Bityutskiy 				 pnum, crc, hdr_crc);
1016a904e3f1SArtem Bityutskiy 			ubi_dump_vid_hdr(vid_hdr);
10176f9fdf62SArtem Bityutskiy 		}
1018049333ceSArtem Bityutskiy 		dbg_bld("bad CRC at PEB %d, calculated %#08x, read %#08x",
1019049333ceSArtem Bityutskiy 			pnum, crc, hdr_crc);
102092e1a7d9SArtem Bityutskiy 		if (!read_err)
102192e1a7d9SArtem Bityutskiy 			return UBI_IO_BAD_HDR;
102292e1a7d9SArtem Bityutskiy 		else
102392e1a7d9SArtem Bityutskiy 			return UBI_IO_BAD_HDR_EBADMSG;
1024801c135cSArtem B. Bityutskiy 	}
1025801c135cSArtem B. Bityutskiy 
1026801c135cSArtem B. Bityutskiy 	err = validate_vid_hdr(ubi, vid_hdr);
1027801c135cSArtem B. Bityutskiy 	if (err) {
102832608703STanya Brokhman 		ubi_err(ubi, "validation failed for PEB %d", pnum);
1029801c135cSArtem B. Bityutskiy 		return -EINVAL;
1030801c135cSArtem B. Bityutskiy 	}
1031801c135cSArtem B. Bityutskiy 
1032801c135cSArtem B. Bityutskiy 	return read_err ? UBI_IO_BITFLIPS : 0;
1033801c135cSArtem B. Bityutskiy }
1034801c135cSArtem B. Bityutskiy 
1035801c135cSArtem B. Bityutskiy /**
1036801c135cSArtem B. Bityutskiy  * ubi_io_write_vid_hdr - write a volume identifier header.
1037801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
1038801c135cSArtem B. Bityutskiy  * @pnum: the physical eraseblock number to write to
10393291b52fSBoris Brezillon  * @vidb: the volume identifier buffer to write
1040801c135cSArtem B. Bityutskiy  *
1041801c135cSArtem B. Bityutskiy  * This function writes the volume identifier header described by @vid_hdr to
1042801c135cSArtem B. Bityutskiy  * physical eraseblock @pnum. This function automatically fills the
10433291b52fSBoris Brezillon  * @vidb->hdr->magic and the @vidb->hdr->version fields, as well as calculates
10443291b52fSBoris Brezillon  * header CRC checksum and stores it at vidb->hdr->hdr_crc.
1045801c135cSArtem B. Bityutskiy  *
1046801c135cSArtem B. Bityutskiy  * This function returns zero in case of success and a negative error code in
1047801c135cSArtem B. Bityutskiy  * case of failure. If %-EIO is returned, the physical eraseblock probably went
1048801c135cSArtem B. Bityutskiy  * bad.
1049801c135cSArtem B. Bityutskiy  */
ubi_io_write_vid_hdr(struct ubi_device * ubi,int pnum,struct ubi_vid_io_buf * vidb)1050e88d6e10SArtem Bityutskiy int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
10513291b52fSBoris Brezillon 			 struct ubi_vid_io_buf *vidb)
1052801c135cSArtem B. Bityutskiy {
10533291b52fSBoris Brezillon 	struct ubi_vid_hdr *vid_hdr = ubi_get_vid_hdr(vidb);
1054801c135cSArtem B. Bityutskiy 	int err;
1055801c135cSArtem B. Bityutskiy 	uint32_t crc;
10563291b52fSBoris Brezillon 	void *p = vidb->buffer;
1057801c135cSArtem B. Bityutskiy 
1058801c135cSArtem B. Bityutskiy 	dbg_io("write VID header to PEB %d", pnum);
1059801c135cSArtem B. Bityutskiy 	ubi_assert(pnum >= 0 &&  pnum < ubi->peb_count);
1060801c135cSArtem B. Bityutskiy 
10618056eb4aSArtem Bityutskiy 	err = self_check_peb_ec_hdr(ubi, pnum);
1062801c135cSArtem B. Bityutskiy 	if (err)
1063adbf05e3SArtem Bityutskiy 		return err;
1064801c135cSArtem B. Bityutskiy 
10653261ebd7SChristoph Hellwig 	vid_hdr->magic = cpu_to_be32(UBI_VID_HDR_MAGIC);
1066801c135cSArtem B. Bityutskiy 	vid_hdr->version = UBI_VERSION;
1067801c135cSArtem B. Bityutskiy 	crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC);
10683261ebd7SChristoph Hellwig 	vid_hdr->hdr_crc = cpu_to_be32(crc);
1069801c135cSArtem B. Bityutskiy 
10708056eb4aSArtem Bityutskiy 	err = self_check_vid_hdr(ubi, pnum, vid_hdr);
1071801c135cSArtem B. Bityutskiy 	if (err)
1072adbf05e3SArtem Bityutskiy 		return err;
1073801c135cSArtem B. Bityutskiy 
107450269067Sdavid.oberhollenzer@sigma-star.at 	if (ubi_dbg_power_cut(ubi, POWER_CUT_VID_WRITE))
107550269067Sdavid.oberhollenzer@sigma-star.at 		return -EROFS;
107650269067Sdavid.oberhollenzer@sigma-star.at 
1077801c135cSArtem B. Bityutskiy 	err = ubi_io_write(ubi, p, pnum, ubi->vid_hdr_aloffset,
1078801c135cSArtem B. Bityutskiy 			   ubi->vid_hdr_alsize);
1079801c135cSArtem B. Bityutskiy 	return err;
1080801c135cSArtem B. Bityutskiy }
1081801c135cSArtem B. Bityutskiy 
1082801c135cSArtem B. Bityutskiy /**
10838056eb4aSArtem Bityutskiy  * self_check_not_bad - ensure that a physical eraseblock is not bad.
1084801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
1085801c135cSArtem B. Bityutskiy  * @pnum: physical eraseblock number to check
1086801c135cSArtem B. Bityutskiy  *
1087adbf05e3SArtem Bityutskiy  * This function returns zero if the physical eraseblock is good, %-EINVAL if
1088adbf05e3SArtem Bityutskiy  * it is bad and a negative error code if an error occurred.
1089801c135cSArtem B. Bityutskiy  */
self_check_not_bad(const struct ubi_device * ubi,int pnum)10908056eb4aSArtem Bityutskiy static int self_check_not_bad(const struct ubi_device *ubi, int pnum)
1091801c135cSArtem B. Bityutskiy {
1092801c135cSArtem B. Bityutskiy 	int err;
1093801c135cSArtem B. Bityutskiy 
109464575574SEzequiel Garcia 	if (!ubi_dbg_chk_io(ubi))
109592d124f5SArtem Bityutskiy 		return 0;
109692d124f5SArtem Bityutskiy 
1097801c135cSArtem B. Bityutskiy 	err = ubi_io_is_bad(ubi, pnum);
1098801c135cSArtem B. Bityutskiy 	if (!err)
1099801c135cSArtem B. Bityutskiy 		return err;
1100801c135cSArtem B. Bityutskiy 
110132608703STanya Brokhman 	ubi_err(ubi, "self-check failed for PEB %d", pnum);
110225886a36SArtem Bityutskiy 	dump_stack();
1103adbf05e3SArtem Bityutskiy 	return err > 0 ? -EINVAL : err;
1104801c135cSArtem B. Bityutskiy }
1105801c135cSArtem B. Bityutskiy 
1106801c135cSArtem B. Bityutskiy /**
11078056eb4aSArtem Bityutskiy  * self_check_ec_hdr - check if an erase counter header is all right.
1108801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
1109801c135cSArtem B. Bityutskiy  * @pnum: physical eraseblock number the erase counter header belongs to
1110801c135cSArtem B. Bityutskiy  * @ec_hdr: the erase counter header to check
1111801c135cSArtem B. Bityutskiy  *
1112801c135cSArtem B. Bityutskiy  * This function returns zero if the erase counter header contains valid
1113adbf05e3SArtem Bityutskiy  * values, and %-EINVAL if not.
1114801c135cSArtem B. Bityutskiy  */
self_check_ec_hdr(const struct ubi_device * ubi,int pnum,const struct ubi_ec_hdr * ec_hdr)11158056eb4aSArtem Bityutskiy static int self_check_ec_hdr(const struct ubi_device *ubi, int pnum,
1116801c135cSArtem B. Bityutskiy 			     const struct ubi_ec_hdr *ec_hdr)
1117801c135cSArtem B. Bityutskiy {
1118801c135cSArtem B. Bityutskiy 	int err;
1119801c135cSArtem B. Bityutskiy 	uint32_t magic;
1120801c135cSArtem B. Bityutskiy 
112164575574SEzequiel Garcia 	if (!ubi_dbg_chk_io(ubi))
112292d124f5SArtem Bityutskiy 		return 0;
112392d124f5SArtem Bityutskiy 
11243261ebd7SChristoph Hellwig 	magic = be32_to_cpu(ec_hdr->magic);
1125801c135cSArtem B. Bityutskiy 	if (magic != UBI_EC_HDR_MAGIC) {
112632608703STanya Brokhman 		ubi_err(ubi, "bad magic %#08x, must be %#08x",
1127801c135cSArtem B. Bityutskiy 			magic, UBI_EC_HDR_MAGIC);
1128801c135cSArtem B. Bityutskiy 		goto fail;
1129801c135cSArtem B. Bityutskiy 	}
1130801c135cSArtem B. Bityutskiy 
1131801c135cSArtem B. Bityutskiy 	err = validate_ec_hdr(ubi, ec_hdr);
1132801c135cSArtem B. Bityutskiy 	if (err) {
113332608703STanya Brokhman 		ubi_err(ubi, "self-check failed for PEB %d", pnum);
1134801c135cSArtem B. Bityutskiy 		goto fail;
1135801c135cSArtem B. Bityutskiy 	}
1136801c135cSArtem B. Bityutskiy 
1137801c135cSArtem B. Bityutskiy 	return 0;
1138801c135cSArtem B. Bityutskiy 
1139801c135cSArtem B. Bityutskiy fail:
1140a904e3f1SArtem Bityutskiy 	ubi_dump_ec_hdr(ec_hdr);
114125886a36SArtem Bityutskiy 	dump_stack();
1142adbf05e3SArtem Bityutskiy 	return -EINVAL;
1143801c135cSArtem B. Bityutskiy }
1144801c135cSArtem B. Bityutskiy 
1145801c135cSArtem B. Bityutskiy /**
11468056eb4aSArtem Bityutskiy  * self_check_peb_ec_hdr - check erase counter header.
1147801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
1148801c135cSArtem B. Bityutskiy  * @pnum: the physical eraseblock number to check
1149801c135cSArtem B. Bityutskiy  *
1150*ec1f97f5SJilin Yuan  * This function returns zero if the erase counter header is all right and
1151adbf05e3SArtem Bityutskiy  * a negative error code if not or if an error occurred.
1152801c135cSArtem B. Bityutskiy  */
self_check_peb_ec_hdr(const struct ubi_device * ubi,int pnum)11538056eb4aSArtem Bityutskiy static int self_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum)
1154801c135cSArtem B. Bityutskiy {
1155801c135cSArtem B. Bityutskiy 	int err;
1156801c135cSArtem B. Bityutskiy 	uint32_t crc, hdr_crc;
1157801c135cSArtem B. Bityutskiy 	struct ubi_ec_hdr *ec_hdr;
1158801c135cSArtem B. Bityutskiy 
115964575574SEzequiel Garcia 	if (!ubi_dbg_chk_io(ubi))
116092d124f5SArtem Bityutskiy 		return 0;
116192d124f5SArtem Bityutskiy 
116233818bbbSArtem Bityutskiy 	ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
1163801c135cSArtem B. Bityutskiy 	if (!ec_hdr)
1164801c135cSArtem B. Bityutskiy 		return -ENOMEM;
1165801c135cSArtem B. Bityutskiy 
1166801c135cSArtem B. Bityutskiy 	err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE);
1167d57f4054SBrian Norris 	if (err && err != UBI_IO_BITFLIPS && !mtd_is_eccerr(err))
1168801c135cSArtem B. Bityutskiy 		goto exit;
1169801c135cSArtem B. Bityutskiy 
1170801c135cSArtem B. Bityutskiy 	crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
11713261ebd7SChristoph Hellwig 	hdr_crc = be32_to_cpu(ec_hdr->hdr_crc);
1172801c135cSArtem B. Bityutskiy 	if (hdr_crc != crc) {
117332608703STanya Brokhman 		ubi_err(ubi, "bad CRC, calculated %#08x, read %#08x",
117432608703STanya Brokhman 			crc, hdr_crc);
117532608703STanya Brokhman 		ubi_err(ubi, "self-check failed for PEB %d", pnum);
1176a904e3f1SArtem Bityutskiy 		ubi_dump_ec_hdr(ec_hdr);
117725886a36SArtem Bityutskiy 		dump_stack();
1178adbf05e3SArtem Bityutskiy 		err = -EINVAL;
1179801c135cSArtem B. Bityutskiy 		goto exit;
1180801c135cSArtem B. Bityutskiy 	}
1181801c135cSArtem B. Bityutskiy 
11828056eb4aSArtem Bityutskiy 	err = self_check_ec_hdr(ubi, pnum, ec_hdr);
1183801c135cSArtem B. Bityutskiy 
1184801c135cSArtem B. Bityutskiy exit:
1185801c135cSArtem B. Bityutskiy 	kfree(ec_hdr);
1186801c135cSArtem B. Bityutskiy 	return err;
1187801c135cSArtem B. Bityutskiy }
1188801c135cSArtem B. Bityutskiy 
1189801c135cSArtem B. Bityutskiy /**
11908056eb4aSArtem Bityutskiy  * self_check_vid_hdr - check that a volume identifier header is all right.
1191801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
1192801c135cSArtem B. Bityutskiy  * @pnum: physical eraseblock number the volume identifier header belongs to
1193801c135cSArtem B. Bityutskiy  * @vid_hdr: the volume identifier header to check
1194801c135cSArtem B. Bityutskiy  *
1195801c135cSArtem B. Bityutskiy  * This function returns zero if the volume identifier header is all right, and
1196adbf05e3SArtem Bityutskiy  * %-EINVAL if not.
1197801c135cSArtem B. Bityutskiy  */
self_check_vid_hdr(const struct ubi_device * ubi,int pnum,const struct ubi_vid_hdr * vid_hdr)11988056eb4aSArtem Bityutskiy static int self_check_vid_hdr(const struct ubi_device *ubi, int pnum,
1199801c135cSArtem B. Bityutskiy 			      const struct ubi_vid_hdr *vid_hdr)
1200801c135cSArtem B. Bityutskiy {
1201801c135cSArtem B. Bityutskiy 	int err;
1202801c135cSArtem B. Bityutskiy 	uint32_t magic;
1203801c135cSArtem B. Bityutskiy 
120464575574SEzequiel Garcia 	if (!ubi_dbg_chk_io(ubi))
120592d124f5SArtem Bityutskiy 		return 0;
120692d124f5SArtem Bityutskiy 
12073261ebd7SChristoph Hellwig 	magic = be32_to_cpu(vid_hdr->magic);
1208801c135cSArtem B. Bityutskiy 	if (magic != UBI_VID_HDR_MAGIC) {
120932608703STanya Brokhman 		ubi_err(ubi, "bad VID header magic %#08x at PEB %d, must be %#08x",
1210801c135cSArtem B. Bityutskiy 			magic, pnum, UBI_VID_HDR_MAGIC);
1211801c135cSArtem B. Bityutskiy 		goto fail;
1212801c135cSArtem B. Bityutskiy 	}
1213801c135cSArtem B. Bityutskiy 
1214801c135cSArtem B. Bityutskiy 	err = validate_vid_hdr(ubi, vid_hdr);
1215801c135cSArtem B. Bityutskiy 	if (err) {
121632608703STanya Brokhman 		ubi_err(ubi, "self-check failed for PEB %d", pnum);
1217801c135cSArtem B. Bityutskiy 		goto fail;
1218801c135cSArtem B. Bityutskiy 	}
1219801c135cSArtem B. Bityutskiy 
1220801c135cSArtem B. Bityutskiy 	return err;
1221801c135cSArtem B. Bityutskiy 
1222801c135cSArtem B. Bityutskiy fail:
122332608703STanya Brokhman 	ubi_err(ubi, "self-check failed for PEB %d", pnum);
1224a904e3f1SArtem Bityutskiy 	ubi_dump_vid_hdr(vid_hdr);
122525886a36SArtem Bityutskiy 	dump_stack();
1226adbf05e3SArtem Bityutskiy 	return -EINVAL;
1227801c135cSArtem B. Bityutskiy 
1228801c135cSArtem B. Bityutskiy }
1229801c135cSArtem B. Bityutskiy 
1230801c135cSArtem B. Bityutskiy /**
12318056eb4aSArtem Bityutskiy  * self_check_peb_vid_hdr - check volume identifier header.
1232801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
1233801c135cSArtem B. Bityutskiy  * @pnum: the physical eraseblock number to check
1234801c135cSArtem B. Bityutskiy  *
1235801c135cSArtem B. Bityutskiy  * This function returns zero if the volume identifier header is all right,
1236adbf05e3SArtem Bityutskiy  * and a negative error code if not or if an error occurred.
1237801c135cSArtem B. Bityutskiy  */
self_check_peb_vid_hdr(const struct ubi_device * ubi,int pnum)12388056eb4aSArtem Bityutskiy static int self_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum)
1239801c135cSArtem B. Bityutskiy {
1240801c135cSArtem B. Bityutskiy 	int err;
1241801c135cSArtem B. Bityutskiy 	uint32_t crc, hdr_crc;
12423291b52fSBoris Brezillon 	struct ubi_vid_io_buf *vidb;
1243801c135cSArtem B. Bityutskiy 	struct ubi_vid_hdr *vid_hdr;
1244801c135cSArtem B. Bityutskiy 	void *p;
1245801c135cSArtem B. Bityutskiy 
124664575574SEzequiel Garcia 	if (!ubi_dbg_chk_io(ubi))
124792d124f5SArtem Bityutskiy 		return 0;
124892d124f5SArtem Bityutskiy 
12493291b52fSBoris Brezillon 	vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS);
12503291b52fSBoris Brezillon 	if (!vidb)
1251801c135cSArtem B. Bityutskiy 		return -ENOMEM;
1252801c135cSArtem B. Bityutskiy 
12533291b52fSBoris Brezillon 	vid_hdr = ubi_get_vid_hdr(vidb);
12543291b52fSBoris Brezillon 	p = vidb->buffer;
1255801c135cSArtem B. Bityutskiy 	err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset,
1256801c135cSArtem B. Bityutskiy 			  ubi->vid_hdr_alsize);
1257d57f4054SBrian Norris 	if (err && err != UBI_IO_BITFLIPS && !mtd_is_eccerr(err))
1258801c135cSArtem B. Bityutskiy 		goto exit;
1259801c135cSArtem B. Bityutskiy 
12602e69d491SBrian Norris 	crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC);
12613261ebd7SChristoph Hellwig 	hdr_crc = be32_to_cpu(vid_hdr->hdr_crc);
1262801c135cSArtem B. Bityutskiy 	if (hdr_crc != crc) {
126332608703STanya Brokhman 		ubi_err(ubi, "bad VID header CRC at PEB %d, calculated %#08x, read %#08x",
1264049333ceSArtem Bityutskiy 			pnum, crc, hdr_crc);
126532608703STanya Brokhman 		ubi_err(ubi, "self-check failed for PEB %d", pnum);
1266a904e3f1SArtem Bityutskiy 		ubi_dump_vid_hdr(vid_hdr);
126725886a36SArtem Bityutskiy 		dump_stack();
1268adbf05e3SArtem Bityutskiy 		err = -EINVAL;
1269801c135cSArtem B. Bityutskiy 		goto exit;
1270801c135cSArtem B. Bityutskiy 	}
1271801c135cSArtem B. Bityutskiy 
12728056eb4aSArtem Bityutskiy 	err = self_check_vid_hdr(ubi, pnum, vid_hdr);
1273801c135cSArtem B. Bityutskiy 
1274801c135cSArtem B. Bityutskiy exit:
12753291b52fSBoris Brezillon 	ubi_free_vid_buf(vidb);
1276801c135cSArtem B. Bityutskiy 	return err;
1277801c135cSArtem B. Bityutskiy }
1278801c135cSArtem B. Bityutskiy 
1279801c135cSArtem B. Bityutskiy /**
128097d6104bSArtem Bityutskiy  * self_check_write - make sure write succeeded.
12816e9065d7SArtem Bityutskiy  * @ubi: UBI device description object
12826e9065d7SArtem Bityutskiy  * @buf: buffer with data which were written
12836e9065d7SArtem Bityutskiy  * @pnum: physical eraseblock number the data were written to
12846e9065d7SArtem Bityutskiy  * @offset: offset within the physical eraseblock the data were written to
12856e9065d7SArtem Bityutskiy  * @len: how many bytes were written
12866e9065d7SArtem Bityutskiy  *
12876e9065d7SArtem Bityutskiy  * This functions reads data which were recently written and compares it with
12886e9065d7SArtem Bityutskiy  * the original data buffer - the data have to match. Returns zero if the data
12896e9065d7SArtem Bityutskiy  * match and a negative error code if not or in case of failure.
12906e9065d7SArtem Bityutskiy  */
self_check_write(struct ubi_device * ubi,const void * buf,int pnum,int offset,int len)129197d6104bSArtem Bityutskiy static int self_check_write(struct ubi_device *ubi, const void *buf, int pnum,
12926e9065d7SArtem Bityutskiy 			    int offset, int len)
12936e9065d7SArtem Bityutskiy {
12946e9065d7SArtem Bityutskiy 	int err, i;
12957950d023SArtem Bityutskiy 	size_t read;
1296a7586743SArtem Bityutskiy 	void *buf1;
12977950d023SArtem Bityutskiy 	loff_t addr = (loff_t)pnum * ubi->peb_size + offset;
12986e9065d7SArtem Bityutskiy 
129964575574SEzequiel Garcia 	if (!ubi_dbg_chk_io(ubi))
130092d124f5SArtem Bityutskiy 		return 0;
130192d124f5SArtem Bityutskiy 
130288dca4caSChristoph Hellwig 	buf1 = __vmalloc(len, GFP_NOFS);
1303a7586743SArtem Bityutskiy 	if (!buf1) {
130432608703STanya Brokhman 		ubi_err(ubi, "cannot allocate memory to check writes");
1305a7586743SArtem Bityutskiy 		return 0;
1306a7586743SArtem Bityutskiy 	}
1307a7586743SArtem Bityutskiy 
1308329ad399SArtem Bityutskiy 	err = mtd_read(ubi->mtd, addr, len, &read, buf1);
1309d57f4054SBrian Norris 	if (err && !mtd_is_bitflip(err))
1310a7586743SArtem Bityutskiy 		goto out_free;
13116e9065d7SArtem Bityutskiy 
13126e9065d7SArtem Bityutskiy 	for (i = 0; i < len; i++) {
13136e9065d7SArtem Bityutskiy 		uint8_t c = ((uint8_t *)buf)[i];
1314a7586743SArtem Bityutskiy 		uint8_t c1 = ((uint8_t *)buf1)[i];
13156e9065d7SArtem Bityutskiy 		int dump_len;
13166e9065d7SArtem Bityutskiy 
13176e9065d7SArtem Bityutskiy 		if (c == c1)
13186e9065d7SArtem Bityutskiy 			continue;
13196e9065d7SArtem Bityutskiy 
132032608703STanya Brokhman 		ubi_err(ubi, "self-check failed for PEB %d:%d, len %d",
13216e9065d7SArtem Bityutskiy 			pnum, offset, len);
132232608703STanya Brokhman 		ubi_msg(ubi, "data differ at position %d", i);
13236e9065d7SArtem Bityutskiy 		dump_len = max_t(int, 128, len - i);
132432608703STanya Brokhman 		ubi_msg(ubi, "hex dump of the original buffer from %d to %d",
13256e9065d7SArtem Bityutskiy 			i, i + dump_len);
13266e9065d7SArtem Bityutskiy 		print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
13276e9065d7SArtem Bityutskiy 			       buf + i, dump_len, 1);
132832608703STanya Brokhman 		ubi_msg(ubi, "hex dump of the read buffer from %d to %d",
13296e9065d7SArtem Bityutskiy 			i, i + dump_len);
13306e9065d7SArtem Bityutskiy 		print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
1331a7586743SArtem Bityutskiy 			       buf1 + i, dump_len, 1);
133225886a36SArtem Bityutskiy 		dump_stack();
13336e9065d7SArtem Bityutskiy 		err = -EINVAL;
1334a7586743SArtem Bityutskiy 		goto out_free;
13356e9065d7SArtem Bityutskiy 	}
13366e9065d7SArtem Bityutskiy 
1337a7586743SArtem Bityutskiy 	vfree(buf1);
13386e9065d7SArtem Bityutskiy 	return 0;
13396e9065d7SArtem Bityutskiy 
1340a7586743SArtem Bityutskiy out_free:
1341a7586743SArtem Bityutskiy 	vfree(buf1);
13426e9065d7SArtem Bityutskiy 	return err;
13436e9065d7SArtem Bityutskiy }
13446e9065d7SArtem Bityutskiy 
13456e9065d7SArtem Bityutskiy /**
134697d6104bSArtem Bityutskiy  * ubi_self_check_all_ff - check that a region of flash is empty.
1347801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
1348801c135cSArtem B. Bityutskiy  * @pnum: the physical eraseblock number to check
1349801c135cSArtem B. Bityutskiy  * @offset: the starting offset within the physical eraseblock to check
1350801c135cSArtem B. Bityutskiy  * @len: the length of the region to check
1351801c135cSArtem B. Bityutskiy  *
1352801c135cSArtem B. Bityutskiy  * This function returns zero if only 0xFF bytes are present at offset
1353adbf05e3SArtem Bityutskiy  * @offset of the physical eraseblock @pnum, and a negative error code if not
1354adbf05e3SArtem Bityutskiy  * or if an error occurred.
1355801c135cSArtem B. Bityutskiy  */
ubi_self_check_all_ff(struct ubi_device * ubi,int pnum,int offset,int len)135697d6104bSArtem Bityutskiy int ubi_self_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len)
1357801c135cSArtem B. Bityutskiy {
1358801c135cSArtem B. Bityutskiy 	size_t read;
1359801c135cSArtem B. Bityutskiy 	int err;
1360332873d6SArtem Bityutskiy 	void *buf;
1361801c135cSArtem B. Bityutskiy 	loff_t addr = (loff_t)pnum * ubi->peb_size + offset;
1362801c135cSArtem B. Bityutskiy 
136364575574SEzequiel Garcia 	if (!ubi_dbg_chk_io(ubi))
136492d124f5SArtem Bityutskiy 		return 0;
136592d124f5SArtem Bityutskiy 
136688dca4caSChristoph Hellwig 	buf = __vmalloc(len, GFP_NOFS);
1367332873d6SArtem Bityutskiy 	if (!buf) {
136832608703STanya Brokhman 		ubi_err(ubi, "cannot allocate memory to check for 0xFFs");
1369332873d6SArtem Bityutskiy 		return 0;
1370332873d6SArtem Bityutskiy 	}
1371332873d6SArtem Bityutskiy 
1372329ad399SArtem Bityutskiy 	err = mtd_read(ubi->mtd, addr, len, &read, buf);
1373d57f4054SBrian Norris 	if (err && !mtd_is_bitflip(err)) {
137432608703STanya Brokhman 		ubi_err(ubi, "err %d while reading %d bytes from PEB %d:%d, read %zd bytes",
1375049333ceSArtem Bityutskiy 			err, len, pnum, offset, read);
1376801c135cSArtem B. Bityutskiy 		goto error;
1377801c135cSArtem B. Bityutskiy 	}
1378801c135cSArtem B. Bityutskiy 
1379332873d6SArtem Bityutskiy 	err = ubi_check_pattern(buf, 0xFF, len);
1380801c135cSArtem B. Bityutskiy 	if (err == 0) {
138132608703STanya Brokhman 		ubi_err(ubi, "flash region at PEB %d:%d, length %d does not contain all 0xFF bytes",
1382049333ceSArtem Bityutskiy 			pnum, offset, len);
1383801c135cSArtem B. Bityutskiy 		goto fail;
1384801c135cSArtem B. Bityutskiy 	}
1385801c135cSArtem B. Bityutskiy 
1386332873d6SArtem Bityutskiy 	vfree(buf);
1387801c135cSArtem B. Bityutskiy 	return 0;
1388801c135cSArtem B. Bityutskiy 
1389801c135cSArtem B. Bityutskiy fail:
139032608703STanya Brokhman 	ubi_err(ubi, "self-check failed for PEB %d", pnum);
139145fc5c81STanya Brokhman 	ubi_msg(ubi, "hex dump of the %d-%d region", offset, offset + len);
1392332873d6SArtem Bityutskiy 	print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, buf, len, 1);
1393adbf05e3SArtem Bityutskiy 	err = -EINVAL;
1394801c135cSArtem B. Bityutskiy error:
139525886a36SArtem Bityutskiy 	dump_stack();
1396332873d6SArtem Bityutskiy 	vfree(buf);
1397801c135cSArtem B. Bityutskiy 	return err;
1398801c135cSArtem B. Bityutskiy }
1399