Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Simon Glass | 18d6653 | 2014-04-10 20:01:25 -0600 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2014 Google, Inc |
| 4 | * Simon Glass <sjg@chromium.org> |
Simon Glass | 18d6653 | 2014-04-10 20:01:25 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #ifndef __CLI_H |
| 8 | #define __CLI_H |
| 9 | |
| 10 | /** |
| 11 | * Go into the command loop |
| 12 | * |
| 13 | * This will return if we get a timeout waiting for a command. See |
| 14 | * CONFIG_BOOT_RETRY_TIME. |
| 15 | */ |
Simon Glass | c1bb2cd | 2014-04-10 20:01:34 -0600 | [diff] [blame] | 16 | void cli_simple_loop(void); |
Simon Glass | 18d6653 | 2014-04-10 20:01:25 -0600 | [diff] [blame] | 17 | |
| 18 | /** |
| 19 | * cli_simple_run_command() - Execute a command with the simple CLI |
| 20 | * |
| 21 | * @cmd: String containing the command to execute |
| 22 | * @flag Flag value - see CMD_FLAG_... |
| 23 | * @return 1 - command executed, repeatable |
| 24 | * 0 - command executed but not repeatable, interrupted commands are |
| 25 | * always considered not repeatable |
| 26 | * -1 - not executed (unrecognized, bootd recursion or too many args) |
| 27 | * (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is |
| 28 | * considered unrecognized) |
| 29 | */ |
| 30 | int cli_simple_run_command(const char *cmd, int flag); |
| 31 | |
| 32 | /** |
Hans de Goede | a06be2d | 2014-08-06 09:37:38 +0200 | [diff] [blame] | 33 | * cli_simple_process_macros() - Expand $() and ${} format env. variables |
| 34 | * |
| 35 | * @param input Input string possible containing $() / ${} vars |
| 36 | * @param output Output string with $() / ${} vars expanded |
Simon Glass | 1a62d64 | 2020-11-05 10:33:47 -0700 | [diff] [blame] | 37 | * @param max_size Maximum size of @output (including terminator) |
| 38 | * @return 0 if OK, -ENOSPC if we ran out of space in @output |
Hans de Goede | a06be2d | 2014-08-06 09:37:38 +0200 | [diff] [blame] | 39 | */ |
Simon Glass | 1a62d64 | 2020-11-05 10:33:47 -0700 | [diff] [blame] | 40 | int cli_simple_process_macros(const char *input, char *output, int max_size); |
Hans de Goede | a06be2d | 2014-08-06 09:37:38 +0200 | [diff] [blame] | 41 | |
| 42 | /** |
Simon Glass | 18d6653 | 2014-04-10 20:01:25 -0600 | [diff] [blame] | 43 | * cli_simple_run_command_list() - Execute a list of command |
| 44 | * |
| 45 | * The commands should be separated by ; or \n and will be executed |
| 46 | * by the built-in parser. |
| 47 | * |
| 48 | * This function cannot take a const char * for the command, since if it |
| 49 | * finds newlines in the string, it replaces them with \0. |
| 50 | * |
| 51 | * @param cmd String containing list of commands |
| 52 | * @param flag Execution flags (CMD_FLAG_...) |
| 53 | * @return 0 on success, or != 0 on error. |
| 54 | */ |
| 55 | int cli_simple_run_command_list(char *cmd, int flag); |
| 56 | |
| 57 | /** |
| 58 | * cli_readline() - read a line into the console_buffer |
| 59 | * |
| 60 | * This is a convenience function which calls cli_readline_into_buffer(). |
| 61 | * |
| 62 | * @prompt: Prompt to display |
| 63 | * @return command line length excluding terminator, or -ve on error |
| 64 | */ |
Simon Glass | e1bf824 | 2014-04-10 20:01:27 -0600 | [diff] [blame] | 65 | int cli_readline(const char *const prompt); |
Simon Glass | 18d6653 | 2014-04-10 20:01:25 -0600 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * readline_into_buffer() - read a line into a buffer |
| 69 | * |
| 70 | * Display the prompt, then read a command line into @buffer. The |
| 71 | * maximum line length is CONFIG_SYS_CBSIZE including a \0 terminator, which |
| 72 | * will always be added. |
| 73 | * |
| 74 | * The command is echoed as it is typed. Command editing is supported if |
| 75 | * CONFIG_CMDLINE_EDITING is defined. Tab auto-complete is supported if |
| 76 | * CONFIG_AUTO_COMPLETE is defined. If CONFIG_BOOT_RETRY_TIME is defined, |
| 77 | * then a timeout will be applied. |
| 78 | * |
| 79 | * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0, |
| 80 | * time out when time goes past endtime (timebase time in ticks). |
| 81 | * |
| 82 | * @prompt: Prompt to display |
| 83 | * @buffer: Place to put the line that is entered |
| 84 | * @timeout: Timeout in milliseconds, 0 if none |
| 85 | * @return command line length excluding terminator, or -ve on error: of the |
| 86 | * timeout is exceeded (either CONFIG_BOOT_RETRY_TIME or the timeout |
| 87 | * parameter), then -2 is returned. If a break is detected (Ctrl-C) then |
| 88 | * -1 is returned. |
| 89 | */ |
Simon Glass | e1bf824 | 2014-04-10 20:01:27 -0600 | [diff] [blame] | 90 | int cli_readline_into_buffer(const char *const prompt, char *buffer, |
| 91 | int timeout); |
Simon Glass | 18d6653 | 2014-04-10 20:01:25 -0600 | [diff] [blame] | 92 | |
| 93 | /** |
| 94 | * parse_line() - split a command line down into separate arguments |
| 95 | * |
| 96 | * The argv[] array is filled with pointers into @line, and each argument |
| 97 | * is terminated by \0 (i.e. @line is changed in the process unless there |
| 98 | * is only one argument). |
| 99 | * |
| 100 | * #argv is terminated by a NULL after the last argument pointer. |
| 101 | * |
| 102 | * At most CONFIG_SYS_MAXARGS arguments are permited - if there are more |
| 103 | * than that then an error is printed, and this function returns |
| 104 | * CONFIG_SYS_MAXARGS, with argv[] set up to that point. |
| 105 | * |
| 106 | * @line: Command line to parse |
| 107 | * @args: Array to hold arguments |
| 108 | * @return number of arguments |
| 109 | */ |
Simon Glass | e1bf824 | 2014-04-10 20:01:27 -0600 | [diff] [blame] | 110 | int cli_simple_parse_line(char *line, char *argv[]); |
Simon Glass | 18d6653 | 2014-04-10 20:01:25 -0600 | [diff] [blame] | 111 | |
Masahiro Yamada | 0f92582 | 2015-08-12 07:31:55 +0900 | [diff] [blame] | 112 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Simon Glass | affb215 | 2014-04-10 20:01:35 -0600 | [diff] [blame] | 113 | /** |
| 114 | * cli_process_fdt() - process the boot command from the FDT |
| 115 | * |
| 116 | * If bootcmmd is defined in the /config node of the FDT, we use that |
| 117 | * as the boot command. Further, if bootsecure is set to 1 (in the same |
| 118 | * node) then we return true, indicating that the command should be executed |
| 119 | * as securely as possible, avoiding the CLI parser. |
| 120 | * |
| 121 | * @cmdp: On entry, the command that will be executed if the FDT does |
| 122 | * not have a command. Returns the command to execute after |
| 123 | * checking the FDT. |
| 124 | * @return true to execute securely, else false |
| 125 | */ |
| 126 | bool cli_process_fdt(const char **cmdp); |
| 127 | |
| 128 | /** cli_secure_boot_cmd() - execute a command as securely as possible |
| 129 | * |
| 130 | * This avoids using the parser, thus executing the command with the |
| 131 | * smallest amount of code. Parameters are not supported. |
| 132 | */ |
| 133 | void cli_secure_boot_cmd(const char *cmd); |
| 134 | #else |
| 135 | static inline bool cli_process_fdt(const char **cmdp) |
| 136 | { |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | static inline void cli_secure_boot_cmd(const char *cmd) |
| 141 | { |
| 142 | } |
| 143 | #endif /* CONFIG_OF_CONTROL */ |
| 144 | |
Simon Glass | c1bb2cd | 2014-04-10 20:01:34 -0600 | [diff] [blame] | 145 | /** |
| 146 | * Go into the command loop |
| 147 | * |
| 148 | * This will return if we get a timeout waiting for a command, but only for |
| 149 | * the simple parser (not hush). See CONFIG_BOOT_RETRY_TIME. |
| 150 | */ |
| 151 | void cli_loop(void); |
| 152 | |
| 153 | /** Set up the command line interpreter ready for action */ |
| 154 | void cli_init(void); |
| 155 | |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 156 | #define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk()) |
| 157 | |
Simon Glass | 18d6653 | 2014-04-10 20:01:25 -0600 | [diff] [blame] | 158 | #endif |