blob: ffddb77d56e50b521481e005b054eeb90567d587 [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 },
Nick Sanders9d57aa72014-03-05 21:38:54 -080058 { do_dump_extcsd, -1,
59 "extcsd dump", "<device>\n"
60 "Print raw extcsd data from <device>.",
61 NULL
62 },
Chris Ballb9c7a172012-02-20 12:34:25 -050063 { do_writeprotect_get, -1,
64 "writeprotect get", "<device>\n"
65 "Determine the eMMC writeprotect status of <device>.",
66 NULL
67 },
68 { do_writeprotect_set, -1,
69 "writeprotect set", "<device>\n"
Chris Ball65997802012-09-21 18:19:25 +080070 "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 -050071 NULL
72 },
Saugata Dasb7e25992012-05-17 09:26:34 -040073 { do_disable_512B_emulation, -1,
74 "disable 512B emulation", "<device>\n"
Chris Ball65997802012-09-21 18:19:25 +080075 "Set the eMMC data sector size to 4KB by disabling emulation on\n<device>.",
Saugata Dasb7e25992012-05-17 09:26:34 -040076 NULL
77 },
Ben Gardinerd91d3692013-05-30 17:12:51 -040078 { do_enh_area_set, -4,
79 "enh_area set", "<-y|-n> " "<start KiB> " "<length KiB> " "<device>\n"
80 "Enable the enhanced user area for the <device>.\nDry-run only unless -y is passed.\nNOTE! This is a one-time programmable (unreversible) change.",
81 NULL
82 },
Ben Gardiner196d0d22013-09-19 11:14:29 -040083 { do_write_reliability_set, -2,
84 "write_reliability set", "<-y|-n> " "<partition> " "<device>\n"
85 "Enable write reliability per partition for the <device>.\nDry-run only unless -y is passed.\nNOTE! This is a one-time programmable (unreversible) change.",
86 NULL
87 },
Ben Gardiner27c357d2013-05-30 17:12:47 -040088 { do_status_get, -1,
89 "status get", "<device>\n"
90 "Print the response to STATUS_SEND (CMD13).",
91 NULL
92 },
Giuseppe CAVALLARO7bd13202012-04-19 10:58:37 +020093 { do_write_boot_en, -3,
94 "bootpart enable", "<boot_partition> " "<send_ack> " "<device>\n"
95 "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.",
96 NULL
97 },
Jaehoon Chung86496512012-09-21 10:08:05 +000098 { do_write_bkops_en, -1,
99 "bkops enable", "<device>\n"
100 "Enable the eMMC BKOPS feature on <device>.\nNOTE! This is a one-time programmable (unreversible) change.",
101 NULL
102 },
Chris Ballf74dfe22012-10-19 16:49:55 -0400103 { do_hwreset_en, -1,
104 "hwreset enable", "<device>\n"
105 "Permanently enable the eMMC H/W Reset feature on <device>.\nNOTE! This is a one-time programmable (unreversible) change.",
106 NULL
107 },
108 { do_hwreset_dis, -1,
109 "hwreset disable", "<device>\n"
110 "Permanently disable the eMMC H/W Reset feature on <device>.\nNOTE! This is a one-time programmable (unreversible) change.",
111 NULL
112 },
Yaniv Gardi21bb4732013-05-26 13:25:33 -0400113 { do_sanitize, -1,
114 "sanitize", "<device>\n"
115 "Send Sanitize command to the <device>.\nThis will delete the unmapped memory region of the device.",
116 NULL
117 },
Gwendal Grignou771984c2014-07-01 12:46:18 -0700118 { do_emmc50_ffu, -2,
119 "ffu", "<image name> <device>\n"
120 "run eMMC 5.0 Field firmware update.\n.",
121 NULL
122 },
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500123 { 0, 0, 0, 0 }
124};
125
126static char *get_prgname(char *programname)
127{
128 char *np;
129 np = strrchr(programname,'/');
130 if(!np)
131 np = programname;
132 else
133 np++;
134
135 return np;
136}
137
138static void print_help(char *programname, struct Command *cmd, int helptype)
139{
140 char *pc;
141
142 printf("\t%s %s ", programname, cmd->verb );
143
144 if (helptype == ADVANCED_HELP && cmd->adv_help)
145 for(pc = cmd->adv_help; *pc; pc++){
146 putchar(*pc);
147 if(*pc == '\n')
148 printf("\t\t");
149 }
150 else
151 for(pc = cmd->help; *pc; pc++){
152 putchar(*pc);
153 if(*pc == '\n')
154 printf("\t\t");
155 }
156
157 putchar('\n');
158}
159
160static void help(char *np)
161{
162 struct Command *cp;
163
164 printf("Usage:\n");
165 for( cp = commands; cp->verb; cp++ )
166 print_help(np, cp, BASIC_HELP);
167
168 printf("\n\t%s help|--help|-h\n\t\tShow the help.\n",np);
169 printf("\n\t%s <cmd> --help\n\t\tShow detailed help for a command or subset of commands.\n",np);
170 printf("\n%s\n", MMC_VERSION);
171}
172
173static int split_command(char *cmd, char ***commands)
174{
175 int c, l;
176 char *p, *s;
177
178 for( *commands = 0, l = c = 0, p = s = cmd ; ; p++, l++ ){
179 if ( *p && *p != ' ' )
180 continue;
181
182 /* c + 2 so that we have room for the null */
183 (*commands) = realloc( (*commands), sizeof(char *)*(c + 2));
184 (*commands)[c] = strndup(s, l);
185 c++;
186 l = 0;
187 s = p+1;
188 if( !*p ) break;
189 }
190
191 (*commands)[c] = 0;
192 return c;
193}
194
195/*
196 This function checks if the passed command is ambiguous
197*/
198static int check_ambiguity(struct Command *cmd, char **argv){
199 int i;
200 struct Command *cp;
201 /* check for ambiguity */
202 for( i = 0 ; i < cmd->ncmds ; i++ ){
203 int match;
204 for( match = 0, cp = commands; cp->verb; cp++ ){
205 int j, skip;
206 char *s1, *s2;
207
208 if( cp->ncmds < i )
209 continue;
210
211 for( skip = 0, j = 0 ; j < i ; j++ )
212 if( strcmp(cmd->cmds[j], cp->cmds[j])){
213 skip=1;
214 break;
215 }
216 if(skip)
217 continue;
218
219 if( !strcmp(cmd->cmds[i], cp->cmds[i]))
220 continue;
221 for(s2 = cp->cmds[i], s1 = argv[i+1];
222 *s1 == *s2 && *s1; s1++, s2++ ) ;
223 if( !*s1 )
224 match++;
225 }
226 if(match){
227 int j;
228 fprintf(stderr, "ERROR: in command '");
229 for( j = 0 ; j <= i ; j++ )
230 fprintf(stderr, "%s%s",j?" ":"", argv[j+1]);
231 fprintf(stderr, "', '%s' is ambiguous\n",argv[j]);
232 return -2;
233 }
234 }
235 return 0;
236}
237
238/*
239 * This function, compacts the program name and the command in the first
240 * element of the '*av' array
241 */
242static int prepare_args(int *ac, char ***av, char *prgname, struct Command *cmd ){
243
244 char **ret;
245 int i;
246 char *newname;
247
248 ret = (char **)malloc(sizeof(char*)*(*ac+1));
249 newname = (char*)malloc(strlen(prgname)+strlen(cmd->verb)+2);
250 if( !ret || !newname ){
251 free(ret);
252 free(newname);
253 return -1;
254 }
255
256 ret[0] = newname;
257 for(i=0; i < *ac ; i++ )
258 ret[i+1] = (*av)[i];
259
260 strcpy(newname, prgname);
261 strcat(newname, " ");
262 strcat(newname, cmd->verb);
263
264 (*ac)++;
265 *av = ret;
266
267 return 0;
268
269}
270
271/*
272 This function performs the following jobs:
273 - show the help if '--help' or 'help' or '-h' are passed
274 - verify that a command is not ambiguous, otherwise show which
275 part of the command is ambiguous
276 - if after a (even partial) command there is '--help' show detailed help
277 for all the matching commands
278 - if the command doesn't match show an error
279 - finally, if a command matches, they return which command matched and
280 the arguments
281
282 The function return 0 in case of help is requested; <0 in case
283 of uncorrect command; >0 in case of matching commands
284 argc, argv are the arg-counter and arg-vector (input)
285 *nargs_ is the number of the arguments after the command (output)
286 **cmd_ is the invoked command (output)
287 ***args_ are the arguments after the command
288
289*/
290static int parse_args(int argc, char **argv,
291 CommandFunction *func_,
292 int *nargs_, char **cmd_, char ***args_ )
293{
294 struct Command *cp;
295 struct Command *matchcmd=0;
296 char *prgname = get_prgname(argv[0]);
297 int i=0, helprequested=0;
298
299 if( argc < 2 || !strcmp(argv[1], "help") ||
300 !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")){
301 help(prgname);
302 return 0;
303 }
304
305 for( cp = commands; cp->verb; cp++ )
306 if( !cp->ncmds)
307 cp->ncmds = split_command(cp->verb, &(cp->cmds));
308
309 for( cp = commands; cp->verb; cp++ ){
310 int match;
311
312 if( argc-1 < cp->ncmds )
313 continue;
314 for( match = 1, i = 0 ; i < cp->ncmds ; i++ ){
315 char *s1, *s2;
316 s1 = cp->cmds[i];
317 s2 = argv[i+1];
318
319 for(s2 = cp->cmds[i], s1 = argv[i+1];
320 *s1 == *s2 && *s1;
321 s1++, s2++ ) ;
322 if( *s1 ){
323 match=0;
324 break;
325 }
326 }
327
328 /* If you understand why this code works ...
329 you are a genious !! */
330 if(argc>i+1 && !strcmp(argv[i+1],"--help")){
331 if(!helprequested)
332 printf("Usage:\n");
333 print_help(prgname, cp, ADVANCED_HELP);
334 helprequested=1;
335 continue;
336 }
337
338 if(!match)
339 continue;
340
341 matchcmd = cp;
342 *nargs_ = argc-matchcmd->ncmds-1;
343 *cmd_ = matchcmd->verb;
344 *args_ = argv+matchcmd->ncmds+1;
345 *func_ = cp->func;
346
347 break;
348 }
349
350 if(helprequested){
351 printf("\n%s\n", MMC_VERSION);
352 return 0;
353 }
354
355 if(!matchcmd){
356 fprintf( stderr, "ERROR: unknown command '%s'\n",argv[1]);
357 help(prgname);
358 return -1;
359 }
360
361 if(check_ambiguity(matchcmd, argv))
362 return -2;
363
364 /* check the number of argument */
365 if (matchcmd->nargs < 0 && matchcmd->nargs < -*nargs_ ){
366 fprintf(stderr, "ERROR: '%s' requires minimum %d arg(s)\n",
367 matchcmd->verb, -matchcmd->nargs);
368 return -2;
369 }
370 if(matchcmd->nargs >= 0 && matchcmd->nargs != *nargs_ && matchcmd->nargs != 999){
371 fprintf(stderr, "ERROR: '%s' requires %d arg(s)\n",
372 matchcmd->verb, matchcmd->nargs);
373 return -2;
374 }
375
376 if (prepare_args( nargs_, args_, prgname, matchcmd )){
377 fprintf(stderr, "ERROR: not enough memory\\n");
378 return -20;
379 }
380
381
382 return 1;
383}
384int main(int ac, char **av )
385{
386 char *cmd=0, **args=0;
387 int nargs=0, r;
388 CommandFunction func=0;
389
390 r = parse_args(ac, av, &func, &nargs, &cmd, &args);
391 if( r <= 0 ){
392 /* error or no command to parse*/
393 exit(-r);
394 }
395
396 exit(func(nargs, args));
397}
398