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