blob: bbc5327643554a8aa3d60e188ad4e70f0b4c8081 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glass18d66532014-04-10 20:01:25 -06002/*
3 * (C) Copyright 2014 Google, Inc
4 * Simon Glass <sjg@chromium.org>
Simon Glass18d66532014-04-10 20:01:25 -06005 */
6
7#ifndef __CLI_H
8#define __CLI_H
9
Simon Glassb08e9d42023-01-06 08:52:20 -060010#include <stdbool.h>
Simon Glassbe5c2ed2023-10-01 19:13:11 -060011#include <linux/types.h>
Simon Glassb08e9d42023-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 Glass32bab0e2023-01-06 08:52:26 -060018 * @emit_upto: Next index to emit from esc_save
19 * @emitting: true if emitting from esc_save
Simon Glassb08e9d42023-01-06 08:52:20 -060020 */
21struct cli_ch_state {
22 int esc_len;
23 char esc_save[8];
24 int emit_upto;
Simon Glass32bab0e2023-01-06 08:52:26 -060025 bool emitting;
Simon Glassb08e9d42023-01-06 08:52:20 -060026};
27
Simon Glass18d66532014-04-10 20:01:25 -060028/**
Simon Glassbe5c2ed2023-10-01 19:13:11 -060029 * 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 Glasse5509ce2023-10-01 19:13:13 -060034 * @buf: Buffer containing line
35 * @prompt: Prompt for the line
Simon Glassbe5c2ed2023-10-01 19:13:11 -060036 */
37struct cli_line_state {
38 uint num;
39 uint eol_num;
Simon Glasse5509ce2023-10-01 19:13:13 -060040 uint len;
Simon Glassbe5c2ed2023-10-01 19:13:11 -060041 bool insert;
Simon Glasse5509ce2023-10-01 19:13:13 -060042 char *buf;
43 const char *prompt;
Simon Glassbe5c2ed2023-10-01 19:13:11 -060044};
45
46/**
Simon Glass18d66532014-04-10 20:01:25 -060047 * Go into the command loop
48 *
49 * This will return if we get a timeout waiting for a command. See
50 * CONFIG_BOOT_RETRY_TIME.
51 */
Simon Glassc1bb2cd2014-04-10 20:01:34 -060052void cli_simple_loop(void);
Simon Glass18d66532014-04-10 20:01:25 -060053
54/**
55 * cli_simple_run_command() - Execute a command with the simple CLI
56 *
57 * @cmd: String containing the command to execute
58 * @flag Flag value - see CMD_FLAG_...
Heinrich Schuchardt185f8122022-01-19 18:05:50 +010059 * Return: 1 - command executed, repeatable
Simon Glass18d66532014-04-10 20:01:25 -060060 * 0 - command executed but not repeatable, interrupted commands are
61 * always considered not repeatable
62 * -1 - not executed (unrecognized, bootd recursion or too many args)
63 * (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is
64 * considered unrecognized)
65 */
66int cli_simple_run_command(const char *cmd, int flag);
67
68/**
Hans de Goedea06be2d2014-08-06 09:37:38 +020069 * cli_simple_process_macros() - Expand $() and ${} format env. variables
70 *
71 * @param input Input string possible containing $() / ${} vars
72 * @param output Output string with $() / ${} vars expanded
Simon Glass1a62d642020-11-05 10:33:47 -070073 * @param max_size Maximum size of @output (including terminator)
Heinrich Schuchardt185f8122022-01-19 18:05:50 +010074 * Return: 0 if OK, -ENOSPC if we ran out of space in @output
Hans de Goedea06be2d2014-08-06 09:37:38 +020075 */
Simon Glass1a62d642020-11-05 10:33:47 -070076int cli_simple_process_macros(const char *input, char *output, int max_size);
Hans de Goedea06be2d2014-08-06 09:37:38 +020077
78/**
Simon Glass18d66532014-04-10 20:01:25 -060079 * cli_simple_run_command_list() - Execute a list of command
80 *
81 * The commands should be separated by ; or \n and will be executed
82 * by the built-in parser.
83 *
84 * This function cannot take a const char * for the command, since if it
85 * finds newlines in the string, it replaces them with \0.
86 *
87 * @param cmd String containing list of commands
88 * @param flag Execution flags (CMD_FLAG_...)
Heinrich Schuchardt185f8122022-01-19 18:05:50 +010089 * Return: 0 on success, or != 0 on error.
Simon Glass18d66532014-04-10 20:01:25 -060090 */
91int cli_simple_run_command_list(char *cmd, int flag);
92
93/**
94 * cli_readline() - read a line into the console_buffer
95 *
96 * This is a convenience function which calls cli_readline_into_buffer().
97 *
98 * @prompt: Prompt to display
Heinrich Schuchardt185f8122022-01-19 18:05:50 +010099 * Return: command line length excluding terminator, or -ve on error
Simon Glass18d66532014-04-10 20:01:25 -0600100 */
Simon Glasse1bf8242014-04-10 20:01:27 -0600101int cli_readline(const char *const prompt);
Simon Glass18d66532014-04-10 20:01:25 -0600102
103/**
104 * readline_into_buffer() - read a line into a buffer
105 *
106 * Display the prompt, then read a command line into @buffer. The
107 * maximum line length is CONFIG_SYS_CBSIZE including a \0 terminator, which
108 * will always be added.
109 *
110 * The command is echoed as it is typed. Command editing is supported if
111 * CONFIG_CMDLINE_EDITING is defined. Tab auto-complete is supported if
112 * CONFIG_AUTO_COMPLETE is defined. If CONFIG_BOOT_RETRY_TIME is defined,
113 * then a timeout will be applied.
114 *
115 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
116 * time out when time goes past endtime (timebase time in ticks).
117 *
118 * @prompt: Prompt to display
119 * @buffer: Place to put the line that is entered
Simon Glassbe0169f2023-03-28 08:34:14 +1300120 * @timeout: Timeout in seconds, 0 if none
121 * Return: command line length excluding terminator, or -ve on error: if the
Simon Glass18d66532014-04-10 20:01:25 -0600122 * timeout is exceeded (either CONFIG_BOOT_RETRY_TIME or the timeout
123 * parameter), then -2 is returned. If a break is detected (Ctrl-C) then
124 * -1 is returned.
125 */
Simon Glasse1bf8242014-04-10 20:01:27 -0600126int cli_readline_into_buffer(const char *const prompt, char *buffer,
127 int timeout);
Simon Glass18d66532014-04-10 20:01:25 -0600128
129/**
130 * parse_line() - split a command line down into separate arguments
131 *
132 * The argv[] array is filled with pointers into @line, and each argument
133 * is terminated by \0 (i.e. @line is changed in the process unless there
134 * is only one argument).
135 *
136 * #argv is terminated by a NULL after the last argument pointer.
137 *
138 * At most CONFIG_SYS_MAXARGS arguments are permited - if there are more
139 * than that then an error is printed, and this function returns
140 * CONFIG_SYS_MAXARGS, with argv[] set up to that point.
141 *
142 * @line: Command line to parse
143 * @args: Array to hold arguments
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100144 * Return: number of arguments
Simon Glass18d66532014-04-10 20:01:25 -0600145 */
Simon Glasse1bf8242014-04-10 20:01:27 -0600146int cli_simple_parse_line(char *line, char *argv[]);
Simon Glass18d66532014-04-10 20:01:25 -0600147
Masahiro Yamada0f925822015-08-12 07:31:55 +0900148#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glassaffb2152014-04-10 20:01:35 -0600149/**
150 * cli_process_fdt() - process the boot command from the FDT
151 *
152 * If bootcmmd is defined in the /config node of the FDT, we use that
153 * as the boot command. Further, if bootsecure is set to 1 (in the same
154 * node) then we return true, indicating that the command should be executed
155 * as securely as possible, avoiding the CLI parser.
156 *
157 * @cmdp: On entry, the command that will be executed if the FDT does
158 * not have a command. Returns the command to execute after
159 * checking the FDT.
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100160 * Return: true to execute securely, else false
Simon Glassaffb2152014-04-10 20:01:35 -0600161 */
162bool cli_process_fdt(const char **cmdp);
163
164/** cli_secure_boot_cmd() - execute a command as securely as possible
165 *
166 * This avoids using the parser, thus executing the command with the
167 * smallest amount of code. Parameters are not supported.
168 */
169void cli_secure_boot_cmd(const char *cmd);
170#else
171static inline bool cli_process_fdt(const char **cmdp)
172{
173 return false;
174}
175
176static inline void cli_secure_boot_cmd(const char *cmd)
177{
178}
179#endif /* CONFIG_OF_CONTROL */
180
Simon Glassc1bb2cd2014-04-10 20:01:34 -0600181/**
182 * Go into the command loop
183 *
184 * This will return if we get a timeout waiting for a command, but only for
185 * the simple parser (not hush). See CONFIG_BOOT_RETRY_TIME.
186 */
187void cli_loop(void);
188
189/** Set up the command line interpreter ready for action */
190void cli_init(void);
191
Simon Glass6493ccc2014-04-10 20:01:26 -0600192#define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
Simon Glassb08e9d42023-01-06 08:52:20 -0600193#define CTL_CH(c) ((c) - 'a' + 1)
194
195/**
196 * cli_ch_init() - Set up the initial state to process input characters
197 *
198 * @cch: State to set up
199 */
200void cli_ch_init(struct cli_ch_state *cch);
201
202/**
203 * cli_ch_process() - Process an input character
204 *
205 * When @ichar is 0, this function returns any characters from an invalid escape
206 * sequence which are still pending in the buffer
207 *
208 * Otherwise it processes the input character. If it is an escape character,
209 * then an escape sequence is started and the function returns 0. If we are in
210 * the middle of an escape sequence, the character is processed and may result
211 * in returning 0 (if more characters are needed) or a valid character (if
212 * @ichar finishes the sequence).
213 *
214 * If @ichar is a valid character and there is no escape sequence in progress,
215 * then it is returned as is.
216 *
217 * If the Enter key is pressed, '\n' is returned.
218 *
219 * Usage should be like this::
220 *
221 * struct cli_ch_state cch;
222 *
223 * cli_ch_init(cch);
224 * do
225 * {
226 * int ichar, ch;
227 *
228 * ichar = cli_ch_process(cch, 0);
229 * if (!ichar) {
230 * ch = getchar();
231 * ichar = cli_ch_process(cch, ch);
232 * }
233 * (handle the ichar character)
234 * } while (!done)
235 *
236 * If tstc() is used to look for keypresses, this function can be called with
237 * @ichar set to -ETIMEDOUT if there is no character after 5-10ms. This allows
238 * the ambgiuity between the Escape key and the arrow keys (which generate an
239 * escape character followed by other characters) to be resolved.
240 *
241 * @cch: Current state
242 * @ichar: Input character to process, or 0 if none, or -ETIMEDOUT if no
243 * character has been received within a small number of milliseconds (this
244 * cancels any existing escape sequence and allows pressing the Escape key to
245 * work)
246 * Returns: Resulting input character after processing, 0 if none, '\e' if
247 * an existing escape sequence was cancelled
248 */
249int cli_ch_process(struct cli_ch_state *cch, int ichar);
Simon Glass6493ccc2014-04-10 20:01:26 -0600250
Simon Glasse5509ce2023-10-01 19:13:13 -0600251/**
252 * cread_line_process_ch() - Process a character for line input
253 *
254 * @cls: CLI line state
255 * @ichar: Character to process
256 * Return: 0 if input is complete, with line in cls->buf, -EINTR if input was
257 * cancelled with Ctrl-C, -EAGAIN if more characters are needed
258 */
259int cread_line_process_ch(struct cli_line_state *cls, char ichar);
260
Simon Glass33eb0b92023-10-01 19:13:06 -0600261/** cread_print_hist_list() - Print the command-line history list */
262void cread_print_hist_list(void);
263
Simon Glass18d66532014-04-10 20:01:25 -0600264#endif