binman: Convert to using the mkimage bintool
[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     def __init__(self, section, etype, node):
36         super().__init__(section, etype, node)
37         self._args = fdt_util.GetString(self._node, 'args').split(' ')
38         self._mkimage_entries = OrderedDict()
39         self.align_default = None
40         self.ReadEntries()
41
42     def ObtainContents(self):
43         data = b''
44         for entry in self._mkimage_entries.values():
45             # First get the input data and put it in a file. If not available,
46             # try later.
47             if not entry.ObtainContents():
48                 return False
49             data += entry.GetData()
50         uniq = self.GetUniqueName()
51         input_fname = tools.GetOutputFilename('mkimage.%s' % uniq)
52         tools.WriteFile(input_fname, data)
53         output_fname = tools.GetOutputFilename('mkimage-out.%s' % uniq)
54         if self.mkimage.run_cmd('-d', input_fname, *self._args,
55                                 output_fname) is not None:
56             self.SetContents(tools.ReadFile(output_fname))
57         else:
58             # Bintool is missing; just use the input data as the output
59             self.record_missing_bintool(self.mkimage)
60             self.SetContents(data)
61
62         return True
63
64     def ReadEntries(self):
65         """Read the subnodes to find out what should go in this image"""
66         for node in self._node.subnodes:
67             entry = Entry.Create(self, node)
68             entry.ReadNode()
69             self._mkimage_entries[entry.name] = entry
70
71     def SetAllowFakeBlob(self, allow_fake):
72         """Set whether the sub nodes allows to create a fake blob
73
74         Args:
75             allow_fake: True if allowed, False if not allowed
76         """
77         for entry in self._mkimage_entries.values():
78             entry.SetAllowFakeBlob(allow_fake)
79
80     def CheckFakedBlobs(self, faked_blobs_list):
81         """Check if any entries in this section have faked external blobs
82
83         If there are faked blobs, the entries are added to the list
84
85         Args:
86             faked_blobs_list: List of Entry objects to be added to
87         """
88         for entry in self._mkimage_entries.values():
89             entry.CheckFakedBlobs(faked_blobs_list)
90
91     def AddBintools(self, tools):
92         self.mkimage = self.AddBintool(tools, 'mkimage')