blob: ce25eb5485860614942dfb822a2aa1f12eef1860 [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
15import command
Simon Glass7fe91732017-11-13 18:55:00 -070016import elf
Simon Glassbf7fd502016-11-25 20:15:51 -070017from image import Image
Simon Glassc55a50f2018-09-14 04:57:19 -060018import state
Simon Glassbf7fd502016-11-25 20:15:51 -070019import tout
20
21# List of images we plan to create
22# Make this global so that it can be referenced from tests
23images = OrderedDict()
24
25def _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 Glassec3f3782017-05-27 07:38:29 -060044def _FindBinmanNode(dtb):
Simon Glassbf7fd502016-11-25 20:15:51 -070045 """Find the 'binman' node in the device tree
46
47 Args:
Simon Glassec3f3782017-05-27 07:38:29 -060048 dtb: Fdt object to scan
Simon Glassbf7fd502016-11-25 20:15:51 -070049 Returns:
50 Node object of /binman node, or None if not found
51 """
Simon Glassec3f3782017-05-27 07:38:29 -060052 for node in dtb.GetRoot().subnodes:
Simon Glassbf7fd502016-11-25 20:15:51 -070053 if node.name == 'binman':
54 return node
55 return None
56
Simon Glassc55a50f2018-09-14 04:57:19 -060057def WriteEntryDocs(modules, test_missing=None):
58 """Write out documentation for all entries
Simon Glassecab8972018-07-06 10:27:40 -060059
60 Args:
Simon Glassc55a50f2018-09-14 04:57:19 -060061 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 Glassecab8972018-07-06 10:27:40 -060065 """
Simon Glassfd8d1f72018-07-17 13:25:36 -060066 from entry import Entry
67 Entry.WriteDocs(modules, test_missing)
68
Simon Glassbf7fd502016-11-25 20:15:51 -070069def 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 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 Glassbf7fd502016-11-25 20:15:51 -0700109 tout.Init(options.verbosity)
Simon Glass19790632017-11-13 18:55:01 -0700110 elf.debug = options.debug
Simon Glass93d17412018-09-14 04:57:23 -0600111 state.use_fake_dtb = options.fake_dtb
Simon Glassbf7fd502016-11-25 20:15:51 -0700112 try:
113 tools.SetInputDirs(options.indir)
114 tools.PrepareOutputDir(options.outdir, options.preserve)
Simon Glassc55a50f2018-09-14 04:57:19 -0600115 state.SetEntryArgs(options.entry_arg)
Simon Glassecab8972018-07-06 10:27:40 -0600116
117 # Get the device tree ready by compiling it and copying the compiled
118 # output into a file in our output directly. Then scan it for use
119 # in binman.
120 dtb_fname = fdt_util.EnsureCompiled(dtb_fname)
Simon Glass6ed45ba2018-09-14 04:57:24 -0600121 fname = tools.GetOutputFilename('u-boot.dtb.out')
122 tools.WriteFile(fname, tools.ReadFile(dtb_fname))
Simon Glassecab8972018-07-06 10:27:40 -0600123 dtb = fdt.FdtScan(fname)
124
Simon Glassec3f3782017-05-27 07:38:29 -0600125 node = _FindBinmanNode(dtb)
Simon Glassbf7fd502016-11-25 20:15:51 -0700126 if not node:
127 raise ValueError("Device tree '%s' does not have a 'binman' "
128 "node" % dtb_fname)
Simon Glassecab8972018-07-06 10:27:40 -0600129
Simon Glassbf7fd502016-11-25 20:15:51 -0700130 images = _ReadImageDesc(node)
Simon Glassecab8972018-07-06 10:27:40 -0600131
Simon Glass0bfa7b02018-09-14 04:57:12 -0600132 if options.image:
133 skip = []
Simon Glass50979152019-05-14 15:53:41 -0600134 for name, image in images.items():
Simon Glass0bfa7b02018-09-14 04:57:12 -0600135 if name not in options.image:
136 del images[name]
137 skip.append(name)
Simon Glasseb833d82019-04-25 21:58:34 -0600138 if skip and options.verbosity >= 2:
Simon Glass2ca84682019-05-14 15:53:37 -0600139 print('Skipping images: %s' % ', '.join(skip))
Simon Glass0bfa7b02018-09-14 04:57:12 -0600140
Simon Glass539aece2018-09-14 04:57:22 -0600141 state.Prepare(images, dtb)
Simon Glass2a72cc72018-09-14 04:57:20 -0600142
Simon Glassecab8972018-07-06 10:27:40 -0600143 # Prepare the device tree by making sure that any missing
144 # properties are added (e.g. 'pos' and 'size'). The values of these
145 # may not be correct yet, but we add placeholders so that the
146 # size of the device tree is correct. Later, in
147 # SetCalculatedProperties() we will insert the correct values
148 # without changing the device-tree size, thus ensuring that our
Simon Glass3ab95982018-08-01 15:22:37 -0600149 # entry offsets remain the same.
Simon Glassecab8972018-07-06 10:27:40 -0600150 for image in images.values():
Simon Glass0a98b282018-09-14 04:57:28 -0600151 image.ExpandEntries()
Simon Glass078ab1a2018-07-06 10:27:41 -0600152 if options.update_fdt:
153 image.AddMissingProperties()
Simon Glassecab8972018-07-06 10:27:40 -0600154 image.ProcessFdt(dtb)
155
Simon Glass2a72cc72018-09-14 04:57:20 -0600156 for dtb_item in state.GetFdts():
157 dtb_item.Sync(auto_resize=True)
158 dtb_item.Pack()
159 dtb_item.Flush()
Simon Glassecab8972018-07-06 10:27:40 -0600160
Simon Glassbf7fd502016-11-25 20:15:51 -0700161 for image in images.values():
162 # Perform all steps for this image, including checking and
163 # writing it. This means that errors found with a later
164 # image will be reported after earlier images are already
165 # completed and written, but that does not seem important.
166 image.GetEntryContents()
Simon Glass3ab95982018-08-01 15:22:37 -0600167 image.GetEntryOffsets()
Simon Glass163ed6c2018-09-14 04:57:36 -0600168 try:
169 image.PackEntries()
170 image.CheckSize()
171 image.CheckEntries()
172 except Exception as e:
173 if options.map:
174 fname = image.WriteMap()
Simon Glass2ca84682019-05-14 15:53:37 -0600175 print("Wrote map file '%s' to show errors" % fname)
Simon Glass163ed6c2018-09-14 04:57:36 -0600176 raise
Simon Glassdbf6be92018-08-01 15:22:42 -0600177 image.SetImagePos()
Simon Glass078ab1a2018-07-06 10:27:41 -0600178 if options.update_fdt:
179 image.SetCalculatedProperties()
Simon Glass2a72cc72018-09-14 04:57:20 -0600180 for dtb_item in state.GetFdts():
181 dtb_item.Sync()
Simon Glassbf7fd502016-11-25 20:15:51 -0700182 image.ProcessEntryContents()
Simon Glass19790632017-11-13 18:55:01 -0700183 image.WriteSymbols()
Simon Glassbf7fd502016-11-25 20:15:51 -0700184 image.BuildImage()
Simon Glass3b0c3822018-06-01 09:38:20 -0600185 if options.map:
186 image.WriteMap()
Simon Glass2a72cc72018-09-14 04:57:20 -0600187
188 # Write the updated FDTs to our output files
189 for dtb_item in state.GetFdts():
190 tools.WriteFile(dtb_item._fname, dtb_item.GetContents())
191
Simon Glassbf7fd502016-11-25 20:15:51 -0700192 finally:
193 tools.FinaliseOutputDir()
194 finally:
195 tout.Uninit()
196
197 return 0