summaryrefslogtreecommitdiff
path: root/libfstools/ubi.c
blob: f9d6e0aaa718ef941862297cae84afaebf7440c8 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
 * Copyright (C) 2014 Daniel Golle <daniel@makrotopia.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 2.1
 * as published by the Free Software Foundation
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>

#include "libfstools.h"
#include "volume.h"

/* fit for UBI_MAX_VOLUME_NAME and sysfs path lengths */
#define BUFLEN		128

/* could use libubi-tiny instead, but already had the code directly reading
 * from sysfs */
const char *const ubi_dir_name = "/sys/devices/virtual/ubi";

struct ubi_volume {
	struct volume v;
	int		ubi_num;
	int		ubi_volid;
};

static struct driver ubi_driver;

static int
read_uint_from_file(char *dirname, char *filename, unsigned int *i)
{
	FILE *f;
	char fname[BUFLEN];
	int ret = -1;

	snprintf(fname, sizeof(fname), "%s/%s", dirname, filename);

	f = fopen(fname, "r");
	if (!f)
		return ret;

	if (fscanf(f, "%u", i) == 1)
		ret = 0;

	fclose(f);
	return ret;
}

static char
*read_string_from_file(char *dirname, char *filename)
{
	FILE *f;
	char fname[BUFLEN];
	char buf[BUFLEN];
	int i;

	snprintf(fname, sizeof(fname), "%s/%s", dirname, filename);

	f = fopen(fname, "r");
	if (!f)
		return NULL;

	if (fgets(buf, sizeof(buf), f) == NULL)
		return NULL;

	fclose(f);

	/* make sure the string is \0 terminated */
	buf[sizeof(buf) - 1] = '\0';

	/* remove trailing whitespace */
	i = strlen(buf) - 1;
	while (i > 0 && buf[i] <= ' ')
		buf[i--] = '\0';

	return strdup(buf);
}

static unsigned int
test_open(char *filename)
{
	FILE *f;

	f = fopen(filename, "r");
	if (!f)
		return 0;

	fclose(f);
	return 1;
}

static int ubi_volume_init(struct volume *v)
{
	struct ubi_volume *p = container_of(v, struct ubi_volume, v);
	char voldir[BUFLEN], voldev[BUFLEN], *volname;
	unsigned int volsize;

	snprintf(voldir, sizeof(voldir), "%s/ubi%u/ubi%u_%u",
		ubi_dir_name, p->ubi_num, p->ubi_num, p->ubi_volid);

	snprintf(voldev, sizeof(voldev), "/dev/ubi%u_%u",
		p->ubi_num, p->ubi_volid);

	volname = read_string_from_file(voldir, "name");
	if (!volname)
		return -1;

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

	v->name = volname;
	v->type = UBIVOLUME;
	v->size = volsize;
	v->blk = strdup(voldev);

	return 0;
}

static struct volume *ubi_volume_match(char *name, int ubi_num, int volid)
{
	char voldir[BUFLEN], volblkdev[BUFLEN], *volname;
	struct ubi_volume *p;

	snprintf(voldir, sizeof(voldir), "%s/ubi%u/ubi%u_%u",
		ubi_dir_name, ubi_num, ubi_num, volid);

	snprintf(volblkdev, sizeof(volblkdev), "/dev/ubiblock%u_%u",
		ubi_num, volid);

	/* skip if ubiblock device exists */
	if (test_open(volblkdev))
		return NULL;

	/* todo: skip existing gluebi device for legacy support */

	volname = read_string_from_file(voldir, "name");
	if (!volname) {
		ULOG_ERR("Couldn't read %s/name\n", voldir);
		return NULL;
	}

	if (strncmp(name, volname, strlen(volname) + 1))
		return NULL;

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

	p->v.drv = &ubi_driver;
	p->ubi_num = ubi_num;
	p->ubi_volid = volid;

	return &p->v;
}

static struct volume *ubi_part_match(char *name, unsigned int ubi_num)
{
	DIR *ubi_dir;
	struct dirent *ubi_dirent;
	unsigned int volid;
	char devdir[BUFLEN];
	struct volume *ret = NULL;

	snprintf(devdir, sizeof(devdir), "%s/ubi%u",
		ubi_dir_name, ubi_num);

	ubi_dir = opendir(devdir);
	if (!ubi_dir)
		return ret;

	while ((ubi_dirent = readdir(ubi_dir)) != NULL) {
		if (strncmp(ubi_dirent->d_name, "ubi", 3))
			continue;

		if (sscanf(ubi_dirent->d_name, "ubi%*u_%u", &volid) != 1)
			continue;

		ret = ubi_volume_match(name, ubi_num, volid);
		if (ret)
			break;
	}
	closedir(ubi_dir);

	return ret;
}

static struct volume *ubi_volume_find(char *name)
{
	struct volume *ret = NULL;
	DIR *ubi_dir;
	struct dirent *ubi_dirent;
	unsigned int ubi_num;

	if (find_filesystem("ubifs"))
		return ret;

	ubi_dir = opendir(ubi_dir_name);
	/* check for os ubi support */
	if (!ubi_dir)
		return ret;

	/* probe ubi devices and volumes */
	while ((ubi_dirent = readdir(ubi_dir)) != NULL) {
		if (ubi_dirent->d_name[0] == '.')
			continue;

		sscanf(ubi_dirent->d_name, "ubi%u", &ubi_num);
		ret = ubi_part_match(name, ubi_num);
		if (ret)
			break;
	}
	closedir(ubi_dir);
	return ret;
}

static int ubi_volume_identify(struct volume *v)
{
	/* Todo: use libblkid-tiny on the ubi chardev */
	return FS_UBIFS;
}

static struct driver ubi_driver = {
	.name = "ubi",
	.find = ubi_volume_find,
	.init = ubi_volume_init,
	.identify = ubi_volume_identify,
};

DRIVER(ubi_driver);