blob: f9146298859e8db53e72462d4f88fde6f22b9f4e [file] [log] [blame]
Goffredo Baroncelli80d26602012-02-12 11:43:14 -05001/*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public
4 * License v2 as published by the Free Software Foundation.
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 * General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public
12 * License along with this program; if not, write to the
13 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14 * Boston, MA 021110-1307, USA.
15 *
Avi Shchislowskidc7ab962016-03-08 14:22:41 -050016 * Modified to add field firmware update support,
17 * those modifications are Copyright (c) 2016 SanDisk Corp.
18 *
Goffredo Baroncelli80d26602012-02-12 11:43:14 -050019 * (This code is based on btrfs-progs/btrfs.c.)
20 */
21
22#define _GNU_SOURCE
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26
Chris Ball45541d52012-02-12 11:49:53 -050027#include "mmc_cmds.h"
28
Goffredo Baroncelli80d26602012-02-12 11:43:14 -050029#define MMC_VERSION "0.1"
30
31#define BASIC_HELP 0
32#define ADVANCED_HELP 1
33
34typedef int (*CommandFunction)(int argc, char **argv);
35
36struct Command {
37 CommandFunction func; /* function which implements the command */
38 int nargs; /* if == 999, any number of arguments
39 if >= 0, number of arguments,
40 if < 0, _minimum_ number of arguments */
41 char *verb; /* verb */
Gwendal Grignoueb1cd012015-01-08 15:34:55 -080042 char *help; /* help lines; from the 2nd line onward they
43 are automatically indented */
44 char *adv_help; /* advanced help message; from the 2nd line
45 onward they are automatically indented */
Goffredo Baroncelli80d26602012-02-12 11:43:14 -050046
47 /* the following fields are run-time filled by the program */
48 char **cmds; /* array of subcommands */
49 int ncmds; /* number of subcommand */
50};
51
52static struct Command commands[] = {
53 /*
54 * avoid short commands different for the case only
55 */
Chris Ball45541d52012-02-12 11:49:53 -050056 { do_read_extcsd, -1,
57 "extcsd read", "<device>\n"
58 "Print extcsd data from <device>.",
59 NULL
60 },
Nick Sanders9d57aa72014-03-05 21:38:54 -080061 { do_dump_extcsd, -1,
62 "extcsd dump", "<device>\n"
63 "Print raw extcsd data from <device>.",
64 NULL
65 },
Chris Ballb9c7a172012-02-20 12:34:25 -050066 { do_writeprotect_get, -1,
67 "writeprotect get", "<device>\n"
68 "Determine the eMMC writeprotect status of <device>.",
69 NULL
70 },
71 { do_writeprotect_set, -1,
72 "writeprotect set", "<device>\n"
Chris Ball65997802012-09-21 18:19:25 +080073 "Set the eMMC writeprotect status of <device>.\nThis sets the eMMC to be write-protected until next boot.",
Chris Ball45541d52012-02-12 11:49:53 -050074 NULL
75 },
Saugata Dasb7e25992012-05-17 09:26:34 -040076 { do_disable_512B_emulation, -1,
77 "disable 512B emulation", "<device>\n"
Chris Ball65997802012-09-21 18:19:25 +080078 "Set the eMMC data sector size to 4KB by disabling emulation on\n<device>.",
Saugata Dasb7e25992012-05-17 09:26:34 -040079 NULL
80 },
Balaji T Kd78ce082015-04-29 18:12:33 -040081 { do_create_gp_partition, -6,
82 "gp create", "<-y|-n> " "<length KiB> " "<partition> " "<enh_attr> " "<ext_attr> " "<device>\n"
83 "create general purpose partition for the <device>.\nDry-run only unless -y is passed.\nNOTE! This is a one-time programmable (unreversible) change.\nTo set enhanced attribute to general partition being created set\n <enh_attr> to 1 else set it to 0.\nTo set extended attribute to general partition\n set <ext_attr> to 1,2 else set it to 0",
84 NULL
85 },
Ben Gardinerd91d3692013-05-30 17:12:51 -040086 { do_enh_area_set, -4,
87 "enh_area set", "<-y|-n> " "<start KiB> " "<length KiB> " "<device>\n"
88 "Enable the enhanced user area for the <device>.\nDry-run only unless -y is passed.\nNOTE! This is a one-time programmable (unreversible) change.",
89 NULL
90 },
Ben Gardiner196d0d22013-09-19 11:14:29 -040091 { do_write_reliability_set, -2,
92 "write_reliability set", "<-y|-n> " "<partition> " "<device>\n"
93 "Enable write reliability per partition for the <device>.\nDry-run only unless -y is passed.\nNOTE! This is a one-time programmable (unreversible) change.",
94 NULL
95 },
Ben Gardiner27c357d2013-05-30 17:12:47 -040096 { do_status_get, -1,
97 "status get", "<device>\n"
98 "Print the response to STATUS_SEND (CMD13).",
99 NULL
100 },
Giuseppe CAVALLARO7bd13202012-04-19 10:58:37 +0200101 { do_write_boot_en, -3,
102 "bootpart enable", "<boot_partition> " "<send_ack> " "<device>\n"
Markus Schuetterlefbc0e6c2016-03-19 08:42:41 +0100103 "Enable the boot partition for the <device>.\nDisable the boot partition for the <device> if <boot_partition> is set to 0.\nTo receive acknowledgment of boot from the card set <send_ack>\nto 1, else set it to 0.",
Giuseppe CAVALLARO7bd13202012-04-19 10:58:37 +0200104 NULL
105 },
Al Cooper794314c2015-05-01 08:24:37 -0400106 { do_boot_bus_conditions_set, -4,
107 "bootbus set", "<boot_mode> " "<reset_boot_bus_conditions> " "<boot_bus_width> " "<device>\n"
108 "Set Boot Bus Conditions.\n"
109 "<boot_mode> must be \"single_backward|single_hs|dual\"\n"
110 "<reset_boot_bus_conditions> must be \"x1|retain\"\n"
111 "<boot_bus_width> must be \"x1|x4|x8\"",
112 NULL
113 },
Jaehoon Chung86496512012-09-21 10:08:05 +0000114 { do_write_bkops_en, -1,
115 "bkops enable", "<device>\n"
116 "Enable the eMMC BKOPS feature on <device>.\nNOTE! This is a one-time programmable (unreversible) change.",
117 NULL
118 },
Chris Ballf74dfe22012-10-19 16:49:55 -0400119 { do_hwreset_en, -1,
120 "hwreset enable", "<device>\n"
121 "Permanently enable the eMMC H/W Reset feature on <device>.\nNOTE! This is a one-time programmable (unreversible) change.",
122 NULL
123 },
124 { do_hwreset_dis, -1,
125 "hwreset disable", "<device>\n"
126 "Permanently disable the eMMC H/W Reset feature on <device>.\nNOTE! This is a one-time programmable (unreversible) change.",
127 NULL
128 },
Yaniv Gardi21bb4732013-05-26 13:25:33 -0400129 { do_sanitize, -1,
130 "sanitize", "<device>\n"
131 "Send Sanitize command to the <device>.\nThis will delete the unmapped memory region of the device.",
132 NULL
133 },
Roman Peniaev023cc7c2014-08-12 23:25:45 +0900134 { do_rpmb_write_key, -1,
135 "rpmb write-key", "<rpmb device> <key file>\n"
Chris Balld186ab52014-08-12 10:44:52 -0400136 "Program authentication key which is 32 bytes length and stored\n"
137 "in the specified file. Also you can specify '-' instead of\n"
138 "key file path to read the key from stdin.\n"
139 "NOTE! This is a one-time programmable (unreversible) change.\n"
Roman Peniaev023cc7c2014-08-12 23:25:45 +0900140 "Example:\n"
Chris Balld186ab52014-08-12 10:44:52 -0400141 " $ echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH | \\\n"
142 " mmc rpmb write-key /dev/mmcblk0rpmb -",
Roman Peniaev023cc7c2014-08-12 23:25:45 +0900143 NULL
144 },
145 { do_rpmb_read_counter, -1,
146 "rpmb read-counter", "<rpmb device>\n"
147 "Counter value for the <rpmb device> will be read to stdout.",
148 NULL
149 },
150 { do_rpmb_read_block, -1,
151 "rpmb read-block", "<rpmb device> <address> <blocks count> <output file> [key file]\n"
Chris Balld186ab52014-08-12 10:44:52 -0400152 "Blocks of 256 bytes will be read from <rpmb device> to output\n"
153 "file or stdout if '-' is specified. If key is specified - read\n"
154 "data will be verified. Instead of regular path you can specify\n"
155 "'-' to read key from stdin.\n"
Roman Peniaev023cc7c2014-08-12 23:25:45 +0900156 "Example:\n"
Chris Balld186ab52014-08-12 10:44:52 -0400157 " $ echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH | \\\n"
158 " mmc rpmb read-block /dev/mmcblk0rpmb 0x02 2 /tmp/block -\n"
Roman Peniaev023cc7c2014-08-12 23:25:45 +0900159 "or read two blocks without verification\n"
160 " $ mmc rpmb read-block /dev/mmcblk0rpmb 0x02 2 /tmp/block",
161 NULL
162 },
163 { do_rpmb_write_block, -1,
164 "rpmb write-block", "<rpmb device> <address> <256 byte data file> <key file>\n"
Chris Balld186ab52014-08-12 10:44:52 -0400165 "Block of 256 bytes will be written from data file to\n"
166 "<rpmb device>. Also you can specify '-' instead of key\n"
167 "file path or data file to read the data from stdin.\n"
Roman Peniaev023cc7c2014-08-12 23:25:45 +0900168 "Example:\n"
Chris Balld186ab52014-08-12 10:44:52 -0400169 " $ (awk 'BEGIN {while (c++<256) printf \"a\"}' | \\\n"
170 " echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH) | \\\n"
Roman Peniaev023cc7c2014-08-12 23:25:45 +0900171 " mmc rpmb write-block /dev/mmcblk0rpmb 0x02 - -",
172 NULL
173 },
Al Cooper786418c2015-04-29 18:12:35 -0400174 { do_cache_en, -1,
175 "cache enable", "<device>\n"
176 "Enable the eMMC cache feature on <device>.\n"
177 "NOTE! The cache is an optional feature on devices >= eMMC4.5.",
178 NULL
179 },
180 { do_cache_dis, -1,
181 "cache disable", "<device>\n"
182 "Disable the eMMC cache feature on <device>.\n"
183 "NOTE! The cache is an optional feature on devices >= eMMC4.5.",
184 NULL
185 },
Sebastian Rasmussen7e00a5a2016-02-23 13:37:28 +0800186 { do_read_csd, -1,
187 "csd read", "<device path>\n"
188 "Print CSD data from <device path>.\n"
189 "The device path should specify the csd file directory.",
190 NULL
191 },
192 { do_read_cid, -1,
193 "cid read", "<device path>\n"
194 "Print CID data from <device path>.\n"
195 "The device path should specify the cid file directory.",
196 NULL
197 },
198 { do_read_scr, -1,
199 "scr read", "<device path>\n"
200 "Print SCR data from <device path>.\n"
201 "The device path should specify the scr file directory.",
202 NULL
203 },
Julius Wernerbcc3e2e2016-04-21 16:53:02 -0700204 { do_blockprotect_enable, -2, "blockprotect enable",
205 "[-p|-r] <device> <sector>\n"
206 "Enable block protection for a given sector. Will write protect all\n"
207 "sectors within the target sector's write protect block. Write protect\n"
208 "block size is a multiple of erase block size and device dependent.\n"
209 "Run mmc blockprotect info to query write protect block size.\n"
210 " -p Protect block permanently. WARNING: THIS IS IRREVERSIBLE!\n"
211 " -r Protect block until next power-on.",
212 NULL
213 },
214 { do_blockprotect_disable, -2, "blockprotect disable",
215 "<device> <sector>\n"
216 "Disable block protection for a given sector. Will disable protection\n"
217 "for all sectors within the target sector's write protect block.\n"
218 "Cannot disable permanent or power-on write protection.",
219 NULL
220 },
221 { do_blockprotect_read, -2, "blockprotect read", "<device> <sector>\n"
222 "Query the current block protection status of a given sector.",
223 NULL
224 },
225 { do_blockprotect_info, -1, "blockprotect info", "<device>\n"
226 "Query information about device's block protect capabilities.",
227 NULL
228 },
Avi Shchislowskidc7ab962016-03-08 14:22:41 -0500229 { do_emmc50_ffu, -2,
230 "old_ffu", "[-k hack_type[:hack_value]] <image name> <device>\n"
231 "run eMMC 5.0 Field firmware update.\n"
232 "Device specific hacks can be specificied.",
233 NULL
234 },
235 { do_ffu, -2,
236 "ffu", "<image name> <device>\n"
237 "Run Field Firmware Update with <image name> on <device>.\n",
238 NULL
239 },
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500240 { 0, 0, 0, 0 }
241};
242
243static char *get_prgname(char *programname)
244{
245 char *np;
246 np = strrchr(programname,'/');
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800247 if (!np)
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500248 np = programname;
249 else
250 np++;
251
252 return np;
253}
254
255static void print_help(char *programname, struct Command *cmd, int helptype)
256{
257 char *pc;
258
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800259 printf("\t%s %s ", programname, cmd->verb);
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500260
261 if (helptype == ADVANCED_HELP && cmd->adv_help)
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800262 for (pc = cmd->adv_help; *pc; pc++) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500263 putchar(*pc);
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800264 if (*pc == '\n')
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500265 printf("\t\t");
266 }
267 else
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800268 for (pc = cmd->help; *pc; pc++) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500269 putchar(*pc);
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800270 if (*pc == '\n')
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500271 printf("\t\t");
272 }
273
274 putchar('\n');
275}
276
277static void help(char *np)
278{
279 struct Command *cp;
280
281 printf("Usage:\n");
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800282 for (cp = commands; cp->verb; cp++)
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500283 print_help(np, cp, BASIC_HELP);
284
285 printf("\n\t%s help|--help|-h\n\t\tShow the help.\n",np);
286 printf("\n\t%s <cmd> --help\n\t\tShow detailed help for a command or subset of commands.\n",np);
287 printf("\n%s\n", MMC_VERSION);
288}
289
290static int split_command(char *cmd, char ***commands)
291{
292 int c, l;
293 char *p, *s;
294
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800295 for (*commands = 0, l = c = 0, p = s = cmd ; ; p++, l++) {
296 if (*p && *p != ' ')
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500297 continue;
298
299 /* c + 2 so that we have room for the null */
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800300 (*commands) = realloc((*commands), sizeof(char *)*(c + 2));
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500301 (*commands)[c] = strndup(s, l);
302 c++;
303 l = 0;
304 s = p+1;
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800305 if (!*p) break;
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500306 }
307
308 (*commands)[c] = 0;
309 return c;
310}
311
312/*
313 This function checks if the passed command is ambiguous
314*/
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800315static int check_ambiguity(struct Command *cmd, char **argv) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500316 int i;
317 struct Command *cp;
318 /* check for ambiguity */
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800319 for (i = 0 ; i < cmd->ncmds ; i++) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500320 int match;
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800321 for (match = 0, cp = commands; cp->verb; cp++) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500322 int j, skip;
323 char *s1, *s2;
324
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800325 if (cp->ncmds < i)
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500326 continue;
327
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800328 for (skip = 0, j = 0 ; j < i ; j++)
329 if (strcmp(cmd->cmds[j], cp->cmds[j])) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500330 skip=1;
331 break;
332 }
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800333 if (skip)
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500334 continue;
335
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800336 if (!strcmp(cmd->cmds[i], cp->cmds[i]))
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500337 continue;
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800338 for (s2 = cp->cmds[i], s1 = argv[i+1];
339 *s1 == *s2 && *s1; s1++, s2++) ;
340 if (!*s1)
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500341 match++;
342 }
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800343 if (match) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500344 int j;
345 fprintf(stderr, "ERROR: in command '");
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800346 for (j = 0 ; j <= i ; j++)
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500347 fprintf(stderr, "%s%s",j?" ":"", argv[j+1]);
348 fprintf(stderr, "', '%s' is ambiguous\n",argv[j]);
349 return -2;
350 }
351 }
352 return 0;
353}
354
355/*
356 * This function, compacts the program name and the command in the first
357 * element of the '*av' array
358 */
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800359static int prepare_args(int *ac, char ***av, char *prgname,
360 struct Command *cmd) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500361
362 char **ret;
363 int i;
364 char *newname;
365
366 ret = (char **)malloc(sizeof(char*)*(*ac+1));
367 newname = (char*)malloc(strlen(prgname)+strlen(cmd->verb)+2);
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800368 if (!ret || !newname) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500369 free(ret);
370 free(newname);
371 return -1;
372 }
373
374 ret[0] = newname;
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800375 for (i=0; i < *ac ; i++)
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500376 ret[i+1] = (*av)[i];
377
378 strcpy(newname, prgname);
379 strcat(newname, " ");
380 strcat(newname, cmd->verb);
381
382 (*ac)++;
383 *av = ret;
384
385 return 0;
386
387}
388
389/*
390 This function performs the following jobs:
391 - show the help if '--help' or 'help' or '-h' are passed
392 - verify that a command is not ambiguous, otherwise show which
393 part of the command is ambiguous
394 - if after a (even partial) command there is '--help' show detailed help
395 for all the matching commands
396 - if the command doesn't match show an error
397 - finally, if a command matches, they return which command matched and
398 the arguments
399
400 The function return 0 in case of help is requested; <0 in case
401 of uncorrect command; >0 in case of matching commands
402 argc, argv are the arg-counter and arg-vector (input)
403 *nargs_ is the number of the arguments after the command (output)
404 **cmd_ is the invoked command (output)
405 ***args_ are the arguments after the command
406
407*/
408static int parse_args(int argc, char **argv,
409 CommandFunction *func_,
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800410 int *nargs_, char **cmd_, char ***args_)
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500411{
412 struct Command *cp;
413 struct Command *matchcmd=0;
414 char *prgname = get_prgname(argv[0]);
415 int i=0, helprequested=0;
416
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800417 if (argc < 2 || !strcmp(argv[1], "help") ||
418 !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500419 help(prgname);
420 return 0;
421 }
422
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800423 for (cp = commands; cp->verb; cp++)
424 if (!cp->ncmds)
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500425 cp->ncmds = split_command(cp->verb, &(cp->cmds));
426
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800427 for (cp = commands; cp->verb; cp++) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500428 int match;
429
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800430 if (argc-1 < cp->ncmds)
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500431 continue;
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800432 for (match = 1, i = 0 ; i < cp->ncmds ; i++) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500433 char *s1, *s2;
434 s1 = cp->cmds[i];
435 s2 = argv[i+1];
436
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800437 for (s2 = cp->cmds[i], s1 = argv[i+1];
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500438 *s1 == *s2 && *s1;
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800439 s1++, s2++) ;
440 if (*s1) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500441 match=0;
442 break;
443 }
444 }
445
446 /* If you understand why this code works ...
447 you are a genious !! */
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800448 if (argc>i+1 && !strcmp(argv[i+1],"--help")) {
449 if (!helprequested)
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500450 printf("Usage:\n");
451 print_help(prgname, cp, ADVANCED_HELP);
452 helprequested=1;
453 continue;
454 }
455
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800456 if (!match)
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500457 continue;
458
459 matchcmd = cp;
460 *nargs_ = argc-matchcmd->ncmds-1;
461 *cmd_ = matchcmd->verb;
462 *args_ = argv+matchcmd->ncmds+1;
463 *func_ = cp->func;
464
465 break;
466 }
467
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800468 if (helprequested) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500469 printf("\n%s\n", MMC_VERSION);
470 return 0;
471 }
472
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800473 if (!matchcmd) {
474 fprintf(stderr, "ERROR: unknown command '%s'\n",argv[1]);
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500475 help(prgname);
476 return -1;
477 }
478
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800479 if (check_ambiguity(matchcmd, argv))
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500480 return -2;
481
482 /* check the number of argument */
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800483 if (matchcmd->nargs < 0 && matchcmd->nargs < -*nargs_) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500484 fprintf(stderr, "ERROR: '%s' requires minimum %d arg(s)\n",
485 matchcmd->verb, -matchcmd->nargs);
486 return -2;
487 }
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800488 if (matchcmd->nargs >= 0 && matchcmd->nargs != *nargs_ && matchcmd->nargs != 999) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500489 fprintf(stderr, "ERROR: '%s' requires %d arg(s)\n",
490 matchcmd->verb, matchcmd->nargs);
491 return -2;
492 }
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800493 if (prepare_args(nargs_, args_, prgname, matchcmd)) {
494 fprintf(stderr, "ERROR: not enough memory\\n");
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500495 return -20;
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800496 }
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500497
498
499 return 1;
500}
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800501int main(int ac, char **av)
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500502{
503 char *cmd=0, **args=0;
504 int nargs=0, r;
505 CommandFunction func=0;
506
507 r = parse_args(ac, av, &func, &nargs, &cmd, &args);
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800508 if (r <= 0) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500509 /* error or no command to parse*/
510 exit(-r);
511 }
512
513 exit(func(nargs, args));
514}
515