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