command.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 <env.h>
  12. #include <linker_lists.h>
  13. #ifndef NULL
  14. #define NULL 0
  15. #endif
  16. /* Default to a width of 8 characters for help message command width */
  17. #ifndef CONFIG_SYS_HELP_CMD_WIDTH
  18. #define CONFIG_SYS_HELP_CMD_WIDTH 10
  19. #endif
  20. #ifndef __ASSEMBLY__
  21. /*
  22. * Monitor Command Table
  23. */
  24. struct cmd_tbl_s {
  25. char *name; /* Command Name */
  26. int maxargs; /* maximum number of arguments */
  27. /*
  28. * Same as ->cmd() except the command
  29. * tells us if it can be repeated.
  30. * Replaces the old ->repeatable field
  31. * which was not able to make
  32. * repeatable property different for
  33. * the main command and sub-commands.
  34. */
  35. int (*cmd_rep)(struct cmd_tbl_s *cmd, int flags, int argc,
  36. char * const argv[], int *repeatable);
  37. /* Implementation function */
  38. int (*cmd)(struct cmd_tbl_s *, int, int, char * const []);
  39. char *usage; /* Usage message (short) */
  40. #ifdef CONFIG_SYS_LONGHELP
  41. char *help; /* Help message (long) */
  42. #endif
  43. #ifdef CONFIG_AUTO_COMPLETE
  44. /* do auto completion on the arguments */
  45. int (*complete)(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]);
  46. #endif
  47. };
  48. typedef struct cmd_tbl_s cmd_tbl_t;
  49. #if defined(CONFIG_CMD_RUN)
  50. extern int do_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
  51. #endif
  52. /* common/command.c */
  53. int _do_help (cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t * cmdtp, int
  54. flag, int argc, char * const argv[]);
  55. cmd_tbl_t *find_cmd(const char *cmd);
  56. cmd_tbl_t *find_cmd_tbl (const char *cmd, cmd_tbl_t *table, int table_len);
  57. int complete_subcmdv(cmd_tbl_t *cmdtp, int count, int argc,
  58. char * const argv[], char last_char, int maxv,
  59. char *cmdv[]);
  60. extern int cmd_usage(const cmd_tbl_t *cmdtp);
  61. /* Dummy ->cmd and ->cmd_rep wrappers. */
  62. int cmd_always_repeatable(cmd_tbl_t *cmdtp, int flag, int argc,
  63. char * const argv[], int *repeatable);
  64. int cmd_never_repeatable(cmd_tbl_t *cmdtp, int flag, int argc,
  65. char * const argv[], int *repeatable);
  66. int cmd_discard_repeatable(cmd_tbl_t *cmdtp, int flag, int argc,
  67. char * const argv[]);
  68. static inline bool cmd_is_repeatable(cmd_tbl_t *cmdtp)
  69. {
  70. return cmdtp->cmd_rep == cmd_always_repeatable;
  71. }
  72. #ifdef CONFIG_AUTO_COMPLETE
  73. extern int var_complete(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]);
  74. extern int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp);
  75. #endif
  76. /**
  77. * cmd_process_error() - report and process a possible error
  78. *
  79. * @cmdtp: Command which caused the error
  80. * @err: Error code (0 if none, -ve for error, like -EIO)
  81. * @return 0 (CMD_RET_SUCCESS) if there is not error,
  82. * 1 (CMD_RET_FAILURE) if an error is found
  83. * -1 (CMD_RET_USAGE) if 'usage' error is found
  84. */
  85. int cmd_process_error(cmd_tbl_t *cmdtp, int err);
  86. /*
  87. * Monitor Command
  88. *
  89. * All commands use a common argument format:
  90. *
  91. * void function (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
  92. */
  93. #if defined(CONFIG_CMD_MEMORY) || \
  94. defined(CONFIG_CMD_I2C) || \
  95. defined(CONFIG_CMD_ITEST) || \
  96. defined(CONFIG_CMD_PCI) || \
  97. defined(CONFIG_CMD_SETEXPR)
  98. #define CMD_DATA_SIZE
  99. extern int cmd_get_data_size(char* arg, int default_size);
  100. #endif
  101. #ifdef CONFIG_CMD_BOOTD
  102. extern int do_bootd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
  103. #endif
  104. #ifdef CONFIG_CMD_BOOTM
  105. extern int do_bootm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
  106. extern int bootm_maybe_autostart(cmd_tbl_t *cmdtp, const char *cmd);
  107. #else
  108. static inline int bootm_maybe_autostart(cmd_tbl_t *cmdtp, const char *cmd)
  109. {
  110. return 0;
  111. }
  112. #endif
  113. extern int do_bootz(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
  114. extern int do_booti(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
  115. extern int common_diskboot(cmd_tbl_t *cmdtp, const char *intf, int argc,
  116. char *const argv[]);
  117. extern int do_reset(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
  118. extern int do_poweroff(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
  119. extern unsigned long do_go_exec(ulong (*entry)(int, char * const []), int argc,
  120. char * const argv[]);
  121. #if defined(CONFIG_CMD_NVEDIT_EFI)
  122. extern int do_env_print_efi(cmd_tbl_t *cmdtp, int flag, int argc,
  123. char * const argv[]);
  124. extern int do_env_set_efi(cmd_tbl_t *cmdtp, int flag, int argc,
  125. char * const argv[]);
  126. #endif
  127. #ifdef CONFIG_CMD_BOOT_SLAVE
  128. extern int do_bootslave(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
  129. #endif
  130. /*
  131. * Error codes that commands return to cmd_process(). We use the standard 0
  132. * and 1 for success and failure, but add one more case - failure with a
  133. * request to call cmd_usage(). But the cmd_process() function handles
  134. * CMD_RET_USAGE itself and after calling cmd_usage() it will return 1.
  135. * This is just a convenience for commands to avoid them having to call
  136. * cmd_usage() all over the place.
  137. */
  138. enum command_ret_t {
  139. CMD_RET_SUCCESS, /* 0 = Success */
  140. CMD_RET_FAILURE, /* 1 = Failure */
  141. CMD_RET_USAGE = -1, /* Failure, please report 'usage' error */
  142. };
  143. /**
  144. * Process a command with arguments. We look up the command and execute it
  145. * if valid. Otherwise we print a usage message.
  146. *
  147. * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
  148. * @param argc Number of arguments (arg 0 must be the command text)
  149. * @param argv Arguments
  150. * @param repeatable This function sets this to 0 if the command is not
  151. * repeatable. If the command is repeatable, the value
  152. * is left unchanged.
  153. * @param ticks If ticks is not null, this function set it to the
  154. * number of ticks the command took to complete.
  155. * @return 0 if the command succeeded, 1 if it failed
  156. */
  157. int cmd_process(int flag, int argc, char * const argv[],
  158. int *repeatable, unsigned long *ticks);
  159. void fixup_cmdtable(cmd_tbl_t *cmdtp, int size);
  160. /**
  161. * board_run_command() - Fallback function to execute a command
  162. *
  163. * When no command line features are enabled in U-Boot, this function is
  164. * called to execute a command. Typically the function can look at the
  165. * command and perform a few very specific tasks, such as booting the
  166. * system in a particular way.
  167. *
  168. * This function is only used when CONFIG_CMDLINE is not enabled.
  169. *
  170. * In normal situations this function should not return, since U-Boot will
  171. * simply hang.
  172. *
  173. * @cmdline: Command line string to execute
  174. * @return 0 if OK, 1 for error
  175. */
  176. int board_run_command(const char *cmdline);
  177. int run_command(const char *cmd, int flag);
  178. int run_command_repeatable(const char *cmd, int flag);
  179. /**
  180. * Run a list of commands separated by ; or even \0
  181. *
  182. * Note that if 'len' is not -1, then the command does not need to be nul
  183. * terminated, Memory will be allocated for the command in that case.
  184. *
  185. * @param cmd List of commands to run, each separated bu semicolon
  186. * @param len Length of commands excluding terminator if known (-1 if not)
  187. * @param flag Execution flags (CMD_FLAG_...)
  188. * @return 0 on success, or != 0 on error.
  189. */
  190. int run_command_list(const char *cmd, int len, int flag);
  191. #endif /* __ASSEMBLY__ */
  192. /*
  193. * Command Flags:
  194. */
  195. #define CMD_FLAG_REPEAT 0x0001 /* repeat last command */
  196. #define CMD_FLAG_BOOTD 0x0002 /* command is from bootd */
  197. #define CMD_FLAG_ENV 0x0004 /* command is from the environment */
  198. #ifdef CONFIG_AUTO_COMPLETE
  199. # define _CMD_COMPLETE(x) x,
  200. #else
  201. # define _CMD_COMPLETE(x)
  202. #endif
  203. #ifdef CONFIG_SYS_LONGHELP
  204. # define _CMD_HELP(x) x,
  205. #else
  206. # define _CMD_HELP(x)
  207. #endif
  208. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  209. #define U_BOOT_SUBCMDS_RELOC(_cmdname) \
  210. static void _cmdname##_subcmds_reloc(void) \
  211. { \
  212. static int relocated; \
  213. \
  214. if (relocated) \
  215. return; \
  216. \
  217. fixup_cmdtable(_cmdname##_subcmds, \
  218. ARRAY_SIZE(_cmdname##_subcmds)); \
  219. relocated = 1; \
  220. }
  221. #else
  222. #define U_BOOT_SUBCMDS_RELOC(_cmdname) \
  223. static void _cmdname##_subcmds_reloc(void) { }
  224. #endif
  225. #define U_BOOT_SUBCMDS_DO_CMD(_cmdname) \
  226. static int do_##_cmdname(cmd_tbl_t *cmdtp, int flag, int argc, \
  227. char * const argv[], int *repeatable) \
  228. { \
  229. cmd_tbl_t *subcmd; \
  230. \
  231. _cmdname##_subcmds_reloc(); \
  232. \
  233. /* We need at least the cmd and subcmd names. */ \
  234. if (argc < 2 || argc > CONFIG_SYS_MAXARGS) \
  235. return CMD_RET_USAGE; \
  236. \
  237. subcmd = find_cmd_tbl(argv[1], _cmdname##_subcmds, \
  238. ARRAY_SIZE(_cmdname##_subcmds)); \
  239. if (!subcmd || argc - 1 > subcmd->maxargs) \
  240. return CMD_RET_USAGE; \
  241. \
  242. if (flag == CMD_FLAG_REPEAT && \
  243. !cmd_is_repeatable(subcmd)) \
  244. return CMD_RET_SUCCESS; \
  245. \
  246. return subcmd->cmd_rep(subcmd, flag, argc - 1, \
  247. argv + 1, repeatable); \
  248. }
  249. #ifdef CONFIG_AUTO_COMPLETE
  250. #define U_BOOT_SUBCMDS_COMPLETE(_cmdname) \
  251. static int complete_##_cmdname(int argc, char * const argv[], \
  252. char last_char, int maxv, \
  253. char *cmdv[]) \
  254. { \
  255. return complete_subcmdv(_cmdname##_subcmds, \
  256. ARRAY_SIZE(_cmdname##_subcmds), \
  257. argc - 1, argv + 1, last_char, \
  258. maxv, cmdv); \
  259. }
  260. #else
  261. #define U_BOOT_SUBCMDS_COMPLETE(_cmdname)
  262. #endif
  263. #define U_BOOT_SUBCMDS(_cmdname, ...) \
  264. static cmd_tbl_t _cmdname##_subcmds[] = { __VA_ARGS__ }; \
  265. U_BOOT_SUBCMDS_RELOC(_cmdname) \
  266. U_BOOT_SUBCMDS_DO_CMD(_cmdname) \
  267. U_BOOT_SUBCMDS_COMPLETE(_cmdname)
  268. #ifdef CONFIG_CMDLINE
  269. #define U_BOOT_CMDREP_MKENT_COMPLETE(_name, _maxargs, _cmd_rep, \
  270. _usage, _help, _comp) \
  271. { #_name, _maxargs, _cmd_rep, cmd_discard_repeatable, \
  272. _usage, _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
  273. #define U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \
  274. _usage, _help, _comp) \
  275. { #_name, _maxargs, \
  276. _rep ? cmd_always_repeatable : cmd_never_repeatable, \
  277. _cmd, _usage, _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
  278. #define U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, _comp) \
  279. ll_entry_declare(cmd_tbl_t, _name, cmd) = \
  280. U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \
  281. _usage, _help, _comp);
  282. #define U_BOOT_CMDREP_COMPLETE(_name, _maxargs, _cmd_rep, _usage, \
  283. _help, _comp) \
  284. ll_entry_declare(cmd_tbl_t, _name, cmd) = \
  285. U_BOOT_CMDREP_MKENT_COMPLETE(_name, _maxargs, _cmd_rep, \
  286. _usage, _help, _comp)
  287. #else
  288. #define U_BOOT_SUBCMD_START(name) static cmd_tbl_t name[] = {};
  289. #define U_BOOT_SUBCMD_END
  290. #define _CMD_REMOVE(_name, _cmd) \
  291. int __remove_ ## _name(void) \
  292. { \
  293. if (0) \
  294. _cmd(NULL, 0, 0, NULL); \
  295. return 0; \
  296. }
  297. #define U_BOOT_CMDREP_MKENT_COMPLETE(_name, _maxargs, _cmd_rep, \
  298. _usage, _help, _comp) \
  299. { #_name, _maxargs, 0 ? _cmd_rep : NULL, NULL, _usage, \
  300. _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
  301. #define U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, \
  302. _help, _comp) \
  303. { #_name, _maxargs, NULL, 0 ? _cmd : NULL, _usage, \
  304. _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
  305. #define U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, \
  306. _comp) \
  307. _CMD_REMOVE(sub_ ## _name, _cmd)
  308. #define U_BOOT_CMDREP_COMPLETE(_name, _maxargs, _cmd_rep, _usage, \
  309. _help, _comp) \
  310. _CMD_REMOVE(sub_ ## _name, _cmd_rep)
  311. #endif /* CONFIG_CMDLINE */
  312. #define U_BOOT_CMD(_name, _maxargs, _rep, _cmd, _usage, _help) \
  313. U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, NULL)
  314. #define U_BOOT_CMD_MKENT(_name, _maxargs, _rep, _cmd, _usage, _help) \
  315. U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \
  316. _usage, _help, NULL)
  317. #define U_BOOT_SUBCMD_MKENT_COMPLETE(_name, _maxargs, _rep, _do_cmd, \
  318. _comp) \
  319. U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _do_cmd, \
  320. "", "", _comp)
  321. #define U_BOOT_SUBCMD_MKENT(_name, _maxargs, _rep, _do_cmd) \
  322. U_BOOT_SUBCMD_MKENT_COMPLETE(_name, _maxargs, _rep, _do_cmd, \
  323. NULL)
  324. #define U_BOOT_CMD_WITH_SUBCMDS(_name, _usage, _help, ...) \
  325. U_BOOT_SUBCMDS(_name, __VA_ARGS__) \
  326. U_BOOT_CMDREP_COMPLETE(_name, CONFIG_SYS_MAXARGS, do_##_name, \
  327. _usage, _help, complete_##_name)
  328. #endif /* __COMMAND_H */