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