blob: 6c5441e8791da4bfa101f3420f67be335287f6c2 [file] [log] [blame]
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001/*
2 * Copyright (c) International Business Machines Corp., 2006
3 * Copyright (c) Nokia Corporation, 2006, 2007
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * Author: Artem Bityutskiy (Битюцкий Артём)
20 */
21
22/*
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +030023 * UBI input/output sub-system.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040024 *
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +030025 * This sub-system provides a uniform way to work with all kinds of the
26 * underlying MTD devices. It also implements handy functions for reading and
27 * writing UBI headers.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040028 *
29 * We are trying to have a paranoid mindset and not to trust to what we read
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +030030 * from the flash media in order to be more secure and robust. So this
31 * sub-system validates every single header it reads from the flash media.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040032 *
33 * Some words about how the eraseblock headers are stored.
34 *
35 * The erase counter header is always stored at offset zero. By default, the
36 * VID header is stored after the EC header at the closest aligned offset
37 * (i.e. aligned to the minimum I/O unit size). Data starts next to the VID
38 * header at the closest aligned offset. But this default layout may be
39 * changed. For example, for different reasons (e.g., optimization) UBI may be
40 * asked to put the VID header at further offset, and even at an unaligned
41 * offset. Of course, if the offset of the VID header is unaligned, UBI adds
42 * proper padding in front of it. Data offset may also be changed but it has to
43 * be aligned.
44 *
45 * About minimal I/O units. In general, UBI assumes flash device model where
46 * there is only one minimal I/O unit size. E.g., in case of NOR flash it is 1,
47 * in case of NAND flash it is a NAND page, etc. This is reported by MTD in the
48 * @ubi->mtd->writesize field. But as an exception, UBI admits of using another
49 * (smaller) minimal I/O unit size for EC and VID headers to make it possible
50 * to do different optimizations.
51 *
52 * This is extremely useful in case of NAND flashes which admit of several
53 * write operations to one NAND page. In this case UBI can fit EC and VID
54 * headers at one NAND page. Thus, UBI may use "sub-page" size as the minimal
55 * I/O unit for the headers (the @ubi->hdrs_min_io_size field). But it still
56 * reports NAND page size (@ubi->min_io_size) as a minimal I/O unit for the UBI
57 * users.
58 *
59 * Example: some Samsung NANDs with 2KiB pages allow 4x 512-byte writes, so
60 * although the minimal I/O unit is 2K, UBI uses 512 bytes for EC and VID
61 * headers.
62 *
63 * Q: why not just to treat sub-page as a minimal I/O unit of this flash
64 * device, e.g., make @ubi->min_io_size = 512 in the example above?
65 *
66 * A: because when writing a sub-page, MTD still writes a full 2K page but the
67 * bytes which are no relevant to the sub-page are 0xFF. So, basically, writing
68 * 4x512 sub-pages is 4 times slower then writing one 2KiB NAND page. Thus, we
69 * prefer to use sub-pages only for EV and VID headers.
70 *
71 * As it was noted above, the VID header may start at a non-aligned offset.
72 * For example, in case of a 2KiB page NAND flash with a 512 bytes sub-page,
73 * the VID header may reside at offset 1984 which is the last 64 bytes of the
74 * last sub-page (EC header is always at offset zero). This causes some
75 * difficulties when reading and writing VID headers.
76 *
77 * Suppose we have a 64-byte buffer and we read a VID header at it. We change
78 * the data and want to write this VID header out. As we can only write in
79 * 512-byte chunks, we have to allocate one more buffer and copy our VID header
80 * to offset 448 of this buffer.
81 *
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +030082 * The I/O sub-system does the following trick in order to avoid this extra
83 * copy. It always allocates a @ubi->vid_hdr_alsize bytes buffer for the VID
84 * header and returns a pointer to offset @ubi->vid_hdr_shift of this buffer.
85 * When the VID header is being written out, it shifts the VID header pointer
86 * back and writes the whole sub-page.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040087 */
88
89#include <linux/crc32.h>
90#include <linux/err.h>
91#include "ubi.h"
92
93#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
94static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum);
95static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum);
96static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum,
97 const struct ubi_ec_hdr *ec_hdr);
98static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum);
99static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum,
100 const struct ubi_vid_hdr *vid_hdr);
Artem Bityutskiyffb6b7e2009-05-12 15:43:44 +0300101static int paranoid_check_empty(struct ubi_device *ubi, int pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400102#else
103#define paranoid_check_not_bad(ubi, pnum) 0
104#define paranoid_check_peb_ec_hdr(ubi, pnum) 0
105#define paranoid_check_ec_hdr(ubi, pnum, ec_hdr) 0
106#define paranoid_check_peb_vid_hdr(ubi, pnum) 0
107#define paranoid_check_vid_hdr(ubi, pnum, vid_hdr) 0
Artem Bityutskiyffb6b7e2009-05-12 15:43:44 +0300108#define paranoid_check_empty(ubi, pnum) 0
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400109#endif
110
111/**
112 * ubi_io_read - read data from a physical eraseblock.
113 * @ubi: UBI device description object
114 * @buf: buffer where to store the read data
115 * @pnum: physical eraseblock number to read from
116 * @offset: offset within the physical eraseblock from where to read
117 * @len: how many bytes to read
118 *
119 * This function reads data from offset @offset of physical eraseblock @pnum
120 * and stores the read data in the @buf buffer. The following return codes are
121 * possible:
122 *
123 * o %0 if all the requested data were successfully read;
124 * o %UBI_IO_BITFLIPS if all the requested data were successfully read, but
125 * correctable bit-flips were detected; this is harmless but may indicate
126 * that this eraseblock may become bad soon (but do not have to);
Artem Bityutskiy63b6c1e2007-07-17 15:04:20 +0300127 * o %-EBADMSG if the MTD subsystem reported about data integrity problems, for
128 * example it can be an ECC error in case of NAND; this most probably means
129 * that the data is corrupted;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400130 * o %-EIO if some I/O error occurred;
131 * o other negative error codes in case of other errors.
132 */
133int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset,
134 int len)
135{
136 int err, retries = 0;
137 size_t read;
138 loff_t addr;
139
140 dbg_io("read %d bytes from PEB %d:%d", len, pnum, offset);
141
142 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
143 ubi_assert(offset >= 0 && offset + len <= ubi->peb_size);
144 ubi_assert(len > 0);
145
146 err = paranoid_check_not_bad(ubi, pnum);
147 if (err)
148 return err > 0 ? -EINVAL : err;
149
150 addr = (loff_t)pnum * ubi->peb_size + offset;
151retry:
152 err = ubi->mtd->read(ubi->mtd, addr, len, &read, buf);
153 if (err) {
154 if (err == -EUCLEAN) {
155 /*
156 * -EUCLEAN is reported if there was a bit-flip which
157 * was corrected, so this is harmless.
Artem Bityutskiy8c1e6ee2008-07-18 12:20:23 +0300158 *
159 * We do not report about it here unless debugging is
160 * enabled. A corresponding message will be printed
161 * later, when it is has been scrubbed.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400162 */
Artem Bityutskiy8c1e6ee2008-07-18 12:20:23 +0300163 dbg_msg("fixable bit-flip detected at PEB %d", pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400164 ubi_assert(len == read);
165 return UBI_IO_BITFLIPS;
166 }
167
168 if (read != len && retries++ < UBI_IO_RETRIES) {
Artem Bityutskiy9c9ec142008-07-18 13:19:52 +0300169 dbg_io("error %d while reading %d bytes from PEB %d:%d,"
170 " read only %zd bytes, retry",
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400171 err, len, pnum, offset, read);
172 yield();
173 goto retry;
174 }
175
176 ubi_err("error %d while reading %d bytes from PEB %d:%d, "
177 "read %zd bytes", err, len, pnum, offset, read);
178 ubi_dbg_dump_stack();
Artem Bityutskiy2362a532007-10-18 20:09:41 +0300179
180 /*
181 * The driver should never return -EBADMSG if it failed to read
182 * all the requested data. But some buggy drivers might do
183 * this, so we change it to -EIO.
184 */
185 if (read != len && err == -EBADMSG) {
186 ubi_assert(0);
187 err = -EIO;
188 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400189 } else {
190 ubi_assert(len == read);
191
192 if (ubi_dbg_is_bitflip()) {
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300193 dbg_gen("bit-flip (emulated)");
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400194 err = UBI_IO_BITFLIPS;
195 }
196 }
197
198 return err;
199}
200
201/**
202 * ubi_io_write - write data to a physical eraseblock.
203 * @ubi: UBI device description object
204 * @buf: buffer with the data to write
205 * @pnum: physical eraseblock number to write to
206 * @offset: offset within the physical eraseblock where to write
207 * @len: how many bytes to write
208 *
209 * This function writes @len bytes of data from buffer @buf to offset @offset
210 * of physical eraseblock @pnum. If all the data were successfully written,
211 * zero is returned. If an error occurred, this function returns a negative
212 * error code. If %-EIO is returned, the physical eraseblock most probably went
213 * bad.
214 *
215 * Note, in case of an error, it is possible that something was still written
216 * to the flash media, but may be some garbage.
217 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300218int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
219 int len)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400220{
221 int err;
222 size_t written;
223 loff_t addr;
224
225 dbg_io("write %d bytes to PEB %d:%d", len, pnum, offset);
226
227 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
228 ubi_assert(offset >= 0 && offset + len <= ubi->peb_size);
229 ubi_assert(offset % ubi->hdrs_min_io_size == 0);
230 ubi_assert(len > 0 && len % ubi->hdrs_min_io_size == 0);
231
232 if (ubi->ro_mode) {
233 ubi_err("read-only mode");
234 return -EROFS;
235 }
236
237 /* The below has to be compiled out if paranoid checks are disabled */
238
239 err = paranoid_check_not_bad(ubi, pnum);
240 if (err)
241 return err > 0 ? -EINVAL : err;
242
243 /* The area we are writing to has to contain all 0xFF bytes */
Artem Bityutskiy40a71a82009-06-28 19:16:55 +0300244 err = ubi_dbg_check_all_ff(ubi, pnum, offset, len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400245 if (err)
246 return err > 0 ? -EINVAL : err;
247
248 if (offset >= ubi->leb_start) {
249 /*
250 * We write to the data area of the physical eraseblock. Make
251 * sure it has valid EC and VID headers.
252 */
253 err = paranoid_check_peb_ec_hdr(ubi, pnum);
254 if (err)
255 return err > 0 ? -EINVAL : err;
256 err = paranoid_check_peb_vid_hdr(ubi, pnum);
257 if (err)
258 return err > 0 ? -EINVAL : err;
259 }
260
261 if (ubi_dbg_is_write_failure()) {
262 dbg_err("cannot write %d bytes to PEB %d:%d "
263 "(emulated)", len, pnum, offset);
264 ubi_dbg_dump_stack();
265 return -EIO;
266 }
267
268 addr = (loff_t)pnum * ubi->peb_size + offset;
269 err = ubi->mtd->write(ubi->mtd, addr, len, &written, buf);
270 if (err) {
271 ubi_err("error %d while writing %d bytes to PEB %d:%d, written"
272 " %zd bytes", err, len, pnum, offset, written);
273 ubi_dbg_dump_stack();
274 } else
275 ubi_assert(written == len);
276
277 return err;
278}
279
280/**
281 * erase_callback - MTD erasure call-back.
282 * @ei: MTD erase information object.
283 *
284 * Note, even though MTD erase interface is asynchronous, all the current
285 * implementations are synchronous anyway.
286 */
287static void erase_callback(struct erase_info *ei)
288{
289 wake_up_interruptible((wait_queue_head_t *)ei->priv);
290}
291
292/**
293 * do_sync_erase - synchronously erase a physical eraseblock.
294 * @ubi: UBI device description object
295 * @pnum: the physical eraseblock number to erase
296 *
297 * This function synchronously erases physical eraseblock @pnum and returns
298 * zero in case of success and a negative error code in case of failure. If
299 * %-EIO is returned, the physical eraseblock most probably went bad.
300 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300301static int do_sync_erase(struct ubi_device *ubi, int pnum)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400302{
303 int err, retries = 0;
304 struct erase_info ei;
305 wait_queue_head_t wq;
306
307 dbg_io("erase PEB %d", pnum);
308
309retry:
310 init_waitqueue_head(&wq);
311 memset(&ei, 0, sizeof(struct erase_info));
312
313 ei.mtd = ubi->mtd;
Brijesh Singh2f176f72007-07-05 15:07:35 +0530314 ei.addr = (loff_t)pnum * ubi->peb_size;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400315 ei.len = ubi->peb_size;
316 ei.callback = erase_callback;
317 ei.priv = (unsigned long)&wq;
318
319 err = ubi->mtd->erase(ubi->mtd, &ei);
320 if (err) {
321 if (retries++ < UBI_IO_RETRIES) {
322 dbg_io("error %d while erasing PEB %d, retry",
323 err, pnum);
324 yield();
325 goto retry;
326 }
327 ubi_err("cannot erase PEB %d, error %d", pnum, err);
328 ubi_dbg_dump_stack();
329 return err;
330 }
331
332 err = wait_event_interruptible(wq, ei.state == MTD_ERASE_DONE ||
333 ei.state == MTD_ERASE_FAILED);
334 if (err) {
335 ubi_err("interrupted PEB %d erasure", pnum);
336 return -EINTR;
337 }
338
339 if (ei.state == MTD_ERASE_FAILED) {
340 if (retries++ < UBI_IO_RETRIES) {
341 dbg_io("error while erasing PEB %d, retry", pnum);
342 yield();
343 goto retry;
344 }
345 ubi_err("cannot erase PEB %d", pnum);
346 ubi_dbg_dump_stack();
347 return -EIO;
348 }
349
Artem Bityutskiy40a71a82009-06-28 19:16:55 +0300350 err = ubi_dbg_check_all_ff(ubi, pnum, 0, ubi->peb_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400351 if (err)
352 return err > 0 ? -EINVAL : err;
353
354 if (ubi_dbg_is_erase_failure() && !err) {
355 dbg_err("cannot erase PEB %d (emulated)", pnum);
356 return -EIO;
357 }
358
359 return 0;
360}
361
362/**
363 * check_pattern - check if buffer contains only a certain byte pattern.
364 * @buf: buffer to check
365 * @patt: the pattern to check
366 * @size: buffer size in bytes
367 *
368 * This function returns %1 in there are only @patt bytes in @buf, and %0 if
369 * something else was also found.
370 */
371static int check_pattern(const void *buf, uint8_t patt, int size)
372{
373 int i;
374
375 for (i = 0; i < size; i++)
376 if (((const uint8_t *)buf)[i] != patt)
377 return 0;
378 return 1;
379}
380
381/* Patterns to write to a physical eraseblock when torturing it */
382static uint8_t patterns[] = {0xa5, 0x5a, 0x0};
383
384/**
385 * torture_peb - test a supposedly bad physical eraseblock.
386 * @ubi: UBI device description object
387 * @pnum: the physical eraseblock number to test
388 *
389 * This function returns %-EIO if the physical eraseblock did not pass the
390 * test, a positive number of erase operations done if the test was
391 * successfully passed, and other negative error codes in case of other errors.
392 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300393static int torture_peb(struct ubi_device *ubi, int pnum)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400394{
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400395 int err, i, patt_count;
396
Artem Bityutskiy8c1e6ee2008-07-18 12:20:23 +0300397 ubi_msg("run torture test for PEB %d", pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400398 patt_count = ARRAY_SIZE(patterns);
399 ubi_assert(patt_count > 0);
400
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300401 mutex_lock(&ubi->buf_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400402 for (i = 0; i < patt_count; i++) {
403 err = do_sync_erase(ubi, pnum);
404 if (err)
405 goto out;
406
407 /* Make sure the PEB contains only 0xFF bytes */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300408 err = ubi_io_read(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400409 if (err)
410 goto out;
411
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300412 err = check_pattern(ubi->peb_buf1, 0xFF, ubi->peb_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400413 if (err == 0) {
414 ubi_err("erased PEB %d, but a non-0xFF byte found",
415 pnum);
416 err = -EIO;
417 goto out;
418 }
419
420 /* Write a pattern and check it */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300421 memset(ubi->peb_buf1, patterns[i], ubi->peb_size);
422 err = ubi_io_write(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400423 if (err)
424 goto out;
425
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300426 memset(ubi->peb_buf1, ~patterns[i], ubi->peb_size);
427 err = ubi_io_read(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400428 if (err)
429 goto out;
430
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300431 err = check_pattern(ubi->peb_buf1, patterns[i], ubi->peb_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400432 if (err == 0) {
433 ubi_err("pattern %x checking failed for PEB %d",
434 patterns[i], pnum);
435 err = -EIO;
436 goto out;
437 }
438 }
439
440 err = patt_count;
Artem Bityutskiy8c1e6ee2008-07-18 12:20:23 +0300441 ubi_msg("PEB %d passed torture test, do not mark it a bad", pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400442
443out:
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300444 mutex_unlock(&ubi->buf_mutex);
Artem Bityutskiy8d2d4012007-07-22 22:32:51 +0300445 if (err == UBI_IO_BITFLIPS || err == -EBADMSG) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400446 /*
447 * If a bit-flip or data integrity error was detected, the test
448 * has not passed because it happened on a freshly erased
449 * physical eraseblock which means something is wrong with it.
450 */
Artem Bityutskiy8d2d4012007-07-22 22:32:51 +0300451 ubi_err("read problems on freshly erased PEB %d, must be bad",
452 pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400453 err = -EIO;
Artem Bityutskiy8d2d4012007-07-22 22:32:51 +0300454 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400455 return err;
456}
457
458/**
459 * ubi_io_sync_erase - synchronously erase a physical eraseblock.
460 * @ubi: UBI device description object
461 * @pnum: physical eraseblock number to erase
462 * @torture: if this physical eraseblock has to be tortured
463 *
464 * This function synchronously erases physical eraseblock @pnum. If @torture
465 * flag is not zero, the physical eraseblock is checked by means of writing
466 * different patterns to it and reading them back. If the torturing is enabled,
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200467 * the physical eraseblock is erased more than once.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400468 *
469 * This function returns the number of erasures made in case of success, %-EIO
470 * if the erasure failed or the torturing test failed, and other negative error
471 * codes in case of other errors. Note, %-EIO means that the physical
472 * eraseblock is bad.
473 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300474int ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400475{
476 int err, ret = 0;
477
478 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
479
480 err = paranoid_check_not_bad(ubi, pnum);
481 if (err != 0)
482 return err > 0 ? -EINVAL : err;
483
484 if (ubi->ro_mode) {
485 ubi_err("read-only mode");
486 return -EROFS;
487 }
488
489 if (torture) {
490 ret = torture_peb(ubi, pnum);
491 if (ret < 0)
492 return ret;
493 }
494
495 err = do_sync_erase(ubi, pnum);
496 if (err)
497 return err;
498
499 return ret + 1;
500}
501
502/**
503 * ubi_io_is_bad - check if a physical eraseblock is bad.
504 * @ubi: UBI device description object
505 * @pnum: the physical eraseblock number to check
506 *
507 * This function returns a positive number if the physical eraseblock is bad,
508 * zero if not, and a negative error code if an error occurred.
509 */
510int ubi_io_is_bad(const struct ubi_device *ubi, int pnum)
511{
512 struct mtd_info *mtd = ubi->mtd;
513
514 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
515
516 if (ubi->bad_allowed) {
517 int ret;
518
519 ret = mtd->block_isbad(mtd, (loff_t)pnum * ubi->peb_size);
520 if (ret < 0)
521 ubi_err("error %d while checking if PEB %d is bad",
522 ret, pnum);
523 else if (ret)
524 dbg_io("PEB %d is bad", pnum);
525 return ret;
526 }
527
528 return 0;
529}
530
531/**
532 * ubi_io_mark_bad - mark a physical eraseblock as bad.
533 * @ubi: UBI device description object
534 * @pnum: the physical eraseblock number to mark
535 *
536 * This function returns zero in case of success and a negative error code in
537 * case of failure.
538 */
539int ubi_io_mark_bad(const struct ubi_device *ubi, int pnum)
540{
541 int err;
542 struct mtd_info *mtd = ubi->mtd;
543
544 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
545
546 if (ubi->ro_mode) {
547 ubi_err("read-only mode");
548 return -EROFS;
549 }
550
551 if (!ubi->bad_allowed)
552 return 0;
553
554 err = mtd->block_markbad(mtd, (loff_t)pnum * ubi->peb_size);
555 if (err)
556 ubi_err("cannot mark PEB %d bad, error %d", pnum, err);
557 return err;
558}
559
560/**
561 * validate_ec_hdr - validate an erase counter header.
562 * @ubi: UBI device description object
563 * @ec_hdr: the erase counter header to check
564 *
565 * This function returns zero if the erase counter header is OK, and %1 if
566 * not.
567 */
568static int validate_ec_hdr(const struct ubi_device *ubi,
569 const struct ubi_ec_hdr *ec_hdr)
570{
571 long long ec;
572 int vid_hdr_offset, leb_start;
573
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300574 ec = be64_to_cpu(ec_hdr->ec);
575 vid_hdr_offset = be32_to_cpu(ec_hdr->vid_hdr_offset);
576 leb_start = be32_to_cpu(ec_hdr->data_offset);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400577
578 if (ec_hdr->version != UBI_VERSION) {
579 ubi_err("node with incompatible UBI version found: "
580 "this UBI version is %d, image version is %d",
581 UBI_VERSION, (int)ec_hdr->version);
582 goto bad;
583 }
584
585 if (vid_hdr_offset != ubi->vid_hdr_offset) {
586 ubi_err("bad VID header offset %d, expected %d",
587 vid_hdr_offset, ubi->vid_hdr_offset);
588 goto bad;
589 }
590
591 if (leb_start != ubi->leb_start) {
592 ubi_err("bad data offset %d, expected %d",
593 leb_start, ubi->leb_start);
594 goto bad;
595 }
596
597 if (ec < 0 || ec > UBI_MAX_ERASECOUNTER) {
598 ubi_err("bad erase counter %lld", ec);
599 goto bad;
600 }
601
602 return 0;
603
604bad:
605 ubi_err("bad EC header");
606 ubi_dbg_dump_ec_hdr(ec_hdr);
607 ubi_dbg_dump_stack();
608 return 1;
609}
610
611/**
612 * ubi_io_read_ec_hdr - read and check an erase counter header.
613 * @ubi: UBI device description object
614 * @pnum: physical eraseblock to read from
615 * @ec_hdr: a &struct ubi_ec_hdr object where to store the read erase counter
616 * header
617 * @verbose: be verbose if the header is corrupted or was not found
618 *
619 * This function reads erase counter header from physical eraseblock @pnum and
620 * stores it in @ec_hdr. This function also checks CRC checksum of the read
621 * erase counter header. The following codes may be returned:
622 *
623 * o %0 if the CRC checksum is correct and the header was successfully read;
624 * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected
625 * and corrected by the flash driver; this is harmless but may indicate that
626 * this eraseblock may become bad soon (but may be not);
627 * o %UBI_IO_BAD_EC_HDR if the erase counter header is corrupted (a CRC error);
628 * o %UBI_IO_PEB_EMPTY if the physical eraseblock is empty;
629 * o a negative error code in case of failure.
630 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300631int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400632 struct ubi_ec_hdr *ec_hdr, int verbose)
633{
634 int err, read_err = 0;
635 uint32_t crc, magic, hdr_crc;
636
637 dbg_io("read EC header from PEB %d", pnum);
638 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
639
640 err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE);
641 if (err) {
642 if (err != UBI_IO_BITFLIPS && err != -EBADMSG)
643 return err;
644
645 /*
646 * We read all the data, but either a correctable bit-flip
647 * occurred, or MTD reported about some data integrity error,
648 * like an ECC error in case of NAND. The former is harmless,
649 * the later may mean that the read data is corrupted. But we
650 * have a CRC check-sum and we will detect this. If the EC
651 * header is still OK, we just report this as there was a
652 * bit-flip.
653 */
654 read_err = err;
655 }
656
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300657 magic = be32_to_cpu(ec_hdr->magic);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400658 if (magic != UBI_EC_HDR_MAGIC) {
659 /*
660 * The magic field is wrong. Let's check if we have read all
661 * 0xFF. If yes, this physical eraseblock is assumed to be
662 * empty.
663 *
664 * But if there was a read error, we do not test it for all
665 * 0xFFs. Even if it does contain all 0xFFs, this error
666 * indicates that something is still wrong with this physical
667 * eraseblock and we anyway cannot treat it as empty.
668 */
669 if (read_err != -EBADMSG &&
670 check_pattern(ec_hdr, 0xFF, UBI_EC_HDR_SIZE)) {
671 /* The physical eraseblock is supposedly empty */
Artem Bityutskiy40a71a82009-06-28 19:16:55 +0300672 err = ubi_dbg_check_all_ff(ubi, pnum, 0, ubi->peb_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400673 if (err)
674 return err > 0 ? UBI_IO_BAD_EC_HDR : err;
675
676 if (verbose)
677 ubi_warn("no EC header found at PEB %d, "
678 "only 0xFF bytes", pnum);
Artem Bityutskiyed458192008-11-12 10:14:10 +0200679 else if (UBI_IO_DEBUG)
680 dbg_msg("no EC header found at PEB %d, "
681 "only 0xFF bytes", pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400682 return UBI_IO_PEB_EMPTY;
683 }
684
685 /*
686 * This is not a valid erase counter header, and these are not
687 * 0xFF bytes. Report that the header is corrupted.
688 */
689 if (verbose) {
690 ubi_warn("bad magic number at PEB %d: %08x instead of "
691 "%08x", pnum, magic, UBI_EC_HDR_MAGIC);
692 ubi_dbg_dump_ec_hdr(ec_hdr);
Artem Bityutskiyed458192008-11-12 10:14:10 +0200693 } else if (UBI_IO_DEBUG)
694 dbg_msg("bad magic number at PEB %d: %08x instead of "
695 "%08x", pnum, magic, UBI_EC_HDR_MAGIC);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400696 return UBI_IO_BAD_EC_HDR;
697 }
698
699 crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300700 hdr_crc = be32_to_cpu(ec_hdr->hdr_crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400701
702 if (hdr_crc != crc) {
703 if (verbose) {
Artem Bityutskiy9c9ec142008-07-18 13:19:52 +0300704 ubi_warn("bad EC header CRC at PEB %d, calculated "
705 "%#08x, read %#08x", pnum, crc, hdr_crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400706 ubi_dbg_dump_ec_hdr(ec_hdr);
Artem Bityutskiyed458192008-11-12 10:14:10 +0200707 } else if (UBI_IO_DEBUG)
708 dbg_msg("bad EC header CRC at PEB %d, calculated "
709 "%#08x, read %#08x", pnum, crc, hdr_crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400710 return UBI_IO_BAD_EC_HDR;
711 }
712
713 /* And of course validate what has just been read from the media */
714 err = validate_ec_hdr(ubi, ec_hdr);
715 if (err) {
716 ubi_err("validation failed for PEB %d", pnum);
717 return -EINVAL;
718 }
719
720 return read_err ? UBI_IO_BITFLIPS : 0;
721}
722
723/**
724 * ubi_io_write_ec_hdr - write an erase counter header.
725 * @ubi: UBI device description object
726 * @pnum: physical eraseblock to write to
727 * @ec_hdr: the erase counter header to write
728 *
729 * This function writes erase counter header described by @ec_hdr to physical
730 * eraseblock @pnum. It also fills most fields of @ec_hdr before writing, so
731 * the caller do not have to fill them. Callers must only fill the @ec_hdr->ec
732 * field.
733 *
734 * This function returns zero in case of success and a negative error code in
735 * case of failure. If %-EIO is returned, the physical eraseblock most probably
736 * went bad.
737 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300738int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400739 struct ubi_ec_hdr *ec_hdr)
740{
741 int err;
742 uint32_t crc;
743
744 dbg_io("write EC header to PEB %d", pnum);
745 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
746
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300747 ec_hdr->magic = cpu_to_be32(UBI_EC_HDR_MAGIC);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400748 ec_hdr->version = UBI_VERSION;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300749 ec_hdr->vid_hdr_offset = cpu_to_be32(ubi->vid_hdr_offset);
750 ec_hdr->data_offset = cpu_to_be32(ubi->leb_start);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400751 crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300752 ec_hdr->hdr_crc = cpu_to_be32(crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400753
754 err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr);
755 if (err)
756 return -EINVAL;
757
758 err = ubi_io_write(ubi, ec_hdr, pnum, 0, ubi->ec_hdr_alsize);
759 return err;
760}
761
762/**
763 * validate_vid_hdr - validate a volume identifier header.
764 * @ubi: UBI device description object
765 * @vid_hdr: the volume identifier header to check
766 *
767 * This function checks that data stored in the volume identifier header
768 * @vid_hdr. Returns zero if the VID header is OK and %1 if not.
769 */
770static int validate_vid_hdr(const struct ubi_device *ubi,
771 const struct ubi_vid_hdr *vid_hdr)
772{
773 int vol_type = vid_hdr->vol_type;
774 int copy_flag = vid_hdr->copy_flag;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300775 int vol_id = be32_to_cpu(vid_hdr->vol_id);
776 int lnum = be32_to_cpu(vid_hdr->lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400777 int compat = vid_hdr->compat;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300778 int data_size = be32_to_cpu(vid_hdr->data_size);
779 int used_ebs = be32_to_cpu(vid_hdr->used_ebs);
780 int data_pad = be32_to_cpu(vid_hdr->data_pad);
781 int data_crc = be32_to_cpu(vid_hdr->data_crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400782 int usable_leb_size = ubi->leb_size - data_pad;
783
784 if (copy_flag != 0 && copy_flag != 1) {
785 dbg_err("bad copy_flag");
786 goto bad;
787 }
788
789 if (vol_id < 0 || lnum < 0 || data_size < 0 || used_ebs < 0 ||
790 data_pad < 0) {
791 dbg_err("negative values");
792 goto bad;
793 }
794
795 if (vol_id >= UBI_MAX_VOLUMES && vol_id < UBI_INTERNAL_VOL_START) {
796 dbg_err("bad vol_id");
797 goto bad;
798 }
799
800 if (vol_id < UBI_INTERNAL_VOL_START && compat != 0) {
801 dbg_err("bad compat");
802 goto bad;
803 }
804
805 if (vol_id >= UBI_INTERNAL_VOL_START && compat != UBI_COMPAT_DELETE &&
806 compat != UBI_COMPAT_RO && compat != UBI_COMPAT_PRESERVE &&
807 compat != UBI_COMPAT_REJECT) {
808 dbg_err("bad compat");
809 goto bad;
810 }
811
812 if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) {
813 dbg_err("bad vol_type");
814 goto bad;
815 }
816
817 if (data_pad >= ubi->leb_size / 2) {
818 dbg_err("bad data_pad");
819 goto bad;
820 }
821
822 if (vol_type == UBI_VID_STATIC) {
823 /*
824 * Although from high-level point of view static volumes may
825 * contain zero bytes of data, but no VID headers can contain
826 * zero at these fields, because they empty volumes do not have
827 * mapped logical eraseblocks.
828 */
829 if (used_ebs == 0) {
830 dbg_err("zero used_ebs");
831 goto bad;
832 }
833 if (data_size == 0) {
834 dbg_err("zero data_size");
835 goto bad;
836 }
837 if (lnum < used_ebs - 1) {
838 if (data_size != usable_leb_size) {
839 dbg_err("bad data_size");
840 goto bad;
841 }
842 } else if (lnum == used_ebs - 1) {
843 if (data_size == 0) {
844 dbg_err("bad data_size at last LEB");
845 goto bad;
846 }
847 } else {
848 dbg_err("too high lnum");
849 goto bad;
850 }
851 } else {
852 if (copy_flag == 0) {
853 if (data_crc != 0) {
854 dbg_err("non-zero data CRC");
855 goto bad;
856 }
857 if (data_size != 0) {
858 dbg_err("non-zero data_size");
859 goto bad;
860 }
861 } else {
862 if (data_size == 0) {
863 dbg_err("zero data_size of copy");
864 goto bad;
865 }
866 }
867 if (used_ebs != 0) {
868 dbg_err("bad used_ebs");
869 goto bad;
870 }
871 }
872
873 return 0;
874
875bad:
876 ubi_err("bad VID header");
877 ubi_dbg_dump_vid_hdr(vid_hdr);
878 ubi_dbg_dump_stack();
879 return 1;
880}
881
882/**
883 * ubi_io_read_vid_hdr - read and check a volume identifier header.
884 * @ubi: UBI device description object
885 * @pnum: physical eraseblock number to read from
886 * @vid_hdr: &struct ubi_vid_hdr object where to store the read volume
887 * identifier header
888 * @verbose: be verbose if the header is corrupted or wasn't found
889 *
890 * This function reads the volume identifier header from physical eraseblock
891 * @pnum and stores it in @vid_hdr. It also checks CRC checksum of the read
892 * volume identifier header. The following codes may be returned:
893 *
894 * o %0 if the CRC checksum is correct and the header was successfully read;
895 * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected
896 * and corrected by the flash driver; this is harmless but may indicate that
897 * this eraseblock may become bad soon;
Artem Bityutskiy815bc5f2009-06-08 19:28:18 +0300898 * o %UBI_IO_BAD_VID_HDR if the volume identifier header is corrupted (a CRC
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400899 * error detected);
900 * o %UBI_IO_PEB_FREE if the physical eraseblock is free (i.e., there is no VID
901 * header there);
902 * o a negative error code in case of failure.
903 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300904int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400905 struct ubi_vid_hdr *vid_hdr, int verbose)
906{
907 int err, read_err = 0;
908 uint32_t crc, magic, hdr_crc;
909 void *p;
910
911 dbg_io("read VID header from PEB %d", pnum);
912 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
913
914 p = (char *)vid_hdr - ubi->vid_hdr_shift;
915 err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset,
916 ubi->vid_hdr_alsize);
917 if (err) {
918 if (err != UBI_IO_BITFLIPS && err != -EBADMSG)
919 return err;
920
921 /*
922 * We read all the data, but either a correctable bit-flip
923 * occurred, or MTD reported about some data integrity error,
924 * like an ECC error in case of NAND. The former is harmless,
925 * the later may mean the read data is corrupted. But we have a
926 * CRC check-sum and we will identify this. If the VID header is
927 * still OK, we just report this as there was a bit-flip.
928 */
929 read_err = err;
930 }
931
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300932 magic = be32_to_cpu(vid_hdr->magic);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400933 if (magic != UBI_VID_HDR_MAGIC) {
934 /*
935 * If we have read all 0xFF bytes, the VID header probably does
936 * not exist and the physical eraseblock is assumed to be free.
937 *
938 * But if there was a read error, we do not test the data for
939 * 0xFFs. Even if it does contain all 0xFFs, this error
940 * indicates that something is still wrong with this physical
941 * eraseblock and it cannot be regarded as free.
942 */
943 if (read_err != -EBADMSG &&
944 check_pattern(vid_hdr, 0xFF, UBI_VID_HDR_SIZE)) {
945 /* The physical eraseblock is supposedly free */
946
947 /*
948 * The below is just a paranoid check, it has to be
949 * compiled out if paranoid checks are disabled.
950 */
Artem Bityutskiyffb6b7e2009-05-12 15:43:44 +0300951 err = paranoid_check_empty(ubi, pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400952 if (err)
953 return err > 0 ? UBI_IO_BAD_VID_HDR : err;
954
955 if (verbose)
956 ubi_warn("no VID header found at PEB %d, "
957 "only 0xFF bytes", pnum);
Artem Bityutskiyed458192008-11-12 10:14:10 +0200958 else if (UBI_IO_DEBUG)
959 dbg_msg("no VID header found at PEB %d, "
960 "only 0xFF bytes", pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400961 return UBI_IO_PEB_FREE;
962 }
963
964 /*
965 * This is not a valid VID header, and these are not 0xFF
966 * bytes. Report that the header is corrupted.
967 */
968 if (verbose) {
969 ubi_warn("bad magic number at PEB %d: %08x instead of "
970 "%08x", pnum, magic, UBI_VID_HDR_MAGIC);
971 ubi_dbg_dump_vid_hdr(vid_hdr);
Artem Bityutskiyed458192008-11-12 10:14:10 +0200972 } else if (UBI_IO_DEBUG)
973 dbg_msg("bad magic number at PEB %d: %08x instead of "
974 "%08x", pnum, magic, UBI_VID_HDR_MAGIC);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400975 return UBI_IO_BAD_VID_HDR;
976 }
977
978 crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC);
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300979 hdr_crc = be32_to_cpu(vid_hdr->hdr_crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400980
981 if (hdr_crc != crc) {
982 if (verbose) {
983 ubi_warn("bad CRC at PEB %d, calculated %#08x, "
984 "read %#08x", pnum, crc, hdr_crc);
985 ubi_dbg_dump_vid_hdr(vid_hdr);
Artem Bityutskiyed458192008-11-12 10:14:10 +0200986 } else if (UBI_IO_DEBUG)
987 dbg_msg("bad CRC at PEB %d, calculated %#08x, "
988 "read %#08x", pnum, crc, hdr_crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400989 return UBI_IO_BAD_VID_HDR;
990 }
991
992 /* Validate the VID header that we have just read */
993 err = validate_vid_hdr(ubi, vid_hdr);
994 if (err) {
995 ubi_err("validation failed for PEB %d", pnum);
996 return -EINVAL;
997 }
998
999 return read_err ? UBI_IO_BITFLIPS : 0;
1000}
1001
1002/**
1003 * ubi_io_write_vid_hdr - write a volume identifier header.
1004 * @ubi: UBI device description object
1005 * @pnum: the physical eraseblock number to write to
1006 * @vid_hdr: the volume identifier header to write
1007 *
1008 * This function writes the volume identifier header described by @vid_hdr to
1009 * physical eraseblock @pnum. This function automatically fills the
1010 * @vid_hdr->magic and the @vid_hdr->version fields, as well as calculates
1011 * header CRC checksum and stores it at vid_hdr->hdr_crc.
1012 *
1013 * This function returns zero in case of success and a negative error code in
1014 * case of failure. If %-EIO is returned, the physical eraseblock probably went
1015 * bad.
1016 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001017int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001018 struct ubi_vid_hdr *vid_hdr)
1019{
1020 int err;
1021 uint32_t crc;
1022 void *p;
1023
1024 dbg_io("write VID header to PEB %d", pnum);
1025 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
1026
1027 err = paranoid_check_peb_ec_hdr(ubi, pnum);
1028 if (err)
Artem Bityutskiyf2863c52008-12-28 12:20:51 +02001029 return err > 0 ? -EINVAL : err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001030
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001031 vid_hdr->magic = cpu_to_be32(UBI_VID_HDR_MAGIC);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001032 vid_hdr->version = UBI_VERSION;
1033 crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC);
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001034 vid_hdr->hdr_crc = cpu_to_be32(crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001035
1036 err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr);
1037 if (err)
1038 return -EINVAL;
1039
1040 p = (char *)vid_hdr - ubi->vid_hdr_shift;
1041 err = ubi_io_write(ubi, p, pnum, ubi->vid_hdr_aloffset,
1042 ubi->vid_hdr_alsize);
1043 return err;
1044}
1045
1046#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
1047
1048/**
1049 * paranoid_check_not_bad - ensure that a physical eraseblock is not bad.
1050 * @ubi: UBI device description object
1051 * @pnum: physical eraseblock number to check
1052 *
1053 * This function returns zero if the physical eraseblock is good, a positive
1054 * number if it is bad and a negative error code if an error occurred.
1055 */
1056static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum)
1057{
1058 int err;
1059
1060 err = ubi_io_is_bad(ubi, pnum);
1061 if (!err)
1062 return err;
1063
1064 ubi_err("paranoid check failed for PEB %d", pnum);
1065 ubi_dbg_dump_stack();
1066 return err;
1067}
1068
1069/**
1070 * paranoid_check_ec_hdr - check if an erase counter header is all right.
1071 * @ubi: UBI device description object
1072 * @pnum: physical eraseblock number the erase counter header belongs to
1073 * @ec_hdr: the erase counter header to check
1074 *
1075 * This function returns zero if the erase counter header contains valid
1076 * values, and %1 if not.
1077 */
1078static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum,
1079 const struct ubi_ec_hdr *ec_hdr)
1080{
1081 int err;
1082 uint32_t magic;
1083
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001084 magic = be32_to_cpu(ec_hdr->magic);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001085 if (magic != UBI_EC_HDR_MAGIC) {
1086 ubi_err("bad magic %#08x, must be %#08x",
1087 magic, UBI_EC_HDR_MAGIC);
1088 goto fail;
1089 }
1090
1091 err = validate_ec_hdr(ubi, ec_hdr);
1092 if (err) {
1093 ubi_err("paranoid check failed for PEB %d", pnum);
1094 goto fail;
1095 }
1096
1097 return 0;
1098
1099fail:
1100 ubi_dbg_dump_ec_hdr(ec_hdr);
1101 ubi_dbg_dump_stack();
1102 return 1;
1103}
1104
1105/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +03001106 * paranoid_check_peb_ec_hdr - check erase counter header.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001107 * @ubi: UBI device description object
1108 * @pnum: the physical eraseblock number to check
1109 *
1110 * This function returns zero if the erase counter header is all right, %1 if
1111 * not, and a negative error code if an error occurred.
1112 */
1113static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum)
1114{
1115 int err;
1116 uint32_t crc, hdr_crc;
1117 struct ubi_ec_hdr *ec_hdr;
1118
Artem Bityutskiy33818bb2007-08-28 21:29:32 +03001119 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001120 if (!ec_hdr)
1121 return -ENOMEM;
1122
1123 err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE);
1124 if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG)
1125 goto exit;
1126
1127 crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001128 hdr_crc = be32_to_cpu(ec_hdr->hdr_crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001129 if (hdr_crc != crc) {
1130 ubi_err("bad CRC, calculated %#08x, read %#08x", crc, hdr_crc);
1131 ubi_err("paranoid check failed for PEB %d", pnum);
1132 ubi_dbg_dump_ec_hdr(ec_hdr);
1133 ubi_dbg_dump_stack();
1134 err = 1;
1135 goto exit;
1136 }
1137
1138 err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr);
1139
1140exit:
1141 kfree(ec_hdr);
1142 return err;
1143}
1144
1145/**
1146 * paranoid_check_vid_hdr - check that a volume identifier header is all right.
1147 * @ubi: UBI device description object
1148 * @pnum: physical eraseblock number the volume identifier header belongs to
1149 * @vid_hdr: the volume identifier header to check
1150 *
1151 * This function returns zero if the volume identifier header is all right, and
1152 * %1 if not.
1153 */
1154static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum,
1155 const struct ubi_vid_hdr *vid_hdr)
1156{
1157 int err;
1158 uint32_t magic;
1159
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001160 magic = be32_to_cpu(vid_hdr->magic);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001161 if (magic != UBI_VID_HDR_MAGIC) {
1162 ubi_err("bad VID header magic %#08x at PEB %d, must be %#08x",
1163 magic, pnum, UBI_VID_HDR_MAGIC);
1164 goto fail;
1165 }
1166
1167 err = validate_vid_hdr(ubi, vid_hdr);
1168 if (err) {
1169 ubi_err("paranoid check failed for PEB %d", pnum);
1170 goto fail;
1171 }
1172
1173 return err;
1174
1175fail:
1176 ubi_err("paranoid check failed for PEB %d", pnum);
1177 ubi_dbg_dump_vid_hdr(vid_hdr);
1178 ubi_dbg_dump_stack();
1179 return 1;
1180
1181}
1182
1183/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +03001184 * paranoid_check_peb_vid_hdr - check volume identifier header.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001185 * @ubi: UBI device description object
1186 * @pnum: the physical eraseblock number to check
1187 *
1188 * This function returns zero if the volume identifier header is all right,
1189 * %1 if not, and a negative error code if an error occurred.
1190 */
1191static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum)
1192{
1193 int err;
1194 uint32_t crc, hdr_crc;
1195 struct ubi_vid_hdr *vid_hdr;
1196 void *p;
1197
Artem Bityutskiy33818bb2007-08-28 21:29:32 +03001198 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001199 if (!vid_hdr)
1200 return -ENOMEM;
1201
1202 p = (char *)vid_hdr - ubi->vid_hdr_shift;
1203 err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset,
1204 ubi->vid_hdr_alsize);
1205 if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG)
1206 goto exit;
1207
1208 crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_EC_HDR_SIZE_CRC);
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001209 hdr_crc = be32_to_cpu(vid_hdr->hdr_crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001210 if (hdr_crc != crc) {
1211 ubi_err("bad VID header CRC at PEB %d, calculated %#08x, "
1212 "read %#08x", pnum, crc, hdr_crc);
1213 ubi_err("paranoid check failed for PEB %d", pnum);
1214 ubi_dbg_dump_vid_hdr(vid_hdr);
1215 ubi_dbg_dump_stack();
1216 err = 1;
1217 goto exit;
1218 }
1219
1220 err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr);
1221
1222exit:
1223 ubi_free_vid_hdr(ubi, vid_hdr);
1224 return err;
1225}
1226
1227/**
Artem Bityutskiy40a71a82009-06-28 19:16:55 +03001228 * ubi_dbg_check_all_ff - check that a region of flash is empty.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001229 * @ubi: UBI device description object
1230 * @pnum: the physical eraseblock number to check
1231 * @offset: the starting offset within the physical eraseblock to check
1232 * @len: the length of the region to check
1233 *
1234 * This function returns zero if only 0xFF bytes are present at offset
1235 * @offset of the physical eraseblock @pnum, %1 if not, and a negative error
1236 * code if an error occurred.
1237 */
Artem Bityutskiy40a71a82009-06-28 19:16:55 +03001238int ubi_dbg_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001239{
1240 size_t read;
1241 int err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001242 loff_t addr = (loff_t)pnum * ubi->peb_size + offset;
1243
Artem Bityutskiy40a71a82009-06-28 19:16:55 +03001244 ubi_assert(!mutex_is_locked(&ubi->dbg_buf_mutex));
1245
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001246 mutex_lock(&ubi->dbg_buf_mutex);
1247 err = ubi->mtd->read(ubi->mtd, addr, len, &read, ubi->dbg_peb_buf);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001248 if (err && err != -EUCLEAN) {
1249 ubi_err("error %d while reading %d bytes from PEB %d:%d, "
1250 "read %zd bytes", err, len, pnum, offset, read);
1251 goto error;
1252 }
1253
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001254 err = check_pattern(ubi->dbg_peb_buf, 0xFF, len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001255 if (err == 0) {
1256 ubi_err("flash region at PEB %d:%d, length %d does not "
1257 "contain all 0xFF bytes", pnum, offset, len);
1258 goto fail;
1259 }
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001260 mutex_unlock(&ubi->dbg_buf_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001261
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001262 return 0;
1263
1264fail:
1265 ubi_err("paranoid check failed for PEB %d", pnum);
Artem Bityutskiyc8566352008-07-16 17:40:22 +03001266 ubi_msg("hex dump of the %d-%d region", offset, offset + len);
Artem Bityutskiy69866462007-08-29 14:56:20 +03001267 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001268 ubi->dbg_peb_buf, len, 1);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001269 err = 1;
1270error:
1271 ubi_dbg_dump_stack();
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001272 mutex_unlock(&ubi->dbg_buf_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001273 return err;
1274}
1275
Artem Bityutskiyffb6b7e2009-05-12 15:43:44 +03001276/**
1277 * paranoid_check_empty - whether a PEB is empty.
1278 * @ubi: UBI device description object
1279 * @pnum: the physical eraseblock number to check
1280 *
1281 * This function makes sure PEB @pnum is empty, which means it contains only
1282 * %0xFF data bytes. Returns zero if the PEB is empty, %1 if not, and a
1283 * negative error code in case of failure.
1284 *
1285 * Empty PEBs have the EC header, and do not have the VID header. The caller of
1286 * this function should have already made sure the PEB does not have the VID
1287 * header. However, this function re-checks that, because it is possible that
1288 * the header and data has already been written to the PEB.
1289 *
1290 * Let's consider a possible scenario. Suppose there are 2 tasks - A and B.
1291 * Task A is in 'wear_leveling_worker()'. It is reading VID header of PEB X to
1292 * find which LEB it corresponds to. PEB X is currently unmapped, and has no
1293 * VID header. Task B is trying to write to PEB X.
1294 *
1295 * Task A: in 'ubi_io_read_vid_hdr()': reads the VID header from PEB X. The
1296 * read data contain all 0xFF bytes;
1297 * Task B: writes VID header and some data to PEB X;
1298 * Task A: assumes PEB X is empty, calls 'paranoid_check_empty()'. And if we
1299 * do not re-read the VID header, and do not cancel the checking if it
1300 * is there, we fail.
1301 */
1302static int paranoid_check_empty(struct ubi_device *ubi, int pnum)
1303{
1304 int err, offs = ubi->vid_hdr_aloffset, len = ubi->vid_hdr_alsize;
1305 size_t read;
1306 uint32_t magic;
1307 const struct ubi_vid_hdr *vid_hdr;
1308
1309 mutex_lock(&ubi->dbg_buf_mutex);
1310 err = ubi->mtd->read(ubi->mtd, offs, len, &read, ubi->dbg_peb_buf);
1311 if (err && err != -EUCLEAN) {
1312 ubi_err("error %d while reading %d bytes from PEB %d:%d, "
1313 "read %zd bytes", err, len, pnum, offs, read);
1314 goto error;
1315 }
1316
1317 vid_hdr = ubi->dbg_peb_buf;
1318 magic = be32_to_cpu(vid_hdr->magic);
1319 if (magic == UBI_VID_HDR_MAGIC)
1320 /* The PEB contains VID header, so it is not empty */
1321 goto out;
1322
1323 err = check_pattern(ubi->dbg_peb_buf, 0xFF, len);
1324 if (err == 0) {
1325 ubi_err("flash region at PEB %d:%d, length %d does not "
1326 "contain all 0xFF bytes", pnum, offs, len);
1327 goto fail;
1328 }
1329
1330out:
1331 mutex_unlock(&ubi->dbg_buf_mutex);
1332 return 0;
1333
1334fail:
1335 ubi_err("paranoid check failed for PEB %d", pnum);
1336 ubi_msg("hex dump of the %d-%d region", offs, offs + len);
1337 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
1338 ubi->dbg_peb_buf, len, 1);
1339 err = 1;
1340error:
1341 ubi_dbg_dump_stack();
1342 mutex_unlock(&ubi->dbg_buf_mutex);
1343 return err;
1344}
1345
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001346#endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */