-
Notifications
You must be signed in to change notification settings - Fork 1
/
mosys.c
319 lines (280 loc) · 7.82 KB
/
mosys.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
/*
* Copyright 2012, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include "mosys/alloc.h"
#include "mosys/big_lock.h"
#include "mosys/command_list.h"
#include "mosys/globals.h"
#include "mosys/kv_pair.h"
#include "mosys/log.h"
#include "mosys/output.h"
#include "mosys/platform.h"
static void usage(void)
{
printf("usage: %s [options] [commands]\n\n"
" Options:\n"
" -k print data in key=value format\n"
" -l print data in long format\n"
" -s [key] print value for single key\n"
" -v verbose (can be used multiple times)\n"
" -f force (ignore mosys lock, sanity checks, etc)\n"
" -t display command tree for detected platform\n"
" -S print supported platform IDs\n"
" -p [id] specify platform id (bypass auto-detection)\n"
" -h print this help\n"
" -V print version\n"
"\n", PROGRAM);
}
static void sub_list(struct platform_cmd *sub)
{
struct platform_cmd *_sub;
if (!sub)
return;
printf(" Commands:\n");
for (_sub = sub->arg.sub; _sub && _sub->name; _sub++) {
if (_sub->desc)
printf(" %-12s %s\n", _sub->name, _sub->desc);
}
printf("\n");
}
static int sub_main(struct platform_intf *intf,
struct platform_cmd *sub, int argc, char **argv)
{
struct platform_cmd *cmd;
if (!intf || !sub) {
errno = ENOSYS;
return -1;
}
switch (sub->type) {
case ARG_TYPE_GETTER:
case ARG_TYPE_SETTER:
if (!sub->arg.func) {
lprintf(LOG_ERR, "Undefined Function\n");
return -1;
}
/* this is a function, it might have more args */
if (argc > 0 && strcmp(argv[0], "help") == 0) {
platform_cmd_usage(sub);
return 0;
}
/* run command handler */
return sub->arg.func(intf, sub, argc, argv);
case ARG_TYPE_SUB:
if (argc == 0) {
sub_list(sub);
return -1; /* generic error */
}
/* this is a sub-command, we must have some more args */
if (argc == 0 || strcmp(argv[0], "help") == 0) {
sub_list(sub);
return 0;
}
/* search for matching sub-command */
for (cmd = sub->arg.sub; cmd && cmd->name; cmd++) {
if (strlen(cmd->name) != strlen(argv[0]))
continue;
if (strcmp(cmd->name, argv[0]) == 0) {
lprintf(LOG_DEBUG, "Subcommand %s (%s)\n",
cmd->name, cmd->desc);
return sub_main(intf, cmd, argc - 1,
&(argv[1]));
}
}
break;
default:
lprintf(LOG_ERR, "Unknown subcommand type\n");
return -1;
}
lprintf(LOG_WARNING, "Command not found\n\n");
sub_list(sub);
errno = EINVAL; /* unknown or invalid subcommand argument */
return -1;
}
static int intf_main(struct platform_intf *intf, int argc, char **argv)
{
struct platform_cmd **_sub, *sub;
int do_list = 0;
int ret = 0;
if (!intf || !intf->sub) {
lprintf(LOG_ERR, "No commands defined for this platform\n");
errno = ENOSYS;
return -1;
}
if (argc == 0 || strcmp(argv[0], "help") == 0) {
do_list = 1;
usage();
printf(" Commands:\n");
}
/* go through subcommand list for this interface */
for (_sub = intf->sub; _sub && *_sub; _sub++) {
sub = *_sub;
if (do_list) {
/*FIXME: if the intf had a main sub, call sub_list */
printf(" %-12s %s\n", sub->name, sub->desc);
continue;
}
lprintf(LOG_DEBUG, "Command: %s (%s)\n", sub->name, argv[0]);
/* is this sub-command is the one they asked for? */
if (strlen(sub->name) != strlen(argv[0]))
continue;
if (strcmp(sub->name, argv[0]) == 0) {
lprintf(LOG_DEBUG, "Found command %s (%s)\n",
sub->name, sub->desc);
return sub_main(intf, sub, argc - 1, &(argv[1]));
}
}
if (do_list) {
printf("\n");
if (argc == 0)
ret = -1;
return ret;
}
lprintf(LOG_WARNING, "Command not found\n\n");
errno = ENOSYS;
return intf_main(intf, 0, NULL); /* trigger a help listing */
}
#define LOCK_TIMEOUT_SECS 180
int mosys_main(int argc, char **argv)
{
int rc, errsv;
int argflag;
int verbose = 0;
int force_lock = 0;
int print_platforms_opt = 0;
int showtree = 0;
char *p_opt = NULL;
struct platform_intf *intf;
enum kv_pair_style style = KV_STYLE_VALUE;
while ((argflag = getopt(argc, argv, "klvftSs:p:Vh")) > 0) {
switch (argflag) {
case 'k':
style = KV_STYLE_PAIR;
break;
case 'l':
style = KV_STYLE_LONG;
break;
case 's':
style = KV_STYLE_SINGLE;
if (!optarg) {
usage();
exit(EXIT_FAILURE);
}
kv_set_single_key(optarg);
break;
case 'v':
verbose++;
break;
case 'f':
force_lock = 1;
break;
case 't':
showtree = 1;
break;
case 'S':
print_platforms_opt = 1;
break;
case 'p':
p_opt = optarg;
break;
case 'V':
printf("mosys version %s\n", VERSION);
exit(EXIT_SUCCESS);
case 'h':
usage();
exit(EXIT_SUCCESS);
default:
usage();
exit(EXIT_FAILURE);
}
}
mosys_set_kv_pair_style(style);
if (print_platforms_opt)
return print_platforms();
/*
* Init the logging system and the default log output file (stderr).
* Anything logged with a level above CONFIG_LOGLEVEL will not be logged
* at all. We use the number of "-v" arguments on the commandline as
* a bias against the default threshold. The more times you use
* "-v", the greater your logging level becomes, and the more
* information will be printed.
*/
mosys_log_init(PROGRAM, CONFIG_LOGLEVEL+verbose, NULL);
#if defined(CONFIG_USE_IPC_LOCK)
/* try to get lock */
if (!force_lock && (mosys_acquire_big_lock(LOCK_TIMEOUT_SECS) < 0)) {
rc = -1;
goto do_exit_1;
}
#endif
/* set the global verbosity level */
mosys_set_verbosity(CONFIG_LOGLEVEL+verbose);
/* try to identify the platform */
intf = mosys_platform_setup(p_opt);
if (!intf) {
lprintf(LOG_ERR, "Platform not supported\n");
rc = -1;
goto do_exit_2;
}
lprintf(LOG_DEBUG, "Platform: %s\n", intf->name);
if (showtree) {
print_tree(intf);
rc = 0;
goto do_exit_3;
}
/* run command */
errno = 0;
rc = intf_main(intf, argc - optind, &(argv[optind]));
errsv = errno;
if (rc < 0 && errsv == ENOSYS)
lprintf(LOG_ERR, "Command not supported on this platform\n");
/* clean up */
do_exit_3:
mosys_platform_destroy(intf);
do_exit_2:
#if defined(CONFIG_USE_IPC_LOCK)
mosys_release_big_lock();
#endif
do_exit_1:
mosys_log_halt();
if (rc < 0 && errsv > 0)
exit(errsv);
exit(rc);
}
int main(int argc, char **argv)
{
mosys_globals_init();
return mosys_main(argc, argv);
}