blob: b587140110916507e299aaa9e2f81e7d500fda48 [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 */
Adrian Hunter0c6c7fa2009-06-26 14:58:01 +0300566static int validate_ec_hdr(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;
Adrian Hunter0c6c7fa2009-06-26 14:58:01 +0300570 int vid_hdr_offset, leb_start, image_seq;
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);
Adrian Hunter0c6c7fa2009-06-26 14:58:01 +0300575 image_seq = be32_to_cpu(ec_hdr->image_seq);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400576
577 if (ec_hdr->version != UBI_VERSION) {
578 ubi_err("node with incompatible UBI version found: "
579 "this UBI version is %d, image version is %d",
580 UBI_VERSION, (int)ec_hdr->version);
581 goto bad;
582 }
583
584 if (vid_hdr_offset != ubi->vid_hdr_offset) {
585 ubi_err("bad VID header offset %d, expected %d",
586 vid_hdr_offset, ubi->vid_hdr_offset);
587 goto bad;
588 }
589
590 if (leb_start != ubi->leb_start) {
591 ubi_err("bad data offset %d, expected %d",
592 leb_start, ubi->leb_start);
593 goto bad;
594 }
595
596 if (ec < 0 || ec > UBI_MAX_ERASECOUNTER) {
597 ubi_err("bad erase counter %lld", ec);
598 goto bad;
599 }
600
Adrian Hunter0c6c7fa2009-06-26 14:58:01 +0300601 if (!ubi->image_seq_set) {
602 ubi->image_seq = image_seq;
603 ubi->image_seq_set = 1;
604 } else if (ubi->image_seq != image_seq) {
605 ubi_err("bad image sequence number %d, expected %d",
606 image_seq, ubi->image_seq);
607 goto bad;
608 }
609
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400610 return 0;
611
612bad:
613 ubi_err("bad EC header");
614 ubi_dbg_dump_ec_hdr(ec_hdr);
615 ubi_dbg_dump_stack();
616 return 1;
617}
618
619/**
620 * ubi_io_read_ec_hdr - read and check an erase counter header.
621 * @ubi: UBI device description object
622 * @pnum: physical eraseblock to read from
623 * @ec_hdr: a &struct ubi_ec_hdr object where to store the read erase counter
624 * header
625 * @verbose: be verbose if the header is corrupted or was not found
626 *
627 * This function reads erase counter header from physical eraseblock @pnum and
628 * stores it in @ec_hdr. This function also checks CRC checksum of the read
629 * erase counter header. The following codes may be returned:
630 *
631 * o %0 if the CRC checksum is correct and the header was successfully read;
632 * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected
633 * and corrected by the flash driver; this is harmless but may indicate that
634 * this eraseblock may become bad soon (but may be not);
635 * o %UBI_IO_BAD_EC_HDR if the erase counter header is corrupted (a CRC error);
636 * o %UBI_IO_PEB_EMPTY if the physical eraseblock is empty;
637 * o a negative error code in case of failure.
638 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300639int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400640 struct ubi_ec_hdr *ec_hdr, int verbose)
641{
642 int err, read_err = 0;
643 uint32_t crc, magic, hdr_crc;
644
645 dbg_io("read EC header from PEB %d", pnum);
646 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
647
648 err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE);
649 if (err) {
650 if (err != UBI_IO_BITFLIPS && err != -EBADMSG)
651 return err;
652
653 /*
654 * We read all the data, but either a correctable bit-flip
655 * occurred, or MTD reported about some data integrity error,
656 * like an ECC error in case of NAND. The former is harmless,
657 * the later may mean that the read data is corrupted. But we
658 * have a CRC check-sum and we will detect this. If the EC
659 * header is still OK, we just report this as there was a
660 * bit-flip.
661 */
662 read_err = err;
663 }
664
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300665 magic = be32_to_cpu(ec_hdr->magic);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400666 if (magic != UBI_EC_HDR_MAGIC) {
667 /*
668 * The magic field is wrong. Let's check if we have read all
669 * 0xFF. If yes, this physical eraseblock is assumed to be
670 * empty.
671 *
672 * But if there was a read error, we do not test it for all
673 * 0xFFs. Even if it does contain all 0xFFs, this error
674 * indicates that something is still wrong with this physical
675 * eraseblock and we anyway cannot treat it as empty.
676 */
677 if (read_err != -EBADMSG &&
678 check_pattern(ec_hdr, 0xFF, UBI_EC_HDR_SIZE)) {
679 /* The physical eraseblock is supposedly empty */
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400680 if (verbose)
681 ubi_warn("no EC header found at PEB %d, "
682 "only 0xFF bytes", pnum);
Artem Bityutskiyed458192008-11-12 10:14:10 +0200683 else if (UBI_IO_DEBUG)
684 dbg_msg("no EC header found at PEB %d, "
685 "only 0xFF bytes", pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400686 return UBI_IO_PEB_EMPTY;
687 }
688
689 /*
690 * This is not a valid erase counter header, and these are not
691 * 0xFF bytes. Report that the header is corrupted.
692 */
693 if (verbose) {
694 ubi_warn("bad magic number at PEB %d: %08x instead of "
695 "%08x", pnum, magic, UBI_EC_HDR_MAGIC);
696 ubi_dbg_dump_ec_hdr(ec_hdr);
Artem Bityutskiyed458192008-11-12 10:14:10 +0200697 } else if (UBI_IO_DEBUG)
698 dbg_msg("bad magic number at PEB %d: %08x instead of "
699 "%08x", pnum, magic, UBI_EC_HDR_MAGIC);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400700 return UBI_IO_BAD_EC_HDR;
701 }
702
703 crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300704 hdr_crc = be32_to_cpu(ec_hdr->hdr_crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400705
706 if (hdr_crc != crc) {
707 if (verbose) {
Artem Bityutskiy9c9ec142008-07-18 13:19:52 +0300708 ubi_warn("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 ubi_dbg_dump_ec_hdr(ec_hdr);
Artem Bityutskiyed458192008-11-12 10:14:10 +0200711 } else if (UBI_IO_DEBUG)
712 dbg_msg("bad EC header CRC at PEB %d, calculated "
713 "%#08x, read %#08x", pnum, crc, hdr_crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400714 return UBI_IO_BAD_EC_HDR;
715 }
716
717 /* And of course validate what has just been read from the media */
718 err = validate_ec_hdr(ubi, ec_hdr);
719 if (err) {
720 ubi_err("validation failed for PEB %d", pnum);
721 return -EINVAL;
722 }
723
724 return read_err ? UBI_IO_BITFLIPS : 0;
725}
726
727/**
728 * ubi_io_write_ec_hdr - write an erase counter header.
729 * @ubi: UBI device description object
730 * @pnum: physical eraseblock to write to
731 * @ec_hdr: the erase counter header to write
732 *
733 * This function writes erase counter header described by @ec_hdr to physical
734 * eraseblock @pnum. It also fills most fields of @ec_hdr before writing, so
735 * the caller do not have to fill them. Callers must only fill the @ec_hdr->ec
736 * field.
737 *
738 * This function returns zero in case of success and a negative error code in
739 * case of failure. If %-EIO is returned, the physical eraseblock most probably
740 * went bad.
741 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300742int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400743 struct ubi_ec_hdr *ec_hdr)
744{
745 int err;
746 uint32_t crc;
747
748 dbg_io("write EC header to PEB %d", pnum);
749 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
750
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300751 ec_hdr->magic = cpu_to_be32(UBI_EC_HDR_MAGIC);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400752 ec_hdr->version = UBI_VERSION;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300753 ec_hdr->vid_hdr_offset = cpu_to_be32(ubi->vid_hdr_offset);
754 ec_hdr->data_offset = cpu_to_be32(ubi->leb_start);
Adrian Hunter0c6c7fa2009-06-26 14:58:01 +0300755 ec_hdr->image_seq = cpu_to_be32(ubi->image_seq);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400756 crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300757 ec_hdr->hdr_crc = cpu_to_be32(crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400758
759 err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr);
760 if (err)
761 return -EINVAL;
762
763 err = ubi_io_write(ubi, ec_hdr, pnum, 0, ubi->ec_hdr_alsize);
764 return err;
765}
766
767/**
768 * validate_vid_hdr - validate a volume identifier header.
769 * @ubi: UBI device description object
770 * @vid_hdr: the volume identifier header to check
771 *
772 * This function checks that data stored in the volume identifier header
773 * @vid_hdr. Returns zero if the VID header is OK and %1 if not.
774 */
775static int validate_vid_hdr(const struct ubi_device *ubi,
776 const struct ubi_vid_hdr *vid_hdr)
777{
778 int vol_type = vid_hdr->vol_type;
779 int copy_flag = vid_hdr->copy_flag;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300780 int vol_id = be32_to_cpu(vid_hdr->vol_id);
781 int lnum = be32_to_cpu(vid_hdr->lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400782 int compat = vid_hdr->compat;
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300783 int data_size = be32_to_cpu(vid_hdr->data_size);
784 int used_ebs = be32_to_cpu(vid_hdr->used_ebs);
785 int data_pad = be32_to_cpu(vid_hdr->data_pad);
786 int data_crc = be32_to_cpu(vid_hdr->data_crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400787 int usable_leb_size = ubi->leb_size - data_pad;
788
789 if (copy_flag != 0 && copy_flag != 1) {
790 dbg_err("bad copy_flag");
791 goto bad;
792 }
793
794 if (vol_id < 0 || lnum < 0 || data_size < 0 || used_ebs < 0 ||
795 data_pad < 0) {
796 dbg_err("negative values");
797 goto bad;
798 }
799
800 if (vol_id >= UBI_MAX_VOLUMES && vol_id < UBI_INTERNAL_VOL_START) {
801 dbg_err("bad vol_id");
802 goto bad;
803 }
804
805 if (vol_id < UBI_INTERNAL_VOL_START && compat != 0) {
806 dbg_err("bad compat");
807 goto bad;
808 }
809
810 if (vol_id >= UBI_INTERNAL_VOL_START && compat != UBI_COMPAT_DELETE &&
811 compat != UBI_COMPAT_RO && compat != UBI_COMPAT_PRESERVE &&
812 compat != UBI_COMPAT_REJECT) {
813 dbg_err("bad compat");
814 goto bad;
815 }
816
817 if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) {
818 dbg_err("bad vol_type");
819 goto bad;
820 }
821
822 if (data_pad >= ubi->leb_size / 2) {
823 dbg_err("bad data_pad");
824 goto bad;
825 }
826
827 if (vol_type == UBI_VID_STATIC) {
828 /*
829 * Although from high-level point of view static volumes may
830 * contain zero bytes of data, but no VID headers can contain
831 * zero at these fields, because they empty volumes do not have
832 * mapped logical eraseblocks.
833 */
834 if (used_ebs == 0) {
835 dbg_err("zero used_ebs");
836 goto bad;
837 }
838 if (data_size == 0) {
839 dbg_err("zero data_size");
840 goto bad;
841 }
842 if (lnum < used_ebs - 1) {
843 if (data_size != usable_leb_size) {
844 dbg_err("bad data_size");
845 goto bad;
846 }
847 } else if (lnum == used_ebs - 1) {
848 if (data_size == 0) {
849 dbg_err("bad data_size at last LEB");
850 goto bad;
851 }
852 } else {
853 dbg_err("too high lnum");
854 goto bad;
855 }
856 } else {
857 if (copy_flag == 0) {
858 if (data_crc != 0) {
859 dbg_err("non-zero data CRC");
860 goto bad;
861 }
862 if (data_size != 0) {
863 dbg_err("non-zero data_size");
864 goto bad;
865 }
866 } else {
867 if (data_size == 0) {
868 dbg_err("zero data_size of copy");
869 goto bad;
870 }
871 }
872 if (used_ebs != 0) {
873 dbg_err("bad used_ebs");
874 goto bad;
875 }
876 }
877
878 return 0;
879
880bad:
881 ubi_err("bad VID header");
882 ubi_dbg_dump_vid_hdr(vid_hdr);
883 ubi_dbg_dump_stack();
884 return 1;
885}
886
887/**
888 * ubi_io_read_vid_hdr - read and check a volume identifier header.
889 * @ubi: UBI device description object
890 * @pnum: physical eraseblock number to read from
891 * @vid_hdr: &struct ubi_vid_hdr object where to store the read volume
892 * identifier header
893 * @verbose: be verbose if the header is corrupted or wasn't found
894 *
895 * This function reads the volume identifier header from physical eraseblock
896 * @pnum and stores it in @vid_hdr. It also checks CRC checksum of the read
897 * volume identifier header. The following codes may be returned:
898 *
899 * o %0 if the CRC checksum is correct and the header was successfully read;
900 * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected
901 * and corrected by the flash driver; this is harmless but may indicate that
902 * this eraseblock may become bad soon;
Artem Bityutskiy815bc5f2009-06-08 19:28:18 +0300903 * o %UBI_IO_BAD_VID_HDR if the volume identifier header is corrupted (a CRC
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400904 * error detected);
905 * o %UBI_IO_PEB_FREE if the physical eraseblock is free (i.e., there is no VID
906 * header there);
907 * o a negative error code in case of failure.
908 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300909int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400910 struct ubi_vid_hdr *vid_hdr, int verbose)
911{
912 int err, read_err = 0;
913 uint32_t crc, magic, hdr_crc;
914 void *p;
915
916 dbg_io("read VID header from PEB %d", pnum);
917 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
918
919 p = (char *)vid_hdr - ubi->vid_hdr_shift;
920 err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset,
921 ubi->vid_hdr_alsize);
922 if (err) {
923 if (err != UBI_IO_BITFLIPS && err != -EBADMSG)
924 return err;
925
926 /*
927 * We read all the data, but either a correctable bit-flip
928 * occurred, or MTD reported about some data integrity error,
929 * like an ECC error in case of NAND. The former is harmless,
930 * the later may mean the read data is corrupted. But we have a
931 * CRC check-sum and we will identify this. If the VID header is
932 * still OK, we just report this as there was a bit-flip.
933 */
934 read_err = err;
935 }
936
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300937 magic = be32_to_cpu(vid_hdr->magic);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400938 if (magic != UBI_VID_HDR_MAGIC) {
939 /*
940 * If we have read all 0xFF bytes, the VID header probably does
941 * not exist and the physical eraseblock is assumed to be free.
942 *
943 * But if there was a read error, we do not test the data for
944 * 0xFFs. Even if it does contain all 0xFFs, this error
945 * indicates that something is still wrong with this physical
946 * eraseblock and it cannot be regarded as free.
947 */
948 if (read_err != -EBADMSG &&
949 check_pattern(vid_hdr, 0xFF, UBI_VID_HDR_SIZE)) {
950 /* The physical eraseblock is supposedly free */
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400951 if (verbose)
952 ubi_warn("no VID header found at PEB %d, "
953 "only 0xFF bytes", pnum);
Artem Bityutskiyed458192008-11-12 10:14:10 +0200954 else if (UBI_IO_DEBUG)
955 dbg_msg("no VID header found at PEB %d, "
956 "only 0xFF bytes", pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400957 return UBI_IO_PEB_FREE;
958 }
959
960 /*
961 * This is not a valid VID header, and these are not 0xFF
962 * bytes. Report that the header is corrupted.
963 */
964 if (verbose) {
965 ubi_warn("bad magic number at PEB %d: %08x instead of "
966 "%08x", pnum, magic, UBI_VID_HDR_MAGIC);
967 ubi_dbg_dump_vid_hdr(vid_hdr);
Artem Bityutskiyed458192008-11-12 10:14:10 +0200968 } else if (UBI_IO_DEBUG)
969 dbg_msg("bad magic number at PEB %d: %08x instead of "
970 "%08x", pnum, magic, UBI_VID_HDR_MAGIC);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400971 return UBI_IO_BAD_VID_HDR;
972 }
973
974 crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC);
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300975 hdr_crc = be32_to_cpu(vid_hdr->hdr_crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400976
977 if (hdr_crc != crc) {
978 if (verbose) {
979 ubi_warn("bad CRC at PEB %d, calculated %#08x, "
980 "read %#08x", pnum, crc, hdr_crc);
981 ubi_dbg_dump_vid_hdr(vid_hdr);
Artem Bityutskiyed458192008-11-12 10:14:10 +0200982 } else if (UBI_IO_DEBUG)
983 dbg_msg("bad CRC at PEB %d, calculated %#08x, "
984 "read %#08x", pnum, crc, hdr_crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400985 return UBI_IO_BAD_VID_HDR;
986 }
987
988 /* Validate the VID header that we have just read */
989 err = validate_vid_hdr(ubi, vid_hdr);
990 if (err) {
991 ubi_err("validation failed for PEB %d", pnum);
992 return -EINVAL;
993 }
994
995 return read_err ? UBI_IO_BITFLIPS : 0;
996}
997
998/**
999 * ubi_io_write_vid_hdr - write a volume identifier header.
1000 * @ubi: UBI device description object
1001 * @pnum: the physical eraseblock number to write to
1002 * @vid_hdr: the volume identifier header to write
1003 *
1004 * This function writes the volume identifier header described by @vid_hdr to
1005 * physical eraseblock @pnum. This function automatically fills the
1006 * @vid_hdr->magic and the @vid_hdr->version fields, as well as calculates
1007 * header CRC checksum and stores it at vid_hdr->hdr_crc.
1008 *
1009 * This function returns zero in case of success and a negative error code in
1010 * case of failure. If %-EIO is returned, the physical eraseblock probably went
1011 * bad.
1012 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001013int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001014 struct ubi_vid_hdr *vid_hdr)
1015{
1016 int err;
1017 uint32_t crc;
1018 void *p;
1019
1020 dbg_io("write VID header to PEB %d", pnum);
1021 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
1022
1023 err = paranoid_check_peb_ec_hdr(ubi, pnum);
1024 if (err)
Artem Bityutskiyf2863c52008-12-28 12:20:51 +02001025 return err > 0 ? -EINVAL : err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001026
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001027 vid_hdr->magic = cpu_to_be32(UBI_VID_HDR_MAGIC);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001028 vid_hdr->version = UBI_VERSION;
1029 crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC);
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001030 vid_hdr->hdr_crc = cpu_to_be32(crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001031
1032 err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr);
1033 if (err)
1034 return -EINVAL;
1035
1036 p = (char *)vid_hdr - ubi->vid_hdr_shift;
1037 err = ubi_io_write(ubi, p, pnum, ubi->vid_hdr_aloffset,
1038 ubi->vid_hdr_alsize);
1039 return err;
1040}
1041
1042#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
1043
1044/**
1045 * paranoid_check_not_bad - ensure that a physical eraseblock is not bad.
1046 * @ubi: UBI device description object
1047 * @pnum: physical eraseblock number to check
1048 *
1049 * This function returns zero if the physical eraseblock is good, a positive
1050 * number if it is bad and a negative error code if an error occurred.
1051 */
1052static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum)
1053{
1054 int err;
1055
1056 err = ubi_io_is_bad(ubi, pnum);
1057 if (!err)
1058 return err;
1059
1060 ubi_err("paranoid check failed for PEB %d", pnum);
1061 ubi_dbg_dump_stack();
1062 return err;
1063}
1064
1065/**
1066 * paranoid_check_ec_hdr - check if an erase counter header is all right.
1067 * @ubi: UBI device description object
1068 * @pnum: physical eraseblock number the erase counter header belongs to
1069 * @ec_hdr: the erase counter header to check
1070 *
1071 * This function returns zero if the erase counter header contains valid
1072 * values, and %1 if not.
1073 */
1074static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum,
1075 const struct ubi_ec_hdr *ec_hdr)
1076{
1077 int err;
1078 uint32_t magic;
1079
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001080 magic = be32_to_cpu(ec_hdr->magic);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001081 if (magic != UBI_EC_HDR_MAGIC) {
1082 ubi_err("bad magic %#08x, must be %#08x",
1083 magic, UBI_EC_HDR_MAGIC);
1084 goto fail;
1085 }
1086
1087 err = validate_ec_hdr(ubi, ec_hdr);
1088 if (err) {
1089 ubi_err("paranoid check failed for PEB %d", pnum);
1090 goto fail;
1091 }
1092
1093 return 0;
1094
1095fail:
1096 ubi_dbg_dump_ec_hdr(ec_hdr);
1097 ubi_dbg_dump_stack();
1098 return 1;
1099}
1100
1101/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +03001102 * paranoid_check_peb_ec_hdr - check erase counter header.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001103 * @ubi: UBI device description object
1104 * @pnum: the physical eraseblock number to check
1105 *
1106 * This function returns zero if the erase counter header is all right, %1 if
1107 * not, and a negative error code if an error occurred.
1108 */
1109static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum)
1110{
1111 int err;
1112 uint32_t crc, hdr_crc;
1113 struct ubi_ec_hdr *ec_hdr;
1114
Artem Bityutskiy33818bb2007-08-28 21:29:32 +03001115 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001116 if (!ec_hdr)
1117 return -ENOMEM;
1118
1119 err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE);
1120 if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG)
1121 goto exit;
1122
1123 crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001124 hdr_crc = be32_to_cpu(ec_hdr->hdr_crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001125 if (hdr_crc != crc) {
1126 ubi_err("bad CRC, calculated %#08x, read %#08x", crc, hdr_crc);
1127 ubi_err("paranoid check failed for PEB %d", pnum);
1128 ubi_dbg_dump_ec_hdr(ec_hdr);
1129 ubi_dbg_dump_stack();
1130 err = 1;
1131 goto exit;
1132 }
1133
1134 err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr);
1135
1136exit:
1137 kfree(ec_hdr);
1138 return err;
1139}
1140
1141/**
1142 * paranoid_check_vid_hdr - check that a volume identifier header is all right.
1143 * @ubi: UBI device description object
1144 * @pnum: physical eraseblock number the volume identifier header belongs to
1145 * @vid_hdr: the volume identifier header to check
1146 *
1147 * This function returns zero if the volume identifier header is all right, and
1148 * %1 if not.
1149 */
1150static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum,
1151 const struct ubi_vid_hdr *vid_hdr)
1152{
1153 int err;
1154 uint32_t magic;
1155
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001156 magic = be32_to_cpu(vid_hdr->magic);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001157 if (magic != UBI_VID_HDR_MAGIC) {
1158 ubi_err("bad VID header magic %#08x at PEB %d, must be %#08x",
1159 magic, pnum, UBI_VID_HDR_MAGIC);
1160 goto fail;
1161 }
1162
1163 err = validate_vid_hdr(ubi, vid_hdr);
1164 if (err) {
1165 ubi_err("paranoid check failed for PEB %d", pnum);
1166 goto fail;
1167 }
1168
1169 return err;
1170
1171fail:
1172 ubi_err("paranoid check failed for PEB %d", pnum);
1173 ubi_dbg_dump_vid_hdr(vid_hdr);
1174 ubi_dbg_dump_stack();
1175 return 1;
1176
1177}
1178
1179/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +03001180 * paranoid_check_peb_vid_hdr - check volume identifier header.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001181 * @ubi: UBI device description object
1182 * @pnum: the physical eraseblock number to check
1183 *
1184 * This function returns zero if the volume identifier header is all right,
1185 * %1 if not, and a negative error code if an error occurred.
1186 */
1187static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum)
1188{
1189 int err;
1190 uint32_t crc, hdr_crc;
1191 struct ubi_vid_hdr *vid_hdr;
1192 void *p;
1193
Artem Bityutskiy33818bb2007-08-28 21:29:32 +03001194 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001195 if (!vid_hdr)
1196 return -ENOMEM;
1197
1198 p = (char *)vid_hdr - ubi->vid_hdr_shift;
1199 err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset,
1200 ubi->vid_hdr_alsize);
1201 if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG)
1202 goto exit;
1203
1204 crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_EC_HDR_SIZE_CRC);
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001205 hdr_crc = be32_to_cpu(vid_hdr->hdr_crc);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001206 if (hdr_crc != crc) {
1207 ubi_err("bad VID header CRC at PEB %d, calculated %#08x, "
1208 "read %#08x", pnum, crc, hdr_crc);
1209 ubi_err("paranoid check failed for PEB %d", pnum);
1210 ubi_dbg_dump_vid_hdr(vid_hdr);
1211 ubi_dbg_dump_stack();
1212 err = 1;
1213 goto exit;
1214 }
1215
1216 err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr);
1217
1218exit:
1219 ubi_free_vid_hdr(ubi, vid_hdr);
1220 return err;
1221}
1222
1223/**
Artem Bityutskiy40a71a82009-06-28 19:16:55 +03001224 * ubi_dbg_check_all_ff - check that a region of flash is empty.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001225 * @ubi: UBI device description object
1226 * @pnum: the physical eraseblock number to check
1227 * @offset: the starting offset within the physical eraseblock to check
1228 * @len: the length of the region to check
1229 *
1230 * This function returns zero if only 0xFF bytes are present at offset
1231 * @offset of the physical eraseblock @pnum, %1 if not, and a negative error
1232 * code if an error occurred.
1233 */
Artem Bityutskiy40a71a82009-06-28 19:16:55 +03001234int ubi_dbg_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001235{
1236 size_t read;
1237 int err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001238 loff_t addr = (loff_t)pnum * ubi->peb_size + offset;
1239
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001240 mutex_lock(&ubi->dbg_buf_mutex);
1241 err = ubi->mtd->read(ubi->mtd, addr, len, &read, ubi->dbg_peb_buf);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001242 if (err && err != -EUCLEAN) {
1243 ubi_err("error %d while reading %d bytes from PEB %d:%d, "
1244 "read %zd bytes", err, len, pnum, offset, read);
1245 goto error;
1246 }
1247
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001248 err = check_pattern(ubi->dbg_peb_buf, 0xFF, len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001249 if (err == 0) {
1250 ubi_err("flash region at PEB %d:%d, length %d does not "
1251 "contain all 0xFF bytes", pnum, offset, len);
1252 goto fail;
1253 }
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001254 mutex_unlock(&ubi->dbg_buf_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001255
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001256 return 0;
1257
1258fail:
1259 ubi_err("paranoid check failed for PEB %d", pnum);
Artem Bityutskiyc8566352008-07-16 17:40:22 +03001260 ubi_msg("hex dump of the %d-%d region", offset, offset + len);
Artem Bityutskiy69866462007-08-29 14:56:20 +03001261 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001262 ubi->dbg_peb_buf, len, 1);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001263 err = 1;
1264error:
1265 ubi_dbg_dump_stack();
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001266 mutex_unlock(&ubi->dbg_buf_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001267 return err;
1268}
1269
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001270#endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */