···482482 uint32_t data = 0;483483 struct ubi_vid_hdr vid_hdr;484484485485- addr = (loff_t)pnum * ubi->peb_size + ubi->vid_hdr_aloffset;485485+ /*486486+ * It is important to first invalidate the EC header, and then the VID487487+ * header. Otherwise a power cut may lead to valid EC header and488488+ * invalid VID header, in which case UBI will treat this PEB as489489+ * corrupted and will try to preserve it, and print scary warnings (see490490+ * the header comment in scan.c for more information).491491+ */492492+ addr = (loff_t)pnum * ubi->peb_size;486493 err = ubi->mtd->write(ubi->mtd, addr, 4, &written, (void *)&data);487494 if (!err) {488488- addr -= ubi->vid_hdr_aloffset;495495+ addr += ubi->vid_hdr_aloffset;489496 err = ubi->mtd->write(ubi->mtd, addr, 4, &written,490497 (void *)&data);491498 if (!err)···501494502495 /*503496 * We failed to write to the media. This was observed with Spansion504504- * S29GL512N NOR flash. Most probably the eraseblock erasure was505505- * interrupted at a very inappropriate moment, so it became unwritable.506506- * In this case we probably anyway have garbage in this PEB.497497+ * S29GL512N NOR flash. Most probably the previously eraseblock erasure498498+ * was interrupted at a very inappropriate moment, so it became499499+ * unwritable. In this case we probably anyway have garbage in this500500+ * PEB.507501 */508502 err1 = ubi_io_read_vid_hdr(ubi, pnum, &vid_hdr, 0);509509- if (err1 == UBI_IO_BAD_HDR_EBADMSG || err1 == UBI_IO_BAD_HDR)510510- /*511511- * The VID header is corrupted, so we can safely erase this512512- * PEB and not afraid that it will be treated as a valid PEB in513513- * case of an unclean reboot.514514- */515515- return 0;503503+ if (err1 == UBI_IO_BAD_HDR_EBADMSG || err1 == UBI_IO_BAD_HDR) {504504+ struct ubi_ec_hdr ec_hdr;505505+506506+ err1 = ubi_io_read_ec_hdr(ubi, pnum, &ec_hdr, 0);507507+ if (err1 == UBI_IO_BAD_HDR_EBADMSG || err1 == UBI_IO_BAD_HDR)508508+ /*509509+ * Both VID and EC headers are corrupted, so we can510510+ * safely erase this PEB and not afraid that it will be511511+ * treated as a valid PEB in case of an unclean reboot.512512+ */513513+ return 0;514514+ }516515517516 /*518517 * The PEB contains a valid VID header, but we cannot invalidate it.
+4
drivers/mtd/ubi/scan.c
···953953 * impossible to distinguish it from a PEB which just954954 * contains garbage because of a power cut during erase955955 * operation. So we just schedule this PEB for erasure.956956+ *957957+ * Besides, in case of NOR flash, we deliberatly958958+ * corrupt both headers because NOR flash erasure is959959+ * slow and can start from the end.956960 */957961 err = 0;958962 else