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