Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) International Business Machines Corp., 2006 |
| 4 | * Copyright (c) Nokia Corporation, 2006, 2007 |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 5 | * |
| 6 | * Author: Artem Bityutskiy (Битюцкий Артём) |
| 7 | */ |
| 8 | |
| 9 | /* |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 10 | * UBI input/output sub-system. |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 11 | * |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 12 | * This sub-system provides a uniform way to work with all kinds of the |
| 13 | * underlying MTD devices. It also implements handy functions for reading and |
| 14 | * writing UBI headers. |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 15 | * |
| 16 | * We are trying to have a paranoid mindset and not to trust to what we read |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 17 | * from the flash media in order to be more secure and robust. So this |
| 18 | * sub-system validates every single header it reads from the flash media. |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 19 | * |
| 20 | * Some words about how the eraseblock headers are stored. |
| 21 | * |
| 22 | * The erase counter header is always stored at offset zero. By default, the |
| 23 | * VID header is stored after the EC header at the closest aligned offset |
| 24 | * (i.e. aligned to the minimum I/O unit size). Data starts next to the VID |
| 25 | * header at the closest aligned offset. But this default layout may be |
| 26 | * changed. For example, for different reasons (e.g., optimization) UBI may be |
| 27 | * asked to put the VID header at further offset, and even at an unaligned |
| 28 | * offset. Of course, if the offset of the VID header is unaligned, UBI adds |
| 29 | * proper padding in front of it. Data offset may also be changed but it has to |
| 30 | * be aligned. |
| 31 | * |
| 32 | * About minimal I/O units. In general, UBI assumes flash device model where |
| 33 | * there is only one minimal I/O unit size. E.g., in case of NOR flash it is 1, |
| 34 | * in case of NAND flash it is a NAND page, etc. This is reported by MTD in the |
| 35 | * @ubi->mtd->writesize field. But as an exception, UBI admits of using another |
| 36 | * (smaller) minimal I/O unit size for EC and VID headers to make it possible |
| 37 | * to do different optimizations. |
| 38 | * |
| 39 | * This is extremely useful in case of NAND flashes which admit of several |
| 40 | * write operations to one NAND page. In this case UBI can fit EC and VID |
| 41 | * headers at one NAND page. Thus, UBI may use "sub-page" size as the minimal |
| 42 | * I/O unit for the headers (the @ubi->hdrs_min_io_size field). But it still |
| 43 | * reports NAND page size (@ubi->min_io_size) as a minimal I/O unit for the UBI |
| 44 | * users. |
| 45 | * |
| 46 | * Example: some Samsung NANDs with 2KiB pages allow 4x 512-byte writes, so |
| 47 | * although the minimal I/O unit is 2K, UBI uses 512 bytes for EC and VID |
| 48 | * headers. |
| 49 | * |
| 50 | * Q: why not just to treat sub-page as a minimal I/O unit of this flash |
| 51 | * device, e.g., make @ubi->min_io_size = 512 in the example above? |
| 52 | * |
| 53 | * A: because when writing a sub-page, MTD still writes a full 2K page but the |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 54 | * bytes which are not relevant to the sub-page are 0xFF. So, basically, |
| 55 | * writing 4x512 sub-pages is 4 times slower than writing one 2KiB NAND page. |
| 56 | * Thus, we prefer to use sub-pages only for EC and VID headers. |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 57 | * |
| 58 | * As it was noted above, the VID header may start at a non-aligned offset. |
| 59 | * For example, in case of a 2KiB page NAND flash with a 512 bytes sub-page, |
| 60 | * the VID header may reside at offset 1984 which is the last 64 bytes of the |
| 61 | * last sub-page (EC header is always at offset zero). This causes some |
| 62 | * difficulties when reading and writing VID headers. |
| 63 | * |
| 64 | * Suppose we have a 64-byte buffer and we read a VID header at it. We change |
| 65 | * the data and want to write this VID header out. As we can only write in |
| 66 | * 512-byte chunks, we have to allocate one more buffer and copy our VID header |
| 67 | * to offset 448 of this buffer. |
| 68 | * |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 69 | * The I/O sub-system does the following trick in order to avoid this extra |
| 70 | * copy. It always allocates a @ubi->vid_hdr_alsize bytes buffer for the VID |
| 71 | * header and returns a pointer to offset @ubi->vid_hdr_shift of this buffer. |
| 72 | * When the VID header is being written out, it shifts the VID header pointer |
| 73 | * back and writes the whole sub-page. |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 74 | */ |
| 75 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 76 | #ifndef __UBOOT__ |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 77 | #include <dm/devres.h> |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 78 | #include <linux/crc32.h> |
| 79 | #include <linux/err.h> |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 80 | #include <linux/slab.h> |
Simon Glass | 48b6c6b | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 81 | #include <u-boot/crc.h> |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 82 | #else |
Alexey Brodkin | 2d2fa49 | 2018-06-05 17:17:57 +0300 | [diff] [blame] | 83 | #include <hexdump.h> |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 84 | #include <ubi_uboot.h> |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 85 | #endif |
| 86 | |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 87 | #include "ubi.h" |
| 88 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 89 | static int self_check_not_bad(const struct ubi_device *ubi, int pnum); |
| 90 | static int self_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum); |
| 91 | static int self_check_ec_hdr(const struct ubi_device *ubi, int pnum, |
| 92 | const struct ubi_ec_hdr *ec_hdr); |
| 93 | static int self_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum); |
| 94 | static int self_check_vid_hdr(const struct ubi_device *ubi, int pnum, |
| 95 | const struct ubi_vid_hdr *vid_hdr); |
| 96 | static int self_check_write(struct ubi_device *ubi, const void *buf, int pnum, |
| 97 | int offset, int len); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 98 | |
| 99 | /** |
| 100 | * ubi_io_read - read data from a physical eraseblock. |
| 101 | * @ubi: UBI device description object |
| 102 | * @buf: buffer where to store the read data |
| 103 | * @pnum: physical eraseblock number to read from |
| 104 | * @offset: offset within the physical eraseblock from where to read |
| 105 | * @len: how many bytes to read |
| 106 | * |
| 107 | * This function reads data from offset @offset of physical eraseblock @pnum |
| 108 | * and stores the read data in the @buf buffer. The following return codes are |
| 109 | * possible: |
| 110 | * |
| 111 | * o %0 if all the requested data were successfully read; |
| 112 | * o %UBI_IO_BITFLIPS if all the requested data were successfully read, but |
| 113 | * correctable bit-flips were detected; this is harmless but may indicate |
| 114 | * that this eraseblock may become bad soon (but do not have to); |
| 115 | * o %-EBADMSG if the MTD subsystem reported about data integrity problems, for |
| 116 | * example it can be an ECC error in case of NAND; this most probably means |
| 117 | * that the data is corrupted; |
| 118 | * o %-EIO if some I/O error occurred; |
| 119 | * o other negative error codes in case of other errors. |
| 120 | */ |
| 121 | int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset, |
| 122 | int len) |
| 123 | { |
| 124 | int err, retries = 0; |
| 125 | size_t read; |
| 126 | loff_t addr; |
| 127 | |
| 128 | dbg_io("read %d bytes from PEB %d:%d", len, pnum, offset); |
| 129 | |
| 130 | ubi_assert(pnum >= 0 && pnum < ubi->peb_count); |
| 131 | ubi_assert(offset >= 0 && offset + len <= ubi->peb_size); |
| 132 | ubi_assert(len > 0); |
| 133 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 134 | err = self_check_not_bad(ubi, pnum); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 135 | if (err) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 136 | return err; |
| 137 | |
| 138 | /* |
| 139 | * Deliberately corrupt the buffer to improve robustness. Indeed, if we |
| 140 | * do not do this, the following may happen: |
| 141 | * 1. The buffer contains data from previous operation, e.g., read from |
| 142 | * another PEB previously. The data looks like expected, e.g., if we |
| 143 | * just do not read anything and return - the caller would not |
| 144 | * notice this. E.g., if we are reading a VID header, the buffer may |
| 145 | * contain a valid VID header from another PEB. |
| 146 | * 2. The driver is buggy and returns us success or -EBADMSG or |
| 147 | * -EUCLEAN, but it does not actually put any data to the buffer. |
| 148 | * |
| 149 | * This may confuse UBI or upper layers - they may think the buffer |
| 150 | * contains valid data while in fact it is just old data. This is |
| 151 | * especially possible because UBI (and UBIFS) relies on CRC, and |
| 152 | * treats data as correct even in case of ECC errors if the CRC is |
| 153 | * correct. |
| 154 | * |
| 155 | * Try to prevent this situation by changing the first byte of the |
| 156 | * buffer. |
| 157 | */ |
| 158 | *((uint8_t *)buf) ^= 0xFF; |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 159 | |
| 160 | addr = (loff_t)pnum * ubi->peb_size + offset; |
| 161 | retry: |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 162 | err = mtd_read(ubi->mtd, addr, len, &read, buf); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 163 | if (err) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 164 | const char *errstr = mtd_is_eccerr(err) ? " (ECC error)" : ""; |
| 165 | |
| 166 | if (mtd_is_bitflip(err)) { |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 167 | /* |
| 168 | * -EUCLEAN is reported if there was a bit-flip which |
| 169 | * was corrected, so this is harmless. |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 170 | * |
| 171 | * We do not report about it here unless debugging is |
| 172 | * enabled. A corresponding message will be printed |
| 173 | * later, when it is has been scrubbed. |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 174 | */ |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 175 | ubi_msg(ubi, "fixable bit-flip detected at PEB %d", |
| 176 | pnum); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 177 | ubi_assert(len == read); |
| 178 | return UBI_IO_BITFLIPS; |
| 179 | } |
| 180 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 181 | if (retries++ < UBI_IO_RETRIES) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 182 | ubi_warn(ubi, "error %d%s while reading %d bytes from PEB %d:%d, read only %zd bytes, retry", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 183 | err, errstr, len, pnum, offset, read); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 184 | yield(); |
| 185 | goto retry; |
| 186 | } |
| 187 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 188 | ubi_err(ubi, "error %d%s while reading %d bytes from PEB %d:%d, read %zd bytes", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 189 | err, errstr, len, pnum, offset, read); |
| 190 | dump_stack(); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 191 | |
| 192 | /* |
| 193 | * The driver should never return -EBADMSG if it failed to read |
| 194 | * all the requested data. But some buggy drivers might do |
| 195 | * this, so we change it to -EIO. |
| 196 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 197 | if (read != len && mtd_is_eccerr(err)) { |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 198 | ubi_assert(0); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 199 | err = -EIO; |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 200 | } |
| 201 | } else { |
| 202 | ubi_assert(len == read); |
| 203 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 204 | if (ubi_dbg_is_bitflip(ubi)) { |
| 205 | dbg_gen("bit-flip (emulated)"); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 206 | err = UBI_IO_BITFLIPS; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | return err; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * ubi_io_write - write data to a physical eraseblock. |
| 215 | * @ubi: UBI device description object |
| 216 | * @buf: buffer with the data to write |
| 217 | * @pnum: physical eraseblock number to write to |
| 218 | * @offset: offset within the physical eraseblock where to write |
| 219 | * @len: how many bytes to write |
| 220 | * |
| 221 | * This function writes @len bytes of data from buffer @buf to offset @offset |
| 222 | * of physical eraseblock @pnum. If all the data were successfully written, |
| 223 | * zero is returned. If an error occurred, this function returns a negative |
| 224 | * error code. If %-EIO is returned, the physical eraseblock most probably went |
| 225 | * bad. |
| 226 | * |
| 227 | * Note, in case of an error, it is possible that something was still written |
| 228 | * to the flash media, but may be some garbage. |
| 229 | */ |
| 230 | int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset, |
| 231 | int len) |
| 232 | { |
| 233 | int err; |
| 234 | size_t written; |
| 235 | loff_t addr; |
| 236 | |
| 237 | dbg_io("write %d bytes to PEB %d:%d", len, pnum, offset); |
| 238 | |
| 239 | ubi_assert(pnum >= 0 && pnum < ubi->peb_count); |
| 240 | ubi_assert(offset >= 0 && offset + len <= ubi->peb_size); |
| 241 | ubi_assert(offset % ubi->hdrs_min_io_size == 0); |
| 242 | ubi_assert(len > 0 && len % ubi->hdrs_min_io_size == 0); |
| 243 | |
| 244 | if (ubi->ro_mode) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 245 | ubi_err(ubi, "read-only mode"); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 246 | return -EROFS; |
| 247 | } |
| 248 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 249 | err = self_check_not_bad(ubi, pnum); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 250 | if (err) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 251 | return err; |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 252 | |
| 253 | /* The area we are writing to has to contain all 0xFF bytes */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 254 | err = ubi_self_check_all_ff(ubi, pnum, offset, len); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 255 | if (err) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 256 | return err; |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 257 | |
| 258 | if (offset >= ubi->leb_start) { |
| 259 | /* |
| 260 | * We write to the data area of the physical eraseblock. Make |
| 261 | * sure it has valid EC and VID headers. |
| 262 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 263 | err = self_check_peb_ec_hdr(ubi, pnum); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 264 | if (err) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 265 | return err; |
| 266 | err = self_check_peb_vid_hdr(ubi, pnum); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 267 | if (err) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 268 | return err; |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 269 | } |
| 270 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 271 | if (ubi_dbg_is_write_failure(ubi)) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 272 | ubi_err(ubi, "cannot write %d bytes to PEB %d:%d (emulated)", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 273 | len, pnum, offset); |
| 274 | dump_stack(); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 275 | return -EIO; |
| 276 | } |
| 277 | |
| 278 | addr = (loff_t)pnum * ubi->peb_size + offset; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 279 | err = mtd_write(ubi->mtd, addr, len, &written, buf); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 280 | if (err) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 281 | ubi_err(ubi, "error %d while writing %d bytes to PEB %d:%d, written %zd bytes", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 282 | err, len, pnum, offset, written); |
| 283 | dump_stack(); |
| 284 | ubi_dump_flash(ubi, pnum, offset, len); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 285 | } else |
| 286 | ubi_assert(written == len); |
| 287 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 288 | if (!err) { |
| 289 | err = self_check_write(ubi, buf, pnum, offset, len); |
| 290 | if (err) |
| 291 | return err; |
| 292 | |
| 293 | /* |
| 294 | * Since we always write sequentially, the rest of the PEB has |
| 295 | * to contain only 0xFF bytes. |
| 296 | */ |
| 297 | offset += len; |
| 298 | len = ubi->peb_size - offset; |
| 299 | if (len) |
| 300 | err = ubi_self_check_all_ff(ubi, pnum, offset, len); |
| 301 | } |
| 302 | |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 303 | return err; |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * erase_callback - MTD erasure call-back. |
| 308 | * @ei: MTD erase information object. |
| 309 | * |
| 310 | * Note, even though MTD erase interface is asynchronous, all the current |
| 311 | * implementations are synchronous anyway. |
| 312 | */ |
| 313 | static void erase_callback(struct erase_info *ei) |
| 314 | { |
| 315 | wake_up_interruptible((wait_queue_head_t *)ei->priv); |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * do_sync_erase - synchronously erase a physical eraseblock. |
| 320 | * @ubi: UBI device description object |
| 321 | * @pnum: the physical eraseblock number to erase |
| 322 | * |
| 323 | * This function synchronously erases physical eraseblock @pnum and returns |
| 324 | * zero in case of success and a negative error code in case of failure. If |
| 325 | * %-EIO is returned, the physical eraseblock most probably went bad. |
| 326 | */ |
| 327 | static int do_sync_erase(struct ubi_device *ubi, int pnum) |
| 328 | { |
| 329 | int err, retries = 0; |
| 330 | struct erase_info ei; |
| 331 | wait_queue_head_t wq; |
| 332 | |
| 333 | dbg_io("erase PEB %d", pnum); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 334 | ubi_assert(pnum >= 0 && pnum < ubi->peb_count); |
| 335 | |
| 336 | if (ubi->ro_mode) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 337 | ubi_err(ubi, "read-only mode"); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 338 | return -EROFS; |
| 339 | } |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 340 | |
| 341 | retry: |
| 342 | init_waitqueue_head(&wq); |
| 343 | memset(&ei, 0, sizeof(struct erase_info)); |
| 344 | |
| 345 | ei.mtd = ubi->mtd; |
| 346 | ei.addr = (loff_t)pnum * ubi->peb_size; |
| 347 | ei.len = ubi->peb_size; |
| 348 | ei.callback = erase_callback; |
| 349 | ei.priv = (unsigned long)&wq; |
| 350 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 351 | err = mtd_erase(ubi->mtd, &ei); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 352 | if (err) { |
| 353 | if (retries++ < UBI_IO_RETRIES) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 354 | ubi_warn(ubi, "error %d while erasing PEB %d, retry", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 355 | err, pnum); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 356 | yield(); |
| 357 | goto retry; |
| 358 | } |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 359 | ubi_err(ubi, "cannot erase PEB %d, error %d", pnum, err); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 360 | dump_stack(); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 361 | return err; |
| 362 | } |
| 363 | |
| 364 | err = wait_event_interruptible(wq, ei.state == MTD_ERASE_DONE || |
| 365 | ei.state == MTD_ERASE_FAILED); |
| 366 | if (err) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 367 | ubi_err(ubi, "interrupted PEB %d erasure", pnum); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 368 | return -EINTR; |
| 369 | } |
| 370 | |
| 371 | if (ei.state == MTD_ERASE_FAILED) { |
| 372 | if (retries++ < UBI_IO_RETRIES) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 373 | ubi_warn(ubi, "error while erasing PEB %d, retry", |
| 374 | pnum); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 375 | yield(); |
| 376 | goto retry; |
| 377 | } |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 378 | ubi_err(ubi, "cannot erase PEB %d", pnum); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 379 | dump_stack(); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 380 | return -EIO; |
| 381 | } |
| 382 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 383 | err = ubi_self_check_all_ff(ubi, pnum, 0, ubi->peb_size); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 384 | if (err) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 385 | return err; |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 386 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 387 | if (ubi_dbg_is_erase_failure(ubi)) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 388 | ubi_err(ubi, "cannot erase PEB %d (emulated)", pnum); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 389 | return -EIO; |
| 390 | } |
| 391 | |
| 392 | return 0; |
| 393 | } |
| 394 | |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 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 | */ |
| 407 | static int torture_peb(struct ubi_device *ubi, int pnum) |
| 408 | { |
| 409 | int err, i, patt_count; |
| 410 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 411 | ubi_msg(ubi, "run torture test for PEB %d", pnum); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 412 | patt_count = ARRAY_SIZE(patterns); |
| 413 | ubi_assert(patt_count > 0); |
| 414 | |
| 415 | mutex_lock(&ubi->buf_mutex); |
| 416 | for (i = 0; i < patt_count; i++) { |
| 417 | err = do_sync_erase(ubi, pnum); |
| 418 | if (err) |
| 419 | goto out; |
| 420 | |
| 421 | /* Make sure the PEB contains only 0xFF bytes */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 422 | err = ubi_io_read(ubi, ubi->peb_buf, pnum, 0, ubi->peb_size); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 423 | if (err) |
| 424 | goto out; |
| 425 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 426 | err = ubi_check_pattern(ubi->peb_buf, 0xFF, ubi->peb_size); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 427 | if (err == 0) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 428 | ubi_err(ubi, "erased PEB %d, but a non-0xFF byte found", |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 429 | pnum); |
| 430 | err = -EIO; |
| 431 | goto out; |
| 432 | } |
| 433 | |
| 434 | /* Write a pattern and check it */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 435 | memset(ubi->peb_buf, patterns[i], ubi->peb_size); |
| 436 | err = ubi_io_write(ubi, ubi->peb_buf, pnum, 0, ubi->peb_size); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 437 | if (err) |
| 438 | goto out; |
| 439 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 440 | memset(ubi->peb_buf, ~patterns[i], ubi->peb_size); |
| 441 | err = ubi_io_read(ubi, ubi->peb_buf, pnum, 0, ubi->peb_size); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 442 | if (err) |
| 443 | goto out; |
| 444 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 445 | err = ubi_check_pattern(ubi->peb_buf, patterns[i], |
| 446 | ubi->peb_size); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 447 | if (err == 0) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 448 | ubi_err(ubi, "pattern %x checking failed for PEB %d", |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 449 | patterns[i], pnum); |
| 450 | err = -EIO; |
| 451 | goto out; |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | err = patt_count; |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 456 | ubi_msg(ubi, "PEB %d passed torture test, do not mark it as bad", pnum); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 457 | |
| 458 | out: |
| 459 | mutex_unlock(&ubi->buf_mutex); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 460 | if (err == UBI_IO_BITFLIPS || mtd_is_eccerr(err)) { |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 461 | /* |
| 462 | * If a bit-flip or data integrity error was detected, the test |
| 463 | * has not passed because it happened on a freshly erased |
| 464 | * physical eraseblock which means something is wrong with it. |
| 465 | */ |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 466 | ubi_err(ubi, "read problems on freshly erased PEB %d, must be bad", |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 467 | pnum); |
| 468 | err = -EIO; |
| 469 | } |
| 470 | return err; |
| 471 | } |
| 472 | |
| 473 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 474 | * nor_erase_prepare - prepare a NOR flash PEB for erasure. |
| 475 | * @ubi: UBI device description object |
| 476 | * @pnum: physical eraseblock number to prepare |
| 477 | * |
| 478 | * NOR flash, or at least some of them, have peculiar embedded PEB erasure |
| 479 | * algorithm: the PEB is first filled with zeroes, then it is erased. And |
| 480 | * filling with zeroes starts from the end of the PEB. This was observed with |
| 481 | * Spansion S29GL512N NOR flash. |
| 482 | * |
| 483 | * This means that in case of a power cut we may end up with intact data at the |
| 484 | * beginning of the PEB, and all zeroes at the end of PEB. In other words, the |
| 485 | * EC and VID headers are OK, but a large chunk of data at the end of PEB is |
| 486 | * zeroed. This makes UBI mistakenly treat this PEB as used and associate it |
| 487 | * with an LEB, which leads to subsequent failures (e.g., UBIFS fails). |
| 488 | * |
| 489 | * This function is called before erasing NOR PEBs and it zeroes out EC and VID |
| 490 | * magic numbers in order to invalidate them and prevent the failures. Returns |
| 491 | * zero in case of success and a negative error code in case of failure. |
| 492 | */ |
| 493 | static int nor_erase_prepare(struct ubi_device *ubi, int pnum) |
| 494 | { |
| 495 | int err; |
| 496 | size_t written; |
| 497 | loff_t addr; |
| 498 | uint32_t data = 0; |
| 499 | struct ubi_ec_hdr ec_hdr; |
| 500 | |
| 501 | /* |
| 502 | * Note, we cannot generally define VID header buffers on stack, |
| 503 | * because of the way we deal with these buffers (see the header |
| 504 | * comment in this file). But we know this is a NOR-specific piece of |
| 505 | * code, so we can do this. But yes, this is error-prone and we should |
| 506 | * (pre-)allocate VID header buffer instead. |
| 507 | */ |
| 508 | struct ubi_vid_hdr vid_hdr; |
| 509 | |
| 510 | /* |
| 511 | * If VID or EC is valid, we have to corrupt them before erasing. |
| 512 | * It is important to first invalidate the EC header, and then the VID |
| 513 | * header. Otherwise a power cut may lead to valid EC header and |
| 514 | * invalid VID header, in which case UBI will treat this PEB as |
| 515 | * corrupted and will try to preserve it, and print scary warnings. |
| 516 | */ |
| 517 | addr = (loff_t)pnum * ubi->peb_size; |
| 518 | err = ubi_io_read_ec_hdr(ubi, pnum, &ec_hdr, 0); |
| 519 | if (err != UBI_IO_BAD_HDR_EBADMSG && err != UBI_IO_BAD_HDR && |
| 520 | err != UBI_IO_FF){ |
| 521 | err = mtd_write(ubi->mtd, addr, 4, &written, (void *)&data); |
| 522 | if(err) |
| 523 | goto error; |
| 524 | } |
| 525 | |
| 526 | err = ubi_io_read_vid_hdr(ubi, pnum, &vid_hdr, 0); |
| 527 | if (err != UBI_IO_BAD_HDR_EBADMSG && err != UBI_IO_BAD_HDR && |
| 528 | err != UBI_IO_FF){ |
| 529 | addr += ubi->vid_hdr_aloffset; |
| 530 | err = mtd_write(ubi->mtd, addr, 4, &written, (void *)&data); |
| 531 | if (err) |
| 532 | goto error; |
| 533 | } |
| 534 | return 0; |
| 535 | |
| 536 | error: |
| 537 | /* |
| 538 | * The PEB contains a valid VID or EC header, but we cannot invalidate |
| 539 | * it. Supposedly the flash media or the driver is screwed up, so |
| 540 | * return an error. |
| 541 | */ |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 542 | ubi_err(ubi, "cannot invalidate PEB %d, write returned %d", pnum, err); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 543 | ubi_dump_flash(ubi, pnum, 0, ubi->peb_size); |
| 544 | return -EIO; |
| 545 | } |
| 546 | |
| 547 | /** |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 548 | * ubi_io_sync_erase - synchronously erase a physical eraseblock. |
| 549 | * @ubi: UBI device description object |
| 550 | * @pnum: physical eraseblock number to erase |
| 551 | * @torture: if this physical eraseblock has to be tortured |
| 552 | * |
| 553 | * This function synchronously erases physical eraseblock @pnum. If @torture |
| 554 | * flag is not zero, the physical eraseblock is checked by means of writing |
| 555 | * different patterns to it and reading them back. If the torturing is enabled, |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 556 | * the physical eraseblock is erased more than once. |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 557 | * |
| 558 | * This function returns the number of erasures made in case of success, %-EIO |
| 559 | * if the erasure failed or the torturing test failed, and other negative error |
| 560 | * codes in case of other errors. Note, %-EIO means that the physical |
| 561 | * eraseblock is bad. |
| 562 | */ |
| 563 | int ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture) |
| 564 | { |
| 565 | int err, ret = 0; |
| 566 | |
| 567 | ubi_assert(pnum >= 0 && pnum < ubi->peb_count); |
| 568 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 569 | err = self_check_not_bad(ubi, pnum); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 570 | if (err != 0) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 571 | return err; |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 572 | |
| 573 | if (ubi->ro_mode) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 574 | ubi_err(ubi, "read-only mode"); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 575 | return -EROFS; |
| 576 | } |
| 577 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 578 | if (ubi->nor_flash) { |
| 579 | err = nor_erase_prepare(ubi, pnum); |
| 580 | if (err) |
| 581 | return err; |
| 582 | } |
| 583 | |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 584 | if (torture) { |
| 585 | ret = torture_peb(ubi, pnum); |
| 586 | if (ret < 0) |
| 587 | return ret; |
| 588 | } |
| 589 | |
| 590 | err = do_sync_erase(ubi, pnum); |
| 591 | if (err) |
| 592 | return err; |
| 593 | |
| 594 | return ret + 1; |
| 595 | } |
| 596 | |
| 597 | /** |
| 598 | * ubi_io_is_bad - check if a physical eraseblock is bad. |
| 599 | * @ubi: UBI device description object |
| 600 | * @pnum: the physical eraseblock number to check |
| 601 | * |
| 602 | * This function returns a positive number if the physical eraseblock is bad, |
| 603 | * zero if not, and a negative error code if an error occurred. |
| 604 | */ |
| 605 | int ubi_io_is_bad(const struct ubi_device *ubi, int pnum) |
| 606 | { |
| 607 | struct mtd_info *mtd = ubi->mtd; |
| 608 | |
| 609 | ubi_assert(pnum >= 0 && pnum < ubi->peb_count); |
| 610 | |
| 611 | if (ubi->bad_allowed) { |
| 612 | int ret; |
| 613 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 614 | ret = mtd_block_isbad(mtd, (loff_t)pnum * ubi->peb_size); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 615 | if (ret < 0) |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 616 | ubi_err(ubi, "error %d while checking if PEB %d is bad", |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 617 | ret, pnum); |
| 618 | else if (ret) |
| 619 | dbg_io("PEB %d is bad", pnum); |
| 620 | return ret; |
| 621 | } |
| 622 | |
| 623 | return 0; |
| 624 | } |
| 625 | |
| 626 | /** |
| 627 | * ubi_io_mark_bad - mark a physical eraseblock as bad. |
| 628 | * @ubi: UBI device description object |
| 629 | * @pnum: the physical eraseblock number to mark |
| 630 | * |
| 631 | * This function returns zero in case of success and a negative error code in |
| 632 | * case of failure. |
| 633 | */ |
| 634 | int ubi_io_mark_bad(const struct ubi_device *ubi, int pnum) |
| 635 | { |
| 636 | int err; |
| 637 | struct mtd_info *mtd = ubi->mtd; |
| 638 | |
| 639 | ubi_assert(pnum >= 0 && pnum < ubi->peb_count); |
| 640 | |
| 641 | if (ubi->ro_mode) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 642 | ubi_err(ubi, "read-only mode"); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 643 | return -EROFS; |
| 644 | } |
| 645 | |
| 646 | if (!ubi->bad_allowed) |
| 647 | return 0; |
| 648 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 649 | err = mtd_block_markbad(mtd, (loff_t)pnum * ubi->peb_size); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 650 | if (err) |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 651 | ubi_err(ubi, "cannot mark PEB %d bad, error %d", pnum, err); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 652 | return err; |
| 653 | } |
| 654 | |
| 655 | /** |
| 656 | * validate_ec_hdr - validate an erase counter header. |
| 657 | * @ubi: UBI device description object |
| 658 | * @ec_hdr: the erase counter header to check |
| 659 | * |
| 660 | * This function returns zero if the erase counter header is OK, and %1 if |
| 661 | * not. |
| 662 | */ |
| 663 | static int validate_ec_hdr(const struct ubi_device *ubi, |
| 664 | const struct ubi_ec_hdr *ec_hdr) |
| 665 | { |
| 666 | long long ec; |
| 667 | int vid_hdr_offset, leb_start; |
| 668 | |
| 669 | ec = be64_to_cpu(ec_hdr->ec); |
| 670 | vid_hdr_offset = be32_to_cpu(ec_hdr->vid_hdr_offset); |
| 671 | leb_start = be32_to_cpu(ec_hdr->data_offset); |
| 672 | |
| 673 | if (ec_hdr->version != UBI_VERSION) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 674 | ubi_err(ubi, "node with incompatible UBI version found: this UBI version is %d, image version is %d", |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 675 | UBI_VERSION, (int)ec_hdr->version); |
| 676 | goto bad; |
| 677 | } |
| 678 | |
| 679 | if (vid_hdr_offset != ubi->vid_hdr_offset) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 680 | ubi_err(ubi, "bad VID header offset %d, expected %d", |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 681 | vid_hdr_offset, ubi->vid_hdr_offset); |
| 682 | goto bad; |
| 683 | } |
| 684 | |
| 685 | if (leb_start != ubi->leb_start) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 686 | ubi_err(ubi, "bad data offset %d, expected %d", |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 687 | leb_start, ubi->leb_start); |
| 688 | goto bad; |
| 689 | } |
| 690 | |
| 691 | if (ec < 0 || ec > UBI_MAX_ERASECOUNTER) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 692 | ubi_err(ubi, "bad erase counter %lld", ec); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 693 | goto bad; |
| 694 | } |
| 695 | |
| 696 | return 0; |
| 697 | |
| 698 | bad: |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 699 | ubi_err(ubi, "bad EC header"); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 700 | ubi_dump_ec_hdr(ec_hdr); |
| 701 | dump_stack(); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 702 | return 1; |
| 703 | } |
| 704 | |
| 705 | /** |
| 706 | * ubi_io_read_ec_hdr - read and check an erase counter header. |
| 707 | * @ubi: UBI device description object |
| 708 | * @pnum: physical eraseblock to read from |
| 709 | * @ec_hdr: a &struct ubi_ec_hdr object where to store the read erase counter |
| 710 | * header |
| 711 | * @verbose: be verbose if the header is corrupted or was not found |
| 712 | * |
| 713 | * This function reads erase counter header from physical eraseblock @pnum and |
| 714 | * stores it in @ec_hdr. This function also checks CRC checksum of the read |
| 715 | * erase counter header. The following codes may be returned: |
| 716 | * |
| 717 | * o %0 if the CRC checksum is correct and the header was successfully read; |
| 718 | * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected |
| 719 | * and corrected by the flash driver; this is harmless but may indicate that |
| 720 | * this eraseblock may become bad soon (but may be not); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 721 | * o %UBI_IO_BAD_HDR if the erase counter header is corrupted (a CRC error); |
| 722 | * o %UBI_IO_BAD_HDR_EBADMSG is the same as %UBI_IO_BAD_HDR, but there also was |
| 723 | * a data integrity error (uncorrectable ECC error in case of NAND); |
| 724 | * o %UBI_IO_FF if only 0xFF bytes were read (the PEB is supposedly empty) |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 725 | * o a negative error code in case of failure. |
| 726 | */ |
| 727 | int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum, |
| 728 | struct ubi_ec_hdr *ec_hdr, int verbose) |
| 729 | { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 730 | int err, read_err; |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 731 | uint32_t crc, magic, hdr_crc; |
| 732 | |
| 733 | dbg_io("read EC header from PEB %d", pnum); |
| 734 | ubi_assert(pnum >= 0 && pnum < ubi->peb_count); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 735 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 736 | read_err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE); |
| 737 | if (read_err) { |
| 738 | if (read_err != UBI_IO_BITFLIPS && !mtd_is_eccerr(read_err)) |
| 739 | return read_err; |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 740 | |
| 741 | /* |
| 742 | * We read all the data, but either a correctable bit-flip |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 743 | * occurred, or MTD reported a data integrity error |
| 744 | * (uncorrectable ECC error in case of NAND). The former is |
| 745 | * harmless, the later may mean that the read data is |
| 746 | * corrupted. But we have a CRC check-sum and we will detect |
| 747 | * this. If the EC header is still OK, we just report this as |
| 748 | * there was a bit-flip, to force scrubbing. |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 749 | */ |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | magic = be32_to_cpu(ec_hdr->magic); |
| 753 | if (magic != UBI_EC_HDR_MAGIC) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 754 | if (mtd_is_eccerr(read_err)) |
| 755 | return UBI_IO_BAD_HDR_EBADMSG; |
| 756 | |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 757 | /* |
| 758 | * The magic field is wrong. Let's check if we have read all |
| 759 | * 0xFF. If yes, this physical eraseblock is assumed to be |
| 760 | * empty. |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 761 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 762 | if (ubi_check_pattern(ec_hdr, 0xFF, UBI_EC_HDR_SIZE)) { |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 763 | /* The physical eraseblock is supposedly empty */ |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 764 | if (verbose) |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 765 | ubi_warn(ubi, "no EC header found at PEB %d, only 0xFF bytes", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 766 | pnum); |
| 767 | dbg_bld("no EC header found at PEB %d, only 0xFF bytes", |
| 768 | pnum); |
| 769 | if (!read_err) |
| 770 | return UBI_IO_FF; |
| 771 | else |
| 772 | return UBI_IO_FF_BITFLIPS; |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | /* |
| 776 | * This is not a valid erase counter header, and these are not |
| 777 | * 0xFF bytes. Report that the header is corrupted. |
| 778 | */ |
| 779 | if (verbose) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 780 | ubi_warn(ubi, "bad magic number at PEB %d: %08x instead of %08x", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 781 | pnum, magic, UBI_EC_HDR_MAGIC); |
| 782 | ubi_dump_ec_hdr(ec_hdr); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 783 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 784 | dbg_bld("bad magic number at PEB %d: %08x instead of %08x", |
| 785 | pnum, magic, UBI_EC_HDR_MAGIC); |
| 786 | return UBI_IO_BAD_HDR; |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC); |
| 790 | hdr_crc = be32_to_cpu(ec_hdr->hdr_crc); |
| 791 | |
| 792 | if (hdr_crc != crc) { |
| 793 | if (verbose) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 794 | ubi_warn(ubi, "bad EC header CRC at PEB %d, calculated %#08x, read %#08x", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 795 | pnum, crc, hdr_crc); |
| 796 | ubi_dump_ec_hdr(ec_hdr); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 797 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 798 | dbg_bld("bad EC header CRC at PEB %d, calculated %#08x, read %#08x", |
| 799 | pnum, crc, hdr_crc); |
| 800 | |
| 801 | if (!read_err) |
| 802 | return UBI_IO_BAD_HDR; |
| 803 | else |
| 804 | return UBI_IO_BAD_HDR_EBADMSG; |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | /* And of course validate what has just been read from the media */ |
| 808 | err = validate_ec_hdr(ubi, ec_hdr); |
| 809 | if (err) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 810 | ubi_err(ubi, "validation failed for PEB %d", pnum); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 811 | return -EINVAL; |
| 812 | } |
| 813 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 814 | /* |
| 815 | * If there was %-EBADMSG, but the header CRC is still OK, report about |
| 816 | * a bit-flip to force scrubbing on this PEB. |
| 817 | */ |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 818 | return read_err ? UBI_IO_BITFLIPS : 0; |
| 819 | } |
| 820 | |
| 821 | /** |
| 822 | * ubi_io_write_ec_hdr - write an erase counter header. |
| 823 | * @ubi: UBI device description object |
| 824 | * @pnum: physical eraseblock to write to |
| 825 | * @ec_hdr: the erase counter header to write |
| 826 | * |
| 827 | * This function writes erase counter header described by @ec_hdr to physical |
| 828 | * eraseblock @pnum. It also fills most fields of @ec_hdr before writing, so |
| 829 | * the caller do not have to fill them. Callers must only fill the @ec_hdr->ec |
| 830 | * field. |
| 831 | * |
| 832 | * This function returns zero in case of success and a negative error code in |
| 833 | * case of failure. If %-EIO is returned, the physical eraseblock most probably |
| 834 | * went bad. |
| 835 | */ |
| 836 | int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum, |
| 837 | struct ubi_ec_hdr *ec_hdr) |
| 838 | { |
| 839 | int err; |
| 840 | uint32_t crc; |
| 841 | |
| 842 | dbg_io("write EC header to PEB %d", pnum); |
| 843 | ubi_assert(pnum >= 0 && pnum < ubi->peb_count); |
| 844 | |
| 845 | ec_hdr->magic = cpu_to_be32(UBI_EC_HDR_MAGIC); |
| 846 | ec_hdr->version = UBI_VERSION; |
| 847 | ec_hdr->vid_hdr_offset = cpu_to_be32(ubi->vid_hdr_offset); |
| 848 | ec_hdr->data_offset = cpu_to_be32(ubi->leb_start); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 849 | ec_hdr->image_seq = cpu_to_be32(ubi->image_seq); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 850 | crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC); |
| 851 | ec_hdr->hdr_crc = cpu_to_be32(crc); |
| 852 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 853 | err = self_check_ec_hdr(ubi, pnum, ec_hdr); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 854 | if (err) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 855 | return err; |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 856 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 857 | if (ubi_dbg_power_cut(ubi, POWER_CUT_EC_WRITE)) |
| 858 | return -EROFS; |
| 859 | |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 860 | err = ubi_io_write(ubi, ec_hdr, pnum, 0, ubi->ec_hdr_alsize); |
| 861 | return err; |
| 862 | } |
| 863 | |
| 864 | /** |
| 865 | * validate_vid_hdr - validate a volume identifier header. |
| 866 | * @ubi: UBI device description object |
| 867 | * @vid_hdr: the volume identifier header to check |
| 868 | * |
| 869 | * This function checks that data stored in the volume identifier header |
| 870 | * @vid_hdr. Returns zero if the VID header is OK and %1 if not. |
| 871 | */ |
| 872 | static int validate_vid_hdr(const struct ubi_device *ubi, |
| 873 | const struct ubi_vid_hdr *vid_hdr) |
| 874 | { |
| 875 | int vol_type = vid_hdr->vol_type; |
| 876 | int copy_flag = vid_hdr->copy_flag; |
| 877 | int vol_id = be32_to_cpu(vid_hdr->vol_id); |
| 878 | int lnum = be32_to_cpu(vid_hdr->lnum); |
| 879 | int compat = vid_hdr->compat; |
| 880 | int data_size = be32_to_cpu(vid_hdr->data_size); |
| 881 | int used_ebs = be32_to_cpu(vid_hdr->used_ebs); |
| 882 | int data_pad = be32_to_cpu(vid_hdr->data_pad); |
| 883 | int data_crc = be32_to_cpu(vid_hdr->data_crc); |
| 884 | int usable_leb_size = ubi->leb_size - data_pad; |
| 885 | |
| 886 | if (copy_flag != 0 && copy_flag != 1) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 887 | ubi_err(ubi, "bad copy_flag"); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 888 | goto bad; |
| 889 | } |
| 890 | |
| 891 | if (vol_id < 0 || lnum < 0 || data_size < 0 || used_ebs < 0 || |
| 892 | data_pad < 0) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 893 | ubi_err(ubi, "negative values"); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 894 | goto bad; |
| 895 | } |
| 896 | |
| 897 | if (vol_id >= UBI_MAX_VOLUMES && vol_id < UBI_INTERNAL_VOL_START) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 898 | ubi_err(ubi, "bad vol_id"); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 899 | goto bad; |
| 900 | } |
| 901 | |
| 902 | if (vol_id < UBI_INTERNAL_VOL_START && compat != 0) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 903 | ubi_err(ubi, "bad compat"); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 904 | goto bad; |
| 905 | } |
| 906 | |
| 907 | if (vol_id >= UBI_INTERNAL_VOL_START && compat != UBI_COMPAT_DELETE && |
| 908 | compat != UBI_COMPAT_RO && compat != UBI_COMPAT_PRESERVE && |
| 909 | compat != UBI_COMPAT_REJECT) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 910 | ubi_err(ubi, "bad compat"); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 911 | goto bad; |
| 912 | } |
| 913 | |
| 914 | if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 915 | ubi_err(ubi, "bad vol_type"); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 916 | goto bad; |
| 917 | } |
| 918 | |
| 919 | if (data_pad >= ubi->leb_size / 2) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 920 | ubi_err(ubi, "bad data_pad"); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 921 | goto bad; |
| 922 | } |
| 923 | |
| 924 | if (vol_type == UBI_VID_STATIC) { |
| 925 | /* |
| 926 | * Although from high-level point of view static volumes may |
| 927 | * contain zero bytes of data, but no VID headers can contain |
| 928 | * zero at these fields, because they empty volumes do not have |
| 929 | * mapped logical eraseblocks. |
| 930 | */ |
| 931 | if (used_ebs == 0) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 932 | ubi_err(ubi, "zero used_ebs"); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 933 | goto bad; |
| 934 | } |
| 935 | if (data_size == 0) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 936 | ubi_err(ubi, "zero data_size"); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 937 | goto bad; |
| 938 | } |
| 939 | if (lnum < used_ebs - 1) { |
| 940 | if (data_size != usable_leb_size) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 941 | ubi_err(ubi, "bad data_size"); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 942 | goto bad; |
| 943 | } |
| 944 | } else if (lnum == used_ebs - 1) { |
| 945 | if (data_size == 0) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 946 | ubi_err(ubi, "bad data_size at last LEB"); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 947 | goto bad; |
| 948 | } |
| 949 | } else { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 950 | ubi_err(ubi, "too high lnum"); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 951 | goto bad; |
| 952 | } |
| 953 | } else { |
| 954 | if (copy_flag == 0) { |
| 955 | if (data_crc != 0) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 956 | ubi_err(ubi, "non-zero data CRC"); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 957 | goto bad; |
| 958 | } |
| 959 | if (data_size != 0) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 960 | ubi_err(ubi, "non-zero data_size"); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 961 | goto bad; |
| 962 | } |
| 963 | } else { |
| 964 | if (data_size == 0) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 965 | ubi_err(ubi, "zero data_size of copy"); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 966 | goto bad; |
| 967 | } |
| 968 | } |
| 969 | if (used_ebs != 0) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 970 | ubi_err(ubi, "bad used_ebs"); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 971 | goto bad; |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | return 0; |
| 976 | |
| 977 | bad: |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 978 | ubi_err(ubi, "bad VID header"); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 979 | ubi_dump_vid_hdr(vid_hdr); |
| 980 | dump_stack(); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 981 | return 1; |
| 982 | } |
| 983 | |
| 984 | /** |
| 985 | * ubi_io_read_vid_hdr - read and check a volume identifier header. |
| 986 | * @ubi: UBI device description object |
| 987 | * @pnum: physical eraseblock number to read from |
| 988 | * @vid_hdr: &struct ubi_vid_hdr object where to store the read volume |
| 989 | * identifier header |
| 990 | * @verbose: be verbose if the header is corrupted or wasn't found |
| 991 | * |
| 992 | * This function reads the volume identifier header from physical eraseblock |
| 993 | * @pnum and stores it in @vid_hdr. It also checks CRC checksum of the read |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 994 | * volume identifier header. The error codes are the same as in |
| 995 | * 'ubi_io_read_ec_hdr()'. |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 996 | * |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 997 | * Note, the implementation of this function is also very similar to |
| 998 | * 'ubi_io_read_ec_hdr()', so refer commentaries in 'ubi_io_read_ec_hdr()'. |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 999 | */ |
| 1000 | int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum, |
| 1001 | struct ubi_vid_hdr *vid_hdr, int verbose) |
| 1002 | { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1003 | int err, read_err; |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1004 | uint32_t crc, magic, hdr_crc; |
| 1005 | void *p; |
| 1006 | |
| 1007 | dbg_io("read VID header from PEB %d", pnum); |
| 1008 | ubi_assert(pnum >= 0 && pnum < ubi->peb_count); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1009 | |
| 1010 | p = (char *)vid_hdr - ubi->vid_hdr_shift; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1011 | read_err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset, |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1012 | ubi->vid_hdr_alsize); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1013 | if (read_err && read_err != UBI_IO_BITFLIPS && !mtd_is_eccerr(read_err)) |
| 1014 | return read_err; |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1015 | |
| 1016 | magic = be32_to_cpu(vid_hdr->magic); |
| 1017 | if (magic != UBI_VID_HDR_MAGIC) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1018 | if (mtd_is_eccerr(read_err)) |
| 1019 | return UBI_IO_BAD_HDR_EBADMSG; |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1020 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1021 | if (ubi_check_pattern(vid_hdr, 0xFF, UBI_VID_HDR_SIZE)) { |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1022 | if (verbose) |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1023 | ubi_warn(ubi, "no VID header found at PEB %d, only 0xFF bytes", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1024 | pnum); |
| 1025 | dbg_bld("no VID header found at PEB %d, only 0xFF bytes", |
| 1026 | pnum); |
| 1027 | if (!read_err) |
| 1028 | return UBI_IO_FF; |
| 1029 | else |
| 1030 | return UBI_IO_FF_BITFLIPS; |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1031 | } |
| 1032 | |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1033 | if (verbose) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1034 | ubi_warn(ubi, "bad magic number at PEB %d: %08x instead of %08x", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1035 | pnum, magic, UBI_VID_HDR_MAGIC); |
| 1036 | ubi_dump_vid_hdr(vid_hdr); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1037 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1038 | dbg_bld("bad magic number at PEB %d: %08x instead of %08x", |
| 1039 | pnum, magic, UBI_VID_HDR_MAGIC); |
| 1040 | return UBI_IO_BAD_HDR; |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC); |
| 1044 | hdr_crc = be32_to_cpu(vid_hdr->hdr_crc); |
| 1045 | |
| 1046 | if (hdr_crc != crc) { |
| 1047 | if (verbose) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1048 | ubi_warn(ubi, "bad CRC at PEB %d, calculated %#08x, read %#08x", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1049 | pnum, crc, hdr_crc); |
| 1050 | ubi_dump_vid_hdr(vid_hdr); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1051 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1052 | dbg_bld("bad CRC at PEB %d, calculated %#08x, read %#08x", |
| 1053 | pnum, crc, hdr_crc); |
| 1054 | if (!read_err) |
| 1055 | return UBI_IO_BAD_HDR; |
| 1056 | else |
| 1057 | return UBI_IO_BAD_HDR_EBADMSG; |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1058 | } |
| 1059 | |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1060 | err = validate_vid_hdr(ubi, vid_hdr); |
| 1061 | if (err) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1062 | ubi_err(ubi, "validation failed for PEB %d", pnum); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1063 | return -EINVAL; |
| 1064 | } |
| 1065 | |
| 1066 | return read_err ? UBI_IO_BITFLIPS : 0; |
| 1067 | } |
| 1068 | |
| 1069 | /** |
| 1070 | * ubi_io_write_vid_hdr - write a volume identifier header. |
| 1071 | * @ubi: UBI device description object |
| 1072 | * @pnum: the physical eraseblock number to write to |
| 1073 | * @vid_hdr: the volume identifier header to write |
| 1074 | * |
| 1075 | * This function writes the volume identifier header described by @vid_hdr to |
| 1076 | * physical eraseblock @pnum. This function automatically fills the |
| 1077 | * @vid_hdr->magic and the @vid_hdr->version fields, as well as calculates |
| 1078 | * header CRC checksum and stores it at vid_hdr->hdr_crc. |
| 1079 | * |
| 1080 | * This function returns zero in case of success and a negative error code in |
| 1081 | * case of failure. If %-EIO is returned, the physical eraseblock probably went |
| 1082 | * bad. |
| 1083 | */ |
| 1084 | int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum, |
| 1085 | struct ubi_vid_hdr *vid_hdr) |
| 1086 | { |
| 1087 | int err; |
| 1088 | uint32_t crc; |
| 1089 | void *p; |
| 1090 | |
| 1091 | dbg_io("write VID header to PEB %d", pnum); |
| 1092 | ubi_assert(pnum >= 0 && pnum < ubi->peb_count); |
| 1093 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1094 | err = self_check_peb_ec_hdr(ubi, pnum); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1095 | if (err) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1096 | return err; |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1097 | |
| 1098 | vid_hdr->magic = cpu_to_be32(UBI_VID_HDR_MAGIC); |
| 1099 | vid_hdr->version = UBI_VERSION; |
| 1100 | crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC); |
| 1101 | vid_hdr->hdr_crc = cpu_to_be32(crc); |
| 1102 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1103 | err = self_check_vid_hdr(ubi, pnum, vid_hdr); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1104 | if (err) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1105 | return err; |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1106 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1107 | if (ubi_dbg_power_cut(ubi, POWER_CUT_VID_WRITE)) |
| 1108 | return -EROFS; |
| 1109 | |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1110 | p = (char *)vid_hdr - ubi->vid_hdr_shift; |
| 1111 | err = ubi_io_write(ubi, p, pnum, ubi->vid_hdr_aloffset, |
| 1112 | ubi->vid_hdr_alsize); |
| 1113 | return err; |
| 1114 | } |
| 1115 | |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1116 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1117 | * self_check_not_bad - ensure that a physical eraseblock is not bad. |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1118 | * @ubi: UBI device description object |
| 1119 | * @pnum: physical eraseblock number to check |
| 1120 | * |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1121 | * This function returns zero if the physical eraseblock is good, %-EINVAL if |
| 1122 | * it is bad and a negative error code if an error occurred. |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1123 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1124 | static int self_check_not_bad(const struct ubi_device *ubi, int pnum) |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1125 | { |
| 1126 | int err; |
| 1127 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1128 | if (!ubi_dbg_chk_io(ubi)) |
| 1129 | return 0; |
| 1130 | |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1131 | err = ubi_io_is_bad(ubi, pnum); |
| 1132 | if (!err) |
| 1133 | return err; |
| 1134 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1135 | ubi_err(ubi, "self-check failed for PEB %d", pnum); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1136 | dump_stack(); |
| 1137 | return err > 0 ? -EINVAL : err; |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1141 | * self_check_ec_hdr - check if an erase counter header is all right. |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1142 | * @ubi: UBI device description object |
| 1143 | * @pnum: physical eraseblock number the erase counter header belongs to |
| 1144 | * @ec_hdr: the erase counter header to check |
| 1145 | * |
| 1146 | * This function returns zero if the erase counter header contains valid |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1147 | * values, and %-EINVAL if not. |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1148 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1149 | static int self_check_ec_hdr(const struct ubi_device *ubi, int pnum, |
| 1150 | const struct ubi_ec_hdr *ec_hdr) |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1151 | { |
| 1152 | int err; |
| 1153 | uint32_t magic; |
| 1154 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1155 | if (!ubi_dbg_chk_io(ubi)) |
| 1156 | return 0; |
| 1157 | |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1158 | magic = be32_to_cpu(ec_hdr->magic); |
| 1159 | if (magic != UBI_EC_HDR_MAGIC) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1160 | ubi_err(ubi, "bad magic %#08x, must be %#08x", |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1161 | magic, UBI_EC_HDR_MAGIC); |
| 1162 | goto fail; |
| 1163 | } |
| 1164 | |
| 1165 | err = validate_ec_hdr(ubi, ec_hdr); |
| 1166 | if (err) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1167 | ubi_err(ubi, "self-check failed for PEB %d", pnum); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1168 | goto fail; |
| 1169 | } |
| 1170 | |
| 1171 | return 0; |
| 1172 | |
| 1173 | fail: |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1174 | ubi_dump_ec_hdr(ec_hdr); |
| 1175 | dump_stack(); |
| 1176 | return -EINVAL; |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1180 | * self_check_peb_ec_hdr - check erase counter header. |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1181 | * @ubi: UBI device description object |
| 1182 | * @pnum: the physical eraseblock number to check |
| 1183 | * |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1184 | * This function returns zero if the erase counter header is all right and and |
| 1185 | * a negative error code if not or if an error occurred. |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1186 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1187 | static int self_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum) |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1188 | { |
| 1189 | int err; |
| 1190 | uint32_t crc, hdr_crc; |
| 1191 | struct ubi_ec_hdr *ec_hdr; |
| 1192 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1193 | if (!ubi_dbg_chk_io(ubi)) |
| 1194 | return 0; |
| 1195 | |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1196 | ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS); |
| 1197 | if (!ec_hdr) |
| 1198 | return -ENOMEM; |
| 1199 | |
| 1200 | err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1201 | if (err && err != UBI_IO_BITFLIPS && !mtd_is_eccerr(err)) |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1202 | goto exit; |
| 1203 | |
| 1204 | crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC); |
| 1205 | hdr_crc = be32_to_cpu(ec_hdr->hdr_crc); |
| 1206 | if (hdr_crc != crc) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1207 | ubi_err(ubi, "bad CRC, calculated %#08x, read %#08x", |
| 1208 | crc, hdr_crc); |
| 1209 | ubi_err(ubi, "self-check failed for PEB %d", pnum); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1210 | ubi_dump_ec_hdr(ec_hdr); |
| 1211 | dump_stack(); |
| 1212 | err = -EINVAL; |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1213 | goto exit; |
| 1214 | } |
| 1215 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1216 | err = self_check_ec_hdr(ubi, pnum, ec_hdr); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1217 | |
| 1218 | exit: |
| 1219 | kfree(ec_hdr); |
| 1220 | return err; |
| 1221 | } |
| 1222 | |
| 1223 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1224 | * self_check_vid_hdr - check that a volume identifier header is all right. |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1225 | * @ubi: UBI device description object |
| 1226 | * @pnum: physical eraseblock number the volume identifier header belongs to |
| 1227 | * @vid_hdr: the volume identifier header to check |
| 1228 | * |
| 1229 | * This function returns zero if the volume identifier header is all right, and |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1230 | * %-EINVAL if not. |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1231 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1232 | static int self_check_vid_hdr(const struct ubi_device *ubi, int pnum, |
| 1233 | const struct ubi_vid_hdr *vid_hdr) |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1234 | { |
| 1235 | int err; |
| 1236 | uint32_t magic; |
| 1237 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1238 | if (!ubi_dbg_chk_io(ubi)) |
| 1239 | return 0; |
| 1240 | |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1241 | magic = be32_to_cpu(vid_hdr->magic); |
| 1242 | if (magic != UBI_VID_HDR_MAGIC) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1243 | ubi_err(ubi, "bad VID header magic %#08x at PEB %d, must be %#08x", |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1244 | magic, pnum, UBI_VID_HDR_MAGIC); |
| 1245 | goto fail; |
| 1246 | } |
| 1247 | |
| 1248 | err = validate_vid_hdr(ubi, vid_hdr); |
| 1249 | if (err) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1250 | ubi_err(ubi, "self-check failed for PEB %d", pnum); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1251 | goto fail; |
| 1252 | } |
| 1253 | |
| 1254 | return err; |
| 1255 | |
| 1256 | fail: |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1257 | ubi_err(ubi, "self-check failed for PEB %d", pnum); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1258 | ubi_dump_vid_hdr(vid_hdr); |
| 1259 | dump_stack(); |
| 1260 | return -EINVAL; |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1261 | |
| 1262 | } |
| 1263 | |
| 1264 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1265 | * self_check_peb_vid_hdr - check volume identifier header. |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1266 | * @ubi: UBI device description object |
| 1267 | * @pnum: the physical eraseblock number to check |
| 1268 | * |
| 1269 | * This function returns zero if the volume identifier header is all right, |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1270 | * and a negative error code if not or if an error occurred. |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1271 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1272 | static int self_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum) |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1273 | { |
| 1274 | int err; |
| 1275 | uint32_t crc, hdr_crc; |
| 1276 | struct ubi_vid_hdr *vid_hdr; |
| 1277 | void *p; |
| 1278 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1279 | if (!ubi_dbg_chk_io(ubi)) |
| 1280 | return 0; |
| 1281 | |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1282 | vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS); |
| 1283 | if (!vid_hdr) |
| 1284 | return -ENOMEM; |
| 1285 | |
| 1286 | p = (char *)vid_hdr - ubi->vid_hdr_shift; |
| 1287 | err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset, |
| 1288 | ubi->vid_hdr_alsize); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1289 | if (err && err != UBI_IO_BITFLIPS && !mtd_is_eccerr(err)) |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1290 | goto exit; |
| 1291 | |
| 1292 | crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_EC_HDR_SIZE_CRC); |
| 1293 | hdr_crc = be32_to_cpu(vid_hdr->hdr_crc); |
| 1294 | if (hdr_crc != crc) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1295 | ubi_err(ubi, "bad VID header CRC at PEB %d, calculated %#08x, read %#08x", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1296 | pnum, crc, hdr_crc); |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1297 | ubi_err(ubi, "self-check failed for PEB %d", pnum); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1298 | ubi_dump_vid_hdr(vid_hdr); |
| 1299 | dump_stack(); |
| 1300 | err = -EINVAL; |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1301 | goto exit; |
| 1302 | } |
| 1303 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1304 | err = self_check_vid_hdr(ubi, pnum, vid_hdr); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1305 | |
| 1306 | exit: |
| 1307 | ubi_free_vid_hdr(ubi, vid_hdr); |
| 1308 | return err; |
| 1309 | } |
| 1310 | |
| 1311 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1312 | * self_check_write - make sure write succeeded. |
| 1313 | * @ubi: UBI device description object |
| 1314 | * @buf: buffer with data which were written |
| 1315 | * @pnum: physical eraseblock number the data were written to |
| 1316 | * @offset: offset within the physical eraseblock the data were written to |
| 1317 | * @len: how many bytes were written |
| 1318 | * |
| 1319 | * This functions reads data which were recently written and compares it with |
| 1320 | * the original data buffer - the data have to match. Returns zero if the data |
| 1321 | * match and a negative error code if not or in case of failure. |
| 1322 | */ |
| 1323 | static int self_check_write(struct ubi_device *ubi, const void *buf, int pnum, |
| 1324 | int offset, int len) |
| 1325 | { |
| 1326 | int err, i; |
| 1327 | size_t read; |
| 1328 | void *buf1; |
| 1329 | loff_t addr = (loff_t)pnum * ubi->peb_size + offset; |
| 1330 | |
| 1331 | if (!ubi_dbg_chk_io(ubi)) |
| 1332 | return 0; |
| 1333 | |
| 1334 | buf1 = __vmalloc(len, GFP_NOFS, PAGE_KERNEL); |
| 1335 | if (!buf1) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1336 | ubi_err(ubi, "cannot allocate memory to check writes"); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1337 | return 0; |
| 1338 | } |
| 1339 | |
| 1340 | err = mtd_read(ubi->mtd, addr, len, &read, buf1); |
| 1341 | if (err && !mtd_is_bitflip(err)) |
| 1342 | goto out_free; |
| 1343 | |
| 1344 | for (i = 0; i < len; i++) { |
| 1345 | uint8_t c = ((uint8_t *)buf)[i]; |
| 1346 | uint8_t c1 = ((uint8_t *)buf1)[i]; |
| 1347 | #if !defined(CONFIG_UBI_SILENCE_MSG) |
| 1348 | int dump_len = max_t(int, 128, len - i); |
| 1349 | #endif |
| 1350 | |
| 1351 | if (c == c1) |
| 1352 | continue; |
| 1353 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1354 | ubi_err(ubi, "self-check failed for PEB %d:%d, len %d", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1355 | pnum, offset, len); |
Markus Klotzbuecher | 9c77e20 | 2019-05-15 15:15:56 +0200 | [diff] [blame] | 1356 | #if !defined(CONFIG_UBI_SILENCE_MSG) |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1357 | ubi_msg(ubi, "data differ at position %d", i); |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1358 | ubi_msg(ubi, "hex dump of the original buffer from %d to %d", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1359 | i, i + dump_len); |
Alexey Brodkin | 2d2fa49 | 2018-06-05 17:17:57 +0300 | [diff] [blame] | 1360 | print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 1, |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1361 | buf + i, dump_len, 1); |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1362 | ubi_msg(ubi, "hex dump of the read buffer from %d to %d", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1363 | i, i + dump_len); |
Alexey Brodkin | 2d2fa49 | 2018-06-05 17:17:57 +0300 | [diff] [blame] | 1364 | print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 1, |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1365 | buf1 + i, dump_len, 1); |
Markus Klotzbuecher | 9c77e20 | 2019-05-15 15:15:56 +0200 | [diff] [blame] | 1366 | #endif |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1367 | dump_stack(); |
| 1368 | err = -EINVAL; |
| 1369 | goto out_free; |
| 1370 | } |
| 1371 | |
| 1372 | vfree(buf1); |
| 1373 | return 0; |
| 1374 | |
| 1375 | out_free: |
| 1376 | vfree(buf1); |
| 1377 | return err; |
| 1378 | } |
| 1379 | |
| 1380 | /** |
| 1381 | * ubi_self_check_all_ff - check that a region of flash is empty. |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1382 | * @ubi: UBI device description object |
| 1383 | * @pnum: the physical eraseblock number to check |
| 1384 | * @offset: the starting offset within the physical eraseblock to check |
| 1385 | * @len: the length of the region to check |
| 1386 | * |
| 1387 | * This function returns zero if only 0xFF bytes are present at offset |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1388 | * @offset of the physical eraseblock @pnum, and a negative error code if not |
| 1389 | * or if an error occurred. |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1390 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1391 | int ubi_self_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len) |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1392 | { |
| 1393 | size_t read; |
| 1394 | int err; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1395 | void *buf; |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1396 | loff_t addr = (loff_t)pnum * ubi->peb_size + offset; |
| 1397 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1398 | if (!ubi_dbg_chk_io(ubi)) |
| 1399 | return 0; |
| 1400 | |
| 1401 | buf = __vmalloc(len, GFP_NOFS, PAGE_KERNEL); |
| 1402 | if (!buf) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1403 | ubi_err(ubi, "cannot allocate memory to check for 0xFFs"); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1404 | return 0; |
| 1405 | } |
| 1406 | |
| 1407 | err = mtd_read(ubi->mtd, addr, len, &read, buf); |
| 1408 | if (err && !mtd_is_bitflip(err)) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1409 | ubi_err(ubi, "err %d while reading %d bytes from PEB %d:%d, read %zd bytes", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1410 | err, len, pnum, offset, read); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1411 | goto error; |
| 1412 | } |
| 1413 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1414 | err = ubi_check_pattern(buf, 0xFF, len); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1415 | if (err == 0) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1416 | ubi_err(ubi, "flash region at PEB %d:%d, length %d does not contain all 0xFF bytes", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1417 | pnum, offset, len); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1418 | goto fail; |
| 1419 | } |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1420 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1421 | vfree(buf); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1422 | return 0; |
| 1423 | |
| 1424 | fail: |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1425 | ubi_err(ubi, "self-check failed for PEB %d", pnum); |
| 1426 | ubi_msg(ubi, "hex dump of the %d-%d region", offset, offset + len); |
Alexey Brodkin | 2d2fa49 | 2018-06-05 17:17:57 +0300 | [diff] [blame] | 1427 | print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 1, buf, len, 1); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1428 | err = -EINVAL; |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1429 | error: |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1430 | dump_stack(); |
| 1431 | vfree(buf); |
Kyungmin Park | a598345 | 2008-11-19 16:26:54 +0100 | [diff] [blame] | 1432 | return err; |
| 1433 | } |