command.h 14 KB

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