xref: /OK3568_Linux_fs/u-boot/include/cli.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * (C) Copyright 2014 Google, Inc
3*4882a593Smuzhiyun  * Simon Glass <sjg@chromium.org>
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #ifndef __CLI_H
9*4882a593Smuzhiyun #define __CLI_H
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun /**
12*4882a593Smuzhiyun  * Go into the command loop
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * This will return if we get a timeout waiting for a command. See
15*4882a593Smuzhiyun  * CONFIG_BOOT_RETRY_TIME.
16*4882a593Smuzhiyun  */
17*4882a593Smuzhiyun void cli_simple_loop(void);
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun /**
20*4882a593Smuzhiyun  * cli_simple_run_command() - Execute a command with the simple CLI
21*4882a593Smuzhiyun  *
22*4882a593Smuzhiyun  * @cmd:	String containing the command to execute
23*4882a593Smuzhiyun  * @flag	Flag value - see CMD_FLAG_...
24*4882a593Smuzhiyun  * @return 1  - command executed, repeatable
25*4882a593Smuzhiyun  *	0  - command executed but not repeatable, interrupted commands are
26*4882a593Smuzhiyun  *	     always considered not repeatable
27*4882a593Smuzhiyun  *	-1 - not executed (unrecognized, bootd recursion or too many args)
28*4882a593Smuzhiyun  *           (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is
29*4882a593Smuzhiyun  *           considered unrecognized)
30*4882a593Smuzhiyun  */
31*4882a593Smuzhiyun int cli_simple_run_command(const char *cmd, int flag);
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun /**
34*4882a593Smuzhiyun  * cli_simple_process_macros() - Expand $() and ${} format env. variables
35*4882a593Smuzhiyun  *
36*4882a593Smuzhiyun  * @param input		Input string possible containing $() / ${} vars
37*4882a593Smuzhiyun  * @param output	Output string with $() / ${} vars expanded
38*4882a593Smuzhiyun  */
39*4882a593Smuzhiyun void cli_simple_process_macros(const char *input, char *output);
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun /**
42*4882a593Smuzhiyun  * cli_simple_run_command_list() - Execute a list of command
43*4882a593Smuzhiyun  *
44*4882a593Smuzhiyun  * The commands should be separated by ; or \n and will be executed
45*4882a593Smuzhiyun  * by the built-in parser.
46*4882a593Smuzhiyun  *
47*4882a593Smuzhiyun  * This function cannot take a const char * for the command, since if it
48*4882a593Smuzhiyun  * finds newlines in the string, it replaces them with \0.
49*4882a593Smuzhiyun  *
50*4882a593Smuzhiyun  * @param cmd	String containing list of commands
51*4882a593Smuzhiyun  * @param flag	Execution flags (CMD_FLAG_...)
52*4882a593Smuzhiyun  * @return 0 on success, or != 0 on error.
53*4882a593Smuzhiyun  */
54*4882a593Smuzhiyun int cli_simple_run_command_list(char *cmd, int flag);
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun /**
57*4882a593Smuzhiyun  * cli_readline() - read a line into the console_buffer
58*4882a593Smuzhiyun  *
59*4882a593Smuzhiyun  * This is a convenience function which calls cli_readline_into_buffer().
60*4882a593Smuzhiyun  *
61*4882a593Smuzhiyun  * @prompt: Prompt to display
62*4882a593Smuzhiyun  * @return command line length excluding terminator, or -ve on error
63*4882a593Smuzhiyun  */
64*4882a593Smuzhiyun int cli_readline(const char *const prompt);
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun /**
67*4882a593Smuzhiyun  * readline_into_buffer() - read a line into a buffer
68*4882a593Smuzhiyun  *
69*4882a593Smuzhiyun  * Display the prompt, then read a command line into @buffer. The
70*4882a593Smuzhiyun  * maximum line length is CONFIG_SYS_CBSIZE including a \0 terminator, which
71*4882a593Smuzhiyun  * will always be added.
72*4882a593Smuzhiyun  *
73*4882a593Smuzhiyun  * The command is echoed as it is typed. Command editing is supported if
74*4882a593Smuzhiyun  * CONFIG_CMDLINE_EDITING is defined. Tab auto-complete is supported if
75*4882a593Smuzhiyun  * CONFIG_AUTO_COMPLETE is defined. If CONFIG_BOOT_RETRY_TIME is defined,
76*4882a593Smuzhiyun  * then a timeout will be applied.
77*4882a593Smuzhiyun  *
78*4882a593Smuzhiyun  * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
79*4882a593Smuzhiyun  * time out when time goes past endtime (timebase time in ticks).
80*4882a593Smuzhiyun  *
81*4882a593Smuzhiyun  * @prompt:	Prompt to display
82*4882a593Smuzhiyun  * @buffer:	Place to put the line that is entered
83*4882a593Smuzhiyun  * @timeout:	Timeout in milliseconds, 0 if none
84*4882a593Smuzhiyun  * @return command line length excluding terminator, or -ve on error: of the
85*4882a593Smuzhiyun  * timeout is exceeded (either CONFIG_BOOT_RETRY_TIME or the timeout
86*4882a593Smuzhiyun  * parameter), then -2 is returned. If a break is detected (Ctrl-C) then
87*4882a593Smuzhiyun  * -1 is returned.
88*4882a593Smuzhiyun  */
89*4882a593Smuzhiyun int cli_readline_into_buffer(const char *const prompt, char *buffer,
90*4882a593Smuzhiyun 				int timeout);
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun /**
93*4882a593Smuzhiyun  * parse_line() - split a command line down into separate arguments
94*4882a593Smuzhiyun  *
95*4882a593Smuzhiyun  * The argv[] array is filled with pointers into @line, and each argument
96*4882a593Smuzhiyun  * is terminated by \0 (i.e. @line is changed in the process unless there
97*4882a593Smuzhiyun  * is only one argument).
98*4882a593Smuzhiyun  *
99*4882a593Smuzhiyun  * #argv is terminated by a NULL after the last argument pointer.
100*4882a593Smuzhiyun  *
101*4882a593Smuzhiyun  * At most CONFIG_SYS_MAXARGS arguments are permited - if there are more
102*4882a593Smuzhiyun  * than that then an error is printed, and this function returns
103*4882a593Smuzhiyun  * CONFIG_SYS_MAXARGS, with argv[] set up to that point.
104*4882a593Smuzhiyun  *
105*4882a593Smuzhiyun  * @line:	Command line to parse
106*4882a593Smuzhiyun  * @args:	Array to hold arguments
107*4882a593Smuzhiyun  * @return number of arguments
108*4882a593Smuzhiyun  */
109*4882a593Smuzhiyun int cli_simple_parse_line(char *line, char *argv[]);
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun #if CONFIG_IS_ENABLED(OF_CONTROL)
112*4882a593Smuzhiyun /**
113*4882a593Smuzhiyun  * cli_process_fdt() - process the boot command from the FDT
114*4882a593Smuzhiyun  *
115*4882a593Smuzhiyun  * If bootcmmd is defined in the /config node of the FDT, we use that
116*4882a593Smuzhiyun  * as the boot command. Further, if bootsecure is set to 1 (in the same
117*4882a593Smuzhiyun  * node) then we return true, indicating that the command should be executed
118*4882a593Smuzhiyun  * as securely as possible, avoiding the CLI parser.
119*4882a593Smuzhiyun  *
120*4882a593Smuzhiyun  * @cmdp:	On entry, the command that will be executed if the FDT does
121*4882a593Smuzhiyun  *		not have a command. Returns the command to execute after
122*4882a593Smuzhiyun  *		checking the FDT.
123*4882a593Smuzhiyun  * @return true to execute securely, else false
124*4882a593Smuzhiyun  */
125*4882a593Smuzhiyun bool cli_process_fdt(const char **cmdp);
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun /** cli_secure_boot_cmd() - execute a command as securely as possible
128*4882a593Smuzhiyun  *
129*4882a593Smuzhiyun  * This avoids using the parser, thus executing the command with the
130*4882a593Smuzhiyun  * smallest amount of code. Parameters are not supported.
131*4882a593Smuzhiyun  */
132*4882a593Smuzhiyun void cli_secure_boot_cmd(const char *cmd);
133*4882a593Smuzhiyun #else
cli_process_fdt(const char ** cmdp)134*4882a593Smuzhiyun static inline bool cli_process_fdt(const char **cmdp)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun 	return false;
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun 
cli_secure_boot_cmd(const char * cmd)139*4882a593Smuzhiyun static inline void cli_secure_boot_cmd(const char *cmd)
140*4882a593Smuzhiyun {
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun #endif /* CONFIG_OF_CONTROL */
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun /**
145*4882a593Smuzhiyun  * Go into the command loop
146*4882a593Smuzhiyun  *
147*4882a593Smuzhiyun  * This will return if we get a timeout waiting for a command, but only for
148*4882a593Smuzhiyun  * the simple parser (not hush). See CONFIG_BOOT_RETRY_TIME.
149*4882a593Smuzhiyun  */
150*4882a593Smuzhiyun void cli_loop(void);
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun /** Set up the command line interpreter ready for action */
153*4882a593Smuzhiyun void cli_init(void);
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun #define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun #endif
158