mtdblock_ro.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 <[email protected]>
  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. if (mtd_read(dev->mtd, (block * 512), 512, &retlen, buf))
  18. return 1;
  19. return 0;
  20. }
  21. static int mtdblock_writesect(struct mtd_blktrans_dev *dev,
  22. unsigned long block, char *buf)
  23. {
  24. size_t retlen;
  25. if (mtd_write(dev->mtd, (block * 512), 512, &retlen, buf))
  26. return 1;
  27. return 0;
  28. }
  29. static void mtdblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
  30. {
  31. struct mtd_blktrans_dev *dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  32. if (!dev)
  33. return;
  34. dev->mtd = mtd;
  35. dev->devnum = mtd->index;
  36. dev->size = mtd->size >> 9;
  37. dev->tr = tr;
  38. dev->readonly = 1;
  39. if (mtd_type_is_nand(mtd))
  40. pr_warn("%s: MTD device '%s' is NAND, please consider using UBI block devices instead.\n",
  41. tr->name, mtd->name);
  42. if (add_mtd_blktrans_dev(dev))
  43. kfree(dev);
  44. }
  45. static void mtdblock_remove_dev(struct mtd_blktrans_dev *dev)
  46. {
  47. del_mtd_blktrans_dev(dev);
  48. }
  49. static struct mtd_blktrans_ops mtdblock_tr = {
  50. .name = "mtdblock",
  51. .major = MTD_BLOCK_MAJOR,
  52. .part_bits = 0,
  53. .blksize = 512,
  54. .readsect = mtdblock_readsect,
  55. .writesect = mtdblock_writesect,
  56. .add_mtd = mtdblock_add_mtd,
  57. .remove_dev = mtdblock_remove_dev,
  58. .owner = THIS_MODULE,
  59. };
  60. module_mtd_blktrans(mtdblock_tr);
  61. MODULE_LICENSE("GPL");
  62. MODULE_AUTHOR("David Woodhouse <[email protected]>");
  63. MODULE_DESCRIPTION("Simple read-only block device emulation access to MTD devices");