aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/mtd/ubi/io.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/mtd/ubi/io.c')
-rw-r--r--drivers/mtd/ubi/io.c120
1 files changed, 95 insertions, 25 deletions
diff --git a/drivers/mtd/ubi/io.c b/drivers/mtd/ubi/io.c
index 8aa51e7a6a7d..b4ecc84c7549 100644
--- a/drivers/mtd/ubi/io.c
+++ b/drivers/mtd/ubi/io.c
@@ -143,7 +143,7 @@ int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset,
143 143
144 err = paranoid_check_not_bad(ubi, pnum); 144 err = paranoid_check_not_bad(ubi, pnum);
145 if (err) 145 if (err)
146 return err > 0 ? -EINVAL : err; 146 return err;
147 147
148 addr = (loff_t)pnum * ubi->peb_size + offset; 148 addr = (loff_t)pnum * ubi->peb_size + offset;
149retry: 149retry:
@@ -236,12 +236,12 @@ int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
236 236
237 err = paranoid_check_not_bad(ubi, pnum); 237 err = paranoid_check_not_bad(ubi, pnum);
238 if (err) 238 if (err)
239 return err > 0 ? -EINVAL : err; 239 return err;
240 240
241 /* The area we are writing to has to contain all 0xFF bytes */ 241 /* The area we are writing to has to contain all 0xFF bytes */
242 err = ubi_dbg_check_all_ff(ubi, pnum, offset, len); 242 err = ubi_dbg_check_all_ff(ubi, pnum, offset, len);
243 if (err) 243 if (err)
244 return err > 0 ? -EINVAL : err; 244 return err;
245 245
246 if (offset >= ubi->leb_start) { 246 if (offset >= ubi->leb_start) {
247 /* 247 /*
@@ -250,10 +250,10 @@ int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
250 */ 250 */
251 err = paranoid_check_peb_ec_hdr(ubi, pnum); 251 err = paranoid_check_peb_ec_hdr(ubi, pnum);
252 if (err) 252 if (err)
253 return err > 0 ? -EINVAL : err; 253 return err;
254 err = paranoid_check_peb_vid_hdr(ubi, pnum); 254 err = paranoid_check_peb_vid_hdr(ubi, pnum);
255 if (err) 255 if (err)
256 return err > 0 ? -EINVAL : err; 256 return err;
257 } 257 }
258 258
259 if (ubi_dbg_is_write_failure()) { 259 if (ubi_dbg_is_write_failure()) {
@@ -273,6 +273,21 @@ int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
273 } else 273 } else
274 ubi_assert(written == len); 274 ubi_assert(written == len);
275 275
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
276 return err; 291 return err;
277} 292}
278 293
@@ -348,7 +363,7 @@ retry:
348 363
349 err = ubi_dbg_check_all_ff(ubi, pnum, 0, ubi->peb_size); 364 err = ubi_dbg_check_all_ff(ubi, pnum, 0, ubi->peb_size);
350 if (err) 365 if (err)
351 return err > 0 ? -EINVAL : err; 366 return err;
352 367
353 if (ubi_dbg_is_erase_failure() && !err) { 368 if (ubi_dbg_is_erase_failure() && !err) {
354 dbg_err("cannot erase PEB %d (emulated)", pnum); 369 dbg_err("cannot erase PEB %d (emulated)", pnum);
@@ -542,7 +557,7 @@ int ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture)
542 557
543 err = paranoid_check_not_bad(ubi, pnum); 558 err = paranoid_check_not_bad(ubi, pnum);
544 if (err != 0) 559 if (err != 0)
545 return err > 0 ? -EINVAL : err; 560 return err;
546 561
547 if (ubi->ro_mode) { 562 if (ubi->ro_mode) {
548 ubi_err("read-only mode"); 563 ubi_err("read-only mode");
@@ -819,7 +834,7 @@ int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum,
819 834
820 err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr); 835 err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr);
821 if (err) 836 if (err)
822 return -EINVAL; 837 return err;
823 838
824 err = ubi_io_write(ubi, ec_hdr, pnum, 0, ubi->ec_hdr_alsize); 839 err = ubi_io_write(ubi, ec_hdr, pnum, 0, ubi->ec_hdr_alsize);
825 return err; 840 return err;
@@ -1083,7 +1098,7 @@ int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
1083 1098
1084 err = paranoid_check_peb_ec_hdr(ubi, pnum); 1099 err = paranoid_check_peb_ec_hdr(ubi, pnum);
1085 if (err) 1100 if (err)
1086 return err > 0 ? -EINVAL : err; 1101 return err;
1087 1102
1088 vid_hdr->magic = cpu_to_be32(UBI_VID_HDR_MAGIC); 1103 vid_hdr->magic = cpu_to_be32(UBI_VID_HDR_MAGIC);
1089 vid_hdr->version = UBI_VERSION; 1104 vid_hdr->version = UBI_VERSION;
@@ -1092,7 +1107,7 @@ int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
1092 1107
1093 err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr); 1108 err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr);
1094 if (err) 1109 if (err)
1095 return -EINVAL; 1110 return err;
1096 1111
1097 p = (char *)vid_hdr - ubi->vid_hdr_shift; 1112 p = (char *)vid_hdr - ubi->vid_hdr_shift;
1098 err = ubi_io_write(ubi, p, pnum, ubi->vid_hdr_aloffset, 1113 err = ubi_io_write(ubi, p, pnum, ubi->vid_hdr_aloffset,
@@ -1107,8 +1122,8 @@ int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
1107 * @ubi: UBI device description object 1122 * @ubi: UBI device description object
1108 * @pnum: physical eraseblock number to check 1123 * @pnum: physical eraseblock number to check
1109 * 1124 *
1110 * This function returns zero if the physical eraseblock is good, a positive 1125 * 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. 1126 * it is bad and a negative error code if an error occurred.
1112 */ 1127 */
1113static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum) 1128static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum)
1114{ 1129{
@@ -1120,7 +1135,7 @@ static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum)
1120 1135
1121 ubi_err("paranoid check failed for PEB %d", pnum); 1136 ubi_err("paranoid check failed for PEB %d", pnum);
1122 ubi_dbg_dump_stack(); 1137 ubi_dbg_dump_stack();
1123 return err; 1138 return err > 0 ? -EINVAL : err;
1124} 1139}
1125 1140
1126/** 1141/**
@@ -1130,7 +1145,7 @@ static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum)
1130 * @ec_hdr: the erase counter header to check 1145 * @ec_hdr: the erase counter header to check
1131 * 1146 *
1132 * This function returns zero if the erase counter header contains valid 1147 * This function returns zero if the erase counter header contains valid
1133 * values, and %1 if not. 1148 * values, and %-EINVAL if not.
1134 */ 1149 */
1135static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum, 1150static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum,
1136 const struct ubi_ec_hdr *ec_hdr) 1151 const struct ubi_ec_hdr *ec_hdr)
@@ -1156,7 +1171,7 @@ static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum,
1156fail: 1171fail:
1157 ubi_dbg_dump_ec_hdr(ec_hdr); 1172 ubi_dbg_dump_ec_hdr(ec_hdr);
1158 ubi_dbg_dump_stack(); 1173 ubi_dbg_dump_stack();
1159 return 1; 1174 return -EINVAL;
1160} 1175}
1161 1176
1162/** 1177/**
@@ -1164,8 +1179,8 @@ fail:
1164 * @ubi: UBI device description object 1179 * @ubi: UBI device description object
1165 * @pnum: the physical eraseblock number to check 1180 * @pnum: the physical eraseblock number to check
1166 * 1181 *
1167 * This function returns zero if the erase counter header is all right, %1 if 1182 * 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. 1183 * a negative error code if not or if an error occurred.
1169 */ 1184 */
1170static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum) 1185static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum)
1171{ 1186{
@@ -1188,7 +1203,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); 1203 ubi_err("paranoid check failed for PEB %d", pnum);
1189 ubi_dbg_dump_ec_hdr(ec_hdr); 1204 ubi_dbg_dump_ec_hdr(ec_hdr);
1190 ubi_dbg_dump_stack(); 1205 ubi_dbg_dump_stack();
1191 err = 1; 1206 err = -EINVAL;
1192 goto exit; 1207 goto exit;
1193 } 1208 }
1194 1209
@@ -1206,7 +1221,7 @@ exit:
1206 * @vid_hdr: the volume identifier header to check 1221 * @vid_hdr: the volume identifier header to check
1207 * 1222 *
1208 * This function returns zero if the volume identifier header is all right, and 1223 * This function returns zero if the volume identifier header is all right, and
1209 * %1 if not. 1224 * %-EINVAL if not.
1210 */ 1225 */
1211static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum, 1226static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum,
1212 const struct ubi_vid_hdr *vid_hdr) 1227 const struct ubi_vid_hdr *vid_hdr)
@@ -1233,7 +1248,7 @@ fail:
1233 ubi_err("paranoid check failed for PEB %d", pnum); 1248 ubi_err("paranoid check failed for PEB %d", pnum);
1234 ubi_dbg_dump_vid_hdr(vid_hdr); 1249 ubi_dbg_dump_vid_hdr(vid_hdr);
1235 ubi_dbg_dump_stack(); 1250 ubi_dbg_dump_stack();
1236 return 1; 1251 return -EINVAL;
1237 1252
1238} 1253}
1239 1254
@@ -1243,7 +1258,7 @@ fail:
1243 * @pnum: the physical eraseblock number to check 1258 * @pnum: the physical eraseblock number to check
1244 * 1259 *
1245 * This function returns zero if the volume identifier header is all right, 1260 * 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. 1261 * and a negative error code if not or if an error occurred.
1247 */ 1262 */
1248static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum) 1263static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum)
1249{ 1264{
@@ -1270,7 +1285,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); 1285 ubi_err("paranoid check failed for PEB %d", pnum);
1271 ubi_dbg_dump_vid_hdr(vid_hdr); 1286 ubi_dbg_dump_vid_hdr(vid_hdr);
1272 ubi_dbg_dump_stack(); 1287 ubi_dbg_dump_stack();
1273 err = 1; 1288 err = -EINVAL;
1274 goto exit; 1289 goto exit;
1275 } 1290 }
1276 1291
@@ -1282,6 +1297,61 @@ exit:
1282} 1297}
1283 1298
1284/** 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 */
1311int 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
1349out_unlock:
1350 mutex_unlock(&ubi->dbg_buf_mutex);
1351 return err;
1352}
1353
1354/**
1285 * ubi_dbg_check_all_ff - check that a region of flash is empty. 1355 * ubi_dbg_check_all_ff - check that a region of flash is empty.
1286 * @ubi: UBI device description object 1356 * @ubi: UBI device description object
1287 * @pnum: the physical eraseblock number to check 1357 * @pnum: the physical eraseblock number to check
@@ -1289,8 +1359,8 @@ exit:
1289 * @len: the length of the region to check 1359 * @len: the length of the region to check
1290 * 1360 *
1291 * This function returns zero if only 0xFF bytes are present at offset 1361 * 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 1362 * @offset of the physical eraseblock @pnum, and a negative error code if not
1293 * code if an error occurred. 1363 * or if an error occurred.
1294 */ 1364 */
1295int ubi_dbg_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len) 1365int ubi_dbg_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len)
1296{ 1366{
@@ -1321,7 +1391,7 @@ fail:
1321 ubi_msg("hex dump of the %d-%d region", offset, offset + len); 1391 ubi_msg("hex dump of the %d-%d region", offset, offset + len);
1322 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, 1392 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
1323 ubi->dbg_peb_buf, len, 1); 1393 ubi->dbg_peb_buf, len, 1);
1324 err = 1; 1394 err = -EINVAL;
1325error: 1395error:
1326 ubi_dbg_dump_stack(); 1396 ubi_dbg_dump_stack();
1327 mutex_unlock(&ubi->dbg_buf_mutex); 1397 mutex_unlock(&ubi->dbg_buf_mutex);