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