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 67be436f62SShinya Kuribayashi * bytes which are not relevant to the sub-page are 0xFF. So, basically, 68be436f62SShinya Kuribayashi * writing 4x512 sub-pages is 4 times slower than writing one 2KiB NAND page. 69be436f62SShinya Kuribayashi * Thus, we prefer to use sub-pages only for EC 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> 915a0e3ad6STejun Heo #include <linux/slab.h> 92801c135cSArtem B. Bityutskiy #include "ubi.h" 93801c135cSArtem B. Bityutskiy 94801c135cSArtem B. Bityutskiy #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID 95801c135cSArtem B. Bityutskiy static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum); 96801c135cSArtem B. Bityutskiy static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum); 97801c135cSArtem B. Bityutskiy static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum, 98801c135cSArtem B. Bityutskiy const struct ubi_ec_hdr *ec_hdr); 99801c135cSArtem B. Bityutskiy static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum); 100801c135cSArtem B. Bityutskiy static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum, 101801c135cSArtem B. Bityutskiy const struct ubi_vid_hdr *vid_hdr); 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 108801c135cSArtem B. Bityutskiy #endif 109801c135cSArtem B. Bityutskiy 110801c135cSArtem B. Bityutskiy /** 111801c135cSArtem B. Bityutskiy * ubi_io_read - read data from a physical eraseblock. 112801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 113801c135cSArtem B. Bityutskiy * @buf: buffer where to store the read data 114801c135cSArtem B. Bityutskiy * @pnum: physical eraseblock number to read from 115801c135cSArtem B. Bityutskiy * @offset: offset within the physical eraseblock from where to read 116801c135cSArtem B. Bityutskiy * @len: how many bytes to read 117801c135cSArtem B. Bityutskiy * 118801c135cSArtem B. Bityutskiy * This function reads data from offset @offset of physical eraseblock @pnum 119801c135cSArtem B. Bityutskiy * and stores the read data in the @buf buffer. The following return codes are 120801c135cSArtem B. Bityutskiy * possible: 121801c135cSArtem B. Bityutskiy * 122801c135cSArtem B. Bityutskiy * o %0 if all the requested data were successfully read; 123801c135cSArtem B. Bityutskiy * o %UBI_IO_BITFLIPS if all the requested data were successfully read, but 124801c135cSArtem B. Bityutskiy * correctable bit-flips were detected; this is harmless but may indicate 125801c135cSArtem B. Bityutskiy * that this eraseblock may become bad soon (but do not have to); 12663b6c1edSArtem Bityutskiy * o %-EBADMSG if the MTD subsystem reported about data integrity problems, for 12763b6c1edSArtem Bityutskiy * example it can be an ECC error in case of NAND; this most probably means 12863b6c1edSArtem Bityutskiy * that the data is corrupted; 129801c135cSArtem B. Bityutskiy * o %-EIO if some I/O error occurred; 130801c135cSArtem B. Bityutskiy * o other negative error codes in case of other errors. 131801c135cSArtem B. Bityutskiy */ 132801c135cSArtem B. Bityutskiy int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset, 133801c135cSArtem B. Bityutskiy int len) 134801c135cSArtem B. Bityutskiy { 135801c135cSArtem B. Bityutskiy int err, retries = 0; 136801c135cSArtem B. Bityutskiy size_t read; 137801c135cSArtem B. Bityutskiy loff_t addr; 138801c135cSArtem B. Bityutskiy 139801c135cSArtem B. Bityutskiy dbg_io("read %d bytes from PEB %d:%d", len, pnum, offset); 140801c135cSArtem B. Bityutskiy 141801c135cSArtem B. Bityutskiy ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 142801c135cSArtem B. Bityutskiy ubi_assert(offset >= 0 && offset + len <= ubi->peb_size); 143801c135cSArtem B. Bityutskiy ubi_assert(len > 0); 144801c135cSArtem B. Bityutskiy 145801c135cSArtem B. Bityutskiy err = paranoid_check_not_bad(ubi, pnum); 146801c135cSArtem B. Bityutskiy if (err) 147adbf05e3SArtem Bityutskiy return err; 148801c135cSArtem B. Bityutskiy 149801c135cSArtem B. Bityutskiy addr = (loff_t)pnum * ubi->peb_size + offset; 150801c135cSArtem B. Bityutskiy retry: 151801c135cSArtem B. Bityutskiy err = ubi->mtd->read(ubi->mtd, addr, len, &read, buf); 152801c135cSArtem B. Bityutskiy if (err) { 153f5d5b1f8SArtem Bityutskiy const char *errstr = (err == -EBADMSG) ? " (ECC error)" : ""; 1541a49af2cSArtem Bityutskiy 155801c135cSArtem B. Bityutskiy if (err == -EUCLEAN) { 156801c135cSArtem B. Bityutskiy /* 157801c135cSArtem B. Bityutskiy * -EUCLEAN is reported if there was a bit-flip which 158801c135cSArtem B. Bityutskiy * was corrected, so this is harmless. 1598c1e6ee1SArtem Bityutskiy * 1608c1e6ee1SArtem Bityutskiy * We do not report about it here unless debugging is 1618c1e6ee1SArtem Bityutskiy * enabled. A corresponding message will be printed 1628c1e6ee1SArtem Bityutskiy * later, when it is has been scrubbed. 163801c135cSArtem B. Bityutskiy */ 1648c1e6ee1SArtem Bityutskiy dbg_msg("fixable bit-flip detected at PEB %d", pnum); 165801c135cSArtem B. Bityutskiy ubi_assert(len == read); 166801c135cSArtem B. Bityutskiy return UBI_IO_BITFLIPS; 167801c135cSArtem B. Bityutskiy } 168801c135cSArtem B. Bityutskiy 169801c135cSArtem B. Bityutskiy if (read != len && retries++ < UBI_IO_RETRIES) { 1701a49af2cSArtem Bityutskiy dbg_io("error %d%s while reading %d bytes from PEB %d:%d," 171801c135cSArtem B. Bityutskiy " read only %zd bytes, retry", 1721a49af2cSArtem Bityutskiy err, errstr, len, pnum, offset, read); 173801c135cSArtem B. Bityutskiy yield(); 174801c135cSArtem B. Bityutskiy goto retry; 175801c135cSArtem B. Bityutskiy } 176801c135cSArtem B. Bityutskiy 177f5d5b1f8SArtem Bityutskiy ubi_err("error %d%s while reading %d bytes from PEB %d:%d, " 1781a49af2cSArtem Bityutskiy "read %zd bytes", err, errstr, len, pnum, offset, read); 179801c135cSArtem B. Bityutskiy ubi_dbg_dump_stack(); 1802362a53eSArtem Bityutskiy 1812362a53eSArtem Bityutskiy /* 1822362a53eSArtem Bityutskiy * The driver should never return -EBADMSG if it failed to read 1832362a53eSArtem Bityutskiy * all the requested data. But some buggy drivers might do 1842362a53eSArtem Bityutskiy * this, so we change it to -EIO. 1852362a53eSArtem Bityutskiy */ 1862362a53eSArtem Bityutskiy if (read != len && err == -EBADMSG) { 1872362a53eSArtem Bityutskiy ubi_assert(0); 1882362a53eSArtem Bityutskiy err = -EIO; 1892362a53eSArtem Bityutskiy } 190801c135cSArtem B. Bityutskiy } else { 191801c135cSArtem B. Bityutskiy ubi_assert(len == read); 192801c135cSArtem B. Bityutskiy 193801c135cSArtem B. Bityutskiy if (ubi_dbg_is_bitflip()) { 194c8566350SArtem Bityutskiy dbg_gen("bit-flip (emulated)"); 195801c135cSArtem B. Bityutskiy err = UBI_IO_BITFLIPS; 196801c135cSArtem B. Bityutskiy } 197801c135cSArtem B. Bityutskiy } 198801c135cSArtem B. Bityutskiy 199801c135cSArtem B. Bityutskiy return err; 200801c135cSArtem B. Bityutskiy } 201801c135cSArtem B. Bityutskiy 202801c135cSArtem B. Bityutskiy /** 203801c135cSArtem B. Bityutskiy * ubi_io_write - write data to a physical eraseblock. 204801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 205801c135cSArtem B. Bityutskiy * @buf: buffer with the data to write 206801c135cSArtem B. Bityutskiy * @pnum: physical eraseblock number to write to 207801c135cSArtem B. Bityutskiy * @offset: offset within the physical eraseblock where to write 208801c135cSArtem B. Bityutskiy * @len: how many bytes to write 209801c135cSArtem B. Bityutskiy * 210801c135cSArtem B. Bityutskiy * This function writes @len bytes of data from buffer @buf to offset @offset 211801c135cSArtem B. Bityutskiy * of physical eraseblock @pnum. If all the data were successfully written, 212801c135cSArtem B. Bityutskiy * zero is returned. If an error occurred, this function returns a negative 213801c135cSArtem B. Bityutskiy * error code. If %-EIO is returned, the physical eraseblock most probably went 214801c135cSArtem B. Bityutskiy * bad. 215801c135cSArtem B. Bityutskiy * 216801c135cSArtem B. Bityutskiy * Note, in case of an error, it is possible that something was still written 217801c135cSArtem B. Bityutskiy * to the flash media, but may be some garbage. 218801c135cSArtem B. Bityutskiy */ 219e88d6e10SArtem Bityutskiy int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset, 220e88d6e10SArtem Bityutskiy int len) 221801c135cSArtem B. Bityutskiy { 222801c135cSArtem B. Bityutskiy int err; 223801c135cSArtem B. Bityutskiy size_t written; 224801c135cSArtem B. Bityutskiy loff_t addr; 225801c135cSArtem B. Bityutskiy 226801c135cSArtem B. Bityutskiy dbg_io("write %d bytes to PEB %d:%d", len, pnum, offset); 227801c135cSArtem B. Bityutskiy 228801c135cSArtem B. Bityutskiy ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 229801c135cSArtem B. Bityutskiy ubi_assert(offset >= 0 && offset + len <= ubi->peb_size); 230801c135cSArtem B. Bityutskiy ubi_assert(offset % ubi->hdrs_min_io_size == 0); 231801c135cSArtem B. Bityutskiy ubi_assert(len > 0 && len % ubi->hdrs_min_io_size == 0); 232801c135cSArtem B. Bityutskiy 233801c135cSArtem B. Bityutskiy if (ubi->ro_mode) { 234801c135cSArtem B. Bityutskiy ubi_err("read-only mode"); 235801c135cSArtem B. Bityutskiy return -EROFS; 236801c135cSArtem B. Bityutskiy } 237801c135cSArtem B. Bityutskiy 238801c135cSArtem B. Bityutskiy /* The below has to be compiled out if paranoid checks are disabled */ 239801c135cSArtem B. Bityutskiy 240801c135cSArtem B. Bityutskiy err = paranoid_check_not_bad(ubi, pnum); 241801c135cSArtem B. Bityutskiy if (err) 242adbf05e3SArtem Bityutskiy return err; 243801c135cSArtem B. Bityutskiy 244801c135cSArtem B. Bityutskiy /* The area we are writing to has to contain all 0xFF bytes */ 24540a71a87SArtem Bityutskiy err = ubi_dbg_check_all_ff(ubi, pnum, offset, len); 246801c135cSArtem B. Bityutskiy if (err) 247adbf05e3SArtem Bityutskiy return err; 248801c135cSArtem B. Bityutskiy 249801c135cSArtem B. Bityutskiy if (offset >= ubi->leb_start) { 250801c135cSArtem B. Bityutskiy /* 251801c135cSArtem B. Bityutskiy * We write to the data area of the physical eraseblock. Make 252801c135cSArtem B. Bityutskiy * sure it has valid EC and VID headers. 253801c135cSArtem B. Bityutskiy */ 254801c135cSArtem B. Bityutskiy err = paranoid_check_peb_ec_hdr(ubi, pnum); 255801c135cSArtem B. Bityutskiy if (err) 256adbf05e3SArtem Bityutskiy return err; 257801c135cSArtem B. Bityutskiy err = paranoid_check_peb_vid_hdr(ubi, pnum); 258801c135cSArtem B. Bityutskiy if (err) 259adbf05e3SArtem Bityutskiy return err; 260801c135cSArtem B. Bityutskiy } 261801c135cSArtem B. Bityutskiy 262801c135cSArtem B. Bityutskiy if (ubi_dbg_is_write_failure()) { 263801c135cSArtem B. Bityutskiy dbg_err("cannot write %d bytes to PEB %d:%d " 264801c135cSArtem B. Bityutskiy "(emulated)", len, pnum, offset); 265801c135cSArtem B. Bityutskiy ubi_dbg_dump_stack(); 266801c135cSArtem B. Bityutskiy return -EIO; 267801c135cSArtem B. Bityutskiy } 268801c135cSArtem B. Bityutskiy 269801c135cSArtem B. Bityutskiy addr = (loff_t)pnum * ubi->peb_size + offset; 270801c135cSArtem B. Bityutskiy err = ubi->mtd->write(ubi->mtd, addr, len, &written, buf); 271801c135cSArtem B. Bityutskiy if (err) { 272801c135cSArtem B. Bityutskiy ubi_err("error %d while writing %d bytes to PEB %d:%d, written " 273801c135cSArtem B. Bityutskiy "%zd bytes", err, len, pnum, offset, written); 274801c135cSArtem B. Bityutskiy ubi_dbg_dump_stack(); 275867996b1SArtem Bityutskiy ubi_dbg_dump_flash(ubi, pnum, offset, len); 276801c135cSArtem B. Bityutskiy } else 277801c135cSArtem B. Bityutskiy ubi_assert(written == len); 278801c135cSArtem B. Bityutskiy 2796e9065d7SArtem Bityutskiy if (!err) { 2806e9065d7SArtem Bityutskiy err = ubi_dbg_check_write(ubi, buf, pnum, offset, len); 2816e9065d7SArtem Bityutskiy if (err) 2826e9065d7SArtem Bityutskiy return err; 2836e9065d7SArtem Bityutskiy 2846e9065d7SArtem Bityutskiy /* 2856e9065d7SArtem Bityutskiy * Since we always write sequentially, the rest of the PEB has 2866e9065d7SArtem Bityutskiy * to contain only 0xFF bytes. 2876e9065d7SArtem Bityutskiy */ 2886e9065d7SArtem Bityutskiy offset += len; 2896e9065d7SArtem Bityutskiy len = ubi->peb_size - offset; 2906e9065d7SArtem Bityutskiy if (len) 2916e9065d7SArtem Bityutskiy err = ubi_dbg_check_all_ff(ubi, pnum, offset, len); 2926e9065d7SArtem Bityutskiy } 2936e9065d7SArtem Bityutskiy 294801c135cSArtem B. Bityutskiy return err; 295801c135cSArtem B. Bityutskiy } 296801c135cSArtem B. Bityutskiy 297801c135cSArtem B. Bityutskiy /** 298801c135cSArtem B. Bityutskiy * erase_callback - MTD erasure call-back. 299801c135cSArtem B. Bityutskiy * @ei: MTD erase information object. 300801c135cSArtem B. Bityutskiy * 301801c135cSArtem B. Bityutskiy * Note, even though MTD erase interface is asynchronous, all the current 302801c135cSArtem B. Bityutskiy * implementations are synchronous anyway. 303801c135cSArtem B. Bityutskiy */ 304801c135cSArtem B. Bityutskiy static void erase_callback(struct erase_info *ei) 305801c135cSArtem B. Bityutskiy { 306801c135cSArtem B. Bityutskiy wake_up_interruptible((wait_queue_head_t *)ei->priv); 307801c135cSArtem B. Bityutskiy } 308801c135cSArtem B. Bityutskiy 309801c135cSArtem B. Bityutskiy /** 310801c135cSArtem B. Bityutskiy * do_sync_erase - synchronously erase a physical eraseblock. 311801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 312801c135cSArtem B. Bityutskiy * @pnum: the physical eraseblock number to erase 313801c135cSArtem B. Bityutskiy * 314801c135cSArtem B. Bityutskiy * This function synchronously erases physical eraseblock @pnum and returns 315801c135cSArtem B. Bityutskiy * zero in case of success and a negative error code in case of failure. If 316801c135cSArtem B. Bityutskiy * %-EIO is returned, the physical eraseblock most probably went bad. 317801c135cSArtem B. Bityutskiy */ 318e88d6e10SArtem Bityutskiy static int do_sync_erase(struct ubi_device *ubi, int pnum) 319801c135cSArtem B. Bityutskiy { 320801c135cSArtem B. Bityutskiy int err, retries = 0; 321801c135cSArtem B. Bityutskiy struct erase_info ei; 322801c135cSArtem B. Bityutskiy wait_queue_head_t wq; 323801c135cSArtem B. Bityutskiy 324801c135cSArtem B. Bityutskiy dbg_io("erase PEB %d", pnum); 325801c135cSArtem B. Bityutskiy 326801c135cSArtem B. Bityutskiy retry: 327801c135cSArtem B. Bityutskiy init_waitqueue_head(&wq); 328801c135cSArtem B. Bityutskiy memset(&ei, 0, sizeof(struct erase_info)); 329801c135cSArtem B. Bityutskiy 330801c135cSArtem B. Bityutskiy ei.mtd = ubi->mtd; 3312f176f79SBrijesh Singh ei.addr = (loff_t)pnum * ubi->peb_size; 332801c135cSArtem B. Bityutskiy ei.len = ubi->peb_size; 333801c135cSArtem B. Bityutskiy ei.callback = erase_callback; 334801c135cSArtem B. Bityutskiy ei.priv = (unsigned long)&wq; 335801c135cSArtem B. Bityutskiy 336801c135cSArtem B. Bityutskiy err = ubi->mtd->erase(ubi->mtd, &ei); 337801c135cSArtem B. Bityutskiy if (err) { 338801c135cSArtem B. Bityutskiy if (retries++ < UBI_IO_RETRIES) { 339801c135cSArtem B. Bityutskiy dbg_io("error %d while erasing PEB %d, retry", 340801c135cSArtem B. Bityutskiy err, pnum); 341801c135cSArtem B. Bityutskiy yield(); 342801c135cSArtem B. Bityutskiy goto retry; 343801c135cSArtem B. Bityutskiy } 344801c135cSArtem B. Bityutskiy ubi_err("cannot erase PEB %d, error %d", pnum, err); 345801c135cSArtem B. Bityutskiy ubi_dbg_dump_stack(); 346801c135cSArtem B. Bityutskiy return err; 347801c135cSArtem B. Bityutskiy } 348801c135cSArtem B. Bityutskiy 349801c135cSArtem B. Bityutskiy err = wait_event_interruptible(wq, ei.state == MTD_ERASE_DONE || 350801c135cSArtem B. Bityutskiy ei.state == MTD_ERASE_FAILED); 351801c135cSArtem B. Bityutskiy if (err) { 352801c135cSArtem B. Bityutskiy ubi_err("interrupted PEB %d erasure", pnum); 353801c135cSArtem B. Bityutskiy return -EINTR; 354801c135cSArtem B. Bityutskiy } 355801c135cSArtem B. Bityutskiy 356801c135cSArtem B. Bityutskiy if (ei.state == MTD_ERASE_FAILED) { 357801c135cSArtem B. Bityutskiy if (retries++ < UBI_IO_RETRIES) { 358801c135cSArtem B. Bityutskiy dbg_io("error while erasing PEB %d, retry", pnum); 359801c135cSArtem B. Bityutskiy yield(); 360801c135cSArtem B. Bityutskiy goto retry; 361801c135cSArtem B. Bityutskiy } 362801c135cSArtem B. Bityutskiy ubi_err("cannot erase PEB %d", pnum); 363801c135cSArtem B. Bityutskiy ubi_dbg_dump_stack(); 364801c135cSArtem B. Bityutskiy return -EIO; 365801c135cSArtem B. Bityutskiy } 366801c135cSArtem B. Bityutskiy 36740a71a87SArtem Bityutskiy err = ubi_dbg_check_all_ff(ubi, pnum, 0, ubi->peb_size); 368801c135cSArtem B. Bityutskiy if (err) 369adbf05e3SArtem Bityutskiy return err; 370801c135cSArtem B. Bityutskiy 371801c135cSArtem B. Bityutskiy if (ubi_dbg_is_erase_failure() && !err) { 372801c135cSArtem B. Bityutskiy dbg_err("cannot erase PEB %d (emulated)", pnum); 373801c135cSArtem B. Bityutskiy return -EIO; 374801c135cSArtem B. Bityutskiy } 375801c135cSArtem B. Bityutskiy 376801c135cSArtem B. Bityutskiy return 0; 377801c135cSArtem B. Bityutskiy } 378801c135cSArtem B. Bityutskiy 379801c135cSArtem B. Bityutskiy /** 380801c135cSArtem B. Bityutskiy * check_pattern - check if buffer contains only a certain byte pattern. 381801c135cSArtem B. Bityutskiy * @buf: buffer to check 382801c135cSArtem B. Bityutskiy * @patt: the pattern to check 383801c135cSArtem B. Bityutskiy * @size: buffer size in bytes 384801c135cSArtem B. Bityutskiy * 385801c135cSArtem B. Bityutskiy * This function returns %1 in there are only @patt bytes in @buf, and %0 if 386801c135cSArtem B. Bityutskiy * something else was also found. 387801c135cSArtem B. Bityutskiy */ 388801c135cSArtem B. Bityutskiy static int check_pattern(const void *buf, uint8_t patt, int size) 389801c135cSArtem B. Bityutskiy { 390801c135cSArtem B. Bityutskiy int i; 391801c135cSArtem B. Bityutskiy 392801c135cSArtem B. Bityutskiy for (i = 0; i < size; i++) 393801c135cSArtem B. Bityutskiy if (((const uint8_t *)buf)[i] != patt) 394801c135cSArtem B. Bityutskiy return 0; 395801c135cSArtem B. Bityutskiy return 1; 396801c135cSArtem B. Bityutskiy } 397801c135cSArtem B. Bityutskiy 398801c135cSArtem B. Bityutskiy /* Patterns to write to a physical eraseblock when torturing it */ 399801c135cSArtem B. Bityutskiy static uint8_t patterns[] = {0xa5, 0x5a, 0x0}; 400801c135cSArtem B. Bityutskiy 401801c135cSArtem B. Bityutskiy /** 402801c135cSArtem B. Bityutskiy * torture_peb - test a supposedly bad physical eraseblock. 403801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 404801c135cSArtem B. Bityutskiy * @pnum: the physical eraseblock number to test 405801c135cSArtem B. Bityutskiy * 406801c135cSArtem B. Bityutskiy * This function returns %-EIO if the physical eraseblock did not pass the 407801c135cSArtem B. Bityutskiy * test, a positive number of erase operations done if the test was 408801c135cSArtem B. Bityutskiy * successfully passed, and other negative error codes in case of other errors. 409801c135cSArtem B. Bityutskiy */ 410e88d6e10SArtem Bityutskiy static int torture_peb(struct ubi_device *ubi, int pnum) 411801c135cSArtem B. Bityutskiy { 412801c135cSArtem B. Bityutskiy int err, i, patt_count; 413801c135cSArtem B. Bityutskiy 4148c1e6ee1SArtem Bityutskiy ubi_msg("run torture test for PEB %d", pnum); 415801c135cSArtem B. Bityutskiy patt_count = ARRAY_SIZE(patterns); 416801c135cSArtem B. Bityutskiy ubi_assert(patt_count > 0); 417801c135cSArtem B. Bityutskiy 418e88d6e10SArtem Bityutskiy mutex_lock(&ubi->buf_mutex); 419801c135cSArtem B. Bityutskiy for (i = 0; i < patt_count; i++) { 420801c135cSArtem B. Bityutskiy err = do_sync_erase(ubi, pnum); 421801c135cSArtem B. Bityutskiy if (err) 422801c135cSArtem B. Bityutskiy goto out; 423801c135cSArtem B. Bityutskiy 424801c135cSArtem B. Bityutskiy /* Make sure the PEB contains only 0xFF bytes */ 425e88d6e10SArtem Bityutskiy err = ubi_io_read(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size); 426801c135cSArtem B. Bityutskiy if (err) 427801c135cSArtem B. Bityutskiy goto out; 428801c135cSArtem B. Bityutskiy 429e88d6e10SArtem Bityutskiy err = check_pattern(ubi->peb_buf1, 0xFF, ubi->peb_size); 430801c135cSArtem B. Bityutskiy if (err == 0) { 431801c135cSArtem B. Bityutskiy ubi_err("erased PEB %d, but a non-0xFF byte found", 432801c135cSArtem B. Bityutskiy pnum); 433801c135cSArtem B. Bityutskiy err = -EIO; 434801c135cSArtem B. Bityutskiy goto out; 435801c135cSArtem B. Bityutskiy } 436801c135cSArtem B. Bityutskiy 437801c135cSArtem B. Bityutskiy /* Write a pattern and check it */ 438e88d6e10SArtem Bityutskiy memset(ubi->peb_buf1, patterns[i], ubi->peb_size); 439e88d6e10SArtem Bityutskiy err = ubi_io_write(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size); 440801c135cSArtem B. Bityutskiy if (err) 441801c135cSArtem B. Bityutskiy goto out; 442801c135cSArtem B. Bityutskiy 443e88d6e10SArtem Bityutskiy memset(ubi->peb_buf1, ~patterns[i], ubi->peb_size); 444e88d6e10SArtem Bityutskiy err = ubi_io_read(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size); 445801c135cSArtem B. Bityutskiy if (err) 446801c135cSArtem B. Bityutskiy goto out; 447801c135cSArtem B. Bityutskiy 448e88d6e10SArtem Bityutskiy err = check_pattern(ubi->peb_buf1, patterns[i], ubi->peb_size); 449801c135cSArtem B. Bityutskiy if (err == 0) { 450801c135cSArtem B. Bityutskiy ubi_err("pattern %x checking failed for PEB %d", 451801c135cSArtem B. Bityutskiy patterns[i], pnum); 452801c135cSArtem B. Bityutskiy err = -EIO; 453801c135cSArtem B. Bityutskiy goto out; 454801c135cSArtem B. Bityutskiy } 455801c135cSArtem B. Bityutskiy } 456801c135cSArtem B. Bityutskiy 457801c135cSArtem B. Bityutskiy err = patt_count; 4588c1e6ee1SArtem Bityutskiy ubi_msg("PEB %d passed torture test, do not mark it a bad", pnum); 459801c135cSArtem B. Bityutskiy 460801c135cSArtem B. Bityutskiy out: 461e88d6e10SArtem Bityutskiy mutex_unlock(&ubi->buf_mutex); 4628d2d4011SArtem Bityutskiy if (err == UBI_IO_BITFLIPS || err == -EBADMSG) { 463801c135cSArtem B. Bityutskiy /* 464801c135cSArtem B. Bityutskiy * If a bit-flip or data integrity error was detected, the test 465801c135cSArtem B. Bityutskiy * has not passed because it happened on a freshly erased 466801c135cSArtem B. Bityutskiy * physical eraseblock which means something is wrong with it. 467801c135cSArtem B. Bityutskiy */ 4688d2d4011SArtem Bityutskiy ubi_err("read problems on freshly erased PEB %d, must be bad", 4698d2d4011SArtem Bityutskiy pnum); 470801c135cSArtem B. Bityutskiy err = -EIO; 4718d2d4011SArtem Bityutskiy } 472801c135cSArtem B. Bityutskiy return err; 473801c135cSArtem B. Bityutskiy } 474801c135cSArtem B. Bityutskiy 475801c135cSArtem B. Bityutskiy /** 476ebf53f42SArtem Bityutskiy * nor_erase_prepare - prepare a NOR flash PEB for erasure. 477ebf53f42SArtem Bityutskiy * @ubi: UBI device description object 478ebf53f42SArtem Bityutskiy * @pnum: physical eraseblock number to prepare 479ebf53f42SArtem Bityutskiy * 480ebf53f42SArtem Bityutskiy * NOR flash, or at least some of them, have peculiar embedded PEB erasure 481ebf53f42SArtem Bityutskiy * algorithm: the PEB is first filled with zeroes, then it is erased. And 482ebf53f42SArtem Bityutskiy * filling with zeroes starts from the end of the PEB. This was observed with 483ebf53f42SArtem Bityutskiy * Spansion S29GL512N NOR flash. 484ebf53f42SArtem Bityutskiy * 485ebf53f42SArtem Bityutskiy * This means that in case of a power cut we may end up with intact data at the 486ebf53f42SArtem Bityutskiy * beginning of the PEB, and all zeroes at the end of PEB. In other words, the 487ebf53f42SArtem Bityutskiy * EC and VID headers are OK, but a large chunk of data at the end of PEB is 488ebf53f42SArtem Bityutskiy * zeroed. This makes UBI mistakenly treat this PEB as used and associate it 489ebf53f42SArtem Bityutskiy * with an LEB, which leads to subsequent failures (e.g., UBIFS fails). 490ebf53f42SArtem Bityutskiy * 491ebf53f42SArtem Bityutskiy * This function is called before erasing NOR PEBs and it zeroes out EC and VID 492ebf53f42SArtem Bityutskiy * magic numbers in order to invalidate them and prevent the failures. Returns 493ebf53f42SArtem Bityutskiy * zero in case of success and a negative error code in case of failure. 494ebf53f42SArtem Bityutskiy */ 495ebf53f42SArtem Bityutskiy static int nor_erase_prepare(struct ubi_device *ubi, int pnum) 496ebf53f42SArtem Bityutskiy { 497de75c771SArtem Bityutskiy int err, err1; 498ebf53f42SArtem Bityutskiy size_t written; 499ebf53f42SArtem Bityutskiy loff_t addr; 500ebf53f42SArtem Bityutskiy uint32_t data = 0; 501de75c771SArtem Bityutskiy struct ubi_vid_hdr vid_hdr; 502ebf53f42SArtem Bityutskiy 5035b289b56SArtem Bityutskiy addr = (loff_t)pnum * ubi->peb_size + ubi->vid_hdr_aloffset; 50483c2099fSArtem Bityutskiy err = ubi->mtd->write(ubi->mtd, addr, 4, &written, (void *)&data); 505de75c771SArtem Bityutskiy if (!err) { 5065b289b56SArtem Bityutskiy addr -= ubi->vid_hdr_aloffset; 507de75c771SArtem Bityutskiy err = ubi->mtd->write(ubi->mtd, addr, 4, &written, 508de75c771SArtem Bityutskiy (void *)&data); 509de75c771SArtem Bityutskiy if (!err) 510de75c771SArtem Bityutskiy return 0; 5115b289b56SArtem Bityutskiy } 5125b289b56SArtem Bityutskiy 513de75c771SArtem Bityutskiy /* 514de75c771SArtem Bityutskiy * We failed to write to the media. This was observed with Spansion 515de75c771SArtem Bityutskiy * S29GL512N NOR flash. Most probably the eraseblock erasure was 516de75c771SArtem Bityutskiy * interrupted at a very inappropriate moment, so it became unwritable. 517de75c771SArtem Bityutskiy * In this case we probably anyway have garbage in this PEB. 518de75c771SArtem Bityutskiy */ 519de75c771SArtem Bityutskiy err1 = ubi_io_read_vid_hdr(ubi, pnum, &vid_hdr, 0); 520eb89580eSArtem Bityutskiy if (err1 == UBI_IO_BAD_HDR_READ || err1 == UBI_IO_BAD_HDR) 521de75c771SArtem Bityutskiy /* 522de75c771SArtem Bityutskiy * The VID header is corrupted, so we can safely erase this 523de75c771SArtem Bityutskiy * PEB and not afraid that it will be treated as a valid PEB in 524de75c771SArtem Bityutskiy * case of an unclean reboot. 525de75c771SArtem Bityutskiy */ 526ebf53f42SArtem Bityutskiy return 0; 527de75c771SArtem Bityutskiy 528de75c771SArtem Bityutskiy /* 529de75c771SArtem Bityutskiy * The PEB contains a valid VID header, but we cannot invalidate it. 530de75c771SArtem Bityutskiy * Supposedly the flash media or the driver is screwed up, so return an 531de75c771SArtem Bityutskiy * error. 532de75c771SArtem Bityutskiy */ 533de75c771SArtem Bityutskiy ubi_err("cannot invalidate PEB %d, write returned %d read returned %d", 534de75c771SArtem Bityutskiy pnum, err, err1); 535de75c771SArtem Bityutskiy ubi_dbg_dump_flash(ubi, pnum, 0, ubi->peb_size); 536de75c771SArtem Bityutskiy return -EIO; 537ebf53f42SArtem Bityutskiy } 538ebf53f42SArtem Bityutskiy 539ebf53f42SArtem Bityutskiy /** 540801c135cSArtem B. Bityutskiy * ubi_io_sync_erase - synchronously erase a physical eraseblock. 541801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 542801c135cSArtem B. Bityutskiy * @pnum: physical eraseblock number to erase 543801c135cSArtem B. Bityutskiy * @torture: if this physical eraseblock has to be tortured 544801c135cSArtem B. Bityutskiy * 545801c135cSArtem B. Bityutskiy * This function synchronously erases physical eraseblock @pnum. If @torture 546801c135cSArtem B. Bityutskiy * flag is not zero, the physical eraseblock is checked by means of writing 547801c135cSArtem B. Bityutskiy * different patterns to it and reading them back. If the torturing is enabled, 548025dfdafSFrederik Schwarzer * the physical eraseblock is erased more than once. 549801c135cSArtem B. Bityutskiy * 550801c135cSArtem B. Bityutskiy * This function returns the number of erasures made in case of success, %-EIO 551801c135cSArtem B. Bityutskiy * if the erasure failed or the torturing test failed, and other negative error 552801c135cSArtem B. Bityutskiy * codes in case of other errors. Note, %-EIO means that the physical 553801c135cSArtem B. Bityutskiy * eraseblock is bad. 554801c135cSArtem B. Bityutskiy */ 555e88d6e10SArtem Bityutskiy int ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture) 556801c135cSArtem B. Bityutskiy { 557801c135cSArtem B. Bityutskiy int err, ret = 0; 558801c135cSArtem B. Bityutskiy 559801c135cSArtem B. Bityutskiy ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 560801c135cSArtem B. Bityutskiy 561801c135cSArtem B. Bityutskiy err = paranoid_check_not_bad(ubi, pnum); 562801c135cSArtem B. Bityutskiy if (err != 0) 563adbf05e3SArtem Bityutskiy return err; 564801c135cSArtem B. Bityutskiy 565801c135cSArtem B. Bityutskiy if (ubi->ro_mode) { 566801c135cSArtem B. Bityutskiy ubi_err("read-only mode"); 567801c135cSArtem B. Bityutskiy return -EROFS; 568801c135cSArtem B. Bityutskiy } 569801c135cSArtem B. Bityutskiy 570ebf53f42SArtem Bityutskiy if (ubi->nor_flash) { 571ebf53f42SArtem Bityutskiy err = nor_erase_prepare(ubi, pnum); 572ebf53f42SArtem Bityutskiy if (err) 573ebf53f42SArtem Bityutskiy return err; 574ebf53f42SArtem Bityutskiy } 575ebf53f42SArtem Bityutskiy 576801c135cSArtem B. Bityutskiy if (torture) { 577801c135cSArtem B. Bityutskiy ret = torture_peb(ubi, pnum); 578801c135cSArtem B. Bityutskiy if (ret < 0) 579801c135cSArtem B. Bityutskiy return ret; 580801c135cSArtem B. Bityutskiy } 581801c135cSArtem B. Bityutskiy 582801c135cSArtem B. Bityutskiy err = do_sync_erase(ubi, pnum); 583801c135cSArtem B. Bityutskiy if (err) 584801c135cSArtem B. Bityutskiy return err; 585801c135cSArtem B. Bityutskiy 586801c135cSArtem B. Bityutskiy return ret + 1; 587801c135cSArtem B. Bityutskiy } 588801c135cSArtem B. Bityutskiy 589801c135cSArtem B. Bityutskiy /** 590801c135cSArtem B. Bityutskiy * ubi_io_is_bad - check if a physical eraseblock is bad. 591801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 592801c135cSArtem B. Bityutskiy * @pnum: the physical eraseblock number to check 593801c135cSArtem B. Bityutskiy * 594801c135cSArtem B. Bityutskiy * This function returns a positive number if the physical eraseblock is bad, 595801c135cSArtem B. Bityutskiy * zero if not, and a negative error code if an error occurred. 596801c135cSArtem B. Bityutskiy */ 597801c135cSArtem B. Bityutskiy int ubi_io_is_bad(const struct ubi_device *ubi, int pnum) 598801c135cSArtem B. Bityutskiy { 599801c135cSArtem B. Bityutskiy struct mtd_info *mtd = ubi->mtd; 600801c135cSArtem B. Bityutskiy 601801c135cSArtem B. Bityutskiy ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 602801c135cSArtem B. Bityutskiy 603801c135cSArtem B. Bityutskiy if (ubi->bad_allowed) { 604801c135cSArtem B. Bityutskiy int ret; 605801c135cSArtem B. Bityutskiy 606801c135cSArtem B. Bityutskiy ret = mtd->block_isbad(mtd, (loff_t)pnum * ubi->peb_size); 607801c135cSArtem B. Bityutskiy if (ret < 0) 608801c135cSArtem B. Bityutskiy ubi_err("error %d while checking if PEB %d is bad", 609801c135cSArtem B. Bityutskiy ret, pnum); 610801c135cSArtem B. Bityutskiy else if (ret) 611801c135cSArtem B. Bityutskiy dbg_io("PEB %d is bad", pnum); 612801c135cSArtem B. Bityutskiy return ret; 613801c135cSArtem B. Bityutskiy } 614801c135cSArtem B. Bityutskiy 615801c135cSArtem B. Bityutskiy return 0; 616801c135cSArtem B. Bityutskiy } 617801c135cSArtem B. Bityutskiy 618801c135cSArtem B. Bityutskiy /** 619801c135cSArtem B. Bityutskiy * ubi_io_mark_bad - mark a physical eraseblock as bad. 620801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 621801c135cSArtem B. Bityutskiy * @pnum: the physical eraseblock number to mark 622801c135cSArtem B. Bityutskiy * 623801c135cSArtem B. Bityutskiy * This function returns zero in case of success and a negative error code in 624801c135cSArtem B. Bityutskiy * case of failure. 625801c135cSArtem B. Bityutskiy */ 626801c135cSArtem B. Bityutskiy int ubi_io_mark_bad(const struct ubi_device *ubi, int pnum) 627801c135cSArtem B. Bityutskiy { 628801c135cSArtem B. Bityutskiy int err; 629801c135cSArtem B. Bityutskiy struct mtd_info *mtd = ubi->mtd; 630801c135cSArtem B. Bityutskiy 631801c135cSArtem B. Bityutskiy ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 632801c135cSArtem B. Bityutskiy 633801c135cSArtem B. Bityutskiy if (ubi->ro_mode) { 634801c135cSArtem B. Bityutskiy ubi_err("read-only mode"); 635801c135cSArtem B. Bityutskiy return -EROFS; 636801c135cSArtem B. Bityutskiy } 637801c135cSArtem B. Bityutskiy 638801c135cSArtem B. Bityutskiy if (!ubi->bad_allowed) 639801c135cSArtem B. Bityutskiy return 0; 640801c135cSArtem B. Bityutskiy 641801c135cSArtem B. Bityutskiy err = mtd->block_markbad(mtd, (loff_t)pnum * ubi->peb_size); 642801c135cSArtem B. Bityutskiy if (err) 643801c135cSArtem B. Bityutskiy ubi_err("cannot mark PEB %d bad, error %d", pnum, err); 644801c135cSArtem B. Bityutskiy return err; 645801c135cSArtem B. Bityutskiy } 646801c135cSArtem B. Bityutskiy 647801c135cSArtem B. Bityutskiy /** 648801c135cSArtem B. Bityutskiy * validate_ec_hdr - validate an erase counter header. 649801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 650801c135cSArtem B. Bityutskiy * @ec_hdr: the erase counter header to check 651801c135cSArtem B. Bityutskiy * 652801c135cSArtem B. Bityutskiy * This function returns zero if the erase counter header is OK, and %1 if 653801c135cSArtem B. Bityutskiy * not. 654801c135cSArtem B. Bityutskiy */ 655fe96efc1SArtem Bityutskiy static int validate_ec_hdr(const struct ubi_device *ubi, 656801c135cSArtem B. Bityutskiy const struct ubi_ec_hdr *ec_hdr) 657801c135cSArtem B. Bityutskiy { 658801c135cSArtem B. Bityutskiy long long ec; 659fe96efc1SArtem Bityutskiy int vid_hdr_offset, leb_start; 660801c135cSArtem B. Bityutskiy 6613261ebd7SChristoph Hellwig ec = be64_to_cpu(ec_hdr->ec); 6623261ebd7SChristoph Hellwig vid_hdr_offset = be32_to_cpu(ec_hdr->vid_hdr_offset); 6633261ebd7SChristoph Hellwig leb_start = be32_to_cpu(ec_hdr->data_offset); 664801c135cSArtem B. Bityutskiy 665801c135cSArtem B. Bityutskiy if (ec_hdr->version != UBI_VERSION) { 666801c135cSArtem B. Bityutskiy ubi_err("node with incompatible UBI version found: " 667801c135cSArtem B. Bityutskiy "this UBI version is %d, image version is %d", 668801c135cSArtem B. Bityutskiy UBI_VERSION, (int)ec_hdr->version); 669801c135cSArtem B. Bityutskiy goto bad; 670801c135cSArtem B. Bityutskiy } 671801c135cSArtem B. Bityutskiy 672801c135cSArtem B. Bityutskiy if (vid_hdr_offset != ubi->vid_hdr_offset) { 673801c135cSArtem B. Bityutskiy ubi_err("bad VID header offset %d, expected %d", 674801c135cSArtem B. Bityutskiy vid_hdr_offset, ubi->vid_hdr_offset); 675801c135cSArtem B. Bityutskiy goto bad; 676801c135cSArtem B. Bityutskiy } 677801c135cSArtem B. Bityutskiy 678801c135cSArtem B. Bityutskiy if (leb_start != ubi->leb_start) { 679801c135cSArtem B. Bityutskiy ubi_err("bad data offset %d, expected %d", 680801c135cSArtem B. Bityutskiy leb_start, ubi->leb_start); 681801c135cSArtem B. Bityutskiy goto bad; 682801c135cSArtem B. Bityutskiy } 683801c135cSArtem B. Bityutskiy 684801c135cSArtem B. Bityutskiy if (ec < 0 || ec > UBI_MAX_ERASECOUNTER) { 685801c135cSArtem B. Bityutskiy ubi_err("bad erase counter %lld", ec); 686801c135cSArtem B. Bityutskiy goto bad; 687801c135cSArtem B. Bityutskiy } 688801c135cSArtem B. Bityutskiy 689801c135cSArtem B. Bityutskiy return 0; 690801c135cSArtem B. Bityutskiy 691801c135cSArtem B. Bityutskiy bad: 692801c135cSArtem B. Bityutskiy ubi_err("bad EC header"); 693801c135cSArtem B. Bityutskiy ubi_dbg_dump_ec_hdr(ec_hdr); 694801c135cSArtem B. Bityutskiy ubi_dbg_dump_stack(); 695801c135cSArtem B. Bityutskiy return 1; 696801c135cSArtem B. Bityutskiy } 697801c135cSArtem B. Bityutskiy 698801c135cSArtem B. Bityutskiy /** 699801c135cSArtem B. Bityutskiy * ubi_io_read_ec_hdr - read and check an erase counter header. 700801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 701801c135cSArtem B. Bityutskiy * @pnum: physical eraseblock to read from 702801c135cSArtem B. Bityutskiy * @ec_hdr: a &struct ubi_ec_hdr object where to store the read erase counter 703801c135cSArtem B. Bityutskiy * header 704801c135cSArtem B. Bityutskiy * @verbose: be verbose if the header is corrupted or was not found 705801c135cSArtem B. Bityutskiy * 706801c135cSArtem B. Bityutskiy * This function reads erase counter header from physical eraseblock @pnum and 707801c135cSArtem B. Bityutskiy * stores it in @ec_hdr. This function also checks CRC checksum of the read 708801c135cSArtem B. Bityutskiy * erase counter header. The following codes may be returned: 709801c135cSArtem B. Bityutskiy * 710801c135cSArtem B. Bityutskiy * o %0 if the CRC checksum is correct and the header was successfully read; 711801c135cSArtem B. Bityutskiy * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected 712801c135cSArtem B. Bityutskiy * and corrected by the flash driver; this is harmless but may indicate that 713801c135cSArtem B. Bityutskiy * this eraseblock may become bad soon (but may be not); 714786d7831SArtem Bityutskiy * o %UBI_IO_BAD_HDR if the erase counter header is corrupted (a CRC error); 715801c135cSArtem B. Bityutskiy * o %UBI_IO_PEB_EMPTY if the physical eraseblock is empty; 716801c135cSArtem B. Bityutskiy * o a negative error code in case of failure. 717801c135cSArtem B. Bityutskiy */ 718e88d6e10SArtem Bityutskiy int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum, 719801c135cSArtem B. Bityutskiy struct ubi_ec_hdr *ec_hdr, int verbose) 720801c135cSArtem B. Bityutskiy { 721801c135cSArtem B. Bityutskiy int err, read_err = 0; 722801c135cSArtem B. Bityutskiy uint32_t crc, magic, hdr_crc; 723801c135cSArtem B. Bityutskiy 724801c135cSArtem B. Bityutskiy dbg_io("read EC header from PEB %d", pnum); 725801c135cSArtem B. Bityutskiy ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 726801c135cSArtem B. Bityutskiy 727801c135cSArtem B. Bityutskiy err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE); 728801c135cSArtem B. Bityutskiy if (err) { 729801c135cSArtem B. Bityutskiy if (err != UBI_IO_BITFLIPS && err != -EBADMSG) 730801c135cSArtem B. Bityutskiy return err; 731801c135cSArtem B. Bityutskiy 732801c135cSArtem B. Bityutskiy /* 733801c135cSArtem B. Bityutskiy * We read all the data, but either a correctable bit-flip 734801c135cSArtem B. Bityutskiy * occurred, or MTD reported about some data integrity error, 735801c135cSArtem B. Bityutskiy * like an ECC error in case of NAND. The former is harmless, 736801c135cSArtem B. Bityutskiy * the later may mean that the read data is corrupted. But we 737801c135cSArtem B. Bityutskiy * have a CRC check-sum and we will detect this. If the EC 738801c135cSArtem B. Bityutskiy * header is still OK, we just report this as there was a 739801c135cSArtem B. Bityutskiy * bit-flip. 740801c135cSArtem B. Bityutskiy */ 741eb89580eSArtem Bityutskiy if (err == -EBADMSG) 742eb89580eSArtem Bityutskiy read_err = UBI_IO_BAD_HDR_READ; 743801c135cSArtem B. Bityutskiy } 744801c135cSArtem B. Bityutskiy 7453261ebd7SChristoph Hellwig magic = be32_to_cpu(ec_hdr->magic); 746801c135cSArtem B. Bityutskiy if (magic != UBI_EC_HDR_MAGIC) { 747eb89580eSArtem Bityutskiy if (read_err) 748eb89580eSArtem Bityutskiy return read_err; 749eb89580eSArtem Bityutskiy 750801c135cSArtem B. Bityutskiy /* 751801c135cSArtem B. Bityutskiy * The magic field is wrong. Let's check if we have read all 752801c135cSArtem B. Bityutskiy * 0xFF. If yes, this physical eraseblock is assumed to be 753801c135cSArtem B. Bityutskiy * empty. 754801c135cSArtem B. Bityutskiy */ 755eb89580eSArtem Bityutskiy if (check_pattern(ec_hdr, 0xFF, UBI_EC_HDR_SIZE)) { 756801c135cSArtem B. Bityutskiy /* The physical eraseblock is supposedly empty */ 757801c135cSArtem B. Bityutskiy if (verbose) 758801c135cSArtem B. Bityutskiy ubi_warn("no EC header found at PEB %d, " 759801c135cSArtem B. Bityutskiy "only 0xFF bytes", pnum); 760ed45819fSArtem Bityutskiy else if (UBI_IO_DEBUG) 761ed45819fSArtem Bityutskiy dbg_msg("no EC header found at PEB %d, " 762ed45819fSArtem Bityutskiy "only 0xFF bytes", pnum); 763801c135cSArtem B. Bityutskiy return UBI_IO_PEB_EMPTY; 764801c135cSArtem B. Bityutskiy } 765801c135cSArtem B. Bityutskiy 766801c135cSArtem B. Bityutskiy /* 767801c135cSArtem B. Bityutskiy * This is not a valid erase counter header, and these are not 768801c135cSArtem B. Bityutskiy * 0xFF bytes. Report that the header is corrupted. 769801c135cSArtem B. Bityutskiy */ 770801c135cSArtem B. Bityutskiy if (verbose) { 771801c135cSArtem B. Bityutskiy ubi_warn("bad magic number at PEB %d: %08x instead of " 772801c135cSArtem B. Bityutskiy "%08x", pnum, magic, UBI_EC_HDR_MAGIC); 773801c135cSArtem B. Bityutskiy ubi_dbg_dump_ec_hdr(ec_hdr); 774ed45819fSArtem Bityutskiy } else if (UBI_IO_DEBUG) 775ed45819fSArtem Bityutskiy dbg_msg("bad magic number at PEB %d: %08x instead of " 776ed45819fSArtem Bityutskiy "%08x", pnum, magic, UBI_EC_HDR_MAGIC); 777786d7831SArtem Bityutskiy return UBI_IO_BAD_HDR; 778801c135cSArtem B. Bityutskiy } 779801c135cSArtem B. Bityutskiy 780801c135cSArtem B. Bityutskiy crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC); 7813261ebd7SChristoph Hellwig hdr_crc = be32_to_cpu(ec_hdr->hdr_crc); 782801c135cSArtem B. Bityutskiy 783801c135cSArtem B. Bityutskiy if (hdr_crc != crc) { 784801c135cSArtem B. Bityutskiy if (verbose) { 7859c9ec147SArtem Bityutskiy ubi_warn("bad EC header CRC at PEB %d, calculated " 7869c9ec147SArtem Bityutskiy "%#08x, read %#08x", pnum, crc, hdr_crc); 787801c135cSArtem B. Bityutskiy ubi_dbg_dump_ec_hdr(ec_hdr); 788ed45819fSArtem Bityutskiy } else if (UBI_IO_DEBUG) 789ed45819fSArtem Bityutskiy dbg_msg("bad EC header CRC at PEB %d, calculated " 790ed45819fSArtem Bityutskiy "%#08x, read %#08x", pnum, crc, hdr_crc); 791eb89580eSArtem Bityutskiy return read_err ?: UBI_IO_BAD_HDR; 792801c135cSArtem B. Bityutskiy } 793801c135cSArtem B. Bityutskiy 794801c135cSArtem B. Bityutskiy /* And of course validate what has just been read from the media */ 795801c135cSArtem B. Bityutskiy err = validate_ec_hdr(ubi, ec_hdr); 796801c135cSArtem B. Bityutskiy if (err) { 797801c135cSArtem B. Bityutskiy ubi_err("validation failed for PEB %d", pnum); 798801c135cSArtem B. Bityutskiy return -EINVAL; 799801c135cSArtem B. Bityutskiy } 800801c135cSArtem B. Bityutskiy 801eb89580eSArtem Bityutskiy /* 802eb89580eSArtem Bityutskiy * If there was %-EBADMSG, but the header CRC is still OK, report about 803eb89580eSArtem Bityutskiy * a bit-flip to force scrubbing on this PEB. 804eb89580eSArtem Bityutskiy */ 805801c135cSArtem B. Bityutskiy return read_err ? UBI_IO_BITFLIPS : 0; 806801c135cSArtem B. Bityutskiy } 807801c135cSArtem B. Bityutskiy 808801c135cSArtem B. Bityutskiy /** 809801c135cSArtem B. Bityutskiy * ubi_io_write_ec_hdr - write an erase counter header. 810801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 811801c135cSArtem B. Bityutskiy * @pnum: physical eraseblock to write to 812801c135cSArtem B. Bityutskiy * @ec_hdr: the erase counter header to write 813801c135cSArtem B. Bityutskiy * 814801c135cSArtem B. Bityutskiy * This function writes erase counter header described by @ec_hdr to physical 815801c135cSArtem B. Bityutskiy * eraseblock @pnum. It also fills most fields of @ec_hdr before writing, so 816801c135cSArtem B. Bityutskiy * the caller do not have to fill them. Callers must only fill the @ec_hdr->ec 817801c135cSArtem B. Bityutskiy * field. 818801c135cSArtem B. Bityutskiy * 819801c135cSArtem B. Bityutskiy * This function returns zero in case of success and a negative error code in 820801c135cSArtem B. Bityutskiy * case of failure. If %-EIO is returned, the physical eraseblock most probably 821801c135cSArtem B. Bityutskiy * went bad. 822801c135cSArtem B. Bityutskiy */ 823e88d6e10SArtem Bityutskiy int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum, 824801c135cSArtem B. Bityutskiy struct ubi_ec_hdr *ec_hdr) 825801c135cSArtem B. Bityutskiy { 826801c135cSArtem B. Bityutskiy int err; 827801c135cSArtem B. Bityutskiy uint32_t crc; 828801c135cSArtem B. Bityutskiy 829801c135cSArtem B. Bityutskiy dbg_io("write EC header to PEB %d", pnum); 830801c135cSArtem B. Bityutskiy ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 831801c135cSArtem B. Bityutskiy 8323261ebd7SChristoph Hellwig ec_hdr->magic = cpu_to_be32(UBI_EC_HDR_MAGIC); 833801c135cSArtem B. Bityutskiy ec_hdr->version = UBI_VERSION; 8343261ebd7SChristoph Hellwig ec_hdr->vid_hdr_offset = cpu_to_be32(ubi->vid_hdr_offset); 8353261ebd7SChristoph Hellwig ec_hdr->data_offset = cpu_to_be32(ubi->leb_start); 8360c6c7fa1SAdrian Hunter ec_hdr->image_seq = cpu_to_be32(ubi->image_seq); 837801c135cSArtem B. Bityutskiy crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC); 8383261ebd7SChristoph Hellwig ec_hdr->hdr_crc = cpu_to_be32(crc); 839801c135cSArtem B. Bityutskiy 840801c135cSArtem B. Bityutskiy err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr); 841801c135cSArtem B. Bityutskiy if (err) 842adbf05e3SArtem Bityutskiy return err; 843801c135cSArtem B. Bityutskiy 844801c135cSArtem B. Bityutskiy err = ubi_io_write(ubi, ec_hdr, pnum, 0, ubi->ec_hdr_alsize); 845801c135cSArtem B. Bityutskiy return err; 846801c135cSArtem B. Bityutskiy } 847801c135cSArtem B. Bityutskiy 848801c135cSArtem B. Bityutskiy /** 849801c135cSArtem B. Bityutskiy * validate_vid_hdr - validate a volume identifier header. 850801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 851801c135cSArtem B. Bityutskiy * @vid_hdr: the volume identifier header to check 852801c135cSArtem B. Bityutskiy * 853801c135cSArtem B. Bityutskiy * This function checks that data stored in the volume identifier header 854801c135cSArtem B. Bityutskiy * @vid_hdr. Returns zero if the VID header is OK and %1 if not. 855801c135cSArtem B. Bityutskiy */ 856801c135cSArtem B. Bityutskiy static int validate_vid_hdr(const struct ubi_device *ubi, 857801c135cSArtem B. Bityutskiy const struct ubi_vid_hdr *vid_hdr) 858801c135cSArtem B. Bityutskiy { 859801c135cSArtem B. Bityutskiy int vol_type = vid_hdr->vol_type; 860801c135cSArtem B. Bityutskiy int copy_flag = vid_hdr->copy_flag; 8613261ebd7SChristoph Hellwig int vol_id = be32_to_cpu(vid_hdr->vol_id); 8623261ebd7SChristoph Hellwig int lnum = be32_to_cpu(vid_hdr->lnum); 863801c135cSArtem B. Bityutskiy int compat = vid_hdr->compat; 8643261ebd7SChristoph Hellwig int data_size = be32_to_cpu(vid_hdr->data_size); 8653261ebd7SChristoph Hellwig int used_ebs = be32_to_cpu(vid_hdr->used_ebs); 8663261ebd7SChristoph Hellwig int data_pad = be32_to_cpu(vid_hdr->data_pad); 8673261ebd7SChristoph Hellwig int data_crc = be32_to_cpu(vid_hdr->data_crc); 868801c135cSArtem B. Bityutskiy int usable_leb_size = ubi->leb_size - data_pad; 869801c135cSArtem B. Bityutskiy 870801c135cSArtem B. Bityutskiy if (copy_flag != 0 && copy_flag != 1) { 871801c135cSArtem B. Bityutskiy dbg_err("bad copy_flag"); 872801c135cSArtem B. Bityutskiy goto bad; 873801c135cSArtem B. Bityutskiy } 874801c135cSArtem B. Bityutskiy 875801c135cSArtem B. Bityutskiy if (vol_id < 0 || lnum < 0 || data_size < 0 || used_ebs < 0 || 876801c135cSArtem B. Bityutskiy data_pad < 0) { 877801c135cSArtem B. Bityutskiy dbg_err("negative values"); 878801c135cSArtem B. Bityutskiy goto bad; 879801c135cSArtem B. Bityutskiy } 880801c135cSArtem B. Bityutskiy 881801c135cSArtem B. Bityutskiy if (vol_id >= UBI_MAX_VOLUMES && vol_id < UBI_INTERNAL_VOL_START) { 882801c135cSArtem B. Bityutskiy dbg_err("bad vol_id"); 883801c135cSArtem B. Bityutskiy goto bad; 884801c135cSArtem B. Bityutskiy } 885801c135cSArtem B. Bityutskiy 886801c135cSArtem B. Bityutskiy if (vol_id < UBI_INTERNAL_VOL_START && compat != 0) { 887801c135cSArtem B. Bityutskiy dbg_err("bad compat"); 888801c135cSArtem B. Bityutskiy goto bad; 889801c135cSArtem B. Bityutskiy } 890801c135cSArtem B. Bityutskiy 891801c135cSArtem B. Bityutskiy if (vol_id >= UBI_INTERNAL_VOL_START && compat != UBI_COMPAT_DELETE && 892801c135cSArtem B. Bityutskiy compat != UBI_COMPAT_RO && compat != UBI_COMPAT_PRESERVE && 893801c135cSArtem B. Bityutskiy compat != UBI_COMPAT_REJECT) { 894801c135cSArtem B. Bityutskiy dbg_err("bad compat"); 895801c135cSArtem B. Bityutskiy goto bad; 896801c135cSArtem B. Bityutskiy } 897801c135cSArtem B. Bityutskiy 898801c135cSArtem B. Bityutskiy if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) { 899801c135cSArtem B. Bityutskiy dbg_err("bad vol_type"); 900801c135cSArtem B. Bityutskiy goto bad; 901801c135cSArtem B. Bityutskiy } 902801c135cSArtem B. Bityutskiy 903801c135cSArtem B. Bityutskiy if (data_pad >= ubi->leb_size / 2) { 904801c135cSArtem B. Bityutskiy dbg_err("bad data_pad"); 905801c135cSArtem B. Bityutskiy goto bad; 906801c135cSArtem B. Bityutskiy } 907801c135cSArtem B. Bityutskiy 908801c135cSArtem B. Bityutskiy if (vol_type == UBI_VID_STATIC) { 909801c135cSArtem B. Bityutskiy /* 910801c135cSArtem B. Bityutskiy * Although from high-level point of view static volumes may 911801c135cSArtem B. Bityutskiy * contain zero bytes of data, but no VID headers can contain 912801c135cSArtem B. Bityutskiy * zero at these fields, because they empty volumes do not have 913801c135cSArtem B. Bityutskiy * mapped logical eraseblocks. 914801c135cSArtem B. Bityutskiy */ 915801c135cSArtem B. Bityutskiy if (used_ebs == 0) { 916801c135cSArtem B. Bityutskiy dbg_err("zero used_ebs"); 917801c135cSArtem B. Bityutskiy goto bad; 918801c135cSArtem B. Bityutskiy } 919801c135cSArtem B. Bityutskiy if (data_size == 0) { 920801c135cSArtem B. Bityutskiy dbg_err("zero data_size"); 921801c135cSArtem B. Bityutskiy goto bad; 922801c135cSArtem B. Bityutskiy } 923801c135cSArtem B. Bityutskiy if (lnum < used_ebs - 1) { 924801c135cSArtem B. Bityutskiy if (data_size != usable_leb_size) { 925801c135cSArtem B. Bityutskiy dbg_err("bad data_size"); 926801c135cSArtem B. Bityutskiy goto bad; 927801c135cSArtem B. Bityutskiy } 928801c135cSArtem B. Bityutskiy } else if (lnum == used_ebs - 1) { 929801c135cSArtem B. Bityutskiy if (data_size == 0) { 930801c135cSArtem B. Bityutskiy dbg_err("bad data_size at last LEB"); 931801c135cSArtem B. Bityutskiy goto bad; 932801c135cSArtem B. Bityutskiy } 933801c135cSArtem B. Bityutskiy } else { 934801c135cSArtem B. Bityutskiy dbg_err("too high lnum"); 935801c135cSArtem B. Bityutskiy goto bad; 936801c135cSArtem B. Bityutskiy } 937801c135cSArtem B. Bityutskiy } else { 938801c135cSArtem B. Bityutskiy if (copy_flag == 0) { 939801c135cSArtem B. Bityutskiy if (data_crc != 0) { 940801c135cSArtem B. Bityutskiy dbg_err("non-zero data CRC"); 941801c135cSArtem B. Bityutskiy goto bad; 942801c135cSArtem B. Bityutskiy } 943801c135cSArtem B. Bityutskiy if (data_size != 0) { 944801c135cSArtem B. Bityutskiy dbg_err("non-zero data_size"); 945801c135cSArtem B. Bityutskiy goto bad; 946801c135cSArtem B. Bityutskiy } 947801c135cSArtem B. Bityutskiy } else { 948801c135cSArtem B. Bityutskiy if (data_size == 0) { 949801c135cSArtem B. Bityutskiy dbg_err("zero data_size of copy"); 950801c135cSArtem B. Bityutskiy goto bad; 951801c135cSArtem B. Bityutskiy } 952801c135cSArtem B. Bityutskiy } 953801c135cSArtem B. Bityutskiy if (used_ebs != 0) { 954801c135cSArtem B. Bityutskiy dbg_err("bad used_ebs"); 955801c135cSArtem B. Bityutskiy goto bad; 956801c135cSArtem B. Bityutskiy } 957801c135cSArtem B. Bityutskiy } 958801c135cSArtem B. Bityutskiy 959801c135cSArtem B. Bityutskiy return 0; 960801c135cSArtem B. Bityutskiy 961801c135cSArtem B. Bityutskiy bad: 962801c135cSArtem B. Bityutskiy ubi_err("bad VID header"); 963801c135cSArtem B. Bityutskiy ubi_dbg_dump_vid_hdr(vid_hdr); 964801c135cSArtem B. Bityutskiy ubi_dbg_dump_stack(); 965801c135cSArtem B. Bityutskiy return 1; 966801c135cSArtem B. Bityutskiy } 967801c135cSArtem B. Bityutskiy 968801c135cSArtem B. Bityutskiy /** 969801c135cSArtem B. Bityutskiy * ubi_io_read_vid_hdr - read and check a volume identifier header. 970801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 971801c135cSArtem B. Bityutskiy * @pnum: physical eraseblock number to read from 972801c135cSArtem B. Bityutskiy * @vid_hdr: &struct ubi_vid_hdr object where to store the read volume 973801c135cSArtem B. Bityutskiy * identifier header 974801c135cSArtem B. Bityutskiy * @verbose: be verbose if the header is corrupted or wasn't found 975801c135cSArtem B. Bityutskiy * 976801c135cSArtem B. Bityutskiy * This function reads the volume identifier header from physical eraseblock 977801c135cSArtem B. Bityutskiy * @pnum and stores it in @vid_hdr. It also checks CRC checksum of the read 978801c135cSArtem B. Bityutskiy * volume identifier header. The following codes may be returned: 979801c135cSArtem B. Bityutskiy * 980801c135cSArtem B. Bityutskiy * o %0 if the CRC checksum is correct and the header was successfully read; 981801c135cSArtem B. Bityutskiy * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected 982801c135cSArtem B. Bityutskiy * and corrected by the flash driver; this is harmless but may indicate that 983801c135cSArtem B. Bityutskiy * this eraseblock may become bad soon; 984786d7831SArtem Bityutskiy * o %UBI_IO_BAD_HDR if the volume identifier header is corrupted (a CRC 985801c135cSArtem B. Bityutskiy * error detected); 986801c135cSArtem B. Bityutskiy * o %UBI_IO_PEB_FREE if the physical eraseblock is free (i.e., there is no VID 987801c135cSArtem B. Bityutskiy * header there); 988801c135cSArtem B. Bityutskiy * o a negative error code in case of failure. 989801c135cSArtem B. Bityutskiy */ 990e88d6e10SArtem Bityutskiy int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum, 991801c135cSArtem B. Bityutskiy struct ubi_vid_hdr *vid_hdr, int verbose) 992801c135cSArtem B. Bityutskiy { 993801c135cSArtem B. Bityutskiy int err, read_err = 0; 994801c135cSArtem B. Bityutskiy uint32_t crc, magic, hdr_crc; 995801c135cSArtem B. Bityutskiy void *p; 996801c135cSArtem B. Bityutskiy 997801c135cSArtem B. Bityutskiy dbg_io("read VID header from PEB %d", pnum); 998801c135cSArtem B. Bityutskiy ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 999801c135cSArtem B. Bityutskiy 1000801c135cSArtem B. Bityutskiy p = (char *)vid_hdr - ubi->vid_hdr_shift; 1001801c135cSArtem B. Bityutskiy err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset, 1002801c135cSArtem B. Bityutskiy ubi->vid_hdr_alsize); 1003801c135cSArtem B. Bityutskiy if (err) { 1004801c135cSArtem B. Bityutskiy if (err != UBI_IO_BITFLIPS && err != -EBADMSG) 1005801c135cSArtem B. Bityutskiy return err; 1006801c135cSArtem B. Bityutskiy 1007801c135cSArtem B. Bityutskiy /* 1008801c135cSArtem B. Bityutskiy * We read all the data, but either a correctable bit-flip 1009801c135cSArtem B. Bityutskiy * occurred, or MTD reported about some data integrity error, 1010801c135cSArtem B. Bityutskiy * like an ECC error in case of NAND. The former is harmless, 1011801c135cSArtem B. Bityutskiy * the later may mean the read data is corrupted. But we have a 1012801c135cSArtem B. Bityutskiy * CRC check-sum and we will identify this. If the VID header is 1013801c135cSArtem B. Bityutskiy * still OK, we just report this as there was a bit-flip. 1014801c135cSArtem B. Bityutskiy */ 1015eb89580eSArtem Bityutskiy if (err == -EBADMSG) 1016eb89580eSArtem Bityutskiy read_err = UBI_IO_BAD_HDR_READ; 1017801c135cSArtem B. Bityutskiy } 1018801c135cSArtem B. Bityutskiy 10193261ebd7SChristoph Hellwig magic = be32_to_cpu(vid_hdr->magic); 1020801c135cSArtem B. Bityutskiy if (magic != UBI_VID_HDR_MAGIC) { 1021eb89580eSArtem Bityutskiy if (read_err) 1022eb89580eSArtem Bityutskiy return read_err; 1023eb89580eSArtem Bityutskiy 1024801c135cSArtem B. Bityutskiy /* 1025801c135cSArtem B. Bityutskiy * If we have read all 0xFF bytes, the VID header probably does 1026801c135cSArtem B. Bityutskiy * not exist and the physical eraseblock is assumed to be free. 1027801c135cSArtem B. Bityutskiy */ 1028eb89580eSArtem Bityutskiy if (check_pattern(vid_hdr, 0xFF, UBI_VID_HDR_SIZE)) { 1029801c135cSArtem B. Bityutskiy /* The physical eraseblock is supposedly free */ 1030801c135cSArtem B. Bityutskiy if (verbose) 1031801c135cSArtem B. Bityutskiy ubi_warn("no VID header found at PEB %d, " 1032801c135cSArtem B. Bityutskiy "only 0xFF bytes", pnum); 1033ed45819fSArtem Bityutskiy else if (UBI_IO_DEBUG) 1034ed45819fSArtem Bityutskiy dbg_msg("no VID header found at PEB %d, " 1035ed45819fSArtem Bityutskiy "only 0xFF bytes", pnum); 1036801c135cSArtem B. Bityutskiy return UBI_IO_PEB_FREE; 1037801c135cSArtem B. Bityutskiy } 1038801c135cSArtem B. Bityutskiy 1039801c135cSArtem B. Bityutskiy /* 1040801c135cSArtem B. Bityutskiy * This is not a valid VID header, and these are not 0xFF 1041801c135cSArtem B. Bityutskiy * bytes. Report that the header is corrupted. 1042801c135cSArtem B. Bityutskiy */ 1043801c135cSArtem B. Bityutskiy if (verbose) { 1044801c135cSArtem B. Bityutskiy ubi_warn("bad magic number at PEB %d: %08x instead of " 1045801c135cSArtem B. Bityutskiy "%08x", pnum, magic, UBI_VID_HDR_MAGIC); 1046801c135cSArtem B. Bityutskiy ubi_dbg_dump_vid_hdr(vid_hdr); 1047ed45819fSArtem Bityutskiy } else if (UBI_IO_DEBUG) 1048ed45819fSArtem Bityutskiy dbg_msg("bad magic number at PEB %d: %08x instead of " 1049ed45819fSArtem Bityutskiy "%08x", pnum, magic, UBI_VID_HDR_MAGIC); 1050786d7831SArtem Bityutskiy return UBI_IO_BAD_HDR; 1051801c135cSArtem B. Bityutskiy } 1052801c135cSArtem B. Bityutskiy 1053801c135cSArtem B. Bityutskiy crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC); 10543261ebd7SChristoph Hellwig hdr_crc = be32_to_cpu(vid_hdr->hdr_crc); 1055801c135cSArtem B. Bityutskiy 1056801c135cSArtem B. Bityutskiy if (hdr_crc != crc) { 1057801c135cSArtem B. Bityutskiy if (verbose) { 1058801c135cSArtem B. Bityutskiy ubi_warn("bad CRC at PEB %d, calculated %#08x, " 1059801c135cSArtem B. Bityutskiy "read %#08x", pnum, crc, hdr_crc); 1060801c135cSArtem B. Bityutskiy ubi_dbg_dump_vid_hdr(vid_hdr); 1061ed45819fSArtem Bityutskiy } else if (UBI_IO_DEBUG) 1062ed45819fSArtem Bityutskiy dbg_msg("bad CRC at PEB %d, calculated %#08x, " 1063ed45819fSArtem Bityutskiy "read %#08x", pnum, crc, hdr_crc); 1064eb89580eSArtem Bityutskiy return read_err ?: UBI_IO_BAD_HDR; 1065801c135cSArtem B. Bityutskiy } 1066801c135cSArtem B. Bityutskiy 1067801c135cSArtem B. Bityutskiy /* Validate the VID header that we have just read */ 1068801c135cSArtem B. Bityutskiy err = validate_vid_hdr(ubi, vid_hdr); 1069801c135cSArtem B. Bityutskiy if (err) { 1070801c135cSArtem B. Bityutskiy ubi_err("validation failed for PEB %d", pnum); 1071801c135cSArtem B. Bityutskiy return -EINVAL; 1072801c135cSArtem B. Bityutskiy } 1073801c135cSArtem B. Bityutskiy 1074eb89580eSArtem Bityutskiy /* 1075eb89580eSArtem Bityutskiy * If there was a read error (%-EBADMSG), but the header CRC is still 1076eb89580eSArtem Bityutskiy * OK, report about a bit-flip to force scrubbing on this PEB. 1077eb89580eSArtem Bityutskiy */ 1078801c135cSArtem B. Bityutskiy return read_err ? UBI_IO_BITFLIPS : 0; 1079801c135cSArtem B. Bityutskiy } 1080801c135cSArtem B. Bityutskiy 1081801c135cSArtem B. Bityutskiy /** 1082801c135cSArtem B. Bityutskiy * ubi_io_write_vid_hdr - write a volume identifier header. 1083801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 1084801c135cSArtem B. Bityutskiy * @pnum: the physical eraseblock number to write to 1085801c135cSArtem B. Bityutskiy * @vid_hdr: the volume identifier header to write 1086801c135cSArtem B. Bityutskiy * 1087801c135cSArtem B. Bityutskiy * This function writes the volume identifier header described by @vid_hdr to 1088801c135cSArtem B. Bityutskiy * physical eraseblock @pnum. This function automatically fills the 1089801c135cSArtem B. Bityutskiy * @vid_hdr->magic and the @vid_hdr->version fields, as well as calculates 1090801c135cSArtem B. Bityutskiy * header CRC checksum and stores it at vid_hdr->hdr_crc. 1091801c135cSArtem B. Bityutskiy * 1092801c135cSArtem B. Bityutskiy * This function returns zero in case of success and a negative error code in 1093801c135cSArtem B. Bityutskiy * case of failure. If %-EIO is returned, the physical eraseblock probably went 1094801c135cSArtem B. Bityutskiy * bad. 1095801c135cSArtem B. Bityutskiy */ 1096e88d6e10SArtem Bityutskiy int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum, 1097801c135cSArtem B. Bityutskiy struct ubi_vid_hdr *vid_hdr) 1098801c135cSArtem B. Bityutskiy { 1099801c135cSArtem B. Bityutskiy int err; 1100801c135cSArtem B. Bityutskiy uint32_t crc; 1101801c135cSArtem B. Bityutskiy void *p; 1102801c135cSArtem B. Bityutskiy 1103801c135cSArtem B. Bityutskiy dbg_io("write VID header to PEB %d", pnum); 1104801c135cSArtem B. Bityutskiy ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 1105801c135cSArtem B. Bityutskiy 1106801c135cSArtem B. Bityutskiy err = paranoid_check_peb_ec_hdr(ubi, pnum); 1107801c135cSArtem B. Bityutskiy if (err) 1108adbf05e3SArtem Bityutskiy return err; 1109801c135cSArtem B. Bityutskiy 11103261ebd7SChristoph Hellwig vid_hdr->magic = cpu_to_be32(UBI_VID_HDR_MAGIC); 1111801c135cSArtem B. Bityutskiy vid_hdr->version = UBI_VERSION; 1112801c135cSArtem B. Bityutskiy crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC); 11133261ebd7SChristoph Hellwig vid_hdr->hdr_crc = cpu_to_be32(crc); 1114801c135cSArtem B. Bityutskiy 1115801c135cSArtem B. Bityutskiy err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr); 1116801c135cSArtem B. Bityutskiy if (err) 1117adbf05e3SArtem Bityutskiy return err; 1118801c135cSArtem B. Bityutskiy 1119801c135cSArtem B. Bityutskiy p = (char *)vid_hdr - ubi->vid_hdr_shift; 1120801c135cSArtem B. Bityutskiy err = ubi_io_write(ubi, p, pnum, ubi->vid_hdr_aloffset, 1121801c135cSArtem B. Bityutskiy ubi->vid_hdr_alsize); 1122801c135cSArtem B. Bityutskiy return err; 1123801c135cSArtem B. Bityutskiy } 1124801c135cSArtem B. Bityutskiy 1125801c135cSArtem B. Bityutskiy #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID 1126801c135cSArtem B. Bityutskiy 1127801c135cSArtem B. Bityutskiy /** 1128801c135cSArtem B. Bityutskiy * paranoid_check_not_bad - ensure that a physical eraseblock is not bad. 1129801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 1130801c135cSArtem B. Bityutskiy * @pnum: physical eraseblock number to check 1131801c135cSArtem B. Bityutskiy * 1132adbf05e3SArtem Bityutskiy * This function returns zero if the physical eraseblock is good, %-EINVAL if 1133adbf05e3SArtem Bityutskiy * it is bad and a negative error code if an error occurred. 1134801c135cSArtem B. Bityutskiy */ 1135801c135cSArtem B. Bityutskiy static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum) 1136801c135cSArtem B. Bityutskiy { 1137801c135cSArtem B. Bityutskiy int err; 1138801c135cSArtem B. Bityutskiy 1139801c135cSArtem B. Bityutskiy err = ubi_io_is_bad(ubi, pnum); 1140801c135cSArtem B. Bityutskiy if (!err) 1141801c135cSArtem B. Bityutskiy return err; 1142801c135cSArtem B. Bityutskiy 1143801c135cSArtem B. Bityutskiy ubi_err("paranoid check failed for PEB %d", pnum); 1144801c135cSArtem B. Bityutskiy ubi_dbg_dump_stack(); 1145adbf05e3SArtem Bityutskiy return err > 0 ? -EINVAL : err; 1146801c135cSArtem B. Bityutskiy } 1147801c135cSArtem B. Bityutskiy 1148801c135cSArtem B. Bityutskiy /** 1149801c135cSArtem B. Bityutskiy * paranoid_check_ec_hdr - check if an erase counter header is all right. 1150801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 1151801c135cSArtem B. Bityutskiy * @pnum: physical eraseblock number the erase counter header belongs to 1152801c135cSArtem B. Bityutskiy * @ec_hdr: the erase counter header to check 1153801c135cSArtem B. Bityutskiy * 1154801c135cSArtem B. Bityutskiy * This function returns zero if the erase counter header contains valid 1155adbf05e3SArtem Bityutskiy * values, and %-EINVAL if not. 1156801c135cSArtem B. Bityutskiy */ 1157801c135cSArtem B. Bityutskiy static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum, 1158801c135cSArtem B. Bityutskiy const struct ubi_ec_hdr *ec_hdr) 1159801c135cSArtem B. Bityutskiy { 1160801c135cSArtem B. Bityutskiy int err; 1161801c135cSArtem B. Bityutskiy uint32_t magic; 1162801c135cSArtem B. Bityutskiy 11633261ebd7SChristoph Hellwig magic = be32_to_cpu(ec_hdr->magic); 1164801c135cSArtem B. Bityutskiy if (magic != UBI_EC_HDR_MAGIC) { 1165801c135cSArtem B. Bityutskiy ubi_err("bad magic %#08x, must be %#08x", 1166801c135cSArtem B. Bityutskiy magic, UBI_EC_HDR_MAGIC); 1167801c135cSArtem B. Bityutskiy goto fail; 1168801c135cSArtem B. Bityutskiy } 1169801c135cSArtem B. Bityutskiy 1170801c135cSArtem B. Bityutskiy err = validate_ec_hdr(ubi, ec_hdr); 1171801c135cSArtem B. Bityutskiy if (err) { 1172801c135cSArtem B. Bityutskiy ubi_err("paranoid check failed for PEB %d", pnum); 1173801c135cSArtem B. Bityutskiy goto fail; 1174801c135cSArtem B. Bityutskiy } 1175801c135cSArtem B. Bityutskiy 1176801c135cSArtem B. Bityutskiy return 0; 1177801c135cSArtem B. Bityutskiy 1178801c135cSArtem B. Bityutskiy fail: 1179801c135cSArtem B. Bityutskiy ubi_dbg_dump_ec_hdr(ec_hdr); 1180801c135cSArtem B. Bityutskiy ubi_dbg_dump_stack(); 1181adbf05e3SArtem Bityutskiy return -EINVAL; 1182801c135cSArtem B. Bityutskiy } 1183801c135cSArtem B. Bityutskiy 1184801c135cSArtem B. Bityutskiy /** 1185ebaaf1afSArtem Bityutskiy * paranoid_check_peb_ec_hdr - check erase counter header. 1186801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 1187801c135cSArtem B. Bityutskiy * @pnum: the physical eraseblock number to check 1188801c135cSArtem B. Bityutskiy * 1189adbf05e3SArtem Bityutskiy * This function returns zero if the erase counter header is all right and and 1190adbf05e3SArtem Bityutskiy * a negative error code if not or if an error occurred. 1191801c135cSArtem B. Bityutskiy */ 1192801c135cSArtem B. Bityutskiy static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum) 1193801c135cSArtem B. Bityutskiy { 1194801c135cSArtem B. Bityutskiy int err; 1195801c135cSArtem B. Bityutskiy uint32_t crc, hdr_crc; 1196801c135cSArtem B. Bityutskiy struct ubi_ec_hdr *ec_hdr; 1197801c135cSArtem B. Bityutskiy 119833818bbbSArtem Bityutskiy ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS); 1199801c135cSArtem B. Bityutskiy if (!ec_hdr) 1200801c135cSArtem B. Bityutskiy return -ENOMEM; 1201801c135cSArtem B. Bityutskiy 1202801c135cSArtem B. Bityutskiy err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE); 1203801c135cSArtem B. Bityutskiy if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG) 1204801c135cSArtem B. Bityutskiy goto exit; 1205801c135cSArtem B. Bityutskiy 1206801c135cSArtem B. Bityutskiy crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC); 12073261ebd7SChristoph Hellwig hdr_crc = be32_to_cpu(ec_hdr->hdr_crc); 1208801c135cSArtem B. Bityutskiy if (hdr_crc != crc) { 1209801c135cSArtem B. Bityutskiy ubi_err("bad CRC, calculated %#08x, read %#08x", crc, hdr_crc); 1210801c135cSArtem B. Bityutskiy ubi_err("paranoid check failed for PEB %d", pnum); 1211801c135cSArtem B. Bityutskiy ubi_dbg_dump_ec_hdr(ec_hdr); 1212801c135cSArtem B. Bityutskiy ubi_dbg_dump_stack(); 1213adbf05e3SArtem Bityutskiy err = -EINVAL; 1214801c135cSArtem B. Bityutskiy goto exit; 1215801c135cSArtem B. Bityutskiy } 1216801c135cSArtem B. Bityutskiy 1217801c135cSArtem B. Bityutskiy err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr); 1218801c135cSArtem B. Bityutskiy 1219801c135cSArtem B. Bityutskiy exit: 1220801c135cSArtem B. Bityutskiy kfree(ec_hdr); 1221801c135cSArtem B. Bityutskiy return err; 1222801c135cSArtem B. Bityutskiy } 1223801c135cSArtem B. Bityutskiy 1224801c135cSArtem B. Bityutskiy /** 1225801c135cSArtem B. Bityutskiy * paranoid_check_vid_hdr - check that a volume identifier header is all right. 1226801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 1227801c135cSArtem B. Bityutskiy * @pnum: physical eraseblock number the volume identifier header belongs to 1228801c135cSArtem B. Bityutskiy * @vid_hdr: the volume identifier header to check 1229801c135cSArtem B. Bityutskiy * 1230801c135cSArtem B. Bityutskiy * This function returns zero if the volume identifier header is all right, and 1231adbf05e3SArtem Bityutskiy * %-EINVAL if not. 1232801c135cSArtem B. Bityutskiy */ 1233801c135cSArtem B. Bityutskiy static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum, 1234801c135cSArtem B. Bityutskiy const struct ubi_vid_hdr *vid_hdr) 1235801c135cSArtem B. Bityutskiy { 1236801c135cSArtem B. Bityutskiy int err; 1237801c135cSArtem B. Bityutskiy uint32_t magic; 1238801c135cSArtem B. Bityutskiy 12393261ebd7SChristoph Hellwig magic = be32_to_cpu(vid_hdr->magic); 1240801c135cSArtem B. Bityutskiy if (magic != UBI_VID_HDR_MAGIC) { 1241801c135cSArtem B. Bityutskiy ubi_err("bad VID header magic %#08x at PEB %d, must be %#08x", 1242801c135cSArtem B. Bityutskiy magic, pnum, UBI_VID_HDR_MAGIC); 1243801c135cSArtem B. Bityutskiy goto fail; 1244801c135cSArtem B. Bityutskiy } 1245801c135cSArtem B. Bityutskiy 1246801c135cSArtem B. Bityutskiy err = validate_vid_hdr(ubi, vid_hdr); 1247801c135cSArtem B. Bityutskiy if (err) { 1248801c135cSArtem B. Bityutskiy ubi_err("paranoid check failed for PEB %d", pnum); 1249801c135cSArtem B. Bityutskiy goto fail; 1250801c135cSArtem B. Bityutskiy } 1251801c135cSArtem B. Bityutskiy 1252801c135cSArtem B. Bityutskiy return err; 1253801c135cSArtem B. Bityutskiy 1254801c135cSArtem B. Bityutskiy fail: 1255801c135cSArtem B. Bityutskiy ubi_err("paranoid check failed for PEB %d", pnum); 1256801c135cSArtem B. Bityutskiy ubi_dbg_dump_vid_hdr(vid_hdr); 1257801c135cSArtem B. Bityutskiy ubi_dbg_dump_stack(); 1258adbf05e3SArtem Bityutskiy return -EINVAL; 1259801c135cSArtem B. Bityutskiy 1260801c135cSArtem B. Bityutskiy } 1261801c135cSArtem B. Bityutskiy 1262801c135cSArtem B. Bityutskiy /** 1263ebaaf1afSArtem Bityutskiy * paranoid_check_peb_vid_hdr - check volume identifier header. 1264801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 1265801c135cSArtem B. Bityutskiy * @pnum: the physical eraseblock number to check 1266801c135cSArtem B. Bityutskiy * 1267801c135cSArtem B. Bityutskiy * This function returns zero if the volume identifier header is all right, 1268adbf05e3SArtem Bityutskiy * and a negative error code if not or if an error occurred. 1269801c135cSArtem B. Bityutskiy */ 1270801c135cSArtem B. Bityutskiy static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum) 1271801c135cSArtem B. Bityutskiy { 1272801c135cSArtem B. Bityutskiy int err; 1273801c135cSArtem B. Bityutskiy uint32_t crc, hdr_crc; 1274801c135cSArtem B. Bityutskiy struct ubi_vid_hdr *vid_hdr; 1275801c135cSArtem B. Bityutskiy void *p; 1276801c135cSArtem B. Bityutskiy 127733818bbbSArtem Bityutskiy vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS); 1278801c135cSArtem B. Bityutskiy if (!vid_hdr) 1279801c135cSArtem B. Bityutskiy return -ENOMEM; 1280801c135cSArtem B. Bityutskiy 1281801c135cSArtem B. Bityutskiy p = (char *)vid_hdr - ubi->vid_hdr_shift; 1282801c135cSArtem B. Bityutskiy err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset, 1283801c135cSArtem B. Bityutskiy ubi->vid_hdr_alsize); 1284801c135cSArtem B. Bityutskiy if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG) 1285801c135cSArtem B. Bityutskiy goto exit; 1286801c135cSArtem B. Bityutskiy 1287801c135cSArtem B. Bityutskiy crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_EC_HDR_SIZE_CRC); 12883261ebd7SChristoph Hellwig hdr_crc = be32_to_cpu(vid_hdr->hdr_crc); 1289801c135cSArtem B. Bityutskiy if (hdr_crc != crc) { 1290801c135cSArtem B. Bityutskiy ubi_err("bad VID header CRC at PEB %d, calculated %#08x, " 1291801c135cSArtem B. Bityutskiy "read %#08x", pnum, crc, hdr_crc); 1292801c135cSArtem B. Bityutskiy ubi_err("paranoid check failed for PEB %d", pnum); 1293801c135cSArtem B. Bityutskiy ubi_dbg_dump_vid_hdr(vid_hdr); 1294801c135cSArtem B. Bityutskiy ubi_dbg_dump_stack(); 1295adbf05e3SArtem Bityutskiy err = -EINVAL; 1296801c135cSArtem B. Bityutskiy goto exit; 1297801c135cSArtem B. Bityutskiy } 1298801c135cSArtem B. Bityutskiy 1299801c135cSArtem B. Bityutskiy err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr); 1300801c135cSArtem B. Bityutskiy 1301801c135cSArtem B. Bityutskiy exit: 1302801c135cSArtem B. Bityutskiy ubi_free_vid_hdr(ubi, vid_hdr); 1303801c135cSArtem B. Bityutskiy return err; 1304801c135cSArtem B. Bityutskiy } 1305801c135cSArtem B. Bityutskiy 1306801c135cSArtem B. Bityutskiy /** 13076e9065d7SArtem Bityutskiy * ubi_dbg_check_write - make sure write succeeded. 13086e9065d7SArtem Bityutskiy * @ubi: UBI device description object 13096e9065d7SArtem Bityutskiy * @buf: buffer with data which were written 13106e9065d7SArtem Bityutskiy * @pnum: physical eraseblock number the data were written to 13116e9065d7SArtem Bityutskiy * @offset: offset within the physical eraseblock the data were written to 13126e9065d7SArtem Bityutskiy * @len: how many bytes were written 13136e9065d7SArtem Bityutskiy * 13146e9065d7SArtem Bityutskiy * This functions reads data which were recently written and compares it with 13156e9065d7SArtem Bityutskiy * the original data buffer - the data have to match. Returns zero if the data 13166e9065d7SArtem Bityutskiy * match and a negative error code if not or in case of failure. 13176e9065d7SArtem Bityutskiy */ 13186e9065d7SArtem Bityutskiy int ubi_dbg_check_write(struct ubi_device *ubi, const void *buf, int pnum, 13196e9065d7SArtem Bityutskiy int offset, int len) 13206e9065d7SArtem Bityutskiy { 13216e9065d7SArtem Bityutskiy int err, i; 13226e9065d7SArtem Bityutskiy 13236e9065d7SArtem Bityutskiy mutex_lock(&ubi->dbg_buf_mutex); 13246e9065d7SArtem Bityutskiy err = ubi_io_read(ubi, ubi->dbg_peb_buf, pnum, offset, len); 13256e9065d7SArtem Bityutskiy if (err) 13266e9065d7SArtem Bityutskiy goto out_unlock; 13276e9065d7SArtem Bityutskiy 13286e9065d7SArtem Bityutskiy for (i = 0; i < len; i++) { 13296e9065d7SArtem Bityutskiy uint8_t c = ((uint8_t *)buf)[i]; 13306e9065d7SArtem Bityutskiy uint8_t c1 = ((uint8_t *)ubi->dbg_peb_buf)[i]; 13316e9065d7SArtem Bityutskiy int dump_len; 13326e9065d7SArtem Bityutskiy 13336e9065d7SArtem Bityutskiy if (c == c1) 13346e9065d7SArtem Bityutskiy continue; 13356e9065d7SArtem Bityutskiy 13366e9065d7SArtem Bityutskiy ubi_err("paranoid check failed for PEB %d:%d, len %d", 13376e9065d7SArtem Bityutskiy pnum, offset, len); 13386e9065d7SArtem Bityutskiy ubi_msg("data differ at position %d", i); 13396e9065d7SArtem Bityutskiy dump_len = max_t(int, 128, len - i); 13406e9065d7SArtem Bityutskiy ubi_msg("hex dump of the original buffer from %d to %d", 13416e9065d7SArtem Bityutskiy i, i + dump_len); 13426e9065d7SArtem Bityutskiy print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, 13436e9065d7SArtem Bityutskiy buf + i, dump_len, 1); 13446e9065d7SArtem Bityutskiy ubi_msg("hex dump of the read buffer from %d to %d", 13456e9065d7SArtem Bityutskiy i, i + dump_len); 13466e9065d7SArtem Bityutskiy print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, 13476e9065d7SArtem Bityutskiy ubi->dbg_peb_buf + i, dump_len, 1); 13486e9065d7SArtem Bityutskiy ubi_dbg_dump_stack(); 13496e9065d7SArtem Bityutskiy err = -EINVAL; 13506e9065d7SArtem Bityutskiy goto out_unlock; 13516e9065d7SArtem Bityutskiy } 13526e9065d7SArtem Bityutskiy mutex_unlock(&ubi->dbg_buf_mutex); 13536e9065d7SArtem Bityutskiy 13546e9065d7SArtem Bityutskiy return 0; 13556e9065d7SArtem Bityutskiy 13566e9065d7SArtem Bityutskiy out_unlock: 13576e9065d7SArtem Bityutskiy mutex_unlock(&ubi->dbg_buf_mutex); 13586e9065d7SArtem Bityutskiy return err; 13596e9065d7SArtem Bityutskiy } 13606e9065d7SArtem Bityutskiy 13616e9065d7SArtem Bityutskiy /** 136240a71a87SArtem Bityutskiy * ubi_dbg_check_all_ff - check that a region of flash is empty. 1363801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 1364801c135cSArtem B. Bityutskiy * @pnum: the physical eraseblock number to check 1365801c135cSArtem B. Bityutskiy * @offset: the starting offset within the physical eraseblock to check 1366801c135cSArtem B. Bityutskiy * @len: the length of the region to check 1367801c135cSArtem B. Bityutskiy * 1368801c135cSArtem B. Bityutskiy * This function returns zero if only 0xFF bytes are present at offset 1369adbf05e3SArtem Bityutskiy * @offset of the physical eraseblock @pnum, and a negative error code if not 1370adbf05e3SArtem Bityutskiy * or if an error occurred. 1371801c135cSArtem B. Bityutskiy */ 137240a71a87SArtem Bityutskiy int ubi_dbg_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len) 1373801c135cSArtem B. Bityutskiy { 1374801c135cSArtem B. Bityutskiy size_t read; 1375801c135cSArtem B. Bityutskiy int err; 1376801c135cSArtem B. Bityutskiy loff_t addr = (loff_t)pnum * ubi->peb_size + offset; 1377801c135cSArtem B. Bityutskiy 1378e88d6e10SArtem Bityutskiy mutex_lock(&ubi->dbg_buf_mutex); 1379e88d6e10SArtem Bityutskiy err = ubi->mtd->read(ubi->mtd, addr, len, &read, ubi->dbg_peb_buf); 1380801c135cSArtem B. Bityutskiy if (err && err != -EUCLEAN) { 1381801c135cSArtem B. Bityutskiy ubi_err("error %d while reading %d bytes from PEB %d:%d, " 1382801c135cSArtem B. Bityutskiy "read %zd bytes", err, len, pnum, offset, read); 1383801c135cSArtem B. Bityutskiy goto error; 1384801c135cSArtem B. Bityutskiy } 1385801c135cSArtem B. Bityutskiy 1386e88d6e10SArtem Bityutskiy err = check_pattern(ubi->dbg_peb_buf, 0xFF, len); 1387801c135cSArtem B. Bityutskiy if (err == 0) { 1388801c135cSArtem B. Bityutskiy ubi_err("flash region at PEB %d:%d, length %d does not " 1389801c135cSArtem B. Bityutskiy "contain all 0xFF bytes", pnum, offset, len); 1390801c135cSArtem B. Bityutskiy goto fail; 1391801c135cSArtem B. Bityutskiy } 1392e88d6e10SArtem Bityutskiy mutex_unlock(&ubi->dbg_buf_mutex); 1393801c135cSArtem B. Bityutskiy 1394801c135cSArtem B. Bityutskiy return 0; 1395801c135cSArtem B. Bityutskiy 1396801c135cSArtem B. Bityutskiy fail: 1397801c135cSArtem B. Bityutskiy ubi_err("paranoid check failed for PEB %d", pnum); 1398c8566350SArtem Bityutskiy ubi_msg("hex dump of the %d-%d region", offset, offset + len); 13996986646bSArtem Bityutskiy print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, 1400e88d6e10SArtem Bityutskiy ubi->dbg_peb_buf, len, 1); 1401adbf05e3SArtem Bityutskiy err = -EINVAL; 1402801c135cSArtem B. Bityutskiy error: 1403801c135cSArtem B. Bityutskiy ubi_dbg_dump_stack(); 1404e88d6e10SArtem Bityutskiy mutex_unlock(&ubi->dbg_buf_mutex); 1405801c135cSArtem B. Bityutskiy return err; 1406801c135cSArtem B. Bityutskiy } 1407801c135cSArtem B. Bityutskiy 1408801c135cSArtem B. Bityutskiy #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */ 1409