summaryrefslogtreecommitdiffstats
path: root/commands/ubi.c
blob: f37684102dfcda6fdfccfdd2620ec7f964ce1759 (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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#include <common.h>
#include <command.h>
#include <fs.h>
#include <fcntl.h>
#include <ioctl.h>
#include <errno.h>
#include <getopt.h>
#include <linux/mtd/mtd.h>
#include <linux/kernel.h>
#include <linux/stat.h>
#include <linux/mtd/mtd-abi.h>
#include <linux/mtd/ubi.h>
#include <mtd/ubi-user.h>
#include <mtd/ubi-media.h>

static int do_ubiupdatevol(int argc, char *argv[])
{
	int count, fd_img, fd_vol, ret = 0;
	uint64_t size = 0;
	struct stat st;
	void *buf;

	if (argc - optind < 2)
		return COMMAND_ERROR_USAGE;

	if (stat(argv[optind + 1], &st)) {
		perror("stat image");
		return 1;
	}

	size = st.st_size;

	if (size == FILESIZE_MAX) {
		printf("%s has unknown filesize, this is not supported\n",
		       argv[optind + 1]);
		return 1;
	}

	fd_img  = open(argv[optind + 1], O_RDONLY);
	if (fd_img < 0) {
		perror("open image");
		return 1;
	}

	fd_vol = open(argv[optind], O_WRONLY | O_TRUNC);
	if (fd_vol < 0) {
		perror("open volume");
		ret = 1;
		goto error_img;
	}

	ret = ioctl(fd_vol, UBI_IOCVOLUP, &size);
	if (ret) {
		printf("failed to start update: %s\n", strerror(-ret));
		goto error;
	}

	buf = xmalloc(RW_BUF_SIZE);

	while (size) {

		count = read(fd_img, buf, RW_BUF_SIZE);
		if (count < 0) {
			perror("read");
			ret = 1;
			break;
		}

		count = write(fd_vol, buf, count);
		if (count < 0) {
			perror("write");
			ret = 1;
			break;
		}

		size -= count;
	}

	free(buf);

error:
	close(fd_vol);
error_img:
	close(fd_img);
	return ret ? 1 : 0;
}


BAREBOX_CMD_HELP_START(ubiupdatevol)
BAREBOX_CMD_HELP_TEXT("Update UBI volume with an image.")
BAREBOX_CMD_HELP_END

BAREBOX_CMD_START(ubiupdatevol)
	.cmd		= do_ubiupdatevol,
	BAREBOX_CMD_DESC("Update an UBI volume")
	BAREBOX_CMD_OPTS("UBIVOL IMAGE")
	BAREBOX_CMD_GROUP(CMD_GRP_PART)
	BAREBOX_CMD_HELP(cmd_ubiupdatevol_help)
BAREBOX_CMD_END


static int do_ubimkvol(int argc, char *argv[])
{
	int opt;
	struct ubi_mkvol_req req;
	int fd, ret;
	uint64_t size;
	uint32_t ubinum;

	req.vol_type = UBI_DYNAMIC_VOLUME;
	req.vol_id = UBI_VOL_NUM_AUTO;

	while ((opt = getopt(argc, argv, "t:n:")) > 0) {
		switch (opt) {
		case 't':
			if (!strcmp(optarg, "dynamic"))
				req.vol_type = UBI_DYNAMIC_VOLUME;
			else if (!strcmp(optarg, "static"))
				req.vol_type = UBI_STATIC_VOLUME;
			else
				return COMMAND_ERROR_USAGE;
			break;
		case 'n':
			req.vol_id = simple_strtoul(optarg, NULL, 0);
			break;
		default:
			return COMMAND_ERROR_USAGE;
		}
	}

	if (argc - optind != 3)
		return COMMAND_ERROR_USAGE;

	size = strtoull_suffix(argv[optind+2], NULL, 0);
	req.name_len = min_t(int, strlen(argv[optind+1]), UBI_VOL_NAME_MAX);
	strncpy(req.name, argv[optind+1], req.name_len);
	req.name[req.name_len] = 0;

	req.bytes = size;
	req.alignment = 1;

	fd = open(argv[optind], O_WRONLY);
	if (fd < 0) {
		perror("open");
		return 1;
	}

	ret = ioctl(fd, UBI_IOCGETUBINUM, &ubinum);
	if (ret) {
		printf("failed to get ubinum: %s\n", strerror(-ret));
		goto err;
	}

	ret = ubi_api_create_volume(ubinum, &req);
err:
	close(fd);

	return ret ? 1 : 0;
}


BAREBOX_CMD_HELP_START(ubimkvol)
BAREBOX_CMD_HELP_TEXT("Create an UBI volume on UBIDEV with NAME and SIZE.")
BAREBOX_CMD_HELP_TEXT("If SIZE is 0 all available space is used for the volume.")
BAREBOX_CMD_HELP_OPT("-t <static|dynamic>",  "volume type, default is dynamic")
BAREBOX_CMD_HELP_OPT("-n <vol_id>",  "volume ID, default is dynamically assigned")
BAREBOX_CMD_HELP_END

BAREBOX_CMD_START(ubimkvol)
	.cmd		= do_ubimkvol,
	BAREBOX_CMD_DESC("create an UBI volume")
	BAREBOX_CMD_OPTS("[-tn] UBIDEV NAME SIZE")
	BAREBOX_CMD_GROUP(CMD_GRP_PART)
	BAREBOX_CMD_HELP(cmd_ubimkvol_help)
BAREBOX_CMD_END


static int do_ubiattach(int argc, char *argv[])
{
	int opt;
	struct mtd_info_user user;
	int fd, ret;
	int vid_hdr_offset = 0;
	int devnum = UBI_DEV_NUM_AUTO;

	while((opt = getopt(argc, argv, "d:O:")) > 0) {
		switch(opt) {
		case 'd':
			devnum = simple_strtoul(optarg, NULL, 0);
			break;
		case 'O':
			vid_hdr_offset = simple_strtoul(optarg, NULL, 0);
			break;
		default:
			return COMMAND_ERROR_USAGE;
		}
	}

	if (optind == argc)
		return COMMAND_ERROR_USAGE;

	fd = open(argv[optind], O_RDWR);
	if (fd < 0) {
		perror("open");
		return 1;
	}

	ret = ioctl(fd, MEMGETINFO, &user);
	if (ret) {
		printf("MEMGETINFO failed: %s\n", strerror(-ret));
		goto err;
	}

	ret = ubi_attach_mtd_dev(user.mtd, devnum, vid_hdr_offset, 20);
	if (ret < 0)
		printf("failed to attach: %s\n", strerror(-ret));
	else
		ret = 0;
err:
	close(fd);

	return ret ? 1 : 0;
}

BAREBOX_CMD_HELP_START(ubiattach)
BAREBOX_CMD_HELP_TEXT("Options:")
BAREBOX_CMD_HELP_OPT ("-d DEVNUM",  "device number")
BAREBOX_CMD_HELP_OPT ("-O OFFS",  "VID header offset")
BAREBOX_CMD_HELP_END

BAREBOX_CMD_START(ubiattach)
	.cmd		= do_ubiattach,
	BAREBOX_CMD_DESC("attach mtd device to UBI")
	BAREBOX_CMD_OPTS("[-dO] MTDDEV")
	BAREBOX_CMD_GROUP(CMD_GRP_PART)
	BAREBOX_CMD_HELP(cmd_ubiattach_help)
BAREBOX_CMD_END

static int do_ubidetach(int argc, char *argv[])
{
	int fd, ret;
	struct mtd_info_user user;

	if (argc != 2)
		return COMMAND_ERROR_USAGE;

	fd = open(argv[optind], O_RDWR);
	if (fd < 0) {
		int ubi_num = simple_strtoul(argv[1], NULL, 0);
		ret = ubi_detach(ubi_num);
		goto out;
	}

	ret = ioctl(fd, MEMGETINFO, &user);
	if (!ret) {
		int ubi_num = ubi_num_get_by_mtd(user.mtd);
		if (ubi_num < 0) {
			ret = ubi_num;
			goto out;
		}

		ret = ubi_detach(ubi_num);
		if (!ret)
			goto out_close;
	}

out_close:
	close(fd);

out:
	if (ret)
		printf("failed to detach: %s\n", strerror(-ret));

	return ret;
}

BAREBOX_CMD_START(ubidetach)
	.cmd		= do_ubidetach,
	BAREBOX_CMD_DESC("detach an UBI device")
	BAREBOX_CMD_OPTS("mtd device/UBINUM")
	BAREBOX_CMD_GROUP(CMD_GRP_PART)
BAREBOX_CMD_END

static int do_ubirmvol(int argc, char *argv[])
{
	struct ubi_volume_desc *desc;
	uint32_t ubinum;
	int fd, ret;

	if (argc != 3)
		return COMMAND_ERROR_USAGE;

	fd = open(argv[1], O_WRONLY);
	if (fd < 0) {
		perror("open");
		return 1;
	}

	ret = ioctl(fd, UBI_IOCGETUBINUM, &ubinum);
	if (ret) {
		printf("failed to get ubinum: %s\n", strerror(-ret));
		goto err;
	}

	desc = ubi_open_volume_nm(ubinum, argv[2], UBI_EXCLUSIVE);
	if (IS_ERR(desc)) {
		ret = PTR_ERR(desc);
		printf("failed to open volume %s: %s\n", argv[2], strerror(-ret));
		goto err;
	}

	ret = ubi_api_remove_volume(desc, 0);
	if (ret)
		printf("failed to remove volume %s: %s\n", argv[2], strerror(-ret));

	ubi_close_volume(desc);
err:
	close(fd);

	return ret ? 1 : 0;
}

BAREBOX_CMD_HELP_START(ubirmvol)
BAREBOX_CMD_HELP_TEXT("Delete UBI volume NAME from UBIDEV")
BAREBOX_CMD_HELP_END

BAREBOX_CMD_START(ubirmvol)
	.cmd		= do_ubirmvol,
	BAREBOX_CMD_DESC("delete an UBI volume")
	BAREBOX_CMD_OPTS("UBIDEV NAME")
	BAREBOX_CMD_GROUP(CMD_GRP_PART)
	BAREBOX_CMD_HELP(cmd_ubirmvol_help)
BAREBOX_CMD_END

static int get_vol_id(u32 ubi_num, const char *name)
{
	struct ubi_volume_desc *desc;
	struct ubi_volume_info vi;

	desc = ubi_open_volume_nm(ubi_num, name, UBI_READONLY);
	if(IS_ERR(desc))
		return PTR_ERR(desc);

	ubi_get_volume_info(desc, &vi);
	ubi_close_volume(desc);

	return vi.vol_id;
};

static int do_ubirename(int argc, char *argv[])
{
	struct ubi_rnvol_req req;
	u32 ubi_num;
	int i, j, fd, ret;

	if ((argc < 4) || (argc % 2))
		return COMMAND_ERROR_USAGE;

	req.count = (argc / 2) - 1;
	if (req.count > UBI_MAX_RNVOL) {
		printf("too many volume renames. (max: %u)\n", UBI_MAX_RNVOL);
		return COMMAND_ERROR_USAGE;
	}

	fd = open(argv[1], O_WRONLY);
	if (fd < 0) {
		perror("unable to open the UBI device");
		return 1;
	}

	ret = ioctl(fd, UBI_IOCGETUBINUM, &ubi_num);

	close(fd);

	if (ret) {
		printf("failed to get ubi num for %s: %s\n", argv[1], strerror(-ret));
		return 1;
	}

	for (i = 2, j = 0; i < argc; ++j, i += 2) {
		req.ents[j].vol_id = get_vol_id(ubi_num, argv[i]);

		if (req.ents[j].vol_id < 0) {
			printf("Volume '%s' does not exist on %s\n", argv[i], argv[1]);
			ret = -EINVAL;
			goto err;
		}

		strncpy(req.ents[j].name, argv[i + 1], UBI_MAX_VOLUME_NAME);
		req.ents[j].name_len = strlen(req.ents[j].name);
	}

	ret = ubi_api_rename_volumes(ubi_num, &req);
	if (ret)
		printf("failed to rename: %s", strerror(-ret));
err:
	return ret ? 1 : 0;
};

BAREBOX_CMD_HELP_START(ubirename)
BAREBOX_CMD_HELP_TEXT("Rename UBI volume(s) from UBIDEV")
BAREBOX_CMD_HELP_END

BAREBOX_CMD_START(ubirename)
	.cmd		= do_ubirename,
	BAREBOX_CMD_DESC("rename UBI volume(s)")
	BAREBOX_CMD_OPTS("UBIDEV OLD_NAME NEW_NAME [OLD_NAME NEW_NAME ...]")
	BAREBOX_CMD_GROUP(CMD_GRP_PART)
	BAREBOX_CMD_HELP(cmd_ubirename_help)
BAREBOX_CMD_END