Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 2 | # Copyright (c) 2016 Google, Inc |
| 3 | # Written by Simon Glass <sjg@chromium.org> |
| 4 | # |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 5 | # Class for an image, the output of binman |
| 6 | # |
| 7 | |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 8 | from __future__ import print_function |
| 9 | |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 10 | from collections import OrderedDict |
| 11 | from operator import attrgetter |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 12 | import re |
| 13 | import sys |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 14 | |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 15 | import fdt_util |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 16 | import bsection |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 17 | import tools |
| 18 | |
| 19 | class 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 Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 31 | _filename: Output filename for image |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 32 | _sections: Sections present in this image (may be one or more) |
Simon Glass | 7ae5f31 | 2018-06-01 09:38:19 -0600 | [diff] [blame] | 33 | |
| 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 Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 38 | """ |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 39 | def __init__(self, name, node, test=False): |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 40 | self._node = node |
| 41 | self._name = name |
| 42 | self._size = None |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 43 | self._filename = '%s.bin' % self._name |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 44 | if test: |
| 45 | self._section = bsection.Section('main-section', self._node, True) |
| 46 | else: |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 47 | self._ReadNode() |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 48 | |
| 49 | def _ReadNode(self): |
| 50 | """Read properties from the image node""" |
| 51 | self._size = fdt_util.GetInt(self._node, 'size') |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 52 | filename = fdt_util.GetString(self._node, 'filename') |
| 53 | if filename: |
| 54 | self._filename = filename |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 55 | self._section = bsection.Section('main-section', self._node) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 56 | |
Simon Glass | 078ab1a | 2018-07-06 10:27:41 -0600 | [diff] [blame] | 57 | def AddMissingProperties(self): |
| 58 | """Add properties that are not present in the device tree |
| 59 | |
| 60 | When binman has completed packing the entries the position and size of |
| 61 | each entry are known. But before this the device tree may not specify |
| 62 | these. Add any missing properties, with a dummy value, so that the |
| 63 | size of the entry is correct. That way we can insert the correct values |
| 64 | later. |
| 65 | """ |
| 66 | self._section.AddMissingProperties() |
| 67 | |
Simon Glass | ecab897 | 2018-07-06 10:27:40 -0600 | [diff] [blame] | 68 | def ProcessFdt(self, fdt): |
| 69 | return self._section.ProcessFdt(fdt) |
| 70 | |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 71 | def GetEntryContents(self): |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 72 | """Call ObtainContents() for the section |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 73 | """ |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 74 | self._section.GetEntryContents() |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 75 | |
| 76 | def GetEntryPositions(self): |
| 77 | """Handle entries that want to set the position/size of other entries |
| 78 | |
| 79 | This calls each entry's GetPositions() method. If it returns a list |
| 80 | of entries to update, it updates them. |
| 81 | """ |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 82 | self._section.GetEntryPositions() |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 83 | |
| 84 | def PackEntries(self): |
| 85 | """Pack all entries into the image""" |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 86 | self._section.PackEntries() |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 87 | |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 88 | def CheckSize(self): |
| 89 | """Check that the image contents does not exceed its size, etc.""" |
| 90 | self._size = self._section.CheckSize() |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 91 | |
| 92 | def CheckEntries(self): |
| 93 | """Check that entries do not overlap or extend outside the image""" |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 94 | self._section.CheckEntries() |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 95 | |
Simon Glass | 078ab1a | 2018-07-06 10:27:41 -0600 | [diff] [blame] | 96 | def SetCalculatedProperties(self): |
| 97 | self._section.SetCalculatedProperties() |
| 98 | |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 99 | def ProcessEntryContents(self): |
| 100 | """Call the ProcessContents() method for each entry |
| 101 | |
| 102 | This is intended to adjust the contents as needed by the entry type. |
| 103 | """ |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 104 | self._section.ProcessEntryContents() |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 105 | |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 106 | def WriteSymbols(self): |
| 107 | """Write symbol values into binary files for access at run time""" |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 108 | self._section.WriteSymbols() |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 109 | |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 110 | def BuildImage(self): |
| 111 | """Write the image to a file""" |
| 112 | fname = tools.GetOutputFilename(self._filename) |
| 113 | with open(fname, 'wb') as fd: |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 114 | self._section.BuildSection(fd, 0) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 115 | |
Simon Glass | 8f1da50 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 116 | def GetEntries(self): |
| 117 | return self._section.GetEntries() |
Simon Glass | 3b0c382 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 118 | |
| 119 | def WriteMap(self): |
| 120 | """Write a map of the image to a .map file""" |
| 121 | filename = '%s.map' % self._name |
| 122 | fname = tools.GetOutputFilename(filename) |
| 123 | with open(fname, 'w') as fd: |
| 124 | print('%8s %8s %s' % ('Position', 'Size', 'Name'), file=fd) |
| 125 | self._section.WriteMap(fd, 0) |