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 9492d124f5SArtem Bityutskiy #ifdef CONFIG_MTD_UBI_DEBUG 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 149276832d8SArtem Bityutskiy /* 150276832d8SArtem Bityutskiy * Deliberately corrupt the buffer to improve robustness. Indeed, if we 151276832d8SArtem Bityutskiy * do not do this, the following may happen: 152276832d8SArtem Bityutskiy * 1. The buffer contains data from previous operation, e.g., read from 153276832d8SArtem Bityutskiy * another PEB previously. The data looks like expected, e.g., if we 154276832d8SArtem Bityutskiy * just do not read anything and return - the caller would not 155276832d8SArtem Bityutskiy * notice this. E.g., if we are reading a VID header, the buffer may 156276832d8SArtem Bityutskiy * contain a valid VID header from another PEB. 157276832d8SArtem Bityutskiy * 2. The driver is buggy and returns us success or -EBADMSG or 158276832d8SArtem Bityutskiy * -EUCLEAN, but it does not actually put any data to the buffer. 159276832d8SArtem Bityutskiy * 160276832d8SArtem Bityutskiy * This may confuse UBI or upper layers - they may think the buffer 161276832d8SArtem Bityutskiy * contains valid data while in fact it is just old data. This is 162276832d8SArtem Bityutskiy * especially possible because UBI (and UBIFS) relies on CRC, and 163276832d8SArtem Bityutskiy * treats data as correct even in case of ECC errors if the CRC is 164276832d8SArtem Bityutskiy * correct. 165276832d8SArtem Bityutskiy * 166276832d8SArtem Bityutskiy * Try to prevent this situation by changing the first byte of the 167276832d8SArtem Bityutskiy * buffer. 168276832d8SArtem Bityutskiy */ 169276832d8SArtem Bityutskiy *((uint8_t *)buf) ^= 0xFF; 170276832d8SArtem Bityutskiy 171801c135cSArtem B. Bityutskiy addr = (loff_t)pnum * ubi->peb_size + offset; 172801c135cSArtem B. Bityutskiy retry: 173801c135cSArtem B. Bityutskiy err = ubi->mtd->read(ubi->mtd, addr, len, &read, buf); 174801c135cSArtem B. Bityutskiy if (err) { 175d57f4054SBrian Norris const char *errstr = mtd_is_eccerr(err) ? " (ECC error)" : ""; 1761a49af2cSArtem Bityutskiy 177d57f4054SBrian Norris if (mtd_is_bitflip(err)) { 178801c135cSArtem B. Bityutskiy /* 179801c135cSArtem B. Bityutskiy * -EUCLEAN is reported if there was a bit-flip which 180801c135cSArtem B. Bityutskiy * was corrected, so this is harmless. 1818c1e6ee1SArtem Bityutskiy * 1828c1e6ee1SArtem Bityutskiy * We do not report about it here unless debugging is 1838c1e6ee1SArtem Bityutskiy * enabled. A corresponding message will be printed 1848c1e6ee1SArtem Bityutskiy * later, when it is has been scrubbed. 185801c135cSArtem B. Bityutskiy */ 1868c1e6ee1SArtem Bityutskiy dbg_msg("fixable bit-flip detected at PEB %d", pnum); 187801c135cSArtem B. Bityutskiy ubi_assert(len == read); 188801c135cSArtem B. Bityutskiy return UBI_IO_BITFLIPS; 189801c135cSArtem B. Bityutskiy } 190801c135cSArtem B. Bityutskiy 191a87f29cbSArtem Bityutskiy if (retries++ < UBI_IO_RETRIES) { 192feddbb34SArtem Bityutskiy dbg_io("error %d%s while reading %d bytes from PEB " 193feddbb34SArtem Bityutskiy "%d:%d, read only %zd bytes, retry", 1941a49af2cSArtem Bityutskiy err, errstr, len, pnum, offset, read); 195801c135cSArtem B. Bityutskiy yield(); 196801c135cSArtem B. Bityutskiy goto retry; 197801c135cSArtem B. Bityutskiy } 198801c135cSArtem B. Bityutskiy 199f5d5b1f8SArtem Bityutskiy ubi_err("error %d%s while reading %d bytes from PEB %d:%d, " 2001a49af2cSArtem Bityutskiy "read %zd bytes", err, errstr, len, pnum, offset, read); 201801c135cSArtem B. Bityutskiy ubi_dbg_dump_stack(); 2022362a53eSArtem Bityutskiy 2032362a53eSArtem Bityutskiy /* 2042362a53eSArtem Bityutskiy * The driver should never return -EBADMSG if it failed to read 2052362a53eSArtem Bityutskiy * all the requested data. But some buggy drivers might do 2062362a53eSArtem Bityutskiy * this, so we change it to -EIO. 2072362a53eSArtem Bityutskiy */ 208d57f4054SBrian Norris if (read != len && mtd_is_eccerr(err)) { 2092362a53eSArtem Bityutskiy ubi_assert(0); 2102362a53eSArtem Bityutskiy err = -EIO; 2112362a53eSArtem Bityutskiy } 212801c135cSArtem B. Bityutskiy } else { 213801c135cSArtem B. Bityutskiy ubi_assert(len == read); 214801c135cSArtem B. Bityutskiy 21527a0f2a3SArtem Bityutskiy if (ubi_dbg_is_bitflip(ubi)) { 216c8566350SArtem Bityutskiy dbg_gen("bit-flip (emulated)"); 217801c135cSArtem B. Bityutskiy err = UBI_IO_BITFLIPS; 218801c135cSArtem B. Bityutskiy } 219801c135cSArtem B. Bityutskiy } 220801c135cSArtem B. Bityutskiy 221801c135cSArtem B. Bityutskiy return err; 222801c135cSArtem B. Bityutskiy } 223801c135cSArtem B. Bityutskiy 224801c135cSArtem B. Bityutskiy /** 225801c135cSArtem B. Bityutskiy * ubi_io_write - write data to a physical eraseblock. 226801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 227801c135cSArtem B. Bityutskiy * @buf: buffer with the data to write 228801c135cSArtem B. Bityutskiy * @pnum: physical eraseblock number to write to 229801c135cSArtem B. Bityutskiy * @offset: offset within the physical eraseblock where to write 230801c135cSArtem B. Bityutskiy * @len: how many bytes to write 231801c135cSArtem B. Bityutskiy * 232801c135cSArtem B. Bityutskiy * This function writes @len bytes of data from buffer @buf to offset @offset 233801c135cSArtem B. Bityutskiy * of physical eraseblock @pnum. If all the data were successfully written, 234801c135cSArtem B. Bityutskiy * zero is returned. If an error occurred, this function returns a negative 235801c135cSArtem B. Bityutskiy * error code. If %-EIO is returned, the physical eraseblock most probably went 236801c135cSArtem B. Bityutskiy * bad. 237801c135cSArtem B. Bityutskiy * 238801c135cSArtem B. Bityutskiy * Note, in case of an error, it is possible that something was still written 239801c135cSArtem B. Bityutskiy * to the flash media, but may be some garbage. 240801c135cSArtem B. Bityutskiy */ 241e88d6e10SArtem Bityutskiy int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset, 242e88d6e10SArtem Bityutskiy int len) 243801c135cSArtem B. Bityutskiy { 244801c135cSArtem B. Bityutskiy int err; 245801c135cSArtem B. Bityutskiy size_t written; 246801c135cSArtem B. Bityutskiy loff_t addr; 247801c135cSArtem B. Bityutskiy 248801c135cSArtem B. Bityutskiy dbg_io("write %d bytes to PEB %d:%d", len, pnum, offset); 249801c135cSArtem B. Bityutskiy 250801c135cSArtem B. Bityutskiy ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 251801c135cSArtem B. Bityutskiy ubi_assert(offset >= 0 && offset + len <= ubi->peb_size); 252801c135cSArtem B. Bityutskiy ubi_assert(offset % ubi->hdrs_min_io_size == 0); 253801c135cSArtem B. Bityutskiy ubi_assert(len > 0 && len % ubi->hdrs_min_io_size == 0); 254801c135cSArtem B. Bityutskiy 255801c135cSArtem B. Bityutskiy if (ubi->ro_mode) { 256801c135cSArtem B. Bityutskiy ubi_err("read-only mode"); 257801c135cSArtem B. Bityutskiy return -EROFS; 258801c135cSArtem B. Bityutskiy } 259801c135cSArtem B. Bityutskiy 260801c135cSArtem B. Bityutskiy /* The below has to be compiled out if paranoid checks are disabled */ 261801c135cSArtem B. Bityutskiy 262801c135cSArtem B. Bityutskiy err = paranoid_check_not_bad(ubi, pnum); 263801c135cSArtem B. Bityutskiy if (err) 264adbf05e3SArtem Bityutskiy return err; 265801c135cSArtem B. Bityutskiy 266801c135cSArtem B. Bityutskiy /* The area we are writing to has to contain all 0xFF bytes */ 26740a71a87SArtem Bityutskiy err = ubi_dbg_check_all_ff(ubi, pnum, offset, len); 268801c135cSArtem B. Bityutskiy if (err) 269adbf05e3SArtem Bityutskiy return err; 270801c135cSArtem B. Bityutskiy 271801c135cSArtem B. Bityutskiy if (offset >= ubi->leb_start) { 272801c135cSArtem B. Bityutskiy /* 273801c135cSArtem B. Bityutskiy * We write to the data area of the physical eraseblock. Make 274801c135cSArtem B. Bityutskiy * sure it has valid EC and VID headers. 275801c135cSArtem B. Bityutskiy */ 276801c135cSArtem B. Bityutskiy err = paranoid_check_peb_ec_hdr(ubi, pnum); 277801c135cSArtem B. Bityutskiy if (err) 278adbf05e3SArtem Bityutskiy return err; 279801c135cSArtem B. Bityutskiy err = paranoid_check_peb_vid_hdr(ubi, pnum); 280801c135cSArtem B. Bityutskiy if (err) 281adbf05e3SArtem Bityutskiy return err; 282801c135cSArtem B. Bityutskiy } 283801c135cSArtem B. Bityutskiy 28427a0f2a3SArtem Bityutskiy if (ubi_dbg_is_write_failure(ubi)) { 285801c135cSArtem B. Bityutskiy dbg_err("cannot write %d bytes to PEB %d:%d " 286801c135cSArtem B. Bityutskiy "(emulated)", len, pnum, offset); 287801c135cSArtem B. Bityutskiy ubi_dbg_dump_stack(); 288801c135cSArtem B. Bityutskiy return -EIO; 289801c135cSArtem B. Bityutskiy } 290801c135cSArtem B. Bityutskiy 291801c135cSArtem B. Bityutskiy addr = (loff_t)pnum * ubi->peb_size + offset; 292801c135cSArtem B. Bityutskiy err = ubi->mtd->write(ubi->mtd, addr, len, &written, buf); 293801c135cSArtem B. Bityutskiy if (err) { 294801c135cSArtem B. Bityutskiy ubi_err("error %d while writing %d bytes to PEB %d:%d, written " 295801c135cSArtem B. Bityutskiy "%zd bytes", err, len, pnum, offset, written); 296801c135cSArtem B. Bityutskiy ubi_dbg_dump_stack(); 297867996b1SArtem Bityutskiy ubi_dbg_dump_flash(ubi, pnum, offset, len); 298801c135cSArtem B. Bityutskiy } else 299801c135cSArtem B. Bityutskiy ubi_assert(written == len); 300801c135cSArtem B. Bityutskiy 3016e9065d7SArtem Bityutskiy if (!err) { 3026e9065d7SArtem Bityutskiy err = ubi_dbg_check_write(ubi, buf, pnum, offset, len); 3036e9065d7SArtem Bityutskiy if (err) 3046e9065d7SArtem Bityutskiy return err; 3056e9065d7SArtem Bityutskiy 3066e9065d7SArtem Bityutskiy /* 3076e9065d7SArtem Bityutskiy * Since we always write sequentially, the rest of the PEB has 3086e9065d7SArtem Bityutskiy * to contain only 0xFF bytes. 3096e9065d7SArtem Bityutskiy */ 3106e9065d7SArtem Bityutskiy offset += len; 3116e9065d7SArtem Bityutskiy len = ubi->peb_size - offset; 3126e9065d7SArtem Bityutskiy if (len) 3136e9065d7SArtem Bityutskiy err = ubi_dbg_check_all_ff(ubi, pnum, offset, len); 3146e9065d7SArtem Bityutskiy } 3156e9065d7SArtem Bityutskiy 316801c135cSArtem B. Bityutskiy return err; 317801c135cSArtem B. Bityutskiy } 318801c135cSArtem B. Bityutskiy 319801c135cSArtem B. Bityutskiy /** 320801c135cSArtem B. Bityutskiy * erase_callback - MTD erasure call-back. 321801c135cSArtem B. Bityutskiy * @ei: MTD erase information object. 322801c135cSArtem B. Bityutskiy * 323801c135cSArtem B. Bityutskiy * Note, even though MTD erase interface is asynchronous, all the current 324801c135cSArtem B. Bityutskiy * implementations are synchronous anyway. 325801c135cSArtem B. Bityutskiy */ 326801c135cSArtem B. Bityutskiy static void erase_callback(struct erase_info *ei) 327801c135cSArtem B. Bityutskiy { 328801c135cSArtem B. Bityutskiy wake_up_interruptible((wait_queue_head_t *)ei->priv); 329801c135cSArtem B. Bityutskiy } 330801c135cSArtem B. Bityutskiy 331801c135cSArtem B. Bityutskiy /** 332801c135cSArtem B. Bityutskiy * do_sync_erase - synchronously erase a physical eraseblock. 333801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 334801c135cSArtem B. Bityutskiy * @pnum: the physical eraseblock number to erase 335801c135cSArtem B. Bityutskiy * 336801c135cSArtem B. Bityutskiy * This function synchronously erases physical eraseblock @pnum and returns 337801c135cSArtem B. Bityutskiy * zero in case of success and a negative error code in case of failure. If 338801c135cSArtem B. Bityutskiy * %-EIO is returned, the physical eraseblock most probably went bad. 339801c135cSArtem B. Bityutskiy */ 340e88d6e10SArtem Bityutskiy static int do_sync_erase(struct ubi_device *ubi, int pnum) 341801c135cSArtem B. Bityutskiy { 342801c135cSArtem B. Bityutskiy int err, retries = 0; 343801c135cSArtem B. Bityutskiy struct erase_info ei; 344801c135cSArtem B. Bityutskiy wait_queue_head_t wq; 345801c135cSArtem B. Bityutskiy 346801c135cSArtem B. Bityutskiy dbg_io("erase PEB %d", pnum); 3473efe5090SArtem Bityutskiy ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 3483efe5090SArtem Bityutskiy 3493efe5090SArtem Bityutskiy if (ubi->ro_mode) { 3503efe5090SArtem Bityutskiy ubi_err("read-only mode"); 3513efe5090SArtem Bityutskiy return -EROFS; 3523efe5090SArtem Bityutskiy } 353801c135cSArtem B. Bityutskiy 354801c135cSArtem B. Bityutskiy retry: 355801c135cSArtem B. Bityutskiy init_waitqueue_head(&wq); 356801c135cSArtem B. Bityutskiy memset(&ei, 0, sizeof(struct erase_info)); 357801c135cSArtem B. Bityutskiy 358801c135cSArtem B. Bityutskiy ei.mtd = ubi->mtd; 3592f176f79SBrijesh Singh ei.addr = (loff_t)pnum * ubi->peb_size; 360801c135cSArtem B. Bityutskiy ei.len = ubi->peb_size; 361801c135cSArtem B. Bityutskiy ei.callback = erase_callback; 362801c135cSArtem B. Bityutskiy ei.priv = (unsigned long)&wq; 363801c135cSArtem B. Bityutskiy 364801c135cSArtem B. Bityutskiy err = ubi->mtd->erase(ubi->mtd, &ei); 365801c135cSArtem B. Bityutskiy if (err) { 366801c135cSArtem B. Bityutskiy if (retries++ < UBI_IO_RETRIES) { 367801c135cSArtem B. Bityutskiy dbg_io("error %d while erasing PEB %d, retry", 368801c135cSArtem B. Bityutskiy err, pnum); 369801c135cSArtem B. Bityutskiy yield(); 370801c135cSArtem B. Bityutskiy goto retry; 371801c135cSArtem B. Bityutskiy } 372801c135cSArtem B. Bityutskiy ubi_err("cannot erase PEB %d, error %d", pnum, err); 373801c135cSArtem B. Bityutskiy ubi_dbg_dump_stack(); 374801c135cSArtem B. Bityutskiy return err; 375801c135cSArtem B. Bityutskiy } 376801c135cSArtem B. Bityutskiy 377801c135cSArtem B. Bityutskiy err = wait_event_interruptible(wq, ei.state == MTD_ERASE_DONE || 378801c135cSArtem B. Bityutskiy ei.state == MTD_ERASE_FAILED); 379801c135cSArtem B. Bityutskiy if (err) { 380801c135cSArtem B. Bityutskiy ubi_err("interrupted PEB %d erasure", pnum); 381801c135cSArtem B. Bityutskiy return -EINTR; 382801c135cSArtem B. Bityutskiy } 383801c135cSArtem B. Bityutskiy 384801c135cSArtem B. Bityutskiy if (ei.state == MTD_ERASE_FAILED) { 385801c135cSArtem B. Bityutskiy if (retries++ < UBI_IO_RETRIES) { 386801c135cSArtem B. Bityutskiy dbg_io("error while erasing PEB %d, retry", pnum); 387801c135cSArtem B. Bityutskiy yield(); 388801c135cSArtem B. Bityutskiy goto retry; 389801c135cSArtem B. Bityutskiy } 390801c135cSArtem B. Bityutskiy ubi_err("cannot erase PEB %d", pnum); 391801c135cSArtem B. Bityutskiy ubi_dbg_dump_stack(); 392801c135cSArtem B. Bityutskiy return -EIO; 393801c135cSArtem B. Bityutskiy } 394801c135cSArtem B. Bityutskiy 39540a71a87SArtem Bityutskiy err = ubi_dbg_check_all_ff(ubi, pnum, 0, ubi->peb_size); 396801c135cSArtem B. Bityutskiy if (err) 397adbf05e3SArtem Bityutskiy return err; 398801c135cSArtem B. Bityutskiy 39927a0f2a3SArtem Bityutskiy if (ubi_dbg_is_erase_failure(ubi)) { 400801c135cSArtem B. Bityutskiy dbg_err("cannot erase PEB %d (emulated)", pnum); 401801c135cSArtem B. Bityutskiy return -EIO; 402801c135cSArtem B. Bityutskiy } 403801c135cSArtem B. Bityutskiy 404801c135cSArtem B. Bityutskiy return 0; 405801c135cSArtem B. Bityutskiy } 406801c135cSArtem B. Bityutskiy 407801c135cSArtem B. Bityutskiy /* Patterns to write to a physical eraseblock when torturing it */ 408801c135cSArtem B. Bityutskiy static uint8_t patterns[] = {0xa5, 0x5a, 0x0}; 409801c135cSArtem B. Bityutskiy 410801c135cSArtem B. Bityutskiy /** 411801c135cSArtem B. Bityutskiy * torture_peb - test a supposedly bad physical eraseblock. 412801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 413801c135cSArtem B. Bityutskiy * @pnum: the physical eraseblock number to test 414801c135cSArtem B. Bityutskiy * 415801c135cSArtem B. Bityutskiy * This function returns %-EIO if the physical eraseblock did not pass the 416801c135cSArtem B. Bityutskiy * test, a positive number of erase operations done if the test was 417801c135cSArtem B. Bityutskiy * successfully passed, and other negative error codes in case of other errors. 418801c135cSArtem B. Bityutskiy */ 419e88d6e10SArtem Bityutskiy static int torture_peb(struct ubi_device *ubi, int pnum) 420801c135cSArtem B. Bityutskiy { 421801c135cSArtem B. Bityutskiy int err, i, patt_count; 422801c135cSArtem B. Bityutskiy 4238c1e6ee1SArtem Bityutskiy ubi_msg("run torture test for PEB %d", pnum); 424801c135cSArtem B. Bityutskiy patt_count = ARRAY_SIZE(patterns); 425801c135cSArtem B. Bityutskiy ubi_assert(patt_count > 0); 426801c135cSArtem B. Bityutskiy 427e88d6e10SArtem Bityutskiy mutex_lock(&ubi->buf_mutex); 428801c135cSArtem B. Bityutskiy for (i = 0; i < patt_count; i++) { 429801c135cSArtem B. Bityutskiy err = do_sync_erase(ubi, pnum); 430801c135cSArtem B. Bityutskiy if (err) 431801c135cSArtem B. Bityutskiy goto out; 432801c135cSArtem B. Bityutskiy 433801c135cSArtem B. Bityutskiy /* Make sure the PEB contains only 0xFF bytes */ 434e88d6e10SArtem Bityutskiy err = ubi_io_read(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size); 435801c135cSArtem B. Bityutskiy if (err) 436801c135cSArtem B. Bityutskiy goto out; 437801c135cSArtem B. Bityutskiy 438bb00e180SArtem Bityutskiy err = ubi_check_pattern(ubi->peb_buf1, 0xFF, ubi->peb_size); 439801c135cSArtem B. Bityutskiy if (err == 0) { 440801c135cSArtem B. Bityutskiy ubi_err("erased PEB %d, but a non-0xFF byte found", 441801c135cSArtem B. Bityutskiy pnum); 442801c135cSArtem B. Bityutskiy err = -EIO; 443801c135cSArtem B. Bityutskiy goto out; 444801c135cSArtem B. Bityutskiy } 445801c135cSArtem B. Bityutskiy 446801c135cSArtem B. Bityutskiy /* Write a pattern and check it */ 447e88d6e10SArtem Bityutskiy memset(ubi->peb_buf1, patterns[i], ubi->peb_size); 448e88d6e10SArtem Bityutskiy err = ubi_io_write(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size); 449801c135cSArtem B. Bityutskiy if (err) 450801c135cSArtem B. Bityutskiy goto out; 451801c135cSArtem B. Bityutskiy 452e88d6e10SArtem Bityutskiy memset(ubi->peb_buf1, ~patterns[i], ubi->peb_size); 453e88d6e10SArtem Bityutskiy err = ubi_io_read(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size); 454801c135cSArtem B. Bityutskiy if (err) 455801c135cSArtem B. Bityutskiy goto out; 456801c135cSArtem B. Bityutskiy 457bb00e180SArtem Bityutskiy err = ubi_check_pattern(ubi->peb_buf1, patterns[i], 458bb00e180SArtem Bityutskiy ubi->peb_size); 459801c135cSArtem B. Bityutskiy if (err == 0) { 460801c135cSArtem B. Bityutskiy ubi_err("pattern %x checking failed for PEB %d", 461801c135cSArtem B. Bityutskiy patterns[i], pnum); 462801c135cSArtem B. Bityutskiy err = -EIO; 463801c135cSArtem B. Bityutskiy goto out; 464801c135cSArtem B. Bityutskiy } 465801c135cSArtem B. Bityutskiy } 466801c135cSArtem B. Bityutskiy 467801c135cSArtem B. Bityutskiy err = patt_count; 46814264144SArtem Bityutskiy ubi_msg("PEB %d passed torture test, do not mark it as bad", pnum); 469801c135cSArtem B. Bityutskiy 470801c135cSArtem B. Bityutskiy out: 471e88d6e10SArtem Bityutskiy mutex_unlock(&ubi->buf_mutex); 472d57f4054SBrian Norris if (err == UBI_IO_BITFLIPS || mtd_is_eccerr(err)) { 473801c135cSArtem B. Bityutskiy /* 474801c135cSArtem B. Bityutskiy * If a bit-flip or data integrity error was detected, the test 475801c135cSArtem B. Bityutskiy * has not passed because it happened on a freshly erased 476801c135cSArtem B. Bityutskiy * physical eraseblock which means something is wrong with it. 477801c135cSArtem B. Bityutskiy */ 4788d2d4011SArtem Bityutskiy ubi_err("read problems on freshly erased PEB %d, must be bad", 4798d2d4011SArtem Bityutskiy pnum); 480801c135cSArtem B. Bityutskiy err = -EIO; 4818d2d4011SArtem Bityutskiy } 482801c135cSArtem B. Bityutskiy return err; 483801c135cSArtem B. Bityutskiy } 484801c135cSArtem B. Bityutskiy 485801c135cSArtem B. Bityutskiy /** 486ebf53f42SArtem Bityutskiy * nor_erase_prepare - prepare a NOR flash PEB for erasure. 487ebf53f42SArtem Bityutskiy * @ubi: UBI device description object 488ebf53f42SArtem Bityutskiy * @pnum: physical eraseblock number to prepare 489ebf53f42SArtem Bityutskiy * 490ebf53f42SArtem Bityutskiy * NOR flash, or at least some of them, have peculiar embedded PEB erasure 491ebf53f42SArtem Bityutskiy * algorithm: the PEB is first filled with zeroes, then it is erased. And 492ebf53f42SArtem Bityutskiy * filling with zeroes starts from the end of the PEB. This was observed with 493ebf53f42SArtem Bityutskiy * Spansion S29GL512N NOR flash. 494ebf53f42SArtem Bityutskiy * 495ebf53f42SArtem Bityutskiy * This means that in case of a power cut we may end up with intact data at the 496ebf53f42SArtem Bityutskiy * beginning of the PEB, and all zeroes at the end of PEB. In other words, the 497ebf53f42SArtem Bityutskiy * EC and VID headers are OK, but a large chunk of data at the end of PEB is 498ebf53f42SArtem Bityutskiy * zeroed. This makes UBI mistakenly treat this PEB as used and associate it 499ebf53f42SArtem Bityutskiy * with an LEB, which leads to subsequent failures (e.g., UBIFS fails). 500ebf53f42SArtem Bityutskiy * 501ebf53f42SArtem Bityutskiy * This function is called before erasing NOR PEBs and it zeroes out EC and VID 502ebf53f42SArtem Bityutskiy * magic numbers in order to invalidate them and prevent the failures. Returns 503ebf53f42SArtem Bityutskiy * zero in case of success and a negative error code in case of failure. 504ebf53f42SArtem Bityutskiy */ 505ebf53f42SArtem Bityutskiy static int nor_erase_prepare(struct ubi_device *ubi, int pnum) 506ebf53f42SArtem Bityutskiy { 507de75c771SArtem Bityutskiy int err, err1; 508ebf53f42SArtem Bityutskiy size_t written; 509ebf53f42SArtem Bityutskiy loff_t addr; 510ebf53f42SArtem Bityutskiy uint32_t data = 0; 5112fff570eSArtem Bityutskiy /* 5122fff570eSArtem Bityutskiy * Note, we cannot generally define VID header buffers on stack, 5132fff570eSArtem Bityutskiy * because of the way we deal with these buffers (see the header 5142fff570eSArtem Bityutskiy * comment in this file). But we know this is a NOR-specific piece of 5152fff570eSArtem Bityutskiy * code, so we can do this. But yes, this is error-prone and we should 5162fff570eSArtem Bityutskiy * (pre-)allocate VID header buffer instead. 5172fff570eSArtem Bityutskiy */ 518de75c771SArtem Bityutskiy struct ubi_vid_hdr vid_hdr; 519ebf53f42SArtem Bityutskiy 5207ac760c2SArtem Bityutskiy /* 5217ac760c2SArtem Bityutskiy * It is important to first invalidate the EC header, and then the VID 5227ac760c2SArtem Bityutskiy * header. Otherwise a power cut may lead to valid EC header and 5237ac760c2SArtem Bityutskiy * invalid VID header, in which case UBI will treat this PEB as 5247ac760c2SArtem Bityutskiy * corrupted and will try to preserve it, and print scary warnings (see 5257ac760c2SArtem Bityutskiy * the header comment in scan.c for more information). 5267ac760c2SArtem Bityutskiy */ 5277ac760c2SArtem Bityutskiy addr = (loff_t)pnum * ubi->peb_size; 52883c2099fSArtem Bityutskiy err = ubi->mtd->write(ubi->mtd, addr, 4, &written, (void *)&data); 529de75c771SArtem Bityutskiy if (!err) { 5307ac760c2SArtem Bityutskiy addr += ubi->vid_hdr_aloffset; 531de75c771SArtem Bityutskiy err = ubi->mtd->write(ubi->mtd, addr, 4, &written, 532de75c771SArtem Bityutskiy (void *)&data); 533de75c771SArtem Bityutskiy if (!err) 534de75c771SArtem Bityutskiy return 0; 5355b289b56SArtem Bityutskiy } 5365b289b56SArtem Bityutskiy 537de75c771SArtem Bityutskiy /* 538de75c771SArtem Bityutskiy * We failed to write to the media. This was observed with Spansion 5397ac760c2SArtem Bityutskiy * S29GL512N NOR flash. Most probably the previously eraseblock erasure 5407ac760c2SArtem Bityutskiy * was interrupted at a very inappropriate moment, so it became 5417ac760c2SArtem Bityutskiy * unwritable. In this case we probably anyway have garbage in this 5427ac760c2SArtem Bityutskiy * PEB. 543de75c771SArtem Bityutskiy */ 544de75c771SArtem Bityutskiy err1 = ubi_io_read_vid_hdr(ubi, pnum, &vid_hdr, 0); 545d4c63813SHolger Brunck if (err1 == UBI_IO_BAD_HDR_EBADMSG || err1 == UBI_IO_BAD_HDR || 546d4c63813SHolger Brunck err1 == UBI_IO_FF) { 5477ac760c2SArtem Bityutskiy struct ubi_ec_hdr ec_hdr; 5487ac760c2SArtem Bityutskiy 5497ac760c2SArtem Bityutskiy err1 = ubi_io_read_ec_hdr(ubi, pnum, &ec_hdr, 0); 550d4c63813SHolger Brunck if (err1 == UBI_IO_BAD_HDR_EBADMSG || err1 == UBI_IO_BAD_HDR || 551d4c63813SHolger Brunck err1 == UBI_IO_FF) 552de75c771SArtem Bityutskiy /* 5537ac760c2SArtem Bityutskiy * Both VID and EC headers are corrupted, so we can 5547ac760c2SArtem Bityutskiy * safely erase this PEB and not afraid that it will be 5557ac760c2SArtem Bityutskiy * treated as a valid PEB in case of an unclean reboot. 556de75c771SArtem Bityutskiy */ 557ebf53f42SArtem Bityutskiy return 0; 5587ac760c2SArtem Bityutskiy } 559de75c771SArtem Bityutskiy 560de75c771SArtem Bityutskiy /* 561de75c771SArtem Bityutskiy * The PEB contains a valid VID header, but we cannot invalidate it. 562de75c771SArtem Bityutskiy * Supposedly the flash media or the driver is screwed up, so return an 563de75c771SArtem Bityutskiy * error. 564de75c771SArtem Bityutskiy */ 565de75c771SArtem Bityutskiy ubi_err("cannot invalidate PEB %d, write returned %d read returned %d", 566de75c771SArtem Bityutskiy pnum, err, err1); 567de75c771SArtem Bityutskiy ubi_dbg_dump_flash(ubi, pnum, 0, ubi->peb_size); 568de75c771SArtem Bityutskiy return -EIO; 569ebf53f42SArtem Bityutskiy } 570ebf53f42SArtem Bityutskiy 571ebf53f42SArtem Bityutskiy /** 572801c135cSArtem B. Bityutskiy * ubi_io_sync_erase - synchronously erase a physical eraseblock. 573801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 574801c135cSArtem B. Bityutskiy * @pnum: physical eraseblock number to erase 575801c135cSArtem B. Bityutskiy * @torture: if this physical eraseblock has to be tortured 576801c135cSArtem B. Bityutskiy * 577801c135cSArtem B. Bityutskiy * This function synchronously erases physical eraseblock @pnum. If @torture 578801c135cSArtem B. Bityutskiy * flag is not zero, the physical eraseblock is checked by means of writing 579801c135cSArtem B. Bityutskiy * different patterns to it and reading them back. If the torturing is enabled, 580025dfdafSFrederik Schwarzer * the physical eraseblock is erased more than once. 581801c135cSArtem B. Bityutskiy * 582801c135cSArtem B. Bityutskiy * This function returns the number of erasures made in case of success, %-EIO 583801c135cSArtem B. Bityutskiy * if the erasure failed or the torturing test failed, and other negative error 584801c135cSArtem B. Bityutskiy * codes in case of other errors. Note, %-EIO means that the physical 585801c135cSArtem B. Bityutskiy * eraseblock is bad. 586801c135cSArtem B. Bityutskiy */ 587e88d6e10SArtem Bityutskiy int ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture) 588801c135cSArtem B. Bityutskiy { 589801c135cSArtem B. Bityutskiy int err, ret = 0; 590801c135cSArtem B. Bityutskiy 591801c135cSArtem B. Bityutskiy ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 592801c135cSArtem B. Bityutskiy 593801c135cSArtem B. Bityutskiy err = paranoid_check_not_bad(ubi, pnum); 594801c135cSArtem B. Bityutskiy if (err != 0) 595adbf05e3SArtem Bityutskiy return err; 596801c135cSArtem B. Bityutskiy 597801c135cSArtem B. Bityutskiy if (ubi->ro_mode) { 598801c135cSArtem B. Bityutskiy ubi_err("read-only mode"); 599801c135cSArtem B. Bityutskiy return -EROFS; 600801c135cSArtem B. Bityutskiy } 601801c135cSArtem B. Bityutskiy 602ebf53f42SArtem Bityutskiy if (ubi->nor_flash) { 603ebf53f42SArtem Bityutskiy err = nor_erase_prepare(ubi, pnum); 604ebf53f42SArtem Bityutskiy if (err) 605ebf53f42SArtem Bityutskiy return err; 606ebf53f42SArtem Bityutskiy } 607ebf53f42SArtem Bityutskiy 608801c135cSArtem B. Bityutskiy if (torture) { 609801c135cSArtem B. Bityutskiy ret = torture_peb(ubi, pnum); 610801c135cSArtem B. Bityutskiy if (ret < 0) 611801c135cSArtem B. Bityutskiy return ret; 612801c135cSArtem B. Bityutskiy } 613801c135cSArtem B. Bityutskiy 614801c135cSArtem B. Bityutskiy err = do_sync_erase(ubi, pnum); 615801c135cSArtem B. Bityutskiy if (err) 616801c135cSArtem B. Bityutskiy return err; 617801c135cSArtem B. Bityutskiy 618801c135cSArtem B. Bityutskiy return ret + 1; 619801c135cSArtem B. Bityutskiy } 620801c135cSArtem B. Bityutskiy 621801c135cSArtem B. Bityutskiy /** 622801c135cSArtem B. Bityutskiy * ubi_io_is_bad - check if a physical eraseblock is bad. 623801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 624801c135cSArtem B. Bityutskiy * @pnum: the physical eraseblock number to check 625801c135cSArtem B. Bityutskiy * 626801c135cSArtem B. Bityutskiy * This function returns a positive number if the physical eraseblock is bad, 627801c135cSArtem B. Bityutskiy * zero if not, and a negative error code if an error occurred. 628801c135cSArtem B. Bityutskiy */ 629801c135cSArtem B. Bityutskiy int ubi_io_is_bad(const struct ubi_device *ubi, int pnum) 630801c135cSArtem B. Bityutskiy { 631801c135cSArtem B. Bityutskiy struct mtd_info *mtd = ubi->mtd; 632801c135cSArtem B. Bityutskiy 633801c135cSArtem B. Bityutskiy ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 634801c135cSArtem B. Bityutskiy 635801c135cSArtem B. Bityutskiy if (ubi->bad_allowed) { 636801c135cSArtem B. Bityutskiy int ret; 637801c135cSArtem B. Bityutskiy 638801c135cSArtem B. Bityutskiy ret = mtd->block_isbad(mtd, (loff_t)pnum * ubi->peb_size); 639801c135cSArtem B. Bityutskiy if (ret < 0) 640801c135cSArtem B. Bityutskiy ubi_err("error %d while checking if PEB %d is bad", 641801c135cSArtem B. Bityutskiy ret, pnum); 642801c135cSArtem B. Bityutskiy else if (ret) 643801c135cSArtem B. Bityutskiy dbg_io("PEB %d is bad", pnum); 644801c135cSArtem B. Bityutskiy return ret; 645801c135cSArtem B. Bityutskiy } 646801c135cSArtem B. Bityutskiy 647801c135cSArtem B. Bityutskiy return 0; 648801c135cSArtem B. Bityutskiy } 649801c135cSArtem B. Bityutskiy 650801c135cSArtem B. Bityutskiy /** 651801c135cSArtem B. Bityutskiy * ubi_io_mark_bad - mark a physical eraseblock as bad. 652801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 653801c135cSArtem B. Bityutskiy * @pnum: the physical eraseblock number to mark 654801c135cSArtem B. Bityutskiy * 655801c135cSArtem B. Bityutskiy * This function returns zero in case of success and a negative error code in 656801c135cSArtem B. Bityutskiy * case of failure. 657801c135cSArtem B. Bityutskiy */ 658801c135cSArtem B. Bityutskiy int ubi_io_mark_bad(const struct ubi_device *ubi, int pnum) 659801c135cSArtem B. Bityutskiy { 660801c135cSArtem B. Bityutskiy int err; 661801c135cSArtem B. Bityutskiy struct mtd_info *mtd = ubi->mtd; 662801c135cSArtem B. Bityutskiy 663801c135cSArtem B. Bityutskiy ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 664801c135cSArtem B. Bityutskiy 665801c135cSArtem B. Bityutskiy if (ubi->ro_mode) { 666801c135cSArtem B. Bityutskiy ubi_err("read-only mode"); 667801c135cSArtem B. Bityutskiy return -EROFS; 668801c135cSArtem B. Bityutskiy } 669801c135cSArtem B. Bityutskiy 670801c135cSArtem B. Bityutskiy if (!ubi->bad_allowed) 671801c135cSArtem B. Bityutskiy return 0; 672801c135cSArtem B. Bityutskiy 673801c135cSArtem B. Bityutskiy err = mtd->block_markbad(mtd, (loff_t)pnum * ubi->peb_size); 674801c135cSArtem B. Bityutskiy if (err) 675801c135cSArtem B. Bityutskiy ubi_err("cannot mark PEB %d bad, error %d", pnum, err); 676801c135cSArtem B. Bityutskiy return err; 677801c135cSArtem B. Bityutskiy } 678801c135cSArtem B. Bityutskiy 679801c135cSArtem B. Bityutskiy /** 680801c135cSArtem B. Bityutskiy * validate_ec_hdr - validate an erase counter header. 681801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 682801c135cSArtem B. Bityutskiy * @ec_hdr: the erase counter header to check 683801c135cSArtem B. Bityutskiy * 684801c135cSArtem B. Bityutskiy * This function returns zero if the erase counter header is OK, and %1 if 685801c135cSArtem B. Bityutskiy * not. 686801c135cSArtem B. Bityutskiy */ 687fe96efc1SArtem Bityutskiy static int validate_ec_hdr(const struct ubi_device *ubi, 688801c135cSArtem B. Bityutskiy const struct ubi_ec_hdr *ec_hdr) 689801c135cSArtem B. Bityutskiy { 690801c135cSArtem B. Bityutskiy long long ec; 691fe96efc1SArtem Bityutskiy int vid_hdr_offset, leb_start; 692801c135cSArtem B. Bityutskiy 6933261ebd7SChristoph Hellwig ec = be64_to_cpu(ec_hdr->ec); 6943261ebd7SChristoph Hellwig vid_hdr_offset = be32_to_cpu(ec_hdr->vid_hdr_offset); 6953261ebd7SChristoph Hellwig leb_start = be32_to_cpu(ec_hdr->data_offset); 696801c135cSArtem B. Bityutskiy 697801c135cSArtem B. Bityutskiy if (ec_hdr->version != UBI_VERSION) { 698801c135cSArtem B. Bityutskiy ubi_err("node with incompatible UBI version found: " 699801c135cSArtem B. Bityutskiy "this UBI version is %d, image version is %d", 700801c135cSArtem B. Bityutskiy UBI_VERSION, (int)ec_hdr->version); 701801c135cSArtem B. Bityutskiy goto bad; 702801c135cSArtem B. Bityutskiy } 703801c135cSArtem B. Bityutskiy 704801c135cSArtem B. Bityutskiy if (vid_hdr_offset != ubi->vid_hdr_offset) { 705801c135cSArtem B. Bityutskiy ubi_err("bad VID header offset %d, expected %d", 706801c135cSArtem B. Bityutskiy vid_hdr_offset, ubi->vid_hdr_offset); 707801c135cSArtem B. Bityutskiy goto bad; 708801c135cSArtem B. Bityutskiy } 709801c135cSArtem B. Bityutskiy 710801c135cSArtem B. Bityutskiy if (leb_start != ubi->leb_start) { 711801c135cSArtem B. Bityutskiy ubi_err("bad data offset %d, expected %d", 712801c135cSArtem B. Bityutskiy leb_start, ubi->leb_start); 713801c135cSArtem B. Bityutskiy goto bad; 714801c135cSArtem B. Bityutskiy } 715801c135cSArtem B. Bityutskiy 716801c135cSArtem B. Bityutskiy if (ec < 0 || ec > UBI_MAX_ERASECOUNTER) { 717801c135cSArtem B. Bityutskiy ubi_err("bad erase counter %lld", ec); 718801c135cSArtem B. Bityutskiy goto bad; 719801c135cSArtem B. Bityutskiy } 720801c135cSArtem B. Bityutskiy 721801c135cSArtem B. Bityutskiy return 0; 722801c135cSArtem B. Bityutskiy 723801c135cSArtem B. Bityutskiy bad: 724801c135cSArtem B. Bityutskiy ubi_err("bad EC header"); 725801c135cSArtem B. Bityutskiy ubi_dbg_dump_ec_hdr(ec_hdr); 726801c135cSArtem B. Bityutskiy ubi_dbg_dump_stack(); 727801c135cSArtem B. Bityutskiy return 1; 728801c135cSArtem B. Bityutskiy } 729801c135cSArtem B. Bityutskiy 730801c135cSArtem B. Bityutskiy /** 731801c135cSArtem B. Bityutskiy * ubi_io_read_ec_hdr - read and check an erase counter header. 732801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 733801c135cSArtem B. Bityutskiy * @pnum: physical eraseblock to read from 734801c135cSArtem B. Bityutskiy * @ec_hdr: a &struct ubi_ec_hdr object where to store the read erase counter 735801c135cSArtem B. Bityutskiy * header 736801c135cSArtem B. Bityutskiy * @verbose: be verbose if the header is corrupted or was not found 737801c135cSArtem B. Bityutskiy * 738801c135cSArtem B. Bityutskiy * This function reads erase counter header from physical eraseblock @pnum and 739801c135cSArtem B. Bityutskiy * stores it in @ec_hdr. This function also checks CRC checksum of the read 740801c135cSArtem B. Bityutskiy * erase counter header. The following codes may be returned: 741801c135cSArtem B. Bityutskiy * 742801c135cSArtem B. Bityutskiy * o %0 if the CRC checksum is correct and the header was successfully read; 743801c135cSArtem B. Bityutskiy * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected 744801c135cSArtem B. Bityutskiy * and corrected by the flash driver; this is harmless but may indicate that 745801c135cSArtem B. Bityutskiy * this eraseblock may become bad soon (but may be not); 746786d7831SArtem Bityutskiy * o %UBI_IO_BAD_HDR if the erase counter header is corrupted (a CRC error); 747756e1df1SArtem Bityutskiy * o %UBI_IO_BAD_HDR_EBADMSG is the same as %UBI_IO_BAD_HDR, but there also was 748756e1df1SArtem Bityutskiy * a data integrity error (uncorrectable ECC error in case of NAND); 74974d82d26SArtem Bityutskiy * o %UBI_IO_FF if only 0xFF bytes were read (the PEB is supposedly empty) 750801c135cSArtem B. Bityutskiy * o a negative error code in case of failure. 751801c135cSArtem B. Bityutskiy */ 752e88d6e10SArtem Bityutskiy int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum, 753801c135cSArtem B. Bityutskiy struct ubi_ec_hdr *ec_hdr, int verbose) 754801c135cSArtem B. Bityutskiy { 75592e1a7d9SArtem Bityutskiy int err, read_err; 756801c135cSArtem B. Bityutskiy uint32_t crc, magic, hdr_crc; 757801c135cSArtem B. Bityutskiy 758801c135cSArtem B. Bityutskiy dbg_io("read EC header from PEB %d", pnum); 759801c135cSArtem B. Bityutskiy ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 760801c135cSArtem B. Bityutskiy 76192e1a7d9SArtem Bityutskiy read_err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE); 76292e1a7d9SArtem Bityutskiy if (read_err) { 763d57f4054SBrian Norris if (read_err != UBI_IO_BITFLIPS && !mtd_is_eccerr(read_err)) 76492e1a7d9SArtem Bityutskiy return read_err; 765801c135cSArtem B. Bityutskiy 766801c135cSArtem B. Bityutskiy /* 767801c135cSArtem B. Bityutskiy * We read all the data, but either a correctable bit-flip 768756e1df1SArtem Bityutskiy * occurred, or MTD reported a data integrity error 769756e1df1SArtem Bityutskiy * (uncorrectable ECC error in case of NAND). The former is 770756e1df1SArtem Bityutskiy * harmless, the later may mean that the read data is 771756e1df1SArtem Bityutskiy * corrupted. But we have a CRC check-sum and we will detect 772756e1df1SArtem Bityutskiy * this. If the EC header is still OK, we just report this as 773756e1df1SArtem Bityutskiy * there was a bit-flip, to force scrubbing. 774801c135cSArtem B. Bityutskiy */ 775801c135cSArtem B. Bityutskiy } 776801c135cSArtem B. Bityutskiy 7773261ebd7SChristoph Hellwig magic = be32_to_cpu(ec_hdr->magic); 778801c135cSArtem B. Bityutskiy if (magic != UBI_EC_HDR_MAGIC) { 779d57f4054SBrian Norris if (mtd_is_eccerr(read_err)) 78092e1a7d9SArtem Bityutskiy return UBI_IO_BAD_HDR_EBADMSG; 781eb89580eSArtem Bityutskiy 782801c135cSArtem B. Bityutskiy /* 783801c135cSArtem B. Bityutskiy * The magic field is wrong. Let's check if we have read all 784801c135cSArtem B. Bityutskiy * 0xFF. If yes, this physical eraseblock is assumed to be 785801c135cSArtem B. Bityutskiy * empty. 786801c135cSArtem B. Bityutskiy */ 787bb00e180SArtem Bityutskiy if (ubi_check_pattern(ec_hdr, 0xFF, UBI_EC_HDR_SIZE)) { 788801c135cSArtem B. Bityutskiy /* The physical eraseblock is supposedly empty */ 789801c135cSArtem B. Bityutskiy if (verbose) 790801c135cSArtem B. Bityutskiy ubi_warn("no EC header found at PEB %d, " 791801c135cSArtem B. Bityutskiy "only 0xFF bytes", pnum); 7926f9fdf62SArtem Bityutskiy dbg_bld("no EC header found at PEB %d, " 793ed45819fSArtem Bityutskiy "only 0xFF bytes", pnum); 79492e1a7d9SArtem Bityutskiy if (!read_err) 79574d82d26SArtem Bityutskiy return UBI_IO_FF; 79692e1a7d9SArtem Bityutskiy else 79792e1a7d9SArtem Bityutskiy return UBI_IO_FF_BITFLIPS; 798801c135cSArtem B. Bityutskiy } 799801c135cSArtem B. Bityutskiy 800801c135cSArtem B. Bityutskiy /* 801801c135cSArtem B. Bityutskiy * This is not a valid erase counter header, and these are not 802801c135cSArtem B. Bityutskiy * 0xFF bytes. Report that the header is corrupted. 803801c135cSArtem B. Bityutskiy */ 804801c135cSArtem B. Bityutskiy if (verbose) { 805801c135cSArtem B. Bityutskiy ubi_warn("bad magic number at PEB %d: %08x instead of " 806801c135cSArtem B. Bityutskiy "%08x", pnum, magic, UBI_EC_HDR_MAGIC); 807801c135cSArtem B. Bityutskiy ubi_dbg_dump_ec_hdr(ec_hdr); 8086f9fdf62SArtem Bityutskiy } 8096f9fdf62SArtem Bityutskiy dbg_bld("bad magic number at PEB %d: %08x instead of " 810ed45819fSArtem Bityutskiy "%08x", pnum, magic, UBI_EC_HDR_MAGIC); 811786d7831SArtem Bityutskiy return UBI_IO_BAD_HDR; 812801c135cSArtem B. Bityutskiy } 813801c135cSArtem B. Bityutskiy 814801c135cSArtem B. Bityutskiy crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC); 8153261ebd7SChristoph Hellwig hdr_crc = be32_to_cpu(ec_hdr->hdr_crc); 816801c135cSArtem B. Bityutskiy 817801c135cSArtem B. Bityutskiy if (hdr_crc != crc) { 818801c135cSArtem B. Bityutskiy if (verbose) { 8199c9ec147SArtem Bityutskiy ubi_warn("bad EC header CRC at PEB %d, calculated " 8209c9ec147SArtem Bityutskiy "%#08x, read %#08x", pnum, crc, hdr_crc); 821801c135cSArtem B. Bityutskiy ubi_dbg_dump_ec_hdr(ec_hdr); 8226f9fdf62SArtem Bityutskiy } 8236f9fdf62SArtem Bityutskiy dbg_bld("bad EC header CRC at PEB %d, calculated " 824ed45819fSArtem Bityutskiy "%#08x, read %#08x", pnum, crc, hdr_crc); 82592e1a7d9SArtem Bityutskiy 82692e1a7d9SArtem Bityutskiy if (!read_err) 82792e1a7d9SArtem Bityutskiy return UBI_IO_BAD_HDR; 82892e1a7d9SArtem Bityutskiy else 82992e1a7d9SArtem Bityutskiy return UBI_IO_BAD_HDR_EBADMSG; 830801c135cSArtem B. Bityutskiy } 831801c135cSArtem B. Bityutskiy 832801c135cSArtem B. Bityutskiy /* And of course validate what has just been read from the media */ 833801c135cSArtem B. Bityutskiy err = validate_ec_hdr(ubi, ec_hdr); 834801c135cSArtem B. Bityutskiy if (err) { 835801c135cSArtem B. Bityutskiy ubi_err("validation failed for PEB %d", pnum); 836801c135cSArtem B. Bityutskiy return -EINVAL; 837801c135cSArtem B. Bityutskiy } 838801c135cSArtem B. Bityutskiy 839eb89580eSArtem Bityutskiy /* 840eb89580eSArtem Bityutskiy * If there was %-EBADMSG, but the header CRC is still OK, report about 841eb89580eSArtem Bityutskiy * a bit-flip to force scrubbing on this PEB. 842eb89580eSArtem Bityutskiy */ 843801c135cSArtem B. Bityutskiy return read_err ? UBI_IO_BITFLIPS : 0; 844801c135cSArtem B. Bityutskiy } 845801c135cSArtem B. Bityutskiy 846801c135cSArtem B. Bityutskiy /** 847801c135cSArtem B. Bityutskiy * ubi_io_write_ec_hdr - write an erase counter header. 848801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 849801c135cSArtem B. Bityutskiy * @pnum: physical eraseblock to write to 850801c135cSArtem B. Bityutskiy * @ec_hdr: the erase counter header to write 851801c135cSArtem B. Bityutskiy * 852801c135cSArtem B. Bityutskiy * This function writes erase counter header described by @ec_hdr to physical 853801c135cSArtem B. Bityutskiy * eraseblock @pnum. It also fills most fields of @ec_hdr before writing, so 854801c135cSArtem B. Bityutskiy * the caller do not have to fill them. Callers must only fill the @ec_hdr->ec 855801c135cSArtem B. Bityutskiy * field. 856801c135cSArtem B. Bityutskiy * 857801c135cSArtem B. Bityutskiy * This function returns zero in case of success and a negative error code in 858801c135cSArtem B. Bityutskiy * case of failure. If %-EIO is returned, the physical eraseblock most probably 859801c135cSArtem B. Bityutskiy * went bad. 860801c135cSArtem B. Bityutskiy */ 861e88d6e10SArtem Bityutskiy int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum, 862801c135cSArtem B. Bityutskiy struct ubi_ec_hdr *ec_hdr) 863801c135cSArtem B. Bityutskiy { 864801c135cSArtem B. Bityutskiy int err; 865801c135cSArtem B. Bityutskiy uint32_t crc; 866801c135cSArtem B. Bityutskiy 867801c135cSArtem B. Bityutskiy dbg_io("write EC header to PEB %d", pnum); 868801c135cSArtem B. Bityutskiy ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 869801c135cSArtem B. Bityutskiy 8703261ebd7SChristoph Hellwig ec_hdr->magic = cpu_to_be32(UBI_EC_HDR_MAGIC); 871801c135cSArtem B. Bityutskiy ec_hdr->version = UBI_VERSION; 8723261ebd7SChristoph Hellwig ec_hdr->vid_hdr_offset = cpu_to_be32(ubi->vid_hdr_offset); 8733261ebd7SChristoph Hellwig ec_hdr->data_offset = cpu_to_be32(ubi->leb_start); 8740c6c7fa1SAdrian Hunter ec_hdr->image_seq = cpu_to_be32(ubi->image_seq); 875801c135cSArtem B. Bityutskiy crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC); 8763261ebd7SChristoph Hellwig ec_hdr->hdr_crc = cpu_to_be32(crc); 877801c135cSArtem B. Bityutskiy 878801c135cSArtem B. Bityutskiy err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr); 879801c135cSArtem B. Bityutskiy if (err) 880adbf05e3SArtem Bityutskiy return err; 881801c135cSArtem B. Bityutskiy 882801c135cSArtem B. Bityutskiy err = ubi_io_write(ubi, ec_hdr, pnum, 0, ubi->ec_hdr_alsize); 883801c135cSArtem B. Bityutskiy return err; 884801c135cSArtem B. Bityutskiy } 885801c135cSArtem B. Bityutskiy 886801c135cSArtem B. Bityutskiy /** 887801c135cSArtem B. Bityutskiy * validate_vid_hdr - validate a volume identifier header. 888801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 889801c135cSArtem B. Bityutskiy * @vid_hdr: the volume identifier header to check 890801c135cSArtem B. Bityutskiy * 891801c135cSArtem B. Bityutskiy * This function checks that data stored in the volume identifier header 892801c135cSArtem B. Bityutskiy * @vid_hdr. Returns zero if the VID header is OK and %1 if not. 893801c135cSArtem B. Bityutskiy */ 894801c135cSArtem B. Bityutskiy static int validate_vid_hdr(const struct ubi_device *ubi, 895801c135cSArtem B. Bityutskiy const struct ubi_vid_hdr *vid_hdr) 896801c135cSArtem B. Bityutskiy { 897801c135cSArtem B. Bityutskiy int vol_type = vid_hdr->vol_type; 898801c135cSArtem B. Bityutskiy int copy_flag = vid_hdr->copy_flag; 8993261ebd7SChristoph Hellwig int vol_id = be32_to_cpu(vid_hdr->vol_id); 9003261ebd7SChristoph Hellwig int lnum = be32_to_cpu(vid_hdr->lnum); 901801c135cSArtem B. Bityutskiy int compat = vid_hdr->compat; 9023261ebd7SChristoph Hellwig int data_size = be32_to_cpu(vid_hdr->data_size); 9033261ebd7SChristoph Hellwig int used_ebs = be32_to_cpu(vid_hdr->used_ebs); 9043261ebd7SChristoph Hellwig int data_pad = be32_to_cpu(vid_hdr->data_pad); 9053261ebd7SChristoph Hellwig int data_crc = be32_to_cpu(vid_hdr->data_crc); 906801c135cSArtem B. Bityutskiy int usable_leb_size = ubi->leb_size - data_pad; 907801c135cSArtem B. Bityutskiy 908801c135cSArtem B. Bityutskiy if (copy_flag != 0 && copy_flag != 1) { 909801c135cSArtem B. Bityutskiy dbg_err("bad copy_flag"); 910801c135cSArtem B. Bityutskiy goto bad; 911801c135cSArtem B. Bityutskiy } 912801c135cSArtem B. Bityutskiy 913801c135cSArtem B. Bityutskiy if (vol_id < 0 || lnum < 0 || data_size < 0 || used_ebs < 0 || 914801c135cSArtem B. Bityutskiy data_pad < 0) { 915801c135cSArtem B. Bityutskiy dbg_err("negative values"); 916801c135cSArtem B. Bityutskiy goto bad; 917801c135cSArtem B. Bityutskiy } 918801c135cSArtem B. Bityutskiy 919801c135cSArtem B. Bityutskiy if (vol_id >= UBI_MAX_VOLUMES && vol_id < UBI_INTERNAL_VOL_START) { 920801c135cSArtem B. Bityutskiy dbg_err("bad vol_id"); 921801c135cSArtem B. Bityutskiy goto bad; 922801c135cSArtem B. Bityutskiy } 923801c135cSArtem B. Bityutskiy 924801c135cSArtem B. Bityutskiy if (vol_id < UBI_INTERNAL_VOL_START && compat != 0) { 925801c135cSArtem B. Bityutskiy dbg_err("bad compat"); 926801c135cSArtem B. Bityutskiy goto bad; 927801c135cSArtem B. Bityutskiy } 928801c135cSArtem B. Bityutskiy 929801c135cSArtem B. Bityutskiy if (vol_id >= UBI_INTERNAL_VOL_START && compat != UBI_COMPAT_DELETE && 930801c135cSArtem B. Bityutskiy compat != UBI_COMPAT_RO && compat != UBI_COMPAT_PRESERVE && 931801c135cSArtem B. Bityutskiy compat != UBI_COMPAT_REJECT) { 932801c135cSArtem B. Bityutskiy dbg_err("bad compat"); 933801c135cSArtem B. Bityutskiy goto bad; 934801c135cSArtem B. Bityutskiy } 935801c135cSArtem B. Bityutskiy 936801c135cSArtem B. Bityutskiy if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) { 937801c135cSArtem B. Bityutskiy dbg_err("bad vol_type"); 938801c135cSArtem B. Bityutskiy goto bad; 939801c135cSArtem B. Bityutskiy } 940801c135cSArtem B. Bityutskiy 941801c135cSArtem B. Bityutskiy if (data_pad >= ubi->leb_size / 2) { 942801c135cSArtem B. Bityutskiy dbg_err("bad data_pad"); 943801c135cSArtem B. Bityutskiy goto bad; 944801c135cSArtem B. Bityutskiy } 945801c135cSArtem B. Bityutskiy 946801c135cSArtem B. Bityutskiy if (vol_type == UBI_VID_STATIC) { 947801c135cSArtem B. Bityutskiy /* 948801c135cSArtem B. Bityutskiy * Although from high-level point of view static volumes may 949801c135cSArtem B. Bityutskiy * contain zero bytes of data, but no VID headers can contain 950801c135cSArtem B. Bityutskiy * zero at these fields, because they empty volumes do not have 951801c135cSArtem B. Bityutskiy * mapped logical eraseblocks. 952801c135cSArtem B. Bityutskiy */ 953801c135cSArtem B. Bityutskiy if (used_ebs == 0) { 954801c135cSArtem B. Bityutskiy dbg_err("zero used_ebs"); 955801c135cSArtem B. Bityutskiy goto bad; 956801c135cSArtem B. Bityutskiy } 957801c135cSArtem B. Bityutskiy if (data_size == 0) { 958801c135cSArtem B. Bityutskiy dbg_err("zero data_size"); 959801c135cSArtem B. Bityutskiy goto bad; 960801c135cSArtem B. Bityutskiy } 961801c135cSArtem B. Bityutskiy if (lnum < used_ebs - 1) { 962801c135cSArtem B. Bityutskiy if (data_size != usable_leb_size) { 963801c135cSArtem B. Bityutskiy dbg_err("bad data_size"); 964801c135cSArtem B. Bityutskiy goto bad; 965801c135cSArtem B. Bityutskiy } 966801c135cSArtem B. Bityutskiy } else if (lnum == used_ebs - 1) { 967801c135cSArtem B. Bityutskiy if (data_size == 0) { 968801c135cSArtem B. Bityutskiy dbg_err("bad data_size at last LEB"); 969801c135cSArtem B. Bityutskiy goto bad; 970801c135cSArtem B. Bityutskiy } 971801c135cSArtem B. Bityutskiy } else { 972801c135cSArtem B. Bityutskiy dbg_err("too high lnum"); 973801c135cSArtem B. Bityutskiy goto bad; 974801c135cSArtem B. Bityutskiy } 975801c135cSArtem B. Bityutskiy } else { 976801c135cSArtem B. Bityutskiy if (copy_flag == 0) { 977801c135cSArtem B. Bityutskiy if (data_crc != 0) { 978801c135cSArtem B. Bityutskiy dbg_err("non-zero data CRC"); 979801c135cSArtem B. Bityutskiy goto bad; 980801c135cSArtem B. Bityutskiy } 981801c135cSArtem B. Bityutskiy if (data_size != 0) { 982801c135cSArtem B. Bityutskiy dbg_err("non-zero data_size"); 983801c135cSArtem B. Bityutskiy goto bad; 984801c135cSArtem B. Bityutskiy } 985801c135cSArtem B. Bityutskiy } else { 986801c135cSArtem B. Bityutskiy if (data_size == 0) { 987801c135cSArtem B. Bityutskiy dbg_err("zero data_size of copy"); 988801c135cSArtem B. Bityutskiy goto bad; 989801c135cSArtem B. Bityutskiy } 990801c135cSArtem B. Bityutskiy } 991801c135cSArtem B. Bityutskiy if (used_ebs != 0) { 992801c135cSArtem B. Bityutskiy dbg_err("bad used_ebs"); 993801c135cSArtem B. Bityutskiy goto bad; 994801c135cSArtem B. Bityutskiy } 995801c135cSArtem B. Bityutskiy } 996801c135cSArtem B. Bityutskiy 997801c135cSArtem B. Bityutskiy return 0; 998801c135cSArtem B. Bityutskiy 999801c135cSArtem B. Bityutskiy bad: 1000801c135cSArtem B. Bityutskiy ubi_err("bad VID header"); 1001801c135cSArtem B. Bityutskiy ubi_dbg_dump_vid_hdr(vid_hdr); 1002801c135cSArtem B. Bityutskiy ubi_dbg_dump_stack(); 1003801c135cSArtem B. Bityutskiy return 1; 1004801c135cSArtem B. Bityutskiy } 1005801c135cSArtem B. Bityutskiy 1006801c135cSArtem B. Bityutskiy /** 1007801c135cSArtem B. Bityutskiy * ubi_io_read_vid_hdr - read and check a volume identifier header. 1008801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 1009801c135cSArtem B. Bityutskiy * @pnum: physical eraseblock number to read from 1010801c135cSArtem B. Bityutskiy * @vid_hdr: &struct ubi_vid_hdr object where to store the read volume 1011801c135cSArtem B. Bityutskiy * identifier header 1012801c135cSArtem B. Bityutskiy * @verbose: be verbose if the header is corrupted or wasn't found 1013801c135cSArtem B. Bityutskiy * 1014801c135cSArtem B. Bityutskiy * This function reads the volume identifier header from physical eraseblock 1015801c135cSArtem B. Bityutskiy * @pnum and stores it in @vid_hdr. It also checks CRC checksum of the read 101674d82d26SArtem Bityutskiy * volume identifier header. The error codes are the same as in 101774d82d26SArtem Bityutskiy * 'ubi_io_read_ec_hdr()'. 1018801c135cSArtem B. Bityutskiy * 101974d82d26SArtem Bityutskiy * Note, the implementation of this function is also very similar to 102074d82d26SArtem Bityutskiy * 'ubi_io_read_ec_hdr()', so refer commentaries in 'ubi_io_read_ec_hdr()'. 1021801c135cSArtem B. Bityutskiy */ 1022e88d6e10SArtem Bityutskiy int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum, 1023801c135cSArtem B. Bityutskiy struct ubi_vid_hdr *vid_hdr, int verbose) 1024801c135cSArtem B. Bityutskiy { 102592e1a7d9SArtem Bityutskiy int err, read_err; 1026801c135cSArtem B. Bityutskiy uint32_t crc, magic, hdr_crc; 1027801c135cSArtem B. Bityutskiy void *p; 1028801c135cSArtem B. Bityutskiy 1029801c135cSArtem B. Bityutskiy dbg_io("read VID header from PEB %d", pnum); 1030801c135cSArtem B. Bityutskiy ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 1031801c135cSArtem B. Bityutskiy 1032801c135cSArtem B. Bityutskiy p = (char *)vid_hdr - ubi->vid_hdr_shift; 103392e1a7d9SArtem Bityutskiy read_err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset, 1034801c135cSArtem B. Bityutskiy ubi->vid_hdr_alsize); 1035d57f4054SBrian Norris if (read_err && read_err != UBI_IO_BITFLIPS && !mtd_is_eccerr(read_err)) 103692e1a7d9SArtem Bityutskiy return read_err; 1037801c135cSArtem B. Bityutskiy 10383261ebd7SChristoph Hellwig magic = be32_to_cpu(vid_hdr->magic); 1039801c135cSArtem B. Bityutskiy if (magic != UBI_VID_HDR_MAGIC) { 1040d57f4054SBrian Norris if (mtd_is_eccerr(read_err)) 104192e1a7d9SArtem Bityutskiy return UBI_IO_BAD_HDR_EBADMSG; 1042eb89580eSArtem Bityutskiy 1043bb00e180SArtem Bityutskiy if (ubi_check_pattern(vid_hdr, 0xFF, UBI_VID_HDR_SIZE)) { 1044801c135cSArtem B. Bityutskiy if (verbose) 1045801c135cSArtem B. Bityutskiy ubi_warn("no VID header found at PEB %d, " 1046801c135cSArtem B. Bityutskiy "only 0xFF bytes", pnum); 10476f9fdf62SArtem Bityutskiy dbg_bld("no VID header found at PEB %d, " 1048ed45819fSArtem Bityutskiy "only 0xFF bytes", pnum); 104992e1a7d9SArtem Bityutskiy if (!read_err) 105074d82d26SArtem Bityutskiy return UBI_IO_FF; 105192e1a7d9SArtem Bityutskiy else 105292e1a7d9SArtem Bityutskiy return UBI_IO_FF_BITFLIPS; 1053801c135cSArtem B. Bityutskiy } 1054801c135cSArtem B. Bityutskiy 1055801c135cSArtem B. Bityutskiy if (verbose) { 1056801c135cSArtem B. Bityutskiy ubi_warn("bad magic number at PEB %d: %08x instead of " 1057801c135cSArtem B. Bityutskiy "%08x", pnum, magic, UBI_VID_HDR_MAGIC); 1058801c135cSArtem B. Bityutskiy ubi_dbg_dump_vid_hdr(vid_hdr); 10596f9fdf62SArtem Bityutskiy } 10606f9fdf62SArtem Bityutskiy dbg_bld("bad magic number at PEB %d: %08x instead of " 1061ed45819fSArtem Bityutskiy "%08x", pnum, magic, UBI_VID_HDR_MAGIC); 1062786d7831SArtem Bityutskiy return UBI_IO_BAD_HDR; 1063801c135cSArtem B. Bityutskiy } 1064801c135cSArtem B. Bityutskiy 1065801c135cSArtem B. Bityutskiy crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC); 10663261ebd7SChristoph Hellwig hdr_crc = be32_to_cpu(vid_hdr->hdr_crc); 1067801c135cSArtem B. Bityutskiy 1068801c135cSArtem B. Bityutskiy if (hdr_crc != crc) { 1069801c135cSArtem B. Bityutskiy if (verbose) { 1070801c135cSArtem B. Bityutskiy ubi_warn("bad CRC at PEB %d, calculated %#08x, " 1071801c135cSArtem B. Bityutskiy "read %#08x", pnum, crc, hdr_crc); 1072801c135cSArtem B. Bityutskiy ubi_dbg_dump_vid_hdr(vid_hdr); 10736f9fdf62SArtem Bityutskiy } 10746f9fdf62SArtem Bityutskiy dbg_bld("bad CRC at PEB %d, calculated %#08x, " 1075ed45819fSArtem Bityutskiy "read %#08x", pnum, crc, hdr_crc); 107692e1a7d9SArtem Bityutskiy if (!read_err) 107792e1a7d9SArtem Bityutskiy return UBI_IO_BAD_HDR; 107892e1a7d9SArtem Bityutskiy else 107992e1a7d9SArtem Bityutskiy return UBI_IO_BAD_HDR_EBADMSG; 1080801c135cSArtem B. Bityutskiy } 1081801c135cSArtem B. Bityutskiy 1082801c135cSArtem B. Bityutskiy err = validate_vid_hdr(ubi, vid_hdr); 1083801c135cSArtem B. Bityutskiy if (err) { 1084801c135cSArtem B. Bityutskiy ubi_err("validation failed for PEB %d", pnum); 1085801c135cSArtem B. Bityutskiy return -EINVAL; 1086801c135cSArtem B. Bityutskiy } 1087801c135cSArtem B. Bityutskiy 1088801c135cSArtem B. Bityutskiy return read_err ? UBI_IO_BITFLIPS : 0; 1089801c135cSArtem B. Bityutskiy } 1090801c135cSArtem B. Bityutskiy 1091801c135cSArtem B. Bityutskiy /** 1092801c135cSArtem B. Bityutskiy * ubi_io_write_vid_hdr - write a volume identifier header. 1093801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 1094801c135cSArtem B. Bityutskiy * @pnum: the physical eraseblock number to write to 1095801c135cSArtem B. Bityutskiy * @vid_hdr: the volume identifier header to write 1096801c135cSArtem B. Bityutskiy * 1097801c135cSArtem B. Bityutskiy * This function writes the volume identifier header described by @vid_hdr to 1098801c135cSArtem B. Bityutskiy * physical eraseblock @pnum. This function automatically fills the 1099801c135cSArtem B. Bityutskiy * @vid_hdr->magic and the @vid_hdr->version fields, as well as calculates 1100801c135cSArtem B. Bityutskiy * header CRC checksum and stores it at vid_hdr->hdr_crc. 1101801c135cSArtem B. Bityutskiy * 1102801c135cSArtem B. Bityutskiy * This function returns zero in case of success and a negative error code in 1103801c135cSArtem B. Bityutskiy * case of failure. If %-EIO is returned, the physical eraseblock probably went 1104801c135cSArtem B. Bityutskiy * bad. 1105801c135cSArtem B. Bityutskiy */ 1106e88d6e10SArtem Bityutskiy int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum, 1107801c135cSArtem B. Bityutskiy struct ubi_vid_hdr *vid_hdr) 1108801c135cSArtem B. Bityutskiy { 1109801c135cSArtem B. Bityutskiy int err; 1110801c135cSArtem B. Bityutskiy uint32_t crc; 1111801c135cSArtem B. Bityutskiy void *p; 1112801c135cSArtem B. Bityutskiy 1113801c135cSArtem B. Bityutskiy dbg_io("write VID header to PEB %d", pnum); 1114801c135cSArtem B. Bityutskiy ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 1115801c135cSArtem B. Bityutskiy 1116801c135cSArtem B. Bityutskiy err = paranoid_check_peb_ec_hdr(ubi, pnum); 1117801c135cSArtem B. Bityutskiy if (err) 1118adbf05e3SArtem Bityutskiy return err; 1119801c135cSArtem B. Bityutskiy 11203261ebd7SChristoph Hellwig vid_hdr->magic = cpu_to_be32(UBI_VID_HDR_MAGIC); 1121801c135cSArtem B. Bityutskiy vid_hdr->version = UBI_VERSION; 1122801c135cSArtem B. Bityutskiy crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC); 11233261ebd7SChristoph Hellwig vid_hdr->hdr_crc = cpu_to_be32(crc); 1124801c135cSArtem B. Bityutskiy 1125801c135cSArtem B. Bityutskiy err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr); 1126801c135cSArtem B. Bityutskiy if (err) 1127adbf05e3SArtem Bityutskiy return err; 1128801c135cSArtem B. Bityutskiy 1129801c135cSArtem B. Bityutskiy p = (char *)vid_hdr - ubi->vid_hdr_shift; 1130801c135cSArtem B. Bityutskiy err = ubi_io_write(ubi, p, pnum, ubi->vid_hdr_aloffset, 1131801c135cSArtem B. Bityutskiy ubi->vid_hdr_alsize); 1132801c135cSArtem B. Bityutskiy return err; 1133801c135cSArtem B. Bityutskiy } 1134801c135cSArtem B. Bityutskiy 113592d124f5SArtem Bityutskiy #ifdef CONFIG_MTD_UBI_DEBUG 1136801c135cSArtem B. Bityutskiy 1137801c135cSArtem B. Bityutskiy /** 1138801c135cSArtem B. Bityutskiy * paranoid_check_not_bad - ensure that a physical eraseblock is not bad. 1139801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 1140801c135cSArtem B. Bityutskiy * @pnum: physical eraseblock number to check 1141801c135cSArtem B. Bityutskiy * 1142adbf05e3SArtem Bityutskiy * This function returns zero if the physical eraseblock is good, %-EINVAL if 1143adbf05e3SArtem Bityutskiy * it is bad and a negative error code if an error occurred. 1144801c135cSArtem B. Bityutskiy */ 1145801c135cSArtem B. Bityutskiy static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum) 1146801c135cSArtem B. Bityutskiy { 1147801c135cSArtem B. Bityutskiy int err; 1148801c135cSArtem B. Bityutskiy 11492a734bb8SArtem Bityutskiy if (!ubi->dbg->chk_io) 115092d124f5SArtem Bityutskiy return 0; 115192d124f5SArtem Bityutskiy 1152801c135cSArtem B. Bityutskiy err = ubi_io_is_bad(ubi, pnum); 1153801c135cSArtem B. Bityutskiy if (!err) 1154801c135cSArtem B. Bityutskiy return err; 1155801c135cSArtem B. Bityutskiy 1156801c135cSArtem B. Bityutskiy ubi_err("paranoid check failed for PEB %d", pnum); 1157801c135cSArtem B. Bityutskiy ubi_dbg_dump_stack(); 1158adbf05e3SArtem Bityutskiy return err > 0 ? -EINVAL : err; 1159801c135cSArtem B. Bityutskiy } 1160801c135cSArtem B. Bityutskiy 1161801c135cSArtem B. Bityutskiy /** 1162801c135cSArtem B. Bityutskiy * paranoid_check_ec_hdr - check if an erase counter header is all right. 1163801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 1164801c135cSArtem B. Bityutskiy * @pnum: physical eraseblock number the erase counter header belongs to 1165801c135cSArtem B. Bityutskiy * @ec_hdr: the erase counter header to check 1166801c135cSArtem B. Bityutskiy * 1167801c135cSArtem B. Bityutskiy * This function returns zero if the erase counter header contains valid 1168adbf05e3SArtem Bityutskiy * values, and %-EINVAL if not. 1169801c135cSArtem B. Bityutskiy */ 1170801c135cSArtem B. Bityutskiy static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum, 1171801c135cSArtem B. Bityutskiy const struct ubi_ec_hdr *ec_hdr) 1172801c135cSArtem B. Bityutskiy { 1173801c135cSArtem B. Bityutskiy int err; 1174801c135cSArtem B. Bityutskiy uint32_t magic; 1175801c135cSArtem B. Bityutskiy 11762a734bb8SArtem Bityutskiy if (!ubi->dbg->chk_io) 117792d124f5SArtem Bityutskiy return 0; 117892d124f5SArtem Bityutskiy 11793261ebd7SChristoph Hellwig magic = be32_to_cpu(ec_hdr->magic); 1180801c135cSArtem B. Bityutskiy if (magic != UBI_EC_HDR_MAGIC) { 1181801c135cSArtem B. Bityutskiy ubi_err("bad magic %#08x, must be %#08x", 1182801c135cSArtem B. Bityutskiy magic, UBI_EC_HDR_MAGIC); 1183801c135cSArtem B. Bityutskiy goto fail; 1184801c135cSArtem B. Bityutskiy } 1185801c135cSArtem B. Bityutskiy 1186801c135cSArtem B. Bityutskiy err = validate_ec_hdr(ubi, ec_hdr); 1187801c135cSArtem B. Bityutskiy if (err) { 1188801c135cSArtem B. Bityutskiy ubi_err("paranoid check failed for PEB %d", pnum); 1189801c135cSArtem B. Bityutskiy goto fail; 1190801c135cSArtem B. Bityutskiy } 1191801c135cSArtem B. Bityutskiy 1192801c135cSArtem B. Bityutskiy return 0; 1193801c135cSArtem B. Bityutskiy 1194801c135cSArtem B. Bityutskiy fail: 1195801c135cSArtem B. Bityutskiy ubi_dbg_dump_ec_hdr(ec_hdr); 1196801c135cSArtem B. Bityutskiy ubi_dbg_dump_stack(); 1197adbf05e3SArtem Bityutskiy return -EINVAL; 1198801c135cSArtem B. Bityutskiy } 1199801c135cSArtem B. Bityutskiy 1200801c135cSArtem B. Bityutskiy /** 1201ebaaf1afSArtem Bityutskiy * paranoid_check_peb_ec_hdr - check erase counter header. 1202801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 1203801c135cSArtem B. Bityutskiy * @pnum: the physical eraseblock number to check 1204801c135cSArtem B. Bityutskiy * 1205adbf05e3SArtem Bityutskiy * This function returns zero if the erase counter header is all right and and 1206adbf05e3SArtem Bityutskiy * a negative error code if not or if an error occurred. 1207801c135cSArtem B. Bityutskiy */ 1208801c135cSArtem B. Bityutskiy static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum) 1209801c135cSArtem B. Bityutskiy { 1210801c135cSArtem B. Bityutskiy int err; 1211801c135cSArtem B. Bityutskiy uint32_t crc, hdr_crc; 1212801c135cSArtem B. Bityutskiy struct ubi_ec_hdr *ec_hdr; 1213801c135cSArtem B. Bityutskiy 12142a734bb8SArtem Bityutskiy if (!ubi->dbg->chk_io) 121592d124f5SArtem Bityutskiy return 0; 121692d124f5SArtem Bityutskiy 121733818bbbSArtem Bityutskiy ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS); 1218801c135cSArtem B. Bityutskiy if (!ec_hdr) 1219801c135cSArtem B. Bityutskiy return -ENOMEM; 1220801c135cSArtem B. Bityutskiy 1221801c135cSArtem B. Bityutskiy err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE); 1222d57f4054SBrian Norris if (err && err != UBI_IO_BITFLIPS && !mtd_is_eccerr(err)) 1223801c135cSArtem B. Bityutskiy goto exit; 1224801c135cSArtem B. Bityutskiy 1225801c135cSArtem B. Bityutskiy crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC); 12263261ebd7SChristoph Hellwig hdr_crc = be32_to_cpu(ec_hdr->hdr_crc); 1227801c135cSArtem B. Bityutskiy if (hdr_crc != crc) { 1228801c135cSArtem B. Bityutskiy ubi_err("bad CRC, calculated %#08x, read %#08x", crc, hdr_crc); 1229801c135cSArtem B. Bityutskiy ubi_err("paranoid check failed for PEB %d", pnum); 1230801c135cSArtem B. Bityutskiy ubi_dbg_dump_ec_hdr(ec_hdr); 1231801c135cSArtem B. Bityutskiy ubi_dbg_dump_stack(); 1232adbf05e3SArtem Bityutskiy err = -EINVAL; 1233801c135cSArtem B. Bityutskiy goto exit; 1234801c135cSArtem B. Bityutskiy } 1235801c135cSArtem B. Bityutskiy 1236801c135cSArtem B. Bityutskiy err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr); 1237801c135cSArtem B. Bityutskiy 1238801c135cSArtem B. Bityutskiy exit: 1239801c135cSArtem B. Bityutskiy kfree(ec_hdr); 1240801c135cSArtem B. Bityutskiy return err; 1241801c135cSArtem B. Bityutskiy } 1242801c135cSArtem B. Bityutskiy 1243801c135cSArtem B. Bityutskiy /** 1244801c135cSArtem B. Bityutskiy * paranoid_check_vid_hdr - check that a volume identifier header is all right. 1245801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 1246801c135cSArtem B. Bityutskiy * @pnum: physical eraseblock number the volume identifier header belongs to 1247801c135cSArtem B. Bityutskiy * @vid_hdr: the volume identifier header to check 1248801c135cSArtem B. Bityutskiy * 1249801c135cSArtem B. Bityutskiy * This function returns zero if the volume identifier header is all right, and 1250adbf05e3SArtem Bityutskiy * %-EINVAL if not. 1251801c135cSArtem B. Bityutskiy */ 1252801c135cSArtem B. Bityutskiy static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum, 1253801c135cSArtem B. Bityutskiy const struct ubi_vid_hdr *vid_hdr) 1254801c135cSArtem B. Bityutskiy { 1255801c135cSArtem B. Bityutskiy int err; 1256801c135cSArtem B. Bityutskiy uint32_t magic; 1257801c135cSArtem B. Bityutskiy 12582a734bb8SArtem Bityutskiy if (!ubi->dbg->chk_io) 125992d124f5SArtem Bityutskiy return 0; 126092d124f5SArtem Bityutskiy 12613261ebd7SChristoph Hellwig magic = be32_to_cpu(vid_hdr->magic); 1262801c135cSArtem B. Bityutskiy if (magic != UBI_VID_HDR_MAGIC) { 1263801c135cSArtem B. Bityutskiy ubi_err("bad VID header magic %#08x at PEB %d, must be %#08x", 1264801c135cSArtem B. Bityutskiy magic, pnum, UBI_VID_HDR_MAGIC); 1265801c135cSArtem B. Bityutskiy goto fail; 1266801c135cSArtem B. Bityutskiy } 1267801c135cSArtem B. Bityutskiy 1268801c135cSArtem B. Bityutskiy err = validate_vid_hdr(ubi, vid_hdr); 1269801c135cSArtem B. Bityutskiy if (err) { 1270801c135cSArtem B. Bityutskiy ubi_err("paranoid check failed for PEB %d", pnum); 1271801c135cSArtem B. Bityutskiy goto fail; 1272801c135cSArtem B. Bityutskiy } 1273801c135cSArtem B. Bityutskiy 1274801c135cSArtem B. Bityutskiy return err; 1275801c135cSArtem B. Bityutskiy 1276801c135cSArtem B. Bityutskiy fail: 1277801c135cSArtem B. Bityutskiy ubi_err("paranoid check failed for PEB %d", pnum); 1278801c135cSArtem B. Bityutskiy ubi_dbg_dump_vid_hdr(vid_hdr); 1279801c135cSArtem B. Bityutskiy ubi_dbg_dump_stack(); 1280adbf05e3SArtem Bityutskiy return -EINVAL; 1281801c135cSArtem B. Bityutskiy 1282801c135cSArtem B. Bityutskiy } 1283801c135cSArtem B. Bityutskiy 1284801c135cSArtem B. Bityutskiy /** 1285ebaaf1afSArtem Bityutskiy * paranoid_check_peb_vid_hdr - check volume identifier header. 1286801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 1287801c135cSArtem B. Bityutskiy * @pnum: the physical eraseblock number to check 1288801c135cSArtem B. Bityutskiy * 1289801c135cSArtem B. Bityutskiy * This function returns zero if the volume identifier header is all right, 1290adbf05e3SArtem Bityutskiy * and a negative error code if not or if an error occurred. 1291801c135cSArtem B. Bityutskiy */ 1292801c135cSArtem B. Bityutskiy static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum) 1293801c135cSArtem B. Bityutskiy { 1294801c135cSArtem B. Bityutskiy int err; 1295801c135cSArtem B. Bityutskiy uint32_t crc, hdr_crc; 1296801c135cSArtem B. Bityutskiy struct ubi_vid_hdr *vid_hdr; 1297801c135cSArtem B. Bityutskiy void *p; 1298801c135cSArtem B. Bityutskiy 12992a734bb8SArtem Bityutskiy if (!ubi->dbg->chk_io) 130092d124f5SArtem Bityutskiy return 0; 130192d124f5SArtem Bityutskiy 130233818bbbSArtem Bityutskiy vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS); 1303801c135cSArtem B. Bityutskiy if (!vid_hdr) 1304801c135cSArtem B. Bityutskiy return -ENOMEM; 1305801c135cSArtem B. Bityutskiy 1306801c135cSArtem B. Bityutskiy p = (char *)vid_hdr - ubi->vid_hdr_shift; 1307801c135cSArtem B. Bityutskiy err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset, 1308801c135cSArtem B. Bityutskiy ubi->vid_hdr_alsize); 1309d57f4054SBrian Norris if (err && err != UBI_IO_BITFLIPS && !mtd_is_eccerr(err)) 1310801c135cSArtem B. Bityutskiy goto exit; 1311801c135cSArtem B. Bityutskiy 1312801c135cSArtem B. Bityutskiy crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_EC_HDR_SIZE_CRC); 13133261ebd7SChristoph Hellwig hdr_crc = be32_to_cpu(vid_hdr->hdr_crc); 1314801c135cSArtem B. Bityutskiy if (hdr_crc != crc) { 1315801c135cSArtem B. Bityutskiy ubi_err("bad VID header CRC at PEB %d, calculated %#08x, " 1316801c135cSArtem B. Bityutskiy "read %#08x", pnum, crc, hdr_crc); 1317801c135cSArtem B. Bityutskiy ubi_err("paranoid check failed for PEB %d", pnum); 1318801c135cSArtem B. Bityutskiy ubi_dbg_dump_vid_hdr(vid_hdr); 1319801c135cSArtem B. Bityutskiy ubi_dbg_dump_stack(); 1320adbf05e3SArtem Bityutskiy err = -EINVAL; 1321801c135cSArtem B. Bityutskiy goto exit; 1322801c135cSArtem B. Bityutskiy } 1323801c135cSArtem B. Bityutskiy 1324801c135cSArtem B. Bityutskiy err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr); 1325801c135cSArtem B. Bityutskiy 1326801c135cSArtem B. Bityutskiy exit: 1327801c135cSArtem B. Bityutskiy ubi_free_vid_hdr(ubi, vid_hdr); 1328801c135cSArtem B. Bityutskiy return err; 1329801c135cSArtem B. Bityutskiy } 1330801c135cSArtem B. Bityutskiy 1331801c135cSArtem B. Bityutskiy /** 13326e9065d7SArtem Bityutskiy * ubi_dbg_check_write - make sure write succeeded. 13336e9065d7SArtem Bityutskiy * @ubi: UBI device description object 13346e9065d7SArtem Bityutskiy * @buf: buffer with data which were written 13356e9065d7SArtem Bityutskiy * @pnum: physical eraseblock number the data were written to 13366e9065d7SArtem Bityutskiy * @offset: offset within the physical eraseblock the data were written to 13376e9065d7SArtem Bityutskiy * @len: how many bytes were written 13386e9065d7SArtem Bityutskiy * 13396e9065d7SArtem Bityutskiy * This functions reads data which were recently written and compares it with 13406e9065d7SArtem Bityutskiy * the original data buffer - the data have to match. Returns zero if the data 13416e9065d7SArtem Bityutskiy * match and a negative error code if not or in case of failure. 13426e9065d7SArtem Bityutskiy */ 13436e9065d7SArtem Bityutskiy int ubi_dbg_check_write(struct ubi_device *ubi, const void *buf, int pnum, 13446e9065d7SArtem Bityutskiy int offset, int len) 13456e9065d7SArtem Bityutskiy { 13466e9065d7SArtem Bityutskiy int err, i; 13477950d023SArtem Bityutskiy size_t read; 1348a7586743SArtem Bityutskiy void *buf1; 13497950d023SArtem Bityutskiy loff_t addr = (loff_t)pnum * ubi->peb_size + offset; 13506e9065d7SArtem Bityutskiy 13512a734bb8SArtem Bityutskiy if (!ubi->dbg->chk_io) 135292d124f5SArtem Bityutskiy return 0; 135392d124f5SArtem Bityutskiy 13543d46b316SArtem Bityutskiy buf1 = __vmalloc(len, GFP_NOFS, PAGE_KERNEL); 1355a7586743SArtem Bityutskiy if (!buf1) { 1356a7586743SArtem Bityutskiy ubi_err("cannot allocate memory to check writes"); 1357a7586743SArtem Bityutskiy return 0; 1358a7586743SArtem Bityutskiy } 1359a7586743SArtem Bityutskiy 1360a7586743SArtem Bityutskiy err = ubi->mtd->read(ubi->mtd, addr, len, &read, buf1); 1361d57f4054SBrian Norris if (err && !mtd_is_bitflip(err)) 1362a7586743SArtem Bityutskiy goto out_free; 13636e9065d7SArtem Bityutskiy 13646e9065d7SArtem Bityutskiy for (i = 0; i < len; i++) { 13656e9065d7SArtem Bityutskiy uint8_t c = ((uint8_t *)buf)[i]; 1366a7586743SArtem Bityutskiy uint8_t c1 = ((uint8_t *)buf1)[i]; 13676e9065d7SArtem Bityutskiy int dump_len; 13686e9065d7SArtem Bityutskiy 13696e9065d7SArtem Bityutskiy if (c == c1) 13706e9065d7SArtem Bityutskiy continue; 13716e9065d7SArtem Bityutskiy 13726e9065d7SArtem Bityutskiy ubi_err("paranoid check failed for PEB %d:%d, len %d", 13736e9065d7SArtem Bityutskiy pnum, offset, len); 13746e9065d7SArtem Bityutskiy ubi_msg("data differ at position %d", i); 13756e9065d7SArtem Bityutskiy dump_len = max_t(int, 128, len - i); 13766e9065d7SArtem Bityutskiy ubi_msg("hex dump of the original buffer from %d to %d", 13776e9065d7SArtem Bityutskiy i, i + dump_len); 13786e9065d7SArtem Bityutskiy print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, 13796e9065d7SArtem Bityutskiy buf + i, dump_len, 1); 13806e9065d7SArtem Bityutskiy ubi_msg("hex dump of the read buffer from %d to %d", 13816e9065d7SArtem Bityutskiy i, i + dump_len); 13826e9065d7SArtem Bityutskiy print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, 1383a7586743SArtem Bityutskiy buf1 + i, dump_len, 1); 13846e9065d7SArtem Bityutskiy ubi_dbg_dump_stack(); 13856e9065d7SArtem Bityutskiy err = -EINVAL; 1386a7586743SArtem Bityutskiy goto out_free; 13876e9065d7SArtem Bityutskiy } 13886e9065d7SArtem Bityutskiy 1389a7586743SArtem Bityutskiy vfree(buf1); 13906e9065d7SArtem Bityutskiy return 0; 13916e9065d7SArtem Bityutskiy 1392a7586743SArtem Bityutskiy out_free: 1393a7586743SArtem Bityutskiy vfree(buf1); 13946e9065d7SArtem Bityutskiy return err; 13956e9065d7SArtem Bityutskiy } 13966e9065d7SArtem Bityutskiy 13976e9065d7SArtem Bityutskiy /** 139840a71a87SArtem Bityutskiy * ubi_dbg_check_all_ff - check that a region of flash is empty. 1399801c135cSArtem B. Bityutskiy * @ubi: UBI device description object 1400801c135cSArtem B. Bityutskiy * @pnum: the physical eraseblock number to check 1401801c135cSArtem B. Bityutskiy * @offset: the starting offset within the physical eraseblock to check 1402801c135cSArtem B. Bityutskiy * @len: the length of the region to check 1403801c135cSArtem B. Bityutskiy * 1404801c135cSArtem B. Bityutskiy * This function returns zero if only 0xFF bytes are present at offset 1405adbf05e3SArtem Bityutskiy * @offset of the physical eraseblock @pnum, and a negative error code if not 1406adbf05e3SArtem Bityutskiy * or if an error occurred. 1407801c135cSArtem B. Bityutskiy */ 140840a71a87SArtem Bityutskiy int ubi_dbg_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len) 1409801c135cSArtem B. Bityutskiy { 1410801c135cSArtem B. Bityutskiy size_t read; 1411801c135cSArtem B. Bityutskiy int err; 1412332873d6SArtem Bityutskiy void *buf; 1413801c135cSArtem B. Bityutskiy loff_t addr = (loff_t)pnum * ubi->peb_size + offset; 1414801c135cSArtem B. Bityutskiy 14152a734bb8SArtem Bityutskiy if (!ubi->dbg->chk_io) 141692d124f5SArtem Bityutskiy return 0; 141792d124f5SArtem Bityutskiy 14183d46b316SArtem Bityutskiy buf = __vmalloc(len, GFP_NOFS, PAGE_KERNEL); 1419332873d6SArtem Bityutskiy if (!buf) { 1420332873d6SArtem Bityutskiy ubi_err("cannot allocate memory to check for 0xFFs"); 1421332873d6SArtem Bityutskiy return 0; 1422332873d6SArtem Bityutskiy } 1423332873d6SArtem Bityutskiy 1424332873d6SArtem Bityutskiy err = ubi->mtd->read(ubi->mtd, addr, len, &read, buf); 1425d57f4054SBrian Norris if (err && !mtd_is_bitflip(err)) { 1426801c135cSArtem B. Bityutskiy ubi_err("error %d while reading %d bytes from PEB %d:%d, " 1427801c135cSArtem B. Bityutskiy "read %zd bytes", err, len, pnum, offset, read); 1428801c135cSArtem B. Bityutskiy goto error; 1429801c135cSArtem B. Bityutskiy } 1430801c135cSArtem B. Bityutskiy 1431332873d6SArtem Bityutskiy err = ubi_check_pattern(buf, 0xFF, len); 1432801c135cSArtem B. Bityutskiy if (err == 0) { 1433801c135cSArtem B. Bityutskiy ubi_err("flash region at PEB %d:%d, length %d does not " 1434801c135cSArtem B. Bityutskiy "contain all 0xFF bytes", pnum, offset, len); 1435801c135cSArtem B. Bityutskiy goto fail; 1436801c135cSArtem B. Bityutskiy } 1437801c135cSArtem B. Bityutskiy 1438332873d6SArtem Bityutskiy vfree(buf); 1439801c135cSArtem B. Bityutskiy return 0; 1440801c135cSArtem B. Bityutskiy 1441801c135cSArtem B. Bityutskiy fail: 1442801c135cSArtem B. Bityutskiy ubi_err("paranoid check failed for PEB %d", pnum); 1443c8566350SArtem Bityutskiy ubi_msg("hex dump of the %d-%d region", offset, offset + len); 1444332873d6SArtem Bityutskiy print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, buf, len, 1); 1445adbf05e3SArtem Bityutskiy err = -EINVAL; 1446801c135cSArtem B. Bityutskiy error: 1447801c135cSArtem B. Bityutskiy ubi_dbg_dump_stack(); 1448332873d6SArtem Bityutskiy vfree(buf); 1449801c135cSArtem B. Bityutskiy return err; 1450801c135cSArtem B. Bityutskiy } 1451801c135cSArtem B. Bityutskiy 145292d124f5SArtem Bityutskiy #endif /* CONFIG_MTD_UBI_DEBUG */ 1453