blob: fb239500ea0906dd32bfc7c415fa46403045974a [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 },
Balaji T Kd78ce082015-04-29 18:12:33 -040078 { do_create_gp_partition, -6,
79 "gp create", "<-y|-n> " "<length KiB> " "<partition> " "<enh_attr> " "<ext_attr> " "<device>\n"
80 "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",
81 NULL
82 },
Ben Gardinerd91d3692013-05-30 17:12:51 -040083 { do_enh_area_set, -4,
84 "enh_area set", "<-y|-n> " "<start KiB> " "<length KiB> " "<device>\n"
85 "Enable the enhanced user area for the <device>.\nDry-run only unless -y is passed.\nNOTE! This is a one-time programmable (unreversible) change.",
86 NULL
87 },
Ben Gardiner196d0d22013-09-19 11:14:29 -040088 { do_write_reliability_set, -2,
89 "write_reliability set", "<-y|-n> " "<partition> " "<device>\n"
90 "Enable write reliability per partition for the <device>.\nDry-run only unless -y is passed.\nNOTE! This is a one-time programmable (unreversible) change.",
91 NULL
92 },
Ben Gardiner27c357d2013-05-30 17:12:47 -040093 { do_status_get, -1,
94 "status get", "<device>\n"
95 "Print the response to STATUS_SEND (CMD13).",
96 NULL
97 },
Giuseppe CAVALLARO7bd13202012-04-19 10:58:37 +020098 { do_write_boot_en, -3,
99 "bootpart enable", "<boot_partition> " "<send_ack> " "<device>\n"
100 "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.",
101 NULL
102 },
Jaehoon Chung86496512012-09-21 10:08:05 +0000103 { do_write_bkops_en, -1,
104 "bkops enable", "<device>\n"
105 "Enable the eMMC BKOPS feature on <device>.\nNOTE! This is a one-time programmable (unreversible) change.",
106 NULL
107 },
Chris Ballf74dfe22012-10-19 16:49:55 -0400108 { do_hwreset_en, -1,
109 "hwreset enable", "<device>\n"
110 "Permanently enable the eMMC H/W Reset feature on <device>.\nNOTE! This is a one-time programmable (unreversible) change.",
111 NULL
112 },
113 { do_hwreset_dis, -1,
114 "hwreset disable", "<device>\n"
115 "Permanently disable the eMMC H/W Reset feature on <device>.\nNOTE! This is a one-time programmable (unreversible) change.",
116 NULL
117 },
Yaniv Gardi21bb4732013-05-26 13:25:33 -0400118 { do_sanitize, -1,
119 "sanitize", "<device>\n"
120 "Send Sanitize command to the <device>.\nThis will delete the unmapped memory region of the device.",
121 NULL
122 },
Gwendal Grignou771984c2014-07-01 12:46:18 -0700123 { do_emmc50_ffu, -2,
Gwendal Grignou0da2c512015-01-08 15:36:03 -0800124 "ffu", "[-k hack_type[:hack_value]] <image name> <device>\n"
125 "run eMMC 5.0 Field firmware update.\n"
126 "Device specific hacks can be specificied.",
Gwendal Grignou771984c2014-07-01 12:46:18 -0700127 NULL
128 },
Roman Peniaev023cc7c2014-08-12 23:25:45 +0900129 { do_rpmb_write_key, -1,
130 "rpmb write-key", "<rpmb device> <key file>\n"
Chris Balld186ab52014-08-12 10:44:52 -0400131 "Program authentication key which is 32 bytes length and stored\n"
132 "in the specified file. Also you can specify '-' instead of\n"
133 "key file path to read the key from stdin.\n"
134 "NOTE! This is a one-time programmable (unreversible) change.\n"
Roman Peniaev023cc7c2014-08-12 23:25:45 +0900135 "Example:\n"
Chris Balld186ab52014-08-12 10:44:52 -0400136 " $ echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH | \\\n"
137 " mmc rpmb write-key /dev/mmcblk0rpmb -",
Roman Peniaev023cc7c2014-08-12 23:25:45 +0900138 NULL
139 },
140 { do_rpmb_read_counter, -1,
141 "rpmb read-counter", "<rpmb device>\n"
142 "Counter value for the <rpmb device> will be read to stdout.",
143 NULL
144 },
145 { do_rpmb_read_block, -1,
146 "rpmb read-block", "<rpmb device> <address> <blocks count> <output file> [key file]\n"
Chris Balld186ab52014-08-12 10:44:52 -0400147 "Blocks of 256 bytes will be read from <rpmb device> to output\n"
148 "file or stdout if '-' is specified. If key is specified - read\n"
149 "data will be verified. Instead of regular path you can specify\n"
150 "'-' to read key from stdin.\n"
Roman Peniaev023cc7c2014-08-12 23:25:45 +0900151 "Example:\n"
Chris Balld186ab52014-08-12 10:44:52 -0400152 " $ echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH | \\\n"
153 " mmc rpmb read-block /dev/mmcblk0rpmb 0x02 2 /tmp/block -\n"
Roman Peniaev023cc7c2014-08-12 23:25:45 +0900154 "or read two blocks without verification\n"
155 " $ mmc rpmb read-block /dev/mmcblk0rpmb 0x02 2 /tmp/block",
156 NULL
157 },
158 { do_rpmb_write_block, -1,
159 "rpmb write-block", "<rpmb device> <address> <256 byte data file> <key file>\n"
Chris Balld186ab52014-08-12 10:44:52 -0400160 "Block of 256 bytes will be written from data file to\n"
161 "<rpmb device>. Also you can specify '-' instead of key\n"
162 "file path or data file to read the data from stdin.\n"
Roman Peniaev023cc7c2014-08-12 23:25:45 +0900163 "Example:\n"
Chris Balld186ab52014-08-12 10:44:52 -0400164 " $ (awk 'BEGIN {while (c++<256) printf \"a\"}' | \\\n"
165 " echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH) | \\\n"
Roman Peniaev023cc7c2014-08-12 23:25:45 +0900166 " mmc rpmb write-block /dev/mmcblk0rpmb 0x02 - -",
167 NULL
168 },
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500169 { 0, 0, 0, 0 }
170};
171
172static char *get_prgname(char *programname)
173{
174 char *np;
175 np = strrchr(programname,'/');
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800176 if (!np)
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500177 np = programname;
178 else
179 np++;
180
181 return np;
182}
183
184static void print_help(char *programname, struct Command *cmd, int helptype)
185{
186 char *pc;
187
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800188 printf("\t%s %s ", programname, cmd->verb);
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500189
190 if (helptype == ADVANCED_HELP && cmd->adv_help)
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800191 for (pc = cmd->adv_help; *pc; pc++) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500192 putchar(*pc);
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800193 if (*pc == '\n')
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500194 printf("\t\t");
195 }
196 else
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800197 for (pc = cmd->help; *pc; pc++) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500198 putchar(*pc);
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800199 if (*pc == '\n')
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500200 printf("\t\t");
201 }
202
203 putchar('\n');
204}
205
206static void help(char *np)
207{
208 struct Command *cp;
209
210 printf("Usage:\n");
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800211 for (cp = commands; cp->verb; cp++)
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500212 print_help(np, cp, BASIC_HELP);
213
214 printf("\n\t%s help|--help|-h\n\t\tShow the help.\n",np);
215 printf("\n\t%s <cmd> --help\n\t\tShow detailed help for a command or subset of commands.\n",np);
216 printf("\n%s\n", MMC_VERSION);
217}
218
219static int split_command(char *cmd, char ***commands)
220{
221 int c, l;
222 char *p, *s;
223
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800224 for (*commands = 0, l = c = 0, p = s = cmd ; ; p++, l++) {
225 if (*p && *p != ' ')
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500226 continue;
227
228 /* c + 2 so that we have room for the null */
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800229 (*commands) = realloc((*commands), sizeof(char *)*(c + 2));
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500230 (*commands)[c] = strndup(s, l);
231 c++;
232 l = 0;
233 s = p+1;
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800234 if (!*p) break;
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500235 }
236
237 (*commands)[c] = 0;
238 return c;
239}
240
241/*
242 This function checks if the passed command is ambiguous
243*/
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800244static int check_ambiguity(struct Command *cmd, char **argv) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500245 int i;
246 struct Command *cp;
247 /* check for ambiguity */
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800248 for (i = 0 ; i < cmd->ncmds ; i++) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500249 int match;
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800250 for (match = 0, cp = commands; cp->verb; cp++) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500251 int j, skip;
252 char *s1, *s2;
253
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800254 if (cp->ncmds < i)
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500255 continue;
256
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800257 for (skip = 0, j = 0 ; j < i ; j++)
258 if (strcmp(cmd->cmds[j], cp->cmds[j])) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500259 skip=1;
260 break;
261 }
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800262 if (skip)
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500263 continue;
264
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800265 if (!strcmp(cmd->cmds[i], cp->cmds[i]))
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500266 continue;
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800267 for (s2 = cp->cmds[i], s1 = argv[i+1];
268 *s1 == *s2 && *s1; s1++, s2++) ;
269 if (!*s1)
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500270 match++;
271 }
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800272 if (match) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500273 int j;
274 fprintf(stderr, "ERROR: in command '");
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800275 for (j = 0 ; j <= i ; j++)
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500276 fprintf(stderr, "%s%s",j?" ":"", argv[j+1]);
277 fprintf(stderr, "', '%s' is ambiguous\n",argv[j]);
278 return -2;
279 }
280 }
281 return 0;
282}
283
284/*
285 * This function, compacts the program name and the command in the first
286 * element of the '*av' array
287 */
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800288static int prepare_args(int *ac, char ***av, char *prgname,
289 struct Command *cmd) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500290
291 char **ret;
292 int i;
293 char *newname;
294
295 ret = (char **)malloc(sizeof(char*)*(*ac+1));
296 newname = (char*)malloc(strlen(prgname)+strlen(cmd->verb)+2);
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800297 if (!ret || !newname) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500298 free(ret);
299 free(newname);
300 return -1;
301 }
302
303 ret[0] = newname;
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800304 for (i=0; i < *ac ; i++)
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500305 ret[i+1] = (*av)[i];
306
307 strcpy(newname, prgname);
308 strcat(newname, " ");
309 strcat(newname, cmd->verb);
310
311 (*ac)++;
312 *av = ret;
313
314 return 0;
315
316}
317
318/*
319 This function performs the following jobs:
320 - show the help if '--help' or 'help' or '-h' are passed
321 - verify that a command is not ambiguous, otherwise show which
322 part of the command is ambiguous
323 - if after a (even partial) command there is '--help' show detailed help
324 for all the matching commands
325 - if the command doesn't match show an error
326 - finally, if a command matches, they return which command matched and
327 the arguments
328
329 The function return 0 in case of help is requested; <0 in case
330 of uncorrect command; >0 in case of matching commands
331 argc, argv are the arg-counter and arg-vector (input)
332 *nargs_ is the number of the arguments after the command (output)
333 **cmd_ is the invoked command (output)
334 ***args_ are the arguments after the command
335
336*/
337static int parse_args(int argc, char **argv,
338 CommandFunction *func_,
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800339 int *nargs_, char **cmd_, char ***args_)
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500340{
341 struct Command *cp;
342 struct Command *matchcmd=0;
343 char *prgname = get_prgname(argv[0]);
344 int i=0, helprequested=0;
345
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800346 if (argc < 2 || !strcmp(argv[1], "help") ||
347 !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500348 help(prgname);
349 return 0;
350 }
351
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800352 for (cp = commands; cp->verb; cp++)
353 if (!cp->ncmds)
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500354 cp->ncmds = split_command(cp->verb, &(cp->cmds));
355
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800356 for (cp = commands; cp->verb; cp++) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500357 int match;
358
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800359 if (argc-1 < cp->ncmds)
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500360 continue;
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800361 for (match = 1, i = 0 ; i < cp->ncmds ; i++) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500362 char *s1, *s2;
363 s1 = cp->cmds[i];
364 s2 = argv[i+1];
365
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800366 for (s2 = cp->cmds[i], s1 = argv[i+1];
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500367 *s1 == *s2 && *s1;
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800368 s1++, s2++) ;
369 if (*s1) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500370 match=0;
371 break;
372 }
373 }
374
375 /* If you understand why this code works ...
376 you are a genious !! */
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800377 if (argc>i+1 && !strcmp(argv[i+1],"--help")) {
378 if (!helprequested)
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500379 printf("Usage:\n");
380 print_help(prgname, cp, ADVANCED_HELP);
381 helprequested=1;
382 continue;
383 }
384
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800385 if (!match)
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500386 continue;
387
388 matchcmd = cp;
389 *nargs_ = argc-matchcmd->ncmds-1;
390 *cmd_ = matchcmd->verb;
391 *args_ = argv+matchcmd->ncmds+1;
392 *func_ = cp->func;
393
394 break;
395 }
396
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800397 if (helprequested) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500398 printf("\n%s\n", MMC_VERSION);
399 return 0;
400 }
401
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800402 if (!matchcmd) {
403 fprintf(stderr, "ERROR: unknown command '%s'\n",argv[1]);
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500404 help(prgname);
405 return -1;
406 }
407
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800408 if (check_ambiguity(matchcmd, argv))
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500409 return -2;
410
411 /* check the number of argument */
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800412 if (matchcmd->nargs < 0 && matchcmd->nargs < -*nargs_) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500413 fprintf(stderr, "ERROR: '%s' requires minimum %d arg(s)\n",
414 matchcmd->verb, -matchcmd->nargs);
415 return -2;
416 }
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800417 if (matchcmd->nargs >= 0 && matchcmd->nargs != *nargs_ && matchcmd->nargs != 999) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500418 fprintf(stderr, "ERROR: '%s' requires %d arg(s)\n",
419 matchcmd->verb, matchcmd->nargs);
420 return -2;
421 }
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800422 if (prepare_args(nargs_, args_, prgname, matchcmd)) {
423 fprintf(stderr, "ERROR: not enough memory\\n");
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500424 return -20;
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800425 }
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500426
427
428 return 1;
429}
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800430int main(int ac, char **av)
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500431{
432 char *cmd=0, **args=0;
433 int nargs=0, r;
434 CommandFunction func=0;
435
436 r = parse_args(ac, av, &func, &nargs, &cmd, &args);
Gwendal Grignoueb1cd012015-01-08 15:34:55 -0800437 if (r <= 0) {
Goffredo Baroncelli80d26602012-02-12 11:43:14 -0500438 /* error or no command to parse*/
439 exit(-r);
440 }
441
442 exit(func(nargs, args));
443}
444