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