blob: 54926adb09fea2f073fb1477f7231131f83f09d2 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0+
Simon Glassbf7fd502016-11-25 20:15:51 -07002# Copyright (c) 2016 Google, Inc
3# Written by Simon Glass <sjg@chromium.org>
4#
Simon Glassbf7fd502016-11-25 20:15:51 -07005
Simon Glass650e5de2021-11-23 11:03:41 -07006"""Command-line parser for binman"""
Simon Glassbf7fd502016-11-25 20:15:51 -07007
Simon Glassc475dec2021-11-23 11:03:42 -07008import argparse
Simon Glass53cd5d92019-07-08 14:25:29 -06009from argparse import ArgumentParser
Simon Glassfe7e9242023-02-22 12:14:49 -070010import os
Simon Glass8d2ef3e2022-02-11 13:23:21 -070011from binman import state
Simon Glass94bb5cd2023-02-23 18:18:19 -070012import os
13import pathlib
14
15BINMAN_DIR = pathlib.Path(__file__).parent
16HAS_TESTS = (BINMAN_DIR / "ftest.py").exists()
Simon Glassbf7fd502016-11-25 20:15:51 -070017
Simon Glass650e5de2021-11-23 11:03:41 -070018def make_extract_parser(subparsers):
19 """make_extract_parser: Make a subparser for the 'extract' command
20
21 Args:
22 subparsers (ArgumentParser): parser to add this one to
23 """
24 extract_parser = subparsers.add_parser('extract',
25 help='Extract files from an image')
Simon Glass943bf782021-11-23 21:09:50 -070026 extract_parser.add_argument('-F', '--format', type=str,
27 help='Select an alternative format for extracted data')
Simon Glass650e5de2021-11-23 11:03:41 -070028 extract_parser.add_argument('-i', '--image', type=str, required=True,
29 help='Image filename to extract')
30 extract_parser.add_argument('-f', '--filename', type=str,
31 help='Output filename to write to')
32 extract_parser.add_argument('-O', '--outdir', type=str, default='',
33 help='Path to directory to use for output files')
34 extract_parser.add_argument('paths', type=str, nargs='*',
35 help='Paths within file to extract (wildcard)')
36 extract_parser.add_argument('-U', '--uncompressed', action='store_true',
37 help='Output raw uncompressed data for compressed entries')
38
Simon Glassc475dec2021-11-23 11:03:42 -070039
40#pylint: disable=R0903
41class BinmanVersion(argparse.Action):
42 """Handles the -V option to binman
43
44 This reads the version information from a file called 'version' in the same
45 directory as this file.
46
47 If not present it assumes this is running from the U-Boot tree and collects
48 the version from the Makefile.
49
50 The format of the version information is three VAR = VALUE lines, for
51 example:
52
53 VERSION = 2022
54 PATCHLEVEL = 01
55 EXTRAVERSION = -rc2
56 """
57 def __init__(self, nargs=0, **kwargs):
58 super().__init__(nargs=nargs, **kwargs)
59
60 def __call__(self, parser, namespace, values, option_string=None):
61 parser._print_message(f'Binman {state.GetVersion()}\n')
62 parser.exit()
63
Simon Glassbf7fd502016-11-25 20:15:51 -070064
65def ParseArgs(argv):
66 """Parse the binman command-line arguments
67
68 Args:
Simon Glass650e5de2021-11-23 11:03:41 -070069 argv (list of str): List of string arguments
70
Simon Glassbf7fd502016-11-25 20:15:51 -070071 Returns:
Simon Glass650e5de2021-11-23 11:03:41 -070072 tuple: (options, args) with the command-line options and arugments.
Simon Glassbf7fd502016-11-25 20:15:51 -070073 options provides access to the options (e.g. option.debug)
74 args is a list of string arguments
75 """
Simon Glass53cd5d92019-07-08 14:25:29 -060076 if '-H' in argv:
77 argv.append('build')
78
79 epilog = '''Binman creates and manipulate images for a board from a set of binaries. Binman is
80controlled by a description in the board device tree.'''
81
82 parser = ArgumentParser(epilog=epilog)
83 parser.add_argument('-B', '--build-dir', type=str, default='b',
84 help='Directory containing the build output')
85 parser.add_argument('-D', '--debug', action='store_true',
86 help='Enabling debugging (provides a full traceback on error)')
87 parser.add_argument('-H', '--full-help', action='store_true',
Simon Glassbf7fd502016-11-25 20:15:51 -070088 default=False, help='Display the README file')
Simon Glassfe7e9242023-02-22 12:14:49 -070089 parser.add_argument('--tooldir', type=str,
90 default=os.path.join(os.getenv('HOME'), '.binman-tools'),
91 help='Set the directory to store tools')
Simon Glass53cd5d92019-07-08 14:25:29 -060092 parser.add_argument('--toolpath', type=str, action='append',
Simon Glassfe7e9242023-02-22 12:14:49 -070093 help='Add a path to the list of directories containing tools')
Simon Glassc69d19c2021-07-06 10:36:37 -060094 parser.add_argument('-T', '--threads', type=int,
95 default=None, help='Number of threads to use (0=single-thread)')
96 parser.add_argument('--test-section-timeout', action='store_true',
97 help='Use a zero timeout for section multi-threading (for testing)')
Simon Glass53cd5d92019-07-08 14:25:29 -060098 parser.add_argument('-v', '--verbosity', default=1,
99 type=int, help='Control verbosity: 0=silent, 1=warnings, 2=notices, '
100 '3=info, 4=detail, 5=debug')
Simon Glassc475dec2021-11-23 11:03:42 -0700101 parser.add_argument('-V', '--version', nargs=0, action=BinmanVersion)
Simon Glass53cd5d92019-07-08 14:25:29 -0600102
103 subparsers = parser.add_subparsers(dest='cmd')
Simon Glass680e3c62020-10-26 17:40:02 -0600104 subparsers.required = True
Simon Glass53cd5d92019-07-08 14:25:29 -0600105
106 build_parser = subparsers.add_parser('build', help='Build firmware image')
107 build_parser.add_argument('-a', '--entry-arg', type=str, action='append',
108 help='Set argument value arg=value')
109 build_parser.add_argument('-b', '--board', type=str,
110 help='Board name to build')
111 build_parser.add_argument('-d', '--dt', type=str,
112 help='Configuration file (.dtb) to use')
113 build_parser.add_argument('--fake-dtb', action='store_true',
114 help='Use fake device tree contents (for testing only)')
Heiko Thierya89c8f22022-01-06 11:49:41 +0100115 build_parser.add_argument('--fake-ext-blobs', action='store_true',
116 help='Create fake ext blobs with dummy content (for testing only)')
Simon Glass4f9ee832022-01-09 20:14:09 -0700117 build_parser.add_argument('--force-missing-bintools', type=str,
118 help='Comma-separated list of bintools to consider missing (for testing)')
Simon Glass53cd5d92019-07-08 14:25:29 -0600119 build_parser.add_argument('-i', '--image', type=str, action='append',
120 help='Image filename to build (if not specified, build all)')
121 build_parser.add_argument('-I', '--indir', action='append',
122 help='Add a path to the list of directories to use for input files')
123 build_parser.add_argument('-m', '--map', action='store_true',
Simon Glass3b0c3822018-06-01 09:38:20 -0600124 default=False, help='Output a map file for each image')
Simon Glass4f9f1052020-07-09 18:39:38 -0600125 build_parser.add_argument('-M', '--allow-missing', action='store_true',
Simon Glassb38da152022-11-09 19:14:42 -0700126 default=False, help='Allow external blobs and bintools to be missing')
Simon Glass63aeaeb2021-03-18 20:25:05 +1300127 build_parser.add_argument('-n', '--no-expanded', action='store_true',
128 help="Don't use 'expanded' versions of entries where available; "
129 "normally 'u-boot' becomes 'u-boot-expanded', for example")
Simon Glass53cd5d92019-07-08 14:25:29 -0600130 build_parser.add_argument('-O', '--outdir', type=str,
Simon Glassbf7fd502016-11-25 20:15:51 -0700131 action='store', help='Path to directory to use for intermediate and '
132 'output files')
Simon Glass53cd5d92019-07-08 14:25:29 -0600133 build_parser.add_argument('-p', '--preserve', action='store_true',\
Simon Glassbf7fd502016-11-25 20:15:51 -0700134 help='Preserve temporary output directory even if option -O is not '
135 'given')
Simon Glass53cd5d92019-07-08 14:25:29 -0600136 build_parser.add_argument('-u', '--update-fdt', action='store_true',
Simon Glass3ab95982018-08-01 15:22:37 -0600137 default=False, help='Update the binman node with offset/size info')
Simon Glass0427bed2021-11-03 21:09:18 -0600138 build_parser.add_argument('--update-fdt-in-elf', type=str,
139 help='Update an ELF file with the output dtb: infile,outfile,begin_sym,end_sym')
Simon Glassb38da152022-11-09 19:14:42 -0700140 build_parser.add_argument(
141 '-W', '--ignore-missing', action='store_true', default=False,
142 help='Return success even if there are missing blobs/bintools (requires -M)')
Simon Glass53cd5d92019-07-08 14:25:29 -0600143
Simon Glass650e5de2021-11-23 11:03:41 -0700144 subparsers.add_parser(
Simon Glassbc570642022-01-09 20:14:11 -0700145 'bintool-docs', help='Write out bintool documentation (see bintool.rst)')
146
147 subparsers.add_parser(
Simon Glass650e5de2021-11-23 11:03:41 -0700148 'entry-docs', help='Write out entry documentation (see entries.rst)')
Simon Glass53cd5d92019-07-08 14:25:29 -0600149
Simon Glass61f564d2019-07-08 14:25:48 -0600150 list_parser = subparsers.add_parser('ls', help='List files in an image')
151 list_parser.add_argument('-i', '--image', type=str, required=True,
152 help='Image filename to list')
153 list_parser.add_argument('paths', type=str, nargs='*',
154 help='Paths within file to list (wildcard)')
155
Simon Glass650e5de2021-11-23 11:03:41 -0700156 make_extract_parser(subparsers)
Simon Glass71ce0ba2019-07-08 14:25:52 -0600157
Simon Glassa6cb9952019-07-20 12:24:15 -0600158 replace_parser = subparsers.add_parser('replace',
159 help='Replace entries in an image')
160 replace_parser.add_argument('-C', '--compressed', action='store_true',
161 help='Input data is already compressed if needed for the entry')
162 replace_parser.add_argument('-i', '--image', type=str, required=True,
Jan Kiszka89cc0522021-11-11 08:13:30 +0100163 help='Image filename to update')
Simon Glassa6cb9952019-07-20 12:24:15 -0600164 replace_parser.add_argument('-f', '--filename', type=str,
165 help='Input filename to read from')
166 replace_parser.add_argument('-F', '--fix-size', action='store_true',
167 help="Don't allow entries to be resized")
168 replace_parser.add_argument('-I', '--indir', type=str, default='',
169 help='Path to directory to use for input files')
170 replace_parser.add_argument('-m', '--map', action='store_true',
171 default=False, help='Output a map file for the updated image')
172 replace_parser.add_argument('paths', type=str, nargs='*',
Jan Kiszka89cc0522021-11-11 08:13:30 +0100173 help='Paths within file to replace (wildcard)')
Simon Glassa6cb9952019-07-20 12:24:15 -0600174
Simon Glass94bb5cd2023-02-23 18:18:19 -0700175 if HAS_TESTS:
176 test_parser = subparsers.add_parser('test', help='Run tests')
177 test_parser.add_argument('-P', '--processes', type=int,
178 help='set number of processes to use for running tests')
179 test_parser.add_argument('-T', '--test-coverage', action='store_true',
180 default=False, help='run tests and check for 100%% coverage')
181 test_parser.add_argument(
182 '-X', '--test-preserve-dirs', action='store_true',
183 help='Preserve and display test-created input directories; also '
184 'preserve the output directory if a single test is run (pass '
185 'test name at the end of the command line')
186 test_parser.add_argument('tests', nargs='*',
187 help='Test names to run (omit for all)')
Simon Glassbf7fd502016-11-25 20:15:51 -0700188
Simon Glass386c63c2022-01-09 20:13:50 -0700189 tool_parser = subparsers.add_parser('tool', help='Check bintools')
190 tool_parser.add_argument('-l', '--list', action='store_true',
191 help='List all known bintools')
Simon Glass94bb5cd2023-02-23 18:18:19 -0700192 tool_parser.add_argument(
193 '-f', '--fetch', action='store_true',
194 help='fetch a bintool from a known location (or: all/missing)')
Simon Glass386c63c2022-01-09 20:13:50 -0700195 tool_parser.add_argument('bintools', type=str, nargs='*')
196
Simon Glassbf7fd502016-11-25 20:15:51 -0700197 return parser.parse_args(argv)