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