mmc.c revision c6cb053ec59e7667e2140c320e2b7d5a90592a20
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_write_reliability_set, -2, 79 "write_reliability set", "<-y|-n> " "<partition> " "<device>\n" 80 "Enable write reliability per partition for the <device>.\nDry-run only unless -y is passed.\nNOTE! This is a one-time programmable (unreversible) change.", 81 NULL 82 }, 83 { do_status_get, -1, 84 "status get", "<device>\n" 85 "Print the response to STATUS_SEND (CMD13).", 86 NULL 87 }, 88 { do_write_boot_en, -3, 89 "bootpart enable", "<boot_partition> " "<send_ack> " "<device>\n" 90 "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.", 91 NULL 92 }, 93 { do_write_bkops_en, -1, 94 "bkops enable", "<device>\n" 95 "Enable the eMMC BKOPS feature on <device>.\nNOTE! This is a one-time programmable (unreversible) change.", 96 NULL 97 }, 98 { do_hwreset_en, -1, 99 "hwreset enable", "<device>\n" 100 "Permanently enable the eMMC H/W Reset feature on <device>.\nNOTE! This is a one-time programmable (unreversible) change.", 101 NULL 102 }, 103 { do_hwreset_dis, -1, 104 "hwreset disable", "<device>\n" 105 "Permanently disable the eMMC H/W Reset feature on <device>.\nNOTE! This is a one-time programmable (unreversible) change.", 106 NULL 107 }, 108 { do_sanitize, -1, 109 "sanitize", "<device>\n" 110 "Send Sanitize command to the <device>.\nThis will delete the unmapped memory region of the device.", 111 NULL 112 }, 113 { do_rpmb_write_key, -1, 114 "rpmb write-key", "<rpmb device> <key file>\n" 115 "Program authentication key which is 32 bytes length and stored in the specified file.\n" 116 "Also you can specify '-' instead of key file path and utility will read the key from stdin.\n" 117 "BEWARE: key can be programmed only once!\n" 118 "Example:\n" 119 " $ echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH | mmc rpmb write-key /dev/mmcblk0rpmb -", 120 NULL 121 }, 122 { do_rpmb_read_counter, -1, 123 "rpmb read-counter", "<rpmb device>\n" 124 "Counter value for the <rpmb device> will be read to stdout.", 125 NULL 126 }, 127 { do_rpmb_read_block, -1, 128 "rpmb read-block", "<rpmb device> <address> <blocks count> <output file> [key file]\n" 129 "Blocks of 256 bytes will be read from <rpmb device> to output file or stdout if '-' is specified instead of regular path.\n" 130 "If key is specified - read data will be verified. Instead of regular path you can specify '-' and key will be read from stdin.\n" 131 "Example:\n" 132 " $ echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH | mmc rpmb read-block /dev/mmcblk0rpmb 0x02 2 /tmp/block -\n" 133 "or read two blocks without verification\n" 134 " $ mmc rpmb read-block /dev/mmcblk0rpmb 0x02 2 /tmp/block", 135 NULL 136 }, 137 { do_rpmb_write_block, -1, 138 "rpmb write-block", "<rpmb device> <address> <256 byte data file> <key file>\n" 139 "Block of 256 bytes will be written from data file to <rpmb device>.\n" 140 "Also you can specify '-' instead of key file path or data file and utility will read the data from stdin.\n" 141 "Example:\n" 142 " $ (awk 'BEGIN {while (c++<256) printf \"a\"}' | echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH) | \\\n" 143 " mmc rpmb write-block /dev/mmcblk0rpmb 0x02 - -", 144 NULL 145 }, 146 { 0, 0, 0, 0 } 147}; 148 149static char *get_prgname(char *programname) 150{ 151 char *np; 152 np = strrchr(programname,'/'); 153 if(!np) 154 np = programname; 155 else 156 np++; 157 158 return np; 159} 160 161static void print_help(char *programname, struct Command *cmd, int helptype) 162{ 163 char *pc; 164 165 printf("\t%s %s ", programname, cmd->verb ); 166 167 if (helptype == ADVANCED_HELP && cmd->adv_help) 168 for(pc = cmd->adv_help; *pc; pc++){ 169 putchar(*pc); 170 if(*pc == '\n') 171 printf("\t\t"); 172 } 173 else 174 for(pc = cmd->help; *pc; pc++){ 175 putchar(*pc); 176 if(*pc == '\n') 177 printf("\t\t"); 178 } 179 180 putchar('\n'); 181} 182 183static void help(char *np) 184{ 185 struct Command *cp; 186 187 printf("Usage:\n"); 188 for( cp = commands; cp->verb; cp++ ) 189 print_help(np, cp, BASIC_HELP); 190 191 printf("\n\t%s help|--help|-h\n\t\tShow the help.\n",np); 192 printf("\n\t%s <cmd> --help\n\t\tShow detailed help for a command or subset of commands.\n",np); 193 printf("\n%s\n", MMC_VERSION); 194} 195 196static int split_command(char *cmd, char ***commands) 197{ 198 int c, l; 199 char *p, *s; 200 201 for( *commands = 0, l = c = 0, p = s = cmd ; ; p++, l++ ){ 202 if ( *p && *p != ' ' ) 203 continue; 204 205 /* c + 2 so that we have room for the null */ 206 (*commands) = realloc( (*commands), sizeof(char *)*(c + 2)); 207 (*commands)[c] = strndup(s, l); 208 c++; 209 l = 0; 210 s = p+1; 211 if( !*p ) break; 212 } 213 214 (*commands)[c] = 0; 215 return c; 216} 217 218/* 219 This function checks if the passed command is ambiguous 220*/ 221static int check_ambiguity(struct Command *cmd, char **argv){ 222 int i; 223 struct Command *cp; 224 /* check for ambiguity */ 225 for( i = 0 ; i < cmd->ncmds ; i++ ){ 226 int match; 227 for( match = 0, cp = commands; cp->verb; cp++ ){ 228 int j, skip; 229 char *s1, *s2; 230 231 if( cp->ncmds < i ) 232 continue; 233 234 for( skip = 0, j = 0 ; j < i ; j++ ) 235 if( strcmp(cmd->cmds[j], cp->cmds[j])){ 236 skip=1; 237 break; 238 } 239 if(skip) 240 continue; 241 242 if( !strcmp(cmd->cmds[i], cp->cmds[i])) 243 continue; 244 for(s2 = cp->cmds[i], s1 = argv[i+1]; 245 *s1 == *s2 && *s1; s1++, s2++ ) ; 246 if( !*s1 ) 247 match++; 248 } 249 if(match){ 250 int j; 251 fprintf(stderr, "ERROR: in command '"); 252 for( j = 0 ; j <= i ; j++ ) 253 fprintf(stderr, "%s%s",j?" ":"", argv[j+1]); 254 fprintf(stderr, "', '%s' is ambiguous\n",argv[j]); 255 return -2; 256 } 257 } 258 return 0; 259} 260 261/* 262 * This function, compacts the program name and the command in the first 263 * element of the '*av' array 264 */ 265static int prepare_args(int *ac, char ***av, char *prgname, struct Command *cmd ){ 266 267 char **ret; 268 int i; 269 char *newname; 270 271 ret = (char **)malloc(sizeof(char*)*(*ac+1)); 272 newname = (char*)malloc(strlen(prgname)+strlen(cmd->verb)+2); 273 if( !ret || !newname ){ 274 free(ret); 275 free(newname); 276 return -1; 277 } 278 279 ret[0] = newname; 280 for(i=0; i < *ac ; i++ ) 281 ret[i+1] = (*av)[i]; 282 283 strcpy(newname, prgname); 284 strcat(newname, " "); 285 strcat(newname, cmd->verb); 286 287 (*ac)++; 288 *av = ret; 289 290 return 0; 291 292} 293 294/* 295 This function performs the following jobs: 296 - show the help if '--help' or 'help' or '-h' are passed 297 - verify that a command is not ambiguous, otherwise show which 298 part of the command is ambiguous 299 - if after a (even partial) command there is '--help' show detailed help 300 for all the matching commands 301 - if the command doesn't match show an error 302 - finally, if a command matches, they return which command matched and 303 the arguments 304 305 The function return 0 in case of help is requested; <0 in case 306 of uncorrect command; >0 in case of matching commands 307 argc, argv are the arg-counter and arg-vector (input) 308 *nargs_ is the number of the arguments after the command (output) 309 **cmd_ is the invoked command (output) 310 ***args_ are the arguments after the command 311 312*/ 313static int parse_args(int argc, char **argv, 314 CommandFunction *func_, 315 int *nargs_, char **cmd_, char ***args_ ) 316{ 317 struct Command *cp; 318 struct Command *matchcmd=0; 319 char *prgname = get_prgname(argv[0]); 320 int i=0, helprequested=0; 321 322 if( argc < 2 || !strcmp(argv[1], "help") || 323 !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")){ 324 help(prgname); 325 return 0; 326 } 327 328 for( cp = commands; cp->verb; cp++ ) 329 if( !cp->ncmds) 330 cp->ncmds = split_command(cp->verb, &(cp->cmds)); 331 332 for( cp = commands; cp->verb; cp++ ){ 333 int match; 334 335 if( argc-1 < cp->ncmds ) 336 continue; 337 for( match = 1, i = 0 ; i < cp->ncmds ; i++ ){ 338 char *s1, *s2; 339 s1 = cp->cmds[i]; 340 s2 = argv[i+1]; 341 342 for(s2 = cp->cmds[i], s1 = argv[i+1]; 343 *s1 == *s2 && *s1; 344 s1++, s2++ ) ; 345 if( *s1 ){ 346 match=0; 347 break; 348 } 349 } 350 351 /* If you understand why this code works ... 352 you are a genious !! */ 353 if(argc>i+1 && !strcmp(argv[i+1],"--help")){ 354 if(!helprequested) 355 printf("Usage:\n"); 356 print_help(prgname, cp, ADVANCED_HELP); 357 helprequested=1; 358 continue; 359 } 360 361 if(!match) 362 continue; 363 364 matchcmd = cp; 365 *nargs_ = argc-matchcmd->ncmds-1; 366 *cmd_ = matchcmd->verb; 367 *args_ = argv+matchcmd->ncmds+1; 368 *func_ = cp->func; 369 370 break; 371 } 372 373 if(helprequested){ 374 printf("\n%s\n", MMC_VERSION); 375 return 0; 376 } 377 378 if(!matchcmd){ 379 fprintf( stderr, "ERROR: unknown command '%s'\n",argv[1]); 380 help(prgname); 381 return -1; 382 } 383 384 if(check_ambiguity(matchcmd, argv)) 385 return -2; 386 387 /* check the number of argument */ 388 if (matchcmd->nargs < 0 && matchcmd->nargs < -*nargs_ ){ 389 fprintf(stderr, "ERROR: '%s' requires minimum %d arg(s)\n", 390 matchcmd->verb, -matchcmd->nargs); 391 return -2; 392 } 393 if(matchcmd->nargs >= 0 && matchcmd->nargs != *nargs_ && matchcmd->nargs != 999){ 394 fprintf(stderr, "ERROR: '%s' requires %d arg(s)\n", 395 matchcmd->verb, matchcmd->nargs); 396 return -2; 397 } 398 399 if (prepare_args( nargs_, args_, prgname, matchcmd )){ 400 fprintf(stderr, "ERROR: not enough memory\\n"); 401 return -20; 402 } 403 404 405 return 1; 406} 407int main(int ac, char **av ) 408{ 409 char *cmd=0, **args=0; 410 int nargs=0, r; 411 CommandFunction func=0; 412 413 r = parse_args(ac, av, &func, &nargs, &cmd, &args); 414 if( r <= 0 ){ 415 /* error or no command to parse*/ 416 exit(-r); 417 } 418 419 exit(func(nargs, args)); 420} 421 422