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