xref: /OK3568_Linux_fs/u-boot/drivers/mtd/ubi/io.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (c) International Business Machines Corp., 2006
3*4882a593Smuzhiyun  * Copyright (c) Nokia Corporation, 2006, 2007
4*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Author: Artem Bityutskiy (Битюцкий Артём)
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun /*
10*4882a593Smuzhiyun  * UBI input/output sub-system.
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * This sub-system provides a uniform way to work with all kinds of the
13*4882a593Smuzhiyun  * underlying MTD devices. It also implements handy functions for reading and
14*4882a593Smuzhiyun  * writing UBI headers.
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  * We are trying to have a paranoid mindset and not to trust to what we read
17*4882a593Smuzhiyun  * from the flash media in order to be more secure and robust. So this
18*4882a593Smuzhiyun  * sub-system validates every single header it reads from the flash media.
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  * Some words about how the eraseblock headers are stored.
21*4882a593Smuzhiyun  *
22*4882a593Smuzhiyun  * The erase counter header is always stored at offset zero. By default, the
23*4882a593Smuzhiyun  * VID header is stored after the EC header at the closest aligned offset
24*4882a593Smuzhiyun  * (i.e. aligned to the minimum I/O unit size). Data starts next to the VID
25*4882a593Smuzhiyun  * header at the closest aligned offset. But this default layout may be
26*4882a593Smuzhiyun  * changed. For example, for different reasons (e.g., optimization) UBI may be
27*4882a593Smuzhiyun  * asked to put the VID header at further offset, and even at an unaligned
28*4882a593Smuzhiyun  * offset. Of course, if the offset of the VID header is unaligned, UBI adds
29*4882a593Smuzhiyun  * proper padding in front of it. Data offset may also be changed but it has to
30*4882a593Smuzhiyun  * be aligned.
31*4882a593Smuzhiyun  *
32*4882a593Smuzhiyun  * About minimal I/O units. In general, UBI assumes flash device model where
33*4882a593Smuzhiyun  * there is only one minimal I/O unit size. E.g., in case of NOR flash it is 1,
34*4882a593Smuzhiyun  * in case of NAND flash it is a NAND page, etc. This is reported by MTD in the
35*4882a593Smuzhiyun  * @ubi->mtd->writesize field. But as an exception, UBI admits of using another
36*4882a593Smuzhiyun  * (smaller) minimal I/O unit size for EC and VID headers to make it possible
37*4882a593Smuzhiyun  * to do different optimizations.
38*4882a593Smuzhiyun  *
39*4882a593Smuzhiyun  * This is extremely useful in case of NAND flashes which admit of several
40*4882a593Smuzhiyun  * write operations to one NAND page. In this case UBI can fit EC and VID
41*4882a593Smuzhiyun  * headers at one NAND page. Thus, UBI may use "sub-page" size as the minimal
42*4882a593Smuzhiyun  * I/O unit for the headers (the @ubi->hdrs_min_io_size field). But it still
43*4882a593Smuzhiyun  * reports NAND page size (@ubi->min_io_size) as a minimal I/O unit for the UBI
44*4882a593Smuzhiyun  * users.
45*4882a593Smuzhiyun  *
46*4882a593Smuzhiyun  * Example: some Samsung NANDs with 2KiB pages allow 4x 512-byte writes, so
47*4882a593Smuzhiyun  * although the minimal I/O unit is 2K, UBI uses 512 bytes for EC and VID
48*4882a593Smuzhiyun  * headers.
49*4882a593Smuzhiyun  *
50*4882a593Smuzhiyun  * Q: why not just to treat sub-page as a minimal I/O unit of this flash
51*4882a593Smuzhiyun  * device, e.g., make @ubi->min_io_size = 512 in the example above?
52*4882a593Smuzhiyun  *
53*4882a593Smuzhiyun  * A: because when writing a sub-page, MTD still writes a full 2K page but the
54*4882a593Smuzhiyun  * bytes which are not relevant to the sub-page are 0xFF. So, basically,
55*4882a593Smuzhiyun  * writing 4x512 sub-pages is 4 times slower than writing one 2KiB NAND page.
56*4882a593Smuzhiyun  * Thus, we prefer to use sub-pages only for EC and VID headers.
57*4882a593Smuzhiyun  *
58*4882a593Smuzhiyun  * As it was noted above, the VID header may start at a non-aligned offset.
59*4882a593Smuzhiyun  * For example, in case of a 2KiB page NAND flash with a 512 bytes sub-page,
60*4882a593Smuzhiyun  * the VID header may reside at offset 1984 which is the last 64 bytes of the
61*4882a593Smuzhiyun  * last sub-page (EC header is always at offset zero). This causes some
62*4882a593Smuzhiyun  * difficulties when reading and writing VID headers.
63*4882a593Smuzhiyun  *
64*4882a593Smuzhiyun  * Suppose we have a 64-byte buffer and we read a VID header at it. We change
65*4882a593Smuzhiyun  * the data and want to write this VID header out. As we can only write in
66*4882a593Smuzhiyun  * 512-byte chunks, we have to allocate one more buffer and copy our VID header
67*4882a593Smuzhiyun  * to offset 448 of this buffer.
68*4882a593Smuzhiyun  *
69*4882a593Smuzhiyun  * The I/O sub-system does the following trick in order to avoid this extra
70*4882a593Smuzhiyun  * copy. It always allocates a @ubi->vid_hdr_alsize bytes buffer for the VID
71*4882a593Smuzhiyun  * header and returns a pointer to offset @ubi->vid_hdr_shift of this buffer.
72*4882a593Smuzhiyun  * When the VID header is being written out, it shifts the VID header pointer
73*4882a593Smuzhiyun  * back and writes the whole sub-page.
74*4882a593Smuzhiyun  */
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun #ifndef __UBOOT__
77*4882a593Smuzhiyun #include <linux/crc32.h>
78*4882a593Smuzhiyun #include <linux/err.h>
79*4882a593Smuzhiyun #include <linux/slab.h>
80*4882a593Smuzhiyun #else
81*4882a593Smuzhiyun #include <hexdump.h>
82*4882a593Smuzhiyun #include <ubi_uboot.h>
83*4882a593Smuzhiyun #endif
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun #include "ubi.h"
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun static int self_check_not_bad(const struct ubi_device *ubi, int pnum);
88*4882a593Smuzhiyun static int self_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum);
89*4882a593Smuzhiyun static int self_check_ec_hdr(const struct ubi_device *ubi, int pnum,
90*4882a593Smuzhiyun 			     const struct ubi_ec_hdr *ec_hdr);
91*4882a593Smuzhiyun static int self_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum);
92*4882a593Smuzhiyun static int self_check_vid_hdr(const struct ubi_device *ubi, int pnum,
93*4882a593Smuzhiyun 			      const struct ubi_vid_hdr *vid_hdr);
94*4882a593Smuzhiyun static int self_check_write(struct ubi_device *ubi, const void *buf, int pnum,
95*4882a593Smuzhiyun 			    int offset, int len);
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun /**
98*4882a593Smuzhiyun  * ubi_io_read - read data from a physical eraseblock.
99*4882a593Smuzhiyun  * @ubi: UBI device description object
100*4882a593Smuzhiyun  * @buf: buffer where to store the read data
101*4882a593Smuzhiyun  * @pnum: physical eraseblock number to read from
102*4882a593Smuzhiyun  * @offset: offset within the physical eraseblock from where to read
103*4882a593Smuzhiyun  * @len: how many bytes to read
104*4882a593Smuzhiyun  *
105*4882a593Smuzhiyun  * This function reads data from offset @offset of physical eraseblock @pnum
106*4882a593Smuzhiyun  * and stores the read data in the @buf buffer. The following return codes are
107*4882a593Smuzhiyun  * possible:
108*4882a593Smuzhiyun  *
109*4882a593Smuzhiyun  * o %0 if all the requested data were successfully read;
110*4882a593Smuzhiyun  * o %UBI_IO_BITFLIPS if all the requested data were successfully read, but
111*4882a593Smuzhiyun  *   correctable bit-flips were detected; this is harmless but may indicate
112*4882a593Smuzhiyun  *   that this eraseblock may become bad soon (but do not have to);
113*4882a593Smuzhiyun  * o %-EBADMSG if the MTD subsystem reported about data integrity problems, for
114*4882a593Smuzhiyun  *   example it can be an ECC error in case of NAND; this most probably means
115*4882a593Smuzhiyun  *   that the data is corrupted;
116*4882a593Smuzhiyun  * o %-EIO if some I/O error occurred;
117*4882a593Smuzhiyun  * o other negative error codes in case of other errors.
118*4882a593Smuzhiyun  */
ubi_io_read(const struct ubi_device * ubi,void * buf,int pnum,int offset,int len)119*4882a593Smuzhiyun int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset,
120*4882a593Smuzhiyun 		int len)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun 	int err, retries = 0;
123*4882a593Smuzhiyun 	size_t read;
124*4882a593Smuzhiyun 	loff_t addr;
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 	dbg_io("read %d bytes from PEB %d:%d", len, pnum, offset);
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 	ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
129*4882a593Smuzhiyun 	ubi_assert(offset >= 0 && offset + len <= ubi->peb_size);
130*4882a593Smuzhiyun 	ubi_assert(len > 0);
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	err = self_check_not_bad(ubi, pnum);
133*4882a593Smuzhiyun 	if (err)
134*4882a593Smuzhiyun 		return err;
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 	/*
137*4882a593Smuzhiyun 	 * Deliberately corrupt the buffer to improve robustness. Indeed, if we
138*4882a593Smuzhiyun 	 * do not do this, the following may happen:
139*4882a593Smuzhiyun 	 * 1. The buffer contains data from previous operation, e.g., read from
140*4882a593Smuzhiyun 	 *    another PEB previously. The data looks like expected, e.g., if we
141*4882a593Smuzhiyun 	 *    just do not read anything and return - the caller would not
142*4882a593Smuzhiyun 	 *    notice this. E.g., if we are reading a VID header, the buffer may
143*4882a593Smuzhiyun 	 *    contain a valid VID header from another PEB.
144*4882a593Smuzhiyun 	 * 2. The driver is buggy and returns us success or -EBADMSG or
145*4882a593Smuzhiyun 	 *    -EUCLEAN, but it does not actually put any data to the buffer.
146*4882a593Smuzhiyun 	 *
147*4882a593Smuzhiyun 	 * This may confuse UBI or upper layers - they may think the buffer
148*4882a593Smuzhiyun 	 * contains valid data while in fact it is just old data. This is
149*4882a593Smuzhiyun 	 * especially possible because UBI (and UBIFS) relies on CRC, and
150*4882a593Smuzhiyun 	 * treats data as correct even in case of ECC errors if the CRC is
151*4882a593Smuzhiyun 	 * correct.
152*4882a593Smuzhiyun 	 *
153*4882a593Smuzhiyun 	 * Try to prevent this situation by changing the first byte of the
154*4882a593Smuzhiyun 	 * buffer.
155*4882a593Smuzhiyun 	 */
156*4882a593Smuzhiyun 	*((uint8_t *)buf) ^= 0xFF;
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 	addr = (loff_t)pnum * ubi->peb_size + offset;
159*4882a593Smuzhiyun retry:
160*4882a593Smuzhiyun 	err = mtd_read(ubi->mtd, addr, len, &read, buf);
161*4882a593Smuzhiyun 	if (err) {
162*4882a593Smuzhiyun 		const char *errstr = mtd_is_eccerr(err) ? " (ECC error)" : "";
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun 		if (mtd_is_bitflip(err)) {
165*4882a593Smuzhiyun 			/*
166*4882a593Smuzhiyun 			 * -EUCLEAN is reported if there was a bit-flip which
167*4882a593Smuzhiyun 			 * was corrected, so this is harmless.
168*4882a593Smuzhiyun 			 *
169*4882a593Smuzhiyun 			 * We do not report about it here unless debugging is
170*4882a593Smuzhiyun 			 * enabled. A corresponding message will be printed
171*4882a593Smuzhiyun 			 * later, when it is has been scrubbed.
172*4882a593Smuzhiyun 			 */
173*4882a593Smuzhiyun 			ubi_msg(ubi, "fixable bit-flip detected at PEB %d",
174*4882a593Smuzhiyun 				pnum);
175*4882a593Smuzhiyun 			ubi_assert(len == read);
176*4882a593Smuzhiyun 			return UBI_IO_BITFLIPS;
177*4882a593Smuzhiyun 		}
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 		if (retries++ < UBI_IO_RETRIES) {
180*4882a593Smuzhiyun 			ubi_warn(ubi, "error %d%s while reading %d bytes from PEB %d:%d, read only %zd bytes, retry",
181*4882a593Smuzhiyun 				 err, errstr, len, pnum, offset, read);
182*4882a593Smuzhiyun 			yield();
183*4882a593Smuzhiyun 			goto retry;
184*4882a593Smuzhiyun 		}
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 		ubi_err(ubi, "error %d%s while reading %d bytes from PEB %d:%d, read %zd bytes",
187*4882a593Smuzhiyun 			err, errstr, len, pnum, offset, read);
188*4882a593Smuzhiyun 		dump_stack();
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 		/*
191*4882a593Smuzhiyun 		 * The driver should never return -EBADMSG if it failed to read
192*4882a593Smuzhiyun 		 * all the requested data. But some buggy drivers might do
193*4882a593Smuzhiyun 		 * this, so we change it to -EIO.
194*4882a593Smuzhiyun 		 */
195*4882a593Smuzhiyun 		if (read != len && mtd_is_eccerr(err)) {
196*4882a593Smuzhiyun 			ubi_assert(0);
197*4882a593Smuzhiyun 			err = -EIO;
198*4882a593Smuzhiyun 		}
199*4882a593Smuzhiyun 	} else {
200*4882a593Smuzhiyun 		ubi_assert(len == read);
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 		if (ubi_dbg_is_bitflip(ubi)) {
203*4882a593Smuzhiyun 			dbg_gen("bit-flip (emulated)");
204*4882a593Smuzhiyun 			err = UBI_IO_BITFLIPS;
205*4882a593Smuzhiyun 		}
206*4882a593Smuzhiyun 	}
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 	return err;
209*4882a593Smuzhiyun }
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun /**
212*4882a593Smuzhiyun  * ubi_io_write - write data to a physical eraseblock.
213*4882a593Smuzhiyun  * @ubi: UBI device description object
214*4882a593Smuzhiyun  * @buf: buffer with the data to write
215*4882a593Smuzhiyun  * @pnum: physical eraseblock number to write to
216*4882a593Smuzhiyun  * @offset: offset within the physical eraseblock where to write
217*4882a593Smuzhiyun  * @len: how many bytes to write
218*4882a593Smuzhiyun  *
219*4882a593Smuzhiyun  * This function writes @len bytes of data from buffer @buf to offset @offset
220*4882a593Smuzhiyun  * of physical eraseblock @pnum. If all the data were successfully written,
221*4882a593Smuzhiyun  * zero is returned. If an error occurred, this function returns a negative
222*4882a593Smuzhiyun  * error code. If %-EIO is returned, the physical eraseblock most probably went
223*4882a593Smuzhiyun  * bad.
224*4882a593Smuzhiyun  *
225*4882a593Smuzhiyun  * Note, in case of an error, it is possible that something was still written
226*4882a593Smuzhiyun  * to the flash media, but may be some garbage.
227*4882a593Smuzhiyun  */
ubi_io_write(struct ubi_device * ubi,const void * buf,int pnum,int offset,int len)228*4882a593Smuzhiyun int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
229*4882a593Smuzhiyun 		 int len)
230*4882a593Smuzhiyun {
231*4882a593Smuzhiyun 	int err;
232*4882a593Smuzhiyun 	size_t written;
233*4882a593Smuzhiyun 	loff_t addr;
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	dbg_io("write %d bytes to PEB %d:%d", len, pnum, offset);
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun 	ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
238*4882a593Smuzhiyun 	ubi_assert(offset >= 0 && offset + len <= ubi->peb_size);
239*4882a593Smuzhiyun 	ubi_assert(offset % ubi->hdrs_min_io_size == 0);
240*4882a593Smuzhiyun 	ubi_assert(len > 0 && len % ubi->hdrs_min_io_size == 0);
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	if (ubi->ro_mode) {
243*4882a593Smuzhiyun 		ubi_err(ubi, "read-only mode");
244*4882a593Smuzhiyun 		return -EROFS;
245*4882a593Smuzhiyun 	}
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 	err = self_check_not_bad(ubi, pnum);
248*4882a593Smuzhiyun 	if (err)
249*4882a593Smuzhiyun 		return err;
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	/* The area we are writing to has to contain all 0xFF bytes */
252*4882a593Smuzhiyun 	err = ubi_self_check_all_ff(ubi, pnum, offset, len);
253*4882a593Smuzhiyun 	if (err)
254*4882a593Smuzhiyun 		return err;
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun 	if (offset >= ubi->leb_start) {
257*4882a593Smuzhiyun 		/*
258*4882a593Smuzhiyun 		 * We write to the data area of the physical eraseblock. Make
259*4882a593Smuzhiyun 		 * sure it has valid EC and VID headers.
260*4882a593Smuzhiyun 		 */
261*4882a593Smuzhiyun 		err = self_check_peb_ec_hdr(ubi, pnum);
262*4882a593Smuzhiyun 		if (err)
263*4882a593Smuzhiyun 			return err;
264*4882a593Smuzhiyun 		err = self_check_peb_vid_hdr(ubi, pnum);
265*4882a593Smuzhiyun 		if (err)
266*4882a593Smuzhiyun 			return err;
267*4882a593Smuzhiyun 	}
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	if (ubi_dbg_is_write_failure(ubi)) {
270*4882a593Smuzhiyun 		ubi_err(ubi, "cannot write %d bytes to PEB %d:%d (emulated)",
271*4882a593Smuzhiyun 			len, pnum, offset);
272*4882a593Smuzhiyun 		dump_stack();
273*4882a593Smuzhiyun 		return -EIO;
274*4882a593Smuzhiyun 	}
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 	addr = (loff_t)pnum * ubi->peb_size + offset;
277*4882a593Smuzhiyun 	err = mtd_write(ubi->mtd, addr, len, &written, buf);
278*4882a593Smuzhiyun 	if (err) {
279*4882a593Smuzhiyun 		ubi_err(ubi, "error %d while writing %d bytes to PEB %d:%d, written %zd bytes",
280*4882a593Smuzhiyun 			err, len, pnum, offset, written);
281*4882a593Smuzhiyun 		dump_stack();
282*4882a593Smuzhiyun 		ubi_dump_flash(ubi, pnum, offset, len);
283*4882a593Smuzhiyun 	} else
284*4882a593Smuzhiyun 		ubi_assert(written == len);
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun 	if (!err) {
287*4882a593Smuzhiyun 		err = self_check_write(ubi, buf, pnum, offset, len);
288*4882a593Smuzhiyun 		if (err)
289*4882a593Smuzhiyun 			return err;
290*4882a593Smuzhiyun 
291*4882a593Smuzhiyun 		/*
292*4882a593Smuzhiyun 		 * Since we always write sequentially, the rest of the PEB has
293*4882a593Smuzhiyun 		 * to contain only 0xFF bytes.
294*4882a593Smuzhiyun 		 */
295*4882a593Smuzhiyun 		offset += len;
296*4882a593Smuzhiyun 		len = ubi->peb_size - offset;
297*4882a593Smuzhiyun 		if (len)
298*4882a593Smuzhiyun 			err = ubi_self_check_all_ff(ubi, pnum, offset, len);
299*4882a593Smuzhiyun 	}
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun 	return err;
302*4882a593Smuzhiyun }
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun /**
305*4882a593Smuzhiyun  * erase_callback - MTD erasure call-back.
306*4882a593Smuzhiyun  * @ei: MTD erase information object.
307*4882a593Smuzhiyun  *
308*4882a593Smuzhiyun  * Note, even though MTD erase interface is asynchronous, all the current
309*4882a593Smuzhiyun  * implementations are synchronous anyway.
310*4882a593Smuzhiyun  */
erase_callback(struct erase_info * ei)311*4882a593Smuzhiyun static void erase_callback(struct erase_info *ei)
312*4882a593Smuzhiyun {
313*4882a593Smuzhiyun 	wake_up_interruptible((wait_queue_head_t *)ei->priv);
314*4882a593Smuzhiyun }
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun /**
317*4882a593Smuzhiyun  * do_sync_erase - synchronously erase a physical eraseblock.
318*4882a593Smuzhiyun  * @ubi: UBI device description object
319*4882a593Smuzhiyun  * @pnum: the physical eraseblock number to erase
320*4882a593Smuzhiyun  *
321*4882a593Smuzhiyun  * This function synchronously erases physical eraseblock @pnum and returns
322*4882a593Smuzhiyun  * zero in case of success and a negative error code in case of failure. If
323*4882a593Smuzhiyun  * %-EIO is returned, the physical eraseblock most probably went bad.
324*4882a593Smuzhiyun  */
do_sync_erase(struct ubi_device * ubi,int pnum)325*4882a593Smuzhiyun static int do_sync_erase(struct ubi_device *ubi, int pnum)
326*4882a593Smuzhiyun {
327*4882a593Smuzhiyun 	int err, retries = 0;
328*4882a593Smuzhiyun 	struct erase_info ei;
329*4882a593Smuzhiyun 	wait_queue_head_t wq;
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun 	dbg_io("erase PEB %d", pnum);
332*4882a593Smuzhiyun 	ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun 	if (ubi->ro_mode) {
335*4882a593Smuzhiyun 		ubi_err(ubi, "read-only mode");
336*4882a593Smuzhiyun 		return -EROFS;
337*4882a593Smuzhiyun 	}
338*4882a593Smuzhiyun 
339*4882a593Smuzhiyun retry:
340*4882a593Smuzhiyun 	init_waitqueue_head(&wq);
341*4882a593Smuzhiyun 	memset(&ei, 0, sizeof(struct erase_info));
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun 	ei.mtd      = ubi->mtd;
344*4882a593Smuzhiyun 	ei.addr     = (loff_t)pnum * ubi->peb_size;
345*4882a593Smuzhiyun 	ei.len      = ubi->peb_size;
346*4882a593Smuzhiyun 	ei.callback = erase_callback;
347*4882a593Smuzhiyun 	ei.priv     = (unsigned long)&wq;
348*4882a593Smuzhiyun 
349*4882a593Smuzhiyun 	err = mtd_erase(ubi->mtd, &ei);
350*4882a593Smuzhiyun 	if (err) {
351*4882a593Smuzhiyun 		if (retries++ < UBI_IO_RETRIES) {
352*4882a593Smuzhiyun 			ubi_warn(ubi, "error %d while erasing PEB %d, retry",
353*4882a593Smuzhiyun 				 err, pnum);
354*4882a593Smuzhiyun 			yield();
355*4882a593Smuzhiyun 			goto retry;
356*4882a593Smuzhiyun 		}
357*4882a593Smuzhiyun 		ubi_err(ubi, "cannot erase PEB %d, error %d", pnum, err);
358*4882a593Smuzhiyun 		dump_stack();
359*4882a593Smuzhiyun 		return err;
360*4882a593Smuzhiyun 	}
361*4882a593Smuzhiyun 
362*4882a593Smuzhiyun 	err = wait_event_interruptible(wq, ei.state == MTD_ERASE_DONE ||
363*4882a593Smuzhiyun 					   ei.state == MTD_ERASE_FAILED);
364*4882a593Smuzhiyun 	if (err) {
365*4882a593Smuzhiyun 		ubi_err(ubi, "interrupted PEB %d erasure", pnum);
366*4882a593Smuzhiyun 		return -EINTR;
367*4882a593Smuzhiyun 	}
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun 	if (ei.state == MTD_ERASE_FAILED) {
370*4882a593Smuzhiyun 		if (retries++ < UBI_IO_RETRIES) {
371*4882a593Smuzhiyun 			ubi_warn(ubi, "error while erasing PEB %d, retry",
372*4882a593Smuzhiyun 				 pnum);
373*4882a593Smuzhiyun 			yield();
374*4882a593Smuzhiyun 			goto retry;
375*4882a593Smuzhiyun 		}
376*4882a593Smuzhiyun 		ubi_err(ubi, "cannot erase PEB %d", pnum);
377*4882a593Smuzhiyun 		dump_stack();
378*4882a593Smuzhiyun 		return -EIO;
379*4882a593Smuzhiyun 	}
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun 	err = ubi_self_check_all_ff(ubi, pnum, 0, ubi->peb_size);
382*4882a593Smuzhiyun 	if (err)
383*4882a593Smuzhiyun 		return err;
384*4882a593Smuzhiyun 
385*4882a593Smuzhiyun 	if (ubi_dbg_is_erase_failure(ubi)) {
386*4882a593Smuzhiyun 		ubi_err(ubi, "cannot erase PEB %d (emulated)", pnum);
387*4882a593Smuzhiyun 		return -EIO;
388*4882a593Smuzhiyun 	}
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun 	return 0;
391*4882a593Smuzhiyun }
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun /* Patterns to write to a physical eraseblock when torturing it */
394*4882a593Smuzhiyun static uint8_t patterns[] = {0xa5, 0x5a, 0x0};
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun /**
397*4882a593Smuzhiyun  * torture_peb - test a supposedly bad physical eraseblock.
398*4882a593Smuzhiyun  * @ubi: UBI device description object
399*4882a593Smuzhiyun  * @pnum: the physical eraseblock number to test
400*4882a593Smuzhiyun  *
401*4882a593Smuzhiyun  * This function returns %-EIO if the physical eraseblock did not pass the
402*4882a593Smuzhiyun  * test, a positive number of erase operations done if the test was
403*4882a593Smuzhiyun  * successfully passed, and other negative error codes in case of other errors.
404*4882a593Smuzhiyun  */
torture_peb(struct ubi_device * ubi,int pnum)405*4882a593Smuzhiyun static int torture_peb(struct ubi_device *ubi, int pnum)
406*4882a593Smuzhiyun {
407*4882a593Smuzhiyun 	int err, i, patt_count;
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun 	ubi_msg(ubi, "run torture test for PEB %d", pnum);
410*4882a593Smuzhiyun 	patt_count = ARRAY_SIZE(patterns);
411*4882a593Smuzhiyun 	ubi_assert(patt_count > 0);
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun 	mutex_lock(&ubi->buf_mutex);
414*4882a593Smuzhiyun 	for (i = 0; i < patt_count; i++) {
415*4882a593Smuzhiyun 		err = do_sync_erase(ubi, pnum);
416*4882a593Smuzhiyun 		if (err)
417*4882a593Smuzhiyun 			goto out;
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun 		/* Make sure the PEB contains only 0xFF bytes */
420*4882a593Smuzhiyun 		err = ubi_io_read(ubi, ubi->peb_buf, pnum, 0, ubi->peb_size);
421*4882a593Smuzhiyun 		if (err)
422*4882a593Smuzhiyun 			goto out;
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun 		err = ubi_check_pattern(ubi->peb_buf, 0xFF, ubi->peb_size);
425*4882a593Smuzhiyun 		if (err == 0) {
426*4882a593Smuzhiyun 			ubi_err(ubi, "erased PEB %d, but a non-0xFF byte found",
427*4882a593Smuzhiyun 				pnum);
428*4882a593Smuzhiyun 			err = -EIO;
429*4882a593Smuzhiyun 			goto out;
430*4882a593Smuzhiyun 		}
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun 		/* Write a pattern and check it */
433*4882a593Smuzhiyun 		memset(ubi->peb_buf, patterns[i], ubi->peb_size);
434*4882a593Smuzhiyun 		err = ubi_io_write(ubi, ubi->peb_buf, pnum, 0, ubi->peb_size);
435*4882a593Smuzhiyun 		if (err)
436*4882a593Smuzhiyun 			goto out;
437*4882a593Smuzhiyun 
438*4882a593Smuzhiyun 		memset(ubi->peb_buf, ~patterns[i], ubi->peb_size);
439*4882a593Smuzhiyun 		err = ubi_io_read(ubi, ubi->peb_buf, pnum, 0, ubi->peb_size);
440*4882a593Smuzhiyun 		if (err)
441*4882a593Smuzhiyun 			goto out;
442*4882a593Smuzhiyun 
443*4882a593Smuzhiyun 		err = ubi_check_pattern(ubi->peb_buf, patterns[i],
444*4882a593Smuzhiyun 					ubi->peb_size);
445*4882a593Smuzhiyun 		if (err == 0) {
446*4882a593Smuzhiyun 			ubi_err(ubi, "pattern %x checking failed for PEB %d",
447*4882a593Smuzhiyun 				patterns[i], pnum);
448*4882a593Smuzhiyun 			err = -EIO;
449*4882a593Smuzhiyun 			goto out;
450*4882a593Smuzhiyun 		}
451*4882a593Smuzhiyun 	}
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun 	err = patt_count;
454*4882a593Smuzhiyun 	ubi_msg(ubi, "PEB %d passed torture test, do not mark it as bad", pnum);
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun out:
457*4882a593Smuzhiyun 	mutex_unlock(&ubi->buf_mutex);
458*4882a593Smuzhiyun 	if (err == UBI_IO_BITFLIPS || mtd_is_eccerr(err)) {
459*4882a593Smuzhiyun 		/*
460*4882a593Smuzhiyun 		 * If a bit-flip or data integrity error was detected, the test
461*4882a593Smuzhiyun 		 * has not passed because it happened on a freshly erased
462*4882a593Smuzhiyun 		 * physical eraseblock which means something is wrong with it.
463*4882a593Smuzhiyun 		 */
464*4882a593Smuzhiyun 		ubi_err(ubi, "read problems on freshly erased PEB %d, must be bad",
465*4882a593Smuzhiyun 			pnum);
466*4882a593Smuzhiyun 		err = -EIO;
467*4882a593Smuzhiyun 	}
468*4882a593Smuzhiyun 	return err;
469*4882a593Smuzhiyun }
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun /**
472*4882a593Smuzhiyun  * nor_erase_prepare - prepare a NOR flash PEB for erasure.
473*4882a593Smuzhiyun  * @ubi: UBI device description object
474*4882a593Smuzhiyun  * @pnum: physical eraseblock number to prepare
475*4882a593Smuzhiyun  *
476*4882a593Smuzhiyun  * NOR flash, or at least some of them, have peculiar embedded PEB erasure
477*4882a593Smuzhiyun  * algorithm: the PEB is first filled with zeroes, then it is erased. And
478*4882a593Smuzhiyun  * filling with zeroes starts from the end of the PEB. This was observed with
479*4882a593Smuzhiyun  * Spansion S29GL512N NOR flash.
480*4882a593Smuzhiyun  *
481*4882a593Smuzhiyun  * This means that in case of a power cut we may end up with intact data at the
482*4882a593Smuzhiyun  * beginning of the PEB, and all zeroes at the end of PEB. In other words, the
483*4882a593Smuzhiyun  * EC and VID headers are OK, but a large chunk of data at the end of PEB is
484*4882a593Smuzhiyun  * zeroed. This makes UBI mistakenly treat this PEB as used and associate it
485*4882a593Smuzhiyun  * with an LEB, which leads to subsequent failures (e.g., UBIFS fails).
486*4882a593Smuzhiyun  *
487*4882a593Smuzhiyun  * This function is called before erasing NOR PEBs and it zeroes out EC and VID
488*4882a593Smuzhiyun  * magic numbers in order to invalidate them and prevent the failures. Returns
489*4882a593Smuzhiyun  * zero in case of success and a negative error code in case of failure.
490*4882a593Smuzhiyun  */
nor_erase_prepare(struct ubi_device * ubi,int pnum)491*4882a593Smuzhiyun static int nor_erase_prepare(struct ubi_device *ubi, int pnum)
492*4882a593Smuzhiyun {
493*4882a593Smuzhiyun 	int err;
494*4882a593Smuzhiyun 	size_t written;
495*4882a593Smuzhiyun 	loff_t addr;
496*4882a593Smuzhiyun 	uint32_t data = 0;
497*4882a593Smuzhiyun 	struct ubi_ec_hdr ec_hdr;
498*4882a593Smuzhiyun 
499*4882a593Smuzhiyun 	/*
500*4882a593Smuzhiyun 	 * Note, we cannot generally define VID header buffers on stack,
501*4882a593Smuzhiyun 	 * because of the way we deal with these buffers (see the header
502*4882a593Smuzhiyun 	 * comment in this file). But we know this is a NOR-specific piece of
503*4882a593Smuzhiyun 	 * code, so we can do this. But yes, this is error-prone and we should
504*4882a593Smuzhiyun 	 * (pre-)allocate VID header buffer instead.
505*4882a593Smuzhiyun 	 */
506*4882a593Smuzhiyun 	struct ubi_vid_hdr vid_hdr;
507*4882a593Smuzhiyun 
508*4882a593Smuzhiyun 	/*
509*4882a593Smuzhiyun 	 * If VID or EC is valid, we have to corrupt them before erasing.
510*4882a593Smuzhiyun 	 * It is important to first invalidate the EC header, and then the VID
511*4882a593Smuzhiyun 	 * header. Otherwise a power cut may lead to valid EC header and
512*4882a593Smuzhiyun 	 * invalid VID header, in which case UBI will treat this PEB as
513*4882a593Smuzhiyun 	 * corrupted and will try to preserve it, and print scary warnings.
514*4882a593Smuzhiyun 	 */
515*4882a593Smuzhiyun 	addr = (loff_t)pnum * ubi->peb_size;
516*4882a593Smuzhiyun 	err = ubi_io_read_ec_hdr(ubi, pnum, &ec_hdr, 0);
517*4882a593Smuzhiyun 	if (err != UBI_IO_BAD_HDR_EBADMSG && err != UBI_IO_BAD_HDR &&
518*4882a593Smuzhiyun 	    err != UBI_IO_FF){
519*4882a593Smuzhiyun 		err = mtd_write(ubi->mtd, addr, 4, &written, (void *)&data);
520*4882a593Smuzhiyun 		if(err)
521*4882a593Smuzhiyun 			goto error;
522*4882a593Smuzhiyun 	}
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun 	err = ubi_io_read_vid_hdr(ubi, pnum, &vid_hdr, 0);
525*4882a593Smuzhiyun 	if (err != UBI_IO_BAD_HDR_EBADMSG && err != UBI_IO_BAD_HDR &&
526*4882a593Smuzhiyun 	    err != UBI_IO_FF){
527*4882a593Smuzhiyun 		addr += ubi->vid_hdr_aloffset;
528*4882a593Smuzhiyun 		err = mtd_write(ubi->mtd, addr, 4, &written, (void *)&data);
529*4882a593Smuzhiyun 		if (err)
530*4882a593Smuzhiyun 			goto error;
531*4882a593Smuzhiyun 	}
532*4882a593Smuzhiyun 	return 0;
533*4882a593Smuzhiyun 
534*4882a593Smuzhiyun error:
535*4882a593Smuzhiyun 	/*
536*4882a593Smuzhiyun 	 * The PEB contains a valid VID or EC header, but we cannot invalidate
537*4882a593Smuzhiyun 	 * it. Supposedly the flash media or the driver is screwed up, so
538*4882a593Smuzhiyun 	 * return an error.
539*4882a593Smuzhiyun 	 */
540*4882a593Smuzhiyun 	ubi_err(ubi, "cannot invalidate PEB %d, write returned %d", pnum, err);
541*4882a593Smuzhiyun 	ubi_dump_flash(ubi, pnum, 0, ubi->peb_size);
542*4882a593Smuzhiyun 	return -EIO;
543*4882a593Smuzhiyun }
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun /**
546*4882a593Smuzhiyun  * ubi_io_sync_erase - synchronously erase a physical eraseblock.
547*4882a593Smuzhiyun  * @ubi: UBI device description object
548*4882a593Smuzhiyun  * @pnum: physical eraseblock number to erase
549*4882a593Smuzhiyun  * @torture: if this physical eraseblock has to be tortured
550*4882a593Smuzhiyun  *
551*4882a593Smuzhiyun  * This function synchronously erases physical eraseblock @pnum. If @torture
552*4882a593Smuzhiyun  * flag is not zero, the physical eraseblock is checked by means of writing
553*4882a593Smuzhiyun  * different patterns to it and reading them back. If the torturing is enabled,
554*4882a593Smuzhiyun  * the physical eraseblock is erased more than once.
555*4882a593Smuzhiyun  *
556*4882a593Smuzhiyun  * This function returns the number of erasures made in case of success, %-EIO
557*4882a593Smuzhiyun  * if the erasure failed or the torturing test failed, and other negative error
558*4882a593Smuzhiyun  * codes in case of other errors. Note, %-EIO means that the physical
559*4882a593Smuzhiyun  * eraseblock is bad.
560*4882a593Smuzhiyun  */
ubi_io_sync_erase(struct ubi_device * ubi,int pnum,int torture)561*4882a593Smuzhiyun int ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture)
562*4882a593Smuzhiyun {
563*4882a593Smuzhiyun 	int err, ret = 0;
564*4882a593Smuzhiyun 
565*4882a593Smuzhiyun 	ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
566*4882a593Smuzhiyun 
567*4882a593Smuzhiyun 	err = self_check_not_bad(ubi, pnum);
568*4882a593Smuzhiyun 	if (err != 0)
569*4882a593Smuzhiyun 		return err;
570*4882a593Smuzhiyun 
571*4882a593Smuzhiyun 	if (ubi->ro_mode) {
572*4882a593Smuzhiyun 		ubi_err(ubi, "read-only mode");
573*4882a593Smuzhiyun 		return -EROFS;
574*4882a593Smuzhiyun 	}
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun 	if (ubi->nor_flash) {
577*4882a593Smuzhiyun 		err = nor_erase_prepare(ubi, pnum);
578*4882a593Smuzhiyun 		if (err)
579*4882a593Smuzhiyun 			return err;
580*4882a593Smuzhiyun 	}
581*4882a593Smuzhiyun 
582*4882a593Smuzhiyun 	if (torture) {
583*4882a593Smuzhiyun 		ret = torture_peb(ubi, pnum);
584*4882a593Smuzhiyun 		if (ret < 0)
585*4882a593Smuzhiyun 			return ret;
586*4882a593Smuzhiyun 	}
587*4882a593Smuzhiyun 
588*4882a593Smuzhiyun 	err = do_sync_erase(ubi, pnum);
589*4882a593Smuzhiyun 	if (err)
590*4882a593Smuzhiyun 		return err;
591*4882a593Smuzhiyun 
592*4882a593Smuzhiyun 	return ret + 1;
593*4882a593Smuzhiyun }
594*4882a593Smuzhiyun 
595*4882a593Smuzhiyun /**
596*4882a593Smuzhiyun  * ubi_io_is_bad - check if a physical eraseblock is bad.
597*4882a593Smuzhiyun  * @ubi: UBI device description object
598*4882a593Smuzhiyun  * @pnum: the physical eraseblock number to check
599*4882a593Smuzhiyun  *
600*4882a593Smuzhiyun  * This function returns a positive number if the physical eraseblock is bad,
601*4882a593Smuzhiyun  * zero if not, and a negative error code if an error occurred.
602*4882a593Smuzhiyun  */
ubi_io_is_bad(const struct ubi_device * ubi,int pnum)603*4882a593Smuzhiyun int ubi_io_is_bad(const struct ubi_device *ubi, int pnum)
604*4882a593Smuzhiyun {
605*4882a593Smuzhiyun 	struct mtd_info *mtd = ubi->mtd;
606*4882a593Smuzhiyun 
607*4882a593Smuzhiyun 	ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
608*4882a593Smuzhiyun 
609*4882a593Smuzhiyun 	if (ubi->bad_allowed) {
610*4882a593Smuzhiyun 		int ret;
611*4882a593Smuzhiyun 
612*4882a593Smuzhiyun 		ret = mtd_block_isbad(mtd, (loff_t)pnum * ubi->peb_size);
613*4882a593Smuzhiyun 		if (ret < 0)
614*4882a593Smuzhiyun 			ubi_err(ubi, "error %d while checking if PEB %d is bad",
615*4882a593Smuzhiyun 				ret, pnum);
616*4882a593Smuzhiyun 		else if (ret)
617*4882a593Smuzhiyun 			dbg_io("PEB %d is bad", pnum);
618*4882a593Smuzhiyun 		return ret;
619*4882a593Smuzhiyun 	}
620*4882a593Smuzhiyun 
621*4882a593Smuzhiyun 	return 0;
622*4882a593Smuzhiyun }
623*4882a593Smuzhiyun 
624*4882a593Smuzhiyun /**
625*4882a593Smuzhiyun  * ubi_io_mark_bad - mark a physical eraseblock as bad.
626*4882a593Smuzhiyun  * @ubi: UBI device description object
627*4882a593Smuzhiyun  * @pnum: the physical eraseblock number to mark
628*4882a593Smuzhiyun  *
629*4882a593Smuzhiyun  * This function returns zero in case of success and a negative error code in
630*4882a593Smuzhiyun  * case of failure.
631*4882a593Smuzhiyun  */
ubi_io_mark_bad(const struct ubi_device * ubi,int pnum)632*4882a593Smuzhiyun int ubi_io_mark_bad(const struct ubi_device *ubi, int pnum)
633*4882a593Smuzhiyun {
634*4882a593Smuzhiyun 	int err;
635*4882a593Smuzhiyun 	struct mtd_info *mtd = ubi->mtd;
636*4882a593Smuzhiyun 
637*4882a593Smuzhiyun 	ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
638*4882a593Smuzhiyun 
639*4882a593Smuzhiyun 	if (ubi->ro_mode) {
640*4882a593Smuzhiyun 		ubi_err(ubi, "read-only mode");
641*4882a593Smuzhiyun 		return -EROFS;
642*4882a593Smuzhiyun 	}
643*4882a593Smuzhiyun 
644*4882a593Smuzhiyun 	if (!ubi->bad_allowed)
645*4882a593Smuzhiyun 		return 0;
646*4882a593Smuzhiyun 
647*4882a593Smuzhiyun 	err = mtd_block_markbad(mtd, (loff_t)pnum * ubi->peb_size);
648*4882a593Smuzhiyun 	if (err)
649*4882a593Smuzhiyun 		ubi_err(ubi, "cannot mark PEB %d bad, error %d", pnum, err);
650*4882a593Smuzhiyun 	return err;
651*4882a593Smuzhiyun }
652*4882a593Smuzhiyun 
653*4882a593Smuzhiyun /**
654*4882a593Smuzhiyun  * validate_ec_hdr - validate an erase counter header.
655*4882a593Smuzhiyun  * @ubi: UBI device description object
656*4882a593Smuzhiyun  * @ec_hdr: the erase counter header to check
657*4882a593Smuzhiyun  *
658*4882a593Smuzhiyun  * This function returns zero if the erase counter header is OK, and %1 if
659*4882a593Smuzhiyun  * not.
660*4882a593Smuzhiyun  */
validate_ec_hdr(const struct ubi_device * ubi,const struct ubi_ec_hdr * ec_hdr)661*4882a593Smuzhiyun static int validate_ec_hdr(const struct ubi_device *ubi,
662*4882a593Smuzhiyun 			   const struct ubi_ec_hdr *ec_hdr)
663*4882a593Smuzhiyun {
664*4882a593Smuzhiyun 	long long ec;
665*4882a593Smuzhiyun 	int vid_hdr_offset, leb_start;
666*4882a593Smuzhiyun 
667*4882a593Smuzhiyun 	ec = be64_to_cpu(ec_hdr->ec);
668*4882a593Smuzhiyun 	vid_hdr_offset = be32_to_cpu(ec_hdr->vid_hdr_offset);
669*4882a593Smuzhiyun 	leb_start = be32_to_cpu(ec_hdr->data_offset);
670*4882a593Smuzhiyun 
671*4882a593Smuzhiyun 	if (ec_hdr->version != UBI_VERSION) {
672*4882a593Smuzhiyun 		ubi_err(ubi, "node with incompatible UBI version found: this UBI version is %d, image version is %d",
673*4882a593Smuzhiyun 			UBI_VERSION, (int)ec_hdr->version);
674*4882a593Smuzhiyun 		goto bad;
675*4882a593Smuzhiyun 	}
676*4882a593Smuzhiyun 
677*4882a593Smuzhiyun 	if (vid_hdr_offset != ubi->vid_hdr_offset) {
678*4882a593Smuzhiyun 		ubi_err(ubi, "bad VID header offset %d, expected %d",
679*4882a593Smuzhiyun 			vid_hdr_offset, ubi->vid_hdr_offset);
680*4882a593Smuzhiyun 		goto bad;
681*4882a593Smuzhiyun 	}
682*4882a593Smuzhiyun 
683*4882a593Smuzhiyun 	if (leb_start != ubi->leb_start) {
684*4882a593Smuzhiyun 		ubi_err(ubi, "bad data offset %d, expected %d",
685*4882a593Smuzhiyun 			leb_start, ubi->leb_start);
686*4882a593Smuzhiyun 		goto bad;
687*4882a593Smuzhiyun 	}
688*4882a593Smuzhiyun 
689*4882a593Smuzhiyun 	if (ec < 0 || ec > UBI_MAX_ERASECOUNTER) {
690*4882a593Smuzhiyun 		ubi_err(ubi, "bad erase counter %lld", ec);
691*4882a593Smuzhiyun 		goto bad;
692*4882a593Smuzhiyun 	}
693*4882a593Smuzhiyun 
694*4882a593Smuzhiyun 	return 0;
695*4882a593Smuzhiyun 
696*4882a593Smuzhiyun bad:
697*4882a593Smuzhiyun 	ubi_err(ubi, "bad EC header");
698*4882a593Smuzhiyun 	ubi_dump_ec_hdr(ec_hdr);
699*4882a593Smuzhiyun 	dump_stack();
700*4882a593Smuzhiyun 	return 1;
701*4882a593Smuzhiyun }
702*4882a593Smuzhiyun 
703*4882a593Smuzhiyun /**
704*4882a593Smuzhiyun  * ubi_io_read_ec_hdr - read and check an erase counter header.
705*4882a593Smuzhiyun  * @ubi: UBI device description object
706*4882a593Smuzhiyun  * @pnum: physical eraseblock to read from
707*4882a593Smuzhiyun  * @ec_hdr: a &struct ubi_ec_hdr object where to store the read erase counter
708*4882a593Smuzhiyun  * header
709*4882a593Smuzhiyun  * @verbose: be verbose if the header is corrupted or was not found
710*4882a593Smuzhiyun  *
711*4882a593Smuzhiyun  * This function reads erase counter header from physical eraseblock @pnum and
712*4882a593Smuzhiyun  * stores it in @ec_hdr. This function also checks CRC checksum of the read
713*4882a593Smuzhiyun  * erase counter header. The following codes may be returned:
714*4882a593Smuzhiyun  *
715*4882a593Smuzhiyun  * o %0 if the CRC checksum is correct and the header was successfully read;
716*4882a593Smuzhiyun  * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected
717*4882a593Smuzhiyun  *   and corrected by the flash driver; this is harmless but may indicate that
718*4882a593Smuzhiyun  *   this eraseblock may become bad soon (but may be not);
719*4882a593Smuzhiyun  * o %UBI_IO_BAD_HDR if the erase counter header is corrupted (a CRC error);
720*4882a593Smuzhiyun  * o %UBI_IO_BAD_HDR_EBADMSG is the same as %UBI_IO_BAD_HDR, but there also was
721*4882a593Smuzhiyun  *   a data integrity error (uncorrectable ECC error in case of NAND);
722*4882a593Smuzhiyun  * o %UBI_IO_FF if only 0xFF bytes were read (the PEB is supposedly empty)
723*4882a593Smuzhiyun  * o a negative error code in case of failure.
724*4882a593Smuzhiyun  */
ubi_io_read_ec_hdr(struct ubi_device * ubi,int pnum,struct ubi_ec_hdr * ec_hdr,int verbose)725*4882a593Smuzhiyun int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
726*4882a593Smuzhiyun 		       struct ubi_ec_hdr *ec_hdr, int verbose)
727*4882a593Smuzhiyun {
728*4882a593Smuzhiyun 	int err, read_err;
729*4882a593Smuzhiyun 	uint32_t crc, magic, hdr_crc;
730*4882a593Smuzhiyun 
731*4882a593Smuzhiyun 	dbg_io("read EC header from PEB %d", pnum);
732*4882a593Smuzhiyun 	ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
733*4882a593Smuzhiyun 
734*4882a593Smuzhiyun 	read_err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE);
735*4882a593Smuzhiyun 	if (read_err) {
736*4882a593Smuzhiyun 		if (read_err != UBI_IO_BITFLIPS && !mtd_is_eccerr(read_err))
737*4882a593Smuzhiyun 			return read_err;
738*4882a593Smuzhiyun 
739*4882a593Smuzhiyun 		/*
740*4882a593Smuzhiyun 		 * We read all the data, but either a correctable bit-flip
741*4882a593Smuzhiyun 		 * occurred, or MTD reported a data integrity error
742*4882a593Smuzhiyun 		 * (uncorrectable ECC error in case of NAND). The former is
743*4882a593Smuzhiyun 		 * harmless, the later may mean that the read data is
744*4882a593Smuzhiyun 		 * corrupted. But we have a CRC check-sum and we will detect
745*4882a593Smuzhiyun 		 * this. If the EC header is still OK, we just report this as
746*4882a593Smuzhiyun 		 * there was a bit-flip, to force scrubbing.
747*4882a593Smuzhiyun 		 */
748*4882a593Smuzhiyun 	}
749*4882a593Smuzhiyun 
750*4882a593Smuzhiyun 	magic = be32_to_cpu(ec_hdr->magic);
751*4882a593Smuzhiyun 	if (magic != UBI_EC_HDR_MAGIC) {
752*4882a593Smuzhiyun 		if (mtd_is_eccerr(read_err))
753*4882a593Smuzhiyun 			return UBI_IO_BAD_HDR_EBADMSG;
754*4882a593Smuzhiyun 
755*4882a593Smuzhiyun 		/*
756*4882a593Smuzhiyun 		 * The magic field is wrong. Let's check if we have read all
757*4882a593Smuzhiyun 		 * 0xFF. If yes, this physical eraseblock is assumed to be
758*4882a593Smuzhiyun 		 * empty.
759*4882a593Smuzhiyun 		 */
760*4882a593Smuzhiyun 		if (ubi_check_pattern(ec_hdr, 0xFF, UBI_EC_HDR_SIZE)) {
761*4882a593Smuzhiyun 			/* The physical eraseblock is supposedly empty */
762*4882a593Smuzhiyun 			if (verbose)
763*4882a593Smuzhiyun 				ubi_warn(ubi, "no EC header found at PEB %d, only 0xFF bytes",
764*4882a593Smuzhiyun 					 pnum);
765*4882a593Smuzhiyun 			dbg_bld("no EC header found at PEB %d, only 0xFF bytes",
766*4882a593Smuzhiyun 				pnum);
767*4882a593Smuzhiyun 			if (!read_err)
768*4882a593Smuzhiyun 				return UBI_IO_FF;
769*4882a593Smuzhiyun 			else
770*4882a593Smuzhiyun 				return UBI_IO_FF_BITFLIPS;
771*4882a593Smuzhiyun 		}
772*4882a593Smuzhiyun 
773*4882a593Smuzhiyun 		/*
774*4882a593Smuzhiyun 		 * This is not a valid erase counter header, and these are not
775*4882a593Smuzhiyun 		 * 0xFF bytes. Report that the header is corrupted.
776*4882a593Smuzhiyun 		 */
777*4882a593Smuzhiyun 		if (verbose) {
778*4882a593Smuzhiyun 			ubi_warn(ubi, "bad magic number at PEB %d: %08x instead of %08x",
779*4882a593Smuzhiyun 				 pnum, magic, UBI_EC_HDR_MAGIC);
780*4882a593Smuzhiyun 			ubi_dump_ec_hdr(ec_hdr);
781*4882a593Smuzhiyun 		}
782*4882a593Smuzhiyun 		dbg_bld("bad magic number at PEB %d: %08x instead of %08x",
783*4882a593Smuzhiyun 			pnum, magic, UBI_EC_HDR_MAGIC);
784*4882a593Smuzhiyun 		return UBI_IO_BAD_HDR;
785*4882a593Smuzhiyun 	}
786*4882a593Smuzhiyun 
787*4882a593Smuzhiyun 	crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
788*4882a593Smuzhiyun 	hdr_crc = be32_to_cpu(ec_hdr->hdr_crc);
789*4882a593Smuzhiyun 
790*4882a593Smuzhiyun 	if (hdr_crc != crc) {
791*4882a593Smuzhiyun 		if (verbose) {
792*4882a593Smuzhiyun 			ubi_warn(ubi, "bad EC header CRC at PEB %d, calculated %#08x, read %#08x",
793*4882a593Smuzhiyun 				 pnum, crc, hdr_crc);
794*4882a593Smuzhiyun 			ubi_dump_ec_hdr(ec_hdr);
795*4882a593Smuzhiyun 		}
796*4882a593Smuzhiyun 		dbg_bld("bad EC header CRC at PEB %d, calculated %#08x, read %#08x",
797*4882a593Smuzhiyun 			pnum, crc, hdr_crc);
798*4882a593Smuzhiyun 
799*4882a593Smuzhiyun 		if (!read_err)
800*4882a593Smuzhiyun 			return UBI_IO_BAD_HDR;
801*4882a593Smuzhiyun 		else
802*4882a593Smuzhiyun 			return UBI_IO_BAD_HDR_EBADMSG;
803*4882a593Smuzhiyun 	}
804*4882a593Smuzhiyun 
805*4882a593Smuzhiyun 	/* And of course validate what has just been read from the media */
806*4882a593Smuzhiyun 	err = validate_ec_hdr(ubi, ec_hdr);
807*4882a593Smuzhiyun 	if (err) {
808*4882a593Smuzhiyun 		ubi_err(ubi, "validation failed for PEB %d", pnum);
809*4882a593Smuzhiyun 		return -EINVAL;
810*4882a593Smuzhiyun 	}
811*4882a593Smuzhiyun 
812*4882a593Smuzhiyun 	/*
813*4882a593Smuzhiyun 	 * If there was %-EBADMSG, but the header CRC is still OK, report about
814*4882a593Smuzhiyun 	 * a bit-flip to force scrubbing on this PEB.
815*4882a593Smuzhiyun 	 */
816*4882a593Smuzhiyun 	return read_err ? UBI_IO_BITFLIPS : 0;
817*4882a593Smuzhiyun }
818*4882a593Smuzhiyun 
819*4882a593Smuzhiyun /**
820*4882a593Smuzhiyun  * ubi_io_write_ec_hdr - write an erase counter header.
821*4882a593Smuzhiyun  * @ubi: UBI device description object
822*4882a593Smuzhiyun  * @pnum: physical eraseblock to write to
823*4882a593Smuzhiyun  * @ec_hdr: the erase counter header to write
824*4882a593Smuzhiyun  *
825*4882a593Smuzhiyun  * This function writes erase counter header described by @ec_hdr to physical
826*4882a593Smuzhiyun  * eraseblock @pnum. It also fills most fields of @ec_hdr before writing, so
827*4882a593Smuzhiyun  * the caller do not have to fill them. Callers must only fill the @ec_hdr->ec
828*4882a593Smuzhiyun  * field.
829*4882a593Smuzhiyun  *
830*4882a593Smuzhiyun  * This function returns zero in case of success and a negative error code in
831*4882a593Smuzhiyun  * case of failure. If %-EIO is returned, the physical eraseblock most probably
832*4882a593Smuzhiyun  * went bad.
833*4882a593Smuzhiyun  */
ubi_io_write_ec_hdr(struct ubi_device * ubi,int pnum,struct ubi_ec_hdr * ec_hdr)834*4882a593Smuzhiyun int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum,
835*4882a593Smuzhiyun 			struct ubi_ec_hdr *ec_hdr)
836*4882a593Smuzhiyun {
837*4882a593Smuzhiyun 	int err;
838*4882a593Smuzhiyun 	uint32_t crc;
839*4882a593Smuzhiyun 
840*4882a593Smuzhiyun 	dbg_io("write EC header to PEB %d", pnum);
841*4882a593Smuzhiyun 	ubi_assert(pnum >= 0 &&  pnum < ubi->peb_count);
842*4882a593Smuzhiyun 
843*4882a593Smuzhiyun 	ec_hdr->magic = cpu_to_be32(UBI_EC_HDR_MAGIC);
844*4882a593Smuzhiyun 	ec_hdr->version = UBI_VERSION;
845*4882a593Smuzhiyun 	ec_hdr->vid_hdr_offset = cpu_to_be32(ubi->vid_hdr_offset);
846*4882a593Smuzhiyun 	ec_hdr->data_offset = cpu_to_be32(ubi->leb_start);
847*4882a593Smuzhiyun 	ec_hdr->image_seq = cpu_to_be32(ubi->image_seq);
848*4882a593Smuzhiyun 	crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
849*4882a593Smuzhiyun 	ec_hdr->hdr_crc = cpu_to_be32(crc);
850*4882a593Smuzhiyun 
851*4882a593Smuzhiyun 	err = self_check_ec_hdr(ubi, pnum, ec_hdr);
852*4882a593Smuzhiyun 	if (err)
853*4882a593Smuzhiyun 		return err;
854*4882a593Smuzhiyun 
855*4882a593Smuzhiyun 	if (ubi_dbg_power_cut(ubi, POWER_CUT_EC_WRITE))
856*4882a593Smuzhiyun 		return -EROFS;
857*4882a593Smuzhiyun 
858*4882a593Smuzhiyun 	err = ubi_io_write(ubi, ec_hdr, pnum, 0, ubi->ec_hdr_alsize);
859*4882a593Smuzhiyun 	return err;
860*4882a593Smuzhiyun }
861*4882a593Smuzhiyun 
862*4882a593Smuzhiyun /**
863*4882a593Smuzhiyun  * validate_vid_hdr - validate a volume identifier header.
864*4882a593Smuzhiyun  * @ubi: UBI device description object
865*4882a593Smuzhiyun  * @vid_hdr: the volume identifier header to check
866*4882a593Smuzhiyun  *
867*4882a593Smuzhiyun  * This function checks that data stored in the volume identifier header
868*4882a593Smuzhiyun  * @vid_hdr. Returns zero if the VID header is OK and %1 if not.
869*4882a593Smuzhiyun  */
validate_vid_hdr(const struct ubi_device * ubi,const struct ubi_vid_hdr * vid_hdr)870*4882a593Smuzhiyun static int validate_vid_hdr(const struct ubi_device *ubi,
871*4882a593Smuzhiyun 			    const struct ubi_vid_hdr *vid_hdr)
872*4882a593Smuzhiyun {
873*4882a593Smuzhiyun 	int vol_type = vid_hdr->vol_type;
874*4882a593Smuzhiyun 	int copy_flag = vid_hdr->copy_flag;
875*4882a593Smuzhiyun 	int vol_id = be32_to_cpu(vid_hdr->vol_id);
876*4882a593Smuzhiyun 	int lnum = be32_to_cpu(vid_hdr->lnum);
877*4882a593Smuzhiyun 	int compat = vid_hdr->compat;
878*4882a593Smuzhiyun 	int data_size = be32_to_cpu(vid_hdr->data_size);
879*4882a593Smuzhiyun 	int used_ebs = be32_to_cpu(vid_hdr->used_ebs);
880*4882a593Smuzhiyun 	int data_pad = be32_to_cpu(vid_hdr->data_pad);
881*4882a593Smuzhiyun 	int data_crc = be32_to_cpu(vid_hdr->data_crc);
882*4882a593Smuzhiyun 	int usable_leb_size = ubi->leb_size - data_pad;
883*4882a593Smuzhiyun 
884*4882a593Smuzhiyun 	if (copy_flag != 0 && copy_flag != 1) {
885*4882a593Smuzhiyun 		ubi_err(ubi, "bad copy_flag");
886*4882a593Smuzhiyun 		goto bad;
887*4882a593Smuzhiyun 	}
888*4882a593Smuzhiyun 
889*4882a593Smuzhiyun 	if (vol_id < 0 || lnum < 0 || data_size < 0 || used_ebs < 0 ||
890*4882a593Smuzhiyun 	    data_pad < 0) {
891*4882a593Smuzhiyun 		ubi_err(ubi, "negative values");
892*4882a593Smuzhiyun 		goto bad;
893*4882a593Smuzhiyun 	}
894*4882a593Smuzhiyun 
895*4882a593Smuzhiyun 	if (vol_id >= UBI_MAX_VOLUMES && vol_id < UBI_INTERNAL_VOL_START) {
896*4882a593Smuzhiyun 		ubi_err(ubi, "bad vol_id");
897*4882a593Smuzhiyun 		goto bad;
898*4882a593Smuzhiyun 	}
899*4882a593Smuzhiyun 
900*4882a593Smuzhiyun 	if (vol_id < UBI_INTERNAL_VOL_START && compat != 0) {
901*4882a593Smuzhiyun 		ubi_err(ubi, "bad compat");
902*4882a593Smuzhiyun 		goto bad;
903*4882a593Smuzhiyun 	}
904*4882a593Smuzhiyun 
905*4882a593Smuzhiyun 	if (vol_id >= UBI_INTERNAL_VOL_START && compat != UBI_COMPAT_DELETE &&
906*4882a593Smuzhiyun 	    compat != UBI_COMPAT_RO && compat != UBI_COMPAT_PRESERVE &&
907*4882a593Smuzhiyun 	    compat != UBI_COMPAT_REJECT) {
908*4882a593Smuzhiyun 		ubi_err(ubi, "bad compat");
909*4882a593Smuzhiyun 		goto bad;
910*4882a593Smuzhiyun 	}
911*4882a593Smuzhiyun 
912*4882a593Smuzhiyun 	if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) {
913*4882a593Smuzhiyun 		ubi_err(ubi, "bad vol_type");
914*4882a593Smuzhiyun 		goto bad;
915*4882a593Smuzhiyun 	}
916*4882a593Smuzhiyun 
917*4882a593Smuzhiyun 	if (data_pad >= ubi->leb_size / 2) {
918*4882a593Smuzhiyun 		ubi_err(ubi, "bad data_pad");
919*4882a593Smuzhiyun 		goto bad;
920*4882a593Smuzhiyun 	}
921*4882a593Smuzhiyun 
922*4882a593Smuzhiyun 	if (vol_type == UBI_VID_STATIC) {
923*4882a593Smuzhiyun 		/*
924*4882a593Smuzhiyun 		 * Although from high-level point of view static volumes may
925*4882a593Smuzhiyun 		 * contain zero bytes of data, but no VID headers can contain
926*4882a593Smuzhiyun 		 * zero at these fields, because they empty volumes do not have
927*4882a593Smuzhiyun 		 * mapped logical eraseblocks.
928*4882a593Smuzhiyun 		 */
929*4882a593Smuzhiyun 		if (used_ebs == 0) {
930*4882a593Smuzhiyun 			ubi_err(ubi, "zero used_ebs");
931*4882a593Smuzhiyun 			goto bad;
932*4882a593Smuzhiyun 		}
933*4882a593Smuzhiyun 		if (data_size == 0) {
934*4882a593Smuzhiyun 			ubi_err(ubi, "zero data_size");
935*4882a593Smuzhiyun 			goto bad;
936*4882a593Smuzhiyun 		}
937*4882a593Smuzhiyun 		if (lnum < used_ebs - 1) {
938*4882a593Smuzhiyun 			if (data_size != usable_leb_size) {
939*4882a593Smuzhiyun 				ubi_err(ubi, "bad data_size");
940*4882a593Smuzhiyun 				goto bad;
941*4882a593Smuzhiyun 			}
942*4882a593Smuzhiyun 		} else if (lnum == used_ebs - 1) {
943*4882a593Smuzhiyun 			if (data_size == 0) {
944*4882a593Smuzhiyun 				ubi_err(ubi, "bad data_size at last LEB");
945*4882a593Smuzhiyun 				goto bad;
946*4882a593Smuzhiyun 			}
947*4882a593Smuzhiyun 		} else {
948*4882a593Smuzhiyun 			ubi_err(ubi, "too high lnum");
949*4882a593Smuzhiyun 			goto bad;
950*4882a593Smuzhiyun 		}
951*4882a593Smuzhiyun 	} else {
952*4882a593Smuzhiyun 		if (copy_flag == 0) {
953*4882a593Smuzhiyun 			if (data_crc != 0) {
954*4882a593Smuzhiyun 				ubi_err(ubi, "non-zero data CRC");
955*4882a593Smuzhiyun 				goto bad;
956*4882a593Smuzhiyun 			}
957*4882a593Smuzhiyun 			if (data_size != 0) {
958*4882a593Smuzhiyun 				ubi_err(ubi, "non-zero data_size");
959*4882a593Smuzhiyun 				goto bad;
960*4882a593Smuzhiyun 			}
961*4882a593Smuzhiyun 		} else {
962*4882a593Smuzhiyun 			if (data_size == 0) {
963*4882a593Smuzhiyun 				ubi_err(ubi, "zero data_size of copy");
964*4882a593Smuzhiyun 				goto bad;
965*4882a593Smuzhiyun 			}
966*4882a593Smuzhiyun 		}
967*4882a593Smuzhiyun 		if (used_ebs != 0) {
968*4882a593Smuzhiyun 			ubi_err(ubi, "bad used_ebs");
969*4882a593Smuzhiyun 			goto bad;
970*4882a593Smuzhiyun 		}
971*4882a593Smuzhiyun 	}
972*4882a593Smuzhiyun 
973*4882a593Smuzhiyun 	return 0;
974*4882a593Smuzhiyun 
975*4882a593Smuzhiyun bad:
976*4882a593Smuzhiyun 	ubi_err(ubi, "bad VID header");
977*4882a593Smuzhiyun 	ubi_dump_vid_hdr(vid_hdr);
978*4882a593Smuzhiyun 	dump_stack();
979*4882a593Smuzhiyun 	return 1;
980*4882a593Smuzhiyun }
981*4882a593Smuzhiyun 
982*4882a593Smuzhiyun /**
983*4882a593Smuzhiyun  * ubi_io_read_vid_hdr - read and check a volume identifier header.
984*4882a593Smuzhiyun  * @ubi: UBI device description object
985*4882a593Smuzhiyun  * @pnum: physical eraseblock number to read from
986*4882a593Smuzhiyun  * @vid_hdr: &struct ubi_vid_hdr object where to store the read volume
987*4882a593Smuzhiyun  * identifier header
988*4882a593Smuzhiyun  * @verbose: be verbose if the header is corrupted or wasn't found
989*4882a593Smuzhiyun  *
990*4882a593Smuzhiyun  * This function reads the volume identifier header from physical eraseblock
991*4882a593Smuzhiyun  * @pnum and stores it in @vid_hdr. It also checks CRC checksum of the read
992*4882a593Smuzhiyun  * volume identifier header. The error codes are the same as in
993*4882a593Smuzhiyun  * 'ubi_io_read_ec_hdr()'.
994*4882a593Smuzhiyun  *
995*4882a593Smuzhiyun  * Note, the implementation of this function is also very similar to
996*4882a593Smuzhiyun  * 'ubi_io_read_ec_hdr()', so refer commentaries in 'ubi_io_read_ec_hdr()'.
997*4882a593Smuzhiyun  */
ubi_io_read_vid_hdr(struct ubi_device * ubi,int pnum,struct ubi_vid_hdr * vid_hdr,int verbose)998*4882a593Smuzhiyun int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
999*4882a593Smuzhiyun 			struct ubi_vid_hdr *vid_hdr, int verbose)
1000*4882a593Smuzhiyun {
1001*4882a593Smuzhiyun 	int err, read_err;
1002*4882a593Smuzhiyun 	uint32_t crc, magic, hdr_crc;
1003*4882a593Smuzhiyun 	void *p;
1004*4882a593Smuzhiyun 
1005*4882a593Smuzhiyun 	dbg_io("read VID header from PEB %d", pnum);
1006*4882a593Smuzhiyun 	ubi_assert(pnum >= 0 &&  pnum < ubi->peb_count);
1007*4882a593Smuzhiyun 
1008*4882a593Smuzhiyun 	p = (char *)vid_hdr - ubi->vid_hdr_shift;
1009*4882a593Smuzhiyun 	read_err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset,
1010*4882a593Smuzhiyun 			  ubi->vid_hdr_alsize);
1011*4882a593Smuzhiyun 	if (read_err && read_err != UBI_IO_BITFLIPS && !mtd_is_eccerr(read_err))
1012*4882a593Smuzhiyun 		return read_err;
1013*4882a593Smuzhiyun 
1014*4882a593Smuzhiyun 	magic = be32_to_cpu(vid_hdr->magic);
1015*4882a593Smuzhiyun 	if (magic != UBI_VID_HDR_MAGIC) {
1016*4882a593Smuzhiyun 		if (mtd_is_eccerr(read_err))
1017*4882a593Smuzhiyun 			return UBI_IO_BAD_HDR_EBADMSG;
1018*4882a593Smuzhiyun 
1019*4882a593Smuzhiyun 		if (ubi_check_pattern(vid_hdr, 0xFF, UBI_VID_HDR_SIZE)) {
1020*4882a593Smuzhiyun 			if (verbose)
1021*4882a593Smuzhiyun 				ubi_warn(ubi, "no VID header found at PEB %d, only 0xFF bytes",
1022*4882a593Smuzhiyun 					 pnum);
1023*4882a593Smuzhiyun 			dbg_bld("no VID header found at PEB %d, only 0xFF bytes",
1024*4882a593Smuzhiyun 				pnum);
1025*4882a593Smuzhiyun 			if (!read_err)
1026*4882a593Smuzhiyun 				return UBI_IO_FF;
1027*4882a593Smuzhiyun 			else
1028*4882a593Smuzhiyun 				return UBI_IO_FF_BITFLIPS;
1029*4882a593Smuzhiyun 		}
1030*4882a593Smuzhiyun 
1031*4882a593Smuzhiyun 		if (verbose) {
1032*4882a593Smuzhiyun 			ubi_warn(ubi, "bad magic number at PEB %d: %08x instead of %08x",
1033*4882a593Smuzhiyun 				 pnum, magic, UBI_VID_HDR_MAGIC);
1034*4882a593Smuzhiyun 			ubi_dump_vid_hdr(vid_hdr);
1035*4882a593Smuzhiyun 		}
1036*4882a593Smuzhiyun 		dbg_bld("bad magic number at PEB %d: %08x instead of %08x",
1037*4882a593Smuzhiyun 			pnum, magic, UBI_VID_HDR_MAGIC);
1038*4882a593Smuzhiyun 		return UBI_IO_BAD_HDR;
1039*4882a593Smuzhiyun 	}
1040*4882a593Smuzhiyun 
1041*4882a593Smuzhiyun 	crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC);
1042*4882a593Smuzhiyun 	hdr_crc = be32_to_cpu(vid_hdr->hdr_crc);
1043*4882a593Smuzhiyun 
1044*4882a593Smuzhiyun 	if (hdr_crc != crc) {
1045*4882a593Smuzhiyun 		if (verbose) {
1046*4882a593Smuzhiyun 			ubi_warn(ubi, "bad CRC at PEB %d, calculated %#08x, read %#08x",
1047*4882a593Smuzhiyun 				 pnum, crc, hdr_crc);
1048*4882a593Smuzhiyun 			ubi_dump_vid_hdr(vid_hdr);
1049*4882a593Smuzhiyun 		}
1050*4882a593Smuzhiyun 		dbg_bld("bad CRC at PEB %d, calculated %#08x, read %#08x",
1051*4882a593Smuzhiyun 			pnum, crc, hdr_crc);
1052*4882a593Smuzhiyun 		if (!read_err)
1053*4882a593Smuzhiyun 			return UBI_IO_BAD_HDR;
1054*4882a593Smuzhiyun 		else
1055*4882a593Smuzhiyun 			return UBI_IO_BAD_HDR_EBADMSG;
1056*4882a593Smuzhiyun 	}
1057*4882a593Smuzhiyun 
1058*4882a593Smuzhiyun 	err = validate_vid_hdr(ubi, vid_hdr);
1059*4882a593Smuzhiyun 	if (err) {
1060*4882a593Smuzhiyun 		ubi_err(ubi, "validation failed for PEB %d", pnum);
1061*4882a593Smuzhiyun 		return -EINVAL;
1062*4882a593Smuzhiyun 	}
1063*4882a593Smuzhiyun 
1064*4882a593Smuzhiyun 	return read_err ? UBI_IO_BITFLIPS : 0;
1065*4882a593Smuzhiyun }
1066*4882a593Smuzhiyun 
1067*4882a593Smuzhiyun /**
1068*4882a593Smuzhiyun  * ubi_io_write_vid_hdr - write a volume identifier header.
1069*4882a593Smuzhiyun  * @ubi: UBI device description object
1070*4882a593Smuzhiyun  * @pnum: the physical eraseblock number to write to
1071*4882a593Smuzhiyun  * @vid_hdr: the volume identifier header to write
1072*4882a593Smuzhiyun  *
1073*4882a593Smuzhiyun  * This function writes the volume identifier header described by @vid_hdr to
1074*4882a593Smuzhiyun  * physical eraseblock @pnum. This function automatically fills the
1075*4882a593Smuzhiyun  * @vid_hdr->magic and the @vid_hdr->version fields, as well as calculates
1076*4882a593Smuzhiyun  * header CRC checksum and stores it at vid_hdr->hdr_crc.
1077*4882a593Smuzhiyun  *
1078*4882a593Smuzhiyun  * This function returns zero in case of success and a negative error code in
1079*4882a593Smuzhiyun  * case of failure. If %-EIO is returned, the physical eraseblock probably went
1080*4882a593Smuzhiyun  * bad.
1081*4882a593Smuzhiyun  */
ubi_io_write_vid_hdr(struct ubi_device * ubi,int pnum,struct ubi_vid_hdr * vid_hdr)1082*4882a593Smuzhiyun int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
1083*4882a593Smuzhiyun 			 struct ubi_vid_hdr *vid_hdr)
1084*4882a593Smuzhiyun {
1085*4882a593Smuzhiyun 	int err;
1086*4882a593Smuzhiyun 	uint32_t crc;
1087*4882a593Smuzhiyun 	void *p;
1088*4882a593Smuzhiyun 
1089*4882a593Smuzhiyun 	dbg_io("write VID header to PEB %d", pnum);
1090*4882a593Smuzhiyun 	ubi_assert(pnum >= 0 &&  pnum < ubi->peb_count);
1091*4882a593Smuzhiyun 
1092*4882a593Smuzhiyun 	err = self_check_peb_ec_hdr(ubi, pnum);
1093*4882a593Smuzhiyun 	if (err)
1094*4882a593Smuzhiyun 		return err;
1095*4882a593Smuzhiyun 
1096*4882a593Smuzhiyun 	vid_hdr->magic = cpu_to_be32(UBI_VID_HDR_MAGIC);
1097*4882a593Smuzhiyun 	vid_hdr->version = UBI_VERSION;
1098*4882a593Smuzhiyun 	crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC);
1099*4882a593Smuzhiyun 	vid_hdr->hdr_crc = cpu_to_be32(crc);
1100*4882a593Smuzhiyun 
1101*4882a593Smuzhiyun 	err = self_check_vid_hdr(ubi, pnum, vid_hdr);
1102*4882a593Smuzhiyun 	if (err)
1103*4882a593Smuzhiyun 		return err;
1104*4882a593Smuzhiyun 
1105*4882a593Smuzhiyun 	if (ubi_dbg_power_cut(ubi, POWER_CUT_VID_WRITE))
1106*4882a593Smuzhiyun 		return -EROFS;
1107*4882a593Smuzhiyun 
1108*4882a593Smuzhiyun 	p = (char *)vid_hdr - ubi->vid_hdr_shift;
1109*4882a593Smuzhiyun 	err = ubi_io_write(ubi, p, pnum, ubi->vid_hdr_aloffset,
1110*4882a593Smuzhiyun 			   ubi->vid_hdr_alsize);
1111*4882a593Smuzhiyun 	return err;
1112*4882a593Smuzhiyun }
1113*4882a593Smuzhiyun 
1114*4882a593Smuzhiyun /**
1115*4882a593Smuzhiyun  * self_check_not_bad - ensure that a physical eraseblock is not bad.
1116*4882a593Smuzhiyun  * @ubi: UBI device description object
1117*4882a593Smuzhiyun  * @pnum: physical eraseblock number to check
1118*4882a593Smuzhiyun  *
1119*4882a593Smuzhiyun  * This function returns zero if the physical eraseblock is good, %-EINVAL if
1120*4882a593Smuzhiyun  * it is bad and a negative error code if an error occurred.
1121*4882a593Smuzhiyun  */
self_check_not_bad(const struct ubi_device * ubi,int pnum)1122*4882a593Smuzhiyun static int self_check_not_bad(const struct ubi_device *ubi, int pnum)
1123*4882a593Smuzhiyun {
1124*4882a593Smuzhiyun 	int err;
1125*4882a593Smuzhiyun 
1126*4882a593Smuzhiyun 	if (!ubi_dbg_chk_io(ubi))
1127*4882a593Smuzhiyun 		return 0;
1128*4882a593Smuzhiyun 
1129*4882a593Smuzhiyun 	err = ubi_io_is_bad(ubi, pnum);
1130*4882a593Smuzhiyun 	if (!err)
1131*4882a593Smuzhiyun 		return err;
1132*4882a593Smuzhiyun 
1133*4882a593Smuzhiyun 	ubi_err(ubi, "self-check failed for PEB %d", pnum);
1134*4882a593Smuzhiyun 	dump_stack();
1135*4882a593Smuzhiyun 	return err > 0 ? -EINVAL : err;
1136*4882a593Smuzhiyun }
1137*4882a593Smuzhiyun 
1138*4882a593Smuzhiyun /**
1139*4882a593Smuzhiyun  * self_check_ec_hdr - check if an erase counter header is all right.
1140*4882a593Smuzhiyun  * @ubi: UBI device description object
1141*4882a593Smuzhiyun  * @pnum: physical eraseblock number the erase counter header belongs to
1142*4882a593Smuzhiyun  * @ec_hdr: the erase counter header to check
1143*4882a593Smuzhiyun  *
1144*4882a593Smuzhiyun  * This function returns zero if the erase counter header contains valid
1145*4882a593Smuzhiyun  * values, and %-EINVAL if not.
1146*4882a593Smuzhiyun  */
self_check_ec_hdr(const struct ubi_device * ubi,int pnum,const struct ubi_ec_hdr * ec_hdr)1147*4882a593Smuzhiyun static int self_check_ec_hdr(const struct ubi_device *ubi, int pnum,
1148*4882a593Smuzhiyun 			     const struct ubi_ec_hdr *ec_hdr)
1149*4882a593Smuzhiyun {
1150*4882a593Smuzhiyun 	int err;
1151*4882a593Smuzhiyun 	uint32_t magic;
1152*4882a593Smuzhiyun 
1153*4882a593Smuzhiyun 	if (!ubi_dbg_chk_io(ubi))
1154*4882a593Smuzhiyun 		return 0;
1155*4882a593Smuzhiyun 
1156*4882a593Smuzhiyun 	magic = be32_to_cpu(ec_hdr->magic);
1157*4882a593Smuzhiyun 	if (magic != UBI_EC_HDR_MAGIC) {
1158*4882a593Smuzhiyun 		ubi_err(ubi, "bad magic %#08x, must be %#08x",
1159*4882a593Smuzhiyun 			magic, UBI_EC_HDR_MAGIC);
1160*4882a593Smuzhiyun 		goto fail;
1161*4882a593Smuzhiyun 	}
1162*4882a593Smuzhiyun 
1163*4882a593Smuzhiyun 	err = validate_ec_hdr(ubi, ec_hdr);
1164*4882a593Smuzhiyun 	if (err) {
1165*4882a593Smuzhiyun 		ubi_err(ubi, "self-check failed for PEB %d", pnum);
1166*4882a593Smuzhiyun 		goto fail;
1167*4882a593Smuzhiyun 	}
1168*4882a593Smuzhiyun 
1169*4882a593Smuzhiyun 	return 0;
1170*4882a593Smuzhiyun 
1171*4882a593Smuzhiyun fail:
1172*4882a593Smuzhiyun 	ubi_dump_ec_hdr(ec_hdr);
1173*4882a593Smuzhiyun 	dump_stack();
1174*4882a593Smuzhiyun 	return -EINVAL;
1175*4882a593Smuzhiyun }
1176*4882a593Smuzhiyun 
1177*4882a593Smuzhiyun /**
1178*4882a593Smuzhiyun  * self_check_peb_ec_hdr - check erase counter header.
1179*4882a593Smuzhiyun  * @ubi: UBI device description object
1180*4882a593Smuzhiyun  * @pnum: the physical eraseblock number to check
1181*4882a593Smuzhiyun  *
1182*4882a593Smuzhiyun  * This function returns zero if the erase counter header is all right and and
1183*4882a593Smuzhiyun  * a negative error code if not or if an error occurred.
1184*4882a593Smuzhiyun  */
self_check_peb_ec_hdr(const struct ubi_device * ubi,int pnum)1185*4882a593Smuzhiyun static int self_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum)
1186*4882a593Smuzhiyun {
1187*4882a593Smuzhiyun 	int err;
1188*4882a593Smuzhiyun 	uint32_t crc, hdr_crc;
1189*4882a593Smuzhiyun 	struct ubi_ec_hdr *ec_hdr;
1190*4882a593Smuzhiyun 
1191*4882a593Smuzhiyun 	if (!ubi_dbg_chk_io(ubi))
1192*4882a593Smuzhiyun 		return 0;
1193*4882a593Smuzhiyun 
1194*4882a593Smuzhiyun 	ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
1195*4882a593Smuzhiyun 	if (!ec_hdr)
1196*4882a593Smuzhiyun 		return -ENOMEM;
1197*4882a593Smuzhiyun 
1198*4882a593Smuzhiyun 	err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE);
1199*4882a593Smuzhiyun 	if (err && err != UBI_IO_BITFLIPS && !mtd_is_eccerr(err))
1200*4882a593Smuzhiyun 		goto exit;
1201*4882a593Smuzhiyun 
1202*4882a593Smuzhiyun 	crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
1203*4882a593Smuzhiyun 	hdr_crc = be32_to_cpu(ec_hdr->hdr_crc);
1204*4882a593Smuzhiyun 	if (hdr_crc != crc) {
1205*4882a593Smuzhiyun 		ubi_err(ubi, "bad CRC, calculated %#08x, read %#08x",
1206*4882a593Smuzhiyun 			crc, hdr_crc);
1207*4882a593Smuzhiyun 		ubi_err(ubi, "self-check failed for PEB %d", pnum);
1208*4882a593Smuzhiyun 		ubi_dump_ec_hdr(ec_hdr);
1209*4882a593Smuzhiyun 		dump_stack();
1210*4882a593Smuzhiyun 		err = -EINVAL;
1211*4882a593Smuzhiyun 		goto exit;
1212*4882a593Smuzhiyun 	}
1213*4882a593Smuzhiyun 
1214*4882a593Smuzhiyun 	err = self_check_ec_hdr(ubi, pnum, ec_hdr);
1215*4882a593Smuzhiyun 
1216*4882a593Smuzhiyun exit:
1217*4882a593Smuzhiyun 	kfree(ec_hdr);
1218*4882a593Smuzhiyun 	return err;
1219*4882a593Smuzhiyun }
1220*4882a593Smuzhiyun 
1221*4882a593Smuzhiyun /**
1222*4882a593Smuzhiyun  * self_check_vid_hdr - check that a volume identifier header is all right.
1223*4882a593Smuzhiyun  * @ubi: UBI device description object
1224*4882a593Smuzhiyun  * @pnum: physical eraseblock number the volume identifier header belongs to
1225*4882a593Smuzhiyun  * @vid_hdr: the volume identifier header to check
1226*4882a593Smuzhiyun  *
1227*4882a593Smuzhiyun  * This function returns zero if the volume identifier header is all right, and
1228*4882a593Smuzhiyun  * %-EINVAL if not.
1229*4882a593Smuzhiyun  */
self_check_vid_hdr(const struct ubi_device * ubi,int pnum,const struct ubi_vid_hdr * vid_hdr)1230*4882a593Smuzhiyun static int self_check_vid_hdr(const struct ubi_device *ubi, int pnum,
1231*4882a593Smuzhiyun 			      const struct ubi_vid_hdr *vid_hdr)
1232*4882a593Smuzhiyun {
1233*4882a593Smuzhiyun 	int err;
1234*4882a593Smuzhiyun 	uint32_t magic;
1235*4882a593Smuzhiyun 
1236*4882a593Smuzhiyun 	if (!ubi_dbg_chk_io(ubi))
1237*4882a593Smuzhiyun 		return 0;
1238*4882a593Smuzhiyun 
1239*4882a593Smuzhiyun 	magic = be32_to_cpu(vid_hdr->magic);
1240*4882a593Smuzhiyun 	if (magic != UBI_VID_HDR_MAGIC) {
1241*4882a593Smuzhiyun 		ubi_err(ubi, "bad VID header magic %#08x at PEB %d, must be %#08x",
1242*4882a593Smuzhiyun 			magic, pnum, UBI_VID_HDR_MAGIC);
1243*4882a593Smuzhiyun 		goto fail;
1244*4882a593Smuzhiyun 	}
1245*4882a593Smuzhiyun 
1246*4882a593Smuzhiyun 	err = validate_vid_hdr(ubi, vid_hdr);
1247*4882a593Smuzhiyun 	if (err) {
1248*4882a593Smuzhiyun 		ubi_err(ubi, "self-check failed for PEB %d", pnum);
1249*4882a593Smuzhiyun 		goto fail;
1250*4882a593Smuzhiyun 	}
1251*4882a593Smuzhiyun 
1252*4882a593Smuzhiyun 	return err;
1253*4882a593Smuzhiyun 
1254*4882a593Smuzhiyun fail:
1255*4882a593Smuzhiyun 	ubi_err(ubi, "self-check failed for PEB %d", pnum);
1256*4882a593Smuzhiyun 	ubi_dump_vid_hdr(vid_hdr);
1257*4882a593Smuzhiyun 	dump_stack();
1258*4882a593Smuzhiyun 	return -EINVAL;
1259*4882a593Smuzhiyun 
1260*4882a593Smuzhiyun }
1261*4882a593Smuzhiyun 
1262*4882a593Smuzhiyun /**
1263*4882a593Smuzhiyun  * self_check_peb_vid_hdr - check volume identifier header.
1264*4882a593Smuzhiyun  * @ubi: UBI device description object
1265*4882a593Smuzhiyun  * @pnum: the physical eraseblock number to check
1266*4882a593Smuzhiyun  *
1267*4882a593Smuzhiyun  * This function returns zero if the volume identifier header is all right,
1268*4882a593Smuzhiyun  * and a negative error code if not or if an error occurred.
1269*4882a593Smuzhiyun  */
self_check_peb_vid_hdr(const struct ubi_device * ubi,int pnum)1270*4882a593Smuzhiyun static int self_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum)
1271*4882a593Smuzhiyun {
1272*4882a593Smuzhiyun 	int err;
1273*4882a593Smuzhiyun 	uint32_t crc, hdr_crc;
1274*4882a593Smuzhiyun 	struct ubi_vid_hdr *vid_hdr;
1275*4882a593Smuzhiyun 	void *p;
1276*4882a593Smuzhiyun 
1277*4882a593Smuzhiyun 	if (!ubi_dbg_chk_io(ubi))
1278*4882a593Smuzhiyun 		return 0;
1279*4882a593Smuzhiyun 
1280*4882a593Smuzhiyun 	vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
1281*4882a593Smuzhiyun 	if (!vid_hdr)
1282*4882a593Smuzhiyun 		return -ENOMEM;
1283*4882a593Smuzhiyun 
1284*4882a593Smuzhiyun 	p = (char *)vid_hdr - ubi->vid_hdr_shift;
1285*4882a593Smuzhiyun 	err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset,
1286*4882a593Smuzhiyun 			  ubi->vid_hdr_alsize);
1287*4882a593Smuzhiyun 	if (err && err != UBI_IO_BITFLIPS && !mtd_is_eccerr(err))
1288*4882a593Smuzhiyun 		goto exit;
1289*4882a593Smuzhiyun 
1290*4882a593Smuzhiyun 	crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_EC_HDR_SIZE_CRC);
1291*4882a593Smuzhiyun 	hdr_crc = be32_to_cpu(vid_hdr->hdr_crc);
1292*4882a593Smuzhiyun 	if (hdr_crc != crc) {
1293*4882a593Smuzhiyun 		ubi_err(ubi, "bad VID header CRC at PEB %d, calculated %#08x, read %#08x",
1294*4882a593Smuzhiyun 			pnum, crc, hdr_crc);
1295*4882a593Smuzhiyun 		ubi_err(ubi, "self-check failed for PEB %d", pnum);
1296*4882a593Smuzhiyun 		ubi_dump_vid_hdr(vid_hdr);
1297*4882a593Smuzhiyun 		dump_stack();
1298*4882a593Smuzhiyun 		err = -EINVAL;
1299*4882a593Smuzhiyun 		goto exit;
1300*4882a593Smuzhiyun 	}
1301*4882a593Smuzhiyun 
1302*4882a593Smuzhiyun 	err = self_check_vid_hdr(ubi, pnum, vid_hdr);
1303*4882a593Smuzhiyun 
1304*4882a593Smuzhiyun exit:
1305*4882a593Smuzhiyun 	ubi_free_vid_hdr(ubi, vid_hdr);
1306*4882a593Smuzhiyun 	return err;
1307*4882a593Smuzhiyun }
1308*4882a593Smuzhiyun 
1309*4882a593Smuzhiyun /**
1310*4882a593Smuzhiyun  * self_check_write - make sure write succeeded.
1311*4882a593Smuzhiyun  * @ubi: UBI device description object
1312*4882a593Smuzhiyun  * @buf: buffer with data which were written
1313*4882a593Smuzhiyun  * @pnum: physical eraseblock number the data were written to
1314*4882a593Smuzhiyun  * @offset: offset within the physical eraseblock the data were written to
1315*4882a593Smuzhiyun  * @len: how many bytes were written
1316*4882a593Smuzhiyun  *
1317*4882a593Smuzhiyun  * This functions reads data which were recently written and compares it with
1318*4882a593Smuzhiyun  * the original data buffer - the data have to match. Returns zero if the data
1319*4882a593Smuzhiyun  * match and a negative error code if not or in case of failure.
1320*4882a593Smuzhiyun  */
self_check_write(struct ubi_device * ubi,const void * buf,int pnum,int offset,int len)1321*4882a593Smuzhiyun static int self_check_write(struct ubi_device *ubi, const void *buf, int pnum,
1322*4882a593Smuzhiyun 			    int offset, int len)
1323*4882a593Smuzhiyun {
1324*4882a593Smuzhiyun 	int err, i;
1325*4882a593Smuzhiyun 	size_t read;
1326*4882a593Smuzhiyun 	void *buf1;
1327*4882a593Smuzhiyun 	loff_t addr = (loff_t)pnum * ubi->peb_size + offset;
1328*4882a593Smuzhiyun 
1329*4882a593Smuzhiyun 	if (!ubi_dbg_chk_io(ubi))
1330*4882a593Smuzhiyun 		return 0;
1331*4882a593Smuzhiyun 
1332*4882a593Smuzhiyun 	buf1 = __vmalloc(len, GFP_NOFS, PAGE_KERNEL);
1333*4882a593Smuzhiyun 	if (!buf1) {
1334*4882a593Smuzhiyun 		ubi_err(ubi, "cannot allocate memory to check writes");
1335*4882a593Smuzhiyun 		return 0;
1336*4882a593Smuzhiyun 	}
1337*4882a593Smuzhiyun 
1338*4882a593Smuzhiyun 	err = mtd_read(ubi->mtd, addr, len, &read, buf1);
1339*4882a593Smuzhiyun 	if (err && !mtd_is_bitflip(err))
1340*4882a593Smuzhiyun 		goto out_free;
1341*4882a593Smuzhiyun 
1342*4882a593Smuzhiyun 	for (i = 0; i < len; i++) {
1343*4882a593Smuzhiyun 		uint8_t c = ((uint8_t *)buf)[i];
1344*4882a593Smuzhiyun 		uint8_t c1 = ((uint8_t *)buf1)[i];
1345*4882a593Smuzhiyun #if !defined(CONFIG_UBI_SILENCE_MSG)
1346*4882a593Smuzhiyun 		int dump_len = max_t(int, 128, len - i);
1347*4882a593Smuzhiyun #endif
1348*4882a593Smuzhiyun 
1349*4882a593Smuzhiyun 		if (c == c1)
1350*4882a593Smuzhiyun 			continue;
1351*4882a593Smuzhiyun 
1352*4882a593Smuzhiyun 		ubi_err(ubi, "self-check failed for PEB %d:%d, len %d",
1353*4882a593Smuzhiyun 			pnum, offset, len);
1354*4882a593Smuzhiyun 		ubi_msg(ubi, "data differ at position %d", i);
1355*4882a593Smuzhiyun 		ubi_msg(ubi, "hex dump of the original buffer from %d to %d",
1356*4882a593Smuzhiyun 			i, i + dump_len);
1357*4882a593Smuzhiyun 		print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 1,
1358*4882a593Smuzhiyun 			       buf + i, dump_len, 1);
1359*4882a593Smuzhiyun 		ubi_msg(ubi, "hex dump of the read buffer from %d to %d",
1360*4882a593Smuzhiyun 			i, i + dump_len);
1361*4882a593Smuzhiyun 		print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 1,
1362*4882a593Smuzhiyun 			       buf1 + i, dump_len, 1);
1363*4882a593Smuzhiyun 		dump_stack();
1364*4882a593Smuzhiyun 		err = -EINVAL;
1365*4882a593Smuzhiyun 		goto out_free;
1366*4882a593Smuzhiyun 	}
1367*4882a593Smuzhiyun 
1368*4882a593Smuzhiyun 	vfree(buf1);
1369*4882a593Smuzhiyun 	return 0;
1370*4882a593Smuzhiyun 
1371*4882a593Smuzhiyun out_free:
1372*4882a593Smuzhiyun 	vfree(buf1);
1373*4882a593Smuzhiyun 	return err;
1374*4882a593Smuzhiyun }
1375*4882a593Smuzhiyun 
1376*4882a593Smuzhiyun /**
1377*4882a593Smuzhiyun  * ubi_self_check_all_ff - check that a region of flash is empty.
1378*4882a593Smuzhiyun  * @ubi: UBI device description object
1379*4882a593Smuzhiyun  * @pnum: the physical eraseblock number to check
1380*4882a593Smuzhiyun  * @offset: the starting offset within the physical eraseblock to check
1381*4882a593Smuzhiyun  * @len: the length of the region to check
1382*4882a593Smuzhiyun  *
1383*4882a593Smuzhiyun  * This function returns zero if only 0xFF bytes are present at offset
1384*4882a593Smuzhiyun  * @offset of the physical eraseblock @pnum, and a negative error code if not
1385*4882a593Smuzhiyun  * or if an error occurred.
1386*4882a593Smuzhiyun  */
ubi_self_check_all_ff(struct ubi_device * ubi,int pnum,int offset,int len)1387*4882a593Smuzhiyun int ubi_self_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len)
1388*4882a593Smuzhiyun {
1389*4882a593Smuzhiyun 	size_t read;
1390*4882a593Smuzhiyun 	int err;
1391*4882a593Smuzhiyun 	void *buf;
1392*4882a593Smuzhiyun 	loff_t addr = (loff_t)pnum * ubi->peb_size + offset;
1393*4882a593Smuzhiyun 
1394*4882a593Smuzhiyun 	if (!ubi_dbg_chk_io(ubi))
1395*4882a593Smuzhiyun 		return 0;
1396*4882a593Smuzhiyun 
1397*4882a593Smuzhiyun 	buf = __vmalloc(len, GFP_NOFS, PAGE_KERNEL);
1398*4882a593Smuzhiyun 	if (!buf) {
1399*4882a593Smuzhiyun 		ubi_err(ubi, "cannot allocate memory to check for 0xFFs");
1400*4882a593Smuzhiyun 		return 0;
1401*4882a593Smuzhiyun 	}
1402*4882a593Smuzhiyun 
1403*4882a593Smuzhiyun 	err = mtd_read(ubi->mtd, addr, len, &read, buf);
1404*4882a593Smuzhiyun 	if (err && !mtd_is_bitflip(err)) {
1405*4882a593Smuzhiyun 		ubi_err(ubi, "err %d while reading %d bytes from PEB %d:%d, read %zd bytes",
1406*4882a593Smuzhiyun 			err, len, pnum, offset, read);
1407*4882a593Smuzhiyun 		goto error;
1408*4882a593Smuzhiyun 	}
1409*4882a593Smuzhiyun 
1410*4882a593Smuzhiyun 	err = ubi_check_pattern(buf, 0xFF, len);
1411*4882a593Smuzhiyun 	if (err == 0) {
1412*4882a593Smuzhiyun 		ubi_err(ubi, "flash region at PEB %d:%d, length %d does not contain all 0xFF bytes",
1413*4882a593Smuzhiyun 			pnum, offset, len);
1414*4882a593Smuzhiyun 		goto fail;
1415*4882a593Smuzhiyun 	}
1416*4882a593Smuzhiyun 
1417*4882a593Smuzhiyun 	vfree(buf);
1418*4882a593Smuzhiyun 	return 0;
1419*4882a593Smuzhiyun 
1420*4882a593Smuzhiyun fail:
1421*4882a593Smuzhiyun 	ubi_err(ubi, "self-check failed for PEB %d", pnum);
1422*4882a593Smuzhiyun 	ubi_msg(ubi, "hex dump of the %d-%d region", offset, offset + len);
1423*4882a593Smuzhiyun 	print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 1, buf, len, 1);
1424*4882a593Smuzhiyun 	err = -EINVAL;
1425*4882a593Smuzhiyun error:
1426*4882a593Smuzhiyun 	dump_stack();
1427*4882a593Smuzhiyun 	vfree(buf);
1428*4882a593Smuzhiyun 	return err;
1429*4882a593Smuzhiyun }
1430