Lines Matching refs:ubi
81 static int self_check_not_bad(const struct ubi_device *ubi, int pnum);
82 static int self_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum);
83 static int self_check_ec_hdr(const struct ubi_device *ubi, int pnum,
85 static int self_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum);
86 static int self_check_vid_hdr(const struct ubi_device *ubi, int pnum,
88 static int self_check_write(struct ubi_device *ubi, const void *buf, int pnum,
113 int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset, in ubi_io_read() argument
122 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); in ubi_io_read()
123 ubi_assert(offset >= 0 && offset + len <= ubi->peb_size); in ubi_io_read()
126 err = self_check_not_bad(ubi, pnum); in ubi_io_read()
152 addr = (loff_t)pnum * ubi->peb_size + offset; in ubi_io_read()
154 err = mtd_read(ubi->mtd, addr, len, &read, buf); in ubi_io_read()
167 ubi_msg(ubi, "fixable bit-flip detected at PEB %d", in ubi_io_read()
174 ubi_warn(ubi, "error %d%s while reading %d bytes from PEB %d:%d, read only %zd bytes, retry", in ubi_io_read()
180 ubi_err(ubi, "error %d%s while reading %d bytes from PEB %d:%d, read %zd bytes", in ubi_io_read()
196 if (ubi_dbg_is_bitflip(ubi)) { in ubi_io_read()
222 int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset, in ubi_io_write() argument
231 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); in ubi_io_write()
232 ubi_assert(offset >= 0 && offset + len <= ubi->peb_size); in ubi_io_write()
233 ubi_assert(offset % ubi->hdrs_min_io_size == 0); in ubi_io_write()
234 ubi_assert(len > 0 && len % ubi->hdrs_min_io_size == 0); in ubi_io_write()
236 if (ubi->ro_mode) { in ubi_io_write()
237 ubi_err(ubi, "read-only mode"); in ubi_io_write()
241 err = self_check_not_bad(ubi, pnum); in ubi_io_write()
246 err = ubi_self_check_all_ff(ubi, pnum, offset, len); in ubi_io_write()
250 if (offset >= ubi->leb_start) { in ubi_io_write()
255 err = self_check_peb_ec_hdr(ubi, pnum); in ubi_io_write()
258 err = self_check_peb_vid_hdr(ubi, pnum); in ubi_io_write()
263 if (ubi_dbg_is_write_failure(ubi)) { in ubi_io_write()
264 ubi_err(ubi, "cannot write %d bytes to PEB %d:%d (emulated)", in ubi_io_write()
270 addr = (loff_t)pnum * ubi->peb_size + offset; in ubi_io_write()
271 err = mtd_write(ubi->mtd, addr, len, &written, buf); in ubi_io_write()
273 ubi_err(ubi, "error %d while writing %d bytes to PEB %d:%d, written %zd bytes", in ubi_io_write()
276 ubi_dump_flash(ubi, pnum, offset, len); in ubi_io_write()
281 err = self_check_write(ubi, buf, pnum, offset, len); in ubi_io_write()
290 len = ubi->peb_size - offset; in ubi_io_write()
292 err = ubi_self_check_all_ff(ubi, pnum, offset, len); in ubi_io_write()
307 static int do_sync_erase(struct ubi_device *ubi, int pnum) in do_sync_erase() argument
313 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); in do_sync_erase()
315 if (ubi->ro_mode) { in do_sync_erase()
316 ubi_err(ubi, "read-only mode"); in do_sync_erase()
323 ei.addr = (loff_t)pnum * ubi->peb_size; in do_sync_erase()
324 ei.len = ubi->peb_size; in do_sync_erase()
326 err = mtd_erase(ubi->mtd, &ei); in do_sync_erase()
329 ubi_warn(ubi, "error %d while erasing PEB %d, retry", in do_sync_erase()
334 ubi_err(ubi, "cannot erase PEB %d, error %d", pnum, err); in do_sync_erase()
339 err = ubi_self_check_all_ff(ubi, pnum, 0, ubi->peb_size); in do_sync_erase()
343 if (ubi_dbg_is_erase_failure(ubi)) { in do_sync_erase()
344 ubi_err(ubi, "cannot erase PEB %d (emulated)", pnum); in do_sync_erase()
363 static int torture_peb(struct ubi_device *ubi, int pnum) in torture_peb() argument
367 ubi_msg(ubi, "run torture test for PEB %d", pnum); in torture_peb()
371 mutex_lock(&ubi->buf_mutex); in torture_peb()
373 err = do_sync_erase(ubi, pnum); in torture_peb()
378 err = ubi_io_read(ubi, ubi->peb_buf, pnum, 0, ubi->peb_size); in torture_peb()
382 err = ubi_check_pattern(ubi->peb_buf, 0xFF, ubi->peb_size); in torture_peb()
384 ubi_err(ubi, "erased PEB %d, but a non-0xFF byte found", in torture_peb()
391 memset(ubi->peb_buf, patterns[i], ubi->peb_size); in torture_peb()
392 err = ubi_io_write(ubi, ubi->peb_buf, pnum, 0, ubi->peb_size); in torture_peb()
396 memset(ubi->peb_buf, ~patterns[i], ubi->peb_size); in torture_peb()
397 err = ubi_io_read(ubi, ubi->peb_buf, pnum, 0, ubi->peb_size); in torture_peb()
401 err = ubi_check_pattern(ubi->peb_buf, patterns[i], in torture_peb()
402 ubi->peb_size); in torture_peb()
404 ubi_err(ubi, "pattern %x checking failed for PEB %d", in torture_peb()
412 ubi_msg(ubi, "PEB %d passed torture test, do not mark it as bad", pnum); in torture_peb()
415 mutex_unlock(&ubi->buf_mutex); in torture_peb()
422 ubi_err(ubi, "read problems on freshly erased PEB %d, must be bad", in torture_peb()
449 static int nor_erase_prepare(struct ubi_device *ubi, int pnum) in nor_erase_prepare() argument
474 addr = (loff_t)pnum * ubi->peb_size; in nor_erase_prepare()
475 err = ubi_io_read_ec_hdr(ubi, pnum, &ec_hdr, 0); in nor_erase_prepare()
478 err = mtd_write(ubi->mtd, addr, 4, &written, (void *)&data); in nor_erase_prepare()
483 ubi_init_vid_buf(ubi, &vidb, &vid_hdr); in nor_erase_prepare()
486 err = ubi_io_read_vid_hdr(ubi, pnum, &vidb, 0); in nor_erase_prepare()
489 addr += ubi->vid_hdr_aloffset; in nor_erase_prepare()
490 err = mtd_write(ubi->mtd, addr, 4, &written, (void *)&data); in nor_erase_prepare()
502 ubi_err(ubi, "cannot invalidate PEB %d, write returned %d", pnum, err); in nor_erase_prepare()
503 ubi_dump_flash(ubi, pnum, 0, ubi->peb_size); in nor_erase_prepare()
523 int ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture) in ubi_io_sync_erase() argument
527 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); in ubi_io_sync_erase()
529 err = self_check_not_bad(ubi, pnum); in ubi_io_sync_erase()
533 if (ubi->ro_mode) { in ubi_io_sync_erase()
534 ubi_err(ubi, "read-only mode"); in ubi_io_sync_erase()
545 if (ubi->nor_flash && ubi->mtd->writesize == 1) { in ubi_io_sync_erase()
546 err = nor_erase_prepare(ubi, pnum); in ubi_io_sync_erase()
552 ret = torture_peb(ubi, pnum); in ubi_io_sync_erase()
557 err = do_sync_erase(ubi, pnum); in ubi_io_sync_erase()
572 int ubi_io_is_bad(const struct ubi_device *ubi, int pnum) in ubi_io_is_bad() argument
574 struct mtd_info *mtd = ubi->mtd; in ubi_io_is_bad()
576 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); in ubi_io_is_bad()
578 if (ubi->bad_allowed) { in ubi_io_is_bad()
581 ret = mtd_block_isbad(mtd, (loff_t)pnum * ubi->peb_size); in ubi_io_is_bad()
583 ubi_err(ubi, "error %d while checking if PEB %d is bad", in ubi_io_is_bad()
601 int ubi_io_mark_bad(const struct ubi_device *ubi, int pnum) in ubi_io_mark_bad() argument
604 struct mtd_info *mtd = ubi->mtd; in ubi_io_mark_bad()
606 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); in ubi_io_mark_bad()
608 if (ubi->ro_mode) { in ubi_io_mark_bad()
609 ubi_err(ubi, "read-only mode"); in ubi_io_mark_bad()
613 if (!ubi->bad_allowed) in ubi_io_mark_bad()
616 err = mtd_block_markbad(mtd, (loff_t)pnum * ubi->peb_size); in ubi_io_mark_bad()
618 ubi_err(ubi, "cannot mark PEB %d bad, error %d", pnum, err); in ubi_io_mark_bad()
630 static int validate_ec_hdr(const struct ubi_device *ubi, in validate_ec_hdr() argument
641 …ubi_err(ubi, "node with incompatible UBI version found: this UBI version is %d, image version is %… in validate_ec_hdr()
646 if (vid_hdr_offset != ubi->vid_hdr_offset) { in validate_ec_hdr()
647 ubi_err(ubi, "bad VID header offset %d, expected %d", in validate_ec_hdr()
648 vid_hdr_offset, ubi->vid_hdr_offset); in validate_ec_hdr()
652 if (leb_start != ubi->leb_start) { in validate_ec_hdr()
653 ubi_err(ubi, "bad data offset %d, expected %d", in validate_ec_hdr()
654 leb_start, ubi->leb_start); in validate_ec_hdr()
659 ubi_err(ubi, "bad erase counter %lld", ec); in validate_ec_hdr()
666 ubi_err(ubi, "bad EC header"); in validate_ec_hdr()
694 int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum, in ubi_io_read_ec_hdr() argument
701 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); in ubi_io_read_ec_hdr()
703 read_err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE); in ubi_io_read_ec_hdr()
732 ubi_warn(ubi, "no EC header found at PEB %d, only 0xFF bytes", in ubi_io_read_ec_hdr()
747 ubi_warn(ubi, "bad magic number at PEB %d: %08x instead of %08x", in ubi_io_read_ec_hdr()
761 ubi_warn(ubi, "bad EC header CRC at PEB %d, calculated %#08x, read %#08x", in ubi_io_read_ec_hdr()
775 err = validate_ec_hdr(ubi, ec_hdr); in ubi_io_read_ec_hdr()
777 ubi_err(ubi, "validation failed for PEB %d", pnum); in ubi_io_read_ec_hdr()
803 int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum, in ubi_io_write_ec_hdr() argument
810 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); in ubi_io_write_ec_hdr()
814 ec_hdr->vid_hdr_offset = cpu_to_be32(ubi->vid_hdr_offset); in ubi_io_write_ec_hdr()
815 ec_hdr->data_offset = cpu_to_be32(ubi->leb_start); in ubi_io_write_ec_hdr()
816 ec_hdr->image_seq = cpu_to_be32(ubi->image_seq); in ubi_io_write_ec_hdr()
820 err = self_check_ec_hdr(ubi, pnum, ec_hdr); in ubi_io_write_ec_hdr()
824 if (ubi_dbg_power_cut(ubi, POWER_CUT_EC_WRITE)) in ubi_io_write_ec_hdr()
827 err = ubi_io_write(ubi, ec_hdr, pnum, 0, ubi->ec_hdr_alsize); in ubi_io_write_ec_hdr()
839 static int validate_vid_hdr(const struct ubi_device *ubi, in validate_vid_hdr() argument
851 int usable_leb_size = ubi->leb_size - data_pad; in validate_vid_hdr()
854 ubi_err(ubi, "bad copy_flag"); in validate_vid_hdr()
860 ubi_err(ubi, "negative values"); in validate_vid_hdr()
865 ubi_err(ubi, "bad vol_id"); in validate_vid_hdr()
870 ubi_err(ubi, "bad compat"); in validate_vid_hdr()
877 ubi_err(ubi, "bad compat"); in validate_vid_hdr()
882 ubi_err(ubi, "bad vol_type"); in validate_vid_hdr()
886 if (data_pad >= ubi->leb_size / 2) { in validate_vid_hdr()
887 ubi_err(ubi, "bad data_pad"); in validate_vid_hdr()
891 if (data_size > ubi->leb_size) { in validate_vid_hdr()
892 ubi_err(ubi, "bad data_size"); in validate_vid_hdr()
904 ubi_err(ubi, "zero used_ebs"); in validate_vid_hdr()
908 ubi_err(ubi, "zero data_size"); in validate_vid_hdr()
913 ubi_err(ubi, "bad data_size"); in validate_vid_hdr()
917 ubi_err(ubi, "too high lnum"); in validate_vid_hdr()
923 ubi_err(ubi, "non-zero data CRC"); in validate_vid_hdr()
927 ubi_err(ubi, "non-zero data_size"); in validate_vid_hdr()
932 ubi_err(ubi, "zero data_size of copy"); in validate_vid_hdr()
937 ubi_err(ubi, "bad used_ebs"); in validate_vid_hdr()
945 ubi_err(ubi, "bad VID header"); in validate_vid_hdr()
966 int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum, in ubi_io_read_vid_hdr() argument
975 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); in ubi_io_read_vid_hdr()
977 read_err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset, in ubi_io_read_vid_hdr()
978 ubi->vid_hdr_shift + UBI_VID_HDR_SIZE); in ubi_io_read_vid_hdr()
989 ubi_warn(ubi, "no VID header found at PEB %d, only 0xFF bytes", in ubi_io_read_vid_hdr()
1000 ubi_warn(ubi, "bad magic number at PEB %d: %08x instead of %08x", in ubi_io_read_vid_hdr()
1014 ubi_warn(ubi, "bad CRC at PEB %d, calculated %#08x, read %#08x", in ubi_io_read_vid_hdr()
1026 err = validate_vid_hdr(ubi, vid_hdr); in ubi_io_read_vid_hdr()
1028 ubi_err(ubi, "validation failed for PEB %d", pnum); in ubi_io_read_vid_hdr()
1050 int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum, in ubi_io_write_vid_hdr() argument
1059 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); in ubi_io_write_vid_hdr()
1061 err = self_check_peb_ec_hdr(ubi, pnum); in ubi_io_write_vid_hdr()
1070 err = self_check_vid_hdr(ubi, pnum, vid_hdr); in ubi_io_write_vid_hdr()
1074 if (ubi_dbg_power_cut(ubi, POWER_CUT_VID_WRITE)) in ubi_io_write_vid_hdr()
1077 err = ubi_io_write(ubi, p, pnum, ubi->vid_hdr_aloffset, in ubi_io_write_vid_hdr()
1078 ubi->vid_hdr_alsize); in ubi_io_write_vid_hdr()
1090 static int self_check_not_bad(const struct ubi_device *ubi, int pnum) in self_check_not_bad() argument
1094 if (!ubi_dbg_chk_io(ubi)) in self_check_not_bad()
1097 err = ubi_io_is_bad(ubi, pnum); in self_check_not_bad()
1101 ubi_err(ubi, "self-check failed for PEB %d", pnum); in self_check_not_bad()
1115 static int self_check_ec_hdr(const struct ubi_device *ubi, int pnum, in self_check_ec_hdr() argument
1121 if (!ubi_dbg_chk_io(ubi)) in self_check_ec_hdr()
1126 ubi_err(ubi, "bad magic %#08x, must be %#08x", in self_check_ec_hdr()
1131 err = validate_ec_hdr(ubi, ec_hdr); in self_check_ec_hdr()
1133 ubi_err(ubi, "self-check failed for PEB %d", pnum); in self_check_ec_hdr()
1153 static int self_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum) in self_check_peb_ec_hdr() argument
1159 if (!ubi_dbg_chk_io(ubi)) in self_check_peb_ec_hdr()
1162 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS); in self_check_peb_ec_hdr()
1166 err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE); in self_check_peb_ec_hdr()
1173 ubi_err(ubi, "bad CRC, calculated %#08x, read %#08x", in self_check_peb_ec_hdr()
1175 ubi_err(ubi, "self-check failed for PEB %d", pnum); in self_check_peb_ec_hdr()
1182 err = self_check_ec_hdr(ubi, pnum, ec_hdr); in self_check_peb_ec_hdr()
1198 static int self_check_vid_hdr(const struct ubi_device *ubi, int pnum, in self_check_vid_hdr() argument
1204 if (!ubi_dbg_chk_io(ubi)) in self_check_vid_hdr()
1209 ubi_err(ubi, "bad VID header magic %#08x at PEB %d, must be %#08x", in self_check_vid_hdr()
1214 err = validate_vid_hdr(ubi, vid_hdr); in self_check_vid_hdr()
1216 ubi_err(ubi, "self-check failed for PEB %d", pnum); in self_check_vid_hdr()
1223 ubi_err(ubi, "self-check failed for PEB %d", pnum); in self_check_vid_hdr()
1238 static int self_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum) in self_check_peb_vid_hdr() argument
1246 if (!ubi_dbg_chk_io(ubi)) in self_check_peb_vid_hdr()
1249 vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS); in self_check_peb_vid_hdr()
1255 err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset, in self_check_peb_vid_hdr()
1256 ubi->vid_hdr_alsize); in self_check_peb_vid_hdr()
1263 ubi_err(ubi, "bad VID header CRC at PEB %d, calculated %#08x, read %#08x", in self_check_peb_vid_hdr()
1265 ubi_err(ubi, "self-check failed for PEB %d", pnum); in self_check_peb_vid_hdr()
1272 err = self_check_vid_hdr(ubi, pnum, vid_hdr); in self_check_peb_vid_hdr()
1291 static int self_check_write(struct ubi_device *ubi, const void *buf, int pnum, in self_check_write() argument
1297 loff_t addr = (loff_t)pnum * ubi->peb_size + offset; in self_check_write()
1299 if (!ubi_dbg_chk_io(ubi)) in self_check_write()
1304 ubi_err(ubi, "cannot allocate memory to check writes"); in self_check_write()
1308 err = mtd_read(ubi->mtd, addr, len, &read, buf1); in self_check_write()
1320 ubi_err(ubi, "self-check failed for PEB %d:%d, len %d", in self_check_write()
1322 ubi_msg(ubi, "data differ at position %d", i); in self_check_write()
1324 ubi_msg(ubi, "hex dump of the original buffer from %d to %d", in self_check_write()
1328 ubi_msg(ubi, "hex dump of the read buffer from %d to %d", in self_check_write()
1356 int ubi_self_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len) in ubi_self_check_all_ff() argument
1361 loff_t addr = (loff_t)pnum * ubi->peb_size + offset; in ubi_self_check_all_ff()
1363 if (!ubi_dbg_chk_io(ubi)) in ubi_self_check_all_ff()
1368 ubi_err(ubi, "cannot allocate memory to check for 0xFFs"); in ubi_self_check_all_ff()
1372 err = mtd_read(ubi->mtd, addr, len, &read, buf); in ubi_self_check_all_ff()
1374 ubi_err(ubi, "err %d while reading %d bytes from PEB %d:%d, read %zd bytes", in ubi_self_check_all_ff()
1381 ubi_err(ubi, "flash region at PEB %d:%d, length %d does not contain all 0xFF bytes", in ubi_self_check_all_ff()
1390 ubi_err(ubi, "self-check failed for PEB %d", pnum); in ubi_self_check_all_ff()
1391 ubi_msg(ubi, "hex dump of the %d-%d region", offset, offset + len); in ubi_self_check_all_ff()