command.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 (CMD_RET_SUCCESS) if there is not error,
  58. * 1 (CMD_RET_FAILURE) if an error is found
  59. * -1 (CMD_RET_USAGE) if 'usage' error is found
  60. */
  61. int cmd_process_error(cmd_tbl_t *cmdtp, int err);
  62. /*
  63. * Monitor Command
  64. *
  65. * All commands use a common argument format:
  66. *
  67. * void function (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
  68. */
  69. #if defined(CONFIG_CMD_MEMORY) || \
  70. defined(CONFIG_CMD_I2C) || \
  71. defined(CONFIG_CMD_ITEST) || \
  72. defined(CONFIG_CMD_PCI)
  73. #define CMD_DATA_SIZE
  74. extern int cmd_get_data_size(char* arg, int default_size);
  75. #endif
  76. #ifdef CONFIG_CMD_BOOTD
  77. extern int do_bootd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
  78. #endif
  79. #ifdef CONFIG_CMD_BOOTM
  80. extern int do_bootm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
  81. extern int bootm_maybe_autostart(cmd_tbl_t *cmdtp, const char *cmd);
  82. #else
  83. static inline int bootm_maybe_autostart(cmd_tbl_t *cmdtp, const char *cmd)
  84. {
  85. return 0;
  86. }
  87. #endif
  88. extern int do_bootz(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
  89. extern int do_booti(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
  90. extern int common_diskboot(cmd_tbl_t *cmdtp, const char *intf, int argc,
  91. char *const argv[]);
  92. extern int do_reset(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
  93. extern int do_poweroff(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
  94. extern unsigned long do_go_exec(ulong (*entry)(int, char * const []), int argc,
  95. char * const argv[]);
  96. /*
  97. * Error codes that commands return to cmd_process(). We use the standard 0
  98. * and 1 for success and failure, but add one more case - failure with a
  99. * request to call cmd_usage(). But the cmd_process() function handles
  100. * CMD_RET_USAGE itself and after calling cmd_usage() it will return 1.
  101. * This is just a convenience for commands to avoid them having to call
  102. * cmd_usage() all over the place.
  103. */
  104. enum command_ret_t {
  105. CMD_RET_SUCCESS, /* 0 = Success */
  106. CMD_RET_FAILURE, /* 1 = Failure */
  107. CMD_RET_USAGE = -1, /* Failure, please report 'usage' error */
  108. };
  109. /**
  110. * Process a command with arguments. We look up the command and execute it
  111. * if valid. Otherwise we print a usage message.
  112. *
  113. * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
  114. * @param argc Number of arguments (arg 0 must be the command text)
  115. * @param argv Arguments
  116. * @param repeatable This function sets this to 0 if the command is not
  117. * repeatable. If the command is repeatable, the value
  118. * is left unchanged.
  119. * @param ticks If ticks is not null, this function set it to the
  120. * number of ticks the command took to complete.
  121. * @return 0 if the command succeeded, 1 if it failed
  122. */
  123. int cmd_process(int flag, int argc, char * const argv[],
  124. int *repeatable, unsigned long *ticks);
  125. void fixup_cmdtable(cmd_tbl_t *cmdtp, int size);
  126. /**
  127. * board_run_command() - Fallback function to execute a command
  128. *
  129. * When no command line features are enabled in U-Boot, this function is
  130. * called to execute a command. Typically the function can look at the
  131. * command and perform a few very specific tasks, such as booting the
  132. * system in a particular way.
  133. *
  134. * This function is only used when CONFIG_CMDLINE is not enabled.
  135. *
  136. * In normal situations this function should not return, since U-Boot will
  137. * simply hang.
  138. *
  139. * @cmdline: Command line string to execute
  140. * @return 0 if OK, 1 for error
  141. */
  142. int board_run_command(const char *cmdline);
  143. #endif /* __ASSEMBLY__ */
  144. /*
  145. * Command Flags:
  146. */
  147. #define CMD_FLAG_REPEAT 0x0001 /* repeat last command */
  148. #define CMD_FLAG_BOOTD 0x0002 /* command is from bootd */
  149. #define CMD_FLAG_ENV 0x0004 /* command is from the environment */
  150. #ifdef CONFIG_AUTO_COMPLETE
  151. # define _CMD_COMPLETE(x) x,
  152. #else
  153. # define _CMD_COMPLETE(x)
  154. #endif
  155. #ifdef CONFIG_SYS_LONGHELP
  156. # define _CMD_HELP(x) x,
  157. #else
  158. # define _CMD_HELP(x)
  159. #endif
  160. #ifdef CONFIG_CMDLINE
  161. #define U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \
  162. _usage, _help, _comp) \
  163. { #_name, _maxargs, _rep, _cmd, _usage, \
  164. _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
  165. #define U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, _comp) \
  166. ll_entry_declare(cmd_tbl_t, _name, cmd) = \
  167. U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \
  168. _usage, _help, _comp);
  169. #else
  170. #define U_BOOT_SUBCMD_START(name) static cmd_tbl_t name[] = {};
  171. #define U_BOOT_SUBCMD_END
  172. #define _CMD_REMOVE(_name, _cmd) \
  173. int __remove_ ## _name(void) \
  174. { \
  175. if (0) \
  176. _cmd(NULL, 0, 0, NULL); \
  177. return 0; \
  178. }
  179. #define U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, \
  180. _help, _comp) \
  181. { #_name, _maxargs, _rep, 0 ? _cmd : NULL, _usage, \
  182. _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
  183. #define U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, \
  184. _comp) \
  185. _CMD_REMOVE(sub_ ## _name, _cmd)
  186. #endif /* CONFIG_CMDLINE */
  187. #define U_BOOT_CMD(_name, _maxargs, _rep, _cmd, _usage, _help) \
  188. U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, NULL)
  189. #define U_BOOT_CMD_MKENT(_name, _maxargs, _rep, _cmd, _usage, _help) \
  190. U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \
  191. _usage, _help, NULL)
  192. #endif /* __COMMAND_H */