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