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 | |
Simon Glass | ae7cf03 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 8 | import hashlib |
Simon Glass | 29aa736 | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 9 | import re |
Simon Glass | 29aa736 | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 10 | |
Simon Glass | c585dd4 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 11 | from dtoc import fdt |
Simon Glass | 29aa736 | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 12 | import os |
Simon Glass | a997ea5 | 2020-04-17 18:09:04 -0600 | [diff] [blame] | 13 | from patman import tools |
| 14 | from patman import tout |
Simon Glass | 29aa736 | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 15 | |
Simon Glass | f184d95 | 2021-03-18 20:25:00 +1300 | [diff] [blame] | 16 | # Map an dtb etype to its expected filename |
| 17 | DTB_TYPE_FNAME = { |
| 18 | 'u-boot-spl-dtb': 'spl/u-boot-spl.dtb', |
| 19 | 'u-boot-tpl-dtb': 'tpl/u-boot-tpl.dtb', |
| 20 | } |
| 21 | |
Simon Glass | 2be381d | 2019-07-20 12:23:32 -0600 | [diff] [blame] | 22 | # Records the device-tree files known to binman, keyed by entry type (e.g. |
| 23 | # 'u-boot-spl-dtb'). These are the output FDT files, which can be updated by |
| 24 | # binman. They have been copied to <xxx>.out files. |
| 25 | # |
Simon Glass | a1ba618 | 2021-03-18 20:25:01 +1300 | [diff] [blame^] | 26 | # key: entry type (e.g. 'u-boot-dtb) |
Simon Glass | 2be381d | 2019-07-20 12:23:32 -0600 | [diff] [blame] | 27 | # value: tuple: |
| 28 | # Fdt object |
| 29 | # Filename |
Simon Glass | 95f1a94 | 2019-07-20 12:23:43 -0600 | [diff] [blame] | 30 | output_fdt_info = {} |
Simon Glass | 29aa736 | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 31 | |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 32 | # Prefix to add to an fdtmap path to turn it into a path to the /binman node |
| 33 | fdt_path_prefix = '' |
| 34 | |
Simon Glass | 29aa736 | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 35 | # Arguments passed to binman to provide arguments to entries |
| 36 | entry_args = {} |
| 37 | |
Simon Glass | 0c9d5b5 | 2018-09-14 04:57:22 -0600 | [diff] [blame] | 38 | # True to use fake device-tree files for testing (see U_BOOT_DTB_DATA in |
| 39 | # ftest.py) |
Simon Glass | 3140201 | 2018-09-14 04:57:23 -0600 | [diff] [blame] | 40 | use_fake_dtb = False |
Simon Glass | 0c9d5b5 | 2018-09-14 04:57:22 -0600 | [diff] [blame] | 41 | |
Simon Glass | bdb4031 | 2018-09-14 04:57:20 -0600 | [diff] [blame] | 42 | # The DTB which contains the full image information |
| 43 | main_dtb = None |
| 44 | |
Simon Glass | cbc80e8 | 2019-07-08 14:25:36 -0600 | [diff] [blame] | 45 | # Allow entries to expand after they have been packed. This is detected and |
| 46 | # forces a re-pack. If not allowed, any attempted expansion causes an error in |
| 47 | # Entry.ProcessContentsUpdate() |
| 48 | allow_entry_expansion = True |
| 49 | |
Simon Glass | 9d8ee32 | 2019-07-20 12:23:58 -0600 | [diff] [blame] | 50 | # Don't allow entries to contract after they have been packed. Instead just |
| 51 | # leave some wasted space. If allowed, this is detected and forces a re-pack, |
| 52 | # but may result in entries that oscillate in size, thus causing a pack error. |
| 53 | # An example is a compressed device tree where the original offset values |
| 54 | # result in a larger compressed size than the new ones, but then after updating |
| 55 | # to the new ones, the compressed size increases, etc. |
| 56 | allow_entry_contraction = False |
| 57 | |
Simon Glass | 2be381d | 2019-07-20 12:23:32 -0600 | [diff] [blame] | 58 | def GetFdtForEtype(etype): |
| 59 | """Get the Fdt object for a particular device-tree entry |
Simon Glass | 29aa736 | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 60 | |
| 61 | Binman keeps track of at least one device-tree file called u-boot.dtb but |
| 62 | can also have others (e.g. for SPL). This function looks up the given |
Simon Glass | 2be381d | 2019-07-20 12:23:32 -0600 | [diff] [blame] | 63 | entry and returns the associated Fdt object. |
Simon Glass | 29aa736 | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 64 | |
| 65 | Args: |
Simon Glass | 2be381d | 2019-07-20 12:23:32 -0600 | [diff] [blame] | 66 | etype: Entry type of device tree (e.g. 'u-boot-dtb') |
Simon Glass | 29aa736 | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 67 | |
| 68 | Returns: |
Simon Glass | 2be381d | 2019-07-20 12:23:32 -0600 | [diff] [blame] | 69 | Fdt object associated with the entry type |
Simon Glass | 29aa736 | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 70 | """ |
Simon Glass | 95f1a94 | 2019-07-20 12:23:43 -0600 | [diff] [blame] | 71 | value = output_fdt_info.get(etype); |
Simon Glass | 385138a | 2019-07-20 12:23:42 -0600 | [diff] [blame] | 72 | if not value: |
| 73 | return None |
| 74 | return value[0] |
Simon Glass | 29aa736 | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 75 | |
Simon Glass | 2be381d | 2019-07-20 12:23:32 -0600 | [diff] [blame] | 76 | def GetFdtPath(etype): |
Simon Glass | 29aa736 | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 77 | """Get the full pathname of a particular Fdt object |
| 78 | |
Simon Glass | a3030a5 | 2019-07-20 12:23:30 -0600 | [diff] [blame] | 79 | Similar to GetFdtForEtype() but returns the pathname associated with the |
| 80 | Fdt. |
Simon Glass | 29aa736 | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 81 | |
| 82 | Args: |
Simon Glass | 2be381d | 2019-07-20 12:23:32 -0600 | [diff] [blame] | 83 | etype: Entry type of device tree (e.g. 'u-boot-dtb') |
Simon Glass | 29aa736 | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 84 | |
| 85 | Returns: |
| 86 | Full path name to the associated Fdt |
| 87 | """ |
Simon Glass | 95f1a94 | 2019-07-20 12:23:43 -0600 | [diff] [blame] | 88 | return output_fdt_info[etype][0]._fname |
Simon Glass | 29aa736 | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 89 | |
Simon Glass | 2be381d | 2019-07-20 12:23:32 -0600 | [diff] [blame] | 90 | def GetFdtContents(etype='u-boot-dtb'): |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 91 | """Looks up the FDT pathname and contents |
| 92 | |
| 93 | This is used to obtain the Fdt pathname and contents when needed by an |
| 94 | entry. It supports a 'fake' dtb, allowing tests to substitute test data for |
| 95 | the real dtb. |
| 96 | |
| 97 | Args: |
Simon Glass | 2be381d | 2019-07-20 12:23:32 -0600 | [diff] [blame] | 98 | etype: Entry type to look up (e.g. 'u-boot.dtb'). |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 99 | |
| 100 | Returns: |
| 101 | tuple: |
| 102 | pathname to Fdt |
| 103 | Fdt data (as bytes) |
| 104 | """ |
Simon Glass | 95f1a94 | 2019-07-20 12:23:43 -0600 | [diff] [blame] | 105 | if etype not in output_fdt_info: |
Simon Glass | 385138a | 2019-07-20 12:23:42 -0600 | [diff] [blame] | 106 | return None, None |
| 107 | if not use_fake_dtb: |
Simon Glass | 2be381d | 2019-07-20 12:23:32 -0600 | [diff] [blame] | 108 | pathname = GetFdtPath(etype) |
| 109 | data = GetFdtForEtype(etype).GetContents() |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 110 | else: |
Simon Glass | 95f1a94 | 2019-07-20 12:23:43 -0600 | [diff] [blame] | 111 | fname = output_fdt_info[etype][1] |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 112 | pathname = tools.GetInputFilename(fname) |
| 113 | data = tools.ReadFile(pathname) |
| 114 | return pathname, data |
| 115 | |
Simon Glass | 74f5feb | 2019-07-20 12:24:08 -0600 | [diff] [blame] | 116 | def UpdateFdtContents(etype, data): |
| 117 | """Update the contents of a particular device tree |
| 118 | |
| 119 | The device tree is updated and written back to its file. This affects what |
| 120 | is returned from future called to GetFdtContents(), etc. |
| 121 | |
| 122 | Args: |
| 123 | etype: Entry type (e.g. 'u-boot-dtb') |
| 124 | data: Data to replace the DTB with |
| 125 | """ |
Simon Glass | a1ba618 | 2021-03-18 20:25:01 +1300 | [diff] [blame^] | 126 | dtb, fname = output_fdt_info[etype] |
Simon Glass | 74f5feb | 2019-07-20 12:24:08 -0600 | [diff] [blame] | 127 | dtb_fname = dtb.GetFilename() |
| 128 | tools.WriteFile(dtb_fname, data) |
| 129 | dtb = fdt.FdtScan(dtb_fname) |
Simon Glass | a1ba618 | 2021-03-18 20:25:01 +1300 | [diff] [blame^] | 130 | output_fdt_info[etype] = [dtb, fname] |
Simon Glass | 74f5feb | 2019-07-20 12:24:08 -0600 | [diff] [blame] | 131 | |
Simon Glass | 29aa736 | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 132 | def SetEntryArgs(args): |
| 133 | """Set the value of the entry args |
| 134 | |
| 135 | This sets up the entry_args dict which is used to supply entry arguments to |
| 136 | entries. |
| 137 | |
| 138 | Args: |
| 139 | args: List of entry arguments, each in the format "name=value" |
| 140 | """ |
| 141 | global entry_args |
| 142 | |
| 143 | entry_args = {} |
| 144 | if args: |
| 145 | for arg in args: |
| 146 | m = re.match('([^=]*)=(.*)', arg) |
| 147 | if not m: |
| 148 | raise ValueError("Invalid entry arguemnt '%s'" % arg) |
| 149 | entry_args[m.group(1)] = m.group(2) |
| 150 | |
| 151 | def GetEntryArg(name): |
| 152 | """Get the value of an entry argument |
| 153 | |
| 154 | Args: |
| 155 | name: Name of argument to retrieve |
| 156 | |
| 157 | Returns: |
| 158 | String value of argument |
| 159 | """ |
| 160 | return entry_args.get(name) |
Simon Glass | bdb4031 | 2018-09-14 04:57:20 -0600 | [diff] [blame] | 161 | |
Simon Glass | 0c9d5b5 | 2018-09-14 04:57:22 -0600 | [diff] [blame] | 162 | def Prepare(images, dtb): |
Simon Glass | bdb4031 | 2018-09-14 04:57:20 -0600 | [diff] [blame] | 163 | """Get device tree files ready for use |
| 164 | |
Simon Glass | 5a30060 | 2019-07-20 12:23:29 -0600 | [diff] [blame] | 165 | This sets up a set of device tree files that can be retrieved by |
| 166 | GetAllFdts(). This includes U-Boot proper and any SPL device trees. |
Simon Glass | bdb4031 | 2018-09-14 04:57:20 -0600 | [diff] [blame] | 167 | |
| 168 | Args: |
Simon Glass | 0c9d5b5 | 2018-09-14 04:57:22 -0600 | [diff] [blame] | 169 | images: List of images being used |
Simon Glass | bdb4031 | 2018-09-14 04:57:20 -0600 | [diff] [blame] | 170 | dtb: Main dtb |
| 171 | """ |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 172 | global output_fdt_info, main_dtb, fdt_path_prefix |
Simon Glass | bdb4031 | 2018-09-14 04:57:20 -0600 | [diff] [blame] | 173 | # Import these here in case libfdt.py is not available, in which case |
| 174 | # the above help option still works. |
Simon Glass | c585dd4 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 175 | from dtoc import fdt |
| 176 | from dtoc import fdt_util |
Simon Glass | bdb4031 | 2018-09-14 04:57:20 -0600 | [diff] [blame] | 177 | |
| 178 | # If we are updating the DTBs we need to put these updated versions |
| 179 | # where Entry_blob_dtb can find them. We can ignore 'u-boot.dtb' |
| 180 | # since it is assumed to be the one passed in with options.dt, and |
| 181 | # was handled just above. |
| 182 | main_dtb = dtb |
Simon Glass | 95f1a94 | 2019-07-20 12:23:43 -0600 | [diff] [blame] | 183 | output_fdt_info.clear() |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 184 | fdt_path_prefix = '' |
Simon Glass | a1ba618 | 2021-03-18 20:25:01 +1300 | [diff] [blame^] | 185 | output_fdt_info['u-boot-dtb'] = [dtb, 'u-boot.dtb'] |
Simon Glass | f184d95 | 2021-03-18 20:25:00 +1300 | [diff] [blame] | 186 | if use_fake_dtb: |
| 187 | for etype, fname in DTB_TYPE_FNAME.items(): |
Simon Glass | a1ba618 | 2021-03-18 20:25:01 +1300 | [diff] [blame^] | 188 | output_fdt_info[etype] = [dtb, fname] |
Simon Glass | f184d95 | 2021-03-18 20:25:00 +1300 | [diff] [blame] | 189 | else: |
Simon Glass | f3aba91 | 2019-07-20 12:23:34 -0600 | [diff] [blame] | 190 | fdt_set = {} |
Simon Glass | 0c9d5b5 | 2018-09-14 04:57:22 -0600 | [diff] [blame] | 191 | for image in images.values(): |
Simon Glass | 132fa72 | 2019-07-20 12:23:33 -0600 | [diff] [blame] | 192 | fdt_set.update(image.GetFdts()) |
| 193 | for etype, other in fdt_set.items(): |
Simon Glass | f184d95 | 2021-03-18 20:25:00 +1300 | [diff] [blame] | 194 | entry, fname = other |
| 195 | infile = tools.GetInputFilename(fname) |
| 196 | fname_dtb = fdt_util.EnsureCompiled(infile) |
Simon Glass | 0c9d5b5 | 2018-09-14 04:57:22 -0600 | [diff] [blame] | 197 | out_fname = tools.GetOutputFilename('%s.out' % |
Simon Glass | f184d95 | 2021-03-18 20:25:00 +1300 | [diff] [blame] | 198 | os.path.split(fname)[1]) |
| 199 | tools.WriteFile(out_fname, tools.ReadFile(fname_dtb)) |
Simon Glass | 0c9d5b5 | 2018-09-14 04:57:22 -0600 | [diff] [blame] | 200 | other_dtb = fdt.FdtScan(out_fname) |
Simon Glass | a1ba618 | 2021-03-18 20:25:01 +1300 | [diff] [blame^] | 201 | output_fdt_info[etype] = [other_dtb, out_fname] |
Simon Glass | bdb4031 | 2018-09-14 04:57:20 -0600 | [diff] [blame] | 202 | |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 203 | def PrepareFromLoadedData(image): |
| 204 | """Get device tree files ready for use with a loaded image |
| 205 | |
| 206 | Loaded images are different from images that are being created by binman, |
| 207 | since there is generally already an fdtmap and we read the description from |
| 208 | that. This provides the position and size of every entry in the image with |
| 209 | no calculation required. |
| 210 | |
| 211 | This function uses the same output_fdt_info[] as Prepare(). It finds the |
| 212 | device tree files, adds a reference to the fdtmap and sets the FDT path |
| 213 | prefix to translate from the fdtmap (where the root node is the image node) |
| 214 | to the normal device tree (where the image node is under a /binman node). |
| 215 | |
| 216 | Args: |
| 217 | images: List of images being used |
| 218 | """ |
| 219 | global output_fdt_info, main_dtb, fdt_path_prefix |
| 220 | |
| 221 | tout.Info('Preparing device trees') |
| 222 | output_fdt_info.clear() |
| 223 | fdt_path_prefix = '' |
Simon Glass | a1ba618 | 2021-03-18 20:25:01 +1300 | [diff] [blame^] | 224 | output_fdt_info['fdtmap'] = [image.fdtmap_dtb, 'u-boot.dtb'] |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 225 | main_dtb = None |
| 226 | tout.Info(" Found device tree type 'fdtmap' '%s'" % image.fdtmap_dtb.name) |
| 227 | for etype, value in image.GetFdts().items(): |
| 228 | entry, fname = value |
| 229 | out_fname = tools.GetOutputFilename('%s.dtb' % entry.etype) |
| 230 | tout.Info(" Found device tree type '%s' at '%s' path '%s'" % |
| 231 | (etype, out_fname, entry.GetPath())) |
| 232 | entry._filename = entry.GetDefaultFilename() |
| 233 | data = entry.ReadData() |
| 234 | |
| 235 | tools.WriteFile(out_fname, data) |
| 236 | dtb = fdt.Fdt(out_fname) |
| 237 | dtb.Scan() |
| 238 | image_node = dtb.GetNode('/binman') |
| 239 | if 'multiple-images' in image_node.props: |
| 240 | image_node = dtb.GetNode('/binman/%s' % image.image_node) |
| 241 | fdt_path_prefix = image_node.path |
Simon Glass | a1ba618 | 2021-03-18 20:25:01 +1300 | [diff] [blame^] | 242 | output_fdt_info[etype] = [dtb, None] |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 243 | tout.Info(" FDT path prefix '%s'" % fdt_path_prefix) |
| 244 | |
| 245 | |
Simon Glass | 5a30060 | 2019-07-20 12:23:29 -0600 | [diff] [blame] | 246 | def GetAllFdts(): |
Simon Glass | bdb4031 | 2018-09-14 04:57:20 -0600 | [diff] [blame] | 247 | """Yield all device tree files being used by binman |
| 248 | |
| 249 | Yields: |
| 250 | Device trees being used (U-Boot proper, SPL, TPL) |
| 251 | """ |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 252 | if main_dtb: |
| 253 | yield main_dtb |
Simon Glass | 95f1a94 | 2019-07-20 12:23:43 -0600 | [diff] [blame] | 254 | for etype in output_fdt_info: |
| 255 | dtb = output_fdt_info[etype][0] |
Simon Glass | 132fa72 | 2019-07-20 12:23:33 -0600 | [diff] [blame] | 256 | if dtb != main_dtb: |
| 257 | yield dtb |
Simon Glass | bdb4031 | 2018-09-14 04:57:20 -0600 | [diff] [blame] | 258 | |
Simon Glass | fb30e29 | 2019-07-20 12:23:51 -0600 | [diff] [blame] | 259 | def GetUpdateNodes(node, for_repack=False): |
Simon Glass | c8135dc | 2018-09-14 04:57:21 -0600 | [diff] [blame] | 260 | """Yield all the nodes that need to be updated in all device trees |
| 261 | |
| 262 | The property referenced by this node is added to any device trees which |
| 263 | have the given node. Due to removable of unwanted notes, SPL and TPL may |
| 264 | not have this node. |
| 265 | |
| 266 | Args: |
| 267 | node: Node object in the main device tree to look up |
Simon Glass | fb30e29 | 2019-07-20 12:23:51 -0600 | [diff] [blame] | 268 | for_repack: True if we want only nodes which need 'repack' properties |
| 269 | added to them (e.g. 'orig-offset'), False to return all nodes. We |
| 270 | don't add repack properties to SPL/TPL device trees. |
Simon Glass | c8135dc | 2018-09-14 04:57:21 -0600 | [diff] [blame] | 271 | |
| 272 | Yields: |
| 273 | Node objects in each device tree that is in use (U-Boot proper, which |
| 274 | is node, SPL and TPL) |
| 275 | """ |
| 276 | yield node |
Simon Glass | a1ba618 | 2021-03-18 20:25:01 +1300 | [diff] [blame^] | 277 | for entry_type, (dtb, fname) in output_fdt_info.items(): |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 278 | if dtb != node.GetFdt(): |
Simon Glass | a1ba618 | 2021-03-18 20:25:01 +1300 | [diff] [blame^] | 279 | if for_repack and entry_type != 'u-boot-dtb': |
Simon Glass | fb30e29 | 2019-07-20 12:23:51 -0600 | [diff] [blame] | 280 | continue |
Simon Glass | 072959a | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 281 | other_node = dtb.GetNode(fdt_path_prefix + node.path) |
Simon Glass | e219aa4 | 2018-09-14 04:57:24 -0600 | [diff] [blame] | 282 | if other_node: |
| 283 | yield other_node |
Simon Glass | c8135dc | 2018-09-14 04:57:21 -0600 | [diff] [blame] | 284 | |
Simon Glass | fb30e29 | 2019-07-20 12:23:51 -0600 | [diff] [blame] | 285 | def AddZeroProp(node, prop, for_repack=False): |
Simon Glass | c8135dc | 2018-09-14 04:57:21 -0600 | [diff] [blame] | 286 | """Add a new property to affected device trees with an integer value of 0. |
| 287 | |
| 288 | Args: |
| 289 | prop_name: Name of property |
Simon Glass | fb30e29 | 2019-07-20 12:23:51 -0600 | [diff] [blame] | 290 | for_repack: True is this property is only needed for repacking |
Simon Glass | c8135dc | 2018-09-14 04:57:21 -0600 | [diff] [blame] | 291 | """ |
Simon Glass | fb30e29 | 2019-07-20 12:23:51 -0600 | [diff] [blame] | 292 | for n in GetUpdateNodes(node, for_repack): |
Simon Glass | c8135dc | 2018-09-14 04:57:21 -0600 | [diff] [blame] | 293 | n.AddZeroProp(prop) |
| 294 | |
Simon Glass | ac6328c | 2018-09-14 04:57:28 -0600 | [diff] [blame] | 295 | def AddSubnode(node, name): |
| 296 | """Add a new subnode to a node in affected device trees |
| 297 | |
| 298 | Args: |
| 299 | node: Node to add to |
| 300 | name: name of node to add |
| 301 | |
| 302 | Returns: |
| 303 | New subnode that was created in main tree |
| 304 | """ |
| 305 | first = None |
| 306 | for n in GetUpdateNodes(node): |
| 307 | subnode = n.AddSubnode(name) |
| 308 | if not first: |
| 309 | first = subnode |
| 310 | return first |
| 311 | |
| 312 | def AddString(node, prop, value): |
| 313 | """Add a new string property to affected device trees |
| 314 | |
| 315 | Args: |
| 316 | prop_name: Name of property |
| 317 | value: String value (which will be \0-terminated in the DT) |
| 318 | """ |
| 319 | for n in GetUpdateNodes(node): |
| 320 | n.AddString(prop, value) |
| 321 | |
Simon Glass | a2af730 | 2021-01-06 21:35:18 -0700 | [diff] [blame] | 322 | def AddInt(node, prop, value): |
| 323 | """Add a new string property to affected device trees |
| 324 | |
| 325 | Args: |
| 326 | prop_name: Name of property |
| 327 | val: Integer value of property |
| 328 | """ |
| 329 | for n in GetUpdateNodes(node): |
| 330 | n.AddInt(prop, value) |
| 331 | |
Simon Glass | fb30e29 | 2019-07-20 12:23:51 -0600 | [diff] [blame] | 332 | def SetInt(node, prop, value, for_repack=False): |
Simon Glass | c8135dc | 2018-09-14 04:57:21 -0600 | [diff] [blame] | 333 | """Update an integer property in affected device trees with an integer value |
| 334 | |
| 335 | This is not allowed to change the size of the FDT. |
| 336 | |
| 337 | Args: |
| 338 | prop_name: Name of property |
Simon Glass | fb30e29 | 2019-07-20 12:23:51 -0600 | [diff] [blame] | 339 | for_repack: True is this property is only needed for repacking |
Simon Glass | c8135dc | 2018-09-14 04:57:21 -0600 | [diff] [blame] | 340 | """ |
Simon Glass | fb30e29 | 2019-07-20 12:23:51 -0600 | [diff] [blame] | 341 | for n in GetUpdateNodes(node, for_repack): |
| 342 | tout.Detail("File %s: Update node '%s' prop '%s' to %#x" % |
Simon Glass | f8a54bc | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 343 | (n.GetFdt().name, n.path, prop, value)) |
Simon Glass | c8135dc | 2018-09-14 04:57:21 -0600 | [diff] [blame] | 344 | n.SetInt(prop, value) |
Simon Glass | ae7cf03 | 2018-09-14 04:57:31 -0600 | [diff] [blame] | 345 | |
| 346 | def CheckAddHashProp(node): |
| 347 | hash_node = node.FindNode('hash') |
| 348 | if hash_node: |
| 349 | algo = hash_node.props.get('algo') |
| 350 | if not algo: |
| 351 | return "Missing 'algo' property for hash node" |
| 352 | if algo.value == 'sha256': |
| 353 | size = 32 |
| 354 | else: |
| 355 | return "Unknown hash algorithm '%s'" % algo |
| 356 | for n in GetUpdateNodes(hash_node): |
| 357 | n.AddEmptyProp('value', size) |
| 358 | |
| 359 | def CheckSetHashValue(node, get_data_func): |
| 360 | hash_node = node.FindNode('hash') |
| 361 | if hash_node: |
| 362 | algo = hash_node.props.get('algo').value |
| 363 | if algo == 'sha256': |
| 364 | m = hashlib.sha256() |
| 365 | m.update(get_data_func()) |
| 366 | data = m.digest() |
| 367 | for n in GetUpdateNodes(hash_node): |
| 368 | n.SetData('value', data) |
Simon Glass | cbc80e8 | 2019-07-08 14:25:36 -0600 | [diff] [blame] | 369 | |
| 370 | def SetAllowEntryExpansion(allow): |
| 371 | """Set whether post-pack expansion of entries is allowed |
| 372 | |
| 373 | Args: |
| 374 | allow: True to allow expansion, False to raise an exception |
| 375 | """ |
| 376 | global allow_entry_expansion |
| 377 | |
| 378 | allow_entry_expansion = allow |
| 379 | |
| 380 | def AllowEntryExpansion(): |
| 381 | """Check whether post-pack expansion of entries is allowed |
| 382 | |
| 383 | Returns: |
| 384 | True if expansion should be allowed, False if an exception should be |
| 385 | raised |
| 386 | """ |
| 387 | return allow_entry_expansion |
Simon Glass | 9d8ee32 | 2019-07-20 12:23:58 -0600 | [diff] [blame] | 388 | |
| 389 | def SetAllowEntryContraction(allow): |
| 390 | """Set whether post-pack contraction of entries is allowed |
| 391 | |
| 392 | Args: |
| 393 | allow: True to allow contraction, False to raise an exception |
| 394 | """ |
| 395 | global allow_entry_contraction |
| 396 | |
| 397 | allow_entry_contraction = allow |
| 398 | |
| 399 | def AllowEntryContraction(): |
| 400 | """Check whether post-pack contraction of entries is allowed |
| 401 | |
| 402 | Returns: |
| 403 | True if contraction should be allowed, False if an exception should be |
| 404 | raised |
| 405 | """ |
| 406 | return allow_entry_contraction |