static int __init mtd_readtest_init(void) { uint64_t tmp; int err, i; printk(KERN_INFO "\n"); printk(KERN_INFO "=================================================\n"); if (dev < 0) { pr_info("Please specify a valid mtd-device via module parameter\n"); return -EINVAL; } pr_info("MTD device: %d\n", dev); mtd = get_mtd_device(NULL, dev); if (IS_ERR(mtd)) { err = PTR_ERR(mtd); pr_err("error: Cannot get MTD device\n"); return err; } if (mtd->writesize == 1) { pr_info("not NAND flash, assume page size is 512 " "bytes.\n"); pgsize = 512; } else pgsize = mtd->writesize; tmp = mtd->size; do_div(tmp, mtd->erasesize); ebcnt = tmp; pgcnt = mtd->erasesize / pgsize; pr_info("MTD device size %llu, eraseblock size %u, " "page size %u, count of eraseblocks %u, pages per " "eraseblock %u, OOB size %u\n", (unsigned long long)mtd->size, mtd->erasesize, pgsize, ebcnt, pgcnt, mtd->oobsize); err = -ENOMEM; iobuf = kmalloc(mtd->erasesize, GFP_KERNEL); if (!iobuf) goto out; iobuf1 = kmalloc(mtd->erasesize, GFP_KERNEL); if (!iobuf1) goto out; bbt = kzalloc(ebcnt, GFP_KERNEL); if (!bbt) goto out; err = mtdtest_scan_for_bad_eraseblocks(mtd, bbt, 0, ebcnt); if (err) goto out; /* Read all eraseblocks 1 page at a time */ pr_info("testing page read\n"); for (i = 0; i < ebcnt; ++i) { int ret; if (bbt[i]) continue; ret = read_eraseblock_by_page(i); if (ret) { dump_eraseblock(i); if (!err) err = ret; } ret = mtdtest_relax(); if (ret) { err = ret; goto out; } } if (err) pr_info("finished with errors\n"); else pr_info("finished\n"); out: kfree(iobuf); kfree(iobuf1); kfree(bbt); put_mtd_device(mtd); if (err) pr_info("error %d occurred\n", err); printk(KERN_INFO "=================================================\n"); return err; }
static int __init mtd_readtest_init(void) { uint64_t tmp; int err, i; printk(KERN_INFO "\n"); printk(KERN_INFO "=================================================\n"); printk(PRINT_PREF "MTD device: %d\n", dev); mtd = get_mtd_device(NULL, dev); if (IS_ERR(mtd)) { err = PTR_ERR(mtd); printk(PRINT_PREF "error: Cannot get MTD device\n"); return err; } if (mtd->writesize == 1) { printk(PRINT_PREF "not NAND flash, assume page size is 512 " "bytes.\n"); pgsize = 512; } else pgsize = mtd->writesize; tmp = mtd->size; do_div(tmp, mtd->erasesize); ebcnt = tmp; pgcnt = mtd->erasesize / mtd->writesize; printk(PRINT_PREF "MTD device size %llu, eraseblock size %u, " "page size %u, count of eraseblocks %u, pages per " "eraseblock %u, OOB size %u\n", (unsigned long long)mtd->size, mtd->erasesize, pgsize, ebcnt, pgcnt, mtd->oobsize); err = -ENOMEM; iobuf = kmalloc(mtd->erasesize, GFP_KERNEL); if (!iobuf) { printk(PRINT_PREF "error: cannot allocate memory\n"); goto out; } iobuf1 = kmalloc(mtd->erasesize, GFP_KERNEL); if (!iobuf1) { printk(PRINT_PREF "error: cannot allocate memory\n"); goto out; } err = scan_for_bad_eraseblocks(); if (err) goto out; /* Read all eraseblocks 1 page at a time */ printk(PRINT_PREF "testing page read\n"); for (i = 0; i < ebcnt; ++i) { int ret; if (bbt[i]) continue; ret = read_eraseblock_by_page(i); if (ret) { dump_eraseblock(i); if (!err) err = ret; } cond_resched(); } if (err) printk(PRINT_PREF "finished with errors\n"); else printk(PRINT_PREF "finished\n"); out: kfree(iobuf); kfree(iobuf1); kfree(bbt); put_mtd_device(mtd); if (err) printk(PRINT_PREF "error %d occurred\n", err); printk(KERN_INFO "=================================================\n"); return err; }
int main(int argc, char **argv) { int status = EXIT_SUCCESS, i, ret, blk; process_options(argc, argv); mtd_desc = libmtd_open(); if (!mtd_desc) return errmsg("can't initialize libmtd"); if (mtd_get_dev_info(mtd_desc, mtddev, &mtd) < 0) return errmsg("mtd_get_dev_info failed"); if (mtd.subpage_size == 1) { puts("not NAND flash, assume page size is 512 bytes."); pgsize = 512; } else { pgsize = mtd.subpage_size; } pgcnt = mtd.eb_size / pgsize; if (count < 0) count = mtd.eb_cnt; if (peb >= mtd.eb_cnt) return errmsg("Physical erase block %d is out of range!\n", peb); if ((peb + (count - 1)*(skip + 1)) >= mtd.eb_cnt) { return errmsg("Given block range exceeds block count of %d!\n", mtd.eb_cnt); } iobuf = xmalloc(mtd.eb_size); iobuf1 = xmalloc(mtd.eb_size); if ((fd = open(mtddev, O_RDWR)) == -1) { perror(mtddev); status = EXIT_FAILURE; goto out; } /* Read all eraseblocks 1 page at a time */ puts("testing page read"); for (i = 0; i < count; ++i) { blk = peb + i*(skip+1); if (mtd_is_bad(&mtd, fd, blk)) { printf("Skipping bad block %d\n", blk); continue; } ret = read_eraseblock_by_page(blk); if (ret && (flags & FLAG_VERBOSE)) { dump_eraseblock(blk); status = EXIT_FAILURE; } } out: free(iobuf); free(iobuf1); return status; }