fit.c 1.8 KB

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