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