blob: 453e88fa96d6eed07881d566b03db603b76a56f4 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glassdec3c012014-04-10 20:01:25 -06002/*
3 * (C) Copyright 2014 Google, Inc
4 * Simon Glass <sjg@chromium.org>
Simon Glassdec3c012014-04-10 20:01:25 -06005 */
6
7#ifndef __CLI_H
8#define __CLI_H
9
Simon Glass7646d5b2023-01-06 08:52:20 -060010#include <stdbool.h>
Simon Glass07627c52023-10-01 19:13:11 -060011#include <linux/types.h>
Simon Glass7646d5b2023-01-06 08:52:20 -060012
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 Glass9d8d3872023-01-06 08:52:26 -060018 * @emit_upto: Next index to emit from esc_save
19 * @emitting: true if emitting from esc_save
Christian Marangib357fc62025-05-25 15:43:58 +020020 * @shortcut_key: Selected shortcut option index
Simon Glass7646d5b2023-01-06 08:52:20 -060021 */
22struct cli_ch_state {
23 int esc_len;
24 char esc_save[8];
25 int emit_upto;
Simon Glass9d8d3872023-01-06 08:52:26 -060026 bool emitting;
Christian Marangib357fc62025-05-25 15:43:58 +020027 int shortcut_key;
Simon Glass7646d5b2023-01-06 08:52:20 -060028};
29
Simon Glassdec3c012014-04-10 20:01:25 -060030/**
Simon Glass07627c52023-10-01 19:13:11 -060031 * struct cli_line_state - state of the line editor
32 *
33 * @num: Current cursor position, where 0 is the start
34 * @eol_num: Number of characters in the buffer
35 * @insert: true if in 'insert' mode
Simon Glass1c67ae42023-10-01 19:13:15 -060036 * @history: true if history should be accessible
Simon Glassb9491312023-10-01 19:13:17 -060037 * @cmd_complete: true if tab completion should be enabled (requires @prompt to
38 * be set)
Simon Glass2f13ae52023-10-01 19:13:13 -060039 * @buf: Buffer containing line
40 * @prompt: Prompt for the line
Simon Glass07627c52023-10-01 19:13:11 -060041 */
42struct cli_line_state {
43 uint num;
44 uint eol_num;
Simon Glass2f13ae52023-10-01 19:13:13 -060045 uint len;
Simon Glass07627c52023-10-01 19:13:11 -060046 bool insert;
Simon Glass1c67ae42023-10-01 19:13:15 -060047 bool history;
Simon Glass3e9bc772023-10-01 19:13:16 -060048 bool cmd_complete;
Simon Glass2f13ae52023-10-01 19:13:13 -060049 char *buf;
50 const char *prompt;
Simon Glass07627c52023-10-01 19:13:11 -060051};
52
53/**
Simon Glassdec3c012014-04-10 20:01:25 -060054 * Go into the command loop
55 *
56 * This will return if we get a timeout waiting for a command. See
57 * CONFIG_BOOT_RETRY_TIME.
58 */
Simon Glass33f79132014-04-10 20:01:34 -060059void cli_simple_loop(void);
Simon Glassdec3c012014-04-10 20:01:25 -060060
61/**
62 * cli_simple_run_command() - Execute a command with the simple CLI
63 *
64 * @cmd: String containing the command to execute
65 * @flag Flag value - see CMD_FLAG_...
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010066 * Return: 1 - command executed, repeatable
Simon Glassdec3c012014-04-10 20:01:25 -060067 * 0 - command executed but not repeatable, interrupted commands are
68 * always considered not repeatable
69 * -1 - not executed (unrecognized, bootd recursion or too many args)
70 * (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is
71 * considered unrecognized)
72 */
73int cli_simple_run_command(const char *cmd, int flag);
74
75/**
Hans de Goedeb01e5102014-08-06 09:37:38 +020076 * cli_simple_process_macros() - Expand $() and ${} format env. variables
77 *
78 * @param input Input string possible containing $() / ${} vars
79 * @param output Output string with $() / ${} vars expanded
Simon Glassc7b03e82020-11-05 10:33:47 -070080 * @param max_size Maximum size of @output (including terminator)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010081 * Return: 0 if OK, -ENOSPC if we ran out of space in @output
Hans de Goedeb01e5102014-08-06 09:37:38 +020082 */
Simon Glassc7b03e82020-11-05 10:33:47 -070083int cli_simple_process_macros(const char *input, char *output, int max_size);
Hans de Goedeb01e5102014-08-06 09:37:38 +020084
85/**
Simon Glassdec3c012014-04-10 20:01:25 -060086 * cli_simple_run_command_list() - Execute a list of command
87 *
88 * The commands should be separated by ; or \n and will be executed
89 * by the built-in parser.
90 *
91 * This function cannot take a const char * for the command, since if it
92 * finds newlines in the string, it replaces them with \0.
93 *
94 * @param cmd String containing list of commands
95 * @param flag Execution flags (CMD_FLAG_...)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010096 * Return: 0 on success, or != 0 on error.
Simon Glassdec3c012014-04-10 20:01:25 -060097 */
98int cli_simple_run_command_list(char *cmd, int flag);
99
100/**
101 * cli_readline() - read a line into the console_buffer
102 *
103 * This is a convenience function which calls cli_readline_into_buffer().
104 *
105 * @prompt: Prompt to display
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100106 * Return: command line length excluding terminator, or -ve on error
Simon Glassdec3c012014-04-10 20:01:25 -0600107 */
Simon Glassbe6aafc2014-04-10 20:01:27 -0600108int cli_readline(const char *const prompt);
Simon Glassdec3c012014-04-10 20:01:25 -0600109
110/**
111 * readline_into_buffer() - read a line into a buffer
112 *
113 * Display the prompt, then read a command line into @buffer. The
114 * maximum line length is CONFIG_SYS_CBSIZE including a \0 terminator, which
115 * will always be added.
116 *
117 * The command is echoed as it is typed. Command editing is supported if
118 * CONFIG_CMDLINE_EDITING is defined. Tab auto-complete is supported if
119 * CONFIG_AUTO_COMPLETE is defined. If CONFIG_BOOT_RETRY_TIME is defined,
120 * then a timeout will be applied.
121 *
122 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
123 * time out when time goes past endtime (timebase time in ticks).
124 *
125 * @prompt: Prompt to display
126 * @buffer: Place to put the line that is entered
Simon Glass26e0cab2023-03-28 08:34:14 +1300127 * @timeout: Timeout in seconds, 0 if none
128 * Return: command line length excluding terminator, or -ve on error: if the
Simon Glassdec3c012014-04-10 20:01:25 -0600129 * timeout is exceeded (either CONFIG_BOOT_RETRY_TIME or the timeout
130 * parameter), then -2 is returned. If a break is detected (Ctrl-C) then
131 * -1 is returned.
132 */
Simon Glassbe6aafc2014-04-10 20:01:27 -0600133int cli_readline_into_buffer(const char *const prompt, char *buffer,
134 int timeout);
Simon Glassdec3c012014-04-10 20:01:25 -0600135
136/**
137 * parse_line() - split a command line down into separate arguments
138 *
139 * The argv[] array is filled with pointers into @line, and each argument
140 * is terminated by \0 (i.e. @line is changed in the process unless there
141 * is only one argument).
142 *
143 * #argv is terminated by a NULL after the last argument pointer.
144 *
145 * At most CONFIG_SYS_MAXARGS arguments are permited - if there are more
146 * than that then an error is printed, and this function returns
147 * CONFIG_SYS_MAXARGS, with argv[] set up to that point.
148 *
149 * @line: Command line to parse
150 * @args: Array to hold arguments
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100151 * Return: number of arguments
Simon Glassdec3c012014-04-10 20:01:25 -0600152 */
Simon Glassbe6aafc2014-04-10 20:01:27 -0600153int cli_simple_parse_line(char *line, char *argv[]);
Simon Glassdec3c012014-04-10 20:01:25 -0600154
Masahiro Yamada366b24f2015-08-12 07:31:55 +0900155#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glass5b47e302014-04-10 20:01:35 -0600156/**
157 * cli_process_fdt() - process the boot command from the FDT
158 *
159 * If bootcmmd is defined in the /config node of the FDT, we use that
160 * as the boot command. Further, if bootsecure is set to 1 (in the same
161 * node) then we return true, indicating that the command should be executed
162 * as securely as possible, avoiding the CLI parser.
163 *
164 * @cmdp: On entry, the command that will be executed if the FDT does
165 * not have a command. Returns the command to execute after
166 * checking the FDT.
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100167 * Return: true to execute securely, else false
Simon Glass5b47e302014-04-10 20:01:35 -0600168 */
169bool cli_process_fdt(const char **cmdp);
170
171/** cli_secure_boot_cmd() - execute a command as securely as possible
172 *
173 * This avoids using the parser, thus executing the command with the
174 * smallest amount of code. Parameters are not supported.
175 */
176void cli_secure_boot_cmd(const char *cmd);
177#else
178static inline bool cli_process_fdt(const char **cmdp)
179{
180 return false;
181}
182
183static inline void cli_secure_boot_cmd(const char *cmd)
184{
185}
186#endif /* CONFIG_OF_CONTROL */
187
Simon Glass33f79132014-04-10 20:01:34 -0600188/**
189 * Go into the command loop
190 *
191 * This will return if we get a timeout waiting for a command, but only for
192 * the simple parser (not hush). See CONFIG_BOOT_RETRY_TIME.
193 */
194void cli_loop(void);
195
196/** Set up the command line interpreter ready for action */
197void cli_init(void);
198
Simon Glass66b8c8b2014-04-10 20:01:26 -0600199#define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
Simon Glass7646d5b2023-01-06 08:52:20 -0600200#define CTL_CH(c) ((c) - 'a' + 1)
201
202/**
203 * cli_ch_init() - Set up the initial state to process input characters
204 *
205 * @cch: State to set up
206 */
207void cli_ch_init(struct cli_ch_state *cch);
208
209/**
210 * cli_ch_process() - Process an input character
211 *
212 * When @ichar is 0, this function returns any characters from an invalid escape
213 * sequence which are still pending in the buffer
214 *
215 * Otherwise it processes the input character. If it is an escape character,
216 * then an escape sequence is started and the function returns 0. If we are in
217 * the middle of an escape sequence, the character is processed and may result
218 * in returning 0 (if more characters are needed) or a valid character (if
219 * @ichar finishes the sequence).
220 *
221 * If @ichar is a valid character and there is no escape sequence in progress,
222 * then it is returned as is.
223 *
224 * If the Enter key is pressed, '\n' is returned.
225 *
226 * Usage should be like this::
227 *
228 * struct cli_ch_state cch;
229 *
230 * cli_ch_init(cch);
231 * do
232 * {
233 * int ichar, ch;
234 *
235 * ichar = cli_ch_process(cch, 0);
236 * if (!ichar) {
237 * ch = getchar();
238 * ichar = cli_ch_process(cch, ch);
239 * }
240 * (handle the ichar character)
241 * } while (!done)
242 *
243 * If tstc() is used to look for keypresses, this function can be called with
244 * @ichar set to -ETIMEDOUT if there is no character after 5-10ms. This allows
245 * the ambgiuity between the Escape key and the arrow keys (which generate an
246 * escape character followed by other characters) to be resolved.
247 *
248 * @cch: Current state
249 * @ichar: Input character to process, or 0 if none, or -ETIMEDOUT if no
250 * character has been received within a small number of milliseconds (this
251 * cancels any existing escape sequence and allows pressing the Escape key to
252 * work)
253 * Returns: Resulting input character after processing, 0 if none, '\e' if
254 * an existing escape sequence was cancelled
255 */
256int cli_ch_process(struct cli_ch_state *cch, int ichar);
Simon Glass66b8c8b2014-04-10 20:01:26 -0600257
Simon Glass2f13ae52023-10-01 19:13:13 -0600258/**
259 * cread_line_process_ch() - Process a character for line input
260 *
261 * @cls: CLI line state
262 * @ichar: Character to process
263 * Return: 0 if input is complete, with line in cls->buf, -EINTR if input was
264 * cancelled with Ctrl-C, -EAGAIN if more characters are needed
265 */
266int cread_line_process_ch(struct cli_line_state *cls, char ichar);
267
Simon Glassb9491312023-10-01 19:13:17 -0600268/**
269 * cli_cread_init() - Set up a new cread struct
270 *
271 * Sets up a new cread state, with history and cmd_complete set to false
272 *
273 * After calling this, you can use cread_line_process_ch() to process characters
274 * received from the user.
275 *
276 * @cls: CLI line state
277 * @buf: Text buffer containing the initial text
278 * @buf_size: Buffer size, including nul terminator
279 */
280void cli_cread_init(struct cli_line_state *cls, char *buf, uint buf_size);
281
Simon Glass4c0bf972023-10-01 19:13:06 -0600282/** cread_print_hist_list() - Print the command-line history list */
283void cread_print_hist_list(void);
284
Simon Glassdec3c012014-04-10 20:01:25 -0600285#endif