Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 2574ef6 | 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 | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 5 | # Class for an image, the output of binman |
| 6 | # |
| 7 | |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 8 | from __future__ import print_function |
| 9 | |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 10 | from collections import OrderedDict |
| 11 | from operator import attrgetter |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 12 | import re |
| 13 | import sys |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 14 | |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 15 | import fdt_util |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 16 | import bsection |
Simon Glass | 2574ef6 | 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 | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 31 | _filename: Output filename for image |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 32 | _sections: Sections present in this image (may be one or more) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 33 | """ |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 34 | def __init__(self, name, node, test=False): |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 35 | self._node = node |
| 36 | self._name = name |
| 37 | self._size = None |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 38 | self._filename = '%s.bin' % self._name |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 39 | if test: |
| 40 | self._section = bsection.Section('main-section', self._node, True) |
| 41 | else: |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 42 | self._ReadNode() |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 43 | |
| 44 | def _ReadNode(self): |
| 45 | """Read properties from the image node""" |
| 46 | self._size = fdt_util.GetInt(self._node, 'size') |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 47 | filename = fdt_util.GetString(self._node, 'filename') |
| 48 | if filename: |
| 49 | self._filename = filename |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 50 | self._section = bsection.Section('main-section', self._node) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 51 | |
| 52 | def GetEntryContents(self): |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 53 | """Call ObtainContents() for the section |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 54 | """ |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 55 | self._section.GetEntryContents() |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 56 | |
| 57 | def GetEntryPositions(self): |
| 58 | """Handle entries that want to set the position/size of other entries |
| 59 | |
| 60 | This calls each entry's GetPositions() method. If it returns a list |
| 61 | of entries to update, it updates them. |
| 62 | """ |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 63 | self._section.GetEntryPositions() |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 64 | |
| 65 | def PackEntries(self): |
| 66 | """Pack all entries into the image""" |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 67 | self._section.PackEntries() |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 68 | |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 69 | def CheckSize(self): |
| 70 | """Check that the image contents does not exceed its size, etc.""" |
| 71 | self._size = self._section.CheckSize() |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 72 | |
| 73 | def CheckEntries(self): |
| 74 | """Check that entries do not overlap or extend outside the image""" |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 75 | self._section.CheckEntries() |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 76 | |
| 77 | def ProcessEntryContents(self): |
| 78 | """Call the ProcessContents() method for each entry |
| 79 | |
| 80 | This is intended to adjust the contents as needed by the entry type. |
| 81 | """ |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 82 | self._section.ProcessEntryContents() |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 83 | |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 84 | def WriteSymbols(self): |
| 85 | """Write symbol values into binary files for access at run time""" |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 86 | self._section.WriteSymbols() |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 87 | |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 88 | def BuildImage(self): |
| 89 | """Write the image to a file""" |
| 90 | fname = tools.GetOutputFilename(self._filename) |
| 91 | with open(fname, 'wb') as fd: |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 92 | self._section.BuildSection(fd, 0) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 93 | |
Simon Glass | eca3221 | 2018-06-01 09:38:12 -0600 | [diff] [blame] | 94 | def GetEntries(self): |
| 95 | return self._section.GetEntries() |