command.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. #define CMD_DATA_SIZE_ERR (-1)
  106. #define CMD_DATA_SIZE_STR (-2)
  107. /**
  108. * cmd_get_data_size() - Get the data-size specifier from a command
  109. *
  110. * This reads a '.x' size specifier appended to a command. For example 'md.b'
  111. * is the 'md' command with a '.b' specifier, meaning that the command should
  112. * use bytes.
  113. *
  114. * Valid characters are:
  115. *
  116. * b - byte
  117. * w - word (16 bits)
  118. * l - long (32 bits)
  119. * q - quad (64 bits)
  120. * s - string
  121. *
  122. * @arg: Pointers to the command to check. If a valid specifier is present it
  123. * will be the last character of the string, following a '.'
  124. * @default_size: Default size to return if there is no specifier
  125. * @return data size in bytes (1, 2, 4, 8) or CMD_DATA_SIZE_ERR for an invalid
  126. * character, or CMD_DATA_SIZE_STR for a string
  127. */
  128. int cmd_get_data_size(char *arg, int default_size);
  129. #endif
  130. #ifdef CONFIG_CMD_BOOTD
  131. extern int do_bootd(struct cmd_tbl *cmdtp, int flag, int argc,
  132. char *const argv[]);
  133. #endif
  134. #ifdef CONFIG_CMD_BOOTM
  135. extern int do_bootm(struct cmd_tbl *cmdtp, int flag, int argc,
  136. char *const argv[]);
  137. extern int bootm_maybe_autostart(struct cmd_tbl *cmdtp, const char *cmd);
  138. #else
  139. static inline int bootm_maybe_autostart(struct cmd_tbl *cmdtp, const char *cmd)
  140. {
  141. return 0;
  142. }
  143. #endif
  144. extern int do_bootz(struct cmd_tbl *cmdtp, int flag, int argc,
  145. char *const argv[]);
  146. extern int do_booti(struct cmd_tbl *cmdtp, int flag, int argc,
  147. char *const argv[]);
  148. extern int common_diskboot(struct cmd_tbl *cmdtp, const char *intf, int argc,
  149. char *const argv[]);
  150. extern int do_reset(struct cmd_tbl *cmdtp, int flag, int argc,
  151. char *const argv[]);
  152. extern int do_poweroff(struct cmd_tbl *cmdtp, int flag, int argc,
  153. char *const argv[]);
  154. extern unsigned long do_go_exec(ulong (*entry)(int, char * const []), int argc,
  155. char *const argv[]);
  156. #if defined(CONFIG_CMD_NVEDIT_EFI)
  157. extern int do_env_print_efi(struct cmd_tbl *cmdtp, int flag, int argc,
  158. char *const argv[]);
  159. extern int do_env_set_efi(struct cmd_tbl *cmdtp, int flag, int argc,
  160. char *const argv[]);
  161. #endif
  162. /**
  163. * setexpr_regex_sub() - Replace a regex pattern with a string
  164. *
  165. * @data: Buffer containing the string to update
  166. * @data_size: Size of buffer (must be large enough for the new string)
  167. * @nbuf: Back-reference buffer
  168. * @nbuf_size: Size of back-reference buffer (must be larger enough for @s plus
  169. * all back-reference expansions)
  170. * @r: Regular expression to find
  171. * @s: String to replace with
  172. * @global: true to replace all matches in @data, false to replace just the
  173. * first
  174. * @return 0 if OK, 1 on error
  175. */
  176. int setexpr_regex_sub(char *data, uint data_size, char *nbuf, uint nbuf_size,
  177. const char *r, const char *s, bool global);
  178. /*
  179. * Error codes that commands return to cmd_process(). We use the standard 0
  180. * and 1 for success and failure, but add one more case - failure with a
  181. * request to call cmd_usage(). But the cmd_process() function handles
  182. * CMD_RET_USAGE itself and after calling cmd_usage() it will return 1.
  183. * This is just a convenience for commands to avoid them having to call
  184. * cmd_usage() all over the place.
  185. */
  186. enum command_ret_t {
  187. CMD_RET_SUCCESS, /* 0 = Success */
  188. CMD_RET_FAILURE, /* 1 = Failure */
  189. CMD_RET_USAGE = -1, /* Failure, please report 'usage' error */
  190. };
  191. /**
  192. * Process a command with arguments. We look up the command and execute it
  193. * if valid. Otherwise we print a usage message.
  194. *
  195. * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
  196. * @param argc Number of arguments (arg 0 must be the command text)
  197. * @param argv Arguments
  198. * @param repeatable This function sets this to 0 if the command is not
  199. * repeatable. If the command is repeatable, the value
  200. * is left unchanged.
  201. * @param ticks If ticks is not null, this function set it to the
  202. * number of ticks the command took to complete.
  203. * @return 0 if the command succeeded, 1 if it failed
  204. */
  205. int cmd_process(int flag, int argc, char *const argv[], int *repeatable,
  206. unsigned long *ticks);
  207. void fixup_cmdtable(struct cmd_tbl *cmdtp, int size);
  208. /**
  209. * board_run_command() - Fallback function to execute a command
  210. *
  211. * When no command line features are enabled in U-Boot, this function is
  212. * called to execute a command. Typically the function can look at the
  213. * command and perform a few very specific tasks, such as booting the
  214. * system in a particular way.
  215. *
  216. * This function is only used when CONFIG_CMDLINE is not enabled.
  217. *
  218. * In normal situations this function should not return, since U-Boot will
  219. * simply hang.
  220. *
  221. * @cmdline: Command line string to execute
  222. * @return 0 if OK, 1 for error
  223. */
  224. int board_run_command(const char *cmdline);
  225. int run_command(const char *cmd, int flag);
  226. int run_command_repeatable(const char *cmd, int flag);
  227. /**
  228. * Run a list of commands separated by ; or even \0
  229. *
  230. * Note that if 'len' is not -1, then the command does not need to be nul
  231. * terminated, Memory will be allocated for the command in that case.
  232. *
  233. * @param cmd List of commands to run, each separated bu semicolon
  234. * @param len Length of commands excluding terminator if known (-1 if not)
  235. * @param flag Execution flags (CMD_FLAG_...)
  236. * @return 0 on success, or != 0 on error.
  237. */
  238. int run_command_list(const char *cmd, int len, int flag);
  239. #endif /* __ASSEMBLY__ */
  240. /*
  241. * Command Flags:
  242. */
  243. #define CMD_FLAG_REPEAT 0x0001 /* repeat last command */
  244. #define CMD_FLAG_BOOTD 0x0002 /* command is from bootd */
  245. #define CMD_FLAG_ENV 0x0004 /* command is from the environment */
  246. #ifdef CONFIG_AUTO_COMPLETE
  247. # define _CMD_COMPLETE(x) x,
  248. #else
  249. # define _CMD_COMPLETE(x)
  250. #endif
  251. #ifdef CONFIG_SYS_LONGHELP
  252. # define _CMD_HELP(x) x,
  253. #else
  254. # define _CMD_HELP(x)
  255. #endif
  256. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  257. #define U_BOOT_SUBCMDS_RELOC(_cmdname) \
  258. static void _cmdname##_subcmds_reloc(void) \
  259. { \
  260. static int relocated; \
  261. \
  262. if (relocated) \
  263. return; \
  264. \
  265. fixup_cmdtable(_cmdname##_subcmds, \
  266. ARRAY_SIZE(_cmdname##_subcmds)); \
  267. relocated = 1; \
  268. }
  269. #else
  270. #define U_BOOT_SUBCMDS_RELOC(_cmdname) \
  271. static void _cmdname##_subcmds_reloc(void) { }
  272. #endif
  273. #define U_BOOT_SUBCMDS_DO_CMD(_cmdname) \
  274. static int do_##_cmdname(struct cmd_tbl *cmdtp, int flag, \
  275. int argc, char *const argv[], \
  276. int *repeatable) \
  277. { \
  278. struct cmd_tbl *subcmd; \
  279. \
  280. _cmdname##_subcmds_reloc(); \
  281. \
  282. /* We need at least the cmd and subcmd names. */ \
  283. if (argc < 2 || argc > CONFIG_SYS_MAXARGS) \
  284. return CMD_RET_USAGE; \
  285. \
  286. subcmd = find_cmd_tbl(argv[1], _cmdname##_subcmds, \
  287. ARRAY_SIZE(_cmdname##_subcmds)); \
  288. if (!subcmd || argc - 1 > subcmd->maxargs) \
  289. return CMD_RET_USAGE; \
  290. \
  291. if (flag == CMD_FLAG_REPEAT && \
  292. !cmd_is_repeatable(subcmd)) \
  293. return CMD_RET_SUCCESS; \
  294. \
  295. return subcmd->cmd_rep(subcmd, flag, argc - 1, \
  296. argv + 1, repeatable); \
  297. }
  298. #ifdef CONFIG_AUTO_COMPLETE
  299. #define U_BOOT_SUBCMDS_COMPLETE(_cmdname) \
  300. static int complete_##_cmdname(int argc, char *const argv[], \
  301. char last_char, int maxv, \
  302. char *cmdv[]) \
  303. { \
  304. return complete_subcmdv(_cmdname##_subcmds, \
  305. ARRAY_SIZE(_cmdname##_subcmds), \
  306. argc - 1, argv + 1, last_char, \
  307. maxv, cmdv); \
  308. }
  309. #else
  310. #define U_BOOT_SUBCMDS_COMPLETE(_cmdname)
  311. #endif
  312. #define U_BOOT_SUBCMDS(_cmdname, ...) \
  313. static struct cmd_tbl _cmdname##_subcmds[] = { __VA_ARGS__ }; \
  314. U_BOOT_SUBCMDS_RELOC(_cmdname) \
  315. U_BOOT_SUBCMDS_DO_CMD(_cmdname) \
  316. U_BOOT_SUBCMDS_COMPLETE(_cmdname)
  317. #ifdef CONFIG_CMDLINE
  318. #define U_BOOT_CMDREP_MKENT_COMPLETE(_name, _maxargs, _cmd_rep, \
  319. _usage, _help, _comp) \
  320. { #_name, _maxargs, _cmd_rep, cmd_discard_repeatable, \
  321. _usage, _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
  322. #define U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \
  323. _usage, _help, _comp) \
  324. { #_name, _maxargs, \
  325. _rep ? cmd_always_repeatable : cmd_never_repeatable, \
  326. _cmd, _usage, _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
  327. #define U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, _comp) \
  328. ll_entry_declare(struct cmd_tbl, _name, cmd) = \
  329. U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \
  330. _usage, _help, _comp);
  331. #define U_BOOT_CMDREP_COMPLETE(_name, _maxargs, _cmd_rep, _usage, \
  332. _help, _comp) \
  333. ll_entry_declare(struct cmd_tbl, _name, cmd) = \
  334. U_BOOT_CMDREP_MKENT_COMPLETE(_name, _maxargs, _cmd_rep, \
  335. _usage, _help, _comp)
  336. #else
  337. #define U_BOOT_SUBCMD_START(name) static struct cmd_tbl name[] = {};
  338. #define U_BOOT_SUBCMD_END
  339. #define _CMD_REMOVE(_name, _cmd) \
  340. int __remove_ ## _name(void) \
  341. { \
  342. if (0) \
  343. _cmd(NULL, 0, 0, NULL); \
  344. return 0; \
  345. }
  346. #define U_BOOT_CMDREP_MKENT_COMPLETE(_name, _maxargs, _cmd_rep, \
  347. _usage, _help, _comp) \
  348. { #_name, _maxargs, 0 ? _cmd_rep : NULL, NULL, _usage, \
  349. _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
  350. #define U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, \
  351. _help, _comp) \
  352. { #_name, _maxargs, NULL, 0 ? _cmd : NULL, _usage, \
  353. _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
  354. #define U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, \
  355. _comp) \
  356. _CMD_REMOVE(sub_ ## _name, _cmd)
  357. #define U_BOOT_CMDREP_COMPLETE(_name, _maxargs, _cmd_rep, _usage, \
  358. _help, _comp) \
  359. _CMD_REMOVE(sub_ ## _name, _cmd_rep)
  360. #endif /* CONFIG_CMDLINE */
  361. #define U_BOOT_CMD(_name, _maxargs, _rep, _cmd, _usage, _help) \
  362. U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, NULL)
  363. #define U_BOOT_CMD_MKENT(_name, _maxargs, _rep, _cmd, _usage, _help) \
  364. U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \
  365. _usage, _help, NULL)
  366. #define U_BOOT_SUBCMD_MKENT_COMPLETE(_name, _maxargs, _rep, _do_cmd, \
  367. _comp) \
  368. U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _do_cmd, \
  369. "", "", _comp)
  370. #define U_BOOT_SUBCMD_MKENT(_name, _maxargs, _rep, _do_cmd) \
  371. U_BOOT_SUBCMD_MKENT_COMPLETE(_name, _maxargs, _rep, _do_cmd, \
  372. NULL)
  373. #define U_BOOT_CMD_WITH_SUBCMDS(_name, _usage, _help, ...) \
  374. U_BOOT_SUBCMDS(_name, __VA_ARGS__) \
  375. U_BOOT_CMDREP_COMPLETE(_name, CONFIG_SYS_MAXARGS, do_##_name, \
  376. _usage, _help, complete_##_name)
  377. #endif /* __COMMAND_H */