blob: b244e7a0cd08d0241b747be3d808df4afa2c9555 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0+
Simon Glass2574ef62016-11-25 20:15:51 -07002# Copyright (c) 2016 Google, Inc
3# Written by Simon Glass <sjg@chromium.org>
4#
Simon Glass2574ef62016-11-25 20:15:51 -07005# Creates binary images from input files controlled by a description
6#
7
Simon Glass7cca27d2019-05-14 15:53:37 -06008from __future__ import print_function
9
Simon Glass2574ef62016-11-25 20:15:51 -070010from collections import OrderedDict
11import os
12import sys
13import tools
14
Simon Glass1de34482019-07-08 13:18:53 -060015import cbfs_util
Simon Glass2574ef62016-11-25 20:15:51 -070016import command
Simon Glass075a45c2017-11-13 18:55:00 -070017import elf
Simon Glass2574ef62016-11-25 20:15:51 -070018from image import Image
Simon Glass29aa7362018-09-14 04:57:19 -060019import state
Simon Glass2574ef62016-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 Glass22c92ca2017-05-27 07:38:29 -060045def _FindBinmanNode(dtb):
Simon Glass2574ef62016-11-25 20:15:51 -070046 """Find the 'binman' node in the device tree
47
48 Args:
Simon Glass22c92ca2017-05-27 07:38:29 -060049 dtb: Fdt object to scan
Simon Glass2574ef62016-11-25 20:15:51 -070050 Returns:
51 Node object of /binman node, or None if not found
52 """
Simon Glass22c92ca2017-05-27 07:38:29 -060053 for node in dtb.GetRoot().subnodes:
Simon Glass2574ef62016-11-25 20:15:51 -070054 if node.name == 'binman':
55 return node
56 return None
57
Simon Glass29aa7362018-09-14 04:57:19 -060058def WriteEntryDocs(modules, test_missing=None):
59 """Write out documentation for all entries
Simon Glass92307732018-07-06 10:27:40 -060060
61 Args:
Simon Glass29aa7362018-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 Glass92307732018-07-06 10:27:40 -060066 """
Simon Glass969616c2018-07-17 13:25:36 -060067 from entry import Entry
68 Entry.WriteDocs(modules, test_missing)
69
Simon Glassb2fd11d2019-07-08 14:25:48 -060070
71def 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 Glass4c613bf2019-07-08 14:25:50 -0600101
102def ReadEntry(image_fname, entry_path, decomp=True):
103 """Extract an entry from an image
104
105 This extracts the data from a particular entry in an image
106
107 Args:
108 image_fname: Image filename to process
109 entry_path: Path to entry to extract
110 decomp: True to return uncompressed data, if the data is compress
111 False to return the raw data
112
113 Returns:
114 data extracted from the entry
115 """
116 image = Image.FromFile(image_fname)
117 entry = image.FindEntryPath(entry_path)
118 return entry.ReadData(decomp)
119
120
Simon Glassf46732a2019-07-08 14:25:29 -0600121def Binman(args):
Simon Glass2574ef62016-11-25 20:15:51 -0700122 """The main control code for binman
123
124 This assumes that help and test options have already been dealt with. It
125 deals with the core task of building images.
126
127 Args:
Simon Glassf46732a2019-07-08 14:25:29 -0600128 args: Command line arguments Namespace object
Simon Glass2574ef62016-11-25 20:15:51 -0700129 """
130 global images
131
Simon Glassf46732a2019-07-08 14:25:29 -0600132 if args.full_help:
Simon Glass2574ef62016-11-25 20:15:51 -0700133 pager = os.getenv('PAGER')
134 if not pager:
135 pager = 'more'
136 fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),
137 'README')
138 command.Run(pager, fname)
139 return 0
140
Simon Glassb2fd11d2019-07-08 14:25:48 -0600141 if args.cmd == 'ls':
142 ListEntries(args.image, args.paths)
143 return 0
144
Simon Glass2574ef62016-11-25 20:15:51 -0700145 # Try to figure out which device tree contains our image description
Simon Glassf46732a2019-07-08 14:25:29 -0600146 if args.dt:
147 dtb_fname = args.dt
Simon Glass2574ef62016-11-25 20:15:51 -0700148 else:
Simon Glassf46732a2019-07-08 14:25:29 -0600149 board = args.board
Simon Glass2574ef62016-11-25 20:15:51 -0700150 if not board:
151 raise ValueError('Must provide a board to process (use -b <board>)')
Simon Glassf46732a2019-07-08 14:25:29 -0600152 board_pathname = os.path.join(args.build_dir, board)
Simon Glass2574ef62016-11-25 20:15:51 -0700153 dtb_fname = os.path.join(board_pathname, 'u-boot.dtb')
Simon Glassf46732a2019-07-08 14:25:29 -0600154 if not args.indir:
155 args.indir = ['.']
156 args.indir.append(board_pathname)
Simon Glass2574ef62016-11-25 20:15:51 -0700157
158 try:
Simon Glass63a336b2018-07-17 13:25:34 -0600159 # Import these here in case libfdt.py is not available, in which case
160 # the above help option still works.
161 import fdt
162 import fdt_util
163
Simon Glassf46732a2019-07-08 14:25:29 -0600164 tout.Init(args.verbosity)
165 elf.debug = args.debug
166 cbfs_util.VERBOSE = args.verbosity > 2
167 state.use_fake_dtb = args.fake_dtb
Simon Glass2574ef62016-11-25 20:15:51 -0700168 try:
Simon Glassf46732a2019-07-08 14:25:29 -0600169 tools.SetInputDirs(args.indir)
170 tools.PrepareOutputDir(args.outdir, args.preserve)
171 tools.SetToolPaths(args.toolpath)
172 state.SetEntryArgs(args.entry_arg)
Simon Glass92307732018-07-06 10:27:40 -0600173
174 # Get the device tree ready by compiling it and copying the compiled
175 # output into a file in our output directly. Then scan it for use
176 # in binman.
177 dtb_fname = fdt_util.EnsureCompiled(dtb_fname)
Simon Glasse219aa42018-09-14 04:57:24 -0600178 fname = tools.GetOutputFilename('u-boot.dtb.out')
179 tools.WriteFile(fname, tools.ReadFile(dtb_fname))
Simon Glass92307732018-07-06 10:27:40 -0600180 dtb = fdt.FdtScan(fname)
181
Simon Glass22c92ca2017-05-27 07:38:29 -0600182 node = _FindBinmanNode(dtb)
Simon Glass2574ef62016-11-25 20:15:51 -0700183 if not node:
184 raise ValueError("Device tree '%s' does not have a 'binman' "
185 "node" % dtb_fname)
Simon Glass92307732018-07-06 10:27:40 -0600186
Simon Glass2574ef62016-11-25 20:15:51 -0700187 images = _ReadImageDesc(node)
Simon Glass92307732018-07-06 10:27:40 -0600188
Simon Glassf46732a2019-07-08 14:25:29 -0600189 if args.image:
Simon Glass3b376c32018-09-14 04:57:12 -0600190 skip = []
Simon Glass42775432019-05-17 22:00:45 -0600191 new_images = OrderedDict()
Simon Glass5f3645b2019-05-14 15:53:41 -0600192 for name, image in images.items():
Simon Glassf46732a2019-07-08 14:25:29 -0600193 if name in args.image:
Simon Glass42775432019-05-17 22:00:45 -0600194 new_images[name] = image
195 else:
Simon Glass3b376c32018-09-14 04:57:12 -0600196 skip.append(name)
Simon Glass42775432019-05-17 22:00:45 -0600197 images = new_images
Simon Glassf46732a2019-07-08 14:25:29 -0600198 if skip and args.verbosity >= 2:
Simon Glass7cca27d2019-05-14 15:53:37 -0600199 print('Skipping images: %s' % ', '.join(skip))
Simon Glass3b376c32018-09-14 04:57:12 -0600200
Simon Glass0c9d5b52018-09-14 04:57:22 -0600201 state.Prepare(images, dtb)
Simon Glassbdb40312018-09-14 04:57:20 -0600202
Simon Glass92307732018-07-06 10:27:40 -0600203 # Prepare the device tree by making sure that any missing
204 # properties are added (e.g. 'pos' and 'size'). The values of these
205 # may not be correct yet, but we add placeholders so that the
206 # size of the device tree is correct. Later, in
207 # SetCalculatedProperties() we will insert the correct values
208 # without changing the device-tree size, thus ensuring that our
Simon Glasse8561af2018-08-01 15:22:37 -0600209 # entry offsets remain the same.
Simon Glass92307732018-07-06 10:27:40 -0600210 for image in images.values():
Simon Glassac6328c2018-09-14 04:57:28 -0600211 image.ExpandEntries()
Simon Glassf46732a2019-07-08 14:25:29 -0600212 if args.update_fdt:
Simon Glasse22f8fa2018-07-06 10:27:41 -0600213 image.AddMissingProperties()
Simon Glass92307732018-07-06 10:27:40 -0600214 image.ProcessFdt(dtb)
215
Simon Glassbdb40312018-09-14 04:57:20 -0600216 for dtb_item in state.GetFdts():
217 dtb_item.Sync(auto_resize=True)
218 dtb_item.Pack()
219 dtb_item.Flush()
Simon Glass92307732018-07-06 10:27:40 -0600220
Simon Glass2574ef62016-11-25 20:15:51 -0700221 for image in images.values():
222 # Perform all steps for this image, including checking and
223 # writing it. This means that errors found with a later
224 # image will be reported after earlier images are already
225 # completed and written, but that does not seem important.
226 image.GetEntryContents()
Simon Glasse8561af2018-08-01 15:22:37 -0600227 image.GetEntryOffsets()
Simon Glasse61b6f62019-07-08 14:25:37 -0600228
229 # We need to pack the entries to figure out where everything
230 # should be placed. This sets the offset/size of each entry.
231 # However, after packing we call ProcessEntryContents() which
232 # may result in an entry changing size. In that case we need to
233 # do another pass. Since the device tree often contains the
234 # final offset/size information we try to make space for this in
235 # AddMissingProperties() above. However, if the device is
236 # compressed we cannot know this compressed size in advance,
237 # since changing an offset from 0x100 to 0x104 (for example) can
238 # alter the compressed size of the device tree. So we need a
239 # third pass for this.
240 passes = 3
241 for pack_pass in range(passes):
242 try:
243 image.PackEntries()
244 image.CheckSize()
245 image.CheckEntries()
246 except Exception as e:
247 if args.map:
248 fname = image.WriteMap()
249 print("Wrote map file '%s' to show errors" % fname)
250 raise
251 image.SetImagePos()
252 if args.update_fdt:
253 image.SetCalculatedProperties()
254 for dtb_item in state.GetFdts():
255 dtb_item.Sync()
256 sizes_ok = image.ProcessEntryContents()
257 if sizes_ok:
258 break
259 image.ResetForPack()
260 if not sizes_ok:
261 image.Raise('Entries expanded after packing (tried %s passes)' %
262 passes)
263
Simon Glass4ca8e042017-11-13 18:55:01 -0700264 image.WriteSymbols()
Simon Glass2574ef62016-11-25 20:15:51 -0700265 image.BuildImage()
Simon Glassf46732a2019-07-08 14:25:29 -0600266 if args.map:
Simon Glass30732662018-06-01 09:38:20 -0600267 image.WriteMap()
Simon Glassbdb40312018-09-14 04:57:20 -0600268
269 # Write the updated FDTs to our output files
270 for dtb_item in state.GetFdts():
271 tools.WriteFile(dtb_item._fname, dtb_item.GetContents())
272
Simon Glass2574ef62016-11-25 20:15:51 -0700273 finally:
274 tools.FinaliseOutputDir()
275 finally:
276 tout.Uninit()
277
278 return 0