2018-05-06 17:58:06 -04:00
|
|
|
# SPDX-License-Identifier: GPL-2.0+
|
2016-11-25 20:15:51 -07:00
|
|
|
# Copyright (c) 2016 Google, Inc
|
|
|
|
# Written by Simon Glass <sjg@chromium.org>
|
|
|
|
#
|
|
|
|
# Class for an image, the output of binman
|
|
|
|
#
|
|
|
|
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-13 18:55:01 -07:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2016-11-25 20:15:51 -07:00
|
|
|
from collections import OrderedDict
|
|
|
|
from operator import attrgetter
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-13 18:55:01 -07:00
|
|
|
import re
|
|
|
|
import sys
|
2016-11-25 20:15:51 -07:00
|
|
|
|
2019-07-08 14:25:47 -06:00
|
|
|
from entry import Entry
|
2019-07-08 14:25:46 -06:00
|
|
|
from etype import fdtmap
|
|
|
|
from etype import image_header
|
2019-07-08 14:25:47 -06:00
|
|
|
from etype import section
|
2019-07-08 14:25:46 -06:00
|
|
|
import fdt
|
2016-11-25 20:15:51 -07:00
|
|
|
import fdt_util
|
|
|
|
import tools
|
|
|
|
|
2019-07-08 14:25:47 -06:00
|
|
|
class Image(section.Entry_section):
|
2016-11-25 20:15:51 -07:00
|
|
|
"""A Image, representing an output from binman
|
|
|
|
|
|
|
|
An image is comprised of a collection of entries each containing binary
|
|
|
|
data. The image size must be large enough to hold all of this data.
|
|
|
|
|
|
|
|
This class implements the various operations needed for images.
|
|
|
|
|
2019-07-08 14:25:47 -06:00
|
|
|
Attributes:
|
|
|
|
filename: Output filename for image
|
2018-06-01 09:38:19 -06:00
|
|
|
|
|
|
|
Args:
|
|
|
|
test: True if this is being called from a test of Images. This this case
|
|
|
|
there is no device tree defining the structure of the section, so
|
|
|
|
we create a section manually.
|
2016-11-25 20:15:51 -07:00
|
|
|
"""
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-13 18:55:01 -07:00
|
|
|
def __init__(self, name, node, test=False):
|
2019-07-08 14:25:47 -06:00
|
|
|
self.image = self
|
|
|
|
section.Entry_section.__init__(self, None, 'section', node, test)
|
|
|
|
self.name = 'main-section'
|
|
|
|
self.image_name = name
|
|
|
|
self._filename = '%s.bin' % self.image_name
|
|
|
|
if not test:
|
|
|
|
filename = fdt_util.GetString(self._node, 'filename')
|
|
|
|
if filename:
|
|
|
|
self._filename = filename
|
2016-11-25 20:15:51 -07:00
|
|
|
|
2019-07-08 14:25:46 -06:00
|
|
|
@classmethod
|
|
|
|
def FromFile(cls, fname):
|
|
|
|
"""Convert an image file into an Image for use in binman
|
|
|
|
|
|
|
|
Args:
|
|
|
|
fname: Filename of image file to read
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Image object on success
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
ValueError if something goes wrong
|
|
|
|
"""
|
|
|
|
data = tools.ReadFile(fname)
|
|
|
|
size = len(data)
|
|
|
|
|
|
|
|
# First look for an image header
|
|
|
|
pos = image_header.LocateHeaderOffset(data)
|
|
|
|
if pos is None:
|
|
|
|
# Look for the FDT map
|
|
|
|
pos = fdtmap.LocateFdtmap(data)
|
|
|
|
if pos is None:
|
|
|
|
raise ValueError('Cannot find FDT map in image')
|
|
|
|
|
|
|
|
# We don't know the FDT size, so check its header first
|
|
|
|
probe_dtb = fdt.Fdt.FromData(
|
|
|
|
data[pos + fdtmap.FDTMAP_HDR_LEN:pos + 256])
|
|
|
|
dtb_size = probe_dtb.GetFdtObj().totalsize()
|
|
|
|
fdtmap_data = data[pos:pos + dtb_size + fdtmap.FDTMAP_HDR_LEN]
|
|
|
|
dtb = fdt.Fdt.FromData(fdtmap_data[fdtmap.FDTMAP_HDR_LEN:])
|
|
|
|
dtb.Scan()
|
|
|
|
|
|
|
|
# Return an Image with the associated nodes
|
|
|
|
return Image('image', dtb.GetRoot())
|
|
|
|
|
2019-07-08 14:25:37 -06:00
|
|
|
def Raise(self, msg):
|
|
|
|
"""Convenience function to raise an error referencing an image"""
|
|
|
|
raise ValueError("Image '%s': %s" % (self._node.path, msg))
|
|
|
|
|
2016-11-25 20:15:51 -07:00
|
|
|
def PackEntries(self):
|
|
|
|
"""Pack all entries into the image"""
|
2019-07-08 14:25:47 -06:00
|
|
|
section.Entry_section.Pack(self, 0)
|
2018-07-06 10:27:41 -06:00
|
|
|
|
2018-08-01 15:22:42 -06:00
|
|
|
def SetImagePos(self):
|
2019-07-08 14:25:47 -06:00
|
|
|
# This first section in the image so it starts at 0
|
|
|
|
section.Entry_section.SetImagePos(self, 0)
|
2018-08-01 15:22:42 -06:00
|
|
|
|
2016-11-25 20:15:51 -07:00
|
|
|
def ProcessEntryContents(self):
|
|
|
|
"""Call the ProcessContents() method for each entry
|
|
|
|
|
|
|
|
This is intended to adjust the contents as needed by the entry type.
|
2019-07-08 14:25:35 -06:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
True if the new data size is OK, False if expansion is needed
|
2016-11-25 20:15:51 -07:00
|
|
|
"""
|
2019-07-08 14:25:47 -06:00
|
|
|
sizes_ok = True
|
|
|
|
for entry in self._entries.values():
|
|
|
|
if not entry.ProcessContents():
|
|
|
|
sizes_ok = False
|
|
|
|
print("Entry '%s' size change" % self._node.path)
|
|
|
|
return sizes_ok
|
2016-11-25 20:15:51 -07:00
|
|
|
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-13 18:55:01 -07:00
|
|
|
def WriteSymbols(self):
|
|
|
|
"""Write symbol values into binary files for access at run time"""
|
2019-07-08 14:25:47 -06:00
|
|
|
section.Entry_section.WriteSymbols(self, self)
|
|
|
|
|
|
|
|
def BuildSection(self, fd, base_offset):
|
|
|
|
"""Write the section to a file"""
|
|
|
|
fd.seek(base_offset)
|
|
|
|
fd.write(self.GetData())
|
binman: Support accessing binman tables at run time
Binman construct images consisting of multiple binary files. These files
sometimes need to know (at run timme) where their peers are located. For
example, SPL may want to know where U-Boot is located in the image, so
that it can jump to U-Boot correctly on boot.
In general the positions where the binaries end up after binman has
finished packing them cannot be known at compile time. One reason for
this is that binman does not know the size of the binaries until
everything is compiled, linked and converted to binaries with objcopy.
To make this work, we add a feature to binman which checks each binary
for symbol names starting with '_binman'. These are then decoded to figure
out which entry and property they refer to. Then binman writes the value
of this symbol into the appropriate binary. With this, the symbol will
have the correct value at run time.
Macros are used to make this easier to use. As an example, this declares
a symbol that will access the 'u-boot-spl' entry to find the 'pos' value
(i.e. the position of SPL in the image):
binman_sym_declare(unsigned long, u_boot_spl, pos);
This converts to a symbol called '_binman_u_boot_spl_prop_pos' in any
binary that includes it. Binman then updates the value in that binary,
ensuring that it can be accessed at runtime with:
ulong u_boot_pos = binman_sym(ulong, u_boot_spl, pos);
This assigns the variable u_boot_pos to the position of SPL in the image.
Signed-off-by: Simon Glass <sjg@chromium.org>
2017-11-13 18:55:01 -07:00
|
|
|
|
2016-11-25 20:15:51 -07:00
|
|
|
def BuildImage(self):
|
|
|
|
"""Write the image to a file"""
|
|
|
|
fname = tools.GetOutputFilename(self._filename)
|
|
|
|
with open(fname, 'wb') as fd:
|
2019-07-08 14:25:47 -06:00
|
|
|
self.BuildSection(fd, 0)
|
2018-06-01 09:38:20 -06:00
|
|
|
|
|
|
|
def WriteMap(self):
|
2018-09-14 04:57:36 -06:00
|
|
|
"""Write a map of the image to a .map file
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Filename of map file written
|
|
|
|
"""
|
2019-07-08 14:25:47 -06:00
|
|
|
filename = '%s.map' % self.image_name
|
2018-06-01 09:38:20 -06:00
|
|
|
fname = tools.GetOutputFilename(filename)
|
|
|
|
with open(fname, 'w') as fd:
|
2018-07-17 13:25:49 -06:00
|
|
|
print('%8s %8s %8s %s' % ('ImagePos', 'Offset', 'Size', 'Name'),
|
|
|
|
file=fd)
|
2019-07-08 14:25:47 -06:00
|
|
|
section.Entry_section.WriteMap(self, fd, 0)
|
2018-09-14 04:57:36 -06:00
|
|
|
return fname
|
2019-07-08 14:25:43 -06:00
|
|
|
|
|
|
|
def BuildEntryList(self):
|
|
|
|
"""List the files in an image
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
List of entry.EntryInfo objects describing all entries in the image
|
|
|
|
"""
|
|
|
|
entries = []
|
2019-07-08 14:25:47 -06:00
|
|
|
self.ListEntries(entries, 0)
|
2019-07-08 14:25:43 -06:00
|
|
|
return entries
|