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 | a944093 | 2017-05-27 07:38:30 -0600 | [diff] [blame] | 15 | import fdt |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 16 | import fdt_util |
| 17 | from image import Image |
| 18 | import tout |
| 19 | |
| 20 | # List of images we plan to create |
| 21 | # Make this global so that it can be referenced from tests |
| 22 | images = OrderedDict() |
| 23 | |
| 24 | def _ReadImageDesc(binman_node): |
| 25 | """Read the image descriptions from the /binman node |
| 26 | |
| 27 | This normally produces a single Image object called 'image'. But if |
| 28 | multiple images are present, they will all be returned. |
| 29 | |
| 30 | Args: |
| 31 | binman_node: Node object of the /binman node |
| 32 | Returns: |
| 33 | OrderedDict of Image objects, each of which describes an image |
| 34 | """ |
| 35 | images = OrderedDict() |
| 36 | if 'multiple-images' in binman_node.props: |
| 37 | for node in binman_node.subnodes: |
| 38 | images[node.name] = Image(node.name, node) |
| 39 | else: |
| 40 | images['image'] = Image('image', binman_node) |
| 41 | return images |
| 42 | |
Simon Glass | 22c92ca | 2017-05-27 07:38:29 -0600 | [diff] [blame] | 43 | def _FindBinmanNode(dtb): |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 44 | """Find the 'binman' node in the device tree |
| 45 | |
| 46 | Args: |
Simon Glass | 22c92ca | 2017-05-27 07:38:29 -0600 | [diff] [blame] | 47 | dtb: Fdt object to scan |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 48 | Returns: |
| 49 | Node object of /binman node, or None if not found |
| 50 | """ |
Simon Glass | 22c92ca | 2017-05-27 07:38:29 -0600 | [diff] [blame] | 51 | for node in dtb.GetRoot().subnodes: |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 52 | if node.name == 'binman': |
| 53 | return node |
| 54 | return None |
| 55 | |
| 56 | def Binman(options, args): |
| 57 | """The main control code for binman |
| 58 | |
| 59 | This assumes that help and test options have already been dealt with. It |
| 60 | deals with the core task of building images. |
| 61 | |
| 62 | Args: |
| 63 | options: Command line options object |
| 64 | args: Command line arguments (list of strings) |
| 65 | """ |
| 66 | global images |
| 67 | |
| 68 | if options.full_help: |
| 69 | pager = os.getenv('PAGER') |
| 70 | if not pager: |
| 71 | pager = 'more' |
| 72 | fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), |
| 73 | 'README') |
| 74 | command.Run(pager, fname) |
| 75 | return 0 |
| 76 | |
| 77 | # Try to figure out which device tree contains our image description |
| 78 | if options.dt: |
| 79 | dtb_fname = options.dt |
| 80 | else: |
| 81 | board = options.board |
| 82 | if not board: |
| 83 | raise ValueError('Must provide a board to process (use -b <board>)') |
| 84 | board_pathname = os.path.join(options.build_dir, board) |
| 85 | dtb_fname = os.path.join(board_pathname, 'u-boot.dtb') |
| 86 | if not options.indir: |
| 87 | options.indir = ['.'] |
| 88 | options.indir.append(board_pathname) |
| 89 | |
| 90 | try: |
| 91 | tout.Init(options.verbosity) |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 92 | elf.debug = options.debug |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 93 | try: |
| 94 | tools.SetInputDirs(options.indir) |
| 95 | tools.PrepareOutputDir(options.outdir, options.preserve) |
Simon Glass | 22c92ca | 2017-05-27 07:38:29 -0600 | [diff] [blame] | 96 | dtb = fdt.FdtScan(dtb_fname) |
| 97 | node = _FindBinmanNode(dtb) |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 98 | if not node: |
| 99 | raise ValueError("Device tree '%s' does not have a 'binman' " |
| 100 | "node" % dtb_fname) |
| 101 | images = _ReadImageDesc(node) |
| 102 | for image in images.values(): |
| 103 | # Perform all steps for this image, including checking and |
| 104 | # writing it. This means that errors found with a later |
| 105 | # image will be reported after earlier images are already |
| 106 | # completed and written, but that does not seem important. |
| 107 | image.GetEntryContents() |
| 108 | image.GetEntryPositions() |
| 109 | image.PackEntries() |
| 110 | image.CheckSize() |
| 111 | image.CheckEntries() |
| 112 | image.ProcessEntryContents() |
Simon Glass | 4ca8e04 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 113 | image.WriteSymbols() |
Simon Glass | 2574ef6 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 114 | image.BuildImage() |
| 115 | finally: |
| 116 | tools.FinaliseOutputDir() |
| 117 | finally: |
| 118 | tout.Uninit() |
| 119 | |
| 120 | return 0 |