diff options
Diffstat (limited to 'drivers/mtd/ubi/io.c')
-rw-r--r-- | drivers/mtd/ubi/io.c | 138 |
1 files changed, 50 insertions, 88 deletions
diff --git a/drivers/mtd/ubi/io.c b/drivers/mtd/ubi/io.c index 332f992f13d9..c2960ac9f39c 100644 --- a/drivers/mtd/ubi/io.c +++ b/drivers/mtd/ubi/io.c | |||
@@ -376,25 +376,6 @@ retry: | |||
376 | return 0; | 376 | return 0; |
377 | } | 377 | } |
378 | 378 | ||
379 | /** | ||
380 | * check_pattern - check if buffer contains only a certain byte pattern. | ||
381 | * @buf: buffer to check | ||
382 | * @patt: the pattern to check | ||
383 | * @size: buffer size in bytes | ||
384 | * | ||
385 | * This function returns %1 in there are only @patt bytes in @buf, and %0 if | ||
386 | * something else was also found. | ||
387 | */ | ||
388 | static int check_pattern(const void *buf, uint8_t patt, int size) | ||
389 | { | ||
390 | int i; | ||
391 | |||
392 | for (i = 0; i < size; i++) | ||
393 | if (((const uint8_t *)buf)[i] != patt) | ||
394 | return 0; | ||
395 | return 1; | ||
396 | } | ||
397 | |||
398 | /* Patterns to write to a physical eraseblock when torturing it */ | 379 | /* Patterns to write to a physical eraseblock when torturing it */ |
399 | static uint8_t patterns[] = {0xa5, 0x5a, 0x0}; | 380 | static uint8_t patterns[] = {0xa5, 0x5a, 0x0}; |
400 | 381 | ||
@@ -426,7 +407,7 @@ static int torture_peb(struct ubi_device *ubi, int pnum) | |||
426 | if (err) | 407 | if (err) |
427 | goto out; | 408 | goto out; |
428 | 409 | ||
429 | err = check_pattern(ubi->peb_buf1, 0xFF, ubi->peb_size); | 410 | err = ubi_check_pattern(ubi->peb_buf1, 0xFF, ubi->peb_size); |
430 | if (err == 0) { | 411 | if (err == 0) { |
431 | ubi_err("erased PEB %d, but a non-0xFF byte found", | 412 | ubi_err("erased PEB %d, but a non-0xFF byte found", |
432 | pnum); | 413 | pnum); |
@@ -445,7 +426,8 @@ static int torture_peb(struct ubi_device *ubi, int pnum) | |||
445 | if (err) | 426 | if (err) |
446 | goto out; | 427 | goto out; |
447 | 428 | ||
448 | err = check_pattern(ubi->peb_buf1, patterns[i], ubi->peb_size); | 429 | err = ubi_check_pattern(ubi->peb_buf1, patterns[i], |
430 | ubi->peb_size); | ||
449 | if (err == 0) { | 431 | if (err == 0) { |
450 | ubi_err("pattern %x checking failed for PEB %d", | 432 | ubi_err("pattern %x checking failed for PEB %d", |
451 | patterns[i], pnum); | 433 | patterns[i], pnum); |
@@ -517,7 +499,7 @@ static int nor_erase_prepare(struct ubi_device *ubi, int pnum) | |||
517 | * In this case we probably anyway have garbage in this PEB. | 499 | * In this case we probably anyway have garbage in this PEB. |
518 | */ | 500 | */ |
519 | err1 = ubi_io_read_vid_hdr(ubi, pnum, &vid_hdr, 0); | 501 | err1 = ubi_io_read_vid_hdr(ubi, pnum, &vid_hdr, 0); |
520 | if (err1 == UBI_IO_BAD_HDR_READ || err1 == UBI_IO_BAD_HDR) | 502 | if (err1 == UBI_IO_BAD_HDR_EBADMSG || err1 == UBI_IO_BAD_HDR) |
521 | /* | 503 | /* |
522 | * The VID header is corrupted, so we can safely erase this | 504 | * The VID header is corrupted, so we can safely erase this |
523 | * PEB and not afraid that it will be treated as a valid PEB in | 505 | * PEB and not afraid that it will be treated as a valid PEB in |
@@ -712,47 +694,47 @@ bad: | |||
712 | * and corrected by the flash driver; this is harmless but may indicate that | 694 | * and corrected by the flash driver; this is harmless but may indicate that |
713 | * this eraseblock may become bad soon (but may be not); | 695 | * this eraseblock may become bad soon (but may be not); |
714 | * o %UBI_IO_BAD_HDR if the erase counter header is corrupted (a CRC error); | 696 | * o %UBI_IO_BAD_HDR if the erase counter header is corrupted (a CRC error); |
715 | * o %UBI_IO_PEB_EMPTY if the physical eraseblock is empty; | 697 | * o %UBI_IO_BAD_HDR_EBADMSG is the same as %UBI_IO_BAD_HDR, but there also was |
698 | * a data integrity error (uncorrectable ECC error in case of NAND); | ||
699 | * o %UBI_IO_FF if only 0xFF bytes were read (the PEB is supposedly empty) | ||
716 | * o a negative error code in case of failure. | 700 | * o a negative error code in case of failure. |
717 | */ | 701 | */ |
718 | int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum, | 702 | int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum, |
719 | struct ubi_ec_hdr *ec_hdr, int verbose) | 703 | struct ubi_ec_hdr *ec_hdr, int verbose) |
720 | { | 704 | { |
721 | int err, read_err = 0; | 705 | int err, read_err; |
722 | uint32_t crc, magic, hdr_crc; | 706 | uint32_t crc, magic, hdr_crc; |
723 | 707 | ||
724 | dbg_io("read EC header from PEB %d", pnum); | 708 | dbg_io("read EC header from PEB %d", pnum); |
725 | ubi_assert(pnum >= 0 && pnum < ubi->peb_count); | 709 | ubi_assert(pnum >= 0 && pnum < ubi->peb_count); |
726 | 710 | ||
727 | err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE); | 711 | read_err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE); |
728 | if (err) { | 712 | if (read_err) { |
729 | if (err != UBI_IO_BITFLIPS && err != -EBADMSG) | 713 | if (read_err != UBI_IO_BITFLIPS && read_err != -EBADMSG) |
730 | return err; | 714 | return read_err; |
731 | 715 | ||
732 | /* | 716 | /* |
733 | * We read all the data, but either a correctable bit-flip | 717 | * We read all the data, but either a correctable bit-flip |
734 | * occurred, or MTD reported about some data integrity error, | 718 | * occurred, or MTD reported a data integrity error |
735 | * like an ECC error in case of NAND. The former is harmless, | 719 | * (uncorrectable ECC error in case of NAND). The former is |
736 | * the later may mean that the read data is corrupted. But we | 720 | * harmless, the later may mean that the read data is |
737 | * have a CRC check-sum and we will detect this. If the EC | 721 | * corrupted. But we have a CRC check-sum and we will detect |
738 | * header is still OK, we just report this as there was a | 722 | * this. If the EC header is still OK, we just report this as |
739 | * bit-flip. | 723 | * there was a bit-flip, to force scrubbing. |
740 | */ | 724 | */ |
741 | if (err == -EBADMSG) | ||
742 | read_err = UBI_IO_BAD_HDR_READ; | ||
743 | } | 725 | } |
744 | 726 | ||
745 | magic = be32_to_cpu(ec_hdr->magic); | 727 | magic = be32_to_cpu(ec_hdr->magic); |
746 | if (magic != UBI_EC_HDR_MAGIC) { | 728 | if (magic != UBI_EC_HDR_MAGIC) { |
747 | if (read_err) | 729 | if (read_err == -EBADMSG) |
748 | return read_err; | 730 | return UBI_IO_BAD_HDR_EBADMSG; |
749 | 731 | ||
750 | /* | 732 | /* |
751 | * The magic field is wrong. Let's check if we have read all | 733 | * The magic field is wrong. Let's check if we have read all |
752 | * 0xFF. If yes, this physical eraseblock is assumed to be | 734 | * 0xFF. If yes, this physical eraseblock is assumed to be |
753 | * empty. | 735 | * empty. |
754 | */ | 736 | */ |
755 | if (check_pattern(ec_hdr, 0xFF, UBI_EC_HDR_SIZE)) { | 737 | if (ubi_check_pattern(ec_hdr, 0xFF, UBI_EC_HDR_SIZE)) { |
756 | /* The physical eraseblock is supposedly empty */ | 738 | /* The physical eraseblock is supposedly empty */ |
757 | if (verbose) | 739 | if (verbose) |
758 | ubi_warn("no EC header found at PEB %d, " | 740 | ubi_warn("no EC header found at PEB %d, " |
@@ -760,7 +742,10 @@ int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum, | |||
760 | else if (UBI_IO_DEBUG) | 742 | else if (UBI_IO_DEBUG) |
761 | dbg_msg("no EC header found at PEB %d, " | 743 | dbg_msg("no EC header found at PEB %d, " |
762 | "only 0xFF bytes", pnum); | 744 | "only 0xFF bytes", pnum); |
763 | return UBI_IO_PEB_EMPTY; | 745 | if (!read_err) |
746 | return UBI_IO_FF; | ||
747 | else | ||
748 | return UBI_IO_FF_BITFLIPS; | ||
764 | } | 749 | } |
765 | 750 | ||
766 | /* | 751 | /* |
@@ -788,7 +773,11 @@ int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum, | |||
788 | } else if (UBI_IO_DEBUG) | 773 | } else if (UBI_IO_DEBUG) |
789 | dbg_msg("bad EC header CRC at PEB %d, calculated " | 774 | dbg_msg("bad EC header CRC at PEB %d, calculated " |
790 | "%#08x, read %#08x", pnum, crc, hdr_crc); | 775 | "%#08x, read %#08x", pnum, crc, hdr_crc); |
791 | return read_err ?: UBI_IO_BAD_HDR; | 776 | |
777 | if (!read_err) | ||
778 | return UBI_IO_BAD_HDR; | ||
779 | else | ||
780 | return UBI_IO_BAD_HDR_EBADMSG; | ||
792 | } | 781 | } |
793 | 782 | ||
794 | /* And of course validate what has just been read from the media */ | 783 | /* And of course validate what has just been read from the media */ |
@@ -975,22 +964,16 @@ bad: | |||
975 | * | 964 | * |
976 | * This function reads the volume identifier header from physical eraseblock | 965 | * This function reads the volume identifier header from physical eraseblock |
977 | * @pnum and stores it in @vid_hdr. It also checks CRC checksum of the read | 966 | * @pnum and stores it in @vid_hdr. It also checks CRC checksum of the read |
978 | * volume identifier header. The following codes may be returned: | 967 | * volume identifier header. The error codes are the same as in |
968 | * 'ubi_io_read_ec_hdr()'. | ||
979 | * | 969 | * |
980 | * o %0 if the CRC checksum is correct and the header was successfully read; | 970 | * Note, the implementation of this function is also very similar to |
981 | * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected | 971 | * 'ubi_io_read_ec_hdr()', so refer commentaries in 'ubi_io_read_ec_hdr()'. |
982 | * and corrected by the flash driver; this is harmless but may indicate that | ||
983 | * this eraseblock may become bad soon; | ||
984 | * o %UBI_IO_BAD_HDR if the volume identifier header is corrupted (a CRC | ||
985 | * error detected); | ||
986 | * o %UBI_IO_PEB_FREE if the physical eraseblock is free (i.e., there is no VID | ||
987 | * header there); | ||
988 | * o a negative error code in case of failure. | ||
989 | */ | 972 | */ |
990 | int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum, | 973 | int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum, |
991 | struct ubi_vid_hdr *vid_hdr, int verbose) | 974 | struct ubi_vid_hdr *vid_hdr, int verbose) |
992 | { | 975 | { |
993 | int err, read_err = 0; | 976 | int err, read_err; |
994 | uint32_t crc, magic, hdr_crc; | 977 | uint32_t crc, magic, hdr_crc; |
995 | void *p; | 978 | void *p; |
996 | 979 | ||
@@ -998,48 +981,29 @@ int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum, | |||
998 | ubi_assert(pnum >= 0 && pnum < ubi->peb_count); | 981 | ubi_assert(pnum >= 0 && pnum < ubi->peb_count); |
999 | 982 | ||
1000 | p = (char *)vid_hdr - ubi->vid_hdr_shift; | 983 | p = (char *)vid_hdr - ubi->vid_hdr_shift; |
1001 | err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset, | 984 | read_err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset, |
1002 | ubi->vid_hdr_alsize); | 985 | ubi->vid_hdr_alsize); |
1003 | if (err) { | 986 | if (read_err && read_err != UBI_IO_BITFLIPS && read_err != -EBADMSG) |
1004 | if (err != UBI_IO_BITFLIPS && err != -EBADMSG) | 987 | return read_err; |
1005 | return err; | ||
1006 | |||
1007 | /* | ||
1008 | * We read all the data, but either a correctable bit-flip | ||
1009 | * occurred, or MTD reported about some data integrity error, | ||
1010 | * like an ECC error in case of NAND. The former is harmless, | ||
1011 | * the later may mean the read data is corrupted. But we have a | ||
1012 | * CRC check-sum and we will identify this. If the VID header is | ||
1013 | * still OK, we just report this as there was a bit-flip. | ||
1014 | */ | ||
1015 | if (err == -EBADMSG) | ||
1016 | read_err = UBI_IO_BAD_HDR_READ; | ||
1017 | } | ||
1018 | 988 | ||
1019 | magic = be32_to_cpu(vid_hdr->magic); | 989 | magic = be32_to_cpu(vid_hdr->magic); |
1020 | if (magic != UBI_VID_HDR_MAGIC) { | 990 | if (magic != UBI_VID_HDR_MAGIC) { |
1021 | if (read_err) | 991 | if (read_err == -EBADMSG) |
1022 | return read_err; | 992 | return UBI_IO_BAD_HDR_EBADMSG; |
1023 | 993 | ||
1024 | /* | 994 | if (ubi_check_pattern(vid_hdr, 0xFF, UBI_VID_HDR_SIZE)) { |
1025 | * If we have read all 0xFF bytes, the VID header probably does | ||
1026 | * not exist and the physical eraseblock is assumed to be free. | ||
1027 | */ | ||
1028 | if (check_pattern(vid_hdr, 0xFF, UBI_VID_HDR_SIZE)) { | ||
1029 | /* The physical eraseblock is supposedly free */ | ||
1030 | if (verbose) | 995 | if (verbose) |
1031 | ubi_warn("no VID header found at PEB %d, " | 996 | ubi_warn("no VID header found at PEB %d, " |
1032 | "only 0xFF bytes", pnum); | 997 | "only 0xFF bytes", pnum); |
1033 | else if (UBI_IO_DEBUG) | 998 | else if (UBI_IO_DEBUG) |
1034 | dbg_msg("no VID header found at PEB %d, " | 999 | dbg_msg("no VID header found at PEB %d, " |
1035 | "only 0xFF bytes", pnum); | 1000 | "only 0xFF bytes", pnum); |
1036 | return UBI_IO_PEB_FREE; | 1001 | if (!read_err) |
1002 | return UBI_IO_FF; | ||
1003 | else | ||
1004 | return UBI_IO_FF_BITFLIPS; | ||
1037 | } | 1005 | } |
1038 | 1006 | ||
1039 | /* | ||
1040 | * This is not a valid VID header, and these are not 0xFF | ||
1041 | * bytes. Report that the header is corrupted. | ||
1042 | */ | ||
1043 | if (verbose) { | 1007 | if (verbose) { |
1044 | ubi_warn("bad magic number at PEB %d: %08x instead of " | 1008 | ubi_warn("bad magic number at PEB %d: %08x instead of " |
1045 | "%08x", pnum, magic, UBI_VID_HDR_MAGIC); | 1009 | "%08x", pnum, magic, UBI_VID_HDR_MAGIC); |
@@ -1061,20 +1025,18 @@ int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum, | |||
1061 | } else if (UBI_IO_DEBUG) | 1025 | } else if (UBI_IO_DEBUG) |
1062 | dbg_msg("bad CRC at PEB %d, calculated %#08x, " | 1026 | dbg_msg("bad CRC at PEB %d, calculated %#08x, " |
1063 | "read %#08x", pnum, crc, hdr_crc); | 1027 | "read %#08x", pnum, crc, hdr_crc); |
1064 | return read_err ?: UBI_IO_BAD_HDR; | 1028 | if (!read_err) |
1029 | return UBI_IO_BAD_HDR; | ||
1030 | else | ||
1031 | return UBI_IO_BAD_HDR_EBADMSG; | ||
1065 | } | 1032 | } |
1066 | 1033 | ||
1067 | /* Validate the VID header that we have just read */ | ||
1068 | err = validate_vid_hdr(ubi, vid_hdr); | 1034 | err = validate_vid_hdr(ubi, vid_hdr); |
1069 | if (err) { | 1035 | if (err) { |
1070 | ubi_err("validation failed for PEB %d", pnum); | 1036 | ubi_err("validation failed for PEB %d", pnum); |
1071 | return -EINVAL; | 1037 | return -EINVAL; |
1072 | } | 1038 | } |
1073 | 1039 | ||
1074 | /* | ||
1075 | * If there was a read error (%-EBADMSG), but the header CRC is still | ||
1076 | * OK, report about a bit-flip to force scrubbing on this PEB. | ||
1077 | */ | ||
1078 | return read_err ? UBI_IO_BITFLIPS : 0; | 1040 | return read_err ? UBI_IO_BITFLIPS : 0; |
1079 | } | 1041 | } |
1080 | 1042 | ||
@@ -1383,7 +1345,7 @@ int ubi_dbg_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len) | |||
1383 | goto error; | 1345 | goto error; |
1384 | } | 1346 | } |
1385 | 1347 | ||
1386 | err = check_pattern(ubi->dbg_peb_buf, 0xFF, len); | 1348 | err = ubi_check_pattern(ubi->dbg_peb_buf, 0xFF, len); |
1387 | if (err == 0) { | 1349 | if (err == 0) { |
1388 | ubi_err("flash region at PEB %d:%d, length %d does not " | 1350 | ubi_err("flash region at PEB %d:%d, length %d does not " |
1389 | "contain all 0xFF bytes", pnum, offset, len); | 1351 | "contain all 0xFF bytes", pnum, offset, len); |