xref: /openbmc/u-boot/doc/README.commands (revision 43a6a1ec9055a53a11d6d735f11cceea13912bbe)
13d9640f5SHeinrich SchuchardtCommand definition
23d9640f5SHeinrich Schuchardt------------------
30d498393Swdenk
40d498393SwdenkCommands are added to U-Boot by creating a new command structure.
53d9640f5SHeinrich SchuchardtThis is done by first including command.h, then using the U_BOOT_CMD() or the
63d9640f5SHeinrich SchuchardtU_BOOT_CMD_COMPLETE macro to fill in a cmd_tbl_t struct.
70d498393Swdenk
80d498393SwdenkU_BOOT_CMD(name, maxargs, repeatable, command, "usage", "help")
93d9640f5SHeinrich SchuchardtU_BOOT_CMD_COMPLETE(name, maxargs, repeatable, command, "usage, "help", comp)
100d498393Swdenk
113d9640f5SHeinrich Schuchardtname:		The name of the command. THIS IS NOT a string.
120d498393Swdenk
133d9640f5SHeinrich Schuchardtmaxargs:	The maximum number of arguments this function takes including
143d9640f5SHeinrich Schuchardt		the command itself.
150d498393Swdenk
163d9640f5SHeinrich Schuchardtrepeatable:	Either 0 or 1 to indicate if autorepeat is allowed.
173d9640f5SHeinrich Schuchardt
183d9640f5SHeinrich Schuchardtcommand:	Pointer to the command function. This is the function that is
193d9640f5SHeinrich Schuchardt		called when the command is issued.
203d9640f5SHeinrich Schuchardt
213d9640f5SHeinrich Schuchardtusage:		Short description. This is a string.
223d9640f5SHeinrich Schuchardt
233d9640f5SHeinrich Schuchardthelp:		Long description. This is a string. The long description is
243d9640f5SHeinrich Schuchardt		only available if CONFIG_SYS_LONGHELP is defined.
253d9640f5SHeinrich Schuchardt
263d9640f5SHeinrich Schuchardtcomp:		Pointer to the completion function. May be NULL.
273d9640f5SHeinrich Schuchardt		This function is called if the user hits the TAB key while
283d9640f5SHeinrich Schuchardt		entering the command arguments to complete the entry. Command
293d9640f5SHeinrich Schuchardt		completion is only available if CONFIG_AUTO_COMPLETE is defined.
303d9640f5SHeinrich Schuchardt
31ca80b561SHeinrich SchuchardtSub-command definition
32ca80b561SHeinrich Schuchardt----------------------
33ca80b561SHeinrich Schuchardt
34ca80b561SHeinrich SchuchardtLikewise an array of cmd_tbl_t holding sub-commands can be created using either
35ca80b561SHeinrich Schuchardtof the following macros:
36ca80b561SHeinrich Schuchardt
37ca80b561SHeinrich Schuchardt* U_BOOT_CMD_MKENT(name, maxargs, repeatable, command, "usage", "help")
38ca80b561SHeinrich Schuchardt* U_BOOT_CMD_MKENTCOMPLETE(name, maxargs, repeatable, command, "usage, "help",
39ca80b561SHeinrich Schuchardt  comp)
40ca80b561SHeinrich Schuchardt
41ca80b561SHeinrich SchuchardtThis table has to be evaluated in the command function of the main command, e.g.
42ca80b561SHeinrich Schuchardt
43ca80b561SHeinrich Schuchardt    static cmd_tbl_t cmd_sub[] = {
44ca80b561SHeinrich Schuchardt        U_BOOT_CMD_MKENT(foo, CONFIG_SYS_MAXARGS, 1, do_foo, "", ""),
45ca80b561SHeinrich Schuchardt        U_BOOT_CMD_MKENT(bar, CONFIG_SYS_MAXARGS, 1, do_bar, "", ""),
46ca80b561SHeinrich Schuchardt    };
47ca80b561SHeinrich Schuchardt
48ca80b561SHeinrich Schuchardt    static int do_cmd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
49ca80b561SHeinrich Schuchardt    {
50ca80b561SHeinrich Schuchardt        cmd_tbl_t *cp;
51ca80b561SHeinrich Schuchardt
52ca80b561SHeinrich Schuchardt        if (argc < 2)
53ca80b561SHeinrich Schuchardt                return CMD_RET_USAGE;
54ca80b561SHeinrich Schuchardt
55ca80b561SHeinrich Schuchardt        /* drop sub-command argument */
56ca80b561SHeinrich Schuchardt        argc--;
57ca80b561SHeinrich Schuchardt        argv++;
58ca80b561SHeinrich Schuchardt
59ca80b561SHeinrich Schuchardt        cp = find_cmd_tbl(argv[0], cmd_ut_sub, ARRAY_SIZE(cmd_sub));
60ca80b561SHeinrich Schuchardt
61ca80b561SHeinrich Schuchardt        if (cp)
62ca80b561SHeinrich Schuchardt            return cp->cmd(cmdtp, flag, argc, argv);
63ca80b561SHeinrich Schuchardt
64ca80b561SHeinrich Schuchardt        return CMD_RET_USAGE;
65ca80b561SHeinrich Schuchardt    }
66ca80b561SHeinrich Schuchardt
673d9640f5SHeinrich SchuchardtCommand function
683d9640f5SHeinrich Schuchardt----------------
693d9640f5SHeinrich Schuchardt
70*f3ccba3eSHeinrich SchuchardtThe command function pointer has to be of type
713d9640f5SHeinrich Schuchardtint (*cmd)(struct cmd_tbl_s *cmdtp, int flag, int argc, const char *argv[]);
723d9640f5SHeinrich Schuchardt
733d9640f5SHeinrich Schuchardtcmdtp:		Table entry describing the command (see above).
743d9640f5SHeinrich Schuchardt
753d9640f5SHeinrich Schuchardtflag:		A bitmap which may contain the following bit:
763d9640f5SHeinrich Schuchardt		CMD_FLAG_REPEAT - The last command is repeated.
773d9640f5SHeinrich Schuchardt		CMD_FLAG_BOOTD  - The command is called by the bootd command.
783d9640f5SHeinrich Schuchardt		CMD_FLAG_ENV    - The command is called by the run command.
793d9640f5SHeinrich Schuchardt
803d9640f5SHeinrich Schuchardtargc:		Number of arguments including the command.
813d9640f5SHeinrich Schuchardt
823d9640f5SHeinrich Schuchardtargv:		Arguments.
833d9640f5SHeinrich Schuchardt
843d9640f5SHeinrich SchuchardtAllowable return value are:
853d9640f5SHeinrich Schuchardt
863d9640f5SHeinrich SchuchardtCMD_SUCCESS	The command was successfully executed.
873d9640f5SHeinrich Schuchardt
883d9640f5SHeinrich SchuchardtCMD_FAILURE	The command failed.
893d9640f5SHeinrich Schuchardt
903d9640f5SHeinrich SchuchardtCMD_RET_USAGE	The command was called with invalid parameters. This value
913d9640f5SHeinrich Schuchardt		leads to the display of the usage string.
923d9640f5SHeinrich Schuchardt
933d9640f5SHeinrich SchuchardtCompletion function
943d9640f5SHeinrich Schuchardt-------------------
953d9640f5SHeinrich Schuchardt
963d9640f5SHeinrich SchuchardtThe completion function pointer has to be of type
973d9640f5SHeinrich Schuchardtint (*complete)(int argc, char *const argv[], char last_char,
983d9640f5SHeinrich Schuchardt		int maxv, char *cmdv[]);
993d9640f5SHeinrich Schuchardt
1003d9640f5SHeinrich Schuchardtargc:		Number of arguments including the command.
1013d9640f5SHeinrich Schuchardt
1023d9640f5SHeinrich Schuchardtargv:		Arguments.
1033d9640f5SHeinrich Schuchardt
1043d9640f5SHeinrich Schuchardtlast_char:	The last character in the command line buffer.
1053d9640f5SHeinrich Schuchardt
1063d9640f5SHeinrich Schuchardtmaxv:		Maximum number of possible completions that may be returned by
1073d9640f5SHeinrich Schuchardt		the function.
1083d9640f5SHeinrich Schuchardt
1093d9640f5SHeinrich Schuchardtcmdv:		Used to return possible values for the last argument. The last
1103d9640f5SHeinrich Schuchardt		possible completion must be followed by NULL.
1113d9640f5SHeinrich Schuchardt
1123d9640f5SHeinrich SchuchardtThe function returns the number of possible completions (without the terminating
1133d9640f5SHeinrich SchuchardtNULL value).
1143d9640f5SHeinrich Schuchardt
1153d9640f5SHeinrich SchuchardtBehind the scene
1163d9640f5SHeinrich Schuchardt----------------
1170d498393Swdenk
118ef123c52SAlbert ARIBAUDThe structure created is named with a special prefix and placed by
119ef123c52SAlbert ARIBAUDthe linker in a special section using the linker lists mechanism
120ef123c52SAlbert ARIBAUD(see include/linker_lists.h)
1210d498393Swdenk
1220d498393SwdenkThis makes it possible for the final link to extract all commands
1230d498393Swdenkcompiled into any object code and construct a static array so the
124ef123c52SAlbert ARIBAUDcommand array can be iterated over using the linker lists macros.
1250d498393Swdenk
126ef123c52SAlbert ARIBAUDThe linker lists feature ensures that the linker does not discard
127ef123c52SAlbert ARIBAUDthese symbols when linking full U-Boot even though they are not
128ef123c52SAlbert ARIBAUDreferenced in the source code as such.
1292d1d6583STom Rini
1300d498393SwdenkIf a new board is defined do not forget to define the command section
1314379ac61SMasahiro Yamadaby writing in u-boot.lds ($(srctree)/board/boardname/u-boot.lds) these
1320d498393Swdenk3 lines:
1330d498393Swdenk
1346c7c946cSMarek Vasut	.u_boot_list : {
135ef123c52SAlbert ARIBAUD		KEEP(*(SORT(.u_boot_list*)));
1366c7c946cSMarek Vasut	}
137