1.. SPDX-License-Identifier: GPL-2.0+
2.. Copyright (c) 2016 Google, Inc
3
4Introduction
5============
6
7Firmware often consists of several components which must be packaged together.
8For example, we may have SPL, U-Boot, a devicetree and an environment area
9grouped together and placed in MMC flash. When the system starts, it must be
10able to find these pieces.
11
12Building firmware should be separate from packaging it. Many of the complexities
13of modern firmware build systems come from trying to do both at once. With
14binman, you build all the pieces that are needed, using whatever assortment of
15projects and build systems are needed, then use binman to stitch everything
16together.
17
18
19What it does
20------------
21
22Binman reads your board's devicetree and finds a node which describes the
23required image layout. It uses this to work out what to place where.
24
25Binman provides a mechanism for building images, from simple SPL + U-Boot
26combinations, to more complex arrangements with many parts. It also allows
27users to inspect images, extract and replace binaries within them, repacking if
28needed.
29
30
31Features
32--------
33
34Apart from basic padding, alignment, and positioning features, Binman supports
35hierarchical images, compression, hashing and dealing with the binary blobs,
36which are a sad trend in open-source firmware at present.
37
38Executable binaries can access the location of other binaries in an image by
39using special linker symbols (zero-overhead but limited) or by reading
40the devicetree description of the image.
41
42Binman is designed primarily for use with U-Boot and associated binaries such
43as ARM Trusted Firmware, but it is suitable for use with other projects, such
44as Zephyr. Binman also provides facilities useful in Chromium OS, such as CBFS,
45vblocks and the like.
46
47Binman provides a way to process binaries before they are included, by adding a
48Python plug-in.
49
50Binman is intended for use with U-Boot but is designed to be general enough
51to be useful in other image-packaging situations.
52
53
54Motivation
55----------
56
57As mentioned above, packaging of firmware is quite a different task from
58building the various parts. In many cases the various binaries which go into image
59come from separate build systems. For example, ARM Trusted Firmware
60is used on ARMv8 devices but is not built in the U-Boot tree. If a Linux kernel
61is included in the firmware image, it is built elsewhere.
62
63It is of course possible to add further build rules to the U-Boot
64build system to cover these cases. It can shell out to other Makefiles and
65build scripts. But it seems preferable to create a clear divide between building
66software and packaging it.
67
68At present this is handled by manual instructions, different for each board,
69on how to create images that will boot. By turning these instructions into a
70standard format, we can support making valid images for any board without
71manual effort, lots of READMEs, etc.
72
73Benefits:
74
75 - Each binary can have its own build system and tool chain without creating
76 any dependencies between them
77 - Avoids the need for a single-shot build: individual parts can be updated
78 and brought in as needed
79 - Provides for a standard image description available in the build and at
80 run-time
81 - SoC-specific image-signing tools can be accommodated
82 - Avoids cluttering the U-Boot build system with image-building code
83 - The image description is automatically available at run-time in U-Boot,
84 SPL. It can be made available to other software also
85 - The image description is easily readable (a text file in devicetree
86 format) and permits flexible packing of binaries
87
88
89Terminology
90-----------
91
92Binman uses the following terms:
93
94- image - an output file containing a firmware image
95- binary - an input binary that goes into the image
96
97
98Installation
99------------
100
101You can install binman using::
102
103 pip install binary-manager
104
105The name was chosen since binman conflicts with an existing package.
106
107If you are using binman within the U-Boot tree, it may be easiest to add a
108symlink from your local `~/.bin` directory to `/path/to/tools/binman/binman`.
109
110
111Relationship to FIT
112-------------------
113
114FIT is U-Boot's official image format. It supports multiple binaries with
115load / execution addresses, compression. It also supports verification
116through hashing and RSA signatures.
117
118FIT was originally designed to support booting a Linux kernel (with an
119optional ramdisk) and devicetree chosen from assorted options in the FIT.
120Now that U-Boot supports configuration via devicetree, it is possible to
121load U-Boot from a FIT, with the devicetree chosen by SPL.
122
123Binman considers FIT to be one of the binaries it can place in the image.
124
125Where possible it is best to put as much as possible in the FIT, with binman
126used to deal with cases not covered by FIT. Examples include initial
127execution (since FIT itself does not have an executable header) and dealing
128with device boundaries, such as the read-only/read-write separation in SPI
129flash.
130
131For U-Boot, binman should not be used to create ad-hoc images in place of
132FIT.
133
134Note that binman can itself create a FIT. This helps to move mkimage
135invocations out of the Makefile and into binman image descriptions. It also
136helps by removing the need for ad-hoc tools like `make_fit_atf.py`.
137
138
139Relationship to mkimage
140-----------------------
141
142The mkimage tool provides a means to create a FIT. Traditionally it has
143needed an image description file: a devicetree, like binman, but in a
144different format. More recently it has started to support a '-f auto' mode
145which can generate that automatically.
146
147More relevant to binman, mkimage also permits creation of many SoC-specific
148image types. These can be listed by running 'mkimage -T list'. Examples
149include 'rksd', the Rockchip SD/MMC boot format. The mkimage tool is often
150called from the U-Boot build system for this reason.
151
152Binman considers the output files created by mkimage to be binary blobs
153which it can place in an image. Binman does not replace the mkimage tool or
154this purpose. It would be possible in some situations to create a new entry
155type for the images in mkimage, but this would not add functionality. It
156seems better to use the mkimage tool to generate binaries and avoid blurring
157the boundaries between building input files (mkimage) and packaging then
158into a final image (binman).
159
160Note that binman can itself invoke mkimage. This helps to move mkimage
161invocations out of the Makefile and into binman image descriptions.
162
163
164Using binman
165============
166
167Example use of binman in U-Boot
168-------------------------------
169
170Binman aims to replace some of the ad-hoc image creation in the U-Boot
171build system.
172
173Consider sunxi. It has the following steps:
174
175 #. It uses a custom mksunxiboot tool to build an SPL image called
176 sunxi-spl.bin. This should better go into mkimage.
177
178 #. It uses mkimage to package U-Boot into a legacy image file (so that it can
179 hold the load and execution address) called u-boot.img.
180
181 #. It builds a final output image called u-boot-sunxi-with-spl.bin which
182 consists of sunxi-spl.bin, some padding and u-boot.img.
183
184Binman is intended to replace the last step. The U-Boot build system builds
185u-boot.bin and sunxi-spl.bin. Binman can then take over creation of
186sunxi-spl.bin by calling mksunxiboot or mkimage. In any case, it would then
187create the image from the component parts.
188
189This simplifies the U-Boot Makefile somewhat, since various pieces of logic
190can be replaced by a call to binman.
191
192
193Invoking binman within U-Boot
194-----------------------------
195
196Within U-Boot, binman is invoked by the build system, i.e., when you type 'make'
197or use buildman to build U-Boot. There is no need to run binman independently
198during development. Everything happens automatically and is set up for your
199SoC or board so that binman produced the right things.
200
201The general policy is that the Makefile builds all the binaries in INPUTS-y
202(the 'inputs' rule), then binman is run to produce the final images (the 'all'
203rule).
204
205There should be only one invocation of binman in Makefile, the very last step
206that pulls everything together. At present there are some arch-specific
207invocations as well, but these should be dropped when those architectures are
208converted to use binman properly.
209
210As above, the term 'binary' is used for something in INPUTS-y and 'image' is
211used for the things that binman creates. Hence, the binaries are inputs to the
212image(s), and it is the image that is actually loaded on the board.
213
214Again, at present, there are a few things created in Makefile which should
215be done by binman (when we get around to it), like `u-boot-ivt.img`,
216`lpc32xx-spl.img`, `u-boot-with-nand-spl.imx`, `u-boot-spl-padx4.sfp` and
217`u-boot-mtk.bin`, just to pick on a few. When completed this will remove about
218400 lines from `Makefile`.
219
220Since binman is invoked only once, it must of course create all the images that
221are needed, in that one invocation. It does this by working through the image
222descriptions one by one, collecting the input binaries, processing them as
223needed and producing the final images.
224
225The same binaries may be used for multiple images. For example, binman may be used
226to produce an SD-card image and a SPI-flash image. In this case the binaries
227going into the process are the same, but binman produces slightly different
228images in each case.
229
230For some SoCs, U-Boot is not the only project that produces the necessary
231binaries. For example, ARM Trusted Firmware (ATF) is a project that produces
232binaries which must be incorporated, such as `bl31.elf` or `bl31.bin`. For this
233to work you must have built ATF before you build U-Boot, and you must tell U-Boot
234where to find the bl31 image, using the BL31 environment variable.
235
236How do you know how to incorporate ATF? It is handled by the atf-bl31 entry type
237(etype). An etype is an implementation of reading a binary into binman, in this
238case the `bl31.bin` file. When you build U-Boot but do not set the BL31
239environment variable, binman provides a help message, which comes from
240`missing-blob-help`::
241
242 See the documentation for your board. You may need to build ARM Trusted
243 Firmware and build with BL31=/path/to/bl31.bin
244
245The mechanism by which binman is advised of this is also in the Makefile. See
246the `-a atf-bl31-path=${BL31}` piece in `cmd_binman`. This tells binman to
247set the EntryArg `atf-bl31-path` to the value of the `BL31` environment
248variable. Within binman, this EntryArg is picked up by the `Entry_atf_bl31`
249etype. An EntryArg is simply an argument to the entry. The `atf-bl31-path`
250name is documented in :ref:`etype_atf_bl31`.
251
252Taking this a little further, when binman is used to create a FIT, it supports
253using an ELF file, e.g. `bl31.elf` and splitting it into separate pieces (with
254`fit,operation = "split-elf"`), each with its own load address.
255
256
257Invoking binman outside U-Boot
258------------------------------
259
260While binman is invoked from within the U-Boot build system, it is also possible
261to invoke it separately. This is typically used in a production build system,
262where signing is completed (with real keys) and any missing binaries are
263provided.
264
265For example, for build testing there is no need to provide a real signature,
266nor is there any need to provide a real ATF BL31 binary (for example). These can
267be added later by invoking binman again, providing all the required inputs
268from the first time, plus any that were missing or placeholders.
269
270Then, in practice binman is often used twice:
271
272- Once within the U-Boot build system, for development and testing
273- Again, outside U-Boot to assembly and final production images
274
275While the same input binaries are used in each case, you will of course you will
276need to create your own binman command line, like that in `cmd_binman` in
277the Makefile. You may find the -I and --toolpath options useful. The
278devicetree file is provided to binman in binary form, so there is no need to
279have access to the original `.dts` sources.
280
281
282Assembling the image description
283--------------------------------
284
285Since binman uses the devicetree for its image description, you can use the
286same files that describe your board's hardware to describe how the image is
287assembled. Typically, the images description is in a common file used by all
288boards with a particular SoC (e.g. `imx8mp-u-boot.dtsi`).
289
290Where a particular board needs to make changes, it can override properties in
291the SoC file, just as it would for any other devicetree property. It can also
292add an image that is specific to the board.
293
294Another way to control the image description to make use of CONFIG options in
295the description. For example, if the start offset of a particular entry varies
296by board, you can add a Kconfig for that and reference it in the description::
297
298 u-boot-spl {
299 };
300
301 fit {
302 offset = <CONFIG_SPL_PAD_TO>;
303 ...
304 };
305
306The SoC can provide a default value, but boards can override that as needed and
307binman will take care of it.
308
309It is even possible to control which entries appear in the image, by using the
310C preprocessor::
311
312 #ifdef CONFIG_HAVE_MRC
313 intel-mrc {
314 offset = <CFG_X86_MRC_ADDR>;
315 };
316 #endif
317
318Only boards which enable `HAVE_MRC` will include this entry.
319
320Obviously, a similar approach can be used to control which images are produced,
321with a Kconfig option to enable a SPI image, for example. However, there is
322no general harm in producing an image that is not used. If a board uses MMC
323but not SPI, but the SoC supports booting from both, then both images can be
324produced, with only one or other being used by a particular board. This can help
325reduce the need for having multiple defconfig targets, for boards where the
326only difference is the boot media, enabling / disabling secure boot, etc.
327
328Of course, you can use the devicetree itself to pass any board-specific
329information that is needed by U-Boot at runtime (see binman_syms_ for how to
330make binman insert these values directly into executables like SPL).
331
332There is one more way this can be done: with individual .dtsi files for each
333image supported by the SoC. Then the board `.dts` file can include the ones it
334wants. This is not recommended, since it is likely to be difficult to maintain
335and harder to understand the relationship between the different boards.
336
337
338Producing images for multiple boards
339------------------------------------
340
341When invoked within U-Boot, binman only builds a single set of images, for
342the chosen board. This is set by the `CONFIG_DEFAULT_DEVICE_TREE` option.
343
344However, U-Boot builds all the devicetree files associated with an
345SoC. These are written in the (e.g. for ARM) `arch/arm/dts` directory. Each of
346these contains the full binman description for that board. Often the best
347approach is to build a single image that includes all these devicetree binaries
348and allow SPL to select the correct one on boot.
349
350However, it is also possible to build separate images for each board, simply by
351invoking binman multiple times, once for each devicetree file, using a
352different output directory. This will produce one set of images for each board.
353
354
355Example use of binman for x86
356-----------------------------
357
358In most cases x86 images have a lot of binary blobs, 'black-box' code
359provided by Intel which must be run for the platform to work. Typically
360these blobs are not relocatable and must be placed at fixed areas in the
361firmware image.
362
363Currently this is handled by ifdtool, which places microcode, FSP, MRC, VGA
364BIOS, reference code and Intel ME binaries into a u-boot.rom file.
365
366Binman is intended to replace all of this, with ifdtool left to handle only
367the configuration of the Intel-format descriptor.
368
369
370Installing binman
371-----------------
372
373First install prerequisites, e.g:
374
375.. code-block:: bash
376
377 sudo apt-get install python-pyelftools python3-pyelftools lzma-alone \
378 liblz4-tool
379
380You can run binman directly if you put it on your PATH. But if you want to
381install into your `~/.local` Python directory, use:
382
383.. code-block:: bash
384
385 pip install tools/patman tools/dtoc tools/binman
386
387Note that binman makes use of libraries from patman and dtoc, which is why these
388need to be installed. Also you need `libfdt` and `pylibfdt` which can be
389installed like this:
390
391.. code-block:: bash
392
393 git clone git://git.kernel.org/pub/scm/utils/dtc/dtc.git
394 cd dtc
395 pip install .
396 make NO_PYTHON=1 install
397
398This installs the `libfdt.so` library into `~/lib` so you can use
399`LD_LIBRARY_PATH=~/lib` when running binman. If you want to install it in the
400system-library directory, replace the last line with:
401
402.. code-block:: bash
403
404 make NO_PYTHON=1 PREFIX=/ install
405
406Running binman
407--------------
408
409Type:
410
411.. code-block:: bash
412
413 make NO_PYTHON=1 PREFIX=/ install
414 binman build -b <board_name>
415
416to build an image for a board. The board name is the same name used when
417configuring U-Boot (e.g. for sandbox_defconfig the board name is 'sandbox').
418Binman assumes that the input files for the build are in ../b/<board_name>.
419
420Or you can specify this explicitly:
421
422.. code-block:: bash
423
424 make NO_PYTHON=1 PREFIX=/ install
425 binman build -I <build_path>
426
427where <build_path> is the build directory containing the output of the U-Boot
428build.
429
430(Future work will make this more configurable)
431
432In either case, binman picks up the devicetree file (u-boot.dtb) and looks
433for its instructions in the 'binman' node.
434
435Binman has a few other options which you can see by running 'binman -h'.
436
437
438Enabling binman for a board
439---------------------------
440
441At present binman is invoked from a rule in the main Makefile. You should be
442able to enable CONFIG_BINMAN to enable this rule.
443
444The output file is typically named image.bin and is in the output
445directory. If input files are needed to you add these to INPUTS-y either in the
446main Makefile or in a config.mk file in your arch subdirectory.
447
448Once binman is executed it will pick up its instructions from a devicetree
449file, typically <soc>-u-boot.dtsi, where <soc> is your CONFIG_SYS_SOC value.
450You can use other, more specific CONFIG options - see 'Automatic .dtsi
451inclusion' below.
452
453.. _binman_syms:
454
455Access to binman entry offsets at run time (symbols)
456----------------------------------------------------
457
458Binman assembles images and determines where each entry is placed in the image.
459This information may be useful to U-Boot at run time. For example, in SPL it
460is useful to be able to find the location of U-Boot so that it can be executed
461when SPL is finished.
462
463Binman allows you to declare symbols in the SPL image which are filled in
464with their correct values during the build. For example:
465
466.. code-block:: c
467
468 binman_sym_declare(ulong, u_boot_any, image_pos);
469
470declares a ulong value which will be assigned to the image-pos of any U-Boot
471image (u-boot.bin, u-boot.img, u-boot-nodtb.bin) that is present in the image.
472You can access this value with something like:
473
474.. code-block:: c
475
476 ulong u_boot_offset = binman_sym(ulong, u_boot_any, image_pos);
477
478Thus u_boot_offset will be set to the image-pos of U-Boot in memory, assuming
479that the whole image has been loaded or is available in flash. You can then
480jump to that address to start U-Boot.
481
482At present this feature is only supported in SPL and TPL. In principle it is
483possible to fill in such symbols in U-Boot proper, as well, but a future C
484library is planned for this instead, to read from the devicetree.
485
486As well as image-pos, it is possible to read the size of an entry and its
487offset (which is the start position of the entry within its parent).
488
489A small technical note: Binman automatically adds the base address of the image
490(i.e. __image_copy_start) to the value of the image-pos symbol, so that when the
491image is loaded to its linked address; the value will be correct and actually
492point into the image.
493
494For example, say SPL is at the start of the image and linked to start at address
49580108000. If U-Boot's image-pos is 0x8000 then binman will write an image-pos
496for U-Boot of 80110000 into the SPL binary, since it assumes the image is loaded
497to 80108000, with SPL at 80108000 and U-Boot at 80110000. In other words, the
498positions are calculated relative to the start address of the image to which
499they are being written.
500
501For x86 devices (with the end-at-4gb property) this base address is not added
502since it is assumed that images are XIP and the offsets already include the
503address.
504
505For non-x86 cases where the symbol is used as a flash offset, the symbols-base
506property can be set to that offset (e.g. 0), so that the unadjusted image-pos
507is written into the image.
508
509While U-Boot's symbol updating is handled automatically by the u-boot-spl
510entry type (and others), it is possible to use this feature with any blob. To
511do this, add a `write-symbols` (boolean) property to the node, set the ELF
512filename using `elf-filename` and set 'elf-base-sym' to the base symbol for the
513start of the binary image (this defaults to `__image_copy_start` which is what
514U-Boot uses). See `testBlobSymbol()` for an example.
515
516.. _binman_fdt:
517
518Access to binman entry offsets at run time (fdt)
519------------------------------------------------
520
521Binman can update the U-Boot FDT to include the final position and size of
522each entry in the images it processes. The option to enable this is -u and it
523causes binman to make sure that the 'offset', 'image-pos' and 'size' properties
524are set correctly for every entry. Since it is not necessary to specify these in
525the image definition, binman calculates the final values and writes these to
526the devicetree. These can be used by U-Boot at run-time to find the location
527of each entry.
528
529Alternatively, an FDT map entry can be used to add a special FDT containing
530just the information about the image. This is preceded by a magic string so can
531be located anywhere in the image. An image header (typically at the start or end
532of the image) can be used to point to the FDT map. See fdtmap and image-header
533entries for more information.
534
535Map files
536---------
537
538The -m option causes binman to output a .map file for each image that it
539generates. This shows the offset and size of each entry. For example::
540
541 Offset Size Name
542 00000000 00000028 main-section
543 00000000 00000010 section@0
544 00000000 00000004 u-boot
545 00000010 00000010 section@1
546 00000000 00000004 u-boot
547
548This shows a hierarchical image with two sections, each with a single entry. The
549offsets of the sections are absolute hex byte offsets within the image. The
550offsets of the entries are relative to their respective sections. The size of
551each entry is also shown, in bytes (hex). The indentation shows the entries
552nested inside their sections.
553
554
555Passing command-line arguments to entries
556-----------------------------------------
557
558Sometimes it is useful to pass binman the value of an entry property from the
559command line. For example, some entries need access to files, and it is not
560always convenient to put these filenames in the image definition (devicetree).
561
562The -a option supports this::
563
564 -a <prop>=<value>
565
566where::
567
568 <prop> is the property to set
569 <value> is the value to set it to
570
571Not all properties can be provided this way. Only some entries support it,
572typically for filenames.
573
574
575Image description format
576========================
577
578The binman node is called 'binman'. An example image description is shown
579below::
580
581 binman {
582 filename = "u-boot-sunxi-with-spl.bin";
583 pad-byte = <0xff>;
584 blob {
585 filename = "spl/sunxi-spl.bin";
586 };
587 u-boot {
588 offset = <CONFIG_SPL_PAD_TO>;
589 };
590 };
591
592
593This requests binman to create an image file called u-boot-sunxi-with-spl.bin
594consisting of a specially formatted SPL (spl/sunxi-spl.bin, built by the
595normal U-Boot Makefile), some 0xff padding, and a U-Boot legacy image. The
596padding comes from the fact that the second binary is placed at
597CONFIG_SPL_PAD_TO. If that line were omitted, then the U-Boot binary would
598immediately follow the SPL binary.
599
600The binman node describes an image. The sub-nodes describe entries in the
601image. Each entry represents a region within the overall image. The name of
602the entry (blob, u-boot) tells binman what to put there. For 'blob' we must
603provide a filename. For 'u-boot', binman knows that this means 'u-boot.bin'.
604
605Entries are normally placed into the image sequentially, one after the other.
606The image size is the total size of all entries. As you can see, you can
607specify the start offset of an entry using the 'offset' property.
608
609Note that due to a devicetree requirement, all entries must have a unique
610name. If you want to put the same binary in the image multiple times, you can
611use any unique name, with the 'type' property providing the type.
612
613The attributes supported for entries are described below.
614
615offset:
616 This sets the offset of an entry within the image or section containing
617 it. The first byte of the image is normally at offset 0. If 'offset' is
618 not provided, binman sets it to the end of the previous region, or the
619 start of the image's entry area (normally 0) if there is no previous
620 region.
621
622align:
623 This sets the alignment of the entry. The entry offset is adjusted
624 so that the entry starts on an aligned boundary within the containing
625 section or image. For example, 'align = <16>' means that the entry will
626 start on a 16-byte boundary. This may mean that padding is added before
627 the entry. The padding is part of the containing section but is not
628 included in the entry, meaning that an empty space may be created before
629 the entry starts. Alignment should be a power of 2. If 'align' is not
630 provided, no alignment is performed.
631
632size:
633 This sets the size of the entry. The contents will be padded out to
634 this size. If this is not provided, it will be set to the size of the
635 contents.
636
637min-size:
638 Sets the minimum size of the entry. This size includes explicit padding
639 ('pad-before' and 'pad-after'), but not padding added to meet alignment
640 requirements. While this does not affect the contents of the entry within
641 binman itself (the padding is performed only when its parent section is
642 assembled), the result will be that the entry ends with the padding
643 bytes, so may grow. Defaults to 0.
644
645pad-before:
646 Padding before the contents of the entry. Normally this is 0, meaning
647 that the contents start at the beginning of the entry. This can be used
648 to offset the entry contents a little. While this does not affect the
649 contents of the entry within binman itself (the padding is performed
650 only when its parent section is assembled), the result will be that
651 the entry starts with the padding bytes, so it may grow. Defaults to 0.
652
653pad-after:
654 Padding after the contents of the entry. Normally this is 0, meaning
655 that the entry ends at the last byte of content (unless adjusted by
656 other properties). This allows room to be created in the image for
657 this entry to expand later. While this does not affect the contents of
658 the entry within binman itself (the padding is performed only when its
659 parent section is assembled), the result will be that the entry ends
660 with the padding bytes, so may grow. Defaults to 0.
661
662align-size:
663 This sets the alignment of the entry size. For example, to ensure
664 that the size of an entry is a multiple of 64 bytes, set this to 64.
665 While this does not affect the contents of the entry within binman
666 itself (the padding is performed only when its parent section is
667 assembled), the result is that the entry ends with the padding
668 bytes, so may grow. If 'align-size' is not provided, no alignment is
669 performed.
670
671align-end:
672 This sets the alignment of the end of an entry with respect to the
673 containing section. Some entries require that they end on an alignment
674 boundary, regardless of where they start. This does not move the start
675 of the entry, so the contents of the entry will still start at the
676 beginning. But there may be padding at the end. While this does not
677 affect the contents of the entry within binman itself (the padding is
678 performed only when its parent section is assembled), the result
679 is that the entry ends with the padding bytes, so may grow.
680 If 'align-end' is not provided, no alignment is performed.
681
682filename:
683 For 'blob' types this provides the filename containing the binary to
684 put into the entry. If binman knows about the entry type (like
685 u-boot-bin), then there is no need to specify this.
686
687type:
688 Sets the type of an entry. This defaults to the entry name, but it is
689 possible to use any name, and then add (for example) 'type = "u-boot"'
690 to specify the type.
691
692offset-unset:
693 Indicates that the offset of this entry should not be set by placing
694 it immediately after the entry before. Instead, is set by another
695 entry which knows where this entry should go. When this boolean
696 property is present, binman will give an error if another entry does
697 not set the offset (with the GetOffsets() method).
698
699image-pos:
700 This cannot be set on entry (or at least it is ignored if it is), but
701 with the -u option, binman will set it to the absolute image position
702 for each entry. This makes it easy to find out exactly where the entry
703 ended up in the image, regardless of parent sections, etc.
704
705extend-size:
706 Extend the size of this entry to fit available space. This space is only
707 limited by the size of the image/section and the position of the next
708 entry.
709
710compress:
711 Sets the compression algorithm to use (for blobs only). See the entry
712 documentation for details.
713
714missing-msg:
715 Sets the tag of the message to show if this entry is missing. This is
716 used for external blobs. When they are missing it is helpful to show
717 information about what needs to be fixed. See missing-blob-help for the
718 message for each tag.
719
720assume-size:
721 Sets the assumed size of a blob entry if it is missing. This allows for a
722 check that the rest of the image fits into the available space, even when
723 the contents are not available. If the entry is missing, Binman will use
724 this assumed size for the entry size, including creating a fake file of that
725 size if requested.
726
727no-expanded:
728 By default, binman substitutes entries with expanded versions if available,
729 so that a `u-boot` entry type turns into `u-boot-expanded`, for example. The
730 `--no-expanded` command-line option disables this globally. The
731 `no-expanded` property disables this just for a single entry. Put the
732 `no-expanded` boolean property in the node to select this behavior.
733
734optional:
735 External blobs are normally required to be present for the image to be
736 built (but see `External blobs`_). This properly allows an entry to be
737 optional, so that when it cannot be found, this problem is ignored and
738 an empty file is used for this blob. This should be used only when the blob
739 is entirely optional and is not needed for correct operation of the image.
740 Note that missing, optional blobs do not produce a non-zero exit code from
741 binman, although it does show a warning about the missing external blob.
742
743insert-template:
744 This is not an entry property, since it is processed early
745 in Binman before the entries are read. It is a list of phandles of nodes to
746 include in the current (target) node. For each node, its subnodes and their
747 properties are brought into the target node. See Templates_ below for
748 more information.
749
750symbols-base:
751 When writing symbols into a binary, the value of that symbol is assumed to
752 be relative to the base address of the binary. This allow the binary to be
753 loaded in memory at its base address, so that symbols point into the binary
754 correctly. In some cases, the binary is in fact not yet in memory, but must
755 be read from storage. In this case there is no base address for the symbols.
756 This property can be set to 0 to indicate this. Other values for
757 symbols-base are allowed, but care must be taken that the code which uses
758 the symbol is aware of the base being used. If omitted, the binary's base
759 address is used.
760
761The attributes supported for images and sections are described below. Several
762of them are like the attributes for entries.
763
764size:
765 Sets the image size in bytes, for example 'size = <0x100000>' for a
766 1MB image.
767
768offset:
769 This is like 'offset' in entries, setting the offset of a section
770 within the image or section containing it. The first byte of the section
771 is normally at offset 0. If 'offset' is not provided, binman sets it to
772 the end of the previous region, or the start of the image's entry area
773 (normally 0) if there is no previous region.
774
775align-size:
776 This sets the alignment of the image size. For example, to ensure
777 that the image ends on a 512-byte boundary, use 'align-size = <512>'.
778 If 'align-size' is not provided, no alignment is performed.
779
780pad-before:
781 This sets the padding before the image entries. The first entry will
782 be positioned after the padding. This defaults to 0.
783
784pad-after:
785 This sets the padding after the image entries. The padding will be
786 placed after the last entry. This defaults to 0.
787
788pad-byte:
789 This specifies the pad byte to use when padding in the image. It
790 defaults to 0. To use 0xff, you would add 'pad-byte = <0xff>'.
791
792filename:
793 This specifies the image filename. It defaults to 'image.bin'.
794
795sort-by-offset:
796 This causes binman to reorder the entries as needed to make sure they
797 are in increasing positional order. This can be used when your entry
798 order may not match the positional order. A common situation is where
799 the 'offset' properties are set by CONFIG options, so their ordering is
800 not known a priori.
801
802 This is a boolean property, so it needs no value. To enable it, add a
803 line 'sort-by-offset;' to your description.
804
805multiple-images:
806 Normally only a single image is generated. To create more than one
807 image, put this property in the binman node. For example, this will
808 create image1.bin containing u-boot.bin, and image2.bin containing
809 both spl/u-boot-spl.bin and u-boot.bin::
810
811 binman {
812 multiple-images;
813 image1 {
814 u-boot {
815 };
816 };
817
818 image2 {
819 spl {
820 };
821 u-boot {
822 };
823 };
824 };
825
826align-default:
827 Specifies the default alignment for entries in this section if they do
828 not specify an alignment. Note that this only applies to top-level entries
829 in the section (direct subentries), not any subentries of those entries.
830 This means that each section must specify its own default alignment, if
831 required.
832
833symlink:
834 Adds a symlink to the image with string given in the symlink property.
835
836overlap:
837 Indicates that this entry overlaps with others in the same section. These
838 entries should appear at the end of the section. Overlapping entries are not
839 packed with other entries, but their contents are written over other entries
840 in the section. Overlapping entries must have an explicit offset and size.
841
842write-symbols:
843 Indicates that the blob should be updated with symbol values calculated by
844 binman. This is automatic for certain entry types, e.g. `u-boot-spl`. See
845 binman_syms_ for more information.
846
847no-write-symbols:
848 Disables symbol writing for this entry. This can be used in entry types
849 where symbol writing is automatic. For example, if `u-boot-spl` refers to
850 the `u_boot_any_image_pos` symbol but U-Boot is not available in the image
851 containing SPL, this can be used to disable the writing. Quite likely this
852 indicates a bug in your setup.
853
854elf-filename:
855 Sets the file name of a blob's associated ELF file. For example, if the
856 blob is `zephyr.bin` then the ELF file may be `zephyr.elf`. This allows
857 binman to locate symbols and understand the structure of the blob. See
858 binman_syms_ for more information.
859
860elf-base-sym:
861 Sets the name of the ELF symbol that points to the start of a blob. For
862 U-Boot this is `__image_copy_start` and that is the default used by binman
863 if this property is missing. For other projects, a difference symbol may be
864 needed. Add this symbol to the properties for the blob so that symbols can
865 be read correctly. See binman_syms_ for more information.
866
867offset-from-elf:
868 Sets the offset of an entry based on a symbol value in another entry.
869 The format is <&phandle>, "sym_name", <offset> where phandle is the entry
870 containing the blob (with associated ELF file providing symbols), <sym_name>
871 is the symbol to lookup (relative to elf-base-sym) and <offset> is an offset
872 to add to that value.
873
874preserve:
875 Indicates that this entry should be preserved by any firmware updates. This
876 flag should be checked by the updater when it is deciding which entries to
877 update. This flag is normally attached to sections but can be attached to
878 a single entry in a section if the updater supports it. Not that binman
879 itself has no control over the updater's behavior, so this is just a
880 signal. It is not enforced by binman.
881
882Examples of the above options can be found in the tests. See the
883tools/binman/test directory.
884
885It is possible to have the same binary appear multiple times in the image,
886either by using a unit number suffix (u-boot@0, u-boot@1) or by using a
887different name for each and specifying the type with the 'type' attribute.
888
889
890Sections and hierarchical images
891--------------------------------
892
893Sometimes it is convenient to split an image into several pieces, each of which
894contains its own set of binaries. An example is a flash device where part of
895the image is read-only, and part is read-write. We can set up sections for each
896of these, and place binaries in them independently. The image is still produced
897as a single output file.
898
899This feature provides a way of creating hierarchical images. For example, here
900is an example image with two copies of U-Boot. One is read-only (ro), intended
901to be written only in the factory. Another is read-write (rw), so that it can be
902upgraded in the field. The sizes are fixed so that the ro/rw boundary is known
903and can be programmed::
904
905 binman {
906 section@0 {
907 read-only;
908 name-prefix = "ro-";
909 size = <0x100000>;
910 u-boot {
911 };
912 };
913 section@1 {
914 name-prefix = "rw-";
915 size = <0x100000>;
916 u-boot {
917 };
918 };
919 };
920
921This image could be placed into a SPI flash chip, with the protection boundary
922set at 1MB.
923
924A few special properties are provided for sections:
925
926read-only:
927 Indicates that this section is read-only. This has no impact on binman's
928 operation, but his property can be read at run time.
929
930name-prefix:
931 This string is prepended to all the names of the binaries in the
932 section. In the example above, the 'u-boot' binaries will be
933 renamed to 'ro-u-boot' and 'rw-u-boot'. This can be useful to
934 distinguish binaries with otherwise identical names.
935
936filename:
937 This allows the contents of the section to be written to a file in the
938 output directory. This can sometimes be useful to use the data in one
939 section in different image, since there is currently no way to share data
940 between images other than through files.
941
942end-at-4gb:
943 For x86 machines the ROM offsets start just before 4GB and extend
944 up so that the image finished at the 4GB boundary. This boolean
945 option can be enabled to support this. The image size must be
946 provided so that binman knows when the image should start. For an
947 8MB ROM, the offset of the first entry would be 0xfff80000 with
948 this option, instead of 0 without this option.
949
950skip-at-start:
951 This property specifies the entry offset of the first entry in the section.
952 It is useful when the Binman image is written to a particular offset in the
953 media. It allows the offset of the first entry to be the media offset, even
954 though it is at the start of the image. It effectively creates a hole at the
955 start of the image, an implied, empty area.
956
957 For example, if the image is written to offset 4K on the media, set
958 skip-at-start to 0x1000. At runtime, the Binman image will assume that it
959 has be written at offset 4K and all symbols and offsets will take account of
960 that. The image-pos values will also be adjusted. The effect is similar to
961 adding an empty 4K region at the start, except that Binman does not actually
962 output it.
963
964 For PowerPC mpc85xx based CPU, CONFIG_TEXT_BASE is the entry
965 offset of the first entry. It can be 0xeff40000 or 0xfff40000 for
966 nor flash boot, 0x201000 for sd boot etc.
967
968 'end-at-4gb' property is not applicable where CONFIG_TEXT_BASE +
969 Image size != 4gb.
970
971Image Properties
972----------------
973
974Image nodes act like sections but also have a few extra properties:
975
976filename:
977 Output filename for the image. This defaults to image.bin (or in the
978 case of multiple images <nodename>.bin where <nodename> is the name of
979 the image node.
980
981allow-repack:
982 Create an image that can be repacked. With this option it is possible
983 to change anything in the image after it is created, including updating
984 the position and size of image components. By default, this is not
985 permitted since it is not possible to know whether this might violate a
986 constraint in the image description. For example, if a section must
987 increase in size to hold a larger binary, that might cause the section
988 to exceed its allow-region (e.g. the read-only portion of flash).
989
990 Adding this property causes the original offset and size values in the
991 image description to be stored in the FDT and fdtmap.
992
993
994Image dependencies
995------------------
996
997Binman does not currently support images that depend on each other. For example,
998if one image creates `fred.bin` and then the next uses this `fred.bin` to
999produce a final `image.bin`, then the behavior is undefined. It may work, or it
1000may produce an error about `fred.bin` being missing, or it may use a version of
1001`fred.bin` from a previous run.
1002
1003Often this can be handled by incorporating the dependency into the second
1004image. For example, instead of::
1005
1006 binman {
1007 multiple-images;
1008
1009 fred {
1010 u-boot {
1011 };
1012 fill {
1013 size = <0x100>;
1014 };
1015 };
1016
1017 image {
1018 blob {
1019 filename = "fred.bin";
1020 };
1021 u-boot-spl {
1022 };
1023 };
1024
1025you can do this::
1026
1027 binman {
1028 image {
1029 fred {
1030 type = "section";
1031 u-boot {
1032 };
1033 fill {
1034 size = <0x100>;
1035 };
1036 };
1037 u-boot-spl {
1038 };
1039 };
1040
1041
1042
1043Hashing Entries
1044---------------
1045
1046It is possible to ask binman to hash the contents of an entry and write that
1047value back to the devicetree node. For example::
1048
1049 binman {
1050 u-boot {
1051 hash {
1052 algo = "sha256";
1053 };
1054 };
1055 };
1056
1057Here, a new 'value' property will be written to the 'hash' node containing
1058the hash of the 'u-boot' entry. Only SHA256 is supported at present. Whole
1059sections can be hashed if desired, by adding the 'hash' node to the section.
1060
1061The hash value can be checked at runtime by hashing the data read and
1062comparing this hash to the value in the devicetree.
1063
1064
1065Expanded entries
1066----------------
1067
1068Binman automatically replaces 'u-boot' with an expanded version of that, i.e.
1069'u-boot-expanded'. This means that when you write::
1070
1071 u-boot {
1072 };
1073
1074you actually get::
1075
1076 u-boot {
1077 type = "u-boot-expanded';
1078 };
1079
1080which in turn expands to::
1081
1082 u-boot {
1083 type = "section";
1084
1085 u-boot-nodtb {
1086 };
1087
1088 u-boot-dtb {
1089 };
1090 };
1091
1092U-Boot's phase binaries comprise two or three pieces. For example, u-boot.bin
1093has the executable followed by a devicetree.
1094
1095With binman we want to be able to update that devicetree with full image
1096information so that it is accessible to the executable. This is tricky
1097if it is not clear where the devicetree starts.
1098
1099The above feature ensures that the devicetree is clearly separated from the
1100U-Boot executable and can be updated separately by binman as needed. It can be
1101disabled with the --no-expanded flag if required.
1102
1103The same applies for u-boot-spl and u-boot-tpl. In those cases, the expansion
1104includes the BSS padding, so for example::
1105
1106 spl {
1107 type = "u-boot-spl"
1108 };
1109
1110you actually get::
1111
1112 spl {
1113 type = "u-boot-expanded';
1114 };
1115
1116which in turn expands to::
1117
1118 spl {
1119 type = "section";
1120
1121 u-boot-spl-nodtb {
1122 };
1123
1124 u-boot-spl-bss-pad {
1125 };
1126
1127 u-boot-spl-dtb {
1128 };
1129 };
1130
1131Of course, we should not expand SPL if it has no devicetree. Also, if the BSS
1132padding is not needed (because BSS is in RAM as with CONFIG_SPL_SEPARATE_BSS),
1133the 'u-boot-spl-bss-pad' subnode should not be created. The use of the expanded
1134entry type is controlled by the UseExpanded() method. In the SPL case it checks
1135the 'spl-dtb' entry arg, which is 'y' or '1' if SPL has a devicetree.
1136
1137For the BSS case, a 'spl-bss-pad' entry arg controls whether it is present. All
1138entry args are provided by the U-Boot Makefile.
1139
1140
1141Optional entries
1142----------------
1143
1144Some entries need to exist only if certain conditions are met. For example, an
1145entry may want to appear in the image only if a file has a particular format.
1146Also, the ``optional`` property may be used to mark entries as optional::
1147
1148 tee-os {
1149 filename = "tee.bin";
1150 optional;
1151 };
1152
1153Obviously the entry must exist in the image description for it to be processed
1154at all, so a way needs to be found to have the entry remove itself.
1155
1156To handle this, when entry.ObtainContents() is called, the entry can call
1157entry.mark_absent() to mark itself as absent, passing a suitable message as the
1158reason.
1159
1160Any absent entries are dropped immediately after ObtainContents() has been
1161called on all entries.
1162
1163It is not possible for an entry to mark itself absent at any other point in the
1164processing. It must happen in the ObtainContents() method.
1165
1166The effect is as if the entry had never been present at all, since the image
1167is packed without it and it disappears from the list of entries.
1168
1169
1170Compression
1171-----------
1172
1173Binman support compression for 'blob' entries (those of type 'blob' and
1174derivatives). To enable this for an entry, add a 'compress' property::
1175
1176 blob {
1177 filename = "datafile";
1178 compress = "lz4";
1179 };
1180
1181The entry will then contain the compressed data, using the 'lz4' compression
1182algorithm. Currently this is the only one that is supported. The uncompressed
1183size is written to the node in an 'uncomp-size' property, if -u is used.
1184
1185Compression is also supported for sections. In that case the entire section is
1186compressed in one block, including all its contents. This means that accessing
1187an entry from the section required decompressing the entire section. Also, the
1188size of a section indicates the space that it consumes in its parent section
1189(and typically the image). With compression, the section may contain more data,
1190and the uncomp-size property indicates that, as above. The contents of the
1191section is compressed first, before any padding is added. This ensures that the
1192padding itself is not compressed, which would be a waste of time.
1193
1194
1195Automatic .dtsi inclusion
1196-------------------------
1197
1198It is sometimes inconvenient to add a 'binman' node to the .dts file for each
1199board. This can be done by using #include to bring in a common file. Another
1200approach supported by the U-Boot build system is to automatically include
1201a common header. You can then put the binman node (and anything else that is
1202specific to U-Boot, such as bootph-all properies) in that header file.
1203
1204Binman will search for the following files in arch/<arch>/dts::
1205
1206 <dts>-u-boot.dtsi where <dts> is the base name of the .dts file
1207 <CONFIG_SYS_SOC>-u-boot.dtsi
1208 <CONFIG_SYS_CPU>-u-boot.dtsi
1209 <CONFIG_SYS_VENDOR>-u-boot.dtsi
1210 u-boot.dtsi
1211
1212U-Boot will only use the first one that it finds. If you need to include a
1213more general file you can do that from the more specific file using #include.
1214If you are having trouble figuring out what is going on, you can use
1215`DEVICE_TREE_DEBUG=1` with your build::
1216
1217 make DEVICE_TREE_DEBUG=1
1218 scripts/Makefile.lib:334: Automatic .dtsi inclusion: options:
1219 arch/arm/dts/juno-r2-u-boot.dtsi arch/arm/dts/-u-boot.dtsi
1220 arch/arm/dts/armv8-u-boot.dtsi arch/arm/dts/armltd-u-boot.dtsi
1221 arch/arm/dts/u-boot.dtsi ... found: "arch/arm/dts/juno-r2-u-boot.dtsi"
1222
1223
1224Templates
1225=========
1226
1227Sometimes multiple images need to be created which have all have a common
1228part. For example, a board may generate SPI and eMMC images which both include
1229a FIT. Since the FIT includes many entries, it is tedious to repeat them twice
1230in the image description.
1231
1232Templates provide a simple way to handle this::
1233
1234 binman {
1235 multiple-images;
1236 common_part: template-1 {
1237 some-property;
1238 fit {
1239 ... lots of entries in here
1240 };
1241
1242 text {
1243 text = "base image";
1244 };
1245 };
1246
1247 spi-image {
1248 filename = "image-spi.bin";
1249 insert-template = <&common_part>;
1250
1251 /* things specific to SPI follow */
1252 footer {
1253 ];
1254
1255 text {
1256 text = "SPI image";
1257 };
1258 };
1259
1260 mmc-image {
1261 filename = "image-mmc.bin";
1262 insert-template = <&common_part>;
1263
1264 /* things specific to MMC follow */
1265 footer {
1266 ];
1267
1268 text {
1269 text = "MMC image";
1270 };
1271 };
1272 };
1273
1274The template node name must start with 'template', so it is not considered to be
1275an image itself.
1276
1277The mechanism is very simple. For each phandle in the 'insert-templates'
1278property, the source node is looked up. Then the subnodes of that source node
1279are copied into the target node, i.e. the one containing the `insert-template`
1280property.
1281
1282If the target node has a node with the same name as a template, its properties
1283override corresponding properties in the template. This allows the template to
1284be uses as a base, with the node providing updates to the properties as needed.
1285The overriding happens recursively.
1286
1287Template nodes appear first in each node that they are inserted into and
1288ordering of template nodes is preserved. Other nodes come afterwards. If a
1289template node also appears in the target node, then the template node sets the
1290order. Thus the template can be used to set the ordering, even if the target
1291node provides all the properties. In the above example, `fit` and `text` appear
1292first in the `spi-image` and `mmc-image` images, followed by `footer`.
1293
1294Where there are multiple template nodes, they are inserted in that order. so
1295the first template node appears first, then the second.
1296
1297Properties in the template node are inserted into the destination node if they
1298do not exist there. In the example above, `some-property` is added to each of
1299`spi-image` and `mmc-image`.
1300
1301Note that template nodes are removed from the binman description after
1302processing and before binman builds the image descriptions.
1303
1304The initial devicetree produced by the templating process is written to the
1305`u-boot.dtb.tmpl1` file. This can be useful to see what is going on if there is
1306a failure before the final `u-boot.dtb.out` file is written. A second
1307`u-boot.dtb.tmpl2` file is written when the templates themselves are removed.
1308
1309Dealing with phandles
1310---------------------
1311
1312Templates can contain phandles and these are copied to the destination node.
1313However this should be used with care, since if a template is instantiated twice
1314then the phandle will be copied twice, resulting in a devicetree with duplicate
1315phandles, i.e. the same phandle used by two different nodes. Binman detects this
1316situation and produces an error, for example::
1317
1318 Duplicate phandle 1 in nodes /binman/image/fit/images/atf/atf-bl31 and
1319 /binman/image-2/fit/images/atf/atf-bl31
1320
1321In this case an atf-bl31 node containing a phandle has been copied into two
1322different target nodes, resulting in the same phandle for each. See
1323testTemplatePhandleDup() for the test case.
1324
1325The solution is typically to put the phandles in the corresponding target nodes
1326(one for each) and remove the phandle from the template.
1327
1328Updating an ELF file
1329====================
1330
1331For the EFI app, where U-Boot is loaded from UEFI and runs as an app, there is
1332no way to update the devicetree after U-Boot is built. Normally this works by
1333creating a new u-boot.dtb.out with he updated devicetree, which is automatically
1334built into the output image. With ELF this is not possible since the ELF is
1335not part of an image, just a stand-along file. We must create an updated ELF
1336file with the new devicetree.
1337
1338This is handled by the --update-fdt-in-elf option. It takes four arguments,
1339separated by comma:
1340
1341 infile - filename of input ELF file, e.g. 'u-boot's
1342 outfile - filename of output ELF file, e.g. 'u-boot.out'
1343 begin_sym - symbol at the start of the embedded devicetree, e.g.
1344 '__dtb_dt_begin'
1345 end_sym - symbol at the start of the embedded devicetree, e.g.
1346 '__dtb_dt_end'
1347
1348When this flag is used, U-Boot does all the normal packaging, but as an
1349additional step, it creates a new ELF file with the new devicetree embedded in
1350it.
1351
1352If logging is enabled you will see a message like this::
1353
1354 Updating file 'u-boot' with data length 0x400a (16394) between symbols
1355 '__dtb_dt_begin' and '__dtb_dt_end'
1356
1357There must be enough space for the updated devicetree. If not, an error like
1358the following is produced::
1359
1360 ValueError: Not enough space in 'u-boot' for data length 0x400a (16394);
1361 size is 0x1744 (5956)
1362
1363
1364Entry Documentation
1365===================
1366
1367For details on the various entry types supported by binman and how to use them,
1368see entries.rst which is generated from the source code using:
1369
1370 binman entry-docs >tools/binman/entries.rst
1371
1372.. toctree::
1373 :maxdepth: 2
1374
1375 entries
1376
1377
1378Managing images
1379===============
1380
1381Listing images
1382--------------
1383
1384It is possible to list the entries in an existing firmware image created by
1385binman, provided that there is an 'fdtmap' entry in the image. For example::
1386
1387 $ binman ls -i image.bin
1388 Name Image-pos Size Entry-type Offset Uncomp-size
1389 ----------------------------------------------------------------------
1390 main-section c00 section 0
1391 u-boot 0 4 u-boot 0
1392 section 5fc section 4
1393 cbfs 100 400 cbfs 0
1394 u-boot 138 4 u-boot 38
1395 u-boot-dtb 180 108 u-boot-dtb 80 3b5
1396 u-boot-dtb 500 1ff u-boot-dtb 400 3b5
1397 fdtmap 6fc 381 fdtmap 6fc
1398 image-header bf8 8 image-header bf8
1399
1400This shows the hierarchy of the image, the position, size and type of each
1401entry, the offset of each entry within its parent and the uncompressed size if
1402the entry is compressed.
1403
1404It is also possible to list just some files in an image, e.g.::
1405
1406 $ binman ls -i image.bin section/cbfs
1407 Name Image-pos Size Entry-type Offset Uncomp-size
1408 --------------------------------------------------------------------
1409 cbfs 100 400 cbfs 0
1410 u-boot 138 4 u-boot 38
1411 u-boot-dtb 180 108 u-boot-dtb 80 3b5
1412
1413or with wildcards::
1414
1415 $ binman ls -i image.bin "*cb*" "*head*"
1416 Name Image-pos Size Entry-type Offset Uncomp-size
1417 ----------------------------------------------------------------------
1418 cbfs 100 400 cbfs 0
1419 u-boot 138 4 u-boot 38
1420 u-boot-dtb 180 108 u-boot-dtb 80 3b5
1421 image-header bf8 8 image-header bf8
1422
1423If an older version of binman is used to list images created by a newer one, it
1424is possible that it will contain entry types that are not supported. These still
1425show with the correct type, but binman just sees them as blobs (plain binary
1426data). Any special features of that etype are not supported by the old binman.
1427
1428
1429Extracting files from images
1430----------------------------
1431
1432You can extract files from an existing firmware image created by binman,
1433provided that there is an 'fdtmap' entry in the image. For example::
1434
1435 $ binman extract -i image.bin section/cbfs/u-boot
1436
1437which will write the uncompressed contents of that entry to the file 'u-boot' in
1438the current directory. You can also extract to a particular file, in this case
1439u-boot.bin::
1440
1441 $ binman extract -i image.bin section/cbfs/u-boot -f u-boot.bin
1442
1443It is possible to extract all files into a destination directory, which will
1444put files in subdirectories matching the entry hierarchy::
1445
1446 $ binman extract -i image.bin -O outdir
1447
1448or just a selection::
1449
1450 $ binman extract -i image.bin "*u-boot*" -O outdir
1451
1452Some entry types have alternative formats, for example fdtmap which allows
1453extracted just the devicetree binary without the fdtmap header::
1454
1455 $ binman extract -i /tmp/b/odroid-c4/image.bin -f out.dtb -F fdt fdtmap
1456 $ fdtdump out.dtb
1457 /dts-v1/;
1458 // magic: 0xd00dfeed
1459 // totalsize: 0x8ab (2219)
1460 // off_dt_struct: 0x38
1461 // off_dt_strings: 0x82c
1462 // off_mem_rsvmap: 0x28
1463 // version: 17
1464 // last_comp_version: 2
1465 // boot_cpuid_phys: 0x0
1466 // size_dt_strings: 0x7f
1467 // size_dt_struct: 0x7f4
1468
1469 / {
1470 image-node = "binman";
1471 image-pos = <0x00000000>;
1472 size = <0x0011162b>;
1473 ...
1474
1475Use `-F list` to see what alternative formats are available::
1476
1477 $ binman extract -i /tmp/b/odroid-c4/image.bin -F list
1478 Flag (-F) Entry type Description
1479 fdt fdtmap Extract the devicetree blob from the fdtmap
1480
1481
1482Replacing files in an image
1483---------------------------
1484
1485You can replace files in an existing firmware image created by binman, provided
1486that there is an 'fdtmap' entry in the image. For example::
1487
1488 $ binman replace -i image.bin section/cbfs/u-boot
1489
1490which will write the contents of the file 'u-boot' from the current directory
1491to the that entry, compressing if necessary. If the entry size changes, you must
1492add the 'allow-repack' property to the original image before generating it (see
1493above), otherwise you will get an error.
1494
1495You can also use a particular file, in this case u-boot.bin::
1496
1497 $ binman replace -i image.bin section/cbfs/u-boot -f u-boot.bin
1498
1499It is possible to replace all files from a source directory which uses the same
1500hierarchy as the entries::
1501
1502 $ binman replace -i image.bin -I indir
1503
1504Files that are missing will generate a warning.
1505
1506You can also replace just a selection of entries::
1507
1508 $ binman replace -i image.bin "*u-boot*" -I indir
1509
1510It is possible to replace whole sections as well, but in that case any
1511information about entries within the section may become outdated. This is
1512because Binman cannot know whether things have moved around or resized within
1513the section, once you have updated its data.
1514
1515Technical note: With 'allow-repack', Binman writes information about the
1516original offset and size properties of each entry, if any were specified, in
1517the 'orig-offset' and 'orig-size' properties. This allows Binman to distinguish
1518between an entry which ended up being packed at an offset (or assigned a size)
1519and an entry which had a particular offset / size requested in the Binman
1520configuration. Where are particular offset / size was requested, this is treated
1521as set in stone, so Binman will ensure it doesn't change. Without this feature,
1522repacking an entry might cause it to disobey the original constraints provided
1523when it was created.
1524
1525
1526Signing FIT container with private key in an image
1527--------------------------------------------------
1528
1529You can sign FIT container with private key in your image.
1530For example::
1531
1532 $ binman sign -i image.bin -k privatekey -a sha256,rsa4096 fit
1533
1534binman will extract FIT container, sign and replace it immediately.
1535
1536If you want to sign and replace FIT container in place::
1537
1538 $ binman sign -i image.bin -k privatekey -a sha256,rsa4096 -f fit.fit fit
1539
1540which will sign FIT container with private key and replace it immediately
1541inside your image.
1542
1543.. _`BinmanLogging`:
1544
1545Logging
1546-------
1547
1548Binman normally operates silently unless there is an error, in which case it
1549just displays the error. The -D/--debug option can be used to create a full
1550backtrace when errors occur. You can use BINMAN_DEBUG=1 when building to select
1551this.
1552
1553Internally binman logs some output while it is running. This can be displayed
1554by increasing the -v/--verbosity from the default of 1:
1555
1556 0: silent
1557 1: warnings only
1558 2: notices (important messages)
1559 3: info about major operations
1560 4: detailed information about each operation
1561 5: debug (all output)
1562
1563You can use BINMAN_VERBOSE=5 (for example) when building to select this.
1564
1565
1566Bintools
1567========
1568
1569`Bintool` is the name binman gives to a binary tool which it uses to create and
1570manipulate binaries that binman cannot handle itself. Bintools are often
1571necessary since Binman only supports a subset of the available file formats
1572natively.
1573
1574Many SoC vendors invent ways to load code into their SoC using new file formats,
1575sometimes changing the format with successive SoC generations. Sometimes the
1576tool is available as Open Source. Sometimes it is a pre-compiled binary that
1577must be downloaded from the vendor's website. Sometimes it is available in
1578source form but difficult or slow to build.
1579
1580Even for images that use bintools, binman still assembles the image from its
1581image description. It may handle parts of the image natively and part with
1582various bintools.
1583
1584Binman relies on these tools so provides various features to manage them:
1585
1586- Determining whether the tool is currently installed
1587- Downloading or building the tool
1588- Determining the version of the tool that is installed
1589- Deciding which tools are needed to build an image
1590
1591The Bintool class is an interface to the tool, a thin level of abstration, using
1592Python functions to run the tool for each purpose (e.g. creating a new
1593structure, adding a file to an existing structure) rather than just lists of
1594string arguments.
1595
1596As with external blobs, bintools (which are like 'external' tools) can be
1597missing. When building an image requires a bintool and it is not installed,
1598binman detects this and reports the problem, but continues to build an image.
1599This is useful in CI systems which want to check that everything is correct but
1600don't have access to the bintools.
1601
1602To make this work, all calls to bintools (e.g. with Bintool.run_cmd()) must cope
1603with the tool being missing, i.e. when None is returned, by:
1604
1605- Calling self.record_missing_bintool()
1606- Setting up some fake contents so binman can continue
1607
1608Of course the image will not work, but binman reports which bintools are needed
1609and also provide a way to fetch them.
1610
1611To see the available bintools, use::
1612
1613 binman tool --list
1614
1615To fetch tools which are missing, use::
1616
1617 binman tool --fetch missing
1618
1619You can also use `--fetch all` to fetch all tools or `--fetch <tool>` to fetch
1620a particular tool. Some tools are built from source code, in which case you will
1621need to have at least the `build-essential` and `git` packages installed.
1622
1623Tools are fetched into the `~/.binman-tools` directory. This directory is
1624automatically added to the toolpath so there is no need to use `--toolpath` to
1625specify it. If you want to use these tools outside binman, you may want to
1626add this directory to your `PATH`. For example, if you use bash, add this to
1627the end of `.bashrc`::
1628
1629 PATH="$HOME/.binman-tools:$PATH"
1630
1631To select a custom directory, use the `--tooldir` option.
1632
1633Bintool Documentation
1634=====================
1635
1636To provide details on the various bintools supported by binman, bintools.rst is
1637generated from the source code using:
1638
1639 binman bintool-docs >tools/binman/bintools.rst
1640
1641.. toctree::
1642 :maxdepth: 2
1643
1644 bintools
1645
1646Binman commands and arguments
1647=============================
1648
1649Usage::
1650
1651 binman [-h] [-B BUILD_DIR] [-D] [--tooldir TOOLDIR] [-H]
1652 [--toolpath TOOLPATH] [-T THREADS] [--test-section-timeout]
1653 [-v VERBOSITY] [-V]
1654 {build,bintool-docs,entry-docs,ls,extract,replace,test,tool} ...
1655
1656Binman provides the following commands:
1657
1658- **build** - build images
1659- **bintools-docs** - generate documentation about bintools
1660- **entry-docs** - generate documentation about entry types
1661- **ls** - list an image
1662- **extract** - extract files from an image
1663- **replace** - replace one or more entries in an image
1664- **test** - run tests
1665- **tool** - manage bintools
1666
1667Options:
1668
1669-h, --help
1670 Show help message and exit
1671
1672-B BUILD_DIR, --build-dir BUILD_DIR
1673 Directory containing the build output
1674
1675-D, --debug
1676 Enabling debugging (provides a full traceback on error)
1677
1678--tooldir TOOLDIR Set the directory to store tools
1679
1680-H, --full-help
1681 Display the README file
1682
1683--toolpath TOOLPATH
1684 Add a path to the list of directories containing tools
1685
1686-T THREADS, --threads THREADS
1687 Number of threads to use (0=single-thread). Note that -T0 is useful for
1688 debugging since everything runs in one thread.
1689
1690-v VERBOSITY, --verbosity VERBOSITY
1691 Control verbosity: 0=silent, 1=warnings, 2=notices, 3=info, 4=detail,
1692 5=debug
1693
1694-V, --version
1695 Show the binman version
1696
1697Test options:
1698
1699--test-section-timeout
1700 Use a zero timeout for section multi-threading (for testing)
1701
1702Commands are described below.
1703
1704binman build
1705------------
1706
1707This builds one or more images using the provided image description.
1708
1709Usage::
1710
1711 binman build [-h] [-a ENTRY_ARG] [-b BOARD] [-d DT] [--fake-dtb]
1712 [--fake-ext-blobs] [--force-missing-bintools FORCE_MISSING_BINTOOLS]
1713 [-i IMAGE] [-I INDIR] [-m] [-M] [-n] [-O OUTDIR] [-p] [-u]
1714 [--update-fdt-in-elf UPDATE_FDT_IN_ELF] [-W]
1715
1716Options:
1717
1718-h, --help
1719 Show help message and exit
1720
1721-a ENTRY_ARG, --entry-arg ENTRY_ARG
1722 Set argument value `arg=value`. See
1723 `Passing command-line arguments to entries`_.
1724
1725-b BOARD, --board BOARD
1726 Board name to build. This can be used instead of `-d`, in which case the
1727 file `u-boot.dtb` is used, within the build directory's board subdirectory.
1728
1729-d DT, --dt DT
1730 Configuration file (.dtb) to use. This must have a top-level node called
1731 `binman`. See `Image description format`_.
1732
1733-i IMAGE, --image IMAGE
1734 Image filename to build (if not specified, build all)
1735
1736-I INDIR, --indir INDIR
1737 Add a path to the list of directories to use for input files. This can be
1738 specified multiple times to add more than one path.
1739
1740-m, --map
1741 Output a map file for each image. See `Map files`_.
1742
1743-M, --allow-missing
1744 Allow external blobs and bintools to be missing. See `External blobs`_.
1745
1746-n, --no-expanded
1747 Don't use 'expanded' versions of entries where available; normally 'u-boot'
1748 becomes 'u-boot-expanded', for example. See `Expanded entries`_.
1749
1750-O OUTDIR, --outdir OUTDIR
1751 Path to directory to use for intermediate and output files
1752
1753-p, --preserve
1754 Preserve temporary output directory even if option -O is not given
1755
1756-u, --update-fdt
1757 Update the binman node with offset/size info. See
1758 `Access to binman entry offsets at run time (fdt)`_.
1759
1760--update-fdt-in-elf UPDATE_FDT_IN_ELF
1761 Update an ELF file with the output dtb. The argument is a string consisting
1762 of four parts, separated by commas. See `Updating an ELF file`_.
1763
1764-W, --ignore-missing
1765 Return success even if there are missing blobs/bintools (requires -M)
1766
1767Options used only for testing:
1768
1769--fake-dtb
1770 Use fake devicetree contents
1771
1772--fake-ext-blobs
1773 Create fake ext blobs with dummy content
1774
1775--force-missing-bintools FORCE_MISSING_BINTOOLS
1776 Comma-separated list of bintools to consider missing
1777
1778binman bintool-docs
1779-------------------
1780
1781Usage::
1782
1783 binman bintool-docs [-h]
1784
1785This outputs documentation for the bintools in rST format. See
1786`Bintool Documentation`_.
1787
1788binman entry-docs
1789-----------------
1790
1791Usage::
1792
1793 binman entry-docs [-h]
1794
1795This outputs documentation for the entry types in rST format. See
1796`Entry Documentation`_.
1797
1798binman ls
1799---------
1800
1801Usage::
1802
1803 binman ls [-h] -i IMAGE [paths ...]
1804
1805Positional arguments:
1806
1807paths
1808 Paths within file to list (wildcard)
1809
1810Options:
1811
1812-h, --help
1813 show help message and exit
1814
1815-i IMAGE, --image IMAGE
1816 Image filename to list
1817
1818This lists an image, showing its contents. See `Listing images`_.
1819
1820binman extract
1821--------------
1822
1823Usage::
1824
1825 binman extract [-h] [-F FORMAT] -i IMAGE [-f FILENAME] [-O OUTDIR] [-U]
1826 [paths ...]
1827
1828Positional arguments:
1829
1830Paths
1831 Paths within file to extract (wildcard)
1832
1833Options:
1834
1835-h, --help
1836 show help message and exit
1837
1838-F FORMAT, --format FORMAT
1839 Select an alternative format for extracted data
1840
1841-i IMAGE, --image IMAGE
1842 Image filename to extract
1843
1844-f FILENAME, --filename FILENAME
1845 Output filename to write to
1846
1847-O OUTDIR, --outdir OUTDIR
1848 Path to directory to use for output files
1849
1850-U, --uncompressed
1851 Output raw uncompressed data for compressed entries
1852
1853This extracts the contents of entries from an image. See
1854`Extracting files from images`_.
1855
1856binman replace
1857--------------
1858
1859Usage::
1860
1861 binman replace [-h] [-C] -i IMAGE [-f FILENAME] [-F] [-I INDIR] [-m]
1862 [paths ...]
1863
1864Positional arguments:
1865
1866paths
1867 Paths within file to replace (wildcard)
1868
1869Options:
1870
1871-h, --help
1872 show help message and exit
1873
1874-C, --compressed
1875 Input data is already compressed if needed for the entry
1876
1877-i IMAGE, --image IMAGE
1878 Image filename to update
1879
1880-f FILENAME, --filename FILENAME
1881 Input filename to read from
1882
1883-F, --fix-size
1884 Don't allow entries to be resized
1885
1886-I INDIR, --indir INDIR
1887 Path to directory to use for input files
1888
1889-m, --map
1890 Output a map file for the updated image
1891
1892-O OUTDIR, --outdir OUTDIR
1893 Path to directory to use for intermediate and output files
1894
1895-p, --preserve
1896 Preserve temporary output directory even if option -O is not given
1897
1898This replaces one or more entries in an existing image. See
1899`Replacing files in an image`_.
1900
1901binman test
1902-----------
1903
1904Usage::
1905
1906 binman test [-h] [-P PROCESSES] [-T] [-X] [tests ...]
1907
1908Positional arguments:
1909
1910tests
1911 Test names to run (omit for all)
1912
1913Options:
1914
1915-h, --help
1916 show help message and exit
1917
1918-P PROCESSES, --processes PROCESSES
1919 set number of processes to use for running tests. This defaults to the
1920 numbering the CPUs on the machine
1921
1922-T, --test-coverage
1923 run tests and check for 100% coverage
1924
1925-X, --test-preserve-dirs
1926 Preserve and display test-created input directories; also preserve the
1927 output directory if a single test is run (pass test name at the end of the
1928 command line
1929
1930binman sign
1931-----------
1932
1933Usage::
1934
1935 binman sign [-h] -a ALGO [-f FILE] -i IMAGE -k KEY [paths ...]
1936
1937positional arguments:
1938
1939paths
1940 Paths within file to sign (wildcard)
1941
1942options:
1943
1944-h, --help
1945 show this help message and exit
1946
1947-a ALGO, --algo ALGO
1948 Hash algorithm e.g. sha256,rsa4096
1949
1950-f FILE, --file FILE
1951 Input filename to sign
1952
1953-i IMAGE, --image IMAGE
1954 Image filename to update
1955
1956-k KEY, --key KEY
1957 Private key file for signing
1958
1959binman tool
1960-----------
1961
1962Usage::
1963
1964 binman tool [-h] [-l] [-f] [bintools ...]
1965
1966Positional arguments:
1967
1968bintools
1969 Bintools to process
1970
1971Options:
1972
1973-h, --help
1974 show help message and exit
1975
1976-l, --list
1977 List all known bintools
1978
1979-f, --fetch
1980 Fetch a bintool from a known location. Use `all` to fetch all and `missing`
1981 to fetch any missing tools.
1982
1983
1984Technical details
1985=================
1986
1987Order of image creation
1988-----------------------
1989
1990Image creation proceeds in the following order, for each entry in the image.
1991
19921. AddMissingProperties() - binman can add calculated values to the device
1993tree as part of its processing, for example the offset and size of each
1994entry. This method adds any properties associated with this, expanding the
1995devicetree as needed. These properties can have placeholder values which are
1996set later by SetCalculatedProperties(). By that stage, the size of sections
1997cannot be changed (since it would cause the images to need to be repacked),
1998but the correct values can be inserted.
1999
20002. ProcessFdt() - process the devicetree information as required by the
2001entry. This may involve adding or deleting properties. If the
2002processing is complete, this method should return True. If the processing
2003cannot complete because it needs the ProcessFdt() method of another entry to
2004run first, this method should return False, in which case it will be called
2005again later.
2006
20073. GetEntryContents() - the contents of each entry are obtained, normally by
2008reading from a file. This calls the Entry.ObtainContents() to read the
2009contents. The default version of Entry.ObtainContents() calls
2010Entry.GetDefaultFilename() and then reads that file. Thus, a common mechanism
2011to select a file to read is to override that function in the subclass. The
2012functions must return True when they have read the contents. Binman will
2013retry calling the functions a few times if False is returned, allowing
2014dependencies between the contents of different entries.
2015
20164. GetEntryOffsets() - calls Entry.GetOffsets() for each entry. This can
2017return a dict containing entries that need updating. The key should be the
2018entry name and the value is a tuple (offset, size). This allows an entry to
2019provide the offset and size for other entries. The default implementation
2020of GetEntryOffsets() returns {}.
2021
20225. PackEntries() - calls Entry.Pack() which figures out the offset and
2023size of an entry. The 'current' image offset is passed in, and the function
2024returns the offset immediately after the entry being packed. The default
2025implementation of Pack() is usually sufficient.
2026
2027Note: for sections, this also checks that the entries do not overlap, nor extend
2028outside the section. If the section does not have a defined size, the size is
2029set large enough to hold all the entries. For entries that are explicitly marked
2030as overlapping, this check is skipped.
2031
20326. SetImagePos() - sets the image position of every entry. This is the absolute
2033position 'image-pos', as opposed to 'offset' which is relative to the containing
2034section. This must be done after all offsets are known, which is why it is quite
2035late in the ordering.
2036
20377. SetCalculatedProperties() - update any calculated properties in the device
2038tree. This sets the correct 'offset' and 'size' vaues, for example.
2039
20408. ProcessEntryContents() - this calls Entry.ProcessContents() on each entry.
2041The default implementatoin does nothing. This can be overriden to adjust the
2042contents of an entry in some way. For example, it would be possible to create
2043an entry containing a hash of the contents of some other entries. At this
2044stage the offset and size of entries should not be adjusted unless absolutely
2045necessary, since it requires a repack (going back to PackEntries()).
2046
20479. ResetForPack() - if the ProcessEntryContents() step failed, in that an entry
2048has changed its size, then there is no alternative but to go back to step 5 and
2049try again, repacking the entries with the updated size. ResetForPack() removes
2050the fixed offset/size values added by binman, so that the packing can start from
2051scratch.
2052
205310. WriteSymbols() - write the value of symbols into the U-Boot SPL binary.
2054See 'Access to binman entry offsets at run time' below for a description of
2055what happens in this stage.
2056
205711. BuildImage() - builds the image and writes it to a file
2058
205912. WriteMap() - writes a text file containing a map of the image. This is the
2060last step.
2061
2062
2063.. _`External tools`:
2064
2065External tools
2066--------------
2067
2068Binman can make use of external command-line tools to handle processing of
2069entry contents or to generate entry contents. These tools are executed using
2070the 'tools' module's Run() method. The tools must exist on the PATH,
2071but the --toolpath option can be used to specify additional search paths to
2072use. This option can be specified multiple times to add more than one path.
2073
2074For some compile tools binman will use the versions specified by commonly used
2075environment variables like CC and HOSTCC for the C compiler, based on whether
2076the tool's output will be used for the target or for the host machine. If those
2077are not given, it will also try to derive target-specific versions from the
2078CROSS_COMPILE environment variable during a cross-compilation.
2079
2080If the tool is not available in the path you can use BINMAN_TOOLPATHS to specify
2081a space-separated list of paths to search, e.g.::
2082
2083 BINMAN_TOOLPATHS="/tools/g12a /tools/tegra" binman ...
2084
2085
2086.. _`External blobs`:
2087
2088External blobs
2089--------------
2090
2091Binary blobs, even if the source code is available, complicate building
2092firmware. The instructions can involve multiple steps and the binaries may be
2093hard to build or obtain. Binman at least provides a unified description of how
2094to build the final image, no matter what steps are needed to get there.
2095
2096Binman also provides a `blob-ext` entry type that pulls in a binary blob from an
2097external file. If the file is missing, binman can optionally complete the build
2098and just report a warning. Use the `-M/--allow-missing` option to enable this.
2099This is useful in CI systems which want to check that everything is correct but
2100don't have access to the blobs.
2101
2102If the blobs are in a different directory, you can specify this with the `-I`
2103option.
2104
2105For U-Boot, you can set the BINMAN_INDIRS environment variable to provide a
2106space-separated list of directories to search for binary blobs::
2107
2108 BINMAN_INDIRS="odroid-c4/fip/g12a \
2109 odroid-c4/build/board/hardkernel/odroidc4/firmware \
2110 odroid-c4/build/scp_task" binman ...
2111
2112Note that binman fails with exit code 103 when there are missing blobs. If you
2113wish binman to continue anyway, you can pass `-W` to binman.
2114
2115
2116Code coverage
2117-------------
2118
2119Binman is a critical tool and is designed to be very testable. Entry
2120implementations target 100% test coverage. Run ``binman test -T`` to check this.
2121
2122To enable Python test coverage on Debian-type distributions (e.g. Ubuntu)::
2123
2124 $ sudo apt-get install python-coverage python3-coverage python-pytest
2125
2126You can also check the coverage provided by a single test, e.g.::
2127
2128 binman test -T testSimple
2129
2130Exit status
2131-----------
2132
2133Binman produces the following exit codes:
2134
21350
2136 Success
2137
21381
2139 Any sort of failure - see output for more details
2140
2141103
2142 There are missing external blobs or bintools. This is only returned if
2143 -M is passed to binman, otherwise missing blobs return an exit status of 1.
2144 Note, if -W is passed as well as -M, then this is converted into a warning
2145 and will return an exit status of 0 instead.
2146
2147
2148U-Boot environment variables for binman
2149---------------------------------------
2150
2151The U-Boot Makefile supports various environment variables to control binman.
2152All of these are set within the Makefile and result in passing various
2153environment variables (or make flags) to binman:
2154
2155BINMAN_DEBUG
2156 Enables backtrace debugging by adding a `-D` argument. See
2157 :ref:`BinmanLogging`.
2158
2159BINMAN_INDIRS
2160 Sets the search path for input files used by binman by adding one or more
2161 `-I` arguments. See :ref:`External blobs`.
2162
2163BINMAN_TOOLPATHS
2164 Sets the search path for external tool used by binman by adding one or more
2165 `--toolpath` arguments. See :ref:`External tools`.
2166
2167BINMAN_VERBOSE
2168 Sets the logging verbosity of binman by adding a `-v` argument. See
2169 :ref:`BinmanLogging`.
2170
2171
2172Error messages
2173--------------
2174
2175This section provides some guidance for some of the less obvious error messages
2176produced by binman.
2177
2178
2179Expected __bss_size symbol
2180~~~~~~~~~~~~~~~~~~~~~~~~~~
2181
2182Example::
2183
2184 binman: Node '/binman/u-boot-spl-ddr/u-boot-spl/u-boot-spl-bss-pad':
2185 Expected __bss_size symbol in spl/u-boot-spl
2186
2187This indicates that binman needs the `__bss_size` symbol to be defined in the
2188SPL binary, where `spl/u-boot-spl` is the ELF file containing the symbols. The
2189symbol tells binman the size of the BSS region, in bytes. It needs this to be
2190able to pad the image so that the following entries do not overlap the BSS,
2191which would cause them to be overwritte by variable access in SPL.
2192
2193These symbols are normally defined in the linker script, immediately after
2194_bss_start and __bss_end are defined, like this::
2195
2196 __bss_size = __bss_end - __bss_start;
2197
2198You may need to add it to your linker script if you get this error.
2199
2200
2201Concurrent tests
2202----------------
2203
2204Binman tries to run tests concurrently. This means that the tests make use of
2205all available CPUs to run.
2206
2207 Enable this::
2208
2209 $ sudo apt-get install python-subunit python3-subunit
2210
2211Use '-P 1' to disable this. It is automatically disabled when code coverage is
2212being used (-T) since they are incompatible.
2213
2214
2215Writing tests
2216-------------
2217
2218See :doc:`../binman_tests`.
2219
2220Debugging tests
2221---------------
2222
2223Sometimes when debugging tests, it is useful to keep the input and output
2224directories so they can be examined later. Use -X or --test-preserve-dirs for
2225this.
2226
2227
2228Running tests on non-x86 architectures
2229--------------------------------------
2230
2231Binman's tests have been written under the assumption that they'll be run on a
2232x86-like host and there hasn't been an attempt to make them portable yet.
2233However, it's possible to run the tests by cross-compiling to x86.
2234
2235To install an x86 cross-compiler on Debian-type distributions (e.g. Ubuntu)::
2236
2237 $ sudo apt-get install gcc-x86-64-linux-gnu
2238
2239Then, you can run the tests under cross-compilation::
2240
2241 $ CROSS_COMPILE=x86_64-linux-gnu- binman test -T
2242
2243You can also use gcc-i686-linux-gnu similar to the above.
2244
2245
2246Writing new entries and debugging
2247---------------------------------
2248
2249The behaviour of entries is defined by the Entry class. All other entries are
2250a subclass of this. An important subclass is Entry_blob which takes binary
2251data from a file and places it in the entry. In fact most entry types are
2252subclasses of Entry_blob.
2253
2254Each entry type is a separate file in the tools/binman/etype directory. Each
2255file contains a class called Entry_<type> where <type> is the entry type.
2256New entry types can be supported by adding new files in that directory.
2257These will automatically be detected by binman when needed.
2258
2259Entry properties are documented in entry.py. The entry subclasses are free
2260to change the values of properties to support special behaviour. For example,
2261when Entry_blob loads a file, it sets content_size to the size of the file.
2262Entry classes can adjust other entries. For example, an entry that knows
2263where other entries should be positioned can set up those entries' offsets
2264so they don't need to be set in the binman decription. It can also adjust
2265entry contents.
2266
2267Most of the time such essoteric behaviour is not needed, but it can be
2268essential for complex images.
2269
2270If you need to specify a particular devicetree compiler to use, you can define
2271the DTC environment variable. This can be useful when the system dtc is too
2272old.
2273
2274To enable a full backtrace and other debugging features in binman, pass
2275BINMAN_DEBUG=1 to your build::
2276
2277 make qemu-x86_defconfig
2278 make BINMAN_DEBUG=1
2279
2280To enable verbose logging from binman, base BINMAN_VERBOSE to your build, which
2281adds a -v<level> option to the call to binman::
2282
2283 make qemu-x86_defconfig
2284 make BINMAN_VERBOSE=5
2285
2286
2287Building sections in parallel
2288-----------------------------
2289
2290By default binman uses multiprocessing to speed up compilation of large images.
2291This works at a section level, with one thread for each entry in the section.
2292This can speed things up if the entries are large and use compression.
2293
2294This feature can be disabled with the '-T' flag, which defaults to a suitable
2295value for your machine. This depends on the Python version, e.g on v3.8 it uses
229612 threads on an 8-core machine. See ConcurrentFutures_ for more details.
2297
2298The special value -T0 selects single-threaded mode, useful for debugging during
2299development, since dealing with exceptions and problems in threads is more
2300difficult. This avoids any use of ThreadPoolExecutor.
2301
2302
2303Collecting data for an entry type
2304---------------------------------
2305
2306Some entry types deal with data obtained from others. For example,
2307`Entry_mkimage` calls the `mkimage` tool with data from its subnodes::
2308
2309 mkimage {
2310 args = "-n test -T script";
2311
2312 u-boot-spl {
2313 };
2314
2315 u-boot {
2316 };
2317 };
2318
2319This shows mkimage being passed a file consisting of SPL and U-Boot proper. It
2320is created by calling `Entry.collect_contents_to_file()`. Note that in this
2321case, the data is passed to mkimage for processing but does not appear
2322separately in the image. It may not appear at all, depending on what mkimage
2323does. The contents of the `mkimage` entry are entirely dependent on the
2324processing done by the entry, with the provided subnodes (`u-boot-spl` and
2325`u-boot`) simply providing the input data for that processing.
2326
2327Note that `Entry.collect_contents_to_file()` simply concatenates the data from
2328the different entries together, with no control over alignment, etc. Another
2329approach is to subclass `Entry_section` so that those features become available,
2330such as `size` and `pad-byte`. Then the contents of the entry can be obtained by
2331calling `super().BuildSectionData()` in the entry's BuildSectionData()
2332implementation to get the input data, then write it to a file and process it
2333however is desired.
2334
2335There are other ways to obtain data also, depending on the situation. If the
2336entry type is simply signing data which exists elsewhere in the image, then
2337you can use `Entry_collection` as a base class. It lets you use a property
2338called `content` which lists the entries containing data to be processed. This
2339is used by `Entry_vblock`, for example::
2340
2341 u_boot: u-boot {
2342 };
2343
2344 vblock {
2345 content = <&u_boot &dtb>;
2346 keyblock = "firmware.keyblock";
2347 signprivate = "firmware_data_key.vbprivk";
2348 version = <1>;
2349 kernelkey = "kernel_subkey.vbpubk";
2350 preamble-flags = <1>;
2351 };
2352
2353 dtb: u-boot-dtb {
2354 };
2355
2356which shows an image containing `u-boot` and `u-boot-dtb`, with the `vblock`
2357image collecting their contents to produce input for its signing process,
2358without affecting those entries, which still appear in the final image
2359untouched.
2360
2361Another example is where an entry type needs several independent pieces of input
2362to function. For example, `Entry_fip` allows a number of different binary blobs
2363to be placed in their own individual places in a custom data structure in the
2364output image. To make that work you can add subnodes for each of them and call
2365`Entry.Create()` on each subnode, as `Entry_fip` does. Then the data for each
2366blob can come from any suitable place, such as an `Entry_u_boot` or an
2367`Entry_blob` or anything else::
2368
2369 atf-fip {
2370 fip-hdr-flags = /bits/ 64 <0x123>;
2371 soc-fw {
2372 fip-flags = /bits/ 64 <0x123456789abcdef>;
2373 filename = "bl31.bin";
2374 };
2375
2376 u-boot {
2377 fip-uuid = [fc 65 13 92 4a 5b 11 ec
2378 94 35 ff 2d 1c fc 79 9c];
2379 };
2380 };
2381
2382The `soc-fw` node is a `blob-ext` (i.e. it reads in a named binary file) whereas
2383`u-boot` is a normal entry type. This works because `Entry_fip` selects the
2384`blob-ext` entry type if the node name (here `soc-fw`) is recognized as being
2385a known blob type.
2386
2387When adding new entry types, you are encouraged to use subnodes to provide the
2388data for processing, unless the `content` approach is more suitable. Consider
2389whether the input entries are contained within (or consumed by) the entry, vs
2390just being 'referenced' by the entry. In the latter case, the `content` approach
2391makes more sense. Ad-hoc properties and other methods of obtaining data are
2392discouraged, since it adds to confusion for users.
2393
2394History / Credits
2395-----------------
2396
2397Binman takes a lot of inspiration from a Chrome OS tool called
2398'cros_bundle_firmware', which I wrote some years ago. That tool was based on
2399a simple and sound design but has expanded over the
2400years. In particular, its handling of x86 images is convoluted.
2401
2402Quite a few lessons have been learned which are hopefully applied here.
2403
2404
2405Design notes
2406------------
2407
2408On the face of it, a tool to create firmware images should be simple:
2409just find all the input binaries and place them at the right place in the
2410image. The difficulty comes from the wide variety of input types (simple
2411flat binaries containing code, packaged data with various headers), packing
2412requirements (alignment, spacing, device boundaries) and other required
2413features such as hierarchical images.
2414
2415The design challenge is to make it easy to create simple images, while
2416allowing the more complex cases to be supported. For example, for most
2417images we don't much care exactly where each binary ends up, so we should
2418not have to specify that unnecessarily.
2419
2420New entry types should aim to provide simple usage where possible. If new
2421core features are needed, they can be added in the Entry base class.
2422
2423
2424To do
2425-----
2426
2427Some ideas:
2428
2429- Use of-platdata to make the information available to code that is unable
2430 to use devicetree (such as a small SPL image). For now, limited info is
2431 available via linker symbols
2432- Allow easy building of images by specifying just the board name
2433- Support building an image for a board (-b) more completely, with a
2434 configurable build directory
2435- Detect invalid properties in nodes
2436- Sort the fdtmap by offset
2437- Output temporary files to a different directory
2438- Rationalise the fdt, fdt_util and pylibfdt modules which currently have some
2439 overlapping and confusing functionality
2440- Update the fdt library to use a better format for Prop.value (the current one
2441 is useful for dtoc but not much else)
2442- Figure out how to make Fdt support changing the node order, so that
2443 Node.AddSubnode() can support adding a node before another, existing node.
2444 Perhaps it should completely regenerate the flat tree?
2445- Support images which depend on each other
2446
2447--
2448Simon Glass <sjg@chromium.org>
24497/7/2016
2450
2451.. _ConcurrentFutures: https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.ThreadPoolExecutor
2452