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