blob: ad3cb4499fe12e91544855478ea6c18b68c9ec67 [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 Glass8fc041f2023-10-01 19:13:15 -060034 * @history: true if history should be accessible
Simon Glass3b487bf2023-10-01 19:13:16 -060035 * @cmd_complete: true if tab completion should be enabled
Simon Glasse5509ce2023-10-01 19:13:13 -060036 * @buf: Buffer containing line
37 * @prompt: Prompt for the line
Simon Glassbe5c2ed2023-10-01 19:13:11 -060038 */
39struct cli_line_state {
40 uint num;
41 uint eol_num;
Simon Glasse5509ce2023-10-01 19:13:13 -060042 uint len;
Simon Glassbe5c2ed2023-10-01 19:13:11 -060043 bool insert;
Simon Glass8fc041f2023-10-01 19:13:15 -060044 bool history;
Simon Glass3b487bf2023-10-01 19:13:16 -060045 bool cmd_complete;
Simon Glasse5509ce2023-10-01 19:13:13 -060046 char *buf;
47 const char *prompt;
Simon Glassbe5c2ed2023-10-01 19:13:11 -060048};
49
50/**
Simon Glass18d66532014-04-10 20:01:25 -060051 * Go into the command loop
52 *
53 * This will return if we get a timeout waiting for a command. See
54 * CONFIG_BOOT_RETRY_TIME.
55 */
Simon Glassc1bb2cd2014-04-10 20:01:34 -060056void cli_simple_loop(void);
Simon Glass18d66532014-04-10 20:01:25 -060057
58/**
59 * cli_simple_run_command() - Execute a command with the simple CLI
60 *
61 * @cmd: String containing the command to execute
62 * @flag Flag value - see CMD_FLAG_...
Heinrich Schuchardt185f8122022-01-19 18:05:50 +010063 * Return: 1 - command executed, repeatable
Simon Glass18d66532014-04-10 20:01:25 -060064 * 0 - command executed but not repeatable, interrupted commands are
65 * always considered not repeatable
66 * -1 - not executed (unrecognized, bootd recursion or too many args)
67 * (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is
68 * considered unrecognized)
69 */
70int cli_simple_run_command(const char *cmd, int flag);
71
72/**
Hans de Goedea06be2d2014-08-06 09:37:38 +020073 * cli_simple_process_macros() - Expand $() and ${} format env. variables
74 *
75 * @param input Input string possible containing $() / ${} vars
76 * @param output Output string with $() / ${} vars expanded
Simon Glass1a62d642020-11-05 10:33:47 -070077 * @param max_size Maximum size of @output (including terminator)
Heinrich Schuchardt185f8122022-01-19 18:05:50 +010078 * Return: 0 if OK, -ENOSPC if we ran out of space in @output
Hans de Goedea06be2d2014-08-06 09:37:38 +020079 */
Simon Glass1a62d642020-11-05 10:33:47 -070080int cli_simple_process_macros(const char *input, char *output, int max_size);
Hans de Goedea06be2d2014-08-06 09:37:38 +020081
82/**
Simon Glass18d66532014-04-10 20:01:25 -060083 * cli_simple_run_command_list() - Execute a list of command
84 *
85 * The commands should be separated by ; or \n and will be executed
86 * by the built-in parser.
87 *
88 * This function cannot take a const char * for the command, since if it
89 * finds newlines in the string, it replaces them with \0.
90 *
91 * @param cmd String containing list of commands
92 * @param flag Execution flags (CMD_FLAG_...)
Heinrich Schuchardt185f8122022-01-19 18:05:50 +010093 * Return: 0 on success, or != 0 on error.
Simon Glass18d66532014-04-10 20:01:25 -060094 */
95int cli_simple_run_command_list(char *cmd, int flag);
96
97/**
98 * cli_readline() - read a line into the console_buffer
99 *
100 * This is a convenience function which calls cli_readline_into_buffer().
101 *
102 * @prompt: Prompt to display
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100103 * Return: command line length excluding terminator, or -ve on error
Simon Glass18d66532014-04-10 20:01:25 -0600104 */
Simon Glasse1bf8242014-04-10 20:01:27 -0600105int cli_readline(const char *const prompt);
Simon Glass18d66532014-04-10 20:01:25 -0600106
107/**
108 * readline_into_buffer() - read a line into a buffer
109 *
110 * Display the prompt, then read a command line into @buffer. The
111 * maximum line length is CONFIG_SYS_CBSIZE including a \0 terminator, which
112 * will always be added.
113 *
114 * The command is echoed as it is typed. Command editing is supported if
115 * CONFIG_CMDLINE_EDITING is defined. Tab auto-complete is supported if
116 * CONFIG_AUTO_COMPLETE is defined. If CONFIG_BOOT_RETRY_TIME is defined,
117 * then a timeout will be applied.
118 *
119 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
120 * time out when time goes past endtime (timebase time in ticks).
121 *
122 * @prompt: Prompt to display
123 * @buffer: Place to put the line that is entered
Simon Glassbe0169f2023-03-28 08:34:14 +1300124 * @timeout: Timeout in seconds, 0 if none
125 * Return: command line length excluding terminator, or -ve on error: if the
Simon Glass18d66532014-04-10 20:01:25 -0600126 * timeout is exceeded (either CONFIG_BOOT_RETRY_TIME or the timeout
127 * parameter), then -2 is returned. If a break is detected (Ctrl-C) then
128 * -1 is returned.
129 */
Simon Glasse1bf8242014-04-10 20:01:27 -0600130int cli_readline_into_buffer(const char *const prompt, char *buffer,
131 int timeout);
Simon Glass18d66532014-04-10 20:01:25 -0600132
133/**
134 * parse_line() - split a command line down into separate arguments
135 *
136 * The argv[] array is filled with pointers into @line, and each argument
137 * is terminated by \0 (i.e. @line is changed in the process unless there
138 * is only one argument).
139 *
140 * #argv is terminated by a NULL after the last argument pointer.
141 *
142 * At most CONFIG_SYS_MAXARGS arguments are permited - if there are more
143 * than that then an error is printed, and this function returns
144 * CONFIG_SYS_MAXARGS, with argv[] set up to that point.
145 *
146 * @line: Command line to parse
147 * @args: Array to hold arguments
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100148 * Return: number of arguments
Simon Glass18d66532014-04-10 20:01:25 -0600149 */
Simon Glasse1bf8242014-04-10 20:01:27 -0600150int cli_simple_parse_line(char *line, char *argv[]);
Simon Glass18d66532014-04-10 20:01:25 -0600151
Masahiro Yamada0f925822015-08-12 07:31:55 +0900152#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glassaffb2152014-04-10 20:01:35 -0600153/**
154 * cli_process_fdt() - process the boot command from the FDT
155 *
156 * If bootcmmd is defined in the /config node of the FDT, we use that
157 * as the boot command. Further, if bootsecure is set to 1 (in the same
158 * node) then we return true, indicating that the command should be executed
159 * as securely as possible, avoiding the CLI parser.
160 *
161 * @cmdp: On entry, the command that will be executed if the FDT does
162 * not have a command. Returns the command to execute after
163 * checking the FDT.
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100164 * Return: true to execute securely, else false
Simon Glassaffb2152014-04-10 20:01:35 -0600165 */
166bool cli_process_fdt(const char **cmdp);
167
168/** cli_secure_boot_cmd() - execute a command as securely as possible
169 *
170 * This avoids using the parser, thus executing the command with the
171 * smallest amount of code. Parameters are not supported.
172 */
173void cli_secure_boot_cmd(const char *cmd);
174#else
175static inline bool cli_process_fdt(const char **cmdp)
176{
177 return false;
178}
179
180static inline void cli_secure_boot_cmd(const char *cmd)
181{
182}
183#endif /* CONFIG_OF_CONTROL */
184
Simon Glassc1bb2cd2014-04-10 20:01:34 -0600185/**
186 * Go into the command loop
187 *
188 * This will return if we get a timeout waiting for a command, but only for
189 * the simple parser (not hush). See CONFIG_BOOT_RETRY_TIME.
190 */
191void cli_loop(void);
192
193/** Set up the command line interpreter ready for action */
194void cli_init(void);
195
Simon Glass6493ccc2014-04-10 20:01:26 -0600196#define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
Simon Glassb08e9d42023-01-06 08:52:20 -0600197#define CTL_CH(c) ((c) - 'a' + 1)
198
199/**
200 * cli_ch_init() - Set up the initial state to process input characters
201 *
202 * @cch: State to set up
203 */
204void cli_ch_init(struct cli_ch_state *cch);
205
206/**
207 * cli_ch_process() - Process an input character
208 *
209 * When @ichar is 0, this function returns any characters from an invalid escape
210 * sequence which are still pending in the buffer
211 *
212 * Otherwise it processes the input character. If it is an escape character,
213 * then an escape sequence is started and the function returns 0. If we are in
214 * the middle of an escape sequence, the character is processed and may result
215 * in returning 0 (if more characters are needed) or a valid character (if
216 * @ichar finishes the sequence).
217 *
218 * If @ichar is a valid character and there is no escape sequence in progress,
219 * then it is returned as is.
220 *
221 * If the Enter key is pressed, '\n' is returned.
222 *
223 * Usage should be like this::
224 *
225 * struct cli_ch_state cch;
226 *
227 * cli_ch_init(cch);
228 * do
229 * {
230 * int ichar, ch;
231 *
232 * ichar = cli_ch_process(cch, 0);
233 * if (!ichar) {
234 * ch = getchar();
235 * ichar = cli_ch_process(cch, ch);
236 * }
237 * (handle the ichar character)
238 * } while (!done)
239 *
240 * If tstc() is used to look for keypresses, this function can be called with
241 * @ichar set to -ETIMEDOUT if there is no character after 5-10ms. This allows
242 * the ambgiuity between the Escape key and the arrow keys (which generate an
243 * escape character followed by other characters) to be resolved.
244 *
245 * @cch: Current state
246 * @ichar: Input character to process, or 0 if none, or -ETIMEDOUT if no
247 * character has been received within a small number of milliseconds (this
248 * cancels any existing escape sequence and allows pressing the Escape key to
249 * work)
250 * Returns: Resulting input character after processing, 0 if none, '\e' if
251 * an existing escape sequence was cancelled
252 */
253int cli_ch_process(struct cli_ch_state *cch, int ichar);
Simon Glass6493ccc2014-04-10 20:01:26 -0600254
Simon Glasse5509ce2023-10-01 19:13:13 -0600255/**
256 * cread_line_process_ch() - Process a character for line input
257 *
258 * @cls: CLI line state
259 * @ichar: Character to process
260 * Return: 0 if input is complete, with line in cls->buf, -EINTR if input was
261 * cancelled with Ctrl-C, -EAGAIN if more characters are needed
262 */
263int cread_line_process_ch(struct cli_line_state *cls, char ichar);
264
Simon Glass33eb0b92023-10-01 19:13:06 -0600265/** cread_print_hist_list() - Print the command-line history list */
266void cread_print_hist_list(void);
267
Simon Glass18d66532014-04-10 20:01:25 -0600268#endif