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