Merge https://gitlab.denx.de/u-boot/custodians/u-boot-marvell
[oweals/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
11
12 class Entry_fmap(Entry):
13     """An entry which contains an Fmap section
14
15     Properties / Entry arguments:
16         None
17
18     FMAP is a simple format used by flashrom, an open-source utility for
19     reading and writing the SPI flash, typically on x86 CPUs. The format
20     provides flashrom with a list of areas, so it knows what it in the flash.
21     It can then read or write just a single area, instead of the whole flash.
22
23     The format is defined by the flashrom project, in the file lib/fmap.h -
24     see www.flashrom.org/Flashrom for more information.
25
26     When used, this entry will be populated with an FMAP which reflects the
27     entries in the current image. Note that any hierarchy is squashed, since
28     FMAP does not support this.
29     """
30     def __init__(self, section, etype, node):
31         Entry.__init__(self, section, etype, node)
32
33     def _GetFmap(self):
34         """Build an FMAP from the entries in the current image
35
36         Returns:
37             FMAP binary data
38         """
39         def _AddEntries(areas, entry):
40             entries = entry.GetEntries()
41             if entries:
42                 for subentry in entries.values():
43                     _AddEntries(areas, subentry)
44             else:
45                 pos = entry.image_pos
46                 if pos is not None:
47                     pos -= entry.section.GetRootSkipAtStart()
48                 areas.append(fmap_util.FmapArea(pos or 0, entry.size or 0,
49                                                 entry.name, 0))
50
51         entries = self.section._image.GetEntries()
52         areas = []
53         for entry in entries.values():
54             _AddEntries(areas, entry)
55         return fmap_util.EncodeFmap(self.section.GetImageSize() or 0, self.name,
56                                     areas)
57
58     def ObtainContents(self):
59         """Obtain a placeholder for the fmap contents"""
60         self.SetContents(self._GetFmap())
61         return True
62
63     def ProcessContents(self):
64         self.SetContents(self._GetFmap())