README 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. # Copyright (c) 2016 Google, Inc
  2. #
  3. # SPDX-License-Identifier: GPL-2.0+
  4. #
  5. Introduction
  6. ------------
  7. Firmware often consists of several components which must be packaged together.
  8. For example, we may have SPL, U-Boot, a device tree and an environment area
  9. grouped together and placed in MMC flash. When the system starts, it must be
  10. able to find these pieces.
  11. So far U-Boot has not provided a way to handle creating such images in a
  12. general way. Each SoC does what it needs to build an image, often packing or
  13. concatenating images in the U-Boot build system.
  14. Binman aims to provide a mechanism for building images, from simple
  15. SPL + U-Boot combinations, to more complex arrangements with many parts.
  16. What it does
  17. ------------
  18. Binman reads your board's device tree and finds a node which describes the
  19. required image layout. It uses this to work out what to place where. The
  20. output file normally contains the device tree, so it is in principle possible
  21. to read an image and extract its constituent parts.
  22. Features
  23. --------
  24. So far binman is pretty simple. It supports binary blobs, such as 'u-boot',
  25. 'spl' and 'fdt'. It supports empty entries (such as setting to 0xff). It can
  26. place entries at a fixed location in the image, or fit them together with
  27. suitable padding and alignment. It provides a way to process binaries before
  28. they are included, by adding a Python plug-in. The device tree is available
  29. to U-Boot at run-time so that the images can be interpreted.
  30. Binman does not yet update the device tree with the final location of
  31. everything when it is done. A simple C structure could be generated for
  32. constrained environments like SPL (using dtoc) but this is also not
  33. implemented.
  34. Binman can also support incorporating filesystems in the image if required.
  35. For example x86 platforms may use CBFS in some cases.
  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. Packaging of firmware is quite a different task from building the various
  41. parts. In many cases the various binaries which go into the image come from
  42. separate build systems. For example, ARM Trusted Firmware is used on ARMv8
  43. devices but is not built in the U-Boot tree. If a Linux kernel is included
  44. 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 accomodated
  61. - Avoids cluttering the U-Boot build system with image-building code
  62. - The image description is automatically available at run-time in U-Boot,
  63. SPL. It can be made available to other software also
  64. - The image description is easily readable (it's a text file in device-tree
  65. format) and permits flexible packing of binaries
  66. Terminology
  67. -----------
  68. Binman uses the following terms:
  69. - image - an output file containing a firmware image
  70. - binary - an input binary that goes into the image
  71. Relationship to FIT
  72. -------------------
  73. FIT is U-Boot's official image format. It supports multiple binaries with
  74. load / execution addresses, compression. It also supports verification
  75. through hashing and RSA signatures.
  76. FIT was originally designed to support booting a Linux kernel (with an
  77. optional ramdisk) and device tree chosen from various options in the FIT.
  78. Now that U-Boot supports configuration via device tree, it is possible to
  79. load U-Boot from a FIT, with the device tree chosen by SPL.
  80. Binman considers FIT to be one of the binaries it can place in the image.
  81. Where possible it is best to put as much as possible in the FIT, with binman
  82. used to deal with cases not covered by FIT. Examples include initial
  83. execution (since FIT itself does not have an executable header) and dealing
  84. with device boundaries, such as the read-only/read-write separation in SPI
  85. flash.
  86. For U-Boot, binman should not be used to create ad-hoc images in place of
  87. FIT.
  88. Relationship to mkimage
  89. -----------------------
  90. The mkimage tool provides a means to create a FIT. Traditionally it has
  91. needed an image description file: a device tree, like binman, but in a
  92. different format. More recently it has started to support a '-f auto' mode
  93. which can generate that automatically.
  94. More relevant to binman, mkimage also permits creation of many SoC-specific
  95. image types. These can be listed by running 'mkimage -T list'. Examples
  96. include 'rksd', the Rockchip SD/MMC boot format. The mkimage tool is often
  97. called from the U-Boot build system for this reason.
  98. Binman considers the output files created by mkimage to be binary blobs
  99. which it can place in an image. Binman does not replace the mkimage tool or
  100. this purpose. It would be possible in some situtions to create a new entry
  101. type for the images in mkimage, but this would not add functionality. It
  102. seems better to use the mkiamge tool to generate binaries and avoid blurring
  103. the boundaries between building input files (mkimage) and packaging then
  104. into a final image (binman).
  105. Example use of binman in U-Boot
  106. -------------------------------
  107. Binman aims to replace some of the ad-hoc image creation in the U-Boot
  108. build system.
  109. Consider sunxi. It has the following steps:
  110. 1. It uses a custom mksunxiboot tool to build an SPL image called
  111. sunxi-spl.bin. This should probably move into mkimage.
  112. 2. It uses mkimage to package U-Boot into a legacy image file (so that it can
  113. hold the load and execution address) called u-boot.img.
  114. 3. It builds a final output image called u-boot-sunxi-with-spl.bin which
  115. consists of sunxi-spl.bin, some padding and u-boot.img.
  116. Binman is intended to replace the last step. The U-Boot build system builds
  117. u-boot.bin and sunxi-spl.bin. Binman can then take over creation of
  118. sunxi-spl.bin (by calling mksunxiboot, or hopefully one day mkimage). In any
  119. case, it would then create the image from the component parts.
  120. This simplifies the U-Boot Makefile somewhat, since various pieces of logic
  121. can be replaced by a call to binman.
  122. Example use of binman for x86
  123. -----------------------------
  124. In most cases x86 images have a lot of binary blobs, 'black-box' code
  125. provided by Intel which must be run for the platform to work. Typically
  126. these blobs are not relocatable and must be placed at fixed areas in the
  127. firmare image.
  128. Currently this is handled by ifdtool, which places microcode, FSP, MRC, VGA
  129. BIOS, reference code and Intel ME binaries into a u-boot.rom file.
  130. Binman is intended to replace all of this, with ifdtool left to handle only
  131. the configuration of the Intel-format descriptor.
  132. Running binman
  133. --------------
  134. Type:
  135. binman -b <board_name>
  136. to build an image for a board. The board name is the same name used when
  137. configuring U-Boot (e.g. for sandbox_defconfig the board name is 'sandbox').
  138. Binman assumes that the input files for the build are in ../b/<board_name>.
  139. Or you can specify this explicitly:
  140. binman -I <build_path>
  141. where <build_path> is the build directory containing the output of the U-Boot
  142. build.
  143. (Future work will make this more configurable)
  144. In either case, binman picks up the device tree file (u-boot.dtb) and looks
  145. for its instructions in the 'binman' node.
  146. Binman has a few other options which you can see by running 'binman -h'.
  147. Image description format
  148. ------------------------
  149. The binman node is called 'binman'. An example image description is shown
  150. below:
  151. binman {
  152. filename = "u-boot-sunxi-with-spl.bin";
  153. pad-byte = <0xff>;
  154. blob {
  155. filename = "spl/sunxi-spl.bin";
  156. };
  157. u-boot {
  158. pos = <CONFIG_SPL_PAD_TO>;
  159. };
  160. };
  161. This requests binman to create an image file called u-boot-sunxi-with-spl.bin
  162. consisting of a specially formatted SPL (spl/sunxi-spl.bin, built by the
  163. normal U-Boot Makefile), some 0xff padding, and a U-Boot legacy image. The
  164. padding comes from the fact that the second binary is placed at
  165. CONFIG_SPL_PAD_TO. If that line were omitted then the U-Boot binary would
  166. immediately follow the SPL binary.
  167. The binman node describes an image. The sub-nodes describe entries in the
  168. image. Each entry represents a region within the overall image. The name of
  169. the entry (blob, u-boot) tells binman what to put there. For 'blob' we must
  170. provide a filename. For 'u-boot', binman knows that this means 'u-boot.bin'.
  171. Entries are normally placed into the image sequentially, one after the other.
  172. The image size is the total size of all entries. As you can see, you can
  173. specify the start position of an entry using the 'pos' property.
  174. Note that due to a device tree requirement, all entries must have a unique
  175. name. If you want to put the same binary in the image multiple times, you can
  176. use any unique name, with the 'type' property providing the type.
  177. The attributes supported for entries are described below.
  178. pos:
  179. This sets the position of an entry within the image. The first byte
  180. of the image is normally at position 0. If 'pos' is not provided,
  181. binman sets it to the end of the previous region, or the start of
  182. the image's entry area (normally 0) if there is no previous region.
  183. align:
  184. This sets the alignment of the entry. The entry position is adjusted
  185. so that the entry starts on an aligned boundary within the image. For
  186. example 'align = <16>' means that the entry will start on a 16-byte
  187. boundary. Alignment shold be a power of 2. If 'align' is not
  188. provided, no alignment is performed.
  189. size:
  190. This sets the size of the entry. The contents will be padded out to
  191. this size. If this is not provided, it will be set to the size of the
  192. contents.
  193. pad-before:
  194. Padding before the contents of the entry. Normally this is 0, meaning
  195. that the contents start at the beginning of the entry. This can be
  196. offset the entry contents a little. Defaults to 0.
  197. pad-after:
  198. Padding after the contents of the entry. Normally this is 0, meaning
  199. that the entry ends at the last byte of content (unless adjusted by
  200. other properties). This allows room to be created in the image for
  201. this entry to expand later. Defaults to 0.
  202. align-size:
  203. This sets the alignment of the entry size. For example, to ensure
  204. that the size of an entry is a multiple of 64 bytes, set this to 64.
  205. If 'align-size' is not provided, no alignment is performed.
  206. align-end:
  207. This sets the alignment of the end of an entry. Some entries require
  208. that they end on an alignment boundary, regardless of where they
  209. start. If 'align-end' is not provided, no alignment is performed.
  210. Note: This is not yet implemented in binman.
  211. filename:
  212. For 'blob' types this provides the filename containing the binary to
  213. put into the entry. If binman knows about the entry type (like
  214. u-boot-bin), then there is no need to specify this.
  215. type:
  216. Sets the type of an entry. This defaults to the entry name, but it is
  217. possible to use any name, and then add (for example) 'type = "u-boot"'
  218. to specify the type.
  219. The attributes supported for images are described below. Several are similar
  220. to those for entries.
  221. size:
  222. Sets the image size in bytes, for example 'size = <0x100000>' for a
  223. 1MB image.
  224. align-size:
  225. This sets the alignment of the image size. For example, to ensure
  226. that the image ends on a 512-byte boundary, use 'align-size = <512>'.
  227. If 'align-size' is not provided, no alignment is performed.
  228. pad-before:
  229. This sets the padding before the image entries. The first entry will
  230. be positionad after the padding. This defaults to 0.
  231. pad-after:
  232. This sets the padding after the image entries. The padding will be
  233. placed after the last entry. This defaults to 0.
  234. pad-byte:
  235. This specifies the pad byte to use when padding in the image. It
  236. defaults to 0. To use 0xff, you would add 'pad-byte = <0xff>'.
  237. filename:
  238. This specifies the image filename. It defaults to 'image.bin'.
  239. sort-by-pos:
  240. This causes binman to reorder the entries as needed to make sure they
  241. are in increasing positional order. This can be used when your entry
  242. order may not match the positional order. A common situation is where
  243. the 'pos' properties are set by CONFIG options, so their ordering is
  244. not known a priori.
  245. This is a boolean property so needs no value. To enable it, add a
  246. line 'sort-by-pos;' to your description.
  247. multiple-images:
  248. Normally only a single image is generated. To create more than one
  249. image, put this property in the binman node. For example, this will
  250. create image1.bin containing u-boot.bin, and image2.bin containing
  251. both spl/u-boot-spl.bin and u-boot.bin:
  252. binman {
  253. multiple-images;
  254. image1 {
  255. u-boot {
  256. };
  257. };
  258. image2 {
  259. spl {
  260. };
  261. u-boot {
  262. };
  263. };
  264. };
  265. end-at-4gb:
  266. For x86 machines the ROM positions start just before 4GB and extend
  267. up so that the image finished at the 4GB boundary. This boolean
  268. option can be enabled to support this. The image size must be
  269. provided so that binman knows when the image should start. For an
  270. 8MB ROM, the position of the first entry would be 0xfff80000 with
  271. this option, instead of 0 without this option.
  272. Examples of the above options can be found in the tests. See the
  273. tools/binman/test directory.
  274. Order of image creation
  275. -----------------------
  276. Image creation proceeds in the following order, for each entry in the image.
  277. 1. GetEntryContents() - the contents of each entry are obtained, normally by
  278. reading from a file. This calls the Entry.ObtainContents() to read the
  279. contents. The default version of Entry.ObtainContents() calls
  280. Entry.GetDefaultFilename() and then reads that file. So a common mechanism
  281. to select a file to read is to override that function in the subclass. The
  282. functions must return True when they have read the contents. Binman will
  283. retry calling the functions a few times if False is returned, allowing
  284. dependencies between the contents of different entries.
  285. 2. GetEntryPositions() - calls Entry.GetPositions() for each entry. This can
  286. return a dict containing entries that need updating. The key should be the
  287. entry name and the value is a tuple (pos, size). This allows an entry to
  288. provide the position and size for other entries. The default implementation
  289. of GetEntryPositions() returns {}.
  290. 3. PackEntries() - calls Entry.Pack() which figures out the position and
  291. size of an entry. The 'current' image position is passed in, and the function
  292. returns the position immediately after the entry being packed. The default
  293. implementation of Pack() is usually sufficient.
  294. 4. CheckSize() - checks that the contents of all the entries fits within
  295. the image size. If the image does not have a defined size, the size is set
  296. large enough to hold all the entries.
  297. 5. CheckEntries() - checks that the entries do not overlap, nor extend
  298. outside the image.
  299. 6. ProcessEntryContents() - this calls Entry.ProcessContents() on each entry.
  300. The default implementatoin does nothing. This can be overriden to adjust the
  301. contents of an entry in some way. For example, it would be possible to create
  302. an entry containing a hash of the contents of some other entries. At this
  303. stage the position and size of entries should not be adjusted.
  304. 7. BuildImage() - builds the image and writes it to a file. This is the final
  305. step.
  306. Advanced Features / Technical docs
  307. ----------------------------------
  308. The behaviour of entries is defined by the Entry class. All other entries are
  309. a subclass of this. An important subclass is Entry_blob which takes binary
  310. data from a file and places it in the entry. In fact most entry types are
  311. subclasses of Entry_blob.
  312. Each entry type is a separate file in the tools/binman/etype directory. Each
  313. file contains a class called Entry_<type> where <type> is the entry type.
  314. New entry types can be supported by adding new files in that directory.
  315. These will automatically be detected by binman when needed.
  316. Entry properties are documented in entry.py. The entry subclasses are free
  317. to change the values of properties to support special behaviour. For example,
  318. when Entry_blob loads a file, it sets content_size to the size of the file.
  319. Entry classes can adjust other entries. For example, an entry that knows
  320. where other entries should be positioned can set up those entries' positions
  321. so they don't need to be set in the binman decription. It can also adjust
  322. entry contents.
  323. Most of the time such essoteric behaviour is not needed, but it can be
  324. essential for complex images.
  325. History / Credits
  326. -----------------
  327. Binman takes a lot of inspiration from a Chrome OS tool called
  328. 'cros_bundle_firmware', which I wrote some years ago. That tool was based on
  329. a reasonably simple and sound design but has expanded greatly over the
  330. years. In particular its handling of x86 images is convoluted.
  331. Quite a few lessons have been learned which are hopefully be applied here.
  332. Design notes
  333. ------------
  334. On the face of it, a tool to create firmware images should be fairly simple:
  335. just find all the input binaries and place them at the right place in the
  336. image. The difficulty comes from the wide variety of input types (simple
  337. flat binaries containing code, packaged data with various headers), packing
  338. requirments (alignment, spacing, device boundaries) and other required
  339. features such as hierarchical images.
  340. The design challenge is to make it easy to create simple images, while
  341. allowing the more complex cases to be supported. For example, for most
  342. images we don't much care exactly where each binary ends up, so we should
  343. not have to specify that unnecessarily.
  344. New entry types should aim to provide simple usage where possible. If new
  345. core features are needed, they can be added in the Entry base class.
  346. To do
  347. -----
  348. Some ideas:
  349. - Fill out the device tree to include the final position and size of each
  350. entry (since the input file may not always specify these)
  351. - Use of-platdata to make the information available to code that is unable
  352. to use device tree (such as a very small SPL image)
  353. - Write an image map to a text file
  354. - Allow easy building of images by specifying just the board name
  355. - Produce a full Python binding for libfdt (for upstream)
  356. - Add an option to decode an image into the constituent binaries
  357. - Suppoort hierarchical images (packing of binaries into another binary
  358. which is then placed in the image)
  359. - Support building an image for a board (-b) more completely, with a
  360. configurable build directory
  361. - Consider making binman work with buildman, although if it is used in the
  362. Makefile, this will be automatic
  363. - Implement align-end
  364. --
  365. Simon Glass <sjg@chromium.org>
  366. 7/7/2016