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