1*83d290c5STom Rini /* SPDX-License-Identifier: GPL-2.0+ */
218d66533SSimon Glass /*
318d66533SSimon Glass * (C) Copyright 2014 Google, Inc
418d66533SSimon Glass * Simon Glass <sjg@chromium.org>
518d66533SSimon Glass */
618d66533SSimon Glass
718d66533SSimon Glass #ifndef __CLI_H
818d66533SSimon Glass #define __CLI_H
918d66533SSimon Glass
1018d66533SSimon Glass /**
1118d66533SSimon Glass * Go into the command loop
1218d66533SSimon Glass *
1318d66533SSimon Glass * This will return if we get a timeout waiting for a command. See
1418d66533SSimon Glass * CONFIG_BOOT_RETRY_TIME.
1518d66533SSimon Glass */
16c1bb2cd0SSimon Glass void cli_simple_loop(void);
1718d66533SSimon Glass
1818d66533SSimon Glass /**
1918d66533SSimon Glass * cli_simple_run_command() - Execute a command with the simple CLI
2018d66533SSimon Glass *
2118d66533SSimon Glass * @cmd: String containing the command to execute
2218d66533SSimon Glass * @flag Flag value - see CMD_FLAG_...
2318d66533SSimon Glass * @return 1 - command executed, repeatable
2418d66533SSimon Glass * 0 - command executed but not repeatable, interrupted commands are
2518d66533SSimon Glass * always considered not repeatable
2618d66533SSimon Glass * -1 - not executed (unrecognized, bootd recursion or too many args)
2718d66533SSimon Glass * (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is
2818d66533SSimon Glass * considered unrecognized)
2918d66533SSimon Glass */
3018d66533SSimon Glass int cli_simple_run_command(const char *cmd, int flag);
3118d66533SSimon Glass
3218d66533SSimon Glass /**
33a06be2d0SHans de Goede * cli_simple_process_macros() - Expand $() and ${} format env. variables
34a06be2d0SHans de Goede *
35a06be2d0SHans de Goede * @param input Input string possible containing $() / ${} vars
36a06be2d0SHans de Goede * @param output Output string with $() / ${} vars expanded
37a06be2d0SHans de Goede */
38a06be2d0SHans de Goede void cli_simple_process_macros(const char *input, char *output);
39a06be2d0SHans de Goede
40a06be2d0SHans de Goede /**
4118d66533SSimon Glass * cli_simple_run_command_list() - Execute a list of command
4218d66533SSimon Glass *
4318d66533SSimon Glass * The commands should be separated by ; or \n and will be executed
4418d66533SSimon Glass * by the built-in parser.
4518d66533SSimon Glass *
4618d66533SSimon Glass * This function cannot take a const char * for the command, since if it
4718d66533SSimon Glass * finds newlines in the string, it replaces them with \0.
4818d66533SSimon Glass *
4918d66533SSimon Glass * @param cmd String containing list of commands
5018d66533SSimon Glass * @param flag Execution flags (CMD_FLAG_...)
5118d66533SSimon Glass * @return 0 on success, or != 0 on error.
5218d66533SSimon Glass */
5318d66533SSimon Glass int cli_simple_run_command_list(char *cmd, int flag);
5418d66533SSimon Glass
5518d66533SSimon Glass /**
5618d66533SSimon Glass * cli_readline() - read a line into the console_buffer
5718d66533SSimon Glass *
5818d66533SSimon Glass * This is a convenience function which calls cli_readline_into_buffer().
5918d66533SSimon Glass *
6018d66533SSimon Glass * @prompt: Prompt to display
6118d66533SSimon Glass * @return command line length excluding terminator, or -ve on error
6218d66533SSimon Glass */
63e1bf824dSSimon Glass int cli_readline(const char *const prompt);
6418d66533SSimon Glass
6518d66533SSimon Glass /**
6618d66533SSimon Glass * readline_into_buffer() - read a line into a buffer
6718d66533SSimon Glass *
6818d66533SSimon Glass * Display the prompt, then read a command line into @buffer. The
6918d66533SSimon Glass * maximum line length is CONFIG_SYS_CBSIZE including a \0 terminator, which
7018d66533SSimon Glass * will always be added.
7118d66533SSimon Glass *
7218d66533SSimon Glass * The command is echoed as it is typed. Command editing is supported if
7318d66533SSimon Glass * CONFIG_CMDLINE_EDITING is defined. Tab auto-complete is supported if
7418d66533SSimon Glass * CONFIG_AUTO_COMPLETE is defined. If CONFIG_BOOT_RETRY_TIME is defined,
7518d66533SSimon Glass * then a timeout will be applied.
7618d66533SSimon Glass *
7718d66533SSimon Glass * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
7818d66533SSimon Glass * time out when time goes past endtime (timebase time in ticks).
7918d66533SSimon Glass *
8018d66533SSimon Glass * @prompt: Prompt to display
8118d66533SSimon Glass * @buffer: Place to put the line that is entered
8218d66533SSimon Glass * @timeout: Timeout in milliseconds, 0 if none
8318d66533SSimon Glass * @return command line length excluding terminator, or -ve on error: of the
8418d66533SSimon Glass * timeout is exceeded (either CONFIG_BOOT_RETRY_TIME or the timeout
8518d66533SSimon Glass * parameter), then -2 is returned. If a break is detected (Ctrl-C) then
8618d66533SSimon Glass * -1 is returned.
8718d66533SSimon Glass */
88e1bf824dSSimon Glass int cli_readline_into_buffer(const char *const prompt, char *buffer,
89e1bf824dSSimon Glass int timeout);
9018d66533SSimon Glass
9118d66533SSimon Glass /**
9218d66533SSimon Glass * parse_line() - split a command line down into separate arguments
9318d66533SSimon Glass *
9418d66533SSimon Glass * The argv[] array is filled with pointers into @line, and each argument
9518d66533SSimon Glass * is terminated by \0 (i.e. @line is changed in the process unless there
9618d66533SSimon Glass * is only one argument).
9718d66533SSimon Glass *
9818d66533SSimon Glass * #argv is terminated by a NULL after the last argument pointer.
9918d66533SSimon Glass *
10018d66533SSimon Glass * At most CONFIG_SYS_MAXARGS arguments are permited - if there are more
10118d66533SSimon Glass * than that then an error is printed, and this function returns
10218d66533SSimon Glass * CONFIG_SYS_MAXARGS, with argv[] set up to that point.
10318d66533SSimon Glass *
10418d66533SSimon Glass * @line: Command line to parse
10518d66533SSimon Glass * @args: Array to hold arguments
10618d66533SSimon Glass * @return number of arguments
10718d66533SSimon Glass */
108e1bf824dSSimon Glass int cli_simple_parse_line(char *line, char *argv[]);
10918d66533SSimon Glass
1100f925822SMasahiro Yamada #if CONFIG_IS_ENABLED(OF_CONTROL)
111affb2156SSimon Glass /**
112affb2156SSimon Glass * cli_process_fdt() - process the boot command from the FDT
113affb2156SSimon Glass *
114affb2156SSimon Glass * If bootcmmd is defined in the /config node of the FDT, we use that
115affb2156SSimon Glass * as the boot command. Further, if bootsecure is set to 1 (in the same
116affb2156SSimon Glass * node) then we return true, indicating that the command should be executed
117affb2156SSimon Glass * as securely as possible, avoiding the CLI parser.
118affb2156SSimon Glass *
119affb2156SSimon Glass * @cmdp: On entry, the command that will be executed if the FDT does
120affb2156SSimon Glass * not have a command. Returns the command to execute after
121affb2156SSimon Glass * checking the FDT.
122affb2156SSimon Glass * @return true to execute securely, else false
123affb2156SSimon Glass */
124affb2156SSimon Glass bool cli_process_fdt(const char **cmdp);
125affb2156SSimon Glass
126affb2156SSimon Glass /** cli_secure_boot_cmd() - execute a command as securely as possible
127affb2156SSimon Glass *
128affb2156SSimon Glass * This avoids using the parser, thus executing the command with the
129affb2156SSimon Glass * smallest amount of code. Parameters are not supported.
130affb2156SSimon Glass */
131affb2156SSimon Glass void cli_secure_boot_cmd(const char *cmd);
132affb2156SSimon Glass #else
cli_process_fdt(const char ** cmdp)133affb2156SSimon Glass static inline bool cli_process_fdt(const char **cmdp)
134affb2156SSimon Glass {
135affb2156SSimon Glass return false;
136affb2156SSimon Glass }
137affb2156SSimon Glass
cli_secure_boot_cmd(const char * cmd)138affb2156SSimon Glass static inline void cli_secure_boot_cmd(const char *cmd)
139affb2156SSimon Glass {
140affb2156SSimon Glass }
141affb2156SSimon Glass #endif /* CONFIG_OF_CONTROL */
142affb2156SSimon Glass
143c1bb2cd0SSimon Glass /**
144c1bb2cd0SSimon Glass * Go into the command loop
145c1bb2cd0SSimon Glass *
146c1bb2cd0SSimon Glass * This will return if we get a timeout waiting for a command, but only for
147c1bb2cd0SSimon Glass * the simple parser (not hush). See CONFIG_BOOT_RETRY_TIME.
148c1bb2cd0SSimon Glass */
149c1bb2cd0SSimon Glass void cli_loop(void);
150c1bb2cd0SSimon Glass
151c1bb2cd0SSimon Glass /** Set up the command line interpreter ready for action */
152c1bb2cd0SSimon Glass void cli_init(void);
153c1bb2cd0SSimon Glass
1546493ccc7SSimon Glass #define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
1556493ccc7SSimon Glass
15618d66533SSimon Glass #endif
157