[PATCH v4 00/14] cmd: Convert command option parsing to getopt()
Simon Glass
sjg at chromium.org
Thu Jul 2 09:59:45 CEST 2026
Hi,
On Wed, 1 Jul 2026 at 12:46, Simon Glass <sjg at chromium.org> wrote:
>
> Most U-Boot commands open-code their option parsing, scanning argv[] for
> "-x" strings by hand. That only recognises options in fixed positions
> (so e.g. 'echo foo -n' misbehaves) and duplicates the same code in
> every command.
>
> This series introduces a getopt-based command signature: a command
> marked CMDF_GETOPT receives a single 'struct getopt_state *' and parses
> options with getopt(), while commands without the flag keep the classic
> (cmdtp, flag, argc, argv) signature, so commands move over one at a
> time.
>
> It starts with the getopt groundwork - optional argv permutation, a
> getopt_pop() helper, dropping the printed error messages and the silent
> variant - then adds the U_BOOT_CMD_GETOPT() signature support and a
> strlower() helper, and converts the first commands: hash, env
> export/import/grep, fastboot and echo.
>
> The signature change itself is code-size-neutral; commands parse in
> POSIX mode (a '+' optstring prefix), so CONFIG_GETOPT_PERMUTE - which
> would make every getopt command carry a writable argv copy - stays off
> by default and is kept only as an opt-in.
>
> The earlier RFCs grew arm64 by about 1.1K, not ideal for a series that
> mainly benefits readability. To fix that, this series also restructures
> struct cmd_tbl so that adding getopt() support does not cost size: the
> rarely-useful ->cmd_rep function pointer is replaced by a
> CMDF_REPEATABLE flag, dropping a pointer from every command entry.
>
> Because that pointer is removed from every command on every board, the
> net result is now a size reduction. On qcom (arm64, ~120 commands) the
> whole series is about 6.9K smaller than mainline.
I just wanted to mention one more thing here, in case people are
confused by this large reduction. Subcommands have their own table so
there are about 250 entries all up. Since it is a PIE arm64 build,
this means a 24-byte relocation for each pointer in addition to the
pointer itself. So we are effectively saving about 32 bytes per
subcommand, not just 8.
Regards,
Simon
More information about the U-Boot
mailing list