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