forked from SanDisk-Open-Source/ufs-utils
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptions.c
351 lines (290 loc) · 7.17 KB
/
options.c
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
// SPDX-License-Identifier: GPL-2.0-or-later
/* Copyright (C) 2019 Western Digital Corporation or its affiliates */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <errno.h>
#include "options.h"
#include "ufs.h"
static int verify_and_set_idn(struct tool_options *options);
static int verify_read(struct tool_options *options);
static int verify_write(struct tool_options *options);
static int verify_and_set_flag_operation(int opr_type,
struct tool_options *options);
static int verify_and_set_device_path(struct tool_options *options);
static int verify_arg_and_set_default(struct tool_options *options);
static int verify_and_set_index(struct tool_options *options);
static int verify_and_set_selector(struct tool_options *options);
int init_options(int opt_cnt, char *opt_arr[], struct tool_options *options)
{
int rc = -EINVAL;
int curr_opt = 0;
while (-1 != (curr_opt = getopt(opt_cnt, opt_arr, "t:p:w:i:s:rocea"))) {
switch (curr_opt) {
case 'a':
rc = verify_read(options);
if (!rc)
options->opr = READ_ALL;
break;
case 't':
rc = verify_and_set_idn(options);
break;
case 'r':
rc = verify_read(options);
if (!rc)
options->opr = READ;
break;
case 'w':
rc = verify_write(options);
if (!rc)
options->opr = WRITE;
break;
case 'c':
rc = verify_and_set_flag_operation(CLEAR_FLAG,
options);
break;
case 'e':
rc = verify_and_set_flag_operation(SET_FLAG, options);
break;
case 'o':
rc = verify_and_set_flag_operation(TOGGLE_FLAG,
options);
break;
case 'p':
rc = verify_and_set_device_path(options);
break;
case 'i':
rc = verify_and_set_index(options);
break;
case 's':
rc = verify_and_set_selector(options);
break;
default:
rc = -EINVAL;
break;
}
if (rc)
break;
}
if (!rc)
rc = verify_arg_and_set_default(options);
return rc;
}
static int verify_and_set_index(struct tool_options *options)
{
int index = INVALID;
if (options->index != INVALID) {
print_error("duplicated index");
goto out;
}
/* In case atoi returned 0 . Check that is real 0 and not error
* arguments . Also check that the value is in correct range
*/
index = atoi(optarg);
if (!optarg || (index == 0 && strcmp(optarg, "0")) || index < 0) {
print_error("Invalid argument for index");
goto out;
}
options->index = index;
return OK;
out:
return ERROR;
}
static int verify_and_set_selector(struct tool_options *options)
{
int selector = INVALID;
if (options->selector != INVALID) {
print_error("duplicated selector");
goto out;
}
/* In case atoi returned 0 . Check that is real 0 and not error
* arguments . Also check that the value is in correct range
*/
selector = atoi(optarg);
if (!optarg || (selector == 0 && strcmp(optarg, "0")) || selector < 0) {
print_error("Invalid argument for selector");
goto out;
}
options->selector = selector;
return OK;
out:
return ERROR;
}
static int verify_and_set_idn(struct tool_options *options)
{
int idn = INVALID;
if (options->idn != INVALID) {
print_error("duplicated desc type option");
goto out;
}
/* In case atoi returned 0. Check that is real 0 and not error
* arguments. Also check that the value is in correct range
*/
idn = atoi(optarg);
if (!optarg || (idn == 0 && strcmp(optarg, "0")) || idn < 0) {
print_error("Invalid argument for idn");
goto out;
}
switch (options->config_type_inx) {
case DESC_TYPE:
if (idn > QUERY_DESC_IDN_MAX) {
print_error("Invalid descriptor idn %d", idn);
goto out;
}
break;
case ATTR_TYPE:
if (idn >= QUERY_ATTR_IDN_MAX) {
print_error("Invalid attr idn %d", idn);
goto out;
}
break;
case FLAG_TYPE:
if (idn > QUERY_FLAG_IDN_PERMANENTLYDISABLEFW) {
print_error("Invalid flag idn %d", idn);
goto out;
}
break;
default:
print_error("Invalid UFS configuration type %d", idn);
goto out;
}
options->idn = idn;
return OK;
out:
return ERROR;
}
static int verify_arg_and_set_default(struct tool_options *options)
{
if (options->path[0] == '\0') {
print_error("Missing device path type");
goto out;
}
if (options->opr == INVALID)
options->opr = READ;
if (options->opr == WRITE && !options->data) {
print_error("Data missed for the write operation");
goto out;
}
if (options->config_type_inx != ERR_HIST_TYPE &&
options->opr != READ_ALL &&
options->idn == INVALID) {
print_error("The type idn is missed");
goto out;
}
if (options->config_type_inx == DESC_TYPE &&
options->idn == QUERY_DESC_IDN_STRING &&
options->index == INVALID) {
print_error("The index is missed");
goto out;
}
if (options->index == INVALID)
options->index = 0;
if (options->selector == INVALID)
options->selector = 0;
return OK;
out:
return ERROR;
}
static int verify_and_set_device_path(struct tool_options *options)
{
if (options->path[0] != '\0') {
print_error("Duplicate Device path %d", options->path[0]);
goto out;
}
if (!optarg || optarg[0] == 0) {
print_error("Device path missed");
goto out;
}
if (strlen(optarg) >= PATH_MAX) {
print_error("Device path is too long");
goto out;
}
strcpy(options->path, optarg);
return OK;
out:
return ERROR;
}
static int verify_read(struct tool_options *options)
{
if (options->opr != INVALID) {
print_error("duplicated operation option(read) 2%d",
options->opr);
goto out;
}
return OK;
out:
return ERROR;
}
static int verify_write(struct tool_options *options)
{
char *endptr;
errno = 0;
if (options->opr != INVALID) {
print_error("duplicated operation option(write)");
goto out;
}
if (!optarg || optarg[0] == 0) {
print_error("Data is missed");
goto out;
}
if (options->config_type_inx == DESC_TYPE) {
int arg_len = strlen(optarg);
int str_desc_max_len = QUERY_DESC_STRING_MAX_SIZE/2 - 2;
if (options->idn != QUERY_DESC_IDN_CONFIGURAION &&
options->idn != QUERY_DESC_IDN_STRING) {
print_error("write unavailable for descriptor = %d",
options->idn);
goto out;
}
if (arg_len > str_desc_max_len) {
print_error("Input data is too big");
goto out;
}
options->data = (char *)malloc(QUERY_DESC_STRING_MAX_SIZE);
if (!options->data) {
print_error("Memory Allocation problem");
goto out;
}
strcpy(options->data, optarg);
}
if (options->config_type_inx == FLAG_TYPE) {
print_error("Please use 'c', 'e', or 'o' for flag operations");
goto out;
}
if (options->config_type_inx == ATTR_TYPE) {
options->data = (__u32 *)calloc(1, sizeof(__u32));
if (!options->data) {
print_error("Memory Allocation problem");
goto out;
}
*(__u32 *)options->data = strtol(optarg, &endptr, 16);
if (errno != 0 || *endptr != '\0') {
print_error("Wrong data");
goto out;
}
}
return OK;
out:
return ERROR;
}
static int
verify_and_set_flag_operation(int opr_type, struct tool_options *options)
{
if (options->opr != INVALID) {
print_error("duplicated operation option");
goto out;
}
if (options->config_type_inx != FLAG_TYPE) {
print_error("-c | -o | -e operation only for the flag type");
goto out;
}
if (opr_type < CLEAR_FLAG || opr_type > TOGGLE_FLAG) {
print_error("Incorrect operation for the flag type");
goto out;
}
options->opr = opr_type;
return OK;
out:
return ERROR;
}