blob: 1ea14218de0217d1ad3eece75879923d6d3446f6 [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 B. Bityutskiy801c1352006-06-27 12:22:22 +0400101#else
102#define paranoid_check_not_bad(ubi, pnum) 0
103#define paranoid_check_peb_ec_hdr(ubi, pnum) 0
104#define paranoid_check_ec_hdr(ubi, pnum, ec_hdr) 0
105#define paranoid_check_peb_vid_hdr(ubi, pnum) 0
106#define paranoid_check_vid_hdr(ubi, pnum, vid_hdr) 0
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400107#endif
108
109/**
110 * ubi_io_read - read data from a physical eraseblock.
111 * @ubi: UBI device description object
112 * @buf: buffer where to store the read data
113 * @pnum: physical eraseblock number to read from
114 * @offset: offset within the physical eraseblock from where to read
115 * @len: how many bytes to read
116 *
117 * This function reads data from offset @offset of physical eraseblock @pnum
118 * and stores the read data in the @buf buffer. The following return codes are
119 * possible:
120 *
121 * o %0 if all the requested data were successfully read;
122 * o %UBI_IO_BITFLIPS if all the requested data were successfully read, but
123 * correctable bit-flips were detected; this is harmless but may indicate
124 * that this eraseblock may become bad soon (but do not have to);
Artem Bityutskiy63b6c1e2007-07-17 15:04:20 +0300125 * o %-EBADMSG if the MTD subsystem reported about data integrity problems, for
126 * example it can be an ECC error in case of NAND; this most probably means
127 * that the data is corrupted;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400128 * o %-EIO if some I/O error occurred;
129 * o other negative error codes in case of other errors.
130 */
131int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset,
132 int len)
133{
134 int err, retries = 0;
135 size_t read;
136 loff_t addr;
137
138 dbg_io("read %d bytes from PEB %d:%d", len, pnum, offset);
139
140 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
141 ubi_assert(offset >= 0 && offset + len <= ubi->peb_size);
142 ubi_assert(len > 0);
143
144 err = paranoid_check_not_bad(ubi, pnum);
145 if (err)
146 return err > 0 ? -EINVAL : err;
147
148 addr = (loff_t)pnum * ubi->peb_size + offset;
149retry:
150 err = ubi->mtd->read(ubi->mtd, addr, len, &read, buf);
151 if (err) {
152 if (err == -EUCLEAN) {
153 /*
154 * -EUCLEAN is reported if there was a bit-flip which
155 * was corrected, so this is harmless.
Artem Bityutskiy8c1e6ee2008-07-18 12:20:23 +0300156 *
157 * We do not report about it here unless debugging is
158 * enabled. A corresponding message will be printed
159 * later, when it is has been scrubbed.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400160 */
Artem Bityutskiy8c1e6ee2008-07-18 12:20:23 +0300161 dbg_msg("fixable bit-flip detected at PEB %d", pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400162 ubi_assert(len == read);
163 return UBI_IO_BITFLIPS;
164 }
165
166 if (read != len && retries++ < UBI_IO_RETRIES) {
Artem Bityutskiy9c9ec142008-07-18 13:19:52 +0300167 dbg_io("error %d while reading %d bytes from PEB %d:%d,"
168 " read only %zd bytes, retry",
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400169 err, len, pnum, offset, read);
170 yield();
171 goto retry;
172 }
173
174 ubi_err("error %d while reading %d bytes from PEB %d:%d, "
175 "read %zd bytes", err, len, pnum, offset, read);
176 ubi_dbg_dump_stack();
Artem Bityutskiy2362a532007-10-18 20:09:41 +0300177
178 /*
179 * The driver should never return -EBADMSG if it failed to read
180 * all the requested data. But some buggy drivers might do
181 * this, so we change it to -EIO.
182 */
183 if (read != len && err == -EBADMSG) {
184 ubi_assert(0);
185 err = -EIO;
186 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400187 } else {
188 ubi_assert(len == read);
189
190 if (ubi_dbg_is_bitflip()) {
Artem Bityutskiyc8566352008-07-16 17:40:22 +0300191 dbg_gen("bit-flip (emulated)");
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400192 err = UBI_IO_BITFLIPS;
193 }
194 }
195
196 return err;
197}
198
199/**
200 * ubi_io_write - write data to a physical eraseblock.
201 * @ubi: UBI device description object
202 * @buf: buffer with the data to write
203 * @pnum: physical eraseblock number to write to
204 * @offset: offset within the physical eraseblock where to write
205 * @len: how many bytes to write
206 *
207 * This function writes @len bytes of data from buffer @buf to offset @offset
208 * of physical eraseblock @pnum. If all the data were successfully written,
209 * zero is returned. If an error occurred, this function returns a negative
210 * error code. If %-EIO is returned, the physical eraseblock most probably went
211 * bad.
212 *
213 * Note, in case of an error, it is possible that something was still written
214 * to the flash media, but may be some garbage.
215 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300216int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
217 int len)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400218{
219 int err;
220 size_t written;
221 loff_t addr;
222
223 dbg_io("write %d bytes to PEB %d:%d", len, pnum, offset);
224
225 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
226 ubi_assert(offset >= 0 && offset + len <= ubi->peb_size);
227 ubi_assert(offset % ubi->hdrs_min_io_size == 0);
228 ubi_assert(len > 0 && len % ubi->hdrs_min_io_size == 0);
229
230 if (ubi->ro_mode) {
231 ubi_err("read-only mode");
232 return -EROFS;
233 }
234
235 /* The below has to be compiled out if paranoid checks are disabled */
236
237 err = paranoid_check_not_bad(ubi, pnum);
238 if (err)
239 return err > 0 ? -EINVAL : err;
240
241 /* The area we are writing to has to contain all 0xFF bytes */
Artem Bityutskiy40a71a82009-06-28 19:16:55 +0300242 err = ubi_dbg_check_all_ff(ubi, pnum, offset, len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400243 if (err)
244 return err > 0 ? -EINVAL : err;
245
246 if (offset >= ubi->leb_start) {
247 /*
248 * We write to the data area of the physical eraseblock. Make
249 * sure it has valid EC and VID headers.
250 */
251 err = paranoid_check_peb_ec_hdr(ubi, pnum);
252 if (err)
253 return err > 0 ? -EINVAL : err;
254 err = paranoid_check_peb_vid_hdr(ubi, pnum);
255 if (err)
256 return err > 0 ? -EINVAL : err;
257 }
258
259 if (ubi_dbg_is_write_failure()) {
260 dbg_err("cannot write %d bytes to PEB %d:%d "
261 "(emulated)", len, pnum, offset);
262 ubi_dbg_dump_stack();
263 return -EIO;
264 }
265
266 addr = (loff_t)pnum * ubi->peb_size + offset;
267 err = ubi->mtd->write(ubi->mtd, addr, len, &written, buf);
268 if (err) {
269 ubi_err("error %d while writing %d bytes to PEB %d:%d, written"
270 " %zd bytes", err, len, pnum, offset, written);
271 ubi_dbg_dump_stack();
272 } else
273 ubi_assert(written == len);
274
275 return err;
276}
277
278/**
279 * erase_callback - MTD erasure call-back.
280 * @ei: MTD erase information object.
281 *
282 * Note, even though MTD erase interface is asynchronous, all the current
283 * implementations are synchronous anyway.
284 */
285static void erase_callback(struct erase_info *ei)
286{
287 wake_up_interruptible((wait_queue_head_t *)ei->priv);
288}
289
290/**
291 * do_sync_erase - synchronously erase a physical eraseblock.
292 * @ubi: UBI device description object
293 * @pnum: the physical eraseblock number to erase
294 *
295 * This function synchronously erases physical eraseblock @pnum and returns
296 * zero in case of success and a negative error code in case of failure. If
297 * %-EIO is returned, the physical eraseblock most probably went bad.
298 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300299static int do_sync_erase(struct ubi_device *ubi, int pnum)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400300{
301 int err, retries = 0;
302 struct erase_info ei;
303 wait_queue_head_t wq;
304
305 dbg_io("erase PEB %d", pnum);
306
307retry:
308 init_waitqueue_head(&wq);
309 memset(&ei, 0, sizeof(struct erase_info));
310
311 ei.mtd = ubi->mtd;
Brijesh Singh2f176f72007-07-05 15:07:35 +0530312 ei.addr = (loff_t)pnum * ubi->peb_size;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400313 ei.len = ubi->peb_size;
314 ei.callback = erase_callback;
315 ei.priv = (unsigned long)&wq;
316
317 err = ubi->mtd->erase(ubi->mtd, &ei);
318 if (err) {
319 if (retries++ < UBI_IO_RETRIES) {
320 dbg_io("error %d while erasing PEB %d, retry",
321 err, pnum);
322 yield();
323 goto retry;
324 }
325 ubi_err("cannot erase PEB %d, error %d", pnum, err);
326 ubi_dbg_dump_stack();
327 return err;
328 }
329
330 err = wait_event_interruptible(wq, ei.state == MTD_ERASE_DONE ||
331 ei.state == MTD_ERASE_FAILED);
332 if (err) {
333 ubi_err("interrupted PEB %d erasure", pnum);
334 return -EINTR;
335 }
336
337 if (ei.state == MTD_ERASE_FAILED) {
338 if (retries++ < UBI_IO_RETRIES) {
339 dbg_io("error while erasing PEB %d, retry", pnum);
340 yield();
341 goto retry;
342 }
343 ubi_err("cannot erase PEB %d", pnum);
344 ubi_dbg_dump_stack();
345 return -EIO;
346 }
347
Artem Bityutskiy40a71a82009-06-28 19:16:55 +0300348 err = ubi_dbg_check_all_ff(ubi, pnum, 0, ubi->peb_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400349 if (err)
350 return err > 0 ? -EINVAL : err;
351
352 if (ubi_dbg_is_erase_failure() && !err) {
353 dbg_err("cannot erase PEB %d (emulated)", pnum);
354 return -EIO;
355 }
356
357 return 0;
358}
359
360/**
361 * check_pattern - check if buffer contains only a certain byte pattern.
362 * @buf: buffer to check
363 * @patt: the pattern to check
364 * @size: buffer size in bytes
365 *
366 * This function returns %1 in there are only @patt bytes in @buf, and %0 if
367 * something else was also found.
368 */
369static int check_pattern(const void *buf, uint8_t patt, int size)
370{
371 int i;
372
373 for (i = 0; i < size; i++)
374 if (((const uint8_t *)buf)[i] != patt)
375 return 0;
376 return 1;
377}
378
379/* Patterns to write to a physical eraseblock when torturing it */
380static uint8_t patterns[] = {0xa5, 0x5a, 0x0};
381
382/**
383 * torture_peb - test a supposedly bad physical eraseblock.
384 * @ubi: UBI device description object
385 * @pnum: the physical eraseblock number to test
386 *
387 * This function returns %-EIO if the physical eraseblock did not pass the
388 * test, a positive number of erase operations done if the test was
389 * successfully passed, and other negative error codes in case of other errors.
390 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300391static int torture_peb(struct ubi_device *ubi, int pnum)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400392{
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400393 int err, i, patt_count;
394
Artem Bityutskiy8c1e6ee2008-07-18 12:20:23 +0300395 ubi_msg("run torture test for PEB %d", pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400396 patt_count = ARRAY_SIZE(patterns);
397 ubi_assert(patt_count > 0);
398
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300399 mutex_lock(&ubi->buf_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400400 for (i = 0; i < patt_count; i++) {
401 err = do_sync_erase(ubi, pnum);
402 if (err)
403 goto out;
404
405 /* Make sure the PEB contains only 0xFF bytes */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300406 err = ubi_io_read(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400407 if (err)
408 goto out;
409
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300410 err = check_pattern(ubi->peb_buf1, 0xFF, ubi->peb_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400411 if (err == 0) {
412 ubi_err("erased PEB %d, but a non-0xFF byte found",
413 pnum);
414 err = -EIO;
415 goto out;
416 }
417
418 /* Write a pattern and check it */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300419 memset(ubi->peb_buf1, patterns[i], ubi->peb_size);
420 err = ubi_io_write(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400421 if (err)
422 goto out;
423
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300424 memset(ubi->peb_buf1, ~patterns[i], ubi->peb_size);
425 err = ubi_io_read(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400426 if (err)
427 goto out;
428
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300429 err = check_pattern(ubi->peb_buf1, patterns[i], ubi->peb_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400430 if (err == 0) {
431 ubi_err("pattern %x checking failed for PEB %d",
432 patterns[i], pnum);
433 err = -EIO;
434 goto out;
435 }
436 }
437
438 err = patt_count;
Artem Bityutskiy8c1e6ee2008-07-18 12:20:23 +0300439 ubi_msg("PEB %d passed torture test, do not mark it a bad", pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400440
441out:
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300442 mutex_unlock(&ubi->buf_mutex);
Artem Bityutskiy8d2d4012007-07-22 22:32:51 +0300443 if (err == UBI_IO_BITFLIPS || err == -EBADMSG) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400444 /*
445 * If a bit-flip or data integrity error was detected, the test
446 * has not passed because it happened on a freshly erased
447 * physical eraseblock which means something is wrong with it.
448 */
Artem Bityutskiy8d2d4012007-07-22 22:32:51 +0300449 ubi_err("read problems on freshly erased PEB %d, must be bad",
450 pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400451 err = -EIO;
Artem Bityutskiy8d2d4012007-07-22 22:32:51 +0300452 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400453 return err;
454}
455
456/**
457 * ubi_io_sync_erase - synchronously erase a physical eraseblock.
458 * @ubi: UBI device description object
459 * @pnum: physical eraseblock number to erase
460 * @torture: if this physical eraseblock has to be tortured
461 *
462 * This function synchronously erases physical eraseblock @pnum. If @torture
463 * flag is not zero, the physical eraseblock is checked by means of writing
464 * different patterns to it and reading them back. If the torturing is enabled,
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200465 * the physical eraseblock is erased more than once.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400466 *
467 * This function returns the number of erasures made in case of success, %-EIO
468 * if the erasure failed or the torturing test failed, and other negative error
469 * codes in case of other errors. Note, %-EIO means that the physical
470 * eraseblock is bad.
471 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300472int ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400473{
474 int err, ret = 0;
475
476 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
477
478 err = paranoid_check_not_bad(ubi, pnum);
479 if (err != 0)
480 return err > 0 ? -EINVAL : err;
481
482 if (ubi->ro_mode) {
483 ubi_err("read-only mode");
484 return -EROFS;
485 }
486
487 if (torture) {
488 ret = torture_peb(ubi, pnum);
489 if (ret < 0)
490 return ret;
491 }
492
493 err = do_sync_erase(ubi, pnum);
494 if (err)
495 return err;
496
497 return ret + 1;
498}
499
500/**
501 * ubi_io_is_bad - check if a physical eraseblock is bad.
502 * @ubi: UBI device description object
503 * @pnum: the physical eraseblock number to check
504 *
505 * This function returns a positive number if the physical eraseblock is bad,
506 * zero if not, and a negative error code if an error occurred.
507 */
508int ubi_io_is_bad(const struct ubi_device *ubi, int pnum)
509{
510 struct mtd_info *mtd = ubi->mtd;
511
512 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
513
514 if (ubi->bad_allowed) {
515 int ret;
516
517 ret = mtd->block_isbad(mtd, (loff_t)pnum * ubi->peb_size);
518 if (ret < 0)
519 ubi_err("error %d while checking if PEB %d is bad",
520 ret, pnum);
521 else if (ret)
522 dbg_io("PEB %d is bad", pnum);
523 return ret;
524 }
525
526 return 0;
527}
528
529/**
530 * ubi_io_mark_bad - mark a physical eraseblock as bad.
531 * @ubi: UBI device description object
532 * @pnum: the physical eraseblock number to mark
533 *
534 * This function returns zero in case of success and a negative error code in
535 * case of failure.
536 */
537int ubi_io_mark_bad(const struct ubi_device *ubi, int pnum)
538{
539 int err;
540 struct mtd_info *mtd = ubi->mtd;
541
542 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
543
544 if (ubi->ro_mode) {
545 ubi_err("read-only mode");
546 return -EROFS;
547 }
548
549 if (!ubi->bad_allowed)
550 return 0;
551
552 err = mtd->block_markbad(mtd, (loff_t)pnum * ubi->peb_size);
553 if (err)
554 ubi_err("cannot mark PEB %d bad, error %d", pnum, err);
555 return err;
556}
557
558/**
559 * validate_ec_hdr - validate an erase counter header.
560 * @ubi: UBI device description object
561 * @ec_hdr: the erase counter header to check
562 *
563 * This function returns zero if the erase counter header is OK, and %1 if
564 * not.
565 */
Artem Bityutskiyfe96efc2009-06-30 16:11:59 +0300566static int validate_ec_hdr(const struct ubi_device *ubi,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400567 const struct ubi_ec_hdr *ec_hdr)
568{
569 long long ec;
Artem Bityutskiyfe96efc2009-06-30 16:11:59 +0300570 int vid_hdr_offset, leb_start;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400571
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300572 ec = be64_to_cpu(ec_hdr->ec);
573 vid_hdr_offset = be32_to_cpu(ec_hdr->vid_hdr_offset);
574 leb_start = be32_to_cpu(ec_hdr->data_offset);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400575
576 if (ec_hdr->version != UBI_VERSION) {
577 ubi_err("node with incompatible UBI version found: "
578 "this UBI version is %d, image version is %d",
579 UBI_VERSION, (int)ec_hdr->version);
580 goto bad;
581 }
582
583 if (vid_hdr_offset != ubi->vid_hdr_offset) {
584 ubi_err("bad VID header offset %d, expected %d",
585 vid_hdr_offset, ubi->vid_hdr_offset);
586 goto bad;
587 }
588
589 if (leb_start != ubi->leb_start) {
590 ubi_err("bad data offset %d, expected %d",
591 leb_start, ubi->leb_start);
592 goto bad;
593 }
594
595 if (ec < 0 || ec > UBI_MAX_ERASECOUNTER) {
596 ubi_err("bad erase counter %lld", ec);
597 goto bad;
598 }
599
600 return 0;
601
602bad:
603 ubi_err("bad EC header");
604 ubi_dbg_dump_ec_hdr(ec_hdr);
605 ubi_dbg_dump_stack();
606 return 1;
607}
608
609/**
610 * ubi_io_read_ec_hdr - read and check an erase counter header.
611 * @ubi: UBI device description object
612 * @pnum: physical eraseblock to read from
613 * @ec_hdr: a &struct ubi_ec_hdr object where to store the read erase counter
614 * header
615 * @verbose: be verbose if the header is corrupted or was not found
616 *
617 * This function reads erase counter header from physical eraseblock @pnum and
618 * stores it in @ec_hdr. This function also checks CRC checksum of the read
619 * erase counter header. The following codes may be returned:
620 *
621 * o %0 if the CRC checksum is correct and the header was successfully read;
622 * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected
623 * and corrected by the flash driver; this is harmless but may indicate that
624 * this eraseblock may become bad soon (but may be not);
625 * o %UBI_IO_BAD_EC_HDR if the erase counter header is corrupted (a CRC error);
626 * o %UBI_IO_PEB_EMPTY if the physical eraseblock is empty;
627 * o a negative error code in case of failure.
628 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300629int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400630 struct ubi_ec_hdr *ec_hdr, int verbose)
631{
632 int err, read_err = 0;
633 uint32_t crc, magic, hdr_crc;
634
635 dbg_io("read EC header from PEB %d", pnum);
636 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
637
638 err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE);
639 if (err) {
640 if (err != UBI_IO_BITFLIPS && err != -EBADMSG)
641 return err;
642
643 /*
644 * We read all the data, but either a correctable bit-flip
645 * occurred, or MTD reported about some data integrity error,
646 * like an ECC error in case of NAND. The former is harmless,
647 * the later may mean that the read data is corrupted. But we
648 * have a CRC check-sum and we will detect this. If the EC
649 * header is still OK, we just report this as there was a
650 * bit-flip.
651 */
652 read_err = err;
653 }
654
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300655 magic = be32_to_cpu(ec_hdr->magic);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400656 if (magic != UBI_EC_HDR_MAGIC) {
657 /*
658 * The magic field is wrong. Let's check if we have read all
659 * 0xFF. If yes, this physical eraseblock is assumed to be
660 * empty.
661 *
662 * But if there was a read error, we do not test it for all
663 * 0xFFs. Even if it does contain all 0xFFs, this error
664 * indicates that something is still wrong with this physical
665 * eraseblock and we anyway cannot treat it as empty.
666 */
667 if (read_err != -EBADMSG &&
668 check_pattern(ec_hdr, 0xFF, UBI_EC_HDR_SIZE)) {
669 /* The physical eraseblock is supposedly empty */
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400670 if (verbose)
671 ubi_warn("no EC header found at PEB %d, "
672 "only 0xFF bytes", pnum);
Artem Bityutskiyed458192008-11-12 10:14:10 +0200673 else if (UBI_IO_DEBUG)
674 dbg_msg("no EC header found at PEB %d, "
675 "only 0xFF bytes", pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400676 return UBI_IO_PEB_EMPTY;
677 }
678
679 /*
680 * This is not a valid erase counter header, and these are not
681 * 0xFF bytes. Report that the header is corrupted.
682 */
683 if (verbose) {
684 ubi_warn("bad magic number at PEB %d: %08x instead of "
685 "%08x", pnum, magic, UBI_EC_HDR_MAGIC);
686 ubi_dbg_dump_ec_hdr(ec_hdr);
Artem Bityutskiyed458192008-11-12 10:14:10 +0200687 } else if (UBI_IO_DEBUG)
688 dbg_msg("bad magic number at PEB %d: %08x instead of "
689 "%08x", pnum, magic, UBI_EC_HDR_MAGIC);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400690 return UBI_IO_BAD_EC_HDR;
691 }
692
693 crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300694 hdr_crc = be32_to_cpu(ec_hdr->hdr_crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400695
696 if (hdr_crc != crc) {
697 if (verbose) {
Artem Bityutskiy9c9ec142008-07-18 13:19:52 +0300698 ubi_warn("bad EC header CRC at PEB %d, calculated "
699 "%#08x, read %#08x", pnum, crc, hdr_crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400700 ubi_dbg_dump_ec_hdr(ec_hdr);
Artem Bityutskiyed458192008-11-12 10:14:10 +0200701 } else if (UBI_IO_DEBUG)
702 dbg_msg("bad EC header CRC at PEB %d, calculated "
703 "%#08x, read %#08x", pnum, crc, hdr_crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400704 return UBI_IO_BAD_EC_HDR;
705 }
706
707 /* And of course validate what has just been read from the media */
708 err = validate_ec_hdr(ubi, ec_hdr);
709 if (err) {
710 ubi_err("validation failed for PEB %d", pnum);
711 return -EINVAL;
712 }
713
714 return read_err ? UBI_IO_BITFLIPS : 0;
715}
716
717/**
718 * ubi_io_write_ec_hdr - write an erase counter header.
719 * @ubi: UBI device description object
720 * @pnum: physical eraseblock to write to
721 * @ec_hdr: the erase counter header to write
722 *
723 * This function writes erase counter header described by @ec_hdr to physical
724 * eraseblock @pnum. It also fills most fields of @ec_hdr before writing, so
725 * the caller do not have to fill them. Callers must only fill the @ec_hdr->ec
726 * field.
727 *
728 * This function returns zero in case of success and a negative error code in
729 * case of failure. If %-EIO is returned, the physical eraseblock most probably
730 * went bad.
731 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300732int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400733 struct ubi_ec_hdr *ec_hdr)
734{
735 int err;
736 uint32_t crc;
737
738 dbg_io("write EC header to PEB %d", pnum);
739 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
740
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300741 ec_hdr->magic = cpu_to_be32(UBI_EC_HDR_MAGIC);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400742 ec_hdr->version = UBI_VERSION;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300743 ec_hdr->vid_hdr_offset = cpu_to_be32(ubi->vid_hdr_offset);
744 ec_hdr->data_offset = cpu_to_be32(ubi->leb_start);
Adrian Hunter0c6c7fa2009-06-26 14:58:01 +0300745 ec_hdr->image_seq = cpu_to_be32(ubi->image_seq);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400746 crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300747 ec_hdr->hdr_crc = cpu_to_be32(crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400748
749 err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr);
750 if (err)
751 return -EINVAL;
752
753 err = ubi_io_write(ubi, ec_hdr, pnum, 0, ubi->ec_hdr_alsize);
754 return err;
755}
756
757/**
758 * validate_vid_hdr - validate a volume identifier header.
759 * @ubi: UBI device description object
760 * @vid_hdr: the volume identifier header to check
761 *
762 * This function checks that data stored in the volume identifier header
763 * @vid_hdr. Returns zero if the VID header is OK and %1 if not.
764 */
765static int validate_vid_hdr(const struct ubi_device *ubi,
766 const struct ubi_vid_hdr *vid_hdr)
767{
768 int vol_type = vid_hdr->vol_type;
769 int copy_flag = vid_hdr->copy_flag;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300770 int vol_id = be32_to_cpu(vid_hdr->vol_id);
771 int lnum = be32_to_cpu(vid_hdr->lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400772 int compat = vid_hdr->compat;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300773 int data_size = be32_to_cpu(vid_hdr->data_size);
774 int used_ebs = be32_to_cpu(vid_hdr->used_ebs);
775 int data_pad = be32_to_cpu(vid_hdr->data_pad);
776 int data_crc = be32_to_cpu(vid_hdr->data_crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400777 int usable_leb_size = ubi->leb_size - data_pad;
778
779 if (copy_flag != 0 && copy_flag != 1) {
780 dbg_err("bad copy_flag");
781 goto bad;
782 }
783
784 if (vol_id < 0 || lnum < 0 || data_size < 0 || used_ebs < 0 ||
785 data_pad < 0) {
786 dbg_err("negative values");
787 goto bad;
788 }
789
790 if (vol_id >= UBI_MAX_VOLUMES && vol_id < UBI_INTERNAL_VOL_START) {
791 dbg_err("bad vol_id");
792 goto bad;
793 }
794
795 if (vol_id < UBI_INTERNAL_VOL_START && compat != 0) {
796 dbg_err("bad compat");
797 goto bad;
798 }
799
800 if (vol_id >= UBI_INTERNAL_VOL_START && compat != UBI_COMPAT_DELETE &&
801 compat != UBI_COMPAT_RO && compat != UBI_COMPAT_PRESERVE &&
802 compat != UBI_COMPAT_REJECT) {
803 dbg_err("bad compat");
804 goto bad;
805 }
806
807 if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) {
808 dbg_err("bad vol_type");
809 goto bad;
810 }
811
812 if (data_pad >= ubi->leb_size / 2) {
813 dbg_err("bad data_pad");
814 goto bad;
815 }
816
817 if (vol_type == UBI_VID_STATIC) {
818 /*
819 * Although from high-level point of view static volumes may
820 * contain zero bytes of data, but no VID headers can contain
821 * zero at these fields, because they empty volumes do not have
822 * mapped logical eraseblocks.
823 */
824 if (used_ebs == 0) {
825 dbg_err("zero used_ebs");
826 goto bad;
827 }
828 if (data_size == 0) {
829 dbg_err("zero data_size");
830 goto bad;
831 }
832 if (lnum < used_ebs - 1) {
833 if (data_size != usable_leb_size) {
834 dbg_err("bad data_size");
835 goto bad;
836 }
837 } else if (lnum == used_ebs - 1) {
838 if (data_size == 0) {
839 dbg_err("bad data_size at last LEB");
840 goto bad;
841 }
842 } else {
843 dbg_err("too high lnum");
844 goto bad;
845 }
846 } else {
847 if (copy_flag == 0) {
848 if (data_crc != 0) {
849 dbg_err("non-zero data CRC");
850 goto bad;
851 }
852 if (data_size != 0) {
853 dbg_err("non-zero data_size");
854 goto bad;
855 }
856 } else {
857 if (data_size == 0) {
858 dbg_err("zero data_size of copy");
859 goto bad;
860 }
861 }
862 if (used_ebs != 0) {
863 dbg_err("bad used_ebs");
864 goto bad;
865 }
866 }
867
868 return 0;
869
870bad:
871 ubi_err("bad VID header");
872 ubi_dbg_dump_vid_hdr(vid_hdr);
873 ubi_dbg_dump_stack();
874 return 1;
875}
876
877/**
878 * ubi_io_read_vid_hdr - read and check a volume identifier header.
879 * @ubi: UBI device description object
880 * @pnum: physical eraseblock number to read from
881 * @vid_hdr: &struct ubi_vid_hdr object where to store the read volume
882 * identifier header
883 * @verbose: be verbose if the header is corrupted or wasn't found
884 *
885 * This function reads the volume identifier header from physical eraseblock
886 * @pnum and stores it in @vid_hdr. It also checks CRC checksum of the read
887 * volume identifier header. The following codes may be returned:
888 *
889 * o %0 if the CRC checksum is correct and the header was successfully read;
890 * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected
891 * and corrected by the flash driver; this is harmless but may indicate that
892 * this eraseblock may become bad soon;
Artem Bityutskiy815bc5f2009-06-08 19:28:18 +0300893 * o %UBI_IO_BAD_VID_HDR if the volume identifier header is corrupted (a CRC
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400894 * error detected);
895 * o %UBI_IO_PEB_FREE if the physical eraseblock is free (i.e., there is no VID
896 * header there);
897 * o a negative error code in case of failure.
898 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300899int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400900 struct ubi_vid_hdr *vid_hdr, int verbose)
901{
902 int err, read_err = 0;
903 uint32_t crc, magic, hdr_crc;
904 void *p;
905
906 dbg_io("read VID header from PEB %d", pnum);
907 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
908
909 p = (char *)vid_hdr - ubi->vid_hdr_shift;
910 err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset,
911 ubi->vid_hdr_alsize);
912 if (err) {
913 if (err != UBI_IO_BITFLIPS && err != -EBADMSG)
914 return err;
915
916 /*
917 * We read all the data, but either a correctable bit-flip
918 * occurred, or MTD reported about some data integrity error,
919 * like an ECC error in case of NAND. The former is harmless,
920 * the later may mean the read data is corrupted. But we have a
921 * CRC check-sum and we will identify this. If the VID header is
922 * still OK, we just report this as there was a bit-flip.
923 */
924 read_err = err;
925 }
926
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300927 magic = be32_to_cpu(vid_hdr->magic);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400928 if (magic != UBI_VID_HDR_MAGIC) {
929 /*
930 * If we have read all 0xFF bytes, the VID header probably does
931 * not exist and the physical eraseblock is assumed to be free.
932 *
933 * But if there was a read error, we do not test the data for
934 * 0xFFs. Even if it does contain all 0xFFs, this error
935 * indicates that something is still wrong with this physical
936 * eraseblock and it cannot be regarded as free.
937 */
938 if (read_err != -EBADMSG &&
939 check_pattern(vid_hdr, 0xFF, UBI_VID_HDR_SIZE)) {
940 /* The physical eraseblock is supposedly free */
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400941 if (verbose)
942 ubi_warn("no VID header found at PEB %d, "
943 "only 0xFF bytes", pnum);
Artem Bityutskiyed458192008-11-12 10:14:10 +0200944 else if (UBI_IO_DEBUG)
945 dbg_msg("no VID header found at PEB %d, "
946 "only 0xFF bytes", pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400947 return UBI_IO_PEB_FREE;
948 }
949
950 /*
951 * This is not a valid VID header, and these are not 0xFF
952 * bytes. Report that the header is corrupted.
953 */
954 if (verbose) {
955 ubi_warn("bad magic number at PEB %d: %08x instead of "
956 "%08x", pnum, magic, UBI_VID_HDR_MAGIC);
957 ubi_dbg_dump_vid_hdr(vid_hdr);
Artem Bityutskiyed458192008-11-12 10:14:10 +0200958 } else if (UBI_IO_DEBUG)
959 dbg_msg("bad magic number at PEB %d: %08x instead of "
960 "%08x", pnum, magic, UBI_VID_HDR_MAGIC);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400961 return UBI_IO_BAD_VID_HDR;
962 }
963
964 crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC);
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300965 hdr_crc = be32_to_cpu(vid_hdr->hdr_crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400966
967 if (hdr_crc != crc) {
968 if (verbose) {
969 ubi_warn("bad CRC at PEB %d, calculated %#08x, "
970 "read %#08x", pnum, crc, hdr_crc);
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 CRC at PEB %d, calculated %#08x, "
974 "read %#08x", pnum, crc, hdr_crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400975 return UBI_IO_BAD_VID_HDR;
976 }
977
978 /* Validate the VID header that we have just read */
979 err = validate_vid_hdr(ubi, vid_hdr);
980 if (err) {
981 ubi_err("validation failed for PEB %d", pnum);
982 return -EINVAL;
983 }
984
985 return read_err ? UBI_IO_BITFLIPS : 0;
986}
987
988/**
989 * ubi_io_write_vid_hdr - write a volume identifier header.
990 * @ubi: UBI device description object
991 * @pnum: the physical eraseblock number to write to
992 * @vid_hdr: the volume identifier header to write
993 *
994 * This function writes the volume identifier header described by @vid_hdr to
995 * physical eraseblock @pnum. This function automatically fills the
996 * @vid_hdr->magic and the @vid_hdr->version fields, as well as calculates
997 * header CRC checksum and stores it at vid_hdr->hdr_crc.
998 *
999 * This function returns zero in case of success and a negative error code in
1000 * case of failure. If %-EIO is returned, the physical eraseblock probably went
1001 * bad.
1002 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001003int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001004 struct ubi_vid_hdr *vid_hdr)
1005{
1006 int err;
1007 uint32_t crc;
1008 void *p;
1009
1010 dbg_io("write VID header to PEB %d", pnum);
1011 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
1012
1013 err = paranoid_check_peb_ec_hdr(ubi, pnum);
1014 if (err)
Artem Bityutskiyf2863c52008-12-28 12:20:51 +02001015 return err > 0 ? -EINVAL : err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001016
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001017 vid_hdr->magic = cpu_to_be32(UBI_VID_HDR_MAGIC);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001018 vid_hdr->version = UBI_VERSION;
1019 crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC);
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001020 vid_hdr->hdr_crc = cpu_to_be32(crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001021
1022 err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr);
1023 if (err)
1024 return -EINVAL;
1025
1026 p = (char *)vid_hdr - ubi->vid_hdr_shift;
1027 err = ubi_io_write(ubi, p, pnum, ubi->vid_hdr_aloffset,
1028 ubi->vid_hdr_alsize);
1029 return err;
1030}
1031
1032#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
1033
1034/**
1035 * paranoid_check_not_bad - ensure that a physical eraseblock is not bad.
1036 * @ubi: UBI device description object
1037 * @pnum: physical eraseblock number to check
1038 *
1039 * This function returns zero if the physical eraseblock is good, a positive
1040 * number if it is bad and a negative error code if an error occurred.
1041 */
1042static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum)
1043{
1044 int err;
1045
1046 err = ubi_io_is_bad(ubi, pnum);
1047 if (!err)
1048 return err;
1049
1050 ubi_err("paranoid check failed for PEB %d", pnum);
1051 ubi_dbg_dump_stack();
1052 return err;
1053}
1054
1055/**
1056 * paranoid_check_ec_hdr - check if an erase counter header is all right.
1057 * @ubi: UBI device description object
1058 * @pnum: physical eraseblock number the erase counter header belongs to
1059 * @ec_hdr: the erase counter header to check
1060 *
1061 * This function returns zero if the erase counter header contains valid
1062 * values, and %1 if not.
1063 */
1064static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum,
1065 const struct ubi_ec_hdr *ec_hdr)
1066{
1067 int err;
1068 uint32_t magic;
1069
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001070 magic = be32_to_cpu(ec_hdr->magic);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001071 if (magic != UBI_EC_HDR_MAGIC) {
1072 ubi_err("bad magic %#08x, must be %#08x",
1073 magic, UBI_EC_HDR_MAGIC);
1074 goto fail;
1075 }
1076
1077 err = validate_ec_hdr(ubi, ec_hdr);
1078 if (err) {
1079 ubi_err("paranoid check failed for PEB %d", pnum);
1080 goto fail;
1081 }
1082
1083 return 0;
1084
1085fail:
1086 ubi_dbg_dump_ec_hdr(ec_hdr);
1087 ubi_dbg_dump_stack();
1088 return 1;
1089}
1090
1091/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +03001092 * paranoid_check_peb_ec_hdr - check erase counter header.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001093 * @ubi: UBI device description object
1094 * @pnum: the physical eraseblock number to check
1095 *
1096 * This function returns zero if the erase counter header is all right, %1 if
1097 * not, and a negative error code if an error occurred.
1098 */
1099static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum)
1100{
1101 int err;
1102 uint32_t crc, hdr_crc;
1103 struct ubi_ec_hdr *ec_hdr;
1104
Artem Bityutskiy33818bb2007-08-28 21:29:32 +03001105 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001106 if (!ec_hdr)
1107 return -ENOMEM;
1108
1109 err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE);
1110 if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG)
1111 goto exit;
1112
1113 crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001114 hdr_crc = be32_to_cpu(ec_hdr->hdr_crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001115 if (hdr_crc != crc) {
1116 ubi_err("bad CRC, calculated %#08x, read %#08x", crc, hdr_crc);
1117 ubi_err("paranoid check failed for PEB %d", pnum);
1118 ubi_dbg_dump_ec_hdr(ec_hdr);
1119 ubi_dbg_dump_stack();
1120 err = 1;
1121 goto exit;
1122 }
1123
1124 err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr);
1125
1126exit:
1127 kfree(ec_hdr);
1128 return err;
1129}
1130
1131/**
1132 * paranoid_check_vid_hdr - check that a volume identifier header is all right.
1133 * @ubi: UBI device description object
1134 * @pnum: physical eraseblock number the volume identifier header belongs to
1135 * @vid_hdr: the volume identifier header to check
1136 *
1137 * This function returns zero if the volume identifier header is all right, and
1138 * %1 if not.
1139 */
1140static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum,
1141 const struct ubi_vid_hdr *vid_hdr)
1142{
1143 int err;
1144 uint32_t magic;
1145
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001146 magic = be32_to_cpu(vid_hdr->magic);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001147 if (magic != UBI_VID_HDR_MAGIC) {
1148 ubi_err("bad VID header magic %#08x at PEB %d, must be %#08x",
1149 magic, pnum, UBI_VID_HDR_MAGIC);
1150 goto fail;
1151 }
1152
1153 err = validate_vid_hdr(ubi, vid_hdr);
1154 if (err) {
1155 ubi_err("paranoid check failed for PEB %d", pnum);
1156 goto fail;
1157 }
1158
1159 return err;
1160
1161fail:
1162 ubi_err("paranoid check failed for PEB %d", pnum);
1163 ubi_dbg_dump_vid_hdr(vid_hdr);
1164 ubi_dbg_dump_stack();
1165 return 1;
1166
1167}
1168
1169/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +03001170 * paranoid_check_peb_vid_hdr - check volume identifier header.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001171 * @ubi: UBI device description object
1172 * @pnum: the physical eraseblock number to check
1173 *
1174 * This function returns zero if the volume identifier header is all right,
1175 * %1 if not, and a negative error code if an error occurred.
1176 */
1177static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum)
1178{
1179 int err;
1180 uint32_t crc, hdr_crc;
1181 struct ubi_vid_hdr *vid_hdr;
1182 void *p;
1183
Artem Bityutskiy33818bb2007-08-28 21:29:32 +03001184 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001185 if (!vid_hdr)
1186 return -ENOMEM;
1187
1188 p = (char *)vid_hdr - ubi->vid_hdr_shift;
1189 err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset,
1190 ubi->vid_hdr_alsize);
1191 if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG)
1192 goto exit;
1193
1194 crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_EC_HDR_SIZE_CRC);
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001195 hdr_crc = be32_to_cpu(vid_hdr->hdr_crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001196 if (hdr_crc != crc) {
1197 ubi_err("bad VID header CRC at PEB %d, calculated %#08x, "
1198 "read %#08x", pnum, crc, hdr_crc);
1199 ubi_err("paranoid check failed for PEB %d", pnum);
1200 ubi_dbg_dump_vid_hdr(vid_hdr);
1201 ubi_dbg_dump_stack();
1202 err = 1;
1203 goto exit;
1204 }
1205
1206 err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr);
1207
1208exit:
1209 ubi_free_vid_hdr(ubi, vid_hdr);
1210 return err;
1211}
1212
1213/**
Artem Bityutskiy40a71a82009-06-28 19:16:55 +03001214 * ubi_dbg_check_all_ff - check that a region of flash is empty.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001215 * @ubi: UBI device description object
1216 * @pnum: the physical eraseblock number to check
1217 * @offset: the starting offset within the physical eraseblock to check
1218 * @len: the length of the region to check
1219 *
1220 * This function returns zero if only 0xFF bytes are present at offset
1221 * @offset of the physical eraseblock @pnum, %1 if not, and a negative error
1222 * code if an error occurred.
1223 */
Artem Bityutskiy40a71a82009-06-28 19:16:55 +03001224int ubi_dbg_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001225{
1226 size_t read;
1227 int err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001228 loff_t addr = (loff_t)pnum * ubi->peb_size + offset;
1229
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001230 mutex_lock(&ubi->dbg_buf_mutex);
1231 err = ubi->mtd->read(ubi->mtd, addr, len, &read, ubi->dbg_peb_buf);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001232 if (err && err != -EUCLEAN) {
1233 ubi_err("error %d while reading %d bytes from PEB %d:%d, "
1234 "read %zd bytes", err, len, pnum, offset, read);
1235 goto error;
1236 }
1237
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001238 err = check_pattern(ubi->dbg_peb_buf, 0xFF, len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001239 if (err == 0) {
1240 ubi_err("flash region at PEB %d:%d, length %d does not "
1241 "contain all 0xFF bytes", pnum, offset, len);
1242 goto fail;
1243 }
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001244 mutex_unlock(&ubi->dbg_buf_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001245
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001246 return 0;
1247
1248fail:
1249 ubi_err("paranoid check failed for PEB %d", pnum);
Artem Bityutskiyc8566352008-07-16 17:40:22 +03001250 ubi_msg("hex dump of the %d-%d region", offset, offset + len);
Artem Bityutskiy69866462007-08-29 14:56:20 +03001251 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001252 ubi->dbg_peb_buf, len, 1);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001253 err = 1;
1254error:
1255 ubi_dbg_dump_stack();
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001256 mutex_unlock(&ubi->dbg_buf_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001257 return err;
1258}
1259
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001260#endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */