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