blob: c85bfe053cf95dd22bfca87daafd53a3dc496f54 [file] [log] [blame]
Simon Glass48bca842022-01-09 20:13:56 -07001# SPDX-License-Identifier: GPL-2.0+
2# Copyright 2022 Google LLC
3#
4"""Bintool implementation for mkimage"""
5
6import re
7
8from binman import bintool
9
10class Bintoolmkimage(bintool.Bintool):
11 """Image generation for U-Boot
12
13 This bintool supports running `mkimage` with some basic parameters as
14 neeed by binman.
15
16 Normally binman uses the mkimage built by U-Boot. But when run outside the
17 U-Boot build system, binman can use the version installed in your system.
18 Support is provided for fetching this on Debian-like systems, using apt.
19 """
20 def __init__(self, name):
21 super().__init__(name, 'Generate image for U-Boot')
22
23 # pylint: disable=R0913
24 def run(self, reset_timestamp=False, output_fname=None, external=False,
25 pad=None, version=False):
26 """Run mkimage
27
28 Args:
29 reset_timestamp: True to update the timestamp in the FIT
30 output_fname: Output filename to write to
31 external: True to create an 'external' FIT, where the binaries are
32 located outside the main data structure
33 pad: Bytes to use for padding the FIT devicetree output. This allows
34 other things to be easily added later, if required, such as
35 signatures
36 version: True to get the mkimage version
37 """
38 args = []
39 if external:
40 args.append('-E')
41 if pad:
42 args += ['-p', f'{pad:x}']
43 if reset_timestamp:
44 args.append('-t')
45 if output_fname:
46 args += ['-F', output_fname]
47 if version:
48 args.append('-V')
49 return self.run_cmd(*args)
50
51 def fetch(self, method):
52 """Fetch handler for mkimage
53
54 This installs mkimage using the apt utility.
55
56 Args:
57 method (FETCH_...): Method to use
58
59 Returns:
60 True if the file was fetched and now installed, None if a method
61 other than FETCH_BIN was requested
62
63 Raises:
64 Valuerror: Fetching could not be completed
65 """
66 if method != bintool.FETCH_BIN:
67 return None
68 return self.apt_install('u-boot-tools')
69
70 def version(self):
71 """Version handler for mkimage
72
73 Returns:
74 str: Version string for mkimage
75 """
76 out = self.run(version=True).strip()
77 if not out:
78 return super().version()
79 m_version = re.match(r'mkimage version (.*)', out)
80 return m_version.group(1) if m_version else out