]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - tools/binman/etype/image_header.py
1 # SPDX-License-Identifier: GPL-2.0+
2 # Copyright (c) 2018 Google, Inc
3 # Written by Simon Glass <sjg@chromium.org>
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 entry
import Entry
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 Entry
.__init
__(self
, 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 image_size
= self
.section
.GetImageSize() or 0
90 self
.offset
= (0 if self
.location
!= 'end' else image_size
- 8)
91 return Entry
.Pack(self
, offset
)
93 def ProcessContents(self
):
94 """Write an updated version of the FDT map to this entry
96 This is necessary since image_pos is not available when ObtainContents()
97 is called, since by then the entries have not been packed in the image.
99 return self
.ProcessContentsUpdate(self
._GetHeader
())