blob: a757c8f4f61a5199dec66520c69d62b038946f94 [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 *
16 * (This code is based on btrfs-progs/btrfs.c.)
17 */
18
19#define _GNU_SOURCE
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23
Chris Ball45541d52012-02-12 11:49:53 -050024#include "mmc_cmds.h"
25
Goffredo Baroncelli80d26602012-02-12 11:43:14 -050026#define MMC_VERSION "0.1"
27
28#define BASIC_HELP 0
29#define ADVANCED_HELP 1
30
31typedef int (*CommandFunction)(int argc, char **argv);
32
33struct Command {
34 CommandFunction func; /* function which implements the command */
35 int nargs; /* if == 999, any number of arguments
36 if >= 0, number of arguments,
37 if < 0, _minimum_ number of arguments */
38 char *verb; /* verb */
39 char *help; /* help lines; from the 2nd line onward they
40 are automatically indented */
41 char *adv_help; /* advanced help message; from the 2nd line
42 onward they are automatically indented */
43
44 /* the following fields are run-time filled by the program */
45 char **cmds; /* array of subcommands */
46 int ncmds; /* number of subcommand */
47};
48
49static struct Command commands[] = {
50 /*
51 * avoid short commands different for the case only
52 */
Chris Ball45541d52012-02-12 11:49:53 -050053 { do_read_extcsd, -1,
54 "extcsd read", "<device>\n"
55 "Print extcsd data from <device>.",
56 NULL
57 },
Chris Ballb9c7a172012-02-20 12:34:25 -050058 { do_writeprotect_get, -1,
59 "writeprotect get", "<device>\n"
60 "Determine the eMMC writeprotect status of <device>.",
61 NULL
62 },
63 { do_writeprotect_set, -1,
64 "writeprotect set", "<device>\n"
Chris Ball65997802012-09-21 18:19:25 +080065 "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 -050066 NULL
67 },
Saugata Dasb7e25992012-05-17 09:26:34 -040068 { do_disable_512B_emulation, -1,
69 "disable 512B emulation", "<device>\n"
Chris Ball65997802012-09-21 18:19:25 +080070 "Set the eMMC data sector size to 4KB by disabling emulation on\n<device>.",
Saugata Dasb7e25992012-05-17 09:26:34 -040071 NULL
72 },
Giuseppe CAVALLARO7bd13202012-04-19 10:58:37 +020073 { do_write_boot_en, -3,
74 "bootpart enable", "<boot_partition> " "<send_ack> " "<device>\n"
75 "Enable the boot partition for the <device>.\nTo receive acknowledgment of boot from the card set <send_ack>\nto 1, else set it to 0.",
76 NULL
77 },
Jaehoon Chung86496512012-09-21 10:08:05 +000078 { do_write_bkops_en, -1,
79 "bkops enable", "<device>\n"
80 "Enable the eMMC BKOPS feature on <device>.\nNOTE! This is a one-time programmable (unreversible) change.",
81 NULL
82 },
Chris Ballf74dfe22012-10-19 16:49:55 -040083 { do_hwreset_en, -1,
84 "hwreset enable", "<device>\n"
85 "Permanently enable the eMMC H/W Reset feature on <device>.\nNOTE! This is a one-time programmable (unreversible) change.",
86 NULL
87 },
88 { do_hwreset_dis, -1,
89 "hwreset disable", "<device>\n"
90 "Permanently disable the eMMC H/W Reset feature on <device>.\nNOTE! This is a one-time programmable (unreversible) change.",
91 NULL
92 },
Yaniv Gardi21bb4732013-05-26 13:25:33 -040093 { do_sanitize, -1,
94 "sanitize", "<device>\n"
95 "Send Sanitize command to the <device>.\nThis will delete the unmapped memory region of the device.",
96 NULL
97 },
Goffredo Baroncelli80d26602012-02-12 11:43:14 -050098 { 0, 0, 0, 0 }
99};
100
101static char *get_prgname(char *programname)
102{
103 char *np;
104 np = strrchr(programname,'/');
105 if(!np)
106 np = programname;
107 else
108 np++;
109
110 return np;
111}
112
113static void print_help(char *programname, struct Command *cmd, int helptype)
114{
115 char *pc;
116
117 printf("\t%s %s ", programname, cmd->verb );
118
119 if (helptype == ADVANCED_HELP && cmd->adv_help)
120 for(pc = cmd->adv_help; *pc; pc++){
121 putchar(*pc);
122 if(*pc == '\n')
123 printf("\t\t");
124 }
125 else
126 for(pc = cmd->help; *pc; pc++){
127 putchar(*pc);
128 if(*pc == '\n')
129 printf("\t\t");
130 }
131
132 putchar('\n');
133}
134
135static void help(char *np)
136{
137 struct Command *cp;
138
139 printf("Usage:\n");
140 for( cp = commands; cp->verb; cp++ )
141 print_help(np, cp, BASIC_HELP);
142
143 printf("\n\t%s help|--help|-h\n\t\tShow the help.\n",np);
144 printf("\n\t%s <cmd> --help\n\t\tShow detailed help for a command or subset of commands.\n",np);
145 printf("\n%s\n", MMC_VERSION);
146}
147
148static int split_command(char *cmd, char ***commands)
149{
150 int c, l;
151 char *p, *s;
152
153 for( *commands = 0, l = c = 0, p = s = cmd ; ; p++, l++ ){
154 if ( *p && *p != ' ' )
155 continue;
156
157 /* c + 2 so that we have room for the null */
158 (*commands) = realloc( (*commands), sizeof(char *)*(c + 2));
159 (*commands)[c] = strndup(s, l);
160 c++;
161 l = 0;
162 s = p+1;
163 if( !*p ) break;
164 }
165
166 (*commands)[c] = 0;
167 return c;
168}
169
170/*
171 This function checks if the passed command is ambiguous
172*/
173static int check_ambiguity(struct Command *cmd, char **argv){
174 int i;
175 struct Command *cp;
176 /* check for ambiguity */
177 for( i = 0 ; i < cmd->ncmds ; i++ ){
178 int match;
179 for( match = 0, cp = commands; cp->verb; cp++ ){
180 int j, skip;
181 char *s1, *s2;
182
183 if( cp->ncmds < i )
184 continue;
185
186 for( skip = 0, j = 0 ; j < i ; j++ )
187 if( strcmp(cmd->cmds[j], cp->cmds[j])){
188 skip=1;
189 break;
190 }
191 if(skip)
192 continue;
193
194 if( !strcmp(cmd->cmds[i], cp->cmds[i]))
195 continue;
196 for(s2 = cp->cmds[i], s1 = argv[i+1];
197 *s1 == *s2 && *s1; s1++, s2++ ) ;
198 if( !*s1 )
199 match++;
200 }
201 if(match){
202 int j;
203 fprintf(stderr, "ERROR: in command '");
204 for( j = 0 ; j <= i ; j++ )
205 fprintf(stderr, "%s%s",j?" ":"", argv[j+1]);
206 fprintf(stderr, "', '%s' is ambiguous\n",argv[j]);
207 return -2;
208 }
209 }
210 return 0;
211}
212
213/*
214 * This function, compacts the program name and the command in the first
215 * element of the '*av' array
216 */
217static int prepare_args(int *ac, char ***av, char *prgname, struct Command *cmd ){
218
219 char **ret;
220 int i;
221 char *newname;
222
223 ret = (char **)malloc(sizeof(char*)*(*ac+1));
224 newname = (char*)malloc(strlen(prgname)+strlen(cmd->verb)+2);
225 if( !ret || !newname ){
226 free(ret);
227 free(newname);
228 return -1;
229 }
230
231 ret[0] = newname;
232 for(i=0; i < *ac ; i++ )
233 ret[i+1] = (*av)[i];
234
235 strcpy(newname, prgname);
236 strcat(newname, " ");
237 strcat(newname, cmd->verb);
238
239 (*ac)++;
240 *av = ret;
241
242 return 0;
243
244}
245
246/*
247 This function performs the following jobs:
248 - show the help if '--help' or 'help' or '-h' are passed
249 - verify that a command is not ambiguous, otherwise show which
250 part of the command is ambiguous
251 - if after a (even partial) command there is '--help' show detailed help
252 for all the matching commands
253 - if the command doesn't match show an error
254 - finally, if a command matches, they return which command matched and
255 the arguments
256
257 The function return 0 in case of help is requested; <0 in case
258 of uncorrect command; >0 in case of matching commands
259 argc, argv are the arg-counter and arg-vector (input)
260 *nargs_ is the number of the arguments after the command (output)
261 **cmd_ is the invoked command (output)
262 ***args_ are the arguments after the command
263
264*/
265static int parse_args(int argc, char **argv,
266 CommandFunction *func_,
267 int *nargs_, char **cmd_, char ***args_ )
268{
269 struct Command *cp;
270 struct Command *matchcmd=0;
271 char *prgname = get_prgname(argv[0]);
272 int i=0, helprequested=0;
273
274 if( argc < 2 || !strcmp(argv[1], "help") ||
275 !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")){
276 help(prgname);
277 return 0;
278 }
279
280 for( cp = commands; cp->verb; cp++ )
281 if( !cp->ncmds)
282 cp->ncmds = split_command(cp->verb, &(cp->cmds));
283
284 for( cp = commands; cp->verb; cp++ ){
285 int match;
286
287 if( argc-1 < cp->ncmds )
288 continue;
289 for( match = 1, i = 0 ; i < cp->ncmds ; i++ ){
290 char *s1, *s2;
291 s1 = cp->cmds[i];
292 s2 = argv[i+1];
293
294 for(s2 = cp->cmds[i], s1 = argv[i+1];
295 *s1 == *s2 && *s1;
296 s1++, s2++ ) ;
297 if( *s1 ){
298 match=0;
299 break;
300 }
301 }
302
303 /* If you understand why this code works ...
304 you are a genious !! */
305 if(argc>i+1 && !strcmp(argv[i+1],"--help")){
306 if(!helprequested)
307 printf("Usage:\n");
308 print_help(prgname, cp, ADVANCED_HELP);
309 helprequested=1;
310 continue;
311 }
312
313 if(!match)
314 continue;
315
316 matchcmd = cp;
317 *nargs_ = argc-matchcmd->ncmds-1;
318 *cmd_ = matchcmd->verb;
319 *args_ = argv+matchcmd->ncmds+1;
320 *func_ = cp->func;
321
322 break;
323 }
324
325 if(helprequested){
326 printf("\n%s\n", MMC_VERSION);
327 return 0;
328 }
329
330 if(!matchcmd){
331 fprintf( stderr, "ERROR: unknown command '%s'\n",argv[1]);
332 help(prgname);
333 return -1;
334 }
335
336 if(check_ambiguity(matchcmd, argv))
337 return -2;
338
339 /* check the number of argument */
340 if (matchcmd->nargs < 0 && matchcmd->nargs < -*nargs_ ){
341 fprintf(stderr, "ERROR: '%s' requires minimum %d arg(s)\n",
342 matchcmd->verb, -matchcmd->nargs);
343 return -2;
344 }
345 if(matchcmd->nargs >= 0 && matchcmd->nargs != *nargs_ && matchcmd->nargs != 999){
346 fprintf(stderr, "ERROR: '%s' requires %d arg(s)\n",
347 matchcmd->verb, matchcmd->nargs);
348 return -2;
349 }
350
351 if (prepare_args( nargs_, args_, prgname, matchcmd )){
352 fprintf(stderr, "ERROR: not enough memory\\n");
353 return -20;
354 }
355
356
357 return 1;
358}
359int main(int ac, char **av )
360{
361 char *cmd=0, **args=0;
362 int nargs=0, r;
363 CommandFunction func=0;
364
365 r = parse_args(ac, av, &func, &nargs, &cmd, &args);
366 if( r <= 0 ){
367 /* error or no command to parse*/
368 exit(-r);
369 }
370
371 exit(func(nargs, args));
372}
373