162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright (c) International Business Machines Corp., 2006 462306a36Sopenharmony_ci * Copyright (c) Nokia Corporation, 2006, 2007 562306a36Sopenharmony_ci * 662306a36Sopenharmony_ci * Author: Artem Bityutskiy (Битюцкий Артём) 762306a36Sopenharmony_ci */ 862306a36Sopenharmony_ci 962306a36Sopenharmony_ci/* 1062306a36Sopenharmony_ci * UBI input/output sub-system. 1162306a36Sopenharmony_ci * 1262306a36Sopenharmony_ci * This sub-system provides a uniform way to work with all kinds of the 1362306a36Sopenharmony_ci * underlying MTD devices. It also implements handy functions for reading and 1462306a36Sopenharmony_ci * writing UBI headers. 1562306a36Sopenharmony_ci * 1662306a36Sopenharmony_ci * We are trying to have a paranoid mindset and not to trust to what we read 1762306a36Sopenharmony_ci * from the flash media in order to be more secure and robust. So this 1862306a36Sopenharmony_ci * sub-system validates every single header it reads from the flash media. 1962306a36Sopenharmony_ci * 2062306a36Sopenharmony_ci * Some words about how the eraseblock headers are stored. 2162306a36Sopenharmony_ci * 2262306a36Sopenharmony_ci * The erase counter header is always stored at offset zero. By default, the 2362306a36Sopenharmony_ci * VID header is stored after the EC header at the closest aligned offset 2462306a36Sopenharmony_ci * (i.e. aligned to the minimum I/O unit size). Data starts next to the VID 2562306a36Sopenharmony_ci * header at the closest aligned offset. But this default layout may be 2662306a36Sopenharmony_ci * changed. For example, for different reasons (e.g., optimization) UBI may be 2762306a36Sopenharmony_ci * asked to put the VID header at further offset, and even at an unaligned 2862306a36Sopenharmony_ci * offset. Of course, if the offset of the VID header is unaligned, UBI adds 2962306a36Sopenharmony_ci * proper padding in front of it. Data offset may also be changed but it has to 3062306a36Sopenharmony_ci * be aligned. 3162306a36Sopenharmony_ci * 3262306a36Sopenharmony_ci * About minimal I/O units. In general, UBI assumes flash device model where 3362306a36Sopenharmony_ci * there is only one minimal I/O unit size. E.g., in case of NOR flash it is 1, 3462306a36Sopenharmony_ci * in case of NAND flash it is a NAND page, etc. This is reported by MTD in the 3562306a36Sopenharmony_ci * @ubi->mtd->writesize field. But as an exception, UBI admits use of another 3662306a36Sopenharmony_ci * (smaller) minimal I/O unit size for EC and VID headers to make it possible 3762306a36Sopenharmony_ci * to do different optimizations. 3862306a36Sopenharmony_ci * 3962306a36Sopenharmony_ci * This is extremely useful in case of NAND flashes which admit of several 4062306a36Sopenharmony_ci * write operations to one NAND page. In this case UBI can fit EC and VID 4162306a36Sopenharmony_ci * headers at one NAND page. Thus, UBI may use "sub-page" size as the minimal 4262306a36Sopenharmony_ci * I/O unit for the headers (the @ubi->hdrs_min_io_size field). But it still 4362306a36Sopenharmony_ci * reports NAND page size (@ubi->min_io_size) as a minimal I/O unit for the UBI 4462306a36Sopenharmony_ci * users. 4562306a36Sopenharmony_ci * 4662306a36Sopenharmony_ci * Example: some Samsung NANDs with 2KiB pages allow 4x 512-byte writes, so 4762306a36Sopenharmony_ci * although the minimal I/O unit is 2K, UBI uses 512 bytes for EC and VID 4862306a36Sopenharmony_ci * headers. 4962306a36Sopenharmony_ci * 5062306a36Sopenharmony_ci * Q: why not just to treat sub-page as a minimal I/O unit of this flash 5162306a36Sopenharmony_ci * device, e.g., make @ubi->min_io_size = 512 in the example above? 5262306a36Sopenharmony_ci * 5362306a36Sopenharmony_ci * A: because when writing a sub-page, MTD still writes a full 2K page but the 5462306a36Sopenharmony_ci * bytes which are not relevant to the sub-page are 0xFF. So, basically, 5562306a36Sopenharmony_ci * writing 4x512 sub-pages is 4 times slower than writing one 2KiB NAND page. 5662306a36Sopenharmony_ci * Thus, we prefer to use sub-pages only for EC and VID headers. 5762306a36Sopenharmony_ci * 5862306a36Sopenharmony_ci * As it was noted above, the VID header may start at a non-aligned offset. 5962306a36Sopenharmony_ci * For example, in case of a 2KiB page NAND flash with a 512 bytes sub-page, 6062306a36Sopenharmony_ci * the VID header may reside at offset 1984 which is the last 64 bytes of the 6162306a36Sopenharmony_ci * last sub-page (EC header is always at offset zero). This causes some 6262306a36Sopenharmony_ci * difficulties when reading and writing VID headers. 6362306a36Sopenharmony_ci * 6462306a36Sopenharmony_ci * Suppose we have a 64-byte buffer and we read a VID header at it. We change 6562306a36Sopenharmony_ci * the data and want to write this VID header out. As we can only write in 6662306a36Sopenharmony_ci * 512-byte chunks, we have to allocate one more buffer and copy our VID header 6762306a36Sopenharmony_ci * to offset 448 of this buffer. 6862306a36Sopenharmony_ci * 6962306a36Sopenharmony_ci * The I/O sub-system does the following trick in order to avoid this extra 7062306a36Sopenharmony_ci * copy. It always allocates a @ubi->vid_hdr_alsize bytes buffer for the VID 7162306a36Sopenharmony_ci * header and returns a pointer to offset @ubi->vid_hdr_shift of this buffer. 7262306a36Sopenharmony_ci * When the VID header is being written out, it shifts the VID header pointer 7362306a36Sopenharmony_ci * back and writes the whole sub-page. 7462306a36Sopenharmony_ci */ 7562306a36Sopenharmony_ci 7662306a36Sopenharmony_ci#include <linux/crc32.h> 7762306a36Sopenharmony_ci#include <linux/err.h> 7862306a36Sopenharmony_ci#include <linux/slab.h> 7962306a36Sopenharmony_ci#include "ubi.h" 8062306a36Sopenharmony_ci 8162306a36Sopenharmony_cistatic int self_check_not_bad(const struct ubi_device *ubi, int pnum); 8262306a36Sopenharmony_cistatic int self_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum); 8362306a36Sopenharmony_cistatic int self_check_ec_hdr(const struct ubi_device *ubi, int pnum, 8462306a36Sopenharmony_ci const struct ubi_ec_hdr *ec_hdr); 8562306a36Sopenharmony_cistatic int self_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum); 8662306a36Sopenharmony_cistatic int self_check_vid_hdr(const struct ubi_device *ubi, int pnum, 8762306a36Sopenharmony_ci const struct ubi_vid_hdr *vid_hdr); 8862306a36Sopenharmony_cistatic int self_check_write(struct ubi_device *ubi, const void *buf, int pnum, 8962306a36Sopenharmony_ci int offset, int len); 9062306a36Sopenharmony_ci 9162306a36Sopenharmony_ci/** 9262306a36Sopenharmony_ci * ubi_io_read - read data from a physical eraseblock. 9362306a36Sopenharmony_ci * @ubi: UBI device description object 9462306a36Sopenharmony_ci * @buf: buffer where to store the read data 9562306a36Sopenharmony_ci * @pnum: physical eraseblock number to read from 9662306a36Sopenharmony_ci * @offset: offset within the physical eraseblock from where to read 9762306a36Sopenharmony_ci * @len: how many bytes to read 9862306a36Sopenharmony_ci * 9962306a36Sopenharmony_ci * This function reads data from offset @offset of physical eraseblock @pnum 10062306a36Sopenharmony_ci * and stores the read data in the @buf buffer. The following return codes are 10162306a36Sopenharmony_ci * possible: 10262306a36Sopenharmony_ci * 10362306a36Sopenharmony_ci * o %0 if all the requested data were successfully read; 10462306a36Sopenharmony_ci * o %UBI_IO_BITFLIPS if all the requested data were successfully read, but 10562306a36Sopenharmony_ci * correctable bit-flips were detected; this is harmless but may indicate 10662306a36Sopenharmony_ci * that this eraseblock may become bad soon (but do not have to); 10762306a36Sopenharmony_ci * o %-EBADMSG if the MTD subsystem reported about data integrity problems, for 10862306a36Sopenharmony_ci * example it can be an ECC error in case of NAND; this most probably means 10962306a36Sopenharmony_ci * that the data is corrupted; 11062306a36Sopenharmony_ci * o %-EIO if some I/O error occurred; 11162306a36Sopenharmony_ci * o other negative error codes in case of other errors. 11262306a36Sopenharmony_ci */ 11362306a36Sopenharmony_ciint ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset, 11462306a36Sopenharmony_ci int len) 11562306a36Sopenharmony_ci{ 11662306a36Sopenharmony_ci int err, retries = 0; 11762306a36Sopenharmony_ci size_t read; 11862306a36Sopenharmony_ci loff_t addr; 11962306a36Sopenharmony_ci 12062306a36Sopenharmony_ci dbg_io("read %d bytes from PEB %d:%d", len, pnum, offset); 12162306a36Sopenharmony_ci 12262306a36Sopenharmony_ci ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 12362306a36Sopenharmony_ci ubi_assert(offset >= 0 && offset + len <= ubi->peb_size); 12462306a36Sopenharmony_ci ubi_assert(len > 0); 12562306a36Sopenharmony_ci 12662306a36Sopenharmony_ci err = self_check_not_bad(ubi, pnum); 12762306a36Sopenharmony_ci if (err) 12862306a36Sopenharmony_ci return err; 12962306a36Sopenharmony_ci 13062306a36Sopenharmony_ci /* 13162306a36Sopenharmony_ci * Deliberately corrupt the buffer to improve robustness. Indeed, if we 13262306a36Sopenharmony_ci * do not do this, the following may happen: 13362306a36Sopenharmony_ci * 1. The buffer contains data from previous operation, e.g., read from 13462306a36Sopenharmony_ci * another PEB previously. The data looks like expected, e.g., if we 13562306a36Sopenharmony_ci * just do not read anything and return - the caller would not 13662306a36Sopenharmony_ci * notice this. E.g., if we are reading a VID header, the buffer may 13762306a36Sopenharmony_ci * contain a valid VID header from another PEB. 13862306a36Sopenharmony_ci * 2. The driver is buggy and returns us success or -EBADMSG or 13962306a36Sopenharmony_ci * -EUCLEAN, but it does not actually put any data to the buffer. 14062306a36Sopenharmony_ci * 14162306a36Sopenharmony_ci * This may confuse UBI or upper layers - they may think the buffer 14262306a36Sopenharmony_ci * contains valid data while in fact it is just old data. This is 14362306a36Sopenharmony_ci * especially possible because UBI (and UBIFS) relies on CRC, and 14462306a36Sopenharmony_ci * treats data as correct even in case of ECC errors if the CRC is 14562306a36Sopenharmony_ci * correct. 14662306a36Sopenharmony_ci * 14762306a36Sopenharmony_ci * Try to prevent this situation by changing the first byte of the 14862306a36Sopenharmony_ci * buffer. 14962306a36Sopenharmony_ci */ 15062306a36Sopenharmony_ci *((uint8_t *)buf) ^= 0xFF; 15162306a36Sopenharmony_ci 15262306a36Sopenharmony_ci addr = (loff_t)pnum * ubi->peb_size + offset; 15362306a36Sopenharmony_ciretry: 15462306a36Sopenharmony_ci err = mtd_read(ubi->mtd, addr, len, &read, buf); 15562306a36Sopenharmony_ci if (err) { 15662306a36Sopenharmony_ci const char *errstr = mtd_is_eccerr(err) ? " (ECC error)" : ""; 15762306a36Sopenharmony_ci 15862306a36Sopenharmony_ci if (mtd_is_bitflip(err)) { 15962306a36Sopenharmony_ci /* 16062306a36Sopenharmony_ci * -EUCLEAN is reported if there was a bit-flip which 16162306a36Sopenharmony_ci * was corrected, so this is harmless. 16262306a36Sopenharmony_ci * 16362306a36Sopenharmony_ci * We do not report about it here unless debugging is 16462306a36Sopenharmony_ci * enabled. A corresponding message will be printed 16562306a36Sopenharmony_ci * later, when it is has been scrubbed. 16662306a36Sopenharmony_ci */ 16762306a36Sopenharmony_ci ubi_msg(ubi, "fixable bit-flip detected at PEB %d", 16862306a36Sopenharmony_ci pnum); 16962306a36Sopenharmony_ci ubi_assert(len == read); 17062306a36Sopenharmony_ci return UBI_IO_BITFLIPS; 17162306a36Sopenharmony_ci } 17262306a36Sopenharmony_ci 17362306a36Sopenharmony_ci if (retries++ < UBI_IO_RETRIES) { 17462306a36Sopenharmony_ci ubi_warn(ubi, "error %d%s while reading %d bytes from PEB %d:%d, read only %zd bytes, retry", 17562306a36Sopenharmony_ci err, errstr, len, pnum, offset, read); 17662306a36Sopenharmony_ci yield(); 17762306a36Sopenharmony_ci goto retry; 17862306a36Sopenharmony_ci } 17962306a36Sopenharmony_ci 18062306a36Sopenharmony_ci ubi_err(ubi, "error %d%s while reading %d bytes from PEB %d:%d, read %zd bytes", 18162306a36Sopenharmony_ci err, errstr, len, pnum, offset, read); 18262306a36Sopenharmony_ci dump_stack(); 18362306a36Sopenharmony_ci 18462306a36Sopenharmony_ci /* 18562306a36Sopenharmony_ci * The driver should never return -EBADMSG if it failed to read 18662306a36Sopenharmony_ci * all the requested data. But some buggy drivers might do 18762306a36Sopenharmony_ci * this, so we change it to -EIO. 18862306a36Sopenharmony_ci */ 18962306a36Sopenharmony_ci if (read != len && mtd_is_eccerr(err)) { 19062306a36Sopenharmony_ci ubi_assert(0); 19162306a36Sopenharmony_ci err = -EIO; 19262306a36Sopenharmony_ci } 19362306a36Sopenharmony_ci } else { 19462306a36Sopenharmony_ci ubi_assert(len == read); 19562306a36Sopenharmony_ci 19662306a36Sopenharmony_ci if (ubi_dbg_is_bitflip(ubi)) { 19762306a36Sopenharmony_ci dbg_gen("bit-flip (emulated)"); 19862306a36Sopenharmony_ci err = UBI_IO_BITFLIPS; 19962306a36Sopenharmony_ci } 20062306a36Sopenharmony_ci } 20162306a36Sopenharmony_ci 20262306a36Sopenharmony_ci return err; 20362306a36Sopenharmony_ci} 20462306a36Sopenharmony_ci 20562306a36Sopenharmony_ci/** 20662306a36Sopenharmony_ci * ubi_io_write - write data to a physical eraseblock. 20762306a36Sopenharmony_ci * @ubi: UBI device description object 20862306a36Sopenharmony_ci * @buf: buffer with the data to write 20962306a36Sopenharmony_ci * @pnum: physical eraseblock number to write to 21062306a36Sopenharmony_ci * @offset: offset within the physical eraseblock where to write 21162306a36Sopenharmony_ci * @len: how many bytes to write 21262306a36Sopenharmony_ci * 21362306a36Sopenharmony_ci * This function writes @len bytes of data from buffer @buf to offset @offset 21462306a36Sopenharmony_ci * of physical eraseblock @pnum. If all the data were successfully written, 21562306a36Sopenharmony_ci * zero is returned. If an error occurred, this function returns a negative 21662306a36Sopenharmony_ci * error code. If %-EIO is returned, the physical eraseblock most probably went 21762306a36Sopenharmony_ci * bad. 21862306a36Sopenharmony_ci * 21962306a36Sopenharmony_ci * Note, in case of an error, it is possible that something was still written 22062306a36Sopenharmony_ci * to the flash media, but may be some garbage. 22162306a36Sopenharmony_ci */ 22262306a36Sopenharmony_ciint ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset, 22362306a36Sopenharmony_ci int len) 22462306a36Sopenharmony_ci{ 22562306a36Sopenharmony_ci int err; 22662306a36Sopenharmony_ci size_t written; 22762306a36Sopenharmony_ci loff_t addr; 22862306a36Sopenharmony_ci 22962306a36Sopenharmony_ci dbg_io("write %d bytes to PEB %d:%d", len, pnum, offset); 23062306a36Sopenharmony_ci 23162306a36Sopenharmony_ci ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 23262306a36Sopenharmony_ci ubi_assert(offset >= 0 && offset + len <= ubi->peb_size); 23362306a36Sopenharmony_ci ubi_assert(offset % ubi->hdrs_min_io_size == 0); 23462306a36Sopenharmony_ci ubi_assert(len > 0 && len % ubi->hdrs_min_io_size == 0); 23562306a36Sopenharmony_ci 23662306a36Sopenharmony_ci if (ubi->ro_mode) { 23762306a36Sopenharmony_ci ubi_err(ubi, "read-only mode"); 23862306a36Sopenharmony_ci return -EROFS; 23962306a36Sopenharmony_ci } 24062306a36Sopenharmony_ci 24162306a36Sopenharmony_ci err = self_check_not_bad(ubi, pnum); 24262306a36Sopenharmony_ci if (err) 24362306a36Sopenharmony_ci return err; 24462306a36Sopenharmony_ci 24562306a36Sopenharmony_ci /* The area we are writing to has to contain all 0xFF bytes */ 24662306a36Sopenharmony_ci err = ubi_self_check_all_ff(ubi, pnum, offset, len); 24762306a36Sopenharmony_ci if (err) 24862306a36Sopenharmony_ci return err; 24962306a36Sopenharmony_ci 25062306a36Sopenharmony_ci if (offset >= ubi->leb_start) { 25162306a36Sopenharmony_ci /* 25262306a36Sopenharmony_ci * We write to the data area of the physical eraseblock. Make 25362306a36Sopenharmony_ci * sure it has valid EC and VID headers. 25462306a36Sopenharmony_ci */ 25562306a36Sopenharmony_ci err = self_check_peb_ec_hdr(ubi, pnum); 25662306a36Sopenharmony_ci if (err) 25762306a36Sopenharmony_ci return err; 25862306a36Sopenharmony_ci err = self_check_peb_vid_hdr(ubi, pnum); 25962306a36Sopenharmony_ci if (err) 26062306a36Sopenharmony_ci return err; 26162306a36Sopenharmony_ci } 26262306a36Sopenharmony_ci 26362306a36Sopenharmony_ci if (ubi_dbg_is_write_failure(ubi)) { 26462306a36Sopenharmony_ci ubi_err(ubi, "cannot write %d bytes to PEB %d:%d (emulated)", 26562306a36Sopenharmony_ci len, pnum, offset); 26662306a36Sopenharmony_ci dump_stack(); 26762306a36Sopenharmony_ci return -EIO; 26862306a36Sopenharmony_ci } 26962306a36Sopenharmony_ci 27062306a36Sopenharmony_ci addr = (loff_t)pnum * ubi->peb_size + offset; 27162306a36Sopenharmony_ci err = mtd_write(ubi->mtd, addr, len, &written, buf); 27262306a36Sopenharmony_ci if (err) { 27362306a36Sopenharmony_ci ubi_err(ubi, "error %d while writing %d bytes to PEB %d:%d, written %zd bytes", 27462306a36Sopenharmony_ci err, len, pnum, offset, written); 27562306a36Sopenharmony_ci dump_stack(); 27662306a36Sopenharmony_ci ubi_dump_flash(ubi, pnum, offset, len); 27762306a36Sopenharmony_ci } else 27862306a36Sopenharmony_ci ubi_assert(written == len); 27962306a36Sopenharmony_ci 28062306a36Sopenharmony_ci if (!err) { 28162306a36Sopenharmony_ci err = self_check_write(ubi, buf, pnum, offset, len); 28262306a36Sopenharmony_ci if (err) 28362306a36Sopenharmony_ci return err; 28462306a36Sopenharmony_ci 28562306a36Sopenharmony_ci /* 28662306a36Sopenharmony_ci * Since we always write sequentially, the rest of the PEB has 28762306a36Sopenharmony_ci * to contain only 0xFF bytes. 28862306a36Sopenharmony_ci */ 28962306a36Sopenharmony_ci offset += len; 29062306a36Sopenharmony_ci len = ubi->peb_size - offset; 29162306a36Sopenharmony_ci if (len) 29262306a36Sopenharmony_ci err = ubi_self_check_all_ff(ubi, pnum, offset, len); 29362306a36Sopenharmony_ci } 29462306a36Sopenharmony_ci 29562306a36Sopenharmony_ci return err; 29662306a36Sopenharmony_ci} 29762306a36Sopenharmony_ci 29862306a36Sopenharmony_ci/** 29962306a36Sopenharmony_ci * do_sync_erase - synchronously erase a physical eraseblock. 30062306a36Sopenharmony_ci * @ubi: UBI device description object 30162306a36Sopenharmony_ci * @pnum: the physical eraseblock number to erase 30262306a36Sopenharmony_ci * 30362306a36Sopenharmony_ci * This function synchronously erases physical eraseblock @pnum and returns 30462306a36Sopenharmony_ci * zero in case of success and a negative error code in case of failure. If 30562306a36Sopenharmony_ci * %-EIO is returned, the physical eraseblock most probably went bad. 30662306a36Sopenharmony_ci */ 30762306a36Sopenharmony_cistatic int do_sync_erase(struct ubi_device *ubi, int pnum) 30862306a36Sopenharmony_ci{ 30962306a36Sopenharmony_ci int err, retries = 0; 31062306a36Sopenharmony_ci struct erase_info ei; 31162306a36Sopenharmony_ci 31262306a36Sopenharmony_ci dbg_io("erase PEB %d", pnum); 31362306a36Sopenharmony_ci ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 31462306a36Sopenharmony_ci 31562306a36Sopenharmony_ci if (ubi->ro_mode) { 31662306a36Sopenharmony_ci ubi_err(ubi, "read-only mode"); 31762306a36Sopenharmony_ci return -EROFS; 31862306a36Sopenharmony_ci } 31962306a36Sopenharmony_ci 32062306a36Sopenharmony_ciretry: 32162306a36Sopenharmony_ci memset(&ei, 0, sizeof(struct erase_info)); 32262306a36Sopenharmony_ci 32362306a36Sopenharmony_ci ei.addr = (loff_t)pnum * ubi->peb_size; 32462306a36Sopenharmony_ci ei.len = ubi->peb_size; 32562306a36Sopenharmony_ci 32662306a36Sopenharmony_ci err = mtd_erase(ubi->mtd, &ei); 32762306a36Sopenharmony_ci if (err) { 32862306a36Sopenharmony_ci if (retries++ < UBI_IO_RETRIES) { 32962306a36Sopenharmony_ci ubi_warn(ubi, "error %d while erasing PEB %d, retry", 33062306a36Sopenharmony_ci err, pnum); 33162306a36Sopenharmony_ci yield(); 33262306a36Sopenharmony_ci goto retry; 33362306a36Sopenharmony_ci } 33462306a36Sopenharmony_ci ubi_err(ubi, "cannot erase PEB %d, error %d", pnum, err); 33562306a36Sopenharmony_ci dump_stack(); 33662306a36Sopenharmony_ci return err; 33762306a36Sopenharmony_ci } 33862306a36Sopenharmony_ci 33962306a36Sopenharmony_ci err = ubi_self_check_all_ff(ubi, pnum, 0, ubi->peb_size); 34062306a36Sopenharmony_ci if (err) 34162306a36Sopenharmony_ci return err; 34262306a36Sopenharmony_ci 34362306a36Sopenharmony_ci if (ubi_dbg_is_erase_failure(ubi)) { 34462306a36Sopenharmony_ci ubi_err(ubi, "cannot erase PEB %d (emulated)", pnum); 34562306a36Sopenharmony_ci return -EIO; 34662306a36Sopenharmony_ci } 34762306a36Sopenharmony_ci 34862306a36Sopenharmony_ci return 0; 34962306a36Sopenharmony_ci} 35062306a36Sopenharmony_ci 35162306a36Sopenharmony_ci/* Patterns to write to a physical eraseblock when torturing it */ 35262306a36Sopenharmony_cistatic uint8_t patterns[] = {0xa5, 0x5a, 0x0}; 35362306a36Sopenharmony_ci 35462306a36Sopenharmony_ci/** 35562306a36Sopenharmony_ci * torture_peb - test a supposedly bad physical eraseblock. 35662306a36Sopenharmony_ci * @ubi: UBI device description object 35762306a36Sopenharmony_ci * @pnum: the physical eraseblock number to test 35862306a36Sopenharmony_ci * 35962306a36Sopenharmony_ci * This function returns %-EIO if the physical eraseblock did not pass the 36062306a36Sopenharmony_ci * test, a positive number of erase operations done if the test was 36162306a36Sopenharmony_ci * successfully passed, and other negative error codes in case of other errors. 36262306a36Sopenharmony_ci */ 36362306a36Sopenharmony_cistatic int torture_peb(struct ubi_device *ubi, int pnum) 36462306a36Sopenharmony_ci{ 36562306a36Sopenharmony_ci int err, i, patt_count; 36662306a36Sopenharmony_ci 36762306a36Sopenharmony_ci ubi_msg(ubi, "run torture test for PEB %d", pnum); 36862306a36Sopenharmony_ci patt_count = ARRAY_SIZE(patterns); 36962306a36Sopenharmony_ci ubi_assert(patt_count > 0); 37062306a36Sopenharmony_ci 37162306a36Sopenharmony_ci mutex_lock(&ubi->buf_mutex); 37262306a36Sopenharmony_ci for (i = 0; i < patt_count; i++) { 37362306a36Sopenharmony_ci err = do_sync_erase(ubi, pnum); 37462306a36Sopenharmony_ci if (err) 37562306a36Sopenharmony_ci goto out; 37662306a36Sopenharmony_ci 37762306a36Sopenharmony_ci /* Make sure the PEB contains only 0xFF bytes */ 37862306a36Sopenharmony_ci err = ubi_io_read(ubi, ubi->peb_buf, pnum, 0, ubi->peb_size); 37962306a36Sopenharmony_ci if (err) 38062306a36Sopenharmony_ci goto out; 38162306a36Sopenharmony_ci 38262306a36Sopenharmony_ci err = ubi_check_pattern(ubi->peb_buf, 0xFF, ubi->peb_size); 38362306a36Sopenharmony_ci if (err == 0) { 38462306a36Sopenharmony_ci ubi_err(ubi, "erased PEB %d, but a non-0xFF byte found", 38562306a36Sopenharmony_ci pnum); 38662306a36Sopenharmony_ci err = -EIO; 38762306a36Sopenharmony_ci goto out; 38862306a36Sopenharmony_ci } 38962306a36Sopenharmony_ci 39062306a36Sopenharmony_ci /* Write a pattern and check it */ 39162306a36Sopenharmony_ci memset(ubi->peb_buf, patterns[i], ubi->peb_size); 39262306a36Sopenharmony_ci err = ubi_io_write(ubi, ubi->peb_buf, pnum, 0, ubi->peb_size); 39362306a36Sopenharmony_ci if (err) 39462306a36Sopenharmony_ci goto out; 39562306a36Sopenharmony_ci 39662306a36Sopenharmony_ci memset(ubi->peb_buf, ~patterns[i], ubi->peb_size); 39762306a36Sopenharmony_ci err = ubi_io_read(ubi, ubi->peb_buf, pnum, 0, ubi->peb_size); 39862306a36Sopenharmony_ci if (err) 39962306a36Sopenharmony_ci goto out; 40062306a36Sopenharmony_ci 40162306a36Sopenharmony_ci err = ubi_check_pattern(ubi->peb_buf, patterns[i], 40262306a36Sopenharmony_ci ubi->peb_size); 40362306a36Sopenharmony_ci if (err == 0) { 40462306a36Sopenharmony_ci ubi_err(ubi, "pattern %x checking failed for PEB %d", 40562306a36Sopenharmony_ci patterns[i], pnum); 40662306a36Sopenharmony_ci err = -EIO; 40762306a36Sopenharmony_ci goto out; 40862306a36Sopenharmony_ci } 40962306a36Sopenharmony_ci } 41062306a36Sopenharmony_ci 41162306a36Sopenharmony_ci err = patt_count; 41262306a36Sopenharmony_ci ubi_msg(ubi, "PEB %d passed torture test, do not mark it as bad", pnum); 41362306a36Sopenharmony_ci 41462306a36Sopenharmony_ciout: 41562306a36Sopenharmony_ci mutex_unlock(&ubi->buf_mutex); 41662306a36Sopenharmony_ci if (err == UBI_IO_BITFLIPS || mtd_is_eccerr(err)) { 41762306a36Sopenharmony_ci /* 41862306a36Sopenharmony_ci * If a bit-flip or data integrity error was detected, the test 41962306a36Sopenharmony_ci * has not passed because it happened on a freshly erased 42062306a36Sopenharmony_ci * physical eraseblock which means something is wrong with it. 42162306a36Sopenharmony_ci */ 42262306a36Sopenharmony_ci ubi_err(ubi, "read problems on freshly erased PEB %d, must be bad", 42362306a36Sopenharmony_ci pnum); 42462306a36Sopenharmony_ci err = -EIO; 42562306a36Sopenharmony_ci } 42662306a36Sopenharmony_ci return err; 42762306a36Sopenharmony_ci} 42862306a36Sopenharmony_ci 42962306a36Sopenharmony_ci/** 43062306a36Sopenharmony_ci * nor_erase_prepare - prepare a NOR flash PEB for erasure. 43162306a36Sopenharmony_ci * @ubi: UBI device description object 43262306a36Sopenharmony_ci * @pnum: physical eraseblock number to prepare 43362306a36Sopenharmony_ci * 43462306a36Sopenharmony_ci * NOR flash, or at least some of them, have peculiar embedded PEB erasure 43562306a36Sopenharmony_ci * algorithm: the PEB is first filled with zeroes, then it is erased. And 43662306a36Sopenharmony_ci * filling with zeroes starts from the end of the PEB. This was observed with 43762306a36Sopenharmony_ci * Spansion S29GL512N NOR flash. 43862306a36Sopenharmony_ci * 43962306a36Sopenharmony_ci * This means that in case of a power cut we may end up with intact data at the 44062306a36Sopenharmony_ci * beginning of the PEB, and all zeroes at the end of PEB. In other words, the 44162306a36Sopenharmony_ci * EC and VID headers are OK, but a large chunk of data at the end of PEB is 44262306a36Sopenharmony_ci * zeroed. This makes UBI mistakenly treat this PEB as used and associate it 44362306a36Sopenharmony_ci * with an LEB, which leads to subsequent failures (e.g., UBIFS fails). 44462306a36Sopenharmony_ci * 44562306a36Sopenharmony_ci * This function is called before erasing NOR PEBs and it zeroes out EC and VID 44662306a36Sopenharmony_ci * magic numbers in order to invalidate them and prevent the failures. Returns 44762306a36Sopenharmony_ci * zero in case of success and a negative error code in case of failure. 44862306a36Sopenharmony_ci */ 44962306a36Sopenharmony_cistatic int nor_erase_prepare(struct ubi_device *ubi, int pnum) 45062306a36Sopenharmony_ci{ 45162306a36Sopenharmony_ci int err; 45262306a36Sopenharmony_ci size_t written; 45362306a36Sopenharmony_ci loff_t addr; 45462306a36Sopenharmony_ci uint32_t data = 0; 45562306a36Sopenharmony_ci struct ubi_ec_hdr ec_hdr; 45662306a36Sopenharmony_ci struct ubi_vid_io_buf vidb; 45762306a36Sopenharmony_ci 45862306a36Sopenharmony_ci /* 45962306a36Sopenharmony_ci * Note, we cannot generally define VID header buffers on stack, 46062306a36Sopenharmony_ci * because of the way we deal with these buffers (see the header 46162306a36Sopenharmony_ci * comment in this file). But we know this is a NOR-specific piece of 46262306a36Sopenharmony_ci * code, so we can do this. But yes, this is error-prone and we should 46362306a36Sopenharmony_ci * (pre-)allocate VID header buffer instead. 46462306a36Sopenharmony_ci */ 46562306a36Sopenharmony_ci struct ubi_vid_hdr vid_hdr; 46662306a36Sopenharmony_ci 46762306a36Sopenharmony_ci /* 46862306a36Sopenharmony_ci * If VID or EC is valid, we have to corrupt them before erasing. 46962306a36Sopenharmony_ci * It is important to first invalidate the EC header, and then the VID 47062306a36Sopenharmony_ci * header. Otherwise a power cut may lead to valid EC header and 47162306a36Sopenharmony_ci * invalid VID header, in which case UBI will treat this PEB as 47262306a36Sopenharmony_ci * corrupted and will try to preserve it, and print scary warnings. 47362306a36Sopenharmony_ci */ 47462306a36Sopenharmony_ci addr = (loff_t)pnum * ubi->peb_size; 47562306a36Sopenharmony_ci err = ubi_io_read_ec_hdr(ubi, pnum, &ec_hdr, 0); 47662306a36Sopenharmony_ci if (err != UBI_IO_BAD_HDR_EBADMSG && err != UBI_IO_BAD_HDR && 47762306a36Sopenharmony_ci err != UBI_IO_FF){ 47862306a36Sopenharmony_ci err = mtd_write(ubi->mtd, addr, 4, &written, (void *)&data); 47962306a36Sopenharmony_ci if(err) 48062306a36Sopenharmony_ci goto error; 48162306a36Sopenharmony_ci } 48262306a36Sopenharmony_ci 48362306a36Sopenharmony_ci ubi_init_vid_buf(ubi, &vidb, &vid_hdr); 48462306a36Sopenharmony_ci ubi_assert(&vid_hdr == ubi_get_vid_hdr(&vidb)); 48562306a36Sopenharmony_ci 48662306a36Sopenharmony_ci err = ubi_io_read_vid_hdr(ubi, pnum, &vidb, 0); 48762306a36Sopenharmony_ci if (err != UBI_IO_BAD_HDR_EBADMSG && err != UBI_IO_BAD_HDR && 48862306a36Sopenharmony_ci err != UBI_IO_FF){ 48962306a36Sopenharmony_ci addr += ubi->vid_hdr_aloffset; 49062306a36Sopenharmony_ci err = mtd_write(ubi->mtd, addr, 4, &written, (void *)&data); 49162306a36Sopenharmony_ci if (err) 49262306a36Sopenharmony_ci goto error; 49362306a36Sopenharmony_ci } 49462306a36Sopenharmony_ci return 0; 49562306a36Sopenharmony_ci 49662306a36Sopenharmony_cierror: 49762306a36Sopenharmony_ci /* 49862306a36Sopenharmony_ci * The PEB contains a valid VID or EC header, but we cannot invalidate 49962306a36Sopenharmony_ci * it. Supposedly the flash media or the driver is screwed up, so 50062306a36Sopenharmony_ci * return an error. 50162306a36Sopenharmony_ci */ 50262306a36Sopenharmony_ci ubi_err(ubi, "cannot invalidate PEB %d, write returned %d", pnum, err); 50362306a36Sopenharmony_ci ubi_dump_flash(ubi, pnum, 0, ubi->peb_size); 50462306a36Sopenharmony_ci return -EIO; 50562306a36Sopenharmony_ci} 50662306a36Sopenharmony_ci 50762306a36Sopenharmony_ci/** 50862306a36Sopenharmony_ci * ubi_io_sync_erase - synchronously erase a physical eraseblock. 50962306a36Sopenharmony_ci * @ubi: UBI device description object 51062306a36Sopenharmony_ci * @pnum: physical eraseblock number to erase 51162306a36Sopenharmony_ci * @torture: if this physical eraseblock has to be tortured 51262306a36Sopenharmony_ci * 51362306a36Sopenharmony_ci * This function synchronously erases physical eraseblock @pnum. If @torture 51462306a36Sopenharmony_ci * flag is not zero, the physical eraseblock is checked by means of writing 51562306a36Sopenharmony_ci * different patterns to it and reading them back. If the torturing is enabled, 51662306a36Sopenharmony_ci * the physical eraseblock is erased more than once. 51762306a36Sopenharmony_ci * 51862306a36Sopenharmony_ci * This function returns the number of erasures made in case of success, %-EIO 51962306a36Sopenharmony_ci * if the erasure failed or the torturing test failed, and other negative error 52062306a36Sopenharmony_ci * codes in case of other errors. Note, %-EIO means that the physical 52162306a36Sopenharmony_ci * eraseblock is bad. 52262306a36Sopenharmony_ci */ 52362306a36Sopenharmony_ciint ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture) 52462306a36Sopenharmony_ci{ 52562306a36Sopenharmony_ci int err, ret = 0; 52662306a36Sopenharmony_ci 52762306a36Sopenharmony_ci ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 52862306a36Sopenharmony_ci 52962306a36Sopenharmony_ci err = self_check_not_bad(ubi, pnum); 53062306a36Sopenharmony_ci if (err != 0) 53162306a36Sopenharmony_ci return err; 53262306a36Sopenharmony_ci 53362306a36Sopenharmony_ci if (ubi->ro_mode) { 53462306a36Sopenharmony_ci ubi_err(ubi, "read-only mode"); 53562306a36Sopenharmony_ci return -EROFS; 53662306a36Sopenharmony_ci } 53762306a36Sopenharmony_ci 53862306a36Sopenharmony_ci /* 53962306a36Sopenharmony_ci * If the flash is ECC-ed then we have to erase the ECC block before we 54062306a36Sopenharmony_ci * can write to it. But the write is in preparation to an erase in the 54162306a36Sopenharmony_ci * first place. This means we cannot zero out EC and VID before the 54262306a36Sopenharmony_ci * erase and we just have to hope the flash starts erasing from the 54362306a36Sopenharmony_ci * start of the page. 54462306a36Sopenharmony_ci */ 54562306a36Sopenharmony_ci if (ubi->nor_flash && ubi->mtd->writesize == 1) { 54662306a36Sopenharmony_ci err = nor_erase_prepare(ubi, pnum); 54762306a36Sopenharmony_ci if (err) 54862306a36Sopenharmony_ci return err; 54962306a36Sopenharmony_ci } 55062306a36Sopenharmony_ci 55162306a36Sopenharmony_ci if (torture) { 55262306a36Sopenharmony_ci ret = torture_peb(ubi, pnum); 55362306a36Sopenharmony_ci if (ret < 0) 55462306a36Sopenharmony_ci return ret; 55562306a36Sopenharmony_ci } 55662306a36Sopenharmony_ci 55762306a36Sopenharmony_ci err = do_sync_erase(ubi, pnum); 55862306a36Sopenharmony_ci if (err) 55962306a36Sopenharmony_ci return err; 56062306a36Sopenharmony_ci 56162306a36Sopenharmony_ci return ret + 1; 56262306a36Sopenharmony_ci} 56362306a36Sopenharmony_ci 56462306a36Sopenharmony_ci/** 56562306a36Sopenharmony_ci * ubi_io_is_bad - check if a physical eraseblock is bad. 56662306a36Sopenharmony_ci * @ubi: UBI device description object 56762306a36Sopenharmony_ci * @pnum: the physical eraseblock number to check 56862306a36Sopenharmony_ci * 56962306a36Sopenharmony_ci * This function returns a positive number if the physical eraseblock is bad, 57062306a36Sopenharmony_ci * zero if not, and a negative error code if an error occurred. 57162306a36Sopenharmony_ci */ 57262306a36Sopenharmony_ciint ubi_io_is_bad(const struct ubi_device *ubi, int pnum) 57362306a36Sopenharmony_ci{ 57462306a36Sopenharmony_ci struct mtd_info *mtd = ubi->mtd; 57562306a36Sopenharmony_ci 57662306a36Sopenharmony_ci ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 57762306a36Sopenharmony_ci 57862306a36Sopenharmony_ci if (ubi->bad_allowed) { 57962306a36Sopenharmony_ci int ret; 58062306a36Sopenharmony_ci 58162306a36Sopenharmony_ci ret = mtd_block_isbad(mtd, (loff_t)pnum * ubi->peb_size); 58262306a36Sopenharmony_ci if (ret < 0) 58362306a36Sopenharmony_ci ubi_err(ubi, "error %d while checking if PEB %d is bad", 58462306a36Sopenharmony_ci ret, pnum); 58562306a36Sopenharmony_ci else if (ret) 58662306a36Sopenharmony_ci dbg_io("PEB %d is bad", pnum); 58762306a36Sopenharmony_ci return ret; 58862306a36Sopenharmony_ci } 58962306a36Sopenharmony_ci 59062306a36Sopenharmony_ci return 0; 59162306a36Sopenharmony_ci} 59262306a36Sopenharmony_ci 59362306a36Sopenharmony_ci/** 59462306a36Sopenharmony_ci * ubi_io_mark_bad - mark a physical eraseblock as bad. 59562306a36Sopenharmony_ci * @ubi: UBI device description object 59662306a36Sopenharmony_ci * @pnum: the physical eraseblock number to mark 59762306a36Sopenharmony_ci * 59862306a36Sopenharmony_ci * This function returns zero in case of success and a negative error code in 59962306a36Sopenharmony_ci * case of failure. 60062306a36Sopenharmony_ci */ 60162306a36Sopenharmony_ciint ubi_io_mark_bad(const struct ubi_device *ubi, int pnum) 60262306a36Sopenharmony_ci{ 60362306a36Sopenharmony_ci int err; 60462306a36Sopenharmony_ci struct mtd_info *mtd = ubi->mtd; 60562306a36Sopenharmony_ci 60662306a36Sopenharmony_ci ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 60762306a36Sopenharmony_ci 60862306a36Sopenharmony_ci if (ubi->ro_mode) { 60962306a36Sopenharmony_ci ubi_err(ubi, "read-only mode"); 61062306a36Sopenharmony_ci return -EROFS; 61162306a36Sopenharmony_ci } 61262306a36Sopenharmony_ci 61362306a36Sopenharmony_ci if (!ubi->bad_allowed) 61462306a36Sopenharmony_ci return 0; 61562306a36Sopenharmony_ci 61662306a36Sopenharmony_ci err = mtd_block_markbad(mtd, (loff_t)pnum * ubi->peb_size); 61762306a36Sopenharmony_ci if (err) 61862306a36Sopenharmony_ci ubi_err(ubi, "cannot mark PEB %d bad, error %d", pnum, err); 61962306a36Sopenharmony_ci return err; 62062306a36Sopenharmony_ci} 62162306a36Sopenharmony_ci 62262306a36Sopenharmony_ci/** 62362306a36Sopenharmony_ci * validate_ec_hdr - validate an erase counter header. 62462306a36Sopenharmony_ci * @ubi: UBI device description object 62562306a36Sopenharmony_ci * @ec_hdr: the erase counter header to check 62662306a36Sopenharmony_ci * 62762306a36Sopenharmony_ci * This function returns zero if the erase counter header is OK, and %1 if 62862306a36Sopenharmony_ci * not. 62962306a36Sopenharmony_ci */ 63062306a36Sopenharmony_cistatic int validate_ec_hdr(const struct ubi_device *ubi, 63162306a36Sopenharmony_ci const struct ubi_ec_hdr *ec_hdr) 63262306a36Sopenharmony_ci{ 63362306a36Sopenharmony_ci long long ec; 63462306a36Sopenharmony_ci int vid_hdr_offset, leb_start; 63562306a36Sopenharmony_ci 63662306a36Sopenharmony_ci ec = be64_to_cpu(ec_hdr->ec); 63762306a36Sopenharmony_ci vid_hdr_offset = be32_to_cpu(ec_hdr->vid_hdr_offset); 63862306a36Sopenharmony_ci leb_start = be32_to_cpu(ec_hdr->data_offset); 63962306a36Sopenharmony_ci 64062306a36Sopenharmony_ci if (ec_hdr->version != UBI_VERSION) { 64162306a36Sopenharmony_ci ubi_err(ubi, "node with incompatible UBI version found: this UBI version is %d, image version is %d", 64262306a36Sopenharmony_ci UBI_VERSION, (int)ec_hdr->version); 64362306a36Sopenharmony_ci goto bad; 64462306a36Sopenharmony_ci } 64562306a36Sopenharmony_ci 64662306a36Sopenharmony_ci if (vid_hdr_offset != ubi->vid_hdr_offset) { 64762306a36Sopenharmony_ci ubi_err(ubi, "bad VID header offset %d, expected %d", 64862306a36Sopenharmony_ci vid_hdr_offset, ubi->vid_hdr_offset); 64962306a36Sopenharmony_ci goto bad; 65062306a36Sopenharmony_ci } 65162306a36Sopenharmony_ci 65262306a36Sopenharmony_ci if (leb_start != ubi->leb_start) { 65362306a36Sopenharmony_ci ubi_err(ubi, "bad data offset %d, expected %d", 65462306a36Sopenharmony_ci leb_start, ubi->leb_start); 65562306a36Sopenharmony_ci goto bad; 65662306a36Sopenharmony_ci } 65762306a36Sopenharmony_ci 65862306a36Sopenharmony_ci if (ec < 0 || ec > UBI_MAX_ERASECOUNTER) { 65962306a36Sopenharmony_ci ubi_err(ubi, "bad erase counter %lld", ec); 66062306a36Sopenharmony_ci goto bad; 66162306a36Sopenharmony_ci } 66262306a36Sopenharmony_ci 66362306a36Sopenharmony_ci return 0; 66462306a36Sopenharmony_ci 66562306a36Sopenharmony_cibad: 66662306a36Sopenharmony_ci ubi_err(ubi, "bad EC header"); 66762306a36Sopenharmony_ci ubi_dump_ec_hdr(ec_hdr); 66862306a36Sopenharmony_ci dump_stack(); 66962306a36Sopenharmony_ci return 1; 67062306a36Sopenharmony_ci} 67162306a36Sopenharmony_ci 67262306a36Sopenharmony_ci/** 67362306a36Sopenharmony_ci * ubi_io_read_ec_hdr - read and check an erase counter header. 67462306a36Sopenharmony_ci * @ubi: UBI device description object 67562306a36Sopenharmony_ci * @pnum: physical eraseblock to read from 67662306a36Sopenharmony_ci * @ec_hdr: a &struct ubi_ec_hdr object where to store the read erase counter 67762306a36Sopenharmony_ci * header 67862306a36Sopenharmony_ci * @verbose: be verbose if the header is corrupted or was not found 67962306a36Sopenharmony_ci * 68062306a36Sopenharmony_ci * This function reads erase counter header from physical eraseblock @pnum and 68162306a36Sopenharmony_ci * stores it in @ec_hdr. This function also checks CRC checksum of the read 68262306a36Sopenharmony_ci * erase counter header. The following codes may be returned: 68362306a36Sopenharmony_ci * 68462306a36Sopenharmony_ci * o %0 if the CRC checksum is correct and the header was successfully read; 68562306a36Sopenharmony_ci * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected 68662306a36Sopenharmony_ci * and corrected by the flash driver; this is harmless but may indicate that 68762306a36Sopenharmony_ci * this eraseblock may become bad soon (but may be not); 68862306a36Sopenharmony_ci * o %UBI_IO_BAD_HDR if the erase counter header is corrupted (a CRC error); 68962306a36Sopenharmony_ci * o %UBI_IO_BAD_HDR_EBADMSG is the same as %UBI_IO_BAD_HDR, but there also was 69062306a36Sopenharmony_ci * a data integrity error (uncorrectable ECC error in case of NAND); 69162306a36Sopenharmony_ci * o %UBI_IO_FF if only 0xFF bytes were read (the PEB is supposedly empty) 69262306a36Sopenharmony_ci * o a negative error code in case of failure. 69362306a36Sopenharmony_ci */ 69462306a36Sopenharmony_ciint ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum, 69562306a36Sopenharmony_ci struct ubi_ec_hdr *ec_hdr, int verbose) 69662306a36Sopenharmony_ci{ 69762306a36Sopenharmony_ci int err, read_err; 69862306a36Sopenharmony_ci uint32_t crc, magic, hdr_crc; 69962306a36Sopenharmony_ci 70062306a36Sopenharmony_ci dbg_io("read EC header from PEB %d", pnum); 70162306a36Sopenharmony_ci ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 70262306a36Sopenharmony_ci 70362306a36Sopenharmony_ci read_err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE); 70462306a36Sopenharmony_ci if (read_err) { 70562306a36Sopenharmony_ci if (read_err != UBI_IO_BITFLIPS && !mtd_is_eccerr(read_err)) 70662306a36Sopenharmony_ci return read_err; 70762306a36Sopenharmony_ci 70862306a36Sopenharmony_ci /* 70962306a36Sopenharmony_ci * We read all the data, but either a correctable bit-flip 71062306a36Sopenharmony_ci * occurred, or MTD reported a data integrity error 71162306a36Sopenharmony_ci * (uncorrectable ECC error in case of NAND). The former is 71262306a36Sopenharmony_ci * harmless, the later may mean that the read data is 71362306a36Sopenharmony_ci * corrupted. But we have a CRC check-sum and we will detect 71462306a36Sopenharmony_ci * this. If the EC header is still OK, we just report this as 71562306a36Sopenharmony_ci * there was a bit-flip, to force scrubbing. 71662306a36Sopenharmony_ci */ 71762306a36Sopenharmony_ci } 71862306a36Sopenharmony_ci 71962306a36Sopenharmony_ci magic = be32_to_cpu(ec_hdr->magic); 72062306a36Sopenharmony_ci if (magic != UBI_EC_HDR_MAGIC) { 72162306a36Sopenharmony_ci if (mtd_is_eccerr(read_err)) 72262306a36Sopenharmony_ci return UBI_IO_BAD_HDR_EBADMSG; 72362306a36Sopenharmony_ci 72462306a36Sopenharmony_ci /* 72562306a36Sopenharmony_ci * The magic field is wrong. Let's check if we have read all 72662306a36Sopenharmony_ci * 0xFF. If yes, this physical eraseblock is assumed to be 72762306a36Sopenharmony_ci * empty. 72862306a36Sopenharmony_ci */ 72962306a36Sopenharmony_ci if (ubi_check_pattern(ec_hdr, 0xFF, UBI_EC_HDR_SIZE)) { 73062306a36Sopenharmony_ci /* The physical eraseblock is supposedly empty */ 73162306a36Sopenharmony_ci if (verbose) 73262306a36Sopenharmony_ci ubi_warn(ubi, "no EC header found at PEB %d, only 0xFF bytes", 73362306a36Sopenharmony_ci pnum); 73462306a36Sopenharmony_ci dbg_bld("no EC header found at PEB %d, only 0xFF bytes", 73562306a36Sopenharmony_ci pnum); 73662306a36Sopenharmony_ci if (!read_err) 73762306a36Sopenharmony_ci return UBI_IO_FF; 73862306a36Sopenharmony_ci else 73962306a36Sopenharmony_ci return UBI_IO_FF_BITFLIPS; 74062306a36Sopenharmony_ci } 74162306a36Sopenharmony_ci 74262306a36Sopenharmony_ci /* 74362306a36Sopenharmony_ci * This is not a valid erase counter header, and these are not 74462306a36Sopenharmony_ci * 0xFF bytes. Report that the header is corrupted. 74562306a36Sopenharmony_ci */ 74662306a36Sopenharmony_ci if (verbose) { 74762306a36Sopenharmony_ci ubi_warn(ubi, "bad magic number at PEB %d: %08x instead of %08x", 74862306a36Sopenharmony_ci pnum, magic, UBI_EC_HDR_MAGIC); 74962306a36Sopenharmony_ci ubi_dump_ec_hdr(ec_hdr); 75062306a36Sopenharmony_ci } 75162306a36Sopenharmony_ci dbg_bld("bad magic number at PEB %d: %08x instead of %08x", 75262306a36Sopenharmony_ci pnum, magic, UBI_EC_HDR_MAGIC); 75362306a36Sopenharmony_ci return UBI_IO_BAD_HDR; 75462306a36Sopenharmony_ci } 75562306a36Sopenharmony_ci 75662306a36Sopenharmony_ci crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC); 75762306a36Sopenharmony_ci hdr_crc = be32_to_cpu(ec_hdr->hdr_crc); 75862306a36Sopenharmony_ci 75962306a36Sopenharmony_ci if (hdr_crc != crc) { 76062306a36Sopenharmony_ci if (verbose) { 76162306a36Sopenharmony_ci ubi_warn(ubi, "bad EC header CRC at PEB %d, calculated %#08x, read %#08x", 76262306a36Sopenharmony_ci pnum, crc, hdr_crc); 76362306a36Sopenharmony_ci ubi_dump_ec_hdr(ec_hdr); 76462306a36Sopenharmony_ci } 76562306a36Sopenharmony_ci dbg_bld("bad EC header CRC at PEB %d, calculated %#08x, read %#08x", 76662306a36Sopenharmony_ci pnum, crc, hdr_crc); 76762306a36Sopenharmony_ci 76862306a36Sopenharmony_ci if (!read_err) 76962306a36Sopenharmony_ci return UBI_IO_BAD_HDR; 77062306a36Sopenharmony_ci else 77162306a36Sopenharmony_ci return UBI_IO_BAD_HDR_EBADMSG; 77262306a36Sopenharmony_ci } 77362306a36Sopenharmony_ci 77462306a36Sopenharmony_ci /* And of course validate what has just been read from the media */ 77562306a36Sopenharmony_ci err = validate_ec_hdr(ubi, ec_hdr); 77662306a36Sopenharmony_ci if (err) { 77762306a36Sopenharmony_ci ubi_err(ubi, "validation failed for PEB %d", pnum); 77862306a36Sopenharmony_ci return -EINVAL; 77962306a36Sopenharmony_ci } 78062306a36Sopenharmony_ci 78162306a36Sopenharmony_ci /* 78262306a36Sopenharmony_ci * If there was %-EBADMSG, but the header CRC is still OK, report about 78362306a36Sopenharmony_ci * a bit-flip to force scrubbing on this PEB. 78462306a36Sopenharmony_ci */ 78562306a36Sopenharmony_ci return read_err ? UBI_IO_BITFLIPS : 0; 78662306a36Sopenharmony_ci} 78762306a36Sopenharmony_ci 78862306a36Sopenharmony_ci/** 78962306a36Sopenharmony_ci * ubi_io_write_ec_hdr - write an erase counter header. 79062306a36Sopenharmony_ci * @ubi: UBI device description object 79162306a36Sopenharmony_ci * @pnum: physical eraseblock to write to 79262306a36Sopenharmony_ci * @ec_hdr: the erase counter header to write 79362306a36Sopenharmony_ci * 79462306a36Sopenharmony_ci * This function writes erase counter header described by @ec_hdr to physical 79562306a36Sopenharmony_ci * eraseblock @pnum. It also fills most fields of @ec_hdr before writing, so 79662306a36Sopenharmony_ci * the caller do not have to fill them. Callers must only fill the @ec_hdr->ec 79762306a36Sopenharmony_ci * field. 79862306a36Sopenharmony_ci * 79962306a36Sopenharmony_ci * This function returns zero in case of success and a negative error code in 80062306a36Sopenharmony_ci * case of failure. If %-EIO is returned, the physical eraseblock most probably 80162306a36Sopenharmony_ci * went bad. 80262306a36Sopenharmony_ci */ 80362306a36Sopenharmony_ciint ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum, 80462306a36Sopenharmony_ci struct ubi_ec_hdr *ec_hdr) 80562306a36Sopenharmony_ci{ 80662306a36Sopenharmony_ci int err; 80762306a36Sopenharmony_ci uint32_t crc; 80862306a36Sopenharmony_ci 80962306a36Sopenharmony_ci dbg_io("write EC header to PEB %d", pnum); 81062306a36Sopenharmony_ci ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 81162306a36Sopenharmony_ci 81262306a36Sopenharmony_ci ec_hdr->magic = cpu_to_be32(UBI_EC_HDR_MAGIC); 81362306a36Sopenharmony_ci ec_hdr->version = UBI_VERSION; 81462306a36Sopenharmony_ci ec_hdr->vid_hdr_offset = cpu_to_be32(ubi->vid_hdr_offset); 81562306a36Sopenharmony_ci ec_hdr->data_offset = cpu_to_be32(ubi->leb_start); 81662306a36Sopenharmony_ci ec_hdr->image_seq = cpu_to_be32(ubi->image_seq); 81762306a36Sopenharmony_ci crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC); 81862306a36Sopenharmony_ci ec_hdr->hdr_crc = cpu_to_be32(crc); 81962306a36Sopenharmony_ci 82062306a36Sopenharmony_ci err = self_check_ec_hdr(ubi, pnum, ec_hdr); 82162306a36Sopenharmony_ci if (err) 82262306a36Sopenharmony_ci return err; 82362306a36Sopenharmony_ci 82462306a36Sopenharmony_ci if (ubi_dbg_power_cut(ubi, POWER_CUT_EC_WRITE)) 82562306a36Sopenharmony_ci return -EROFS; 82662306a36Sopenharmony_ci 82762306a36Sopenharmony_ci err = ubi_io_write(ubi, ec_hdr, pnum, 0, ubi->ec_hdr_alsize); 82862306a36Sopenharmony_ci return err; 82962306a36Sopenharmony_ci} 83062306a36Sopenharmony_ci 83162306a36Sopenharmony_ci/** 83262306a36Sopenharmony_ci * validate_vid_hdr - validate a volume identifier header. 83362306a36Sopenharmony_ci * @ubi: UBI device description object 83462306a36Sopenharmony_ci * @vid_hdr: the volume identifier header to check 83562306a36Sopenharmony_ci * 83662306a36Sopenharmony_ci * This function checks that data stored in the volume identifier header 83762306a36Sopenharmony_ci * @vid_hdr. Returns zero if the VID header is OK and %1 if not. 83862306a36Sopenharmony_ci */ 83962306a36Sopenharmony_cistatic int validate_vid_hdr(const struct ubi_device *ubi, 84062306a36Sopenharmony_ci const struct ubi_vid_hdr *vid_hdr) 84162306a36Sopenharmony_ci{ 84262306a36Sopenharmony_ci int vol_type = vid_hdr->vol_type; 84362306a36Sopenharmony_ci int copy_flag = vid_hdr->copy_flag; 84462306a36Sopenharmony_ci int vol_id = be32_to_cpu(vid_hdr->vol_id); 84562306a36Sopenharmony_ci int lnum = be32_to_cpu(vid_hdr->lnum); 84662306a36Sopenharmony_ci int compat = vid_hdr->compat; 84762306a36Sopenharmony_ci int data_size = be32_to_cpu(vid_hdr->data_size); 84862306a36Sopenharmony_ci int used_ebs = be32_to_cpu(vid_hdr->used_ebs); 84962306a36Sopenharmony_ci int data_pad = be32_to_cpu(vid_hdr->data_pad); 85062306a36Sopenharmony_ci int data_crc = be32_to_cpu(vid_hdr->data_crc); 85162306a36Sopenharmony_ci int usable_leb_size = ubi->leb_size - data_pad; 85262306a36Sopenharmony_ci 85362306a36Sopenharmony_ci if (copy_flag != 0 && copy_flag != 1) { 85462306a36Sopenharmony_ci ubi_err(ubi, "bad copy_flag"); 85562306a36Sopenharmony_ci goto bad; 85662306a36Sopenharmony_ci } 85762306a36Sopenharmony_ci 85862306a36Sopenharmony_ci if (vol_id < 0 || lnum < 0 || data_size < 0 || used_ebs < 0 || 85962306a36Sopenharmony_ci data_pad < 0) { 86062306a36Sopenharmony_ci ubi_err(ubi, "negative values"); 86162306a36Sopenharmony_ci goto bad; 86262306a36Sopenharmony_ci } 86362306a36Sopenharmony_ci 86462306a36Sopenharmony_ci if (vol_id >= UBI_MAX_VOLUMES && vol_id < UBI_INTERNAL_VOL_START) { 86562306a36Sopenharmony_ci ubi_err(ubi, "bad vol_id"); 86662306a36Sopenharmony_ci goto bad; 86762306a36Sopenharmony_ci } 86862306a36Sopenharmony_ci 86962306a36Sopenharmony_ci if (vol_id < UBI_INTERNAL_VOL_START && compat != 0) { 87062306a36Sopenharmony_ci ubi_err(ubi, "bad compat"); 87162306a36Sopenharmony_ci goto bad; 87262306a36Sopenharmony_ci } 87362306a36Sopenharmony_ci 87462306a36Sopenharmony_ci if (vol_id >= UBI_INTERNAL_VOL_START && compat != UBI_COMPAT_DELETE && 87562306a36Sopenharmony_ci compat != UBI_COMPAT_RO && compat != UBI_COMPAT_PRESERVE && 87662306a36Sopenharmony_ci compat != UBI_COMPAT_REJECT) { 87762306a36Sopenharmony_ci ubi_err(ubi, "bad compat"); 87862306a36Sopenharmony_ci goto bad; 87962306a36Sopenharmony_ci } 88062306a36Sopenharmony_ci 88162306a36Sopenharmony_ci if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) { 88262306a36Sopenharmony_ci ubi_err(ubi, "bad vol_type"); 88362306a36Sopenharmony_ci goto bad; 88462306a36Sopenharmony_ci } 88562306a36Sopenharmony_ci 88662306a36Sopenharmony_ci if (data_pad >= ubi->leb_size / 2) { 88762306a36Sopenharmony_ci ubi_err(ubi, "bad data_pad"); 88862306a36Sopenharmony_ci goto bad; 88962306a36Sopenharmony_ci } 89062306a36Sopenharmony_ci 89162306a36Sopenharmony_ci if (data_size > ubi->leb_size) { 89262306a36Sopenharmony_ci ubi_err(ubi, "bad data_size"); 89362306a36Sopenharmony_ci goto bad; 89462306a36Sopenharmony_ci } 89562306a36Sopenharmony_ci 89662306a36Sopenharmony_ci if (vol_type == UBI_VID_STATIC) { 89762306a36Sopenharmony_ci /* 89862306a36Sopenharmony_ci * Although from high-level point of view static volumes may 89962306a36Sopenharmony_ci * contain zero bytes of data, but no VID headers can contain 90062306a36Sopenharmony_ci * zero at these fields, because they empty volumes do not have 90162306a36Sopenharmony_ci * mapped logical eraseblocks. 90262306a36Sopenharmony_ci */ 90362306a36Sopenharmony_ci if (used_ebs == 0) { 90462306a36Sopenharmony_ci ubi_err(ubi, "zero used_ebs"); 90562306a36Sopenharmony_ci goto bad; 90662306a36Sopenharmony_ci } 90762306a36Sopenharmony_ci if (data_size == 0) { 90862306a36Sopenharmony_ci ubi_err(ubi, "zero data_size"); 90962306a36Sopenharmony_ci goto bad; 91062306a36Sopenharmony_ci } 91162306a36Sopenharmony_ci if (lnum < used_ebs - 1) { 91262306a36Sopenharmony_ci if (data_size != usable_leb_size) { 91362306a36Sopenharmony_ci ubi_err(ubi, "bad data_size"); 91462306a36Sopenharmony_ci goto bad; 91562306a36Sopenharmony_ci } 91662306a36Sopenharmony_ci } else if (lnum > used_ebs - 1) { 91762306a36Sopenharmony_ci ubi_err(ubi, "too high lnum"); 91862306a36Sopenharmony_ci goto bad; 91962306a36Sopenharmony_ci } 92062306a36Sopenharmony_ci } else { 92162306a36Sopenharmony_ci if (copy_flag == 0) { 92262306a36Sopenharmony_ci if (data_crc != 0) { 92362306a36Sopenharmony_ci ubi_err(ubi, "non-zero data CRC"); 92462306a36Sopenharmony_ci goto bad; 92562306a36Sopenharmony_ci } 92662306a36Sopenharmony_ci if (data_size != 0) { 92762306a36Sopenharmony_ci ubi_err(ubi, "non-zero data_size"); 92862306a36Sopenharmony_ci goto bad; 92962306a36Sopenharmony_ci } 93062306a36Sopenharmony_ci } else { 93162306a36Sopenharmony_ci if (data_size == 0) { 93262306a36Sopenharmony_ci ubi_err(ubi, "zero data_size of copy"); 93362306a36Sopenharmony_ci goto bad; 93462306a36Sopenharmony_ci } 93562306a36Sopenharmony_ci } 93662306a36Sopenharmony_ci if (used_ebs != 0) { 93762306a36Sopenharmony_ci ubi_err(ubi, "bad used_ebs"); 93862306a36Sopenharmony_ci goto bad; 93962306a36Sopenharmony_ci } 94062306a36Sopenharmony_ci } 94162306a36Sopenharmony_ci 94262306a36Sopenharmony_ci return 0; 94362306a36Sopenharmony_ci 94462306a36Sopenharmony_cibad: 94562306a36Sopenharmony_ci ubi_err(ubi, "bad VID header"); 94662306a36Sopenharmony_ci ubi_dump_vid_hdr(vid_hdr); 94762306a36Sopenharmony_ci dump_stack(); 94862306a36Sopenharmony_ci return 1; 94962306a36Sopenharmony_ci} 95062306a36Sopenharmony_ci 95162306a36Sopenharmony_ci/** 95262306a36Sopenharmony_ci * ubi_io_read_vid_hdr - read and check a volume identifier header. 95362306a36Sopenharmony_ci * @ubi: UBI device description object 95462306a36Sopenharmony_ci * @pnum: physical eraseblock number to read from 95562306a36Sopenharmony_ci * @vidb: the volume identifier buffer to store data in 95662306a36Sopenharmony_ci * @verbose: be verbose if the header is corrupted or wasn't found 95762306a36Sopenharmony_ci * 95862306a36Sopenharmony_ci * This function reads the volume identifier header from physical eraseblock 95962306a36Sopenharmony_ci * @pnum and stores it in @vidb. It also checks CRC checksum of the read 96062306a36Sopenharmony_ci * volume identifier header. The error codes are the same as in 96162306a36Sopenharmony_ci * 'ubi_io_read_ec_hdr()'. 96262306a36Sopenharmony_ci * 96362306a36Sopenharmony_ci * Note, the implementation of this function is also very similar to 96462306a36Sopenharmony_ci * 'ubi_io_read_ec_hdr()', so refer commentaries in 'ubi_io_read_ec_hdr()'. 96562306a36Sopenharmony_ci */ 96662306a36Sopenharmony_ciint ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum, 96762306a36Sopenharmony_ci struct ubi_vid_io_buf *vidb, int verbose) 96862306a36Sopenharmony_ci{ 96962306a36Sopenharmony_ci int err, read_err; 97062306a36Sopenharmony_ci uint32_t crc, magic, hdr_crc; 97162306a36Sopenharmony_ci struct ubi_vid_hdr *vid_hdr = ubi_get_vid_hdr(vidb); 97262306a36Sopenharmony_ci void *p = vidb->buffer; 97362306a36Sopenharmony_ci 97462306a36Sopenharmony_ci dbg_io("read VID header from PEB %d", pnum); 97562306a36Sopenharmony_ci ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 97662306a36Sopenharmony_ci 97762306a36Sopenharmony_ci read_err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset, 97862306a36Sopenharmony_ci ubi->vid_hdr_shift + UBI_VID_HDR_SIZE); 97962306a36Sopenharmony_ci if (read_err && read_err != UBI_IO_BITFLIPS && !mtd_is_eccerr(read_err)) 98062306a36Sopenharmony_ci return read_err; 98162306a36Sopenharmony_ci 98262306a36Sopenharmony_ci magic = be32_to_cpu(vid_hdr->magic); 98362306a36Sopenharmony_ci if (magic != UBI_VID_HDR_MAGIC) { 98462306a36Sopenharmony_ci if (mtd_is_eccerr(read_err)) 98562306a36Sopenharmony_ci return UBI_IO_BAD_HDR_EBADMSG; 98662306a36Sopenharmony_ci 98762306a36Sopenharmony_ci if (ubi_check_pattern(vid_hdr, 0xFF, UBI_VID_HDR_SIZE)) { 98862306a36Sopenharmony_ci if (verbose) 98962306a36Sopenharmony_ci ubi_warn(ubi, "no VID header found at PEB %d, only 0xFF bytes", 99062306a36Sopenharmony_ci pnum); 99162306a36Sopenharmony_ci dbg_bld("no VID header found at PEB %d, only 0xFF bytes", 99262306a36Sopenharmony_ci pnum); 99362306a36Sopenharmony_ci if (!read_err) 99462306a36Sopenharmony_ci return UBI_IO_FF; 99562306a36Sopenharmony_ci else 99662306a36Sopenharmony_ci return UBI_IO_FF_BITFLIPS; 99762306a36Sopenharmony_ci } 99862306a36Sopenharmony_ci 99962306a36Sopenharmony_ci if (verbose) { 100062306a36Sopenharmony_ci ubi_warn(ubi, "bad magic number at PEB %d: %08x instead of %08x", 100162306a36Sopenharmony_ci pnum, magic, UBI_VID_HDR_MAGIC); 100262306a36Sopenharmony_ci ubi_dump_vid_hdr(vid_hdr); 100362306a36Sopenharmony_ci } 100462306a36Sopenharmony_ci dbg_bld("bad magic number at PEB %d: %08x instead of %08x", 100562306a36Sopenharmony_ci pnum, magic, UBI_VID_HDR_MAGIC); 100662306a36Sopenharmony_ci return UBI_IO_BAD_HDR; 100762306a36Sopenharmony_ci } 100862306a36Sopenharmony_ci 100962306a36Sopenharmony_ci crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC); 101062306a36Sopenharmony_ci hdr_crc = be32_to_cpu(vid_hdr->hdr_crc); 101162306a36Sopenharmony_ci 101262306a36Sopenharmony_ci if (hdr_crc != crc) { 101362306a36Sopenharmony_ci if (verbose) { 101462306a36Sopenharmony_ci ubi_warn(ubi, "bad CRC at PEB %d, calculated %#08x, read %#08x", 101562306a36Sopenharmony_ci pnum, crc, hdr_crc); 101662306a36Sopenharmony_ci ubi_dump_vid_hdr(vid_hdr); 101762306a36Sopenharmony_ci } 101862306a36Sopenharmony_ci dbg_bld("bad CRC at PEB %d, calculated %#08x, read %#08x", 101962306a36Sopenharmony_ci pnum, crc, hdr_crc); 102062306a36Sopenharmony_ci if (!read_err) 102162306a36Sopenharmony_ci return UBI_IO_BAD_HDR; 102262306a36Sopenharmony_ci else 102362306a36Sopenharmony_ci return UBI_IO_BAD_HDR_EBADMSG; 102462306a36Sopenharmony_ci } 102562306a36Sopenharmony_ci 102662306a36Sopenharmony_ci err = validate_vid_hdr(ubi, vid_hdr); 102762306a36Sopenharmony_ci if (err) { 102862306a36Sopenharmony_ci ubi_err(ubi, "validation failed for PEB %d", pnum); 102962306a36Sopenharmony_ci return -EINVAL; 103062306a36Sopenharmony_ci } 103162306a36Sopenharmony_ci 103262306a36Sopenharmony_ci return read_err ? UBI_IO_BITFLIPS : 0; 103362306a36Sopenharmony_ci} 103462306a36Sopenharmony_ci 103562306a36Sopenharmony_ci/** 103662306a36Sopenharmony_ci * ubi_io_write_vid_hdr - write a volume identifier header. 103762306a36Sopenharmony_ci * @ubi: UBI device description object 103862306a36Sopenharmony_ci * @pnum: the physical eraseblock number to write to 103962306a36Sopenharmony_ci * @vidb: the volume identifier buffer to write 104062306a36Sopenharmony_ci * 104162306a36Sopenharmony_ci * This function writes the volume identifier header described by @vid_hdr to 104262306a36Sopenharmony_ci * physical eraseblock @pnum. This function automatically fills the 104362306a36Sopenharmony_ci * @vidb->hdr->magic and the @vidb->hdr->version fields, as well as calculates 104462306a36Sopenharmony_ci * header CRC checksum and stores it at vidb->hdr->hdr_crc. 104562306a36Sopenharmony_ci * 104662306a36Sopenharmony_ci * This function returns zero in case of success and a negative error code in 104762306a36Sopenharmony_ci * case of failure. If %-EIO is returned, the physical eraseblock probably went 104862306a36Sopenharmony_ci * bad. 104962306a36Sopenharmony_ci */ 105062306a36Sopenharmony_ciint ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum, 105162306a36Sopenharmony_ci struct ubi_vid_io_buf *vidb) 105262306a36Sopenharmony_ci{ 105362306a36Sopenharmony_ci struct ubi_vid_hdr *vid_hdr = ubi_get_vid_hdr(vidb); 105462306a36Sopenharmony_ci int err; 105562306a36Sopenharmony_ci uint32_t crc; 105662306a36Sopenharmony_ci void *p = vidb->buffer; 105762306a36Sopenharmony_ci 105862306a36Sopenharmony_ci dbg_io("write VID header to PEB %d", pnum); 105962306a36Sopenharmony_ci ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 106062306a36Sopenharmony_ci 106162306a36Sopenharmony_ci err = self_check_peb_ec_hdr(ubi, pnum); 106262306a36Sopenharmony_ci if (err) 106362306a36Sopenharmony_ci return err; 106462306a36Sopenharmony_ci 106562306a36Sopenharmony_ci vid_hdr->magic = cpu_to_be32(UBI_VID_HDR_MAGIC); 106662306a36Sopenharmony_ci vid_hdr->version = UBI_VERSION; 106762306a36Sopenharmony_ci crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC); 106862306a36Sopenharmony_ci vid_hdr->hdr_crc = cpu_to_be32(crc); 106962306a36Sopenharmony_ci 107062306a36Sopenharmony_ci err = self_check_vid_hdr(ubi, pnum, vid_hdr); 107162306a36Sopenharmony_ci if (err) 107262306a36Sopenharmony_ci return err; 107362306a36Sopenharmony_ci 107462306a36Sopenharmony_ci if (ubi_dbg_power_cut(ubi, POWER_CUT_VID_WRITE)) 107562306a36Sopenharmony_ci return -EROFS; 107662306a36Sopenharmony_ci 107762306a36Sopenharmony_ci err = ubi_io_write(ubi, p, pnum, ubi->vid_hdr_aloffset, 107862306a36Sopenharmony_ci ubi->vid_hdr_alsize); 107962306a36Sopenharmony_ci return err; 108062306a36Sopenharmony_ci} 108162306a36Sopenharmony_ci 108262306a36Sopenharmony_ci/** 108362306a36Sopenharmony_ci * self_check_not_bad - ensure that a physical eraseblock is not bad. 108462306a36Sopenharmony_ci * @ubi: UBI device description object 108562306a36Sopenharmony_ci * @pnum: physical eraseblock number to check 108662306a36Sopenharmony_ci * 108762306a36Sopenharmony_ci * This function returns zero if the physical eraseblock is good, %-EINVAL if 108862306a36Sopenharmony_ci * it is bad and a negative error code if an error occurred. 108962306a36Sopenharmony_ci */ 109062306a36Sopenharmony_cistatic int self_check_not_bad(const struct ubi_device *ubi, int pnum) 109162306a36Sopenharmony_ci{ 109262306a36Sopenharmony_ci int err; 109362306a36Sopenharmony_ci 109462306a36Sopenharmony_ci if (!ubi_dbg_chk_io(ubi)) 109562306a36Sopenharmony_ci return 0; 109662306a36Sopenharmony_ci 109762306a36Sopenharmony_ci err = ubi_io_is_bad(ubi, pnum); 109862306a36Sopenharmony_ci if (!err) 109962306a36Sopenharmony_ci return err; 110062306a36Sopenharmony_ci 110162306a36Sopenharmony_ci ubi_err(ubi, "self-check failed for PEB %d", pnum); 110262306a36Sopenharmony_ci dump_stack(); 110362306a36Sopenharmony_ci return err > 0 ? -EINVAL : err; 110462306a36Sopenharmony_ci} 110562306a36Sopenharmony_ci 110662306a36Sopenharmony_ci/** 110762306a36Sopenharmony_ci * self_check_ec_hdr - check if an erase counter header is all right. 110862306a36Sopenharmony_ci * @ubi: UBI device description object 110962306a36Sopenharmony_ci * @pnum: physical eraseblock number the erase counter header belongs to 111062306a36Sopenharmony_ci * @ec_hdr: the erase counter header to check 111162306a36Sopenharmony_ci * 111262306a36Sopenharmony_ci * This function returns zero if the erase counter header contains valid 111362306a36Sopenharmony_ci * values, and %-EINVAL if not. 111462306a36Sopenharmony_ci */ 111562306a36Sopenharmony_cistatic int self_check_ec_hdr(const struct ubi_device *ubi, int pnum, 111662306a36Sopenharmony_ci const struct ubi_ec_hdr *ec_hdr) 111762306a36Sopenharmony_ci{ 111862306a36Sopenharmony_ci int err; 111962306a36Sopenharmony_ci uint32_t magic; 112062306a36Sopenharmony_ci 112162306a36Sopenharmony_ci if (!ubi_dbg_chk_io(ubi)) 112262306a36Sopenharmony_ci return 0; 112362306a36Sopenharmony_ci 112462306a36Sopenharmony_ci magic = be32_to_cpu(ec_hdr->magic); 112562306a36Sopenharmony_ci if (magic != UBI_EC_HDR_MAGIC) { 112662306a36Sopenharmony_ci ubi_err(ubi, "bad magic %#08x, must be %#08x", 112762306a36Sopenharmony_ci magic, UBI_EC_HDR_MAGIC); 112862306a36Sopenharmony_ci goto fail; 112962306a36Sopenharmony_ci } 113062306a36Sopenharmony_ci 113162306a36Sopenharmony_ci err = validate_ec_hdr(ubi, ec_hdr); 113262306a36Sopenharmony_ci if (err) { 113362306a36Sopenharmony_ci ubi_err(ubi, "self-check failed for PEB %d", pnum); 113462306a36Sopenharmony_ci goto fail; 113562306a36Sopenharmony_ci } 113662306a36Sopenharmony_ci 113762306a36Sopenharmony_ci return 0; 113862306a36Sopenharmony_ci 113962306a36Sopenharmony_cifail: 114062306a36Sopenharmony_ci ubi_dump_ec_hdr(ec_hdr); 114162306a36Sopenharmony_ci dump_stack(); 114262306a36Sopenharmony_ci return -EINVAL; 114362306a36Sopenharmony_ci} 114462306a36Sopenharmony_ci 114562306a36Sopenharmony_ci/** 114662306a36Sopenharmony_ci * self_check_peb_ec_hdr - check erase counter header. 114762306a36Sopenharmony_ci * @ubi: UBI device description object 114862306a36Sopenharmony_ci * @pnum: the physical eraseblock number to check 114962306a36Sopenharmony_ci * 115062306a36Sopenharmony_ci * This function returns zero if the erase counter header is all right and 115162306a36Sopenharmony_ci * a negative error code if not or if an error occurred. 115262306a36Sopenharmony_ci */ 115362306a36Sopenharmony_cistatic int self_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum) 115462306a36Sopenharmony_ci{ 115562306a36Sopenharmony_ci int err; 115662306a36Sopenharmony_ci uint32_t crc, hdr_crc; 115762306a36Sopenharmony_ci struct ubi_ec_hdr *ec_hdr; 115862306a36Sopenharmony_ci 115962306a36Sopenharmony_ci if (!ubi_dbg_chk_io(ubi)) 116062306a36Sopenharmony_ci return 0; 116162306a36Sopenharmony_ci 116262306a36Sopenharmony_ci ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS); 116362306a36Sopenharmony_ci if (!ec_hdr) 116462306a36Sopenharmony_ci return -ENOMEM; 116562306a36Sopenharmony_ci 116662306a36Sopenharmony_ci err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE); 116762306a36Sopenharmony_ci if (err && err != UBI_IO_BITFLIPS && !mtd_is_eccerr(err)) 116862306a36Sopenharmony_ci goto exit; 116962306a36Sopenharmony_ci 117062306a36Sopenharmony_ci crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC); 117162306a36Sopenharmony_ci hdr_crc = be32_to_cpu(ec_hdr->hdr_crc); 117262306a36Sopenharmony_ci if (hdr_crc != crc) { 117362306a36Sopenharmony_ci ubi_err(ubi, "bad CRC, calculated %#08x, read %#08x", 117462306a36Sopenharmony_ci crc, hdr_crc); 117562306a36Sopenharmony_ci ubi_err(ubi, "self-check failed for PEB %d", pnum); 117662306a36Sopenharmony_ci ubi_dump_ec_hdr(ec_hdr); 117762306a36Sopenharmony_ci dump_stack(); 117862306a36Sopenharmony_ci err = -EINVAL; 117962306a36Sopenharmony_ci goto exit; 118062306a36Sopenharmony_ci } 118162306a36Sopenharmony_ci 118262306a36Sopenharmony_ci err = self_check_ec_hdr(ubi, pnum, ec_hdr); 118362306a36Sopenharmony_ci 118462306a36Sopenharmony_ciexit: 118562306a36Sopenharmony_ci kfree(ec_hdr); 118662306a36Sopenharmony_ci return err; 118762306a36Sopenharmony_ci} 118862306a36Sopenharmony_ci 118962306a36Sopenharmony_ci/** 119062306a36Sopenharmony_ci * self_check_vid_hdr - check that a volume identifier header is all right. 119162306a36Sopenharmony_ci * @ubi: UBI device description object 119262306a36Sopenharmony_ci * @pnum: physical eraseblock number the volume identifier header belongs to 119362306a36Sopenharmony_ci * @vid_hdr: the volume identifier header to check 119462306a36Sopenharmony_ci * 119562306a36Sopenharmony_ci * This function returns zero if the volume identifier header is all right, and 119662306a36Sopenharmony_ci * %-EINVAL if not. 119762306a36Sopenharmony_ci */ 119862306a36Sopenharmony_cistatic int self_check_vid_hdr(const struct ubi_device *ubi, int pnum, 119962306a36Sopenharmony_ci const struct ubi_vid_hdr *vid_hdr) 120062306a36Sopenharmony_ci{ 120162306a36Sopenharmony_ci int err; 120262306a36Sopenharmony_ci uint32_t magic; 120362306a36Sopenharmony_ci 120462306a36Sopenharmony_ci if (!ubi_dbg_chk_io(ubi)) 120562306a36Sopenharmony_ci return 0; 120662306a36Sopenharmony_ci 120762306a36Sopenharmony_ci magic = be32_to_cpu(vid_hdr->magic); 120862306a36Sopenharmony_ci if (magic != UBI_VID_HDR_MAGIC) { 120962306a36Sopenharmony_ci ubi_err(ubi, "bad VID header magic %#08x at PEB %d, must be %#08x", 121062306a36Sopenharmony_ci magic, pnum, UBI_VID_HDR_MAGIC); 121162306a36Sopenharmony_ci goto fail; 121262306a36Sopenharmony_ci } 121362306a36Sopenharmony_ci 121462306a36Sopenharmony_ci err = validate_vid_hdr(ubi, vid_hdr); 121562306a36Sopenharmony_ci if (err) { 121662306a36Sopenharmony_ci ubi_err(ubi, "self-check failed for PEB %d", pnum); 121762306a36Sopenharmony_ci goto fail; 121862306a36Sopenharmony_ci } 121962306a36Sopenharmony_ci 122062306a36Sopenharmony_ci return err; 122162306a36Sopenharmony_ci 122262306a36Sopenharmony_cifail: 122362306a36Sopenharmony_ci ubi_err(ubi, "self-check failed for PEB %d", pnum); 122462306a36Sopenharmony_ci ubi_dump_vid_hdr(vid_hdr); 122562306a36Sopenharmony_ci dump_stack(); 122662306a36Sopenharmony_ci return -EINVAL; 122762306a36Sopenharmony_ci 122862306a36Sopenharmony_ci} 122962306a36Sopenharmony_ci 123062306a36Sopenharmony_ci/** 123162306a36Sopenharmony_ci * self_check_peb_vid_hdr - check volume identifier header. 123262306a36Sopenharmony_ci * @ubi: UBI device description object 123362306a36Sopenharmony_ci * @pnum: the physical eraseblock number to check 123462306a36Sopenharmony_ci * 123562306a36Sopenharmony_ci * This function returns zero if the volume identifier header is all right, 123662306a36Sopenharmony_ci * and a negative error code if not or if an error occurred. 123762306a36Sopenharmony_ci */ 123862306a36Sopenharmony_cistatic int self_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum) 123962306a36Sopenharmony_ci{ 124062306a36Sopenharmony_ci int err; 124162306a36Sopenharmony_ci uint32_t crc, hdr_crc; 124262306a36Sopenharmony_ci struct ubi_vid_io_buf *vidb; 124362306a36Sopenharmony_ci struct ubi_vid_hdr *vid_hdr; 124462306a36Sopenharmony_ci void *p; 124562306a36Sopenharmony_ci 124662306a36Sopenharmony_ci if (!ubi_dbg_chk_io(ubi)) 124762306a36Sopenharmony_ci return 0; 124862306a36Sopenharmony_ci 124962306a36Sopenharmony_ci vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS); 125062306a36Sopenharmony_ci if (!vidb) 125162306a36Sopenharmony_ci return -ENOMEM; 125262306a36Sopenharmony_ci 125362306a36Sopenharmony_ci vid_hdr = ubi_get_vid_hdr(vidb); 125462306a36Sopenharmony_ci p = vidb->buffer; 125562306a36Sopenharmony_ci err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset, 125662306a36Sopenharmony_ci ubi->vid_hdr_alsize); 125762306a36Sopenharmony_ci if (err && err != UBI_IO_BITFLIPS && !mtd_is_eccerr(err)) 125862306a36Sopenharmony_ci goto exit; 125962306a36Sopenharmony_ci 126062306a36Sopenharmony_ci crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC); 126162306a36Sopenharmony_ci hdr_crc = be32_to_cpu(vid_hdr->hdr_crc); 126262306a36Sopenharmony_ci if (hdr_crc != crc) { 126362306a36Sopenharmony_ci ubi_err(ubi, "bad VID header CRC at PEB %d, calculated %#08x, read %#08x", 126462306a36Sopenharmony_ci pnum, crc, hdr_crc); 126562306a36Sopenharmony_ci ubi_err(ubi, "self-check failed for PEB %d", pnum); 126662306a36Sopenharmony_ci ubi_dump_vid_hdr(vid_hdr); 126762306a36Sopenharmony_ci dump_stack(); 126862306a36Sopenharmony_ci err = -EINVAL; 126962306a36Sopenharmony_ci goto exit; 127062306a36Sopenharmony_ci } 127162306a36Sopenharmony_ci 127262306a36Sopenharmony_ci err = self_check_vid_hdr(ubi, pnum, vid_hdr); 127362306a36Sopenharmony_ci 127462306a36Sopenharmony_ciexit: 127562306a36Sopenharmony_ci ubi_free_vid_buf(vidb); 127662306a36Sopenharmony_ci return err; 127762306a36Sopenharmony_ci} 127862306a36Sopenharmony_ci 127962306a36Sopenharmony_ci/** 128062306a36Sopenharmony_ci * self_check_write - make sure write succeeded. 128162306a36Sopenharmony_ci * @ubi: UBI device description object 128262306a36Sopenharmony_ci * @buf: buffer with data which were written 128362306a36Sopenharmony_ci * @pnum: physical eraseblock number the data were written to 128462306a36Sopenharmony_ci * @offset: offset within the physical eraseblock the data were written to 128562306a36Sopenharmony_ci * @len: how many bytes were written 128662306a36Sopenharmony_ci * 128762306a36Sopenharmony_ci * This functions reads data which were recently written and compares it with 128862306a36Sopenharmony_ci * the original data buffer - the data have to match. Returns zero if the data 128962306a36Sopenharmony_ci * match and a negative error code if not or in case of failure. 129062306a36Sopenharmony_ci */ 129162306a36Sopenharmony_cistatic int self_check_write(struct ubi_device *ubi, const void *buf, int pnum, 129262306a36Sopenharmony_ci int offset, int len) 129362306a36Sopenharmony_ci{ 129462306a36Sopenharmony_ci int err, i; 129562306a36Sopenharmony_ci size_t read; 129662306a36Sopenharmony_ci void *buf1; 129762306a36Sopenharmony_ci loff_t addr = (loff_t)pnum * ubi->peb_size + offset; 129862306a36Sopenharmony_ci 129962306a36Sopenharmony_ci if (!ubi_dbg_chk_io(ubi)) 130062306a36Sopenharmony_ci return 0; 130162306a36Sopenharmony_ci 130262306a36Sopenharmony_ci buf1 = __vmalloc(len, GFP_NOFS); 130362306a36Sopenharmony_ci if (!buf1) { 130462306a36Sopenharmony_ci ubi_err(ubi, "cannot allocate memory to check writes"); 130562306a36Sopenharmony_ci return 0; 130662306a36Sopenharmony_ci } 130762306a36Sopenharmony_ci 130862306a36Sopenharmony_ci err = mtd_read(ubi->mtd, addr, len, &read, buf1); 130962306a36Sopenharmony_ci if (err && !mtd_is_bitflip(err)) 131062306a36Sopenharmony_ci goto out_free; 131162306a36Sopenharmony_ci 131262306a36Sopenharmony_ci for (i = 0; i < len; i++) { 131362306a36Sopenharmony_ci uint8_t c = ((uint8_t *)buf)[i]; 131462306a36Sopenharmony_ci uint8_t c1 = ((uint8_t *)buf1)[i]; 131562306a36Sopenharmony_ci int dump_len; 131662306a36Sopenharmony_ci 131762306a36Sopenharmony_ci if (c == c1) 131862306a36Sopenharmony_ci continue; 131962306a36Sopenharmony_ci 132062306a36Sopenharmony_ci ubi_err(ubi, "self-check failed for PEB %d:%d, len %d", 132162306a36Sopenharmony_ci pnum, offset, len); 132262306a36Sopenharmony_ci ubi_msg(ubi, "data differ at position %d", i); 132362306a36Sopenharmony_ci dump_len = max_t(int, 128, len - i); 132462306a36Sopenharmony_ci ubi_msg(ubi, "hex dump of the original buffer from %d to %d", 132562306a36Sopenharmony_ci i, i + dump_len); 132662306a36Sopenharmony_ci print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, 132762306a36Sopenharmony_ci buf + i, dump_len, 1); 132862306a36Sopenharmony_ci ubi_msg(ubi, "hex dump of the read buffer from %d to %d", 132962306a36Sopenharmony_ci i, i + dump_len); 133062306a36Sopenharmony_ci print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, 133162306a36Sopenharmony_ci buf1 + i, dump_len, 1); 133262306a36Sopenharmony_ci dump_stack(); 133362306a36Sopenharmony_ci err = -EINVAL; 133462306a36Sopenharmony_ci goto out_free; 133562306a36Sopenharmony_ci } 133662306a36Sopenharmony_ci 133762306a36Sopenharmony_ci vfree(buf1); 133862306a36Sopenharmony_ci return 0; 133962306a36Sopenharmony_ci 134062306a36Sopenharmony_ciout_free: 134162306a36Sopenharmony_ci vfree(buf1); 134262306a36Sopenharmony_ci return err; 134362306a36Sopenharmony_ci} 134462306a36Sopenharmony_ci 134562306a36Sopenharmony_ci/** 134662306a36Sopenharmony_ci * ubi_self_check_all_ff - check that a region of flash is empty. 134762306a36Sopenharmony_ci * @ubi: UBI device description object 134862306a36Sopenharmony_ci * @pnum: the physical eraseblock number to check 134962306a36Sopenharmony_ci * @offset: the starting offset within the physical eraseblock to check 135062306a36Sopenharmony_ci * @len: the length of the region to check 135162306a36Sopenharmony_ci * 135262306a36Sopenharmony_ci * This function returns zero if only 0xFF bytes are present at offset 135362306a36Sopenharmony_ci * @offset of the physical eraseblock @pnum, and a negative error code if not 135462306a36Sopenharmony_ci * or if an error occurred. 135562306a36Sopenharmony_ci */ 135662306a36Sopenharmony_ciint ubi_self_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len) 135762306a36Sopenharmony_ci{ 135862306a36Sopenharmony_ci size_t read; 135962306a36Sopenharmony_ci int err; 136062306a36Sopenharmony_ci void *buf; 136162306a36Sopenharmony_ci loff_t addr = (loff_t)pnum * ubi->peb_size + offset; 136262306a36Sopenharmony_ci 136362306a36Sopenharmony_ci if (!ubi_dbg_chk_io(ubi)) 136462306a36Sopenharmony_ci return 0; 136562306a36Sopenharmony_ci 136662306a36Sopenharmony_ci buf = __vmalloc(len, GFP_NOFS); 136762306a36Sopenharmony_ci if (!buf) { 136862306a36Sopenharmony_ci ubi_err(ubi, "cannot allocate memory to check for 0xFFs"); 136962306a36Sopenharmony_ci return 0; 137062306a36Sopenharmony_ci } 137162306a36Sopenharmony_ci 137262306a36Sopenharmony_ci err = mtd_read(ubi->mtd, addr, len, &read, buf); 137362306a36Sopenharmony_ci if (err && !mtd_is_bitflip(err)) { 137462306a36Sopenharmony_ci ubi_err(ubi, "err %d while reading %d bytes from PEB %d:%d, read %zd bytes", 137562306a36Sopenharmony_ci err, len, pnum, offset, read); 137662306a36Sopenharmony_ci goto error; 137762306a36Sopenharmony_ci } 137862306a36Sopenharmony_ci 137962306a36Sopenharmony_ci err = ubi_check_pattern(buf, 0xFF, len); 138062306a36Sopenharmony_ci if (err == 0) { 138162306a36Sopenharmony_ci ubi_err(ubi, "flash region at PEB %d:%d, length %d does not contain all 0xFF bytes", 138262306a36Sopenharmony_ci pnum, offset, len); 138362306a36Sopenharmony_ci goto fail; 138462306a36Sopenharmony_ci } 138562306a36Sopenharmony_ci 138662306a36Sopenharmony_ci vfree(buf); 138762306a36Sopenharmony_ci return 0; 138862306a36Sopenharmony_ci 138962306a36Sopenharmony_cifail: 139062306a36Sopenharmony_ci ubi_err(ubi, "self-check failed for PEB %d", pnum); 139162306a36Sopenharmony_ci ubi_msg(ubi, "hex dump of the %d-%d region", offset, offset + len); 139262306a36Sopenharmony_ci print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, buf, len, 1); 139362306a36Sopenharmony_ci err = -EINVAL; 139462306a36Sopenharmony_cierror: 139562306a36Sopenharmony_ci dump_stack(); 139662306a36Sopenharmony_ci vfree(buf); 139762306a36Sopenharmony_ci return err; 139862306a36Sopenharmony_ci} 1399