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