xref: /openbmc/u-boot/tools/binman/README (revision 0a4357c4)
1bf7fd50bSSimon Glass# SPDX-License-Identifier: GPL-2.0+
283d290c5STom Rini# Copyright (c) 2016 Google, Inc
3bf7fd50bSSimon Glass
4bf7fd50bSSimon GlassIntroduction
5bf7fd50bSSimon Glass------------
6bf7fd50bSSimon Glass
7bf7fd50bSSimon GlassFirmware often consists of several components which must be packaged together.
8bf7fd50bSSimon GlassFor example, we may have SPL, U-Boot, a device tree and an environment area
9bf7fd50bSSimon Glassgrouped together and placed in MMC flash. When the system starts, it must be
10bf7fd50bSSimon Glassable to find these pieces.
11bf7fd50bSSimon Glass
12bf7fd50bSSimon GlassSo far U-Boot has not provided a way to handle creating such images in a
13bf7fd50bSSimon Glassgeneral way. Each SoC does what it needs to build an image, often packing or
14bf7fd50bSSimon Glassconcatenating images in the U-Boot build system.
15bf7fd50bSSimon Glass
16bf7fd50bSSimon GlassBinman aims to provide a mechanism for building images, from simple
17bf7fd50bSSimon GlassSPL + U-Boot combinations, to more complex arrangements with many parts.
18bf7fd50bSSimon Glass
19bf7fd50bSSimon Glass
20bf7fd50bSSimon GlassWhat it does
21bf7fd50bSSimon Glass------------
22bf7fd50bSSimon Glass
23bf7fd50bSSimon GlassBinman reads your board's device tree and finds a node which describes the
24bf7fd50bSSimon Glassrequired image layout. It uses this to work out what to place where. The
25bf7fd50bSSimon Glassoutput file normally contains the device tree, so it is in principle possible
26bf7fd50bSSimon Glassto read an image and extract its constituent parts.
27bf7fd50bSSimon Glass
28bf7fd50bSSimon Glass
29bf7fd50bSSimon GlassFeatures
30bf7fd50bSSimon Glass--------
31bf7fd50bSSimon Glass
32bf7fd50bSSimon GlassSo far binman is pretty simple. It supports binary blobs, such as 'u-boot',
33bf7fd50bSSimon Glass'spl' and 'fdt'. It supports empty entries (such as setting to 0xff). It can
34bf7fd50bSSimon Glassplace entries at a fixed location in the image, or fit them together with
35bf7fd50bSSimon Glasssuitable padding and alignment. It provides a way to process binaries before
36bf7fd50bSSimon Glassthey are included, by adding a Python plug-in. The device tree is available
37bf7fd50bSSimon Glassto U-Boot at run-time so that the images can be interpreted.
38bf7fd50bSSimon Glass
39bf7fd50bSSimon GlassBinman does not yet update the device tree with the final location of
40bf7fd50bSSimon Glasseverything when it is done. A simple C structure could be generated for
41bf7fd50bSSimon Glassconstrained environments like SPL (using dtoc) but this is also not
42bf7fd50bSSimon Glassimplemented.
43bf7fd50bSSimon Glass
44bf7fd50bSSimon GlassBinman can also support incorporating filesystems in the image if required.
45bf7fd50bSSimon GlassFor example x86 platforms may use CBFS in some cases.
46bf7fd50bSSimon Glass
47bf7fd50bSSimon GlassBinman is intended for use with U-Boot but is designed to be general enough
48bf7fd50bSSimon Glassto be useful in other image-packaging situations.
49bf7fd50bSSimon Glass
50bf7fd50bSSimon Glass
51bf7fd50bSSimon GlassMotivation
52bf7fd50bSSimon Glass----------
53bf7fd50bSSimon Glass
54bf7fd50bSSimon GlassPackaging of firmware is quite a different task from building the various
55bf7fd50bSSimon Glassparts. In many cases the various binaries which go into the image come from
56bf7fd50bSSimon Glassseparate build systems. For example, ARM Trusted Firmware is used on ARMv8
57bf7fd50bSSimon Glassdevices but is not built in the U-Boot tree. If a Linux kernel is included
58bf7fd50bSSimon Glassin the firmware image, it is built elsewhere.
59bf7fd50bSSimon Glass
60bf7fd50bSSimon GlassIt is of course possible to add more and more build rules to the U-Boot
61bf7fd50bSSimon Glassbuild system to cover these cases. It can shell out to other Makefiles and
62bf7fd50bSSimon Glassbuild scripts. But it seems better to create a clear divide between building
63bf7fd50bSSimon Glasssoftware and packaging it.
64bf7fd50bSSimon Glass
65bf7fd50bSSimon GlassAt present this is handled by manual instructions, different for each board,
66bf7fd50bSSimon Glasson how to create images that will boot. By turning these instructions into a
67bf7fd50bSSimon Glassstandard format, we can support making valid images for any board without
68bf7fd50bSSimon Glassmanual effort, lots of READMEs, etc.
69bf7fd50bSSimon Glass
70bf7fd50bSSimon GlassBenefits:
71bf7fd50bSSimon Glass- Each binary can have its own build system and tool chain without creating
72bf7fd50bSSimon Glassany dependencies between them
73bf7fd50bSSimon Glass- Avoids the need for a single-shot build: individual parts can be updated
74bf7fd50bSSimon Glassand brought in as needed
75bf7fd50bSSimon Glass- Provides for a standard image description available in the build and at
76bf7fd50bSSimon Glassrun-time
77bf7fd50bSSimon Glass- SoC-specific image-signing tools can be accomodated
78bf7fd50bSSimon Glass- Avoids cluttering the U-Boot build system with image-building code
79bf7fd50bSSimon Glass- The image description is automatically available at run-time in U-Boot,
80bf7fd50bSSimon GlassSPL. It can be made available to other software also
81bf7fd50bSSimon Glass- The image description is easily readable (it's a text file in device-tree
82bf7fd50bSSimon Glassformat) and permits flexible packing of binaries
83bf7fd50bSSimon Glass
84bf7fd50bSSimon Glass
85bf7fd50bSSimon GlassTerminology
86bf7fd50bSSimon Glass-----------
87bf7fd50bSSimon Glass
88bf7fd50bSSimon GlassBinman uses the following terms:
89bf7fd50bSSimon Glass
90bf7fd50bSSimon Glass- image - an output file containing a firmware image
91bf7fd50bSSimon Glass- binary - an input binary that goes into the image
92bf7fd50bSSimon Glass
93bf7fd50bSSimon Glass
94bf7fd50bSSimon GlassRelationship to FIT
95bf7fd50bSSimon Glass-------------------
96bf7fd50bSSimon Glass
97bf7fd50bSSimon GlassFIT is U-Boot's official image format. It supports multiple binaries with
98bf7fd50bSSimon Glassload / execution addresses, compression. It also supports verification
99bf7fd50bSSimon Glassthrough hashing and RSA signatures.
100bf7fd50bSSimon Glass
101bf7fd50bSSimon GlassFIT was originally designed to support booting a Linux kernel (with an
102bf7fd50bSSimon Glassoptional ramdisk) and device tree chosen from various options in the FIT.
103bf7fd50bSSimon GlassNow that U-Boot supports configuration via device tree, it is possible to
104bf7fd50bSSimon Glassload U-Boot from a FIT, with the device tree chosen by SPL.
105bf7fd50bSSimon Glass
106bf7fd50bSSimon GlassBinman considers FIT to be one of the binaries it can place in the image.
107bf7fd50bSSimon Glass
108bf7fd50bSSimon GlassWhere possible it is best to put as much as possible in the FIT, with binman
109bf7fd50bSSimon Glassused to deal with cases not covered by FIT. Examples include initial
110bf7fd50bSSimon Glassexecution (since FIT itself does not have an executable header) and dealing
111bf7fd50bSSimon Glasswith device boundaries, such as the read-only/read-write separation in SPI
112bf7fd50bSSimon Glassflash.
113bf7fd50bSSimon Glass
114bf7fd50bSSimon GlassFor U-Boot, binman should not be used to create ad-hoc images in place of
115bf7fd50bSSimon GlassFIT.
116bf7fd50bSSimon Glass
117bf7fd50bSSimon Glass
118bf7fd50bSSimon GlassRelationship to mkimage
119bf7fd50bSSimon Glass-----------------------
120bf7fd50bSSimon Glass
121bf7fd50bSSimon GlassThe mkimage tool provides a means to create a FIT. Traditionally it has
122bf7fd50bSSimon Glassneeded an image description file: a device tree, like binman, but in a
123bf7fd50bSSimon Glassdifferent format. More recently it has started to support a '-f auto' mode
124bf7fd50bSSimon Glasswhich can generate that automatically.
125bf7fd50bSSimon Glass
126bf7fd50bSSimon GlassMore relevant to binman, mkimage also permits creation of many SoC-specific
127bf7fd50bSSimon Glassimage types. These can be listed by running 'mkimage -T list'. Examples
128bf7fd50bSSimon Glassinclude 'rksd', the Rockchip SD/MMC boot format. The mkimage tool is often
129bf7fd50bSSimon Glasscalled from the U-Boot build system for this reason.
130bf7fd50bSSimon Glass
131bf7fd50bSSimon GlassBinman considers the output files created by mkimage to be binary blobs
132bf7fd50bSSimon Glasswhich it can place in an image. Binman does not replace the mkimage tool or
133bf7fd50bSSimon Glassthis purpose. It would be possible in some situtions to create a new entry
134bf7fd50bSSimon Glasstype for the images in mkimage, but this would not add functionality. It
135bf7fd50bSSimon Glassseems better to use the mkiamge tool to generate binaries and avoid blurring
136bf7fd50bSSimon Glassthe boundaries between building input files (mkimage) and packaging then
137bf7fd50bSSimon Glassinto a final image (binman).
138bf7fd50bSSimon Glass
139bf7fd50bSSimon Glass
140bf7fd50bSSimon GlassExample use of binman in U-Boot
141bf7fd50bSSimon Glass-------------------------------
142bf7fd50bSSimon Glass
143bf7fd50bSSimon GlassBinman aims to replace some of the ad-hoc image creation in the U-Boot
144bf7fd50bSSimon Glassbuild system.
145bf7fd50bSSimon Glass
146bf7fd50bSSimon GlassConsider sunxi. It has the following steps:
147bf7fd50bSSimon Glass
148bf7fd50bSSimon Glass1. It uses a custom mksunxiboot tool to build an SPL image called
149bf7fd50bSSimon Glasssunxi-spl.bin. This should probably move into mkimage.
150bf7fd50bSSimon Glass
151bf7fd50bSSimon Glass2. It uses mkimage to package U-Boot into a legacy image file (so that it can
152bf7fd50bSSimon Glasshold the load and execution address) called u-boot.img.
153bf7fd50bSSimon Glass
154bf7fd50bSSimon Glass3. It builds a final output image called u-boot-sunxi-with-spl.bin which
155bf7fd50bSSimon Glassconsists of sunxi-spl.bin, some padding and u-boot.img.
156bf7fd50bSSimon Glass
157bf7fd50bSSimon GlassBinman is intended to replace the last step. The U-Boot build system builds
158bf7fd50bSSimon Glassu-boot.bin and sunxi-spl.bin. Binman can then take over creation of
159bf7fd50bSSimon Glasssunxi-spl.bin (by calling mksunxiboot, or hopefully one day mkimage). In any
160bf7fd50bSSimon Glasscase, it would then create the image from the component parts.
161bf7fd50bSSimon Glass
162bf7fd50bSSimon GlassThis simplifies the U-Boot Makefile somewhat, since various pieces of logic
163bf7fd50bSSimon Glasscan be replaced by a call to binman.
164bf7fd50bSSimon Glass
165bf7fd50bSSimon Glass
166bf7fd50bSSimon GlassExample use of binman for x86
167bf7fd50bSSimon Glass-----------------------------
168bf7fd50bSSimon Glass
169bf7fd50bSSimon GlassIn most cases x86 images have a lot of binary blobs, 'black-box' code
170bf7fd50bSSimon Glassprovided by Intel which must be run for the platform to work. Typically
171bf7fd50bSSimon Glassthese blobs are not relocatable and must be placed at fixed areas in the
172bf7fd50bSSimon Glassfirmare image.
173bf7fd50bSSimon Glass
174bf7fd50bSSimon GlassCurrently this is handled by ifdtool, which places microcode, FSP, MRC, VGA
175bf7fd50bSSimon GlassBIOS, reference code and Intel ME binaries into a u-boot.rom file.
176bf7fd50bSSimon Glass
177bf7fd50bSSimon GlassBinman is intended to replace all of this, with ifdtool left to handle only
178bf7fd50bSSimon Glassthe configuration of the Intel-format descriptor.
179bf7fd50bSSimon Glass
180bf7fd50bSSimon Glass
181bf7fd50bSSimon GlassRunning binman
182bf7fd50bSSimon Glass--------------
183bf7fd50bSSimon Glass
184bf7fd50bSSimon GlassType:
185bf7fd50bSSimon Glass
186bf7fd50bSSimon Glass	binman -b <board_name>
187bf7fd50bSSimon Glass
188bf7fd50bSSimon Glassto build an image for a board. The board name is the same name used when
189bf7fd50bSSimon Glassconfiguring U-Boot (e.g. for sandbox_defconfig the board name is 'sandbox').
190bf7fd50bSSimon GlassBinman assumes that the input files for the build are in ../b/<board_name>.
191bf7fd50bSSimon Glass
192bf7fd50bSSimon GlassOr you can specify this explicitly:
193bf7fd50bSSimon Glass
194bf7fd50bSSimon Glass	binman -I <build_path>
195bf7fd50bSSimon Glass
196bf7fd50bSSimon Glasswhere <build_path> is the build directory containing the output of the U-Boot
197bf7fd50bSSimon Glassbuild.
198bf7fd50bSSimon Glass
199bf7fd50bSSimon Glass(Future work will make this more configurable)
200bf7fd50bSSimon Glass
201bf7fd50bSSimon GlassIn either case, binman picks up the device tree file (u-boot.dtb) and looks
202bf7fd50bSSimon Glassfor its instructions in the 'binman' node.
203bf7fd50bSSimon Glass
204bf7fd50bSSimon GlassBinman has a few other options which you can see by running 'binman -h'.
205bf7fd50bSSimon Glass
206bf7fd50bSSimon Glass
2079c0a8b1fSSimon GlassEnabling binman for a board
2089c0a8b1fSSimon Glass---------------------------
2099c0a8b1fSSimon Glass
2109c0a8b1fSSimon GlassAt present binman is invoked from a rule in the main Makefile. Typically you
2119c0a8b1fSSimon Glasswill have a rule like:
2129c0a8b1fSSimon Glass
2139c0a8b1fSSimon Glassifneq ($(CONFIG_ARCH_<something>),)
2149c0a8b1fSSimon Glassu-boot-<your_suffix>.bin: <input_file_1> <input_file_2> checkbinman FORCE
2159c0a8b1fSSimon Glass	$(call if_changed,binman)
2169c0a8b1fSSimon Glassendif
2179c0a8b1fSSimon Glass
2189c0a8b1fSSimon GlassThis assumes that u-boot-<your_suffix>.bin is a target, and is the final file
2199c0a8b1fSSimon Glassthat you need to produce. You can make it a target by adding it to ALL-y
2209c0a8b1fSSimon Glasseither in the main Makefile or in a config.mk file in your arch subdirectory.
2219c0a8b1fSSimon Glass
2229c0a8b1fSSimon GlassOnce binman is executed it will pick up its instructions from a device-tree
2239c0a8b1fSSimon Glassfile, typically <soc>-u-boot.dtsi, where <soc> is your CONFIG_SYS_SOC value.
2249c0a8b1fSSimon GlassYou can use other, more specific CONFIG options - see 'Automatic .dtsi
2259c0a8b1fSSimon Glassinclusion' below.
2269c0a8b1fSSimon Glass
2279c0a8b1fSSimon Glass
228bf7fd50bSSimon GlassImage description format
229bf7fd50bSSimon Glass------------------------
230bf7fd50bSSimon Glass
231bf7fd50bSSimon GlassThe binman node is called 'binman'. An example image description is shown
232bf7fd50bSSimon Glassbelow:
233bf7fd50bSSimon Glass
234bf7fd50bSSimon Glass	binman {
235bf7fd50bSSimon Glass		filename = "u-boot-sunxi-with-spl.bin";
236bf7fd50bSSimon Glass		pad-byte = <0xff>;
237bf7fd50bSSimon Glass		blob {
238bf7fd50bSSimon Glass			filename = "spl/sunxi-spl.bin";
239bf7fd50bSSimon Glass		};
240bf7fd50bSSimon Glass		u-boot {
241bf7fd50bSSimon Glass			pos = <CONFIG_SPL_PAD_TO>;
242bf7fd50bSSimon Glass		};
243bf7fd50bSSimon Glass	};
244bf7fd50bSSimon Glass
245bf7fd50bSSimon Glass
246bf7fd50bSSimon GlassThis requests binman to create an image file called u-boot-sunxi-with-spl.bin
247bf7fd50bSSimon Glassconsisting of a specially formatted SPL (spl/sunxi-spl.bin, built by the
248bf7fd50bSSimon Glassnormal U-Boot Makefile), some 0xff padding, and a U-Boot legacy image. The
249bf7fd50bSSimon Glasspadding comes from the fact that the second binary is placed at
250bf7fd50bSSimon GlassCONFIG_SPL_PAD_TO. If that line were omitted then the U-Boot binary would
251bf7fd50bSSimon Glassimmediately follow the SPL binary.
252bf7fd50bSSimon Glass
253bf7fd50bSSimon GlassThe binman node describes an image. The sub-nodes describe entries in the
254bf7fd50bSSimon Glassimage. Each entry represents a region within the overall image. The name of
255bf7fd50bSSimon Glassthe entry (blob, u-boot) tells binman what to put there. For 'blob' we must
256bf7fd50bSSimon Glassprovide a filename. For 'u-boot', binman knows that this means 'u-boot.bin'.
257bf7fd50bSSimon Glass
258bf7fd50bSSimon GlassEntries are normally placed into the image sequentially, one after the other.
259bf7fd50bSSimon GlassThe image size is the total size of all entries. As you can see, you can
260bf7fd50bSSimon Glassspecify the start position of an entry using the 'pos' property.
261bf7fd50bSSimon Glass
262bf7fd50bSSimon GlassNote that due to a device tree requirement, all entries must have a unique
263bf7fd50bSSimon Glassname. If you want to put the same binary in the image multiple times, you can
264bf7fd50bSSimon Glassuse any unique name, with the 'type' property providing the type.
265bf7fd50bSSimon Glass
266bf7fd50bSSimon GlassThe attributes supported for entries are described below.
267bf7fd50bSSimon Glass
268bf7fd50bSSimon Glasspos:
269bf7fd50bSSimon Glass	This sets the position of an entry within the image. The first byte
270bf7fd50bSSimon Glass	of the image is normally at position 0. If 'pos' is not provided,
271bf7fd50bSSimon Glass	binman sets it to the end of the previous region, or the start of
272bf7fd50bSSimon Glass	the image's entry area (normally 0) if there is no previous region.
273bf7fd50bSSimon Glass
274bf7fd50bSSimon Glassalign:
275bf7fd50bSSimon Glass	This sets the alignment of the entry. The entry position is adjusted
276bf7fd50bSSimon Glass	so that the entry starts on an aligned boundary within the image. For
277bf7fd50bSSimon Glass	example 'align = <16>' means that the entry will start on a 16-byte
278bf7fd50bSSimon Glass	boundary. Alignment shold be a power of 2. If 'align' is not
279bf7fd50bSSimon Glass	provided, no alignment is performed.
280bf7fd50bSSimon Glass
281bf7fd50bSSimon Glasssize:
282bf7fd50bSSimon Glass	This sets the size of the entry. The contents will be padded out to
283bf7fd50bSSimon Glass	this size. If this is not provided, it will be set to the size of the
284bf7fd50bSSimon Glass	contents.
285bf7fd50bSSimon Glass
286bf7fd50bSSimon Glasspad-before:
287bf7fd50bSSimon Glass	Padding before the contents of the entry. Normally this is 0, meaning
288bf7fd50bSSimon Glass	that the contents start at the beginning of the entry. This can be
289bf7fd50bSSimon Glass	offset the entry contents a little. Defaults to 0.
290bf7fd50bSSimon Glass
291bf7fd50bSSimon Glasspad-after:
292bf7fd50bSSimon Glass	Padding after the contents of the entry. Normally this is 0, meaning
293bf7fd50bSSimon Glass	that the entry ends at the last byte of content (unless adjusted by
294bf7fd50bSSimon Glass	other properties). This allows room to be created in the image for
295bf7fd50bSSimon Glass	this entry to expand later. Defaults to 0.
296bf7fd50bSSimon Glass
297bf7fd50bSSimon Glassalign-size:
298bf7fd50bSSimon Glass	This sets the alignment of the entry size. For example, to ensure
299bf7fd50bSSimon Glass	that the size of an entry is a multiple of 64 bytes, set this to 64.
300bf7fd50bSSimon Glass	If 'align-size' is not provided, no alignment is performed.
301bf7fd50bSSimon Glass
302bf7fd50bSSimon Glassalign-end:
303bf7fd50bSSimon Glass	This sets the alignment of the end of an entry. Some entries require
304bf7fd50bSSimon Glass	that they end on an alignment boundary, regardless of where they
305844e5b20SSimon Glass	start. This does not move the start of the entry, so the contents of
306844e5b20SSimon Glass	the entry will still start at the beginning. But there may be padding
307844e5b20SSimon Glass	at the end. If 'align-end' is not provided, no alignment is performed.
308bf7fd50bSSimon Glass
309bf7fd50bSSimon Glassfilename:
310bf7fd50bSSimon Glass	For 'blob' types this provides the filename containing the binary to
311bf7fd50bSSimon Glass	put into the entry. If binman knows about the entry type (like
312bf7fd50bSSimon Glass	u-boot-bin), then there is no need to specify this.
313bf7fd50bSSimon Glass
314bf7fd50bSSimon Glasstype:
315bf7fd50bSSimon Glass	Sets the type of an entry. This defaults to the entry name, but it is
316bf7fd50bSSimon Glass	possible to use any name, and then add (for example) 'type = "u-boot"'
317bf7fd50bSSimon Glass	to specify the type.
318bf7fd50bSSimon Glass
319258fb0e6SSimon Glasspos-unset:
320258fb0e6SSimon Glass	Indicates that the position of this entry should not be set by placing
321258fb0e6SSimon Glass	it immediately after the entry before. Instead, is set by another
322258fb0e6SSimon Glass	entry which knows where this entry should go. When this boolean
323258fb0e6SSimon Glass	property is present, binman will give an error if another entry does
324258fb0e6SSimon Glass	not set the position (with the GetPositions() method).
325258fb0e6SSimon Glass
326bf7fd50bSSimon Glass
327bf7fd50bSSimon GlassThe attributes supported for images are described below. Several are similar
328bf7fd50bSSimon Glassto those for entries.
329bf7fd50bSSimon Glass
330bf7fd50bSSimon Glasssize:
331bf7fd50bSSimon Glass	Sets the image size in bytes, for example 'size = <0x100000>' for a
332bf7fd50bSSimon Glass	1MB image.
333bf7fd50bSSimon Glass
334bf7fd50bSSimon Glassalign-size:
335bf7fd50bSSimon Glass	This sets the alignment of the image size. For example, to ensure
336bf7fd50bSSimon Glass	that the image ends on a 512-byte boundary, use 'align-size = <512>'.
337bf7fd50bSSimon Glass	If 'align-size' is not provided, no alignment is performed.
338bf7fd50bSSimon Glass
339bf7fd50bSSimon Glasspad-before:
340bf7fd50bSSimon Glass	This sets the padding before the image entries. The first entry will
341bf7fd50bSSimon Glass	be positionad after the padding. This defaults to 0.
342bf7fd50bSSimon Glass
343bf7fd50bSSimon Glasspad-after:
344bf7fd50bSSimon Glass	This sets the padding after the image entries. The padding will be
345bf7fd50bSSimon Glass	placed after the last entry. This defaults to 0.
346bf7fd50bSSimon Glass
347bf7fd50bSSimon Glasspad-byte:
348bf7fd50bSSimon Glass	This specifies the pad byte to use when padding in the image. It
349bf7fd50bSSimon Glass	defaults to 0. To use 0xff, you would add 'pad-byte = <0xff>'.
350bf7fd50bSSimon Glass
351bf7fd50bSSimon Glassfilename:
352bf7fd50bSSimon Glass	This specifies the image filename. It defaults to 'image.bin'.
353bf7fd50bSSimon Glass
354bf7fd50bSSimon Glasssort-by-pos:
355bf7fd50bSSimon Glass	This causes binman to reorder the entries as needed to make sure they
356bf7fd50bSSimon Glass	are in increasing positional order. This can be used when your entry
357bf7fd50bSSimon Glass	order may not match the positional order. A common situation is where
358bf7fd50bSSimon Glass	the 'pos' properties are set by CONFIG options, so their ordering is
359bf7fd50bSSimon Glass	not known a priori.
360bf7fd50bSSimon Glass
361bf7fd50bSSimon Glass	This is a boolean property so needs no value. To enable it, add a
362bf7fd50bSSimon Glass	line 'sort-by-pos;' to your description.
363bf7fd50bSSimon Glass
364bf7fd50bSSimon Glassmultiple-images:
365bf7fd50bSSimon Glass	Normally only a single image is generated. To create more than one
366bf7fd50bSSimon Glass	image, put this property in the binman node. For example, this will
367bf7fd50bSSimon Glass	create image1.bin containing u-boot.bin, and image2.bin containing
368bf7fd50bSSimon Glass	both spl/u-boot-spl.bin and u-boot.bin:
369bf7fd50bSSimon Glass
370bf7fd50bSSimon Glass	binman {
371bf7fd50bSSimon Glass		multiple-images;
372bf7fd50bSSimon Glass		image1 {
373bf7fd50bSSimon Glass			u-boot {
374bf7fd50bSSimon Glass			};
375bf7fd50bSSimon Glass		};
376bf7fd50bSSimon Glass
377bf7fd50bSSimon Glass		image2 {
378bf7fd50bSSimon Glass			spl {
379bf7fd50bSSimon Glass			};
380bf7fd50bSSimon Glass			u-boot {
381bf7fd50bSSimon Glass			};
382bf7fd50bSSimon Glass		};
383bf7fd50bSSimon Glass	};
384bf7fd50bSSimon Glass
385bf7fd50bSSimon Glassend-at-4gb:
386bf7fd50bSSimon Glass	For x86 machines the ROM positions start just before 4GB and extend
387bf7fd50bSSimon Glass	up so that the image finished at the 4GB boundary. This boolean
388bf7fd50bSSimon Glass	option can be enabled to support this. The image size must be
389bf7fd50bSSimon Glass	provided so that binman knows when the image should start. For an
390bf7fd50bSSimon Glass	8MB ROM, the position of the first entry would be 0xfff80000 with
391bf7fd50bSSimon Glass	this option, instead of 0 without this option.
392bf7fd50bSSimon Glass
393bf7fd50bSSimon Glass
394bf7fd50bSSimon GlassExamples of the above options can be found in the tests. See the
395bf7fd50bSSimon Glasstools/binman/test directory.
396bf7fd50bSSimon Glass
397dd57c13bSSimon GlassIt is possible to have the same binary appear multiple times in the image,
398dd57c13bSSimon Glasseither by using a unit number suffix (u-boot@0, u-boot@1) or by using a
399dd57c13bSSimon Glassdifferent name for each and specifying the type with the 'type' attribute.
400dd57c13bSSimon Glass
401bf7fd50bSSimon Glass
4021854695bSSimon GlassSections and hiearchical images
4031854695bSSimon Glass-------------------------------
4041854695bSSimon Glass
4051854695bSSimon GlassSometimes it is convenient to split an image into several pieces, each of which
4061854695bSSimon Glasscontains its own set of binaries. An example is a flash device where part of
4071854695bSSimon Glassthe image is read-only and part is read-write. We can set up sections for each
4081854695bSSimon Glassof these, and place binaries in them independently. The image is still produced
4091854695bSSimon Glassas a single output file.
4101854695bSSimon Glass
4111854695bSSimon GlassThis feature provides a way of creating hierarchical images. For example here
4127ae5f315SSimon Glassis an example image with two copies of U-Boot. One is read-only (ro), intended
4137ae5f315SSimon Glassto be written only in the factory. Another is read-write (rw), so that it can be
4141854695bSSimon Glassupgraded in the field. The sizes are fixed so that the ro/rw boundary is known
4151854695bSSimon Glassand can be programmed:
4161854695bSSimon Glass
4171854695bSSimon Glass	binman {
4181854695bSSimon Glass		section@0 {
4191854695bSSimon Glass			read-only;
420c8d48efbSSimon Glass			name-prefix = "ro-";
4211854695bSSimon Glass			size = <0x100000>;
4221854695bSSimon Glass			u-boot {
4231854695bSSimon Glass			};
4241854695bSSimon Glass		};
4251854695bSSimon Glass		section@1 {
426c8d48efbSSimon Glass			name-prefix = "rw-";
4271854695bSSimon Glass			size = <0x100000>;
4281854695bSSimon Glass			u-boot {
4291854695bSSimon Glass			};
4301854695bSSimon Glass		};
4311854695bSSimon Glass	};
4321854695bSSimon Glass
4331854695bSSimon GlassThis image could be placed into a SPI flash chip, with the protection boundary
4341854695bSSimon Glassset at 1MB.
4351854695bSSimon Glass
4361854695bSSimon GlassA few special properties are provided for sections:
4371854695bSSimon Glass
4381854695bSSimon Glassread-only:
4391854695bSSimon Glass	Indicates that this section is read-only. This has no impact on binman's
4401854695bSSimon Glass	operation, but his property can be read at run time.
4411854695bSSimon Glass
442c8d48efbSSimon Glassname-prefix:
443c8d48efbSSimon Glass	This string is prepended to all the names of the binaries in the
444c8d48efbSSimon Glass	section. In the example above, the 'u-boot' binaries which actually be
445c8d48efbSSimon Glass	renamed to 'ro-u-boot' and 'rw-u-boot'. This can be useful to
446c8d48efbSSimon Glass	distinguish binaries with otherwise identical names.
447c8d48efbSSimon Glass
4481854695bSSimon Glass
449e0ff8551SSimon GlassSpecial properties
450e0ff8551SSimon Glass------------------
451e0ff8551SSimon Glass
452e0ff8551SSimon GlassSome entries support special properties, documented here:
453e0ff8551SSimon Glass
454e0ff8551SSimon Glassu-boot-with-ucode-ptr:
455e0ff8551SSimon Glass	optional-ucode: boolean property to make microcode optional. If the
456e0ff8551SSimon Glass		u-boot.bin image does not include microcode, no error will
457e0ff8551SSimon Glass		be generated.
458e0ff8551SSimon Glass
459e0ff8551SSimon Glass
460bf7fd50bSSimon GlassOrder of image creation
461bf7fd50bSSimon Glass-----------------------
462bf7fd50bSSimon Glass
463bf7fd50bSSimon GlassImage creation proceeds in the following order, for each entry in the image.
464bf7fd50bSSimon Glass
465bf7fd50bSSimon Glass1. GetEntryContents() - the contents of each entry are obtained, normally by
466bf7fd50bSSimon Glassreading from a file. This calls the Entry.ObtainContents() to read the
467bf7fd50bSSimon Glasscontents. The default version of Entry.ObtainContents() calls
468bf7fd50bSSimon GlassEntry.GetDefaultFilename() and then reads that file. So a common mechanism
469bf7fd50bSSimon Glassto select a file to read is to override that function in the subclass. The
470bf7fd50bSSimon Glassfunctions must return True when they have read the contents. Binman will
471bf7fd50bSSimon Glassretry calling the functions a few times if False is returned, allowing
472bf7fd50bSSimon Glassdependencies between the contents of different entries.
473bf7fd50bSSimon Glass
474bf7fd50bSSimon Glass2. GetEntryPositions() - calls Entry.GetPositions() for each entry. This can
475bf7fd50bSSimon Glassreturn a dict containing entries that need updating. The key should be the
476bf7fd50bSSimon Glassentry name and the value is a tuple (pos, size). This allows an entry to
477bf7fd50bSSimon Glassprovide the position and size for other entries. The default implementation
478bf7fd50bSSimon Glassof GetEntryPositions() returns {}.
479bf7fd50bSSimon Glass
480bf7fd50bSSimon Glass3. PackEntries() - calls Entry.Pack() which figures out the position and
481bf7fd50bSSimon Glasssize of an entry. The 'current' image position is passed in, and the function
482bf7fd50bSSimon Glassreturns the position immediately after the entry being packed. The default
483bf7fd50bSSimon Glassimplementation of Pack() is usually sufficient.
484bf7fd50bSSimon Glass
485bf7fd50bSSimon Glass4. CheckSize() - checks that the contents of all the entries fits within
486bf7fd50bSSimon Glassthe image size. If the image does not have a defined size, the size is set
487bf7fd50bSSimon Glasslarge enough to hold all the entries.
488bf7fd50bSSimon Glass
489bf7fd50bSSimon Glass5. CheckEntries() - checks that the entries do not overlap, nor extend
490bf7fd50bSSimon Glassoutside the image.
491bf7fd50bSSimon Glass
492bf7fd50bSSimon Glass6. ProcessEntryContents() - this calls Entry.ProcessContents() on each entry.
493bf7fd50bSSimon GlassThe default implementatoin does nothing. This can be overriden to adjust the
494bf7fd50bSSimon Glasscontents of an entry in some way. For example, it would be possible to create
495bf7fd50bSSimon Glassan entry containing a hash of the contents of some other entries. At this
496bf7fd50bSSimon Glassstage the position and size of entries should not be adjusted.
497bf7fd50bSSimon Glass
498*0a4357c4SSimon Glass7. WriteSymbols() - write the value of symbols into the U-Boot SPL binary.
499*0a4357c4SSimon GlassSee 'Access to binman entry positions at run time' below for a description of
500*0a4357c4SSimon Glasswhat happens in this stage.
50139c1502cSSimon Glass
502*0a4357c4SSimon Glass8. BuildImage() - builds the image and writes it to a file. This is the final
503bf7fd50bSSimon Glassstep.
504bf7fd50bSSimon Glass
505bf7fd50bSSimon Glass
5066d427c6bSSimon GlassAutomatic .dtsi inclusion
5076d427c6bSSimon Glass-------------------------
5086d427c6bSSimon Glass
5096d427c6bSSimon GlassIt is sometimes inconvenient to add a 'binman' node to the .dts file for each
5106d427c6bSSimon Glassboard. This can be done by using #include to bring in a common file. Another
5116d427c6bSSimon Glassapproach supported by the U-Boot build system is to automatically include
5126d427c6bSSimon Glassa common header. You can then put the binman node (and anything else that is
5136d427c6bSSimon Glassspecific to U-Boot, such as u-boot,dm-pre-reloc properies) in that header
5146d427c6bSSimon Glassfile.
5156d427c6bSSimon Glass
5166d427c6bSSimon GlassBinman will search for the following files in arch/<arch>/dts:
5176d427c6bSSimon Glass
5186d427c6bSSimon Glass   <dts>-u-boot.dtsi where <dts> is the base name of the .dts file
5196d427c6bSSimon Glass   <CONFIG_SYS_SOC>-u-boot.dtsi
5206d427c6bSSimon Glass   <CONFIG_SYS_CPU>-u-boot.dtsi
5216d427c6bSSimon Glass   <CONFIG_SYS_VENDOR>-u-boot.dtsi
5226d427c6bSSimon Glass   u-boot.dtsi
5236d427c6bSSimon Glass
5246d427c6bSSimon GlassU-Boot will only use the first one that it finds. If you need to include a
5256d427c6bSSimon Glassmore general file you can do that from the more specific file using #include.
5266d427c6bSSimon GlassIf you are having trouble figuring out what is going on, you can uncomment
5276d427c6bSSimon Glassthe 'warning' line in scripts/Makefile.lib to see what it has found:
5286d427c6bSSimon Glass
5296d427c6bSSimon Glass   # Uncomment for debugging
530511fd0b2SSimon Glass   # This shows all the files that were considered and the one that we chose.
531511fd0b2SSimon Glass   # u_boot_dtsi_options_debug = $(u_boot_dtsi_options_raw)
5326d427c6bSSimon Glass
5336d427c6bSSimon Glass
53439c1502cSSimon GlassAccess to binman entry positions at run time
53539c1502cSSimon Glass--------------------------------------------
53639c1502cSSimon Glass
53739c1502cSSimon GlassBinman assembles images and determines where each entry is placed in the image.
53839c1502cSSimon GlassThis information may be useful to U-Boot at run time. For example, in SPL it
53939c1502cSSimon Glassis useful to be able to find the location of U-Boot so that it can be executed
54039c1502cSSimon Glasswhen SPL is finished.
54139c1502cSSimon Glass
54239c1502cSSimon GlassBinman allows you to declare symbols in the SPL image which are filled in
54339c1502cSSimon Glasswith their correct values during the build. For example:
54439c1502cSSimon Glass
54539c1502cSSimon Glass    binman_sym_declare(ulong, u_boot_any, pos);
54639c1502cSSimon Glass
54739c1502cSSimon Glassdeclares a ulong value which will be assigned to the position of any U-Boot
54839c1502cSSimon Glassimage (u-boot.bin, u-boot.img, u-boot-nodtb.bin) that is present in the image.
54939c1502cSSimon GlassYou can access this value with something like:
55039c1502cSSimon Glass
55139c1502cSSimon Glass    ulong u_boot_pos = binman_sym(ulong, u_boot_any, pos);
55239c1502cSSimon Glass
55339c1502cSSimon GlassThus u_boot_pos will be set to the position of U-Boot in memory, assuming that
55439c1502cSSimon Glassthe whole image has been loaded, or is available in flash. You can then jump to
55539c1502cSSimon Glassthat address to start U-Boot.
55639c1502cSSimon Glass
55739c1502cSSimon GlassAt present this feature is only supported in SPL. In principle it is possible
55839c1502cSSimon Glassto fill in such symbols in U-Boot proper, as well.
55939c1502cSSimon Glass
56039c1502cSSimon Glass
5613b0c3821SSimon GlassMap files
5623b0c3821SSimon Glass---------
5633b0c3821SSimon Glass
5643b0c3821SSimon GlassThe -m option causes binman to output a .map file for each image that it
5653b0c3821SSimon Glassgenerates. This shows the position and size of each entry. For example:
5663b0c3821SSimon Glass
5673b0c3821SSimon Glass    Position      Size  Name
5683b0c3821SSimon Glass    00000000  00000010  section@0
5693b0c3821SSimon Glass     00000000  00000004  u-boot
5703b0c3821SSimon Glass    00000010  00000010  section@1
5713b0c3821SSimon Glass     00000000  00000004  u-boot
5723b0c3821SSimon Glass
5733b0c3821SSimon GlassThis shows a hierarchical image with two sections, each with a single entry. The
5743b0c3821SSimon Glasspositions of the sections are absolute hex byte offsets within the image. The
5753b0c3821SSimon Glasspositions of the entries are relative to their respective sections. The size of
5763b0c3821SSimon Glasseach entry is also shown, in bytes (hex). The indentation shows the entries
5773b0c3821SSimon Glassnested inside their sections.
5783b0c3821SSimon Glass
5793b0c3821SSimon Glass
5806d427c6bSSimon GlassCode coverage
5816d427c6bSSimon Glass-------------
5826d427c6bSSimon Glass
5836d427c6bSSimon GlassBinman is a critical tool and is designed to be very testable. Entry
5846d427c6bSSimon Glassimplementations target 100% test coverage. Run 'binman -T' to check this.
5856d427c6bSSimon Glass
5866d427c6bSSimon GlassTo enable Python test coverage on Debian-type distributions (e.g. Ubuntu):
5876d427c6bSSimon Glass
58816d836cdSTom Rini   $ sudo apt-get install python-coverage python-pytest
5896d427c6bSSimon Glass
5906d427c6bSSimon Glass
591bf7fd50bSSimon GlassAdvanced Features / Technical docs
592bf7fd50bSSimon Glass----------------------------------
593bf7fd50bSSimon Glass
594bf7fd50bSSimon GlassThe behaviour of entries is defined by the Entry class. All other entries are
595bf7fd50bSSimon Glassa subclass of this. An important subclass is Entry_blob which takes binary
596bf7fd50bSSimon Glassdata from a file and places it in the entry. In fact most entry types are
597bf7fd50bSSimon Glasssubclasses of Entry_blob.
598bf7fd50bSSimon Glass
599bf7fd50bSSimon GlassEach entry type is a separate file in the tools/binman/etype directory. Each
600bf7fd50bSSimon Glassfile contains a class called Entry_<type> where <type> is the entry type.
601bf7fd50bSSimon GlassNew entry types can be supported by adding new files in that directory.
602bf7fd50bSSimon GlassThese will automatically be detected by binman when needed.
603bf7fd50bSSimon Glass
604bf7fd50bSSimon GlassEntry properties are documented in entry.py. The entry subclasses are free
605bf7fd50bSSimon Glassto change the values of properties to support special behaviour. For example,
606bf7fd50bSSimon Glasswhen Entry_blob loads a file, it sets content_size to the size of the file.
607bf7fd50bSSimon GlassEntry classes can adjust other entries. For example, an entry that knows
608bf7fd50bSSimon Glasswhere other entries should be positioned can set up those entries' positions
609bf7fd50bSSimon Glassso they don't need to be set in the binman decription. It can also adjust
610bf7fd50bSSimon Glassentry contents.
611bf7fd50bSSimon Glass
612bf7fd50bSSimon GlassMost of the time such essoteric behaviour is not needed, but it can be
613bf7fd50bSSimon Glassessential for complex images.
614bf7fd50bSSimon Glass
6153ed0de31SSimon GlassIf you need to specify a particular device-tree compiler to use, you can define
6163ed0de31SSimon Glassthe DTC environment variable. This can be useful when the system dtc is too
6173ed0de31SSimon Glassold.
6183ed0de31SSimon Glass
619bf7fd50bSSimon Glass
620bf7fd50bSSimon GlassHistory / Credits
621bf7fd50bSSimon Glass-----------------
622bf7fd50bSSimon Glass
623bf7fd50bSSimon GlassBinman takes a lot of inspiration from a Chrome OS tool called
624bf7fd50bSSimon Glass'cros_bundle_firmware', which I wrote some years ago. That tool was based on
625bf7fd50bSSimon Glassa reasonably simple and sound design but has expanded greatly over the
626bf7fd50bSSimon Glassyears. In particular its handling of x86 images is convoluted.
627bf7fd50bSSimon Glass
6287ae5f315SSimon GlassQuite a few lessons have been learned which are hopefully applied here.
629bf7fd50bSSimon Glass
630bf7fd50bSSimon Glass
631bf7fd50bSSimon GlassDesign notes
632bf7fd50bSSimon Glass------------
633bf7fd50bSSimon Glass
634bf7fd50bSSimon GlassOn the face of it, a tool to create firmware images should be fairly simple:
635bf7fd50bSSimon Glassjust find all the input binaries and place them at the right place in the
636bf7fd50bSSimon Glassimage. The difficulty comes from the wide variety of input types (simple
637bf7fd50bSSimon Glassflat binaries containing code, packaged data with various headers), packing
638bf7fd50bSSimon Glassrequirments (alignment, spacing, device boundaries) and other required
639bf7fd50bSSimon Glassfeatures such as hierarchical images.
640bf7fd50bSSimon Glass
641bf7fd50bSSimon GlassThe design challenge is to make it easy to create simple images, while
642bf7fd50bSSimon Glassallowing the more complex cases to be supported. For example, for most
643bf7fd50bSSimon Glassimages we don't much care exactly where each binary ends up, so we should
644bf7fd50bSSimon Glassnot have to specify that unnecessarily.
645bf7fd50bSSimon Glass
646bf7fd50bSSimon GlassNew entry types should aim to provide simple usage where possible. If new
647bf7fd50bSSimon Glasscore features are needed, they can be added in the Entry base class.
648bf7fd50bSSimon Glass
649bf7fd50bSSimon Glass
650bf7fd50bSSimon GlassTo do
651bf7fd50bSSimon Glass-----
652bf7fd50bSSimon Glass
653bf7fd50bSSimon GlassSome ideas:
654bf7fd50bSSimon Glass- Fill out the device tree to include the final position and size of each
65539c1502cSSimon Glass  entry (since the input file may not always specify these). See also
65639c1502cSSimon Glass  'Access to binman entry positions at run time' above
657bf7fd50bSSimon Glass- Use of-platdata to make the information available to code that is unable
658bf7fd50bSSimon Glass  to use device tree (such as a very small SPL image)
659bf7fd50bSSimon Glass- Allow easy building of images by specifying just the board name
660bf7fd50bSSimon Glass- Produce a full Python binding for libfdt (for upstream)
661bf7fd50bSSimon Glass- Add an option to decode an image into the constituent binaries
662bf7fd50bSSimon Glass- Support building an image for a board (-b) more completely, with a
663bf7fd50bSSimon Glass  configurable build directory
664bf7fd50bSSimon Glass- Consider making binman work with buildman, although if it is used in the
665bf7fd50bSSimon Glass  Makefile, this will be automatic
666bf7fd50bSSimon Glass
667bf7fd50bSSimon Glass--
668bf7fd50bSSimon GlassSimon Glass <sjg@chromium.org>
669bf7fd50bSSimon Glass7/7/2016
670