1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * (C) Copyright 2014 Google, Inc
4  * Simon Glass <sjg@chromium.org>
5  */
6 
7 #ifndef __CLI_H
8 #define __CLI_H
9 
10 #include <stdbool.h>
11 #include <linux/types.h>
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
18  * @emit_upto: Next index to emit from esc_save
19  * @emitting: true if emitting from esc_save
20  * @shortcut_key: Selected shortcut option index
21  */
22 struct cli_ch_state {
23 	int esc_len;
24 	char esc_save[8];
25 	int emit_upto;
26 	bool emitting;
27 	int shortcut_key;
28 };
29 
30 /**
31  * 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
36  * @history: true if history should be accessible
37  * @cmd_complete: true if tab completion should be enabled (requires @prompt to
38  *	be set)
39  * @buf: Buffer containing line
40  * @prompt: Prompt for the line
41  */
42 struct cli_line_state {
43 	uint num;
44 	uint eol_num;
45 	uint len;
46 	bool insert;
47 	bool history;
48 	bool cmd_complete;
49 	char *buf;
50 	const char *prompt;
51 };
52 
53 /**
54  * 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  */
59 void cli_simple_loop(void);
60 
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_...
66  * Return: 1  - command executed, repeatable
67  *	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  */
73 int cli_simple_run_command(const char *cmd, int flag);
74 
75 /**
76  * 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
80  * @param max_size	Maximum size of @output (including terminator)
81  * Return: 0 if OK, -ENOSPC if we ran out of space in @output
82  */
83 int cli_simple_process_macros(const char *input, char *output, int max_size);
84 
85 /**
86  * 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_...)
96  * Return: 0 on success, or != 0 on error.
97  */
98 int 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
106  * Return: command line length excluding terminator, or -ve on error
107  */
108 int cli_readline(const char *const prompt);
109 
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
127  * @timeout:	Timeout in seconds, 0 if none
128  * Return: command line length excluding terminator, or -ve on error: if the
129  * 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  */
133 int cli_readline_into_buffer(const char *const prompt, char *buffer,
134 				int timeout);
135 
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
151  * Return: number of arguments
152  */
153 int cli_simple_parse_line(char *line, char *argv[]);
154 
155 #if CONFIG_IS_ENABLED(OF_CONTROL)
156 /**
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.
167  * Return: true to execute securely, else false
168  */
169 bool 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  */
176 void cli_secure_boot_cmd(const char *cmd);
177 #else
cli_process_fdt(const char ** cmdp)178 static inline bool cli_process_fdt(const char **cmdp)
179 {
180 	return false;
181 }
182 
cli_secure_boot_cmd(const char * cmd)183 static inline void cli_secure_boot_cmd(const char *cmd)
184 {
185 }
186 #endif /* CONFIG_OF_CONTROL */
187 
188 /**
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  */
194 void cli_loop(void);
195 
196 /** Set up the command line interpreter ready for action */
197 void cli_init(void);
198 
199 #define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
200 #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  */
207 void 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  */
256 int cli_ch_process(struct cli_ch_state *cch, int ichar);
257 
258 /**
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  */
266 int cread_line_process_ch(struct cli_line_state *cls, char ichar);
267 
268 /**
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  */
280 void cli_cread_init(struct cli_line_state *cls, char *buf, uint buf_size);
281 
282 /** cread_print_hist_list() - Print the command-line history list */
283 void cread_print_hist_list(void);
284 
285 #endif
286