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 8from binman.entry import Entry 9from binman import fmap_util 10from u_boot_pylib import tools 11from u_boot_pylib.tools import to_hex_size 12from u_boot_pylib import tout 13 14 15class Entry_fmap(Entry): 16 """An entry which contains an Fmap section 17 18 Properties / Entry arguments: 19 None 20 21 FMAP is a simple format used by flashrom, an open-source utility for 22 reading and writing the SPI flash, typically on x86 CPUs. The format 23 provides flashrom with a list of areas, so it knows what it in the flash. 24 It can then read or write just a single area, instead of the whole flash. 25 26 The format is defined by the flashrom project, in the file lib/fmap.h - 27 see www.flashrom.org/Flashrom for more information. 28 29 When used, this entry will be populated with an FMAP which reflects the 30 entries in the current image. Note that any hierarchy is squashed, since 31 FMAP does not support this. Sections are represented as an area appearing 32 before its contents, so that it is possible to reconstruct the hierarchy 33 from the FMAP by using the offset information. This convention does not 34 seem to be documented, but is used in Chromium OS. 35 36 To mark an area as preserved, use the normal 'preserved' flag in the entry. 37 This will result in the corresponding FMAP area having the 38 FMAP_AREA_PRESERVE flag. This flag does not automatically propagate down to 39 child entries. 40 41 CBFS entries appear as a single entry, i.e. the sub-entries are ignored. 42 """ 43 def __init__(self, section, etype, node): 44 super().__init__(section, etype, node) 45 46 def _GetFmap(self): 47 """Build an FMAP from the entries in the current image 48 49 Returns: 50 FMAP binary data 51 """ 52 def _AddEntries(areas, entry): 53 entries = entry.GetEntries() 54 tout.debug("fmap: Add entry '%s' type '%s' (%s subentries)" % 55 (entry.GetPath(), entry.etype, to_hex_size(entries))) 56 57 # Collect any flag (separate lines to ensure code coverage) 58 flags = 0 59 if entry.preserve: 60 flags = fmap_util.FMAP_AREA_PRESERVE 61 62 if entries and entry.etype != 'cbfs': 63 # Create an area for the section, which encompasses all entries 64 # within it 65 if entry.image_pos is None: 66 pos = 0 67 else: 68 pos = entry.image_pos - entry.GetRootSkipAtStart() 69 70 # Drop @ symbols in name 71 name = entry.name.replace('@', '') 72 areas.append( 73 fmap_util.FmapArea(pos, entry.size or 0, name, flags)) 74 for subentry in entries.values(): 75 _AddEntries(areas, subentry) 76 else: 77 pos = entry.image_pos 78 if pos is not None: 79 pos -= entry.section.GetRootSkipAtStart() 80 areas.append(fmap_util.FmapArea(pos or 0, entry.size or 0, 81 entry.name, flags)) 82 83 entries = self.GetImage().GetEntries() 84 areas = [] 85 for entry in entries.values(): 86 _AddEntries(areas, entry) 87 return fmap_util.EncodeFmap(self.section.GetImageSize() or 0, self.name, 88 areas) 89 90 def ObtainContents(self): 91 """Obtain a placeholder for the fmap contents""" 92 self.SetContents(self._GetFmap()) 93 return True 94 95 def ProcessContents(self): 96 return self.ProcessContentsUpdate(self._GetFmap()) 97