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 | # Creates binary images from input files controlled by a description |
| 6 | # |
| 7 | |
Simon Glass | 2ca8468 | 2019-05-14 15:53:37 -0600 | [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 | import os |
| 12 | import sys |
| 13 | import tools |
| 14 | |
Simon Glass | ac62fba | 2019-07-08 13:18:53 -0600 | [diff] [blame] | 15 | import cbfs_util |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 16 | import command |
Simon Glass | 7fe9173 | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 17 | import elf |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 18 | from image import Image |
Simon Glass | c55a50f | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 19 | import state |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 20 | import tout |
| 21 | |
| 22 | # List of images we plan to create |
| 23 | # Make this global so that it can be referenced from tests |
| 24 | images = OrderedDict() |
| 25 | |
| 26 | def _ReadImageDesc(binman_node): |
| 27 | """Read the image descriptions from the /binman node |
| 28 | |
| 29 | This normally produces a single Image object called 'image'. But if |
| 30 | multiple images are present, they will all be returned. |
| 31 | |
| 32 | Args: |
| 33 | binman_node: Node object of the /binman node |
| 34 | Returns: |
| 35 | OrderedDict of Image objects, each of which describes an image |
| 36 | """ |
| 37 | images = OrderedDict() |
| 38 | if 'multiple-images' in binman_node.props: |
| 39 | for node in binman_node.subnodes: |
| 40 | images[node.name] = Image(node.name, node) |
| 41 | else: |
| 42 | images['image'] = Image('image', binman_node) |
| 43 | return images |
| 44 | |
Simon Glass | ec3f378 | 2017-05-27 07:38:29 -0600 | [diff] [blame] | 45 | def _FindBinmanNode(dtb): |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 46 | """Find the 'binman' node in the device tree |
| 47 | |
| 48 | Args: |
Simon Glass | ec3f378 | 2017-05-27 07:38:29 -0600 | [diff] [blame] | 49 | dtb: Fdt object to scan |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 50 | Returns: |
| 51 | Node object of /binman node, or None if not found |
| 52 | """ |
Simon Glass | ec3f378 | 2017-05-27 07:38:29 -0600 | [diff] [blame] | 53 | for node in dtb.GetRoot().subnodes: |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 54 | if node.name == 'binman': |
| 55 | return node |
| 56 | return None |
| 57 | |
Simon Glass | c55a50f | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 58 | def WriteEntryDocs(modules, test_missing=None): |
| 59 | """Write out documentation for all entries |
Simon Glass | ecab897 | 2018-07-06 10:27:40 -0600 | [diff] [blame] | 60 | |
| 61 | Args: |
Simon Glass | c55a50f | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 62 | modules: List of Module objects to get docs for |
| 63 | test_missing: Used for testing only, to force an entry's documeentation |
| 64 | to show as missing even if it is present. Should be set to None in |
| 65 | normal use. |
Simon Glass | ecab897 | 2018-07-06 10:27:40 -0600 | [diff] [blame] | 66 | """ |
Simon Glass | fd8d1f7 | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 67 | from entry import Entry |
| 68 | Entry.WriteDocs(modules, test_missing) |
| 69 | |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 70 | def Binman(options, args): |
| 71 | """The main control code for binman |
| 72 | |
| 73 | This assumes that help and test options have already been dealt with. It |
| 74 | deals with the core task of building images. |
| 75 | |
| 76 | Args: |
| 77 | options: Command line options object |
| 78 | args: Command line arguments (list of strings) |
| 79 | """ |
| 80 | global images |
| 81 | |
| 82 | if options.full_help: |
| 83 | pager = os.getenv('PAGER') |
| 84 | if not pager: |
| 85 | pager = 'more' |
| 86 | fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), |
| 87 | 'README') |
| 88 | command.Run(pager, fname) |
| 89 | return 0 |
| 90 | |
| 91 | # Try to figure out which device tree contains our image description |
| 92 | if options.dt: |
| 93 | dtb_fname = options.dt |
| 94 | else: |
| 95 | board = options.board |
| 96 | if not board: |
| 97 | raise ValueError('Must provide a board to process (use -b <board>)') |
| 98 | board_pathname = os.path.join(options.build_dir, board) |
| 99 | dtb_fname = os.path.join(board_pathname, 'u-boot.dtb') |
| 100 | if not options.indir: |
| 101 | options.indir = ['.'] |
| 102 | options.indir.append(board_pathname) |
| 103 | |
| 104 | try: |
Simon Glass | 9b1a804 | 2018-07-17 13:25:34 -0600 | [diff] [blame] | 105 | # Import these here in case libfdt.py is not available, in which case |
| 106 | # the above help option still works. |
| 107 | import fdt |
| 108 | import fdt_util |
| 109 | |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 110 | tout.Init(options.verbosity) |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 111 | elf.debug = options.debug |
Simon Glass | ac62fba | 2019-07-08 13:18:53 -0600 | [diff] [blame] | 112 | cbfs_util.VERBOSE = options.verbosity > 2 |
Simon Glass | 93d1741 | 2018-09-14 04:57:23 -0600 | [diff] [blame] | 113 | state.use_fake_dtb = options.fake_dtb |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 114 | try: |
| 115 | tools.SetInputDirs(options.indir) |
| 116 | tools.PrepareOutputDir(options.outdir, options.preserve) |
Simon Glass | c7d8035 | 2019-07-08 13:18:28 -0600 | [diff] [blame] | 117 | tools.SetToolPaths(options.toolpath) |
Simon Glass | c55a50f | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 118 | state.SetEntryArgs(options.entry_arg) |
Simon Glass | ecab897 | 2018-07-06 10:27:40 -0600 | [diff] [blame] | 119 | |
| 120 | # Get the device tree ready by compiling it and copying the compiled |
| 121 | # output into a file in our output directly. Then scan it for use |
| 122 | # in binman. |
| 123 | dtb_fname = fdt_util.EnsureCompiled(dtb_fname) |
Simon Glass | 6ed45ba | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 124 | fname = tools.GetOutputFilename('u-boot.dtb.out') |
| 125 | tools.WriteFile(fname, tools.ReadFile(dtb_fname)) |
Simon Glass | ecab897 | 2018-07-06 10:27:40 -0600 | [diff] [blame] | 126 | dtb = fdt.FdtScan(fname) |
| 127 | |
Simon Glass | ec3f378 | 2017-05-27 07:38:29 -0600 | [diff] [blame] | 128 | node = _FindBinmanNode(dtb) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 129 | if not node: |
| 130 | raise ValueError("Device tree '%s' does not have a 'binman' " |
| 131 | "node" % dtb_fname) |
Simon Glass | ecab897 | 2018-07-06 10:27:40 -0600 | [diff] [blame] | 132 | |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 133 | images = _ReadImageDesc(node) |
Simon Glass | ecab897 | 2018-07-06 10:27:40 -0600 | [diff] [blame] | 134 | |
Simon Glass | 0bfa7b0 | 2018-09-14 04:57:12 -0600 | [diff] [blame] | 135 | if options.image: |
| 136 | skip = [] |
Simon Glass | 58632a7 | 2019-05-17 22:00:45 -0600 | [diff] [blame] | 137 | new_images = OrderedDict() |
Simon Glass | 5097915 | 2019-05-14 15:53:41 -0600 | [diff] [blame] | 138 | for name, image in images.items(): |
Simon Glass | 58632a7 | 2019-05-17 22:00:45 -0600 | [diff] [blame] | 139 | if name in options.image: |
| 140 | new_images[name] = image |
| 141 | else: |
Simon Glass | 0bfa7b0 | 2018-09-14 04:57:12 -0600 | [diff] [blame] | 142 | skip.append(name) |
Simon Glass | 58632a7 | 2019-05-17 22:00:45 -0600 | [diff] [blame] | 143 | images = new_images |
Simon Glass | eb833d8 | 2019-04-25 21:58:34 -0600 | [diff] [blame] | 144 | if skip and options.verbosity >= 2: |
Simon Glass | 2ca8468 | 2019-05-14 15:53:37 -0600 | [diff] [blame] | 145 | print('Skipping images: %s' % ', '.join(skip)) |
Simon Glass | 0bfa7b0 | 2018-09-14 04:57:12 -0600 | [diff] [blame] | 146 | |
Simon Glass | 539aece | 2018-09-14 04:57:22 -0600 | [diff] [blame] | 147 | state.Prepare(images, dtb) |
Simon Glass | 2a72cc7 | 2018-09-14 04:57:20 -0600 | [diff] [blame] | 148 | |
Simon Glass | ecab897 | 2018-07-06 10:27:40 -0600 | [diff] [blame] | 149 | # Prepare the device tree by making sure that any missing |
| 150 | # properties are added (e.g. 'pos' and 'size'). The values of these |
| 151 | # may not be correct yet, but we add placeholders so that the |
| 152 | # size of the device tree is correct. Later, in |
| 153 | # SetCalculatedProperties() we will insert the correct values |
| 154 | # without changing the device-tree size, thus ensuring that our |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 155 | # entry offsets remain the same. |
Simon Glass | ecab897 | 2018-07-06 10:27:40 -0600 | [diff] [blame] | 156 | for image in images.values(): |
Simon Glass | 0a98b28 | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 157 | image.ExpandEntries() |
Simon Glass | 078ab1a | 2018-07-06 10:27:41 -0600 | [diff] [blame] | 158 | if options.update_fdt: |
| 159 | image.AddMissingProperties() |
Simon Glass | ecab897 | 2018-07-06 10:27:40 -0600 | [diff] [blame] | 160 | image.ProcessFdt(dtb) |
| 161 | |
Simon Glass | 2a72cc7 | 2018-09-14 04:57:20 -0600 | [diff] [blame] | 162 | for dtb_item in state.GetFdts(): |
| 163 | dtb_item.Sync(auto_resize=True) |
| 164 | dtb_item.Pack() |
| 165 | dtb_item.Flush() |
Simon Glass | ecab897 | 2018-07-06 10:27:40 -0600 | [diff] [blame] | 166 | |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 167 | for image in images.values(): |
| 168 | # Perform all steps for this image, including checking and |
| 169 | # writing it. This means that errors found with a later |
| 170 | # image will be reported after earlier images are already |
| 171 | # completed and written, but that does not seem important. |
| 172 | image.GetEntryContents() |
Simon Glass | 3ab9598 | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 173 | image.GetEntryOffsets() |
Simon Glass | 163ed6c | 2018-09-14 04:57:36 -0600 | [diff] [blame] | 174 | try: |
| 175 | image.PackEntries() |
| 176 | image.CheckSize() |
| 177 | image.CheckEntries() |
| 178 | except Exception as e: |
| 179 | if options.map: |
| 180 | fname = image.WriteMap() |
Simon Glass | 2ca8468 | 2019-05-14 15:53:37 -0600 | [diff] [blame] | 181 | print("Wrote map file '%s' to show errors" % fname) |
Simon Glass | 163ed6c | 2018-09-14 04:57:36 -0600 | [diff] [blame] | 182 | raise |
Simon Glass | dbf6be9 | 2018-08-01 15:22:42 -0600 | [diff] [blame] | 183 | image.SetImagePos() |
Simon Glass | 078ab1a | 2018-07-06 10:27:41 -0600 | [diff] [blame] | 184 | if options.update_fdt: |
| 185 | image.SetCalculatedProperties() |
Simon Glass | 2a72cc7 | 2018-09-14 04:57:20 -0600 | [diff] [blame] | 186 | for dtb_item in state.GetFdts(): |
| 187 | dtb_item.Sync() |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 188 | image.ProcessEntryContents() |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 189 | image.WriteSymbols() |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 190 | image.BuildImage() |
Simon Glass | 3b0c382 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 191 | if options.map: |
| 192 | image.WriteMap() |
Simon Glass | 2a72cc7 | 2018-09-14 04:57:20 -0600 | [diff] [blame] | 193 | |
| 194 | # Write the updated FDTs to our output files |
| 195 | for dtb_item in state.GetFdts(): |
| 196 | tools.WriteFile(dtb_item._fname, dtb_item.GetContents()) |
| 197 | |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 198 | finally: |
| 199 | tools.FinaliseOutputDir() |
| 200 | finally: |
| 201 | tout.Uninit() |
| 202 | |
| 203 | return 0 |