README.commands 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. Command definition
  2. ------------------
  3. Commands are added to U-Boot by creating a new command structure.
  4. This is done by first including command.h, then using the U_BOOT_CMD() or the
  5. U_BOOT_CMD_COMPLETE macro to fill in a cmd_tbl_t struct.
  6. U_BOOT_CMD(name, maxargs, repeatable, command, "usage", "help")
  7. U_BOOT_CMD_COMPLETE(name, maxargs, repeatable, command, "usage, "help", comp)
  8. name: The name of the command. THIS IS NOT a string.
  9. maxargs: The maximum number of arguments this function takes including
  10. the command itself.
  11. repeatable: Either 0 or 1 to indicate if autorepeat is allowed.
  12. command: Pointer to the command function. This is the function that is
  13. called when the command is issued.
  14. usage: Short description. This is a string.
  15. help: Long description. This is a string. The long description is
  16. only available if CONFIG_SYS_LONGHELP is defined.
  17. comp: Pointer to the completion function. May be NULL.
  18. This function is called if the user hits the TAB key while
  19. entering the command arguments to complete the entry. Command
  20. completion is only available if CONFIG_AUTO_COMPLETE is defined.
  21. Sub-command definition
  22. ----------------------
  23. Likewise an array of cmd_tbl_t holding sub-commands can be created using either
  24. of the following macros:
  25. * U_BOOT_CMD_MKENT(name, maxargs, repeatable, command, "usage", "help")
  26. * U_BOOT_CMD_MKENTCOMPLETE(name, maxargs, repeatable, command, "usage, "help",
  27. comp)
  28. This table has to be evaluated in the command function of the main command, e.g.
  29. static cmd_tbl_t cmd_sub[] = {
  30. U_BOOT_CMD_MKENT(foo, CONFIG_SYS_MAXARGS, 1, do_foo, "", ""),
  31. U_BOOT_CMD_MKENT(bar, CONFIG_SYS_MAXARGS, 1, do_bar, "", ""),
  32. };
  33. static int do_cmd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  34. {
  35. cmd_tbl_t *cp;
  36. if (argc < 2)
  37. return CMD_RET_USAGE;
  38. /* drop sub-command argument */
  39. argc--;
  40. argv++;
  41. cp = find_cmd_tbl(argv[0], cmd_ut_sub, ARRAY_SIZE(cmd_sub));
  42. if (cp)
  43. return cp->cmd(cmdtp, flag, argc, argv);
  44. return CMD_RET_USAGE;
  45. }
  46. Command function
  47. ----------------
  48. The command function pointer has to be of type
  49. int (*cmd)(struct cmd_tbl_s *cmdtp, int flag, int argc, const char *argv[]);
  50. cmdtp: Table entry describing the command (see above).
  51. flag: A bitmap which may contain the following bit:
  52. CMD_FLAG_REPEAT - The last command is repeated.
  53. CMD_FLAG_BOOTD - The command is called by the bootd command.
  54. CMD_FLAG_ENV - The command is called by the run command.
  55. argc: Number of arguments including the command.
  56. argv: Arguments.
  57. Allowable return value are:
  58. CMD_SUCCESS The command was successfully executed.
  59. CMD_FAILURE The command failed.
  60. CMD_RET_USAGE The command was called with invalid parameters. This value
  61. leads to the display of the usage string.
  62. Completion function
  63. -------------------
  64. The completion function pointer has to be of type
  65. int (*complete)(int argc, char *const argv[], char last_char,
  66. int maxv, char *cmdv[]);
  67. argc: Number of arguments including the command.
  68. argv: Arguments.
  69. last_char: The last character in the command line buffer.
  70. maxv: Maximum number of possible completions that may be returned by
  71. the function.
  72. cmdv: Used to return possible values for the last argument. The last
  73. possible completion must be followed by NULL.
  74. The function returns the number of possible completions (without the terminating
  75. NULL value).
  76. Behind the scene
  77. ----------------
  78. The structure created is named with a special prefix and placed by
  79. the linker in a special section using the linker lists mechanism
  80. (see include/linker_lists.h)
  81. This makes it possible for the final link to extract all commands
  82. compiled into any object code and construct a static array so the
  83. command array can be iterated over using the linker lists macros.
  84. The linker lists feature ensures that the linker does not discard
  85. these symbols when linking full U-Boot even though they are not
  86. referenced in the source code as such.
  87. If a new board is defined do not forget to define the command section
  88. by writing in u-boot.lds ($(srctree)/board/boardname/u-boot.lds) these
  89. 3 lines:
  90. .u_boot_list : {
  91. KEEP(*(SORT(.u_boot_list*)));
  92. }