1 # SPDX-License-Identifier: GPL-2.0+
2 # Copyright (c) 2018 Google, Inc
5 """Entry-type module for an image header which points to the FDT map
7 This creates an 8-byte entry with a magic number and the offset of the FDT map
8 (which is another entry in the image), relative to the start or end of the
14 from binman.entry import Entry
15 from dtoc import fdt_util
17 IMAGE_HEADER_MAGIC = b'BinM'
20 def LocateHeaderOffset(data):
21 """Search an image for an image header
27 Offset of image header in the image, or None if not found
29 hdr_pos = data.find(IMAGE_HEADER_MAGIC)
32 hdr = data[hdr_pos:hdr_pos + IMAGE_HEADER_LEN]
33 if len(hdr) == IMAGE_HEADER_LEN:
34 offset = struct.unpack('<I', hdr[4:])[0]
35 if hdr_pos == len(data) - IMAGE_HEADER_LEN:
36 pos = size + offset - (1 << 32)
42 class Entry_image_header(Entry):
43 """An entry which contains a pointer to the FDT map
45 Properties / Entry arguments:
46 location: Location of header ("start" or "end" of image). This is
47 optional. If omitted then the entry must have an offset property.
49 This adds an 8-byte entry to the start or end of the image, pointing to the
50 location of the FDT map. The format is a magic number followed by an offset
51 from the start or end of the image, in twos-compliment format.
53 This entry must be in the top-level part of the image.
55 NOTE: If the location is at the start/end, you will probably need to specify
56 sort-by-offset for the image, unless you actually put the image header
57 first/last in the entry list.
59 def __init__(self, section, etype, node):
60 super().__init__(section, etype, node)
61 self.location = fdt_util.GetString(self._node, 'location')
64 image_pos = self.GetSiblingImagePos('fdtmap')
65 if image_pos == False:
66 self.Raise("'image_header' section must have an 'fdtmap' sibling")
67 elif image_pos is None:
68 # This will be available when called from ProcessContents(), but not
69 # when called from ObtainContents()
72 image_size = self.section.GetImageSize() or 0
73 base = (0 if self.location != 'end' else image_size)
74 offset = (image_pos - base) & 0xffffffff
75 data = IMAGE_HEADER_MAGIC + struct.pack('<I', offset)
78 def ObtainContents(self):
79 """Obtain a placeholder for the header contents"""
80 self.SetContents(self._GetHeader())
83 def Pack(self, offset):
84 """Special pack method to set the offset to start/end of image"""
86 if self.location not in ['start', 'end']:
87 self.Raise("Invalid location '%s', expected 'start' or 'end'" %
89 order = self.GetSiblingOrder()
90 if self.location != order and not self.section.GetSort():
91 self.Raise("Invalid sibling order '%s' for image-header: Must be at '%s' to match location" %
92 (order, self.location))
93 if self.location != 'end':
96 image_size = self.section.GetImageSize()
97 if image_size is None:
98 # We don't know the image, but this must be the last entry,
99 # so we can assume it goes
102 offset = image_size - IMAGE_HEADER_LEN
103 offset += self.section.GetStartOffset()
104 return super().Pack(offset)
106 def ProcessContents(self):
107 """Write an updated version of the FDT map to this entry
109 This is necessary since image_pos is not available when ObtainContents()
110 is called, since by then the entries have not been packed in the image.
112 return self.ProcessContentsUpdate(self._GetHeader())