command.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. #include <linux/compiler_attributes.h>
  14. #ifndef NULL
  15. #define NULL 0
  16. #endif
  17. /* Default to a width of 8 characters for help message command width */
  18. #ifndef CFG_SYS_HELP_CMD_WIDTH
  19. #define CFG_SYS_HELP_CMD_WIDTH 10
  20. #endif
  21. #ifndef __ASSEMBLY__
  22. /*
  23. * Monitor Command Table
  24. */
  25. struct cmd_tbl {
  26. char *name; /* Command Name */
  27. int maxargs; /* maximum number of arguments */
  28. /*
  29. * Same as ->cmd() except the command
  30. * tells us if it can be repeated.
  31. * Replaces the old ->repeatable field
  32. * which was not able to make
  33. * repeatable property different for
  34. * the main command and sub-commands.
  35. */
  36. int (*cmd_rep)(struct cmd_tbl *cmd, int flags, int argc,
  37. char *const argv[], int *repeatable);
  38. /* Implementation function */
  39. int (*cmd)(struct cmd_tbl *cmd, int flags, int argc,
  40. char *const argv[]);
  41. char *usage; /* Usage message (short) */
  42. #ifdef CONFIG_SYS_LONGHELP
  43. const char *help; /* Help message (long) */
  44. #endif
  45. #ifdef CONFIG_AUTO_COMPLETE
  46. /* do auto completion on the arguments */
  47. int (*complete)(int argc, char *const argv[],
  48. char last_char, int maxv, char *cmdv[]);
  49. #endif
  50. };
  51. #if defined(CONFIG_CMD_RUN)
  52. int do_run(struct cmd_tbl *cmdtp, int flag, int argc,
  53. char *const argv[]);
  54. #endif
  55. /* common/command.c */
  56. int _do_help(struct cmd_tbl *cmd_start, int cmd_items, struct cmd_tbl *cmdtp,
  57. int flag, int argc, char *const argv[]);
  58. struct cmd_tbl *find_cmd(const char *cmd);
  59. struct cmd_tbl *find_cmd_tbl(const char *cmd, struct cmd_tbl *table,
  60. int table_len);
  61. int complete_subcmdv(struct cmd_tbl *cmdtp, int count, int argc,
  62. char *const argv[], char last_char, int maxv,
  63. char *cmdv[]);
  64. int cmd_usage(const struct cmd_tbl *cmdtp);
  65. /* Dummy ->cmd and ->cmd_rep wrappers. */
  66. int cmd_always_repeatable(struct cmd_tbl *cmdtp, int flag, int argc,
  67. char *const argv[], int *repeatable);
  68. int cmd_never_repeatable(struct cmd_tbl *cmdtp, int flag, int argc,
  69. char *const argv[], int *repeatable);
  70. int cmd_discard_repeatable(struct cmd_tbl *cmdtp, int flag, int argc,
  71. char *const argv[]);
  72. static inline bool cmd_is_repeatable(struct cmd_tbl *cmdtp)
  73. {
  74. return cmdtp->cmd_rep == cmd_always_repeatable;
  75. }
  76. #ifdef CONFIG_AUTO_COMPLETE
  77. int var_complete(int argc, char *const argv[], char last_char, int maxv,
  78. char *cmdv[]);
  79. int cmd_auto_complete(const char *const prompt, char *buf, int *np,
  80. int *colp);
  81. #endif
  82. /**
  83. * cmd_process_error() - report and process a possible error
  84. *
  85. * @cmdtp: Command which caused the error
  86. * @err: Error code (0 if none, -ve for error, like -EIO)
  87. * Return: 0 (CMD_RET_SUCCESS) if there is not error,
  88. * 1 (CMD_RET_FAILURE) if an error is found
  89. * -1 (CMD_RET_USAGE) if 'usage' error is found
  90. */
  91. int cmd_process_error(struct cmd_tbl *cmdtp, int err);
  92. /*
  93. * Monitor Command
  94. *
  95. * All commands use a common argument format:
  96. *
  97. * void function(struct cmd_tbl *cmdtp, int flag, int argc,
  98. * char *const argv[]);
  99. */
  100. #if defined(CONFIG_CMD_MEMORY) || \
  101. defined(CONFIG_CMD_I2C) || \
  102. defined(CONFIG_CMD_ITEST) || \
  103. defined(CONFIG_CMD_PCI) || \
  104. defined(CONFIG_CMD_SETEXPR)
  105. #define CMD_DATA_SIZE
  106. #define CMD_DATA_SIZE_ERR (-1)
  107. #define CMD_DATA_SIZE_STR (-2)
  108. /**
  109. * cmd_get_data_size() - Get the data-size specifier from a command
  110. *
  111. * This reads a '.x' size specifier appended to a command. For example 'md.b'
  112. * is the 'md' command with a '.b' specifier, meaning that the command should
  113. * use bytes.
  114. *
  115. * Valid characters are:
  116. *
  117. * b - byte
  118. * w - word (16 bits)
  119. * l - long (32 bits)
  120. * q - quad (64 bits)
  121. * s - string
  122. *
  123. * @arg: Pointers to the command to check. If a valid specifier is present it
  124. * will be the last character of the string, following a '.'
  125. * @default_size: Default size to return if there is no specifier
  126. * Return: data size in bytes (1, 2, 4, 8) or CMD_DATA_SIZE_ERR for an invalid
  127. * character, or CMD_DATA_SIZE_STR for a string
  128. */
  129. int cmd_get_data_size(char *arg, int default_size);
  130. #endif
  131. #ifdef CONFIG_CMD_BOOTD
  132. int do_bootd(struct cmd_tbl *cmdtp, int flag, int argc,
  133. char *const argv[]);
  134. #endif
  135. int do_bootm(struct cmd_tbl *cmdtp, int flag, int argc,
  136. char *const argv[]);
  137. #ifdef CONFIG_CMD_BOOTM
  138. int bootm_maybe_autostart(struct cmd_tbl *cmdtp, const char *cmd);
  139. #else
  140. static inline int bootm_maybe_autostart(struct cmd_tbl *cmdtp, const char *cmd)
  141. {
  142. return 0;
  143. }
  144. #endif
  145. int do_bootz(struct cmd_tbl *cmdtp, int flag, int argc,
  146. char *const argv[]);
  147. int do_booti(struct cmd_tbl *cmdtp, int flag, int argc,
  148. char *const argv[]);
  149. int do_zboot_parent(struct cmd_tbl *cmdtp, int flag, int argc,
  150. char *const argv[], int *repeatable);
  151. int common_diskboot(struct cmd_tbl *cmdtp, const char *intf, int argc,
  152. char *const argv[]);
  153. int do_reset(struct cmd_tbl *cmdtp, int flag, int argc,
  154. char *const argv[]);
  155. int do_poweroff(struct cmd_tbl *cmdtp, int flag, int argc,
  156. char *const argv[]);
  157. unsigned long do_go_exec(ulong (*entry)(int, char * const []), int argc,
  158. char *const argv[]);
  159. #if defined(CONFIG_CMD_NVEDIT_EFI)
  160. int do_env_print_efi(struct cmd_tbl *cmdtp, int flag, int argc,
  161. char *const argv[]);
  162. int do_env_set_efi(struct cmd_tbl *cmdtp, int flag, int argc,
  163. char *const argv[]);
  164. #endif
  165. /**
  166. * setexpr_regex_sub() - Replace a regex pattern with a string
  167. *
  168. * @data: Buffer containing the string to update
  169. * @data_size: Size of buffer (must be large enough for the new string)
  170. * @nbuf: Back-reference buffer
  171. * @nbuf_size: Size of back-reference buffer (must be larger enough for @s plus
  172. * all back-reference expansions)
  173. * @r: Regular expression to find
  174. * @s: String to replace with
  175. * @global: true to replace all matches in @data, false to replace just the
  176. * first
  177. * Return: 0 if OK, 1 on error
  178. */
  179. int setexpr_regex_sub(char *data, uint data_size, char *nbuf, uint nbuf_size,
  180. const char *r, const char *s, bool global);
  181. /*
  182. * Error codes that commands return to cmd_process(). We use the standard 0
  183. * and 1 for success and failure, but add one more case - failure with a
  184. * request to call cmd_usage(). But the cmd_process() function handles
  185. * CMD_RET_USAGE itself and after calling cmd_usage() it will return 1.
  186. * This is just a convenience for commands to avoid them having to call
  187. * cmd_usage() all over the place.
  188. */
  189. enum command_ret_t {
  190. CMD_RET_SUCCESS, /* 0 = Success */
  191. CMD_RET_FAILURE, /* 1 = Failure */
  192. CMD_RET_USAGE = -1, /* Failure, please report 'usage' error */
  193. };
  194. /**
  195. * Process a command with arguments. We look up the command and execute it
  196. * if valid. Otherwise we print a usage message.
  197. *
  198. * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
  199. * @param argc Number of arguments (arg 0 must be the command text)
  200. * @param argv Arguments
  201. * @param repeatable This function sets this to 0 if the command is not
  202. * repeatable. If the command is repeatable, the value
  203. * is left unchanged.
  204. * @param ticks If ticks is not null, this function set it to the
  205. * number of ticks the command took to complete.
  206. * Return: 0 if command succeeded, else non-zero (CMD_RET_...)
  207. */
  208. enum command_ret_t cmd_process(int flag, int argc, char *const argv[],
  209. int *repeatable, unsigned long *ticks);
  210. void fixup_cmdtable(struct cmd_tbl *cmdtp, int size);
  211. /**
  212. * board_run_command() - Fallback function to execute a command
  213. *
  214. * When no command line features are enabled in U-Boot, this function is
  215. * called to execute a command. Typically the function can look at the
  216. * command and perform a few very specific tasks, such as booting the
  217. * system in a particular way.
  218. *
  219. * This function is only used when CONFIG_CMDLINE is not enabled.
  220. *
  221. * In normal situations this function should not return, since U-Boot will
  222. * simply hang.
  223. *
  224. * @cmdline: Command line string to execute
  225. * Return: 0 if OK, 1 for error
  226. */
  227. int board_run_command(const char *cmdline);
  228. int run_command(const char *cmd, int flag);
  229. int run_command_repeatable(const char *cmd, int flag);
  230. /**
  231. * run_commandf() - Run a command created by a format string
  232. *
  233. * @fmt: printf() format string
  234. * @...: Arguments to use (flag is always 0)
  235. *
  236. * The command cannot be larger than (CONFIG_SYS_CBSIZE - 1) characters.
  237. *
  238. * Return:
  239. * Returns 0 on success, -EIO if internal output error occurred, -ENOSPC in
  240. * case of 'fmt' string truncation, or != 0 on error, specific for
  241. * run_command().
  242. */
  243. int run_commandf(const char *fmt, ...) __printf(1, 2);
  244. /**
  245. * Run a list of commands separated by ; or even \0
  246. *
  247. * Note that if 'len' is not -1, then the command does not need to be nul
  248. * terminated, Memory will be allocated for the command in that case.
  249. *
  250. * @param cmd List of commands to run, each separated bu semicolon
  251. * @param len Length of commands excluding terminator if known (-1 if not)
  252. * @param flag Execution flags (CMD_FLAG_...)
  253. * Return: 0 on success, or != 0 on error.
  254. */
  255. int run_command_list(const char *cmd, int len, int flag);
  256. /**
  257. * cmd_source_script() - Execute a script
  258. *
  259. * Executes a U-Boot script at a particular address in memory. The script should
  260. * have a header (FIT or legacy) with the script type (IH_TYPE_SCRIPT).
  261. *
  262. * @addr: Address of script
  263. * @fit_uname: FIT subimage name
  264. * Return: result code (enum command_ret_t)
  265. */
  266. int cmd_source_script(ulong addr, const char *fit_uname, const char *confname);
  267. #endif /* __ASSEMBLY__ */
  268. /*
  269. * Command Flags:
  270. */
  271. #define CMD_FLAG_REPEAT 0x0001 /* repeat last command */
  272. #define CMD_FLAG_BOOTD 0x0002 /* command is from bootd */
  273. #define CMD_FLAG_ENV 0x0004 /* command is from the environment */
  274. #ifdef CONFIG_AUTO_COMPLETE
  275. # define _CMD_COMPLETE(x) x,
  276. #else
  277. # define _CMD_COMPLETE(x)
  278. #endif
  279. #ifdef CONFIG_SYS_LONGHELP
  280. # define _CMD_HELP(x) x,
  281. #else
  282. # define _CMD_HELP(x)
  283. #endif
  284. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  285. #define U_BOOT_SUBCMDS_RELOC(_cmdname) \
  286. static void _cmdname##_subcmds_reloc(void) \
  287. { \
  288. static int relocated; \
  289. \
  290. if (relocated) \
  291. return; \
  292. \
  293. fixup_cmdtable(_cmdname##_subcmds, \
  294. ARRAY_SIZE(_cmdname##_subcmds)); \
  295. relocated = 1; \
  296. }
  297. #else
  298. #define U_BOOT_SUBCMDS_RELOC(_cmdname) \
  299. static void _cmdname##_subcmds_reloc(void) { }
  300. #endif
  301. #define U_BOOT_SUBCMDS_DO_CMD(_cmdname) \
  302. static int do_##_cmdname(struct cmd_tbl *cmdtp, int flag, \
  303. int argc, char *const argv[], \
  304. int *repeatable) \
  305. { \
  306. struct cmd_tbl *subcmd; \
  307. \
  308. _cmdname##_subcmds_reloc(); \
  309. \
  310. /* We need at least the cmd and subcmd names. */ \
  311. if (argc < 2 || argc > CONFIG_SYS_MAXARGS) \
  312. return CMD_RET_USAGE; \
  313. \
  314. subcmd = find_cmd_tbl(argv[1], _cmdname##_subcmds, \
  315. ARRAY_SIZE(_cmdname##_subcmds)); \
  316. if (!subcmd || argc - 1 > subcmd->maxargs) \
  317. return CMD_RET_USAGE; \
  318. \
  319. if (flag == CMD_FLAG_REPEAT && \
  320. !cmd_is_repeatable(subcmd)) \
  321. return CMD_RET_SUCCESS; \
  322. \
  323. return subcmd->cmd_rep(subcmd, flag, argc - 1, \
  324. argv + 1, repeatable); \
  325. }
  326. #ifdef CONFIG_AUTO_COMPLETE
  327. #define U_BOOT_SUBCMDS_COMPLETE(_cmdname) \
  328. static int complete_##_cmdname(int argc, char *const argv[], \
  329. char last_char, int maxv, \
  330. char *cmdv[]) \
  331. { \
  332. return complete_subcmdv(_cmdname##_subcmds, \
  333. ARRAY_SIZE(_cmdname##_subcmds), \
  334. argc - 1, argv + 1, last_char, \
  335. maxv, cmdv); \
  336. }
  337. #else
  338. #define U_BOOT_SUBCMDS_COMPLETE(_cmdname)
  339. #endif
  340. #define U_BOOT_SUBCMDS(_cmdname, ...) \
  341. static struct cmd_tbl _cmdname##_subcmds[] = { __VA_ARGS__ }; \
  342. U_BOOT_SUBCMDS_RELOC(_cmdname) \
  343. U_BOOT_SUBCMDS_DO_CMD(_cmdname) \
  344. U_BOOT_SUBCMDS_COMPLETE(_cmdname)
  345. #if CONFIG_IS_ENABLED(CMDLINE)
  346. #define U_BOOT_CMDREP_MKENT_COMPLETE(_name, _maxargs, _cmd_rep, \
  347. _usage, _help, _comp) \
  348. { #_name, _maxargs, _cmd_rep, cmd_discard_repeatable, \
  349. _usage, _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
  350. #define U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \
  351. _usage, _help, _comp) \
  352. { #_name, _maxargs, \
  353. _rep ? cmd_always_repeatable : cmd_never_repeatable, \
  354. _cmd, _usage, _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
  355. #define U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, _comp) \
  356. ll_entry_declare(struct cmd_tbl, _name, cmd) = \
  357. U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \
  358. _usage, _help, _comp);
  359. #define U_BOOT_CMDREP_COMPLETE(_name, _maxargs, _cmd_rep, _usage, \
  360. _help, _comp) \
  361. ll_entry_declare(struct cmd_tbl, _name, cmd) = \
  362. U_BOOT_CMDREP_MKENT_COMPLETE(_name, _maxargs, _cmd_rep, \
  363. _usage, _help, _comp)
  364. #else
  365. #define U_BOOT_SUBCMD_START(name) static struct cmd_tbl name[] = {};
  366. #define U_BOOT_SUBCMD_END
  367. #define _CMD_REMOVE(_name, _cmd) \
  368. int __remove_ ## _name(void) \
  369. { \
  370. if (0) \
  371. _cmd(NULL, 0, 0, NULL); \
  372. return 0; \
  373. }
  374. #define _CMD_REMOVE_REP(_name, _cmd) \
  375. int __remove_ ## _name(void) \
  376. { \
  377. if (0) \
  378. _cmd(NULL, 0, 0, NULL, NULL); \
  379. return 0; \
  380. }
  381. #define U_BOOT_CMDREP_MKENT_COMPLETE(_name, _maxargs, _cmd_rep, \
  382. _usage, _help, _comp) \
  383. { #_name, _maxargs, 0 ? _cmd_rep : NULL, NULL, _usage, \
  384. _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
  385. #define U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, \
  386. _help, _comp) \
  387. { #_name, _maxargs, NULL, 0 ? _cmd : NULL, _usage, \
  388. _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
  389. #define U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, \
  390. _comp) \
  391. _CMD_REMOVE(sub_ ## _name, _cmd)
  392. #define U_BOOT_CMDREP_COMPLETE(_name, _maxargs, _cmd_rep, _usage, \
  393. _help, _comp) \
  394. _CMD_REMOVE_REP(sub_ ## _name, _cmd_rep)
  395. #endif /* CONFIG_CMDLINE */
  396. #define U_BOOT_CMD(_name, _maxargs, _rep, _cmd, _usage, _help) \
  397. U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, NULL)
  398. #define U_BOOT_CMD_MKENT(_name, _maxargs, _rep, _cmd, _usage, _help) \
  399. U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, \
  400. _usage, _help, NULL)
  401. #define U_BOOT_SUBCMD_MKENT_COMPLETE(_name, _maxargs, _rep, _do_cmd, \
  402. _comp) \
  403. U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _do_cmd, \
  404. "", "", _comp)
  405. #define U_BOOT_SUBCMD_MKENT(_name, _maxargs, _rep, _do_cmd) \
  406. U_BOOT_SUBCMD_MKENT_COMPLETE(_name, _maxargs, _rep, _do_cmd, \
  407. NULL)
  408. #define U_BOOT_CMD_WITH_SUBCMDS(_name, _usage, _help, ...) \
  409. U_BOOT_SUBCMDS(_name, __VA_ARGS__) \
  410. U_BOOT_CMDREP_COMPLETE(_name, CONFIG_SYS_MAXARGS, do_##_name, \
  411. _usage, _help, complete_##_name)
  412. #endif /* __COMMAND_H */