U-boot comes with a lot of stock commands which one can run on the u-boot console similar to the Linux console commands like ‘ls’. The source for every command can be found under ‘common/‘ directory with file names starting with ‘cmd_‘. However, not all commands are enabled by default.
A user may either want to enable a command the stock or add his/her own command. Both are possible in just few simple steps.
Note: You can check the list of commands already enabled from the commands list from the u-boot console by running the command ‘help‘ or simply ‘?‘.
1. Enabling a command from the stock
From the code, you can open ‘common/Makefile‘ and under the ‘# command section you can find the list of all the commands masked with config flags ‘CONFIG_*’. To enable a command, you have to simply #define the corresponding flag under the ‘include/configs/<board>.h‘ file and build the source. Now, you can see the command in the list of commands by running ‘help‘.
To enable a command ‘source‘, in the ‘common/Makefile‘, you can find
obj-$(CONFIG_CMD_SOURCE) += cmd_source.o

Simply include the corresponding flag in ‘include/configs/<board>.h‘ file as follows
or in another ways, simply replace $(CONFIG_CMD_SOURCE) with ‘y’ as follows.
obj-y += cmd_source.o
will enable the command irrespective of any flags defined. However, the first method is most reliable and controllable from the board.h config file.
2. Adding and enabling a new command of your own
In this case, there are three things to be done.
  • Write an API(function) that implements your command.
  • Register your function to the command list.
  • Enable your command.
Write an API that implements your command: First thing is to write an API that implements your expected functionality. But, the API should follow a fixed prototype.
/* Prototype */
int do_funcname(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) 
*cmdtp – Command table pointer (comparable to function vector table)
flag – Unused
argc – Argument count, including command name itself
argv[] –  Array of arguments (string)
Register your API to the command table: Registering your command to the command table is necessary for both, listed and command-line arguments to be properly passed to your API.
U-Boot provides the following prototype to register your API.
/* Prototype */
U_BOOT_CMD(_name, _maxargs, _rep, _cmd, _usage, _help)
_name – Command Name (the name you want to run the command)
_maxargs – Maximum number of arguments
_rep – Autorepeat allowed? (0-No/1-Yes)
_cmd – Implementation function (pointer to your API)
_usage – Usage – Short help message (string)
_help – Long help message (string)

Note: Both the above prototypes are available in ‘include/command.h and hence need to include ‘command.h‘ to your command file header.
Enable your command: Finally, all set to have your own command except one last thing, enabling them. For that, you have to follow the same method as the first case as follow.
obj-y += <filename>.o
or have a new flag defined, say CONFIG_CMD_FILENAME and
obj-$(CONFIG_CMD_FILENAME) += <filename>.o

1. doc/README.commands in u-boot source.
2. xillybus.com

Leave a Reply

Your email address will not be published. Required fields are marked *