command.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * (C) Copyright 2000-2009
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. */
  6. /*
  7. * Definitions for Command Processor
  8. */
  9. #ifndef __COMMAND_H
  10. #define __COMMAND_H
  11. #include <linker_lists.h>
  12. #ifndef NULL
  13. #define NULL 0
  14. #endif
  15. /* Default to a width of 8 characters for help message command width */
  16. #ifndef CONFIG_SYS_HELP_CMD_WIDTH
  17. #define CONFIG_SYS_HELP_CMD_WIDTH 8
  18. #endif
  19. #ifndef __ASSEMBLY__
  20. /*
  21. * Monitor Command Table
  22. */
  23. struct cmd_tbl_s {
  24. char *name; /* Command Name */
  25. int maxargs; /* maximum number of arguments */
  26. int repeatable; /* autorepeat allowed? */
  27. /* Implementation function */
  28. int (*cmd)(struct cmd_tbl_s *, int, int, char * const []);
  29. char *usage; /* Usage message (short) */
  30. #ifdef CONFIG_SYS_LONGHELP
  31. char *help; /* Help message (long) */
  32. #endif
  33. #ifdef CONFIG_AUTO_COMPLETE
  34. /* do auto completion on the arguments */
  35. int (*complete)(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]);
  36. #endif
  37. };
  38. typedef struct cmd_tbl_s cmd_tbl_t;
  39. #if defined(CONFIG_CMD_RUN)
  40. extern int do_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
  41. #endif
  42. /* common/command.c */
  43. int _do_help (cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t * cmdtp, int
  44. flag, int argc, char * const argv[]);
  45. cmd_tbl_t *find_cmd(const char *cmd);
  46. cmd_tbl_t *find_cmd_tbl (const char *cmd, cmd_tbl_t *table, int table_len);
  47. extern int cmd_usage(const cmd_tbl_t *cmdtp);
  48. #ifdef CONFIG_AUTO_COMPLETE
  49. extern int var_complete(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]);
  50. extern int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp);
  51. #endif
  52. /**
  53. * cmd_process_error() - report and process a possible error
  54. *
  55. * @cmdtp: Command which caused the error
  56. * @err: Error code (0 if none, -ve for error, like -EIO)
  57. * @return 0 if there is not error, 1 (CMD_RET_FAILURE) if an error is found
  58. */
  59. int cmd_process_error(cmd_tbl_t *cmdtp, int err);
  60. /*
  61. * Monitor Command
  62. *
  63. * All commands use a common argument format:
  64. *
  65. * void function (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
  66. */
  67. #if defined(CONFIG_CMD_MEMORY) || \
  68. defined(CONFIG_CMD_I2C) || \
  69. defined(CONFIG_CMD_ITEST) || \
  70. defined(CONFIG_CMD_PCI)
  71. #define CMD_DATA_SIZE
  72. extern int cmd_get_data_size(char* arg, int default_size);
  73. #endif
  74. #ifdef CONFIG_CMD_BOOTD
  75. extern int do_bootd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
  76. #endif
  77. #ifdef CONFIG_CMD_BOOTM
  78. extern int do_bootm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
  79. extern int bootm_maybe_autostart(cmd_tbl_t *cmdtp, const char *cmd);
  80. #else
  81. static inline int bootm_maybe_autostart(cmd_tbl_t *cmdtp, const char *cmd)
  82. {
  83. return 0;
  84. }
  85. #endif
  86. extern int do_bootz(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
  87. extern int do_booti(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
  88. extern int common_diskboot(cmd_tbl_t *cmdtp, const char *intf, int argc,
  89. char *const argv[]);
  90. extern int do_reset(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
  91. extern int do_poweroff(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
  92. extern unsigned long do_go_exec(ulong (*entry)(int, char * const []), int argc,
  93. char * const argv[]);
  94. /*
  95. * Error codes that commands return to cmd_process(). We use the standard 0
  96. * and 1 for success and failure, but add one more case - failure with a
  97. * request to call cmd_usage(). But the cmd_process() function handles
  98. * CMD_RET_USAGE itself and after calling cmd_usage() it will return 1.
  99. * This is just a convenience for commands to avoid them having to call
  100. * cmd_usage() all over the place.
  101. */
  102. enum command_ret_t {
  103. CMD_RET_SUCCESS, /* 0 = Success */
  104. CMD_RET_FAILURE, /* 1 = Failure */
  105. CMD_RET_USAGE = -1, /* Failure, please report 'usage' error */
  106. };
  107. /**
  108. * Process a command with arguments. We look up the command and execute it
  109. * if valid. Otherwise we print a usage message.
  110. *
  111. * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
  112. * @param argc Number of arguments (arg 0 must be the command text)
  113. * @param argv Arguments
  114. * @param repeatable This function sets this to 0 if the command is not
  115. * repeatable. If the command is repeatable, the value
  116. * is left unchanged.
  117. * @param ticks If ticks is not null, this function set it to the
  118. * number of ticks the command took to complete.
  119. * @return 0 if the command succeeded, 1 if it failed
  120. */
  121. int cmd_process(int flag, int argc, char * const argv[],
  122. int *repeatable, unsigned long *ticks);
  123. void fixup_cmdtable(cmd_tbl_t *cmdtp, int size);
  124. /**
  125. * board_run_command() - Fallback function to execute a command
  126. *
  127. * When no command line features are enabled in U-Boot, this function is
  128. * called to execute a command. Typically the function can look at the
  129. * command and perform a few very specific tasks, such as booting the
  130. * system in a particular way.
  131. *
  132. * This function is only used when CONFIG_CMDLINE is not enabled.
  133. *
  134. * In normal situations this function should not return, since U-Boot will
  135. * simply hang.
  136. *
  137. * @cmdline: Command line string to execute
  138. * @return 0 if OK, 1 for error
  139. */
  140. int board_run_command(const char *cmdline);
  141. #endif /* __ASSEMBLY__ */
  142. /*
  143. * Command Flags:
  144. */
  145. #define CMD_FLAG_REPEAT 0x0001 /* repeat last command */
  146. #define CMD_FLAG_BOOTD 0x0002 /* command is from bootd */
  147. #define CMD_FLAG_ENV 0x0004 /* command is from the environment */
  148. #ifdef CONFIG_AUTO_COMPLETE
  149. # define _CMD_COMPLETE(x) x,
  150. #else
  151. # define _CMD_COMPLETE(x)
  152. #endif
  153. #ifdef CONFIG_SYS_LONGHELP
  154. # define _CMD_HELP(x) x,
  155. #else
  156. # define _CMD_HELP(x)
  157. #endif
  158. #ifdef CONFIG_CMDLINE
  159. #define U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \
  160. _usage, _help, _comp) \
  161. { #_name, _maxargs, _rep, _cmd, _usage, \
  162. _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
  163. #define U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, _comp) \
  164. ll_entry_declare(cmd_tbl_t, _name, cmd) = \
  165. U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \
  166. _usage, _help, _comp);
  167. #else
  168. #define U_BOOT_SUBCMD_START(name) static cmd_tbl_t name[] = {};
  169. #define U_BOOT_SUBCMD_END
  170. #define _CMD_REMOVE(_name, _cmd) \
  171. int __remove_ ## _name(void) \
  172. { \
  173. if (0) \
  174. _cmd(NULL, 0, 0, NULL); \
  175. return 0; \
  176. }
  177. #define U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, \
  178. _help, _comp) \
  179. { #_name, _maxargs, _rep, 0 ? _cmd : NULL, _usage, \
  180. _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
  181. #define U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, \
  182. _comp) \
  183. _CMD_REMOVE(sub_ ## _name, _cmd)
  184. #endif /* CONFIG_CMDLINE */
  185. #define U_BOOT_CMD(_name, _maxargs, _rep, _cmd, _usage, _help) \
  186. U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, NULL)
  187. #define U_BOOT_CMD_MKENT(_name, _maxargs, _rep, _cmd, _usage, _help) \
  188. U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \
  189. _usage, _help, NULL)
  190. #endif /* __COMMAND_H */