Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | bf7fd50 | 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 | bf7fd50 | 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 |
Simon Glass | bf77667 | 2020-04-17 18:09:04 -0600 | [diff] [blame] | 11 | from patman import tools |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 12 | |
Simon Glass | 1628793 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 13 | from binman import cbfs_util |
| 14 | from binman import elf |
Simon Glass | bf77667 | 2020-04-17 18:09:04 -0600 | [diff] [blame] | 15 | from patman import command |
| 16 | from patman import tout |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 17 | |
| 18 | # List of images we plan to create |
| 19 | # Make this global so that it can be referenced from tests |
| 20 | images = OrderedDict() |
| 21 | |
| 22 | def _ReadImageDesc(binman_node): |
| 23 | """Read the image descriptions from the /binman node |
| 24 | |
| 25 | This normally produces a single Image object called 'image'. But if |
| 26 | multiple images are present, they will all be returned. |
| 27 | |
| 28 | Args: |
| 29 | binman_node: Node object of the /binman node |
| 30 | Returns: |
| 31 | OrderedDict of Image objects, each of which describes an image |
| 32 | """ |
| 33 | images = OrderedDict() |
| 34 | if 'multiple-images' in binman_node.props: |
| 35 | for node in binman_node.subnodes: |
| 36 | images[node.name] = Image(node.name, node) |
| 37 | else: |
| 38 | images['image'] = Image('image', binman_node) |
| 39 | return images |
| 40 | |
Simon Glass | ec3f378 | 2017-05-27 07:38:29 -0600 | [diff] [blame] | 41 | def _FindBinmanNode(dtb): |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 42 | """Find the 'binman' node in the device tree |
| 43 | |
| 44 | Args: |
Simon Glass | ec3f378 | 2017-05-27 07:38:29 -0600 | [diff] [blame] | 45 | dtb: Fdt object to scan |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 46 | Returns: |
| 47 | Node object of /binman node, or None if not found |
| 48 | """ |
Simon Glass | ec3f378 | 2017-05-27 07:38:29 -0600 | [diff] [blame] | 49 | for node in dtb.GetRoot().subnodes: |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 50 | if node.name == 'binman': |
| 51 | return node |
| 52 | return None |
| 53 | |
Simon Glass | c55a50f | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 54 | def WriteEntryDocs(modules, test_missing=None): |
| 55 | """Write out documentation for all entries |
Simon Glass | ecab897 | 2018-07-06 10:27:40 -0600 | [diff] [blame] | 56 | |
| 57 | Args: |
Simon Glass | c55a50f | 2018-09-14 04:57:19 -0600 | [diff] [blame] | 58 | modules: List of Module objects to get docs for |
| 59 | test_missing: Used for testing only, to force an entry's documeentation |
| 60 | to show as missing even if it is present. Should be set to None in |
| 61 | normal use. |
Simon Glass | ecab897 | 2018-07-06 10:27:40 -0600 | [diff] [blame] | 62 | """ |
Simon Glass | 1628793 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 63 | from binman.entry import Entry |
Simon Glass | fd8d1f7 | 2018-07-17 13:25:36 -0600 | [diff] [blame] | 64 | Entry.WriteDocs(modules, test_missing) |
| 65 | |
Simon Glass | 61f564d | 2019-07-08 14:25:48 -0600 | [diff] [blame] | 66 | |
| 67 | def ListEntries(image_fname, entry_paths): |
| 68 | """List the entries in an image |
| 69 | |
| 70 | This decodes the supplied image and displays a table of entries from that |
| 71 | image, preceded by a header. |
| 72 | |
| 73 | Args: |
| 74 | image_fname: Image filename to process |
| 75 | entry_paths: List of wildcarded paths (e.g. ['*dtb*', 'u-boot*', |
| 76 | 'section/u-boot']) |
| 77 | """ |
| 78 | image = Image.FromFile(image_fname) |
| 79 | |
| 80 | entries, lines, widths = image.GetListEntries(entry_paths) |
| 81 | |
| 82 | num_columns = len(widths) |
| 83 | for linenum, line in enumerate(lines): |
| 84 | if linenum == 1: |
| 85 | # Print header line |
| 86 | print('-' * (sum(widths) + num_columns * 2)) |
| 87 | out = '' |
| 88 | for i, item in enumerate(line): |
| 89 | width = -widths[i] |
| 90 | if item.startswith('>'): |
| 91 | width = -width |
| 92 | item = item[1:] |
| 93 | txt = '%*s ' % (width, item) |
| 94 | out += txt |
| 95 | print(out.rstrip()) |
| 96 | |
Simon Glass | f667e45 | 2019-07-08 14:25:50 -0600 | [diff] [blame] | 97 | |
| 98 | def ReadEntry(image_fname, entry_path, decomp=True): |
| 99 | """Extract an entry from an image |
| 100 | |
| 101 | This extracts the data from a particular entry in an image |
| 102 | |
| 103 | Args: |
| 104 | image_fname: Image filename to process |
| 105 | entry_path: Path to entry to extract |
| 106 | decomp: True to return uncompressed data, if the data is compress |
| 107 | False to return the raw data |
| 108 | |
| 109 | Returns: |
| 110 | data extracted from the entry |
| 111 | """ |
Simon Glass | 8dbb744 | 2019-08-24 07:22:44 -0600 | [diff] [blame] | 112 | global Image |
| 113 | from image import Image |
| 114 | |
Simon Glass | f667e45 | 2019-07-08 14:25:50 -0600 | [diff] [blame] | 115 | image = Image.FromFile(image_fname) |
| 116 | entry = image.FindEntryPath(entry_path) |
| 117 | return entry.ReadData(decomp) |
| 118 | |
| 119 | |
Simon Glass | 71ce0ba | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 120 | def ExtractEntries(image_fname, output_fname, outdir, entry_paths, |
| 121 | decomp=True): |
| 122 | """Extract the data from one or more entries and write it to files |
| 123 | |
| 124 | Args: |
| 125 | image_fname: Image filename to process |
| 126 | output_fname: Single output filename to use if extracting one file, None |
| 127 | otherwise |
| 128 | outdir: Output directory to use (for any number of files), else None |
| 129 | entry_paths: List of entry paths to extract |
Simon Glass | 3ad804e | 2019-07-20 12:24:12 -0600 | [diff] [blame] | 130 | decomp: True to decompress the entry data |
Simon Glass | 71ce0ba | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 131 | |
| 132 | Returns: |
| 133 | List of EntryInfo records that were written |
| 134 | """ |
| 135 | image = Image.FromFile(image_fname) |
| 136 | |
| 137 | # Output an entry to a single file, as a special case |
| 138 | if output_fname: |
| 139 | if not entry_paths: |
Simon Glass | bb5edc1 | 2019-07-20 12:24:14 -0600 | [diff] [blame] | 140 | raise ValueError('Must specify an entry path to write with -f') |
Simon Glass | 71ce0ba | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 141 | if len(entry_paths) != 1: |
Simon Glass | bb5edc1 | 2019-07-20 12:24:14 -0600 | [diff] [blame] | 142 | raise ValueError('Must specify exactly one entry path to write with -f') |
Simon Glass | 71ce0ba | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 143 | entry = image.FindEntryPath(entry_paths[0]) |
| 144 | data = entry.ReadData(decomp) |
| 145 | tools.WriteFile(output_fname, data) |
| 146 | tout.Notice("Wrote %#x bytes to file '%s'" % (len(data), output_fname)) |
| 147 | return |
| 148 | |
| 149 | # Otherwise we will output to a path given by the entry path of each entry. |
| 150 | # This means that entries will appear in subdirectories if they are part of |
| 151 | # a sub-section. |
| 152 | einfos = image.GetListEntries(entry_paths)[0] |
| 153 | tout.Notice('%d entries match and will be written' % len(einfos)) |
| 154 | for einfo in einfos: |
| 155 | entry = einfo.entry |
| 156 | data = entry.ReadData(decomp) |
| 157 | path = entry.GetPath()[1:] |
| 158 | fname = os.path.join(outdir, path) |
| 159 | |
| 160 | # If this entry has children, create a directory for it and put its |
| 161 | # data in a file called 'root' in that directory |
| 162 | if entry.GetEntries(): |
| 163 | if not os.path.exists(fname): |
| 164 | os.makedirs(fname) |
| 165 | fname = os.path.join(fname, 'root') |
| 166 | tout.Notice("Write entry '%s' to '%s'" % (entry.GetPath(), fname)) |
| 167 | tools.WriteFile(fname, data) |
| 168 | return einfos |
| 169 | |
| 170 | |
Simon Glass | d7fa4e4 | 2019-07-20 12:24:13 -0600 | [diff] [blame] | 171 | def BeforeReplace(image, allow_resize): |
| 172 | """Handle getting an image ready for replacing entries in it |
| 173 | |
| 174 | Args: |
| 175 | image: Image to prepare |
| 176 | """ |
| 177 | state.PrepareFromLoadedData(image) |
| 178 | image.LoadData() |
| 179 | |
| 180 | # If repacking, drop the old offset/size values except for the original |
| 181 | # ones, so we are only left with the constraints. |
| 182 | if allow_resize: |
| 183 | image.ResetForPack() |
| 184 | |
| 185 | |
| 186 | def ReplaceOneEntry(image, entry, data, do_compress, allow_resize): |
| 187 | """Handle replacing a single entry an an image |
| 188 | |
| 189 | Args: |
| 190 | image: Image to update |
| 191 | entry: Entry to write |
| 192 | data: Data to replace with |
| 193 | do_compress: True to compress the data if needed, False if data is |
| 194 | already compressed so should be used as is |
| 195 | allow_resize: True to allow entries to change size (this does a re-pack |
| 196 | of the entries), False to raise an exception |
| 197 | """ |
| 198 | if not entry.WriteData(data, do_compress): |
| 199 | if not image.allow_repack: |
| 200 | entry.Raise('Entry data size does not match, but allow-repack is not present for this image') |
| 201 | if not allow_resize: |
| 202 | entry.Raise('Entry data size does not match, but resize is disabled') |
| 203 | |
| 204 | |
| 205 | def AfterReplace(image, allow_resize, write_map): |
| 206 | """Handle write out an image after replacing entries in it |
| 207 | |
| 208 | Args: |
| 209 | image: Image to write |
| 210 | allow_resize: True to allow entries to change size (this does a re-pack |
| 211 | of the entries), False to raise an exception |
| 212 | write_map: True to write a map file |
| 213 | """ |
| 214 | tout.Info('Processing image') |
| 215 | ProcessImage(image, update_fdt=True, write_map=write_map, |
| 216 | get_contents=False, allow_resize=allow_resize) |
| 217 | |
| 218 | |
| 219 | def WriteEntryToImage(image, entry, data, do_compress=True, allow_resize=True, |
| 220 | write_map=False): |
| 221 | BeforeReplace(image, allow_resize) |
| 222 | tout.Info('Writing data to %s' % entry.GetPath()) |
| 223 | ReplaceOneEntry(image, entry, data, do_compress, allow_resize) |
| 224 | AfterReplace(image, allow_resize=allow_resize, write_map=write_map) |
| 225 | |
| 226 | |
Simon Glass | 3ad804e | 2019-07-20 12:24:12 -0600 | [diff] [blame] | 227 | def WriteEntry(image_fname, entry_path, data, do_compress=True, |
| 228 | allow_resize=True, write_map=False): |
Simon Glass | 22a76b7 | 2019-07-20 12:24:11 -0600 | [diff] [blame] | 229 | """Replace an entry in an image |
| 230 | |
| 231 | This replaces the data in a particular entry in an image. This size of the |
| 232 | new data must match the size of the old data unless allow_resize is True. |
| 233 | |
| 234 | Args: |
| 235 | image_fname: Image filename to process |
| 236 | entry_path: Path to entry to extract |
| 237 | data: Data to replace with |
Simon Glass | 3ad804e | 2019-07-20 12:24:12 -0600 | [diff] [blame] | 238 | do_compress: True to compress the data if needed, False if data is |
Simon Glass | 22a76b7 | 2019-07-20 12:24:11 -0600 | [diff] [blame] | 239 | already compressed so should be used as is |
| 240 | allow_resize: True to allow entries to change size (this does a re-pack |
| 241 | of the entries), False to raise an exception |
Simon Glass | 3ad804e | 2019-07-20 12:24:12 -0600 | [diff] [blame] | 242 | write_map: True to write a map file |
Simon Glass | 22a76b7 | 2019-07-20 12:24:11 -0600 | [diff] [blame] | 243 | |
| 244 | Returns: |
| 245 | Image object that was updated |
| 246 | """ |
Simon Glass | d7fa4e4 | 2019-07-20 12:24:13 -0600 | [diff] [blame] | 247 | tout.Info("Write entry '%s', file '%s'" % (entry_path, image_fname)) |
Simon Glass | 22a76b7 | 2019-07-20 12:24:11 -0600 | [diff] [blame] | 248 | image = Image.FromFile(image_fname) |
| 249 | entry = image.FindEntryPath(entry_path) |
Simon Glass | d7fa4e4 | 2019-07-20 12:24:13 -0600 | [diff] [blame] | 250 | WriteEntryToImage(image, entry, data, do_compress=do_compress, |
| 251 | allow_resize=allow_resize, write_map=write_map) |
Simon Glass | 22a76b7 | 2019-07-20 12:24:11 -0600 | [diff] [blame] | 252 | |
Simon Glass | 22a76b7 | 2019-07-20 12:24:11 -0600 | [diff] [blame] | 253 | return image |
| 254 | |
Simon Glass | a6cb995 | 2019-07-20 12:24:15 -0600 | [diff] [blame] | 255 | |
| 256 | def ReplaceEntries(image_fname, input_fname, indir, entry_paths, |
| 257 | do_compress=True, allow_resize=True, write_map=False): |
| 258 | """Replace the data from one or more entries from input files |
| 259 | |
| 260 | Args: |
| 261 | image_fname: Image filename to process |
| 262 | input_fname: Single input ilename to use if replacing one file, None |
| 263 | otherwise |
| 264 | indir: Input directory to use (for any number of files), else None |
| 265 | entry_paths: List of entry paths to extract |
| 266 | do_compress: True if the input data is uncompressed and may need to be |
| 267 | compressed if the entry requires it, False if the data is already |
| 268 | compressed. |
| 269 | write_map: True to write a map file |
| 270 | |
| 271 | Returns: |
| 272 | List of EntryInfo records that were written |
| 273 | """ |
| 274 | image = Image.FromFile(image_fname) |
| 275 | |
| 276 | # Replace an entry from a single file, as a special case |
| 277 | if input_fname: |
| 278 | if not entry_paths: |
| 279 | raise ValueError('Must specify an entry path to read with -f') |
| 280 | if len(entry_paths) != 1: |
| 281 | raise ValueError('Must specify exactly one entry path to write with -f') |
| 282 | entry = image.FindEntryPath(entry_paths[0]) |
| 283 | data = tools.ReadFile(input_fname) |
| 284 | tout.Notice("Read %#x bytes from file '%s'" % (len(data), input_fname)) |
| 285 | WriteEntryToImage(image, entry, data, do_compress=do_compress, |
| 286 | allow_resize=allow_resize, write_map=write_map) |
| 287 | return |
| 288 | |
| 289 | # Otherwise we will input from a path given by the entry path of each entry. |
| 290 | # This means that files must appear in subdirectories if they are part of |
| 291 | # a sub-section. |
| 292 | einfos = image.GetListEntries(entry_paths)[0] |
| 293 | tout.Notice("Replacing %d matching entries in image '%s'" % |
| 294 | (len(einfos), image_fname)) |
| 295 | |
| 296 | BeforeReplace(image, allow_resize) |
| 297 | |
| 298 | for einfo in einfos: |
| 299 | entry = einfo.entry |
| 300 | if entry.GetEntries(): |
| 301 | tout.Info("Skipping section entry '%s'" % entry.GetPath()) |
| 302 | continue |
| 303 | |
| 304 | path = entry.GetPath()[1:] |
| 305 | fname = os.path.join(indir, path) |
| 306 | |
| 307 | if os.path.exists(fname): |
| 308 | tout.Notice("Write entry '%s' from file '%s'" % |
| 309 | (entry.GetPath(), fname)) |
| 310 | data = tools.ReadFile(fname) |
| 311 | ReplaceOneEntry(image, entry, data, do_compress, allow_resize) |
| 312 | else: |
| 313 | tout.Warning("Skipping entry '%s' from missing file '%s'" % |
| 314 | (entry.GetPath(), fname)) |
| 315 | |
| 316 | AfterReplace(image, allow_resize=allow_resize, write_map=write_map) |
| 317 | return image |
| 318 | |
| 319 | |
Simon Glass | a8573c4 | 2019-07-20 12:23:27 -0600 | [diff] [blame] | 320 | def PrepareImagesAndDtbs(dtb_fname, select_images, update_fdt): |
| 321 | """Prepare the images to be processed and select the device tree |
| 322 | |
| 323 | This function: |
| 324 | - reads in the device tree |
| 325 | - finds and scans the binman node to create all entries |
| 326 | - selects which images to build |
| 327 | - Updates the device tress with placeholder properties for offset, |
| 328 | image-pos, etc. |
| 329 | |
| 330 | Args: |
| 331 | dtb_fname: Filename of the device tree file to use (.dts or .dtb) |
| 332 | selected_images: List of images to output, or None for all |
| 333 | update_fdt: True to update the FDT wth entry offsets, etc. |
| 334 | """ |
| 335 | # Import these here in case libfdt.py is not available, in which case |
| 336 | # the above help option still works. |
Simon Glass | 1628793 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 337 | from dtoc import fdt |
| 338 | from dtoc import fdt_util |
Simon Glass | a8573c4 | 2019-07-20 12:23:27 -0600 | [diff] [blame] | 339 | global images |
| 340 | |
| 341 | # Get the device tree ready by compiling it and copying the compiled |
| 342 | # output into a file in our output directly. Then scan it for use |
| 343 | # in binman. |
| 344 | dtb_fname = fdt_util.EnsureCompiled(dtb_fname) |
| 345 | fname = tools.GetOutputFilename('u-boot.dtb.out') |
| 346 | tools.WriteFile(fname, tools.ReadFile(dtb_fname)) |
| 347 | dtb = fdt.FdtScan(fname) |
| 348 | |
| 349 | node = _FindBinmanNode(dtb) |
| 350 | if not node: |
| 351 | raise ValueError("Device tree '%s' does not have a 'binman' " |
| 352 | "node" % dtb_fname) |
| 353 | |
| 354 | images = _ReadImageDesc(node) |
| 355 | |
| 356 | if select_images: |
| 357 | skip = [] |
| 358 | new_images = OrderedDict() |
| 359 | for name, image in images.items(): |
| 360 | if name in select_images: |
| 361 | new_images[name] = image |
| 362 | else: |
| 363 | skip.append(name) |
| 364 | images = new_images |
| 365 | tout.Notice('Skipping images: %s' % ', '.join(skip)) |
| 366 | |
| 367 | state.Prepare(images, dtb) |
| 368 | |
| 369 | # Prepare the device tree by making sure that any missing |
| 370 | # properties are added (e.g. 'pos' and 'size'). The values of these |
| 371 | # may not be correct yet, but we add placeholders so that the |
| 372 | # size of the device tree is correct. Later, in |
| 373 | # SetCalculatedProperties() we will insert the correct values |
| 374 | # without changing the device-tree size, thus ensuring that our |
| 375 | # entry offsets remain the same. |
| 376 | for image in images.values(): |
| 377 | image.ExpandEntries() |
| 378 | if update_fdt: |
| 379 | image.AddMissingProperties() |
| 380 | image.ProcessFdt(dtb) |
| 381 | |
Simon Glass | 4bdd115 | 2019-07-20 12:23:29 -0600 | [diff] [blame] | 382 | for dtb_item in state.GetAllFdts(): |
Simon Glass | a8573c4 | 2019-07-20 12:23:27 -0600 | [diff] [blame] | 383 | dtb_item.Sync(auto_resize=True) |
| 384 | dtb_item.Pack() |
| 385 | dtb_item.Flush() |
| 386 | return images |
| 387 | |
| 388 | |
Simon Glass | 51014aa | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 389 | def ProcessImage(image, update_fdt, write_map, get_contents=True, |
| 390 | allow_resize=True): |
Simon Glass | b88e81c | 2019-07-20 12:23:24 -0600 | [diff] [blame] | 391 | """Perform all steps for this image, including checking and # writing it. |
| 392 | |
| 393 | This means that errors found with a later image will be reported after |
| 394 | earlier images are already completed and written, but that does not seem |
| 395 | important. |
| 396 | |
| 397 | Args: |
| 398 | image: Image to process |
| 399 | update_fdt: True to update the FDT wth entry offsets, etc. |
| 400 | write_map: True to write a map file |
Simon Glass | 10f9d00 | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 401 | get_contents: True to get the image contents from files, etc., False if |
| 402 | the contents is already present |
Simon Glass | 51014aa | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 403 | allow_resize: True to allow entries to change size (this does a re-pack |
| 404 | of the entries), False to raise an exception |
Simon Glass | b88e81c | 2019-07-20 12:23:24 -0600 | [diff] [blame] | 405 | """ |
Simon Glass | 10f9d00 | 2019-07-20 12:23:50 -0600 | [diff] [blame] | 406 | if get_contents: |
| 407 | image.GetEntryContents() |
Simon Glass | b88e81c | 2019-07-20 12:23:24 -0600 | [diff] [blame] | 408 | image.GetEntryOffsets() |
| 409 | |
| 410 | # We need to pack the entries to figure out where everything |
| 411 | # should be placed. This sets the offset/size of each entry. |
| 412 | # However, after packing we call ProcessEntryContents() which |
| 413 | # may result in an entry changing size. In that case we need to |
| 414 | # do another pass. Since the device tree often contains the |
| 415 | # final offset/size information we try to make space for this in |
| 416 | # AddMissingProperties() above. However, if the device is |
| 417 | # compressed we cannot know this compressed size in advance, |
| 418 | # since changing an offset from 0x100 to 0x104 (for example) can |
| 419 | # alter the compressed size of the device tree. So we need a |
| 420 | # third pass for this. |
Simon Glass | eb0f4a4 | 2019-07-20 12:24:06 -0600 | [diff] [blame] | 421 | passes = 5 |
Simon Glass | b88e81c | 2019-07-20 12:23:24 -0600 | [diff] [blame] | 422 | for pack_pass in range(passes): |
| 423 | try: |
| 424 | image.PackEntries() |
| 425 | image.CheckSize() |
| 426 | image.CheckEntries() |
| 427 | except Exception as e: |
| 428 | if write_map: |
| 429 | fname = image.WriteMap() |
| 430 | print("Wrote map file '%s' to show errors" % fname) |
| 431 | raise |
| 432 | image.SetImagePos() |
| 433 | if update_fdt: |
| 434 | image.SetCalculatedProperties() |
Simon Glass | 4bdd115 | 2019-07-20 12:23:29 -0600 | [diff] [blame] | 435 | for dtb_item in state.GetAllFdts(): |
Simon Glass | b88e81c | 2019-07-20 12:23:24 -0600 | [diff] [blame] | 436 | dtb_item.Sync() |
Simon Glass | 51014aa | 2019-07-20 12:23:56 -0600 | [diff] [blame] | 437 | dtb_item.Flush() |
Simon Glass | 261cbe0 | 2019-08-24 07:23:12 -0600 | [diff] [blame] | 438 | image.WriteSymbols() |
Simon Glass | b88e81c | 2019-07-20 12:23:24 -0600 | [diff] [blame] | 439 | sizes_ok = image.ProcessEntryContents() |
| 440 | if sizes_ok: |
| 441 | break |
| 442 | image.ResetForPack() |
Simon Glass | aed6c0b | 2019-08-24 07:23:13 -0600 | [diff] [blame] | 443 | tout.Info('Pack completed after %d pass(es)' % (pack_pass + 1)) |
Simon Glass | b88e81c | 2019-07-20 12:23:24 -0600 | [diff] [blame] | 444 | if not sizes_ok: |
Simon Glass | 61ec04f | 2019-07-20 12:23:58 -0600 | [diff] [blame] | 445 | image.Raise('Entries changed size after packing (tried %s passes)' % |
Simon Glass | b88e81c | 2019-07-20 12:23:24 -0600 | [diff] [blame] | 446 | passes) |
| 447 | |
Simon Glass | b88e81c | 2019-07-20 12:23:24 -0600 | [diff] [blame] | 448 | image.BuildImage() |
| 449 | if write_map: |
| 450 | image.WriteMap() |
| 451 | |
| 452 | |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 453 | def Binman(args): |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 454 | """The main control code for binman |
| 455 | |
| 456 | This assumes that help and test options have already been dealt with. It |
| 457 | deals with the core task of building images. |
| 458 | |
| 459 | Args: |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 460 | args: Command line arguments Namespace object |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 461 | """ |
Simon Glass | 8dbb744 | 2019-08-24 07:22:44 -0600 | [diff] [blame] | 462 | global Image |
| 463 | global state |
| 464 | |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 465 | if args.full_help: |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 466 | pager = os.getenv('PAGER') |
| 467 | if not pager: |
| 468 | pager = 'more' |
| 469 | fname = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), |
| 470 | 'README') |
| 471 | command.Run(pager, fname) |
| 472 | return 0 |
| 473 | |
Simon Glass | 8dbb744 | 2019-08-24 07:22:44 -0600 | [diff] [blame] | 474 | # Put these here so that we can import this module without libfdt |
| 475 | from image import Image |
Simon Glass | 1628793 | 2020-04-17 18:09:03 -0600 | [diff] [blame] | 476 | from binman import state |
Simon Glass | 8dbb744 | 2019-08-24 07:22:44 -0600 | [diff] [blame] | 477 | |
Simon Glass | 7bc4f0f | 2019-09-15 18:10:36 -0600 | [diff] [blame] | 478 | if args.cmd in ['ls', 'extract', 'replace']: |
Simon Glass | 96b6c50 | 2019-07-20 12:23:53 -0600 | [diff] [blame] | 479 | try: |
Simon Glass | 7bc4f0f | 2019-09-15 18:10:36 -0600 | [diff] [blame] | 480 | tout.Init(args.verbosity) |
Simon Glass | 96b6c50 | 2019-07-20 12:23:53 -0600 | [diff] [blame] | 481 | tools.PrepareOutputDir(None) |
Simon Glass | 7bc4f0f | 2019-09-15 18:10:36 -0600 | [diff] [blame] | 482 | if args.cmd == 'ls': |
| 483 | ListEntries(args.image, args.paths) |
Simon Glass | 61f564d | 2019-07-08 14:25:48 -0600 | [diff] [blame] | 484 | |
Simon Glass | 7bc4f0f | 2019-09-15 18:10:36 -0600 | [diff] [blame] | 485 | if args.cmd == 'extract': |
| 486 | ExtractEntries(args.image, args.filename, args.outdir, args.paths, |
| 487 | not args.uncompressed) |
Simon Glass | 71ce0ba | 2019-07-08 14:25:52 -0600 | [diff] [blame] | 488 | |
Simon Glass | 7bc4f0f | 2019-09-15 18:10:36 -0600 | [diff] [blame] | 489 | if args.cmd == 'replace': |
| 490 | ReplaceEntries(args.image, args.filename, args.indir, args.paths, |
| 491 | do_compress=not args.compressed, |
| 492 | allow_resize=not args.fix_size, write_map=args.map) |
| 493 | except: |
| 494 | raise |
Simon Glass | a6cb995 | 2019-07-20 12:24:15 -0600 | [diff] [blame] | 495 | finally: |
| 496 | tools.FinaliseOutputDir() |
| 497 | return 0 |
| 498 | |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 499 | # Try to figure out which device tree contains our image description |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 500 | if args.dt: |
| 501 | dtb_fname = args.dt |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 502 | else: |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 503 | board = args.board |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 504 | if not board: |
| 505 | raise ValueError('Must provide a board to process (use -b <board>)') |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 506 | board_pathname = os.path.join(args.build_dir, board) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 507 | dtb_fname = os.path.join(board_pathname, 'u-boot.dtb') |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 508 | if not args.indir: |
| 509 | args.indir = ['.'] |
| 510 | args.indir.append(board_pathname) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 511 | |
| 512 | try: |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 513 | tout.Init(args.verbosity) |
| 514 | elf.debug = args.debug |
| 515 | cbfs_util.VERBOSE = args.verbosity > 2 |
| 516 | state.use_fake_dtb = args.fake_dtb |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 517 | try: |
Simon Glass | 53cd5d9 | 2019-07-08 14:25:29 -0600 | [diff] [blame] | 518 | tools.SetInputDirs(args.indir) |
| 519 | tools.PrepareOutputDir(args.outdir, args.preserve) |
| 520 | tools.SetToolPaths(args.toolpath) |
| 521 | state.SetEntryArgs(args.entry_arg) |
Simon Glass | ecab897 | 2018-07-06 10:27:40 -0600 | [diff] [blame] | 522 | |
Simon Glass | a8573c4 | 2019-07-20 12:23:27 -0600 | [diff] [blame] | 523 | images = PrepareImagesAndDtbs(dtb_fname, args.image, |
| 524 | args.update_fdt) |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 525 | for image in images.values(): |
Simon Glass | b88e81c | 2019-07-20 12:23:24 -0600 | [diff] [blame] | 526 | ProcessImage(image, args.update_fdt, args.map) |
Simon Glass | 2a72cc7 | 2018-09-14 04:57:20 -0600 | [diff] [blame] | 527 | |
| 528 | # Write the updated FDTs to our output files |
Simon Glass | 4bdd115 | 2019-07-20 12:23:29 -0600 | [diff] [blame] | 529 | for dtb_item in state.GetAllFdts(): |
Simon Glass | 2a72cc7 | 2018-09-14 04:57:20 -0600 | [diff] [blame] | 530 | tools.WriteFile(dtb_item._fname, dtb_item.GetContents()) |
| 531 | |
Simon Glass | bf7fd50 | 2016-11-25 20:15:51 -0700 | [diff] [blame] | 532 | finally: |
| 533 | tools.FinaliseOutputDir() |
| 534 | finally: |
| 535 | tout.Uninit() |
| 536 | |
| 537 | return 0 |