Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1 | /* |
| 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 Bityutskiy | 85c6e6e | 2008-07-16 10:25:56 +0300 | [diff] [blame] | 23 | * UBI input/output sub-system. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 24 | * |
Artem Bityutskiy | 85c6e6e | 2008-07-16 10:25:56 +0300 | [diff] [blame] | 25 | * 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. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 28 | * |
| 29 | * We are trying to have a paranoid mindset and not to trust to what we read |
Artem Bityutskiy | 85c6e6e | 2008-07-16 10:25:56 +0300 | [diff] [blame] | 30 | * 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. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 32 | * |
| 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 Bityutskiy | 85c6e6e | 2008-07-16 10:25:56 +0300 | [diff] [blame] | 82 | * 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. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 87 | */ |
| 88 | |
| 89 | #include <linux/crc32.h> |
| 90 | #include <linux/err.h> |
| 91 | #include "ubi.h" |
| 92 | |
| 93 | #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID |
| 94 | static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum); |
| 95 | static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum); |
| 96 | static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum, |
| 97 | const struct ubi_ec_hdr *ec_hdr); |
| 98 | static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum); |
| 99 | static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum, |
| 100 | const struct ubi_vid_hdr *vid_hdr); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 101 | #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. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 107 | #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 Bityutskiy | 63b6c1e | 2007-07-17 15:04:20 +0300 | [diff] [blame] | 125 | * 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. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 128 | * o %-EIO if some I/O error occurred; |
| 129 | * o other negative error codes in case of other errors. |
| 130 | */ |
| 131 | int 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) |
Artem Bityutskiy | adbf05e | 2010-01-20 10:28:58 +0200 | [diff] [blame] | 146 | return err; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 147 | |
| 148 | addr = (loff_t)pnum * ubi->peb_size + offset; |
| 149 | retry: |
| 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 Bityutskiy | 8c1e6ee | 2008-07-18 12:20:23 +0300 | [diff] [blame] | 156 | * |
| 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. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 160 | */ |
Artem Bityutskiy | 8c1e6ee | 2008-07-18 12:20:23 +0300 | [diff] [blame] | 161 | dbg_msg("fixable bit-flip detected at PEB %d", pnum); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 162 | ubi_assert(len == read); |
| 163 | return UBI_IO_BITFLIPS; |
| 164 | } |
| 165 | |
| 166 | if (read != len && retries++ < UBI_IO_RETRIES) { |
Artem Bityutskiy | 9c9ec14 | 2008-07-18 13:19:52 +0300 | [diff] [blame] | 167 | dbg_io("error %d while reading %d bytes from PEB %d:%d," |
| 168 | " read only %zd bytes, retry", |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 169 | 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 Bityutskiy | 2362a53 | 2007-10-18 20:09:41 +0300 | [diff] [blame] | 177 | |
| 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. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 187 | } else { |
| 188 | ubi_assert(len == read); |
| 189 | |
| 190 | if (ubi_dbg_is_bitflip()) { |
Artem Bityutskiy | c856635 | 2008-07-16 17:40:22 +0300 | [diff] [blame] | 191 | dbg_gen("bit-flip (emulated)"); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 192 | 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 Bityutskiy | e88d6e10 | 2007-08-29 14:51:52 +0300 | [diff] [blame] | 216 | int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset, |
| 217 | int len) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 218 | { |
| 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) |
Artem Bityutskiy | adbf05e | 2010-01-20 10:28:58 +0200 | [diff] [blame] | 239 | return err; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 240 | |
| 241 | /* The area we are writing to has to contain all 0xFF bytes */ |
Artem Bityutskiy | 40a71a8 | 2009-06-28 19:16:55 +0300 | [diff] [blame] | 242 | err = ubi_dbg_check_all_ff(ubi, pnum, offset, len); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 243 | if (err) |
Artem Bityutskiy | adbf05e | 2010-01-20 10:28:58 +0200 | [diff] [blame] | 244 | return err; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 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) |
Artem Bityutskiy | adbf05e | 2010-01-20 10:28:58 +0200 | [diff] [blame] | 253 | return err; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 254 | err = paranoid_check_peb_vid_hdr(ubi, pnum); |
| 255 | if (err) |
Artem Bityutskiy | adbf05e | 2010-01-20 10:28:58 +0200 | [diff] [blame] | 256 | return err; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 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) { |
Artem Bityutskiy | ebf53f4 | 2009-07-06 08:57:53 +0300 | [diff] [blame] | 269 | ubi_err("error %d while writing %d bytes to PEB %d:%d, written " |
| 270 | "%zd bytes", err, len, pnum, offset, written); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 271 | ubi_dbg_dump_stack(); |
Artem Bityutskiy | 867996b | 2009-07-24 15:31:33 +0300 | [diff] [blame] | 272 | ubi_dbg_dump_flash(ubi, pnum, offset, len); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 273 | } else |
| 274 | ubi_assert(written == len); |
| 275 | |
Artem Bityutskiy | 6e9065d | 2010-01-25 17:09:30 +0200 | [diff] [blame] | 276 | if (!err) { |
| 277 | err = ubi_dbg_check_write(ubi, buf, pnum, offset, len); |
| 278 | if (err) |
| 279 | return err; |
| 280 | |
| 281 | /* |
| 282 | * Since we always write sequentially, the rest of the PEB has |
| 283 | * to contain only 0xFF bytes. |
| 284 | */ |
| 285 | offset += len; |
| 286 | len = ubi->peb_size - offset; |
| 287 | if (len) |
| 288 | err = ubi_dbg_check_all_ff(ubi, pnum, offset, len); |
| 289 | } |
| 290 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 291 | return err; |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * erase_callback - MTD erasure call-back. |
| 296 | * @ei: MTD erase information object. |
| 297 | * |
| 298 | * Note, even though MTD erase interface is asynchronous, all the current |
| 299 | * implementations are synchronous anyway. |
| 300 | */ |
| 301 | static void erase_callback(struct erase_info *ei) |
| 302 | { |
| 303 | wake_up_interruptible((wait_queue_head_t *)ei->priv); |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * do_sync_erase - synchronously erase a physical eraseblock. |
| 308 | * @ubi: UBI device description object |
| 309 | * @pnum: the physical eraseblock number to erase |
| 310 | * |
| 311 | * This function synchronously erases physical eraseblock @pnum and returns |
| 312 | * zero in case of success and a negative error code in case of failure. If |
| 313 | * %-EIO is returned, the physical eraseblock most probably went bad. |
| 314 | */ |
Artem Bityutskiy | e88d6e10 | 2007-08-29 14:51:52 +0300 | [diff] [blame] | 315 | static int do_sync_erase(struct ubi_device *ubi, int pnum) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 316 | { |
| 317 | int err, retries = 0; |
| 318 | struct erase_info ei; |
| 319 | wait_queue_head_t wq; |
| 320 | |
| 321 | dbg_io("erase PEB %d", pnum); |
| 322 | |
| 323 | retry: |
| 324 | init_waitqueue_head(&wq); |
| 325 | memset(&ei, 0, sizeof(struct erase_info)); |
| 326 | |
| 327 | ei.mtd = ubi->mtd; |
Brijesh Singh | 2f176f7 | 2007-07-05 15:07:35 +0530 | [diff] [blame] | 328 | ei.addr = (loff_t)pnum * ubi->peb_size; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 329 | ei.len = ubi->peb_size; |
| 330 | ei.callback = erase_callback; |
| 331 | ei.priv = (unsigned long)&wq; |
| 332 | |
| 333 | err = ubi->mtd->erase(ubi->mtd, &ei); |
| 334 | if (err) { |
| 335 | if (retries++ < UBI_IO_RETRIES) { |
| 336 | dbg_io("error %d while erasing PEB %d, retry", |
| 337 | err, pnum); |
| 338 | yield(); |
| 339 | goto retry; |
| 340 | } |
| 341 | ubi_err("cannot erase PEB %d, error %d", pnum, err); |
| 342 | ubi_dbg_dump_stack(); |
| 343 | return err; |
| 344 | } |
| 345 | |
| 346 | err = wait_event_interruptible(wq, ei.state == MTD_ERASE_DONE || |
| 347 | ei.state == MTD_ERASE_FAILED); |
| 348 | if (err) { |
| 349 | ubi_err("interrupted PEB %d erasure", pnum); |
| 350 | return -EINTR; |
| 351 | } |
| 352 | |
| 353 | if (ei.state == MTD_ERASE_FAILED) { |
| 354 | if (retries++ < UBI_IO_RETRIES) { |
| 355 | dbg_io("error while erasing PEB %d, retry", pnum); |
| 356 | yield(); |
| 357 | goto retry; |
| 358 | } |
| 359 | ubi_err("cannot erase PEB %d", pnum); |
| 360 | ubi_dbg_dump_stack(); |
| 361 | return -EIO; |
| 362 | } |
| 363 | |
Artem Bityutskiy | 40a71a8 | 2009-06-28 19:16:55 +0300 | [diff] [blame] | 364 | err = ubi_dbg_check_all_ff(ubi, pnum, 0, ubi->peb_size); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 365 | if (err) |
Artem Bityutskiy | adbf05e | 2010-01-20 10:28:58 +0200 | [diff] [blame] | 366 | return err; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 367 | |
| 368 | if (ubi_dbg_is_erase_failure() && !err) { |
| 369 | dbg_err("cannot erase PEB %d (emulated)", pnum); |
| 370 | return -EIO; |
| 371 | } |
| 372 | |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * check_pattern - check if buffer contains only a certain byte pattern. |
| 378 | * @buf: buffer to check |
| 379 | * @patt: the pattern to check |
| 380 | * @size: buffer size in bytes |
| 381 | * |
| 382 | * This function returns %1 in there are only @patt bytes in @buf, and %0 if |
| 383 | * something else was also found. |
| 384 | */ |
| 385 | static int check_pattern(const void *buf, uint8_t patt, int size) |
| 386 | { |
| 387 | int i; |
| 388 | |
| 389 | for (i = 0; i < size; i++) |
| 390 | if (((const uint8_t *)buf)[i] != patt) |
| 391 | return 0; |
| 392 | return 1; |
| 393 | } |
| 394 | |
| 395 | /* Patterns to write to a physical eraseblock when torturing it */ |
| 396 | static uint8_t patterns[] = {0xa5, 0x5a, 0x0}; |
| 397 | |
| 398 | /** |
| 399 | * torture_peb - test a supposedly bad physical eraseblock. |
| 400 | * @ubi: UBI device description object |
| 401 | * @pnum: the physical eraseblock number to test |
| 402 | * |
| 403 | * This function returns %-EIO if the physical eraseblock did not pass the |
| 404 | * test, a positive number of erase operations done if the test was |
| 405 | * successfully passed, and other negative error codes in case of other errors. |
| 406 | */ |
Artem Bityutskiy | e88d6e10 | 2007-08-29 14:51:52 +0300 | [diff] [blame] | 407 | static int torture_peb(struct ubi_device *ubi, int pnum) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 408 | { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 409 | int err, i, patt_count; |
| 410 | |
Artem Bityutskiy | 8c1e6ee | 2008-07-18 12:20:23 +0300 | [diff] [blame] | 411 | ubi_msg("run torture test for PEB %d", pnum); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 412 | patt_count = ARRAY_SIZE(patterns); |
| 413 | ubi_assert(patt_count > 0); |
| 414 | |
Artem Bityutskiy | e88d6e10 | 2007-08-29 14:51:52 +0300 | [diff] [blame] | 415 | mutex_lock(&ubi->buf_mutex); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 416 | for (i = 0; i < patt_count; i++) { |
| 417 | err = do_sync_erase(ubi, pnum); |
| 418 | if (err) |
| 419 | goto out; |
| 420 | |
| 421 | /* Make sure the PEB contains only 0xFF bytes */ |
Artem Bityutskiy | e88d6e10 | 2007-08-29 14:51:52 +0300 | [diff] [blame] | 422 | err = ubi_io_read(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 423 | if (err) |
| 424 | goto out; |
| 425 | |
Artem Bityutskiy | e88d6e10 | 2007-08-29 14:51:52 +0300 | [diff] [blame] | 426 | err = check_pattern(ubi->peb_buf1, 0xFF, ubi->peb_size); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 427 | if (err == 0) { |
| 428 | ubi_err("erased PEB %d, but a non-0xFF byte found", |
| 429 | pnum); |
| 430 | err = -EIO; |
| 431 | goto out; |
| 432 | } |
| 433 | |
| 434 | /* Write a pattern and check it */ |
Artem Bityutskiy | e88d6e10 | 2007-08-29 14:51:52 +0300 | [diff] [blame] | 435 | memset(ubi->peb_buf1, patterns[i], ubi->peb_size); |
| 436 | err = ubi_io_write(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 437 | if (err) |
| 438 | goto out; |
| 439 | |
Artem Bityutskiy | e88d6e10 | 2007-08-29 14:51:52 +0300 | [diff] [blame] | 440 | memset(ubi->peb_buf1, ~patterns[i], ubi->peb_size); |
| 441 | err = ubi_io_read(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 442 | if (err) |
| 443 | goto out; |
| 444 | |
Artem Bityutskiy | e88d6e10 | 2007-08-29 14:51:52 +0300 | [diff] [blame] | 445 | err = check_pattern(ubi->peb_buf1, patterns[i], ubi->peb_size); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 446 | if (err == 0) { |
| 447 | ubi_err("pattern %x checking failed for PEB %d", |
| 448 | patterns[i], pnum); |
| 449 | err = -EIO; |
| 450 | goto out; |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | err = patt_count; |
Artem Bityutskiy | 8c1e6ee | 2008-07-18 12:20:23 +0300 | [diff] [blame] | 455 | ubi_msg("PEB %d passed torture test, do not mark it a bad", pnum); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 456 | |
| 457 | out: |
Artem Bityutskiy | e88d6e10 | 2007-08-29 14:51:52 +0300 | [diff] [blame] | 458 | mutex_unlock(&ubi->buf_mutex); |
Artem Bityutskiy | 8d2d401 | 2007-07-22 22:32:51 +0300 | [diff] [blame] | 459 | if (err == UBI_IO_BITFLIPS || err == -EBADMSG) { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 460 | /* |
| 461 | * If a bit-flip or data integrity error was detected, the test |
| 462 | * has not passed because it happened on a freshly erased |
| 463 | * physical eraseblock which means something is wrong with it. |
| 464 | */ |
Artem Bityutskiy | 8d2d401 | 2007-07-22 22:32:51 +0300 | [diff] [blame] | 465 | ubi_err("read problems on freshly erased PEB %d, must be bad", |
| 466 | pnum); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 467 | err = -EIO; |
Artem Bityutskiy | 8d2d401 | 2007-07-22 22:32:51 +0300 | [diff] [blame] | 468 | } |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 469 | return err; |
| 470 | } |
| 471 | |
| 472 | /** |
Artem Bityutskiy | ebf53f4 | 2009-07-06 08:57:53 +0300 | [diff] [blame] | 473 | * nor_erase_prepare - prepare a NOR flash PEB for erasure. |
| 474 | * @ubi: UBI device description object |
| 475 | * @pnum: physical eraseblock number to prepare |
| 476 | * |
| 477 | * NOR flash, or at least some of them, have peculiar embedded PEB erasure |
| 478 | * algorithm: the PEB is first filled with zeroes, then it is erased. And |
| 479 | * filling with zeroes starts from the end of the PEB. This was observed with |
| 480 | * Spansion S29GL512N NOR flash. |
| 481 | * |
| 482 | * This means that in case of a power cut we may end up with intact data at the |
| 483 | * beginning of the PEB, and all zeroes at the end of PEB. In other words, the |
| 484 | * EC and VID headers are OK, but a large chunk of data at the end of PEB is |
| 485 | * zeroed. This makes UBI mistakenly treat this PEB as used and associate it |
| 486 | * with an LEB, which leads to subsequent failures (e.g., UBIFS fails). |
| 487 | * |
| 488 | * This function is called before erasing NOR PEBs and it zeroes out EC and VID |
| 489 | * magic numbers in order to invalidate them and prevent the failures. Returns |
| 490 | * zero in case of success and a negative error code in case of failure. |
| 491 | */ |
| 492 | static int nor_erase_prepare(struct ubi_device *ubi, int pnum) |
| 493 | { |
Artem Bityutskiy | de75c77 | 2009-07-24 16:18:04 +0300 | [diff] [blame] | 494 | int err, err1; |
Artem Bityutskiy | ebf53f4 | 2009-07-06 08:57:53 +0300 | [diff] [blame] | 495 | size_t written; |
| 496 | loff_t addr; |
| 497 | uint32_t data = 0; |
Artem Bityutskiy | de75c77 | 2009-07-24 16:18:04 +0300 | [diff] [blame] | 498 | struct ubi_vid_hdr vid_hdr; |
Artem Bityutskiy | ebf53f4 | 2009-07-06 08:57:53 +0300 | [diff] [blame] | 499 | |
Artem Bityutskiy | 5b289b5 | 2009-07-19 14:09:46 +0300 | [diff] [blame] | 500 | addr = (loff_t)pnum * ubi->peb_size + ubi->vid_hdr_aloffset; |
Artem Bityutskiy | 83c2099 | 2009-07-08 10:15:41 +0300 | [diff] [blame] | 501 | err = ubi->mtd->write(ubi->mtd, addr, 4, &written, (void *)&data); |
Artem Bityutskiy | de75c77 | 2009-07-24 16:18:04 +0300 | [diff] [blame] | 502 | if (!err) { |
| 503 | addr -= ubi->vid_hdr_aloffset; |
| 504 | err = ubi->mtd->write(ubi->mtd, addr, 4, &written, |
| 505 | (void *)&data); |
| 506 | if (!err) |
| 507 | return 0; |
Artem Bityutskiy | ebf53f4 | 2009-07-06 08:57:53 +0300 | [diff] [blame] | 508 | } |
| 509 | |
Artem Bityutskiy | de75c77 | 2009-07-24 16:18:04 +0300 | [diff] [blame] | 510 | /* |
| 511 | * We failed to write to the media. This was observed with Spansion |
| 512 | * S29GL512N NOR flash. Most probably the eraseblock erasure was |
| 513 | * interrupted at a very inappropriate moment, so it became unwritable. |
| 514 | * In this case we probably anyway have garbage in this PEB. |
| 515 | */ |
| 516 | err1 = ubi_io_read_vid_hdr(ubi, pnum, &vid_hdr, 0); |
| 517 | if (err1 == UBI_IO_BAD_VID_HDR) |
| 518 | /* |
| 519 | * The VID header is corrupted, so we can safely erase this |
| 520 | * PEB and not afraid that it will be treated as a valid PEB in |
| 521 | * case of an unclean reboot. |
| 522 | */ |
| 523 | return 0; |
Artem Bityutskiy | 5b289b5 | 2009-07-19 14:09:46 +0300 | [diff] [blame] | 524 | |
Artem Bityutskiy | de75c77 | 2009-07-24 16:18:04 +0300 | [diff] [blame] | 525 | /* |
| 526 | * The PEB contains a valid VID header, but we cannot invalidate it. |
| 527 | * Supposedly the flash media or the driver is screwed up, so return an |
| 528 | * error. |
| 529 | */ |
| 530 | ubi_err("cannot invalidate PEB %d, write returned %d read returned %d", |
| 531 | pnum, err, err1); |
| 532 | ubi_dbg_dump_flash(ubi, pnum, 0, ubi->peb_size); |
| 533 | return -EIO; |
Artem Bityutskiy | ebf53f4 | 2009-07-06 08:57:53 +0300 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | /** |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 537 | * ubi_io_sync_erase - synchronously erase a physical eraseblock. |
| 538 | * @ubi: UBI device description object |
| 539 | * @pnum: physical eraseblock number to erase |
| 540 | * @torture: if this physical eraseblock has to be tortured |
| 541 | * |
| 542 | * This function synchronously erases physical eraseblock @pnum. If @torture |
| 543 | * flag is not zero, the physical eraseblock is checked by means of writing |
| 544 | * different patterns to it and reading them back. If the torturing is enabled, |
Frederik Schwarzer | 025dfda | 2008-10-16 19:02:37 +0200 | [diff] [blame] | 545 | * the physical eraseblock is erased more than once. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 546 | * |
| 547 | * This function returns the number of erasures made in case of success, %-EIO |
| 548 | * if the erasure failed or the torturing test failed, and other negative error |
| 549 | * codes in case of other errors. Note, %-EIO means that the physical |
| 550 | * eraseblock is bad. |
| 551 | */ |
Artem Bityutskiy | e88d6e10 | 2007-08-29 14:51:52 +0300 | [diff] [blame] | 552 | int ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 553 | { |
| 554 | int err, ret = 0; |
| 555 | |
| 556 | ubi_assert(pnum >= 0 && pnum < ubi->peb_count); |
| 557 | |
| 558 | err = paranoid_check_not_bad(ubi, pnum); |
| 559 | if (err != 0) |
Artem Bityutskiy | adbf05e | 2010-01-20 10:28:58 +0200 | [diff] [blame] | 560 | return err; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 561 | |
| 562 | if (ubi->ro_mode) { |
| 563 | ubi_err("read-only mode"); |
| 564 | return -EROFS; |
| 565 | } |
| 566 | |
Artem Bityutskiy | ebf53f4 | 2009-07-06 08:57:53 +0300 | [diff] [blame] | 567 | if (ubi->nor_flash) { |
| 568 | err = nor_erase_prepare(ubi, pnum); |
| 569 | if (err) |
| 570 | return err; |
| 571 | } |
| 572 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 573 | if (torture) { |
| 574 | ret = torture_peb(ubi, pnum); |
| 575 | if (ret < 0) |
| 576 | return ret; |
| 577 | } |
| 578 | |
| 579 | err = do_sync_erase(ubi, pnum); |
| 580 | if (err) |
| 581 | return err; |
| 582 | |
| 583 | return ret + 1; |
| 584 | } |
| 585 | |
| 586 | /** |
| 587 | * ubi_io_is_bad - check if a physical eraseblock is bad. |
| 588 | * @ubi: UBI device description object |
| 589 | * @pnum: the physical eraseblock number to check |
| 590 | * |
| 591 | * This function returns a positive number if the physical eraseblock is bad, |
| 592 | * zero if not, and a negative error code if an error occurred. |
| 593 | */ |
| 594 | int ubi_io_is_bad(const struct ubi_device *ubi, int pnum) |
| 595 | { |
| 596 | struct mtd_info *mtd = ubi->mtd; |
| 597 | |
| 598 | ubi_assert(pnum >= 0 && pnum < ubi->peb_count); |
| 599 | |
| 600 | if (ubi->bad_allowed) { |
| 601 | int ret; |
| 602 | |
| 603 | ret = mtd->block_isbad(mtd, (loff_t)pnum * ubi->peb_size); |
| 604 | if (ret < 0) |
| 605 | ubi_err("error %d while checking if PEB %d is bad", |
| 606 | ret, pnum); |
| 607 | else if (ret) |
| 608 | dbg_io("PEB %d is bad", pnum); |
| 609 | return ret; |
| 610 | } |
| 611 | |
| 612 | return 0; |
| 613 | } |
| 614 | |
| 615 | /** |
| 616 | * ubi_io_mark_bad - mark a physical eraseblock as bad. |
| 617 | * @ubi: UBI device description object |
| 618 | * @pnum: the physical eraseblock number to mark |
| 619 | * |
| 620 | * This function returns zero in case of success and a negative error code in |
| 621 | * case of failure. |
| 622 | */ |
| 623 | int ubi_io_mark_bad(const struct ubi_device *ubi, int pnum) |
| 624 | { |
| 625 | int err; |
| 626 | struct mtd_info *mtd = ubi->mtd; |
| 627 | |
| 628 | ubi_assert(pnum >= 0 && pnum < ubi->peb_count); |
| 629 | |
| 630 | if (ubi->ro_mode) { |
| 631 | ubi_err("read-only mode"); |
| 632 | return -EROFS; |
| 633 | } |
| 634 | |
| 635 | if (!ubi->bad_allowed) |
| 636 | return 0; |
| 637 | |
| 638 | err = mtd->block_markbad(mtd, (loff_t)pnum * ubi->peb_size); |
| 639 | if (err) |
| 640 | ubi_err("cannot mark PEB %d bad, error %d", pnum, err); |
| 641 | return err; |
| 642 | } |
| 643 | |
| 644 | /** |
| 645 | * validate_ec_hdr - validate an erase counter header. |
| 646 | * @ubi: UBI device description object |
| 647 | * @ec_hdr: the erase counter header to check |
| 648 | * |
| 649 | * This function returns zero if the erase counter header is OK, and %1 if |
| 650 | * not. |
| 651 | */ |
Artem Bityutskiy | fe96efc | 2009-06-30 16:11:59 +0300 | [diff] [blame] | 652 | static int validate_ec_hdr(const struct ubi_device *ubi, |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 653 | const struct ubi_ec_hdr *ec_hdr) |
| 654 | { |
| 655 | long long ec; |
Artem Bityutskiy | fe96efc | 2009-06-30 16:11:59 +0300 | [diff] [blame] | 656 | int vid_hdr_offset, leb_start; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 657 | |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 658 | ec = be64_to_cpu(ec_hdr->ec); |
| 659 | vid_hdr_offset = be32_to_cpu(ec_hdr->vid_hdr_offset); |
| 660 | leb_start = be32_to_cpu(ec_hdr->data_offset); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 661 | |
| 662 | if (ec_hdr->version != UBI_VERSION) { |
| 663 | ubi_err("node with incompatible UBI version found: " |
| 664 | "this UBI version is %d, image version is %d", |
| 665 | UBI_VERSION, (int)ec_hdr->version); |
| 666 | goto bad; |
| 667 | } |
| 668 | |
| 669 | if (vid_hdr_offset != ubi->vid_hdr_offset) { |
| 670 | ubi_err("bad VID header offset %d, expected %d", |
| 671 | vid_hdr_offset, ubi->vid_hdr_offset); |
| 672 | goto bad; |
| 673 | } |
| 674 | |
| 675 | if (leb_start != ubi->leb_start) { |
| 676 | ubi_err("bad data offset %d, expected %d", |
| 677 | leb_start, ubi->leb_start); |
| 678 | goto bad; |
| 679 | } |
| 680 | |
| 681 | if (ec < 0 || ec > UBI_MAX_ERASECOUNTER) { |
| 682 | ubi_err("bad erase counter %lld", ec); |
| 683 | goto bad; |
| 684 | } |
| 685 | |
| 686 | return 0; |
| 687 | |
| 688 | bad: |
| 689 | ubi_err("bad EC header"); |
| 690 | ubi_dbg_dump_ec_hdr(ec_hdr); |
| 691 | ubi_dbg_dump_stack(); |
| 692 | return 1; |
| 693 | } |
| 694 | |
| 695 | /** |
| 696 | * ubi_io_read_ec_hdr - read and check an erase counter header. |
| 697 | * @ubi: UBI device description object |
| 698 | * @pnum: physical eraseblock to read from |
| 699 | * @ec_hdr: a &struct ubi_ec_hdr object where to store the read erase counter |
| 700 | * header |
| 701 | * @verbose: be verbose if the header is corrupted or was not found |
| 702 | * |
| 703 | * This function reads erase counter header from physical eraseblock @pnum and |
| 704 | * stores it in @ec_hdr. This function also checks CRC checksum of the read |
| 705 | * erase counter header. The following codes may be returned: |
| 706 | * |
| 707 | * o %0 if the CRC checksum is correct and the header was successfully read; |
| 708 | * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected |
| 709 | * and corrected by the flash driver; this is harmless but may indicate that |
| 710 | * this eraseblock may become bad soon (but may be not); |
| 711 | * o %UBI_IO_BAD_EC_HDR if the erase counter header is corrupted (a CRC error); |
| 712 | * o %UBI_IO_PEB_EMPTY if the physical eraseblock is empty; |
| 713 | * o a negative error code in case of failure. |
| 714 | */ |
Artem Bityutskiy | e88d6e10 | 2007-08-29 14:51:52 +0300 | [diff] [blame] | 715 | int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum, |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 716 | struct ubi_ec_hdr *ec_hdr, int verbose) |
| 717 | { |
| 718 | int err, read_err = 0; |
| 719 | uint32_t crc, magic, hdr_crc; |
| 720 | |
| 721 | dbg_io("read EC header from PEB %d", pnum); |
| 722 | ubi_assert(pnum >= 0 && pnum < ubi->peb_count); |
| 723 | |
| 724 | err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE); |
| 725 | if (err) { |
| 726 | if (err != UBI_IO_BITFLIPS && err != -EBADMSG) |
| 727 | return err; |
| 728 | |
| 729 | /* |
| 730 | * We read all the data, but either a correctable bit-flip |
| 731 | * occurred, or MTD reported about some data integrity error, |
| 732 | * like an ECC error in case of NAND. The former is harmless, |
| 733 | * the later may mean that the read data is corrupted. But we |
| 734 | * have a CRC check-sum and we will detect this. If the EC |
| 735 | * header is still OK, we just report this as there was a |
| 736 | * bit-flip. |
| 737 | */ |
| 738 | read_err = err; |
| 739 | } |
| 740 | |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 741 | magic = be32_to_cpu(ec_hdr->magic); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 742 | if (magic != UBI_EC_HDR_MAGIC) { |
| 743 | /* |
| 744 | * The magic field is wrong. Let's check if we have read all |
| 745 | * 0xFF. If yes, this physical eraseblock is assumed to be |
| 746 | * empty. |
| 747 | * |
| 748 | * But if there was a read error, we do not test it for all |
| 749 | * 0xFFs. Even if it does contain all 0xFFs, this error |
| 750 | * indicates that something is still wrong with this physical |
| 751 | * eraseblock and we anyway cannot treat it as empty. |
| 752 | */ |
| 753 | if (read_err != -EBADMSG && |
| 754 | check_pattern(ec_hdr, 0xFF, UBI_EC_HDR_SIZE)) { |
| 755 | /* The physical eraseblock is supposedly empty */ |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 756 | if (verbose) |
| 757 | ubi_warn("no EC header found at PEB %d, " |
| 758 | "only 0xFF bytes", pnum); |
Artem Bityutskiy | ed45819 | 2008-11-12 10:14:10 +0200 | [diff] [blame] | 759 | else if (UBI_IO_DEBUG) |
| 760 | dbg_msg("no EC header found at PEB %d, " |
| 761 | "only 0xFF bytes", pnum); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 762 | return UBI_IO_PEB_EMPTY; |
| 763 | } |
| 764 | |
| 765 | /* |
| 766 | * This is not a valid erase counter header, and these are not |
| 767 | * 0xFF bytes. Report that the header is corrupted. |
| 768 | */ |
| 769 | if (verbose) { |
| 770 | ubi_warn("bad magic number at PEB %d: %08x instead of " |
| 771 | "%08x", pnum, magic, UBI_EC_HDR_MAGIC); |
| 772 | ubi_dbg_dump_ec_hdr(ec_hdr); |
Artem Bityutskiy | ed45819 | 2008-11-12 10:14:10 +0200 | [diff] [blame] | 773 | } else if (UBI_IO_DEBUG) |
| 774 | dbg_msg("bad magic number at PEB %d: %08x instead of " |
| 775 | "%08x", pnum, magic, UBI_EC_HDR_MAGIC); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 776 | return UBI_IO_BAD_EC_HDR; |
| 777 | } |
| 778 | |
| 779 | crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC); |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 780 | hdr_crc = be32_to_cpu(ec_hdr->hdr_crc); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 781 | |
| 782 | if (hdr_crc != crc) { |
| 783 | if (verbose) { |
Artem Bityutskiy | 9c9ec14 | 2008-07-18 13:19:52 +0300 | [diff] [blame] | 784 | ubi_warn("bad EC header CRC at PEB %d, calculated " |
| 785 | "%#08x, read %#08x", pnum, crc, hdr_crc); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 786 | ubi_dbg_dump_ec_hdr(ec_hdr); |
Artem Bityutskiy | ed45819 | 2008-11-12 10:14:10 +0200 | [diff] [blame] | 787 | } else if (UBI_IO_DEBUG) |
| 788 | dbg_msg("bad EC header CRC at PEB %d, calculated " |
| 789 | "%#08x, read %#08x", pnum, crc, hdr_crc); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 790 | return UBI_IO_BAD_EC_HDR; |
| 791 | } |
| 792 | |
| 793 | /* And of course validate what has just been read from the media */ |
| 794 | err = validate_ec_hdr(ubi, ec_hdr); |
| 795 | if (err) { |
| 796 | ubi_err("validation failed for PEB %d", pnum); |
| 797 | return -EINVAL; |
| 798 | } |
| 799 | |
| 800 | return read_err ? UBI_IO_BITFLIPS : 0; |
| 801 | } |
| 802 | |
| 803 | /** |
| 804 | * ubi_io_write_ec_hdr - write an erase counter header. |
| 805 | * @ubi: UBI device description object |
| 806 | * @pnum: physical eraseblock to write to |
| 807 | * @ec_hdr: the erase counter header to write |
| 808 | * |
| 809 | * This function writes erase counter header described by @ec_hdr to physical |
| 810 | * eraseblock @pnum. It also fills most fields of @ec_hdr before writing, so |
| 811 | * the caller do not have to fill them. Callers must only fill the @ec_hdr->ec |
| 812 | * field. |
| 813 | * |
| 814 | * This function returns zero in case of success and a negative error code in |
| 815 | * case of failure. If %-EIO is returned, the physical eraseblock most probably |
| 816 | * went bad. |
| 817 | */ |
Artem Bityutskiy | e88d6e10 | 2007-08-29 14:51:52 +0300 | [diff] [blame] | 818 | int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum, |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 819 | struct ubi_ec_hdr *ec_hdr) |
| 820 | { |
| 821 | int err; |
| 822 | uint32_t crc; |
| 823 | |
| 824 | dbg_io("write EC header to PEB %d", pnum); |
| 825 | ubi_assert(pnum >= 0 && pnum < ubi->peb_count); |
| 826 | |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 827 | ec_hdr->magic = cpu_to_be32(UBI_EC_HDR_MAGIC); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 828 | ec_hdr->version = UBI_VERSION; |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 829 | ec_hdr->vid_hdr_offset = cpu_to_be32(ubi->vid_hdr_offset); |
| 830 | ec_hdr->data_offset = cpu_to_be32(ubi->leb_start); |
Adrian Hunter | 0c6c7fa | 2009-06-26 14:58:01 +0300 | [diff] [blame] | 831 | ec_hdr->image_seq = cpu_to_be32(ubi->image_seq); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 832 | crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC); |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 833 | ec_hdr->hdr_crc = cpu_to_be32(crc); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 834 | |
| 835 | err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr); |
| 836 | if (err) |
Artem Bityutskiy | adbf05e | 2010-01-20 10:28:58 +0200 | [diff] [blame] | 837 | return err; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 838 | |
| 839 | err = ubi_io_write(ubi, ec_hdr, pnum, 0, ubi->ec_hdr_alsize); |
| 840 | return err; |
| 841 | } |
| 842 | |
| 843 | /** |
| 844 | * validate_vid_hdr - validate a volume identifier header. |
| 845 | * @ubi: UBI device description object |
| 846 | * @vid_hdr: the volume identifier header to check |
| 847 | * |
| 848 | * This function checks that data stored in the volume identifier header |
| 849 | * @vid_hdr. Returns zero if the VID header is OK and %1 if not. |
| 850 | */ |
| 851 | static int validate_vid_hdr(const struct ubi_device *ubi, |
| 852 | const struct ubi_vid_hdr *vid_hdr) |
| 853 | { |
| 854 | int vol_type = vid_hdr->vol_type; |
| 855 | int copy_flag = vid_hdr->copy_flag; |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 856 | int vol_id = be32_to_cpu(vid_hdr->vol_id); |
| 857 | int lnum = be32_to_cpu(vid_hdr->lnum); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 858 | int compat = vid_hdr->compat; |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 859 | int data_size = be32_to_cpu(vid_hdr->data_size); |
| 860 | int used_ebs = be32_to_cpu(vid_hdr->used_ebs); |
| 861 | int data_pad = be32_to_cpu(vid_hdr->data_pad); |
| 862 | int data_crc = be32_to_cpu(vid_hdr->data_crc); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 863 | int usable_leb_size = ubi->leb_size - data_pad; |
| 864 | |
| 865 | if (copy_flag != 0 && copy_flag != 1) { |
| 866 | dbg_err("bad copy_flag"); |
| 867 | goto bad; |
| 868 | } |
| 869 | |
| 870 | if (vol_id < 0 || lnum < 0 || data_size < 0 || used_ebs < 0 || |
| 871 | data_pad < 0) { |
| 872 | dbg_err("negative values"); |
| 873 | goto bad; |
| 874 | } |
| 875 | |
| 876 | if (vol_id >= UBI_MAX_VOLUMES && vol_id < UBI_INTERNAL_VOL_START) { |
| 877 | dbg_err("bad vol_id"); |
| 878 | goto bad; |
| 879 | } |
| 880 | |
| 881 | if (vol_id < UBI_INTERNAL_VOL_START && compat != 0) { |
| 882 | dbg_err("bad compat"); |
| 883 | goto bad; |
| 884 | } |
| 885 | |
| 886 | if (vol_id >= UBI_INTERNAL_VOL_START && compat != UBI_COMPAT_DELETE && |
| 887 | compat != UBI_COMPAT_RO && compat != UBI_COMPAT_PRESERVE && |
| 888 | compat != UBI_COMPAT_REJECT) { |
| 889 | dbg_err("bad compat"); |
| 890 | goto bad; |
| 891 | } |
| 892 | |
| 893 | if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) { |
| 894 | dbg_err("bad vol_type"); |
| 895 | goto bad; |
| 896 | } |
| 897 | |
| 898 | if (data_pad >= ubi->leb_size / 2) { |
| 899 | dbg_err("bad data_pad"); |
| 900 | goto bad; |
| 901 | } |
| 902 | |
| 903 | if (vol_type == UBI_VID_STATIC) { |
| 904 | /* |
| 905 | * Although from high-level point of view static volumes may |
| 906 | * contain zero bytes of data, but no VID headers can contain |
| 907 | * zero at these fields, because they empty volumes do not have |
| 908 | * mapped logical eraseblocks. |
| 909 | */ |
| 910 | if (used_ebs == 0) { |
| 911 | dbg_err("zero used_ebs"); |
| 912 | goto bad; |
| 913 | } |
| 914 | if (data_size == 0) { |
| 915 | dbg_err("zero data_size"); |
| 916 | goto bad; |
| 917 | } |
| 918 | if (lnum < used_ebs - 1) { |
| 919 | if (data_size != usable_leb_size) { |
| 920 | dbg_err("bad data_size"); |
| 921 | goto bad; |
| 922 | } |
| 923 | } else if (lnum == used_ebs - 1) { |
| 924 | if (data_size == 0) { |
| 925 | dbg_err("bad data_size at last LEB"); |
| 926 | goto bad; |
| 927 | } |
| 928 | } else { |
| 929 | dbg_err("too high lnum"); |
| 930 | goto bad; |
| 931 | } |
| 932 | } else { |
| 933 | if (copy_flag == 0) { |
| 934 | if (data_crc != 0) { |
| 935 | dbg_err("non-zero data CRC"); |
| 936 | goto bad; |
| 937 | } |
| 938 | if (data_size != 0) { |
| 939 | dbg_err("non-zero data_size"); |
| 940 | goto bad; |
| 941 | } |
| 942 | } else { |
| 943 | if (data_size == 0) { |
| 944 | dbg_err("zero data_size of copy"); |
| 945 | goto bad; |
| 946 | } |
| 947 | } |
| 948 | if (used_ebs != 0) { |
| 949 | dbg_err("bad used_ebs"); |
| 950 | goto bad; |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | return 0; |
| 955 | |
| 956 | bad: |
| 957 | ubi_err("bad VID header"); |
| 958 | ubi_dbg_dump_vid_hdr(vid_hdr); |
| 959 | ubi_dbg_dump_stack(); |
| 960 | return 1; |
| 961 | } |
| 962 | |
| 963 | /** |
| 964 | * ubi_io_read_vid_hdr - read and check a volume identifier header. |
| 965 | * @ubi: UBI device description object |
| 966 | * @pnum: physical eraseblock number to read from |
| 967 | * @vid_hdr: &struct ubi_vid_hdr object where to store the read volume |
| 968 | * identifier header |
| 969 | * @verbose: be verbose if the header is corrupted or wasn't found |
| 970 | * |
| 971 | * This function reads the volume identifier header from physical eraseblock |
| 972 | * @pnum and stores it in @vid_hdr. It also checks CRC checksum of the read |
| 973 | * volume identifier header. The following codes may be returned: |
| 974 | * |
| 975 | * o %0 if the CRC checksum is correct and the header was successfully read; |
| 976 | * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected |
| 977 | * and corrected by the flash driver; this is harmless but may indicate that |
| 978 | * this eraseblock may become bad soon; |
Artem Bityutskiy | 815bc5f | 2009-06-08 19:28:18 +0300 | [diff] [blame] | 979 | * o %UBI_IO_BAD_VID_HDR if the volume identifier header is corrupted (a CRC |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 980 | * error detected); |
| 981 | * o %UBI_IO_PEB_FREE if the physical eraseblock is free (i.e., there is no VID |
| 982 | * header there); |
| 983 | * o a negative error code in case of failure. |
| 984 | */ |
Artem Bityutskiy | e88d6e10 | 2007-08-29 14:51:52 +0300 | [diff] [blame] | 985 | int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum, |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 986 | struct ubi_vid_hdr *vid_hdr, int verbose) |
| 987 | { |
| 988 | int err, read_err = 0; |
| 989 | uint32_t crc, magic, hdr_crc; |
| 990 | void *p; |
| 991 | |
| 992 | dbg_io("read VID header from PEB %d", pnum); |
| 993 | ubi_assert(pnum >= 0 && pnum < ubi->peb_count); |
| 994 | |
| 995 | p = (char *)vid_hdr - ubi->vid_hdr_shift; |
| 996 | err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset, |
| 997 | ubi->vid_hdr_alsize); |
| 998 | if (err) { |
| 999 | if (err != UBI_IO_BITFLIPS && err != -EBADMSG) |
| 1000 | return err; |
| 1001 | |
| 1002 | /* |
| 1003 | * We read all the data, but either a correctable bit-flip |
| 1004 | * occurred, or MTD reported about some data integrity error, |
| 1005 | * like an ECC error in case of NAND. The former is harmless, |
| 1006 | * the later may mean the read data is corrupted. But we have a |
| 1007 | * CRC check-sum and we will identify this. If the VID header is |
| 1008 | * still OK, we just report this as there was a bit-flip. |
| 1009 | */ |
| 1010 | read_err = err; |
| 1011 | } |
| 1012 | |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 1013 | magic = be32_to_cpu(vid_hdr->magic); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1014 | if (magic != UBI_VID_HDR_MAGIC) { |
| 1015 | /* |
| 1016 | * If we have read all 0xFF bytes, the VID header probably does |
| 1017 | * not exist and the physical eraseblock is assumed to be free. |
| 1018 | * |
| 1019 | * But if there was a read error, we do not test the data for |
| 1020 | * 0xFFs. Even if it does contain all 0xFFs, this error |
| 1021 | * indicates that something is still wrong with this physical |
| 1022 | * eraseblock and it cannot be regarded as free. |
| 1023 | */ |
| 1024 | if (read_err != -EBADMSG && |
| 1025 | check_pattern(vid_hdr, 0xFF, UBI_VID_HDR_SIZE)) { |
| 1026 | /* The physical eraseblock is supposedly free */ |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1027 | if (verbose) |
| 1028 | ubi_warn("no VID header found at PEB %d, " |
| 1029 | "only 0xFF bytes", pnum); |
Artem Bityutskiy | ed45819 | 2008-11-12 10:14:10 +0200 | [diff] [blame] | 1030 | else if (UBI_IO_DEBUG) |
| 1031 | dbg_msg("no VID header found at PEB %d, " |
| 1032 | "only 0xFF bytes", pnum); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1033 | return UBI_IO_PEB_FREE; |
| 1034 | } |
| 1035 | |
| 1036 | /* |
| 1037 | * This is not a valid VID header, and these are not 0xFF |
| 1038 | * bytes. Report that the header is corrupted. |
| 1039 | */ |
| 1040 | if (verbose) { |
| 1041 | ubi_warn("bad magic number at PEB %d: %08x instead of " |
| 1042 | "%08x", pnum, magic, UBI_VID_HDR_MAGIC); |
| 1043 | ubi_dbg_dump_vid_hdr(vid_hdr); |
Artem Bityutskiy | ed45819 | 2008-11-12 10:14:10 +0200 | [diff] [blame] | 1044 | } else if (UBI_IO_DEBUG) |
| 1045 | dbg_msg("bad magic number at PEB %d: %08x instead of " |
| 1046 | "%08x", pnum, magic, UBI_VID_HDR_MAGIC); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1047 | return UBI_IO_BAD_VID_HDR; |
| 1048 | } |
| 1049 | |
| 1050 | crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC); |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 1051 | hdr_crc = be32_to_cpu(vid_hdr->hdr_crc); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1052 | |
| 1053 | if (hdr_crc != crc) { |
| 1054 | if (verbose) { |
| 1055 | ubi_warn("bad CRC at PEB %d, calculated %#08x, " |
| 1056 | "read %#08x", pnum, crc, hdr_crc); |
| 1057 | ubi_dbg_dump_vid_hdr(vid_hdr); |
Artem Bityutskiy | ed45819 | 2008-11-12 10:14:10 +0200 | [diff] [blame] | 1058 | } else if (UBI_IO_DEBUG) |
| 1059 | dbg_msg("bad CRC at PEB %d, calculated %#08x, " |
| 1060 | "read %#08x", pnum, crc, hdr_crc); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1061 | return UBI_IO_BAD_VID_HDR; |
| 1062 | } |
| 1063 | |
| 1064 | /* Validate the VID header that we have just read */ |
| 1065 | err = validate_vid_hdr(ubi, vid_hdr); |
| 1066 | if (err) { |
| 1067 | ubi_err("validation failed for PEB %d", pnum); |
| 1068 | return -EINVAL; |
| 1069 | } |
| 1070 | |
| 1071 | return read_err ? UBI_IO_BITFLIPS : 0; |
| 1072 | } |
| 1073 | |
| 1074 | /** |
| 1075 | * ubi_io_write_vid_hdr - write a volume identifier header. |
| 1076 | * @ubi: UBI device description object |
| 1077 | * @pnum: the physical eraseblock number to write to |
| 1078 | * @vid_hdr: the volume identifier header to write |
| 1079 | * |
| 1080 | * This function writes the volume identifier header described by @vid_hdr to |
| 1081 | * physical eraseblock @pnum. This function automatically fills the |
| 1082 | * @vid_hdr->magic and the @vid_hdr->version fields, as well as calculates |
| 1083 | * header CRC checksum and stores it at vid_hdr->hdr_crc. |
| 1084 | * |
| 1085 | * This function returns zero in case of success and a negative error code in |
| 1086 | * case of failure. If %-EIO is returned, the physical eraseblock probably went |
| 1087 | * bad. |
| 1088 | */ |
Artem Bityutskiy | e88d6e10 | 2007-08-29 14:51:52 +0300 | [diff] [blame] | 1089 | int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum, |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1090 | struct ubi_vid_hdr *vid_hdr) |
| 1091 | { |
| 1092 | int err; |
| 1093 | uint32_t crc; |
| 1094 | void *p; |
| 1095 | |
| 1096 | dbg_io("write VID header to PEB %d", pnum); |
| 1097 | ubi_assert(pnum >= 0 && pnum < ubi->peb_count); |
| 1098 | |
| 1099 | err = paranoid_check_peb_ec_hdr(ubi, pnum); |
| 1100 | if (err) |
Artem Bityutskiy | adbf05e | 2010-01-20 10:28:58 +0200 | [diff] [blame] | 1101 | return err; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1102 | |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 1103 | vid_hdr->magic = cpu_to_be32(UBI_VID_HDR_MAGIC); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1104 | vid_hdr->version = UBI_VERSION; |
| 1105 | crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC); |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 1106 | vid_hdr->hdr_crc = cpu_to_be32(crc); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1107 | |
| 1108 | err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr); |
| 1109 | if (err) |
Artem Bityutskiy | adbf05e | 2010-01-20 10:28:58 +0200 | [diff] [blame] | 1110 | return err; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1111 | |
| 1112 | p = (char *)vid_hdr - ubi->vid_hdr_shift; |
| 1113 | err = ubi_io_write(ubi, p, pnum, ubi->vid_hdr_aloffset, |
| 1114 | ubi->vid_hdr_alsize); |
| 1115 | return err; |
| 1116 | } |
| 1117 | |
| 1118 | #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID |
| 1119 | |
| 1120 | /** |
| 1121 | * paranoid_check_not_bad - ensure that a physical eraseblock is not bad. |
| 1122 | * @ubi: UBI device description object |
| 1123 | * @pnum: physical eraseblock number to check |
| 1124 | * |
Artem Bityutskiy | adbf05e | 2010-01-20 10:28:58 +0200 | [diff] [blame] | 1125 | * This function returns zero if the physical eraseblock is good, %-EINVAL if |
| 1126 | * it is bad and a negative error code if an error occurred. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1127 | */ |
| 1128 | static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum) |
| 1129 | { |
| 1130 | int err; |
| 1131 | |
| 1132 | err = ubi_io_is_bad(ubi, pnum); |
| 1133 | if (!err) |
| 1134 | return err; |
| 1135 | |
| 1136 | ubi_err("paranoid check failed for PEB %d", pnum); |
| 1137 | ubi_dbg_dump_stack(); |
Artem Bityutskiy | adbf05e | 2010-01-20 10:28:58 +0200 | [diff] [blame] | 1138 | return err > 0 ? -EINVAL : err; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | /** |
| 1142 | * paranoid_check_ec_hdr - check if an erase counter header is all right. |
| 1143 | * @ubi: UBI device description object |
| 1144 | * @pnum: physical eraseblock number the erase counter header belongs to |
| 1145 | * @ec_hdr: the erase counter header to check |
| 1146 | * |
| 1147 | * This function returns zero if the erase counter header contains valid |
Artem Bityutskiy | adbf05e | 2010-01-20 10:28:58 +0200 | [diff] [blame] | 1148 | * values, and %-EINVAL if not. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1149 | */ |
| 1150 | static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum, |
| 1151 | const struct ubi_ec_hdr *ec_hdr) |
| 1152 | { |
| 1153 | int err; |
| 1154 | uint32_t magic; |
| 1155 | |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 1156 | magic = be32_to_cpu(ec_hdr->magic); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1157 | if (magic != UBI_EC_HDR_MAGIC) { |
| 1158 | ubi_err("bad magic %#08x, must be %#08x", |
| 1159 | magic, UBI_EC_HDR_MAGIC); |
| 1160 | goto fail; |
| 1161 | } |
| 1162 | |
| 1163 | err = validate_ec_hdr(ubi, ec_hdr); |
| 1164 | if (err) { |
| 1165 | ubi_err("paranoid check failed for PEB %d", pnum); |
| 1166 | goto fail; |
| 1167 | } |
| 1168 | |
| 1169 | return 0; |
| 1170 | |
| 1171 | fail: |
| 1172 | ubi_dbg_dump_ec_hdr(ec_hdr); |
| 1173 | ubi_dbg_dump_stack(); |
Artem Bityutskiy | adbf05e | 2010-01-20 10:28:58 +0200 | [diff] [blame] | 1174 | return -EINVAL; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | /** |
Artem Bityutskiy | ebaaf1a | 2008-07-18 13:34:32 +0300 | [diff] [blame] | 1178 | * paranoid_check_peb_ec_hdr - check erase counter header. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1179 | * @ubi: UBI device description object |
| 1180 | * @pnum: the physical eraseblock number to check |
| 1181 | * |
Artem Bityutskiy | adbf05e | 2010-01-20 10:28:58 +0200 | [diff] [blame] | 1182 | * This function returns zero if the erase counter header is all right and and |
| 1183 | * a negative error code if not or if an error occurred. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1184 | */ |
| 1185 | static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum) |
| 1186 | { |
| 1187 | int err; |
| 1188 | uint32_t crc, hdr_crc; |
| 1189 | struct ubi_ec_hdr *ec_hdr; |
| 1190 | |
Artem Bityutskiy | 33818bb | 2007-08-28 21:29:32 +0300 | [diff] [blame] | 1191 | ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1192 | if (!ec_hdr) |
| 1193 | return -ENOMEM; |
| 1194 | |
| 1195 | err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE); |
| 1196 | if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG) |
| 1197 | goto exit; |
| 1198 | |
| 1199 | crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC); |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 1200 | hdr_crc = be32_to_cpu(ec_hdr->hdr_crc); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1201 | if (hdr_crc != crc) { |
| 1202 | ubi_err("bad CRC, calculated %#08x, read %#08x", crc, hdr_crc); |
| 1203 | ubi_err("paranoid check failed for PEB %d", pnum); |
| 1204 | ubi_dbg_dump_ec_hdr(ec_hdr); |
| 1205 | ubi_dbg_dump_stack(); |
Artem Bityutskiy | adbf05e | 2010-01-20 10:28:58 +0200 | [diff] [blame] | 1206 | err = -EINVAL; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1207 | goto exit; |
| 1208 | } |
| 1209 | |
| 1210 | err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr); |
| 1211 | |
| 1212 | exit: |
| 1213 | kfree(ec_hdr); |
| 1214 | return err; |
| 1215 | } |
| 1216 | |
| 1217 | /** |
| 1218 | * paranoid_check_vid_hdr - check that a volume identifier header is all right. |
| 1219 | * @ubi: UBI device description object |
| 1220 | * @pnum: physical eraseblock number the volume identifier header belongs to |
| 1221 | * @vid_hdr: the volume identifier header to check |
| 1222 | * |
| 1223 | * This function returns zero if the volume identifier header is all right, and |
Artem Bityutskiy | adbf05e | 2010-01-20 10:28:58 +0200 | [diff] [blame] | 1224 | * %-EINVAL if not. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1225 | */ |
| 1226 | static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum, |
| 1227 | const struct ubi_vid_hdr *vid_hdr) |
| 1228 | { |
| 1229 | int err; |
| 1230 | uint32_t magic; |
| 1231 | |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 1232 | magic = be32_to_cpu(vid_hdr->magic); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1233 | if (magic != UBI_VID_HDR_MAGIC) { |
| 1234 | ubi_err("bad VID header magic %#08x at PEB %d, must be %#08x", |
| 1235 | magic, pnum, UBI_VID_HDR_MAGIC); |
| 1236 | goto fail; |
| 1237 | } |
| 1238 | |
| 1239 | err = validate_vid_hdr(ubi, vid_hdr); |
| 1240 | if (err) { |
| 1241 | ubi_err("paranoid check failed for PEB %d", pnum); |
| 1242 | goto fail; |
| 1243 | } |
| 1244 | |
| 1245 | return err; |
| 1246 | |
| 1247 | fail: |
| 1248 | ubi_err("paranoid check failed for PEB %d", pnum); |
| 1249 | ubi_dbg_dump_vid_hdr(vid_hdr); |
| 1250 | ubi_dbg_dump_stack(); |
Artem Bityutskiy | adbf05e | 2010-01-20 10:28:58 +0200 | [diff] [blame] | 1251 | return -EINVAL; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1252 | |
| 1253 | } |
| 1254 | |
| 1255 | /** |
Artem Bityutskiy | ebaaf1a | 2008-07-18 13:34:32 +0300 | [diff] [blame] | 1256 | * paranoid_check_peb_vid_hdr - check volume identifier header. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1257 | * @ubi: UBI device description object |
| 1258 | * @pnum: the physical eraseblock number to check |
| 1259 | * |
| 1260 | * This function returns zero if the volume identifier header is all right, |
Artem Bityutskiy | adbf05e | 2010-01-20 10:28:58 +0200 | [diff] [blame] | 1261 | * and a negative error code if not or if an error occurred. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1262 | */ |
| 1263 | static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum) |
| 1264 | { |
| 1265 | int err; |
| 1266 | uint32_t crc, hdr_crc; |
| 1267 | struct ubi_vid_hdr *vid_hdr; |
| 1268 | void *p; |
| 1269 | |
Artem Bityutskiy | 33818bb | 2007-08-28 21:29:32 +0300 | [diff] [blame] | 1270 | vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1271 | if (!vid_hdr) |
| 1272 | return -ENOMEM; |
| 1273 | |
| 1274 | p = (char *)vid_hdr - ubi->vid_hdr_shift; |
| 1275 | err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset, |
| 1276 | ubi->vid_hdr_alsize); |
| 1277 | if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG) |
| 1278 | goto exit; |
| 1279 | |
| 1280 | crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_EC_HDR_SIZE_CRC); |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 1281 | hdr_crc = be32_to_cpu(vid_hdr->hdr_crc); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1282 | if (hdr_crc != crc) { |
| 1283 | ubi_err("bad VID header CRC at PEB %d, calculated %#08x, " |
| 1284 | "read %#08x", pnum, crc, hdr_crc); |
| 1285 | ubi_err("paranoid check failed for PEB %d", pnum); |
| 1286 | ubi_dbg_dump_vid_hdr(vid_hdr); |
| 1287 | ubi_dbg_dump_stack(); |
Artem Bityutskiy | adbf05e | 2010-01-20 10:28:58 +0200 | [diff] [blame] | 1288 | err = -EINVAL; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1289 | goto exit; |
| 1290 | } |
| 1291 | |
| 1292 | err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr); |
| 1293 | |
| 1294 | exit: |
| 1295 | ubi_free_vid_hdr(ubi, vid_hdr); |
| 1296 | return err; |
| 1297 | } |
| 1298 | |
| 1299 | /** |
Artem Bityutskiy | 6e9065d | 2010-01-25 17:09:30 +0200 | [diff] [blame] | 1300 | * ubi_dbg_check_write - make sure write succeeded. |
| 1301 | * @ubi: UBI device description object |
| 1302 | * @buf: buffer with data which were written |
| 1303 | * @pnum: physical eraseblock number the data were written to |
| 1304 | * @offset: offset within the physical eraseblock the data were written to |
| 1305 | * @len: how many bytes were written |
| 1306 | * |
| 1307 | * This functions reads data which were recently written and compares it with |
| 1308 | * the original data buffer - the data have to match. Returns zero if the data |
| 1309 | * match and a negative error code if not or in case of failure. |
| 1310 | */ |
| 1311 | int ubi_dbg_check_write(struct ubi_device *ubi, const void *buf, int pnum, |
| 1312 | int offset, int len) |
| 1313 | { |
| 1314 | int err, i; |
| 1315 | |
| 1316 | mutex_lock(&ubi->dbg_buf_mutex); |
| 1317 | err = ubi_io_read(ubi, ubi->dbg_peb_buf, pnum, offset, len); |
| 1318 | if (err) |
| 1319 | goto out_unlock; |
| 1320 | |
| 1321 | for (i = 0; i < len; i++) { |
| 1322 | uint8_t c = ((uint8_t *)buf)[i]; |
| 1323 | uint8_t c1 = ((uint8_t *)ubi->dbg_peb_buf)[i]; |
| 1324 | int dump_len; |
| 1325 | |
| 1326 | if (c == c1) |
| 1327 | continue; |
| 1328 | |
| 1329 | ubi_err("paranoid check failed for PEB %d:%d, len %d", |
| 1330 | pnum, offset, len); |
| 1331 | ubi_msg("data differ at position %d", i); |
| 1332 | dump_len = max_t(int, 128, len - i); |
| 1333 | ubi_msg("hex dump of the original buffer from %d to %d", |
| 1334 | i, i + dump_len); |
| 1335 | print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, |
| 1336 | buf + i, dump_len, 1); |
| 1337 | ubi_msg("hex dump of the read buffer from %d to %d", |
| 1338 | i, i + dump_len); |
| 1339 | print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, |
| 1340 | ubi->dbg_peb_buf + i, dump_len, 1); |
| 1341 | ubi_dbg_dump_stack(); |
| 1342 | err = -EINVAL; |
| 1343 | goto out_unlock; |
| 1344 | } |
| 1345 | mutex_unlock(&ubi->dbg_buf_mutex); |
| 1346 | |
| 1347 | return 0; |
| 1348 | |
| 1349 | out_unlock: |
| 1350 | mutex_unlock(&ubi->dbg_buf_mutex); |
| 1351 | return err; |
| 1352 | } |
| 1353 | |
| 1354 | /** |
Artem Bityutskiy | 40a71a8 | 2009-06-28 19:16:55 +0300 | [diff] [blame] | 1355 | * ubi_dbg_check_all_ff - check that a region of flash is empty. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1356 | * @ubi: UBI device description object |
| 1357 | * @pnum: the physical eraseblock number to check |
| 1358 | * @offset: the starting offset within the physical eraseblock to check |
| 1359 | * @len: the length of the region to check |
| 1360 | * |
| 1361 | * This function returns zero if only 0xFF bytes are present at offset |
Artem Bityutskiy | adbf05e | 2010-01-20 10:28:58 +0200 | [diff] [blame] | 1362 | * @offset of the physical eraseblock @pnum, and a negative error code if not |
| 1363 | * or if an error occurred. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1364 | */ |
Artem Bityutskiy | 40a71a8 | 2009-06-28 19:16:55 +0300 | [diff] [blame] | 1365 | int ubi_dbg_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1366 | { |
| 1367 | size_t read; |
| 1368 | int err; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1369 | loff_t addr = (loff_t)pnum * ubi->peb_size + offset; |
| 1370 | |
Artem Bityutskiy | e88d6e10 | 2007-08-29 14:51:52 +0300 | [diff] [blame] | 1371 | mutex_lock(&ubi->dbg_buf_mutex); |
| 1372 | err = ubi->mtd->read(ubi->mtd, addr, len, &read, ubi->dbg_peb_buf); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1373 | if (err && err != -EUCLEAN) { |
| 1374 | ubi_err("error %d while reading %d bytes from PEB %d:%d, " |
| 1375 | "read %zd bytes", err, len, pnum, offset, read); |
| 1376 | goto error; |
| 1377 | } |
| 1378 | |
Artem Bityutskiy | e88d6e10 | 2007-08-29 14:51:52 +0300 | [diff] [blame] | 1379 | err = check_pattern(ubi->dbg_peb_buf, 0xFF, len); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1380 | if (err == 0) { |
| 1381 | ubi_err("flash region at PEB %d:%d, length %d does not " |
| 1382 | "contain all 0xFF bytes", pnum, offset, len); |
| 1383 | goto fail; |
| 1384 | } |
Artem Bityutskiy | e88d6e10 | 2007-08-29 14:51:52 +0300 | [diff] [blame] | 1385 | mutex_unlock(&ubi->dbg_buf_mutex); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1386 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1387 | return 0; |
| 1388 | |
| 1389 | fail: |
| 1390 | ubi_err("paranoid check failed for PEB %d", pnum); |
Artem Bityutskiy | c856635 | 2008-07-16 17:40:22 +0300 | [diff] [blame] | 1391 | ubi_msg("hex dump of the %d-%d region", offset, offset + len); |
Artem Bityutskiy | 6986646 | 2007-08-29 14:56:20 +0300 | [diff] [blame] | 1392 | print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, |
Artem Bityutskiy | e88d6e10 | 2007-08-29 14:51:52 +0300 | [diff] [blame] | 1393 | ubi->dbg_peb_buf, len, 1); |
Artem Bityutskiy | adbf05e | 2010-01-20 10:28:58 +0200 | [diff] [blame] | 1394 | err = -EINVAL; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1395 | error: |
| 1396 | ubi_dbg_dump_stack(); |
Artem Bityutskiy | e88d6e10 | 2007-08-29 14:51:52 +0300 | [diff] [blame] | 1397 | mutex_unlock(&ubi->dbg_buf_mutex); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1398 | return err; |
| 1399 | } |
| 1400 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1401 | #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */ |