Skip to content

Commit 6e9065d

Browse files
Artem BityutskiyArtem Bityutskiy
authored andcommitted
UBI: add write checking
Add an extra debugging check function which validates writes. After every write it reads the data back, compares it with the original data, and complains if they mismatch. Useful for debugging. No-op if extra debugging checks are disabled. Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
1 parent adbf05e commit 6e9065d

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

‎drivers/mtd/ubi/debug.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,11 @@ void ubi_dbg_dump_flash(struct ubi_device *ubi, int pnum, int offset, int len);
9696

9797
#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
9898
int ubi_dbg_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len);
99+
int ubi_dbg_check_write(struct ubi_device *ubi, const void *buf, int pnum,
100+
int offset, int len);
99101
#else
100102
#define ubi_dbg_check_all_ff(ubi, pnum, offset, len) 0
103+
#define ubi_dbg_check_write(ubi, buf, pnum, offset, len) 0
101104
#endif
102105

103106
#ifdef CONFIG_MTD_UBI_DEBUG_DISABLE_BGT
@@ -176,6 +179,7 @@ static inline int ubi_dbg_is_erase_failure(void)
176179
#define ubi_dbg_is_write_failure() 0
177180
#define ubi_dbg_is_erase_failure() 0
178181
#define ubi_dbg_check_all_ff(ubi, pnum, offset, len) 0
182+
#define ubi_dbg_check_write(ubi, buf, pnum, offset, len) 0
179183

180184
#endif /* !CONFIG_MTD_UBI_DEBUG */
181185
#endif /* !__UBI_DEBUG_H__ */

‎drivers/mtd/ubi/io.c‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,21 @@ int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
273273
} else
274274
ubi_assert(written == len);
275275

276+
if (!err) {
277+
err = ubi_dbg_check_write(ubi, buf, pnum, offset, len);
278+
if (err)
279+
return err;
280+
281+
/*
282+
* Since we always write sequentially, the rest of the PEB has
283+
* to contain only 0xFF bytes.
284+
*/
285+
offset += len;
286+
len = ubi->peb_size - offset;
287+
if (len)
288+
err = ubi_dbg_check_all_ff(ubi, pnum, offset, len);
289+
}
290+
276291
return err;
277292
}
278293

@@ -1281,6 +1296,61 @@ static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum)
12811296
return err;
12821297
}
12831298

1299+
/**
1300+
* ubi_dbg_check_write - make sure write succeeded.
1301+
* @ubi: UBI device description object
1302+
* @buf: buffer with data which were written
1303+
* @pnum: physical eraseblock number the data were written to
1304+
* @offset: offset within the physical eraseblock the data were written to
1305+
* @len: how many bytes were written
1306+
*
1307+
* This functions reads data which were recently written and compares it with
1308+
* the original data buffer - the data have to match. Returns zero if the data
1309+
* match and a negative error code if not or in case of failure.
1310+
*/
1311+
int ubi_dbg_check_write(struct ubi_device *ubi, const void *buf, int pnum,
1312+
int offset, int len)
1313+
{
1314+
int err, i;
1315+
1316+
mutex_lock(&ubi->dbg_buf_mutex);
1317+
err = ubi_io_read(ubi, ubi->dbg_peb_buf, pnum, offset, len);
1318+
if (err)
1319+
goto out_unlock;
1320+
1321+
for (i = 0; i < len; i++) {
1322+
uint8_t c = ((uint8_t *)buf)[i];
1323+
uint8_t c1 = ((uint8_t *)ubi->dbg_peb_buf)[i];
1324+
int dump_len;
1325+
1326+
if (c == c1)
1327+
continue;
1328+
1329+
ubi_err("paranoid check failed for PEB %d:%d, len %d",
1330+
pnum, offset, len);
1331+
ubi_msg("data differ at position %d", i);
1332+
dump_len = max_t(int, 128, len - i);
1333+
ubi_msg("hex dump of the original buffer from %d to %d",
1334+
i, i + dump_len);
1335+
print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
1336+
buf + i, dump_len, 1);
1337+
ubi_msg("hex dump of the read buffer from %d to %d",
1338+
i, i + dump_len);
1339+
print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
1340+
ubi->dbg_peb_buf + i, dump_len, 1);
1341+
ubi_dbg_dump_stack();
1342+
err = -EINVAL;
1343+
goto out_unlock;
1344+
}
1345+
mutex_unlock(&ubi->dbg_buf_mutex);
1346+
1347+
return 0;
1348+
1349+
out_unlock:
1350+
mutex_unlock(&ubi->dbg_buf_mutex);
1351+
return err;
1352+
}
1353+
12841354
/**
12851355
* ubi_dbg_check_all_ff - check that a region of flash is empty.
12861356
* @ubi: UBI device description object

0 commit comments

Comments
 (0)