binman: Allow mkimage to use a non-zero fake-blob size
[platform/kernel/u-boot.git] / tools / binman / etype / mkimage.py
1 # SPDX-License-Identifier: GPL-2.0+
2 # Copyright (c) 2016 Google, Inc
3 # Written by Simon Glass <sjg@chromium.org>
4 #
5 # Entry-type module for producing an image using mkimage
6 #
7
8 from collections import OrderedDict
9
10 from binman.entry import Entry
11 from dtoc import fdt_util
12 from patman import tools
13
14 class Entry_mkimage(Entry):
15     """Binary produced by mkimage
16
17     Properties / Entry arguments:
18         - datafile: Filename for -d argument
19         - args: Other arguments to pass
20
21     The data passed to mkimage is collected from subnodes of the mkimage node,
22     e.g.::
23
24         mkimage {
25             args = "-n test -T imximage";
26
27             u-boot-spl {
28             };
29         };
30
31     This calls mkimage to create an imximage with u-boot-spl.bin as the input
32     file. The output from mkimage then becomes part of the image produced by
33     binman.
34
35     To use CONFIG options in the arguments, use a string list instead, as in
36     this example which also produces four arguments::
37
38         mkimage {
39             args = "-n", CONFIG_SYS_SOC, "-T imximage";
40
41             u-boot-spl {
42             };
43         };
44
45     """
46     def __init__(self, section, etype, node):
47         super().__init__(section, etype, node)
48         self._args = fdt_util.GetArgs(self._node, 'args')
49         self._mkimage_entries = OrderedDict()
50         self.align_default = None
51         self.ReadEntries()
52
53     def ObtainContents(self):
54         # Use a non-zero size for any fake files to keep mkimage happy
55         data, input_fname, uniq = self.collect_contents_to_file(
56             self._mkimage_entries.values(), 'mkimage', 1024)
57         if data is None:
58             return False
59         output_fname = tools.get_output_filename('mkimage-out.%s' % uniq)
60         if self.mkimage.run_cmd('-d', input_fname, *self._args,
61                                 output_fname) is not None:
62             self.SetContents(tools.read_file(output_fname))
63         else:
64             # Bintool is missing; just use the input data as the output
65             self.record_missing_bintool(self.mkimage)
66             self.SetContents(data)
67
68         return True
69
70     def ReadEntries(self):
71         """Read the subnodes to find out what should go in this image"""
72         for node in self._node.subnodes:
73             entry = Entry.Create(self, node)
74             entry.ReadNode()
75             self._mkimage_entries[entry.name] = entry
76
77     def SetAllowMissing(self, allow_missing):
78         """Set whether a section allows missing external blobs
79
80         Args:
81             allow_missing: True if allowed, False if not allowed
82         """
83         self.allow_missing = allow_missing
84         for entry in self._mkimage_entries.values():
85             entry.SetAllowMissing(allow_missing)
86
87     def SetAllowFakeBlob(self, allow_fake):
88         """Set whether the sub nodes allows to create a fake blob
89
90         Args:
91             allow_fake: True if allowed, False if not allowed
92         """
93         for entry in self._mkimage_entries.values():
94             entry.SetAllowFakeBlob(allow_fake)
95
96     def CheckFakedBlobs(self, faked_blobs_list):
97         """Check if any entries in this section have faked external blobs
98
99         If there are faked blobs, the entries are added to the list
100
101         Args:
102             faked_blobs_list: List of Entry objects to be added to
103         """
104         for entry in self._mkimage_entries.values():
105             entry.CheckFakedBlobs(faked_blobs_list)
106
107     def AddBintools(self, btools):
108         self.mkimage = self.AddBintool(btools, 'mkimage')