···482 uint32_t data = 0;483 struct ubi_vid_hdr vid_hdr;484485- addr = (loff_t)pnum * ubi->peb_size + ubi->vid_hdr_aloffset;0000000486 err = ubi->mtd->write(ubi->mtd, addr, 4, &written, (void *)&data);487 if (!err) {488- addr -= ubi->vid_hdr_aloffset;489 err = ubi->mtd->write(ubi->mtd, addr, 4, &written,490 (void *)&data);491 if (!err)···501502 /*503 * We failed to write to the media. This was observed with Spansion504- * S29GL512N NOR flash. Most probably the eraseblock erasure was505- * interrupted at a very inappropriate moment, so it became unwritable.506- * In this case we probably anyway have garbage in this PEB.0507 */508 err1 = ubi_io_read_vid_hdr(ubi, pnum, &vid_hdr, 0);509- if (err1 == UBI_IO_BAD_HDR_EBADMSG || err1 == UBI_IO_BAD_HDR)510- /*511- * The VID header is corrupted, so we can safely erase this512- * PEB and not afraid that it will be treated as a valid PEB in513- * case of an unclean reboot.514- */515- return 0;00000516517 /*518 * The PEB contains a valid VID header, but we cannot invalidate it.
···482 uint32_t data = 0;483 struct ubi_vid_hdr vid_hdr;484485+ /*486+ * It is important to first invalidate the EC header, and then the VID487+ * header. Otherwise a power cut may lead to valid EC header and488+ * invalid VID header, in which case UBI will treat this PEB as489+ * corrupted and will try to preserve it, and print scary warnings (see490+ * the header comment in scan.c for more information).491+ */492+ addr = (loff_t)pnum * ubi->peb_size;493 err = ubi->mtd->write(ubi->mtd, addr, 4, &written, (void *)&data);494 if (!err) {495+ addr += ubi->vid_hdr_aloffset;496 err = ubi->mtd->write(ubi->mtd, addr, 4, &written,497 (void *)&data);498 if (!err)···494495 /*496 * We failed to write to the media. This was observed with Spansion497+ * S29GL512N NOR flash. Most probably the previously eraseblock erasure498+ * was interrupted at a very inappropriate moment, so it became499+ * unwritable. In this case we probably anyway have garbage in this500+ * PEB.501 */502 err1 = ubi_io_read_vid_hdr(ubi, pnum, &vid_hdr, 0);503+ if (err1 == UBI_IO_BAD_HDR_EBADMSG || err1 == UBI_IO_BAD_HDR) {504+ struct ubi_ec_hdr ec_hdr;505+506+ err1 = ubi_io_read_ec_hdr(ubi, pnum, &ec_hdr, 0);507+ if (err1 == UBI_IO_BAD_HDR_EBADMSG || err1 == UBI_IO_BAD_HDR)508+ /*509+ * Both VID and EC headers are corrupted, so we can510+ * safely erase this PEB and not afraid that it will be511+ * treated as a valid PEB in case of an unclean reboot.512+ */513+ return 0;514+ }515516 /*517 * The PEB contains a valid VID header, but we cannot invalidate it.
+4
drivers/mtd/ubi/scan.c
···953 * impossible to distinguish it from a PEB which just954 * contains garbage because of a power cut during erase955 * operation. So we just schedule this PEB for erasure.0000956 */957 err = 0;958 else
···953 * impossible to distinguish it from a PEB which just954 * contains garbage because of a power cut during erase955 * operation. So we just schedule this PEB for erasure.956+ *957+ * Besides, in case of NOR flash, we deliberatly958+ * corrupt both headers because NOR flash erasure is959+ * slow and can start from the end.960 */961 err = 0;962 else