blob: a30426d12facc54707cbcc9f59415880972e997d [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 },
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500118 { 0, 0, 0, 0 }
119};
120
121static char *get_prgname(char *programname)
122{
123 char *np;
124 np = strrchr(programname,'/');
125 if(!np)
126 np = programname;
127 else
128 np++;
129
130 return np;
131}
132
133static void print_help(char *programname, struct Command *cmd, int helptype)
134{
135 char *pc;
136
137 printf("\t%s %s ", programname, cmd->verb );
138
139 if (helptype == ADVANCED_HELP && cmd->adv_help)
140 for(pc = cmd->adv_help; *pc; pc++){
141 putchar(*pc);
142 if(*pc == '\n')
143 printf("\t\t");
144 }
145 else
146 for(pc = cmd->help; *pc; pc++){
147 putchar(*pc);
148 if(*pc == '\n')
149 printf("\t\t");
150 }
151
152 putchar('\n');
153}
154
155static void help(char *np)
156{
157 struct Command *cp;
158
159 printf("Usage:\n");
160 for( cp = commands; cp->verb; cp++ )
161 print_help(np, cp, BASIC_HELP);
162
163 printf("\n\t%s help|--help|-h\n\t\tShow the help.\n",np);
164 printf("\n\t%s <cmd> --help\n\t\tShow detailed help for a command or subset of commands.\n",np);
165 printf("\n%s\n", MMC_VERSION);
166}
167
168static int split_command(char *cmd, char ***commands)
169{
170 int c, l;
171 char *p, *s;
172
173 for( *commands = 0, l = c = 0, p = s = cmd ; ; p++, l++ ){
174 if ( *p && *p != ' ' )
175 continue;
176
177 /* c + 2 so that we have room for the null */
178 (*commands) = realloc( (*commands), sizeof(char *)*(c + 2));
179 (*commands)[c] = strndup(s, l);
180 c++;
181 l = 0;
182 s = p+1;
183 if( !*p ) break;
184 }
185
186 (*commands)[c] = 0;
187 return c;
188}
189
190/*
191 This function checks if the passed command is ambiguous
192*/
193static int check_ambiguity(struct Command *cmd, char **argv){
194 int i;
195 struct Command *cp;
196 /* check for ambiguity */
197 for( i = 0 ; i < cmd->ncmds ; i++ ){
198 int match;
199 for( match = 0, cp = commands; cp->verb; cp++ ){
200 int j, skip;
201 char *s1, *s2;
202
203 if( cp->ncmds < i )
204 continue;
205
206 for( skip = 0, j = 0 ; j < i ; j++ )
207 if( strcmp(cmd->cmds[j], cp->cmds[j])){
208 skip=1;
209 break;
210 }
211 if(skip)
212 continue;
213
214 if( !strcmp(cmd->cmds[i], cp->cmds[i]))
215 continue;
216 for(s2 = cp->cmds[i], s1 = argv[i+1];
217 *s1 == *s2 && *s1; s1++, s2++ ) ;
218 if( !*s1 )
219 match++;
220 }
221 if(match){
222 int j;
223 fprintf(stderr, "ERROR: in command '");
224 for( j = 0 ; j <= i ; j++ )
225 fprintf(stderr, "%s%s",j?" ":"", argv[j+1]);
226 fprintf(stderr, "', '%s' is ambiguous\n",argv[j]);
227 return -2;
228 }
229 }
230 return 0;
231}
232
233/*
234 * This function, compacts the program name and the command in the first
235 * element of the '*av' array
236 */
237static int prepare_args(int *ac, char ***av, char *prgname, struct Command *cmd ){
238
239 char **ret;
240 int i;
241 char *newname;
242
243 ret = (char **)malloc(sizeof(char*)*(*ac+1));
244 newname = (char*)malloc(strlen(prgname)+strlen(cmd->verb)+2);
245 if( !ret || !newname ){
246 free(ret);
247 free(newname);
248 return -1;
249 }
250
251 ret[0] = newname;
252 for(i=0; i < *ac ; i++ )
253 ret[i+1] = (*av)[i];
254
255 strcpy(newname, prgname);
256 strcat(newname, " ");
257 strcat(newname, cmd->verb);
258
259 (*ac)++;
260 *av = ret;
261
262 return 0;
263
264}
265
266/*
267 This function performs the following jobs:
268 - show the help if '--help' or 'help' or '-h' are passed
269 - verify that a command is not ambiguous, otherwise show which
270 part of the command is ambiguous
271 - if after a (even partial) command there is '--help' show detailed help
272 for all the matching commands
273 - if the command doesn't match show an error
274 - finally, if a command matches, they return which command matched and
275 the arguments
276
277 The function return 0 in case of help is requested; <0 in case
278 of uncorrect command; >0 in case of matching commands
279 argc, argv are the arg-counter and arg-vector (input)
280 *nargs_ is the number of the arguments after the command (output)
281 **cmd_ is the invoked command (output)
282 ***args_ are the arguments after the command
283
284*/
285static int parse_args(int argc, char **argv,
286 CommandFunction *func_,
287 int *nargs_, char **cmd_, char ***args_ )
288{
289 struct Command *cp;
290 struct Command *matchcmd=0;
291 char *prgname = get_prgname(argv[0]);
292 int i=0, helprequested=0;
293
294 if( argc < 2 || !strcmp(argv[1], "help") ||
295 !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")){
296 help(prgname);
297 return 0;
298 }
299
300 for( cp = commands; cp->verb; cp++ )
301 if( !cp->ncmds)
302 cp->ncmds = split_command(cp->verb, &(cp->cmds));
303
304 for( cp = commands; cp->verb; cp++ ){
305 int match;
306
307 if( argc-1 < cp->ncmds )
308 continue;
309 for( match = 1, i = 0 ; i < cp->ncmds ; i++ ){
310 char *s1, *s2;
311 s1 = cp->cmds[i];
312 s2 = argv[i+1];
313
314 for(s2 = cp->cmds[i], s1 = argv[i+1];
315 *s1 == *s2 && *s1;
316 s1++, s2++ ) ;
317 if( *s1 ){
318 match=0;
319 break;
320 }
321 }
322
323 /* If you understand why this code works ...
324 you are a genious !! */
325 if(argc>i+1 && !strcmp(argv[i+1],"--help")){
326 if(!helprequested)
327 printf("Usage:\n");
328 print_help(prgname, cp, ADVANCED_HELP);
329 helprequested=1;
330 continue;
331 }
332
333 if(!match)
334 continue;
335
336 matchcmd = cp;
337 *nargs_ = argc-matchcmd->ncmds-1;
338 *cmd_ = matchcmd->verb;
339 *args_ = argv+matchcmd->ncmds+1;
340 *func_ = cp->func;
341
342 break;
343 }
344
345 if(helprequested){
346 printf("\n%s\n", MMC_VERSION);
347 return 0;
348 }
349
350 if(!matchcmd){
351 fprintf( stderr, "ERROR: unknown command '%s'\n",argv[1]);
352 help(prgname);
353 return -1;
354 }
355
356 if(check_ambiguity(matchcmd, argv))
357 return -2;
358
359 /* check the number of argument */
360 if (matchcmd->nargs < 0 && matchcmd->nargs < -*nargs_ ){
361 fprintf(stderr, "ERROR: '%s' requires minimum %d arg(s)\n",
362 matchcmd->verb, -matchcmd->nargs);
363 return -2;
364 }
365 if(matchcmd->nargs >= 0 && matchcmd->nargs != *nargs_ && matchcmd->nargs != 999){
366 fprintf(stderr, "ERROR: '%s' requires %d arg(s)\n",
367 matchcmd->verb, matchcmd->nargs);
368 return -2;
369 }
370
371 if (prepare_args( nargs_, args_, prgname, matchcmd )){
372 fprintf(stderr, "ERROR: not enough memory\\n");
373 return -20;
374 }
375
376
377 return 1;
378}
379int main(int ac, char **av )
380{
381 char *cmd=0, **args=0;
382 int nargs=0, r;
383 CommandFunction func=0;
384
385 r = parse_args(ac, av, &func, &nargs, &cmd, &args);
386 if( r <= 0 ){
387 /* error or no command to parse*/
388 exit(-r);
389 }
390
391 exit(func(nargs, args));
392}
393