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