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 | |
Simon Glass | b08e9d4 | 2023-01-06 08:52:20 -0600 | [diff] [blame] | 10 | #include <stdbool.h> |
| 11 | |
| 12 | /** |
| 13 | * struct cli_ch_state - state information for reading cmdline characters |
| 14 | * |
| 15 | * @esc_len: Number of escape characters read so far |
| 16 | * @esc_save: Escape characters collected so far |
Simon Glass | 32bab0e | 2023-01-06 08:52:26 -0600 | [diff] [blame] | 17 | * @emit_upto: Next index to emit from esc_save |
| 18 | * @emitting: true if emitting from esc_save |
Simon Glass | b08e9d4 | 2023-01-06 08:52:20 -0600 | [diff] [blame] | 19 | */ |
| 20 | struct cli_ch_state { |
| 21 | int esc_len; |
| 22 | char esc_save[8]; |
| 23 | int emit_upto; |
Simon Glass | 32bab0e | 2023-01-06 08:52:26 -0600 | [diff] [blame] | 24 | bool emitting; |
Simon Glass | b08e9d4 | 2023-01-06 08:52:20 -0600 | [diff] [blame] | 25 | }; |
| 26 | |
Simon Glass | 18d6653 | 2014-04-10 20:01:25 -0600 | [diff] [blame] | 27 | /** |
| 28 | * Go into the command loop |
| 29 | * |
| 30 | * This will return if we get a timeout waiting for a command. See |
| 31 | * CONFIG_BOOT_RETRY_TIME. |
| 32 | */ |
Simon Glass | c1bb2cd | 2014-04-10 20:01:34 -0600 | [diff] [blame] | 33 | void cli_simple_loop(void); |
Simon Glass | 18d6653 | 2014-04-10 20:01:25 -0600 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * cli_simple_run_command() - Execute a command with the simple CLI |
| 37 | * |
| 38 | * @cmd: String containing the command to execute |
| 39 | * @flag Flag value - see CMD_FLAG_... |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 40 | * Return: 1 - command executed, repeatable |
Simon Glass | 18d6653 | 2014-04-10 20:01:25 -0600 | [diff] [blame] | 41 | * 0 - command executed but not repeatable, interrupted commands are |
| 42 | * always considered not repeatable |
| 43 | * -1 - not executed (unrecognized, bootd recursion or too many args) |
| 44 | * (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is |
| 45 | * considered unrecognized) |
| 46 | */ |
| 47 | int cli_simple_run_command(const char *cmd, int flag); |
| 48 | |
| 49 | /** |
Hans de Goede | a06be2d | 2014-08-06 09:37:38 +0200 | [diff] [blame] | 50 | * cli_simple_process_macros() - Expand $() and ${} format env. variables |
| 51 | * |
| 52 | * @param input Input string possible containing $() / ${} vars |
| 53 | * @param output Output string with $() / ${} vars expanded |
Simon Glass | 1a62d64 | 2020-11-05 10:33:47 -0700 | [diff] [blame] | 54 | * @param max_size Maximum size of @output (including terminator) |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 55 | * 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] | 56 | */ |
Simon Glass | 1a62d64 | 2020-11-05 10:33:47 -0700 | [diff] [blame] | 57 | 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] | 58 | |
| 59 | /** |
Simon Glass | 18d6653 | 2014-04-10 20:01:25 -0600 | [diff] [blame] | 60 | * cli_simple_run_command_list() - Execute a list of command |
| 61 | * |
| 62 | * The commands should be separated by ; or \n and will be executed |
| 63 | * by the built-in parser. |
| 64 | * |
| 65 | * This function cannot take a const char * for the command, since if it |
| 66 | * finds newlines in the string, it replaces them with \0. |
| 67 | * |
| 68 | * @param cmd String containing list of commands |
| 69 | * @param flag Execution flags (CMD_FLAG_...) |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 70 | * Return: 0 on success, or != 0 on error. |
Simon Glass | 18d6653 | 2014-04-10 20:01:25 -0600 | [diff] [blame] | 71 | */ |
| 72 | int cli_simple_run_command_list(char *cmd, int flag); |
| 73 | |
| 74 | /** |
| 75 | * cli_readline() - read a line into the console_buffer |
| 76 | * |
| 77 | * This is a convenience function which calls cli_readline_into_buffer(). |
| 78 | * |
| 79 | * @prompt: Prompt to display |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 80 | * Return: command line length excluding terminator, or -ve on error |
Simon Glass | 18d6653 | 2014-04-10 20:01:25 -0600 | [diff] [blame] | 81 | */ |
Simon Glass | e1bf824 | 2014-04-10 20:01:27 -0600 | [diff] [blame] | 82 | int cli_readline(const char *const prompt); |
Simon Glass | 18d6653 | 2014-04-10 20:01:25 -0600 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * readline_into_buffer() - read a line into a buffer |
| 86 | * |
| 87 | * Display the prompt, then read a command line into @buffer. The |
| 88 | * maximum line length is CONFIG_SYS_CBSIZE including a \0 terminator, which |
| 89 | * will always be added. |
| 90 | * |
| 91 | * The command is echoed as it is typed. Command editing is supported if |
| 92 | * CONFIG_CMDLINE_EDITING is defined. Tab auto-complete is supported if |
| 93 | * CONFIG_AUTO_COMPLETE is defined. If CONFIG_BOOT_RETRY_TIME is defined, |
| 94 | * then a timeout will be applied. |
| 95 | * |
| 96 | * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0, |
| 97 | * time out when time goes past endtime (timebase time in ticks). |
| 98 | * |
| 99 | * @prompt: Prompt to display |
| 100 | * @buffer: Place to put the line that is entered |
Simon Glass | be0169f | 2023-03-28 08:34:14 +1300 | [diff] [blame] | 101 | * @timeout: Timeout in seconds, 0 if none |
| 102 | * Return: command line length excluding terminator, or -ve on error: if the |
Simon Glass | 18d6653 | 2014-04-10 20:01:25 -0600 | [diff] [blame] | 103 | * timeout is exceeded (either CONFIG_BOOT_RETRY_TIME or the timeout |
| 104 | * parameter), then -2 is returned. If a break is detected (Ctrl-C) then |
| 105 | * -1 is returned. |
| 106 | */ |
Simon Glass | e1bf824 | 2014-04-10 20:01:27 -0600 | [diff] [blame] | 107 | int cli_readline_into_buffer(const char *const prompt, char *buffer, |
| 108 | int timeout); |
Simon Glass | 18d6653 | 2014-04-10 20:01:25 -0600 | [diff] [blame] | 109 | |
| 110 | /** |
| 111 | * parse_line() - split a command line down into separate arguments |
| 112 | * |
| 113 | * The argv[] array is filled with pointers into @line, and each argument |
| 114 | * is terminated by \0 (i.e. @line is changed in the process unless there |
| 115 | * is only one argument). |
| 116 | * |
| 117 | * #argv is terminated by a NULL after the last argument pointer. |
| 118 | * |
| 119 | * At most CONFIG_SYS_MAXARGS arguments are permited - if there are more |
| 120 | * than that then an error is printed, and this function returns |
| 121 | * CONFIG_SYS_MAXARGS, with argv[] set up to that point. |
| 122 | * |
| 123 | * @line: Command line to parse |
| 124 | * @args: Array to hold arguments |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 125 | * Return: number of arguments |
Simon Glass | 18d6653 | 2014-04-10 20:01:25 -0600 | [diff] [blame] | 126 | */ |
Simon Glass | e1bf824 | 2014-04-10 20:01:27 -0600 | [diff] [blame] | 127 | int cli_simple_parse_line(char *line, char *argv[]); |
Simon Glass | 18d6653 | 2014-04-10 20:01:25 -0600 | [diff] [blame] | 128 | |
Masahiro Yamada | 0f92582 | 2015-08-12 07:31:55 +0900 | [diff] [blame] | 129 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Simon Glass | affb215 | 2014-04-10 20:01:35 -0600 | [diff] [blame] | 130 | /** |
| 131 | * cli_process_fdt() - process the boot command from the FDT |
| 132 | * |
| 133 | * If bootcmmd is defined in the /config node of the FDT, we use that |
| 134 | * as the boot command. Further, if bootsecure is set to 1 (in the same |
| 135 | * node) then we return true, indicating that the command should be executed |
| 136 | * as securely as possible, avoiding the CLI parser. |
| 137 | * |
| 138 | * @cmdp: On entry, the command that will be executed if the FDT does |
| 139 | * not have a command. Returns the command to execute after |
| 140 | * checking the FDT. |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 141 | * Return: true to execute securely, else false |
Simon Glass | affb215 | 2014-04-10 20:01:35 -0600 | [diff] [blame] | 142 | */ |
| 143 | bool cli_process_fdt(const char **cmdp); |
| 144 | |
| 145 | /** cli_secure_boot_cmd() - execute a command as securely as possible |
| 146 | * |
| 147 | * This avoids using the parser, thus executing the command with the |
| 148 | * smallest amount of code. Parameters are not supported. |
| 149 | */ |
| 150 | void cli_secure_boot_cmd(const char *cmd); |
| 151 | #else |
| 152 | static inline bool cli_process_fdt(const char **cmdp) |
| 153 | { |
| 154 | return false; |
| 155 | } |
| 156 | |
| 157 | static inline void cli_secure_boot_cmd(const char *cmd) |
| 158 | { |
| 159 | } |
| 160 | #endif /* CONFIG_OF_CONTROL */ |
| 161 | |
Simon Glass | c1bb2cd | 2014-04-10 20:01:34 -0600 | [diff] [blame] | 162 | /** |
| 163 | * Go into the command loop |
| 164 | * |
| 165 | * This will return if we get a timeout waiting for a command, but only for |
| 166 | * the simple parser (not hush). See CONFIG_BOOT_RETRY_TIME. |
| 167 | */ |
| 168 | void cli_loop(void); |
| 169 | |
| 170 | /** Set up the command line interpreter ready for action */ |
| 171 | void cli_init(void); |
| 172 | |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 173 | #define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk()) |
Simon Glass | b08e9d4 | 2023-01-06 08:52:20 -0600 | [diff] [blame] | 174 | #define CTL_CH(c) ((c) - 'a' + 1) |
| 175 | |
| 176 | /** |
| 177 | * cli_ch_init() - Set up the initial state to process input characters |
| 178 | * |
| 179 | * @cch: State to set up |
| 180 | */ |
| 181 | void cli_ch_init(struct cli_ch_state *cch); |
| 182 | |
| 183 | /** |
| 184 | * cli_ch_process() - Process an input character |
| 185 | * |
| 186 | * When @ichar is 0, this function returns any characters from an invalid escape |
| 187 | * sequence which are still pending in the buffer |
| 188 | * |
| 189 | * Otherwise it processes the input character. If it is an escape character, |
| 190 | * then an escape sequence is started and the function returns 0. If we are in |
| 191 | * the middle of an escape sequence, the character is processed and may result |
| 192 | * in returning 0 (if more characters are needed) or a valid character (if |
| 193 | * @ichar finishes the sequence). |
| 194 | * |
| 195 | * If @ichar is a valid character and there is no escape sequence in progress, |
| 196 | * then it is returned as is. |
| 197 | * |
| 198 | * If the Enter key is pressed, '\n' is returned. |
| 199 | * |
| 200 | * Usage should be like this:: |
| 201 | * |
| 202 | * struct cli_ch_state cch; |
| 203 | * |
| 204 | * cli_ch_init(cch); |
| 205 | * do |
| 206 | * { |
| 207 | * int ichar, ch; |
| 208 | * |
| 209 | * ichar = cli_ch_process(cch, 0); |
| 210 | * if (!ichar) { |
| 211 | * ch = getchar(); |
| 212 | * ichar = cli_ch_process(cch, ch); |
| 213 | * } |
| 214 | * (handle the ichar character) |
| 215 | * } while (!done) |
| 216 | * |
| 217 | * If tstc() is used to look for keypresses, this function can be called with |
| 218 | * @ichar set to -ETIMEDOUT if there is no character after 5-10ms. This allows |
| 219 | * the ambgiuity between the Escape key and the arrow keys (which generate an |
| 220 | * escape character followed by other characters) to be resolved. |
| 221 | * |
| 222 | * @cch: Current state |
| 223 | * @ichar: Input character to process, or 0 if none, or -ETIMEDOUT if no |
| 224 | * character has been received within a small number of milliseconds (this |
| 225 | * cancels any existing escape sequence and allows pressing the Escape key to |
| 226 | * work) |
| 227 | * Returns: Resulting input character after processing, 0 if none, '\e' if |
| 228 | * an existing escape sequence was cancelled |
| 229 | */ |
| 230 | int cli_ch_process(struct cli_ch_state *cch, int ichar); |
Simon Glass | 6493ccc | 2014-04-10 20:01:26 -0600 | [diff] [blame] | 231 | |
Simon Glass | 18d6653 | 2014-04-10 20:01:25 -0600 | [diff] [blame] | 232 | #endif |