fit.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. #include "common.h"
  3. #define BUFLEN 64
  4. #define DEVPATHSTR_SIZE 15
  5. static const char *const fit0 = "/dev/fit0";
  6. static const char *const fitrw = "/dev/fitrw";
  7. struct devpath {
  8. char prefix[5];
  9. char device[11];
  10. };
  11. struct fit_volume {
  12. struct volume v;
  13. union {
  14. char devpathstr[DEVPATHSTR_SIZE+1];
  15. struct devpath devpath;
  16. } dev;
  17. };
  18. static struct driver fit_driver;
  19. static int fit_volume_identify(struct volume *v)
  20. {
  21. struct fit_volume *p = container_of(v, struct fit_volume, v);
  22. int ret = FS_NONE;
  23. FILE *f;
  24. f = fopen(p->dev.devpathstr, "r");
  25. if (!f)
  26. return ret;
  27. ret = block_file_identify(f, 0);
  28. fclose(f);
  29. return ret;
  30. }
  31. static int fit_volume_init(struct volume *v)
  32. {
  33. struct fit_volume *p = container_of(v, struct fit_volume, v);
  34. char voldir[BUFLEN];
  35. unsigned int volsize;
  36. snprintf(voldir, sizeof(voldir), "%s/%s", block_dir_name, p->dev.devpath.device);
  37. if (read_uint_from_file(voldir, "size", &volsize))
  38. return -1;
  39. v->type = BLOCKDEV;
  40. v->size = volsize << 9; /* size is returned in sectors of 512 bytes */
  41. v->blk = p->dev.devpathstr;
  42. return block_volume_format(v, 0, p->dev.devpathstr);
  43. }
  44. static struct volume *fit_volume_find(char *name)
  45. {
  46. struct fit_volume *p;
  47. struct stat buf;
  48. const char *fname;
  49. int ret;
  50. if (!strcmp(name, "rootfs"))
  51. fname = fit0;
  52. else if (!strcmp(name, "rootfs_data"))
  53. fname = fitrw;
  54. else
  55. return NULL;
  56. ret = stat(fname, &buf);
  57. if (ret)
  58. return NULL;
  59. p = calloc(1, sizeof(struct fit_volume));
  60. if (!p)
  61. return NULL;
  62. strncpy(p->dev.devpathstr, fname, DEVPATHSTR_SIZE);
  63. p->v.drv = &fit_driver;
  64. p->v.blk = p->dev.devpathstr;
  65. p->v.name = name;
  66. return &p->v;
  67. }
  68. static struct driver fit_driver = {
  69. .name = "fit",
  70. .priority = 30,
  71. .find = fit_volume_find,
  72. .init = fit_volume_init,
  73. .identify = fit_volume_identify,
  74. };
  75. DRIVER(fit_driver);