summaryrefslogtreecommitdiff
path: root/ubi-utils/src/ubirmvol.c
blob: cb47b6cb029427ab49546b4757dbaee70609fad8 (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
/*
 * Copyright (c) International Business Machines Corp., 2006
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 * An utility to create UBI volumes.
 *
 * Autor: Artem B. Bityutskiy <dedekind@oktetlabs.ru>
 */

#include <stdio.h>
#include <stdint.h>
#include <getopt.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "config.h"
#include <libubi.h>

static void usage(void);
static int param_sanity_check(ubi_lib_t lib);
static int parse_options(int argc, char * const argv[]);

/*
 * The below variables are set by command line options.
 */
static int vol_id = -1;
static int devn = -1;

int main(int argc, char * const argv[])
{
	int err, old_errno;
	ubi_lib_t lib;

	err = parse_options(argc, argv);
	if (err)
		return err == 1 ? 0 : -1;

	if (devn == -1) {
		fprintf(stderr, "Device number was not specified\n");
		fprintf(stderr, "Use -h option for help\n");
		return -1;
	}

	err = ubi_open(&lib);
	if (err) {
		perror("Cannot open libubi");
		return -1;
	}

	err = param_sanity_check(lib);
	if (err) {
		perror("Input parameters check");
		fprintf(stderr, "Use -h option for help\n");
		goto out_libubi;
	}

	err = ubi_rmvol(lib, devn, vol_id);
	old_errno = errno;
	if (err < 0) {
		perror("Cannot remove volume");
		fprintf(stderr, "    err=%d errno=%d\n", err, old_errno);
		goto out_libubi;
	}

	return 0;

out_libubi:
	ubi_close(&lib);
	return -1;
}

/* 'getopt()' option string */
static const char *optstring = "hd:n:";

static int parse_options(int argc, char * const argv[])
{
	int opt = 0;

	while (opt != -1) {
		char *endp;

		opt = getopt(argc, argv, optstring);

		switch (opt) {
		case 'h':
			usage();
			return 1;
		case 'n':
			vol_id = strtoul(optarg, &endp, 0);
			if (*endp != '\0' || endp == optarg || vol_id < 0) {
				fprintf(stderr, "Bad volume "
					"number: \"%s\"\n", optarg);
				goto out;
			}
			break;
		case 'd':
			devn = strtoul(optarg, &endp, 0);
			if (*endp != '\0' || endp == optarg || devn < 0) {
				fprintf(stderr, "Bad UBI device "
					"number: \"%s\"\n", optarg);
				goto out;
			}
			break;
		case ':':
			fprintf(stderr, "Parameter is missing\n");
			goto out;
		case '?':
			fprintf(stderr, "Unknown parameter\n");
			goto out;
		case -1:
			break;
		default:
			fprintf(stderr, "Internal error\n");
			goto out;
		}
	}

	return 0;

out:
	errno = EINVAL;
	return -1;
}

static int param_sanity_check(ubi_lib_t lib)
{
	int err;
	struct ubi_info ubi;

	if (vol_id == -1) {
		fprintf(stderr, "Volume ID was not specified\n");
		goto out;
	}

	err = ubi_get_info(lib, &ubi);
	if (err)
		return -1;

	if (devn >= (int)ubi.dev_count) {
		fprintf(stderr, "Device %d does not exist\n", devn);
		goto out;
	}

	return 0;

out:
	errno = EINVAL;
	return -1;
}

static void usage(void)
{
	printf("Usage: ubi_rmvol OPTIONS\n"
	       "Command line options:\n"
	       "\t-h - this help message\n"
	       "\t-d - UBI device number\n"
	       "\t-n VOLNUM - volume number to remove\n");
}