summaryrefslogtreecommitdiffstats
path: root/libfstools/fit.c
blob: a8f0c6648ad30ac81821ec388a0505c444b4f32c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// SPDX-License-Identifier: GPL-2.0-or-later

#include "common.h"

#define BUFLEN 64
#define DEVPATHSTR_SIZE 15

static const char *const fit0 = "/dev/fit0";
static const char *const fitrw = "/dev/fitrw";

struct devpath {
	char prefix[5];
	char device[11];
};

struct fit_volume {
	struct volume v;
	union {
		char devpathstr[DEVPATHSTR_SIZE+1];
		struct devpath devpath;
	} dev;
};

static struct driver fit_driver;

static int fit_volume_identify(struct volume *v)
{
	struct fit_volume *p = container_of(v, struct fit_volume, v);
	int ret = FS_NONE;
	FILE *f;

	f = fopen(p->dev.devpathstr, "r");
	if (!f)
		return ret;

	ret = block_file_identify(f, 0);

	fclose(f);

	return ret;
}

static int fit_volume_init(struct volume *v)
{
	struct fit_volume *p = container_of(v, struct fit_volume, v);
	char voldir[BUFLEN];
	unsigned int volsize;

	snprintf(voldir, sizeof(voldir), "%s/%s", block_dir_name, p->dev.devpath.device);

	if (read_uint_from_file(voldir, "size", &volsize))
		return -1;

	v->type = BLOCKDEV;
	v->size = volsize << 9; /* size is returned in sectors of 512 bytes */
	v->blk = p->dev.devpathstr;

	return block_volume_format(v, 0, p->dev.devpathstr);
}

static struct volume *fit_volume_find(char *name)
{
	struct fit_volume *p;
	struct stat buf;
	const char *fname;
	int ret;

	if (!strcmp(name, "rootfs"))
		fname = fit0;
	else if (!strcmp(name, "rootfs_data"))
		fname = fitrw;
	else
		return NULL;

	ret = stat(fname, &buf);
	if (ret)
		return NULL;

	p = calloc(1, sizeof(struct fit_volume));
	if (!p)
		return NULL;

	strncpy(p->dev.devpathstr, fname, DEVPATHSTR_SIZE);
	p->v.drv = &fit_driver;
	p->v.blk = p->dev.devpathstr;
	p->v.name = name;

	return &p->v;
}

static struct driver fit_driver = {
	.name = "fit",
	.priority = 30,
	.find = fit_volume_find,
	.init = fit_volume_init,
	.identify = fit_volume_identify,
};

DRIVER(fit_driver);