xref: /openbmc/u-boot/tools/binman/etype/fmap.py (revision 592cd5de)
111e36cceSSimon Glass# SPDX-License-Identifier: GPL-2.0+
211e36cceSSimon Glass# Copyright (c) 2018 Google, Inc
311e36cceSSimon Glass# Written by Simon Glass <sjg@chromium.org>
411e36cceSSimon Glass#
511e36cceSSimon Glass# Entry-type module for a Flash map, as used by the flashrom SPI flash tool
611e36cceSSimon Glass#
711e36cceSSimon Glass
811e36cceSSimon Glassfrom entry import Entry
911e36cceSSimon Glassimport fmap_util
1011e36cceSSimon Glass
1111e36cceSSimon Glass
1211e36cceSSimon Glassclass Entry_fmap(Entry):
1311e36cceSSimon Glass    """An entry which contains an Fmap section
1411e36cceSSimon Glass
1511e36cceSSimon Glass    Properties / Entry arguments:
1611e36cceSSimon Glass        None
1711e36cceSSimon Glass
1811e36cceSSimon Glass    FMAP is a simple format used by flashrom, an open-source utility for
1911e36cceSSimon Glass    reading and writing the SPI flash, typically on x86 CPUs. The format
2011e36cceSSimon Glass    provides flashrom with a list of areas, so it knows what it in the flash.
2111e36cceSSimon Glass    It can then read or write just a single area, instead of the whole flash.
2211e36cceSSimon Glass
2311e36cceSSimon Glass    The format is defined by the flashrom project, in the file lib/fmap.h -
2411e36cceSSimon Glass    see www.flashrom.org/Flashrom for more information.
2511e36cceSSimon Glass
2611e36cceSSimon Glass    When used, this entry will be populated with an FMAP which reflects the
2711e36cceSSimon Glass    entries in the current image. Note that any hierarchy is squashed, since
2811e36cceSSimon Glass    FMAP does not support this.
2911e36cceSSimon Glass    """
3011e36cceSSimon Glass    def __init__(self, section, etype, node):
3111e36cceSSimon Glass        Entry.__init__(self, section, etype, node)
3211e36cceSSimon Glass
3311e36cceSSimon Glass    def _GetFmap(self):
3411e36cceSSimon Glass        """Build an FMAP from the entries in the current image
3511e36cceSSimon Glass
3611e36cceSSimon Glass        Returns:
3711e36cceSSimon Glass            FMAP binary data
3811e36cceSSimon Glass        """
3911e36cceSSimon Glass        def _AddEntries(areas, entry):
4011e36cceSSimon Glass            entries = entry.GetEntries()
4111e36cceSSimon Glass            if entries:
4211e36cceSSimon Glass                for subentry in entries.values():
4311e36cceSSimon Glass                    _AddEntries(areas, subentry)
4411e36cceSSimon Glass            else:
45*f8f8df6eSSimon Glass                pos = entry.image_pos
46*f8f8df6eSSimon Glass                if pos is not None:
47*f8f8df6eSSimon Glass                    pos -= entry.section.GetRootSkipAtStart()
48*f8f8df6eSSimon Glass                areas.append(fmap_util.FmapArea(pos or 0, entry.size or 0,
49*f8f8df6eSSimon Glass                                                entry.name, 0))
5011e36cceSSimon Glass
51*f8f8df6eSSimon Glass        entries = self.section._image.GetEntries()
5211e36cceSSimon Glass        areas = []
5311e36cceSSimon Glass        for entry in entries.values():
5411e36cceSSimon Glass            _AddEntries(areas, entry)
55*f8f8df6eSSimon Glass        return fmap_util.EncodeFmap(self.section.GetImageSize() or 0, self.name,
5611e36cceSSimon Glass                                    areas)
5711e36cceSSimon Glass
5811e36cceSSimon Glass    def ObtainContents(self):
5911e36cceSSimon Glass        """Obtain a placeholder for the fmap contents"""
6011e36cceSSimon Glass        self.SetContents(self._GetFmap())
6111e36cceSSimon Glass        return True
6211e36cceSSimon Glass
6311e36cceSSimon Glass    def ProcessContents(self):
6411e36cceSSimon Glass        self.SetContents(self._GetFmap())
65