blob: 835b66c99f5f188a74e9934860552e74760fa370 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0+
Simon Glass2574ef62016-11-25 20:15:51 -07002# Copyright (c) 2016 Google, Inc
3# Written by Simon Glass <sjg@chromium.org>
4#
Simon Glass2574ef62016-11-25 20:15:51 -07005# Class for an image, the output of binman
6#
7
Simon Glass4ca8e042017-11-13 18:55:01 -07008from __future__ import print_function
9
Simon Glass2574ef62016-11-25 20:15:51 -070010from collections import OrderedDict
11from operator import attrgetter
Simon Glass4ca8e042017-11-13 18:55:01 -070012import re
13import sys
Simon Glass2574ef62016-11-25 20:15:51 -070014
Simon Glass2574ef62016-11-25 20:15:51 -070015import fdt_util
Simon Glasseca32212018-06-01 09:38:12 -060016import bsection
Simon Glass2574ef62016-11-25 20:15:51 -070017import tools
18
19class Image:
20 """A Image, representing an output from binman
21
22 An image is comprised of a collection of entries each containing binary
23 data. The image size must be large enough to hold all of this data.
24
25 This class implements the various operations needed for images.
26
27 Atrtributes:
28 _node: Node object that contains the image definition in device tree
29 _name: Image name
30 _size: Image size in bytes, or None if not known yet
Simon Glass2574ef62016-11-25 20:15:51 -070031 _filename: Output filename for image
Simon Glasseca32212018-06-01 09:38:12 -060032 _sections: Sections present in this image (may be one or more)
Simon Glass1e324002018-06-01 09:38:19 -060033
34 Args:
35 test: True if this is being called from a test of Images. This this case
36 there is no device tree defining the structure of the section, so
37 we create a section manually.
Simon Glass2574ef62016-11-25 20:15:51 -070038 """
Simon Glass4ca8e042017-11-13 18:55:01 -070039 def __init__(self, name, node, test=False):
Simon Glass2574ef62016-11-25 20:15:51 -070040 self._node = node
41 self._name = name
42 self._size = None
Simon Glass2574ef62016-11-25 20:15:51 -070043 self._filename = '%s.bin' % self._name
Simon Glasseca32212018-06-01 09:38:12 -060044 if test:
45 self._section = bsection.Section('main-section', self._node, True)
46 else:
Simon Glass4ca8e042017-11-13 18:55:01 -070047 self._ReadNode()
Simon Glass2574ef62016-11-25 20:15:51 -070048
49 def _ReadNode(self):
50 """Read properties from the image node"""
51 self._size = fdt_util.GetInt(self._node, 'size')
Simon Glass2574ef62016-11-25 20:15:51 -070052 filename = fdt_util.GetString(self._node, 'filename')
53 if filename:
54 self._filename = filename
Simon Glasseca32212018-06-01 09:38:12 -060055 self._section = bsection.Section('main-section', self._node)
Simon Glass2574ef62016-11-25 20:15:51 -070056
57 def GetEntryContents(self):
Simon Glasseca32212018-06-01 09:38:12 -060058 """Call ObtainContents() for the section
Simon Glass2574ef62016-11-25 20:15:51 -070059 """
Simon Glasseca32212018-06-01 09:38:12 -060060 self._section.GetEntryContents()
Simon Glass2574ef62016-11-25 20:15:51 -070061
62 def GetEntryPositions(self):
63 """Handle entries that want to set the position/size of other entries
64
65 This calls each entry's GetPositions() method. If it returns a list
66 of entries to update, it updates them.
67 """
Simon Glasseca32212018-06-01 09:38:12 -060068 self._section.GetEntryPositions()
Simon Glass2574ef62016-11-25 20:15:51 -070069
70 def PackEntries(self):
71 """Pack all entries into the image"""
Simon Glasseca32212018-06-01 09:38:12 -060072 self._section.PackEntries()
Simon Glass2574ef62016-11-25 20:15:51 -070073
Simon Glasseca32212018-06-01 09:38:12 -060074 def CheckSize(self):
75 """Check that the image contents does not exceed its size, etc."""
76 self._size = self._section.CheckSize()
Simon Glass2574ef62016-11-25 20:15:51 -070077
78 def CheckEntries(self):
79 """Check that entries do not overlap or extend outside the image"""
Simon Glasseca32212018-06-01 09:38:12 -060080 self._section.CheckEntries()
Simon Glass2574ef62016-11-25 20:15:51 -070081
82 def ProcessEntryContents(self):
83 """Call the ProcessContents() method for each entry
84
85 This is intended to adjust the contents as needed by the entry type.
86 """
Simon Glasseca32212018-06-01 09:38:12 -060087 self._section.ProcessEntryContents()
Simon Glass2574ef62016-11-25 20:15:51 -070088
Simon Glass4ca8e042017-11-13 18:55:01 -070089 def WriteSymbols(self):
90 """Write symbol values into binary files for access at run time"""
Simon Glasseca32212018-06-01 09:38:12 -060091 self._section.WriteSymbols()
Simon Glass4ca8e042017-11-13 18:55:01 -070092
Simon Glass2574ef62016-11-25 20:15:51 -070093 def BuildImage(self):
94 """Write the image to a file"""
95 fname = tools.GetOutputFilename(self._filename)
96 with open(fname, 'wb') as fd:
Simon Glasseca32212018-06-01 09:38:12 -060097 self._section.BuildSection(fd, 0)
Simon Glass2574ef62016-11-25 20:15:51 -070098
Simon Glasseca32212018-06-01 09:38:12 -060099 def GetEntries(self):
100 return self._section.GetEntries()
Simon Glass30732662018-06-01 09:38:20 -0600101
102 def WriteMap(self):
103 """Write a map of the image to a .map file"""
104 filename = '%s.map' % self._name
105 fname = tools.GetOutputFilename(filename)
106 with open(fname, 'w') as fd:
107 print('%8s %8s %s' % ('Position', 'Size', 'Name'), file=fd)
108 self._section.WriteMap(fd, 0)