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