command.h 12 KB

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