binman.rst 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401
  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 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. Installation
  72. ------------
  73. You can install binman using::
  74. pip install binary-manager
  75. The name is chosen since binman conflicts with an existing package.
  76. If you are using binman within the U-Boot tree, it may be easiest to add a
  77. symlink from your local `~/.bin` directory to `/path/to/tools/binman/binman`.
  78. Relationship to FIT
  79. -------------------
  80. FIT is U-Boot's official image format. It supports multiple binaries with
  81. load / execution addresses, compression. It also supports verification
  82. through hashing and RSA signatures.
  83. FIT was originally designed to support booting a Linux kernel (with an
  84. optional ramdisk) and device tree chosen from various options in the FIT.
  85. Now that U-Boot supports configuration via device tree, it is possible to
  86. load U-Boot from a FIT, with the device tree chosen by SPL.
  87. Binman considers FIT to be one of the binaries it can place in the image.
  88. Where possible it is best to put as much as possible in the FIT, with binman
  89. used to deal with cases not covered by FIT. Examples include initial
  90. execution (since FIT itself does not have an executable header) and dealing
  91. with device boundaries, such as the read-only/read-write separation in SPI
  92. flash.
  93. For U-Boot, binman should not be used to create ad-hoc images in place of
  94. FIT.
  95. Note that binman can itself create a FIT. This helps to move mkimage
  96. invocations out of the Makefile and into binman image descriptions. It also
  97. helps by removing the need for ad-hoc tools like `make_fit_atf.py`.
  98. Relationship to mkimage
  99. -----------------------
  100. The mkimage tool provides a means to create a FIT. Traditionally it has
  101. needed an image description file: a device tree, like binman, but in a
  102. different format. More recently it has started to support a '-f auto' mode
  103. which can generate that automatically.
  104. More relevant to binman, mkimage also permits creation of many SoC-specific
  105. image types. These can be listed by running 'mkimage -T list'. Examples
  106. include 'rksd', the Rockchip SD/MMC boot format. The mkimage tool is often
  107. called from the U-Boot build system for this reason.
  108. Binman considers the output files created by mkimage to be binary blobs
  109. which it can place in an image. Binman does not replace the mkimage tool or
  110. this purpose. It would be possible in some situations to create a new entry
  111. type for the images in mkimage, but this would not add functionality. It
  112. seems better to use the mkimage tool to generate binaries and avoid blurring
  113. the boundaries between building input files (mkimage) and packaging then
  114. into a final image (binman).
  115. Note that binman can itself invoke mkimage. This helps to move mkimage
  116. invocations out of the Makefile and into binman image descriptions.
  117. Using binman
  118. ============
  119. Example use of binman in U-Boot
  120. -------------------------------
  121. Binman aims to replace some of the ad-hoc image creation in the U-Boot
  122. build system.
  123. Consider sunxi. It has the following steps:
  124. #. It uses a custom mksunxiboot tool to build an SPL image called
  125. sunxi-spl.bin. This should probably move into mkimage.
  126. #. It uses mkimage to package U-Boot into a legacy image file (so that it can
  127. hold the load and execution address) called u-boot.img.
  128. #. It builds a final output image called u-boot-sunxi-with-spl.bin which
  129. consists of sunxi-spl.bin, some padding and u-boot.img.
  130. Binman is intended to replace the last step. The U-Boot build system builds
  131. u-boot.bin and sunxi-spl.bin. Binman can then take over creation of
  132. sunxi-spl.bin by calling mksunxiboot or mkimage. In any case, it would then
  133. create the image from the component parts.
  134. This simplifies the U-Boot Makefile somewhat, since various pieces of logic
  135. can be replaced by a call to binman.
  136. Invoking binman within U-Boot
  137. -----------------------------
  138. Within U-Boot, binman is invoked by the build system, i.e. when you type 'make'
  139. or use buildman to build U-Boot. There is no need to run binman independently
  140. during development. Everything happens automatically and is set up for your
  141. SoC or board so that binman produced the right things.
  142. The general policy is that the Makefile builds all the binaries in INPUTS-y
  143. (the 'inputs' rule), then binman is run to produce the final images (the 'all'
  144. rule).
  145. There should be only one invocation of binman in Makefile, the very last step
  146. that pulls everything together. At present there are some arch-specific
  147. invocations as well, but these should be dropped when those architectures are
  148. converted to use binman properly.
  149. As above, the term 'binary' is used for something in INPUTS-y and 'image' is
  150. used for the things that binman creates. So the binaries are inputs to the
  151. image(s) and it is the image that is actually loaded on the board.
  152. Again, at present, there are a number of things created in Makefile which should
  153. be done by binman (when we get around to it), like `u-boot-ivt.img`,
  154. `lpc32xx-spl.img`, `u-boot-with-nand-spl.imx`, `u-boot-spl-padx4.sfp` and
  155. `u-boot-mtk.bin`, just to pick on a few. When completed this will remove about
  156. 400 lines from `Makefile`.
  157. Since binman is invoked only once, it must of course create all the images that
  158. are needed, in that one invocation. It does this by working through the image
  159. descriptions one by one, collecting the input binaries, processing them as
  160. needed and producing the final images.
  161. The same binaries may be used by multiple images. For example binman may be used
  162. to produce an SD-card image and a SPI-flash image. In this case the binaries
  163. going into the process are the same, but binman produces slightly different
  164. images in each case.
  165. For some SoCs, U-Boot is not the only project that produces the necessary
  166. binaries. For example, ARM Trusted Firmware (ATF) is a project that produces
  167. binaries which must be incorporate, such as `bl31.elf` or `bl31.bin`. For this
  168. to work you must have built ATF before you build U-Boot and you must tell U-Boot
  169. where to find the bl31 image, using the BL31 environment variable.
  170. How do you know how to incorporate ATF? It is handled by the atf-bl31 entry type
  171. (etype). An etype is an implementation of reading a binary into binman, in this
  172. case the `bl31.bin` file. When you build U-Boot but do not set the BL31
  173. environment variable, binman provides a help message, which comes from
  174. `missing-blob-help`::
  175. See the documentation for your board. You may need to build ARM Trusted
  176. Firmware and build with BL31=/path/to/bl31.bin
  177. The mechanism by which binman is advised of this is also in the Makefile. See
  178. the `-a atf-bl31-path=${BL31}` piece in `cmd_binman`. This tells binman to
  179. set the EntryArg `atf-bl31-path` to the value of the `BL31` environment
  180. variable. Within binman, this EntryArg is picked up by the `Entry_atf_bl31`
  181. etype. An EntryArg is simply an argument to the entry. The `atf-bl31-path`
  182. name is documented in :ref:`etype_atf_bl31`.
  183. Taking this a little further, when binman is used to create a FIT, it supports
  184. using an ELF file, e.g. `bl31.elf` and splitting it into separate pieces (with
  185. `fit,operation = "split-elf"`), each with its own load address.
  186. Invoking binman outside U-Boot
  187. ------------------------------
  188. While binman is invoked from within the U-Boot build system, it is also possible
  189. to invoke it separately. This is typically used in a production build system,
  190. where signing is completed (with real keys) and any missing binaries are
  191. provided.
  192. For example, for build testing there is no need to provide a real signature,
  193. nor is there any need to provide a real ATF BL31 binary (for example). These can
  194. be added later by invoking binman again, providing all the required inputs
  195. from the first time, plus any that were missing or placeholders.
  196. So in practice binman is often used twice:
  197. - once within the U-Boot build system, for development and testing
  198. - again outside U-Boot to assembly and final production images
  199. While the same input binaries are used in each case, you will of course you will
  200. need to create your own binman command line, similar to that in `cmd_binman` in
  201. the Makefile. You may find the -I and --toolpath options useful. The
  202. device tree file is provided to binman in binary form, so there is no need to
  203. have access to the original `.dts` sources.
  204. Assembling the image description
  205. --------------------------------
  206. Since binman uses the device tree for its image description, you can use the
  207. same files that describe your board's hardware to describe how the image is
  208. assembled. Typically the images description is in a common file used by all
  209. boards with a particular SoC (e.g. `imx8mp-u-boot.dtsi`).
  210. Where a particular boards needs to make changes, it can override properties in
  211. the SoC file, just as it would for any other device tree property. It can also
  212. add a image that is specific to the board.
  213. Another way to control the image description to make use of CONFIG options in
  214. the description. For example, if the start offset of a particular entry varies
  215. by board, you can add a Kconfig for that and reference it in the description::
  216. u-boot-spl {
  217. };
  218. fit {
  219. offset = <CONFIG_SPL_PAD_TO>;
  220. ...
  221. };
  222. The SoC can provide a default value but boards can override that as needed and
  223. binman will take care of it.
  224. It is even possible to control which entries appear in the image, by using the
  225. C preprocessor::
  226. #ifdef CONFIG_HAVE_MRC
  227. intel-mrc {
  228. offset = <CFG_X86_MRC_ADDR>;
  229. };
  230. #endif
  231. Only boards which enable `HAVE_MRC` will include this entry.
  232. Obviously a similar approach can be used to control which images are produced,
  233. with a Kconfig option to enable a SPI image, for example. However there is
  234. generally no harm in producing an image that is not used. If a board uses MMC
  235. but not SPI, but the SoC supports booting from both, then both images can be
  236. produced, with only on or other being used by particular boards. This can help
  237. reduce the need for having multiple defconfig targets for a board where the
  238. only difference is the boot media, enabling / disabling secure boot, etc.
  239. Of course you can use the device tree itself to pass any board-specific
  240. information that is needed by U-Boot at runtime (see binman_syms_ for how to
  241. make binman insert these values directly into executables like SPL).
  242. There is one more way this can be done: with individual .dtsi files for each
  243. image supported by the SoC. Then the board `.dts` file can include the ones it
  244. wants. This is not recommended, since it is likely to be difficult to maintain
  245. and harder to understand the relationship between the different boards.
  246. Producing images for multiple boards
  247. ------------------------------------
  248. When invoked within U-Boot, binman only builds a single set of images, for
  249. the chosen board. This is set by the `CONFIG_DEFAULT_DEVICE_TREE` option.
  250. However, U-Boot generally builds all the device tree files associated with an
  251. SoC. These are written to the (e.g. for ARM) `arch/arm/dts` directory. Each of
  252. these contains the full binman description for that board. Often the best
  253. approach is to build a single image that includes all these device tree binaries
  254. and allow SPL to select the correct one on boot.
  255. However, it is also possible to build separate images for each board, simply by
  256. invoking binman multiple times, once for each device tree file, using a
  257. different output directory. This will produce one set of images for each board.
  258. Example use of binman for x86
  259. -----------------------------
  260. In most cases x86 images have a lot of binary blobs, 'black-box' code
  261. provided by Intel which must be run for the platform to work. Typically
  262. these blobs are not relocatable and must be placed at fixed areas in the
  263. firmware image.
  264. Currently this is handled by ifdtool, which places microcode, FSP, MRC, VGA
  265. BIOS, reference code and Intel ME binaries into a u-boot.rom file.
  266. Binman is intended to replace all of this, with ifdtool left to handle only
  267. the configuration of the Intel-format descriptor.
  268. Installing binman
  269. -----------------
  270. First install prerequisites, e.g:
  271. .. code-block:: bash
  272. sudo apt-get install python-pyelftools python3-pyelftools lzma-alone \
  273. liblz4-tool
  274. You can run binman directly if you put it on your PATH. But if you want to
  275. install into your `~/.local` Python directory, use:
  276. .. code-block:: bash
  277. pip install tools/patman tools/dtoc tools/binman
  278. Note that binman makes use of libraries from patman and dtoc, which is why these
  279. need to be installed. Also you need `libfdt` and `pylibfdt` which can be
  280. installed like this:
  281. .. code-block:: bash
  282. git clone git://git.kernel.org/pub/scm/utils/dtc/dtc.git
  283. cd dtc
  284. pip install .
  285. make NO_PYTHON=1 install
  286. This installs the `libfdt.so` library into `~/lib` so you can use
  287. `LD_LIBRARY_PATH=~/lib` when running binman. If you want to install it in the
  288. system-library directory, replace the last line with:
  289. .. code-block:: bash
  290. make NO_PYTHON=1 PREFIX=/ install
  291. Running binman
  292. --------------
  293. Type:
  294. .. code-block:: bash
  295. make NO_PYTHON=1 PREFIX=/ install
  296. binman build -b <board_name>
  297. to build an image for a board. The board name is the same name used when
  298. configuring U-Boot (e.g. for sandbox_defconfig the board name is 'sandbox').
  299. Binman assumes that the input files for the build are in ../b/<board_name>.
  300. Or you can specify this explicitly:
  301. .. code-block:: bash
  302. make NO_PYTHON=1 PREFIX=/ install
  303. binman build -I <build_path>
  304. where <build_path> is the build directory containing the output of the U-Boot
  305. build.
  306. (Future work will make this more configurable)
  307. In either case, binman picks up the device tree file (u-boot.dtb) and looks
  308. for its instructions in the 'binman' node.
  309. Binman has a few other options which you can see by running 'binman -h'.
  310. Enabling binman for a board
  311. ---------------------------
  312. At present binman is invoked from a rule in the main Makefile. You should be
  313. able to enable CONFIG_BINMAN to enable this rule.
  314. The output file is typically named image.bin and is located in the output
  315. directory. If input files are needed to you add these to INPUTS-y either in the
  316. main Makefile or in a config.mk file in your arch subdirectory.
  317. Once binman is executed it will pick up its instructions from a device-tree
  318. file, typically <soc>-u-boot.dtsi, where <soc> is your CONFIG_SYS_SOC value.
  319. You can use other, more specific CONFIG options - see 'Automatic .dtsi
  320. inclusion' below.
  321. .. _binman_syms:
  322. Access to binman entry offsets at run time (symbols)
  323. ----------------------------------------------------
  324. Binman assembles images and determines where each entry is placed in the image.
  325. This information may be useful to U-Boot at run time. For example, in SPL it
  326. is useful to be able to find the location of U-Boot so that it can be executed
  327. when SPL is finished.
  328. Binman allows you to declare symbols in the SPL image which are filled in
  329. with their correct values during the build. For example:
  330. .. code-block:: c
  331. binman_sym_declare(ulong, u_boot_any, image_pos);
  332. declares a ulong value which will be assigned to the image-pos of any U-Boot
  333. image (u-boot.bin, u-boot.img, u-boot-nodtb.bin) that is present in the image.
  334. You can access this value with something like:
  335. .. code-block:: c
  336. ulong u_boot_offset = binman_sym(ulong, u_boot_any, image_pos);
  337. Thus u_boot_offset will be set to the image-pos of U-Boot in memory, assuming
  338. that the whole image has been loaded, or is available in flash. You can then
  339. jump to that address to start U-Boot.
  340. At present this feature is only supported in SPL and TPL. In principle it is
  341. possible to fill in such symbols in U-Boot proper, as well, but a future C
  342. library is planned for this instead, to read from the device tree.
  343. As well as image-pos, it is possible to read the size of an entry and its
  344. offset (which is the start position of the entry within its parent).
  345. A small technical note: Binman automatically adds the base address of the image
  346. (i.e. __image_copy_start) to the value of the image-pos symbol, so that when the
  347. image is loaded to its linked address, the value will be correct and actually
  348. point into the image.
  349. For example, say SPL is at the start of the image and linked to start at address
  350. 80108000. If U-Boot's image-pos is 0x8000 then binman will write an image-pos
  351. for U-Boot of 80110000 into the SPL binary, since it assumes the image is loaded
  352. to 80108000, with SPL at 80108000 and U-Boot at 80110000.
  353. For x86 devices (with the end-at-4gb property) this base address is not added
  354. since it is assumed that images are XIP and the offsets already include the
  355. address.
  356. While U-Boot's symbol updating is handled automatically by the u-boot-spl
  357. entry type (and others), it is possible to use this feature with any blob. To
  358. do this, add a `write-symbols` (boolean) property to the node, set the ELF
  359. filename using `elf-filename` and set 'elf-base-sym' to the base symbol for the
  360. start of the binary image (this defaults to `__image_copy_start` which is what
  361. U-Boot uses). See `testBlobSymbol()` for an example.
  362. .. _binman_fdt:
  363. Access to binman entry offsets at run time (fdt)
  364. ------------------------------------------------
  365. Binman can update the U-Boot FDT to include the final position and size of
  366. each entry in the images it processes. The option to enable this is -u and it
  367. causes binman to make sure that the 'offset', 'image-pos' and 'size' properties
  368. are set correctly for every entry. Since it is not necessary to specify these in
  369. the image definition, binman calculates the final values and writes these to
  370. the device tree. These can be used by U-Boot at run-time to find the location
  371. of each entry.
  372. Alternatively, an FDT map entry can be used to add a special FDT containing
  373. just the information about the image. This is preceded by a magic string so can
  374. be located anywhere in the image. An image header (typically at the start or end
  375. of the image) can be used to point to the FDT map. See fdtmap and image-header
  376. entries for more information.
  377. Map files
  378. ---------
  379. The -m option causes binman to output a .map file for each image that it
  380. generates. This shows the offset and size of each entry. For example::
  381. Offset Size Name
  382. 00000000 00000028 main-section
  383. 00000000 00000010 section@0
  384. 00000000 00000004 u-boot
  385. 00000010 00000010 section@1
  386. 00000000 00000004 u-boot
  387. This shows a hierarchical image with two sections, each with a single entry. The
  388. offsets of the sections are absolute hex byte offsets within the image. The
  389. offsets of the entries are relative to their respective sections. The size of
  390. each entry is also shown, in bytes (hex). The indentation shows the entries
  391. nested inside their sections.
  392. Passing command-line arguments to entries
  393. -----------------------------------------
  394. Sometimes it is useful to pass binman the value of an entry property from the
  395. command line. For example some entries need access to files and it is not
  396. always convenient to put these filenames in the image definition (device tree).
  397. The -a option supports this::
  398. -a <prop>=<value>
  399. where::
  400. <prop> is the property to set
  401. <value> is the value to set it to
  402. Not all properties can be provided this way. Only some entries support it,
  403. typically for filenames.
  404. Image description format
  405. ========================
  406. The binman node is called 'binman'. An example image description is shown
  407. below::
  408. binman {
  409. filename = "u-boot-sunxi-with-spl.bin";
  410. pad-byte = <0xff>;
  411. blob {
  412. filename = "spl/sunxi-spl.bin";
  413. };
  414. u-boot {
  415. offset = <CONFIG_SPL_PAD_TO>;
  416. };
  417. };
  418. This requests binman to create an image file called u-boot-sunxi-with-spl.bin
  419. consisting of a specially formatted SPL (spl/sunxi-spl.bin, built by the
  420. normal U-Boot Makefile), some 0xff padding, and a U-Boot legacy image. The
  421. padding comes from the fact that the second binary is placed at
  422. CONFIG_SPL_PAD_TO. If that line were omitted then the U-Boot binary would
  423. immediately follow the SPL binary.
  424. The binman node describes an image. The sub-nodes describe entries in the
  425. image. Each entry represents a region within the overall image. The name of
  426. the entry (blob, u-boot) tells binman what to put there. For 'blob' we must
  427. provide a filename. For 'u-boot', binman knows that this means 'u-boot.bin'.
  428. Entries are normally placed into the image sequentially, one after the other.
  429. The image size is the total size of all entries. As you can see, you can
  430. specify the start offset of an entry using the 'offset' property.
  431. Note that due to a device tree requirement, all entries must have a unique
  432. name. If you want to put the same binary in the image multiple times, you can
  433. use any unique name, with the 'type' property providing the type.
  434. The attributes supported for entries are described below.
  435. offset:
  436. This sets the offset of an entry within the image or section containing
  437. it. The first byte of the image is normally at offset 0. If 'offset' is
  438. not provided, binman sets it to the end of the previous region, or the
  439. start of the image's entry area (normally 0) if there is no previous
  440. region.
  441. align:
  442. This sets the alignment of the entry. The entry offset is adjusted
  443. so that the entry starts on an aligned boundary within the containing
  444. section or image. For example 'align = <16>' means that the entry will
  445. start on a 16-byte boundary. This may mean that padding is added before
  446. the entry. The padding is part of the containing section but is not
  447. included in the entry, meaning that an empty space may be created before
  448. the entry starts. Alignment should be a power of 2. If 'align' is not
  449. provided, no alignment is performed.
  450. size:
  451. This sets the size of the entry. The contents will be padded out to
  452. this size. If this is not provided, it will be set to the size of the
  453. contents.
  454. min-size:
  455. Sets the minimum size of the entry. This size includes explicit padding
  456. ('pad-before' and 'pad-after'), but not padding added to meet alignment
  457. requirements. While this does not affect the contents of the entry within
  458. binman itself (the padding is performed only when its parent section is
  459. assembled), the end result will be that the entry ends with the padding
  460. bytes, so may grow. Defaults to 0.
  461. pad-before:
  462. Padding before the contents of the entry. Normally this is 0, meaning
  463. that the contents start at the beginning of the entry. This can be used
  464. to offset the entry contents a little. While this does not affect the
  465. contents of the entry within binman itself (the padding is performed
  466. only when its parent section is assembled), the end result will be that
  467. the entry starts with the padding bytes, so may grow. Defaults to 0.
  468. pad-after:
  469. Padding after the contents of the entry. Normally this is 0, meaning
  470. that the entry ends at the last byte of content (unless adjusted by
  471. other properties). This allows room to be created in the image for
  472. this entry to expand later. While this does not affect the contents of
  473. the entry within binman itself (the padding is performed only when its
  474. parent section is assembled), the end result will be that the entry ends
  475. with the padding bytes, so may grow. Defaults to 0.
  476. align-size:
  477. This sets the alignment of the entry size. For example, to ensure
  478. that the size of an entry is a multiple of 64 bytes, set this to 64.
  479. While this does not affect the contents of the entry within binman
  480. itself (the padding is performed only when its parent section is
  481. assembled), the end result is that the entry ends with the padding
  482. bytes, so may grow. If 'align-size' is not provided, no alignment is
  483. performed.
  484. align-end:
  485. This sets the alignment of the end of an entry with respect to the
  486. containing section. Some entries require that they end on an alignment
  487. boundary, regardless of where they start. This does not move the start
  488. of the entry, so the contents of the entry will still start at the
  489. beginning. But there may be padding at the end. While this does not
  490. affect the contents of the entry within binman itself (the padding is
  491. performed only when its parent section is assembled), the end result
  492. is that the entry ends with the padding bytes, so may grow.
  493. If 'align-end' is not provided, no alignment is performed.
  494. filename:
  495. For 'blob' types this provides the filename containing the binary to
  496. put into the entry. If binman knows about the entry type (like
  497. u-boot-bin), then there is no need to specify this.
  498. type:
  499. Sets the type of an entry. This defaults to the entry name, but it is
  500. possible to use any name, and then add (for example) 'type = "u-boot"'
  501. to specify the type.
  502. offset-unset:
  503. Indicates that the offset of this entry should not be set by placing
  504. it immediately after the entry before. Instead, is set by another
  505. entry which knows where this entry should go. When this boolean
  506. property is present, binman will give an error if another entry does
  507. not set the offset (with the GetOffsets() method).
  508. image-pos:
  509. This cannot be set on entry (or at least it is ignored if it is), but
  510. with the -u option, binman will set it to the absolute image position
  511. for each entry. This makes it easy to find out exactly where the entry
  512. ended up in the image, regardless of parent sections, etc.
  513. extend-size:
  514. Extend the size of this entry to fit available space. This space is only
  515. limited by the size of the image/section and the position of the next
  516. entry.
  517. compress:
  518. Sets the compression algortihm to use (for blobs only). See the entry
  519. documentation for details.
  520. missing-msg:
  521. Sets the tag of the message to show if this entry is missing. This is
  522. used for external blobs. When they are missing it is helpful to show
  523. information about what needs to be fixed. See missing-blob-help for the
  524. message for each tag.
  525. no-expanded:
  526. By default binman substitutes entries with expanded versions if available,
  527. so that a `u-boot` entry type turns into `u-boot-expanded`, for example. The
  528. `--no-expanded` command-line option disables this globally. The
  529. `no-expanded` property disables this just for a single entry. Put the
  530. `no-expanded` boolean property in the node to select this behaviour.
  531. optional:
  532. External blobs are normally required to be present for the image to be
  533. built (but see `External blobs`_). This properly allows an entry to be
  534. optional, so that when it is cannot be found, this problem is ignored and
  535. an empty file is used for this blob. This should be used only when the blob
  536. is entirely optional and is not needed for correct operation of the image.
  537. Note that missing, optional blobs do not produce a non-zero exit code from
  538. binman, although it does show a warning about the missing external blob.
  539. insert-template:
  540. This is not strictly speaking an entry property, since it is processed early
  541. in Binman before the entries are read. It is a list of phandles of nodes to
  542. include in the current (target) node. For each node, its subnodes and their
  543. properties are brought into the target node. See Templates_ below for
  544. more information.
  545. The attributes supported for images and sections are described below. Several
  546. are similar to those for entries.
  547. size:
  548. Sets the image size in bytes, for example 'size = <0x100000>' for a
  549. 1MB image.
  550. offset:
  551. This is similar to 'offset' in entries, setting the offset of a section
  552. within the image or section containing it. The first byte of the section
  553. is normally at offset 0. If 'offset' is not provided, binman sets it to
  554. the end of the previous region, or the start of the image's entry area
  555. (normally 0) if there is no previous region.
  556. align-size:
  557. This sets the alignment of the image size. For example, to ensure
  558. that the image ends on a 512-byte boundary, use 'align-size = <512>'.
  559. If 'align-size' is not provided, no alignment is performed.
  560. pad-before:
  561. This sets the padding before the image entries. The first entry will
  562. be positioned after the padding. This defaults to 0.
  563. pad-after:
  564. This sets the padding after the image entries. The padding will be
  565. placed after the last entry. This defaults to 0.
  566. pad-byte:
  567. This specifies the pad byte to use when padding in the image. It
  568. defaults to 0. To use 0xff, you would add 'pad-byte = <0xff>'.
  569. filename:
  570. This specifies the image filename. It defaults to 'image.bin'.
  571. sort-by-offset:
  572. This causes binman to reorder the entries as needed to make sure they
  573. are in increasing positional order. This can be used when your entry
  574. order may not match the positional order. A common situation is where
  575. the 'offset' properties are set by CONFIG options, so their ordering is
  576. not known a priori.
  577. This is a boolean property so needs no value. To enable it, add a
  578. line 'sort-by-offset;' to your description.
  579. multiple-images:
  580. Normally only a single image is generated. To create more than one
  581. image, put this property in the binman node. For example, this will
  582. create image1.bin containing u-boot.bin, and image2.bin containing
  583. both spl/u-boot-spl.bin and u-boot.bin::
  584. binman {
  585. multiple-images;
  586. image1 {
  587. u-boot {
  588. };
  589. };
  590. image2 {
  591. spl {
  592. };
  593. u-boot {
  594. };
  595. };
  596. };
  597. end-at-4gb:
  598. For x86 machines the ROM offsets start just before 4GB and extend
  599. up so that the image finished at the 4GB boundary. This boolean
  600. option can be enabled to support this. The image size must be
  601. provided so that binman knows when the image should start. For an
  602. 8MB ROM, the offset of the first entry would be 0xfff80000 with
  603. this option, instead of 0 without this option.
  604. skip-at-start:
  605. This property specifies the entry offset of the first entry.
  606. For PowerPC mpc85xx based CPU, CONFIG_TEXT_BASE is the entry
  607. offset of the first entry. It can be 0xeff40000 or 0xfff40000 for
  608. nor flash boot, 0x201000 for sd boot etc.
  609. 'end-at-4gb' property is not applicable where CONFIG_TEXT_BASE +
  610. Image size != 4gb.
  611. align-default:
  612. Specifies the default alignment for entries in this section, if they do
  613. not specify an alignment. Note that this only applies to top-level entries
  614. in the section (direct subentries), not any subentries of those entries.
  615. This means that each section must specify its own default alignment, if
  616. required.
  617. symlink:
  618. Adds a symlink to the image with string given in the symlink property.
  619. overlap:
  620. Indicates that this entry overlaps with others in the same section. These
  621. entries should appear at the end of the section. Overlapping entries are not
  622. packed with other entries, but their contents are written over other entries
  623. in the section. Overlapping entries must have an explicit offset and size.
  624. write-symbols:
  625. Indicates that the blob should be updated with symbol values calculated by
  626. binman. This is automatic for certain entry types, e.g. `u-boot-spl`. See
  627. binman_syms_ for more information.
  628. no-write-symbols:
  629. Disables symbol writing for this entry. This can be used in entry types
  630. where symbol writing is automatic. For example, if `u-boot-spl` refers to
  631. the `u_boot_any_image_pos` symbol but U-Boot is not available in the image
  632. containing SPL, this can be used to disable the writing. Quite likely this
  633. indicates a bug in your setup.
  634. elf-filename:
  635. Sets the file name of a blob's associated ELF file. For example, if the
  636. blob is `zephyr.bin` then the ELF file may be `zephyr.elf`. This allows
  637. binman to locate symbols and understand the structure of the blob. See
  638. binman_syms_ for more information.
  639. elf-base-sym:
  640. Sets the name of the ELF symbol that points to the start of a blob. For
  641. U-Boot this is `__image_copy_start` and that is the default used by binman
  642. if this property is missing. For other projects, a difference symbol may be
  643. needed. Add this symbol to the properties for the blob so that symbols can
  644. be read correctly. See binman_syms_ for more information.
  645. offset-from-elf:
  646. Sets the offset of an entry based on a symbol value in an another entry.
  647. The format is <&phandle>, "sym_name", <offset> where phandle is the entry
  648. containing the blob (with associated ELF file providing symbols), <sym_name>
  649. is the symbol to lookup (relative to elf-base-sym) and <offset> is an offset
  650. to add to that value.
  651. preserve:
  652. Indicates that this entry should be preserved by any firmware updates. This
  653. flag should be checked by the updater when it is deciding which entries to
  654. update. This flag is normally attached to sections but can be attached to
  655. a single entry in a section if the updater supports it. Not that binman
  656. itself has no control over the updater's behaviour, so this is just a
  657. signal. It is not enforced by binman.
  658. Examples of the above options can be found in the tests. See the
  659. tools/binman/test directory.
  660. It is possible to have the same binary appear multiple times in the image,
  661. either by using a unit number suffix (u-boot@0, u-boot@1) or by using a
  662. different name for each and specifying the type with the 'type' attribute.
  663. Sections and hierachical images
  664. -------------------------------
  665. Sometimes it is convenient to split an image into several pieces, each of which
  666. contains its own set of binaries. An example is a flash device where part of
  667. the image is read-only and part is read-write. We can set up sections for each
  668. of these, and place binaries in them independently. The image is still produced
  669. as a single output file.
  670. This feature provides a way of creating hierarchical images. For example here
  671. is an example image with two copies of U-Boot. One is read-only (ro), intended
  672. to be written only in the factory. Another is read-write (rw), so that it can be
  673. upgraded in the field. The sizes are fixed so that the ro/rw boundary is known
  674. and can be programmed::
  675. binman {
  676. section@0 {
  677. read-only;
  678. name-prefix = "ro-";
  679. size = <0x100000>;
  680. u-boot {
  681. };
  682. };
  683. section@1 {
  684. name-prefix = "rw-";
  685. size = <0x100000>;
  686. u-boot {
  687. };
  688. };
  689. };
  690. This image could be placed into a SPI flash chip, with the protection boundary
  691. set at 1MB.
  692. A few special properties are provided for sections:
  693. read-only:
  694. Indicates that this section is read-only. This has no impact on binman's
  695. operation, but his property can be read at run time.
  696. name-prefix:
  697. This string is prepended to all the names of the binaries in the
  698. section. In the example above, the 'u-boot' binaries which actually be
  699. renamed to 'ro-u-boot' and 'rw-u-boot'. This can be useful to
  700. distinguish binaries with otherwise identical names.
  701. filename:
  702. This allows the contents of the section to be written to a file in the
  703. output directory. This can sometimes be useful to use the data in one
  704. section in different image, since there is currently no way to share data
  705. beteen images other than through files.
  706. Image Properties
  707. ----------------
  708. Image nodes act like sections but also have a few extra properties:
  709. filename:
  710. Output filename for the image. This defaults to image.bin (or in the
  711. case of multiple images <nodename>.bin where <nodename> is the name of
  712. the image node.
  713. allow-repack:
  714. Create an image that can be repacked. With this option it is possible
  715. to change anything in the image after it is created, including updating
  716. the position and size of image components. By default this is not
  717. permitted since it is not possibly to know whether this might violate a
  718. constraint in the image description. For example, if a section has to
  719. increase in size to hold a larger binary, that might cause the section
  720. to fall out of its allow region (e.g. read-only portion of flash).
  721. Adding this property causes the original offset and size values in the
  722. image description to be stored in the FDT and fdtmap.
  723. Image dependencies
  724. ------------------
  725. Binman does not currently support images that depend on each other. For example,
  726. if one image creates `fred.bin` and then the next uses this `fred.bin` to
  727. produce a final `image.bin`, then the behaviour is undefined. It may work, or it
  728. may produce an error about `fred.bin` being missing, or it may use a version of
  729. `fred.bin` from a previous run.
  730. Often this can be handled by incorporating the dependency into the second
  731. image. For example, instead of::
  732. binman {
  733. multiple-images;
  734. fred {
  735. u-boot {
  736. };
  737. fill {
  738. size = <0x100>;
  739. };
  740. };
  741. image {
  742. blob {
  743. filename = "fred.bin";
  744. };
  745. u-boot-spl {
  746. };
  747. };
  748. you can do this::
  749. binman {
  750. image {
  751. fred {
  752. type = "section";
  753. u-boot {
  754. };
  755. fill {
  756. size = <0x100>;
  757. };
  758. };
  759. u-boot-spl {
  760. };
  761. };
  762. Hashing Entries
  763. ---------------
  764. It is possible to ask binman to hash the contents of an entry and write that
  765. value back to the device-tree node. For example::
  766. binman {
  767. u-boot {
  768. hash {
  769. algo = "sha256";
  770. };
  771. };
  772. };
  773. Here, a new 'value' property will be written to the 'hash' node containing
  774. the hash of the 'u-boot' entry. Only SHA256 is supported at present. Whole
  775. sections can be hased if desired, by adding the 'hash' node to the section.
  776. The has value can be chcked at runtime by hashing the data actually read and
  777. comparing this has to the value in the device tree.
  778. Expanded entries
  779. ----------------
  780. Binman automatically replaces 'u-boot' with an expanded version of that, i.e.
  781. 'u-boot-expanded'. This means that when you write::
  782. u-boot {
  783. };
  784. you actually get::
  785. u-boot {
  786. type = "u-boot-expanded';
  787. };
  788. which in turn expands to::
  789. u-boot {
  790. type = "section";
  791. u-boot-nodtb {
  792. };
  793. u-boot-dtb {
  794. };
  795. };
  796. U-Boot's various phase binaries actually comprise two or three pieces.
  797. For example, u-boot.bin has the executable followed by a devicetree.
  798. With binman we want to be able to update that devicetree with full image
  799. information so that it is accessible to the executable. This is tricky
  800. if it is not clear where the devicetree starts.
  801. The above feature ensures that the devicetree is clearly separated from the
  802. U-Boot executable and can be updated separately by binman as needed. It can be
  803. disabled with the --no-expanded flag if required.
  804. The same applies for u-boot-spl and u-boot-tpl. In those cases, the expansion
  805. includes the BSS padding, so for example::
  806. spl {
  807. type = "u-boot-spl"
  808. };
  809. you actually get::
  810. spl {
  811. type = "u-boot-expanded';
  812. };
  813. which in turn expands to::
  814. spl {
  815. type = "section";
  816. u-boot-spl-nodtb {
  817. };
  818. u-boot-spl-bss-pad {
  819. };
  820. u-boot-spl-dtb {
  821. };
  822. };
  823. Of course we should not expand SPL if it has no devicetree. Also if the BSS
  824. padding is not needed (because BSS is in RAM as with CONFIG_SPL_SEPARATE_BSS),
  825. the 'u-boot-spl-bss-pad' subnode should not be created. The use of the expaned
  826. entry type is controlled by the UseExpanded() method. In the SPL case it checks
  827. the 'spl-dtb' entry arg, which is 'y' or '1' if SPL has a devicetree.
  828. For the BSS case, a 'spl-bss-pad' entry arg controls whether it is present. All
  829. entry args are provided by the U-Boot Makefile.
  830. Optional entries
  831. ----------------
  832. Some entries need to exist only if certain conditions are met. For example, an
  833. entry may want to appear in the image only if a file has a particular format.
  834. Obviously the entry must exist in the image description for it to be processed
  835. at all, so a way needs to be found to have the entry remove itself.
  836. To handle this, when entry.ObtainContents() is called, the entry can call
  837. entry.mark_absent() to mark itself as absent, passing a suitable message as the
  838. reason.
  839. Any absent entries are dropped immediately after ObtainContents() has been
  840. called on all entries.
  841. It is not possible for an entry to mark itself absent at any other point in the
  842. processing. It must happen in the ObtainContents() method.
  843. The effect is as if the entry had never been present at all, since the image
  844. is packed without it and it disappears from the list of entries.
  845. Compression
  846. -----------
  847. Binman support compression for 'blob' entries (those of type 'blob' and
  848. derivatives). To enable this for an entry, add a 'compress' property::
  849. blob {
  850. filename = "datafile";
  851. compress = "lz4";
  852. };
  853. The entry will then contain the compressed data, using the 'lz4' compression
  854. algorithm. Currently this is the only one that is supported. The uncompressed
  855. size is written to the node in an 'uncomp-size' property, if -u is used.
  856. Compression is also supported for sections. In that case the entire section is
  857. compressed in one block, including all its contents. This means that accessing
  858. an entry from the section required decompressing the entire section. Also, the
  859. size of a section indicates the space that it consumes in its parent section
  860. (and typically the image). With compression, the section may contain more data,
  861. and the uncomp-size property indicates that, as above. The contents of the
  862. section is compressed first, before any padding is added. This ensures that the
  863. padding itself is not compressed, which would be a waste of time.
  864. Automatic .dtsi inclusion
  865. -------------------------
  866. It is sometimes inconvenient to add a 'binman' node to the .dts file for each
  867. board. This can be done by using #include to bring in a common file. Another
  868. approach supported by the U-Boot build system is to automatically include
  869. a common header. You can then put the binman node (and anything else that is
  870. specific to U-Boot, such as bootph-all properies) in that header file.
  871. Binman will search for the following files in arch/<arch>/dts::
  872. <dts>-u-boot.dtsi where <dts> is the base name of the .dts file
  873. <CONFIG_SYS_SOC>-u-boot.dtsi
  874. <CONFIG_SYS_CPU>-u-boot.dtsi
  875. <CONFIG_SYS_VENDOR>-u-boot.dtsi
  876. u-boot.dtsi
  877. U-Boot will only use the first one that it finds. If you need to include a
  878. more general file you can do that from the more specific file using #include.
  879. If you are having trouble figuring out what is going on, you can use
  880. `DEVICE_TREE_DEBUG=1` with your build::
  881. make DEVICE_TREE_DEBUG=1
  882. scripts/Makefile.lib:334: Automatic .dtsi inclusion: options:
  883. arch/arm/dts/juno-r2-u-boot.dtsi arch/arm/dts/-u-boot.dtsi
  884. arch/arm/dts/armv8-u-boot.dtsi arch/arm/dts/armltd-u-boot.dtsi
  885. arch/arm/dts/u-boot.dtsi ... found: "arch/arm/dts/juno-r2-u-boot.dtsi"
  886. Templates
  887. =========
  888. Sometimes multiple images need to be created which have all have a common
  889. part. For example, a board may generate SPI and eMMC images which both include
  890. a FIT. Since the FIT includes many entries, it is tedious to repeat them twice
  891. in the image description.
  892. Templates provide a simple way to handle this::
  893. binman {
  894. multiple-images;
  895. common_part: template-1 {
  896. some-property;
  897. fit {
  898. ... lots of entries in here
  899. };
  900. text {
  901. text = "base image";
  902. };
  903. };
  904. spi-image {
  905. filename = "image-spi.bin";
  906. insert-template = <&fit>;
  907. /* things specific to SPI follow */
  908. footer {
  909. ];
  910. text {
  911. text = "SPI image";
  912. };
  913. };
  914. mmc-image {
  915. filename = "image-mmc.bin";
  916. insert-template = <&fit>;
  917. /* things specific to MMC follow */
  918. footer {
  919. ];
  920. text {
  921. text = "MMC image";
  922. };
  923. };
  924. };
  925. The template node name must start with 'template', so it is not considered to be
  926. an image itself.
  927. The mechanism is very simple. For each phandle in the 'insert-templates'
  928. property, the source node is looked up. Then the subnodes of that source node
  929. are copied into the target node, i.e. the one containing the `insert-template`
  930. property.
  931. If the target node has a node with the same name as a template, its properties
  932. override corresponding properties in the template. This allows the template to
  933. be uses as a base, with the node providing updates to the properties as needed.
  934. The overriding happens recursively.
  935. Template nodes appear first in each node that they are inserted into and
  936. ordering of template nodes is preserved. Other nodes come afterwards. If a
  937. template node also appears in the target node, then the template node sets the
  938. order. Thus the template can be used to set the ordering, even if the target
  939. node provides all the properties. In the above example, `fit` and `text` appear
  940. first in the `spi-image` and `mmc-image` images, followed by `footer`.
  941. Where there are multiple template nodes, they are inserted in that order. so
  942. the first template node appears first, then the second.
  943. Properties in the template node are inserted into the destination node if they
  944. do not exist there. In the example above, `some-property` is added to each of
  945. `spi-image` and `mmc-image`.
  946. Note that template nodes are removed from the binman description after
  947. processing and before binman builds the image descriptions.
  948. The initial devicetree produced by the templating process is written to the
  949. `u-boot.dtb.tmpl1` file. This can be useful to see what is going on if there is
  950. a failure before the final `u-boot.dtb.out` file is written. A second
  951. `u-boot.dtb.tmpl2` file is written when the templates themselves are removed.
  952. Dealing with phandles
  953. ---------------------
  954. Templates can contain phandles and these are copied to the destination node.
  955. However this should be used with care, since if a template is instantiated twice
  956. then the phandle will be copied twice, resulting in a devicetree with duplicate
  957. phandles, i.e. the same phandle used by two different nodes. Binman detects this
  958. situation and produces an error, for example::
  959. Duplicate phandle 1 in nodes /binman/image/fit/images/atf/atf-bl31 and
  960. /binman/image-2/fit/images/atf/atf-bl31
  961. In this case an atf-bl31 node containing a phandle has been copied into two
  962. different target nodes, resulting in the same phandle for each. See
  963. testTemplatePhandleDup() for the test case.
  964. The solution is typically to put the phandles in the corresponding target nodes
  965. (one for each) and remove the phandle from the template.
  966. Updating an ELF file
  967. ====================
  968. For the EFI app, where U-Boot is loaded from UEFI and runs as an app, there is
  969. no way to update the devicetree after U-Boot is built. Normally this works by
  970. creating a new u-boot.dtb.out with he updated devicetree, which is automatically
  971. built into the output image. With ELF this is not possible since the ELF is
  972. not part of an image, just a stand-along file. We must create an updated ELF
  973. file with the new devicetree.
  974. This is handled by the --update-fdt-in-elf option. It takes four arguments,
  975. separated by comma:
  976. infile - filename of input ELF file, e.g. 'u-boot's
  977. outfile - filename of output ELF file, e.g. 'u-boot.out'
  978. begin_sym - symbol at the start of the embedded devicetree, e.g.
  979. '__dtb_dt_begin'
  980. end_sym - symbol at the start of the embedded devicetree, e.g.
  981. '__dtb_dt_end'
  982. When this flag is used, U-Boot does all the normal packaging, but as an
  983. additional step, it creates a new ELF file with the new devicetree embedded in
  984. it.
  985. If logging is enabled you will see a message like this::
  986. Updating file 'u-boot' with data length 0x400a (16394) between symbols
  987. '__dtb_dt_begin' and '__dtb_dt_end'
  988. There must be enough space for the updated devicetree. If not, an error like
  989. the following is produced::
  990. ValueError: Not enough space in 'u-boot' for data length 0x400a (16394);
  991. size is 0x1744 (5956)
  992. Entry Documentation
  993. ===================
  994. For details on the various entry types supported by binman and how to use them,
  995. see entries.rst which is generated from the source code using:
  996. binman entry-docs >tools/binman/entries.rst
  997. .. toctree::
  998. :maxdepth: 2
  999. entries
  1000. Managing images
  1001. ===============
  1002. Listing images
  1003. --------------
  1004. It is possible to list the entries in an existing firmware image created by
  1005. binman, provided that there is an 'fdtmap' entry in the image. For example::
  1006. $ binman ls -i image.bin
  1007. Name Image-pos Size Entry-type Offset Uncomp-size
  1008. ----------------------------------------------------------------------
  1009. main-section c00 section 0
  1010. u-boot 0 4 u-boot 0
  1011. section 5fc section 4
  1012. cbfs 100 400 cbfs 0
  1013. u-boot 138 4 u-boot 38
  1014. u-boot-dtb 180 108 u-boot-dtb 80 3b5
  1015. u-boot-dtb 500 1ff u-boot-dtb 400 3b5
  1016. fdtmap 6fc 381 fdtmap 6fc
  1017. image-header bf8 8 image-header bf8
  1018. This shows the hierarchy of the image, the position, size and type of each
  1019. entry, the offset of each entry within its parent and the uncompressed size if
  1020. the entry is compressed.
  1021. It is also possible to list just some files in an image, e.g.::
  1022. $ binman ls -i image.bin section/cbfs
  1023. Name Image-pos Size Entry-type Offset Uncomp-size
  1024. --------------------------------------------------------------------
  1025. cbfs 100 400 cbfs 0
  1026. u-boot 138 4 u-boot 38
  1027. u-boot-dtb 180 108 u-boot-dtb 80 3b5
  1028. or with wildcards::
  1029. $ binman ls -i image.bin "*cb*" "*head*"
  1030. Name Image-pos Size Entry-type Offset Uncomp-size
  1031. ----------------------------------------------------------------------
  1032. cbfs 100 400 cbfs 0
  1033. u-boot 138 4 u-boot 38
  1034. u-boot-dtb 180 108 u-boot-dtb 80 3b5
  1035. image-header bf8 8 image-header bf8
  1036. If an older version of binman is used to list images created by a newer one, it
  1037. is possible that it will contain entry types that are not supported. These still
  1038. show with the correct type, but binman just sees them as blobs (plain binary
  1039. data). Any special features of that etype are not supported by the old binman.
  1040. Extracting files from images
  1041. ----------------------------
  1042. You can extract files from an existing firmware image created by binman,
  1043. provided that there is an 'fdtmap' entry in the image. For example::
  1044. $ binman extract -i image.bin section/cbfs/u-boot
  1045. which will write the uncompressed contents of that entry to the file 'u-boot' in
  1046. the current directory. You can also extract to a particular file, in this case
  1047. u-boot.bin::
  1048. $ binman extract -i image.bin section/cbfs/u-boot -f u-boot.bin
  1049. It is possible to extract all files into a destination directory, which will
  1050. put files in subdirectories matching the entry hierarchy::
  1051. $ binman extract -i image.bin -O outdir
  1052. or just a selection::
  1053. $ binman extract -i image.bin "*u-boot*" -O outdir
  1054. Some entry types have alternative formats, for example fdtmap which allows
  1055. extracted just the devicetree binary without the fdtmap header::
  1056. $ binman extract -i /tmp/b/odroid-c4/image.bin -f out.dtb -F fdt fdtmap
  1057. $ fdtdump out.dtb
  1058. /dts-v1/;
  1059. // magic: 0xd00dfeed
  1060. // totalsize: 0x8ab (2219)
  1061. // off_dt_struct: 0x38
  1062. // off_dt_strings: 0x82c
  1063. // off_mem_rsvmap: 0x28
  1064. // version: 17
  1065. // last_comp_version: 2
  1066. // boot_cpuid_phys: 0x0
  1067. // size_dt_strings: 0x7f
  1068. // size_dt_struct: 0x7f4
  1069. / {
  1070. image-node = "binman";
  1071. image-pos = <0x00000000>;
  1072. size = <0x0011162b>;
  1073. ...
  1074. Use `-F list` to see what alternative formats are available::
  1075. $ binman extract -i /tmp/b/odroid-c4/image.bin -F list
  1076. Flag (-F) Entry type Description
  1077. fdt fdtmap Extract the devicetree blob from the fdtmap
  1078. Replacing files in an image
  1079. ---------------------------
  1080. You can replace files in an existing firmware image created by binman, provided
  1081. that there is an 'fdtmap' entry in the image. For example::
  1082. $ binman replace -i image.bin section/cbfs/u-boot
  1083. which will write the contents of the file 'u-boot' from the current directory
  1084. to the that entry, compressing if necessary. If the entry size changes, you must
  1085. add the 'allow-repack' property to the original image before generating it (see
  1086. above), otherwise you will get an error.
  1087. You can also use a particular file, in this case u-boot.bin::
  1088. $ binman replace -i image.bin section/cbfs/u-boot -f u-boot.bin
  1089. It is possible to replace all files from a source directory which uses the same
  1090. hierarchy as the entries::
  1091. $ binman replace -i image.bin -I indir
  1092. Files that are missing will generate a warning.
  1093. You can also replace just a selection of entries::
  1094. $ binman replace -i image.bin "*u-boot*" -I indir
  1095. It is possible to replace whole sections as well, but in that case any
  1096. information about entries within the section may become outdated. This is
  1097. because Binman cannot know whether things have moved around or resized within
  1098. the section, once you have updated its data.
  1099. Technical note: With 'allow-repack', Binman writes information about the
  1100. original offset and size properties of each entry, if any were specified, in
  1101. the 'orig-offset' and 'orig-size' properties. This allows Binman to distinguish
  1102. between an entry which ended up being packed at an offset (or assigned a size)
  1103. and an entry which had a particular offset / size requested in the Binman
  1104. configuration. Where are particular offset / size was requested, this is treated
  1105. as set in stone, so Binman will ensure it doesn't change. Without this feature,
  1106. repacking an entry might cause it to disobey the original constraints provided
  1107. when it was created.
  1108. Signing FIT container with private key in an image
  1109. --------------------------------------------------
  1110. You can sign FIT container with private key in your image.
  1111. For example::
  1112. $ binman sign -i image.bin -k privatekey -a sha256,rsa4096 fit
  1113. binman will extract FIT container, sign and replace it immediately.
  1114. If you want to sign and replace FIT container in place::
  1115. $ binman sign -i image.bin -k privatekey -a sha256,rsa4096 -f fit.fit fit
  1116. which will sign FIT container with private key and replace it immediately
  1117. inside your image.
  1118. .. _`BinmanLogging`:
  1119. Logging
  1120. -------
  1121. Binman normally operates silently unless there is an error, in which case it
  1122. just displays the error. The -D/--debug option can be used to create a full
  1123. backtrace when errors occur. You can use BINMAN_DEBUG=1 when building to select
  1124. this.
  1125. Internally binman logs some output while it is running. This can be displayed
  1126. by increasing the -v/--verbosity from the default of 1:
  1127. 0: silent
  1128. 1: warnings only
  1129. 2: notices (important messages)
  1130. 3: info about major operations
  1131. 4: detailed information about each operation
  1132. 5: debug (all output)
  1133. You can use BINMAN_VERBOSE=5 (for example) when building to select this.
  1134. Bintools
  1135. ========
  1136. `Bintool` is the name binman gives to a binary tool which it uses to create and
  1137. manipulate binaries that binman cannot handle itself. Bintools are often
  1138. necessary since Binman only supports a subset of the available file formats
  1139. natively.
  1140. Many SoC vendors invent ways to load code into their SoC using new file formats,
  1141. sometimes changing the format with successive SoC generations. Sometimes the
  1142. tool is available as Open Source. Sometimes it is a pre-compiled binary that
  1143. must be downloaded from the vendor's website. Sometimes it is available in
  1144. source form but difficult or slow to build.
  1145. Even for images that use bintools, binman still assembles the image from its
  1146. image description. It may handle parts of the image natively and part with
  1147. various bintools.
  1148. Binman relies on these tools so provides various features to manage them:
  1149. - Determining whether the tool is currently installed
  1150. - Downloading or building the tool
  1151. - Determining the version of the tool that is installed
  1152. - Deciding which tools are needed to build an image
  1153. The Bintool class is an interface to the tool, a thin level of abstration, using
  1154. Python functions to run the tool for each purpose (e.g. creating a new
  1155. structure, adding a file to an existing structure) rather than just lists of
  1156. string arguments.
  1157. As with external blobs, bintools (which are like 'external' tools) can be
  1158. missing. When building an image requires a bintool and it is not installed,
  1159. binman detects this and reports the problem, but continues to build an image.
  1160. This is useful in CI systems which want to check that everything is correct but
  1161. don't have access to the bintools.
  1162. To make this work, all calls to bintools (e.g. with Bintool.run_cmd()) must cope
  1163. with the tool being missing, i.e. when None is returned, by:
  1164. - Calling self.record_missing_bintool()
  1165. - Setting up some fake contents so binman can continue
  1166. Of course the image will not work, but binman reports which bintools are needed
  1167. and also provide a way to fetch them.
  1168. To see the available bintools, use::
  1169. binman tool --list
  1170. To fetch tools which are missing, use::
  1171. binman tool --fetch missing
  1172. You can also use `--fetch all` to fetch all tools or `--fetch <tool>` to fetch
  1173. a particular tool. Some tools are built from source code, in which case you will
  1174. need to have at least the `build-essential` and `git` packages installed.
  1175. Tools are fetched into the `~/.binman-tools` directory. This directory is
  1176. automatically added to the toolpath so there is no need to use `--toolpath` to
  1177. specify it. If you want to use these tools outside binman, you may want to
  1178. add this directory to your `PATH`. For example, if you use bash, add this to
  1179. the end of `.bashrc`::
  1180. PATH="$HOME/.binman-tools:$PATH"
  1181. To select a custom directory, use the `--tooldir` option.
  1182. Bintool Documentation
  1183. =====================
  1184. To provide details on the various bintools supported by binman, bintools.rst is
  1185. generated from the source code using:
  1186. binman bintool-docs >tools/binman/bintools.rst
  1187. .. toctree::
  1188. :maxdepth: 2
  1189. bintools
  1190. Binman commands and arguments
  1191. =============================
  1192. Usage::
  1193. binman [-h] [-B BUILD_DIR] [-D] [--tooldir TOOLDIR] [-H]
  1194. [--toolpath TOOLPATH] [-T THREADS] [--test-section-timeout]
  1195. [-v VERBOSITY] [-V]
  1196. {build,bintool-docs,entry-docs,ls,extract,replace,test,tool} ...
  1197. Binman provides the following commands:
  1198. - **build** - build images
  1199. - **bintools-docs** - generate documentation about bintools
  1200. - **entry-docs** - generate documentation about entry types
  1201. - **ls** - list an image
  1202. - **extract** - extract files from an image
  1203. - **replace** - replace one or more entries in an image
  1204. - **test** - run tests
  1205. - **tool** - manage bintools
  1206. Options:
  1207. -h, --help
  1208. Show help message and exit
  1209. -B BUILD_DIR, --build-dir BUILD_DIR
  1210. Directory containing the build output
  1211. -D, --debug
  1212. Enabling debugging (provides a full traceback on error)
  1213. --tooldir TOOLDIR Set the directory to store tools
  1214. -H, --full-help
  1215. Display the README file
  1216. --toolpath TOOLPATH
  1217. Add a path to the list of directories containing tools
  1218. -T THREADS, --threads THREADS
  1219. Number of threads to use (0=single-thread). Note that -T0 is useful for
  1220. debugging since everything runs in one thread.
  1221. -v VERBOSITY, --verbosity VERBOSITY
  1222. Control verbosity: 0=silent, 1=warnings, 2=notices, 3=info, 4=detail,
  1223. 5=debug
  1224. -V, --version
  1225. Show the binman version
  1226. Test options:
  1227. --test-section-timeout
  1228. Use a zero timeout for section multi-threading (for testing)
  1229. Commands are described below.
  1230. binman build
  1231. ------------
  1232. This builds one or more images using the provided image description.
  1233. Usage::
  1234. binman build [-h] [-a ENTRY_ARG] [-b BOARD] [-d DT] [--fake-dtb]
  1235. [--fake-ext-blobs] [--force-missing-bintools FORCE_MISSING_BINTOOLS]
  1236. [-i IMAGE] [-I INDIR] [-m] [-M] [-n] [-O OUTDIR] [-p] [-u]
  1237. [--update-fdt-in-elf UPDATE_FDT_IN_ELF] [-W]
  1238. Options:
  1239. -h, --help
  1240. Show help message and exit
  1241. -a ENTRY_ARG, --entry-arg ENTRY_ARG
  1242. Set argument value `arg=value`. See
  1243. `Passing command-line arguments to entries`_.
  1244. -b BOARD, --board BOARD
  1245. Board name to build. This can be used instead of `-d`, in which case the
  1246. file `u-boot.dtb` is used, within the build directory's board subdirectory.
  1247. -d DT, --dt DT
  1248. Configuration file (.dtb) to use. This must have a top-level node called
  1249. `binman`. See `Image description format`_.
  1250. -i IMAGE, --image IMAGE
  1251. Image filename to build (if not specified, build all)
  1252. -I INDIR, --indir INDIR
  1253. Add a path to the list of directories to use for input files. This can be
  1254. specified multiple times to add more than one path.
  1255. -m, --map
  1256. Output a map file for each image. See `Map files`_.
  1257. -M, --allow-missing
  1258. Allow external blobs and bintools to be missing. See `External blobs`_.
  1259. -n, --no-expanded
  1260. Don't use 'expanded' versions of entries where available; normally 'u-boot'
  1261. becomes 'u-boot-expanded', for example. See `Expanded entries`_.
  1262. -O OUTDIR, --outdir OUTDIR
  1263. Path to directory to use for intermediate and output files
  1264. -p, --preserve
  1265. Preserve temporary output directory even if option -O is not given
  1266. -u, --update-fdt
  1267. Update the binman node with offset/size info. See
  1268. `Access to binman entry offsets at run time (fdt)`_.
  1269. --update-fdt-in-elf UPDATE_FDT_IN_ELF
  1270. Update an ELF file with the output dtb. The argument is a string consisting
  1271. of four parts, separated by commas. See `Updating an ELF file`_.
  1272. -W, --ignore-missing
  1273. Return success even if there are missing blobs/bintools (requires -M)
  1274. Options used only for testing:
  1275. --fake-dtb
  1276. Use fake device tree contents
  1277. --fake-ext-blobs
  1278. Create fake ext blobs with dummy content
  1279. --force-missing-bintools FORCE_MISSING_BINTOOLS
  1280. Comma-separated list of bintools to consider missing
  1281. binman bintool-docs
  1282. -------------------
  1283. Usage::
  1284. binman bintool-docs [-h]
  1285. This outputs documentation for the bintools in rST format. See
  1286. `Bintool Documentation`_.
  1287. binman entry-docs
  1288. -----------------
  1289. Usage::
  1290. binman entry-docs [-h]
  1291. This outputs documentation for the entry types in rST format. See
  1292. `Entry Documentation`_.
  1293. binman ls
  1294. ---------
  1295. Usage::
  1296. binman ls [-h] -i IMAGE [paths ...]
  1297. Positional arguments:
  1298. paths
  1299. Paths within file to list (wildcard)
  1300. Pptions:
  1301. -h, --help
  1302. show help message and exit
  1303. -i IMAGE, --image IMAGE
  1304. Image filename to list
  1305. This lists an image, showing its contents. See `Listing images`_.
  1306. binman extract
  1307. --------------
  1308. Usage::
  1309. binman extract [-h] [-F FORMAT] -i IMAGE [-f FILENAME] [-O OUTDIR] [-U]
  1310. [paths ...]
  1311. Positional arguments:
  1312. Paths
  1313. Paths within file to extract (wildcard)
  1314. Options:
  1315. -h, --help
  1316. show help message and exit
  1317. -F FORMAT, --format FORMAT
  1318. Select an alternative format for extracted data
  1319. -i IMAGE, --image IMAGE
  1320. Image filename to extract
  1321. -f FILENAME, --filename FILENAME
  1322. Output filename to write to
  1323. -O OUTDIR, --outdir OUTDIR
  1324. Path to directory to use for output files
  1325. -U, --uncompressed
  1326. Output raw uncompressed data for compressed entries
  1327. This extracts the contents of entries from an image. See
  1328. `Extracting files from images`_.
  1329. binman replace
  1330. --------------
  1331. Usage::
  1332. binman replace [-h] [-C] -i IMAGE [-f FILENAME] [-F] [-I INDIR] [-m]
  1333. [paths ...]
  1334. Positional arguments:
  1335. paths
  1336. Paths within file to replace (wildcard)
  1337. Options:
  1338. -h, --help
  1339. show help message and exit
  1340. -C, --compressed
  1341. Input data is already compressed if needed for the entry
  1342. -i IMAGE, --image IMAGE
  1343. Image filename to update
  1344. -f FILENAME, --filename FILENAME
  1345. Input filename to read from
  1346. -F, --fix-size
  1347. Don't allow entries to be resized
  1348. -I INDIR, --indir INDIR
  1349. Path to directory to use for input files
  1350. -m, --map
  1351. Output a map file for the updated image
  1352. -O OUTDIR, --outdir OUTDIR
  1353. Path to directory to use for intermediate and output files
  1354. -p, --preserve
  1355. Preserve temporary output directory even if option -O is not given
  1356. This replaces one or more entries in an existing image. See
  1357. `Replacing files in an image`_.
  1358. binman test
  1359. -----------
  1360. Usage::
  1361. binman test [-h] [-P PROCESSES] [-T] [-X] [tests ...]
  1362. Positional arguments:
  1363. tests
  1364. Test names to run (omit for all)
  1365. Options:
  1366. -h, --help
  1367. show help message and exit
  1368. -P PROCESSES, --processes PROCESSES
  1369. set number of processes to use for running tests. This defaults to the
  1370. number of CPUs on the machine
  1371. -T, --test-coverage
  1372. run tests and check for 100% coverage
  1373. -X, --test-preserve-dirs
  1374. Preserve and display test-created input directories; also preserve the
  1375. output directory if a single test is run (pass test name at the end of the
  1376. command line
  1377. binman sign
  1378. -----------
  1379. Usage::
  1380. binman sign [-h] -a ALGO [-f FILE] -i IMAGE -k KEY [paths ...]
  1381. positional arguments:
  1382. paths
  1383. Paths within file to sign (wildcard)
  1384. options:
  1385. -h, --help
  1386. show this help message and exit
  1387. -a ALGO, --algo ALGO
  1388. Hash algorithm e.g. sha256,rsa4096
  1389. -f FILE, --file FILE
  1390. Input filename to sign
  1391. -i IMAGE, --image IMAGE
  1392. Image filename to update
  1393. -k KEY, --key KEY
  1394. Private key file for signing
  1395. binman tool
  1396. -----------
  1397. Usage::
  1398. binman tool [-h] [-l] [-f] [bintools ...]
  1399. Positional arguments:
  1400. bintools
  1401. Bintools to process
  1402. Options:
  1403. -h, --help
  1404. show help message and exit
  1405. -l, --list
  1406. List all known bintools
  1407. -f, --fetch
  1408. Fetch a bintool from a known location. Use `all` to fetch all and `missing`
  1409. to fetch any missing tools.
  1410. Technical details
  1411. =================
  1412. Order of image creation
  1413. -----------------------
  1414. Image creation proceeds in the following order, for each entry in the image.
  1415. 1. AddMissingProperties() - binman can add calculated values to the device
  1416. tree as part of its processing, for example the offset and size of each
  1417. entry. This method adds any properties associated with this, expanding the
  1418. device tree as needed. These properties can have placeholder values which are
  1419. set later by SetCalculatedProperties(). By that stage the size of sections
  1420. cannot be changed (since it would cause the images to need to be repacked),
  1421. but the correct values can be inserted.
  1422. 2. ProcessFdt() - process the device tree information as required by the
  1423. particular entry. This may involve adding or deleting properties. If the
  1424. processing is complete, this method should return True. If the processing
  1425. cannot complete because it needs the ProcessFdt() method of another entry to
  1426. run first, this method should return False, in which case it will be called
  1427. again later.
  1428. 3. GetEntryContents() - the contents of each entry are obtained, normally by
  1429. reading from a file. This calls the Entry.ObtainContents() to read the
  1430. contents. The default version of Entry.ObtainContents() calls
  1431. Entry.GetDefaultFilename() and then reads that file. So a common mechanism
  1432. to select a file to read is to override that function in the subclass. The
  1433. functions must return True when they have read the contents. Binman will
  1434. retry calling the functions a few times if False is returned, allowing
  1435. dependencies between the contents of different entries.
  1436. 4. GetEntryOffsets() - calls Entry.GetOffsets() for each entry. This can
  1437. return a dict containing entries that need updating. The key should be the
  1438. entry name and the value is a tuple (offset, size). This allows an entry to
  1439. provide the offset and size for other entries. The default implementation
  1440. of GetEntryOffsets() returns {}.
  1441. 5. PackEntries() - calls Entry.Pack() which figures out the offset and
  1442. size of an entry. The 'current' image offset is passed in, and the function
  1443. returns the offset immediately after the entry being packed. The default
  1444. implementation of Pack() is usually sufficient.
  1445. Note: for sections, this also checks that the entries do not overlap, nor extend
  1446. outside the section. If the section does not have a defined size, the size is
  1447. set large enough to hold all the entries. For entries that are explicitly marked
  1448. as overlapping, this check is skipped.
  1449. 6. SetImagePos() - sets the image position of every entry. This is the absolute
  1450. position 'image-pos', as opposed to 'offset' which is relative to the containing
  1451. section. This must be done after all offsets are known, which is why it is quite
  1452. late in the ordering.
  1453. 7. SetCalculatedProperties() - update any calculated properties in the device
  1454. tree. This sets the correct 'offset' and 'size' vaues, for example.
  1455. 8. ProcessEntryContents() - this calls Entry.ProcessContents() on each entry.
  1456. The default implementatoin does nothing. This can be overriden to adjust the
  1457. contents of an entry in some way. For example, it would be possible to create
  1458. an entry containing a hash of the contents of some other entries. At this
  1459. stage the offset and size of entries should not be adjusted unless absolutely
  1460. necessary, since it requires a repack (going back to PackEntries()).
  1461. 9. ResetForPack() - if the ProcessEntryContents() step failed, in that an entry
  1462. has changed its size, then there is no alternative but to go back to step 5 and
  1463. try again, repacking the entries with the updated size. ResetForPack() removes
  1464. the fixed offset/size values added by binman, so that the packing can start from
  1465. scratch.
  1466. 10. WriteSymbols() - write the value of symbols into the U-Boot SPL binary.
  1467. See 'Access to binman entry offsets at run time' below for a description of
  1468. what happens in this stage.
  1469. 11. BuildImage() - builds the image and writes it to a file
  1470. 12. WriteMap() - writes a text file containing a map of the image. This is the
  1471. final step.
  1472. .. _`External tools`:
  1473. External tools
  1474. --------------
  1475. Binman can make use of external command-line tools to handle processing of
  1476. entry contents or to generate entry contents. These tools are executed using
  1477. the 'tools' module's Run() method. The tools generally must exist on the PATH,
  1478. but the --toolpath option can be used to specify additional search paths to
  1479. use. This option can be specified multiple times to add more than one path.
  1480. For some compile tools binman will use the versions specified by commonly-used
  1481. environment variables like CC and HOSTCC for the C compiler, based on whether
  1482. the tool's output will be used for the target or for the host machine. If those
  1483. aren't given, it will also try to derive target-specific versions from the
  1484. CROSS_COMPILE environment variable during a cross-compilation.
  1485. If the tool is not available in the path you can use BINMAN_TOOLPATHS to specify
  1486. a space-separated list of paths to search, e.g.::
  1487. BINMAN_TOOLPATHS="/tools/g12a /tools/tegra" binman ...
  1488. .. _`External blobs`:
  1489. External blobs
  1490. --------------
  1491. Binary blobs, even if the source code is available, complicate building
  1492. firmware. The instructions can involve multiple steps and the binaries may be
  1493. hard to build or obtain. Binman at least provides a unified description of how
  1494. to build the final image, no matter what steps are needed to get there.
  1495. Binman also provides a `blob-ext` entry type that pulls in a binary blob from an
  1496. external file. If the file is missing, binman can optionally complete the build
  1497. and just report a warning. Use the `-M/--allow-missing` option to enble this.
  1498. This is useful in CI systems which want to check that everything is correct but
  1499. don't have access to the blobs.
  1500. If the blobs are in a different directory, you can specify this with the `-I`
  1501. option.
  1502. For U-Boot, you can use set the BINMAN_INDIRS environment variable to provide a
  1503. space-separated list of directories to search for binary blobs::
  1504. BINMAN_INDIRS="odroid-c4/fip/g12a \
  1505. odroid-c4/build/board/hardkernel/odroidc4/firmware \
  1506. odroid-c4/build/scp_task" binman ...
  1507. Note that binman fails with exit code 103 when there are missing blobs. If you
  1508. wish binman to continue anyway, you can pass `-W` to binman.
  1509. Code coverage
  1510. -------------
  1511. Binman is a critical tool and is designed to be very testable. Entry
  1512. implementations target 100% test coverage. Run 'binman test -T' to check this.
  1513. To enable Python test coverage on Debian-type distributions (e.g. Ubuntu)::
  1514. $ sudo apt-get install python-coverage python3-coverage python-pytest
  1515. Exit status
  1516. -----------
  1517. Binman produces the following exit codes:
  1518. 0
  1519. Success
  1520. 1
  1521. Any sort of failure - see output for more details
  1522. 103
  1523. There are missing external blobs or bintools. This is only returned if
  1524. -M is passed to binman, otherwise missing blobs return an exit status of 1.
  1525. Note, if -W is passed as well as -M, then this is converted into a warning
  1526. and will return an exit status of 0 instead.
  1527. U-Boot environment variables for binman
  1528. ---------------------------------------
  1529. The U-Boot Makefile supports various environment variables to control binman.
  1530. All of these are set within the Makefile and result in passing various
  1531. environment variables (or make flags) to binman:
  1532. BINMAN_DEBUG
  1533. Enables backtrace debugging by adding a `-D` argument. See
  1534. :ref:`BinmanLogging`.
  1535. BINMAN_INDIRS
  1536. Sets the search path for input files used by binman by adding one or more
  1537. `-I` arguments. See :ref:`External blobs`.
  1538. BINMAN_TOOLPATHS
  1539. Sets the search path for external tool used by binman by adding one or more
  1540. `--toolpath` arguments. See :ref:`External tools`.
  1541. BINMAN_VERBOSE
  1542. Sets the logging verbosity of binman by adding a `-v` argument. See
  1543. :ref:`BinmanLogging`.
  1544. Error messages
  1545. --------------
  1546. This section provides some guidance for some of the less obvious error messages
  1547. produced by binman.
  1548. Expected __bss_size symbol
  1549. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  1550. Example::
  1551. binman: Node '/binman/u-boot-spl-ddr/u-boot-spl/u-boot-spl-bss-pad':
  1552. Expected __bss_size symbol in spl/u-boot-spl
  1553. This indicates that binman needs the `__bss_size` symbol to be defined in the
  1554. SPL binary, where `spl/u-boot-spl` is the ELF file containing the symbols. The
  1555. symbol tells binman the size of the BSS region, in bytes. It needs this to be
  1556. able to pad the image so that the following entries do not overlap the BSS,
  1557. which would cause them to be overwritte by variable access in SPL.
  1558. This symbols is normally defined in the linker script, immediately after
  1559. _bss_start and __bss_end are defined, like this::
  1560. __bss_size = __bss_end - __bss_start;
  1561. You may need to add it to your linker script if you get this error.
  1562. Concurrent tests
  1563. ----------------
  1564. Binman tries to run tests concurrently. This means that the tests make use of
  1565. all available CPUs to run.
  1566. To enable this::
  1567. $ sudo apt-get install python-subunit python3-subunit
  1568. Use '-P 1' to disable this. It is automatically disabled when code coverage is
  1569. being used (-T) since they are incompatible.
  1570. Debugging tests
  1571. ---------------
  1572. Sometimes when debugging tests it is useful to keep the input and output
  1573. directories so they can be examined later. Use -X or --test-preserve-dirs for
  1574. this.
  1575. Running tests on non-x86 architectures
  1576. --------------------------------------
  1577. Binman's tests have been written under the assumption that they'll be run on a
  1578. x86-like host and there hasn't been an attempt to make them portable yet.
  1579. However, it's possible to run the tests by cross-compiling to x86.
  1580. To install an x86 cross-compiler on Debian-type distributions (e.g. Ubuntu)::
  1581. $ sudo apt-get install gcc-x86-64-linux-gnu
  1582. Then, you can run the tests under cross-compilation::
  1583. $ CROSS_COMPILE=x86_64-linux-gnu- binman test -T
  1584. You can also use gcc-i686-linux-gnu similar to the above.
  1585. Writing new entries and debugging
  1586. ---------------------------------
  1587. The behaviour of entries is defined by the Entry class. All other entries are
  1588. a subclass of this. An important subclass is Entry_blob which takes binary
  1589. data from a file and places it in the entry. In fact most entry types are
  1590. subclasses of Entry_blob.
  1591. Each entry type is a separate file in the tools/binman/etype directory. Each
  1592. file contains a class called Entry_<type> where <type> is the entry type.
  1593. New entry types can be supported by adding new files in that directory.
  1594. These will automatically be detected by binman when needed.
  1595. Entry properties are documented in entry.py. The entry subclasses are free
  1596. to change the values of properties to support special behaviour. For example,
  1597. when Entry_blob loads a file, it sets content_size to the size of the file.
  1598. Entry classes can adjust other entries. For example, an entry that knows
  1599. where other entries should be positioned can set up those entries' offsets
  1600. so they don't need to be set in the binman decription. It can also adjust
  1601. entry contents.
  1602. Most of the time such essoteric behaviour is not needed, but it can be
  1603. essential for complex images.
  1604. If you need to specify a particular device-tree compiler to use, you can define
  1605. the DTC environment variable. This can be useful when the system dtc is too
  1606. old.
  1607. To enable a full backtrace and other debugging features in binman, pass
  1608. BINMAN_DEBUG=1 to your build::
  1609. make qemu-x86_defconfig
  1610. make BINMAN_DEBUG=1
  1611. To enable verbose logging from binman, base BINMAN_VERBOSE to your build, which
  1612. adds a -v<level> option to the call to binman::
  1613. make qemu-x86_defconfig
  1614. make BINMAN_VERBOSE=5
  1615. Building sections in parallel
  1616. -----------------------------
  1617. By default binman uses multiprocessing to speed up compilation of large images.
  1618. This works at a section level, with one thread for each entry in the section.
  1619. This can speed things up if the entries are large and use compression.
  1620. This feature can be disabled with the '-T' flag, which defaults to a suitable
  1621. value for your machine. This depends on the Python version, e.g on v3.8 it uses
  1622. 12 threads on an 8-core machine. See ConcurrentFutures_ for more details.
  1623. The special value -T0 selects single-threaded mode, useful for debugging during
  1624. development, since dealing with exceptions and problems in threads is more
  1625. difficult. This avoids any use of ThreadPoolExecutor.
  1626. Collecting data for an entry type
  1627. ---------------------------------
  1628. Some entry types deal with data obtained from others. For example,
  1629. `Entry_mkimage` calls the `mkimage` tool with data from its subnodes::
  1630. mkimage {
  1631. args = "-n test -T script";
  1632. u-boot-spl {
  1633. };
  1634. u-boot {
  1635. };
  1636. };
  1637. This shows mkimage being passed a file consisting of SPL and U-Boot proper. It
  1638. is created by calling `Entry.collect_contents_to_file()`. Note that in this
  1639. case, the data is passed to mkimage for processing but does not appear
  1640. separately in the image. It may not appear at all, depending on what mkimage
  1641. does. The contents of the `mkimage` entry are entirely dependent on the
  1642. processing done by the entry, with the provided subnodes (`u-boot-spl` and
  1643. `u-boot`) simply providing the input data for that processing.
  1644. Note that `Entry.collect_contents_to_file()` simply concatenates the data from
  1645. the different entries together, with no control over alignment, etc. Another
  1646. approach is to subclass `Entry_section` so that those features become available,
  1647. such as `size` and `pad-byte`. Then the contents of the entry can be obtained by
  1648. calling `super().BuildSectionData()` in the entry's BuildSectionData()
  1649. implementation to get the input data, then write it to a file and process it
  1650. however is desired.
  1651. There are other ways to obtain data also, depending on the situation. If the
  1652. entry type is simply signing data which exists elsewhere in the image, then
  1653. you can use `Entry_collection` as a base class. It lets you use a property
  1654. called `content` which lists the entries containing data to be processed. This
  1655. is used by `Entry_vblock`, for example::
  1656. u_boot: u-boot {
  1657. };
  1658. vblock {
  1659. content = <&u_boot &dtb>;
  1660. keyblock = "firmware.keyblock";
  1661. signprivate = "firmware_data_key.vbprivk";
  1662. version = <1>;
  1663. kernelkey = "kernel_subkey.vbpubk";
  1664. preamble-flags = <1>;
  1665. };
  1666. dtb: u-boot-dtb {
  1667. };
  1668. which shows an image containing `u-boot` and `u-boot-dtb`, with the `vblock`
  1669. image collecting their contents to produce input for its signing process,
  1670. without affecting those entries, which still appear in the final image
  1671. untouched.
  1672. Another example is where an entry type needs several independent pieces of input
  1673. to function. For example, `Entry_fip` allows a number of different binary blobs
  1674. to be placed in their own individual places in a custom data structure in the
  1675. output image. To make that work you can add subnodes for each of them and call
  1676. `Entry.Create()` on each subnode, as `Entry_fip` does. Then the data for each
  1677. blob can come from any suitable place, such as an `Entry_u_boot` or an
  1678. `Entry_blob` or anything else::
  1679. atf-fip {
  1680. fip-hdr-flags = /bits/ 64 <0x123>;
  1681. soc-fw {
  1682. fip-flags = /bits/ 64 <0x123456789abcdef>;
  1683. filename = "bl31.bin";
  1684. };
  1685. u-boot {
  1686. fip-uuid = [fc 65 13 92 4a 5b 11 ec
  1687. 94 35 ff 2d 1c fc 79 9c];
  1688. };
  1689. };
  1690. The `soc-fw` node is a `blob-ext` (i.e. it reads in a named binary file) whereas
  1691. `u-boot` is a normal entry type. This works because `Entry_fip` selects the
  1692. `blob-ext` entry type if the node name (here `soc-fw`) is recognised as being
  1693. a known blob type.
  1694. When adding new entry types you are encouraged to use subnodes to provide the
  1695. data for processing, unless the `content` approach is more suitable. Consider
  1696. whether the input entries are contained within (or consumed by) the entry, vs
  1697. just being 'referenced' by the entry. In the latter case, the `content` approach
  1698. makes more sense. Ad-hoc properties and other methods of obtaining data are
  1699. discouraged, since it adds to confusion for users.
  1700. History / Credits
  1701. -----------------
  1702. Binman takes a lot of inspiration from a Chrome OS tool called
  1703. 'cros_bundle_firmware', which I wrote some years ago. That tool was based on
  1704. a reasonably simple and sound design but has expanded greatly over the
  1705. years. In particular its handling of x86 images is convoluted.
  1706. Quite a few lessons have been learned which are hopefully applied here.
  1707. Design notes
  1708. ------------
  1709. On the face of it, a tool to create firmware images should be fairly simple:
  1710. just find all the input binaries and place them at the right place in the
  1711. image. The difficulty comes from the wide variety of input types (simple
  1712. flat binaries containing code, packaged data with various headers), packing
  1713. requirments (alignment, spacing, device boundaries) and other required
  1714. features such as hierarchical images.
  1715. The design challenge is to make it easy to create simple images, while
  1716. allowing the more complex cases to be supported. For example, for most
  1717. images we don't much care exactly where each binary ends up, so we should
  1718. not have to specify that unnecessarily.
  1719. New entry types should aim to provide simple usage where possible. If new
  1720. core features are needed, they can be added in the Entry base class.
  1721. To do
  1722. -----
  1723. Some ideas:
  1724. - Use of-platdata to make the information available to code that is unable
  1725. to use device tree (such as a very small SPL image). For now, limited info is
  1726. available via linker symbols
  1727. - Allow easy building of images by specifying just the board name
  1728. - Support building an image for a board (-b) more completely, with a
  1729. configurable build directory
  1730. - Detect invalid properties in nodes
  1731. - Sort the fdtmap by offset
  1732. - Output temporary files to a different directory
  1733. - Rationalise the fdt, fdt_util and pylibfdt modules which currently have some
  1734. overlapping and confusing functionality
  1735. - Update the fdt library to use a better format for Prop.value (the current one
  1736. is useful for dtoc but not much else)
  1737. - Figure out how to make Fdt support changing the node order, so that
  1738. Node.AddSubnode() can support adding a node before another, existing node.
  1739. Perhaps it should completely regenerate the flat tree?
  1740. - Support images which depend on each other
  1741. --
  1742. Simon Glass <sjg@chromium.org>
  1743. 7/7/2016
  1744. .. _ConcurrentFutures: https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.ThreadPoolExecutor