1801c135cSArtem B. Bityutskiy /* 2801c135cSArtem B. Bityutskiy * Copyright (c) International Business Machines Corp., 2006 3801c135cSArtem B. Bityutskiy * Copyright (c) Nokia Corporation, 2006, 2007 4801c135cSArtem B. Bityutskiy * 5801c135cSArtem B. Bityutskiy * This program is free software; you can redistribute it and/or modify 6801c135cSArtem B. Bityutskiy * it under the terms of the GNU General Public License as published by 7801c135cSArtem B. Bityutskiy * the Free Software Foundation; either version 2 of the License, or 8801c135cSArtem B. Bityutskiy * (at your option) any later version. 9801c135cSArtem B. Bityutskiy * 10801c135cSArtem B. Bityutskiy * This program is distributed in the hope that it will be useful, 11801c135cSArtem B. Bityutskiy * but WITHOUT ANY WARRANTY; without even the implied warranty of 12801c135cSArtem B. Bityutskiy * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 13801c135cSArtem B. Bityutskiy * the GNU General Public License for more details. 14801c135cSArtem B. Bityutskiy * 15801c135cSArtem B. Bityutskiy * You should have received a copy of the GNU General Public License 16801c135cSArtem B. Bityutskiy * along with this program; if not, write to the Free Software 17801c135cSArtem B. Bityutskiy * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18801c135cSArtem B. Bityutskiy * 19801c135cSArtem B. Bityutskiy * Author: Artem Bityutskiy (Битюцкий Артём) 20801c135cSArtem B. Bityutskiy */ 21801c135cSArtem B. Bityutskiy 22801c135cSArtem B. Bityutskiy /* 2385c6e6e2SArtem Bityutskiy * UBI input/output sub-system. 24801c135cSArtem B. Bityutskiy * 2585c6e6e2SArtem Bityutskiy * This sub-system provides a uniform way to work with all kinds of the 2685c6e6e2SArtem Bityutskiy * underlying MTD devices. It also implements handy functions for reading and 2785c6e6e2SArtem Bityutskiy * writing UBI headers. 28801c135cSArtem B. Bityutskiy * 29801c135cSArtem B. Bityutskiy * We are trying to have a paranoid mindset and not to trust to what we read 3085c6e6e2SArtem Bityutskiy * from the flash media in order to be more secure and robust. So this 3185c6e6e2SArtem Bityutskiy * sub-system validates every single header it reads from the flash media. 32801c135cSArtem B. Bityutskiy * 33801c135cSArtem B. Bityutskiy * Some words about how the eraseblock headers are stored. 34801c135cSArtem B. Bityutskiy * 35801c135cSArtem B. Bityutskiy * The erase counter header is always stored at offset zero. By default, the 36801c135cSArtem B. Bityutskiy * VID header is stored after the EC header at the closest aligned offset 37801c135cSArtem B. Bityutskiy * (i.e. aligned to the minimum I/O unit size). Data starts next to the VID 38801c135cSArtem B. Bityutskiy * header at the closest aligned offset. But this default layout may be 39801c135cSArtem B. Bityutskiy * changed. For example, for different reasons (e.g., optimization) UBI may be 40801c135cSArtem B. Bityutskiy * asked to put the VID header at further offset, and even at an unaligned 41801c135cSArtem B. Bityutskiy * offset. Of course, if the offset of the VID header is unaligned, UBI adds 42801c135cSArtem B. Bityutskiy * proper padding in front of it. Data offset may also be changed but it has to 43801c135cSArtem B. Bityutskiy * be aligned. 44801c135cSArtem B. Bityutskiy * 45801c135cSArtem B. Bityutskiy * About minimal I/O units. In general, UBI assumes flash device model where 46801c135cSArtem B. Bityutskiy * there is only one minimal I/O unit size. E.g., in case of NOR flash it is 1, 47801c135cSArtem B. Bityutskiy * in case of NAND flash it is a NAND page, etc. This is reported by MTD in the 48801c135cSArtem B. Bityutskiy * @ubi->mtd->writesize field. But as an exception, UBI admits of using another 49801c135cSArtem B. Bityutskiy * (smaller) minimal I/O unit size for EC and VID headers to make it possible 50801c135cSArtem B. Bityutskiy * to do different optimizations. 51801c135cSArtem B. Bityutskiy * 52801c135cSArtem B. Bityutskiy * This is extremely useful in case of NAND flashes which admit of several 53801c135cSArtem B. Bityutskiy * write operations to one NAND page. In this case UBI can fit EC and VID 54801c135cSArtem B. Bityutskiy * headers at one NAND page. Thus, UBI may use "sub-page" size as the minimal 55801c135cSArtem B. Bityutskiy * I/O unit for the headers (the @ubi->hdrs_min_io_size field). But it still 56801c135cSArtem B. Bityutskiy * reports NAND page size (@ubi->min_io_size) as a minimal I/O unit for the UBI 57801c135cSArtem B. Bityutskiy * users. 58801c135cSArtem B. Bityutskiy * 59801c135cSArtem B. Bityutskiy * Example: some Samsung NANDs with 2KiB pages allow 4x 512-byte writes, so 60801c135cSArtem B. Bityutskiy * although the minimal I/O unit is 2K, UBI uses 512 bytes for EC and VID 61801c135cSArtem B. Bityutskiy * headers. 62801c135cSArtem B. Bityutskiy * 63801c135cSArtem B. Bityutskiy * Q: why not just to treat sub-page as a minimal I/O unit of this flash 64801c135cSArtem B. Bityutskiy * device, e.g., make @ubi->min_io_size = 512 in the example above? 65801c135cSArtem B. Bityutskiy * 66801c135cSArtem B. Bityutskiy * A: because when writing a sub-page, MTD still writes a full 2K page but the 67801c135cSArtem B. Bityutskiy * bytes which are no relevant to the sub-page are 0xFF. So, basically, writing 68801c135cSArtem B. Bityutskiy * 4x512 sub-pages is 4 times slower then writing one 2KiB NAND page. Thus, we 69801c135cSArtem B. Bityutskiy * prefer to use sub-pages only for EV and VID headers. 70801c135cSArtem B. Bityutskiy * 71801c135cSArtem B. Bityutskiy * As it was noted above, the VID header may start at a non-aligned offset. 72801c135cSArtem B. Bityutskiy * For example, in case of a 2KiB page NAND flash with a 512 bytes sub-page, 73801c135cSArtem B. Bityutskiy * the VID header may reside at offset 1984 which is the last 64 bytes of the 74801c135cSArtem B. Bityutskiy * last sub-page (EC header is always at offset zero). This causes some 75801c135cSArtem B. Bityutskiy * difficulties when reading and writing VID headers. 76801c135cSArtem B. Bityutskiy * 77801c135cSArtem B. Bityutskiy * Suppose we have a 64-byte buffer and we read a VID header at it. We change 78801c135cSArtem B. Bityutskiy * the data and want to write this VID header out. As we can only write in 79801c135cSArtem B. Bityutskiy * 512-byte chunks, we have to allocate one more buffer and copy our VID header 80801c135cSArtem B. Bityutskiy * to offset 448 of this buffer. 81801c135cSArtem B. Bityutskiy * 8285c6e6e2SArtem Bityutskiy * The I/O sub-system does the following trick in order to avoid this extra 8385c6e6e2SArtem Bityutskiy * copy. It always allocates a @ubi->vid_hdr_alsize bytes buffer for the VID 8485c6e6e2SArtem Bityutskiy * header and returns a pointer to offset @ubi->vid_hdr_shift of this buffer. 8585c6e6e2SArtem Bityutskiy * When the VID header is being written out, it shifts the VID header pointer 8685c6e6e2SArtem Bityutskiy * back and writes the whole sub-page. 87801c135cSArtem B. Bityutskiy */ 88801c135cSArtem B. Bityutskiy 89801c135cSArtem B. Bityutskiy #include <linux/crc32.h> 90801c135cSArtem B. Bityutskiy #include <linux/err.h> 91801c135cSArtem B. Bityutskiy #include "ubi.h" 92801c135cSArtem B. Bityutskiy 93801c135cSArtem B. Bityutskiy #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID 94801c135cSArtem B. Bityutskiy static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum); 95801c135cSArtem B. Bityutskiy static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum); 96801c135cSArtem B. Bityutskiy static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum, 97801c135cSArtem B. Bityutskiy const struct ubi_ec_hdr *ec_hdr); 98801c135cSArtem B. Bityutskiy static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum); 99801c135cSArtem B. Bityutskiy static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum, 100801c135cSArtem B. Bityutskiy const struct ubi_vid_hdr *vid_hdr); 101ffb6b7e4SArtem Bityutskiy static int paranoid_check_empty(struct ubi_device *ubi, int pnum); 102801c135cSArtem B. Bityutskiy #else 103801c135cSArtem B. Bityutskiy #define paranoid_check_not_bad(ubi, pnum) 0 104801c135cSArtem B. Bityutskiy #define paranoid_check_peb_ec_hdr(ubi, pnum) 0 105801c135cSArtem B. Bityutskiy #define paranoid_check_ec_hdr(ubi, pnum, ec_hdr) 0 106801c135cSArtem B. Bityutskiy #define paranoid_check_peb_vid_hdr(ubi, pnum) 0 107801c135cSArtem B. Bityutskiy #define paranoid_check_vid_hdr(ubi, pnum, vid_hdr) 0 108ffb6b7e4SArtem Bityutskiy #define paranoid_check_empty(ubi, pnum) 0 109801c135cSArtem B. Bityutskiy #endif 110801c135cSArtem B. Bityutskiy 111801c135cSArtem B. Bityutskiy /** 112801c135cSArtem B. Bityutskiy * ubi_io_read - read data from a physical eraseblock. 113801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 114801c135cSArtem B. Bityutskiy * @buf: buffer where to store the read data 115801c135cSArtem B. Bityutskiy * @pnum: physical eraseblock number to read from 116801c135cSArtem B. Bityutskiy * @offset: offset within the physical eraseblock from where to read 117801c135cSArtem B. Bityutskiy * @len: how many bytes to read 118801c135cSArtem B. Bityutskiy * 119801c135cSArtem B. Bityutskiy * This function reads data from offset @offset of physical eraseblock @pnum 120801c135cSArtem B. Bityutskiy * and stores the read data in the @buf buffer. The following return codes are 121801c135cSArtem B. Bityutskiy * possible: 122801c135cSArtem B. Bityutskiy * 123801c135cSArtem B. Bityutskiy * o %0 if all the requested data were successfully read; 124801c135cSArtem B. Bityutskiy * o %UBI_IO_BITFLIPS if all the requested data were successfully read, but 125801c135cSArtem B. Bityutskiy * correctable bit-flips were detected; this is harmless but may indicate 126801c135cSArtem B. Bityutskiy * that this eraseblock may become bad soon (but do not have to); 12763b6c1edSArtem Bityutskiy * o %-EBADMSG if the MTD subsystem reported about data integrity problems, for 12863b6c1edSArtem Bityutskiy * example it can be an ECC error in case of NAND; this most probably means 12963b6c1edSArtem Bityutskiy * that the data is corrupted; 130801c135cSArtem B. Bityutskiy * o %-EIO if some I/O error occurred; 131801c135cSArtem B. Bityutskiy * o other negative error codes in case of other errors. 132801c135cSArtem B. Bityutskiy */ 133801c135cSArtem B. Bityutskiy int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset, 134801c135cSArtem B. Bityutskiy int len) 135801c135cSArtem B. Bityutskiy { 136801c135cSArtem B. Bityutskiy int err, retries = 0; 137801c135cSArtem B. Bityutskiy size_t read; 138801c135cSArtem B. Bityutskiy loff_t addr; 139801c135cSArtem B. Bityutskiy 140801c135cSArtem B. Bityutskiy dbg_io("read %d bytes from PEB %d:%d", len, pnum, offset); 141801c135cSArtem B. Bityutskiy 142801c135cSArtem B. Bityutskiy ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 143801c135cSArtem B. Bityutskiy ubi_assert(offset >= 0 && offset + len <= ubi->peb_size); 144801c135cSArtem B. Bityutskiy ubi_assert(len > 0); 145801c135cSArtem B. Bityutskiy 146801c135cSArtem B. Bityutskiy err = paranoid_check_not_bad(ubi, pnum); 147801c135cSArtem B. Bityutskiy if (err) 148801c135cSArtem B. Bityutskiy return err > 0 ? -EINVAL : err; 149801c135cSArtem B. Bityutskiy 150801c135cSArtem B. Bityutskiy addr = (loff_t)pnum * ubi->peb_size + offset; 151801c135cSArtem B. Bityutskiy retry: 152801c135cSArtem B. Bityutskiy err = ubi->mtd->read(ubi->mtd, addr, len, &read, buf); 153801c135cSArtem B. Bityutskiy if (err) { 154801c135cSArtem B. Bityutskiy if (err == -EUCLEAN) { 155801c135cSArtem B. Bityutskiy /* 156801c135cSArtem B. Bityutskiy * -EUCLEAN is reported if there was a bit-flip which 157801c135cSArtem B. Bityutskiy * was corrected, so this is harmless. 1588c1e6ee1SArtem Bityutskiy * 1598c1e6ee1SArtem Bityutskiy * We do not report about it here unless debugging is 1608c1e6ee1SArtem Bityutskiy * enabled. A corresponding message will be printed 1618c1e6ee1SArtem Bityutskiy * later, when it is has been scrubbed. 162801c135cSArtem B. Bityutskiy */ 1638c1e6ee1SArtem Bityutskiy dbg_msg("fixable bit-flip detected at PEB %d", pnum); 164801c135cSArtem B. Bityutskiy ubi_assert(len == read); 165801c135cSArtem B. Bityutskiy return UBI_IO_BITFLIPS; 166801c135cSArtem B. Bityutskiy } 167801c135cSArtem B. Bityutskiy 168801c135cSArtem B. Bityutskiy if (read != len && retries++ < UBI_IO_RETRIES) { 169801c135cSArtem B. Bityutskiy dbg_io("error %d while reading %d bytes from PEB %d:%d," 170801c135cSArtem B. Bityutskiy " read only %zd bytes, retry", 171801c135cSArtem B. Bityutskiy err, len, pnum, offset, read); 172801c135cSArtem B. Bityutskiy yield(); 173801c135cSArtem B. Bityutskiy goto retry; 174801c135cSArtem B. Bityutskiy } 175801c135cSArtem B. Bityutskiy 176801c135cSArtem B. Bityutskiy ubi_err("error %d while reading %d bytes from PEB %d:%d, " 177801c135cSArtem B. Bityutskiy "read %zd bytes", err, len, pnum, offset, read); 178801c135cSArtem B. Bityutskiy ubi_dbg_dump_stack(); 1792362a53eSArtem Bityutskiy 1802362a53eSArtem Bityutskiy /* 1812362a53eSArtem Bityutskiy * The driver should never return -EBADMSG if it failed to read 1822362a53eSArtem Bityutskiy * all the requested data. But some buggy drivers might do 1832362a53eSArtem Bityutskiy * this, so we change it to -EIO. 1842362a53eSArtem Bityutskiy */ 1852362a53eSArtem Bityutskiy if (read != len && err == -EBADMSG) { 1862362a53eSArtem Bityutskiy ubi_assert(0); 1872362a53eSArtem Bityutskiy err = -EIO; 1882362a53eSArtem Bityutskiy } 189801c135cSArtem B. Bityutskiy } else { 190801c135cSArtem B. Bityutskiy ubi_assert(len == read); 191801c135cSArtem B. Bityutskiy 192801c135cSArtem B. Bityutskiy if (ubi_dbg_is_bitflip()) { 193c8566350SArtem Bityutskiy dbg_gen("bit-flip (emulated)"); 194801c135cSArtem B. Bityutskiy err = UBI_IO_BITFLIPS; 195801c135cSArtem B. Bityutskiy } 196801c135cSArtem B. Bityutskiy } 197801c135cSArtem B. Bityutskiy 198801c135cSArtem B. Bityutskiy return err; 199801c135cSArtem B. Bityutskiy } 200801c135cSArtem B. Bityutskiy 201801c135cSArtem B. Bityutskiy /** 202801c135cSArtem B. Bityutskiy * ubi_io_write - write data to a physical eraseblock. 203801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 204801c135cSArtem B. Bityutskiy * @buf: buffer with the data to write 205801c135cSArtem B. Bityutskiy * @pnum: physical eraseblock number to write to 206801c135cSArtem B. Bityutskiy * @offset: offset within the physical eraseblock where to write 207801c135cSArtem B. Bityutskiy * @len: how many bytes to write 208801c135cSArtem B. Bityutskiy * 209801c135cSArtem B. Bityutskiy * This function writes @len bytes of data from buffer @buf to offset @offset 210801c135cSArtem B. Bityutskiy * of physical eraseblock @pnum. If all the data were successfully written, 211801c135cSArtem B. Bityutskiy * zero is returned. If an error occurred, this function returns a negative 212801c135cSArtem B. Bityutskiy * error code. If %-EIO is returned, the physical eraseblock most probably went 213801c135cSArtem B. Bityutskiy * bad. 214801c135cSArtem B. Bityutskiy * 215801c135cSArtem B. Bityutskiy * Note, in case of an error, it is possible that something was still written 216801c135cSArtem B. Bityutskiy * to the flash media, but may be some garbage. 217801c135cSArtem B. Bityutskiy */ 218e88d6e10SArtem Bityutskiy int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset, 219e88d6e10SArtem Bityutskiy int len) 220801c135cSArtem B. Bityutskiy { 221801c135cSArtem B. Bityutskiy int err; 222801c135cSArtem B. Bityutskiy size_t written; 223801c135cSArtem B. Bityutskiy loff_t addr; 224801c135cSArtem B. Bityutskiy 225801c135cSArtem B. Bityutskiy dbg_io("write %d bytes to PEB %d:%d", len, pnum, offset); 226801c135cSArtem B. Bityutskiy 227801c135cSArtem B. Bityutskiy ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 228801c135cSArtem B. Bityutskiy ubi_assert(offset >= 0 && offset + len <= ubi->peb_size); 229801c135cSArtem B. Bityutskiy ubi_assert(offset % ubi->hdrs_min_io_size == 0); 230801c135cSArtem B. Bityutskiy ubi_assert(len > 0 && len % ubi->hdrs_min_io_size == 0); 231801c135cSArtem B. Bityutskiy 232801c135cSArtem B. Bityutskiy if (ubi->ro_mode) { 233801c135cSArtem B. Bityutskiy ubi_err("read-only mode"); 234801c135cSArtem B. Bityutskiy return -EROFS; 235801c135cSArtem B. Bityutskiy } 236801c135cSArtem B. Bityutskiy 237801c135cSArtem B. Bityutskiy /* The below has to be compiled out if paranoid checks are disabled */ 238801c135cSArtem B. Bityutskiy 239801c135cSArtem B. Bityutskiy err = paranoid_check_not_bad(ubi, pnum); 240801c135cSArtem B. Bityutskiy if (err) 241801c135cSArtem B. Bityutskiy return err > 0 ? -EINVAL : err; 242801c135cSArtem B. Bityutskiy 243801c135cSArtem B. Bityutskiy /* The area we are writing to has to contain all 0xFF bytes */ 24440a71a87SArtem Bityutskiy err = ubi_dbg_check_all_ff(ubi, pnum, offset, len); 245801c135cSArtem B. Bityutskiy if (err) 246801c135cSArtem B. Bityutskiy return err > 0 ? -EINVAL : err; 247801c135cSArtem B. Bityutskiy 248801c135cSArtem B. Bityutskiy if (offset >= ubi->leb_start) { 249801c135cSArtem B. Bityutskiy /* 250801c135cSArtem B. Bityutskiy * We write to the data area of the physical eraseblock. Make 251801c135cSArtem B. Bityutskiy * sure it has valid EC and VID headers. 252801c135cSArtem B. Bityutskiy */ 253801c135cSArtem B. Bityutskiy err = paranoid_check_peb_ec_hdr(ubi, pnum); 254801c135cSArtem B. Bityutskiy if (err) 255801c135cSArtem B. Bityutskiy return err > 0 ? -EINVAL : err; 256801c135cSArtem B. Bityutskiy err = paranoid_check_peb_vid_hdr(ubi, pnum); 257801c135cSArtem B. Bityutskiy if (err) 258801c135cSArtem B. Bityutskiy return err > 0 ? -EINVAL : err; 259801c135cSArtem B. Bityutskiy } 260801c135cSArtem B. Bityutskiy 261801c135cSArtem B. Bityutskiy if (ubi_dbg_is_write_failure()) { 262801c135cSArtem B. Bityutskiy dbg_err("cannot write %d bytes to PEB %d:%d " 263801c135cSArtem B. Bityutskiy "(emulated)", len, pnum, offset); 264801c135cSArtem B. Bityutskiy ubi_dbg_dump_stack(); 265801c135cSArtem B. Bityutskiy return -EIO; 266801c135cSArtem B. Bityutskiy } 267801c135cSArtem B. Bityutskiy 268801c135cSArtem B. Bityutskiy addr = (loff_t)pnum * ubi->peb_size + offset; 269801c135cSArtem B. Bityutskiy err = ubi->mtd->write(ubi->mtd, addr, len, &written, buf); 270801c135cSArtem B. Bityutskiy if (err) { 271801c135cSArtem B. Bityutskiy ubi_err("error %d while writing %d bytes to PEB %d:%d, written" 272801c135cSArtem B. Bityutskiy " %zd bytes", err, len, pnum, offset, written); 273801c135cSArtem B. Bityutskiy ubi_dbg_dump_stack(); 274801c135cSArtem B. Bityutskiy } else 275801c135cSArtem B. Bityutskiy ubi_assert(written == len); 276801c135cSArtem B. Bityutskiy 277801c135cSArtem B. Bityutskiy return err; 278801c135cSArtem B. Bityutskiy } 279801c135cSArtem B. Bityutskiy 280801c135cSArtem B. Bityutskiy /** 281801c135cSArtem B. Bityutskiy * erase_callback - MTD erasure call-back. 282801c135cSArtem B. Bityutskiy * @ei: MTD erase information object. 283801c135cSArtem B. Bityutskiy * 284801c135cSArtem B. Bityutskiy * Note, even though MTD erase interface is asynchronous, all the current 285801c135cSArtem B. Bityutskiy * implementations are synchronous anyway. 286801c135cSArtem B. Bityutskiy */ 287801c135cSArtem B. Bityutskiy static void erase_callback(struct erase_info *ei) 288801c135cSArtem B. Bityutskiy { 289801c135cSArtem B. Bityutskiy wake_up_interruptible((wait_queue_head_t *)ei->priv); 290801c135cSArtem B. Bityutskiy } 291801c135cSArtem B. Bityutskiy 292801c135cSArtem B. Bityutskiy /** 293801c135cSArtem B. Bityutskiy * do_sync_erase - synchronously erase a physical eraseblock. 294801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 295801c135cSArtem B. Bityutskiy * @pnum: the physical eraseblock number to erase 296801c135cSArtem B. Bityutskiy * 297801c135cSArtem B. Bityutskiy * This function synchronously erases physical eraseblock @pnum and returns 298801c135cSArtem B. Bityutskiy * zero in case of success and a negative error code in case of failure. If 299801c135cSArtem B. Bityutskiy * %-EIO is returned, the physical eraseblock most probably went bad. 300801c135cSArtem B. Bityutskiy */ 301e88d6e10SArtem Bityutskiy static int do_sync_erase(struct ubi_device *ubi, int pnum) 302801c135cSArtem B. Bityutskiy { 303801c135cSArtem B. Bityutskiy int err, retries = 0; 304801c135cSArtem B. Bityutskiy struct erase_info ei; 305801c135cSArtem B. Bityutskiy wait_queue_head_t wq; 306801c135cSArtem B. Bityutskiy 307801c135cSArtem B. Bityutskiy dbg_io("erase PEB %d", pnum); 308801c135cSArtem B. Bityutskiy 309801c135cSArtem B. Bityutskiy retry: 310801c135cSArtem B. Bityutskiy init_waitqueue_head(&wq); 311801c135cSArtem B. Bityutskiy memset(&ei, 0, sizeof(struct erase_info)); 312801c135cSArtem B. Bityutskiy 313801c135cSArtem B. Bityutskiy ei.mtd = ubi->mtd; 3142f176f79SBrijesh Singh ei.addr = (loff_t)pnum * ubi->peb_size; 315801c135cSArtem B. Bityutskiy ei.len = ubi->peb_size; 316801c135cSArtem B. Bityutskiy ei.callback = erase_callback; 317801c135cSArtem B. Bityutskiy ei.priv = (unsigned long)&wq; 318801c135cSArtem B. Bityutskiy 319801c135cSArtem B. Bityutskiy err = ubi->mtd->erase(ubi->mtd, &ei); 320801c135cSArtem B. Bityutskiy if (err) { 321801c135cSArtem B. Bityutskiy if (retries++ < UBI_IO_RETRIES) { 322801c135cSArtem B. Bityutskiy dbg_io("error %d while erasing PEB %d, retry", 323801c135cSArtem B. Bityutskiy err, pnum); 324801c135cSArtem B. Bityutskiy yield(); 325801c135cSArtem B. Bityutskiy goto retry; 326801c135cSArtem B. Bityutskiy } 327801c135cSArtem B. Bityutskiy ubi_err("cannot erase PEB %d, error %d", pnum, err); 328801c135cSArtem B. Bityutskiy ubi_dbg_dump_stack(); 329801c135cSArtem B. Bityutskiy return err; 330801c135cSArtem B. Bityutskiy } 331801c135cSArtem B. Bityutskiy 332801c135cSArtem B. Bityutskiy err = wait_event_interruptible(wq, ei.state == MTD_ERASE_DONE || 333801c135cSArtem B. Bityutskiy ei.state == MTD_ERASE_FAILED); 334801c135cSArtem B. Bityutskiy if (err) { 335801c135cSArtem B. Bityutskiy ubi_err("interrupted PEB %d erasure", pnum); 336801c135cSArtem B. Bityutskiy return -EINTR; 337801c135cSArtem B. Bityutskiy } 338801c135cSArtem B. Bityutskiy 339801c135cSArtem B. Bityutskiy if (ei.state == MTD_ERASE_FAILED) { 340801c135cSArtem B. Bityutskiy if (retries++ < UBI_IO_RETRIES) { 341801c135cSArtem B. Bityutskiy dbg_io("error while erasing PEB %d, retry", pnum); 342801c135cSArtem B. Bityutskiy yield(); 343801c135cSArtem B. Bityutskiy goto retry; 344801c135cSArtem B. Bityutskiy } 345801c135cSArtem B. Bityutskiy ubi_err("cannot erase PEB %d", pnum); 346801c135cSArtem B. Bityutskiy ubi_dbg_dump_stack(); 347801c135cSArtem B. Bityutskiy return -EIO; 348801c135cSArtem B. Bityutskiy } 349801c135cSArtem B. Bityutskiy 35040a71a87SArtem Bityutskiy err = ubi_dbg_check_all_ff(ubi, pnum, 0, ubi->peb_size); 351801c135cSArtem B. Bityutskiy if (err) 352801c135cSArtem B. Bityutskiy return err > 0 ? -EINVAL : err; 353801c135cSArtem B. Bityutskiy 354801c135cSArtem B. Bityutskiy if (ubi_dbg_is_erase_failure() && !err) { 355801c135cSArtem B. Bityutskiy dbg_err("cannot erase PEB %d (emulated)", pnum); 356801c135cSArtem B. Bityutskiy return -EIO; 357801c135cSArtem B. Bityutskiy } 358801c135cSArtem B. Bityutskiy 359801c135cSArtem B. Bityutskiy return 0; 360801c135cSArtem B. Bityutskiy } 361801c135cSArtem B. Bityutskiy 362801c135cSArtem B. Bityutskiy /** 363801c135cSArtem B. Bityutskiy * check_pattern - check if buffer contains only a certain byte pattern. 364801c135cSArtem B. Bityutskiy * @buf: buffer to check 365801c135cSArtem B. Bityutskiy * @patt: the pattern to check 366801c135cSArtem B. Bityutskiy * @size: buffer size in bytes 367801c135cSArtem B. Bityutskiy * 368801c135cSArtem B. Bityutskiy * This function returns %1 in there are only @patt bytes in @buf, and %0 if 369801c135cSArtem B. Bityutskiy * something else was also found. 370801c135cSArtem B. Bityutskiy */ 371801c135cSArtem B. Bityutskiy static int check_pattern(const void *buf, uint8_t patt, int size) 372801c135cSArtem B. Bityutskiy { 373801c135cSArtem B. Bityutskiy int i; 374801c135cSArtem B. Bityutskiy 375801c135cSArtem B. Bityutskiy for (i = 0; i < size; i++) 376801c135cSArtem B. Bityutskiy if (((const uint8_t *)buf)[i] != patt) 377801c135cSArtem B. Bityutskiy return 0; 378801c135cSArtem B. Bityutskiy return 1; 379801c135cSArtem B. Bityutskiy } 380801c135cSArtem B. Bityutskiy 381801c135cSArtem B. Bityutskiy /* Patterns to write to a physical eraseblock when torturing it */ 382801c135cSArtem B. Bityutskiy static uint8_t patterns[] = {0xa5, 0x5a, 0x0}; 383801c135cSArtem B. Bityutskiy 384801c135cSArtem B. Bityutskiy /** 385801c135cSArtem B. Bityutskiy * torture_peb - test a supposedly bad physical eraseblock. 386801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 387801c135cSArtem B. Bityutskiy * @pnum: the physical eraseblock number to test 388801c135cSArtem B. Bityutskiy * 389801c135cSArtem B. Bityutskiy * This function returns %-EIO if the physical eraseblock did not pass the 390801c135cSArtem B. Bityutskiy * test, a positive number of erase operations done if the test was 391801c135cSArtem B. Bityutskiy * successfully passed, and other negative error codes in case of other errors. 392801c135cSArtem B. Bityutskiy */ 393e88d6e10SArtem Bityutskiy static int torture_peb(struct ubi_device *ubi, int pnum) 394801c135cSArtem B. Bityutskiy { 395801c135cSArtem B. Bityutskiy int err, i, patt_count; 396801c135cSArtem B. Bityutskiy 3978c1e6ee1SArtem Bityutskiy ubi_msg("run torture test for PEB %d", pnum); 398801c135cSArtem B. Bityutskiy patt_count = ARRAY_SIZE(patterns); 399801c135cSArtem B. Bityutskiy ubi_assert(patt_count > 0); 400801c135cSArtem B. Bityutskiy 401e88d6e10SArtem Bityutskiy mutex_lock(&ubi->buf_mutex); 402801c135cSArtem B. Bityutskiy for (i = 0; i < patt_count; i++) { 403801c135cSArtem B. Bityutskiy err = do_sync_erase(ubi, pnum); 404801c135cSArtem B. Bityutskiy if (err) 405801c135cSArtem B. Bityutskiy goto out; 406801c135cSArtem B. Bityutskiy 407801c135cSArtem B. Bityutskiy /* Make sure the PEB contains only 0xFF bytes */ 408e88d6e10SArtem Bityutskiy err = ubi_io_read(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size); 409801c135cSArtem B. Bityutskiy if (err) 410801c135cSArtem B. Bityutskiy goto out; 411801c135cSArtem B. Bityutskiy 412e88d6e10SArtem Bityutskiy err = check_pattern(ubi->peb_buf1, 0xFF, ubi->peb_size); 413801c135cSArtem B. Bityutskiy if (err == 0) { 414801c135cSArtem B. Bityutskiy ubi_err("erased PEB %d, but a non-0xFF byte found", 415801c135cSArtem B. Bityutskiy pnum); 416801c135cSArtem B. Bityutskiy err = -EIO; 417801c135cSArtem B. Bityutskiy goto out; 418801c135cSArtem B. Bityutskiy } 419801c135cSArtem B. Bityutskiy 420801c135cSArtem B. Bityutskiy /* Write a pattern and check it */ 421e88d6e10SArtem Bityutskiy memset(ubi->peb_buf1, patterns[i], ubi->peb_size); 422e88d6e10SArtem Bityutskiy err = ubi_io_write(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size); 423801c135cSArtem B. Bityutskiy if (err) 424801c135cSArtem B. Bityutskiy goto out; 425801c135cSArtem B. Bityutskiy 426e88d6e10SArtem Bityutskiy memset(ubi->peb_buf1, ~patterns[i], ubi->peb_size); 427e88d6e10SArtem Bityutskiy err = ubi_io_read(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size); 428801c135cSArtem B. Bityutskiy if (err) 429801c135cSArtem B. Bityutskiy goto out; 430801c135cSArtem B. Bityutskiy 431e88d6e10SArtem Bityutskiy err = check_pattern(ubi->peb_buf1, patterns[i], ubi->peb_size); 432801c135cSArtem B. Bityutskiy if (err == 0) { 433801c135cSArtem B. Bityutskiy ubi_err("pattern %x checking failed for PEB %d", 434801c135cSArtem B. Bityutskiy patterns[i], pnum); 435801c135cSArtem B. Bityutskiy err = -EIO; 436801c135cSArtem B. Bityutskiy goto out; 437801c135cSArtem B. Bityutskiy } 438801c135cSArtem B. Bityutskiy } 439801c135cSArtem B. Bityutskiy 440801c135cSArtem B. Bityutskiy err = patt_count; 4418c1e6ee1SArtem Bityutskiy ubi_msg("PEB %d passed torture test, do not mark it a bad", pnum); 442801c135cSArtem B. Bityutskiy 443801c135cSArtem B. Bityutskiy out: 444e88d6e10SArtem Bityutskiy mutex_unlock(&ubi->buf_mutex); 4458d2d4011SArtem Bityutskiy if (err == UBI_IO_BITFLIPS || err == -EBADMSG) { 446801c135cSArtem B. Bityutskiy /* 447801c135cSArtem B. Bityutskiy * If a bit-flip or data integrity error was detected, the test 448801c135cSArtem B. Bityutskiy * has not passed because it happened on a freshly erased 449801c135cSArtem B. Bityutskiy * physical eraseblock which means something is wrong with it. 450801c135cSArtem B. Bityutskiy */ 4518d2d4011SArtem Bityutskiy ubi_err("read problems on freshly erased PEB %d, must be bad", 4528d2d4011SArtem Bityutskiy pnum); 453801c135cSArtem B. Bityutskiy err = -EIO; 4548d2d4011SArtem Bityutskiy } 455801c135cSArtem B. Bityutskiy return err; 456801c135cSArtem B. Bityutskiy } 457801c135cSArtem B. Bityutskiy 458801c135cSArtem B. Bityutskiy /** 459801c135cSArtem B. Bityutskiy * ubi_io_sync_erase - synchronously erase a physical eraseblock. 460801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 461801c135cSArtem B. Bityutskiy * @pnum: physical eraseblock number to erase 462801c135cSArtem B. Bityutskiy * @torture: if this physical eraseblock has to be tortured 463801c135cSArtem B. Bityutskiy * 464801c135cSArtem B. Bityutskiy * This function synchronously erases physical eraseblock @pnum. If @torture 465801c135cSArtem B. Bityutskiy * flag is not zero, the physical eraseblock is checked by means of writing 466801c135cSArtem B. Bityutskiy * different patterns to it and reading them back. If the torturing is enabled, 467025dfdafSFrederik Schwarzer * the physical eraseblock is erased more than once. 468801c135cSArtem B. Bityutskiy * 469801c135cSArtem B. Bityutskiy * This function returns the number of erasures made in case of success, %-EIO 470801c135cSArtem B. Bityutskiy * if the erasure failed or the torturing test failed, and other negative error 471801c135cSArtem B. Bityutskiy * codes in case of other errors. Note, %-EIO means that the physical 472801c135cSArtem B. Bityutskiy * eraseblock is bad. 473801c135cSArtem B. Bityutskiy */ 474e88d6e10SArtem Bityutskiy int ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture) 475801c135cSArtem B. Bityutskiy { 476801c135cSArtem B. Bityutskiy int err, ret = 0; 477801c135cSArtem B. Bityutskiy 478801c135cSArtem B. Bityutskiy ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 479801c135cSArtem B. Bityutskiy 480801c135cSArtem B. Bityutskiy err = paranoid_check_not_bad(ubi, pnum); 481801c135cSArtem B. Bityutskiy if (err != 0) 482801c135cSArtem B. Bityutskiy return err > 0 ? -EINVAL : err; 483801c135cSArtem B. Bityutskiy 484801c135cSArtem B. Bityutskiy if (ubi->ro_mode) { 485801c135cSArtem B. Bityutskiy ubi_err("read-only mode"); 486801c135cSArtem B. Bityutskiy return -EROFS; 487801c135cSArtem B. Bityutskiy } 488801c135cSArtem B. Bityutskiy 489801c135cSArtem B. Bityutskiy if (torture) { 490801c135cSArtem B. Bityutskiy ret = torture_peb(ubi, pnum); 491801c135cSArtem B. Bityutskiy if (ret < 0) 492801c135cSArtem B. Bityutskiy return ret; 493801c135cSArtem B. Bityutskiy } 494801c135cSArtem B. Bityutskiy 495801c135cSArtem B. Bityutskiy err = do_sync_erase(ubi, pnum); 496801c135cSArtem B. Bityutskiy if (err) 497801c135cSArtem B. Bityutskiy return err; 498801c135cSArtem B. Bityutskiy 499801c135cSArtem B. Bityutskiy return ret + 1; 500801c135cSArtem B. Bityutskiy } 501801c135cSArtem B. Bityutskiy 502801c135cSArtem B. Bityutskiy /** 503801c135cSArtem B. Bityutskiy * ubi_io_is_bad - check if a physical eraseblock is bad. 504801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 505801c135cSArtem B. Bityutskiy * @pnum: the physical eraseblock number to check 506801c135cSArtem B. Bityutskiy * 507801c135cSArtem B. Bityutskiy * This function returns a positive number if the physical eraseblock is bad, 508801c135cSArtem B. Bityutskiy * zero if not, and a negative error code if an error occurred. 509801c135cSArtem B. Bityutskiy */ 510801c135cSArtem B. Bityutskiy int ubi_io_is_bad(const struct ubi_device *ubi, int pnum) 511801c135cSArtem B. Bityutskiy { 512801c135cSArtem B. Bityutskiy struct mtd_info *mtd = ubi->mtd; 513801c135cSArtem B. Bityutskiy 514801c135cSArtem B. Bityutskiy ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 515801c135cSArtem B. Bityutskiy 516801c135cSArtem B. Bityutskiy if (ubi->bad_allowed) { 517801c135cSArtem B. Bityutskiy int ret; 518801c135cSArtem B. Bityutskiy 519801c135cSArtem B. Bityutskiy ret = mtd->block_isbad(mtd, (loff_t)pnum * ubi->peb_size); 520801c135cSArtem B. Bityutskiy if (ret < 0) 521801c135cSArtem B. Bityutskiy ubi_err("error %d while checking if PEB %d is bad", 522801c135cSArtem B. Bityutskiy ret, pnum); 523801c135cSArtem B. Bityutskiy else if (ret) 524801c135cSArtem B. Bityutskiy dbg_io("PEB %d is bad", pnum); 525801c135cSArtem B. Bityutskiy return ret; 526801c135cSArtem B. Bityutskiy } 527801c135cSArtem B. Bityutskiy 528801c135cSArtem B. Bityutskiy return 0; 529801c135cSArtem B. Bityutskiy } 530801c135cSArtem B. Bityutskiy 531801c135cSArtem B. Bityutskiy /** 532801c135cSArtem B. Bityutskiy * ubi_io_mark_bad - mark a physical eraseblock as bad. 533801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 534801c135cSArtem B. Bityutskiy * @pnum: the physical eraseblock number to mark 535801c135cSArtem B. Bityutskiy * 536801c135cSArtem B. Bityutskiy * This function returns zero in case of success and a negative error code in 537801c135cSArtem B. Bityutskiy * case of failure. 538801c135cSArtem B. Bityutskiy */ 539801c135cSArtem B. Bityutskiy int ubi_io_mark_bad(const struct ubi_device *ubi, int pnum) 540801c135cSArtem B. Bityutskiy { 541801c135cSArtem B. Bityutskiy int err; 542801c135cSArtem B. Bityutskiy struct mtd_info *mtd = ubi->mtd; 543801c135cSArtem B. Bityutskiy 544801c135cSArtem B. Bityutskiy ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 545801c135cSArtem B. Bityutskiy 546801c135cSArtem B. Bityutskiy if (ubi->ro_mode) { 547801c135cSArtem B. Bityutskiy ubi_err("read-only mode"); 548801c135cSArtem B. Bityutskiy return -EROFS; 549801c135cSArtem B. Bityutskiy } 550801c135cSArtem B. Bityutskiy 551801c135cSArtem B. Bityutskiy if (!ubi->bad_allowed) 552801c135cSArtem B. Bityutskiy return 0; 553801c135cSArtem B. Bityutskiy 554801c135cSArtem B. Bityutskiy err = mtd->block_markbad(mtd, (loff_t)pnum * ubi->peb_size); 555801c135cSArtem B. Bityutskiy if (err) 556801c135cSArtem B. Bityutskiy ubi_err("cannot mark PEB %d bad, error %d", pnum, err); 557801c135cSArtem B. Bityutskiy return err; 558801c135cSArtem B. Bityutskiy } 559801c135cSArtem B. Bityutskiy 560801c135cSArtem B. Bityutskiy /** 561801c135cSArtem B. Bityutskiy * validate_ec_hdr - validate an erase counter header. 562801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 563801c135cSArtem B. Bityutskiy * @ec_hdr: the erase counter header to check 564801c135cSArtem B. Bityutskiy * 565801c135cSArtem B. Bityutskiy * This function returns zero if the erase counter header is OK, and %1 if 566801c135cSArtem B. Bityutskiy * not. 567801c135cSArtem B. Bityutskiy */ 568801c135cSArtem B. Bityutskiy static int validate_ec_hdr(const struct ubi_device *ubi, 569801c135cSArtem B. Bityutskiy const struct ubi_ec_hdr *ec_hdr) 570801c135cSArtem B. Bityutskiy { 571801c135cSArtem B. Bityutskiy long long ec; 572801c135cSArtem B. Bityutskiy int vid_hdr_offset, leb_start; 573801c135cSArtem B. Bityutskiy 5743261ebd7SChristoph Hellwig ec = be64_to_cpu(ec_hdr->ec); 5753261ebd7SChristoph Hellwig vid_hdr_offset = be32_to_cpu(ec_hdr->vid_hdr_offset); 5763261ebd7SChristoph Hellwig leb_start = be32_to_cpu(ec_hdr->data_offset); 577801c135cSArtem B. Bityutskiy 578801c135cSArtem B. Bityutskiy if (ec_hdr->version != UBI_VERSION) { 579801c135cSArtem B. Bityutskiy ubi_err("node with incompatible UBI version found: " 580801c135cSArtem B. Bityutskiy "this UBI version is %d, image version is %d", 581801c135cSArtem B. Bityutskiy UBI_VERSION, (int)ec_hdr->version); 582801c135cSArtem B. Bityutskiy goto bad; 583801c135cSArtem B. Bityutskiy } 584801c135cSArtem B. Bityutskiy 585801c135cSArtem B. Bityutskiy if (vid_hdr_offset != ubi->vid_hdr_offset) { 586801c135cSArtem B. Bityutskiy ubi_err("bad VID header offset %d, expected %d", 587801c135cSArtem B. Bityutskiy vid_hdr_offset, ubi->vid_hdr_offset); 588801c135cSArtem B. Bityutskiy goto bad; 589801c135cSArtem B. Bityutskiy } 590801c135cSArtem B. Bityutskiy 591801c135cSArtem B. Bityutskiy if (leb_start != ubi->leb_start) { 592801c135cSArtem B. Bityutskiy ubi_err("bad data offset %d, expected %d", 593801c135cSArtem B. Bityutskiy leb_start, ubi->leb_start); 594801c135cSArtem B. Bityutskiy goto bad; 595801c135cSArtem B. Bityutskiy } 596801c135cSArtem B. Bityutskiy 597801c135cSArtem B. Bityutskiy if (ec < 0 || ec > UBI_MAX_ERASECOUNTER) { 598801c135cSArtem B. Bityutskiy ubi_err("bad erase counter %lld", ec); 599801c135cSArtem B. Bityutskiy goto bad; 600801c135cSArtem B. Bityutskiy } 601801c135cSArtem B. Bityutskiy 602801c135cSArtem B. Bityutskiy return 0; 603801c135cSArtem B. Bityutskiy 604801c135cSArtem B. Bityutskiy bad: 605801c135cSArtem B. Bityutskiy ubi_err("bad EC header"); 606801c135cSArtem B. Bityutskiy ubi_dbg_dump_ec_hdr(ec_hdr); 607801c135cSArtem B. Bityutskiy ubi_dbg_dump_stack(); 608801c135cSArtem B. Bityutskiy return 1; 609801c135cSArtem B. Bityutskiy } 610801c135cSArtem B. Bityutskiy 611801c135cSArtem B. Bityutskiy /** 612801c135cSArtem B. Bityutskiy * ubi_io_read_ec_hdr - read and check an erase counter header. 613801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 614801c135cSArtem B. Bityutskiy * @pnum: physical eraseblock to read from 615801c135cSArtem B. Bityutskiy * @ec_hdr: a &struct ubi_ec_hdr object where to store the read erase counter 616801c135cSArtem B. Bityutskiy * header 617801c135cSArtem B. Bityutskiy * @verbose: be verbose if the header is corrupted or was not found 618801c135cSArtem B. Bityutskiy * 619801c135cSArtem B. Bityutskiy * This function reads erase counter header from physical eraseblock @pnum and 620801c135cSArtem B. Bityutskiy * stores it in @ec_hdr. This function also checks CRC checksum of the read 621801c135cSArtem B. Bityutskiy * erase counter header. The following codes may be returned: 622801c135cSArtem B. Bityutskiy * 623801c135cSArtem B. Bityutskiy * o %0 if the CRC checksum is correct and the header was successfully read; 624801c135cSArtem B. Bityutskiy * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected 625801c135cSArtem B. Bityutskiy * and corrected by the flash driver; this is harmless but may indicate that 626801c135cSArtem B. Bityutskiy * this eraseblock may become bad soon (but may be not); 627801c135cSArtem B. Bityutskiy * o %UBI_IO_BAD_EC_HDR if the erase counter header is corrupted (a CRC error); 628801c135cSArtem B. Bityutskiy * o %UBI_IO_PEB_EMPTY if the physical eraseblock is empty; 629801c135cSArtem B. Bityutskiy * o a negative error code in case of failure. 630801c135cSArtem B. Bityutskiy */ 631e88d6e10SArtem Bityutskiy int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum, 632801c135cSArtem B. Bityutskiy struct ubi_ec_hdr *ec_hdr, int verbose) 633801c135cSArtem B. Bityutskiy { 634801c135cSArtem B. Bityutskiy int err, read_err = 0; 635801c135cSArtem B. Bityutskiy uint32_t crc, magic, hdr_crc; 636801c135cSArtem B. Bityutskiy 637801c135cSArtem B. Bityutskiy dbg_io("read EC header from PEB %d", pnum); 638801c135cSArtem B. Bityutskiy ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 639801c135cSArtem B. Bityutskiy 640801c135cSArtem B. Bityutskiy err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE); 641801c135cSArtem B. Bityutskiy if (err) { 642801c135cSArtem B. Bityutskiy if (err != UBI_IO_BITFLIPS && err != -EBADMSG) 643801c135cSArtem B. Bityutskiy return err; 644801c135cSArtem B. Bityutskiy 645801c135cSArtem B. Bityutskiy /* 646801c135cSArtem B. Bityutskiy * We read all the data, but either a correctable bit-flip 647801c135cSArtem B. Bityutskiy * occurred, or MTD reported about some data integrity error, 648801c135cSArtem B. Bityutskiy * like an ECC error in case of NAND. The former is harmless, 649801c135cSArtem B. Bityutskiy * the later may mean that the read data is corrupted. But we 650801c135cSArtem B. Bityutskiy * have a CRC check-sum and we will detect this. If the EC 651801c135cSArtem B. Bityutskiy * header is still OK, we just report this as there was a 652801c135cSArtem B. Bityutskiy * bit-flip. 653801c135cSArtem B. Bityutskiy */ 654801c135cSArtem B. Bityutskiy read_err = err; 655801c135cSArtem B. Bityutskiy } 656801c135cSArtem B. Bityutskiy 6573261ebd7SChristoph Hellwig magic = be32_to_cpu(ec_hdr->magic); 658801c135cSArtem B. Bityutskiy if (magic != UBI_EC_HDR_MAGIC) { 659801c135cSArtem B. Bityutskiy /* 660801c135cSArtem B. Bityutskiy * The magic field is wrong. Let's check if we have read all 661801c135cSArtem B. Bityutskiy * 0xFF. If yes, this physical eraseblock is assumed to be 662801c135cSArtem B. Bityutskiy * empty. 663801c135cSArtem B. Bityutskiy * 664801c135cSArtem B. Bityutskiy * But if there was a read error, we do not test it for all 665801c135cSArtem B. Bityutskiy * 0xFFs. Even if it does contain all 0xFFs, this error 666801c135cSArtem B. Bityutskiy * indicates that something is still wrong with this physical 667801c135cSArtem B. Bityutskiy * eraseblock and we anyway cannot treat it as empty. 668801c135cSArtem B. Bityutskiy */ 669801c135cSArtem B. Bityutskiy if (read_err != -EBADMSG && 670801c135cSArtem B. Bityutskiy check_pattern(ec_hdr, 0xFF, UBI_EC_HDR_SIZE)) { 671801c135cSArtem B. Bityutskiy /* The physical eraseblock is supposedly empty */ 67240a71a87SArtem Bityutskiy err = ubi_dbg_check_all_ff(ubi, pnum, 0, ubi->peb_size); 673801c135cSArtem B. Bityutskiy if (err) 674801c135cSArtem B. Bityutskiy return err > 0 ? UBI_IO_BAD_EC_HDR : err; 675801c135cSArtem B. Bityutskiy 676801c135cSArtem B. Bityutskiy if (verbose) 677801c135cSArtem B. Bityutskiy ubi_warn("no EC header found at PEB %d, " 678801c135cSArtem B. Bityutskiy "only 0xFF bytes", pnum); 679ed45819fSArtem Bityutskiy else if (UBI_IO_DEBUG) 680ed45819fSArtem Bityutskiy dbg_msg("no EC header found at PEB %d, " 681ed45819fSArtem Bityutskiy "only 0xFF bytes", pnum); 682801c135cSArtem B. Bityutskiy return UBI_IO_PEB_EMPTY; 683801c135cSArtem B. Bityutskiy } 684801c135cSArtem B. Bityutskiy 685801c135cSArtem B. Bityutskiy /* 686801c135cSArtem B. Bityutskiy * This is not a valid erase counter header, and these are not 687801c135cSArtem B. Bityutskiy * 0xFF bytes. Report that the header is corrupted. 688801c135cSArtem B. Bityutskiy */ 689801c135cSArtem B. Bityutskiy if (verbose) { 690801c135cSArtem B. Bityutskiy ubi_warn("bad magic number at PEB %d: %08x instead of " 691801c135cSArtem B. Bityutskiy "%08x", pnum, magic, UBI_EC_HDR_MAGIC); 692801c135cSArtem B. Bityutskiy ubi_dbg_dump_ec_hdr(ec_hdr); 693ed45819fSArtem Bityutskiy } else if (UBI_IO_DEBUG) 694ed45819fSArtem Bityutskiy dbg_msg("bad magic number at PEB %d: %08x instead of " 695ed45819fSArtem Bityutskiy "%08x", pnum, magic, UBI_EC_HDR_MAGIC); 696801c135cSArtem B. Bityutskiy return UBI_IO_BAD_EC_HDR; 697801c135cSArtem B. Bityutskiy } 698801c135cSArtem B. Bityutskiy 699801c135cSArtem B. Bityutskiy crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC); 7003261ebd7SChristoph Hellwig hdr_crc = be32_to_cpu(ec_hdr->hdr_crc); 701801c135cSArtem B. Bityutskiy 702801c135cSArtem B. Bityutskiy if (hdr_crc != crc) { 703801c135cSArtem B. Bityutskiy if (verbose) { 7049c9ec147SArtem Bityutskiy ubi_warn("bad EC header CRC at PEB %d, calculated " 7059c9ec147SArtem Bityutskiy "%#08x, read %#08x", pnum, crc, hdr_crc); 706801c135cSArtem B. Bityutskiy ubi_dbg_dump_ec_hdr(ec_hdr); 707ed45819fSArtem Bityutskiy } else if (UBI_IO_DEBUG) 708ed45819fSArtem Bityutskiy dbg_msg("bad EC header CRC at PEB %d, calculated " 709ed45819fSArtem Bityutskiy "%#08x, read %#08x", pnum, crc, hdr_crc); 710801c135cSArtem B. Bityutskiy return UBI_IO_BAD_EC_HDR; 711801c135cSArtem B. Bityutskiy } 712801c135cSArtem B. Bityutskiy 713801c135cSArtem B. Bityutskiy /* And of course validate what has just been read from the media */ 714801c135cSArtem B. Bityutskiy err = validate_ec_hdr(ubi, ec_hdr); 715801c135cSArtem B. Bityutskiy if (err) { 716801c135cSArtem B. Bityutskiy ubi_err("validation failed for PEB %d", pnum); 717801c135cSArtem B. Bityutskiy return -EINVAL; 718801c135cSArtem B. Bityutskiy } 719801c135cSArtem B. Bityutskiy 720801c135cSArtem B. Bityutskiy return read_err ? UBI_IO_BITFLIPS : 0; 721801c135cSArtem B. Bityutskiy } 722801c135cSArtem B. Bityutskiy 723801c135cSArtem B. Bityutskiy /** 724801c135cSArtem B. Bityutskiy * ubi_io_write_ec_hdr - write an erase counter header. 725801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 726801c135cSArtem B. Bityutskiy * @pnum: physical eraseblock to write to 727801c135cSArtem B. Bityutskiy * @ec_hdr: the erase counter header to write 728801c135cSArtem B. Bityutskiy * 729801c135cSArtem B. Bityutskiy * This function writes erase counter header described by @ec_hdr to physical 730801c135cSArtem B. Bityutskiy * eraseblock @pnum. It also fills most fields of @ec_hdr before writing, so 731801c135cSArtem B. Bityutskiy * the caller do not have to fill them. Callers must only fill the @ec_hdr->ec 732801c135cSArtem B. Bityutskiy * field. 733801c135cSArtem B. Bityutskiy * 734801c135cSArtem B. Bityutskiy * This function returns zero in case of success and a negative error code in 735801c135cSArtem B. Bityutskiy * case of failure. If %-EIO is returned, the physical eraseblock most probably 736801c135cSArtem B. Bityutskiy * went bad. 737801c135cSArtem B. Bityutskiy */ 738e88d6e10SArtem Bityutskiy int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum, 739801c135cSArtem B. Bityutskiy struct ubi_ec_hdr *ec_hdr) 740801c135cSArtem B. Bityutskiy { 741801c135cSArtem B. Bityutskiy int err; 742801c135cSArtem B. Bityutskiy uint32_t crc; 743801c135cSArtem B. Bityutskiy 744801c135cSArtem B. Bityutskiy dbg_io("write EC header to PEB %d", pnum); 745801c135cSArtem B. Bityutskiy ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 746801c135cSArtem B. Bityutskiy 7473261ebd7SChristoph Hellwig ec_hdr->magic = cpu_to_be32(UBI_EC_HDR_MAGIC); 748801c135cSArtem B. Bityutskiy ec_hdr->version = UBI_VERSION; 7493261ebd7SChristoph Hellwig ec_hdr->vid_hdr_offset = cpu_to_be32(ubi->vid_hdr_offset); 7503261ebd7SChristoph Hellwig ec_hdr->data_offset = cpu_to_be32(ubi->leb_start); 751801c135cSArtem B. Bityutskiy crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC); 7523261ebd7SChristoph Hellwig ec_hdr->hdr_crc = cpu_to_be32(crc); 753801c135cSArtem B. Bityutskiy 754801c135cSArtem B. Bityutskiy err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr); 755801c135cSArtem B. Bityutskiy if (err) 756801c135cSArtem B. Bityutskiy return -EINVAL; 757801c135cSArtem B. Bityutskiy 758801c135cSArtem B. Bityutskiy err = ubi_io_write(ubi, ec_hdr, pnum, 0, ubi->ec_hdr_alsize); 759801c135cSArtem B. Bityutskiy return err; 760801c135cSArtem B. Bityutskiy } 761801c135cSArtem B. Bityutskiy 762801c135cSArtem B. Bityutskiy /** 763801c135cSArtem B. Bityutskiy * validate_vid_hdr - validate a volume identifier header. 764801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 765801c135cSArtem B. Bityutskiy * @vid_hdr: the volume identifier header to check 766801c135cSArtem B. Bityutskiy * 767801c135cSArtem B. Bityutskiy * This function checks that data stored in the volume identifier header 768801c135cSArtem B. Bityutskiy * @vid_hdr. Returns zero if the VID header is OK and %1 if not. 769801c135cSArtem B. Bityutskiy */ 770801c135cSArtem B. Bityutskiy static int validate_vid_hdr(const struct ubi_device *ubi, 771801c135cSArtem B. Bityutskiy const struct ubi_vid_hdr *vid_hdr) 772801c135cSArtem B. Bityutskiy { 773801c135cSArtem B. Bityutskiy int vol_type = vid_hdr->vol_type; 774801c135cSArtem B. Bityutskiy int copy_flag = vid_hdr->copy_flag; 7753261ebd7SChristoph Hellwig int vol_id = be32_to_cpu(vid_hdr->vol_id); 7763261ebd7SChristoph Hellwig int lnum = be32_to_cpu(vid_hdr->lnum); 777801c135cSArtem B. Bityutskiy int compat = vid_hdr->compat; 7783261ebd7SChristoph Hellwig int data_size = be32_to_cpu(vid_hdr->data_size); 7793261ebd7SChristoph Hellwig int used_ebs = be32_to_cpu(vid_hdr->used_ebs); 7803261ebd7SChristoph Hellwig int data_pad = be32_to_cpu(vid_hdr->data_pad); 7813261ebd7SChristoph Hellwig int data_crc = be32_to_cpu(vid_hdr->data_crc); 782801c135cSArtem B. Bityutskiy int usable_leb_size = ubi->leb_size - data_pad; 783801c135cSArtem B. Bityutskiy 784801c135cSArtem B. Bityutskiy if (copy_flag != 0 && copy_flag != 1) { 785801c135cSArtem B. Bityutskiy dbg_err("bad copy_flag"); 786801c135cSArtem B. Bityutskiy goto bad; 787801c135cSArtem B. Bityutskiy } 788801c135cSArtem B. Bityutskiy 789801c135cSArtem B. Bityutskiy if (vol_id < 0 || lnum < 0 || data_size < 0 || used_ebs < 0 || 790801c135cSArtem B. Bityutskiy data_pad < 0) { 791801c135cSArtem B. Bityutskiy dbg_err("negative values"); 792801c135cSArtem B. Bityutskiy goto bad; 793801c135cSArtem B. Bityutskiy } 794801c135cSArtem B. Bityutskiy 795801c135cSArtem B. Bityutskiy if (vol_id >= UBI_MAX_VOLUMES && vol_id < UBI_INTERNAL_VOL_START) { 796801c135cSArtem B. Bityutskiy dbg_err("bad vol_id"); 797801c135cSArtem B. Bityutskiy goto bad; 798801c135cSArtem B. Bityutskiy } 799801c135cSArtem B. Bityutskiy 800801c135cSArtem B. Bityutskiy if (vol_id < UBI_INTERNAL_VOL_START && compat != 0) { 801801c135cSArtem B. Bityutskiy dbg_err("bad compat"); 802801c135cSArtem B. Bityutskiy goto bad; 803801c135cSArtem B. Bityutskiy } 804801c135cSArtem B. Bityutskiy 805801c135cSArtem B. Bityutskiy if (vol_id >= UBI_INTERNAL_VOL_START && compat != UBI_COMPAT_DELETE && 806801c135cSArtem B. Bityutskiy compat != UBI_COMPAT_RO && compat != UBI_COMPAT_PRESERVE && 807801c135cSArtem B. Bityutskiy compat != UBI_COMPAT_REJECT) { 808801c135cSArtem B. Bityutskiy dbg_err("bad compat"); 809801c135cSArtem B. Bityutskiy goto bad; 810801c135cSArtem B. Bityutskiy } 811801c135cSArtem B. Bityutskiy 812801c135cSArtem B. Bityutskiy if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) { 813801c135cSArtem B. Bityutskiy dbg_err("bad vol_type"); 814801c135cSArtem B. Bityutskiy goto bad; 815801c135cSArtem B. Bityutskiy } 816801c135cSArtem B. Bityutskiy 817801c135cSArtem B. Bityutskiy if (data_pad >= ubi->leb_size / 2) { 818801c135cSArtem B. Bityutskiy dbg_err("bad data_pad"); 819801c135cSArtem B. Bityutskiy goto bad; 820801c135cSArtem B. Bityutskiy } 821801c135cSArtem B. Bityutskiy 822801c135cSArtem B. Bityutskiy if (vol_type == UBI_VID_STATIC) { 823801c135cSArtem B. Bityutskiy /* 824801c135cSArtem B. Bityutskiy * Although from high-level point of view static volumes may 825801c135cSArtem B. Bityutskiy * contain zero bytes of data, but no VID headers can contain 826801c135cSArtem B. Bityutskiy * zero at these fields, because they empty volumes do not have 827801c135cSArtem B. Bityutskiy * mapped logical eraseblocks. 828801c135cSArtem B. Bityutskiy */ 829801c135cSArtem B. Bityutskiy if (used_ebs == 0) { 830801c135cSArtem B. Bityutskiy dbg_err("zero used_ebs"); 831801c135cSArtem B. Bityutskiy goto bad; 832801c135cSArtem B. Bityutskiy } 833801c135cSArtem B. Bityutskiy if (data_size == 0) { 834801c135cSArtem B. Bityutskiy dbg_err("zero data_size"); 835801c135cSArtem B. Bityutskiy goto bad; 836801c135cSArtem B. Bityutskiy } 837801c135cSArtem B. Bityutskiy if (lnum < used_ebs - 1) { 838801c135cSArtem B. Bityutskiy if (data_size != usable_leb_size) { 839801c135cSArtem B. Bityutskiy dbg_err("bad data_size"); 840801c135cSArtem B. Bityutskiy goto bad; 841801c135cSArtem B. Bityutskiy } 842801c135cSArtem B. Bityutskiy } else if (lnum == used_ebs - 1) { 843801c135cSArtem B. Bityutskiy if (data_size == 0) { 844801c135cSArtem B. Bityutskiy dbg_err("bad data_size at last LEB"); 845801c135cSArtem B. Bityutskiy goto bad; 846801c135cSArtem B. Bityutskiy } 847801c135cSArtem B. Bityutskiy } else { 848801c135cSArtem B. Bityutskiy dbg_err("too high lnum"); 849801c135cSArtem B. Bityutskiy goto bad; 850801c135cSArtem B. Bityutskiy } 851801c135cSArtem B. Bityutskiy } else { 852801c135cSArtem B. Bityutskiy if (copy_flag == 0) { 853801c135cSArtem B. Bityutskiy if (data_crc != 0) { 854801c135cSArtem B. Bityutskiy dbg_err("non-zero data CRC"); 855801c135cSArtem B. Bityutskiy goto bad; 856801c135cSArtem B. Bityutskiy } 857801c135cSArtem B. Bityutskiy if (data_size != 0) { 858801c135cSArtem B. Bityutskiy dbg_err("non-zero data_size"); 859801c135cSArtem B. Bityutskiy goto bad; 860801c135cSArtem B. Bityutskiy } 861801c135cSArtem B. Bityutskiy } else { 862801c135cSArtem B. Bityutskiy if (data_size == 0) { 863801c135cSArtem B. Bityutskiy dbg_err("zero data_size of copy"); 864801c135cSArtem B. Bityutskiy goto bad; 865801c135cSArtem B. Bityutskiy } 866801c135cSArtem B. Bityutskiy } 867801c135cSArtem B. Bityutskiy if (used_ebs != 0) { 868801c135cSArtem B. Bityutskiy dbg_err("bad used_ebs"); 869801c135cSArtem B. Bityutskiy goto bad; 870801c135cSArtem B. Bityutskiy } 871801c135cSArtem B. Bityutskiy } 872801c135cSArtem B. Bityutskiy 873801c135cSArtem B. Bityutskiy return 0; 874801c135cSArtem B. Bityutskiy 875801c135cSArtem B. Bityutskiy bad: 876801c135cSArtem B. Bityutskiy ubi_err("bad VID header"); 877801c135cSArtem B. Bityutskiy ubi_dbg_dump_vid_hdr(vid_hdr); 878801c135cSArtem B. Bityutskiy ubi_dbg_dump_stack(); 879801c135cSArtem B. Bityutskiy return 1; 880801c135cSArtem B. Bityutskiy } 881801c135cSArtem B. Bityutskiy 882801c135cSArtem B. Bityutskiy /** 883801c135cSArtem B. Bityutskiy * ubi_io_read_vid_hdr - read and check a volume identifier header. 884801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 885801c135cSArtem B. Bityutskiy * @pnum: physical eraseblock number to read from 886801c135cSArtem B. Bityutskiy * @vid_hdr: &struct ubi_vid_hdr object where to store the read volume 887801c135cSArtem B. Bityutskiy * identifier header 888801c135cSArtem B. Bityutskiy * @verbose: be verbose if the header is corrupted or wasn't found 889801c135cSArtem B. Bityutskiy * 890801c135cSArtem B. Bityutskiy * This function reads the volume identifier header from physical eraseblock 891801c135cSArtem B. Bityutskiy * @pnum and stores it in @vid_hdr. It also checks CRC checksum of the read 892801c135cSArtem B. Bityutskiy * volume identifier header. The following codes may be returned: 893801c135cSArtem B. Bityutskiy * 894801c135cSArtem B. Bityutskiy * o %0 if the CRC checksum is correct and the header was successfully read; 895801c135cSArtem B. Bityutskiy * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected 896801c135cSArtem B. Bityutskiy * and corrected by the flash driver; this is harmless but may indicate that 897801c135cSArtem B. Bityutskiy * this eraseblock may become bad soon; 898815bc5f8SArtem Bityutskiy * o %UBI_IO_BAD_VID_HDR if the volume identifier header is corrupted (a CRC 899801c135cSArtem B. Bityutskiy * error detected); 900801c135cSArtem B. Bityutskiy * o %UBI_IO_PEB_FREE if the physical eraseblock is free (i.e., there is no VID 901801c135cSArtem B. Bityutskiy * header there); 902801c135cSArtem B. Bityutskiy * o a negative error code in case of failure. 903801c135cSArtem B. Bityutskiy */ 904e88d6e10SArtem Bityutskiy int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum, 905801c135cSArtem B. Bityutskiy struct ubi_vid_hdr *vid_hdr, int verbose) 906801c135cSArtem B. Bityutskiy { 907801c135cSArtem B. Bityutskiy int err, read_err = 0; 908801c135cSArtem B. Bityutskiy uint32_t crc, magic, hdr_crc; 909801c135cSArtem B. Bityutskiy void *p; 910801c135cSArtem B. Bityutskiy 911801c135cSArtem B. Bityutskiy dbg_io("read VID header from PEB %d", pnum); 912801c135cSArtem B. Bityutskiy ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 913801c135cSArtem B. Bityutskiy 914801c135cSArtem B. Bityutskiy p = (char *)vid_hdr - ubi->vid_hdr_shift; 915801c135cSArtem B. Bityutskiy err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset, 916801c135cSArtem B. Bityutskiy ubi->vid_hdr_alsize); 917801c135cSArtem B. Bityutskiy if (err) { 918801c135cSArtem B. Bityutskiy if (err != UBI_IO_BITFLIPS && err != -EBADMSG) 919801c135cSArtem B. Bityutskiy return err; 920801c135cSArtem B. Bityutskiy 921801c135cSArtem B. Bityutskiy /* 922801c135cSArtem B. Bityutskiy * We read all the data, but either a correctable bit-flip 923801c135cSArtem B. Bityutskiy * occurred, or MTD reported about some data integrity error, 924801c135cSArtem B. Bityutskiy * like an ECC error in case of NAND. The former is harmless, 925801c135cSArtem B. Bityutskiy * the later may mean the read data is corrupted. But we have a 926801c135cSArtem B. Bityutskiy * CRC check-sum and we will identify this. If the VID header is 927801c135cSArtem B. Bityutskiy * still OK, we just report this as there was a bit-flip. 928801c135cSArtem B. Bityutskiy */ 929801c135cSArtem B. Bityutskiy read_err = err; 930801c135cSArtem B. Bityutskiy } 931801c135cSArtem B. Bityutskiy 9323261ebd7SChristoph Hellwig magic = be32_to_cpu(vid_hdr->magic); 933801c135cSArtem B. Bityutskiy if (magic != UBI_VID_HDR_MAGIC) { 934801c135cSArtem B. Bityutskiy /* 935801c135cSArtem B. Bityutskiy * If we have read all 0xFF bytes, the VID header probably does 936801c135cSArtem B. Bityutskiy * not exist and the physical eraseblock is assumed to be free. 937801c135cSArtem B. Bityutskiy * 938801c135cSArtem B. Bityutskiy * But if there was a read error, we do not test the data for 939801c135cSArtem B. Bityutskiy * 0xFFs. Even if it does contain all 0xFFs, this error 940801c135cSArtem B. Bityutskiy * indicates that something is still wrong with this physical 941801c135cSArtem B. Bityutskiy * eraseblock and it cannot be regarded as free. 942801c135cSArtem B. Bityutskiy */ 943801c135cSArtem B. Bityutskiy if (read_err != -EBADMSG && 944801c135cSArtem B. Bityutskiy check_pattern(vid_hdr, 0xFF, UBI_VID_HDR_SIZE)) { 945801c135cSArtem B. Bityutskiy /* The physical eraseblock is supposedly free */ 946801c135cSArtem B. Bityutskiy 947801c135cSArtem B. Bityutskiy /* 948801c135cSArtem B. Bityutskiy * The below is just a paranoid check, it has to be 949801c135cSArtem B. Bityutskiy * compiled out if paranoid checks are disabled. 950801c135cSArtem B. Bityutskiy */ 951ffb6b7e4SArtem Bityutskiy err = paranoid_check_empty(ubi, pnum); 952801c135cSArtem B. Bityutskiy if (err) 953801c135cSArtem B. Bityutskiy return err > 0 ? UBI_IO_BAD_VID_HDR : err; 954801c135cSArtem B. Bityutskiy 955801c135cSArtem B. Bityutskiy if (verbose) 956801c135cSArtem B. Bityutskiy ubi_warn("no VID header found at PEB %d, " 957801c135cSArtem B. Bityutskiy "only 0xFF bytes", pnum); 958ed45819fSArtem Bityutskiy else if (UBI_IO_DEBUG) 959ed45819fSArtem Bityutskiy dbg_msg("no VID header found at PEB %d, " 960ed45819fSArtem Bityutskiy "only 0xFF bytes", pnum); 961801c135cSArtem B. Bityutskiy return UBI_IO_PEB_FREE; 962801c135cSArtem B. Bityutskiy } 963801c135cSArtem B. Bityutskiy 964801c135cSArtem B. Bityutskiy /* 965801c135cSArtem B. Bityutskiy * This is not a valid VID header, and these are not 0xFF 966801c135cSArtem B. Bityutskiy * bytes. Report that the header is corrupted. 967801c135cSArtem B. Bityutskiy */ 968801c135cSArtem B. Bityutskiy if (verbose) { 969801c135cSArtem B. Bityutskiy ubi_warn("bad magic number at PEB %d: %08x instead of " 970801c135cSArtem B. Bityutskiy "%08x", pnum, magic, UBI_VID_HDR_MAGIC); 971801c135cSArtem B. Bityutskiy ubi_dbg_dump_vid_hdr(vid_hdr); 972ed45819fSArtem Bityutskiy } else if (UBI_IO_DEBUG) 973ed45819fSArtem Bityutskiy dbg_msg("bad magic number at PEB %d: %08x instead of " 974ed45819fSArtem Bityutskiy "%08x", pnum, magic, UBI_VID_HDR_MAGIC); 975801c135cSArtem B. Bityutskiy return UBI_IO_BAD_VID_HDR; 976801c135cSArtem B. Bityutskiy } 977801c135cSArtem B. Bityutskiy 978801c135cSArtem B. Bityutskiy crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC); 9793261ebd7SChristoph Hellwig hdr_crc = be32_to_cpu(vid_hdr->hdr_crc); 980801c135cSArtem B. Bityutskiy 981801c135cSArtem B. Bityutskiy if (hdr_crc != crc) { 982801c135cSArtem B. Bityutskiy if (verbose) { 983801c135cSArtem B. Bityutskiy ubi_warn("bad CRC at PEB %d, calculated %#08x, " 984801c135cSArtem B. Bityutskiy "read %#08x", pnum, crc, hdr_crc); 985801c135cSArtem B. Bityutskiy ubi_dbg_dump_vid_hdr(vid_hdr); 986ed45819fSArtem Bityutskiy } else if (UBI_IO_DEBUG) 987ed45819fSArtem Bityutskiy dbg_msg("bad CRC at PEB %d, calculated %#08x, " 988ed45819fSArtem Bityutskiy "read %#08x", pnum, crc, hdr_crc); 989801c135cSArtem B. Bityutskiy return UBI_IO_BAD_VID_HDR; 990801c135cSArtem B. Bityutskiy } 991801c135cSArtem B. Bityutskiy 992801c135cSArtem B. Bityutskiy /* Validate the VID header that we have just read */ 993801c135cSArtem B. Bityutskiy err = validate_vid_hdr(ubi, vid_hdr); 994801c135cSArtem B. Bityutskiy if (err) { 995801c135cSArtem B. Bityutskiy ubi_err("validation failed for PEB %d", pnum); 996801c135cSArtem B. Bityutskiy return -EINVAL; 997801c135cSArtem B. Bityutskiy } 998801c135cSArtem B. Bityutskiy 999801c135cSArtem B. Bityutskiy return read_err ? UBI_IO_BITFLIPS : 0; 1000801c135cSArtem B. Bityutskiy } 1001801c135cSArtem B. Bityutskiy 1002801c135cSArtem B. Bityutskiy /** 1003801c135cSArtem B. Bityutskiy * ubi_io_write_vid_hdr - write a volume identifier header. 1004801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 1005801c135cSArtem B. Bityutskiy * @pnum: the physical eraseblock number to write to 1006801c135cSArtem B. Bityutskiy * @vid_hdr: the volume identifier header to write 1007801c135cSArtem B. Bityutskiy * 1008801c135cSArtem B. Bityutskiy * This function writes the volume identifier header described by @vid_hdr to 1009801c135cSArtem B. Bityutskiy * physical eraseblock @pnum. This function automatically fills the 1010801c135cSArtem B. Bityutskiy * @vid_hdr->magic and the @vid_hdr->version fields, as well as calculates 1011801c135cSArtem B. Bityutskiy * header CRC checksum and stores it at vid_hdr->hdr_crc. 1012801c135cSArtem B. Bityutskiy * 1013801c135cSArtem B. Bityutskiy * This function returns zero in case of success and a negative error code in 1014801c135cSArtem B. Bityutskiy * case of failure. If %-EIO is returned, the physical eraseblock probably went 1015801c135cSArtem B. Bityutskiy * bad. 1016801c135cSArtem B. Bityutskiy */ 1017e88d6e10SArtem Bityutskiy int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum, 1018801c135cSArtem B. Bityutskiy struct ubi_vid_hdr *vid_hdr) 1019801c135cSArtem B. Bityutskiy { 1020801c135cSArtem B. Bityutskiy int err; 1021801c135cSArtem B. Bityutskiy uint32_t crc; 1022801c135cSArtem B. Bityutskiy void *p; 1023801c135cSArtem B. Bityutskiy 1024801c135cSArtem B. Bityutskiy dbg_io("write VID header to PEB %d", pnum); 1025801c135cSArtem B. Bityutskiy ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 1026801c135cSArtem B. Bityutskiy 1027801c135cSArtem B. Bityutskiy err = paranoid_check_peb_ec_hdr(ubi, pnum); 1028801c135cSArtem B. Bityutskiy if (err) 1029801c135cSArtem B. Bityutskiy return err > 0 ? -EINVAL : err; 1030801c135cSArtem B. Bityutskiy 10313261ebd7SChristoph Hellwig vid_hdr->magic = cpu_to_be32(UBI_VID_HDR_MAGIC); 1032801c135cSArtem B. Bityutskiy vid_hdr->version = UBI_VERSION; 1033801c135cSArtem B. Bityutskiy crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC); 10343261ebd7SChristoph Hellwig vid_hdr->hdr_crc = cpu_to_be32(crc); 1035801c135cSArtem B. Bityutskiy 1036801c135cSArtem B. Bityutskiy err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr); 1037801c135cSArtem B. Bityutskiy if (err) 1038801c135cSArtem B. Bityutskiy return -EINVAL; 1039801c135cSArtem B. Bityutskiy 1040801c135cSArtem B. Bityutskiy p = (char *)vid_hdr - ubi->vid_hdr_shift; 1041801c135cSArtem B. Bityutskiy err = ubi_io_write(ubi, p, pnum, ubi->vid_hdr_aloffset, 1042801c135cSArtem B. Bityutskiy ubi->vid_hdr_alsize); 1043801c135cSArtem B. Bityutskiy return err; 1044801c135cSArtem B. Bityutskiy } 1045801c135cSArtem B. Bityutskiy 1046801c135cSArtem B. Bityutskiy #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID 1047801c135cSArtem B. Bityutskiy 1048801c135cSArtem B. Bityutskiy /** 1049801c135cSArtem B. Bityutskiy * paranoid_check_not_bad - ensure that a physical eraseblock is not bad. 1050801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 1051801c135cSArtem B. Bityutskiy * @pnum: physical eraseblock number to check 1052801c135cSArtem B. Bityutskiy * 1053801c135cSArtem B. Bityutskiy * This function returns zero if the physical eraseblock is good, a positive 1054801c135cSArtem B. Bityutskiy * number if it is bad and a negative error code if an error occurred. 1055801c135cSArtem B. Bityutskiy */ 1056801c135cSArtem B. Bityutskiy static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum) 1057801c135cSArtem B. Bityutskiy { 1058801c135cSArtem B. Bityutskiy int err; 1059801c135cSArtem B. Bityutskiy 1060801c135cSArtem B. Bityutskiy err = ubi_io_is_bad(ubi, pnum); 1061801c135cSArtem B. Bityutskiy if (!err) 1062801c135cSArtem B. Bityutskiy return err; 1063801c135cSArtem B. Bityutskiy 1064801c135cSArtem B. Bityutskiy ubi_err("paranoid check failed for PEB %d", pnum); 1065801c135cSArtem B. Bityutskiy ubi_dbg_dump_stack(); 1066801c135cSArtem B. Bityutskiy return err; 1067801c135cSArtem B. Bityutskiy } 1068801c135cSArtem B. Bityutskiy 1069801c135cSArtem B. Bityutskiy /** 1070801c135cSArtem B. Bityutskiy * paranoid_check_ec_hdr - check if an erase counter header is all right. 1071801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 1072801c135cSArtem B. Bityutskiy * @pnum: physical eraseblock number the erase counter header belongs to 1073801c135cSArtem B. Bityutskiy * @ec_hdr: the erase counter header to check 1074801c135cSArtem B. Bityutskiy * 1075801c135cSArtem B. Bityutskiy * This function returns zero if the erase counter header contains valid 1076801c135cSArtem B. Bityutskiy * values, and %1 if not. 1077801c135cSArtem B. Bityutskiy */ 1078801c135cSArtem B. Bityutskiy static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum, 1079801c135cSArtem B. Bityutskiy const struct ubi_ec_hdr *ec_hdr) 1080801c135cSArtem B. Bityutskiy { 1081801c135cSArtem B. Bityutskiy int err; 1082801c135cSArtem B. Bityutskiy uint32_t magic; 1083801c135cSArtem B. Bityutskiy 10843261ebd7SChristoph Hellwig magic = be32_to_cpu(ec_hdr->magic); 1085801c135cSArtem B. Bityutskiy if (magic != UBI_EC_HDR_MAGIC) { 1086801c135cSArtem B. Bityutskiy ubi_err("bad magic %#08x, must be %#08x", 1087801c135cSArtem B. Bityutskiy magic, UBI_EC_HDR_MAGIC); 1088801c135cSArtem B. Bityutskiy goto fail; 1089801c135cSArtem B. Bityutskiy } 1090801c135cSArtem B. Bityutskiy 1091801c135cSArtem B. Bityutskiy err = validate_ec_hdr(ubi, ec_hdr); 1092801c135cSArtem B. Bityutskiy if (err) { 1093801c135cSArtem B. Bityutskiy ubi_err("paranoid check failed for PEB %d", pnum); 1094801c135cSArtem B. Bityutskiy goto fail; 1095801c135cSArtem B. Bityutskiy } 1096801c135cSArtem B. Bityutskiy 1097801c135cSArtem B. Bityutskiy return 0; 1098801c135cSArtem B. Bityutskiy 1099801c135cSArtem B. Bityutskiy fail: 1100801c135cSArtem B. Bityutskiy ubi_dbg_dump_ec_hdr(ec_hdr); 1101801c135cSArtem B. Bityutskiy ubi_dbg_dump_stack(); 1102801c135cSArtem B. Bityutskiy return 1; 1103801c135cSArtem B. Bityutskiy } 1104801c135cSArtem B. Bityutskiy 1105801c135cSArtem B. Bityutskiy /** 1106ebaaf1afSArtem Bityutskiy * paranoid_check_peb_ec_hdr - check erase counter header. 1107801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 1108801c135cSArtem B. Bityutskiy * @pnum: the physical eraseblock number to check 1109801c135cSArtem B. Bityutskiy * 1110801c135cSArtem B. Bityutskiy * This function returns zero if the erase counter header is all right, %1 if 1111801c135cSArtem B. Bityutskiy * not, and a negative error code if an error occurred. 1112801c135cSArtem B. Bityutskiy */ 1113801c135cSArtem B. Bityutskiy static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum) 1114801c135cSArtem B. Bityutskiy { 1115801c135cSArtem B. Bityutskiy int err; 1116801c135cSArtem B. Bityutskiy uint32_t crc, hdr_crc; 1117801c135cSArtem B. Bityutskiy struct ubi_ec_hdr *ec_hdr; 1118801c135cSArtem B. Bityutskiy 111933818bbbSArtem Bityutskiy ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS); 1120801c135cSArtem B. Bityutskiy if (!ec_hdr) 1121801c135cSArtem B. Bityutskiy return -ENOMEM; 1122801c135cSArtem B. Bityutskiy 1123801c135cSArtem B. Bityutskiy err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE); 1124801c135cSArtem B. Bityutskiy if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG) 1125801c135cSArtem B. Bityutskiy goto exit; 1126801c135cSArtem B. Bityutskiy 1127801c135cSArtem B. Bityutskiy crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC); 11283261ebd7SChristoph Hellwig hdr_crc = be32_to_cpu(ec_hdr->hdr_crc); 1129801c135cSArtem B. Bityutskiy if (hdr_crc != crc) { 1130801c135cSArtem B. Bityutskiy ubi_err("bad CRC, calculated %#08x, read %#08x", crc, hdr_crc); 1131801c135cSArtem B. Bityutskiy ubi_err("paranoid check failed for PEB %d", pnum); 1132801c135cSArtem B. Bityutskiy ubi_dbg_dump_ec_hdr(ec_hdr); 1133801c135cSArtem B. Bityutskiy ubi_dbg_dump_stack(); 1134801c135cSArtem B. Bityutskiy err = 1; 1135801c135cSArtem B. Bityutskiy goto exit; 1136801c135cSArtem B. Bityutskiy } 1137801c135cSArtem B. Bityutskiy 1138801c135cSArtem B. Bityutskiy err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr); 1139801c135cSArtem B. Bityutskiy 1140801c135cSArtem B. Bityutskiy exit: 1141801c135cSArtem B. Bityutskiy kfree(ec_hdr); 1142801c135cSArtem B. Bityutskiy return err; 1143801c135cSArtem B. Bityutskiy } 1144801c135cSArtem B. Bityutskiy 1145801c135cSArtem B. Bityutskiy /** 1146801c135cSArtem B. Bityutskiy * paranoid_check_vid_hdr - check that a volume identifier header is all right. 1147801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 1148801c135cSArtem B. Bityutskiy * @pnum: physical eraseblock number the volume identifier header belongs to 1149801c135cSArtem B. Bityutskiy * @vid_hdr: the volume identifier header to check 1150801c135cSArtem B. Bityutskiy * 1151801c135cSArtem B. Bityutskiy * This function returns zero if the volume identifier header is all right, and 1152801c135cSArtem B. Bityutskiy * %1 if not. 1153801c135cSArtem B. Bityutskiy */ 1154801c135cSArtem B. Bityutskiy static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum, 1155801c135cSArtem B. Bityutskiy const struct ubi_vid_hdr *vid_hdr) 1156801c135cSArtem B. Bityutskiy { 1157801c135cSArtem B. Bityutskiy int err; 1158801c135cSArtem B. Bityutskiy uint32_t magic; 1159801c135cSArtem B. Bityutskiy 11603261ebd7SChristoph Hellwig magic = be32_to_cpu(vid_hdr->magic); 1161801c135cSArtem B. Bityutskiy if (magic != UBI_VID_HDR_MAGIC) { 1162801c135cSArtem B. Bityutskiy ubi_err("bad VID header magic %#08x at PEB %d, must be %#08x", 1163801c135cSArtem B. Bityutskiy magic, pnum, UBI_VID_HDR_MAGIC); 1164801c135cSArtem B. Bityutskiy goto fail; 1165801c135cSArtem B. Bityutskiy } 1166801c135cSArtem B. Bityutskiy 1167801c135cSArtem B. Bityutskiy err = validate_vid_hdr(ubi, vid_hdr); 1168801c135cSArtem B. Bityutskiy if (err) { 1169801c135cSArtem B. Bityutskiy ubi_err("paranoid check failed for PEB %d", pnum); 1170801c135cSArtem B. Bityutskiy goto fail; 1171801c135cSArtem B. Bityutskiy } 1172801c135cSArtem B. Bityutskiy 1173801c135cSArtem B. Bityutskiy return err; 1174801c135cSArtem B. Bityutskiy 1175801c135cSArtem B. Bityutskiy fail: 1176801c135cSArtem B. Bityutskiy ubi_err("paranoid check failed for PEB %d", pnum); 1177801c135cSArtem B. Bityutskiy ubi_dbg_dump_vid_hdr(vid_hdr); 1178801c135cSArtem B. Bityutskiy ubi_dbg_dump_stack(); 1179801c135cSArtem B. Bityutskiy return 1; 1180801c135cSArtem B. Bityutskiy 1181801c135cSArtem B. Bityutskiy } 1182801c135cSArtem B. Bityutskiy 1183801c135cSArtem B. Bityutskiy /** 1184ebaaf1afSArtem Bityutskiy * paranoid_check_peb_vid_hdr - check volume identifier header. 1185801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 1186801c135cSArtem B. Bityutskiy * @pnum: the physical eraseblock number to check 1187801c135cSArtem B. Bityutskiy * 1188801c135cSArtem B. Bityutskiy * This function returns zero if the volume identifier header is all right, 1189801c135cSArtem B. Bityutskiy * %1 if not, and a negative error code if an error occurred. 1190801c135cSArtem B. Bityutskiy */ 1191801c135cSArtem B. Bityutskiy static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum) 1192801c135cSArtem B. Bityutskiy { 1193801c135cSArtem B. Bityutskiy int err; 1194801c135cSArtem B. Bityutskiy uint32_t crc, hdr_crc; 1195801c135cSArtem B. Bityutskiy struct ubi_vid_hdr *vid_hdr; 1196801c135cSArtem B. Bityutskiy void *p; 1197801c135cSArtem B. Bityutskiy 119833818bbbSArtem Bityutskiy vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS); 1199801c135cSArtem B. Bityutskiy if (!vid_hdr) 1200801c135cSArtem B. Bityutskiy return -ENOMEM; 1201801c135cSArtem B. Bityutskiy 1202801c135cSArtem B. Bityutskiy p = (char *)vid_hdr - ubi->vid_hdr_shift; 1203801c135cSArtem B. Bityutskiy err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset, 1204801c135cSArtem B. Bityutskiy ubi->vid_hdr_alsize); 1205801c135cSArtem B. Bityutskiy if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG) 1206801c135cSArtem B. Bityutskiy goto exit; 1207801c135cSArtem B. Bityutskiy 1208801c135cSArtem B. Bityutskiy crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_EC_HDR_SIZE_CRC); 12093261ebd7SChristoph Hellwig hdr_crc = be32_to_cpu(vid_hdr->hdr_crc); 1210801c135cSArtem B. Bityutskiy if (hdr_crc != crc) { 1211801c135cSArtem B. Bityutskiy ubi_err("bad VID header CRC at PEB %d, calculated %#08x, " 1212801c135cSArtem B. Bityutskiy "read %#08x", pnum, crc, hdr_crc); 1213801c135cSArtem B. Bityutskiy ubi_err("paranoid check failed for PEB %d", pnum); 1214801c135cSArtem B. Bityutskiy ubi_dbg_dump_vid_hdr(vid_hdr); 1215801c135cSArtem B. Bityutskiy ubi_dbg_dump_stack(); 1216801c135cSArtem B. Bityutskiy err = 1; 1217801c135cSArtem B. Bityutskiy goto exit; 1218801c135cSArtem B. Bityutskiy } 1219801c135cSArtem B. Bityutskiy 1220801c135cSArtem B. Bityutskiy err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr); 1221801c135cSArtem B. Bityutskiy 1222801c135cSArtem B. Bityutskiy exit: 1223801c135cSArtem B. Bityutskiy ubi_free_vid_hdr(ubi, vid_hdr); 1224801c135cSArtem B. Bityutskiy return err; 1225801c135cSArtem B. Bityutskiy } 1226801c135cSArtem B. Bityutskiy 1227801c135cSArtem B. Bityutskiy /** 122840a71a87SArtem Bityutskiy * ubi_dbg_check_all_ff - check that a region of flash is empty. 1229801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 1230801c135cSArtem B. Bityutskiy * @pnum: the physical eraseblock number to check 1231801c135cSArtem B. Bityutskiy * @offset: the starting offset within the physical eraseblock to check 1232801c135cSArtem B. Bityutskiy * @len: the length of the region to check 1233801c135cSArtem B. Bityutskiy * 1234801c135cSArtem B. Bityutskiy * This function returns zero if only 0xFF bytes are present at offset 1235801c135cSArtem B. Bityutskiy * @offset of the physical eraseblock @pnum, %1 if not, and a negative error 1236801c135cSArtem B. Bityutskiy * code if an error occurred. 1237801c135cSArtem B. Bityutskiy */ 123840a71a87SArtem Bityutskiy int ubi_dbg_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len) 1239801c135cSArtem B. Bityutskiy { 1240801c135cSArtem B. Bityutskiy size_t read; 1241801c135cSArtem B. Bityutskiy int err; 1242801c135cSArtem B. Bityutskiy loff_t addr = (loff_t)pnum * ubi->peb_size + offset; 1243801c135cSArtem B. Bityutskiy 124440a71a87SArtem Bityutskiy ubi_assert(!mutex_is_locked(&ubi->dbg_buf_mutex)); 124540a71a87SArtem Bityutskiy 1246e88d6e10SArtem Bityutskiy mutex_lock(&ubi->dbg_buf_mutex); 1247e88d6e10SArtem Bityutskiy err = ubi->mtd->read(ubi->mtd, addr, len, &read, ubi->dbg_peb_buf); 1248801c135cSArtem B. Bityutskiy if (err && err != -EUCLEAN) { 1249801c135cSArtem B. Bityutskiy ubi_err("error %d while reading %d bytes from PEB %d:%d, " 1250801c135cSArtem B. Bityutskiy "read %zd bytes", err, len, pnum, offset, read); 1251801c135cSArtem B. Bityutskiy goto error; 1252801c135cSArtem B. Bityutskiy } 1253801c135cSArtem B. Bityutskiy 1254e88d6e10SArtem Bityutskiy err = check_pattern(ubi->dbg_peb_buf, 0xFF, len); 1255801c135cSArtem B. Bityutskiy if (err == 0) { 1256801c135cSArtem B. Bityutskiy ubi_err("flash region at PEB %d:%d, length %d does not " 1257801c135cSArtem B. Bityutskiy "contain all 0xFF bytes", pnum, offset, len); 1258801c135cSArtem B. Bityutskiy goto fail; 1259801c135cSArtem B. Bityutskiy } 1260e88d6e10SArtem Bityutskiy mutex_unlock(&ubi->dbg_buf_mutex); 1261801c135cSArtem B. Bityutskiy 1262801c135cSArtem B. Bityutskiy return 0; 1263801c135cSArtem B. Bityutskiy 1264801c135cSArtem B. Bityutskiy fail: 1265801c135cSArtem B. Bityutskiy ubi_err("paranoid check failed for PEB %d", pnum); 1266c8566350SArtem Bityutskiy ubi_msg("hex dump of the %d-%d region", offset, offset + len); 12676986646bSArtem Bityutskiy print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, 1268e88d6e10SArtem Bityutskiy ubi->dbg_peb_buf, len, 1); 1269801c135cSArtem B. Bityutskiy err = 1; 1270801c135cSArtem B. Bityutskiy error: 1271801c135cSArtem B. Bityutskiy ubi_dbg_dump_stack(); 1272e88d6e10SArtem Bityutskiy mutex_unlock(&ubi->dbg_buf_mutex); 1273801c135cSArtem B. Bityutskiy return err; 1274801c135cSArtem B. Bityutskiy } 1275801c135cSArtem B. Bityutskiy 1276ffb6b7e4SArtem Bityutskiy /** 1277ffb6b7e4SArtem Bityutskiy * paranoid_check_empty - whether a PEB is empty. 1278ffb6b7e4SArtem Bityutskiy * @ubi: UBI device description object 1279ffb6b7e4SArtem Bityutskiy * @pnum: the physical eraseblock number to check 1280ffb6b7e4SArtem Bityutskiy * 1281ffb6b7e4SArtem Bityutskiy * This function makes sure PEB @pnum is empty, which means it contains only 1282ffb6b7e4SArtem Bityutskiy * %0xFF data bytes. Returns zero if the PEB is empty, %1 if not, and a 1283ffb6b7e4SArtem Bityutskiy * negative error code in case of failure. 1284ffb6b7e4SArtem Bityutskiy * 1285ffb6b7e4SArtem Bityutskiy * Empty PEBs have the EC header, and do not have the VID header. The caller of 1286ffb6b7e4SArtem Bityutskiy * this function should have already made sure the PEB does not have the VID 1287ffb6b7e4SArtem Bityutskiy * header. However, this function re-checks that, because it is possible that 1288ffb6b7e4SArtem Bityutskiy * the header and data has already been written to the PEB. 1289ffb6b7e4SArtem Bityutskiy * 1290ffb6b7e4SArtem Bityutskiy * Let's consider a possible scenario. Suppose there are 2 tasks - A and B. 1291ffb6b7e4SArtem Bityutskiy * Task A is in 'wear_leveling_worker()'. It is reading VID header of PEB X to 1292ffb6b7e4SArtem Bityutskiy * find which LEB it corresponds to. PEB X is currently unmapped, and has no 1293ffb6b7e4SArtem Bityutskiy * VID header. Task B is trying to write to PEB X. 1294ffb6b7e4SArtem Bityutskiy * 1295ffb6b7e4SArtem Bityutskiy * Task A: in 'ubi_io_read_vid_hdr()': reads the VID header from PEB X. The 1296ffb6b7e4SArtem Bityutskiy * read data contain all 0xFF bytes; 1297ffb6b7e4SArtem Bityutskiy * Task B: writes VID header and some data to PEB X; 1298ffb6b7e4SArtem Bityutskiy * Task A: assumes PEB X is empty, calls 'paranoid_check_empty()'. And if we 1299ffb6b7e4SArtem Bityutskiy * do not re-read the VID header, and do not cancel the checking if it 1300ffb6b7e4SArtem Bityutskiy * is there, we fail. 1301ffb6b7e4SArtem Bityutskiy */ 1302ffb6b7e4SArtem Bityutskiy static int paranoid_check_empty(struct ubi_device *ubi, int pnum) 1303ffb6b7e4SArtem Bityutskiy { 1304ffb6b7e4SArtem Bityutskiy int err, offs = ubi->vid_hdr_aloffset, len = ubi->vid_hdr_alsize; 1305ffb6b7e4SArtem Bityutskiy size_t read; 1306ffb6b7e4SArtem Bityutskiy uint32_t magic; 1307ffb6b7e4SArtem Bityutskiy const struct ubi_vid_hdr *vid_hdr; 1308ffb6b7e4SArtem Bityutskiy 1309ffb6b7e4SArtem Bityutskiy mutex_lock(&ubi->dbg_buf_mutex); 1310ffb6b7e4SArtem Bityutskiy err = ubi->mtd->read(ubi->mtd, offs, len, &read, ubi->dbg_peb_buf); 1311ffb6b7e4SArtem Bityutskiy if (err && err != -EUCLEAN) { 1312ffb6b7e4SArtem Bityutskiy ubi_err("error %d while reading %d bytes from PEB %d:%d, " 1313ffb6b7e4SArtem Bityutskiy "read %zd bytes", err, len, pnum, offs, read); 1314ffb6b7e4SArtem Bityutskiy goto error; 1315ffb6b7e4SArtem Bityutskiy } 1316ffb6b7e4SArtem Bityutskiy 1317ffb6b7e4SArtem Bityutskiy vid_hdr = ubi->dbg_peb_buf; 1318ffb6b7e4SArtem Bityutskiy magic = be32_to_cpu(vid_hdr->magic); 1319ffb6b7e4SArtem Bityutskiy if (magic == UBI_VID_HDR_MAGIC) 1320ffb6b7e4SArtem Bityutskiy /* The PEB contains VID header, so it is not empty */ 1321ffb6b7e4SArtem Bityutskiy goto out; 1322ffb6b7e4SArtem Bityutskiy 1323ffb6b7e4SArtem Bityutskiy err = check_pattern(ubi->dbg_peb_buf, 0xFF, len); 1324ffb6b7e4SArtem Bityutskiy if (err == 0) { 1325ffb6b7e4SArtem Bityutskiy ubi_err("flash region at PEB %d:%d, length %d does not " 1326ffb6b7e4SArtem Bityutskiy "contain all 0xFF bytes", pnum, offs, len); 1327ffb6b7e4SArtem Bityutskiy goto fail; 1328ffb6b7e4SArtem Bityutskiy } 1329ffb6b7e4SArtem Bityutskiy 1330ffb6b7e4SArtem Bityutskiy out: 1331ffb6b7e4SArtem Bityutskiy mutex_unlock(&ubi->dbg_buf_mutex); 1332ffb6b7e4SArtem Bityutskiy return 0; 1333ffb6b7e4SArtem Bityutskiy 1334ffb6b7e4SArtem Bityutskiy fail: 1335ffb6b7e4SArtem Bityutskiy ubi_err("paranoid check failed for PEB %d", pnum); 1336ffb6b7e4SArtem Bityutskiy ubi_msg("hex dump of the %d-%d region", offs, offs + len); 1337ffb6b7e4SArtem Bityutskiy print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, 1338ffb6b7e4SArtem Bityutskiy ubi->dbg_peb_buf, len, 1); 1339ffb6b7e4SArtem Bityutskiy err = 1; 1340ffb6b7e4SArtem Bityutskiy error: 1341ffb6b7e4SArtem Bityutskiy ubi_dbg_dump_stack(); 1342ffb6b7e4SArtem Bityutskiy mutex_unlock(&ubi->dbg_buf_mutex); 1343ffb6b7e4SArtem Bityutskiy return err; 1344ffb6b7e4SArtem Bityutskiy } 1345ffb6b7e4SArtem Bityutskiy 1346801c135cSArtem B. Bityutskiy #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */ 1347