binman: Convert Image to a subclass of Entry
[platform/kernel/u-boot.git] / tools / binman / etype / fmap.py
1 # SPDX-License-Identifier: GPL-2.0+
2 # Copyright (c) 2018 Google, Inc
3 # Written by Simon Glass <sjg@chromium.org>
4 #
5 # Entry-type module for a Flash map, as used by the flashrom SPI flash tool
6 #
7
8 from entry import Entry
9 import fmap_util
10 import tools
11
12
13 class Entry_fmap(Entry):
14     """An entry which contains an Fmap section
15
16     Properties / Entry arguments:
17         None
18
19     FMAP is a simple format used by flashrom, an open-source utility for
20     reading and writing the SPI flash, typically on x86 CPUs. The format
21     provides flashrom with a list of areas, so it knows what it in the flash.
22     It can then read or write just a single area, instead of the whole flash.
23
24     The format is defined by the flashrom project, in the file lib/fmap.h -
25     see www.flashrom.org/Flashrom for more information.
26
27     When used, this entry will be populated with an FMAP which reflects the
28     entries in the current image. Note that any hierarchy is squashed, since
29     FMAP does not support this.
30     """
31     def __init__(self, section, etype, node):
32         Entry.__init__(self, section, etype, node)
33
34     def _GetFmap(self):
35         """Build an FMAP from the entries in the current image
36
37         Returns:
38             FMAP binary data
39         """
40         def _AddEntries(areas, entry):
41             entries = entry.GetEntries()
42             if entries:
43                 for subentry in entries.values():
44                     _AddEntries(areas, subentry)
45             else:
46                 pos = entry.image_pos
47                 if pos is not None:
48                     pos -= entry.section.GetRootSkipAtStart()
49                 areas.append(fmap_util.FmapArea(pos or 0, entry.size or 0,
50                                             tools.FromUnicode(entry.name), 0))
51
52         entries = self.section.image.GetEntries()
53         areas = []
54         for entry in entries.values():
55             _AddEntries(areas, entry)
56         return fmap_util.EncodeFmap(self.section.GetImageSize() or 0, self.name,
57                                     areas)
58
59     def ObtainContents(self):
60         """Obtain a placeholder for the fmap contents"""
61         self.SetContents(self._GetFmap())
62         return True
63
64     def ProcessContents(self):
65         return self.ProcessContentsUpdate(self._GetFmap())