blob: 8ba22d814226411ecee68a5afbe6155c54a14c5d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kyungmin Parka5983452008-11-19 16:26:54 +01002/*
3 * Copyright (c) International Business Machines Corp., 2006
4 * Copyright (c) Nokia Corporation, 2006, 2007
Kyungmin Parka5983452008-11-19 16:26:54 +01005 *
6 * Author: Artem Bityutskiy (Битюцкий Артём)
7 */
8
9/*
Heiko Schocherf5895d12014-06-24 10:10:04 +020010 * UBI input/output sub-system.
Kyungmin Parka5983452008-11-19 16:26:54 +010011 *
Heiko Schocherf5895d12014-06-24 10:10:04 +020012 * This sub-system provides a uniform way to work with all kinds of the
13 * underlying MTD devices. It also implements handy functions for reading and
14 * writing UBI headers.
Kyungmin Parka5983452008-11-19 16:26:54 +010015 *
16 * We are trying to have a paranoid mindset and not to trust to what we read
Heiko Schocherf5895d12014-06-24 10:10:04 +020017 * from the flash media in order to be more secure and robust. So this
18 * sub-system validates every single header it reads from the flash media.
Kyungmin Parka5983452008-11-19 16:26:54 +010019 *
20 * Some words about how the eraseblock headers are stored.
21 *
22 * The erase counter header is always stored at offset zero. By default, the
23 * VID header is stored after the EC header at the closest aligned offset
24 * (i.e. aligned to the minimum I/O unit size). Data starts next to the VID
25 * header at the closest aligned offset. But this default layout may be
26 * changed. For example, for different reasons (e.g., optimization) UBI may be
27 * asked to put the VID header at further offset, and even at an unaligned
28 * offset. Of course, if the offset of the VID header is unaligned, UBI adds
29 * proper padding in front of it. Data offset may also be changed but it has to
30 * be aligned.
31 *
32 * About minimal I/O units. In general, UBI assumes flash device model where
33 * there is only one minimal I/O unit size. E.g., in case of NOR flash it is 1,
34 * in case of NAND flash it is a NAND page, etc. This is reported by MTD in the
35 * @ubi->mtd->writesize field. But as an exception, UBI admits of using another
36 * (smaller) minimal I/O unit size for EC and VID headers to make it possible
37 * to do different optimizations.
38 *
39 * This is extremely useful in case of NAND flashes which admit of several
40 * write operations to one NAND page. In this case UBI can fit EC and VID
41 * headers at one NAND page. Thus, UBI may use "sub-page" size as the minimal
42 * I/O unit for the headers (the @ubi->hdrs_min_io_size field). But it still
43 * reports NAND page size (@ubi->min_io_size) as a minimal I/O unit for the UBI
44 * users.
45 *
46 * Example: some Samsung NANDs with 2KiB pages allow 4x 512-byte writes, so
47 * although the minimal I/O unit is 2K, UBI uses 512 bytes for EC and VID
48 * headers.
49 *
50 * Q: why not just to treat sub-page as a minimal I/O unit of this flash
51 * device, e.g., make @ubi->min_io_size = 512 in the example above?
52 *
53 * A: because when writing a sub-page, MTD still writes a full 2K page but the
Heiko Schocherf5895d12014-06-24 10:10:04 +020054 * bytes which are not relevant to the sub-page are 0xFF. So, basically,
55 * writing 4x512 sub-pages is 4 times slower than writing one 2KiB NAND page.
56 * Thus, we prefer to use sub-pages only for EC and VID headers.
Kyungmin Parka5983452008-11-19 16:26:54 +010057 *
58 * As it was noted above, the VID header may start at a non-aligned offset.
59 * For example, in case of a 2KiB page NAND flash with a 512 bytes sub-page,
60 * the VID header may reside at offset 1984 which is the last 64 bytes of the
61 * last sub-page (EC header is always at offset zero). This causes some
62 * difficulties when reading and writing VID headers.
63 *
64 * Suppose we have a 64-byte buffer and we read a VID header at it. We change
65 * the data and want to write this VID header out. As we can only write in
66 * 512-byte chunks, we have to allocate one more buffer and copy our VID header
67 * to offset 448 of this buffer.
68 *
Heiko Schocherf5895d12014-06-24 10:10:04 +020069 * The I/O sub-system does the following trick in order to avoid this extra
70 * copy. It always allocates a @ubi->vid_hdr_alsize bytes buffer for the VID
71 * header and returns a pointer to offset @ubi->vid_hdr_shift of this buffer.
72 * When the VID header is being written out, it shifts the VID header pointer
73 * back and writes the whole sub-page.
Kyungmin Parka5983452008-11-19 16:26:54 +010074 */
75
Heiko Schocherf5895d12014-06-24 10:10:04 +020076#ifndef __UBOOT__
Simon Glassd66c5f72020-02-03 07:36:15 -070077#include <dm/devres.h>
Kyungmin Parka5983452008-11-19 16:26:54 +010078#include <linux/crc32.h>
79#include <linux/err.h>
Heiko Schocherf5895d12014-06-24 10:10:04 +020080#include <linux/slab.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070081#include <u-boot/crc.h>
Heiko Schocherf5895d12014-06-24 10:10:04 +020082#else
Alexey Brodkin2d2fa492018-06-05 17:17:57 +030083#include <hexdump.h>
Heiko Schocherf5895d12014-06-24 10:10:04 +020084#include <ubi_uboot.h>
Kyungmin Parka5983452008-11-19 16:26:54 +010085#endif
86
Kyungmin Parka5983452008-11-19 16:26:54 +010087#include "ubi.h"
88
Heiko Schocherf5895d12014-06-24 10:10:04 +020089static int self_check_not_bad(const struct ubi_device *ubi, int pnum);
90static int self_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum);
91static int self_check_ec_hdr(const struct ubi_device *ubi, int pnum,
92 const struct ubi_ec_hdr *ec_hdr);
93static int self_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum);
94static int self_check_vid_hdr(const struct ubi_device *ubi, int pnum,
95 const struct ubi_vid_hdr *vid_hdr);
96static int self_check_write(struct ubi_device *ubi, const void *buf, int pnum,
97 int offset, int len);
Kyungmin Parka5983452008-11-19 16:26:54 +010098
99/**
100 * ubi_io_read - read data from a physical eraseblock.
101 * @ubi: UBI device description object
102 * @buf: buffer where to store the read data
103 * @pnum: physical eraseblock number to read from
104 * @offset: offset within the physical eraseblock from where to read
105 * @len: how many bytes to read
106 *
107 * This function reads data from offset @offset of physical eraseblock @pnum
108 * and stores the read data in the @buf buffer. The following return codes are
109 * possible:
110 *
111 * o %0 if all the requested data were successfully read;
112 * o %UBI_IO_BITFLIPS if all the requested data were successfully read, but
113 * correctable bit-flips were detected; this is harmless but may indicate
114 * that this eraseblock may become bad soon (but do not have to);
115 * o %-EBADMSG if the MTD subsystem reported about data integrity problems, for
116 * example it can be an ECC error in case of NAND; this most probably means
117 * that the data is corrupted;
118 * o %-EIO if some I/O error occurred;
119 * o other negative error codes in case of other errors.
120 */
121int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset,
122 int len)
123{
124 int err, retries = 0;
125 size_t read;
126 loff_t addr;
127
128 dbg_io("read %d bytes from PEB %d:%d", len, pnum, offset);
129
130 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
131 ubi_assert(offset >= 0 && offset + len <= ubi->peb_size);
132 ubi_assert(len > 0);
133
Heiko Schocherf5895d12014-06-24 10:10:04 +0200134 err = self_check_not_bad(ubi, pnum);
Kyungmin Parka5983452008-11-19 16:26:54 +0100135 if (err)
Heiko Schocherf5895d12014-06-24 10:10:04 +0200136 return err;
137
138 /*
139 * Deliberately corrupt the buffer to improve robustness. Indeed, if we
140 * do not do this, the following may happen:
141 * 1. The buffer contains data from previous operation, e.g., read from
142 * another PEB previously. The data looks like expected, e.g., if we
143 * just do not read anything and return - the caller would not
144 * notice this. E.g., if we are reading a VID header, the buffer may
145 * contain a valid VID header from another PEB.
146 * 2. The driver is buggy and returns us success or -EBADMSG or
147 * -EUCLEAN, but it does not actually put any data to the buffer.
148 *
149 * This may confuse UBI or upper layers - they may think the buffer
150 * contains valid data while in fact it is just old data. This is
151 * especially possible because UBI (and UBIFS) relies on CRC, and
152 * treats data as correct even in case of ECC errors if the CRC is
153 * correct.
154 *
155 * Try to prevent this situation by changing the first byte of the
156 * buffer.
157 */
158 *((uint8_t *)buf) ^= 0xFF;
Kyungmin Parka5983452008-11-19 16:26:54 +0100159
160 addr = (loff_t)pnum * ubi->peb_size + offset;
161retry:
Sergey Lapin3a38a552013-01-14 03:46:50 +0000162 err = mtd_read(ubi->mtd, addr, len, &read, buf);
Kyungmin Parka5983452008-11-19 16:26:54 +0100163 if (err) {
Heiko Schocherf5895d12014-06-24 10:10:04 +0200164 const char *errstr = mtd_is_eccerr(err) ? " (ECC error)" : "";
165
166 if (mtd_is_bitflip(err)) {
Kyungmin Parka5983452008-11-19 16:26:54 +0100167 /*
168 * -EUCLEAN is reported if there was a bit-flip which
169 * was corrected, so this is harmless.
Heiko Schocherf5895d12014-06-24 10:10:04 +0200170 *
171 * We do not report about it here unless debugging is
172 * enabled. A corresponding message will be printed
173 * later, when it is has been scrubbed.
Kyungmin Parka5983452008-11-19 16:26:54 +0100174 */
Heiko Schocher94b66de2015-10-22 06:19:21 +0200175 ubi_msg(ubi, "fixable bit-flip detected at PEB %d",
176 pnum);
Kyungmin Parka5983452008-11-19 16:26:54 +0100177 ubi_assert(len == read);
178 return UBI_IO_BITFLIPS;
179 }
180
Heiko Schocherf5895d12014-06-24 10:10:04 +0200181 if (retries++ < UBI_IO_RETRIES) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200182 ubi_warn(ubi, "error %d%s while reading %d bytes from PEB %d:%d, read only %zd bytes, retry",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200183 err, errstr, len, pnum, offset, read);
Kyungmin Parka5983452008-11-19 16:26:54 +0100184 yield();
185 goto retry;
186 }
187
Heiko Schocher94b66de2015-10-22 06:19:21 +0200188 ubi_err(ubi, "error %d%s while reading %d bytes from PEB %d:%d, read %zd bytes",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200189 err, errstr, len, pnum, offset, read);
190 dump_stack();
Kyungmin Parka5983452008-11-19 16:26:54 +0100191
192 /*
193 * The driver should never return -EBADMSG if it failed to read
194 * all the requested data. But some buggy drivers might do
195 * this, so we change it to -EIO.
196 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200197 if (read != len && mtd_is_eccerr(err)) {
Kyungmin Parka5983452008-11-19 16:26:54 +0100198 ubi_assert(0);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200199 err = -EIO;
Kyungmin Parka5983452008-11-19 16:26:54 +0100200 }
201 } else {
202 ubi_assert(len == read);
203
Heiko Schocherf5895d12014-06-24 10:10:04 +0200204 if (ubi_dbg_is_bitflip(ubi)) {
205 dbg_gen("bit-flip (emulated)");
Kyungmin Parka5983452008-11-19 16:26:54 +0100206 err = UBI_IO_BITFLIPS;
207 }
208 }
209
210 return err;
211}
212
213/**
214 * ubi_io_write - write data to a physical eraseblock.
215 * @ubi: UBI device description object
216 * @buf: buffer with the data to write
217 * @pnum: physical eraseblock number to write to
218 * @offset: offset within the physical eraseblock where to write
219 * @len: how many bytes to write
220 *
221 * This function writes @len bytes of data from buffer @buf to offset @offset
222 * of physical eraseblock @pnum. If all the data were successfully written,
223 * zero is returned. If an error occurred, this function returns a negative
224 * error code. If %-EIO is returned, the physical eraseblock most probably went
225 * bad.
226 *
227 * Note, in case of an error, it is possible that something was still written
228 * to the flash media, but may be some garbage.
229 */
230int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
231 int len)
232{
233 int err;
234 size_t written;
235 loff_t addr;
236
237 dbg_io("write %d bytes to PEB %d:%d", len, pnum, offset);
238
239 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
240 ubi_assert(offset >= 0 && offset + len <= ubi->peb_size);
241 ubi_assert(offset % ubi->hdrs_min_io_size == 0);
242 ubi_assert(len > 0 && len % ubi->hdrs_min_io_size == 0);
243
244 if (ubi->ro_mode) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200245 ubi_err(ubi, "read-only mode");
Kyungmin Parka5983452008-11-19 16:26:54 +0100246 return -EROFS;
247 }
248
Heiko Schocherf5895d12014-06-24 10:10:04 +0200249 err = self_check_not_bad(ubi, pnum);
Kyungmin Parka5983452008-11-19 16:26:54 +0100250 if (err)
Heiko Schocherf5895d12014-06-24 10:10:04 +0200251 return err;
Kyungmin Parka5983452008-11-19 16:26:54 +0100252
253 /* The area we are writing to has to contain all 0xFF bytes */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200254 err = ubi_self_check_all_ff(ubi, pnum, offset, len);
Kyungmin Parka5983452008-11-19 16:26:54 +0100255 if (err)
Heiko Schocherf5895d12014-06-24 10:10:04 +0200256 return err;
Kyungmin Parka5983452008-11-19 16:26:54 +0100257
258 if (offset >= ubi->leb_start) {
259 /*
260 * We write to the data area of the physical eraseblock. Make
261 * sure it has valid EC and VID headers.
262 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200263 err = self_check_peb_ec_hdr(ubi, pnum);
Kyungmin Parka5983452008-11-19 16:26:54 +0100264 if (err)
Heiko Schocherf5895d12014-06-24 10:10:04 +0200265 return err;
266 err = self_check_peb_vid_hdr(ubi, pnum);
Kyungmin Parka5983452008-11-19 16:26:54 +0100267 if (err)
Heiko Schocherf5895d12014-06-24 10:10:04 +0200268 return err;
Kyungmin Parka5983452008-11-19 16:26:54 +0100269 }
270
Heiko Schocherf5895d12014-06-24 10:10:04 +0200271 if (ubi_dbg_is_write_failure(ubi)) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200272 ubi_err(ubi, "cannot write %d bytes to PEB %d:%d (emulated)",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200273 len, pnum, offset);
274 dump_stack();
Kyungmin Parka5983452008-11-19 16:26:54 +0100275 return -EIO;
276 }
277
278 addr = (loff_t)pnum * ubi->peb_size + offset;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000279 err = mtd_write(ubi->mtd, addr, len, &written, buf);
Kyungmin Parka5983452008-11-19 16:26:54 +0100280 if (err) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200281 ubi_err(ubi, "error %d while writing %d bytes to PEB %d:%d, written %zd bytes",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200282 err, len, pnum, offset, written);
283 dump_stack();
284 ubi_dump_flash(ubi, pnum, offset, len);
Kyungmin Parka5983452008-11-19 16:26:54 +0100285 } else
286 ubi_assert(written == len);
287
Heiko Schocherf5895d12014-06-24 10:10:04 +0200288 if (!err) {
289 err = self_check_write(ubi, buf, pnum, offset, len);
290 if (err)
291 return err;
292
293 /*
294 * Since we always write sequentially, the rest of the PEB has
295 * to contain only 0xFF bytes.
296 */
297 offset += len;
298 len = ubi->peb_size - offset;
299 if (len)
300 err = ubi_self_check_all_ff(ubi, pnum, offset, len);
301 }
302
Kyungmin Parka5983452008-11-19 16:26:54 +0100303 return err;
304}
305
306/**
307 * erase_callback - MTD erasure call-back.
308 * @ei: MTD erase information object.
309 *
310 * Note, even though MTD erase interface is asynchronous, all the current
311 * implementations are synchronous anyway.
312 */
313static void erase_callback(struct erase_info *ei)
314{
315 wake_up_interruptible((wait_queue_head_t *)ei->priv);
316}
317
318/**
319 * do_sync_erase - synchronously erase a physical eraseblock.
320 * @ubi: UBI device description object
321 * @pnum: the physical eraseblock number to erase
322 *
323 * This function synchronously erases physical eraseblock @pnum and returns
324 * zero in case of success and a negative error code in case of failure. If
325 * %-EIO is returned, the physical eraseblock most probably went bad.
326 */
327static int do_sync_erase(struct ubi_device *ubi, int pnum)
328{
329 int err, retries = 0;
330 struct erase_info ei;
331 wait_queue_head_t wq;
332
333 dbg_io("erase PEB %d", pnum);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200334 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
335
336 if (ubi->ro_mode) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200337 ubi_err(ubi, "read-only mode");
Heiko Schocherf5895d12014-06-24 10:10:04 +0200338 return -EROFS;
339 }
Kyungmin Parka5983452008-11-19 16:26:54 +0100340
341retry:
342 init_waitqueue_head(&wq);
343 memset(&ei, 0, sizeof(struct erase_info));
344
345 ei.mtd = ubi->mtd;
346 ei.addr = (loff_t)pnum * ubi->peb_size;
347 ei.len = ubi->peb_size;
348 ei.callback = erase_callback;
349 ei.priv = (unsigned long)&wq;
350
Sergey Lapin3a38a552013-01-14 03:46:50 +0000351 err = mtd_erase(ubi->mtd, &ei);
Kyungmin Parka5983452008-11-19 16:26:54 +0100352 if (err) {
353 if (retries++ < UBI_IO_RETRIES) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200354 ubi_warn(ubi, "error %d while erasing PEB %d, retry",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200355 err, pnum);
Kyungmin Parka5983452008-11-19 16:26:54 +0100356 yield();
357 goto retry;
358 }
Heiko Schocher94b66de2015-10-22 06:19:21 +0200359 ubi_err(ubi, "cannot erase PEB %d, error %d", pnum, err);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200360 dump_stack();
Kyungmin Parka5983452008-11-19 16:26:54 +0100361 return err;
362 }
363
364 err = wait_event_interruptible(wq, ei.state == MTD_ERASE_DONE ||
365 ei.state == MTD_ERASE_FAILED);
366 if (err) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200367 ubi_err(ubi, "interrupted PEB %d erasure", pnum);
Kyungmin Parka5983452008-11-19 16:26:54 +0100368 return -EINTR;
369 }
370
371 if (ei.state == MTD_ERASE_FAILED) {
372 if (retries++ < UBI_IO_RETRIES) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200373 ubi_warn(ubi, "error while erasing PEB %d, retry",
374 pnum);
Kyungmin Parka5983452008-11-19 16:26:54 +0100375 yield();
376 goto retry;
377 }
Heiko Schocher94b66de2015-10-22 06:19:21 +0200378 ubi_err(ubi, "cannot erase PEB %d", pnum);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200379 dump_stack();
Kyungmin Parka5983452008-11-19 16:26:54 +0100380 return -EIO;
381 }
382
Heiko Schocherf5895d12014-06-24 10:10:04 +0200383 err = ubi_self_check_all_ff(ubi, pnum, 0, ubi->peb_size);
Kyungmin Parka5983452008-11-19 16:26:54 +0100384 if (err)
Heiko Schocherf5895d12014-06-24 10:10:04 +0200385 return err;
Kyungmin Parka5983452008-11-19 16:26:54 +0100386
Heiko Schocherf5895d12014-06-24 10:10:04 +0200387 if (ubi_dbg_is_erase_failure(ubi)) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200388 ubi_err(ubi, "cannot erase PEB %d (emulated)", pnum);
Kyungmin Parka5983452008-11-19 16:26:54 +0100389 return -EIO;
390 }
391
392 return 0;
393}
394
Kyungmin Parka5983452008-11-19 16:26:54 +0100395/* Patterns to write to a physical eraseblock when torturing it */
396static uint8_t patterns[] = {0xa5, 0x5a, 0x0};
397
398/**
399 * torture_peb - test a supposedly bad physical eraseblock.
400 * @ubi: UBI device description object
401 * @pnum: the physical eraseblock number to test
402 *
403 * This function returns %-EIO if the physical eraseblock did not pass the
404 * test, a positive number of erase operations done if the test was
405 * successfully passed, and other negative error codes in case of other errors.
406 */
407static int torture_peb(struct ubi_device *ubi, int pnum)
408{
409 int err, i, patt_count;
410
Heiko Schocher94b66de2015-10-22 06:19:21 +0200411 ubi_msg(ubi, "run torture test for PEB %d", pnum);
Kyungmin Parka5983452008-11-19 16:26:54 +0100412 patt_count = ARRAY_SIZE(patterns);
413 ubi_assert(patt_count > 0);
414
415 mutex_lock(&ubi->buf_mutex);
416 for (i = 0; i < patt_count; i++) {
417 err = do_sync_erase(ubi, pnum);
418 if (err)
419 goto out;
420
421 /* Make sure the PEB contains only 0xFF bytes */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200422 err = ubi_io_read(ubi, ubi->peb_buf, pnum, 0, ubi->peb_size);
Kyungmin Parka5983452008-11-19 16:26:54 +0100423 if (err)
424 goto out;
425
Heiko Schocherf5895d12014-06-24 10:10:04 +0200426 err = ubi_check_pattern(ubi->peb_buf, 0xFF, ubi->peb_size);
Kyungmin Parka5983452008-11-19 16:26:54 +0100427 if (err == 0) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200428 ubi_err(ubi, "erased PEB %d, but a non-0xFF byte found",
Kyungmin Parka5983452008-11-19 16:26:54 +0100429 pnum);
430 err = -EIO;
431 goto out;
432 }
433
434 /* Write a pattern and check it */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200435 memset(ubi->peb_buf, patterns[i], ubi->peb_size);
436 err = ubi_io_write(ubi, ubi->peb_buf, pnum, 0, ubi->peb_size);
Kyungmin Parka5983452008-11-19 16:26:54 +0100437 if (err)
438 goto out;
439
Heiko Schocherf5895d12014-06-24 10:10:04 +0200440 memset(ubi->peb_buf, ~patterns[i], ubi->peb_size);
441 err = ubi_io_read(ubi, ubi->peb_buf, pnum, 0, ubi->peb_size);
Kyungmin Parka5983452008-11-19 16:26:54 +0100442 if (err)
443 goto out;
444
Heiko Schocherf5895d12014-06-24 10:10:04 +0200445 err = ubi_check_pattern(ubi->peb_buf, patterns[i],
446 ubi->peb_size);
Kyungmin Parka5983452008-11-19 16:26:54 +0100447 if (err == 0) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200448 ubi_err(ubi, "pattern %x checking failed for PEB %d",
Kyungmin Parka5983452008-11-19 16:26:54 +0100449 patterns[i], pnum);
450 err = -EIO;
451 goto out;
452 }
453 }
454
455 err = patt_count;
Heiko Schocher94b66de2015-10-22 06:19:21 +0200456 ubi_msg(ubi, "PEB %d passed torture test, do not mark it as bad", pnum);
Kyungmin Parka5983452008-11-19 16:26:54 +0100457
458out:
459 mutex_unlock(&ubi->buf_mutex);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200460 if (err == UBI_IO_BITFLIPS || mtd_is_eccerr(err)) {
Kyungmin Parka5983452008-11-19 16:26:54 +0100461 /*
462 * If a bit-flip or data integrity error was detected, the test
463 * has not passed because it happened on a freshly erased
464 * physical eraseblock which means something is wrong with it.
465 */
Heiko Schocher94b66de2015-10-22 06:19:21 +0200466 ubi_err(ubi, "read problems on freshly erased PEB %d, must be bad",
Kyungmin Parka5983452008-11-19 16:26:54 +0100467 pnum);
468 err = -EIO;
469 }
470 return err;
471}
472
473/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200474 * nor_erase_prepare - prepare a NOR flash PEB for erasure.
475 * @ubi: UBI device description object
476 * @pnum: physical eraseblock number to prepare
477 *
478 * NOR flash, or at least some of them, have peculiar embedded PEB erasure
479 * algorithm: the PEB is first filled with zeroes, then it is erased. And
480 * filling with zeroes starts from the end of the PEB. This was observed with
481 * Spansion S29GL512N NOR flash.
482 *
483 * This means that in case of a power cut we may end up with intact data at the
484 * beginning of the PEB, and all zeroes at the end of PEB. In other words, the
485 * EC and VID headers are OK, but a large chunk of data at the end of PEB is
486 * zeroed. This makes UBI mistakenly treat this PEB as used and associate it
487 * with an LEB, which leads to subsequent failures (e.g., UBIFS fails).
488 *
489 * This function is called before erasing NOR PEBs and it zeroes out EC and VID
490 * magic numbers in order to invalidate them and prevent the failures. Returns
491 * zero in case of success and a negative error code in case of failure.
492 */
493static int nor_erase_prepare(struct ubi_device *ubi, int pnum)
494{
495 int err;
496 size_t written;
497 loff_t addr;
498 uint32_t data = 0;
499 struct ubi_ec_hdr ec_hdr;
500
501 /*
502 * Note, we cannot generally define VID header buffers on stack,
503 * because of the way we deal with these buffers (see the header
504 * comment in this file). But we know this is a NOR-specific piece of
505 * code, so we can do this. But yes, this is error-prone and we should
506 * (pre-)allocate VID header buffer instead.
507 */
508 struct ubi_vid_hdr vid_hdr;
509
510 /*
511 * If VID or EC is valid, we have to corrupt them before erasing.
512 * It is important to first invalidate the EC header, and then the VID
513 * header. Otherwise a power cut may lead to valid EC header and
514 * invalid VID header, in which case UBI will treat this PEB as
515 * corrupted and will try to preserve it, and print scary warnings.
516 */
517 addr = (loff_t)pnum * ubi->peb_size;
518 err = ubi_io_read_ec_hdr(ubi, pnum, &ec_hdr, 0);
519 if (err != UBI_IO_BAD_HDR_EBADMSG && err != UBI_IO_BAD_HDR &&
520 err != UBI_IO_FF){
521 err = mtd_write(ubi->mtd, addr, 4, &written, (void *)&data);
522 if(err)
523 goto error;
524 }
525
526 err = ubi_io_read_vid_hdr(ubi, pnum, &vid_hdr, 0);
527 if (err != UBI_IO_BAD_HDR_EBADMSG && err != UBI_IO_BAD_HDR &&
528 err != UBI_IO_FF){
529 addr += ubi->vid_hdr_aloffset;
530 err = mtd_write(ubi->mtd, addr, 4, &written, (void *)&data);
531 if (err)
532 goto error;
533 }
534 return 0;
535
536error:
537 /*
538 * The PEB contains a valid VID or EC header, but we cannot invalidate
539 * it. Supposedly the flash media or the driver is screwed up, so
540 * return an error.
541 */
Heiko Schocher94b66de2015-10-22 06:19:21 +0200542 ubi_err(ubi, "cannot invalidate PEB %d, write returned %d", pnum, err);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200543 ubi_dump_flash(ubi, pnum, 0, ubi->peb_size);
544 return -EIO;
545}
546
547/**
Kyungmin Parka5983452008-11-19 16:26:54 +0100548 * ubi_io_sync_erase - synchronously erase a physical eraseblock.
549 * @ubi: UBI device description object
550 * @pnum: physical eraseblock number to erase
551 * @torture: if this physical eraseblock has to be tortured
552 *
553 * This function synchronously erases physical eraseblock @pnum. If @torture
554 * flag is not zero, the physical eraseblock is checked by means of writing
555 * different patterns to it and reading them back. If the torturing is enabled,
Heiko Schocherf5895d12014-06-24 10:10:04 +0200556 * the physical eraseblock is erased more than once.
Kyungmin Parka5983452008-11-19 16:26:54 +0100557 *
558 * This function returns the number of erasures made in case of success, %-EIO
559 * if the erasure failed or the torturing test failed, and other negative error
560 * codes in case of other errors. Note, %-EIO means that the physical
561 * eraseblock is bad.
562 */
563int ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture)
564{
565 int err, ret = 0;
566
567 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
568
Heiko Schocherf5895d12014-06-24 10:10:04 +0200569 err = self_check_not_bad(ubi, pnum);
Kyungmin Parka5983452008-11-19 16:26:54 +0100570 if (err != 0)
Heiko Schocherf5895d12014-06-24 10:10:04 +0200571 return err;
Kyungmin Parka5983452008-11-19 16:26:54 +0100572
573 if (ubi->ro_mode) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200574 ubi_err(ubi, "read-only mode");
Kyungmin Parka5983452008-11-19 16:26:54 +0100575 return -EROFS;
576 }
577
Heiko Schocherf5895d12014-06-24 10:10:04 +0200578 if (ubi->nor_flash) {
579 err = nor_erase_prepare(ubi, pnum);
580 if (err)
581 return err;
582 }
583
Kyungmin Parka5983452008-11-19 16:26:54 +0100584 if (torture) {
585 ret = torture_peb(ubi, pnum);
586 if (ret < 0)
587 return ret;
588 }
589
590 err = do_sync_erase(ubi, pnum);
591 if (err)
592 return err;
593
594 return ret + 1;
595}
596
597/**
598 * ubi_io_is_bad - check if a physical eraseblock is bad.
599 * @ubi: UBI device description object
600 * @pnum: the physical eraseblock number to check
601 *
602 * This function returns a positive number if the physical eraseblock is bad,
603 * zero if not, and a negative error code if an error occurred.
604 */
605int ubi_io_is_bad(const struct ubi_device *ubi, int pnum)
606{
607 struct mtd_info *mtd = ubi->mtd;
608
609 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
610
611 if (ubi->bad_allowed) {
612 int ret;
613
Sergey Lapin3a38a552013-01-14 03:46:50 +0000614 ret = mtd_block_isbad(mtd, (loff_t)pnum * ubi->peb_size);
Kyungmin Parka5983452008-11-19 16:26:54 +0100615 if (ret < 0)
Heiko Schocher94b66de2015-10-22 06:19:21 +0200616 ubi_err(ubi, "error %d while checking if PEB %d is bad",
Kyungmin Parka5983452008-11-19 16:26:54 +0100617 ret, pnum);
618 else if (ret)
619 dbg_io("PEB %d is bad", pnum);
620 return ret;
621 }
622
623 return 0;
624}
625
626/**
627 * ubi_io_mark_bad - mark a physical eraseblock as bad.
628 * @ubi: UBI device description object
629 * @pnum: the physical eraseblock number to mark
630 *
631 * This function returns zero in case of success and a negative error code in
632 * case of failure.
633 */
634int ubi_io_mark_bad(const struct ubi_device *ubi, int pnum)
635{
636 int err;
637 struct mtd_info *mtd = ubi->mtd;
638
639 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
640
641 if (ubi->ro_mode) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200642 ubi_err(ubi, "read-only mode");
Kyungmin Parka5983452008-11-19 16:26:54 +0100643 return -EROFS;
644 }
645
646 if (!ubi->bad_allowed)
647 return 0;
648
Sergey Lapin3a38a552013-01-14 03:46:50 +0000649 err = mtd_block_markbad(mtd, (loff_t)pnum * ubi->peb_size);
Kyungmin Parka5983452008-11-19 16:26:54 +0100650 if (err)
Heiko Schocher94b66de2015-10-22 06:19:21 +0200651 ubi_err(ubi, "cannot mark PEB %d bad, error %d", pnum, err);
Kyungmin Parka5983452008-11-19 16:26:54 +0100652 return err;
653}
654
655/**
656 * validate_ec_hdr - validate an erase counter header.
657 * @ubi: UBI device description object
658 * @ec_hdr: the erase counter header to check
659 *
660 * This function returns zero if the erase counter header is OK, and %1 if
661 * not.
662 */
663static int validate_ec_hdr(const struct ubi_device *ubi,
664 const struct ubi_ec_hdr *ec_hdr)
665{
666 long long ec;
667 int vid_hdr_offset, leb_start;
668
669 ec = be64_to_cpu(ec_hdr->ec);
670 vid_hdr_offset = be32_to_cpu(ec_hdr->vid_hdr_offset);
671 leb_start = be32_to_cpu(ec_hdr->data_offset);
672
673 if (ec_hdr->version != UBI_VERSION) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200674 ubi_err(ubi, "node with incompatible UBI version found: this UBI version is %d, image version is %d",
Kyungmin Parka5983452008-11-19 16:26:54 +0100675 UBI_VERSION, (int)ec_hdr->version);
676 goto bad;
677 }
678
679 if (vid_hdr_offset != ubi->vid_hdr_offset) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200680 ubi_err(ubi, "bad VID header offset %d, expected %d",
Kyungmin Parka5983452008-11-19 16:26:54 +0100681 vid_hdr_offset, ubi->vid_hdr_offset);
682 goto bad;
683 }
684
685 if (leb_start != ubi->leb_start) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200686 ubi_err(ubi, "bad data offset %d, expected %d",
Kyungmin Parka5983452008-11-19 16:26:54 +0100687 leb_start, ubi->leb_start);
688 goto bad;
689 }
690
691 if (ec < 0 || ec > UBI_MAX_ERASECOUNTER) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200692 ubi_err(ubi, "bad erase counter %lld", ec);
Kyungmin Parka5983452008-11-19 16:26:54 +0100693 goto bad;
694 }
695
696 return 0;
697
698bad:
Heiko Schocher94b66de2015-10-22 06:19:21 +0200699 ubi_err(ubi, "bad EC header");
Heiko Schocherf5895d12014-06-24 10:10:04 +0200700 ubi_dump_ec_hdr(ec_hdr);
701 dump_stack();
Kyungmin Parka5983452008-11-19 16:26:54 +0100702 return 1;
703}
704
705/**
706 * ubi_io_read_ec_hdr - read and check an erase counter header.
707 * @ubi: UBI device description object
708 * @pnum: physical eraseblock to read from
709 * @ec_hdr: a &struct ubi_ec_hdr object where to store the read erase counter
710 * header
711 * @verbose: be verbose if the header is corrupted or was not found
712 *
713 * This function reads erase counter header from physical eraseblock @pnum and
714 * stores it in @ec_hdr. This function also checks CRC checksum of the read
715 * erase counter header. The following codes may be returned:
716 *
717 * o %0 if the CRC checksum is correct and the header was successfully read;
718 * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected
719 * and corrected by the flash driver; this is harmless but may indicate that
720 * this eraseblock may become bad soon (but may be not);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200721 * o %UBI_IO_BAD_HDR if the erase counter header is corrupted (a CRC error);
722 * o %UBI_IO_BAD_HDR_EBADMSG is the same as %UBI_IO_BAD_HDR, but there also was
723 * a data integrity error (uncorrectable ECC error in case of NAND);
724 * o %UBI_IO_FF if only 0xFF bytes were read (the PEB is supposedly empty)
Kyungmin Parka5983452008-11-19 16:26:54 +0100725 * o a negative error code in case of failure.
726 */
727int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
728 struct ubi_ec_hdr *ec_hdr, int verbose)
729{
Heiko Schocherf5895d12014-06-24 10:10:04 +0200730 int err, read_err;
Kyungmin Parka5983452008-11-19 16:26:54 +0100731 uint32_t crc, magic, hdr_crc;
732
733 dbg_io("read EC header from PEB %d", pnum);
734 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
Kyungmin Parka5983452008-11-19 16:26:54 +0100735
Heiko Schocherf5895d12014-06-24 10:10:04 +0200736 read_err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE);
737 if (read_err) {
738 if (read_err != UBI_IO_BITFLIPS && !mtd_is_eccerr(read_err))
739 return read_err;
Kyungmin Parka5983452008-11-19 16:26:54 +0100740
741 /*
742 * We read all the data, but either a correctable bit-flip
Heiko Schocherf5895d12014-06-24 10:10:04 +0200743 * occurred, or MTD reported a data integrity error
744 * (uncorrectable ECC error in case of NAND). The former is
745 * harmless, the later may mean that the read data is
746 * corrupted. But we have a CRC check-sum and we will detect
747 * this. If the EC header is still OK, we just report this as
748 * there was a bit-flip, to force scrubbing.
Kyungmin Parka5983452008-11-19 16:26:54 +0100749 */
Kyungmin Parka5983452008-11-19 16:26:54 +0100750 }
751
752 magic = be32_to_cpu(ec_hdr->magic);
753 if (magic != UBI_EC_HDR_MAGIC) {
Heiko Schocherf5895d12014-06-24 10:10:04 +0200754 if (mtd_is_eccerr(read_err))
755 return UBI_IO_BAD_HDR_EBADMSG;
756
Kyungmin Parka5983452008-11-19 16:26:54 +0100757 /*
758 * The magic field is wrong. Let's check if we have read all
759 * 0xFF. If yes, this physical eraseblock is assumed to be
760 * empty.
Kyungmin Parka5983452008-11-19 16:26:54 +0100761 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200762 if (ubi_check_pattern(ec_hdr, 0xFF, UBI_EC_HDR_SIZE)) {
Kyungmin Parka5983452008-11-19 16:26:54 +0100763 /* The physical eraseblock is supposedly empty */
Kyungmin Parka5983452008-11-19 16:26:54 +0100764 if (verbose)
Heiko Schocher94b66de2015-10-22 06:19:21 +0200765 ubi_warn(ubi, "no EC header found at PEB %d, only 0xFF bytes",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200766 pnum);
767 dbg_bld("no EC header found at PEB %d, only 0xFF bytes",
768 pnum);
769 if (!read_err)
770 return UBI_IO_FF;
771 else
772 return UBI_IO_FF_BITFLIPS;
Kyungmin Parka5983452008-11-19 16:26:54 +0100773 }
774
775 /*
776 * This is not a valid erase counter header, and these are not
777 * 0xFF bytes. Report that the header is corrupted.
778 */
779 if (verbose) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200780 ubi_warn(ubi, "bad magic number at PEB %d: %08x instead of %08x",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200781 pnum, magic, UBI_EC_HDR_MAGIC);
782 ubi_dump_ec_hdr(ec_hdr);
Kyungmin Parka5983452008-11-19 16:26:54 +0100783 }
Heiko Schocherf5895d12014-06-24 10:10:04 +0200784 dbg_bld("bad magic number at PEB %d: %08x instead of %08x",
785 pnum, magic, UBI_EC_HDR_MAGIC);
786 return UBI_IO_BAD_HDR;
Kyungmin Parka5983452008-11-19 16:26:54 +0100787 }
788
789 crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
790 hdr_crc = be32_to_cpu(ec_hdr->hdr_crc);
791
792 if (hdr_crc != crc) {
793 if (verbose) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200794 ubi_warn(ubi, "bad EC header CRC at PEB %d, calculated %#08x, read %#08x",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200795 pnum, crc, hdr_crc);
796 ubi_dump_ec_hdr(ec_hdr);
Kyungmin Parka5983452008-11-19 16:26:54 +0100797 }
Heiko Schocherf5895d12014-06-24 10:10:04 +0200798 dbg_bld("bad EC header CRC at PEB %d, calculated %#08x, read %#08x",
799 pnum, crc, hdr_crc);
800
801 if (!read_err)
802 return UBI_IO_BAD_HDR;
803 else
804 return UBI_IO_BAD_HDR_EBADMSG;
Kyungmin Parka5983452008-11-19 16:26:54 +0100805 }
806
807 /* And of course validate what has just been read from the media */
808 err = validate_ec_hdr(ubi, ec_hdr);
809 if (err) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200810 ubi_err(ubi, "validation failed for PEB %d", pnum);
Kyungmin Parka5983452008-11-19 16:26:54 +0100811 return -EINVAL;
812 }
813
Heiko Schocherf5895d12014-06-24 10:10:04 +0200814 /*
815 * If there was %-EBADMSG, but the header CRC is still OK, report about
816 * a bit-flip to force scrubbing on this PEB.
817 */
Kyungmin Parka5983452008-11-19 16:26:54 +0100818 return read_err ? UBI_IO_BITFLIPS : 0;
819}
820
821/**
822 * ubi_io_write_ec_hdr - write an erase counter header.
823 * @ubi: UBI device description object
824 * @pnum: physical eraseblock to write to
825 * @ec_hdr: the erase counter header to write
826 *
827 * This function writes erase counter header described by @ec_hdr to physical
828 * eraseblock @pnum. It also fills most fields of @ec_hdr before writing, so
829 * the caller do not have to fill them. Callers must only fill the @ec_hdr->ec
830 * field.
831 *
832 * This function returns zero in case of success and a negative error code in
833 * case of failure. If %-EIO is returned, the physical eraseblock most probably
834 * went bad.
835 */
836int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum,
837 struct ubi_ec_hdr *ec_hdr)
838{
839 int err;
840 uint32_t crc;
841
842 dbg_io("write EC header to PEB %d", pnum);
843 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
844
845 ec_hdr->magic = cpu_to_be32(UBI_EC_HDR_MAGIC);
846 ec_hdr->version = UBI_VERSION;
847 ec_hdr->vid_hdr_offset = cpu_to_be32(ubi->vid_hdr_offset);
848 ec_hdr->data_offset = cpu_to_be32(ubi->leb_start);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200849 ec_hdr->image_seq = cpu_to_be32(ubi->image_seq);
Kyungmin Parka5983452008-11-19 16:26:54 +0100850 crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
851 ec_hdr->hdr_crc = cpu_to_be32(crc);
852
Heiko Schocherf5895d12014-06-24 10:10:04 +0200853 err = self_check_ec_hdr(ubi, pnum, ec_hdr);
Kyungmin Parka5983452008-11-19 16:26:54 +0100854 if (err)
Heiko Schocherf5895d12014-06-24 10:10:04 +0200855 return err;
Kyungmin Parka5983452008-11-19 16:26:54 +0100856
Heiko Schocher94b66de2015-10-22 06:19:21 +0200857 if (ubi_dbg_power_cut(ubi, POWER_CUT_EC_WRITE))
858 return -EROFS;
859
Kyungmin Parka5983452008-11-19 16:26:54 +0100860 err = ubi_io_write(ubi, ec_hdr, pnum, 0, ubi->ec_hdr_alsize);
861 return err;
862}
863
864/**
865 * validate_vid_hdr - validate a volume identifier header.
866 * @ubi: UBI device description object
867 * @vid_hdr: the volume identifier header to check
868 *
869 * This function checks that data stored in the volume identifier header
870 * @vid_hdr. Returns zero if the VID header is OK and %1 if not.
871 */
872static int validate_vid_hdr(const struct ubi_device *ubi,
873 const struct ubi_vid_hdr *vid_hdr)
874{
875 int vol_type = vid_hdr->vol_type;
876 int copy_flag = vid_hdr->copy_flag;
877 int vol_id = be32_to_cpu(vid_hdr->vol_id);
878 int lnum = be32_to_cpu(vid_hdr->lnum);
879 int compat = vid_hdr->compat;
880 int data_size = be32_to_cpu(vid_hdr->data_size);
881 int used_ebs = be32_to_cpu(vid_hdr->used_ebs);
882 int data_pad = be32_to_cpu(vid_hdr->data_pad);
883 int data_crc = be32_to_cpu(vid_hdr->data_crc);
884 int usable_leb_size = ubi->leb_size - data_pad;
885
886 if (copy_flag != 0 && copy_flag != 1) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200887 ubi_err(ubi, "bad copy_flag");
Kyungmin Parka5983452008-11-19 16:26:54 +0100888 goto bad;
889 }
890
891 if (vol_id < 0 || lnum < 0 || data_size < 0 || used_ebs < 0 ||
892 data_pad < 0) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200893 ubi_err(ubi, "negative values");
Kyungmin Parka5983452008-11-19 16:26:54 +0100894 goto bad;
895 }
896
897 if (vol_id >= UBI_MAX_VOLUMES && vol_id < UBI_INTERNAL_VOL_START) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200898 ubi_err(ubi, "bad vol_id");
Kyungmin Parka5983452008-11-19 16:26:54 +0100899 goto bad;
900 }
901
902 if (vol_id < UBI_INTERNAL_VOL_START && compat != 0) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200903 ubi_err(ubi, "bad compat");
Kyungmin Parka5983452008-11-19 16:26:54 +0100904 goto bad;
905 }
906
907 if (vol_id >= UBI_INTERNAL_VOL_START && compat != UBI_COMPAT_DELETE &&
908 compat != UBI_COMPAT_RO && compat != UBI_COMPAT_PRESERVE &&
909 compat != UBI_COMPAT_REJECT) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200910 ubi_err(ubi, "bad compat");
Kyungmin Parka5983452008-11-19 16:26:54 +0100911 goto bad;
912 }
913
914 if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200915 ubi_err(ubi, "bad vol_type");
Kyungmin Parka5983452008-11-19 16:26:54 +0100916 goto bad;
917 }
918
919 if (data_pad >= ubi->leb_size / 2) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200920 ubi_err(ubi, "bad data_pad");
Kyungmin Parka5983452008-11-19 16:26:54 +0100921 goto bad;
922 }
923
924 if (vol_type == UBI_VID_STATIC) {
925 /*
926 * Although from high-level point of view static volumes may
927 * contain zero bytes of data, but no VID headers can contain
928 * zero at these fields, because they empty volumes do not have
929 * mapped logical eraseblocks.
930 */
931 if (used_ebs == 0) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200932 ubi_err(ubi, "zero used_ebs");
Kyungmin Parka5983452008-11-19 16:26:54 +0100933 goto bad;
934 }
935 if (data_size == 0) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200936 ubi_err(ubi, "zero data_size");
Kyungmin Parka5983452008-11-19 16:26:54 +0100937 goto bad;
938 }
939 if (lnum < used_ebs - 1) {
940 if (data_size != usable_leb_size) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200941 ubi_err(ubi, "bad data_size");
Kyungmin Parka5983452008-11-19 16:26:54 +0100942 goto bad;
943 }
944 } else if (lnum == used_ebs - 1) {
945 if (data_size == 0) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200946 ubi_err(ubi, "bad data_size at last LEB");
Kyungmin Parka5983452008-11-19 16:26:54 +0100947 goto bad;
948 }
949 } else {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200950 ubi_err(ubi, "too high lnum");
Kyungmin Parka5983452008-11-19 16:26:54 +0100951 goto bad;
952 }
953 } else {
954 if (copy_flag == 0) {
955 if (data_crc != 0) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200956 ubi_err(ubi, "non-zero data CRC");
Kyungmin Parka5983452008-11-19 16:26:54 +0100957 goto bad;
958 }
959 if (data_size != 0) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200960 ubi_err(ubi, "non-zero data_size");
Kyungmin Parka5983452008-11-19 16:26:54 +0100961 goto bad;
962 }
963 } else {
964 if (data_size == 0) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200965 ubi_err(ubi, "zero data_size of copy");
Kyungmin Parka5983452008-11-19 16:26:54 +0100966 goto bad;
967 }
968 }
969 if (used_ebs != 0) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200970 ubi_err(ubi, "bad used_ebs");
Kyungmin Parka5983452008-11-19 16:26:54 +0100971 goto bad;
972 }
973 }
974
975 return 0;
976
977bad:
Heiko Schocher94b66de2015-10-22 06:19:21 +0200978 ubi_err(ubi, "bad VID header");
Heiko Schocherf5895d12014-06-24 10:10:04 +0200979 ubi_dump_vid_hdr(vid_hdr);
980 dump_stack();
Kyungmin Parka5983452008-11-19 16:26:54 +0100981 return 1;
982}
983
984/**
985 * ubi_io_read_vid_hdr - read and check a volume identifier header.
986 * @ubi: UBI device description object
987 * @pnum: physical eraseblock number to read from
988 * @vid_hdr: &struct ubi_vid_hdr object where to store the read volume
989 * identifier header
990 * @verbose: be verbose if the header is corrupted or wasn't found
991 *
992 * This function reads the volume identifier header from physical eraseblock
993 * @pnum and stores it in @vid_hdr. It also checks CRC checksum of the read
Heiko Schocherf5895d12014-06-24 10:10:04 +0200994 * volume identifier header. The error codes are the same as in
995 * 'ubi_io_read_ec_hdr()'.
Kyungmin Parka5983452008-11-19 16:26:54 +0100996 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200997 * Note, the implementation of this function is also very similar to
998 * 'ubi_io_read_ec_hdr()', so refer commentaries in 'ubi_io_read_ec_hdr()'.
Kyungmin Parka5983452008-11-19 16:26:54 +0100999 */
1000int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
1001 struct ubi_vid_hdr *vid_hdr, int verbose)
1002{
Heiko Schocherf5895d12014-06-24 10:10:04 +02001003 int err, read_err;
Kyungmin Parka5983452008-11-19 16:26:54 +01001004 uint32_t crc, magic, hdr_crc;
1005 void *p;
1006
1007 dbg_io("read VID header from PEB %d", pnum);
1008 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
Kyungmin Parka5983452008-11-19 16:26:54 +01001009
1010 p = (char *)vid_hdr - ubi->vid_hdr_shift;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001011 read_err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset,
Kyungmin Parka5983452008-11-19 16:26:54 +01001012 ubi->vid_hdr_alsize);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001013 if (read_err && read_err != UBI_IO_BITFLIPS && !mtd_is_eccerr(read_err))
1014 return read_err;
Kyungmin Parka5983452008-11-19 16:26:54 +01001015
1016 magic = be32_to_cpu(vid_hdr->magic);
1017 if (magic != UBI_VID_HDR_MAGIC) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02001018 if (mtd_is_eccerr(read_err))
1019 return UBI_IO_BAD_HDR_EBADMSG;
Kyungmin Parka5983452008-11-19 16:26:54 +01001020
Heiko Schocherf5895d12014-06-24 10:10:04 +02001021 if (ubi_check_pattern(vid_hdr, 0xFF, UBI_VID_HDR_SIZE)) {
Kyungmin Parka5983452008-11-19 16:26:54 +01001022 if (verbose)
Heiko Schocher94b66de2015-10-22 06:19:21 +02001023 ubi_warn(ubi, "no VID header found at PEB %d, only 0xFF bytes",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001024 pnum);
1025 dbg_bld("no VID header found at PEB %d, only 0xFF bytes",
1026 pnum);
1027 if (!read_err)
1028 return UBI_IO_FF;
1029 else
1030 return UBI_IO_FF_BITFLIPS;
Kyungmin Parka5983452008-11-19 16:26:54 +01001031 }
1032
Kyungmin Parka5983452008-11-19 16:26:54 +01001033 if (verbose) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001034 ubi_warn(ubi, "bad magic number at PEB %d: %08x instead of %08x",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001035 pnum, magic, UBI_VID_HDR_MAGIC);
1036 ubi_dump_vid_hdr(vid_hdr);
Kyungmin Parka5983452008-11-19 16:26:54 +01001037 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001038 dbg_bld("bad magic number at PEB %d: %08x instead of %08x",
1039 pnum, magic, UBI_VID_HDR_MAGIC);
1040 return UBI_IO_BAD_HDR;
Kyungmin Parka5983452008-11-19 16:26:54 +01001041 }
1042
1043 crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC);
1044 hdr_crc = be32_to_cpu(vid_hdr->hdr_crc);
1045
1046 if (hdr_crc != crc) {
1047 if (verbose) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001048 ubi_warn(ubi, "bad CRC at PEB %d, calculated %#08x, read %#08x",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001049 pnum, crc, hdr_crc);
1050 ubi_dump_vid_hdr(vid_hdr);
Kyungmin Parka5983452008-11-19 16:26:54 +01001051 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001052 dbg_bld("bad CRC at PEB %d, calculated %#08x, read %#08x",
1053 pnum, crc, hdr_crc);
1054 if (!read_err)
1055 return UBI_IO_BAD_HDR;
1056 else
1057 return UBI_IO_BAD_HDR_EBADMSG;
Kyungmin Parka5983452008-11-19 16:26:54 +01001058 }
1059
Kyungmin Parka5983452008-11-19 16:26:54 +01001060 err = validate_vid_hdr(ubi, vid_hdr);
1061 if (err) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001062 ubi_err(ubi, "validation failed for PEB %d", pnum);
Kyungmin Parka5983452008-11-19 16:26:54 +01001063 return -EINVAL;
1064 }
1065
1066 return read_err ? UBI_IO_BITFLIPS : 0;
1067}
1068
1069/**
1070 * ubi_io_write_vid_hdr - write a volume identifier header.
1071 * @ubi: UBI device description object
1072 * @pnum: the physical eraseblock number to write to
1073 * @vid_hdr: the volume identifier header to write
1074 *
1075 * This function writes the volume identifier header described by @vid_hdr to
1076 * physical eraseblock @pnum. This function automatically fills the
1077 * @vid_hdr->magic and the @vid_hdr->version fields, as well as calculates
1078 * header CRC checksum and stores it at vid_hdr->hdr_crc.
1079 *
1080 * This function returns zero in case of success and a negative error code in
1081 * case of failure. If %-EIO is returned, the physical eraseblock probably went
1082 * bad.
1083 */
1084int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
1085 struct ubi_vid_hdr *vid_hdr)
1086{
1087 int err;
1088 uint32_t crc;
1089 void *p;
1090
1091 dbg_io("write VID header to PEB %d", pnum);
1092 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
1093
Heiko Schocherf5895d12014-06-24 10:10:04 +02001094 err = self_check_peb_ec_hdr(ubi, pnum);
Kyungmin Parka5983452008-11-19 16:26:54 +01001095 if (err)
Heiko Schocherf5895d12014-06-24 10:10:04 +02001096 return err;
Kyungmin Parka5983452008-11-19 16:26:54 +01001097
1098 vid_hdr->magic = cpu_to_be32(UBI_VID_HDR_MAGIC);
1099 vid_hdr->version = UBI_VERSION;
1100 crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC);
1101 vid_hdr->hdr_crc = cpu_to_be32(crc);
1102
Heiko Schocherf5895d12014-06-24 10:10:04 +02001103 err = self_check_vid_hdr(ubi, pnum, vid_hdr);
Kyungmin Parka5983452008-11-19 16:26:54 +01001104 if (err)
Heiko Schocherf5895d12014-06-24 10:10:04 +02001105 return err;
Kyungmin Parka5983452008-11-19 16:26:54 +01001106
Heiko Schocher94b66de2015-10-22 06:19:21 +02001107 if (ubi_dbg_power_cut(ubi, POWER_CUT_VID_WRITE))
1108 return -EROFS;
1109
Kyungmin Parka5983452008-11-19 16:26:54 +01001110 p = (char *)vid_hdr - ubi->vid_hdr_shift;
1111 err = ubi_io_write(ubi, p, pnum, ubi->vid_hdr_aloffset,
1112 ubi->vid_hdr_alsize);
1113 return err;
1114}
1115
Kyungmin Parka5983452008-11-19 16:26:54 +01001116/**
Heiko Schocherf5895d12014-06-24 10:10:04 +02001117 * self_check_not_bad - ensure that a physical eraseblock is not bad.
Kyungmin Parka5983452008-11-19 16:26:54 +01001118 * @ubi: UBI device description object
1119 * @pnum: physical eraseblock number to check
1120 *
Heiko Schocherf5895d12014-06-24 10:10:04 +02001121 * This function returns zero if the physical eraseblock is good, %-EINVAL if
1122 * it is bad and a negative error code if an error occurred.
Kyungmin Parka5983452008-11-19 16:26:54 +01001123 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02001124static int self_check_not_bad(const struct ubi_device *ubi, int pnum)
Kyungmin Parka5983452008-11-19 16:26:54 +01001125{
1126 int err;
1127
Heiko Schocherf5895d12014-06-24 10:10:04 +02001128 if (!ubi_dbg_chk_io(ubi))
1129 return 0;
1130
Kyungmin Parka5983452008-11-19 16:26:54 +01001131 err = ubi_io_is_bad(ubi, pnum);
1132 if (!err)
1133 return err;
1134
Heiko Schocher94b66de2015-10-22 06:19:21 +02001135 ubi_err(ubi, "self-check failed for PEB %d", pnum);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001136 dump_stack();
1137 return err > 0 ? -EINVAL : err;
Kyungmin Parka5983452008-11-19 16:26:54 +01001138}
1139
1140/**
Heiko Schocherf5895d12014-06-24 10:10:04 +02001141 * self_check_ec_hdr - check if an erase counter header is all right.
Kyungmin Parka5983452008-11-19 16:26:54 +01001142 * @ubi: UBI device description object
1143 * @pnum: physical eraseblock number the erase counter header belongs to
1144 * @ec_hdr: the erase counter header to check
1145 *
1146 * This function returns zero if the erase counter header contains valid
Heiko Schocherf5895d12014-06-24 10:10:04 +02001147 * values, and %-EINVAL if not.
Kyungmin Parka5983452008-11-19 16:26:54 +01001148 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02001149static int self_check_ec_hdr(const struct ubi_device *ubi, int pnum,
1150 const struct ubi_ec_hdr *ec_hdr)
Kyungmin Parka5983452008-11-19 16:26:54 +01001151{
1152 int err;
1153 uint32_t magic;
1154
Heiko Schocherf5895d12014-06-24 10:10:04 +02001155 if (!ubi_dbg_chk_io(ubi))
1156 return 0;
1157
Kyungmin Parka5983452008-11-19 16:26:54 +01001158 magic = be32_to_cpu(ec_hdr->magic);
1159 if (magic != UBI_EC_HDR_MAGIC) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001160 ubi_err(ubi, "bad magic %#08x, must be %#08x",
Kyungmin Parka5983452008-11-19 16:26:54 +01001161 magic, UBI_EC_HDR_MAGIC);
1162 goto fail;
1163 }
1164
1165 err = validate_ec_hdr(ubi, ec_hdr);
1166 if (err) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001167 ubi_err(ubi, "self-check failed for PEB %d", pnum);
Kyungmin Parka5983452008-11-19 16:26:54 +01001168 goto fail;
1169 }
1170
1171 return 0;
1172
1173fail:
Heiko Schocherf5895d12014-06-24 10:10:04 +02001174 ubi_dump_ec_hdr(ec_hdr);
1175 dump_stack();
1176 return -EINVAL;
Kyungmin Parka5983452008-11-19 16:26:54 +01001177}
1178
1179/**
Heiko Schocherf5895d12014-06-24 10:10:04 +02001180 * self_check_peb_ec_hdr - check erase counter header.
Kyungmin Parka5983452008-11-19 16:26:54 +01001181 * @ubi: UBI device description object
1182 * @pnum: the physical eraseblock number to check
1183 *
Heiko Schocherf5895d12014-06-24 10:10:04 +02001184 * This function returns zero if the erase counter header is all right and and
1185 * a negative error code if not or if an error occurred.
Kyungmin Parka5983452008-11-19 16:26:54 +01001186 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02001187static int self_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum)
Kyungmin Parka5983452008-11-19 16:26:54 +01001188{
1189 int err;
1190 uint32_t crc, hdr_crc;
1191 struct ubi_ec_hdr *ec_hdr;
1192
Heiko Schocherf5895d12014-06-24 10:10:04 +02001193 if (!ubi_dbg_chk_io(ubi))
1194 return 0;
1195
Kyungmin Parka5983452008-11-19 16:26:54 +01001196 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
1197 if (!ec_hdr)
1198 return -ENOMEM;
1199
1200 err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001201 if (err && err != UBI_IO_BITFLIPS && !mtd_is_eccerr(err))
Kyungmin Parka5983452008-11-19 16:26:54 +01001202 goto exit;
1203
1204 crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
1205 hdr_crc = be32_to_cpu(ec_hdr->hdr_crc);
1206 if (hdr_crc != crc) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001207 ubi_err(ubi, "bad CRC, calculated %#08x, read %#08x",
1208 crc, hdr_crc);
1209 ubi_err(ubi, "self-check failed for PEB %d", pnum);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001210 ubi_dump_ec_hdr(ec_hdr);
1211 dump_stack();
1212 err = -EINVAL;
Kyungmin Parka5983452008-11-19 16:26:54 +01001213 goto exit;
1214 }
1215
Heiko Schocherf5895d12014-06-24 10:10:04 +02001216 err = self_check_ec_hdr(ubi, pnum, ec_hdr);
Kyungmin Parka5983452008-11-19 16:26:54 +01001217
1218exit:
1219 kfree(ec_hdr);
1220 return err;
1221}
1222
1223/**
Heiko Schocherf5895d12014-06-24 10:10:04 +02001224 * self_check_vid_hdr - check that a volume identifier header is all right.
Kyungmin Parka5983452008-11-19 16:26:54 +01001225 * @ubi: UBI device description object
1226 * @pnum: physical eraseblock number the volume identifier header belongs to
1227 * @vid_hdr: the volume identifier header to check
1228 *
1229 * This function returns zero if the volume identifier header is all right, and
Heiko Schocherf5895d12014-06-24 10:10:04 +02001230 * %-EINVAL if not.
Kyungmin Parka5983452008-11-19 16:26:54 +01001231 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02001232static int self_check_vid_hdr(const struct ubi_device *ubi, int pnum,
1233 const struct ubi_vid_hdr *vid_hdr)
Kyungmin Parka5983452008-11-19 16:26:54 +01001234{
1235 int err;
1236 uint32_t magic;
1237
Heiko Schocherf5895d12014-06-24 10:10:04 +02001238 if (!ubi_dbg_chk_io(ubi))
1239 return 0;
1240
Kyungmin Parka5983452008-11-19 16:26:54 +01001241 magic = be32_to_cpu(vid_hdr->magic);
1242 if (magic != UBI_VID_HDR_MAGIC) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001243 ubi_err(ubi, "bad VID header magic %#08x at PEB %d, must be %#08x",
Kyungmin Parka5983452008-11-19 16:26:54 +01001244 magic, pnum, UBI_VID_HDR_MAGIC);
1245 goto fail;
1246 }
1247
1248 err = validate_vid_hdr(ubi, vid_hdr);
1249 if (err) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001250 ubi_err(ubi, "self-check failed for PEB %d", pnum);
Kyungmin Parka5983452008-11-19 16:26:54 +01001251 goto fail;
1252 }
1253
1254 return err;
1255
1256fail:
Heiko Schocher94b66de2015-10-22 06:19:21 +02001257 ubi_err(ubi, "self-check failed for PEB %d", pnum);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001258 ubi_dump_vid_hdr(vid_hdr);
1259 dump_stack();
1260 return -EINVAL;
Kyungmin Parka5983452008-11-19 16:26:54 +01001261
1262}
1263
1264/**
Heiko Schocherf5895d12014-06-24 10:10:04 +02001265 * self_check_peb_vid_hdr - check volume identifier header.
Kyungmin Parka5983452008-11-19 16:26:54 +01001266 * @ubi: UBI device description object
1267 * @pnum: the physical eraseblock number to check
1268 *
1269 * This function returns zero if the volume identifier header is all right,
Heiko Schocherf5895d12014-06-24 10:10:04 +02001270 * and a negative error code if not or if an error occurred.
Kyungmin Parka5983452008-11-19 16:26:54 +01001271 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02001272static int self_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum)
Kyungmin Parka5983452008-11-19 16:26:54 +01001273{
1274 int err;
1275 uint32_t crc, hdr_crc;
1276 struct ubi_vid_hdr *vid_hdr;
1277 void *p;
1278
Heiko Schocherf5895d12014-06-24 10:10:04 +02001279 if (!ubi_dbg_chk_io(ubi))
1280 return 0;
1281
Kyungmin Parka5983452008-11-19 16:26:54 +01001282 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
1283 if (!vid_hdr)
1284 return -ENOMEM;
1285
1286 p = (char *)vid_hdr - ubi->vid_hdr_shift;
1287 err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset,
1288 ubi->vid_hdr_alsize);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001289 if (err && err != UBI_IO_BITFLIPS && !mtd_is_eccerr(err))
Kyungmin Parka5983452008-11-19 16:26:54 +01001290 goto exit;
1291
1292 crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_EC_HDR_SIZE_CRC);
1293 hdr_crc = be32_to_cpu(vid_hdr->hdr_crc);
1294 if (hdr_crc != crc) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001295 ubi_err(ubi, "bad VID header CRC at PEB %d, calculated %#08x, read %#08x",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001296 pnum, crc, hdr_crc);
Heiko Schocher94b66de2015-10-22 06:19:21 +02001297 ubi_err(ubi, "self-check failed for PEB %d", pnum);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001298 ubi_dump_vid_hdr(vid_hdr);
1299 dump_stack();
1300 err = -EINVAL;
Kyungmin Parka5983452008-11-19 16:26:54 +01001301 goto exit;
1302 }
1303
Heiko Schocherf5895d12014-06-24 10:10:04 +02001304 err = self_check_vid_hdr(ubi, pnum, vid_hdr);
Kyungmin Parka5983452008-11-19 16:26:54 +01001305
1306exit:
1307 ubi_free_vid_hdr(ubi, vid_hdr);
1308 return err;
1309}
1310
1311/**
Heiko Schocherf5895d12014-06-24 10:10:04 +02001312 * self_check_write - make sure write succeeded.
1313 * @ubi: UBI device description object
1314 * @buf: buffer with data which were written
1315 * @pnum: physical eraseblock number the data were written to
1316 * @offset: offset within the physical eraseblock the data were written to
1317 * @len: how many bytes were written
1318 *
1319 * This functions reads data which were recently written and compares it with
1320 * the original data buffer - the data have to match. Returns zero if the data
1321 * match and a negative error code if not or in case of failure.
1322 */
1323static int self_check_write(struct ubi_device *ubi, const void *buf, int pnum,
1324 int offset, int len)
1325{
1326 int err, i;
1327 size_t read;
1328 void *buf1;
1329 loff_t addr = (loff_t)pnum * ubi->peb_size + offset;
1330
1331 if (!ubi_dbg_chk_io(ubi))
1332 return 0;
1333
1334 buf1 = __vmalloc(len, GFP_NOFS, PAGE_KERNEL);
1335 if (!buf1) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001336 ubi_err(ubi, "cannot allocate memory to check writes");
Heiko Schocherf5895d12014-06-24 10:10:04 +02001337 return 0;
1338 }
1339
1340 err = mtd_read(ubi->mtd, addr, len, &read, buf1);
1341 if (err && !mtd_is_bitflip(err))
1342 goto out_free;
1343
1344 for (i = 0; i < len; i++) {
1345 uint8_t c = ((uint8_t *)buf)[i];
1346 uint8_t c1 = ((uint8_t *)buf1)[i];
1347#if !defined(CONFIG_UBI_SILENCE_MSG)
1348 int dump_len = max_t(int, 128, len - i);
1349#endif
1350
1351 if (c == c1)
1352 continue;
1353
Heiko Schocher94b66de2015-10-22 06:19:21 +02001354 ubi_err(ubi, "self-check failed for PEB %d:%d, len %d",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001355 pnum, offset, len);
Markus Klotzbuecher9c77e202019-05-15 15:15:56 +02001356#if !defined(CONFIG_UBI_SILENCE_MSG)
Heiko Schocher94b66de2015-10-22 06:19:21 +02001357 ubi_msg(ubi, "data differ at position %d", i);
Heiko Schocher94b66de2015-10-22 06:19:21 +02001358 ubi_msg(ubi, "hex dump of the original buffer from %d to %d",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001359 i, i + dump_len);
Alexey Brodkin2d2fa492018-06-05 17:17:57 +03001360 print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 1,
Heiko Schocherf5895d12014-06-24 10:10:04 +02001361 buf + i, dump_len, 1);
Heiko Schocher94b66de2015-10-22 06:19:21 +02001362 ubi_msg(ubi, "hex dump of the read buffer from %d to %d",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001363 i, i + dump_len);
Alexey Brodkin2d2fa492018-06-05 17:17:57 +03001364 print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 1,
Heiko Schocherf5895d12014-06-24 10:10:04 +02001365 buf1 + i, dump_len, 1);
Markus Klotzbuecher9c77e202019-05-15 15:15:56 +02001366#endif
Heiko Schocherf5895d12014-06-24 10:10:04 +02001367 dump_stack();
1368 err = -EINVAL;
1369 goto out_free;
1370 }
1371
1372 vfree(buf1);
1373 return 0;
1374
1375out_free:
1376 vfree(buf1);
1377 return err;
1378}
1379
1380/**
1381 * ubi_self_check_all_ff - check that a region of flash is empty.
Kyungmin Parka5983452008-11-19 16:26:54 +01001382 * @ubi: UBI device description object
1383 * @pnum: the physical eraseblock number to check
1384 * @offset: the starting offset within the physical eraseblock to check
1385 * @len: the length of the region to check
1386 *
1387 * This function returns zero if only 0xFF bytes are present at offset
Heiko Schocherf5895d12014-06-24 10:10:04 +02001388 * @offset of the physical eraseblock @pnum, and a negative error code if not
1389 * or if an error occurred.
Kyungmin Parka5983452008-11-19 16:26:54 +01001390 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02001391int ubi_self_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len)
Kyungmin Parka5983452008-11-19 16:26:54 +01001392{
1393 size_t read;
1394 int err;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001395 void *buf;
Kyungmin Parka5983452008-11-19 16:26:54 +01001396 loff_t addr = (loff_t)pnum * ubi->peb_size + offset;
1397
Heiko Schocherf5895d12014-06-24 10:10:04 +02001398 if (!ubi_dbg_chk_io(ubi))
1399 return 0;
1400
1401 buf = __vmalloc(len, GFP_NOFS, PAGE_KERNEL);
1402 if (!buf) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001403 ubi_err(ubi, "cannot allocate memory to check for 0xFFs");
Heiko Schocherf5895d12014-06-24 10:10:04 +02001404 return 0;
1405 }
1406
1407 err = mtd_read(ubi->mtd, addr, len, &read, buf);
1408 if (err && !mtd_is_bitflip(err)) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001409 ubi_err(ubi, "err %d while reading %d bytes from PEB %d:%d, read %zd bytes",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001410 err, len, pnum, offset, read);
Kyungmin Parka5983452008-11-19 16:26:54 +01001411 goto error;
1412 }
1413
Heiko Schocherf5895d12014-06-24 10:10:04 +02001414 err = ubi_check_pattern(buf, 0xFF, len);
Kyungmin Parka5983452008-11-19 16:26:54 +01001415 if (err == 0) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001416 ubi_err(ubi, "flash region at PEB %d:%d, length %d does not contain all 0xFF bytes",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001417 pnum, offset, len);
Kyungmin Parka5983452008-11-19 16:26:54 +01001418 goto fail;
1419 }
Kyungmin Parka5983452008-11-19 16:26:54 +01001420
Heiko Schocherf5895d12014-06-24 10:10:04 +02001421 vfree(buf);
Kyungmin Parka5983452008-11-19 16:26:54 +01001422 return 0;
1423
1424fail:
Heiko Schocher94b66de2015-10-22 06:19:21 +02001425 ubi_err(ubi, "self-check failed for PEB %d", pnum);
1426 ubi_msg(ubi, "hex dump of the %d-%d region", offset, offset + len);
Alexey Brodkin2d2fa492018-06-05 17:17:57 +03001427 print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 1, buf, len, 1);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001428 err = -EINVAL;
Kyungmin Parka5983452008-11-19 16:26:54 +01001429error:
Heiko Schocherf5895d12014-06-24 10:10:04 +02001430 dump_stack();
1431 vfree(buf);
Kyungmin Parka5983452008-11-19 16:26:54 +01001432 return err;
1433}