91 #include <linux/slab.h>
94 static int self_check_not_bad(
const struct ubi_device *ubi,
int pnum);
95 static int self_check_peb_ec_hdr(
const struct ubi_device *ubi,
int pnum);
96 static int self_check_ec_hdr(
const struct ubi_device *ubi,
int pnum,
98 static int self_check_peb_vid_hdr(
const struct ubi_device *ubi,
int pnum);
99 static int self_check_vid_hdr(
const struct ubi_device *ubi,
int pnum,
101 static int self_check_write(
struct ubi_device *ubi,
const void *
buf,
int pnum,
133 dbg_io(
"read %d bytes from PEB %d:%d", len, pnum, offset);
135 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
136 ubi_assert(offset >= 0 && offset + len <= ubi->peb_size);
139 err = self_check_not_bad(ubi, pnum);
165 addr = (loff_t)pnum * ubi->
peb_size + offset;
169 const char *
errstr = mtd_is_eccerr(err) ?
" (ECC error)" :
"";
171 if (mtd_is_bitflip(err)) {
180 ubi_msg(
"fixable bit-flip detected at PEB %d", pnum);
186 ubi_warn(
"error %d%s while reading %d bytes from PEB %d:%d, read only %zd bytes, retry",
187 err, errstr, len, pnum, offset, read);
192 ubi_err(
"error %d%s while reading %d bytes from PEB %d:%d, read %zd bytes",
193 err, errstr, len, pnum, offset, read);
201 if (read != len && mtd_is_eccerr(err)) {
208 if (ubi_dbg_is_bitflip(ubi)) {
209 dbg_gen(
"bit-flip (emulated)");
241 dbg_io(
"write %d bytes to PEB %d:%d", len, pnum, offset);
243 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
244 ubi_assert(offset >= 0 && offset + len <= ubi->peb_size);
253 err = self_check_not_bad(ubi, pnum);
267 err = self_check_peb_ec_hdr(ubi, pnum);
270 err = self_check_peb_vid_hdr(ubi, pnum);
275 if (ubi_dbg_is_write_failure(ubi)) {
276 ubi_err(
"cannot write %d bytes to PEB %d:%d (emulated)",
282 addr = (loff_t)pnum * ubi->
peb_size + offset;
285 ubi_err(
"error %d while writing %d bytes to PEB %d:%d, written %zd bytes",
286 err, len, pnum, offset, written);
293 err = self_check_write(ubi, buf, pnum, offset, len);
317 static void erase_callback(
struct erase_info *ei)
331 static int do_sync_erase(
struct ubi_device *ubi,
int pnum)
337 dbg_io(
"erase PEB %d", pnum);
338 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
353 ei.
priv = (
unsigned long)&wq;
358 ubi_warn(
"error %d while erasing PEB %d, retry",
363 ubi_err(
"cannot erase PEB %d, error %d", pnum, err);
371 ubi_err(
"interrupted PEB %d erasure", pnum);
377 ubi_warn(
"error while erasing PEB %d, retry", pnum);
381 ubi_err(
"cannot erase PEB %d", pnum);
390 if (ubi_dbg_is_erase_failure(ubi)) {
391 ubi_err(
"cannot erase PEB %d (emulated)", pnum);
399 static uint8_t patterns[] = {0xa5, 0x5a, 0x0};
410 static int torture_peb(
struct ubi_device *ubi,
int pnum)
412 int err,
i, patt_count;
414 ubi_msg(
"run torture test for PEB %d", pnum);
419 for (i = 0; i < patt_count; i++) {
420 err = do_sync_erase(ubi, pnum);
431 ubi_err(
"erased PEB %d, but a non-0xFF byte found",
451 ubi_err(
"pattern %x checking failed for PEB %d",
459 ubi_msg(
"PEB %d passed torture test, do not mark it as bad", pnum);
469 ubi_err(
"read problems on freshly erased PEB %d, must be bad",
496 static int nor_erase_prepare(
struct ubi_device *ubi,
int pnum)
517 addr = (loff_t)pnum * ubi->
peb_size;
521 err =
mtd_write(ubi->
mtd, addr, 4, &written, (
void *)&data);
554 ubi_err(
"cannot invalidate PEB %d, write returned %d read returned %d",
580 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
582 err = self_check_not_bad(ubi, pnum);
592 err = nor_erase_prepare(ubi, pnum);
598 ret = torture_peb(ubi, pnum);
603 err = do_sync_erase(ubi, pnum);
622 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
629 ubi_err(
"error %d while checking if PEB %d is bad",
632 dbg_io(
"PEB %d is bad", pnum);
652 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
664 ubi_err(
"cannot mark PEB %d bad, error %d", pnum, err);
676 static int validate_ec_hdr(
const struct ubi_device *ubi,
680 int vid_hdr_offset, leb_start;
687 ubi_err(
"node with incompatible UBI version found: this UBI version is %d, image version is %d",
693 ubi_err(
"bad VID header offset %d, expected %d",
699 ubi_err(
"bad data offset %d, expected %d",
705 ubi_err(
"bad erase counter %lld", ec);
746 dbg_io(
"read EC header from PEB %d", pnum);
747 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
767 if (mtd_is_eccerr(read_err))
778 ubi_warn(
"no EC header found at PEB %d, only 0xFF bytes",
780 dbg_bld(
"no EC header found at PEB %d, only 0xFF bytes",
793 ubi_warn(
"bad magic number at PEB %d: %08x instead of %08x",
797 dbg_bld(
"bad magic number at PEB %d: %08x instead of %08x",
805 if (hdr_crc != crc) {
807 ubi_warn(
"bad EC header CRC at PEB %d, calculated %#08x, read %#08x",
811 dbg_bld(
"bad EC header CRC at PEB %d, calculated %#08x, read %#08x",
821 err = validate_ec_hdr(ubi, ec_hdr);
823 ubi_err(
"validation failed for PEB %d", pnum);
855 dbg_io(
"write EC header to PEB %d", pnum);
856 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
866 err = self_check_ec_hdr(ubi, pnum, ec_hdr);
882 static int validate_vid_hdr(
const struct ubi_device *ubi,
889 int compat = vid_hdr->
compat;
894 int usable_leb_size = ubi->
leb_size - data_pad;
896 if (copy_flag != 0 && copy_flag != 1) {
901 if (vol_id < 0 || lnum < 0 || data_size < 0 || used_ebs < 0 ||
929 if (data_pad >= ubi->
leb_size / 2) {
945 if (data_size == 0) {
949 if (lnum < used_ebs - 1) {
950 if (data_size != usable_leb_size) {
954 }
else if (lnum == used_ebs - 1) {
955 if (data_size == 0) {
956 ubi_err(
"bad data_size at last LEB");
964 if (copy_flag == 0) {
969 if (data_size != 0) {
974 if (data_size == 0) {
975 ubi_err(
"zero data_size of copy");
1017 dbg_io(
"read VID header from PEB %d", pnum);
1018 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
1023 if (read_err && read_err !=
UBI_IO_BITFLIPS && !mtd_is_eccerr(read_err))
1028 if (mtd_is_eccerr(read_err))
1033 ubi_warn(
"no VID header found at PEB %d, only 0xFF bytes",
1035 dbg_bld(
"no VID header found at PEB %d, only 0xFF bytes",
1044 ubi_warn(
"bad magic number at PEB %d: %08x instead of %08x",
1048 dbg_bld(
"bad magic number at PEB %d: %08x instead of %08x",
1056 if (hdr_crc != crc) {
1058 ubi_warn(
"bad CRC at PEB %d, calculated %#08x, read %#08x",
1059 pnum, crc, hdr_crc);
1062 dbg_bld(
"bad CRC at PEB %d, calculated %#08x, read %#08x",
1063 pnum, crc, hdr_crc);
1070 err = validate_vid_hdr(ubi, vid_hdr);
1072 ubi_err(
"validation failed for PEB %d", pnum);
1101 dbg_io(
"write VID header to PEB %d", pnum);
1102 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
1104 err = self_check_peb_ec_hdr(ubi, pnum);
1113 err = self_check_vid_hdr(ubi, pnum, vid_hdr);
1131 static int self_check_not_bad(
const struct ubi_device *ubi,
int pnum)
1135 if (!ubi->
dbg->chk_io)
1142 ubi_err(
"self-check failed for PEB %d", pnum);
1156 static int self_check_ec_hdr(
const struct ubi_device *ubi,
int pnum,
1162 if (!ubi->
dbg->chk_io)
1167 ubi_err(
"bad magic %#08x, must be %#08x",
1172 err = validate_ec_hdr(ubi, ec_hdr);
1174 ubi_err(
"self-check failed for PEB %d", pnum);
1194 static int self_check_peb_ec_hdr(
const struct ubi_device *ubi,
int pnum)
1200 if (!ubi->
dbg->chk_io)
1213 if (hdr_crc != crc) {
1214 ubi_err(
"bad CRC, calculated %#08x, read %#08x", crc, hdr_crc);
1215 ubi_err(
"self-check failed for PEB %d", pnum);
1222 err = self_check_ec_hdr(ubi, pnum, ec_hdr);
1238 static int self_check_vid_hdr(
const struct ubi_device *ubi,
int pnum,
1244 if (!ubi->
dbg->chk_io)
1249 ubi_err(
"bad VID header magic %#08x at PEB %d, must be %#08x",
1254 err = validate_vid_hdr(ubi, vid_hdr);
1256 ubi_err(
"self-check failed for PEB %d", pnum);
1263 ubi_err(
"self-check failed for PEB %d", pnum);
1278 static int self_check_peb_vid_hdr(
const struct ubi_device *ubi,
int pnum)
1285 if (!ubi->
dbg->chk_io)
1288 vid_hdr = ubi_zalloc_vid_hdr(ubi,
GFP_NOFS);
1300 if (hdr_crc != crc) {
1301 ubi_err(
"bad VID header CRC at PEB %d, calculated %#08x, read %#08x",
1302 pnum, crc, hdr_crc);
1303 ubi_err(
"self-check failed for PEB %d", pnum);
1310 err = self_check_vid_hdr(ubi, pnum, vid_hdr);
1313 ubi_free_vid_hdr(ubi, vid_hdr);
1329 static int self_check_write(
struct ubi_device *ubi,
const void *
buf,
int pnum,
1335 loff_t addr = (loff_t)pnum * ubi->
peb_size + offset;
1337 if (!ubi->
dbg->chk_io)
1342 ubi_err(
"cannot allocate memory to check writes");
1347 if (err && !mtd_is_bitflip(err))
1350 for (i = 0; i < len; i++) {
1358 ubi_err(
"self-check failed for PEB %d:%d, len %d",
1360 ubi_msg(
"data differ at position %d", i);
1361 dump_len =
max_t(
int, 128, len - i);
1362 ubi_msg(
"hex dump of the original buffer from %d to %d",
1365 buf + i, dump_len, 1);
1366 ubi_msg(
"hex dump of the read buffer from %d to %d",
1369 buf1 + i, dump_len, 1);
1399 loff_t addr = (loff_t)pnum * ubi->
peb_size + offset;
1401 if (!ubi->
dbg->chk_io)
1406 ubi_err(
"cannot allocate memory to check for 0xFFs");
1411 if (err && !mtd_is_bitflip(err)) {
1412 ubi_err(
"error %d while reading %d bytes from PEB %d:%d, read %zd bytes",
1413 err, len, pnum, offset, read);
1419 ubi_err(
"flash region at PEB %d:%d, length %d does not contain all 0xFF bytes",
1428 ubi_err(
"self-check failed for PEB %d", pnum);
1429 ubi_msg(
"hex dump of the %d-%d region", offset, offset + len);