12d262c48SKyungmin Park /* 22d262c48SKyungmin Park * Copyright (c) International Business Machines Corp., 2006 32d262c48SKyungmin Park * Copyright (c) Nokia Corporation, 2006, 2007 42d262c48SKyungmin Park * 52d262c48SKyungmin Park * This program is free software; you can redistribute it and/or modify 62d262c48SKyungmin Park * it under the terms of the GNU General Public License as published by 72d262c48SKyungmin Park * the Free Software Foundation; either version 2 of the License, or 82d262c48SKyungmin Park * (at your option) any later version. 92d262c48SKyungmin Park * 102d262c48SKyungmin Park * This program is distributed in the hope that it will be useful, 112d262c48SKyungmin Park * but WITHOUT ANY WARRANTY; without even the implied warranty of 122d262c48SKyungmin Park * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 132d262c48SKyungmin Park * the GNU General Public License for more details. 142d262c48SKyungmin Park * 152d262c48SKyungmin Park * You should have received a copy of the GNU General Public License 162d262c48SKyungmin Park * along with this program; if not, write to the Free Software 172d262c48SKyungmin Park * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 182d262c48SKyungmin Park * 192d262c48SKyungmin Park * Author: Artem Bityutskiy (Битюцкий Артём) 202d262c48SKyungmin Park */ 212d262c48SKyungmin Park 222d262c48SKyungmin Park /* 232d262c48SKyungmin Park * UBI input/output unit. 242d262c48SKyungmin Park * 252d262c48SKyungmin Park * This unit provides a uniform way to work with all kinds of the underlying 262d262c48SKyungmin Park * MTD devices. It also implements handy functions for reading and writing UBI 272d262c48SKyungmin Park * headers. 282d262c48SKyungmin Park * 292d262c48SKyungmin Park * We are trying to have a paranoid mindset and not to trust to what we read 302d262c48SKyungmin Park * from the flash media in order to be more secure and robust. So this unit 312d262c48SKyungmin Park * validates every single header it reads from the flash media. 322d262c48SKyungmin Park * 332d262c48SKyungmin Park * Some words about how the eraseblock headers are stored. 342d262c48SKyungmin Park * 352d262c48SKyungmin Park * The erase counter header is always stored at offset zero. By default, the 362d262c48SKyungmin Park * VID header is stored after the EC header at the closest aligned offset 372d262c48SKyungmin Park * (i.e. aligned to the minimum I/O unit size). Data starts next to the VID 382d262c48SKyungmin Park * header at the closest aligned offset. But this default layout may be 392d262c48SKyungmin Park * changed. For example, for different reasons (e.g., optimization) UBI may be 402d262c48SKyungmin Park * asked to put the VID header at further offset, and even at an unaligned 412d262c48SKyungmin Park * offset. Of course, if the offset of the VID header is unaligned, UBI adds 422d262c48SKyungmin Park * proper padding in front of it. Data offset may also be changed but it has to 432d262c48SKyungmin Park * be aligned. 442d262c48SKyungmin Park * 452d262c48SKyungmin Park * About minimal I/O units. In general, UBI assumes flash device model where 462d262c48SKyungmin Park * there is only one minimal I/O unit size. E.g., in case of NOR flash it is 1, 472d262c48SKyungmin Park * in case of NAND flash it is a NAND page, etc. This is reported by MTD in the 482d262c48SKyungmin Park * @ubi->mtd->writesize field. But as an exception, UBI admits of using another 492d262c48SKyungmin Park * (smaller) minimal I/O unit size for EC and VID headers to make it possible 502d262c48SKyungmin Park * to do different optimizations. 512d262c48SKyungmin Park * 522d262c48SKyungmin Park * This is extremely useful in case of NAND flashes which admit of several 532d262c48SKyungmin Park * write operations to one NAND page. In this case UBI can fit EC and VID 542d262c48SKyungmin Park * headers at one NAND page. Thus, UBI may use "sub-page" size as the minimal 552d262c48SKyungmin Park * I/O unit for the headers (the @ubi->hdrs_min_io_size field). But it still 562d262c48SKyungmin Park * reports NAND page size (@ubi->min_io_size) as a minimal I/O unit for the UBI 572d262c48SKyungmin Park * users. 582d262c48SKyungmin Park * 592d262c48SKyungmin Park * Example: some Samsung NANDs with 2KiB pages allow 4x 512-byte writes, so 602d262c48SKyungmin Park * although the minimal I/O unit is 2K, UBI uses 512 bytes for EC and VID 612d262c48SKyungmin Park * headers. 622d262c48SKyungmin Park * 632d262c48SKyungmin Park * Q: why not just to treat sub-page as a minimal I/O unit of this flash 642d262c48SKyungmin Park * device, e.g., make @ubi->min_io_size = 512 in the example above? 652d262c48SKyungmin Park * 662d262c48SKyungmin Park * A: because when writing a sub-page, MTD still writes a full 2K page but the 672d262c48SKyungmin Park * bytes which are no relevant to the sub-page are 0xFF. So, basically, writing 682d262c48SKyungmin Park * 4x512 sub-pages is 4 times slower then writing one 2KiB NAND page. Thus, we 692d262c48SKyungmin Park * prefer to use sub-pages only for EV and VID headers. 702d262c48SKyungmin Park * 712d262c48SKyungmin Park * As it was noted above, the VID header may start at a non-aligned offset. 722d262c48SKyungmin Park * For example, in case of a 2KiB page NAND flash with a 512 bytes sub-page, 732d262c48SKyungmin Park * the VID header may reside at offset 1984 which is the last 64 bytes of the 742d262c48SKyungmin Park * last sub-page (EC header is always at offset zero). This causes some 752d262c48SKyungmin Park * difficulties when reading and writing VID headers. 762d262c48SKyungmin Park * 772d262c48SKyungmin Park * Suppose we have a 64-byte buffer and we read a VID header at it. We change 782d262c48SKyungmin Park * the data and want to write this VID header out. As we can only write in 792d262c48SKyungmin Park * 512-byte chunks, we have to allocate one more buffer and copy our VID header 802d262c48SKyungmin Park * to offset 448 of this buffer. 812d262c48SKyungmin Park * 822d262c48SKyungmin Park * The I/O unit does the following trick in order to avoid this extra copy. 832d262c48SKyungmin Park * It always allocates a @ubi->vid_hdr_alsize bytes buffer for the VID header 842d262c48SKyungmin Park * and returns a pointer to offset @ubi->vid_hdr_shift of this buffer. When the 852d262c48SKyungmin Park * VID header is being written out, it shifts the VID header pointer back and 862d262c48SKyungmin Park * writes the whole sub-page. 872d262c48SKyungmin Park */ 882d262c48SKyungmin Park 892d262c48SKyungmin Park #ifdef UBI_LINUX 902d262c48SKyungmin Park #include <linux/crc32.h> 912d262c48SKyungmin Park #include <linux/err.h> 922d262c48SKyungmin Park #endif 932d262c48SKyungmin Park 942d262c48SKyungmin Park #include <ubi_uboot.h> 952d262c48SKyungmin Park #include "ubi.h" 962d262c48SKyungmin Park 972d262c48SKyungmin Park #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID 982d262c48SKyungmin Park static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum); 992d262c48SKyungmin Park static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum); 1002d262c48SKyungmin Park static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum, 1012d262c48SKyungmin Park const struct ubi_ec_hdr *ec_hdr); 1022d262c48SKyungmin Park static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum); 1032d262c48SKyungmin Park static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum, 1042d262c48SKyungmin Park const struct ubi_vid_hdr *vid_hdr); 1052d262c48SKyungmin Park static int paranoid_check_all_ff(struct ubi_device *ubi, int pnum, int offset, 1062d262c48SKyungmin Park int len); 1072d262c48SKyungmin Park #else 1082d262c48SKyungmin Park #define paranoid_check_not_bad(ubi, pnum) 0 1092d262c48SKyungmin Park #define paranoid_check_peb_ec_hdr(ubi, pnum) 0 1102d262c48SKyungmin Park #define paranoid_check_ec_hdr(ubi, pnum, ec_hdr) 0 1112d262c48SKyungmin Park #define paranoid_check_peb_vid_hdr(ubi, pnum) 0 1122d262c48SKyungmin Park #define paranoid_check_vid_hdr(ubi, pnum, vid_hdr) 0 1132d262c48SKyungmin Park #define paranoid_check_all_ff(ubi, pnum, offset, len) 0 1142d262c48SKyungmin Park #endif 1152d262c48SKyungmin Park 1162d262c48SKyungmin Park /** 1172d262c48SKyungmin Park * ubi_io_read - read data from a physical eraseblock. 1182d262c48SKyungmin Park * @ubi: UBI device description object 1192d262c48SKyungmin Park * @buf: buffer where to store the read data 1202d262c48SKyungmin Park * @pnum: physical eraseblock number to read from 1212d262c48SKyungmin Park * @offset: offset within the physical eraseblock from where to read 1222d262c48SKyungmin Park * @len: how many bytes to read 1232d262c48SKyungmin Park * 1242d262c48SKyungmin Park * This function reads data from offset @offset of physical eraseblock @pnum 1252d262c48SKyungmin Park * and stores the read data in the @buf buffer. The following return codes are 1262d262c48SKyungmin Park * possible: 1272d262c48SKyungmin Park * 1282d262c48SKyungmin Park * o %0 if all the requested data were successfully read; 1292d262c48SKyungmin Park * o %UBI_IO_BITFLIPS if all the requested data were successfully read, but 1302d262c48SKyungmin Park * correctable bit-flips were detected; this is harmless but may indicate 1312d262c48SKyungmin Park * that this eraseblock may become bad soon (but do not have to); 1322d262c48SKyungmin Park * o %-EBADMSG if the MTD subsystem reported about data integrity problems, for 1332d262c48SKyungmin Park * example it can be an ECC error in case of NAND; this most probably means 1342d262c48SKyungmin Park * that the data is corrupted; 1352d262c48SKyungmin Park * o %-EIO if some I/O error occurred; 1362d262c48SKyungmin Park * o other negative error codes in case of other errors. 1372d262c48SKyungmin Park */ 1382d262c48SKyungmin Park int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset, 1392d262c48SKyungmin Park int len) 1402d262c48SKyungmin Park { 1412d262c48SKyungmin Park int err, retries = 0; 1422d262c48SKyungmin Park size_t read; 1432d262c48SKyungmin Park loff_t addr; 1442d262c48SKyungmin Park 1452d262c48SKyungmin Park dbg_io("read %d bytes from PEB %d:%d", len, pnum, offset); 1462d262c48SKyungmin Park 1472d262c48SKyungmin Park ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 1482d262c48SKyungmin Park ubi_assert(offset >= 0 && offset + len <= ubi->peb_size); 1492d262c48SKyungmin Park ubi_assert(len > 0); 1502d262c48SKyungmin Park 1512d262c48SKyungmin Park err = paranoid_check_not_bad(ubi, pnum); 1522d262c48SKyungmin Park if (err) 1532d262c48SKyungmin Park return err > 0 ? -EINVAL : err; 1542d262c48SKyungmin Park 1552d262c48SKyungmin Park addr = (loff_t)pnum * ubi->peb_size + offset; 1562d262c48SKyungmin Park retry: 1572d262c48SKyungmin Park err = ubi->mtd->read(ubi->mtd, addr, len, &read, buf); 1582d262c48SKyungmin Park if (err) { 1592d262c48SKyungmin Park if (err == -EUCLEAN) { 1602d262c48SKyungmin Park /* 1612d262c48SKyungmin Park * -EUCLEAN is reported if there was a bit-flip which 1622d262c48SKyungmin Park * was corrected, so this is harmless. 1632d262c48SKyungmin Park */ 1642d262c48SKyungmin Park ubi_msg("fixable bit-flip detected at PEB %d", pnum); 1652d262c48SKyungmin Park ubi_assert(len == read); 1662d262c48SKyungmin Park return UBI_IO_BITFLIPS; 1672d262c48SKyungmin Park } 1682d262c48SKyungmin Park 1692d262c48SKyungmin Park if (read != len && retries++ < UBI_IO_RETRIES) { 1702d262c48SKyungmin Park dbg_io("error %d while reading %d bytes from PEB %d:%d, " 1712d262c48SKyungmin Park "read only %zd bytes, retry", 1722d262c48SKyungmin Park err, len, pnum, offset, read); 1732d262c48SKyungmin Park yield(); 1742d262c48SKyungmin Park goto retry; 1752d262c48SKyungmin Park } 1762d262c48SKyungmin Park 1772d262c48SKyungmin Park ubi_err("error %d while reading %d bytes from PEB %d:%d, " 1782d262c48SKyungmin Park "read %zd bytes", err, len, pnum, offset, read); 1792d262c48SKyungmin Park ubi_dbg_dump_stack(); 1802d262c48SKyungmin Park 1812d262c48SKyungmin Park /* 1822d262c48SKyungmin Park * The driver should never return -EBADMSG if it failed to read 1832d262c48SKyungmin Park * all the requested data. But some buggy drivers might do 1842d262c48SKyungmin Park * this, so we change it to -EIO. 1852d262c48SKyungmin Park */ 1862d262c48SKyungmin Park if (read != len && err == -EBADMSG) { 1872d262c48SKyungmin Park ubi_assert(0); 1882d262c48SKyungmin Park printk("%s[%d] not here\n", __func__, __LINE__); 189*455ae7e8SWolfgang Denk /* err = -EIO; */ 1902d262c48SKyungmin Park } 1912d262c48SKyungmin Park } else { 1922d262c48SKyungmin Park ubi_assert(len == read); 1932d262c48SKyungmin Park 1942d262c48SKyungmin Park if (ubi_dbg_is_bitflip()) { 1952d262c48SKyungmin Park dbg_msg("bit-flip (emulated)"); 1962d262c48SKyungmin Park err = UBI_IO_BITFLIPS; 1972d262c48SKyungmin Park } 1982d262c48SKyungmin Park } 1992d262c48SKyungmin Park 2002d262c48SKyungmin Park return err; 2012d262c48SKyungmin Park } 2022d262c48SKyungmin Park 2032d262c48SKyungmin Park /** 2042d262c48SKyungmin Park * ubi_io_write - write data to a physical eraseblock. 2052d262c48SKyungmin Park * @ubi: UBI device description object 2062d262c48SKyungmin Park * @buf: buffer with the data to write 2072d262c48SKyungmin Park * @pnum: physical eraseblock number to write to 2082d262c48SKyungmin Park * @offset: offset within the physical eraseblock where to write 2092d262c48SKyungmin Park * @len: how many bytes to write 2102d262c48SKyungmin Park * 2112d262c48SKyungmin Park * This function writes @len bytes of data from buffer @buf to offset @offset 2122d262c48SKyungmin Park * of physical eraseblock @pnum. If all the data were successfully written, 2132d262c48SKyungmin Park * zero is returned. If an error occurred, this function returns a negative 2142d262c48SKyungmin Park * error code. If %-EIO is returned, the physical eraseblock most probably went 2152d262c48SKyungmin Park * bad. 2162d262c48SKyungmin Park * 2172d262c48SKyungmin Park * Note, in case of an error, it is possible that something was still written 2182d262c48SKyungmin Park * to the flash media, but may be some garbage. 2192d262c48SKyungmin Park */ 2202d262c48SKyungmin Park int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset, 2212d262c48SKyungmin Park int len) 2222d262c48SKyungmin Park { 2232d262c48SKyungmin Park int err; 2242d262c48SKyungmin Park size_t written; 2252d262c48SKyungmin Park loff_t addr; 2262d262c48SKyungmin Park 2272d262c48SKyungmin Park dbg_io("write %d bytes to PEB %d:%d", len, pnum, offset); 2282d262c48SKyungmin Park 2292d262c48SKyungmin Park ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 2302d262c48SKyungmin Park ubi_assert(offset >= 0 && offset + len <= ubi->peb_size); 2312d262c48SKyungmin Park ubi_assert(offset % ubi->hdrs_min_io_size == 0); 2322d262c48SKyungmin Park ubi_assert(len > 0 && len % ubi->hdrs_min_io_size == 0); 2332d262c48SKyungmin Park 2342d262c48SKyungmin Park if (ubi->ro_mode) { 2352d262c48SKyungmin Park ubi_err("read-only mode"); 2362d262c48SKyungmin Park return -EROFS; 2372d262c48SKyungmin Park } 2382d262c48SKyungmin Park 2392d262c48SKyungmin Park /* The below has to be compiled out if paranoid checks are disabled */ 2402d262c48SKyungmin Park 2412d262c48SKyungmin Park err = paranoid_check_not_bad(ubi, pnum); 2422d262c48SKyungmin Park if (err) 2432d262c48SKyungmin Park return err > 0 ? -EINVAL : err; 2442d262c48SKyungmin Park 2452d262c48SKyungmin Park /* The area we are writing to has to contain all 0xFF bytes */ 2462d262c48SKyungmin Park err = paranoid_check_all_ff(ubi, pnum, offset, len); 2472d262c48SKyungmin Park if (err) 2482d262c48SKyungmin Park return err > 0 ? -EINVAL : err; 2492d262c48SKyungmin Park 2502d262c48SKyungmin Park if (offset >= ubi->leb_start) { 2512d262c48SKyungmin Park /* 2522d262c48SKyungmin Park * We write to the data area of the physical eraseblock. Make 2532d262c48SKyungmin Park * sure it has valid EC and VID headers. 2542d262c48SKyungmin Park */ 2552d262c48SKyungmin Park err = paranoid_check_peb_ec_hdr(ubi, pnum); 2562d262c48SKyungmin Park if (err) 2572d262c48SKyungmin Park return err > 0 ? -EINVAL : err; 2582d262c48SKyungmin Park err = paranoid_check_peb_vid_hdr(ubi, pnum); 2592d262c48SKyungmin Park if (err) 2602d262c48SKyungmin Park return err > 0 ? -EINVAL : err; 2612d262c48SKyungmin Park } 2622d262c48SKyungmin Park 2632d262c48SKyungmin Park if (ubi_dbg_is_write_failure()) { 2642d262c48SKyungmin Park dbg_err("cannot write %d bytes to PEB %d:%d " 2652d262c48SKyungmin Park "(emulated)", len, pnum, offset); 2662d262c48SKyungmin Park ubi_dbg_dump_stack(); 2672d262c48SKyungmin Park return -EIO; 2682d262c48SKyungmin Park } 2692d262c48SKyungmin Park 2702d262c48SKyungmin Park addr = (loff_t)pnum * ubi->peb_size + offset; 2712d262c48SKyungmin Park err = ubi->mtd->write(ubi->mtd, addr, len, &written, buf); 2722d262c48SKyungmin Park if (err) { 2732d262c48SKyungmin Park ubi_err("error %d while writing %d bytes to PEB %d:%d, written" 2742d262c48SKyungmin Park " %zd bytes", err, len, pnum, offset, written); 2752d262c48SKyungmin Park ubi_dbg_dump_stack(); 2762d262c48SKyungmin Park } else 2772d262c48SKyungmin Park ubi_assert(written == len); 2782d262c48SKyungmin Park 2792d262c48SKyungmin Park return err; 2802d262c48SKyungmin Park } 2812d262c48SKyungmin Park 2822d262c48SKyungmin Park /** 2832d262c48SKyungmin Park * erase_callback - MTD erasure call-back. 2842d262c48SKyungmin Park * @ei: MTD erase information object. 2852d262c48SKyungmin Park * 2862d262c48SKyungmin Park * Note, even though MTD erase interface is asynchronous, all the current 2872d262c48SKyungmin Park * implementations are synchronous anyway. 2882d262c48SKyungmin Park */ 2892d262c48SKyungmin Park static void erase_callback(struct erase_info *ei) 2902d262c48SKyungmin Park { 2912d262c48SKyungmin Park wake_up_interruptible((wait_queue_head_t *)ei->priv); 2922d262c48SKyungmin Park } 2932d262c48SKyungmin Park 2942d262c48SKyungmin Park /** 2952d262c48SKyungmin Park * do_sync_erase - synchronously erase a physical eraseblock. 2962d262c48SKyungmin Park * @ubi: UBI device description object 2972d262c48SKyungmin Park * @pnum: the physical eraseblock number to erase 2982d262c48SKyungmin Park * 2992d262c48SKyungmin Park * This function synchronously erases physical eraseblock @pnum and returns 3002d262c48SKyungmin Park * zero in case of success and a negative error code in case of failure. If 3012d262c48SKyungmin Park * %-EIO is returned, the physical eraseblock most probably went bad. 3022d262c48SKyungmin Park */ 3032d262c48SKyungmin Park static int do_sync_erase(struct ubi_device *ubi, int pnum) 3042d262c48SKyungmin Park { 3052d262c48SKyungmin Park int err, retries = 0; 3062d262c48SKyungmin Park struct erase_info ei; 3072d262c48SKyungmin Park wait_queue_head_t wq; 3082d262c48SKyungmin Park 3092d262c48SKyungmin Park dbg_io("erase PEB %d", pnum); 3102d262c48SKyungmin Park 3112d262c48SKyungmin Park retry: 3122d262c48SKyungmin Park init_waitqueue_head(&wq); 3132d262c48SKyungmin Park memset(&ei, 0, sizeof(struct erase_info)); 3142d262c48SKyungmin Park 3152d262c48SKyungmin Park ei.mtd = ubi->mtd; 3162d262c48SKyungmin Park ei.addr = (loff_t)pnum * ubi->peb_size; 3172d262c48SKyungmin Park ei.len = ubi->peb_size; 3182d262c48SKyungmin Park ei.callback = erase_callback; 3192d262c48SKyungmin Park ei.priv = (unsigned long)&wq; 3202d262c48SKyungmin Park 3212d262c48SKyungmin Park err = ubi->mtd->erase(ubi->mtd, &ei); 3222d262c48SKyungmin Park if (err) { 3232d262c48SKyungmin Park if (retries++ < UBI_IO_RETRIES) { 3242d262c48SKyungmin Park dbg_io("error %d while erasing PEB %d, retry", 3252d262c48SKyungmin Park err, pnum); 3262d262c48SKyungmin Park yield(); 3272d262c48SKyungmin Park goto retry; 3282d262c48SKyungmin Park } 3292d262c48SKyungmin Park ubi_err("cannot erase PEB %d, error %d", pnum, err); 3302d262c48SKyungmin Park ubi_dbg_dump_stack(); 3312d262c48SKyungmin Park return err; 3322d262c48SKyungmin Park } 3332d262c48SKyungmin Park 3342d262c48SKyungmin Park err = wait_event_interruptible(wq, ei.state == MTD_ERASE_DONE || 3352d262c48SKyungmin Park ei.state == MTD_ERASE_FAILED); 3362d262c48SKyungmin Park if (err) { 3372d262c48SKyungmin Park ubi_err("interrupted PEB %d erasure", pnum); 3382d262c48SKyungmin Park return -EINTR; 3392d262c48SKyungmin Park } 3402d262c48SKyungmin Park 3412d262c48SKyungmin Park if (ei.state == MTD_ERASE_FAILED) { 3422d262c48SKyungmin Park if (retries++ < UBI_IO_RETRIES) { 3432d262c48SKyungmin Park dbg_io("error while erasing PEB %d, retry", pnum); 3442d262c48SKyungmin Park yield(); 3452d262c48SKyungmin Park goto retry; 3462d262c48SKyungmin Park } 3472d262c48SKyungmin Park ubi_err("cannot erase PEB %d", pnum); 3482d262c48SKyungmin Park ubi_dbg_dump_stack(); 3492d262c48SKyungmin Park return -EIO; 3502d262c48SKyungmin Park } 3512d262c48SKyungmin Park 3522d262c48SKyungmin Park err = paranoid_check_all_ff(ubi, pnum, 0, ubi->peb_size); 3532d262c48SKyungmin Park if (err) 3542d262c48SKyungmin Park return err > 0 ? -EINVAL : err; 3552d262c48SKyungmin Park 3562d262c48SKyungmin Park if (ubi_dbg_is_erase_failure() && !err) { 3572d262c48SKyungmin Park dbg_err("cannot erase PEB %d (emulated)", pnum); 3582d262c48SKyungmin Park return -EIO; 3592d262c48SKyungmin Park } 3602d262c48SKyungmin Park 3612d262c48SKyungmin Park return 0; 3622d262c48SKyungmin Park } 3632d262c48SKyungmin Park 3642d262c48SKyungmin Park /** 3652d262c48SKyungmin Park * check_pattern - check if buffer contains only a certain byte pattern. 3662d262c48SKyungmin Park * @buf: buffer to check 3672d262c48SKyungmin Park * @patt: the pattern to check 3682d262c48SKyungmin Park * @size: buffer size in bytes 3692d262c48SKyungmin Park * 3702d262c48SKyungmin Park * This function returns %1 in there are only @patt bytes in @buf, and %0 if 3712d262c48SKyungmin Park * something else was also found. 3722d262c48SKyungmin Park */ 3732d262c48SKyungmin Park static int check_pattern(const void *buf, uint8_t patt, int size) 3742d262c48SKyungmin Park { 3752d262c48SKyungmin Park int i; 3762d262c48SKyungmin Park 3772d262c48SKyungmin Park for (i = 0; i < size; i++) 3782d262c48SKyungmin Park if (((const uint8_t *)buf)[i] != patt) 3792d262c48SKyungmin Park return 0; 3802d262c48SKyungmin Park return 1; 3812d262c48SKyungmin Park } 3822d262c48SKyungmin Park 3832d262c48SKyungmin Park /* Patterns to write to a physical eraseblock when torturing it */ 3842d262c48SKyungmin Park static uint8_t patterns[] = {0xa5, 0x5a, 0x0}; 3852d262c48SKyungmin Park 3862d262c48SKyungmin Park /** 3872d262c48SKyungmin Park * torture_peb - test a supposedly bad physical eraseblock. 3882d262c48SKyungmin Park * @ubi: UBI device description object 3892d262c48SKyungmin Park * @pnum: the physical eraseblock number to test 3902d262c48SKyungmin Park * 3912d262c48SKyungmin Park * This function returns %-EIO if the physical eraseblock did not pass the 3922d262c48SKyungmin Park * test, a positive number of erase operations done if the test was 3932d262c48SKyungmin Park * successfully passed, and other negative error codes in case of other errors. 3942d262c48SKyungmin Park */ 3952d262c48SKyungmin Park static int torture_peb(struct ubi_device *ubi, int pnum) 3962d262c48SKyungmin Park { 3972d262c48SKyungmin Park int err, i, patt_count; 3982d262c48SKyungmin Park 3992d262c48SKyungmin Park patt_count = ARRAY_SIZE(patterns); 4002d262c48SKyungmin Park ubi_assert(patt_count > 0); 4012d262c48SKyungmin Park 4022d262c48SKyungmin Park mutex_lock(&ubi->buf_mutex); 4032d262c48SKyungmin Park for (i = 0; i < patt_count; i++) { 4042d262c48SKyungmin Park err = do_sync_erase(ubi, pnum); 4052d262c48SKyungmin Park if (err) 4062d262c48SKyungmin Park goto out; 4072d262c48SKyungmin Park 4082d262c48SKyungmin Park /* Make sure the PEB contains only 0xFF bytes */ 4092d262c48SKyungmin Park err = ubi_io_read(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size); 4102d262c48SKyungmin Park if (err) 4112d262c48SKyungmin Park goto out; 4122d262c48SKyungmin Park 4132d262c48SKyungmin Park err = check_pattern(ubi->peb_buf1, 0xFF, ubi->peb_size); 4142d262c48SKyungmin Park if (err == 0) { 4152d262c48SKyungmin Park ubi_err("erased PEB %d, but a non-0xFF byte found", 4162d262c48SKyungmin Park pnum); 4172d262c48SKyungmin Park err = -EIO; 4182d262c48SKyungmin Park goto out; 4192d262c48SKyungmin Park } 4202d262c48SKyungmin Park 4212d262c48SKyungmin Park /* Write a pattern and check it */ 4222d262c48SKyungmin Park memset(ubi->peb_buf1, patterns[i], ubi->peb_size); 4232d262c48SKyungmin Park err = ubi_io_write(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size); 4242d262c48SKyungmin Park if (err) 4252d262c48SKyungmin Park goto out; 4262d262c48SKyungmin Park 4272d262c48SKyungmin Park memset(ubi->peb_buf1, ~patterns[i], ubi->peb_size); 4282d262c48SKyungmin Park err = ubi_io_read(ubi, ubi->peb_buf1, pnum, 0, ubi->peb_size); 4292d262c48SKyungmin Park if (err) 4302d262c48SKyungmin Park goto out; 4312d262c48SKyungmin Park 4322d262c48SKyungmin Park err = check_pattern(ubi->peb_buf1, patterns[i], ubi->peb_size); 4332d262c48SKyungmin Park if (err == 0) { 4342d262c48SKyungmin Park ubi_err("pattern %x checking failed for PEB %d", 4352d262c48SKyungmin Park patterns[i], pnum); 4362d262c48SKyungmin Park err = -EIO; 4372d262c48SKyungmin Park goto out; 4382d262c48SKyungmin Park } 4392d262c48SKyungmin Park } 4402d262c48SKyungmin Park 4412d262c48SKyungmin Park err = patt_count; 4422d262c48SKyungmin Park 4432d262c48SKyungmin Park out: 4442d262c48SKyungmin Park mutex_unlock(&ubi->buf_mutex); 4452d262c48SKyungmin Park if (err == UBI_IO_BITFLIPS || err == -EBADMSG) { 4462d262c48SKyungmin Park /* 4472d262c48SKyungmin Park * If a bit-flip or data integrity error was detected, the test 4482d262c48SKyungmin Park * has not passed because it happened on a freshly erased 4492d262c48SKyungmin Park * physical eraseblock which means something is wrong with it. 4502d262c48SKyungmin Park */ 4512d262c48SKyungmin Park ubi_err("read problems on freshly erased PEB %d, must be bad", 4522d262c48SKyungmin Park pnum); 4532d262c48SKyungmin Park err = -EIO; 4542d262c48SKyungmin Park } 4552d262c48SKyungmin Park return err; 4562d262c48SKyungmin Park } 4572d262c48SKyungmin Park 4582d262c48SKyungmin Park /** 4592d262c48SKyungmin Park * ubi_io_sync_erase - synchronously erase a physical eraseblock. 4602d262c48SKyungmin Park * @ubi: UBI device description object 4612d262c48SKyungmin Park * @pnum: physical eraseblock number to erase 4622d262c48SKyungmin Park * @torture: if this physical eraseblock has to be tortured 4632d262c48SKyungmin Park * 4642d262c48SKyungmin Park * This function synchronously erases physical eraseblock @pnum. If @torture 4652d262c48SKyungmin Park * flag is not zero, the physical eraseblock is checked by means of writing 4662d262c48SKyungmin Park * different patterns to it and reading them back. If the torturing is enabled, 4672d262c48SKyungmin Park * the physical eraseblock is erased more then once. 4682d262c48SKyungmin Park * 4692d262c48SKyungmin Park * This function returns the number of erasures made in case of success, %-EIO 4702d262c48SKyungmin Park * if the erasure failed or the torturing test failed, and other negative error 4712d262c48SKyungmin Park * codes in case of other errors. Note, %-EIO means that the physical 4722d262c48SKyungmin Park * eraseblock is bad. 4732d262c48SKyungmin Park */ 4742d262c48SKyungmin Park int ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture) 4752d262c48SKyungmin Park { 4762d262c48SKyungmin Park int err, ret = 0; 4772d262c48SKyungmin Park 4782d262c48SKyungmin Park ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 4792d262c48SKyungmin Park 4802d262c48SKyungmin Park err = paranoid_check_not_bad(ubi, pnum); 4812d262c48SKyungmin Park if (err != 0) 4822d262c48SKyungmin Park return err > 0 ? -EINVAL : err; 4832d262c48SKyungmin Park 4842d262c48SKyungmin Park if (ubi->ro_mode) { 4852d262c48SKyungmin Park ubi_err("read-only mode"); 4862d262c48SKyungmin Park return -EROFS; 4872d262c48SKyungmin Park } 4882d262c48SKyungmin Park 4892d262c48SKyungmin Park if (torture) { 4902d262c48SKyungmin Park ret = torture_peb(ubi, pnum); 4912d262c48SKyungmin Park if (ret < 0) 4922d262c48SKyungmin Park return ret; 4932d262c48SKyungmin Park } 4942d262c48SKyungmin Park 4952d262c48SKyungmin Park err = do_sync_erase(ubi, pnum); 4962d262c48SKyungmin Park if (err) 4972d262c48SKyungmin Park return err; 4982d262c48SKyungmin Park 4992d262c48SKyungmin Park return ret + 1; 5002d262c48SKyungmin Park } 5012d262c48SKyungmin Park 5022d262c48SKyungmin Park /** 5032d262c48SKyungmin Park * ubi_io_is_bad - check if a physical eraseblock is bad. 5042d262c48SKyungmin Park * @ubi: UBI device description object 5052d262c48SKyungmin Park * @pnum: the physical eraseblock number to check 5062d262c48SKyungmin Park * 5072d262c48SKyungmin Park * This function returns a positive number if the physical eraseblock is bad, 5082d262c48SKyungmin Park * zero if not, and a negative error code if an error occurred. 5092d262c48SKyungmin Park */ 5102d262c48SKyungmin Park int ubi_io_is_bad(const struct ubi_device *ubi, int pnum) 5112d262c48SKyungmin Park { 5122d262c48SKyungmin Park struct mtd_info *mtd = ubi->mtd; 5132d262c48SKyungmin Park 5142d262c48SKyungmin Park ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 5152d262c48SKyungmin Park 5162d262c48SKyungmin Park if (ubi->bad_allowed) { 5172d262c48SKyungmin Park int ret; 5182d262c48SKyungmin Park 5192d262c48SKyungmin Park ret = mtd->block_isbad(mtd, (loff_t)pnum * ubi->peb_size); 5202d262c48SKyungmin Park if (ret < 0) 5212d262c48SKyungmin Park ubi_err("error %d while checking if PEB %d is bad", 5222d262c48SKyungmin Park ret, pnum); 5232d262c48SKyungmin Park else if (ret) 5242d262c48SKyungmin Park dbg_io("PEB %d is bad", pnum); 5252d262c48SKyungmin Park return ret; 5262d262c48SKyungmin Park } 5272d262c48SKyungmin Park 5282d262c48SKyungmin Park return 0; 5292d262c48SKyungmin Park } 5302d262c48SKyungmin Park 5312d262c48SKyungmin Park /** 5322d262c48SKyungmin Park * ubi_io_mark_bad - mark a physical eraseblock as bad. 5332d262c48SKyungmin Park * @ubi: UBI device description object 5342d262c48SKyungmin Park * @pnum: the physical eraseblock number to mark 5352d262c48SKyungmin Park * 5362d262c48SKyungmin Park * This function returns zero in case of success and a negative error code in 5372d262c48SKyungmin Park * case of failure. 5382d262c48SKyungmin Park */ 5392d262c48SKyungmin Park int ubi_io_mark_bad(const struct ubi_device *ubi, int pnum) 5402d262c48SKyungmin Park { 5412d262c48SKyungmin Park int err; 5422d262c48SKyungmin Park struct mtd_info *mtd = ubi->mtd; 5432d262c48SKyungmin Park 5442d262c48SKyungmin Park ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 5452d262c48SKyungmin Park 5462d262c48SKyungmin Park if (ubi->ro_mode) { 5472d262c48SKyungmin Park ubi_err("read-only mode"); 5482d262c48SKyungmin Park return -EROFS; 5492d262c48SKyungmin Park } 5502d262c48SKyungmin Park 5512d262c48SKyungmin Park if (!ubi->bad_allowed) 5522d262c48SKyungmin Park return 0; 5532d262c48SKyungmin Park 5542d262c48SKyungmin Park err = mtd->block_markbad(mtd, (loff_t)pnum * ubi->peb_size); 5552d262c48SKyungmin Park if (err) 5562d262c48SKyungmin Park ubi_err("cannot mark PEB %d bad, error %d", pnum, err); 5572d262c48SKyungmin Park return err; 5582d262c48SKyungmin Park } 5592d262c48SKyungmin Park 5602d262c48SKyungmin Park /** 5612d262c48SKyungmin Park * validate_ec_hdr - validate an erase counter header. 5622d262c48SKyungmin Park * @ubi: UBI device description object 5632d262c48SKyungmin Park * @ec_hdr: the erase counter header to check 5642d262c48SKyungmin Park * 5652d262c48SKyungmin Park * This function returns zero if the erase counter header is OK, and %1 if 5662d262c48SKyungmin Park * not. 5672d262c48SKyungmin Park */ 5682d262c48SKyungmin Park static int validate_ec_hdr(const struct ubi_device *ubi, 5692d262c48SKyungmin Park const struct ubi_ec_hdr *ec_hdr) 5702d262c48SKyungmin Park { 5712d262c48SKyungmin Park long long ec; 5722d262c48SKyungmin Park int vid_hdr_offset, leb_start; 5732d262c48SKyungmin Park 5742d262c48SKyungmin Park ec = be64_to_cpu(ec_hdr->ec); 5752d262c48SKyungmin Park vid_hdr_offset = be32_to_cpu(ec_hdr->vid_hdr_offset); 5762d262c48SKyungmin Park leb_start = be32_to_cpu(ec_hdr->data_offset); 5772d262c48SKyungmin Park 5782d262c48SKyungmin Park if (ec_hdr->version != UBI_VERSION) { 5792d262c48SKyungmin Park ubi_err("node with incompatible UBI version found: " 5802d262c48SKyungmin Park "this UBI version is %d, image version is %d", 5812d262c48SKyungmin Park UBI_VERSION, (int)ec_hdr->version); 5822d262c48SKyungmin Park goto bad; 5832d262c48SKyungmin Park } 5842d262c48SKyungmin Park 5852d262c48SKyungmin Park if (vid_hdr_offset != ubi->vid_hdr_offset) { 5862d262c48SKyungmin Park ubi_err("bad VID header offset %d, expected %d", 5872d262c48SKyungmin Park vid_hdr_offset, ubi->vid_hdr_offset); 5882d262c48SKyungmin Park goto bad; 5892d262c48SKyungmin Park } 5902d262c48SKyungmin Park 5912d262c48SKyungmin Park if (leb_start != ubi->leb_start) { 5922d262c48SKyungmin Park ubi_err("bad data offset %d, expected %d", 5932d262c48SKyungmin Park leb_start, ubi->leb_start); 5942d262c48SKyungmin Park goto bad; 5952d262c48SKyungmin Park } 5962d262c48SKyungmin Park 5972d262c48SKyungmin Park if (ec < 0 || ec > UBI_MAX_ERASECOUNTER) { 5982d262c48SKyungmin Park ubi_err("bad erase counter %lld", ec); 5992d262c48SKyungmin Park goto bad; 6002d262c48SKyungmin Park } 6012d262c48SKyungmin Park 6022d262c48SKyungmin Park return 0; 6032d262c48SKyungmin Park 6042d262c48SKyungmin Park bad: 6052d262c48SKyungmin Park ubi_err("bad EC header"); 6062d262c48SKyungmin Park ubi_dbg_dump_ec_hdr(ec_hdr); 6072d262c48SKyungmin Park ubi_dbg_dump_stack(); 6082d262c48SKyungmin Park return 1; 6092d262c48SKyungmin Park } 6102d262c48SKyungmin Park 6112d262c48SKyungmin Park /** 6122d262c48SKyungmin Park * ubi_io_read_ec_hdr - read and check an erase counter header. 6132d262c48SKyungmin Park * @ubi: UBI device description object 6142d262c48SKyungmin Park * @pnum: physical eraseblock to read from 6152d262c48SKyungmin Park * @ec_hdr: a &struct ubi_ec_hdr object where to store the read erase counter 6162d262c48SKyungmin Park * header 6172d262c48SKyungmin Park * @verbose: be verbose if the header is corrupted or was not found 6182d262c48SKyungmin Park * 6192d262c48SKyungmin Park * This function reads erase counter header from physical eraseblock @pnum and 6202d262c48SKyungmin Park * stores it in @ec_hdr. This function also checks CRC checksum of the read 6212d262c48SKyungmin Park * erase counter header. The following codes may be returned: 6222d262c48SKyungmin Park * 6232d262c48SKyungmin Park * o %0 if the CRC checksum is correct and the header was successfully read; 6242d262c48SKyungmin Park * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected 6252d262c48SKyungmin Park * and corrected by the flash driver; this is harmless but may indicate that 6262d262c48SKyungmin Park * this eraseblock may become bad soon (but may be not); 6272d262c48SKyungmin Park * o %UBI_IO_BAD_EC_HDR if the erase counter header is corrupted (a CRC error); 6282d262c48SKyungmin Park * o %UBI_IO_PEB_EMPTY if the physical eraseblock is empty; 6292d262c48SKyungmin Park * o a negative error code in case of failure. 6302d262c48SKyungmin Park */ 6312d262c48SKyungmin Park int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum, 6322d262c48SKyungmin Park struct ubi_ec_hdr *ec_hdr, int verbose) 6332d262c48SKyungmin Park { 6342d262c48SKyungmin Park int err, read_err = 0; 6352d262c48SKyungmin Park uint32_t crc, magic, hdr_crc; 6362d262c48SKyungmin Park 6372d262c48SKyungmin Park dbg_io("read EC header from PEB %d", pnum); 6382d262c48SKyungmin Park ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 6392d262c48SKyungmin Park if (UBI_IO_DEBUG) 6402d262c48SKyungmin Park verbose = 1; 6412d262c48SKyungmin Park 6422d262c48SKyungmin Park err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE); 6432d262c48SKyungmin Park if (err) { 6442d262c48SKyungmin Park if (err != UBI_IO_BITFLIPS && err != -EBADMSG) 6452d262c48SKyungmin Park return err; 6462d262c48SKyungmin Park 6472d262c48SKyungmin Park /* 6482d262c48SKyungmin Park * We read all the data, but either a correctable bit-flip 6492d262c48SKyungmin Park * occurred, or MTD reported about some data integrity error, 6502d262c48SKyungmin Park * like an ECC error in case of NAND. The former is harmless, 6512d262c48SKyungmin Park * the later may mean that the read data is corrupted. But we 6522d262c48SKyungmin Park * have a CRC check-sum and we will detect this. If the EC 6532d262c48SKyungmin Park * header is still OK, we just report this as there was a 6542d262c48SKyungmin Park * bit-flip. 6552d262c48SKyungmin Park */ 6562d262c48SKyungmin Park read_err = err; 6572d262c48SKyungmin Park } 6582d262c48SKyungmin Park 6592d262c48SKyungmin Park magic = be32_to_cpu(ec_hdr->magic); 6602d262c48SKyungmin Park if (magic != UBI_EC_HDR_MAGIC) { 6612d262c48SKyungmin Park /* 6622d262c48SKyungmin Park * The magic field is wrong. Let's check if we have read all 6632d262c48SKyungmin Park * 0xFF. If yes, this physical eraseblock is assumed to be 6642d262c48SKyungmin Park * empty. 6652d262c48SKyungmin Park * 6662d262c48SKyungmin Park * But if there was a read error, we do not test it for all 6672d262c48SKyungmin Park * 0xFFs. Even if it does contain all 0xFFs, this error 6682d262c48SKyungmin Park * indicates that something is still wrong with this physical 6692d262c48SKyungmin Park * eraseblock and we anyway cannot treat it as empty. 6702d262c48SKyungmin Park */ 6712d262c48SKyungmin Park if (read_err != -EBADMSG && 6722d262c48SKyungmin Park check_pattern(ec_hdr, 0xFF, UBI_EC_HDR_SIZE)) { 6732d262c48SKyungmin Park /* The physical eraseblock is supposedly empty */ 6742d262c48SKyungmin Park 6752d262c48SKyungmin Park /* 6762d262c48SKyungmin Park * The below is just a paranoid check, it has to be 6772d262c48SKyungmin Park * compiled out if paranoid checks are disabled. 6782d262c48SKyungmin Park */ 6792d262c48SKyungmin Park err = paranoid_check_all_ff(ubi, pnum, 0, 6802d262c48SKyungmin Park ubi->peb_size); 6812d262c48SKyungmin Park if (err) 6822d262c48SKyungmin Park return err > 0 ? UBI_IO_BAD_EC_HDR : err; 6832d262c48SKyungmin Park 6842d262c48SKyungmin Park if (verbose) 6852d262c48SKyungmin Park ubi_warn("no EC header found at PEB %d, " 6862d262c48SKyungmin Park "only 0xFF bytes", pnum); 6872d262c48SKyungmin Park return UBI_IO_PEB_EMPTY; 6882d262c48SKyungmin Park } 6892d262c48SKyungmin Park 6902d262c48SKyungmin Park /* 6912d262c48SKyungmin Park * This is not a valid erase counter header, and these are not 6922d262c48SKyungmin Park * 0xFF bytes. Report that the header is corrupted. 6932d262c48SKyungmin Park */ 6942d262c48SKyungmin Park if (verbose) { 6952d262c48SKyungmin Park ubi_warn("bad magic number at PEB %d: %08x instead of " 6962d262c48SKyungmin Park "%08x", pnum, magic, UBI_EC_HDR_MAGIC); 6972d262c48SKyungmin Park ubi_dbg_dump_ec_hdr(ec_hdr); 6982d262c48SKyungmin Park } 6992d262c48SKyungmin Park return UBI_IO_BAD_EC_HDR; 7002d262c48SKyungmin Park } 7012d262c48SKyungmin Park 7022d262c48SKyungmin Park crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC); 7032d262c48SKyungmin Park hdr_crc = be32_to_cpu(ec_hdr->hdr_crc); 7042d262c48SKyungmin Park 7052d262c48SKyungmin Park if (hdr_crc != crc) { 7062d262c48SKyungmin Park if (verbose) { 7072d262c48SKyungmin Park ubi_warn("bad EC header CRC at PEB %d, calculated %#08x," 7082d262c48SKyungmin Park " read %#08x", pnum, crc, hdr_crc); 7092d262c48SKyungmin Park ubi_dbg_dump_ec_hdr(ec_hdr); 7102d262c48SKyungmin Park } 7112d262c48SKyungmin Park return UBI_IO_BAD_EC_HDR; 7122d262c48SKyungmin Park } 7132d262c48SKyungmin Park 7142d262c48SKyungmin Park /* And of course validate what has just been read from the media */ 7152d262c48SKyungmin Park err = validate_ec_hdr(ubi, ec_hdr); 7162d262c48SKyungmin Park if (err) { 7172d262c48SKyungmin Park ubi_err("validation failed for PEB %d", pnum); 7182d262c48SKyungmin Park return -EINVAL; 7192d262c48SKyungmin Park } 7202d262c48SKyungmin Park 7212d262c48SKyungmin Park return read_err ? UBI_IO_BITFLIPS : 0; 7222d262c48SKyungmin Park } 7232d262c48SKyungmin Park 7242d262c48SKyungmin Park /** 7252d262c48SKyungmin Park * ubi_io_write_ec_hdr - write an erase counter header. 7262d262c48SKyungmin Park * @ubi: UBI device description object 7272d262c48SKyungmin Park * @pnum: physical eraseblock to write to 7282d262c48SKyungmin Park * @ec_hdr: the erase counter header to write 7292d262c48SKyungmin Park * 7302d262c48SKyungmin Park * This function writes erase counter header described by @ec_hdr to physical 7312d262c48SKyungmin Park * eraseblock @pnum. It also fills most fields of @ec_hdr before writing, so 7322d262c48SKyungmin Park * the caller do not have to fill them. Callers must only fill the @ec_hdr->ec 7332d262c48SKyungmin Park * field. 7342d262c48SKyungmin Park * 7352d262c48SKyungmin Park * This function returns zero in case of success and a negative error code in 7362d262c48SKyungmin Park * case of failure. If %-EIO is returned, the physical eraseblock most probably 7372d262c48SKyungmin Park * went bad. 7382d262c48SKyungmin Park */ 7392d262c48SKyungmin Park int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum, 7402d262c48SKyungmin Park struct ubi_ec_hdr *ec_hdr) 7412d262c48SKyungmin Park { 7422d262c48SKyungmin Park int err; 7432d262c48SKyungmin Park uint32_t crc; 7442d262c48SKyungmin Park 7452d262c48SKyungmin Park dbg_io("write EC header to PEB %d", pnum); 7462d262c48SKyungmin Park ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 7472d262c48SKyungmin Park 7482d262c48SKyungmin Park ec_hdr->magic = cpu_to_be32(UBI_EC_HDR_MAGIC); 7492d262c48SKyungmin Park ec_hdr->version = UBI_VERSION; 7502d262c48SKyungmin Park ec_hdr->vid_hdr_offset = cpu_to_be32(ubi->vid_hdr_offset); 7512d262c48SKyungmin Park ec_hdr->data_offset = cpu_to_be32(ubi->leb_start); 7522d262c48SKyungmin Park crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC); 7532d262c48SKyungmin Park ec_hdr->hdr_crc = cpu_to_be32(crc); 7542d262c48SKyungmin Park 7552d262c48SKyungmin Park err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr); 7562d262c48SKyungmin Park if (err) 7572d262c48SKyungmin Park return -EINVAL; 7582d262c48SKyungmin Park 7592d262c48SKyungmin Park err = ubi_io_write(ubi, ec_hdr, pnum, 0, ubi->ec_hdr_alsize); 7602d262c48SKyungmin Park return err; 7612d262c48SKyungmin Park } 7622d262c48SKyungmin Park 7632d262c48SKyungmin Park /** 7642d262c48SKyungmin Park * validate_vid_hdr - validate a volume identifier header. 7652d262c48SKyungmin Park * @ubi: UBI device description object 7662d262c48SKyungmin Park * @vid_hdr: the volume identifier header to check 7672d262c48SKyungmin Park * 7682d262c48SKyungmin Park * This function checks that data stored in the volume identifier header 7692d262c48SKyungmin Park * @vid_hdr. Returns zero if the VID header is OK and %1 if not. 7702d262c48SKyungmin Park */ 7712d262c48SKyungmin Park static int validate_vid_hdr(const struct ubi_device *ubi, 7722d262c48SKyungmin Park const struct ubi_vid_hdr *vid_hdr) 7732d262c48SKyungmin Park { 7742d262c48SKyungmin Park int vol_type = vid_hdr->vol_type; 7752d262c48SKyungmin Park int copy_flag = vid_hdr->copy_flag; 7762d262c48SKyungmin Park int vol_id = be32_to_cpu(vid_hdr->vol_id); 7772d262c48SKyungmin Park int lnum = be32_to_cpu(vid_hdr->lnum); 7782d262c48SKyungmin Park int compat = vid_hdr->compat; 7792d262c48SKyungmin Park int data_size = be32_to_cpu(vid_hdr->data_size); 7802d262c48SKyungmin Park int used_ebs = be32_to_cpu(vid_hdr->used_ebs); 7812d262c48SKyungmin Park int data_pad = be32_to_cpu(vid_hdr->data_pad); 7822d262c48SKyungmin Park int data_crc = be32_to_cpu(vid_hdr->data_crc); 7832d262c48SKyungmin Park int usable_leb_size = ubi->leb_size - data_pad; 7842d262c48SKyungmin Park 7852d262c48SKyungmin Park if (copy_flag != 0 && copy_flag != 1) { 7862d262c48SKyungmin Park dbg_err("bad copy_flag"); 7872d262c48SKyungmin Park goto bad; 7882d262c48SKyungmin Park } 7892d262c48SKyungmin Park 7902d262c48SKyungmin Park if (vol_id < 0 || lnum < 0 || data_size < 0 || used_ebs < 0 || 7912d262c48SKyungmin Park data_pad < 0) { 7922d262c48SKyungmin Park dbg_err("negative values"); 7932d262c48SKyungmin Park goto bad; 7942d262c48SKyungmin Park } 7952d262c48SKyungmin Park 7962d262c48SKyungmin Park if (vol_id >= UBI_MAX_VOLUMES && vol_id < UBI_INTERNAL_VOL_START) { 7972d262c48SKyungmin Park dbg_err("bad vol_id"); 7982d262c48SKyungmin Park goto bad; 7992d262c48SKyungmin Park } 8002d262c48SKyungmin Park 8012d262c48SKyungmin Park if (vol_id < UBI_INTERNAL_VOL_START && compat != 0) { 8022d262c48SKyungmin Park dbg_err("bad compat"); 8032d262c48SKyungmin Park goto bad; 8042d262c48SKyungmin Park } 8052d262c48SKyungmin Park 8062d262c48SKyungmin Park if (vol_id >= UBI_INTERNAL_VOL_START && compat != UBI_COMPAT_DELETE && 8072d262c48SKyungmin Park compat != UBI_COMPAT_RO && compat != UBI_COMPAT_PRESERVE && 8082d262c48SKyungmin Park compat != UBI_COMPAT_REJECT) { 8092d262c48SKyungmin Park dbg_err("bad compat"); 8102d262c48SKyungmin Park goto bad; 8112d262c48SKyungmin Park } 8122d262c48SKyungmin Park 8132d262c48SKyungmin Park if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) { 8142d262c48SKyungmin Park dbg_err("bad vol_type"); 8152d262c48SKyungmin Park goto bad; 8162d262c48SKyungmin Park } 8172d262c48SKyungmin Park 8182d262c48SKyungmin Park if (data_pad >= ubi->leb_size / 2) { 8192d262c48SKyungmin Park dbg_err("bad data_pad"); 8202d262c48SKyungmin Park goto bad; 8212d262c48SKyungmin Park } 8222d262c48SKyungmin Park 8232d262c48SKyungmin Park if (vol_type == UBI_VID_STATIC) { 8242d262c48SKyungmin Park /* 8252d262c48SKyungmin Park * Although from high-level point of view static volumes may 8262d262c48SKyungmin Park * contain zero bytes of data, but no VID headers can contain 8272d262c48SKyungmin Park * zero at these fields, because they empty volumes do not have 8282d262c48SKyungmin Park * mapped logical eraseblocks. 8292d262c48SKyungmin Park */ 8302d262c48SKyungmin Park if (used_ebs == 0) { 8312d262c48SKyungmin Park dbg_err("zero used_ebs"); 8322d262c48SKyungmin Park goto bad; 8332d262c48SKyungmin Park } 8342d262c48SKyungmin Park if (data_size == 0) { 8352d262c48SKyungmin Park dbg_err("zero data_size"); 8362d262c48SKyungmin Park goto bad; 8372d262c48SKyungmin Park } 8382d262c48SKyungmin Park if (lnum < used_ebs - 1) { 8392d262c48SKyungmin Park if (data_size != usable_leb_size) { 8402d262c48SKyungmin Park dbg_err("bad data_size"); 8412d262c48SKyungmin Park goto bad; 8422d262c48SKyungmin Park } 8432d262c48SKyungmin Park } else if (lnum == used_ebs - 1) { 8442d262c48SKyungmin Park if (data_size == 0) { 8452d262c48SKyungmin Park dbg_err("bad data_size at last LEB"); 8462d262c48SKyungmin Park goto bad; 8472d262c48SKyungmin Park } 8482d262c48SKyungmin Park } else { 8492d262c48SKyungmin Park dbg_err("too high lnum"); 8502d262c48SKyungmin Park goto bad; 8512d262c48SKyungmin Park } 8522d262c48SKyungmin Park } else { 8532d262c48SKyungmin Park if (copy_flag == 0) { 8542d262c48SKyungmin Park if (data_crc != 0) { 8552d262c48SKyungmin Park dbg_err("non-zero data CRC"); 8562d262c48SKyungmin Park goto bad; 8572d262c48SKyungmin Park } 8582d262c48SKyungmin Park if (data_size != 0) { 8592d262c48SKyungmin Park dbg_err("non-zero data_size"); 8602d262c48SKyungmin Park goto bad; 8612d262c48SKyungmin Park } 8622d262c48SKyungmin Park } else { 8632d262c48SKyungmin Park if (data_size == 0) { 8642d262c48SKyungmin Park dbg_err("zero data_size of copy"); 8652d262c48SKyungmin Park goto bad; 8662d262c48SKyungmin Park } 8672d262c48SKyungmin Park } 8682d262c48SKyungmin Park if (used_ebs != 0) { 8692d262c48SKyungmin Park dbg_err("bad used_ebs"); 8702d262c48SKyungmin Park goto bad; 8712d262c48SKyungmin Park } 8722d262c48SKyungmin Park } 8732d262c48SKyungmin Park 8742d262c48SKyungmin Park return 0; 8752d262c48SKyungmin Park 8762d262c48SKyungmin Park bad: 8772d262c48SKyungmin Park ubi_err("bad VID header"); 8782d262c48SKyungmin Park ubi_dbg_dump_vid_hdr(vid_hdr); 8792d262c48SKyungmin Park ubi_dbg_dump_stack(); 8802d262c48SKyungmin Park return 1; 8812d262c48SKyungmin Park } 8822d262c48SKyungmin Park 8832d262c48SKyungmin Park /** 8842d262c48SKyungmin Park * ubi_io_read_vid_hdr - read and check a volume identifier header. 8852d262c48SKyungmin Park * @ubi: UBI device description object 8862d262c48SKyungmin Park * @pnum: physical eraseblock number to read from 8872d262c48SKyungmin Park * @vid_hdr: &struct ubi_vid_hdr object where to store the read volume 8882d262c48SKyungmin Park * identifier header 8892d262c48SKyungmin Park * @verbose: be verbose if the header is corrupted or wasn't found 8902d262c48SKyungmin Park * 8912d262c48SKyungmin Park * This function reads the volume identifier header from physical eraseblock 8922d262c48SKyungmin Park * @pnum and stores it in @vid_hdr. It also checks CRC checksum of the read 8932d262c48SKyungmin Park * volume identifier header. The following codes may be returned: 8942d262c48SKyungmin Park * 8952d262c48SKyungmin Park * o %0 if the CRC checksum is correct and the header was successfully read; 8962d262c48SKyungmin Park * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected 8972d262c48SKyungmin Park * and corrected by the flash driver; this is harmless but may indicate that 8982d262c48SKyungmin Park * this eraseblock may become bad soon; 8992d262c48SKyungmin Park * o %UBI_IO_BAD_VID_HRD if the volume identifier header is corrupted (a CRC 9002d262c48SKyungmin Park * error detected); 9012d262c48SKyungmin Park * o %UBI_IO_PEB_FREE if the physical eraseblock is free (i.e., there is no VID 9022d262c48SKyungmin Park * header there); 9032d262c48SKyungmin Park * o a negative error code in case of failure. 9042d262c48SKyungmin Park */ 9052d262c48SKyungmin Park int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum, 9062d262c48SKyungmin Park struct ubi_vid_hdr *vid_hdr, int verbose) 9072d262c48SKyungmin Park { 9082d262c48SKyungmin Park int err, read_err = 0; 9092d262c48SKyungmin Park uint32_t crc, magic, hdr_crc; 9102d262c48SKyungmin Park void *p; 9112d262c48SKyungmin Park 9122d262c48SKyungmin Park dbg_io("read VID header from PEB %d", pnum); 9132d262c48SKyungmin Park ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 9142d262c48SKyungmin Park if (UBI_IO_DEBUG) 9152d262c48SKyungmin Park verbose = 1; 9162d262c48SKyungmin Park 9172d262c48SKyungmin Park p = (char *)vid_hdr - ubi->vid_hdr_shift; 9182d262c48SKyungmin Park err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset, 9192d262c48SKyungmin Park ubi->vid_hdr_alsize); 9202d262c48SKyungmin Park if (err) { 9212d262c48SKyungmin Park if (err != UBI_IO_BITFLIPS && err != -EBADMSG) 9222d262c48SKyungmin Park return err; 9232d262c48SKyungmin Park 9242d262c48SKyungmin Park /* 9252d262c48SKyungmin Park * We read all the data, but either a correctable bit-flip 9262d262c48SKyungmin Park * occurred, or MTD reported about some data integrity error, 9272d262c48SKyungmin Park * like an ECC error in case of NAND. The former is harmless, 9282d262c48SKyungmin Park * the later may mean the read data is corrupted. But we have a 9292d262c48SKyungmin Park * CRC check-sum and we will identify this. If the VID header is 9302d262c48SKyungmin Park * still OK, we just report this as there was a bit-flip. 9312d262c48SKyungmin Park */ 9322d262c48SKyungmin Park read_err = err; 9332d262c48SKyungmin Park } 9342d262c48SKyungmin Park 9352d262c48SKyungmin Park magic = be32_to_cpu(vid_hdr->magic); 9362d262c48SKyungmin Park if (magic != UBI_VID_HDR_MAGIC) { 9372d262c48SKyungmin Park /* 9382d262c48SKyungmin Park * If we have read all 0xFF bytes, the VID header probably does 9392d262c48SKyungmin Park * not exist and the physical eraseblock is assumed to be free. 9402d262c48SKyungmin Park * 9412d262c48SKyungmin Park * But if there was a read error, we do not test the data for 9422d262c48SKyungmin Park * 0xFFs. Even if it does contain all 0xFFs, this error 9432d262c48SKyungmin Park * indicates that something is still wrong with this physical 9442d262c48SKyungmin Park * eraseblock and it cannot be regarded as free. 9452d262c48SKyungmin Park */ 9462d262c48SKyungmin Park if (read_err != -EBADMSG && 9472d262c48SKyungmin Park check_pattern(vid_hdr, 0xFF, UBI_VID_HDR_SIZE)) { 9482d262c48SKyungmin Park /* The physical eraseblock is supposedly free */ 9492d262c48SKyungmin Park 9502d262c48SKyungmin Park /* 9512d262c48SKyungmin Park * The below is just a paranoid check, it has to be 9522d262c48SKyungmin Park * compiled out if paranoid checks are disabled. 9532d262c48SKyungmin Park */ 9542d262c48SKyungmin Park err = paranoid_check_all_ff(ubi, pnum, ubi->leb_start, 9552d262c48SKyungmin Park ubi->leb_size); 9562d262c48SKyungmin Park if (err) 9572d262c48SKyungmin Park return err > 0 ? UBI_IO_BAD_VID_HDR : err; 9582d262c48SKyungmin Park 9592d262c48SKyungmin Park if (verbose) 9602d262c48SKyungmin Park ubi_warn("no VID header found at PEB %d, " 9612d262c48SKyungmin Park "only 0xFF bytes", pnum); 9622d262c48SKyungmin Park return UBI_IO_PEB_FREE; 9632d262c48SKyungmin Park } 9642d262c48SKyungmin Park 9652d262c48SKyungmin Park /* 9662d262c48SKyungmin Park * This is not a valid VID header, and these are not 0xFF 9672d262c48SKyungmin Park * bytes. Report that the header is corrupted. 9682d262c48SKyungmin Park */ 9692d262c48SKyungmin Park if (verbose) { 9702d262c48SKyungmin Park ubi_warn("bad magic number at PEB %d: %08x instead of " 9712d262c48SKyungmin Park "%08x", pnum, magic, UBI_VID_HDR_MAGIC); 9722d262c48SKyungmin Park ubi_dbg_dump_vid_hdr(vid_hdr); 9732d262c48SKyungmin Park } 9742d262c48SKyungmin Park return UBI_IO_BAD_VID_HDR; 9752d262c48SKyungmin Park } 9762d262c48SKyungmin Park 9772d262c48SKyungmin Park crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC); 9782d262c48SKyungmin Park hdr_crc = be32_to_cpu(vid_hdr->hdr_crc); 9792d262c48SKyungmin Park 9802d262c48SKyungmin Park if (hdr_crc != crc) { 9812d262c48SKyungmin Park if (verbose) { 9822d262c48SKyungmin Park ubi_warn("bad CRC at PEB %d, calculated %#08x, " 9832d262c48SKyungmin Park "read %#08x", pnum, crc, hdr_crc); 9842d262c48SKyungmin Park ubi_dbg_dump_vid_hdr(vid_hdr); 9852d262c48SKyungmin Park } 9862d262c48SKyungmin Park return UBI_IO_BAD_VID_HDR; 9872d262c48SKyungmin Park } 9882d262c48SKyungmin Park 9892d262c48SKyungmin Park /* Validate the VID header that we have just read */ 9902d262c48SKyungmin Park err = validate_vid_hdr(ubi, vid_hdr); 9912d262c48SKyungmin Park if (err) { 9922d262c48SKyungmin Park ubi_err("validation failed for PEB %d", pnum); 9932d262c48SKyungmin Park return -EINVAL; 9942d262c48SKyungmin Park } 9952d262c48SKyungmin Park 9962d262c48SKyungmin Park return read_err ? UBI_IO_BITFLIPS : 0; 9972d262c48SKyungmin Park } 9982d262c48SKyungmin Park 9992d262c48SKyungmin Park /** 10002d262c48SKyungmin Park * ubi_io_write_vid_hdr - write a volume identifier header. 10012d262c48SKyungmin Park * @ubi: UBI device description object 10022d262c48SKyungmin Park * @pnum: the physical eraseblock number to write to 10032d262c48SKyungmin Park * @vid_hdr: the volume identifier header to write 10042d262c48SKyungmin Park * 10052d262c48SKyungmin Park * This function writes the volume identifier header described by @vid_hdr to 10062d262c48SKyungmin Park * physical eraseblock @pnum. This function automatically fills the 10072d262c48SKyungmin Park * @vid_hdr->magic and the @vid_hdr->version fields, as well as calculates 10082d262c48SKyungmin Park * header CRC checksum and stores it at vid_hdr->hdr_crc. 10092d262c48SKyungmin Park * 10102d262c48SKyungmin Park * This function returns zero in case of success and a negative error code in 10112d262c48SKyungmin Park * case of failure. If %-EIO is returned, the physical eraseblock probably went 10122d262c48SKyungmin Park * bad. 10132d262c48SKyungmin Park */ 10142d262c48SKyungmin Park int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum, 10152d262c48SKyungmin Park struct ubi_vid_hdr *vid_hdr) 10162d262c48SKyungmin Park { 10172d262c48SKyungmin Park int err; 10182d262c48SKyungmin Park uint32_t crc; 10192d262c48SKyungmin Park void *p; 10202d262c48SKyungmin Park 10212d262c48SKyungmin Park dbg_io("write VID header to PEB %d", pnum); 10222d262c48SKyungmin Park ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 10232d262c48SKyungmin Park 10242d262c48SKyungmin Park err = paranoid_check_peb_ec_hdr(ubi, pnum); 10252d262c48SKyungmin Park if (err) 10262d262c48SKyungmin Park return err > 0 ? -EINVAL: err; 10272d262c48SKyungmin Park 10282d262c48SKyungmin Park vid_hdr->magic = cpu_to_be32(UBI_VID_HDR_MAGIC); 10292d262c48SKyungmin Park vid_hdr->version = UBI_VERSION; 10302d262c48SKyungmin Park crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC); 10312d262c48SKyungmin Park vid_hdr->hdr_crc = cpu_to_be32(crc); 10322d262c48SKyungmin Park 10332d262c48SKyungmin Park err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr); 10342d262c48SKyungmin Park if (err) 10352d262c48SKyungmin Park return -EINVAL; 10362d262c48SKyungmin Park 10372d262c48SKyungmin Park p = (char *)vid_hdr - ubi->vid_hdr_shift; 10382d262c48SKyungmin Park err = ubi_io_write(ubi, p, pnum, ubi->vid_hdr_aloffset, 10392d262c48SKyungmin Park ubi->vid_hdr_alsize); 10402d262c48SKyungmin Park return err; 10412d262c48SKyungmin Park } 10422d262c48SKyungmin Park 10432d262c48SKyungmin Park #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID 10442d262c48SKyungmin Park 10452d262c48SKyungmin Park /** 10462d262c48SKyungmin Park * paranoid_check_not_bad - ensure that a physical eraseblock is not bad. 10472d262c48SKyungmin Park * @ubi: UBI device description object 10482d262c48SKyungmin Park * @pnum: physical eraseblock number to check 10492d262c48SKyungmin Park * 10502d262c48SKyungmin Park * This function returns zero if the physical eraseblock is good, a positive 10512d262c48SKyungmin Park * number if it is bad and a negative error code if an error occurred. 10522d262c48SKyungmin Park */ 10532d262c48SKyungmin Park static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum) 10542d262c48SKyungmin Park { 10552d262c48SKyungmin Park int err; 10562d262c48SKyungmin Park 10572d262c48SKyungmin Park err = ubi_io_is_bad(ubi, pnum); 10582d262c48SKyungmin Park if (!err) 10592d262c48SKyungmin Park return err; 10602d262c48SKyungmin Park 10612d262c48SKyungmin Park ubi_err("paranoid check failed for PEB %d", pnum); 10622d262c48SKyungmin Park ubi_dbg_dump_stack(); 10632d262c48SKyungmin Park return err; 10642d262c48SKyungmin Park } 10652d262c48SKyungmin Park 10662d262c48SKyungmin Park /** 10672d262c48SKyungmin Park * paranoid_check_ec_hdr - check if an erase counter header is all right. 10682d262c48SKyungmin Park * @ubi: UBI device description object 10692d262c48SKyungmin Park * @pnum: physical eraseblock number the erase counter header belongs to 10702d262c48SKyungmin Park * @ec_hdr: the erase counter header to check 10712d262c48SKyungmin Park * 10722d262c48SKyungmin Park * This function returns zero if the erase counter header contains valid 10732d262c48SKyungmin Park * values, and %1 if not. 10742d262c48SKyungmin Park */ 10752d262c48SKyungmin Park static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum, 10762d262c48SKyungmin Park const struct ubi_ec_hdr *ec_hdr) 10772d262c48SKyungmin Park { 10782d262c48SKyungmin Park int err; 10792d262c48SKyungmin Park uint32_t magic; 10802d262c48SKyungmin Park 10812d262c48SKyungmin Park magic = be32_to_cpu(ec_hdr->magic); 10822d262c48SKyungmin Park if (magic != UBI_EC_HDR_MAGIC) { 10832d262c48SKyungmin Park ubi_err("bad magic %#08x, must be %#08x", 10842d262c48SKyungmin Park magic, UBI_EC_HDR_MAGIC); 10852d262c48SKyungmin Park goto fail; 10862d262c48SKyungmin Park } 10872d262c48SKyungmin Park 10882d262c48SKyungmin Park err = validate_ec_hdr(ubi, ec_hdr); 10892d262c48SKyungmin Park if (err) { 10902d262c48SKyungmin Park ubi_err("paranoid check failed for PEB %d", pnum); 10912d262c48SKyungmin Park goto fail; 10922d262c48SKyungmin Park } 10932d262c48SKyungmin Park 10942d262c48SKyungmin Park return 0; 10952d262c48SKyungmin Park 10962d262c48SKyungmin Park fail: 10972d262c48SKyungmin Park ubi_dbg_dump_ec_hdr(ec_hdr); 10982d262c48SKyungmin Park ubi_dbg_dump_stack(); 10992d262c48SKyungmin Park return 1; 11002d262c48SKyungmin Park } 11012d262c48SKyungmin Park 11022d262c48SKyungmin Park /** 11032d262c48SKyungmin Park * paranoid_check_peb_ec_hdr - check that the erase counter header of a 11042d262c48SKyungmin Park * physical eraseblock is in-place and is all right. 11052d262c48SKyungmin Park * @ubi: UBI device description object 11062d262c48SKyungmin Park * @pnum: the physical eraseblock number to check 11072d262c48SKyungmin Park * 11082d262c48SKyungmin Park * This function returns zero if the erase counter header is all right, %1 if 11092d262c48SKyungmin Park * not, and a negative error code if an error occurred. 11102d262c48SKyungmin Park */ 11112d262c48SKyungmin Park static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum) 11122d262c48SKyungmin Park { 11132d262c48SKyungmin Park int err; 11142d262c48SKyungmin Park uint32_t crc, hdr_crc; 11152d262c48SKyungmin Park struct ubi_ec_hdr *ec_hdr; 11162d262c48SKyungmin Park 11172d262c48SKyungmin Park ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS); 11182d262c48SKyungmin Park if (!ec_hdr) 11192d262c48SKyungmin Park return -ENOMEM; 11202d262c48SKyungmin Park 11212d262c48SKyungmin Park err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE); 11222d262c48SKyungmin Park if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG) 11232d262c48SKyungmin Park goto exit; 11242d262c48SKyungmin Park 11252d262c48SKyungmin Park crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC); 11262d262c48SKyungmin Park hdr_crc = be32_to_cpu(ec_hdr->hdr_crc); 11272d262c48SKyungmin Park if (hdr_crc != crc) { 11282d262c48SKyungmin Park ubi_err("bad CRC, calculated %#08x, read %#08x", crc, hdr_crc); 11292d262c48SKyungmin Park ubi_err("paranoid check failed for PEB %d", pnum); 11302d262c48SKyungmin Park ubi_dbg_dump_ec_hdr(ec_hdr); 11312d262c48SKyungmin Park ubi_dbg_dump_stack(); 11322d262c48SKyungmin Park err = 1; 11332d262c48SKyungmin Park goto exit; 11342d262c48SKyungmin Park } 11352d262c48SKyungmin Park 11362d262c48SKyungmin Park err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr); 11372d262c48SKyungmin Park 11382d262c48SKyungmin Park exit: 11392d262c48SKyungmin Park kfree(ec_hdr); 11402d262c48SKyungmin Park return err; 11412d262c48SKyungmin Park } 11422d262c48SKyungmin Park 11432d262c48SKyungmin Park /** 11442d262c48SKyungmin Park * paranoid_check_vid_hdr - check that a volume identifier header is all right. 11452d262c48SKyungmin Park * @ubi: UBI device description object 11462d262c48SKyungmin Park * @pnum: physical eraseblock number the volume identifier header belongs to 11472d262c48SKyungmin Park * @vid_hdr: the volume identifier header to check 11482d262c48SKyungmin Park * 11492d262c48SKyungmin Park * This function returns zero if the volume identifier header is all right, and 11502d262c48SKyungmin Park * %1 if not. 11512d262c48SKyungmin Park */ 11522d262c48SKyungmin Park static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum, 11532d262c48SKyungmin Park const struct ubi_vid_hdr *vid_hdr) 11542d262c48SKyungmin Park { 11552d262c48SKyungmin Park int err; 11562d262c48SKyungmin Park uint32_t magic; 11572d262c48SKyungmin Park 11582d262c48SKyungmin Park magic = be32_to_cpu(vid_hdr->magic); 11592d262c48SKyungmin Park if (magic != UBI_VID_HDR_MAGIC) { 11602d262c48SKyungmin Park ubi_err("bad VID header magic %#08x at PEB %d, must be %#08x", 11612d262c48SKyungmin Park magic, pnum, UBI_VID_HDR_MAGIC); 11622d262c48SKyungmin Park goto fail; 11632d262c48SKyungmin Park } 11642d262c48SKyungmin Park 11652d262c48SKyungmin Park err = validate_vid_hdr(ubi, vid_hdr); 11662d262c48SKyungmin Park if (err) { 11672d262c48SKyungmin Park ubi_err("paranoid check failed for PEB %d", pnum); 11682d262c48SKyungmin Park goto fail; 11692d262c48SKyungmin Park } 11702d262c48SKyungmin Park 11712d262c48SKyungmin Park return err; 11722d262c48SKyungmin Park 11732d262c48SKyungmin Park fail: 11742d262c48SKyungmin Park ubi_err("paranoid check failed for PEB %d", pnum); 11752d262c48SKyungmin Park ubi_dbg_dump_vid_hdr(vid_hdr); 11762d262c48SKyungmin Park ubi_dbg_dump_stack(); 11772d262c48SKyungmin Park return 1; 11782d262c48SKyungmin Park 11792d262c48SKyungmin Park } 11802d262c48SKyungmin Park 11812d262c48SKyungmin Park /** 11822d262c48SKyungmin Park * paranoid_check_peb_vid_hdr - check that the volume identifier header of a 11832d262c48SKyungmin Park * physical eraseblock is in-place and is all right. 11842d262c48SKyungmin Park * @ubi: UBI device description object 11852d262c48SKyungmin Park * @pnum: the physical eraseblock number to check 11862d262c48SKyungmin Park * 11872d262c48SKyungmin Park * This function returns zero if the volume identifier header is all right, 11882d262c48SKyungmin Park * %1 if not, and a negative error code if an error occurred. 11892d262c48SKyungmin Park */ 11902d262c48SKyungmin Park static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum) 11912d262c48SKyungmin Park { 11922d262c48SKyungmin Park int err; 11932d262c48SKyungmin Park uint32_t crc, hdr_crc; 11942d262c48SKyungmin Park struct ubi_vid_hdr *vid_hdr; 11952d262c48SKyungmin Park void *p; 11962d262c48SKyungmin Park 11972d262c48SKyungmin Park vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS); 11982d262c48SKyungmin Park if (!vid_hdr) 11992d262c48SKyungmin Park return -ENOMEM; 12002d262c48SKyungmin Park 12012d262c48SKyungmin Park p = (char *)vid_hdr - ubi->vid_hdr_shift; 12022d262c48SKyungmin Park err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset, 12032d262c48SKyungmin Park ubi->vid_hdr_alsize); 12042d262c48SKyungmin Park if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG) 12052d262c48SKyungmin Park goto exit; 12062d262c48SKyungmin Park 12072d262c48SKyungmin Park crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_EC_HDR_SIZE_CRC); 12082d262c48SKyungmin Park hdr_crc = be32_to_cpu(vid_hdr->hdr_crc); 12092d262c48SKyungmin Park if (hdr_crc != crc) { 12102d262c48SKyungmin Park ubi_err("bad VID header CRC at PEB %d, calculated %#08x, " 12112d262c48SKyungmin Park "read %#08x", pnum, crc, hdr_crc); 12122d262c48SKyungmin Park ubi_err("paranoid check failed for PEB %d", pnum); 12132d262c48SKyungmin Park ubi_dbg_dump_vid_hdr(vid_hdr); 12142d262c48SKyungmin Park ubi_dbg_dump_stack(); 12152d262c48SKyungmin Park err = 1; 12162d262c48SKyungmin Park goto exit; 12172d262c48SKyungmin Park } 12182d262c48SKyungmin Park 12192d262c48SKyungmin Park err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr); 12202d262c48SKyungmin Park 12212d262c48SKyungmin Park exit: 12222d262c48SKyungmin Park ubi_free_vid_hdr(ubi, vid_hdr); 12232d262c48SKyungmin Park return err; 12242d262c48SKyungmin Park } 12252d262c48SKyungmin Park 12262d262c48SKyungmin Park /** 12272d262c48SKyungmin Park * paranoid_check_all_ff - check that a region of flash is empty. 12282d262c48SKyungmin Park * @ubi: UBI device description object 12292d262c48SKyungmin Park * @pnum: the physical eraseblock number to check 12302d262c48SKyungmin Park * @offset: the starting offset within the physical eraseblock to check 12312d262c48SKyungmin Park * @len: the length of the region to check 12322d262c48SKyungmin Park * 12332d262c48SKyungmin Park * This function returns zero if only 0xFF bytes are present at offset 12342d262c48SKyungmin Park * @offset of the physical eraseblock @pnum, %1 if not, and a negative error 12352d262c48SKyungmin Park * code if an error occurred. 12362d262c48SKyungmin Park */ 12372d262c48SKyungmin Park static int paranoid_check_all_ff(struct ubi_device *ubi, int pnum, int offset, 12382d262c48SKyungmin Park int len) 12392d262c48SKyungmin Park { 12402d262c48SKyungmin Park size_t read; 12412d262c48SKyungmin Park int err; 12422d262c48SKyungmin Park loff_t addr = (loff_t)pnum * ubi->peb_size + offset; 12432d262c48SKyungmin Park 12442d262c48SKyungmin Park mutex_lock(&ubi->dbg_buf_mutex); 12452d262c48SKyungmin Park err = ubi->mtd->read(ubi->mtd, addr, len, &read, ubi->dbg_peb_buf); 12462d262c48SKyungmin Park if (err && err != -EUCLEAN) { 12472d262c48SKyungmin Park ubi_err("error %d while reading %d bytes from PEB %d:%d, " 12482d262c48SKyungmin Park "read %zd bytes", err, len, pnum, offset, read); 12492d262c48SKyungmin Park goto error; 12502d262c48SKyungmin Park } 12512d262c48SKyungmin Park 12522d262c48SKyungmin Park err = check_pattern(ubi->dbg_peb_buf, 0xFF, len); 12532d262c48SKyungmin Park if (err == 0) { 12542d262c48SKyungmin Park ubi_err("flash region at PEB %d:%d, length %d does not " 12552d262c48SKyungmin Park "contain all 0xFF bytes", pnum, offset, len); 12562d262c48SKyungmin Park goto fail; 12572d262c48SKyungmin Park } 12582d262c48SKyungmin Park mutex_unlock(&ubi->dbg_buf_mutex); 12592d262c48SKyungmin Park 12602d262c48SKyungmin Park return 0; 12612d262c48SKyungmin Park 12622d262c48SKyungmin Park fail: 12632d262c48SKyungmin Park ubi_err("paranoid check failed for PEB %d", pnum); 12642d262c48SKyungmin Park dbg_msg("hex dump of the %d-%d region", offset, offset + len); 12652d262c48SKyungmin Park print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, 12662d262c48SKyungmin Park ubi->dbg_peb_buf, len, 1); 12672d262c48SKyungmin Park err = 1; 12682d262c48SKyungmin Park error: 12692d262c48SKyungmin Park ubi_dbg_dump_stack(); 12702d262c48SKyungmin Park mutex_unlock(&ubi->dbg_buf_mutex); 12712d262c48SKyungmin Park return err; 12722d262c48SKyungmin Park } 12732d262c48SKyungmin Park 12742d262c48SKyungmin Park #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */ 1275