io.c (16217dc79dbc599b110dda26d0421df47904bba4) | io.c (3291b52f9ff0acc80a8ee3f92a960db937dccecb) |
---|---|
1/* 2 * Copyright (c) International Business Machines Corp., 2006 3 * Copyright (c) Nokia Corporation, 2006, 2007 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; either version 2 of the License, or 8 * (at your option) any later version. --- 488 unchanged lines hidden (view full) --- 497 */ 498static int nor_erase_prepare(struct ubi_device *ubi, int pnum) 499{ 500 int err; 501 size_t written; 502 loff_t addr; 503 uint32_t data = 0; 504 struct ubi_ec_hdr ec_hdr; | 1/* 2 * Copyright (c) International Business Machines Corp., 2006 3 * Copyright (c) Nokia Corporation, 2006, 2007 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; either version 2 of the License, or 8 * (at your option) any later version. --- 488 unchanged lines hidden (view full) --- 497 */ 498static int nor_erase_prepare(struct ubi_device *ubi, int pnum) 499{ 500 int err; 501 size_t written; 502 loff_t addr; 503 uint32_t data = 0; 504 struct ubi_ec_hdr ec_hdr; |
505 struct ubi_vid_io_buf vidb; |
|
505 506 /* 507 * Note, we cannot generally define VID header buffers on stack, 508 * because of the way we deal with these buffers (see the header 509 * comment in this file). But we know this is a NOR-specific piece of 510 * code, so we can do this. But yes, this is error-prone and we should 511 * (pre-)allocate VID header buffer instead. 512 */ --- 10 unchanged lines hidden (view full) --- 523 err = ubi_io_read_ec_hdr(ubi, pnum, &ec_hdr, 0); 524 if (err != UBI_IO_BAD_HDR_EBADMSG && err != UBI_IO_BAD_HDR && 525 err != UBI_IO_FF){ 526 err = mtd_write(ubi->mtd, addr, 4, &written, (void *)&data); 527 if(err) 528 goto error; 529 } 530 | 506 507 /* 508 * Note, we cannot generally define VID header buffers on stack, 509 * because of the way we deal with these buffers (see the header 510 * comment in this file). But we know this is a NOR-specific piece of 511 * code, so we can do this. But yes, this is error-prone and we should 512 * (pre-)allocate VID header buffer instead. 513 */ --- 10 unchanged lines hidden (view full) --- 524 err = ubi_io_read_ec_hdr(ubi, pnum, &ec_hdr, 0); 525 if (err != UBI_IO_BAD_HDR_EBADMSG && err != UBI_IO_BAD_HDR && 526 err != UBI_IO_FF){ 527 err = mtd_write(ubi->mtd, addr, 4, &written, (void *)&data); 528 if(err) 529 goto error; 530 } 531 |
531 err = ubi_io_read_vid_hdr(ubi, pnum, &vid_hdr, 0); | 532 ubi_init_vid_buf(ubi, &vidb, &vid_hdr); 533 ubi_assert(&vid_hdr == ubi_get_vid_hdr(&vidb)); 534 535 err = ubi_io_read_vid_hdr(ubi, pnum, &vidb, 0); |
532 if (err != UBI_IO_BAD_HDR_EBADMSG && err != UBI_IO_BAD_HDR && 533 err != UBI_IO_FF){ 534 addr += ubi->vid_hdr_aloffset; 535 err = mtd_write(ubi->mtd, addr, 4, &written, (void *)&data); 536 if (err) 537 goto error; 538 } 539 return 0; --- 450 unchanged lines hidden (view full) --- 990 dump_stack(); 991 return 1; 992} 993 994/** 995 * ubi_io_read_vid_hdr - read and check a volume identifier header. 996 * @ubi: UBI device description object 997 * @pnum: physical eraseblock number to read from | 536 if (err != UBI_IO_BAD_HDR_EBADMSG && err != UBI_IO_BAD_HDR && 537 err != UBI_IO_FF){ 538 addr += ubi->vid_hdr_aloffset; 539 err = mtd_write(ubi->mtd, addr, 4, &written, (void *)&data); 540 if (err) 541 goto error; 542 } 543 return 0; --- 450 unchanged lines hidden (view full) --- 994 dump_stack(); 995 return 1; 996} 997 998/** 999 * ubi_io_read_vid_hdr - read and check a volume identifier header. 1000 * @ubi: UBI device description object 1001 * @pnum: physical eraseblock number to read from |
998 * @vid_hdr: &struct ubi_vid_hdr object where to store the read volume 999 * identifier header | 1002 * @vidb: the volume identifier buffer to store data in |
1000 * @verbose: be verbose if the header is corrupted or wasn't found 1001 * 1002 * This function reads the volume identifier header from physical eraseblock | 1003 * @verbose: be verbose if the header is corrupted or wasn't found 1004 * 1005 * This function reads the volume identifier header from physical eraseblock |
1003 * @pnum and stores it in @vid_hdr. It also checks CRC checksum of the read | 1006 * @pnum and stores it in @vidb. It also checks CRC checksum of the read |
1004 * volume identifier header. The error codes are the same as in 1005 * 'ubi_io_read_ec_hdr()'. 1006 * 1007 * Note, the implementation of this function is also very similar to 1008 * 'ubi_io_read_ec_hdr()', so refer commentaries in 'ubi_io_read_ec_hdr()'. 1009 */ 1010int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum, | 1007 * volume identifier header. The error codes are the same as in 1008 * 'ubi_io_read_ec_hdr()'. 1009 * 1010 * Note, the implementation of this function is also very similar to 1011 * 'ubi_io_read_ec_hdr()', so refer commentaries in 'ubi_io_read_ec_hdr()'. 1012 */ 1013int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum, |
1011 struct ubi_vid_hdr *vid_hdr, int verbose) | 1014 struct ubi_vid_io_buf *vidb, int verbose) |
1012{ 1013 int err, read_err; 1014 uint32_t crc, magic, hdr_crc; | 1015{ 1016 int err, read_err; 1017 uint32_t crc, magic, hdr_crc; |
1015 void *p; | 1018 struct ubi_vid_hdr *vid_hdr = ubi_get_vid_hdr(vidb); 1019 void *p = vidb->buffer; |
1016 1017 dbg_io("read VID header from PEB %d", pnum); 1018 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 1019 | 1020 1021 dbg_io("read VID header from PEB %d", pnum); 1022 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 1023 |
1020 p = (char *)vid_hdr - ubi->vid_hdr_shift; | |
1021 read_err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset, 1022 ubi->vid_hdr_shift + UBI_VID_HDR_SIZE); 1023 if (read_err && read_err != UBI_IO_BITFLIPS && !mtd_is_eccerr(read_err)) 1024 return read_err; 1025 1026 magic = be32_to_cpu(vid_hdr->magic); 1027 if (magic != UBI_VID_HDR_MAGIC) { 1028 if (mtd_is_eccerr(read_err)) --- 46 unchanged lines hidden (view full) --- 1075 1076 return read_err ? UBI_IO_BITFLIPS : 0; 1077} 1078 1079/** 1080 * ubi_io_write_vid_hdr - write a volume identifier header. 1081 * @ubi: UBI device description object 1082 * @pnum: the physical eraseblock number to write to | 1024 read_err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset, 1025 ubi->vid_hdr_shift + UBI_VID_HDR_SIZE); 1026 if (read_err && read_err != UBI_IO_BITFLIPS && !mtd_is_eccerr(read_err)) 1027 return read_err; 1028 1029 magic = be32_to_cpu(vid_hdr->magic); 1030 if (magic != UBI_VID_HDR_MAGIC) { 1031 if (mtd_is_eccerr(read_err)) --- 46 unchanged lines hidden (view full) --- 1078 1079 return read_err ? UBI_IO_BITFLIPS : 0; 1080} 1081 1082/** 1083 * ubi_io_write_vid_hdr - write a volume identifier header. 1084 * @ubi: UBI device description object 1085 * @pnum: the physical eraseblock number to write to |
1083 * @vid_hdr: the volume identifier header to write | 1086 * @vidb: the volume identifier buffer to write |
1084 * 1085 * This function writes the volume identifier header described by @vid_hdr to 1086 * physical eraseblock @pnum. This function automatically fills the | 1087 * 1088 * This function writes the volume identifier header described by @vid_hdr to 1089 * physical eraseblock @pnum. This function automatically fills the |
1087 * @vid_hdr->magic and the @vid_hdr->version fields, as well as calculates 1088 * header CRC checksum and stores it at vid_hdr->hdr_crc. | 1090 * @vidb->hdr->magic and the @vidb->hdr->version fields, as well as calculates 1091 * header CRC checksum and stores it at vidb->hdr->hdr_crc. |
1089 * 1090 * This function returns zero in case of success and a negative error code in 1091 * case of failure. If %-EIO is returned, the physical eraseblock probably went 1092 * bad. 1093 */ 1094int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum, | 1092 * 1093 * This function returns zero in case of success and a negative error code in 1094 * case of failure. If %-EIO is returned, the physical eraseblock probably went 1095 * bad. 1096 */ 1097int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum, |
1095 struct ubi_vid_hdr *vid_hdr) | 1098 struct ubi_vid_io_buf *vidb) |
1096{ | 1099{ |
1100 struct ubi_vid_hdr *vid_hdr = ubi_get_vid_hdr(vidb); |
|
1097 int err; 1098 uint32_t crc; | 1101 int err; 1102 uint32_t crc; |
1099 void *p; | 1103 void *p = vidb->buffer; |
1100 1101 dbg_io("write VID header to PEB %d", pnum); 1102 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 1103 1104 err = self_check_peb_ec_hdr(ubi, pnum); 1105 if (err) 1106 return err; 1107 --- 4 unchanged lines hidden (view full) --- 1112 1113 err = self_check_vid_hdr(ubi, pnum, vid_hdr); 1114 if (err) 1115 return err; 1116 1117 if (ubi_dbg_power_cut(ubi, POWER_CUT_VID_WRITE)) 1118 return -EROFS; 1119 | 1104 1105 dbg_io("write VID header to PEB %d", pnum); 1106 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 1107 1108 err = self_check_peb_ec_hdr(ubi, pnum); 1109 if (err) 1110 return err; 1111 --- 4 unchanged lines hidden (view full) --- 1116 1117 err = self_check_vid_hdr(ubi, pnum, vid_hdr); 1118 if (err) 1119 return err; 1120 1121 if (ubi_dbg_power_cut(ubi, POWER_CUT_VID_WRITE)) 1122 return -EROFS; 1123 |
1120 p = (char *)vid_hdr - ubi->vid_hdr_shift; | |
1121 err = ubi_io_write(ubi, p, pnum, ubi->vid_hdr_aloffset, 1122 ubi->vid_hdr_alsize); 1123 return err; 1124} 1125 1126/** 1127 * self_check_not_bad - ensure that a physical eraseblock is not bad. 1128 * @ubi: UBI device description object --- 149 unchanged lines hidden (view full) --- 1278 * 1279 * This function returns zero if the volume identifier header is all right, 1280 * and a negative error code if not or if an error occurred. 1281 */ 1282static int self_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum) 1283{ 1284 int err; 1285 uint32_t crc, hdr_crc; | 1124 err = ubi_io_write(ubi, p, pnum, ubi->vid_hdr_aloffset, 1125 ubi->vid_hdr_alsize); 1126 return err; 1127} 1128 1129/** 1130 * self_check_not_bad - ensure that a physical eraseblock is not bad. 1131 * @ubi: UBI device description object --- 149 unchanged lines hidden (view full) --- 1281 * 1282 * This function returns zero if the volume identifier header is all right, 1283 * and a negative error code if not or if an error occurred. 1284 */ 1285static int self_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum) 1286{ 1287 int err; 1288 uint32_t crc, hdr_crc; |
1289 struct ubi_vid_io_buf *vidb; |
|
1286 struct ubi_vid_hdr *vid_hdr; 1287 void *p; 1288 1289 if (!ubi_dbg_chk_io(ubi)) 1290 return 0; 1291 | 1290 struct ubi_vid_hdr *vid_hdr; 1291 void *p; 1292 1293 if (!ubi_dbg_chk_io(ubi)) 1294 return 0; 1295 |
1292 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS); 1293 if (!vid_hdr) | 1296 vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS); 1297 if (!vidb) |
1294 return -ENOMEM; 1295 | 1298 return -ENOMEM; 1299 |
1296 p = (char *)vid_hdr - ubi->vid_hdr_shift; | 1300 vid_hdr = ubi_get_vid_hdr(vidb); 1301 p = vidb->buffer; |
1297 err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset, 1298 ubi->vid_hdr_alsize); 1299 if (err && err != UBI_IO_BITFLIPS && !mtd_is_eccerr(err)) 1300 goto exit; 1301 1302 crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC); 1303 hdr_crc = be32_to_cpu(vid_hdr->hdr_crc); 1304 if (hdr_crc != crc) { --- 4 unchanged lines hidden (view full) --- 1309 dump_stack(); 1310 err = -EINVAL; 1311 goto exit; 1312 } 1313 1314 err = self_check_vid_hdr(ubi, pnum, vid_hdr); 1315 1316exit: | 1302 err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset, 1303 ubi->vid_hdr_alsize); 1304 if (err && err != UBI_IO_BITFLIPS && !mtd_is_eccerr(err)) 1305 goto exit; 1306 1307 crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC); 1308 hdr_crc = be32_to_cpu(vid_hdr->hdr_crc); 1309 if (hdr_crc != crc) { --- 4 unchanged lines hidden (view full) --- 1314 dump_stack(); 1315 err = -EINVAL; 1316 goto exit; 1317 } 1318 1319 err = self_check_vid_hdr(ubi, pnum, vid_hdr); 1320 1321exit: |
1317 ubi_free_vid_hdr(ubi, vid_hdr); | 1322 ubi_free_vid_buf(vidb); |
1318 return err; 1319} 1320 1321/** 1322 * self_check_write - make sure write succeeded. 1323 * @ubi: UBI device description object 1324 * @buf: buffer with data which were written 1325 * @pnum: physical eraseblock number the data were written to --- 115 unchanged lines hidden --- | 1323 return err; 1324} 1325 1326/** 1327 * self_check_write - make sure write succeeded. 1328 * @ubi: UBI device description object 1329 * @buf: buffer with data which were written 1330 * @pnum: physical eraseblock number the data were written to --- 115 unchanged lines hidden --- |