Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 1 | # Copyright (c) 2016 Google, Inc |
| 2 | # Written by Simon Glass <sjg@chromium.org> |
| 3 | # |
| 4 | # SPDX-License-Identifier: GPL-2.0+ |
| 5 | # |
| 6 | # Creates binary images from input files controlled by a description |
| 7 | # |
| 8 | |
| 9 | from collections import OrderedDict |
| 10 | import os |
| 11 | import sys |
| 12 | import tools |
| 13 | |
| 14 | import command |
Simon Glass | 7fe9173 | 2017-11-13 18:55:00 -0700 | [diff] [blame] | 15 | import elf |
Simon Glass | 99ed4a2 | 2017-05-27 07:38:30 -0600 | [diff] [blame] | 16 | import fdt |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 17 | import fdt_util |
| 18 | from image import Image |
| 19 | import tout |
| 20 | |
| 21 | # List of images we plan to create |
| 22 | # Make this global so that it can be referenced from tests |
| 23 | images = OrderedDict() |
| 24 | |
| 25 | def _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 Glass | ec3f378 | 2017-05-27 07:38:29 -0600 | [diff] [blame] | 44 | def _FindBinmanNode(dtb): |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 45 | """Find the 'binman' node in the device tree |
| 46 | |
| 47 | Args: |
Simon Glass | ec3f378 | 2017-05-27 07:38:29 -0600 | [diff] [blame] | 48 | dtb: Fdt object to scan |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 49 | Returns: |
| 50 | Node object of /binman node, or None if not found |
| 51 | """ |
Simon Glass | ec3f378 | 2017-05-27 07:38:29 -0600 | [diff] [blame] | 52 | for node in dtb.GetRoot().subnodes: |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 53 | if node.name == 'binman': |
| 54 | return node |
| 55 | return None |
| 56 | |
| 57 | def Binman(options, args): |
| 58 | """The main control code for binman |
| 59 | |
| 60 | This assumes that help and test options have already been dealt with. It |
| 61 | deals with the core task of building images. |
| 62 | |
| 63 | Args: |
| 64 | options: Command line options object |
| 65 | args: Command line arguments (list of strings) |
| 66 | """ |
| 67 | global images |
| 68 | |
| 69 | if options.full_help: |
| 70 | pager = os.getenv('PAGER') |
| 71 | if not pager: |
| 72 | pager = 'more' |
| 73 | fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), |
| 74 | 'README') |
| 75 | command.Run(pager, fname) |
| 76 | return 0 |
| 77 | |
| 78 | # Try to figure out which device tree contains our image description |
| 79 | if options.dt: |
| 80 | dtb_fname = options.dt |
| 81 | else: |
| 82 | board = options.board |
| 83 | if not board: |
| 84 | raise ValueError('Must provide a board to process (use -b <board>)') |
| 85 | board_pathname = os.path.join(options.build_dir, board) |
| 86 | dtb_fname = os.path.join(board_pathname, 'u-boot.dtb') |
| 87 | if not options.indir: |
| 88 | options.indir = ['.'] |
| 89 | options.indir.append(board_pathname) |
| 90 | |
| 91 | try: |
| 92 | tout.Init(options.verbosity) |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 93 | elf.debug = options.debug |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 94 | try: |
| 95 | tools.SetInputDirs(options.indir) |
| 96 | tools.PrepareOutputDir(options.outdir, options.preserve) |
Simon Glass | ec3f378 | 2017-05-27 07:38:29 -0600 | [diff] [blame] | 97 | dtb = fdt.FdtScan(dtb_fname) |
| 98 | node = _FindBinmanNode(dtb) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 99 | if not node: |
| 100 | raise ValueError("Device tree '%s' does not have a 'binman' " |
| 101 | "node" % dtb_fname) |
| 102 | images = _ReadImageDesc(node) |
| 103 | for image in images.values(): |
| 104 | # Perform all steps for this image, including checking and |
| 105 | # writing it. This means that errors found with a later |
| 106 | # image will be reported after earlier images are already |
| 107 | # completed and written, but that does not seem important. |
| 108 | image.GetEntryContents() |
| 109 | image.GetEntryPositions() |
| 110 | image.PackEntries() |
| 111 | image.CheckSize() |
| 112 | image.CheckEntries() |
| 113 | image.ProcessEntryContents() |
Simon Glass | 1979063 | 2017-11-13 18:55:01 -0700 | [diff] [blame] | 114 | image.WriteSymbols() |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 115 | image.BuildImage() |
| 116 | finally: |
| 117 | tools.FinaliseOutputDir() |
| 118 | finally: |
| 119 | tout.Uninit() |
| 120 | |
| 121 | return 0 |