Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | d6d592f | 2014-04-10 20:01:29 -0600 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2000 |
| 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 5 | * |
| 6 | * Add to readline cmdline-editing by |
| 7 | * (C) Copyright 2005 |
| 8 | * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com> |
Simon Glass | d6d592f | 2014-04-10 20:01:29 -0600 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <cli.h> |
| 13 | #include <cli_hush.h> |
Simon Glass | adaaa48 | 2019-11-14 12:57:43 -0700 | [diff] [blame] | 14 | #include <command.h> |
Simon Glass | a73bda4 | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 15 | #include <console.h> |
Simon Glass | 0af6e2d | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 16 | #include <env.h> |
Simon Glass | 5b47e30 | 2014-04-10 20:01:35 -0600 | [diff] [blame] | 17 | #include <fdtdec.h> |
Simon Glass | f11478f | 2019-12-28 10:45:07 -0700 | [diff] [blame] | 18 | #include <hang.h> |
Simon Glass | d6d592f | 2014-04-10 20:01:29 -0600 | [diff] [blame] | 19 | #include <malloc.h> |
| 20 | |
Simon Glass | 5b47e30 | 2014-04-10 20:01:35 -0600 | [diff] [blame] | 21 | DECLARE_GLOBAL_DATA_PTR; |
| 22 | |
Simon Glass | 4ef90b7 | 2016-03-19 02:18:38 -0600 | [diff] [blame] | 23 | #ifdef CONFIG_CMDLINE |
Simon Glass | d6d592f | 2014-04-10 20:01:29 -0600 | [diff] [blame] | 24 | /* |
| 25 | * Run a command using the selected parser. |
| 26 | * |
| 27 | * @param cmd Command to run |
| 28 | * @param flag Execution flags (CMD_FLAG_...) |
| 29 | * @return 0 on success, or != 0 on error. |
| 30 | */ |
| 31 | int run_command(const char *cmd, int flag) |
| 32 | { |
Andrew F. Davis | 9b0fbdc | 2019-01-17 13:43:04 -0600 | [diff] [blame] | 33 | #if !CONFIG_IS_ENABLED(HUSH_PARSER) |
Simon Glass | d6d592f | 2014-04-10 20:01:29 -0600 | [diff] [blame] | 34 | /* |
| 35 | * cli_run_command can return 0 or 1 for success, so clean up |
| 36 | * its result. |
| 37 | */ |
| 38 | if (cli_simple_run_command(cmd, flag) == -1) |
| 39 | return 1; |
| 40 | |
| 41 | return 0; |
| 42 | #else |
Simon Glass | 94cb9c2 | 2014-10-07 13:59:43 -0600 | [diff] [blame] | 43 | int hush_flags = FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP; |
| 44 | |
| 45 | if (flag & CMD_FLAG_ENV) |
| 46 | hush_flags |= FLAG_CONT_ON_NEWLINE; |
| 47 | return parse_string_outer(cmd, hush_flags); |
Simon Glass | d6d592f | 2014-04-10 20:01:29 -0600 | [diff] [blame] | 48 | #endif |
| 49 | } |
| 50 | |
Thomas Betker | be6346b | 2014-06-05 20:07:57 +0200 | [diff] [blame] | 51 | /* |
| 52 | * Run a command using the selected parser, and check if it is repeatable. |
| 53 | * |
| 54 | * @param cmd Command to run |
| 55 | * @param flag Execution flags (CMD_FLAG_...) |
| 56 | * @return 0 (not repeatable) or 1 (repeatable) on success, -1 on error. |
| 57 | */ |
| 58 | int run_command_repeatable(const char *cmd, int flag) |
| 59 | { |
Masahiro Yamada | 17a48e4 | 2016-06-21 02:11:19 +0900 | [diff] [blame] | 60 | #ifndef CONFIG_HUSH_PARSER |
Thomas Betker | be6346b | 2014-06-05 20:07:57 +0200 | [diff] [blame] | 61 | return cli_simple_run_command(cmd, flag); |
| 62 | #else |
| 63 | /* |
| 64 | * parse_string_outer() returns 1 for failure, so clean up |
| 65 | * its result. |
| 66 | */ |
| 67 | if (parse_string_outer(cmd, |
| 68 | FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP)) |
| 69 | return -1; |
| 70 | |
| 71 | return 0; |
| 72 | #endif |
| 73 | } |
Sean Anderson | 41f0a66 | 2020-01-10 12:32:19 -0500 | [diff] [blame^] | 74 | #else |
| 75 | __weak int board_run_command(const char *cmdline) |
| 76 | { |
| 77 | printf("## Commands are disabled. Please enable CONFIG_CMDLINE.\n"); |
| 78 | |
| 79 | return 1; |
| 80 | } |
Simon Glass | 4ef90b7 | 2016-03-19 02:18:38 -0600 | [diff] [blame] | 81 | #endif /* CONFIG_CMDLINE */ |
Thomas Betker | be6346b | 2014-06-05 20:07:57 +0200 | [diff] [blame] | 82 | |
Simon Glass | d6d592f | 2014-04-10 20:01:29 -0600 | [diff] [blame] | 83 | int run_command_list(const char *cmd, int len, int flag) |
| 84 | { |
| 85 | int need_buff = 1; |
| 86 | char *buff = (char *)cmd; /* cast away const */ |
| 87 | int rcode = 0; |
| 88 | |
| 89 | if (len == -1) { |
| 90 | len = strlen(cmd); |
Masahiro Yamada | 17a48e4 | 2016-06-21 02:11:19 +0900 | [diff] [blame] | 91 | #ifdef CONFIG_HUSH_PARSER |
Simon Glass | d6d592f | 2014-04-10 20:01:29 -0600 | [diff] [blame] | 92 | /* hush will never change our string */ |
| 93 | need_buff = 0; |
| 94 | #else |
| 95 | /* the built-in parser will change our string if it sees \n */ |
| 96 | need_buff = strchr(cmd, '\n') != NULL; |
| 97 | #endif |
| 98 | } |
| 99 | if (need_buff) { |
| 100 | buff = malloc(len + 1); |
| 101 | if (!buff) |
| 102 | return 1; |
| 103 | memcpy(buff, cmd, len); |
| 104 | buff[len] = '\0'; |
| 105 | } |
Masahiro Yamada | 17a48e4 | 2016-06-21 02:11:19 +0900 | [diff] [blame] | 106 | #ifdef CONFIG_HUSH_PARSER |
Simon Glass | d6d592f | 2014-04-10 20:01:29 -0600 | [diff] [blame] | 107 | rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON); |
| 108 | #else |
| 109 | /* |
| 110 | * This function will overwrite any \n it sees with a \0, which |
| 111 | * is why it can't work with a const char *. Here we are making |
| 112 | * using of internal knowledge of this function, to avoid always |
| 113 | * doing a malloc() which is actually required only in a case that |
| 114 | * is pretty rare. |
| 115 | */ |
Simon Glass | 4ef90b7 | 2016-03-19 02:18:38 -0600 | [diff] [blame] | 116 | #ifdef CONFIG_CMDLINE |
Simon Glass | d6d592f | 2014-04-10 20:01:29 -0600 | [diff] [blame] | 117 | rcode = cli_simple_run_command_list(buff, flag); |
Simon Glass | 4ef90b7 | 2016-03-19 02:18:38 -0600 | [diff] [blame] | 118 | #else |
| 119 | rcode = board_run_command(buff); |
| 120 | #endif |
Peng Fan | 6322551 | 2015-12-22 17:14:13 +0800 | [diff] [blame] | 121 | #endif |
Simon Glass | d6d592f | 2014-04-10 20:01:29 -0600 | [diff] [blame] | 122 | if (need_buff) |
| 123 | free(buff); |
Simon Glass | d6d592f | 2014-04-10 20:01:29 -0600 | [diff] [blame] | 124 | |
| 125 | return rcode; |
| 126 | } |
| 127 | |
| 128 | /****************************************************************************/ |
| 129 | |
| 130 | #if defined(CONFIG_CMD_RUN) |
| 131 | int do_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 132 | { |
| 133 | int i; |
| 134 | |
| 135 | if (argc < 2) |
| 136 | return CMD_RET_USAGE; |
| 137 | |
| 138 | for (i = 1; i < argc; ++i) { |
| 139 | char *arg; |
| 140 | |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 141 | arg = env_get(argv[i]); |
Simon Glass | d6d592f | 2014-04-10 20:01:29 -0600 | [diff] [blame] | 142 | if (arg == NULL) { |
| 143 | printf("## Error: \"%s\" not defined\n", argv[i]); |
| 144 | return 1; |
| 145 | } |
| 146 | |
Simon Glass | 94cb9c2 | 2014-10-07 13:59:43 -0600 | [diff] [blame] | 147 | if (run_command(arg, flag | CMD_FLAG_ENV) != 0) |
Simon Glass | d6d592f | 2014-04-10 20:01:29 -0600 | [diff] [blame] | 148 | return 1; |
| 149 | } |
| 150 | return 0; |
| 151 | } |
| 152 | #endif |
Simon Glass | 33f7913 | 2014-04-10 20:01:34 -0600 | [diff] [blame] | 153 | |
Masahiro Yamada | 366b24f | 2015-08-12 07:31:55 +0900 | [diff] [blame] | 154 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Simon Glass | 5b47e30 | 2014-04-10 20:01:35 -0600 | [diff] [blame] | 155 | bool cli_process_fdt(const char **cmdp) |
| 156 | { |
| 157 | /* Allow the fdt to override the boot command */ |
| 158 | char *env = fdtdec_get_config_string(gd->fdt_blob, "bootcmd"); |
| 159 | if (env) |
| 160 | *cmdp = env; |
| 161 | /* |
| 162 | * If the bootsecure option was chosen, use secure_boot_cmd(). |
| 163 | * Always use 'env' in this case, since bootsecure requres that the |
| 164 | * bootcmd was specified in the FDT too. |
| 165 | */ |
| 166 | return fdtdec_get_config_int(gd->fdt_blob, "bootsecure", 0) != 0; |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * Runs the given boot command securely. Specifically: |
| 171 | * - Doesn't run the command with the shell (run_command or parse_string_outer), |
| 172 | * since that's a lot of code surface that an attacker might exploit. |
| 173 | * Because of this, we don't do any argument parsing--the secure boot command |
| 174 | * has to be a full-fledged u-boot command. |
| 175 | * - Doesn't check for keypresses before booting, since that could be a |
| 176 | * security hole; also disables Ctrl-C. |
| 177 | * - Doesn't allow the command to return. |
| 178 | * |
| 179 | * Upon any failures, this function will drop into an infinite loop after |
| 180 | * printing the error message to console. |
| 181 | */ |
| 182 | void cli_secure_boot_cmd(const char *cmd) |
| 183 | { |
Simon Glass | 4ef90b7 | 2016-03-19 02:18:38 -0600 | [diff] [blame] | 184 | #ifdef CONFIG_CMDLINE |
Simon Glass | 5b47e30 | 2014-04-10 20:01:35 -0600 | [diff] [blame] | 185 | cmd_tbl_t *cmdtp; |
Simon Glass | 4ef90b7 | 2016-03-19 02:18:38 -0600 | [diff] [blame] | 186 | #endif |
Simon Glass | 5b47e30 | 2014-04-10 20:01:35 -0600 | [diff] [blame] | 187 | int rc; |
| 188 | |
| 189 | if (!cmd) { |
| 190 | printf("## Error: Secure boot command not specified\n"); |
| 191 | goto err; |
| 192 | } |
| 193 | |
| 194 | /* Disable Ctrl-C just in case some command is used that checks it. */ |
| 195 | disable_ctrlc(1); |
| 196 | |
| 197 | /* Find the command directly. */ |
Simon Glass | 4ef90b7 | 2016-03-19 02:18:38 -0600 | [diff] [blame] | 198 | #ifdef CONFIG_CMDLINE |
Simon Glass | 5b47e30 | 2014-04-10 20:01:35 -0600 | [diff] [blame] | 199 | cmdtp = find_cmd(cmd); |
| 200 | if (!cmdtp) { |
| 201 | printf("## Error: \"%s\" not defined\n", cmd); |
| 202 | goto err; |
| 203 | } |
| 204 | |
| 205 | /* Run the command, forcing no flags and faking argc and argv. */ |
| 206 | rc = (cmdtp->cmd)(cmdtp, 0, 1, (char **)&cmd); |
| 207 | |
Simon Glass | 4ef90b7 | 2016-03-19 02:18:38 -0600 | [diff] [blame] | 208 | #else |
| 209 | rc = board_run_command(cmd); |
| 210 | #endif |
| 211 | |
Simon Glass | 5b47e30 | 2014-04-10 20:01:35 -0600 | [diff] [blame] | 212 | /* Shouldn't ever return from boot command. */ |
| 213 | printf("## Error: \"%s\" returned (code %d)\n", cmd, rc); |
| 214 | |
| 215 | err: |
| 216 | /* |
| 217 | * Not a whole lot to do here. Rebooting won't help much, since we'll |
| 218 | * just end up right back here. Just loop. |
| 219 | */ |
| 220 | hang(); |
| 221 | } |
Masahiro Yamada | 366b24f | 2015-08-12 07:31:55 +0900 | [diff] [blame] | 222 | #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */ |
Simon Glass | 5b47e30 | 2014-04-10 20:01:35 -0600 | [diff] [blame] | 223 | |
Simon Glass | 33f7913 | 2014-04-10 20:01:34 -0600 | [diff] [blame] | 224 | void cli_loop(void) |
| 225 | { |
Heiko Schocher | 7df72c3 | 2019-04-12 12:37:03 +0200 | [diff] [blame] | 226 | bootstage_mark(BOOTSTAGE_ID_ENTER_CLI_LOOP); |
Masahiro Yamada | 17a48e4 | 2016-06-21 02:11:19 +0900 | [diff] [blame] | 227 | #ifdef CONFIG_HUSH_PARSER |
Simon Glass | 33f7913 | 2014-04-10 20:01:34 -0600 | [diff] [blame] | 228 | parse_file_outer(); |
| 229 | /* This point is never reached */ |
| 230 | for (;;); |
Stefan Roese | 824300a | 2016-04-04 16:32:15 +0200 | [diff] [blame] | 231 | #elif defined(CONFIG_CMDLINE) |
Simon Glass | 33f7913 | 2014-04-10 20:01:34 -0600 | [diff] [blame] | 232 | cli_simple_loop(); |
Simon Glass | 4ef90b7 | 2016-03-19 02:18:38 -0600 | [diff] [blame] | 233 | #else |
| 234 | printf("## U-Boot command line is disabled. Please enable CONFIG_CMDLINE\n"); |
Masahiro Yamada | 17a48e4 | 2016-06-21 02:11:19 +0900 | [diff] [blame] | 235 | #endif /*CONFIG_HUSH_PARSER*/ |
Simon Glass | 33f7913 | 2014-04-10 20:01:34 -0600 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | void cli_init(void) |
| 239 | { |
Masahiro Yamada | 17a48e4 | 2016-06-21 02:11:19 +0900 | [diff] [blame] | 240 | #ifdef CONFIG_HUSH_PARSER |
Simon Glass | 33f7913 | 2014-04-10 20:01:34 -0600 | [diff] [blame] | 241 | u_boot_hush_start(); |
| 242 | #endif |
| 243 | |
| 244 | #if defined(CONFIG_HUSH_INIT_VAR) |
| 245 | hush_init_var(); |
| 246 | #endif |
| 247 | } |