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 |
Simon Glass | a997ea5 | 2020-04-17 18:09:04 -0600 | [diff] [blame] | 11 | from patman import tools |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 12 | |
Simon Glass | c585dd4 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 13 | from binman import cbfs_util |
| 14 | from binman import elf |
Simon Glass | a997ea5 | 2020-04-17 18:09:04 -0600 | [diff] [blame] | 15 | from patman import command |
| 16 | from patman import tout |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 17 | |
| 18 | # List of images we plan to create |
| 19 | # Make this global so that it can be referenced from tests |
| 20 | images = OrderedDict() |
| 21 | |
| 22 | def _ReadImageDesc(binman_node): |
| 23 | """Read the image descriptions from the /binman node |
| 24 | |
| 25 | This normally produces a single Image object called 'image'. But if |
| 26 | multiple images are present, they will all be returned. |
| 27 | |
| 28 | Args: |
| 29 | binman_node: Node object of the /binman node |
| 30 | Returns: |
| 31 | OrderedDict of Image objects, each of which describes an image |
| 32 | """ |
| 33 | images = OrderedDict() |
| 34 | if 'multiple-images' in binman_node.props: |
| 35 | for node in binman_node.subnodes: |
| 36 | images[node.name] = Image(node.name, node) |
| 37 | else: |
| 38 | images['image'] = Image('image', binman_node) |
| 39 | return images |
| 40 | |
Simon Glass | 22c92ca | 2017-05-27 07:38:29 -0600 | [diff] [blame] | 41 | def _FindBinmanNode(dtb): |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 42 | """Find the 'binman' node in the device tree |
| 43 | |
| 44 | Args: |
Simon Glass | 22c92ca | 2017-05-27 07:38:29 -0600 | [diff] [blame] | 45 | dtb: Fdt object to scan |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 46 | Returns: |
| 47 | Node object of /binman node, or None if not found |
| 48 | """ |
Simon Glass | 22c92ca | 2017-05-27 07:38:29 -0600 | [diff] [blame] | 49 | for node in dtb.GetRoot().subnodes: |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 50 | if node.name == 'binman': |
| 51 | return node |
| 52 | return None |
| 53 | |
Simon Glass | 29aa736 | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 54 | def WriteEntryDocs(modules, test_missing=None): |
| 55 | """Write out documentation for all entries |
Simon Glass | 9230773 | 2018-07-06 10:27:40 -0600 | [diff] [blame] | 56 | |
| 57 | Args: |
Simon Glass | 29aa736 | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 58 | modules: List of Module objects to get docs for |
| 59 | test_missing: Used for testing only, to force an entry's documeentation |
| 60 | to show as missing even if it is present. Should be set to None in |
| 61 | normal use. |
Simon Glass | 9230773 | 2018-07-06 10:27:40 -0600 | [diff] [blame] | 62 | """ |
Simon Glass | c585dd4 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 63 | from binman.entry import Entry |
Simon Glass | 969616c | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 64 | Entry.WriteDocs(modules, test_missing) |
| 65 | |
Simon Glass | b2fd11d | 2019-07-08 14:25:48 -0600 | [diff] [blame] | 66 | |
| 67 | def ListEntries(image_fname, entry_paths): |
| 68 | """List the entries in an image |
| 69 | |
| 70 | This decodes the supplied image and displays a table of entries from that |
| 71 | image, preceded by a header. |
| 72 | |
| 73 | Args: |
| 74 | image_fname: Image filename to process |
| 75 | entry_paths: List of wildcarded paths (e.g. ['*dtb*', 'u-boot*', |
| 76 | 'section/u-boot']) |
| 77 | """ |
| 78 | image = Image.FromFile(image_fname) |
| 79 | |
| 80 | entries, lines, widths = image.GetListEntries(entry_paths) |
| 81 | |
| 82 | num_columns = len(widths) |
| 83 | for linenum, line in enumerate(lines): |
| 84 | if linenum == 1: |
| 85 | # Print header line |
| 86 | print('-' * (sum(widths) + num_columns * 2)) |
| 87 | out = '' |
| 88 | for i, item in enumerate(line): |
| 89 | width = -widths[i] |
| 90 | if item.startswith('>'): |
| 91 | width = -width |
| 92 | item = item[1:] |
| 93 | txt = '%*s ' % (width, item) |
| 94 | out += txt |
| 95 | print(out.rstrip()) |
| 96 | |
Simon Glass | 4c613bf | 2019-07-08 14:25:50 -0600 | [diff] [blame] | 97 | |
| 98 | def ReadEntry(image_fname, entry_path, decomp=True): |
| 99 | """Extract an entry from an image |
| 100 | |
| 101 | This extracts the data from a particular entry in an image |
| 102 | |
| 103 | Args: |
| 104 | image_fname: Image filename to process |
| 105 | entry_path: Path to entry to extract |
| 106 | decomp: True to return uncompressed data, if the data is compress |
| 107 | False to return the raw data |
| 108 | |
| 109 | Returns: |
| 110 | data extracted from the entry |
| 111 | """ |
Simon Glass | b9ba4e0 | 2019-08-24 07:22:44 -0600 | [diff] [blame] | 112 | global Image |
| 113 | from image import Image |
| 114 | |
Simon Glass | 4c613bf | 2019-07-08 14:25:50 -0600 | [diff] [blame] | 115 | image = Image.FromFile(image_fname) |
| 116 | entry = image.FindEntryPath(entry_path) |
| 117 | return entry.ReadData(decomp) |
| 118 | |
| 119 | |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 120 | def ExtractEntries(image_fname, output_fname, outdir, entry_paths, |
| 121 | decomp=True): |
| 122 | """Extract the data from one or more entries and write it to files |
| 123 | |
| 124 | Args: |
| 125 | image_fname: Image filename to process |
| 126 | output_fname: Single output filename to use if extracting one file, None |
| 127 | otherwise |
| 128 | outdir: Output directory to use (for any number of files), else None |
| 129 | entry_paths: List of entry paths to extract |
Simon Glass | d48f94e | 2019-07-20 12:24:12 -0600 | [diff] [blame] | 130 | decomp: True to decompress the entry data |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 131 | |
| 132 | Returns: |
| 133 | List of EntryInfo records that were written |
| 134 | """ |
| 135 | image = Image.FromFile(image_fname) |
| 136 | |
| 137 | # Output an entry to a single file, as a special case |
| 138 | if output_fname: |
| 139 | if not entry_paths: |
Simon Glass | a772d3f | 2019-07-20 12:24:14 -0600 | [diff] [blame] | 140 | raise ValueError('Must specify an entry path to write with -f') |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 141 | if len(entry_paths) != 1: |
Simon Glass | a772d3f | 2019-07-20 12:24:14 -0600 | [diff] [blame] | 142 | raise ValueError('Must specify exactly one entry path to write with -f') |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 143 | entry = image.FindEntryPath(entry_paths[0]) |
| 144 | data = entry.ReadData(decomp) |
| 145 | tools.WriteFile(output_fname, data) |
| 146 | tout.Notice("Wrote %#x bytes to file '%s'" % (len(data), output_fname)) |
| 147 | return |
| 148 | |
| 149 | # Otherwise we will output to a path given by the entry path of each entry. |
| 150 | # This means that entries will appear in subdirectories if they are part of |
| 151 | # a sub-section. |
| 152 | einfos = image.GetListEntries(entry_paths)[0] |
| 153 | tout.Notice('%d entries match and will be written' % len(einfos)) |
| 154 | for einfo in einfos: |
| 155 | entry = einfo.entry |
| 156 | data = entry.ReadData(decomp) |
| 157 | path = entry.GetPath()[1:] |
| 158 | fname = os.path.join(outdir, path) |
| 159 | |
| 160 | # If this entry has children, create a directory for it and put its |
| 161 | # data in a file called 'root' in that directory |
| 162 | if entry.GetEntries(): |
| 163 | if not os.path.exists(fname): |
| 164 | os.makedirs(fname) |
| 165 | fname = os.path.join(fname, 'root') |
| 166 | tout.Notice("Write entry '%s' to '%s'" % (entry.GetPath(), fname)) |
| 167 | tools.WriteFile(fname, data) |
| 168 | return einfos |
| 169 | |
| 170 | |
Simon Glass | 274bd0e | 2019-07-20 12:24:13 -0600 | [diff] [blame] | 171 | def BeforeReplace(image, allow_resize): |
| 172 | """Handle getting an image ready for replacing entries in it |
| 173 | |
| 174 | Args: |
| 175 | image: Image to prepare |
| 176 | """ |
| 177 | state.PrepareFromLoadedData(image) |
| 178 | image.LoadData() |
| 179 | |
| 180 | # If repacking, drop the old offset/size values except for the original |
| 181 | # ones, so we are only left with the constraints. |
| 182 | if allow_resize: |
| 183 | image.ResetForPack() |
| 184 | |
| 185 | |
| 186 | def ReplaceOneEntry(image, entry, data, do_compress, allow_resize): |
| 187 | """Handle replacing a single entry an an image |
| 188 | |
| 189 | Args: |
| 190 | image: Image to update |
| 191 | entry: Entry to write |
| 192 | data: Data to replace with |
| 193 | do_compress: True to compress the data if needed, False if data is |
| 194 | already compressed so should be used as is |
| 195 | allow_resize: True to allow entries to change size (this does a re-pack |
| 196 | of the entries), False to raise an exception |
| 197 | """ |
| 198 | if not entry.WriteData(data, do_compress): |
| 199 | if not image.allow_repack: |
| 200 | entry.Raise('Entry data size does not match, but allow-repack is not present for this image') |
| 201 | if not allow_resize: |
| 202 | entry.Raise('Entry data size does not match, but resize is disabled') |
| 203 | |
| 204 | |
| 205 | def AfterReplace(image, allow_resize, write_map): |
| 206 | """Handle write out an image after replacing entries in it |
| 207 | |
| 208 | Args: |
| 209 | image: Image to write |
| 210 | allow_resize: True to allow entries to change size (this does a re-pack |
| 211 | of the entries), False to raise an exception |
| 212 | write_map: True to write a map file |
| 213 | """ |
| 214 | tout.Info('Processing image') |
| 215 | ProcessImage(image, update_fdt=True, write_map=write_map, |
| 216 | get_contents=False, allow_resize=allow_resize) |
| 217 | |
| 218 | |
| 219 | def WriteEntryToImage(image, entry, data, do_compress=True, allow_resize=True, |
| 220 | write_map=False): |
| 221 | BeforeReplace(image, allow_resize) |
| 222 | tout.Info('Writing data to %s' % entry.GetPath()) |
| 223 | ReplaceOneEntry(image, entry, data, do_compress, allow_resize) |
| 224 | AfterReplace(image, allow_resize=allow_resize, write_map=write_map) |
| 225 | |
| 226 | |
Simon Glass | d48f94e | 2019-07-20 12:24:12 -0600 | [diff] [blame] | 227 | def WriteEntry(image_fname, entry_path, data, do_compress=True, |
| 228 | allow_resize=True, write_map=False): |
Simon Glass | 3971c95 | 2019-07-20 12:24:11 -0600 | [diff] [blame] | 229 | """Replace an entry in an image |
| 230 | |
| 231 | This replaces the data in a particular entry in an image. This size of the |
| 232 | new data must match the size of the old data unless allow_resize is True. |
| 233 | |
| 234 | Args: |
| 235 | image_fname: Image filename to process |
| 236 | entry_path: Path to entry to extract |
| 237 | data: Data to replace with |
Simon Glass | d48f94e | 2019-07-20 12:24:12 -0600 | [diff] [blame] | 238 | do_compress: True to compress the data if needed, False if data is |
Simon Glass | 3971c95 | 2019-07-20 12:24:11 -0600 | [diff] [blame] | 239 | already compressed so should be used as is |
| 240 | allow_resize: True to allow entries to change size (this does a re-pack |
| 241 | of the entries), False to raise an exception |
Simon Glass | d48f94e | 2019-07-20 12:24:12 -0600 | [diff] [blame] | 242 | write_map: True to write a map file |
Simon Glass | 3971c95 | 2019-07-20 12:24:11 -0600 | [diff] [blame] | 243 | |
| 244 | Returns: |
| 245 | Image object that was updated |
| 246 | """ |
Simon Glass | 274bd0e | 2019-07-20 12:24:13 -0600 | [diff] [blame] | 247 | tout.Info("Write entry '%s', file '%s'" % (entry_path, image_fname)) |
Simon Glass | 3971c95 | 2019-07-20 12:24:11 -0600 | [diff] [blame] | 248 | image = Image.FromFile(image_fname) |
| 249 | entry = image.FindEntryPath(entry_path) |
Simon Glass | 274bd0e | 2019-07-20 12:24:13 -0600 | [diff] [blame] | 250 | WriteEntryToImage(image, entry, data, do_compress=do_compress, |
| 251 | allow_resize=allow_resize, write_map=write_map) |
Simon Glass | 3971c95 | 2019-07-20 12:24:11 -0600 | [diff] [blame] | 252 | |
Simon Glass | 3971c95 | 2019-07-20 12:24:11 -0600 | [diff] [blame] | 253 | return image |
| 254 | |
Simon Glass | 30033c2 | 2019-07-20 12:24:15 -0600 | [diff] [blame] | 255 | |
| 256 | def ReplaceEntries(image_fname, input_fname, indir, entry_paths, |
| 257 | do_compress=True, allow_resize=True, write_map=False): |
| 258 | """Replace the data from one or more entries from input files |
| 259 | |
| 260 | Args: |
| 261 | image_fname: Image filename to process |
| 262 | input_fname: Single input ilename to use if replacing one file, None |
| 263 | otherwise |
| 264 | indir: Input directory to use (for any number of files), else None |
| 265 | entry_paths: List of entry paths to extract |
| 266 | do_compress: True if the input data is uncompressed and may need to be |
| 267 | compressed if the entry requires it, False if the data is already |
| 268 | compressed. |
| 269 | write_map: True to write a map file |
| 270 | |
| 271 | Returns: |
| 272 | List of EntryInfo records that were written |
| 273 | """ |
| 274 | image = Image.FromFile(image_fname) |
| 275 | |
| 276 | # Replace an entry from a single file, as a special case |
| 277 | if input_fname: |
| 278 | if not entry_paths: |
| 279 | raise ValueError('Must specify an entry path to read with -f') |
| 280 | if len(entry_paths) != 1: |
| 281 | raise ValueError('Must specify exactly one entry path to write with -f') |
| 282 | entry = image.FindEntryPath(entry_paths[0]) |
| 283 | data = tools.ReadFile(input_fname) |
| 284 | tout.Notice("Read %#x bytes from file '%s'" % (len(data), input_fname)) |
| 285 | WriteEntryToImage(image, entry, data, do_compress=do_compress, |
| 286 | allow_resize=allow_resize, write_map=write_map) |
| 287 | return |
| 288 | |
| 289 | # Otherwise we will input from a path given by the entry path of each entry. |
| 290 | # This means that files must appear in subdirectories if they are part of |
| 291 | # a sub-section. |
| 292 | einfos = image.GetListEntries(entry_paths)[0] |
| 293 | tout.Notice("Replacing %d matching entries in image '%s'" % |
| 294 | (len(einfos), image_fname)) |
| 295 | |
| 296 | BeforeReplace(image, allow_resize) |
| 297 | |
| 298 | for einfo in einfos: |
| 299 | entry = einfo.entry |
| 300 | if entry.GetEntries(): |
| 301 | tout.Info("Skipping section entry '%s'" % entry.GetPath()) |
| 302 | continue |
| 303 | |
| 304 | path = entry.GetPath()[1:] |
| 305 | fname = os.path.join(indir, path) |
| 306 | |
| 307 | if os.path.exists(fname): |
| 308 | tout.Notice("Write entry '%s' from file '%s'" % |
| 309 | (entry.GetPath(), fname)) |
| 310 | data = tools.ReadFile(fname) |
| 311 | ReplaceOneEntry(image, entry, data, do_compress, allow_resize) |
| 312 | else: |
| 313 | tout.Warning("Skipping entry '%s' from missing file '%s'" % |
| 314 | (entry.GetPath(), fname)) |
| 315 | |
| 316 | AfterReplace(image, allow_resize=allow_resize, write_map=write_map) |
| 317 | return image |
| 318 | |
| 319 | |
Simon Glass | d3151ff | 2019-07-20 12:23:27 -0600 | [diff] [blame] | 320 | def PrepareImagesAndDtbs(dtb_fname, select_images, update_fdt): |
| 321 | """Prepare the images to be processed and select the device tree |
| 322 | |
| 323 | This function: |
| 324 | - reads in the device tree |
| 325 | - finds and scans the binman node to create all entries |
| 326 | - selects which images to build |
| 327 | - Updates the device tress with placeholder properties for offset, |
| 328 | image-pos, etc. |
| 329 | |
| 330 | Args: |
| 331 | dtb_fname: Filename of the device tree file to use (.dts or .dtb) |
| 332 | selected_images: List of images to output, or None for all |
| 333 | update_fdt: True to update the FDT wth entry offsets, etc. |
| 334 | """ |
| 335 | # Import these here in case libfdt.py is not available, in which case |
| 336 | # the above help option still works. |
Simon Glass | c585dd4 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 337 | from dtoc import fdt |
| 338 | from dtoc import fdt_util |
Simon Glass | d3151ff | 2019-07-20 12:23:27 -0600 | [diff] [blame] | 339 | global images |
| 340 | |
| 341 | # Get the device tree ready by compiling it and copying the compiled |
| 342 | # output into a file in our output directly. Then scan it for use |
| 343 | # in binman. |
| 344 | dtb_fname = fdt_util.EnsureCompiled(dtb_fname) |
| 345 | fname = tools.GetOutputFilename('u-boot.dtb.out') |
| 346 | tools.WriteFile(fname, tools.ReadFile(dtb_fname)) |
| 347 | dtb = fdt.FdtScan(fname) |
| 348 | |
| 349 | node = _FindBinmanNode(dtb) |
| 350 | if not node: |
| 351 | raise ValueError("Device tree '%s' does not have a 'binman' " |
| 352 | "node" % dtb_fname) |
| 353 | |
| 354 | images = _ReadImageDesc(node) |
| 355 | |
| 356 | if select_images: |
| 357 | skip = [] |
| 358 | new_images = OrderedDict() |
| 359 | for name, image in images.items(): |
| 360 | if name in select_images: |
| 361 | new_images[name] = image |
| 362 | else: |
| 363 | skip.append(name) |
| 364 | images = new_images |
| 365 | tout.Notice('Skipping images: %s' % ', '.join(skip)) |
| 366 | |
| 367 | state.Prepare(images, dtb) |
| 368 | |
| 369 | # Prepare the device tree by making sure that any missing |
| 370 | # properties are added (e.g. 'pos' and 'size'). The values of these |
| 371 | # may not be correct yet, but we add placeholders so that the |
| 372 | # size of the device tree is correct. Later, in |
| 373 | # SetCalculatedProperties() we will insert the correct values |
| 374 | # without changing the device-tree size, thus ensuring that our |
| 375 | # entry offsets remain the same. |
| 376 | for image in images.values(): |
| 377 | image.ExpandEntries() |
| 378 | if update_fdt: |
| 379 | image.AddMissingProperties() |
| 380 | image.ProcessFdt(dtb) |
| 381 | |
Simon Glass | 5a30060 | 2019-07-20 12:23:29 -0600 | [diff] [blame] | 382 | for dtb_item in state.GetAllFdts(): |
Simon Glass | d3151ff | 2019-07-20 12:23:27 -0600 | [diff] [blame] | 383 | dtb_item.Sync(auto_resize=True) |
| 384 | dtb_item.Pack() |
| 385 | dtb_item.Flush() |
| 386 | return images |
| 387 | |
| 388 | |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 389 | def ProcessImage(image, update_fdt, write_map, get_contents=True, |
Simon Glass | 5d94cc6 | 2020-07-09 18:39:38 -0600 | [diff] [blame^] | 390 | allow_resize=True, allow_missing=False): |
Simon Glass | b766c5e5 | 2019-07-20 12:23:24 -0600 | [diff] [blame] | 391 | """Perform all steps for this image, including checking and # writing it. |
| 392 | |
| 393 | This means that errors found with a later image will be reported after |
| 394 | earlier images are already completed and written, but that does not seem |
| 395 | important. |
| 396 | |
| 397 | Args: |
| 398 | image: Image to process |
| 399 | update_fdt: True to update the FDT wth entry offsets, etc. |
| 400 | write_map: True to write a map file |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 401 | get_contents: True to get the image contents from files, etc., False if |
| 402 | the contents is already present |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 403 | allow_resize: True to allow entries to change size (this does a re-pack |
| 404 | of the entries), False to raise an exception |
Simon Glass | 5d94cc6 | 2020-07-09 18:39:38 -0600 | [diff] [blame^] | 405 | allow_missing: Allow blob_ext objects to be missing |
Simon Glass | b766c5e5 | 2019-07-20 12:23:24 -0600 | [diff] [blame] | 406 | """ |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 407 | if get_contents: |
Simon Glass | 5d94cc6 | 2020-07-09 18:39:38 -0600 | [diff] [blame^] | 408 | image.SetAllowMissing(allow_missing) |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 409 | image.GetEntryContents() |
Simon Glass | b766c5e5 | 2019-07-20 12:23:24 -0600 | [diff] [blame] | 410 | image.GetEntryOffsets() |
| 411 | |
| 412 | # We need to pack the entries to figure out where everything |
| 413 | # should be placed. This sets the offset/size of each entry. |
| 414 | # However, after packing we call ProcessEntryContents() which |
| 415 | # may result in an entry changing size. In that case we need to |
| 416 | # do another pass. Since the device tree often contains the |
| 417 | # final offset/size information we try to make space for this in |
| 418 | # AddMissingProperties() above. However, if the device is |
| 419 | # compressed we cannot know this compressed size in advance, |
| 420 | # since changing an offset from 0x100 to 0x104 (for example) can |
| 421 | # alter the compressed size of the device tree. So we need a |
| 422 | # third pass for this. |
Simon Glass | 37fdd14 | 2019-07-20 12:24:06 -0600 | [diff] [blame] | 423 | passes = 5 |
Simon Glass | b766c5e5 | 2019-07-20 12:23:24 -0600 | [diff] [blame] | 424 | for pack_pass in range(passes): |
| 425 | try: |
| 426 | image.PackEntries() |
| 427 | image.CheckSize() |
| 428 | image.CheckEntries() |
| 429 | except Exception as e: |
| 430 | if write_map: |
| 431 | fname = image.WriteMap() |
| 432 | print("Wrote map file '%s' to show errors" % fname) |
| 433 | raise |
| 434 | image.SetImagePos() |
| 435 | if update_fdt: |
| 436 | image.SetCalculatedProperties() |
Simon Glass | 5a30060 | 2019-07-20 12:23:29 -0600 | [diff] [blame] | 437 | for dtb_item in state.GetAllFdts(): |
Simon Glass | b766c5e5 | 2019-07-20 12:23:24 -0600 | [diff] [blame] | 438 | dtb_item.Sync() |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 439 | dtb_item.Flush() |
Simon Glass | e594341 | 2019-08-24 07:23:12 -0600 | [diff] [blame] | 440 | image.WriteSymbols() |
Simon Glass | b766c5e5 | 2019-07-20 12:23:24 -0600 | [diff] [blame] | 441 | sizes_ok = image.ProcessEntryContents() |
| 442 | if sizes_ok: |
| 443 | break |
| 444 | image.ResetForPack() |
Simon Glass | 6bf9b47 | 2019-08-24 07:23:13 -0600 | [diff] [blame] | 445 | tout.Info('Pack completed after %d pass(es)' % (pack_pass + 1)) |
Simon Glass | b766c5e5 | 2019-07-20 12:23:24 -0600 | [diff] [blame] | 446 | if not sizes_ok: |
Simon Glass | 9d8ee32 | 2019-07-20 12:23:58 -0600 | [diff] [blame] | 447 | image.Raise('Entries changed size after packing (tried %s passes)' % |
Simon Glass | b766c5e5 | 2019-07-20 12:23:24 -0600 | [diff] [blame] | 448 | passes) |
| 449 | |
Simon Glass | b766c5e5 | 2019-07-20 12:23:24 -0600 | [diff] [blame] | 450 | image.BuildImage() |
| 451 | if write_map: |
| 452 | image.WriteMap() |
| 453 | |
| 454 | |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 455 | def Binman(args): |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 456 | """The main control code for binman |
| 457 | |
| 458 | This assumes that help and test options have already been dealt with. It |
| 459 | deals with the core task of building images. |
| 460 | |
| 461 | Args: |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 462 | args: Command line arguments Namespace object |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 463 | """ |
Simon Glass | b9ba4e0 | 2019-08-24 07:22:44 -0600 | [diff] [blame] | 464 | global Image |
| 465 | global state |
| 466 | |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 467 | if args.full_help: |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 468 | pager = os.getenv('PAGER') |
| 469 | if not pager: |
| 470 | pager = 'more' |
| 471 | fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), |
| 472 | 'README') |
| 473 | command.Run(pager, fname) |
| 474 | return 0 |
| 475 | |
Simon Glass | b9ba4e0 | 2019-08-24 07:22:44 -0600 | [diff] [blame] | 476 | # Put these here so that we can import this module without libfdt |
| 477 | from image import Image |
Simon Glass | c585dd4 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 478 | from binman import state |
Simon Glass | b9ba4e0 | 2019-08-24 07:22:44 -0600 | [diff] [blame] | 479 | |
Simon Glass | df08cbb | 2019-09-15 18:10:36 -0600 | [diff] [blame] | 480 | if args.cmd in ['ls', 'extract', 'replace']: |
Simon Glass | 9b7f500 | 2019-07-20 12:23:53 -0600 | [diff] [blame] | 481 | try: |
Simon Glass | df08cbb | 2019-09-15 18:10:36 -0600 | [diff] [blame] | 482 | tout.Init(args.verbosity) |
Simon Glass | 9b7f500 | 2019-07-20 12:23:53 -0600 | [diff] [blame] | 483 | tools.PrepareOutputDir(None) |
Simon Glass | df08cbb | 2019-09-15 18:10:36 -0600 | [diff] [blame] | 484 | if args.cmd == 'ls': |
| 485 | ListEntries(args.image, args.paths) |
Simon Glass | b2fd11d | 2019-07-08 14:25:48 -0600 | [diff] [blame] | 486 | |
Simon Glass | df08cbb | 2019-09-15 18:10:36 -0600 | [diff] [blame] | 487 | if args.cmd == 'extract': |
| 488 | ExtractEntries(args.image, args.filename, args.outdir, args.paths, |
| 489 | not args.uncompressed) |
Simon Glass | 980a284 | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 490 | |
Simon Glass | df08cbb | 2019-09-15 18:10:36 -0600 | [diff] [blame] | 491 | if args.cmd == 'replace': |
| 492 | ReplaceEntries(args.image, args.filename, args.indir, args.paths, |
| 493 | do_compress=not args.compressed, |
| 494 | allow_resize=not args.fix_size, write_map=args.map) |
| 495 | except: |
| 496 | raise |
Simon Glass | 30033c2 | 2019-07-20 12:24:15 -0600 | [diff] [blame] | 497 | finally: |
| 498 | tools.FinaliseOutputDir() |
| 499 | return 0 |
| 500 | |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 501 | # Try to figure out which device tree contains our image description |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 502 | if args.dt: |
| 503 | dtb_fname = args.dt |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 504 | else: |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 505 | board = args.board |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 506 | if not board: |
| 507 | raise ValueError('Must provide a board to process (use -b <board>)') |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 508 | board_pathname = os.path.join(args.build_dir, board) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 509 | dtb_fname = os.path.join(board_pathname, 'u-boot.dtb') |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 510 | if not args.indir: |
| 511 | args.indir = ['.'] |
| 512 | args.indir.append(board_pathname) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 513 | |
| 514 | try: |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 515 | tout.Init(args.verbosity) |
| 516 | elf.debug = args.debug |
| 517 | cbfs_util.VERBOSE = args.verbosity > 2 |
| 518 | state.use_fake_dtb = args.fake_dtb |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 519 | try: |
Simon Glass | f46732a | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 520 | tools.SetInputDirs(args.indir) |
| 521 | tools.PrepareOutputDir(args.outdir, args.preserve) |
| 522 | tools.SetToolPaths(args.toolpath) |
| 523 | state.SetEntryArgs(args.entry_arg) |
Simon Glass | 9230773 | 2018-07-06 10:27:40 -0600 | [diff] [blame] | 524 | |
Simon Glass | d3151ff | 2019-07-20 12:23:27 -0600 | [diff] [blame] | 525 | images = PrepareImagesAndDtbs(dtb_fname, args.image, |
| 526 | args.update_fdt) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 527 | for image in images.values(): |
Simon Glass | 5d94cc6 | 2020-07-09 18:39:38 -0600 | [diff] [blame^] | 528 | ProcessImage(image, args.update_fdt, args.map, |
| 529 | allow_missing=args.allow_missing) |
Simon Glass | bdb4031 | 2018-09-14 04:57:20 -0600 | [diff] [blame] | 530 | |
| 531 | # Write the updated FDTs to our output files |
Simon Glass | 5a30060 | 2019-07-20 12:23:29 -0600 | [diff] [blame] | 532 | for dtb_item in state.GetAllFdts(): |
Simon Glass | bdb4031 | 2018-09-14 04:57:20 -0600 | [diff] [blame] | 533 | tools.WriteFile(dtb_item._fname, dtb_item.GetContents()) |
| 534 | |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 535 | finally: |
| 536 | tools.FinaliseOutputDir() |
| 537 | finally: |
| 538 | tout.Uninit() |
| 539 | |
| 540 | return 0 |