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