cbfs.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. """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.align_default = None
  141. self._cbfs_entries = OrderedDict()
  142. self._ReadSubnodes()
  143. self.reader = None
  144. def ObtainContents(self, skip=None):
  145. arch = cbfs_util.find_arch(self._cbfs_arg)
  146. if arch is None:
  147. self.Raise("Invalid architecture '%s'" % self._cbfs_arg)
  148. if self.size is None:
  149. self.Raise("'cbfs' entry must have a size property")
  150. cbfs = CbfsWriter(self.size, arch)
  151. for entry in self._cbfs_entries.values():
  152. # First get the input data and put it in a file. If not available,
  153. # try later.
  154. if entry != skip and not entry.ObtainContents():
  155. return False
  156. data = entry.GetData()
  157. cfile = None
  158. if entry._type == 'raw':
  159. cfile = cbfs.add_file_raw(entry._cbfs_name, data,
  160. entry._cbfs_offset,
  161. entry._cbfs_compress)
  162. elif entry._type == 'stage':
  163. cfile = cbfs.add_file_stage(entry._cbfs_name, data,
  164. entry._cbfs_offset)
  165. else:
  166. entry.Raise("Unknown cbfs-type '%s' (use 'raw', 'stage')" %
  167. entry._type)
  168. if cfile:
  169. entry._cbfs_file = cfile
  170. data = cbfs.get_data()
  171. self.SetContents(data)
  172. return True
  173. def _ReadSubnodes(self):
  174. """Read the subnodes to find out what should go in this CBFS"""
  175. for node in self._node.subnodes:
  176. entry = Entry.Create(self, node)
  177. entry.ReadNode()
  178. entry._cbfs_name = fdt_util.GetString(node, 'cbfs-name', entry.name)
  179. entry._type = fdt_util.GetString(node, 'cbfs-type')
  180. compress = fdt_util.GetString(node, 'cbfs-compress', 'none')
  181. entry._cbfs_offset = fdt_util.GetInt(node, 'cbfs-offset')
  182. entry._cbfs_compress = cbfs_util.find_compress(compress)
  183. if entry._cbfs_compress is None:
  184. self.Raise("Invalid compression in '%s': '%s'" %
  185. (node.name, compress))
  186. self._cbfs_entries[entry._cbfs_name] = entry
  187. def SetImagePos(self, image_pos):
  188. """Override this function to set all the entry properties from CBFS
  189. We can only do this once image_pos is known
  190. Args:
  191. image_pos: Position of this entry in the image
  192. """
  193. super().SetImagePos(image_pos)
  194. # Now update the entries with info from the CBFS entries
  195. for entry in self._cbfs_entries.values():
  196. cfile = entry._cbfs_file
  197. entry.size = cfile.data_len
  198. entry.offset = cfile.calced_cbfs_offset
  199. entry.image_pos = self.image_pos + entry.offset
  200. if entry._cbfs_compress:
  201. entry.uncomp_size = cfile.memlen
  202. def AddMissingProperties(self, have_image_pos):
  203. super().AddMissingProperties(have_image_pos)
  204. for entry in self._cbfs_entries.values():
  205. entry.AddMissingProperties(have_image_pos)
  206. if entry._cbfs_compress:
  207. state.AddZeroProp(entry._node, 'uncomp-size')
  208. # Store the 'compress' property, since we don't look at
  209. # 'cbfs-compress' in Entry.ReadData()
  210. state.AddString(entry._node, 'compress',
  211. cbfs_util.compress_name(entry._cbfs_compress))
  212. def SetCalculatedProperties(self):
  213. """Set the value of device-tree properties calculated by binman"""
  214. super().SetCalculatedProperties()
  215. for entry in self._cbfs_entries.values():
  216. state.SetInt(entry._node, 'offset', entry.offset)
  217. state.SetInt(entry._node, 'size', entry.size)
  218. state.SetInt(entry._node, 'image-pos', entry.image_pos)
  219. if entry.uncomp_size is not None:
  220. state.SetInt(entry._node, 'uncomp-size', entry.uncomp_size)
  221. def ListEntries(self, entries, indent):
  222. """Override this method to list all files in the section"""
  223. super().ListEntries(entries, indent)
  224. for entry in self._cbfs_entries.values():
  225. entry.ListEntries(entries, indent + 1)
  226. def GetEntries(self):
  227. return self._cbfs_entries
  228. def ReadData(self, decomp=True):
  229. data = super().ReadData(True)
  230. return data
  231. def ReadChildData(self, child, decomp=True):
  232. if not self.reader:
  233. data = super().ReadData(True)
  234. self.reader = cbfs_util.CbfsReader(data)
  235. reader = self.reader
  236. cfile = reader.files.get(child.name)
  237. return cfile.data if decomp else cfile.orig_data
  238. def WriteChildData(self, child):
  239. self.ObtainContents(skip=child)
  240. return True