binman.rst 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  1. .. SPDX-License-Identifier: GPL-2.0+
  2. .. Copyright (c) 2016 Google, Inc
  3. Introduction
  4. ============
  5. Firmware often consists of several components which must be packaged together.
  6. For example, we may have SPL, U-Boot, a device tree and an environment area
  7. grouped together and placed in MMC flash. When the system starts, it must be
  8. able to find these pieces.
  9. Building firmware should be separate from packaging it. Many of the complexities
  10. of modern firmware build systems come from trying to do both at once. With
  11. binman, you build all the pieces that are needed, using whatever assortment of
  12. projects and build systems are needed, then use binman to stitch everything
  13. together.
  14. What it does
  15. ------------
  16. Binman reads your board's device tree and finds a node which describes the
  17. required image layout. It uses this to work out what to place where.
  18. Binman provides a mechanism for building images, from simple SPL + U-Boot
  19. combinations, to more complex arrangements with many parts. It also allows
  20. users to inspect images, extract and replace binaries within them, repacking if
  21. needed.
  22. Features
  23. --------
  24. Apart from basic padding, alignment and positioning features, Binman supports
  25. hierarchical images, compression, hashing and dealing with the binary blobs
  26. which are a sad trend in open-source firmware at present.
  27. Executable binaries can access the location of other binaries in an image by
  28. using special linker symbols (zero-overhead but somewhat limited) or by reading
  29. the devicetree description of the image.
  30. Binman is designed primarily for use with U-Boot and associated binaries such
  31. as ARM Trusted Firmware, but it is suitable for use with other projects, such
  32. as Zephyr. Binman also provides facilities useful in Chromium OS, such as CBFS,
  33. vblocks and and the like.
  34. Binman provides a way to process binaries before they are included, by adding a
  35. Python plug-in.
  36. Binman is intended for use with U-Boot but is designed to be general enough
  37. to be useful in other image-packaging situations.
  38. Motivation
  39. ----------
  40. As mentioned above, packaging of firmware is quite a different task from
  41. building the various parts. In many cases the various binaries which go into
  42. the image come from separate build systems. For example, ARM Trusted Firmware
  43. is used on ARMv8 devices but is not built in the U-Boot tree. If a Linux kernel
  44. is included in the firmware image, it is built elsewhere.
  45. It is of course possible to add more and more build rules to the U-Boot
  46. build system to cover these cases. It can shell out to other Makefiles and
  47. build scripts. But it seems better to create a clear divide between building
  48. software and packaging it.
  49. At present this is handled by manual instructions, different for each board,
  50. on how to create images that will boot. By turning these instructions into a
  51. standard format, we can support making valid images for any board without
  52. manual effort, lots of READMEs, etc.
  53. Benefits:
  54. - Each binary can have its own build system and tool chain without creating
  55. any dependencies between them
  56. - Avoids the need for a single-shot build: individual parts can be updated
  57. and brought in as needed
  58. - Provides for a standard image description available in the build and at
  59. run-time
  60. - SoC-specific image-signing tools can be accommodated
  61. - Avoids cluttering the U-Boot build system with image-building code
  62. - The image description is automatically available at run-time in U-Boot,
  63. SPL. It can be made available to other software also
  64. - The image description is easily readable (it's a text file in device-tree
  65. format) and permits flexible packing of binaries
  66. Terminology
  67. -----------
  68. Binman uses the following terms:
  69. - image - an output file containing a firmware image
  70. - binary - an input binary that goes into the image
  71. Relationship to FIT
  72. -------------------
  73. FIT is U-Boot's official image format. It supports multiple binaries with
  74. load / execution addresses, compression. It also supports verification
  75. through hashing and RSA signatures.
  76. FIT was originally designed to support booting a Linux kernel (with an
  77. optional ramdisk) and device tree chosen from various options in the FIT.
  78. Now that U-Boot supports configuration via device tree, it is possible to
  79. load U-Boot from a FIT, with the device tree chosen by SPL.
  80. Binman considers FIT to be one of the binaries it can place in the image.
  81. Where possible it is best to put as much as possible in the FIT, with binman
  82. used to deal with cases not covered by FIT. Examples include initial
  83. execution (since FIT itself does not have an executable header) and dealing
  84. with device boundaries, such as the read-only/read-write separation in SPI
  85. flash.
  86. For U-Boot, binman should not be used to create ad-hoc images in place of
  87. FIT.
  88. Relationship to mkimage
  89. -----------------------
  90. The mkimage tool provides a means to create a FIT. Traditionally it has
  91. needed an image description file: a device tree, like binman, but in a
  92. different format. More recently it has started to support a '-f auto' mode
  93. which can generate that automatically.
  94. More relevant to binman, mkimage also permits creation of many SoC-specific
  95. image types. These can be listed by running 'mkimage -T list'. Examples
  96. include 'rksd', the Rockchip SD/MMC boot format. The mkimage tool is often
  97. called from the U-Boot build system for this reason.
  98. Binman considers the output files created by mkimage to be binary blobs
  99. which it can place in an image. Binman does not replace the mkimage tool or
  100. this purpose. It would be possible in some situations to create a new entry
  101. type for the images in mkimage, but this would not add functionality. It
  102. seems better to use the mkimage tool to generate binaries and avoid blurring
  103. the boundaries between building input files (mkimage) and packaging then
  104. into a final image (binman).
  105. Using binman
  106. ============
  107. Example use of binman in U-Boot
  108. -------------------------------
  109. Binman aims to replace some of the ad-hoc image creation in the U-Boot
  110. build system.
  111. Consider sunxi. It has the following steps:
  112. #. It uses a custom mksunxiboot tool to build an SPL image called
  113. sunxi-spl.bin. This should probably move into mkimage.
  114. #. It uses mkimage to package U-Boot into a legacy image file (so that it can
  115. hold the load and execution address) called u-boot.img.
  116. #. It builds a final output image called u-boot-sunxi-with-spl.bin which
  117. consists of sunxi-spl.bin, some padding and u-boot.img.
  118. Binman is intended to replace the last step. The U-Boot build system builds
  119. u-boot.bin and sunxi-spl.bin. Binman can then take over creation of
  120. sunxi-spl.bin (by calling mksunxiboot, or hopefully one day mkimage). In any
  121. case, it would then create the image from the component parts.
  122. This simplifies the U-Boot Makefile somewhat, since various pieces of logic
  123. can be replaced by a call to binman.
  124. Example use of binman for x86
  125. -----------------------------
  126. In most cases x86 images have a lot of binary blobs, 'black-box' code
  127. provided by Intel which must be run for the platform to work. Typically
  128. these blobs are not relocatable and must be placed at fixed areas in the
  129. firmware image.
  130. Currently this is handled by ifdtool, which places microcode, FSP, MRC, VGA
  131. BIOS, reference code and Intel ME binaries into a u-boot.rom file.
  132. Binman is intended to replace all of this, with ifdtool left to handle only
  133. the configuration of the Intel-format descriptor.
  134. Running binman
  135. --------------
  136. First install prerequisites, e.g::
  137. sudo apt-get install python-pyelftools python3-pyelftools lzma-alone \
  138. liblz4-tool
  139. Type::
  140. binman build -b <board_name>
  141. to build an image for a board. The board name is the same name used when
  142. configuring U-Boot (e.g. for sandbox_defconfig the board name is 'sandbox').
  143. Binman assumes that the input files for the build are in ../b/<board_name>.
  144. Or you can specify this explicitly::
  145. binman build -I <build_path>
  146. where <build_path> is the build directory containing the output of the U-Boot
  147. build.
  148. (Future work will make this more configurable)
  149. In either case, binman picks up the device tree file (u-boot.dtb) and looks
  150. for its instructions in the 'binman' node.
  151. Binman has a few other options which you can see by running 'binman -h'.
  152. Enabling binman for a board
  153. ---------------------------
  154. At present binman is invoked from a rule in the main Makefile. You should be
  155. able to enable CONFIG_BINMAN to enable this rule.
  156. The output file is typically named image.bin and is located in the output
  157. directory. If input files are needed to you add these to INPUTS-y either in the
  158. main Makefile or in a config.mk file in your arch subdirectory.
  159. Once binman is executed it will pick up its instructions from a device-tree
  160. file, typically <soc>-u-boot.dtsi, where <soc> is your CONFIG_SYS_SOC value.
  161. You can use other, more specific CONFIG options - see 'Automatic .dtsi
  162. inclusion' below.
  163. Using binman with OF_BOARD or OF_PRIOR_STAGE
  164. --------------------------------------------
  165. Normally binman is used with a board configured with OF_SEPARATE or OF_EMBED.
  166. This is a typical scenario where a device tree source that contains the binman
  167. node is provided in the arch/<arch>/dts directory for a specific board.
  168. However for a board configured with OF_BOARD or OF_PRIOR_STAGE, no device tree
  169. blob is provided in the U-Boot build phase hence the binman node information
  170. is not available. In order to support such use case, a new Kconfig option
  171. BINMAN_STANDALONE_FDT is introduced, to tell the build system that a standalone
  172. device tree blob containing binman node is explicitly required.
  173. Note there is a Kconfig option BINMAN_FDT which enables U-Boot run time to
  174. access information about binman entries, stored in the device tree in a binman
  175. node. Generally speaking, this option makes sense for OF_SEPARATE or OF_EMBED.
  176. For the other OF_CONTROL methods, it's quite possible binman node is not
  177. available as binman is invoked during the build phase, thus this option is not
  178. turned on by default for these OF_CONTROL methods.
  179. See qemu-riscv64_spl_defconfig for an example of how binman is used with
  180. OF_PRIOR_STAGE to generate u-boot.itb image.
  181. Access to binman entry offsets at run time (symbols)
  182. ----------------------------------------------------
  183. Binman assembles images and determines where each entry is placed in the image.
  184. This information may be useful to U-Boot at run time. For example, in SPL it
  185. is useful to be able to find the location of U-Boot so that it can be executed
  186. when SPL is finished.
  187. Binman allows you to declare symbols in the SPL image which are filled in
  188. with their correct values during the build. For example::
  189. binman_sym_declare(ulong, u_boot_any, image_pos);
  190. declares a ulong value which will be assigned to the image-pos of any U-Boot
  191. image (u-boot.bin, u-boot.img, u-boot-nodtb.bin) that is present in the image.
  192. You can access this value with something like::
  193. ulong u_boot_offset = binman_sym(ulong, u_boot_any, image_pos);
  194. Thus u_boot_offset will be set to the image-pos of U-Boot in memory, assuming
  195. that the whole image has been loaded, or is available in flash. You can then
  196. jump to that address to start U-Boot.
  197. At present this feature is only supported in SPL and TPL. In principle it is
  198. possible to fill in such symbols in U-Boot proper, as well, but a future C
  199. library is planned for this instead, to read from the device tree.
  200. As well as image-pos, it is possible to read the size of an entry and its
  201. offset (which is the start position of the entry within its parent).
  202. A small technical note: Binman automatically adds the base address of the image
  203. (i.e. __image_copy_start) to the value of the image-pos symbol, so that when the
  204. image is loaded to its linked address, the value will be correct and actually
  205. point into the image.
  206. For example, say SPL is at the start of the image and linked to start at address
  207. 80108000. If U-Boot's image-pos is 0x8000 then binman will write an image-pos
  208. for U-Boot of 80110000 into the SPL binary, since it assumes the image is loaded
  209. to 80108000, with SPL at 80108000 and U-Boot at 80110000.
  210. For x86 devices (with the end-at-4gb property) this base address is not added
  211. since it is assumed that images are XIP and the offsets already include the
  212. address.
  213. Access to binman entry offsets at run time (fdt)
  214. ------------------------------------------------
  215. Binman can update the U-Boot FDT to include the final position and size of
  216. each entry in the images it processes. The option to enable this is -u and it
  217. causes binman to make sure that the 'offset', 'image-pos' and 'size' properties
  218. are set correctly for every entry. Since it is not necessary to specify these in
  219. the image definition, binman calculates the final values and writes these to
  220. the device tree. These can be used by U-Boot at run-time to find the location
  221. of each entry.
  222. Alternatively, an FDT map entry can be used to add a special FDT containing
  223. just the information about the image. This is preceded by a magic string so can
  224. be located anywhere in the image. An image header (typically at the start or end
  225. of the image) can be used to point to the FDT map. See fdtmap and image-header
  226. entries for more information.
  227. Map files
  228. ---------
  229. The -m option causes binman to output a .map file for each image that it
  230. generates. This shows the offset and size of each entry. For example::
  231. Offset Size Name
  232. 00000000 00000028 main-section
  233. 00000000 00000010 section@0
  234. 00000000 00000004 u-boot
  235. 00000010 00000010 section@1
  236. 00000000 00000004 u-boot
  237. This shows a hierarchical image with two sections, each with a single entry. The
  238. offsets of the sections are absolute hex byte offsets within the image. The
  239. offsets of the entries are relative to their respective sections. The size of
  240. each entry is also shown, in bytes (hex). The indentation shows the entries
  241. nested inside their sections.
  242. Passing command-line arguments to entries
  243. -----------------------------------------
  244. Sometimes it is useful to pass binman the value of an entry property from the
  245. command line. For example some entries need access to files and it is not
  246. always convenient to put these filenames in the image definition (device tree).
  247. The -a option supports this::
  248. -a <prop>=<value>
  249. where::
  250. <prop> is the property to set
  251. <value> is the value to set it to
  252. Not all properties can be provided this way. Only some entries support it,
  253. typically for filenames.
  254. Image description format
  255. ========================
  256. The binman node is called 'binman'. An example image description is shown
  257. below::
  258. binman {
  259. filename = "u-boot-sunxi-with-spl.bin";
  260. pad-byte = <0xff>;
  261. blob {
  262. filename = "spl/sunxi-spl.bin";
  263. };
  264. u-boot {
  265. offset = <CONFIG_SPL_PAD_TO>;
  266. };
  267. };
  268. This requests binman to create an image file called u-boot-sunxi-with-spl.bin
  269. consisting of a specially formatted SPL (spl/sunxi-spl.bin, built by the
  270. normal U-Boot Makefile), some 0xff padding, and a U-Boot legacy image. The
  271. padding comes from the fact that the second binary is placed at
  272. CONFIG_SPL_PAD_TO. If that line were omitted then the U-Boot binary would
  273. immediately follow the SPL binary.
  274. The binman node describes an image. The sub-nodes describe entries in the
  275. image. Each entry represents a region within the overall image. The name of
  276. the entry (blob, u-boot) tells binman what to put there. For 'blob' we must
  277. provide a filename. For 'u-boot', binman knows that this means 'u-boot.bin'.
  278. Entries are normally placed into the image sequentially, one after the other.
  279. The image size is the total size of all entries. As you can see, you can
  280. specify the start offset of an entry using the 'offset' property.
  281. Note that due to a device tree requirement, all entries must have a unique
  282. name. If you want to put the same binary in the image multiple times, you can
  283. use any unique name, with the 'type' property providing the type.
  284. The attributes supported for entries are described below.
  285. offset:
  286. This sets the offset of an entry within the image or section containing
  287. it. The first byte of the image is normally at offset 0. If 'offset' is
  288. not provided, binman sets it to the end of the previous region, or the
  289. start of the image's entry area (normally 0) if there is no previous
  290. region.
  291. align:
  292. This sets the alignment of the entry. The entry offset is adjusted
  293. so that the entry starts on an aligned boundary within the containing
  294. section or image. For example 'align = <16>' means that the entry will
  295. start on a 16-byte boundary. This may mean that padding is added before
  296. the entry. The padding is part of the containing section but is not
  297. included in the entry, meaning that an empty space may be created before
  298. the entry starts. Alignment should be a power of 2. If 'align' is not
  299. provided, no alignment is performed.
  300. size:
  301. This sets the size of the entry. The contents will be padded out to
  302. this size. If this is not provided, it will be set to the size of the
  303. contents.
  304. pad-before:
  305. Padding before the contents of the entry. Normally this is 0, meaning
  306. that the contents start at the beginning of the entry. This can be used
  307. to offset the entry contents a little. While this does not affect the
  308. contents of the entry within binman itself (the padding is performed
  309. only when its parent section is assembled), the end result will be that
  310. the entry starts with the padding bytes, so may grow. Defaults to 0.
  311. pad-after:
  312. Padding after the contents of the entry. Normally this is 0, meaning
  313. that the entry ends at the last byte of content (unless adjusted by
  314. other properties). This allows room to be created in the image for
  315. this entry to expand later. While this does not affect the contents of
  316. the entry within binman itself (the padding is performed only when its
  317. parent section is assembled), the end result will be that the entry ends
  318. with the padding bytes, so may grow. Defaults to 0.
  319. align-size:
  320. This sets the alignment of the entry size. For example, to ensure
  321. that the size of an entry is a multiple of 64 bytes, set this to 64.
  322. While this does not affect the contents of the entry within binman
  323. itself (the padding is performed only when its parent section is
  324. assembled), the end result is that the entry ends with the padding
  325. bytes, so may grow. If 'align-size' is not provided, no alignment is
  326. performed.
  327. align-end:
  328. This sets the alignment of the end of an entry with respect to the
  329. containing section. Some entries require that they end on an alignment
  330. boundary, regardless of where they start. This does not move the start
  331. of the entry, so the contents of the entry will still start at the
  332. beginning. But there may be padding at the end. While this does not
  333. affect the contents of the entry within binman itself (the padding is
  334. performed only when its parent section is assembled), the end result
  335. is that the entry ends with the padding bytes, so may grow.
  336. If 'align-end' is not provided, no alignment is performed.
  337. filename:
  338. For 'blob' types this provides the filename containing the binary to
  339. put into the entry. If binman knows about the entry type (like
  340. u-boot-bin), then there is no need to specify this.
  341. type:
  342. Sets the type of an entry. This defaults to the entry name, but it is
  343. possible to use any name, and then add (for example) 'type = "u-boot"'
  344. to specify the type.
  345. offset-unset:
  346. Indicates that the offset of this entry should not be set by placing
  347. it immediately after the entry before. Instead, is set by another
  348. entry which knows where this entry should go. When this boolean
  349. property is present, binman will give an error if another entry does
  350. not set the offset (with the GetOffsets() method).
  351. image-pos:
  352. This cannot be set on entry (or at least it is ignored if it is), but
  353. with the -u option, binman will set it to the absolute image position
  354. for each entry. This makes it easy to find out exactly where the entry
  355. ended up in the image, regardless of parent sections, etc.
  356. expand-size:
  357. Expand the size of this entry to fit available space. This space is only
  358. limited by the size of the image/section and the position of the next
  359. entry.
  360. compress:
  361. Sets the compression algortihm to use (for blobs only). See the entry
  362. documentation for details.
  363. missing-msg:
  364. Sets the tag of the message to show if this entry is missing. This is
  365. used for external blobs. When they are missing it is helpful to show
  366. information about what needs to be fixed. See missing-blob-help for the
  367. message for each tag.
  368. no-expanded:
  369. By default binman substitutes entries with expanded versions if available,
  370. so that a `u-boot` entry type turns into `u-boot-expanded`, for example. The
  371. `--no-expanded` command-line option disables this globally. The
  372. `no-expanded` property disables this just for a single entry. Put the
  373. `no-expanded` boolean property in the node to select this behaviour.
  374. The attributes supported for images and sections are described below. Several
  375. are similar to those for entries.
  376. size:
  377. Sets the image size in bytes, for example 'size = <0x100000>' for a
  378. 1MB image.
  379. offset:
  380. This is similar to 'offset' in entries, setting the offset of a section
  381. within the image or section containing it. The first byte of the section
  382. is normally at offset 0. If 'offset' is not provided, binman sets it to
  383. the end of the previous region, or the start of the image's entry area
  384. (normally 0) if there is no previous region.
  385. align-size:
  386. This sets the alignment of the image size. For example, to ensure
  387. that the image ends on a 512-byte boundary, use 'align-size = <512>'.
  388. If 'align-size' is not provided, no alignment is performed.
  389. pad-before:
  390. This sets the padding before the image entries. The first entry will
  391. be positioned after the padding. This defaults to 0.
  392. pad-after:
  393. This sets the padding after the image entries. The padding will be
  394. placed after the last entry. This defaults to 0.
  395. pad-byte:
  396. This specifies the pad byte to use when padding in the image. It
  397. defaults to 0. To use 0xff, you would add 'pad-byte = <0xff>'.
  398. filename:
  399. This specifies the image filename. It defaults to 'image.bin'.
  400. sort-by-offset:
  401. This causes binman to reorder the entries as needed to make sure they
  402. are in increasing positional order. This can be used when your entry
  403. order may not match the positional order. A common situation is where
  404. the 'offset' properties are set by CONFIG options, so their ordering is
  405. not known a priori.
  406. This is a boolean property so needs no value. To enable it, add a
  407. line 'sort-by-offset;' to your description.
  408. multiple-images:
  409. Normally only a single image is generated. To create more than one
  410. image, put this property in the binman node. For example, this will
  411. create image1.bin containing u-boot.bin, and image2.bin containing
  412. both spl/u-boot-spl.bin and u-boot.bin::
  413. binman {
  414. multiple-images;
  415. image1 {
  416. u-boot {
  417. };
  418. };
  419. image2 {
  420. spl {
  421. };
  422. u-boot {
  423. };
  424. };
  425. };
  426. end-at-4gb:
  427. For x86 machines the ROM offsets start just before 4GB and extend
  428. up so that the image finished at the 4GB boundary. This boolean
  429. option can be enabled to support this. The image size must be
  430. provided so that binman knows when the image should start. For an
  431. 8MB ROM, the offset of the first entry would be 0xfff80000 with
  432. this option, instead of 0 without this option.
  433. skip-at-start:
  434. This property specifies the entry offset of the first entry.
  435. For PowerPC mpc85xx based CPU, CONFIG_SYS_TEXT_BASE is the entry
  436. offset of the first entry. It can be 0xeff40000 or 0xfff40000 for
  437. nor flash boot, 0x201000 for sd boot etc.
  438. 'end-at-4gb' property is not applicable where CONFIG_SYS_TEXT_BASE +
  439. Image size != 4gb.
  440. align-default:
  441. Specifies the default alignment for entries in this section, if they do
  442. not specify an alignment. Note that this only applies to top-level entries
  443. in the section (direct subentries), not any subentries of those entries.
  444. This means that each section must specify its own default alignment, if
  445. required.
  446. Examples of the above options can be found in the tests. See the
  447. tools/binman/test directory.
  448. It is possible to have the same binary appear multiple times in the image,
  449. either by using a unit number suffix (u-boot@0, u-boot@1) or by using a
  450. different name for each and specifying the type with the 'type' attribute.
  451. Sections and hierachical images
  452. -------------------------------
  453. Sometimes it is convenient to split an image into several pieces, each of which
  454. contains its own set of binaries. An example is a flash device where part of
  455. the image is read-only and part is read-write. We can set up sections for each
  456. of these, and place binaries in them independently. The image is still produced
  457. as a single output file.
  458. This feature provides a way of creating hierarchical images. For example here
  459. is an example image with two copies of U-Boot. One is read-only (ro), intended
  460. to be written only in the factory. Another is read-write (rw), so that it can be
  461. upgraded in the field. The sizes are fixed so that the ro/rw boundary is known
  462. and can be programmed::
  463. binman {
  464. section@0 {
  465. read-only;
  466. name-prefix = "ro-";
  467. size = <0x100000>;
  468. u-boot {
  469. };
  470. };
  471. section@1 {
  472. name-prefix = "rw-";
  473. size = <0x100000>;
  474. u-boot {
  475. };
  476. };
  477. };
  478. This image could be placed into a SPI flash chip, with the protection boundary
  479. set at 1MB.
  480. A few special properties are provided for sections:
  481. read-only:
  482. Indicates that this section is read-only. This has no impact on binman's
  483. operation, but his property can be read at run time.
  484. name-prefix:
  485. This string is prepended to all the names of the binaries in the
  486. section. In the example above, the 'u-boot' binaries which actually be
  487. renamed to 'ro-u-boot' and 'rw-u-boot'. This can be useful to
  488. distinguish binaries with otherwise identical names.
  489. Image Properties
  490. ----------------
  491. Image nodes act like sections but also have a few extra properties:
  492. filename:
  493. Output filename for the image. This defaults to image.bin (or in the
  494. case of multiple images <nodename>.bin where <nodename> is the name of
  495. the image node.
  496. allow-repack:
  497. Create an image that can be repacked. With this option it is possible
  498. to change anything in the image after it is created, including updating
  499. the position and size of image components. By default this is not
  500. permitted since it is not possibly to know whether this might violate a
  501. constraint in the image description. For example, if a section has to
  502. increase in size to hold a larger binary, that might cause the section
  503. to fall out of its allow region (e.g. read-only portion of flash).
  504. Adding this property causes the original offset and size values in the
  505. image description to be stored in the FDT and fdtmap.
  506. Hashing Entries
  507. ---------------
  508. It is possible to ask binman to hash the contents of an entry and write that
  509. value back to the device-tree node. For example::
  510. binman {
  511. u-boot {
  512. hash {
  513. algo = "sha256";
  514. };
  515. };
  516. };
  517. Here, a new 'value' property will be written to the 'hash' node containing
  518. the hash of the 'u-boot' entry. Only SHA256 is supported at present. Whole
  519. sections can be hased if desired, by adding the 'hash' node to the section.
  520. The has value can be chcked at runtime by hashing the data actually read and
  521. comparing this has to the value in the device tree.
  522. Expanded entries
  523. ----------------
  524. Binman automatically replaces 'u-boot' with an expanded version of that, i.e.
  525. 'u-boot-expanded'. This means that when you write::
  526. u-boot {
  527. };
  528. you actually get::
  529. u-boot {
  530. type = "u-boot-expanded';
  531. };
  532. which in turn expands to::
  533. u-boot {
  534. type = "section";
  535. u-boot-nodtb {
  536. };
  537. u-boot-dtb {
  538. };
  539. };
  540. U-Boot's various phase binaries actually comprise two or three pieces.
  541. For example, u-boot.bin has the executable followed by a devicetree.
  542. With binman we want to be able to update that devicetree with full image
  543. information so that it is accessible to the executable. This is tricky
  544. if it is not clear where the devicetree starts.
  545. The above feature ensures that the devicetree is clearly separated from the
  546. U-Boot executable and can be updated separately by binman as needed. It can be
  547. disabled with the --no-expanded flag if required.
  548. The same applies for u-boot-spl and u-boot-spl. In those cases, the expansion
  549. includes the BSS padding, so for example::
  550. spl {
  551. type = "u-boot-spl"
  552. };
  553. you actually get::
  554. spl {
  555. type = "u-boot-expanded';
  556. };
  557. which in turn expands to::
  558. spl {
  559. type = "section";
  560. u-boot-spl-nodtb {
  561. };
  562. u-boot-spl-bss-pad {
  563. };
  564. u-boot-spl-dtb {
  565. };
  566. };
  567. Of course we should not expand SPL if it has no devicetree. Also if the BSS
  568. padding is not needed (because BSS is in RAM as with CONFIG_SPL_SEPARATE_BSS),
  569. the 'u-boot-spl-bss-pad' subnode should not be created. The use of the expaned
  570. entry type is controlled by the UseExpanded() method. In the SPL case it checks
  571. the 'spl-dtb' entry arg, which is 'y' or '1' if SPL has a devicetree.
  572. For the BSS case, a 'spl-bss-pad' entry arg controls whether it is present. All
  573. entry args are provided by the U-Boot Makefile.
  574. Compression
  575. -----------
  576. Binman support compression for 'blob' entries (those of type 'blob' and
  577. derivatives). To enable this for an entry, add a 'compress' property::
  578. blob {
  579. filename = "datafile";
  580. compress = "lz4";
  581. };
  582. The entry will then contain the compressed data, using the 'lz4' compression
  583. algorithm. Currently this is the only one that is supported. The uncompressed
  584. size is written to the node in an 'uncomp-size' property, if -u is used.
  585. Compression is also supported for sections. In that case the entire section is
  586. compressed in one block, including all its contents. This means that accessing
  587. an entry from the section required decompressing the entire section. Also, the
  588. size of a section indicates the space that it consumes in its parent section
  589. (and typically the image). With compression, the section may contain more data,
  590. and the uncomp-size property indicates that, as above. The contents of the
  591. section is compressed first, before any padding is added. This ensures that the
  592. padding itself is not compressed, which would be a waste of time.
  593. Automatic .dtsi inclusion
  594. -------------------------
  595. It is sometimes inconvenient to add a 'binman' node to the .dts file for each
  596. board. This can be done by using #include to bring in a common file. Another
  597. approach supported by the U-Boot build system is to automatically include
  598. a common header. You can then put the binman node (and anything else that is
  599. specific to U-Boot, such as u-boot,dm-pre-reloc properies) in that header
  600. file.
  601. Binman will search for the following files in arch/<arch>/dts::
  602. <dts>-u-boot.dtsi where <dts> is the base name of the .dts file
  603. <CONFIG_SYS_SOC>-u-boot.dtsi
  604. <CONFIG_SYS_CPU>-u-boot.dtsi
  605. <CONFIG_SYS_VENDOR>-u-boot.dtsi
  606. u-boot.dtsi
  607. U-Boot will only use the first one that it finds. If you need to include a
  608. more general file you can do that from the more specific file using #include.
  609. If you are having trouble figuring out what is going on, you can uncomment
  610. the 'warning' line in scripts/Makefile.lib to see what it has found::
  611. # Uncomment for debugging
  612. # This shows all the files that were considered and the one that we chose.
  613. # u_boot_dtsi_options_debug = $(u_boot_dtsi_options_raw)
  614. Entry Documentation
  615. ===================
  616. For details on the various entry types supported by binman and how to use them,
  617. see entries.rst which is generated from the source code using:
  618. binman entry-docs >tools/binman/entries.rst
  619. .. toctree::
  620. :maxdepth: 2
  621. entries
  622. Managing images
  623. ===============
  624. Listing images
  625. --------------
  626. It is possible to list the entries in an existing firmware image created by
  627. binman, provided that there is an 'fdtmap' entry in the image. For example::
  628. $ binman ls -i image.bin
  629. Name Image-pos Size Entry-type Offset Uncomp-size
  630. ----------------------------------------------------------------------
  631. main-section c00 section 0
  632. u-boot 0 4 u-boot 0
  633. section 5fc section 4
  634. cbfs 100 400 cbfs 0
  635. u-boot 138 4 u-boot 38
  636. u-boot-dtb 180 108 u-boot-dtb 80 3b5
  637. u-boot-dtb 500 1ff u-boot-dtb 400 3b5
  638. fdtmap 6fc 381 fdtmap 6fc
  639. image-header bf8 8 image-header bf8
  640. This shows the hierarchy of the image, the position, size and type of each
  641. entry, the offset of each entry within its parent and the uncompressed size if
  642. the entry is compressed.
  643. It is also possible to list just some files in an image, e.g.::
  644. $ binman ls -i image.bin section/cbfs
  645. Name Image-pos Size Entry-type Offset Uncomp-size
  646. --------------------------------------------------------------------
  647. cbfs 100 400 cbfs 0
  648. u-boot 138 4 u-boot 38
  649. u-boot-dtb 180 108 u-boot-dtb 80 3b5
  650. or with wildcards::
  651. $ binman ls -i image.bin "*cb*" "*head*"
  652. Name Image-pos Size Entry-type Offset Uncomp-size
  653. ----------------------------------------------------------------------
  654. cbfs 100 400 cbfs 0
  655. u-boot 138 4 u-boot 38
  656. u-boot-dtb 180 108 u-boot-dtb 80 3b5
  657. image-header bf8 8 image-header bf8
  658. Extracting files from images
  659. ----------------------------
  660. You can extract files from an existing firmware image created by binman,
  661. provided that there is an 'fdtmap' entry in the image. For example::
  662. $ binman extract -i image.bin section/cbfs/u-boot
  663. which will write the uncompressed contents of that entry to the file 'u-boot' in
  664. the current directory. You can also extract to a particular file, in this case
  665. u-boot.bin::
  666. $ binman extract -i image.bin section/cbfs/u-boot -f u-boot.bin
  667. It is possible to extract all files into a destination directory, which will
  668. put files in subdirectories matching the entry hierarchy::
  669. $ binman extract -i image.bin -O outdir
  670. or just a selection::
  671. $ binman extract -i image.bin "*u-boot*" -O outdir
  672. Replacing files in an image
  673. ---------------------------
  674. You can replace files in an existing firmware image created by binman, provided
  675. that there is an 'fdtmap' entry in the image. For example:
  676. $ binman replace -i image.bin section/cbfs/u-boot
  677. which will write the contents of the file 'u-boot' from the current directory
  678. to the that entry, compressing if necessary. If the entry size changes, you must
  679. add the 'allow-repack' property to the original image before generating it (see
  680. above), otherwise you will get an error.
  681. You can also use a particular file, in this case u-boot.bin::
  682. $ binman replace -i image.bin section/cbfs/u-boot -f u-boot.bin
  683. It is possible to replace all files from a source directory which uses the same
  684. hierarchy as the entries::
  685. $ binman replace -i image.bin -I indir
  686. Files that are missing will generate a warning.
  687. You can also replace just a selection of entries::
  688. $ binman replace -i image.bin "*u-boot*" -I indir
  689. Logging
  690. -------
  691. Binman normally operates silently unless there is an error, in which case it
  692. just displays the error. The -D/--debug option can be used to create a full
  693. backtrace when errors occur. You can use BINMAN_DEBUG=1 when building to select
  694. this.
  695. Internally binman logs some output while it is running. This can be displayed
  696. by increasing the -v/--verbosity from the default of 1:
  697. 0: silent
  698. 1: warnings only
  699. 2: notices (important messages)
  700. 3: info about major operations
  701. 4: detailed information about each operation
  702. 5: debug (all output)
  703. You can use BINMAN_VERBOSE=5 (for example) when building to select this.
  704. Technical details
  705. =================
  706. Order of image creation
  707. -----------------------
  708. Image creation proceeds in the following order, for each entry in the image.
  709. 1. AddMissingProperties() - binman can add calculated values to the device
  710. tree as part of its processing, for example the offset and size of each
  711. entry. This method adds any properties associated with this, expanding the
  712. device tree as needed. These properties can have placeholder values which are
  713. set later by SetCalculatedProperties(). By that stage the size of sections
  714. cannot be changed (since it would cause the images to need to be repacked),
  715. but the correct values can be inserted.
  716. 2. ProcessFdt() - process the device tree information as required by the
  717. particular entry. This may involve adding or deleting properties. If the
  718. processing is complete, this method should return True. If the processing
  719. cannot complete because it needs the ProcessFdt() method of another entry to
  720. run first, this method should return False, in which case it will be called
  721. again later.
  722. 3. GetEntryContents() - the contents of each entry are obtained, normally by
  723. reading from a file. This calls the Entry.ObtainContents() to read the
  724. contents. The default version of Entry.ObtainContents() calls
  725. Entry.GetDefaultFilename() and then reads that file. So a common mechanism
  726. to select a file to read is to override that function in the subclass. The
  727. functions must return True when they have read the contents. Binman will
  728. retry calling the functions a few times if False is returned, allowing
  729. dependencies between the contents of different entries.
  730. 4. GetEntryOffsets() - calls Entry.GetOffsets() for each entry. This can
  731. return a dict containing entries that need updating. The key should be the
  732. entry name and the value is a tuple (offset, size). This allows an entry to
  733. provide the offset and size for other entries. The default implementation
  734. of GetEntryOffsets() returns {}.
  735. 5. PackEntries() - calls Entry.Pack() which figures out the offset and
  736. size of an entry. The 'current' image offset is passed in, and the function
  737. returns the offset immediately after the entry being packed. The default
  738. implementation of Pack() is usually sufficient.
  739. Note: for sections, this also checks that the entries do not overlap, nor extend
  740. outside the section. If the section does not have a defined size, the size is
  741. set large enough to hold all the entries.
  742. 6. SetImagePos() - sets the image position of every entry. This is the absolute
  743. position 'image-pos', as opposed to 'offset' which is relative to the containing
  744. section. This must be done after all offsets are known, which is why it is quite
  745. late in the ordering.
  746. 7. SetCalculatedProperties() - update any calculated properties in the device
  747. tree. This sets the correct 'offset' and 'size' vaues, for example.
  748. 8. ProcessEntryContents() - this calls Entry.ProcessContents() on each entry.
  749. The default implementatoin does nothing. This can be overriden to adjust the
  750. contents of an entry in some way. For example, it would be possible to create
  751. an entry containing a hash of the contents of some other entries. At this
  752. stage the offset and size of entries should not be adjusted unless absolutely
  753. necessary, since it requires a repack (going back to PackEntries()).
  754. 9. ResetForPack() - if the ProcessEntryContents() step failed, in that an entry
  755. has changed its size, then there is no alternative but to go back to step 5 and
  756. try again, repacking the entries with the updated size. ResetForPack() removes
  757. the fixed offset/size values added by binman, so that the packing can start from
  758. scratch.
  759. 10. WriteSymbols() - write the value of symbols into the U-Boot SPL binary.
  760. See 'Access to binman entry offsets at run time' below for a description of
  761. what happens in this stage.
  762. 11. BuildImage() - builds the image and writes it to a file
  763. 12. WriteMap() - writes a text file containing a map of the image. This is the
  764. final step.
  765. External tools
  766. --------------
  767. Binman can make use of external command-line tools to handle processing of
  768. entry contents or to generate entry contents. These tools are executed using
  769. the 'tools' module's Run() method. The tools generally must exist on the PATH,
  770. but the --toolpath option can be used to specify additional search paths to
  771. use. This option can be specified multiple times to add more than one path.
  772. For some compile tools binman will use the versions specified by commonly-used
  773. environment variables like CC and HOSTCC for the C compiler, based on whether
  774. the tool's output will be used for the target or for the host machine. If those
  775. aren't given, it will also try to derive target-specific versions from the
  776. CROSS_COMPILE environment variable during a cross-compilation.
  777. Code coverage
  778. -------------
  779. Binman is a critical tool and is designed to be very testable. Entry
  780. implementations target 100% test coverage. Run 'binman test -T' to check this.
  781. To enable Python test coverage on Debian-type distributions (e.g. Ubuntu)::
  782. $ sudo apt-get install python-coverage python3-coverage python-pytest
  783. Concurrent tests
  784. ----------------
  785. Binman tries to run tests concurrently. This means that the tests make use of
  786. all available CPUs to run.
  787. To enable this::
  788. $ sudo apt-get install python-subunit python3-subunit
  789. Use '-P 1' to disable this. It is automatically disabled when code coverage is
  790. being used (-T) since they are incompatible.
  791. Debugging tests
  792. ---------------
  793. Sometimes when debugging tests it is useful to keep the input and output
  794. directories so they can be examined later. Use -X or --test-preserve-dirs for
  795. this.
  796. Running tests on non-x86 architectures
  797. --------------------------------------
  798. Binman's tests have been written under the assumption that they'll be run on a
  799. x86-like host and there hasn't been an attempt to make them portable yet.
  800. However, it's possible to run the tests by cross-compiling to x86.
  801. To install an x86 cross-compiler on Debian-type distributions (e.g. Ubuntu)::
  802. $ sudo apt-get install gcc-x86-64-linux-gnu
  803. Then, you can run the tests under cross-compilation::
  804. $ CROSS_COMPILE=x86_64-linux-gnu- binman test -T
  805. You can also use gcc-i686-linux-gnu similar to the above.
  806. Writing new entries and debugging
  807. ---------------------------------
  808. The behaviour of entries is defined by the Entry class. All other entries are
  809. a subclass of this. An important subclass is Entry_blob which takes binary
  810. data from a file and places it in the entry. In fact most entry types are
  811. subclasses of Entry_blob.
  812. Each entry type is a separate file in the tools/binman/etype directory. Each
  813. file contains a class called Entry_<type> where <type> is the entry type.
  814. New entry types can be supported by adding new files in that directory.
  815. These will automatically be detected by binman when needed.
  816. Entry properties are documented in entry.py. The entry subclasses are free
  817. to change the values of properties to support special behaviour. For example,
  818. when Entry_blob loads a file, it sets content_size to the size of the file.
  819. Entry classes can adjust other entries. For example, an entry that knows
  820. where other entries should be positioned can set up those entries' offsets
  821. so they don't need to be set in the binman decription. It can also adjust
  822. entry contents.
  823. Most of the time such essoteric behaviour is not needed, but it can be
  824. essential for complex images.
  825. If you need to specify a particular device-tree compiler to use, you can define
  826. the DTC environment variable. This can be useful when the system dtc is too
  827. old.
  828. To enable a full backtrace and other debugging features in binman, pass
  829. BINMAN_DEBUG=1 to your build::
  830. make qemu-x86_defconfig
  831. make BINMAN_DEBUG=1
  832. To enable verbose logging from binman, base BINMAN_VERBOSE to your build, which
  833. adds a -v<level> option to the call to binman::
  834. make qemu-x86_defconfig
  835. make BINMAN_VERBOSE=5
  836. History / Credits
  837. -----------------
  838. Binman takes a lot of inspiration from a Chrome OS tool called
  839. 'cros_bundle_firmware', which I wrote some years ago. That tool was based on
  840. a reasonably simple and sound design but has expanded greatly over the
  841. years. In particular its handling of x86 images is convoluted.
  842. Quite a few lessons have been learned which are hopefully applied here.
  843. Design notes
  844. ------------
  845. On the face of it, a tool to create firmware images should be fairly simple:
  846. just find all the input binaries and place them at the right place in the
  847. image. The difficulty comes from the wide variety of input types (simple
  848. flat binaries containing code, packaged data with various headers), packing
  849. requirments (alignment, spacing, device boundaries) and other required
  850. features such as hierarchical images.
  851. The design challenge is to make it easy to create simple images, while
  852. allowing the more complex cases to be supported. For example, for most
  853. images we don't much care exactly where each binary ends up, so we should
  854. not have to specify that unnecessarily.
  855. New entry types should aim to provide simple usage where possible. If new
  856. core features are needed, they can be added in the Entry base class.
  857. To do
  858. -----
  859. Some ideas:
  860. - Use of-platdata to make the information available to code that is unable
  861. to use device tree (such as a very small SPL image). For now, limited info is
  862. available via linker symbols
  863. - Allow easy building of images by specifying just the board name
  864. - Support building an image for a board (-b) more completely, with a
  865. configurable build directory
  866. - Detect invalid properties in nodes
  867. - Sort the fdtmap by offset
  868. - Output temporary files to a different directory
  869. --
  870. Simon Glass <sjg@chromium.org>
  871. 7/7/2016