Lines Matching refs:ubi
87 static int self_check_not_bad(const struct ubi_device *ubi, int pnum);
88 static int self_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum);
89 static int self_check_ec_hdr(const struct ubi_device *ubi, int pnum,
91 static int self_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum);
92 static int self_check_vid_hdr(const struct ubi_device *ubi, int pnum,
94 static int self_check_write(struct ubi_device *ubi, const void *buf, int pnum,
119 int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset, in ubi_io_read() argument
128 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); in ubi_io_read()
129 ubi_assert(offset >= 0 && offset + len <= ubi->peb_size); in ubi_io_read()
132 err = self_check_not_bad(ubi, pnum); in ubi_io_read()
158 addr = (loff_t)pnum * ubi->peb_size + offset; in ubi_io_read()
160 err = mtd_read(ubi->mtd, addr, len, &read, buf); in ubi_io_read()
173 ubi_msg(ubi, "fixable bit-flip detected at PEB %d", in ubi_io_read()
180 ubi_warn(ubi, "error %d%s while reading %d bytes from PEB %d:%d, read only %zd bytes, retry", in ubi_io_read()
186 ubi_err(ubi, "error %d%s while reading %d bytes from PEB %d:%d, read %zd bytes", in ubi_io_read()
202 if (ubi_dbg_is_bitflip(ubi)) { in ubi_io_read()
228 int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset, in ubi_io_write() argument
237 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); in ubi_io_write()
238 ubi_assert(offset >= 0 && offset + len <= ubi->peb_size); in ubi_io_write()
239 ubi_assert(offset % ubi->hdrs_min_io_size == 0); in ubi_io_write()
240 ubi_assert(len > 0 && len % ubi->hdrs_min_io_size == 0); in ubi_io_write()
242 if (ubi->ro_mode) { in ubi_io_write()
243 ubi_err(ubi, "read-only mode"); in ubi_io_write()
247 err = self_check_not_bad(ubi, pnum); in ubi_io_write()
252 err = ubi_self_check_all_ff(ubi, pnum, offset, len); in ubi_io_write()
256 if (offset >= ubi->leb_start) { in ubi_io_write()
261 err = self_check_peb_ec_hdr(ubi, pnum); in ubi_io_write()
264 err = self_check_peb_vid_hdr(ubi, pnum); in ubi_io_write()
269 if (ubi_dbg_is_write_failure(ubi)) { in ubi_io_write()
270 ubi_err(ubi, "cannot write %d bytes to PEB %d:%d (emulated)", in ubi_io_write()
276 addr = (loff_t)pnum * ubi->peb_size + offset; in ubi_io_write()
277 err = mtd_write(ubi->mtd, addr, len, &written, buf); in ubi_io_write()
279 ubi_err(ubi, "error %d while writing %d bytes to PEB %d:%d, written %zd bytes", in ubi_io_write()
282 ubi_dump_flash(ubi, pnum, offset, len); in ubi_io_write()
287 err = self_check_write(ubi, buf, pnum, offset, len); in ubi_io_write()
296 len = ubi->peb_size - offset; in ubi_io_write()
298 err = ubi_self_check_all_ff(ubi, pnum, offset, len); in ubi_io_write()
325 static int do_sync_erase(struct ubi_device *ubi, int pnum) in do_sync_erase() argument
332 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); in do_sync_erase()
334 if (ubi->ro_mode) { in do_sync_erase()
335 ubi_err(ubi, "read-only mode"); in do_sync_erase()
343 ei.mtd = ubi->mtd; in do_sync_erase()
344 ei.addr = (loff_t)pnum * ubi->peb_size; in do_sync_erase()
345 ei.len = ubi->peb_size; in do_sync_erase()
349 err = mtd_erase(ubi->mtd, &ei); in do_sync_erase()
352 ubi_warn(ubi, "error %d while erasing PEB %d, retry", in do_sync_erase()
357 ubi_err(ubi, "cannot erase PEB %d, error %d", pnum, err); in do_sync_erase()
365 ubi_err(ubi, "interrupted PEB %d erasure", pnum); in do_sync_erase()
371 ubi_warn(ubi, "error while erasing PEB %d, retry", in do_sync_erase()
376 ubi_err(ubi, "cannot erase PEB %d", pnum); in do_sync_erase()
381 err = ubi_self_check_all_ff(ubi, pnum, 0, ubi->peb_size); in do_sync_erase()
385 if (ubi_dbg_is_erase_failure(ubi)) { in do_sync_erase()
386 ubi_err(ubi, "cannot erase PEB %d (emulated)", pnum); in do_sync_erase()
405 static int torture_peb(struct ubi_device *ubi, int pnum) in torture_peb() argument
409 ubi_msg(ubi, "run torture test for PEB %d", pnum); in torture_peb()
413 mutex_lock(&ubi->buf_mutex); in torture_peb()
415 err = do_sync_erase(ubi, pnum); in torture_peb()
420 err = ubi_io_read(ubi, ubi->peb_buf, pnum, 0, ubi->peb_size); in torture_peb()
424 err = ubi_check_pattern(ubi->peb_buf, 0xFF, ubi->peb_size); in torture_peb()
426 ubi_err(ubi, "erased PEB %d, but a non-0xFF byte found", in torture_peb()
433 memset(ubi->peb_buf, patterns[i], ubi->peb_size); in torture_peb()
434 err = ubi_io_write(ubi, ubi->peb_buf, pnum, 0, ubi->peb_size); in torture_peb()
438 memset(ubi->peb_buf, ~patterns[i], ubi->peb_size); in torture_peb()
439 err = ubi_io_read(ubi, ubi->peb_buf, pnum, 0, ubi->peb_size); in torture_peb()
443 err = ubi_check_pattern(ubi->peb_buf, patterns[i], in torture_peb()
444 ubi->peb_size); in torture_peb()
446 ubi_err(ubi, "pattern %x checking failed for PEB %d", in torture_peb()
454 ubi_msg(ubi, "PEB %d passed torture test, do not mark it as bad", pnum); in torture_peb()
457 mutex_unlock(&ubi->buf_mutex); in torture_peb()
464 ubi_err(ubi, "read problems on freshly erased PEB %d, must be bad", in torture_peb()
491 static int nor_erase_prepare(struct ubi_device *ubi, int pnum) in nor_erase_prepare() argument
515 addr = (loff_t)pnum * ubi->peb_size; in nor_erase_prepare()
516 err = ubi_io_read_ec_hdr(ubi, pnum, &ec_hdr, 0); in nor_erase_prepare()
519 err = mtd_write(ubi->mtd, addr, 4, &written, (void *)&data); in nor_erase_prepare()
524 err = ubi_io_read_vid_hdr(ubi, pnum, &vid_hdr, 0); in nor_erase_prepare()
527 addr += ubi->vid_hdr_aloffset; in nor_erase_prepare()
528 err = mtd_write(ubi->mtd, addr, 4, &written, (void *)&data); in nor_erase_prepare()
540 ubi_err(ubi, "cannot invalidate PEB %d, write returned %d", pnum, err); in nor_erase_prepare()
541 ubi_dump_flash(ubi, pnum, 0, ubi->peb_size); in nor_erase_prepare()
561 int ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture) in ubi_io_sync_erase() argument
565 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); in ubi_io_sync_erase()
567 err = self_check_not_bad(ubi, pnum); in ubi_io_sync_erase()
571 if (ubi->ro_mode) { in ubi_io_sync_erase()
572 ubi_err(ubi, "read-only mode"); in ubi_io_sync_erase()
576 if (ubi->nor_flash) { in ubi_io_sync_erase()
577 err = nor_erase_prepare(ubi, pnum); in ubi_io_sync_erase()
583 ret = torture_peb(ubi, pnum); in ubi_io_sync_erase()
588 err = do_sync_erase(ubi, pnum); in ubi_io_sync_erase()
603 int ubi_io_is_bad(const struct ubi_device *ubi, int pnum) in ubi_io_is_bad() argument
605 struct mtd_info *mtd = ubi->mtd; in ubi_io_is_bad()
607 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); in ubi_io_is_bad()
609 if (ubi->bad_allowed) { in ubi_io_is_bad()
612 ret = mtd_block_isbad(mtd, (loff_t)pnum * ubi->peb_size); in ubi_io_is_bad()
614 ubi_err(ubi, "error %d while checking if PEB %d is bad", in ubi_io_is_bad()
632 int ubi_io_mark_bad(const struct ubi_device *ubi, int pnum) in ubi_io_mark_bad() argument
635 struct mtd_info *mtd = ubi->mtd; in ubi_io_mark_bad()
637 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); in ubi_io_mark_bad()
639 if (ubi->ro_mode) { in ubi_io_mark_bad()
640 ubi_err(ubi, "read-only mode"); in ubi_io_mark_bad()
644 if (!ubi->bad_allowed) in ubi_io_mark_bad()
647 err = mtd_block_markbad(mtd, (loff_t)pnum * ubi->peb_size); in ubi_io_mark_bad()
649 ubi_err(ubi, "cannot mark PEB %d bad, error %d", pnum, err); in ubi_io_mark_bad()
661 static int validate_ec_hdr(const struct ubi_device *ubi, in validate_ec_hdr() argument
672 …ubi_err(ubi, "node with incompatible UBI version found: this UBI version is %d, image version is %… in validate_ec_hdr()
677 if (vid_hdr_offset != ubi->vid_hdr_offset) { in validate_ec_hdr()
678 ubi_err(ubi, "bad VID header offset %d, expected %d", in validate_ec_hdr()
679 vid_hdr_offset, ubi->vid_hdr_offset); in validate_ec_hdr()
683 if (leb_start != ubi->leb_start) { in validate_ec_hdr()
684 ubi_err(ubi, "bad data offset %d, expected %d", in validate_ec_hdr()
685 leb_start, ubi->leb_start); in validate_ec_hdr()
690 ubi_err(ubi, "bad erase counter %lld", ec); in validate_ec_hdr()
697 ubi_err(ubi, "bad EC header"); in validate_ec_hdr()
725 int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum, in ubi_io_read_ec_hdr() argument
732 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); in ubi_io_read_ec_hdr()
734 read_err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE); in ubi_io_read_ec_hdr()
763 ubi_warn(ubi, "no EC header found at PEB %d, only 0xFF bytes", in ubi_io_read_ec_hdr()
778 ubi_warn(ubi, "bad magic number at PEB %d: %08x instead of %08x", in ubi_io_read_ec_hdr()
792 ubi_warn(ubi, "bad EC header CRC at PEB %d, calculated %#08x, read %#08x", in ubi_io_read_ec_hdr()
806 err = validate_ec_hdr(ubi, ec_hdr); in ubi_io_read_ec_hdr()
808 ubi_err(ubi, "validation failed for PEB %d", pnum); in ubi_io_read_ec_hdr()
834 int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum, in ubi_io_write_ec_hdr() argument
841 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); in ubi_io_write_ec_hdr()
845 ec_hdr->vid_hdr_offset = cpu_to_be32(ubi->vid_hdr_offset); in ubi_io_write_ec_hdr()
846 ec_hdr->data_offset = cpu_to_be32(ubi->leb_start); in ubi_io_write_ec_hdr()
847 ec_hdr->image_seq = cpu_to_be32(ubi->image_seq); in ubi_io_write_ec_hdr()
851 err = self_check_ec_hdr(ubi, pnum, ec_hdr); in ubi_io_write_ec_hdr()
855 if (ubi_dbg_power_cut(ubi, POWER_CUT_EC_WRITE)) in ubi_io_write_ec_hdr()
858 err = ubi_io_write(ubi, ec_hdr, pnum, 0, ubi->ec_hdr_alsize); in ubi_io_write_ec_hdr()
870 static int validate_vid_hdr(const struct ubi_device *ubi, in validate_vid_hdr() argument
882 int usable_leb_size = ubi->leb_size - data_pad; in validate_vid_hdr()
885 ubi_err(ubi, "bad copy_flag"); in validate_vid_hdr()
891 ubi_err(ubi, "negative values"); in validate_vid_hdr()
896 ubi_err(ubi, "bad vol_id"); in validate_vid_hdr()
901 ubi_err(ubi, "bad compat"); in validate_vid_hdr()
908 ubi_err(ubi, "bad compat"); in validate_vid_hdr()
913 ubi_err(ubi, "bad vol_type"); in validate_vid_hdr()
917 if (data_pad >= ubi->leb_size / 2) { in validate_vid_hdr()
918 ubi_err(ubi, "bad data_pad"); in validate_vid_hdr()
930 ubi_err(ubi, "zero used_ebs"); in validate_vid_hdr()
934 ubi_err(ubi, "zero data_size"); in validate_vid_hdr()
939 ubi_err(ubi, "bad data_size"); in validate_vid_hdr()
944 ubi_err(ubi, "bad data_size at last LEB"); in validate_vid_hdr()
948 ubi_err(ubi, "too high lnum"); in validate_vid_hdr()
954 ubi_err(ubi, "non-zero data CRC"); in validate_vid_hdr()
958 ubi_err(ubi, "non-zero data_size"); in validate_vid_hdr()
963 ubi_err(ubi, "zero data_size of copy"); in validate_vid_hdr()
968 ubi_err(ubi, "bad used_ebs"); in validate_vid_hdr()
976 ubi_err(ubi, "bad VID header"); in validate_vid_hdr()
998 int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum, in ubi_io_read_vid_hdr() argument
1006 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); in ubi_io_read_vid_hdr()
1008 p = (char *)vid_hdr - ubi->vid_hdr_shift; in ubi_io_read_vid_hdr()
1009 read_err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset, in ubi_io_read_vid_hdr()
1010 ubi->vid_hdr_alsize); in ubi_io_read_vid_hdr()
1021 ubi_warn(ubi, "no VID header found at PEB %d, only 0xFF bytes", in ubi_io_read_vid_hdr()
1032 ubi_warn(ubi, "bad magic number at PEB %d: %08x instead of %08x", in ubi_io_read_vid_hdr()
1046 ubi_warn(ubi, "bad CRC at PEB %d, calculated %#08x, read %#08x", in ubi_io_read_vid_hdr()
1058 err = validate_vid_hdr(ubi, vid_hdr); in ubi_io_read_vid_hdr()
1060 ubi_err(ubi, "validation failed for PEB %d", pnum); in ubi_io_read_vid_hdr()
1082 int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum, in ubi_io_write_vid_hdr() argument
1090 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); in ubi_io_write_vid_hdr()
1092 err = self_check_peb_ec_hdr(ubi, pnum); in ubi_io_write_vid_hdr()
1101 err = self_check_vid_hdr(ubi, pnum, vid_hdr); in ubi_io_write_vid_hdr()
1105 if (ubi_dbg_power_cut(ubi, POWER_CUT_VID_WRITE)) in ubi_io_write_vid_hdr()
1108 p = (char *)vid_hdr - ubi->vid_hdr_shift; in ubi_io_write_vid_hdr()
1109 err = ubi_io_write(ubi, p, pnum, ubi->vid_hdr_aloffset, in ubi_io_write_vid_hdr()
1110 ubi->vid_hdr_alsize); in ubi_io_write_vid_hdr()
1122 static int self_check_not_bad(const struct ubi_device *ubi, int pnum) in self_check_not_bad() argument
1126 if (!ubi_dbg_chk_io(ubi)) in self_check_not_bad()
1129 err = ubi_io_is_bad(ubi, pnum); in self_check_not_bad()
1133 ubi_err(ubi, "self-check failed for PEB %d", pnum); in self_check_not_bad()
1147 static int self_check_ec_hdr(const struct ubi_device *ubi, int pnum, in self_check_ec_hdr() argument
1153 if (!ubi_dbg_chk_io(ubi)) in self_check_ec_hdr()
1158 ubi_err(ubi, "bad magic %#08x, must be %#08x", in self_check_ec_hdr()
1163 err = validate_ec_hdr(ubi, ec_hdr); in self_check_ec_hdr()
1165 ubi_err(ubi, "self-check failed for PEB %d", pnum); in self_check_ec_hdr()
1185 static int self_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum) in self_check_peb_ec_hdr() argument
1191 if (!ubi_dbg_chk_io(ubi)) in self_check_peb_ec_hdr()
1194 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS); in self_check_peb_ec_hdr()
1198 err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE); in self_check_peb_ec_hdr()
1205 ubi_err(ubi, "bad CRC, calculated %#08x, read %#08x", in self_check_peb_ec_hdr()
1207 ubi_err(ubi, "self-check failed for PEB %d", pnum); in self_check_peb_ec_hdr()
1214 err = self_check_ec_hdr(ubi, pnum, ec_hdr); in self_check_peb_ec_hdr()
1230 static int self_check_vid_hdr(const struct ubi_device *ubi, int pnum, in self_check_vid_hdr() argument
1236 if (!ubi_dbg_chk_io(ubi)) in self_check_vid_hdr()
1241 ubi_err(ubi, "bad VID header magic %#08x at PEB %d, must be %#08x", in self_check_vid_hdr()
1246 err = validate_vid_hdr(ubi, vid_hdr); in self_check_vid_hdr()
1248 ubi_err(ubi, "self-check failed for PEB %d", pnum); in self_check_vid_hdr()
1255 ubi_err(ubi, "self-check failed for PEB %d", pnum); in self_check_vid_hdr()
1270 static int self_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum) in self_check_peb_vid_hdr() argument
1277 if (!ubi_dbg_chk_io(ubi)) in self_check_peb_vid_hdr()
1280 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS); in self_check_peb_vid_hdr()
1284 p = (char *)vid_hdr - ubi->vid_hdr_shift; in self_check_peb_vid_hdr()
1285 err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset, in self_check_peb_vid_hdr()
1286 ubi->vid_hdr_alsize); in self_check_peb_vid_hdr()
1293 ubi_err(ubi, "bad VID header CRC at PEB %d, calculated %#08x, read %#08x", in self_check_peb_vid_hdr()
1295 ubi_err(ubi, "self-check failed for PEB %d", pnum); in self_check_peb_vid_hdr()
1302 err = self_check_vid_hdr(ubi, pnum, vid_hdr); in self_check_peb_vid_hdr()
1305 ubi_free_vid_hdr(ubi, vid_hdr); in self_check_peb_vid_hdr()
1321 static int self_check_write(struct ubi_device *ubi, const void *buf, int pnum, in self_check_write() argument
1327 loff_t addr = (loff_t)pnum * ubi->peb_size + offset; in self_check_write()
1329 if (!ubi_dbg_chk_io(ubi)) in self_check_write()
1334 ubi_err(ubi, "cannot allocate memory to check writes"); in self_check_write()
1338 err = mtd_read(ubi->mtd, addr, len, &read, buf1); in self_check_write()
1352 ubi_err(ubi, "self-check failed for PEB %d:%d, len %d", in self_check_write()
1354 ubi_msg(ubi, "data differ at position %d", i); in self_check_write()
1355 ubi_msg(ubi, "hex dump of the original buffer from %d to %d", in self_check_write()
1359 ubi_msg(ubi, "hex dump of the read buffer from %d to %d", in self_check_write()
1387 int ubi_self_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len) in ubi_self_check_all_ff() argument
1392 loff_t addr = (loff_t)pnum * ubi->peb_size + offset; in ubi_self_check_all_ff()
1394 if (!ubi_dbg_chk_io(ubi)) in ubi_self_check_all_ff()
1399 ubi_err(ubi, "cannot allocate memory to check for 0xFFs"); in ubi_self_check_all_ff()
1403 err = mtd_read(ubi->mtd, addr, len, &read, buf); in ubi_self_check_all_ff()
1405 ubi_err(ubi, "err %d while reading %d bytes from PEB %d:%d, read %zd bytes", in ubi_self_check_all_ff()
1412 ubi_err(ubi, "flash region at PEB %d:%d, length %d does not contain all 0xFF bytes", in ubi_self_check_all_ff()
1421 ubi_err(ubi, "self-check failed for PEB %d", pnum); in ubi_self_check_all_ff()
1422 ubi_msg(ubi, "hex dump of the %d-%d region", offset, offset + len); in ubi_self_check_all_ff()