cli.h 5.1 KB

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