blob: 35faf1150629e3c0bfb87969b60702937181990f [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0+
Simon Glassbf7fd502016-11-25 20:15:51 -07002# Copyright (c) 2016 Google, Inc
3# Written by Simon Glass <sjg@chromium.org>
4#
Simon Glassbf7fd502016-11-25 20:15:51 -07005# Creates binary images from input files controlled by a description
6#
7
Simon Glass2ca84682019-05-14 15:53:37 -06008from __future__ import print_function
9
Simon Glassbf7fd502016-11-25 20:15:51 -070010from collections import OrderedDict
11import os
12import sys
13import tools
14
Simon Glassac62fba2019-07-08 13:18:53 -060015import cbfs_util
Simon Glassbf7fd502016-11-25 20:15:51 -070016import command
Simon Glass7fe91732017-11-13 18:55:00 -070017import elf
Simon Glassbf7fd502016-11-25 20:15:51 -070018from image import Image
Simon Glassc55a50f2018-09-14 04:57:19 -060019import state
Simon Glassbf7fd502016-11-25 20:15:51 -070020import tout
21
22# List of images we plan to create
23# Make this global so that it can be referenced from tests
24images = OrderedDict()
25
26def _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 Glassec3f3782017-05-27 07:38:29 -060045def _FindBinmanNode(dtb):
Simon Glassbf7fd502016-11-25 20:15:51 -070046 """Find the 'binman' node in the device tree
47
48 Args:
Simon Glassec3f3782017-05-27 07:38:29 -060049 dtb: Fdt object to scan
Simon Glassbf7fd502016-11-25 20:15:51 -070050 Returns:
51 Node object of /binman node, or None if not found
52 """
Simon Glassec3f3782017-05-27 07:38:29 -060053 for node in dtb.GetRoot().subnodes:
Simon Glassbf7fd502016-11-25 20:15:51 -070054 if node.name == 'binman':
55 return node
56 return None
57
Simon Glassc55a50f2018-09-14 04:57:19 -060058def WriteEntryDocs(modules, test_missing=None):
59 """Write out documentation for all entries
Simon Glassecab8972018-07-06 10:27:40 -060060
61 Args:
Simon Glassc55a50f2018-09-14 04:57:19 -060062 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 Glassecab8972018-07-06 10:27:40 -060066 """
Simon Glassfd8d1f72018-07-17 13:25:36 -060067 from entry import Entry
68 Entry.WriteDocs(modules, test_missing)
69
Simon Glass53cd5d92019-07-08 14:25:29 -060070def Binman(args):
Simon Glassbf7fd502016-11-25 20:15:51 -070071 """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:
Simon Glass53cd5d92019-07-08 14:25:29 -060077 args: Command line arguments Namespace object
Simon Glassbf7fd502016-11-25 20:15:51 -070078 """
79 global images
80
Simon Glass53cd5d92019-07-08 14:25:29 -060081 if args.full_help:
Simon Glassbf7fd502016-11-25 20:15:51 -070082 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
Simon Glass53cd5d92019-07-08 14:25:29 -060091 if args.dt:
92 dtb_fname = args.dt
Simon Glassbf7fd502016-11-25 20:15:51 -070093 else:
Simon Glass53cd5d92019-07-08 14:25:29 -060094 board = args.board
Simon Glassbf7fd502016-11-25 20:15:51 -070095 if not board:
96 raise ValueError('Must provide a board to process (use -b <board>)')
Simon Glass53cd5d92019-07-08 14:25:29 -060097 board_pathname = os.path.join(args.build_dir, board)
Simon Glassbf7fd502016-11-25 20:15:51 -070098 dtb_fname = os.path.join(board_pathname, 'u-boot.dtb')
Simon Glass53cd5d92019-07-08 14:25:29 -060099 if not args.indir:
100 args.indir = ['.']
101 args.indir.append(board_pathname)
Simon Glassbf7fd502016-11-25 20:15:51 -0700102
103 try:
Simon Glass9b1a8042018-07-17 13:25:34 -0600104 # 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 Glass53cd5d92019-07-08 14:25:29 -0600109 tout.Init(args.verbosity)
110 elf.debug = args.debug
111 cbfs_util.VERBOSE = args.verbosity > 2
112 state.use_fake_dtb = args.fake_dtb
Simon Glassbf7fd502016-11-25 20:15:51 -0700113 try:
Simon Glass53cd5d92019-07-08 14:25:29 -0600114 tools.SetInputDirs(args.indir)
115 tools.PrepareOutputDir(args.outdir, args.preserve)
116 tools.SetToolPaths(args.toolpath)
117 state.SetEntryArgs(args.entry_arg)
Simon Glassecab8972018-07-06 10:27:40 -0600118
119 # Get the device tree ready by compiling it and copying the compiled
120 # output into a file in our output directly. Then scan it for use
121 # in binman.
122 dtb_fname = fdt_util.EnsureCompiled(dtb_fname)
Simon Glass6ed45ba2018-09-14 04:57:24 -0600123 fname = tools.GetOutputFilename('u-boot.dtb.out')
124 tools.WriteFile(fname, tools.ReadFile(dtb_fname))
Simon Glassecab8972018-07-06 10:27:40 -0600125 dtb = fdt.FdtScan(fname)
126
Simon Glassec3f3782017-05-27 07:38:29 -0600127 node = _FindBinmanNode(dtb)
Simon Glassbf7fd502016-11-25 20:15:51 -0700128 if not node:
129 raise ValueError("Device tree '%s' does not have a 'binman' "
130 "node" % dtb_fname)
Simon Glassecab8972018-07-06 10:27:40 -0600131
Simon Glassbf7fd502016-11-25 20:15:51 -0700132 images = _ReadImageDesc(node)
Simon Glassecab8972018-07-06 10:27:40 -0600133
Simon Glass53cd5d92019-07-08 14:25:29 -0600134 if args.image:
Simon Glass0bfa7b02018-09-14 04:57:12 -0600135 skip = []
Simon Glass58632a72019-05-17 22:00:45 -0600136 new_images = OrderedDict()
Simon Glass50979152019-05-14 15:53:41 -0600137 for name, image in images.items():
Simon Glass53cd5d92019-07-08 14:25:29 -0600138 if name in args.image:
Simon Glass58632a72019-05-17 22:00:45 -0600139 new_images[name] = image
140 else:
Simon Glass0bfa7b02018-09-14 04:57:12 -0600141 skip.append(name)
Simon Glass58632a72019-05-17 22:00:45 -0600142 images = new_images
Simon Glass53cd5d92019-07-08 14:25:29 -0600143 if skip and args.verbosity >= 2:
Simon Glass2ca84682019-05-14 15:53:37 -0600144 print('Skipping images: %s' % ', '.join(skip))
Simon Glass0bfa7b02018-09-14 04:57:12 -0600145
Simon Glass539aece2018-09-14 04:57:22 -0600146 state.Prepare(images, dtb)
Simon Glass2a72cc72018-09-14 04:57:20 -0600147
Simon Glassecab8972018-07-06 10:27:40 -0600148 # Prepare the device tree by making sure that any missing
149 # properties are added (e.g. 'pos' and 'size'). The values of these
150 # may not be correct yet, but we add placeholders so that the
151 # size of the device tree is correct. Later, in
152 # SetCalculatedProperties() we will insert the correct values
153 # without changing the device-tree size, thus ensuring that our
Simon Glass3ab95982018-08-01 15:22:37 -0600154 # entry offsets remain the same.
Simon Glassecab8972018-07-06 10:27:40 -0600155 for image in images.values():
Simon Glass0a98b282018-09-14 04:57:28 -0600156 image.ExpandEntries()
Simon Glass53cd5d92019-07-08 14:25:29 -0600157 if args.update_fdt:
Simon Glass078ab1a2018-07-06 10:27:41 -0600158 image.AddMissingProperties()
Simon Glassecab8972018-07-06 10:27:40 -0600159 image.ProcessFdt(dtb)
160
Simon Glass2a72cc72018-09-14 04:57:20 -0600161 for dtb_item in state.GetFdts():
162 dtb_item.Sync(auto_resize=True)
163 dtb_item.Pack()
164 dtb_item.Flush()
Simon Glassecab8972018-07-06 10:27:40 -0600165
Simon Glassbf7fd502016-11-25 20:15:51 -0700166 for image in images.values():
167 # Perform all steps for this image, including checking and
168 # writing it. This means that errors found with a later
169 # image will be reported after earlier images are already
170 # completed and written, but that does not seem important.
171 image.GetEntryContents()
Simon Glass3ab95982018-08-01 15:22:37 -0600172 image.GetEntryOffsets()
Simon Glassc52c9e72019-07-08 14:25:37 -0600173
174 # We need to pack the entries to figure out where everything
175 # should be placed. This sets the offset/size of each entry.
176 # However, after packing we call ProcessEntryContents() which
177 # may result in an entry changing size. In that case we need to
178 # do another pass. Since the device tree often contains the
179 # final offset/size information we try to make space for this in
180 # AddMissingProperties() above. However, if the device is
181 # compressed we cannot know this compressed size in advance,
182 # since changing an offset from 0x100 to 0x104 (for example) can
183 # alter the compressed size of the device tree. So we need a
184 # third pass for this.
185 passes = 3
186 for pack_pass in range(passes):
187 try:
188 image.PackEntries()
189 image.CheckSize()
190 image.CheckEntries()
191 except Exception as e:
192 if args.map:
193 fname = image.WriteMap()
194 print("Wrote map file '%s' to show errors" % fname)
195 raise
196 image.SetImagePos()
197 if args.update_fdt:
198 image.SetCalculatedProperties()
199 for dtb_item in state.GetFdts():
200 dtb_item.Sync()
201 sizes_ok = image.ProcessEntryContents()
202 if sizes_ok:
203 break
204 image.ResetForPack()
205 if not sizes_ok:
206 image.Raise('Entries expanded after packing (tried %s passes)' %
207 passes)
208
Simon Glass19790632017-11-13 18:55:01 -0700209 image.WriteSymbols()
Simon Glassbf7fd502016-11-25 20:15:51 -0700210 image.BuildImage()
Simon Glass53cd5d92019-07-08 14:25:29 -0600211 if args.map:
Simon Glass3b0c3822018-06-01 09:38:20 -0600212 image.WriteMap()
Simon Glass2a72cc72018-09-14 04:57:20 -0600213
214 # Write the updated FDTs to our output files
215 for dtb_item in state.GetFdts():
216 tools.WriteFile(dtb_item._fname, dtb_item.GetContents())
217
Simon Glassbf7fd502016-11-25 20:15:51 -0700218 finally:
219 tools.FinaliseOutputDir()
220 finally:
221 tout.Uninit()
222
223 return 0