cmdline.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. # SPDX-License-Identifier: GPL-2.0+
  2. # Copyright (c) 2016 Google, Inc
  3. # Written by Simon Glass <sjg@chromium.org>
  4. #
  5. """Command-line parser for binman"""
  6. import argparse
  7. from argparse import ArgumentParser
  8. import os
  9. from binman import state
  10. import os
  11. import pathlib
  12. BINMAN_DIR = pathlib.Path(__file__).parent
  13. HAS_TESTS = (BINMAN_DIR / "ftest.py").exists()
  14. def make_extract_parser(subparsers):
  15. """make_extract_parser: Make a subparser for the 'extract' command
  16. Args:
  17. subparsers (ArgumentParser): parser to add this one to
  18. """
  19. extract_parser = subparsers.add_parser('extract',
  20. help='Extract files from an image')
  21. extract_parser.add_argument('-F', '--format', type=str,
  22. help='Select an alternative format for extracted data')
  23. extract_parser.add_argument('-i', '--image', type=str, required=True,
  24. help='Image filename to extract')
  25. extract_parser.add_argument('-f', '--filename', type=str,
  26. help='Output filename to write to')
  27. extract_parser.add_argument('-O', '--outdir', type=str, default='',
  28. help='Path to directory to use for output files')
  29. extract_parser.add_argument('paths', type=str, nargs='*',
  30. help='Paths within file to extract (wildcard)')
  31. extract_parser.add_argument('-U', '--uncompressed', action='store_true',
  32. help='Output raw uncompressed data for compressed entries')
  33. #pylint: disable=R0903
  34. class BinmanVersion(argparse.Action):
  35. """Handles the -V option to binman
  36. This reads the version information from a file called 'version' in the same
  37. directory as this file.
  38. If not present it assumes this is running from the U-Boot tree and collects
  39. the version from the Makefile.
  40. The format of the version information is three VAR = VALUE lines, for
  41. example:
  42. VERSION = 2022
  43. PATCHLEVEL = 01
  44. EXTRAVERSION = -rc2
  45. """
  46. def __init__(self, nargs=0, **kwargs):
  47. super().__init__(nargs=nargs, **kwargs)
  48. def __call__(self, parser, namespace, values, option_string=None):
  49. parser._print_message(f'Binman {state.GetVersion()}\n')
  50. parser.exit()
  51. def ParseArgs(argv):
  52. """Parse the binman command-line arguments
  53. Args:
  54. argv (list of str): List of string arguments
  55. Returns:
  56. tuple: (options, args) with the command-line options and arugments.
  57. options provides access to the options (e.g. option.debug)
  58. args is a list of string arguments
  59. """
  60. def _AddPreserve(pars):
  61. pars.add_argument('-O', '--outdir', type=str,
  62. action='store', help='Path to directory to use for intermediate '
  63. 'and output files')
  64. pars.add_argument('-p', '--preserve', action='store_true',\
  65. help='Preserve temporary output directory even if option -O is not '
  66. 'given')
  67. if '-H' in argv:
  68. argv.append('build')
  69. epilog = '''Binman creates and manipulate images for a board from a set of binaries. Binman is
  70. controlled by a description in the board device tree.'''
  71. parser = ArgumentParser(epilog=epilog)
  72. parser.add_argument('-B', '--build-dir', type=str, default='b',
  73. help='Directory containing the build output')
  74. parser.add_argument('-D', '--debug', action='store_true',
  75. help='Enabling debugging (provides a full traceback on error)')
  76. parser.add_argument('-H', '--full-help', action='store_true',
  77. default=False, help='Display the README file')
  78. parser.add_argument('--tooldir', type=str,
  79. default=os.path.join(os.path.expanduser('~/.binman-tools')),
  80. help='Set the directory to store tools')
  81. parser.add_argument('--toolpath', type=str, action='append',
  82. help='Add a path to the list of directories containing tools')
  83. parser.add_argument('-T', '--threads', type=int,
  84. default=None, help='Number of threads to use (0=single-thread)')
  85. parser.add_argument('--test-section-timeout', action='store_true',
  86. help='Use a zero timeout for section multi-threading (for testing)')
  87. parser.add_argument('-v', '--verbosity', default=1,
  88. type=int, help='Control verbosity: 0=silent, 1=warnings, 2=notices, '
  89. '3=info, 4=detail, 5=debug')
  90. parser.add_argument('-V', '--version', nargs=0, action=BinmanVersion)
  91. subparsers = parser.add_subparsers(dest='cmd')
  92. subparsers.required = True
  93. build_parser = subparsers.add_parser('build', help='Build firmware image')
  94. build_parser.add_argument('-a', '--entry-arg', type=str, action='append',
  95. help='Set argument value arg=value')
  96. build_parser.add_argument('-b', '--board', type=str,
  97. help='Board name to build')
  98. build_parser.add_argument('-d', '--dt', type=str,
  99. help='Configuration file (.dtb) to use')
  100. build_parser.add_argument('--fake-dtb', action='store_true',
  101. help='Use fake device tree contents (for testing only)')
  102. build_parser.add_argument('--fake-ext-blobs', action='store_true',
  103. help='Create fake ext blobs with dummy content (for testing only)')
  104. build_parser.add_argument('--force-missing-bintools', type=str,
  105. help='Comma-separated list of bintools to consider missing (for testing)')
  106. build_parser.add_argument('-i', '--image', type=str, action='append',
  107. help='Image filename to build (if not specified, build all)')
  108. build_parser.add_argument('-I', '--indir', action='append',
  109. help='Add a path to the list of directories to use for input files')
  110. build_parser.add_argument('-m', '--map', action='store_true',
  111. default=False, help='Output a map file for each image')
  112. build_parser.add_argument('-M', '--allow-missing', action='store_true',
  113. default=False, help='Allow external blobs and bintools to be missing')
  114. build_parser.add_argument('-n', '--no-expanded', action='store_true',
  115. help="Don't use 'expanded' versions of entries where available; "
  116. "normally 'u-boot' becomes 'u-boot-expanded', for example")
  117. _AddPreserve(build_parser)
  118. build_parser.add_argument('-u', '--update-fdt', action='store_true',
  119. default=False, help='Update the binman node with offset/size info')
  120. build_parser.add_argument('--update-fdt-in-elf', type=str,
  121. help='Update an ELF file with the output dtb: infile,outfile,begin_sym,end_sym')
  122. build_parser.add_argument(
  123. '-W', '--ignore-missing', action='store_true', default=False,
  124. help='Return success even if there are missing blobs/bintools (requires -M)')
  125. subparsers.add_parser(
  126. 'bintool-docs', help='Write out bintool documentation (see bintool.rst)')
  127. subparsers.add_parser(
  128. 'entry-docs', help='Write out entry documentation (see entries.rst)')
  129. list_parser = subparsers.add_parser('ls', help='List files in an image')
  130. list_parser.add_argument('-i', '--image', type=str, required=True,
  131. help='Image filename to list')
  132. list_parser.add_argument('paths', type=str, nargs='*',
  133. help='Paths within file to list (wildcard)')
  134. make_extract_parser(subparsers)
  135. replace_parser = subparsers.add_parser('replace',
  136. help='Replace entries in an image')
  137. replace_parser.add_argument('-C', '--compressed', action='store_true',
  138. help='Input data is already compressed if needed for the entry')
  139. replace_parser.add_argument('-i', '--image', type=str, required=True,
  140. help='Image filename to update')
  141. replace_parser.add_argument('-f', '--filename', type=str,
  142. help='Input filename to read from')
  143. replace_parser.add_argument('-F', '--fix-size', action='store_true',
  144. help="Don't allow entries to be resized")
  145. replace_parser.add_argument('-I', '--indir', type=str, default='',
  146. help='Path to directory to use for input files')
  147. replace_parser.add_argument('-m', '--map', action='store_true',
  148. default=False, help='Output a map file for the updated image')
  149. _AddPreserve(replace_parser)
  150. replace_parser.add_argument('paths', type=str, nargs='*',
  151. help='Paths within file to replace (wildcard)')
  152. sign_parser = subparsers.add_parser('sign',
  153. help='Sign entries in image')
  154. sign_parser.add_argument('-a', '--algo', type=str, required=True,
  155. help='Hash algorithm e.g. sha256,rsa4096')
  156. sign_parser.add_argument('-f', '--file', type=str, required=False,
  157. help='Input filename to sign')
  158. sign_parser.add_argument('-i', '--image', type=str, required=True,
  159. help='Image filename to update')
  160. sign_parser.add_argument('-k', '--key', type=str, required=True,
  161. help='Private key file for signing')
  162. sign_parser.add_argument('paths', type=str, nargs='*',
  163. help='Paths within file to sign (wildcard)')
  164. if HAS_TESTS:
  165. test_parser = subparsers.add_parser('test', help='Run tests')
  166. test_parser.add_argument('-P', '--processes', type=int,
  167. help='set number of processes to use for running tests')
  168. test_parser.add_argument('-T', '--test-coverage', action='store_true',
  169. default=False, help='run tests and check for 100%% coverage')
  170. test_parser.add_argument(
  171. '-X', '--test-preserve-dirs', action='store_true',
  172. help='Preserve and display test-created input directories; also '
  173. 'preserve the output directory if a single test is run (pass '
  174. 'test name at the end of the command line')
  175. test_parser.add_argument('tests', nargs='*',
  176. help='Test names to run (omit for all)')
  177. tool_parser = subparsers.add_parser('tool', help='Check bintools')
  178. tool_parser.add_argument('-l', '--list', action='store_true',
  179. help='List all known bintools')
  180. tool_parser.add_argument(
  181. '-f', '--fetch', action='store_true',
  182. help='fetch a bintool from a known location (or: all/missing)')
  183. tool_parser.add_argument('bintools', type=str, nargs='*')
  184. return parser.parse_args(argv)