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 | # Creates binary images from input files controlled by a description |
| 6 | # |
| 7 | |
Simon Glass | 7cca27d | 2019-05-14 15:53:37 -0600 | [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 | import os |
| 12 | import sys |
| 13 | import tools |
| 14 | |
Simon Glass | 1de3448 | 2019-07-08 13:18:53 -0600 | [diff] [blame] | 15 | import cbfs_util |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 16 | import command |
Simon Glass | 075a45c | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 17 | import elf |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 18 | from image import Image |
Simon Glass | 29aa736 | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 19 | import state |
Simon Glass | 2574ef6 | 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 | 22c92ca | 2017-05-27 07:38:29 -0600 | [diff] [blame] | 45 | def _FindBinmanNode(dtb): |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 46 | """Find the 'binman' node in the device tree |
| 47 | |
| 48 | Args: |
Simon Glass | 22c92ca | 2017-05-27 07:38:29 -0600 | [diff] [blame] | 49 | dtb: Fdt object to scan |
Simon Glass | 2574ef6 | 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 | 22c92ca | 2017-05-27 07:38:29 -0600 | [diff] [blame] | 53 | for node in dtb.GetRoot().subnodes: |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 54 | if node.name == 'binman': |
| 55 | return node |
| 56 | return None |
| 57 | |
Simon Glass | 29aa736 | 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 | 9230773 | 2018-07-06 10:27:40 -0600 | [diff] [blame] | 60 | |
| 61 | Args: |
Simon Glass | 29aa736 | 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 | 9230773 | 2018-07-06 10:27:40 -0600 | [diff] [blame] | 66 | """ |
Simon Glass | 969616c | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 67 | from entry import Entry |
| 68 | Entry.WriteDocs(modules, test_missing) |
| 69 | |
Simon Glass | b2fd11d | 2019-07-08 14:25:48 -0600 | [diff] [blame] | 70 | |
| 71 | def ListEntries(image_fname, entry_paths): |
| 72 | """List the entries in an image |
| 73 | |
| 74 | This decodes the supplied image and displays a table of entries from that |
| 75 | image, preceded by a header. |
| 76 | |
| 77 | Args: |
| 78 | image_fname: Image filename to process |
| 79 | entry_paths: List of wildcarded paths (e.g. ['*dtb*', 'u-boot*', |
| 80 | 'section/u-boot']) |
| 81 | """ |
| 82 | image = Image.FromFile(image_fname) |
| 83 | |
| 84 | entries, lines, widths = image.GetListEntries(entry_paths) |
| 85 | |
| 86 | num_columns = len(widths) |
| 87 | for linenum, line in enumerate(lines): |
| 88 | if linenum == 1: |
| 89 | # Print header line |
| 90 | print('-' * (sum(widths) + num_columns * 2)) |
| 91 | out = '' |
| 92 | for i, item in enumerate(line): |
| 93 | width = -widths[i] |
| 94 | if item.startswith('>'): |
| 95 | width = -width |
| 96 | item = item[1:] |
| 97 | txt = '%*s ' % (width, item) |
| 98 | out += txt |
| 99 | print(out.rstrip()) |
| 100 | |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 101 | def Binman(args): |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 102 | """The main control code for binman |
| 103 | |
| 104 | This assumes that help and test options have already been dealt with. It |
| 105 | deals with the core task of building images. |
| 106 | |
| 107 | Args: |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 108 | args: Command line arguments Namespace object |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 109 | """ |
| 110 | global images |
| 111 | |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 112 | if args.full_help: |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 113 | pager = os.getenv('PAGER') |
| 114 | if not pager: |
| 115 | pager = 'more' |
| 116 | fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), |
| 117 | 'README') |
| 118 | command.Run(pager, fname) |
| 119 | return 0 |
| 120 | |
Simon Glass | b2fd11d | 2019-07-08 14:25:48 -0600 | [diff] [blame] | 121 | if args.cmd == 'ls': |
| 122 | ListEntries(args.image, args.paths) |
| 123 | return 0 |
| 124 | |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 125 | # Try to figure out which device tree contains our image description |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 126 | if args.dt: |
| 127 | dtb_fname = args.dt |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 128 | else: |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 129 | board = args.board |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 130 | if not board: |
| 131 | raise ValueError('Must provide a board to process (use -b <board>)') |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 132 | board_pathname = os.path.join(args.build_dir, board) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 133 | dtb_fname = os.path.join(board_pathname, 'u-boot.dtb') |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 134 | if not args.indir: |
| 135 | args.indir = ['.'] |
| 136 | args.indir.append(board_pathname) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 137 | |
| 138 | try: |
Simon Glass | 63a336b | 2018-07-17 13:25:34 -0600 | [diff] [blame] | 139 | # Import these here in case libfdt.py is not available, in which case |
| 140 | # the above help option still works. |
| 141 | import fdt |
| 142 | import fdt_util |
| 143 | |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 144 | tout.Init(args.verbosity) |
| 145 | elf.debug = args.debug |
| 146 | cbfs_util.VERBOSE = args.verbosity > 2 |
| 147 | state.use_fake_dtb = args.fake_dtb |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 148 | try: |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 149 | tools.SetInputDirs(args.indir) |
| 150 | tools.PrepareOutputDir(args.outdir, args.preserve) |
| 151 | tools.SetToolPaths(args.toolpath) |
| 152 | state.SetEntryArgs(args.entry_arg) |
Simon Glass | 9230773 | 2018-07-06 10:27:40 -0600 | [diff] [blame] | 153 | |
| 154 | # Get the device tree ready by compiling it and copying the compiled |
| 155 | # output into a file in our output directly. Then scan it for use |
| 156 | # in binman. |
| 157 | dtb_fname = fdt_util.EnsureCompiled(dtb_fname) |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 158 | fname = tools.GetOutputFilename('u-boot.dtb.out') |
| 159 | tools.WriteFile(fname, tools.ReadFile(dtb_fname)) |
Simon Glass | 9230773 | 2018-07-06 10:27:40 -0600 | [diff] [blame] | 160 | dtb = fdt.FdtScan(fname) |
| 161 | |
Simon Glass | 22c92ca | 2017-05-27 07:38:29 -0600 | [diff] [blame] | 162 | node = _FindBinmanNode(dtb) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 163 | if not node: |
| 164 | raise ValueError("Device tree '%s' does not have a 'binman' " |
| 165 | "node" % dtb_fname) |
Simon Glass | 9230773 | 2018-07-06 10:27:40 -0600 | [diff] [blame] | 166 | |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 167 | images = _ReadImageDesc(node) |
Simon Glass | 9230773 | 2018-07-06 10:27:40 -0600 | [diff] [blame] | 168 | |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 169 | if args.image: |
Simon Glass | 3b376c3 | 2018-09-14 04:57:12 -0600 | [diff] [blame] | 170 | skip = [] |
Simon Glass | 4277543 | 2019-05-17 22:00:45 -0600 | [diff] [blame] | 171 | new_images = OrderedDict() |
Simon Glass | 5f3645b | 2019-05-14 15:53:41 -0600 | [diff] [blame] | 172 | for name, image in images.items(): |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 173 | if name in args.image: |
Simon Glass | 4277543 | 2019-05-17 22:00:45 -0600 | [diff] [blame] | 174 | new_images[name] = image |
| 175 | else: |
Simon Glass | 3b376c3 | 2018-09-14 04:57:12 -0600 | [diff] [blame] | 176 | skip.append(name) |
Simon Glass | 4277543 | 2019-05-17 22:00:45 -0600 | [diff] [blame] | 177 | images = new_images |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 178 | if skip and args.verbosity >= 2: |
Simon Glass | 7cca27d | 2019-05-14 15:53:37 -0600 | [diff] [blame] | 179 | print('Skipping images: %s' % ', '.join(skip)) |
Simon Glass | 3b376c3 | 2018-09-14 04:57:12 -0600 | [diff] [blame] | 180 | |
Simon Glass | 0c9d5b5 | 2018-09-14 04:57:22 -0600 | [diff] [blame] | 181 | state.Prepare(images, dtb) |
Simon Glass | bdb4031 | 2018-09-14 04:57:20 -0600 | [diff] [blame] | 182 | |
Simon Glass | 9230773 | 2018-07-06 10:27:40 -0600 | [diff] [blame] | 183 | # Prepare the device tree by making sure that any missing |
| 184 | # properties are added (e.g. 'pos' and 'size'). The values of these |
| 185 | # may not be correct yet, but we add placeholders so that the |
| 186 | # size of the device tree is correct. Later, in |
| 187 | # SetCalculatedProperties() we will insert the correct values |
| 188 | # without changing the device-tree size, thus ensuring that our |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 189 | # entry offsets remain the same. |
Simon Glass | 9230773 | 2018-07-06 10:27:40 -0600 | [diff] [blame] | 190 | for image in images.values(): |
Simon Glass | ac6328c | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 191 | image.ExpandEntries() |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 192 | if args.update_fdt: |
Simon Glass | e22f8fa | 2018-07-06 10:27:41 -0600 | [diff] [blame] | 193 | image.AddMissingProperties() |
Simon Glass | 9230773 | 2018-07-06 10:27:40 -0600 | [diff] [blame] | 194 | image.ProcessFdt(dtb) |
| 195 | |
Simon Glass | bdb4031 | 2018-09-14 04:57:20 -0600 | [diff] [blame] | 196 | for dtb_item in state.GetFdts(): |
| 197 | dtb_item.Sync(auto_resize=True) |
| 198 | dtb_item.Pack() |
| 199 | dtb_item.Flush() |
Simon Glass | 9230773 | 2018-07-06 10:27:40 -0600 | [diff] [blame] | 200 | |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 201 | for image in images.values(): |
| 202 | # Perform all steps for this image, including checking and |
| 203 | # writing it. This means that errors found with a later |
| 204 | # image will be reported after earlier images are already |
| 205 | # completed and written, but that does not seem important. |
| 206 | image.GetEntryContents() |
Simon Glass | e8561af | 2018-08-01 15:22:37 -0600 | [diff] [blame] | 207 | image.GetEntryOffsets() |
Simon Glass | e61b6f6 | 2019-07-08 14:25:37 -0600 | [diff] [blame] | 208 | |
| 209 | # We need to pack the entries to figure out where everything |
| 210 | # should be placed. This sets the offset/size of each entry. |
| 211 | # However, after packing we call ProcessEntryContents() which |
| 212 | # may result in an entry changing size. In that case we need to |
| 213 | # do another pass. Since the device tree often contains the |
| 214 | # final offset/size information we try to make space for this in |
| 215 | # AddMissingProperties() above. However, if the device is |
| 216 | # compressed we cannot know this compressed size in advance, |
| 217 | # since changing an offset from 0x100 to 0x104 (for example) can |
| 218 | # alter the compressed size of the device tree. So we need a |
| 219 | # third pass for this. |
| 220 | passes = 3 |
| 221 | for pack_pass in range(passes): |
| 222 | try: |
| 223 | image.PackEntries() |
| 224 | image.CheckSize() |
| 225 | image.CheckEntries() |
| 226 | except Exception as e: |
| 227 | if args.map: |
| 228 | fname = image.WriteMap() |
| 229 | print("Wrote map file '%s' to show errors" % fname) |
| 230 | raise |
| 231 | image.SetImagePos() |
| 232 | if args.update_fdt: |
| 233 | image.SetCalculatedProperties() |
| 234 | for dtb_item in state.GetFdts(): |
| 235 | dtb_item.Sync() |
| 236 | sizes_ok = image.ProcessEntryContents() |
| 237 | if sizes_ok: |
| 238 | break |
| 239 | image.ResetForPack() |
| 240 | if not sizes_ok: |
| 241 | image.Raise('Entries expanded after packing (tried %s passes)' % |
| 242 | passes) |
| 243 | |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 244 | image.WriteSymbols() |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 245 | image.BuildImage() |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 246 | if args.map: |
Simon Glass | 3073266 | 2018-06-01 09:38:20 -0600 | [diff] [blame] | 247 | image.WriteMap() |
Simon Glass | bdb4031 | 2018-09-14 04:57:20 -0600 | [diff] [blame] | 248 | |
| 249 | # Write the updated FDTs to our output files |
| 250 | for dtb_item in state.GetFdts(): |
| 251 | tools.WriteFile(dtb_item._fname, dtb_item.GetContents()) |
| 252 | |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 253 | finally: |
| 254 | tools.FinaliseOutputDir() |
| 255 | finally: |
| 256 | tout.Uninit() |
| 257 | |
| 258 | return 0 |