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