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