cbfs.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. # SPDX-License-Identifier: GPL-2.0+
  2. # Copyright 2019 Google LLC
  3. # Written by Simon Glass <sjg@chromium.org>
  4. #
  5. # Entry-type module for a Coreboot Filesystem (CBFS)
  6. #
  7. from collections import OrderedDict
  8. from binman import cbfs_util
  9. from binman.cbfs_util import CbfsWriter
  10. from binman.entry import Entry
  11. from dtoc import fdt_util
  12. class Entry_cbfs(Entry):
  13. """Entry containing a Coreboot Filesystem (CBFS)
  14. A CBFS provides a way to group files into a group. It has a simple directory
  15. structure and allows the position of individual files to be set, since it is
  16. designed to support execute-in-place in an x86 SPI-flash device. Where XIP
  17. is not used, it supports compression and storing ELF files.
  18. CBFS is used by coreboot as its way of orgnanising SPI-flash contents.
  19. The contents of the CBFS are defined by subnodes of the cbfs entry, e.g.::
  20. cbfs {
  21. size = <0x100000>;
  22. u-boot {
  23. cbfs-type = "raw";
  24. };
  25. u-boot-dtb {
  26. cbfs-type = "raw";
  27. };
  28. };
  29. This creates a CBFS 1MB in size two files in it: u-boot.bin and u-boot.dtb.
  30. Note that the size is required since binman does not support calculating it.
  31. The contents of each entry is just what binman would normally provide if it
  32. were not a CBFS node. A blob type can be used to import arbitrary files as
  33. with the second subnode below::
  34. cbfs {
  35. size = <0x100000>;
  36. u-boot {
  37. cbfs-name = "BOOT";
  38. cbfs-type = "raw";
  39. };
  40. dtb {
  41. type = "blob";
  42. filename = "u-boot.dtb";
  43. cbfs-type = "raw";
  44. cbfs-compress = "lz4";
  45. cbfs-offset = <0x100000>;
  46. };
  47. };
  48. This creates a CBFS 1MB in size with u-boot.bin (named "BOOT") and
  49. u-boot.dtb (named "dtb") and compressed with the lz4 algorithm.
  50. Properties supported in the top-level CBFS node:
  51. cbfs-arch:
  52. Defaults to "x86", but you can specify the architecture if needed.
  53. Properties supported in the CBFS entry subnodes:
  54. cbfs-name:
  55. This is the name of the file created in CBFS. It defaults to the entry
  56. name (which is the node name), but you can override it with this
  57. property.
  58. cbfs-type:
  59. This is the CBFS file type. The following are supported:
  60. raw:
  61. This is a 'raw' file, although compression is supported. It can be
  62. used to store any file in CBFS.
  63. stage:
  64. This is an ELF file that has been loaded (i.e. mapped to memory), so
  65. appears in the CBFS as a flat binary. The input file must be an ELF
  66. image, for example this puts "u-boot" (the ELF image) into a 'stage'
  67. entry::
  68. cbfs {
  69. size = <0x100000>;
  70. u-boot-elf {
  71. cbfs-name = "BOOT";
  72. cbfs-type = "stage";
  73. };
  74. };
  75. You can use your own ELF file with something like::
  76. cbfs {
  77. size = <0x100000>;
  78. something {
  79. type = "blob";
  80. filename = "cbfs-stage.elf";
  81. cbfs-type = "stage";
  82. };
  83. };
  84. As mentioned, the file is converted to a flat binary, so it is
  85. equivalent to adding "u-boot.bin", for example, but with the load and
  86. start addresses specified by the ELF. At present there is no option
  87. to add a flat binary with a load/start address, similar to the
  88. 'add-flat-binary' option in cbfstool.
  89. cbfs-offset:
  90. This is the offset of the file's data within the CBFS. It is used to
  91. specify where the file should be placed in cases where a fixed position
  92. is needed. Typical uses are for code which is not relocatable and must
  93. execute in-place from a particular address. This works because SPI flash
  94. is generally mapped into memory on x86 devices. The file header is
  95. placed before this offset so that the data start lines up exactly with
  96. the chosen offset. If this property is not provided, then the file is
  97. placed in the next available spot.
  98. The current implementation supports only a subset of CBFS features. It does
  99. not support other file types (e.g. payload), adding multiple files (like the
  100. 'files' entry with a pattern supported by binman), putting files at a
  101. particular offset in the CBFS and a few other things.
  102. Of course binman can create images containing multiple CBFSs, simply by
  103. defining these in the binman config::
  104. binman {
  105. size = <0x800000>;
  106. cbfs {
  107. offset = <0x100000>;
  108. size = <0x100000>;
  109. u-boot {
  110. cbfs-type = "raw";
  111. };
  112. u-boot-dtb {
  113. cbfs-type = "raw";
  114. };
  115. };
  116. cbfs2 {
  117. offset = <0x700000>;
  118. size = <0x100000>;
  119. u-boot {
  120. cbfs-type = "raw";
  121. };
  122. u-boot-dtb {
  123. cbfs-type = "raw";
  124. };
  125. image {
  126. type = "blob";
  127. filename = "image.jpg";
  128. };
  129. };
  130. };
  131. This creates an 8MB image with two CBFSs, one at offset 1MB, one at 7MB,
  132. both of size 1MB.
  133. """
  134. def __init__(self, section, etype, node):
  135. # Put this here to allow entry-docs and help to work without libfdt
  136. global state
  137. from binman import state
  138. super().__init__(section, etype, node)
  139. self._cbfs_arg = fdt_util.GetString(node, 'cbfs-arch', 'x86')
  140. self._cbfs_entries = OrderedDict()
  141. self._ReadSubnodes()
  142. self.reader = None
  143. def ObtainContents(self, skip=None):
  144. arch = cbfs_util.find_arch(self._cbfs_arg)
  145. if arch is None:
  146. self.Raise("Invalid architecture '%s'" % self._cbfs_arg)
  147. if self.size is None:
  148. self.Raise("'cbfs' entry must have a size property")
  149. cbfs = CbfsWriter(self.size, arch)
  150. for entry in self._cbfs_entries.values():
  151. # First get the input data and put it in a file. If not available,
  152. # try later.
  153. if entry != skip and not entry.ObtainContents():
  154. return False
  155. data = entry.GetData()
  156. cfile = None
  157. if entry._type == 'raw':
  158. cfile = cbfs.add_file_raw(entry._cbfs_name, data,
  159. entry._cbfs_offset,
  160. entry._cbfs_compress)
  161. elif entry._type == 'stage':
  162. cfile = cbfs.add_file_stage(entry._cbfs_name, data,
  163. entry._cbfs_offset)
  164. else:
  165. entry.Raise("Unknown cbfs-type '%s' (use 'raw', 'stage')" %
  166. entry._type)
  167. if cfile:
  168. entry._cbfs_file = cfile
  169. data = cbfs.get_data()
  170. self.SetContents(data)
  171. return True
  172. def _ReadSubnodes(self):
  173. """Read the subnodes to find out what should go in this CBFS"""
  174. for node in self._node.subnodes:
  175. entry = Entry.Create(self, node)
  176. entry.ReadNode()
  177. entry._cbfs_name = fdt_util.GetString(node, 'cbfs-name', entry.name)
  178. entry._type = fdt_util.GetString(node, 'cbfs-type')
  179. compress = fdt_util.GetString(node, 'cbfs-compress', 'none')
  180. entry._cbfs_offset = fdt_util.GetInt(node, 'cbfs-offset')
  181. entry._cbfs_compress = cbfs_util.find_compress(compress)
  182. if entry._cbfs_compress is None:
  183. self.Raise("Invalid compression in '%s': '%s'" %
  184. (node.name, compress))
  185. self._cbfs_entries[entry._cbfs_name] = entry
  186. def SetImagePos(self, image_pos):
  187. """Override this function to set all the entry properties from CBFS
  188. We can only do this once image_pos is known
  189. Args:
  190. image_pos: Position of this entry in the image
  191. """
  192. super().SetImagePos(image_pos)
  193. # Now update the entries with info from the CBFS entries
  194. for entry in self._cbfs_entries.values():
  195. cfile = entry._cbfs_file
  196. entry.size = cfile.data_len
  197. entry.offset = cfile.calced_cbfs_offset
  198. entry.image_pos = self.image_pos + entry.offset
  199. if entry._cbfs_compress:
  200. entry.uncomp_size = cfile.memlen
  201. def AddMissingProperties(self, have_image_pos):
  202. super().AddMissingProperties(have_image_pos)
  203. for entry in self._cbfs_entries.values():
  204. entry.AddMissingProperties(have_image_pos)
  205. if entry._cbfs_compress:
  206. state.AddZeroProp(entry._node, 'uncomp-size')
  207. # Store the 'compress' property, since we don't look at
  208. # 'cbfs-compress' in Entry.ReadData()
  209. state.AddString(entry._node, 'compress',
  210. cbfs_util.compress_name(entry._cbfs_compress))
  211. def SetCalculatedProperties(self):
  212. """Set the value of device-tree properties calculated by binman"""
  213. super().SetCalculatedProperties()
  214. for entry in self._cbfs_entries.values():
  215. state.SetInt(entry._node, 'offset', entry.offset)
  216. state.SetInt(entry._node, 'size', entry.size)
  217. state.SetInt(entry._node, 'image-pos', entry.image_pos)
  218. if entry.uncomp_size is not None:
  219. state.SetInt(entry._node, 'uncomp-size', entry.uncomp_size)
  220. def ListEntries(self, entries, indent):
  221. """Override this method to list all files in the section"""
  222. super().ListEntries(entries, indent)
  223. for entry in self._cbfs_entries.values():
  224. entry.ListEntries(entries, indent + 1)
  225. def GetEntries(self):
  226. return self._cbfs_entries
  227. def ReadData(self, decomp=True):
  228. data = super().ReadData(True)
  229. return data
  230. def ReadChildData(self, child, decomp=True):
  231. if not self.reader:
  232. data = super().ReadData(True)
  233. self.reader = cbfs_util.CbfsReader(data)
  234. reader = self.reader
  235. cfile = reader.files.get(child.name)
  236. return cfile.data if decomp else cfile.orig_data
  237. def WriteChildData(self, child):
  238. self.ObtainContents(skip=child)
  239. return True