mtdblock_ro.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Simple read-only (writable only for RAM) mtdblock driver
  4. *
  5. * Copyright © 2001-2010 David Woodhouse <dwmw2@infradead.org>
  6. */
  7. #include <linux/init.h>
  8. #include <linux/slab.h>
  9. #include <linux/mtd/mtd.h>
  10. #include <linux/mtd/blktrans.h>
  11. #include <linux/module.h>
  12. #include <linux/major.h>
  13. static int mtdblock_readsect(struct mtd_blktrans_dev *dev,
  14. unsigned long block, char *buf)
  15. {
  16. size_t retlen;
  17. int err;
  18. err = mtd_read(dev->mtd, (block * 512), 512, &retlen, buf);
  19. if (err && !mtd_is_bitflip(err))
  20. return 1;
  21. return 0;
  22. }
  23. static int mtdblock_writesect(struct mtd_blktrans_dev *dev,
  24. unsigned long block, char *buf)
  25. {
  26. size_t retlen;
  27. if (mtd_write(dev->mtd, (block * 512), 512, &retlen, buf))
  28. return 1;
  29. return 0;
  30. }
  31. static void mtdblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
  32. {
  33. struct mtd_blktrans_dev *dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  34. if (!dev)
  35. return;
  36. dev->mtd = mtd;
  37. dev->devnum = mtd->index;
  38. dev->size = mtd->size >> 9;
  39. dev->tr = tr;
  40. dev->readonly = 1;
  41. if (mtd_type_is_nand(mtd))
  42. pr_warn_ratelimited("%s: MTD device '%s' is NAND, please consider using UBI block devices instead.\n",
  43. tr->name, mtd->name);
  44. if (add_mtd_blktrans_dev(dev))
  45. kfree(dev);
  46. }
  47. static void mtdblock_remove_dev(struct mtd_blktrans_dev *dev)
  48. {
  49. del_mtd_blktrans_dev(dev);
  50. }
  51. static struct mtd_blktrans_ops mtdblock_tr = {
  52. .name = "mtdblock",
  53. .major = MTD_BLOCK_MAJOR,
  54. .part_bits = 0,
  55. .blksize = 512,
  56. .readsect = mtdblock_readsect,
  57. .writesect = mtdblock_writesect,
  58. .add_mtd = mtdblock_add_mtd,
  59. .remove_dev = mtdblock_remove_dev,
  60. .owner = THIS_MODULE,
  61. };
  62. module_mtd_blktrans(mtdblock_tr);
  63. MODULE_LICENSE("GPL");
  64. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  65. MODULE_DESCRIPTION("Simple read-only block device emulation access to MTD devices");