blob: a13d9aeb3f56b50e2b41e23a4fc320e2ea8acdd7 [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 },
Balaji T K78912362015-04-29 18:12:33 -040073 { do_create_gp_partition, -6,
74 "gp create", "<-y|-n> " "<length KiB> " "<partition> " "<enh_attr> " "<ext_attr> " "<device>\n"
75 "create general purpose partition for the <device>.\nDry-run only unless -y is passed.\nNOTE! This is a one-time programmable (unreversible) change.\nTo set enhanced attribute to general partition being created set\n <enh_attr> to 1 else set it to 0.\nTo set extended attribute to general partition\n set <ext_attr> to 1,2 else set it to 0",
76 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 },
Al Cooper64c2de82015-05-01 08:24:37 -040098 { do_boot_bus_conditions_set, -4,
99 "bootbus set", "<boot_mode> " "<reset_boot_bus_conditions> " "<boot_bus_width> " "<device>\n"
100 "Set Boot Bus Conditions.\n"
101 "<boot_mode> must be \"single_backward|single_hs|dual\"\n"
102 "<reset_boot_bus_conditions> must be \"x1|retain\"\n"
103 "<boot_bus_width> must be \"x1|x4|x8\"",
104 NULL
105 },
Jaehoon Chung86496512012-09-21 10:08:05 +0000106 { do_write_bkops_en, -1,
107 "bkops enable", "<device>\n"
108 "Enable the eMMC BKOPS feature on <device>.\nNOTE! This is a one-time programmable (unreversible) change.",
109 NULL
110 },
Chris Ballf74dfe22012-10-19 16:49:55 -0400111 { do_hwreset_en, -1,
112 "hwreset enable", "<device>\n"
113 "Permanently enable the eMMC H/W Reset feature on <device>.\nNOTE! This is a one-time programmable (unreversible) change.",
114 NULL
115 },
116 { do_hwreset_dis, -1,
117 "hwreset disable", "<device>\n"
118 "Permanently disable the eMMC H/W Reset feature on <device>.\nNOTE! This is a one-time programmable (unreversible) change.",
119 NULL
120 },
Yaniv Gardi21bb4732013-05-26 13:25:33 -0400121 { do_sanitize, -1,
122 "sanitize", "<device>\n"
123 "Send Sanitize command to the <device>.\nThis will delete the unmapped memory region of the device.",
124 NULL
125 },
Roman Peniaevc6cb0532014-08-12 23:25:45 +0900126 { do_rpmb_write_key, -1,
127 "rpmb write-key", "<rpmb device> <key file>\n"
Chris Ballf4eb2412014-08-12 10:44:52 -0400128 "Program authentication key which is 32 bytes length and stored\n"
129 "in the specified file. Also you can specify '-' instead of\n"
130 "key file path to read the key from stdin.\n"
131 "NOTE! This is a one-time programmable (unreversible) change.\n"
Roman Peniaevc6cb0532014-08-12 23:25:45 +0900132 "Example:\n"
Chris Ballf4eb2412014-08-12 10:44:52 -0400133 " $ echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH | \\\n"
134 " mmc rpmb write-key /dev/mmcblk0rpmb -",
Roman Peniaevc6cb0532014-08-12 23:25:45 +0900135 NULL
136 },
137 { do_rpmb_read_counter, -1,
138 "rpmb read-counter", "<rpmb device>\n"
139 "Counter value for the <rpmb device> will be read to stdout.",
140 NULL
141 },
142 { do_rpmb_read_block, -1,
143 "rpmb read-block", "<rpmb device> <address> <blocks count> <output file> [key file]\n"
Chris Ballf4eb2412014-08-12 10:44:52 -0400144 "Blocks of 256 bytes will be read from <rpmb device> to output\n"
145 "file or stdout if '-' is specified. If key is specified - read\n"
146 "data will be verified. Instead of regular path you can specify\n"
147 "'-' to read key from stdin.\n"
Roman Peniaevc6cb0532014-08-12 23:25:45 +0900148 "Example:\n"
Chris Ballf4eb2412014-08-12 10:44:52 -0400149 " $ echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH | \\\n"
150 " mmc rpmb read-block /dev/mmcblk0rpmb 0x02 2 /tmp/block -\n"
Roman Peniaevc6cb0532014-08-12 23:25:45 +0900151 "or read two blocks without verification\n"
152 " $ mmc rpmb read-block /dev/mmcblk0rpmb 0x02 2 /tmp/block",
153 NULL
154 },
155 { do_rpmb_write_block, -1,
156 "rpmb write-block", "<rpmb device> <address> <256 byte data file> <key file>\n"
Chris Ballf4eb2412014-08-12 10:44:52 -0400157 "Block of 256 bytes will be written from data file to\n"
158 "<rpmb device>. Also you can specify '-' instead of key\n"
159 "file path or data file to read the data from stdin.\n"
Roman Peniaevc6cb0532014-08-12 23:25:45 +0900160 "Example:\n"
Chris Ballf4eb2412014-08-12 10:44:52 -0400161 " $ (awk 'BEGIN {while (c++<256) printf \"a\"}' | \\\n"
162 " echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH) | \\\n"
Roman Peniaevc6cb0532014-08-12 23:25:45 +0900163 " mmc rpmb write-block /dev/mmcblk0rpmb 0x02 - -",
164 NULL
165 },
Al Cooperd0b46442015-04-29 18:12:35 -0400166 { do_cache_en, -1,
167 "cache enable", "<device>\n"
168 "Enable the eMMC cache feature on <device>.\n"
169 "NOTE! The cache is an optional feature on devices >= eMMC4.5.",
170 NULL
171 },
172 { do_cache_dis, -1,
173 "cache disable", "<device>\n"
174 "Disable the eMMC cache feature on <device>.\n"
175 "NOTE! The cache is an optional feature on devices >= eMMC4.5.",
176 NULL
177 },
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500178 { 0, 0, 0, 0 }
179};
180
181static char *get_prgname(char *programname)
182{
183 char *np;
184 np = strrchr(programname,'/');
185 if(!np)
186 np = programname;
187 else
188 np++;
189
190 return np;
191}
192
193static void print_help(char *programname, struct Command *cmd, int helptype)
194{
195 char *pc;
196
197 printf("\t%s %s ", programname, cmd->verb );
198
199 if (helptype == ADVANCED_HELP && cmd->adv_help)
200 for(pc = cmd->adv_help; *pc; pc++){
201 putchar(*pc);
202 if(*pc == '\n')
203 printf("\t\t");
204 }
205 else
206 for(pc = cmd->help; *pc; pc++){
207 putchar(*pc);
208 if(*pc == '\n')
209 printf("\t\t");
210 }
211
212 putchar('\n');
213}
214
215static void help(char *np)
216{
217 struct Command *cp;
218
219 printf("Usage:\n");
220 for( cp = commands; cp->verb; cp++ )
221 print_help(np, cp, BASIC_HELP);
222
223 printf("\n\t%s help|--help|-h\n\t\tShow the help.\n",np);
224 printf("\n\t%s <cmd> --help\n\t\tShow detailed help for a command or subset of commands.\n",np);
225 printf("\n%s\n", MMC_VERSION);
226}
227
228static int split_command(char *cmd, char ***commands)
229{
230 int c, l;
231 char *p, *s;
232
233 for( *commands = 0, l = c = 0, p = s = cmd ; ; p++, l++ ){
234 if ( *p && *p != ' ' )
235 continue;
236
237 /* c + 2 so that we have room for the null */
238 (*commands) = realloc( (*commands), sizeof(char *)*(c + 2));
239 (*commands)[c] = strndup(s, l);
240 c++;
241 l = 0;
242 s = p+1;
243 if( !*p ) break;
244 }
245
246 (*commands)[c] = 0;
247 return c;
248}
249
250/*
251 This function checks if the passed command is ambiguous
252*/
253static int check_ambiguity(struct Command *cmd, char **argv){
254 int i;
255 struct Command *cp;
256 /* check for ambiguity */
257 for( i = 0 ; i < cmd->ncmds ; i++ ){
258 int match;
259 for( match = 0, cp = commands; cp->verb; cp++ ){
260 int j, skip;
261 char *s1, *s2;
262
263 if( cp->ncmds < i )
264 continue;
265
266 for( skip = 0, j = 0 ; j < i ; j++ )
267 if( strcmp(cmd->cmds[j], cp->cmds[j])){
268 skip=1;
269 break;
270 }
271 if(skip)
272 continue;
273
274 if( !strcmp(cmd->cmds[i], cp->cmds[i]))
275 continue;
276 for(s2 = cp->cmds[i], s1 = argv[i+1];
277 *s1 == *s2 && *s1; s1++, s2++ ) ;
278 if( !*s1 )
279 match++;
280 }
281 if(match){
282 int j;
283 fprintf(stderr, "ERROR: in command '");
284 for( j = 0 ; j <= i ; j++ )
285 fprintf(stderr, "%s%s",j?" ":"", argv[j+1]);
286 fprintf(stderr, "', '%s' is ambiguous\n",argv[j]);
287 return -2;
288 }
289 }
290 return 0;
291}
292
293/*
294 * This function, compacts the program name and the command in the first
295 * element of the '*av' array
296 */
297static int prepare_args(int *ac, char ***av, char *prgname, struct Command *cmd ){
298
299 char **ret;
300 int i;
301 char *newname;
302
303 ret = (char **)malloc(sizeof(char*)*(*ac+1));
304 newname = (char*)malloc(strlen(prgname)+strlen(cmd->verb)+2);
305 if( !ret || !newname ){
306 free(ret);
307 free(newname);
308 return -1;
309 }
310
311 ret[0] = newname;
312 for(i=0; i < *ac ; i++ )
313 ret[i+1] = (*av)[i];
314
315 strcpy(newname, prgname);
316 strcat(newname, " ");
317 strcat(newname, cmd->verb);
318
319 (*ac)++;
320 *av = ret;
321
322 return 0;
323
324}
325
326/*
327 This function performs the following jobs:
328 - show the help if '--help' or 'help' or '-h' are passed
329 - verify that a command is not ambiguous, otherwise show which
330 part of the command is ambiguous
331 - if after a (even partial) command there is '--help' show detailed help
332 for all the matching commands
333 - if the command doesn't match show an error
334 - finally, if a command matches, they return which command matched and
335 the arguments
336
337 The function return 0 in case of help is requested; <0 in case
338 of uncorrect command; >0 in case of matching commands
339 argc, argv are the arg-counter and arg-vector (input)
340 *nargs_ is the number of the arguments after the command (output)
341 **cmd_ is the invoked command (output)
342 ***args_ are the arguments after the command
343
344*/
345static int parse_args(int argc, char **argv,
346 CommandFunction *func_,
347 int *nargs_, char **cmd_, char ***args_ )
348{
349 struct Command *cp;
350 struct Command *matchcmd=0;
351 char *prgname = get_prgname(argv[0]);
352 int i=0, helprequested=0;
353
354 if( argc < 2 || !strcmp(argv[1], "help") ||
355 !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")){
356 help(prgname);
357 return 0;
358 }
359
360 for( cp = commands; cp->verb; cp++ )
361 if( !cp->ncmds)
362 cp->ncmds = split_command(cp->verb, &(cp->cmds));
363
364 for( cp = commands; cp->verb; cp++ ){
365 int match;
366
367 if( argc-1 < cp->ncmds )
368 continue;
369 for( match = 1, i = 0 ; i < cp->ncmds ; i++ ){
370 char *s1, *s2;
371 s1 = cp->cmds[i];
372 s2 = argv[i+1];
373
374 for(s2 = cp->cmds[i], s1 = argv[i+1];
375 *s1 == *s2 && *s1;
376 s1++, s2++ ) ;
377 if( *s1 ){
378 match=0;
379 break;
380 }
381 }
382
383 /* If you understand why this code works ...
384 you are a genious !! */
385 if(argc>i+1 && !strcmp(argv[i+1],"--help")){
386 if(!helprequested)
387 printf("Usage:\n");
388 print_help(prgname, cp, ADVANCED_HELP);
389 helprequested=1;
390 continue;
391 }
392
393 if(!match)
394 continue;
395
396 matchcmd = cp;
397 *nargs_ = argc-matchcmd->ncmds-1;
398 *cmd_ = matchcmd->verb;
399 *args_ = argv+matchcmd->ncmds+1;
400 *func_ = cp->func;
401
402 break;
403 }
404
405 if(helprequested){
406 printf("\n%s\n", MMC_VERSION);
407 return 0;
408 }
409
410 if(!matchcmd){
411 fprintf( stderr, "ERROR: unknown command '%s'\n",argv[1]);
412 help(prgname);
413 return -1;
414 }
415
416 if(check_ambiguity(matchcmd, argv))
417 return -2;
418
419 /* check the number of argument */
420 if (matchcmd->nargs < 0 && matchcmd->nargs < -*nargs_ ){
421 fprintf(stderr, "ERROR: '%s' requires minimum %d arg(s)\n",
422 matchcmd->verb, -matchcmd->nargs);
423 return -2;
424 }
425 if(matchcmd->nargs >= 0 && matchcmd->nargs != *nargs_ && matchcmd->nargs != 999){
426 fprintf(stderr, "ERROR: '%s' requires %d arg(s)\n",
427 matchcmd->verb, matchcmd->nargs);
428 return -2;
429 }
430
431 if (prepare_args( nargs_, args_, prgname, matchcmd )){
432 fprintf(stderr, "ERROR: not enough memory\\n");
433 return -20;
434 }
435
436
437 return 1;
438}
439int main(int ac, char **av )
440{
441 char *cmd=0, **args=0;
442 int nargs=0, r;
443 CommandFunction func=0;
444
445 r = parse_args(ac, av, &func, &nargs, &cmd, &args);
446 if( r <= 0 ){
447 /* error or no command to parse*/
448 exit(-r);
449 }
450
451 exit(func(nargs, args));
452}
453