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 */ |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 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 */ |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 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 | }, |
Nick Sanders | 9d57aa7 | 2014-03-05 21:38:54 -0800 | [diff] [blame] | 58 | { do_dump_extcsd, -1, |
| 59 | "extcsd dump", "<device>\n" |
| 60 | "Print raw extcsd data from <device>.", |
| 61 | NULL |
| 62 | }, |
Chris Ball | b9c7a17 | 2012-02-20 12:34:25 -0500 | [diff] [blame] | 63 | { 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 Ball | 6599780 | 2012-09-21 18:19:25 +0800 | [diff] [blame] | 70 | "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] | 71 | NULL |
| 72 | }, |
Saugata Das | b7e2599 | 2012-05-17 09:26:34 -0400 | [diff] [blame] | 73 | { do_disable_512B_emulation, -1, |
| 74 | "disable 512B emulation", "<device>\n" |
Chris Ball | 6599780 | 2012-09-21 18:19:25 +0800 | [diff] [blame] | 75 | "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] | 76 | NULL |
| 77 | }, |
Balaji T K | d78ce08 | 2015-04-29 18:12:33 -0400 | [diff] [blame] | 78 | { 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 Gardiner | d91d369 | 2013-05-30 17:12:51 -0400 | [diff] [blame] | 83 | { 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 Gardiner | 196d0d2 | 2013-09-19 11:14:29 -0400 | [diff] [blame] | 88 | { 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 Gardiner | 27c357d | 2013-05-30 17:12:47 -0400 | [diff] [blame] | 93 | { do_status_get, -1, |
| 94 | "status get", "<device>\n" |
| 95 | "Print the response to STATUS_SEND (CMD13).", |
| 96 | NULL |
| 97 | }, |
Giuseppe CAVALLARO | 7bd1320 | 2012-04-19 10:58:37 +0200 | [diff] [blame] | 98 | { 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 | }, |
Al Cooper | 794314c | 2015-05-01 08:24:37 -0400 | [diff] [blame] | 103 | { do_boot_bus_conditions_set, -4, |
| 104 | "bootbus set", "<boot_mode> " "<reset_boot_bus_conditions> " "<boot_bus_width> " "<device>\n" |
| 105 | "Set Boot Bus Conditions.\n" |
| 106 | "<boot_mode> must be \"single_backward|single_hs|dual\"\n" |
| 107 | "<reset_boot_bus_conditions> must be \"x1|retain\"\n" |
| 108 | "<boot_bus_width> must be \"x1|x4|x8\"", |
| 109 | NULL |
| 110 | }, |
Jaehoon Chung | 8649651 | 2012-09-21 10:08:05 +0000 | [diff] [blame] | 111 | { do_write_bkops_en, -1, |
| 112 | "bkops enable", "<device>\n" |
| 113 | "Enable the eMMC BKOPS feature on <device>.\nNOTE! This is a one-time programmable (unreversible) change.", |
| 114 | NULL |
| 115 | }, |
Chris Ball | f74dfe2 | 2012-10-19 16:49:55 -0400 | [diff] [blame] | 116 | { do_hwreset_en, -1, |
| 117 | "hwreset enable", "<device>\n" |
| 118 | "Permanently enable the eMMC H/W Reset feature on <device>.\nNOTE! This is a one-time programmable (unreversible) change.", |
| 119 | NULL |
| 120 | }, |
| 121 | { do_hwreset_dis, -1, |
| 122 | "hwreset disable", "<device>\n" |
| 123 | "Permanently disable the eMMC H/W Reset feature on <device>.\nNOTE! This is a one-time programmable (unreversible) change.", |
| 124 | NULL |
| 125 | }, |
Yaniv Gardi | 21bb473 | 2013-05-26 13:25:33 -0400 | [diff] [blame] | 126 | { do_sanitize, -1, |
| 127 | "sanitize", "<device>\n" |
| 128 | "Send Sanitize command to the <device>.\nThis will delete the unmapped memory region of the device.", |
| 129 | NULL |
| 130 | }, |
Gwendal Grignou | 771984c | 2014-07-01 12:46:18 -0700 | [diff] [blame] | 131 | { do_emmc50_ffu, -2, |
Gwendal Grignou | 0da2c51 | 2015-01-08 15:36:03 -0800 | [diff] [blame] | 132 | "ffu", "[-k hack_type[:hack_value]] <image name> <device>\n" |
| 133 | "run eMMC 5.0 Field firmware update.\n" |
| 134 | "Device specific hacks can be specificied.", |
Gwendal Grignou | 771984c | 2014-07-01 12:46:18 -0700 | [diff] [blame] | 135 | NULL |
| 136 | }, |
Roman Peniaev | 023cc7c | 2014-08-12 23:25:45 +0900 | [diff] [blame] | 137 | { do_rpmb_write_key, -1, |
| 138 | "rpmb write-key", "<rpmb device> <key file>\n" |
Chris Ball | d186ab5 | 2014-08-12 10:44:52 -0400 | [diff] [blame] | 139 | "Program authentication key which is 32 bytes length and stored\n" |
| 140 | "in the specified file. Also you can specify '-' instead of\n" |
| 141 | "key file path to read the key from stdin.\n" |
| 142 | "NOTE! This is a one-time programmable (unreversible) change.\n" |
Roman Peniaev | 023cc7c | 2014-08-12 23:25:45 +0900 | [diff] [blame] | 143 | "Example:\n" |
Chris Ball | d186ab5 | 2014-08-12 10:44:52 -0400 | [diff] [blame] | 144 | " $ echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH | \\\n" |
| 145 | " mmc rpmb write-key /dev/mmcblk0rpmb -", |
Roman Peniaev | 023cc7c | 2014-08-12 23:25:45 +0900 | [diff] [blame] | 146 | NULL |
| 147 | }, |
| 148 | { do_rpmb_read_counter, -1, |
| 149 | "rpmb read-counter", "<rpmb device>\n" |
| 150 | "Counter value for the <rpmb device> will be read to stdout.", |
| 151 | NULL |
| 152 | }, |
| 153 | { do_rpmb_read_block, -1, |
| 154 | "rpmb read-block", "<rpmb device> <address> <blocks count> <output file> [key file]\n" |
Chris Ball | d186ab5 | 2014-08-12 10:44:52 -0400 | [diff] [blame] | 155 | "Blocks of 256 bytes will be read from <rpmb device> to output\n" |
| 156 | "file or stdout if '-' is specified. If key is specified - read\n" |
| 157 | "data will be verified. Instead of regular path you can specify\n" |
| 158 | "'-' to read key from stdin.\n" |
Roman Peniaev | 023cc7c | 2014-08-12 23:25:45 +0900 | [diff] [blame] | 159 | "Example:\n" |
Chris Ball | d186ab5 | 2014-08-12 10:44:52 -0400 | [diff] [blame] | 160 | " $ echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH | \\\n" |
| 161 | " mmc rpmb read-block /dev/mmcblk0rpmb 0x02 2 /tmp/block -\n" |
Roman Peniaev | 023cc7c | 2014-08-12 23:25:45 +0900 | [diff] [blame] | 162 | "or read two blocks without verification\n" |
| 163 | " $ mmc rpmb read-block /dev/mmcblk0rpmb 0x02 2 /tmp/block", |
| 164 | NULL |
| 165 | }, |
| 166 | { do_rpmb_write_block, -1, |
| 167 | "rpmb write-block", "<rpmb device> <address> <256 byte data file> <key file>\n" |
Chris Ball | d186ab5 | 2014-08-12 10:44:52 -0400 | [diff] [blame] | 168 | "Block of 256 bytes will be written from data file to\n" |
| 169 | "<rpmb device>. Also you can specify '-' instead of key\n" |
| 170 | "file path or data file to read the data from stdin.\n" |
Roman Peniaev | 023cc7c | 2014-08-12 23:25:45 +0900 | [diff] [blame] | 171 | "Example:\n" |
Chris Ball | d186ab5 | 2014-08-12 10:44:52 -0400 | [diff] [blame] | 172 | " $ (awk 'BEGIN {while (c++<256) printf \"a\"}' | \\\n" |
| 173 | " echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH) | \\\n" |
Roman Peniaev | 023cc7c | 2014-08-12 23:25:45 +0900 | [diff] [blame] | 174 | " mmc rpmb write-block /dev/mmcblk0rpmb 0x02 - -", |
| 175 | NULL |
| 176 | }, |
Al Cooper | 786418c | 2015-04-29 18:12:35 -0400 | [diff] [blame] | 177 | { do_cache_en, -1, |
| 178 | "cache enable", "<device>\n" |
| 179 | "Enable the eMMC cache feature on <device>.\n" |
| 180 | "NOTE! The cache is an optional feature on devices >= eMMC4.5.", |
| 181 | NULL |
| 182 | }, |
| 183 | { do_cache_dis, -1, |
| 184 | "cache disable", "<device>\n" |
| 185 | "Disable the eMMC cache feature on <device>.\n" |
| 186 | "NOTE! The cache is an optional feature on devices >= eMMC4.5.", |
| 187 | NULL |
| 188 | }, |
Sebastian Rasmussen | 7e00a5a | 2016-02-23 13:37:28 +0800 | [diff] [blame] | 189 | { do_read_csd, -1, |
| 190 | "csd read", "<device path>\n" |
| 191 | "Print CSD data from <device path>.\n" |
| 192 | "The device path should specify the csd file directory.", |
| 193 | NULL |
| 194 | }, |
| 195 | { do_read_cid, -1, |
| 196 | "cid read", "<device path>\n" |
| 197 | "Print CID data from <device path>.\n" |
| 198 | "The device path should specify the cid file directory.", |
| 199 | NULL |
| 200 | }, |
| 201 | { do_read_scr, -1, |
| 202 | "scr read", "<device path>\n" |
| 203 | "Print SCR data from <device path>.\n" |
| 204 | "The device path should specify the scr file directory.", |
| 205 | NULL |
| 206 | }, |
Julius Werner | bcc3e2e | 2016-04-21 16:53:02 -0700 | [diff] [blame^] | 207 | { do_blockprotect_enable, -2, "blockprotect enable", |
| 208 | "[-p|-r] <device> <sector>\n" |
| 209 | "Enable block protection for a given sector. Will write protect all\n" |
| 210 | "sectors within the target sector's write protect block. Write protect\n" |
| 211 | "block size is a multiple of erase block size and device dependent.\n" |
| 212 | "Run mmc blockprotect info to query write protect block size.\n" |
| 213 | " -p Protect block permanently. WARNING: THIS IS IRREVERSIBLE!\n" |
| 214 | " -r Protect block until next power-on.", |
| 215 | NULL |
| 216 | }, |
| 217 | { do_blockprotect_disable, -2, "blockprotect disable", |
| 218 | "<device> <sector>\n" |
| 219 | "Disable block protection for a given sector. Will disable protection\n" |
| 220 | "for all sectors within the target sector's write protect block.\n" |
| 221 | "Cannot disable permanent or power-on write protection.", |
| 222 | NULL |
| 223 | }, |
| 224 | { do_blockprotect_read, -2, "blockprotect read", "<device> <sector>\n" |
| 225 | "Query the current block protection status of a given sector.", |
| 226 | NULL |
| 227 | }, |
| 228 | { do_blockprotect_info, -1, "blockprotect info", "<device>\n" |
| 229 | "Query information about device's block protect capabilities.", |
| 230 | NULL |
| 231 | }, |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 232 | { 0, 0, 0, 0 } |
| 233 | }; |
| 234 | |
| 235 | static char *get_prgname(char *programname) |
| 236 | { |
| 237 | char *np; |
| 238 | np = strrchr(programname,'/'); |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 239 | if (!np) |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 240 | np = programname; |
| 241 | else |
| 242 | np++; |
| 243 | |
| 244 | return np; |
| 245 | } |
| 246 | |
| 247 | static void print_help(char *programname, struct Command *cmd, int helptype) |
| 248 | { |
| 249 | char *pc; |
| 250 | |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 251 | printf("\t%s %s ", programname, cmd->verb); |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 252 | |
| 253 | if (helptype == ADVANCED_HELP && cmd->adv_help) |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 254 | for (pc = cmd->adv_help; *pc; pc++) { |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 255 | putchar(*pc); |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 256 | if (*pc == '\n') |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 257 | printf("\t\t"); |
| 258 | } |
| 259 | else |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 260 | for (pc = cmd->help; *pc; pc++) { |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 261 | putchar(*pc); |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 262 | if (*pc == '\n') |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 263 | printf("\t\t"); |
| 264 | } |
| 265 | |
| 266 | putchar('\n'); |
| 267 | } |
| 268 | |
| 269 | static void help(char *np) |
| 270 | { |
| 271 | struct Command *cp; |
| 272 | |
| 273 | printf("Usage:\n"); |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 274 | for (cp = commands; cp->verb; cp++) |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 275 | print_help(np, cp, BASIC_HELP); |
| 276 | |
| 277 | printf("\n\t%s help|--help|-h\n\t\tShow the help.\n",np); |
| 278 | printf("\n\t%s <cmd> --help\n\t\tShow detailed help for a command or subset of commands.\n",np); |
| 279 | printf("\n%s\n", MMC_VERSION); |
| 280 | } |
| 281 | |
| 282 | static int split_command(char *cmd, char ***commands) |
| 283 | { |
| 284 | int c, l; |
| 285 | char *p, *s; |
| 286 | |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 287 | for (*commands = 0, l = c = 0, p = s = cmd ; ; p++, l++) { |
| 288 | if (*p && *p != ' ') |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 289 | continue; |
| 290 | |
| 291 | /* c + 2 so that we have room for the null */ |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 292 | (*commands) = realloc((*commands), sizeof(char *)*(c + 2)); |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 293 | (*commands)[c] = strndup(s, l); |
| 294 | c++; |
| 295 | l = 0; |
| 296 | s = p+1; |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 297 | if (!*p) break; |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | (*commands)[c] = 0; |
| 301 | return c; |
| 302 | } |
| 303 | |
| 304 | /* |
| 305 | This function checks if the passed command is ambiguous |
| 306 | */ |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 307 | static int check_ambiguity(struct Command *cmd, char **argv) { |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 308 | int i; |
| 309 | struct Command *cp; |
| 310 | /* check for ambiguity */ |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 311 | for (i = 0 ; i < cmd->ncmds ; i++) { |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 312 | int match; |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 313 | for (match = 0, cp = commands; cp->verb; cp++) { |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 314 | int j, skip; |
| 315 | char *s1, *s2; |
| 316 | |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 317 | if (cp->ncmds < i) |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 318 | continue; |
| 319 | |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 320 | for (skip = 0, j = 0 ; j < i ; j++) |
| 321 | if (strcmp(cmd->cmds[j], cp->cmds[j])) { |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 322 | skip=1; |
| 323 | break; |
| 324 | } |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 325 | if (skip) |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 326 | continue; |
| 327 | |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 328 | if (!strcmp(cmd->cmds[i], cp->cmds[i])) |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 329 | continue; |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 330 | for (s2 = cp->cmds[i], s1 = argv[i+1]; |
| 331 | *s1 == *s2 && *s1; s1++, s2++) ; |
| 332 | if (!*s1) |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 333 | match++; |
| 334 | } |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 335 | if (match) { |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 336 | int j; |
| 337 | fprintf(stderr, "ERROR: in command '"); |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 338 | for (j = 0 ; j <= i ; j++) |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 339 | fprintf(stderr, "%s%s",j?" ":"", argv[j+1]); |
| 340 | fprintf(stderr, "', '%s' is ambiguous\n",argv[j]); |
| 341 | return -2; |
| 342 | } |
| 343 | } |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | /* |
| 348 | * This function, compacts the program name and the command in the first |
| 349 | * element of the '*av' array |
| 350 | */ |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 351 | static int prepare_args(int *ac, char ***av, char *prgname, |
| 352 | struct Command *cmd) { |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 353 | |
| 354 | char **ret; |
| 355 | int i; |
| 356 | char *newname; |
| 357 | |
| 358 | ret = (char **)malloc(sizeof(char*)*(*ac+1)); |
| 359 | newname = (char*)malloc(strlen(prgname)+strlen(cmd->verb)+2); |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 360 | if (!ret || !newname) { |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 361 | free(ret); |
| 362 | free(newname); |
| 363 | return -1; |
| 364 | } |
| 365 | |
| 366 | ret[0] = newname; |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 367 | for (i=0; i < *ac ; i++) |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 368 | ret[i+1] = (*av)[i]; |
| 369 | |
| 370 | strcpy(newname, prgname); |
| 371 | strcat(newname, " "); |
| 372 | strcat(newname, cmd->verb); |
| 373 | |
| 374 | (*ac)++; |
| 375 | *av = ret; |
| 376 | |
| 377 | return 0; |
| 378 | |
| 379 | } |
| 380 | |
| 381 | /* |
| 382 | This function performs the following jobs: |
| 383 | - show the help if '--help' or 'help' or '-h' are passed |
| 384 | - verify that a command is not ambiguous, otherwise show which |
| 385 | part of the command is ambiguous |
| 386 | - if after a (even partial) command there is '--help' show detailed help |
| 387 | for all the matching commands |
| 388 | - if the command doesn't match show an error |
| 389 | - finally, if a command matches, they return which command matched and |
| 390 | the arguments |
| 391 | |
| 392 | The function return 0 in case of help is requested; <0 in case |
| 393 | of uncorrect command; >0 in case of matching commands |
| 394 | argc, argv are the arg-counter and arg-vector (input) |
| 395 | *nargs_ is the number of the arguments after the command (output) |
| 396 | **cmd_ is the invoked command (output) |
| 397 | ***args_ are the arguments after the command |
| 398 | |
| 399 | */ |
| 400 | static int parse_args(int argc, char **argv, |
| 401 | CommandFunction *func_, |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 402 | int *nargs_, char **cmd_, char ***args_) |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 403 | { |
| 404 | struct Command *cp; |
| 405 | struct Command *matchcmd=0; |
| 406 | char *prgname = get_prgname(argv[0]); |
| 407 | int i=0, helprequested=0; |
| 408 | |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 409 | if (argc < 2 || !strcmp(argv[1], "help") || |
| 410 | !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) { |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 411 | help(prgname); |
| 412 | return 0; |
| 413 | } |
| 414 | |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 415 | for (cp = commands; cp->verb; cp++) |
| 416 | if (!cp->ncmds) |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 417 | cp->ncmds = split_command(cp->verb, &(cp->cmds)); |
| 418 | |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 419 | for (cp = commands; cp->verb; cp++) { |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 420 | int match; |
| 421 | |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 422 | if (argc-1 < cp->ncmds) |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 423 | continue; |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 424 | for (match = 1, i = 0 ; i < cp->ncmds ; i++) { |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 425 | char *s1, *s2; |
| 426 | s1 = cp->cmds[i]; |
| 427 | s2 = argv[i+1]; |
| 428 | |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 429 | for (s2 = cp->cmds[i], s1 = argv[i+1]; |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 430 | *s1 == *s2 && *s1; |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 431 | s1++, s2++) ; |
| 432 | if (*s1) { |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 433 | match=0; |
| 434 | break; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | /* If you understand why this code works ... |
| 439 | you are a genious !! */ |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 440 | if (argc>i+1 && !strcmp(argv[i+1],"--help")) { |
| 441 | if (!helprequested) |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 442 | printf("Usage:\n"); |
| 443 | print_help(prgname, cp, ADVANCED_HELP); |
| 444 | helprequested=1; |
| 445 | continue; |
| 446 | } |
| 447 | |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 448 | if (!match) |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 449 | continue; |
| 450 | |
| 451 | matchcmd = cp; |
| 452 | *nargs_ = argc-matchcmd->ncmds-1; |
| 453 | *cmd_ = matchcmd->verb; |
| 454 | *args_ = argv+matchcmd->ncmds+1; |
| 455 | *func_ = cp->func; |
| 456 | |
| 457 | break; |
| 458 | } |
| 459 | |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 460 | if (helprequested) { |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 461 | printf("\n%s\n", MMC_VERSION); |
| 462 | return 0; |
| 463 | } |
| 464 | |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 465 | if (!matchcmd) { |
| 466 | fprintf(stderr, "ERROR: unknown command '%s'\n",argv[1]); |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 467 | help(prgname); |
| 468 | return -1; |
| 469 | } |
| 470 | |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 471 | if (check_ambiguity(matchcmd, argv)) |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 472 | return -2; |
| 473 | |
| 474 | /* check the number of argument */ |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 475 | if (matchcmd->nargs < 0 && matchcmd->nargs < -*nargs_) { |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 476 | fprintf(stderr, "ERROR: '%s' requires minimum %d arg(s)\n", |
| 477 | matchcmd->verb, -matchcmd->nargs); |
| 478 | return -2; |
| 479 | } |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 480 | if (matchcmd->nargs >= 0 && matchcmd->nargs != *nargs_ && matchcmd->nargs != 999) { |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 481 | fprintf(stderr, "ERROR: '%s' requires %d arg(s)\n", |
| 482 | matchcmd->verb, matchcmd->nargs); |
| 483 | return -2; |
| 484 | } |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 485 | if (prepare_args(nargs_, args_, prgname, matchcmd)) { |
| 486 | fprintf(stderr, "ERROR: not enough memory\\n"); |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 487 | return -20; |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 488 | } |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 489 | |
| 490 | |
| 491 | return 1; |
| 492 | } |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 493 | int main(int ac, char **av) |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 494 | { |
| 495 | char *cmd=0, **args=0; |
| 496 | int nargs=0, r; |
| 497 | CommandFunction func=0; |
| 498 | |
| 499 | r = parse_args(ac, av, &func, &nargs, &cmd, &args); |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 500 | if (r <= 0) { |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 501 | /* error or no command to parse*/ |
| 502 | exit(-r); |
| 503 | } |
| 504 | |
| 505 | exit(func(nargs, args)); |
| 506 | } |
| 507 | |