Simon Glass | c55a50f | 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 | |
Simon Glass | 03ebc20 | 2021-07-06 10:36:41 -0600 | [diff] [blame] | 8 | from collections import defaultdict |
Simon Glass | e0e5df9 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 9 | import hashlib |
Simon Glass | c55a50f | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 10 | import re |
Simon Glass | 03ebc20 | 2021-07-06 10:36:41 -0600 | [diff] [blame] | 11 | import time |
Simon Glass | c69d19c | 2021-07-06 10:36:37 -0600 | [diff] [blame] | 12 | import threading |
Simon Glass | c55a50f | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 13 | |
Simon Glass | 1628793 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 14 | from dtoc import fdt |
Simon Glass | c55a50f | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 15 | import os |
Simon Glass | 4583c00 | 2023-02-23 18:18:04 -0700 | [diff] [blame] | 16 | from u_boot_pylib import tools |
| 17 | from u_boot_pylib import tout |
Simon Glass | c55a50f | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 18 | |
Simon Glass | c475dec | 2021-11-23 11:03:42 -0700 | [diff] [blame] | 19 | OUR_PATH = os.path.dirname(os.path.realpath(__file__)) |
| 20 | |
Simon Glass | 7697170 | 2021-03-18 20:25:00 +1300 | [diff] [blame] | 21 | # Map an dtb etype to its expected filename |
| 22 | DTB_TYPE_FNAME = { |
| 23 | 'u-boot-spl-dtb': 'spl/u-boot-spl.dtb', |
| 24 | 'u-boot-tpl-dtb': 'tpl/u-boot-tpl.dtb', |
Simon Glass | 6ad2452 | 2022-02-28 07:16:54 -0700 | [diff] [blame] | 25 | 'u-boot-vpl-dtb': 'vpl/u-boot-vpl.dtb', |
Simon Glass | 7697170 | 2021-03-18 20:25:00 +1300 | [diff] [blame] | 26 | } |
| 27 | |
Simon Glass | fb5e8b1 | 2019-07-20 12:23:32 -0600 | [diff] [blame] | 28 | # Records the device-tree files known to binman, keyed by entry type (e.g. |
| 29 | # 'u-boot-spl-dtb'). These are the output FDT files, which can be updated by |
| 30 | # binman. They have been copied to <xxx>.out files. |
| 31 | # |
Simon Glass | cb8bebb | 2021-03-18 20:25:01 +1300 | [diff] [blame] | 32 | # key: entry type (e.g. 'u-boot-dtb) |
Simon Glass | fb5e8b1 | 2019-07-20 12:23:32 -0600 | [diff] [blame] | 33 | # value: tuple: |
| 34 | # Fdt object |
| 35 | # Filename |
Simon Glass | 6ca0dcb | 2019-07-20 12:23:43 -0600 | [diff] [blame] | 36 | output_fdt_info = {} |
Simon Glass | c55a50f | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 37 | |
Simon Glass | 10f9d00 | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 38 | # Prefix to add to an fdtmap path to turn it into a path to the /binman node |
| 39 | fdt_path_prefix = '' |
| 40 | |
Simon Glass | c55a50f | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 41 | # Arguments passed to binman to provide arguments to entries |
| 42 | entry_args = {} |
| 43 | |
Simon Glass | 539aece | 2018-09-14 04:57:22 -0600 | [diff] [blame] | 44 | # True to use fake device-tree files for testing (see U_BOOT_DTB_DATA in |
| 45 | # ftest.py) |
Simon Glass | 93d1741 | 2018-09-14 04:57:23 -0600 | [diff] [blame] | 46 | use_fake_dtb = False |
Simon Glass | 539aece | 2018-09-14 04:57:22 -0600 | [diff] [blame] | 47 | |
Simon Glass | 2a72cc7 | 2018-09-14 04:57:20 -0600 | [diff] [blame] | 48 | # The DTB which contains the full image information |
| 49 | main_dtb = None |
| 50 | |
Simon Glass | bf6906b | 2019-07-08 14:25:36 -0600 | [diff] [blame] | 51 | # Allow entries to expand after they have been packed. This is detected and |
| 52 | # forces a re-pack. If not allowed, any attempted expansion causes an error in |
| 53 | # Entry.ProcessContentsUpdate() |
| 54 | allow_entry_expansion = True |
| 55 | |
Simon Glass | 61ec04f | 2019-07-20 12:23:58 -0600 | [diff] [blame] | 56 | # Don't allow entries to contract after they have been packed. Instead just |
| 57 | # leave some wasted space. If allowed, this is detected and forces a re-pack, |
| 58 | # but may result in entries that oscillate in size, thus causing a pack error. |
| 59 | # An example is a compressed device tree where the original offset values |
| 60 | # result in a larger compressed size than the new ones, but then after updating |
| 61 | # to the new ones, the compressed size increases, etc. |
| 62 | allow_entry_contraction = False |
| 63 | |
Simon Glass | c69d19c | 2021-07-06 10:36:37 -0600 | [diff] [blame] | 64 | # Number of threads to use for binman (None means machine-dependent) |
| 65 | num_threads = None |
| 66 | |
Simon Glass | 03ebc20 | 2021-07-06 10:36:41 -0600 | [diff] [blame] | 67 | |
| 68 | class Timing: |
| 69 | """Holds information about an operation that is being timed |
| 70 | |
| 71 | Properties: |
| 72 | name: Operation name (only one of each name is stored) |
| 73 | start: Start time of operation in seconds (None if not start) |
| 74 | accum:: Amount of time spent on this operation so far, in seconds |
| 75 | """ |
| 76 | def __init__(self, name): |
| 77 | self.name = name |
| 78 | self.start = None # cause an error if TimingStart() is not called |
| 79 | self.accum = 0.0 |
| 80 | |
| 81 | |
| 82 | # Holds timing info for each name: |
| 83 | # key: name of Timing info (Timing.name) |
| 84 | # value: Timing object |
| 85 | timing_info = {} |
| 86 | |
| 87 | |
Simon Glass | fb5e8b1 | 2019-07-20 12:23:32 -0600 | [diff] [blame] | 88 | def GetFdtForEtype(etype): |
| 89 | """Get the Fdt object for a particular device-tree entry |
Simon Glass | c55a50f | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 90 | |
| 91 | Binman keeps track of at least one device-tree file called u-boot.dtb but |
| 92 | can also have others (e.g. for SPL). This function looks up the given |
Simon Glass | fb5e8b1 | 2019-07-20 12:23:32 -0600 | [diff] [blame] | 93 | entry and returns the associated Fdt object. |
Simon Glass | c55a50f | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 94 | |
| 95 | Args: |
Simon Glass | fb5e8b1 | 2019-07-20 12:23:32 -0600 | [diff] [blame] | 96 | etype: Entry type of device tree (e.g. 'u-boot-dtb') |
Simon Glass | c55a50f | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 97 | |
| 98 | Returns: |
Simon Glass | fb5e8b1 | 2019-07-20 12:23:32 -0600 | [diff] [blame] | 99 | Fdt object associated with the entry type |
Simon Glass | c55a50f | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 100 | """ |
Simon Glass | 6ca0dcb | 2019-07-20 12:23:43 -0600 | [diff] [blame] | 101 | value = output_fdt_info.get(etype); |
Simon Glass | 6a3b5b5 | 2019-07-20 12:23:42 -0600 | [diff] [blame] | 102 | if not value: |
| 103 | return None |
| 104 | return value[0] |
Simon Glass | c55a50f | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 105 | |
Simon Glass | fb5e8b1 | 2019-07-20 12:23:32 -0600 | [diff] [blame] | 106 | def GetFdtPath(etype): |
Simon Glass | c55a50f | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 107 | """Get the full pathname of a particular Fdt object |
| 108 | |
Simon Glass | 726e296 | 2019-07-20 12:23:30 -0600 | [diff] [blame] | 109 | Similar to GetFdtForEtype() but returns the pathname associated with the |
| 110 | Fdt. |
Simon Glass | c55a50f | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 111 | |
| 112 | Args: |
Simon Glass | fb5e8b1 | 2019-07-20 12:23:32 -0600 | [diff] [blame] | 113 | etype: Entry type of device tree (e.g. 'u-boot-dtb') |
Simon Glass | c55a50f | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 114 | |
| 115 | Returns: |
| 116 | Full path name to the associated Fdt |
| 117 | """ |
Simon Glass | 6ca0dcb | 2019-07-20 12:23:43 -0600 | [diff] [blame] | 118 | return output_fdt_info[etype][0]._fname |
Simon Glass | c55a50f | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 119 | |
Simon Glass | fb5e8b1 | 2019-07-20 12:23:32 -0600 | [diff] [blame] | 120 | def GetFdtContents(etype='u-boot-dtb'): |
Simon Glass | 6ed45ba | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 121 | """Looks up the FDT pathname and contents |
| 122 | |
| 123 | This is used to obtain the Fdt pathname and contents when needed by an |
| 124 | entry. It supports a 'fake' dtb, allowing tests to substitute test data for |
| 125 | the real dtb. |
| 126 | |
| 127 | Args: |
Simon Glass | fb5e8b1 | 2019-07-20 12:23:32 -0600 | [diff] [blame] | 128 | etype: Entry type to look up (e.g. 'u-boot.dtb'). |
Simon Glass | 6ed45ba | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 129 | |
| 130 | Returns: |
| 131 | tuple: |
| 132 | pathname to Fdt |
| 133 | Fdt data (as bytes) |
| 134 | """ |
Simon Glass | 6ca0dcb | 2019-07-20 12:23:43 -0600 | [diff] [blame] | 135 | if etype not in output_fdt_info: |
Simon Glass | 6a3b5b5 | 2019-07-20 12:23:42 -0600 | [diff] [blame] | 136 | return None, None |
| 137 | if not use_fake_dtb: |
Simon Glass | fb5e8b1 | 2019-07-20 12:23:32 -0600 | [diff] [blame] | 138 | pathname = GetFdtPath(etype) |
| 139 | data = GetFdtForEtype(etype).GetContents() |
Simon Glass | 6ed45ba | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 140 | else: |
Simon Glass | 6ca0dcb | 2019-07-20 12:23:43 -0600 | [diff] [blame] | 141 | fname = output_fdt_info[etype][1] |
Simon Glass | c1aa66e | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 142 | pathname = tools.get_input_filename(fname) |
| 143 | data = tools.read_file(pathname) |
Simon Glass | 6ed45ba | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 144 | return pathname, data |
| 145 | |
Simon Glass | f6e0249 | 2019-07-20 12:24:08 -0600 | [diff] [blame] | 146 | def UpdateFdtContents(etype, data): |
| 147 | """Update the contents of a particular device tree |
| 148 | |
| 149 | The device tree is updated and written back to its file. This affects what |
| 150 | is returned from future called to GetFdtContents(), etc. |
| 151 | |
| 152 | Args: |
| 153 | etype: Entry type (e.g. 'u-boot-dtb') |
| 154 | data: Data to replace the DTB with |
| 155 | """ |
Simon Glass | cb8bebb | 2021-03-18 20:25:01 +1300 | [diff] [blame] | 156 | dtb, fname = output_fdt_info[etype] |
Simon Glass | f6e0249 | 2019-07-20 12:24:08 -0600 | [diff] [blame] | 157 | dtb_fname = dtb.GetFilename() |
Simon Glass | c1aa66e | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 158 | tools.write_file(dtb_fname, data) |
Simon Glass | f6e0249 | 2019-07-20 12:24:08 -0600 | [diff] [blame] | 159 | dtb = fdt.FdtScan(dtb_fname) |
Simon Glass | cb8bebb | 2021-03-18 20:25:01 +1300 | [diff] [blame] | 160 | output_fdt_info[etype] = [dtb, fname] |
Simon Glass | f6e0249 | 2019-07-20 12:24:08 -0600 | [diff] [blame] | 161 | |
Simon Glass | c55a50f | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 162 | def SetEntryArgs(args): |
| 163 | """Set the value of the entry args |
| 164 | |
| 165 | This sets up the entry_args dict which is used to supply entry arguments to |
| 166 | entries. |
| 167 | |
| 168 | Args: |
| 169 | args: List of entry arguments, each in the format "name=value" |
| 170 | """ |
| 171 | global entry_args |
| 172 | |
| 173 | entry_args = {} |
Simon Glass | f3385a5 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 174 | tout.debug('Processing entry args:') |
Simon Glass | c55a50f | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 175 | if args: |
| 176 | for arg in args: |
| 177 | m = re.match('([^=]*)=(.*)', arg) |
| 178 | if not m: |
| 179 | raise ValueError("Invalid entry arguemnt '%s'" % arg) |
Simon Glass | 0668492 | 2021-03-18 20:25:07 +1300 | [diff] [blame] | 180 | name, value = m.groups() |
Simon Glass | f3385a5 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 181 | tout.debug(' %20s = %s' % (name, value)) |
Simon Glass | 0668492 | 2021-03-18 20:25:07 +1300 | [diff] [blame] | 182 | entry_args[name] = value |
Simon Glass | f3385a5 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 183 | tout.debug('Processing entry args done') |
Simon Glass | c55a50f | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 184 | |
| 185 | def GetEntryArg(name): |
| 186 | """Get the value of an entry argument |
| 187 | |
| 188 | Args: |
| 189 | name: Name of argument to retrieve |
| 190 | |
| 191 | Returns: |
| 192 | String value of argument |
| 193 | """ |
| 194 | return entry_args.get(name) |
Simon Glass | 2a72cc7 | 2018-09-14 04:57:20 -0600 | [diff] [blame] | 195 | |
Simon Glass | 0668492 | 2021-03-18 20:25:07 +1300 | [diff] [blame] | 196 | def GetEntryArgBool(name): |
| 197 | """Get the value of an entry argument as a boolean |
| 198 | |
| 199 | Args: |
| 200 | name: Name of argument to retrieve |
| 201 | |
| 202 | Returns: |
| 203 | False if the entry argument is consider False (empty, '0' or 'n'), else |
| 204 | True |
| 205 | """ |
| 206 | val = GetEntryArg(name) |
| 207 | return val and val not in ['n', '0'] |
| 208 | |
Simon Glass | 539aece | 2018-09-14 04:57:22 -0600 | [diff] [blame] | 209 | def Prepare(images, dtb): |
Simon Glass | 2a72cc7 | 2018-09-14 04:57:20 -0600 | [diff] [blame] | 210 | """Get device tree files ready for use |
| 211 | |
Simon Glass | 4bdd115 | 2019-07-20 12:23:29 -0600 | [diff] [blame] | 212 | This sets up a set of device tree files that can be retrieved by |
| 213 | GetAllFdts(). This includes U-Boot proper and any SPL device trees. |
Simon Glass | 2a72cc7 | 2018-09-14 04:57:20 -0600 | [diff] [blame] | 214 | |
| 215 | Args: |
Simon Glass | 539aece | 2018-09-14 04:57:22 -0600 | [diff] [blame] | 216 | images: List of images being used |
Simon Glass | 2a72cc7 | 2018-09-14 04:57:20 -0600 | [diff] [blame] | 217 | dtb: Main dtb |
| 218 | """ |
Simon Glass | 10f9d00 | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 219 | global output_fdt_info, main_dtb, fdt_path_prefix |
Simon Glass | 2a72cc7 | 2018-09-14 04:57:20 -0600 | [diff] [blame] | 220 | # Import these here in case libfdt.py is not available, in which case |
| 221 | # the above help option still works. |
Simon Glass | 1628793 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 222 | from dtoc import fdt |
| 223 | from dtoc import fdt_util |
Simon Glass | 2a72cc7 | 2018-09-14 04:57:20 -0600 | [diff] [blame] | 224 | |
| 225 | # If we are updating the DTBs we need to put these updated versions |
| 226 | # where Entry_blob_dtb can find them. We can ignore 'u-boot.dtb' |
| 227 | # since it is assumed to be the one passed in with options.dt, and |
| 228 | # was handled just above. |
| 229 | main_dtb = dtb |
Simon Glass | 6ca0dcb | 2019-07-20 12:23:43 -0600 | [diff] [blame] | 230 | output_fdt_info.clear() |
Simon Glass | 10f9d00 | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 231 | fdt_path_prefix = '' |
Simon Glass | cb8bebb | 2021-03-18 20:25:01 +1300 | [diff] [blame] | 232 | output_fdt_info['u-boot-dtb'] = [dtb, 'u-boot.dtb'] |
Simon Glass | 7697170 | 2021-03-18 20:25:00 +1300 | [diff] [blame] | 233 | if use_fake_dtb: |
| 234 | for etype, fname in DTB_TYPE_FNAME.items(): |
Simon Glass | cb8bebb | 2021-03-18 20:25:01 +1300 | [diff] [blame] | 235 | output_fdt_info[etype] = [dtb, fname] |
Simon Glass | 7697170 | 2021-03-18 20:25:00 +1300 | [diff] [blame] | 236 | else: |
Simon Glass | f49462e | 2019-07-20 12:23:34 -0600 | [diff] [blame] | 237 | fdt_set = {} |
Simon Glass | 5187b80 | 2021-03-18 20:25:03 +1300 | [diff] [blame] | 238 | for etype, fname in DTB_TYPE_FNAME.items(): |
Simon Glass | c1aa66e | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 239 | infile = tools.get_input_filename(fname, allow_missing=True) |
Simon Glass | 5187b80 | 2021-03-18 20:25:03 +1300 | [diff] [blame] | 240 | if infile and os.path.exists(infile): |
| 241 | fname_dtb = fdt_util.EnsureCompiled(infile) |
Simon Glass | c1aa66e | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 242 | out_fname = tools.get_output_filename('%s.out' % |
Simon Glass | 5187b80 | 2021-03-18 20:25:03 +1300 | [diff] [blame] | 243 | os.path.split(fname)[1]) |
Simon Glass | c1aa66e | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 244 | tools.write_file(out_fname, tools.read_file(fname_dtb)) |
Simon Glass | 5187b80 | 2021-03-18 20:25:03 +1300 | [diff] [blame] | 245 | other_dtb = fdt.FdtScan(out_fname) |
| 246 | output_fdt_info[etype] = [other_dtb, out_fname] |
| 247 | |
Simon Glass | 2a72cc7 | 2018-09-14 04:57:20 -0600 | [diff] [blame] | 248 | |
Simon Glass | 10f9d00 | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 249 | def PrepareFromLoadedData(image): |
| 250 | """Get device tree files ready for use with a loaded image |
| 251 | |
| 252 | Loaded images are different from images that are being created by binman, |
| 253 | since there is generally already an fdtmap and we read the description from |
| 254 | that. This provides the position and size of every entry in the image with |
| 255 | no calculation required. |
| 256 | |
| 257 | This function uses the same output_fdt_info[] as Prepare(). It finds the |
| 258 | device tree files, adds a reference to the fdtmap and sets the FDT path |
| 259 | prefix to translate from the fdtmap (where the root node is the image node) |
| 260 | to the normal device tree (where the image node is under a /binman node). |
| 261 | |
| 262 | Args: |
| 263 | images: List of images being used |
| 264 | """ |
| 265 | global output_fdt_info, main_dtb, fdt_path_prefix |
| 266 | |
Simon Glass | f3385a5 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 267 | tout.info('Preparing device trees') |
Simon Glass | 10f9d00 | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 268 | output_fdt_info.clear() |
| 269 | fdt_path_prefix = '' |
Simon Glass | cb8bebb | 2021-03-18 20:25:01 +1300 | [diff] [blame] | 270 | output_fdt_info['fdtmap'] = [image.fdtmap_dtb, 'u-boot.dtb'] |
Simon Glass | 10f9d00 | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 271 | main_dtb = None |
Simon Glass | f3385a5 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 272 | tout.info(" Found device tree type 'fdtmap' '%s'" % image.fdtmap_dtb.name) |
Simon Glass | 10f9d00 | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 273 | for etype, value in image.GetFdts().items(): |
| 274 | entry, fname = value |
Simon Glass | c1aa66e | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 275 | out_fname = tools.get_output_filename('%s.dtb' % entry.etype) |
Simon Glass | f3385a5 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 276 | tout.info(" Found device tree type '%s' at '%s' path '%s'" % |
Simon Glass | 10f9d00 | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 277 | (etype, out_fname, entry.GetPath())) |
| 278 | entry._filename = entry.GetDefaultFilename() |
| 279 | data = entry.ReadData() |
| 280 | |
Simon Glass | c1aa66e | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 281 | tools.write_file(out_fname, data) |
Simon Glass | 10f9d00 | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 282 | dtb = fdt.Fdt(out_fname) |
| 283 | dtb.Scan() |
| 284 | image_node = dtb.GetNode('/binman') |
| 285 | if 'multiple-images' in image_node.props: |
| 286 | image_node = dtb.GetNode('/binman/%s' % image.image_node) |
| 287 | fdt_path_prefix = image_node.path |
Simon Glass | cb8bebb | 2021-03-18 20:25:01 +1300 | [diff] [blame] | 288 | output_fdt_info[etype] = [dtb, None] |
Simon Glass | f3385a5 | 2022-01-29 14:14:15 -0700 | [diff] [blame] | 289 | tout.info(" FDT path prefix '%s'" % fdt_path_prefix) |
Simon Glass | 10f9d00 | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 290 | |
| 291 | |
Simon Glass | 4bdd115 | 2019-07-20 12:23:29 -0600 | [diff] [blame] | 292 | def GetAllFdts(): |
Simon Glass | 2a72cc7 | 2018-09-14 04:57:20 -0600 | [diff] [blame] | 293 | """Yield all device tree files being used by binman |
| 294 | |
| 295 | Yields: |
Simon Glass | 6ad2452 | 2022-02-28 07:16:54 -0700 | [diff] [blame] | 296 | Device trees being used (U-Boot proper, SPL, TPL, VPL) |
Simon Glass | 2a72cc7 | 2018-09-14 04:57:20 -0600 | [diff] [blame] | 297 | """ |
Simon Glass | 10f9d00 | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 298 | if main_dtb: |
| 299 | yield main_dtb |
Simon Glass | 6ca0dcb | 2019-07-20 12:23:43 -0600 | [diff] [blame] | 300 | for etype in output_fdt_info: |
| 301 | dtb = output_fdt_info[etype][0] |
Simon Glass | 77e4ef1 | 2019-07-20 12:23:33 -0600 | [diff] [blame] | 302 | if dtb != main_dtb: |
| 303 | yield dtb |
Simon Glass | 2a72cc7 | 2018-09-14 04:57:20 -0600 | [diff] [blame] | 304 | |
Simon Glass | 12bb1a9 | 2019-07-20 12:23:51 -0600 | [diff] [blame] | 305 | def GetUpdateNodes(node, for_repack=False): |
Simon Glass | f46621d | 2018-09-14 04:57:21 -0600 | [diff] [blame] | 306 | """Yield all the nodes that need to be updated in all device trees |
| 307 | |
| 308 | The property referenced by this node is added to any device trees which |
Jonas Karlman | dd4bdad | 2023-02-19 22:02:03 +0000 | [diff] [blame] | 309 | have the given node. Due to removable of unwanted nodes, SPL and TPL may |
Simon Glass | f46621d | 2018-09-14 04:57:21 -0600 | [diff] [blame] | 310 | not have this node. |
| 311 | |
| 312 | Args: |
| 313 | node: Node object in the main device tree to look up |
Simon Glass | 12bb1a9 | 2019-07-20 12:23:51 -0600 | [diff] [blame] | 314 | for_repack: True if we want only nodes which need 'repack' properties |
| 315 | added to them (e.g. 'orig-offset'), False to return all nodes. We |
| 316 | don't add repack properties to SPL/TPL device trees. |
Simon Glass | f46621d | 2018-09-14 04:57:21 -0600 | [diff] [blame] | 317 | |
| 318 | Yields: |
| 319 | Node objects in each device tree that is in use (U-Boot proper, which |
| 320 | is node, SPL and TPL) |
| 321 | """ |
| 322 | yield node |
Simon Glass | cb8bebb | 2021-03-18 20:25:01 +1300 | [diff] [blame] | 323 | for entry_type, (dtb, fname) in output_fdt_info.items(): |
Simon Glass | 6ed45ba | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 324 | if dtb != node.GetFdt(): |
Simon Glass | cb8bebb | 2021-03-18 20:25:01 +1300 | [diff] [blame] | 325 | if for_repack and entry_type != 'u-boot-dtb': |
Simon Glass | 12bb1a9 | 2019-07-20 12:23:51 -0600 | [diff] [blame] | 326 | continue |
Simon Glass | 10f9d00 | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 327 | other_node = dtb.GetNode(fdt_path_prefix + node.path) |
Simon Glass | 6ed45ba | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 328 | if other_node: |
| 329 | yield other_node |
Simon Glass | f46621d | 2018-09-14 04:57:21 -0600 | [diff] [blame] | 330 | |
Simon Glass | 12bb1a9 | 2019-07-20 12:23:51 -0600 | [diff] [blame] | 331 | def AddZeroProp(node, prop, for_repack=False): |
Simon Glass | f46621d | 2018-09-14 04:57:21 -0600 | [diff] [blame] | 332 | """Add a new property to affected device trees with an integer value of 0. |
| 333 | |
| 334 | Args: |
| 335 | prop_name: Name of property |
Simon Glass | 12bb1a9 | 2019-07-20 12:23:51 -0600 | [diff] [blame] | 336 | for_repack: True is this property is only needed for repacking |
Simon Glass | f46621d | 2018-09-14 04:57:21 -0600 | [diff] [blame] | 337 | """ |
Simon Glass | 12bb1a9 | 2019-07-20 12:23:51 -0600 | [diff] [blame] | 338 | for n in GetUpdateNodes(node, for_repack): |
Simon Glass | f46621d | 2018-09-14 04:57:21 -0600 | [diff] [blame] | 339 | n.AddZeroProp(prop) |
| 340 | |
Simon Glass | 0a98b28 | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 341 | def AddSubnode(node, name): |
| 342 | """Add a new subnode to a node in affected device trees |
| 343 | |
| 344 | Args: |
| 345 | node: Node to add to |
| 346 | name: name of node to add |
| 347 | |
| 348 | Returns: |
| 349 | New subnode that was created in main tree |
| 350 | """ |
| 351 | first = None |
| 352 | for n in GetUpdateNodes(node): |
| 353 | subnode = n.AddSubnode(name) |
| 354 | if not first: |
| 355 | first = subnode |
| 356 | return first |
| 357 | |
| 358 | def AddString(node, prop, value): |
| 359 | """Add a new string property to affected device trees |
| 360 | |
| 361 | Args: |
| 362 | prop_name: Name of property |
| 363 | value: String value (which will be \0-terminated in the DT) |
| 364 | """ |
| 365 | for n in GetUpdateNodes(node): |
| 366 | n.AddString(prop, value) |
| 367 | |
Simon Glass | 6eb9932 | 2021-01-06 21:35:18 -0700 | [diff] [blame] | 368 | def AddInt(node, prop, value): |
| 369 | """Add a new string property to affected device trees |
| 370 | |
| 371 | Args: |
| 372 | prop_name: Name of property |
| 373 | val: Integer value of property |
| 374 | """ |
| 375 | for n in GetUpdateNodes(node): |
| 376 | n.AddInt(prop, value) |
| 377 | |
Simon Glass | 12bb1a9 | 2019-07-20 12:23:51 -0600 | [diff] [blame] | 378 | def SetInt(node, prop, value, for_repack=False): |
Simon Glass | f46621d | 2018-09-14 04:57:21 -0600 | [diff] [blame] | 379 | """Update an integer property in affected device trees with an integer value |
| 380 | |
| 381 | This is not allowed to change the size of the FDT. |
| 382 | |
| 383 | Args: |
| 384 | prop_name: Name of property |
Simon Glass | 12bb1a9 | 2019-07-20 12:23:51 -0600 | [diff] [blame] | 385 | for_repack: True is this property is only needed for repacking |
Simon Glass | f46621d | 2018-09-14 04:57:21 -0600 | [diff] [blame] | 386 | """ |
Simon Glass | 12bb1a9 | 2019-07-20 12:23:51 -0600 | [diff] [blame] | 387 | for n in GetUpdateNodes(node, for_repack): |
Simon Glass | 24142ea | 2023-07-18 07:24:09 -0600 | [diff] [blame] | 388 | tout.debug("File %s: Update node '%s' prop '%s' to %#x" % |
| 389 | (n.GetFdt().name, n.path, prop, value)) |
Simon Glass | f46621d | 2018-09-14 04:57:21 -0600 | [diff] [blame] | 390 | n.SetInt(prop, value) |
Simon Glass | e0e5df9 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 391 | |
| 392 | def CheckAddHashProp(node): |
| 393 | hash_node = node.FindNode('hash') |
| 394 | if hash_node: |
| 395 | algo = hash_node.props.get('algo') |
| 396 | if not algo: |
| 397 | return "Missing 'algo' property for hash node" |
| 398 | if algo.value == 'sha256': |
| 399 | size = 32 |
| 400 | else: |
Simon Glass | 8db1f99 | 2022-02-08 10:59:44 -0700 | [diff] [blame] | 401 | return "Unknown hash algorithm '%s'" % algo.value |
Simon Glass | e0e5df9 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 402 | for n in GetUpdateNodes(hash_node): |
| 403 | n.AddEmptyProp('value', size) |
| 404 | |
| 405 | def CheckSetHashValue(node, get_data_func): |
| 406 | hash_node = node.FindNode('hash') |
| 407 | if hash_node: |
| 408 | algo = hash_node.props.get('algo').value |
| 409 | if algo == 'sha256': |
| 410 | m = hashlib.sha256() |
| 411 | m.update(get_data_func()) |
| 412 | data = m.digest() |
| 413 | for n in GetUpdateNodes(hash_node): |
| 414 | n.SetData('value', data) |
Simon Glass | bf6906b | 2019-07-08 14:25:36 -0600 | [diff] [blame] | 415 | |
| 416 | def SetAllowEntryExpansion(allow): |
| 417 | """Set whether post-pack expansion of entries is allowed |
| 418 | |
| 419 | Args: |
| 420 | allow: True to allow expansion, False to raise an exception |
| 421 | """ |
| 422 | global allow_entry_expansion |
| 423 | |
| 424 | allow_entry_expansion = allow |
| 425 | |
| 426 | def AllowEntryExpansion(): |
| 427 | """Check whether post-pack expansion of entries is allowed |
| 428 | |
| 429 | Returns: |
| 430 | True if expansion should be allowed, False if an exception should be |
| 431 | raised |
| 432 | """ |
| 433 | return allow_entry_expansion |
Simon Glass | 61ec04f | 2019-07-20 12:23:58 -0600 | [diff] [blame] | 434 | |
| 435 | def SetAllowEntryContraction(allow): |
| 436 | """Set whether post-pack contraction of entries is allowed |
| 437 | |
| 438 | Args: |
| 439 | allow: True to allow contraction, False to raise an exception |
| 440 | """ |
| 441 | global allow_entry_contraction |
| 442 | |
| 443 | allow_entry_contraction = allow |
| 444 | |
| 445 | def AllowEntryContraction(): |
| 446 | """Check whether post-pack contraction of entries is allowed |
| 447 | |
| 448 | Returns: |
| 449 | True if contraction should be allowed, False if an exception should be |
| 450 | raised |
| 451 | """ |
| 452 | return allow_entry_contraction |
Simon Glass | c69d19c | 2021-07-06 10:36:37 -0600 | [diff] [blame] | 453 | |
| 454 | def SetThreads(threads): |
| 455 | """Set the number of threads to use when building sections |
| 456 | |
| 457 | Args: |
| 458 | threads: Number of threads to use (None for default, 0 for |
| 459 | single-threaded) |
| 460 | """ |
| 461 | global num_threads |
| 462 | |
| 463 | num_threads = threads |
| 464 | |
| 465 | def GetThreads(): |
| 466 | """Get the number of threads to use when building sections |
| 467 | |
| 468 | Returns: |
| 469 | Number of threads to use (None for default, 0 for single-threaded) |
| 470 | """ |
| 471 | return num_threads |
Simon Glass | 03ebc20 | 2021-07-06 10:36:41 -0600 | [diff] [blame] | 472 | |
| 473 | def GetTiming(name): |
| 474 | """Get the timing info for a particular operation |
| 475 | |
| 476 | The object is created if it does not already exist. |
| 477 | |
| 478 | Args: |
| 479 | name: Operation name to get |
| 480 | |
| 481 | Returns: |
| 482 | Timing object for the current thread |
| 483 | """ |
| 484 | threaded_name = '%s:%d' % (name, threading.get_ident()) |
| 485 | timing = timing_info.get(threaded_name) |
| 486 | if not timing: |
| 487 | timing = Timing(threaded_name) |
| 488 | timing_info[threaded_name] = timing |
| 489 | return timing |
| 490 | |
| 491 | def TimingStart(name): |
| 492 | """Start the timer for an operation |
| 493 | |
| 494 | Args: |
| 495 | name: Operation name to start |
| 496 | """ |
| 497 | timing = GetTiming(name) |
| 498 | timing.start = time.monotonic() |
| 499 | |
| 500 | def TimingAccum(name): |
| 501 | """Stop and accumlate the time for an operation |
| 502 | |
| 503 | This measures the time since the last TimingStart() and adds that to the |
| 504 | accumulated time. |
| 505 | |
| 506 | Args: |
| 507 | name: Operation name to start |
| 508 | """ |
| 509 | timing = GetTiming(name) |
| 510 | timing.accum += time.monotonic() - timing.start |
| 511 | |
| 512 | def TimingShow(): |
| 513 | """Show all timing information""" |
| 514 | duration = defaultdict(float) |
| 515 | for threaded_name, timing in timing_info.items(): |
| 516 | name = threaded_name.split(':')[0] |
| 517 | duration[name] += timing.accum |
| 518 | |
| 519 | for name, seconds in duration.items(): |
| 520 | print('%10s: %10.1fms' % (name, seconds * 1000)) |
Simon Glass | c475dec | 2021-11-23 11:03:42 -0700 | [diff] [blame] | 521 | |
| 522 | def GetVersion(path=OUR_PATH): |
| 523 | """Get the version string for binman |
| 524 | |
| 525 | Args: |
| 526 | path: Path to 'version' file |
| 527 | |
| 528 | Returns: |
| 529 | str: String version, e.g. 'v2021.10' |
| 530 | """ |
| 531 | version_fname = os.path.join(path, 'version') |
| 532 | if os.path.exists(version_fname): |
Simon Glass | c1aa66e | 2022-01-29 14:14:04 -0700 | [diff] [blame] | 533 | version = tools.read_file(version_fname, binary=False) |
Simon Glass | c475dec | 2021-11-23 11:03:42 -0700 | [diff] [blame] | 534 | else: |
| 535 | version = '(unreleased)' |
| 536 | return version |