blob: f8dacdc1d2610df2353a55b57fa57c9f0d133b4e [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 */
Gwendal Grignoueb1cd012015-01-08 15:34:55 -080039 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 */
Goffredo Baroncelli80d26602012-02-12 11:43:14 -050043
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 },
Gwendal Grignou771984c2014-07-01 12:46:18 -0700118 { do_emmc50_ffu, -2,
Gwendal Grignou0da2c512015-01-08 15:36:03 -0800119 "ffu", "[-k hack_type[:hack_value]] <image name> <device>\n"
120 "run eMMC 5.0 Field firmware update.\n"
121 "Device specific hacks can be specificied.",
Gwendal Grignou771984c2014-07-01 12:46:18 -0700122 NULL
123 },
Roman Peniaev023cc7c2014-08-12 23:25:45 +0900124 { do_rpmb_write_key, -1,
125 "rpmb write-key", "<rpmb device> <key file>\n"
126 "Program authentication key which is 32 bytes length and stored in the specified file.\n"
127 "Also you can specify '-' instead of key file path and utility will read the key from stdin.\n"
128 "BEWARE: key can be programmed only once!\n"
129 "Example:\n"
130 " $ echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH | mmc rpmb write-key /dev/mmcblk0rpmb -",
131 NULL
132 },
133 { do_rpmb_read_counter, -1,
134 "rpmb read-counter", "<rpmb device>\n"
135 "Counter value for the <rpmb device> will be read to stdout.",
136 NULL
137 },
138 { do_rpmb_read_block, -1,
139 "rpmb read-block", "<rpmb device> <address> <blocks count> <output file> [key file]\n"
140 "Blocks of 256 bytes will be read from <rpmb device> to output file or stdout if '-' is specified instead of regular path.\n"
141 "If key is specified - read data will be verified. Instead of regular path you can specify '-' and key will be read from stdin.\n"
142 "Example:\n"
143 " $ echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH | mmc rpmb read-block /dev/mmcblk0rpmb 0x02 2 /tmp/block -\n"
144 "or read two blocks without verification\n"
145 " $ mmc rpmb read-block /dev/mmcblk0rpmb 0x02 2 /tmp/block",
146 NULL
147 },
148 { do_rpmb_write_block, -1,
149 "rpmb write-block", "<rpmb device> <address> <256 byte data file> <key file>\n"
150 "Block of 256 bytes will be written from data file to <rpmb device>.\n"
151 "Also you can specify '-' instead of key file path or data file and utility will read the data from stdin.\n"
152 "Example:\n"
153 " $ (awk 'BEGIN {while (c++<256) printf \"a\"}' | echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH) | \\\n"
154 " mmc rpmb write-block /dev/mmcblk0rpmb 0x02 - -",
155 NULL
156 },
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500157 { 0, 0, 0, 0 }
158};
159
160static char *get_prgname(char *programname)
161{
162 char *np;
163 np = strrchr(programname,'/');
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800164 if (!np)
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500165 np = programname;
166 else
167 np++;
168
169 return np;
170}
171
172static void print_help(char *programname, struct Command *cmd, int helptype)
173{
174 char *pc;
175
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800176 printf("\t%s %s ", programname, cmd->verb);
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500177
178 if (helptype == ADVANCED_HELP && cmd->adv_help)
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800179 for (pc = cmd->adv_help; *pc; pc++) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500180 putchar(*pc);
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800181 if (*pc == '\n')
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500182 printf("\t\t");
183 }
184 else
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800185 for (pc = cmd->help; *pc; pc++) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500186 putchar(*pc);
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800187 if (*pc == '\n')
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500188 printf("\t\t");
189 }
190
191 putchar('\n');
192}
193
194static void help(char *np)
195{
196 struct Command *cp;
197
198 printf("Usage:\n");
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800199 for (cp = commands; cp->verb; cp++)
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500200 print_help(np, cp, BASIC_HELP);
201
202 printf("\n\t%s help|--help|-h\n\t\tShow the help.\n",np);
203 printf("\n\t%s <cmd> --help\n\t\tShow detailed help for a command or subset of commands.\n",np);
204 printf("\n%s\n", MMC_VERSION);
205}
206
207static int split_command(char *cmd, char ***commands)
208{
209 int c, l;
210 char *p, *s;
211
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800212 for (*commands = 0, l = c = 0, p = s = cmd ; ; p++, l++) {
213 if (*p && *p != ' ')
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500214 continue;
215
216 /* c + 2 so that we have room for the null */
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800217 (*commands) = realloc((*commands), sizeof(char *)*(c + 2));
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500218 (*commands)[c] = strndup(s, l);
219 c++;
220 l = 0;
221 s = p+1;
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800222 if (!*p) break;
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500223 }
224
225 (*commands)[c] = 0;
226 return c;
227}
228
229/*
230 This function checks if the passed command is ambiguous
231*/
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800232static int check_ambiguity(struct Command *cmd, char **argv) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500233 int i;
234 struct Command *cp;
235 /* check for ambiguity */
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800236 for (i = 0 ; i < cmd->ncmds ; i++) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500237 int match;
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800238 for (match = 0, cp = commands; cp->verb; cp++) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500239 int j, skip;
240 char *s1, *s2;
241
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800242 if (cp->ncmds < i)
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500243 continue;
244
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800245 for (skip = 0, j = 0 ; j < i ; j++)
246 if (strcmp(cmd->cmds[j], cp->cmds[j])) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500247 skip=1;
248 break;
249 }
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800250 if (skip)
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500251 continue;
252
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800253 if (!strcmp(cmd->cmds[i], cp->cmds[i]))
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500254 continue;
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800255 for (s2 = cp->cmds[i], s1 = argv[i+1];
256 *s1 == *s2 && *s1; s1++, s2++) ;
257 if (!*s1)
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500258 match++;
259 }
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800260 if (match) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500261 int j;
262 fprintf(stderr, "ERROR: in command '");
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800263 for (j = 0 ; j <= i ; j++)
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500264 fprintf(stderr, "%s%s",j?" ":"", argv[j+1]);
265 fprintf(stderr, "', '%s' is ambiguous\n",argv[j]);
266 return -2;
267 }
268 }
269 return 0;
270}
271
272/*
273 * This function, compacts the program name and the command in the first
274 * element of the '*av' array
275 */
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800276static int prepare_args(int *ac, char ***av, char *prgname,
277 struct Command *cmd) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500278
279 char **ret;
280 int i;
281 char *newname;
282
283 ret = (char **)malloc(sizeof(char*)*(*ac+1));
284 newname = (char*)malloc(strlen(prgname)+strlen(cmd->verb)+2);
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800285 if (!ret || !newname) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500286 free(ret);
287 free(newname);
288 return -1;
289 }
290
291 ret[0] = newname;
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800292 for (i=0; i < *ac ; i++)
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500293 ret[i+1] = (*av)[i];
294
295 strcpy(newname, prgname);
296 strcat(newname, " ");
297 strcat(newname, cmd->verb);
298
299 (*ac)++;
300 *av = ret;
301
302 return 0;
303
304}
305
306/*
307 This function performs the following jobs:
308 - show the help if '--help' or 'help' or '-h' are passed
309 - verify that a command is not ambiguous, otherwise show which
310 part of the command is ambiguous
311 - if after a (even partial) command there is '--help' show detailed help
312 for all the matching commands
313 - if the command doesn't match show an error
314 - finally, if a command matches, they return which command matched and
315 the arguments
316
317 The function return 0 in case of help is requested; <0 in case
318 of uncorrect command; >0 in case of matching commands
319 argc, argv are the arg-counter and arg-vector (input)
320 *nargs_ is the number of the arguments after the command (output)
321 **cmd_ is the invoked command (output)
322 ***args_ are the arguments after the command
323
324*/
325static int parse_args(int argc, char **argv,
326 CommandFunction *func_,
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800327 int *nargs_, char **cmd_, char ***args_)
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500328{
329 struct Command *cp;
330 struct Command *matchcmd=0;
331 char *prgname = get_prgname(argv[0]);
332 int i=0, helprequested=0;
333
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800334 if (argc < 2 || !strcmp(argv[1], "help") ||
335 !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500336 help(prgname);
337 return 0;
338 }
339
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800340 for (cp = commands; cp->verb; cp++)
341 if (!cp->ncmds)
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500342 cp->ncmds = split_command(cp->verb, &(cp->cmds));
343
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800344 for (cp = commands; cp->verb; cp++) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500345 int match;
346
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800347 if (argc-1 < cp->ncmds)
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500348 continue;
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800349 for (match = 1, i = 0 ; i < cp->ncmds ; i++) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500350 char *s1, *s2;
351 s1 = cp->cmds[i];
352 s2 = argv[i+1];
353
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800354 for (s2 = cp->cmds[i], s1 = argv[i+1];
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500355 *s1 == *s2 && *s1;
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800356 s1++, s2++) ;
357 if (*s1) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500358 match=0;
359 break;
360 }
361 }
362
363 /* If you understand why this code works ...
364 you are a genious !! */
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800365 if (argc>i+1 && !strcmp(argv[i+1],"--help")) {
366 if (!helprequested)
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500367 printf("Usage:\n");
368 print_help(prgname, cp, ADVANCED_HELP);
369 helprequested=1;
370 continue;
371 }
372
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800373 if (!match)
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500374 continue;
375
376 matchcmd = cp;
377 *nargs_ = argc-matchcmd->ncmds-1;
378 *cmd_ = matchcmd->verb;
379 *args_ = argv+matchcmd->ncmds+1;
380 *func_ = cp->func;
381
382 break;
383 }
384
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800385 if (helprequested) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500386 printf("\n%s\n", MMC_VERSION);
387 return 0;
388 }
389
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800390 if (!matchcmd) {
391 fprintf(stderr, "ERROR: unknown command '%s'\n",argv[1]);
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500392 help(prgname);
393 return -1;
394 }
395
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800396 if (check_ambiguity(matchcmd, argv))
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500397 return -2;
398
399 /* check the number of argument */
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800400 if (matchcmd->nargs < 0 && matchcmd->nargs < -*nargs_) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500401 fprintf(stderr, "ERROR: '%s' requires minimum %d arg(s)\n",
402 matchcmd->verb, -matchcmd->nargs);
403 return -2;
404 }
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800405 if (matchcmd->nargs >= 0 && matchcmd->nargs != *nargs_ && matchcmd->nargs != 999) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500406 fprintf(stderr, "ERROR: '%s' requires %d arg(s)\n",
407 matchcmd->verb, matchcmd->nargs);
408 return -2;
409 }
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800410 if (prepare_args(nargs_, args_, prgname, matchcmd)) {
411 fprintf(stderr, "ERROR: not enough memory\\n");
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500412 return -20;
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800413 }
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500414
415
416 return 1;
417}
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800418int main(int ac, char **av)
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500419{
420 char *cmd=0, **args=0;
421 int nargs=0, r;
422 CommandFunction func=0;
423
424 r = parse_args(ac, av, &func, &nargs, &cmd, &args);
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800425 if (r <= 0) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500426 /* error or no command to parse*/
427 exit(-r);
428 }
429
430 exit(func(nargs, args));
431}
432