Simon Glass | 29aa736 | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
| 2 | # Copyright 2018 Google, Inc |
| 3 | # Written by Simon Glass <sjg@chromium.org> |
| 4 | # |
| 5 | # Holds and modifies the state information held by binman |
| 6 | # |
| 7 | |
| 8 | import re |
| 9 | from sets import Set |
| 10 | |
| 11 | import os |
| 12 | import tools |
| 13 | |
| 14 | # Records the device-tree files known to binman, keyed by filename (e.g. |
| 15 | # 'u-boot-spl.dtb') |
| 16 | fdt_files = {} |
| 17 | |
| 18 | # Arguments passed to binman to provide arguments to entries |
| 19 | entry_args = {} |
| 20 | |
Simon Glass | 0c9d5b5 | 2018-09-14 04:57:22 -0600 | [diff] [blame^] | 21 | # True to use fake device-tree files for testing (see U_BOOT_DTB_DATA in |
| 22 | # ftest.py) |
| 23 | use_fake_dtb = True |
| 24 | |
Simon Glass | bdb4031 | 2018-09-14 04:57:20 -0600 | [diff] [blame] | 25 | # Set of all device tree files references by images |
| 26 | fdt_set = Set() |
| 27 | |
| 28 | # Same as above, but excluding the main one |
| 29 | fdt_subset = Set() |
| 30 | |
| 31 | # The DTB which contains the full image information |
| 32 | main_dtb = None |
| 33 | |
Simon Glass | 29aa736 | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 34 | def GetFdt(fname): |
| 35 | """Get the Fdt object for a particular device-tree filename |
| 36 | |
| 37 | Binman keeps track of at least one device-tree file called u-boot.dtb but |
| 38 | can also have others (e.g. for SPL). This function looks up the given |
| 39 | filename and returns the associated Fdt object. |
| 40 | |
| 41 | Args: |
| 42 | fname: Filename to look up (e.g. 'u-boot.dtb'). |
| 43 | |
| 44 | Returns: |
| 45 | Fdt object associated with the filename |
| 46 | """ |
| 47 | return fdt_files[fname] |
| 48 | |
| 49 | def GetFdtPath(fname): |
| 50 | """Get the full pathname of a particular Fdt object |
| 51 | |
| 52 | Similar to GetFdt() but returns the pathname associated with the Fdt. |
| 53 | |
| 54 | Args: |
| 55 | fname: Filename to look up (e.g. 'u-boot.dtb'). |
| 56 | |
| 57 | Returns: |
| 58 | Full path name to the associated Fdt |
| 59 | """ |
| 60 | return fdt_files[fname]._fname |
| 61 | |
| 62 | def SetEntryArgs(args): |
| 63 | """Set the value of the entry args |
| 64 | |
| 65 | This sets up the entry_args dict which is used to supply entry arguments to |
| 66 | entries. |
| 67 | |
| 68 | Args: |
| 69 | args: List of entry arguments, each in the format "name=value" |
| 70 | """ |
| 71 | global entry_args |
| 72 | |
| 73 | entry_args = {} |
| 74 | if args: |
| 75 | for arg in args: |
| 76 | m = re.match('([^=]*)=(.*)', arg) |
| 77 | if not m: |
| 78 | raise ValueError("Invalid entry arguemnt '%s'" % arg) |
| 79 | entry_args[m.group(1)] = m.group(2) |
| 80 | |
| 81 | def GetEntryArg(name): |
| 82 | """Get the value of an entry argument |
| 83 | |
| 84 | Args: |
| 85 | name: Name of argument to retrieve |
| 86 | |
| 87 | Returns: |
| 88 | String value of argument |
| 89 | """ |
| 90 | return entry_args.get(name) |
Simon Glass | bdb4031 | 2018-09-14 04:57:20 -0600 | [diff] [blame] | 91 | |
Simon Glass | 0c9d5b5 | 2018-09-14 04:57:22 -0600 | [diff] [blame^] | 92 | def Prepare(images, dtb): |
Simon Glass | bdb4031 | 2018-09-14 04:57:20 -0600 | [diff] [blame] | 93 | """Get device tree files ready for use |
| 94 | |
| 95 | This sets up a set of device tree files that can be retrieved by GetFdts(). |
| 96 | At present there is only one, that for U-Boot proper. |
| 97 | |
| 98 | Args: |
Simon Glass | 0c9d5b5 | 2018-09-14 04:57:22 -0600 | [diff] [blame^] | 99 | images: List of images being used |
Simon Glass | bdb4031 | 2018-09-14 04:57:20 -0600 | [diff] [blame] | 100 | dtb: Main dtb |
| 101 | """ |
| 102 | global fdt_set, fdt_subset, fdt_files, main_dtb |
| 103 | # Import these here in case libfdt.py is not available, in which case |
| 104 | # the above help option still works. |
| 105 | import fdt |
| 106 | import fdt_util |
| 107 | |
| 108 | # If we are updating the DTBs we need to put these updated versions |
| 109 | # where Entry_blob_dtb can find them. We can ignore 'u-boot.dtb' |
| 110 | # since it is assumed to be the one passed in with options.dt, and |
| 111 | # was handled just above. |
| 112 | main_dtb = dtb |
| 113 | fdt_files.clear() |
| 114 | fdt_files['u-boot.dtb'] = dtb |
Simon Glass | bdb4031 | 2018-09-14 04:57:20 -0600 | [diff] [blame] | 115 | fdt_subset = Set() |
Simon Glass | 0c9d5b5 | 2018-09-14 04:57:22 -0600 | [diff] [blame^] | 116 | if not use_fake_dtb: |
| 117 | for image in images.values(): |
| 118 | fdt_subset.update(image.GetFdtSet()) |
| 119 | fdt_subset.discard('u-boot.dtb') |
| 120 | for other_fname in fdt_subset: |
| 121 | infile = tools.GetInputFilename(other_fname) |
| 122 | other_fname_dtb = fdt_util.EnsureCompiled(infile) |
| 123 | out_fname = tools.GetOutputFilename('%s.out' % |
| 124 | os.path.split(other_fname)[1]) |
| 125 | tools.WriteFile(out_fname, tools.ReadFile(other_fname_dtb)) |
| 126 | other_dtb = fdt.FdtScan(out_fname) |
| 127 | fdt_files[other_fname] = other_dtb |
Simon Glass | bdb4031 | 2018-09-14 04:57:20 -0600 | [diff] [blame] | 128 | |
| 129 | def GetFdts(): |
| 130 | """Yield all device tree files being used by binman |
| 131 | |
| 132 | Yields: |
| 133 | Device trees being used (U-Boot proper, SPL, TPL) |
| 134 | """ |
| 135 | yield main_dtb |
| 136 | |
Simon Glass | c8135dc | 2018-09-14 04:57:21 -0600 | [diff] [blame] | 137 | def GetUpdateNodes(node): |
| 138 | """Yield all the nodes that need to be updated in all device trees |
| 139 | |
| 140 | The property referenced by this node is added to any device trees which |
| 141 | have the given node. Due to removable of unwanted notes, SPL and TPL may |
| 142 | not have this node. |
| 143 | |
| 144 | Args: |
| 145 | node: Node object in the main device tree to look up |
| 146 | |
| 147 | Yields: |
| 148 | Node objects in each device tree that is in use (U-Boot proper, which |
| 149 | is node, SPL and TPL) |
| 150 | """ |
| 151 | yield node |
| 152 | |
| 153 | def AddZeroProp(node, prop): |
| 154 | """Add a new property to affected device trees with an integer value of 0. |
| 155 | |
| 156 | Args: |
| 157 | prop_name: Name of property |
| 158 | """ |
| 159 | for n in GetUpdateNodes(node): |
| 160 | n.AddZeroProp(prop) |
| 161 | |
| 162 | def SetInt(node, prop, value): |
| 163 | """Update an integer property in affected device trees with an integer value |
| 164 | |
| 165 | This is not allowed to change the size of the FDT. |
| 166 | |
| 167 | Args: |
| 168 | prop_name: Name of property |
| 169 | """ |
| 170 | for n in GetUpdateNodes(node): |
| 171 | n.SetInt(prop, value) |