diff options
Diffstat (limited to 'drivers/mtd/ubi/io.c')
-rw-r--r-- | drivers/mtd/ubi/io.c | 121 |
1 files changed, 96 insertions, 25 deletions
diff --git a/drivers/mtd/ubi/io.c b/drivers/mtd/ubi/io.c index 8aa51e7a6a7d..533b1a4b9af1 100644 --- a/drivers/mtd/ubi/io.c +++ b/drivers/mtd/ubi/io.c | |||
@@ -88,6 +88,7 @@ | |||
88 | 88 | ||
89 | #include <linux/crc32.h> | 89 | #include <linux/crc32.h> |
90 | #include <linux/err.h> | 90 | #include <linux/err.h> |
91 | #include <linux/slab.h> | ||
91 | #include "ubi.h" | 92 | #include "ubi.h" |
92 | 93 | ||
93 | #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID | 94 | #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID |
@@ -143,7 +144,7 @@ int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset, | |||
143 | 144 | ||
144 | err = paranoid_check_not_bad(ubi, pnum); | 145 | err = paranoid_check_not_bad(ubi, pnum); |
145 | if (err) | 146 | if (err) |
146 | return err > 0 ? -EINVAL : err; | 147 | return err; |
147 | 148 | ||
148 | addr = (loff_t)pnum * ubi->peb_size + offset; | 149 | addr = (loff_t)pnum * ubi->peb_size + offset; |
149 | retry: | 150 | retry: |
@@ -236,12 +237,12 @@ int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset, | |||
236 | 237 | ||
237 | err = paranoid_check_not_bad(ubi, pnum); | 238 | err = paranoid_check_not_bad(ubi, pnum); |
238 | if (err) | 239 | if (err) |
239 | return err > 0 ? -EINVAL : err; | 240 | return err; |
240 | 241 | ||
241 | /* The area we are writing to has to contain all 0xFF bytes */ | 242 | /* The area we are writing to has to contain all 0xFF bytes */ |
242 | err = ubi_dbg_check_all_ff(ubi, pnum, offset, len); | 243 | err = ubi_dbg_check_all_ff(ubi, pnum, offset, len); |
243 | if (err) | 244 | if (err) |
244 | return err > 0 ? -EINVAL : err; | 245 | return err; |
245 | 246 | ||
246 | if (offset >= ubi->leb_start) { | 247 | if (offset >= ubi->leb_start) { |
247 | /* | 248 | /* |
@@ -250,10 +251,10 @@ int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset, | |||
250 | */ | 251 | */ |
251 | err = paranoid_check_peb_ec_hdr(ubi, pnum); | 252 | err = paranoid_check_peb_ec_hdr(ubi, pnum); |
252 | if (err) | 253 | if (err) |
253 | return err > 0 ? -EINVAL : err; | 254 | return err; |
254 | err = paranoid_check_peb_vid_hdr(ubi, pnum); | 255 | err = paranoid_check_peb_vid_hdr(ubi, pnum); |
255 | if (err) | 256 | if (err) |
256 | return err > 0 ? -EINVAL : err; | 257 | return err; |
257 | } | 258 | } |
258 | 259 | ||
259 | if (ubi_dbg_is_write_failure()) { | 260 | if (ubi_dbg_is_write_failure()) { |
@@ -273,6 +274,21 @@ int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset, | |||
273 | } else | 274 | } else |
274 | ubi_assert(written == len); | 275 | ubi_assert(written == len); |
275 | 276 | ||
277 | if (!err) { | ||
278 | err = ubi_dbg_check_write(ubi, buf, pnum, offset, len); | ||
279 | if (err) | ||
280 | return err; | ||
281 | |||
282 | /* | ||
283 | * Since we always write sequentially, the rest of the PEB has | ||
284 | * to contain only 0xFF bytes. | ||
285 | */ | ||
286 | offset += len; | ||
287 | len = ubi->peb_size - offset; | ||
288 | if (len) | ||
289 | err = ubi_dbg_check_all_ff(ubi, pnum, offset, len); | ||
290 | } | ||
291 | |||
276 | return err; | 292 | return err; |
277 | } | 293 | } |
278 | 294 | ||
@@ -348,7 +364,7 @@ retry: | |||
348 | 364 | ||
349 | err = ubi_dbg_check_all_ff(ubi, pnum, 0, ubi->peb_size); | 365 | err = ubi_dbg_check_all_ff(ubi, pnum, 0, ubi->peb_size); |
350 | if (err) | 366 | if (err) |
351 | return err > 0 ? -EINVAL : err; | 367 | return err; |
352 | 368 | ||
353 | if (ubi_dbg_is_erase_failure() && !err) { | 369 | if (ubi_dbg_is_erase_failure() && !err) { |
354 | dbg_err("cannot erase PEB %d (emulated)", pnum); | 370 | dbg_err("cannot erase PEB %d (emulated)", pnum); |
@@ -542,7 +558,7 @@ int ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture) | |||
542 | 558 | ||
543 | err = paranoid_check_not_bad(ubi, pnum); | 559 | err = paranoid_check_not_bad(ubi, pnum); |
544 | if (err != 0) | 560 | if (err != 0) |
545 | return err > 0 ? -EINVAL : err; | 561 | return err; |
546 | 562 | ||
547 | if (ubi->ro_mode) { | 563 | if (ubi->ro_mode) { |
548 | ubi_err("read-only mode"); | 564 | ubi_err("read-only mode"); |
@@ -819,7 +835,7 @@ int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum, | |||
819 | 835 | ||
820 | err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr); | 836 | err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr); |
821 | if (err) | 837 | if (err) |
822 | return -EINVAL; | 838 | return err; |
823 | 839 | ||
824 | err = ubi_io_write(ubi, ec_hdr, pnum, 0, ubi->ec_hdr_alsize); | 840 | err = ubi_io_write(ubi, ec_hdr, pnum, 0, ubi->ec_hdr_alsize); |
825 | return err; | 841 | return err; |
@@ -1083,7 +1099,7 @@ int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum, | |||
1083 | 1099 | ||
1084 | err = paranoid_check_peb_ec_hdr(ubi, pnum); | 1100 | err = paranoid_check_peb_ec_hdr(ubi, pnum); |
1085 | if (err) | 1101 | if (err) |
1086 | return err > 0 ? -EINVAL : err; | 1102 | return err; |
1087 | 1103 | ||
1088 | vid_hdr->magic = cpu_to_be32(UBI_VID_HDR_MAGIC); | 1104 | vid_hdr->magic = cpu_to_be32(UBI_VID_HDR_MAGIC); |
1089 | vid_hdr->version = UBI_VERSION; | 1105 | vid_hdr->version = UBI_VERSION; |
@@ -1092,7 +1108,7 @@ int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum, | |||
1092 | 1108 | ||
1093 | err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr); | 1109 | err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr); |
1094 | if (err) | 1110 | if (err) |
1095 | return -EINVAL; | 1111 | return err; |
1096 | 1112 | ||
1097 | p = (char *)vid_hdr - ubi->vid_hdr_shift; | 1113 | p = (char *)vid_hdr - ubi->vid_hdr_shift; |
1098 | err = ubi_io_write(ubi, p, pnum, ubi->vid_hdr_aloffset, | 1114 | err = ubi_io_write(ubi, p, pnum, ubi->vid_hdr_aloffset, |
@@ -1107,8 +1123,8 @@ int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum, | |||
1107 | * @ubi: UBI device description object | 1123 | * @ubi: UBI device description object |
1108 | * @pnum: physical eraseblock number to check | 1124 | * @pnum: physical eraseblock number to check |
1109 | * | 1125 | * |
1110 | * This function returns zero if the physical eraseblock is good, a positive | 1126 | * This function returns zero if the physical eraseblock is good, %-EINVAL if |
1111 | * number if it is bad and a negative error code if an error occurred. | 1127 | * it is bad and a negative error code if an error occurred. |
1112 | */ | 1128 | */ |
1113 | static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum) | 1129 | static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum) |
1114 | { | 1130 | { |
@@ -1120,7 +1136,7 @@ static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum) | |||
1120 | 1136 | ||
1121 | ubi_err("paranoid check failed for PEB %d", pnum); | 1137 | ubi_err("paranoid check failed for PEB %d", pnum); |
1122 | ubi_dbg_dump_stack(); | 1138 | ubi_dbg_dump_stack(); |
1123 | return err; | 1139 | return err > 0 ? -EINVAL : err; |
1124 | } | 1140 | } |
1125 | 1141 | ||
1126 | /** | 1142 | /** |
@@ -1130,7 +1146,7 @@ static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum) | |||
1130 | * @ec_hdr: the erase counter header to check | 1146 | * @ec_hdr: the erase counter header to check |
1131 | * | 1147 | * |
1132 | * This function returns zero if the erase counter header contains valid | 1148 | * This function returns zero if the erase counter header contains valid |
1133 | * values, and %1 if not. | 1149 | * values, and %-EINVAL if not. |
1134 | */ | 1150 | */ |
1135 | static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum, | 1151 | static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum, |
1136 | const struct ubi_ec_hdr *ec_hdr) | 1152 | const struct ubi_ec_hdr *ec_hdr) |
@@ -1156,7 +1172,7 @@ static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum, | |||
1156 | fail: | 1172 | fail: |
1157 | ubi_dbg_dump_ec_hdr(ec_hdr); | 1173 | ubi_dbg_dump_ec_hdr(ec_hdr); |
1158 | ubi_dbg_dump_stack(); | 1174 | ubi_dbg_dump_stack(); |
1159 | return 1; | 1175 | return -EINVAL; |
1160 | } | 1176 | } |
1161 | 1177 | ||
1162 | /** | 1178 | /** |
@@ -1164,8 +1180,8 @@ fail: | |||
1164 | * @ubi: UBI device description object | 1180 | * @ubi: UBI device description object |
1165 | * @pnum: the physical eraseblock number to check | 1181 | * @pnum: the physical eraseblock number to check |
1166 | * | 1182 | * |
1167 | * This function returns zero if the erase counter header is all right, %1 if | 1183 | * This function returns zero if the erase counter header is all right and and |
1168 | * not, and a negative error code if an error occurred. | 1184 | * a negative error code if not or if an error occurred. |
1169 | */ | 1185 | */ |
1170 | static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum) | 1186 | static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum) |
1171 | { | 1187 | { |
@@ -1188,7 +1204,7 @@ static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum) | |||
1188 | ubi_err("paranoid check failed for PEB %d", pnum); | 1204 | ubi_err("paranoid check failed for PEB %d", pnum); |
1189 | ubi_dbg_dump_ec_hdr(ec_hdr); | 1205 | ubi_dbg_dump_ec_hdr(ec_hdr); |
1190 | ubi_dbg_dump_stack(); | 1206 | ubi_dbg_dump_stack(); |
1191 | err = 1; | 1207 | err = -EINVAL; |
1192 | goto exit; | 1208 | goto exit; |
1193 | } | 1209 | } |
1194 | 1210 | ||
@@ -1206,7 +1222,7 @@ exit: | |||
1206 | * @vid_hdr: the volume identifier header to check | 1222 | * @vid_hdr: the volume identifier header to check |
1207 | * | 1223 | * |
1208 | * This function returns zero if the volume identifier header is all right, and | 1224 | * This function returns zero if the volume identifier header is all right, and |
1209 | * %1 if not. | 1225 | * %-EINVAL if not. |
1210 | */ | 1226 | */ |
1211 | static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum, | 1227 | static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum, |
1212 | const struct ubi_vid_hdr *vid_hdr) | 1228 | const struct ubi_vid_hdr *vid_hdr) |
@@ -1233,7 +1249,7 @@ fail: | |||
1233 | ubi_err("paranoid check failed for PEB %d", pnum); | 1249 | ubi_err("paranoid check failed for PEB %d", pnum); |
1234 | ubi_dbg_dump_vid_hdr(vid_hdr); | 1250 | ubi_dbg_dump_vid_hdr(vid_hdr); |
1235 | ubi_dbg_dump_stack(); | 1251 | ubi_dbg_dump_stack(); |
1236 | return 1; | 1252 | return -EINVAL; |
1237 | 1253 | ||
1238 | } | 1254 | } |
1239 | 1255 | ||
@@ -1243,7 +1259,7 @@ fail: | |||
1243 | * @pnum: the physical eraseblock number to check | 1259 | * @pnum: the physical eraseblock number to check |
1244 | * | 1260 | * |
1245 | * This function returns zero if the volume identifier header is all right, | 1261 | * This function returns zero if the volume identifier header is all right, |
1246 | * %1 if not, and a negative error code if an error occurred. | 1262 | * and a negative error code if not or if an error occurred. |
1247 | */ | 1263 | */ |
1248 | static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum) | 1264 | static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum) |
1249 | { | 1265 | { |
@@ -1270,7 +1286,7 @@ static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum) | |||
1270 | ubi_err("paranoid check failed for PEB %d", pnum); | 1286 | ubi_err("paranoid check failed for PEB %d", pnum); |
1271 | ubi_dbg_dump_vid_hdr(vid_hdr); | 1287 | ubi_dbg_dump_vid_hdr(vid_hdr); |
1272 | ubi_dbg_dump_stack(); | 1288 | ubi_dbg_dump_stack(); |
1273 | err = 1; | 1289 | err = -EINVAL; |
1274 | goto exit; | 1290 | goto exit; |
1275 | } | 1291 | } |
1276 | 1292 | ||
@@ -1282,6 +1298,61 @@ exit: | |||
1282 | } | 1298 | } |
1283 | 1299 | ||
1284 | /** | 1300 | /** |
1301 | * ubi_dbg_check_write - make sure write succeeded. | ||
1302 | * @ubi: UBI device description object | ||
1303 | * @buf: buffer with data which were written | ||
1304 | * @pnum: physical eraseblock number the data were written to | ||
1305 | * @offset: offset within the physical eraseblock the data were written to | ||
1306 | * @len: how many bytes were written | ||
1307 | * | ||
1308 | * This functions reads data which were recently written and compares it with | ||
1309 | * the original data buffer - the data have to match. Returns zero if the data | ||
1310 | * match and a negative error code if not or in case of failure. | ||
1311 | */ | ||
1312 | int ubi_dbg_check_write(struct ubi_device *ubi, const void *buf, int pnum, | ||
1313 | int offset, int len) | ||
1314 | { | ||
1315 | int err, i; | ||
1316 | |||
1317 | mutex_lock(&ubi->dbg_buf_mutex); | ||
1318 | err = ubi_io_read(ubi, ubi->dbg_peb_buf, pnum, offset, len); | ||
1319 | if (err) | ||
1320 | goto out_unlock; | ||
1321 | |||
1322 | for (i = 0; i < len; i++) { | ||
1323 | uint8_t c = ((uint8_t *)buf)[i]; | ||
1324 | uint8_t c1 = ((uint8_t *)ubi->dbg_peb_buf)[i]; | ||
1325 | int dump_len; | ||
1326 | |||
1327 | if (c == c1) | ||
1328 | continue; | ||
1329 | |||
1330 | ubi_err("paranoid check failed for PEB %d:%d, len %d", | ||
1331 | pnum, offset, len); | ||
1332 | ubi_msg("data differ at position %d", i); | ||
1333 | dump_len = max_t(int, 128, len - i); | ||
1334 | ubi_msg("hex dump of the original buffer from %d to %d", | ||
1335 | i, i + dump_len); | ||
1336 | print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, | ||
1337 | buf + i, dump_len, 1); | ||
1338 | ubi_msg("hex dump of the read buffer from %d to %d", | ||
1339 | i, i + dump_len); | ||
1340 | print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, | ||
1341 | ubi->dbg_peb_buf + i, dump_len, 1); | ||
1342 | ubi_dbg_dump_stack(); | ||
1343 | err = -EINVAL; | ||
1344 | goto out_unlock; | ||
1345 | } | ||
1346 | mutex_unlock(&ubi->dbg_buf_mutex); | ||
1347 | |||
1348 | return 0; | ||
1349 | |||
1350 | out_unlock: | ||
1351 | mutex_unlock(&ubi->dbg_buf_mutex); | ||
1352 | return err; | ||
1353 | } | ||
1354 | |||
1355 | /** | ||
1285 | * ubi_dbg_check_all_ff - check that a region of flash is empty. | 1356 | * ubi_dbg_check_all_ff - check that a region of flash is empty. |
1286 | * @ubi: UBI device description object | 1357 | * @ubi: UBI device description object |
1287 | * @pnum: the physical eraseblock number to check | 1358 | * @pnum: the physical eraseblock number to check |
@@ -1289,8 +1360,8 @@ exit: | |||
1289 | * @len: the length of the region to check | 1360 | * @len: the length of the region to check |
1290 | * | 1361 | * |
1291 | * This function returns zero if only 0xFF bytes are present at offset | 1362 | * This function returns zero if only 0xFF bytes are present at offset |
1292 | * @offset of the physical eraseblock @pnum, %1 if not, and a negative error | 1363 | * @offset of the physical eraseblock @pnum, and a negative error code if not |
1293 | * code if an error occurred. | 1364 | * or if an error occurred. |
1294 | */ | 1365 | */ |
1295 | int ubi_dbg_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len) | 1366 | int ubi_dbg_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len) |
1296 | { | 1367 | { |
@@ -1321,7 +1392,7 @@ fail: | |||
1321 | ubi_msg("hex dump of the %d-%d region", offset, offset + len); | 1392 | ubi_msg("hex dump of the %d-%d region", offset, offset + len); |
1322 | print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, | 1393 | print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, |
1323 | ubi->dbg_peb_buf, len, 1); | 1394 | ubi->dbg_peb_buf, len, 1); |
1324 | err = 1; | 1395 | err = -EINVAL; |
1325 | error: | 1396 | error: |
1326 | ubi_dbg_dump_stack(); | 1397 | ubi_dbg_dump_stack(); |
1327 | mutex_unlock(&ubi->dbg_buf_mutex); | 1398 | mutex_unlock(&ubi->dbg_buf_mutex); |