Commit 2c7ca5cc authored by Qi Wang 王起 (qiwang)'s avatar Qi Wang 王起 (qiwang) Committed by Artem Bityutskiy

UBI: avoid program operation on NOR flash after erasure interrupted

nor_erase_prepare() will be called before erase a NOR flash, it will program '0'
into a block to mark this block. But program data into a erasure interrupted block
can cause program timtout(several minutes at most) error, could impact other
operation on NOR flash. So UBIFS can read this block first to avoid unneeded
program operation.

This patch try to put read operation at head of write operation in
nor_erase_prepare(), read out the data.
If the data is already corrupt, then no need to program any data into this block,
just go to erase this block.

This patch is validated on Micron NOR flash, part number is:JS28F512M29EWHA
Signed-off-by: default avatarQi Wang <qiwang@micron.com>
Signed-off-by: default avatarArtem Bityutskiy <artem.bityutskiy@linux.intel.com>
parent b6b44e0a
...@@ -495,10 +495,12 @@ static int torture_peb(struct ubi_device *ubi, int pnum) ...@@ -495,10 +495,12 @@ static int torture_peb(struct ubi_device *ubi, int pnum)
*/ */
static int nor_erase_prepare(struct ubi_device *ubi, int pnum) static int nor_erase_prepare(struct ubi_device *ubi, int pnum)
{ {
int err, err1; int err;
size_t written; size_t written;
loff_t addr; loff_t addr;
uint32_t data = 0; uint32_t data = 0;
struct ubi_ec_hdr ec_hdr;
/* /*
* Note, we cannot generally define VID header buffers on stack, * Note, we cannot generally define VID header buffers on stack,
* because of the way we deal with these buffers (see the header * because of the way we deal with these buffers (see the header
...@@ -509,50 +511,38 @@ static int nor_erase_prepare(struct ubi_device *ubi, int pnum) ...@@ -509,50 +511,38 @@ static int nor_erase_prepare(struct ubi_device *ubi, int pnum)
struct ubi_vid_hdr vid_hdr; struct ubi_vid_hdr vid_hdr;
/* /*
* If VID or EC is valid, we have to corrupt them before erasing.
* It is important to first invalidate the EC header, and then the VID * It is important to first invalidate the EC header, and then the VID
* header. Otherwise a power cut may lead to valid EC header and * header. Otherwise a power cut may lead to valid EC header and
* invalid VID header, in which case UBI will treat this PEB as * invalid VID header, in which case UBI will treat this PEB as
* corrupted and will try to preserve it, and print scary warnings. * corrupted and will try to preserve it, and print scary warnings.
*/ */
addr = (loff_t)pnum * ubi->peb_size; addr = (loff_t)pnum * ubi->peb_size;
err = mtd_write(ubi->mtd, addr, 4, &written, (void *)&data); err = ubi_io_read_ec_hdr(ubi, pnum, &ec_hdr, 0);
if (!err) { if (err != UBI_IO_BAD_HDR_EBADMSG && err != UBI_IO_BAD_HDR &&
addr += ubi->vid_hdr_aloffset; err != UBI_IO_FF){
err = mtd_write(ubi->mtd, addr, 4, &written, (void *)&data); err = mtd_write(ubi->mtd, addr, 4, &written, (void *)&data);
if (!err) if(err)
return 0; goto error;
} }
/* err = ubi_io_read_vid_hdr(ubi, pnum, &vid_hdr, 0);
* We failed to write to the media. This was observed with Spansion if (err != UBI_IO_BAD_HDR_EBADMSG && err != UBI_IO_BAD_HDR &&
* S29GL512N NOR flash. Most probably the previously eraseblock erasure err != UBI_IO_FF){
* was interrupted at a very inappropriate moment, so it became addr += ubi->vid_hdr_aloffset;
* unwritable. In this case we probably anyway have garbage in this err = mtd_write(ubi->mtd, addr, 4, &written, (void *)&data);
* PEB. if (err)
*/ goto error;
err1 = ubi_io_read_vid_hdr(ubi, pnum, &vid_hdr, 0);
if (err1 == UBI_IO_BAD_HDR_EBADMSG || err1 == UBI_IO_BAD_HDR ||
err1 == UBI_IO_FF) {
struct ubi_ec_hdr ec_hdr;
err1 = ubi_io_read_ec_hdr(ubi, pnum, &ec_hdr, 0);
if (err1 == UBI_IO_BAD_HDR_EBADMSG || err1 == UBI_IO_BAD_HDR ||
err1 == UBI_IO_FF)
/*
* Both VID and EC headers are corrupted, so we can
* safely erase this PEB and not afraid that it will be
* treated as a valid PEB in case of an unclean reboot.
*/
return 0;
} }
return 0;
error:
/* /*
* The PEB contains a valid VID header, but we cannot invalidate it. * The PEB contains a valid VID or EC header, but we cannot invalidate
* Supposedly the flash media or the driver is screwed up, so return an * it. Supposedly the flash media or the driver is screwed up, so
* error. * return an error.
*/ */
ubi_err("cannot invalidate PEB %d, write returned %d read returned %d", ubi_err("cannot invalidate PEB %d, write returned %d", pnum, err);
pnum, err, err1);
ubi_dump_flash(ubi, pnum, 0, ubi->peb_size); ubi_dump_flash(ubi, pnum, 0, ubi->peb_size);
return -EIO; return -EIO;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment