fit.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. # SPDX-License-Identifier: GPL-2.0+
  2. # Copyright (c) 2016 Google, Inc
  3. # Written by Simon Glass <sjg@chromium.org>
  4. #
  5. # Entry-type module for producing a FIT
  6. #
  7. from collections import defaultdict, OrderedDict
  8. import libfdt
  9. from binman.entry import Entry, EntryArg
  10. from dtoc import fdt_util
  11. from dtoc.fdt import Fdt
  12. from patman import tools
  13. class Entry_fit(Entry):
  14. """Flat Image Tree (FIT)
  15. This calls mkimage to create a FIT (U-Boot Flat Image Tree) based on the
  16. input provided.
  17. Nodes for the FIT should be written out in the binman configuration just as
  18. they would be in a file passed to mkimage.
  19. For example, this creates an image containing a FIT with U-Boot SPL::
  20. binman {
  21. fit {
  22. description = "Test FIT";
  23. fit,fdt-list = "of-list";
  24. images {
  25. kernel@1 {
  26. description = "SPL";
  27. os = "u-boot";
  28. type = "rkspi";
  29. arch = "arm";
  30. compression = "none";
  31. load = <0>;
  32. entry = <0>;
  33. u-boot-spl {
  34. };
  35. };
  36. };
  37. };
  38. };
  39. U-Boot supports creating fdt and config nodes automatically. To do this,
  40. pass an of-list property (e.g. -a of-list=file1 file2). This tells binman
  41. that you want to generates nodes for two files: file1.dtb and file2.dtb
  42. The fit,fdt-list property (see above) indicates that of-list should be used.
  43. If the property is missing you will get an error.
  44. Then add a 'generator node', a node with a name starting with '@'::
  45. images {
  46. @fdt-SEQ {
  47. description = "fdt-NAME";
  48. type = "flat_dt";
  49. compression = "none";
  50. };
  51. };
  52. This tells binman to create nodes fdt-1 and fdt-2 for each of your two
  53. files. All the properties you specify will be included in the node. This
  54. node acts like a template to generate the nodes. The generator node itself
  55. does not appear in the output - it is replaced with what binman generates.
  56. You can create config nodes in a similar way::
  57. configurations {
  58. default = "@config-DEFAULT-SEQ";
  59. @config-SEQ {
  60. description = "NAME";
  61. firmware = "atf";
  62. loadables = "uboot";
  63. fdt = "fdt-SEQ";
  64. };
  65. };
  66. This tells binman to create nodes config-1 and config-2, i.e. a config for
  67. each of your two files.
  68. Available substitutions for '@' nodes are:
  69. SEQ:
  70. Sequence number of the generated fdt (1, 2, ...)
  71. NAME
  72. Name of the dtb as provided (i.e. without adding '.dtb')
  73. Note that if no devicetree files are provided (with '-a of-list' as above)
  74. then no nodes will be generated.
  75. The 'default' property, if present, will be automatically set to the name
  76. if of configuration whose devicetree matches the 'default-dt' entry
  77. argument, e.g. with '-a default-dt=sun50i-a64-pine64-lts'.
  78. Available substitutions for '@' property values are
  79. DEFAULT-SEQ:
  80. Sequence number of the default fdt,as provided by the 'default-dt' entry
  81. argument
  82. Properties (in the 'fit' node itself):
  83. fit,external-offset: Indicates that the contents of the FIT are external
  84. and provides the external offset. This is passsed to mkimage via
  85. the -E and -p flags.
  86. """
  87. def __init__(self, section, etype, node):
  88. """
  89. Members:
  90. _fit: FIT file being built
  91. _fit_sections: dict:
  92. key: relative path to entry Node (from the base of the FIT)
  93. value: Entry_section object comprising the contents of this
  94. node
  95. """
  96. super().__init__(section, etype, node)
  97. self._fit = None
  98. self._fit_sections = {}
  99. self._fit_props = {}
  100. for pname, prop in self._node.props.items():
  101. if pname.startswith('fit,'):
  102. self._fit_props[pname] = prop
  103. self._fdts = None
  104. self._fit_list_prop = self._fit_props.get('fit,fdt-list')
  105. if self._fit_list_prop:
  106. fdts, = self.GetEntryArgsOrProps(
  107. [EntryArg(self._fit_list_prop.value, str)])
  108. if fdts is not None:
  109. self._fdts = fdts.split()
  110. self._fit_default_dt = self.GetEntryArgsOrProps([EntryArg('default-dt',
  111. str)])[0]
  112. def ReadNode(self):
  113. self._ReadSubnodes()
  114. super().ReadNode()
  115. def _ReadSubnodes(self):
  116. def _AddNode(base_node, depth, node):
  117. """Add a node to the FIT
  118. Args:
  119. base_node: Base Node of the FIT (with 'description' property)
  120. depth: Current node depth (0 is the base node)
  121. node: Current node to process
  122. There are two cases to deal with:
  123. - hash and signature nodes which become part of the FIT
  124. - binman entries which are used to define the 'data' for each
  125. image
  126. """
  127. for pname, prop in node.props.items():
  128. if not pname.startswith('fit,'):
  129. if pname == 'default':
  130. val = prop.value
  131. # Handle the 'default' property
  132. if val.startswith('@'):
  133. if not self._fdts:
  134. continue
  135. if not self._fit_default_dt:
  136. self.Raise("Generated 'default' node requires default-dt entry argument")
  137. if self._fit_default_dt not in self._fdts:
  138. self.Raise("default-dt entry argument '%s' not found in fdt list: %s" %
  139. (self._fit_default_dt,
  140. ', '.join(self._fdts)))
  141. seq = self._fdts.index(self._fit_default_dt)
  142. val = val[1:].replace('DEFAULT-SEQ', str(seq + 1))
  143. fsw.property_string(pname, val)
  144. continue
  145. fsw.property(pname, prop.bytes)
  146. rel_path = node.path[len(base_node.path):]
  147. in_images = rel_path.startswith('/images')
  148. has_images = depth == 2 and in_images
  149. if has_images:
  150. # This node is a FIT subimage node (e.g. "/images/kernel")
  151. # containing content nodes. We collect the subimage nodes and
  152. # section entries for them here to merge the content subnodes
  153. # together and put the merged contents in the subimage node's
  154. # 'data' property later.
  155. entry = Entry.Create(self.section, node, etype='section')
  156. entry.ReadNode()
  157. self._fit_sections[rel_path] = entry
  158. for subnode in node.subnodes:
  159. if has_images and not (subnode.name.startswith('hash') or
  160. subnode.name.startswith('signature')):
  161. # This subnode is a content node not meant to appear in
  162. # the FIT (e.g. "/images/kernel/u-boot"), so don't call
  163. # fsw.add_node() or _AddNode() for it.
  164. pass
  165. elif subnode.name.startswith('@'):
  166. if self._fdts:
  167. # Generate notes for each FDT
  168. for seq, fdt_fname in enumerate(self._fdts):
  169. node_name = subnode.name[1:].replace('SEQ',
  170. str(seq + 1))
  171. fname = tools.GetInputFilename(fdt_fname + '.dtb')
  172. with fsw.add_node(node_name):
  173. for pname, prop in subnode.props.items():
  174. val = prop.bytes.replace(
  175. b'NAME', tools.ToBytes(fdt_fname))
  176. val = val.replace(
  177. b'SEQ', tools.ToBytes(str(seq + 1)))
  178. fsw.property(pname, val)
  179. # Add data for 'fdt' nodes (but not 'config')
  180. if depth == 1 and in_images:
  181. fsw.property('data',
  182. tools.ReadFile(fname))
  183. else:
  184. if self._fdts is None:
  185. if self._fit_list_prop:
  186. self.Raise("Generator node requires '%s' entry argument" %
  187. self._fit_list_prop.value)
  188. else:
  189. self.Raise("Generator node requires 'fit,fdt-list' property")
  190. else:
  191. with fsw.add_node(subnode.name):
  192. _AddNode(base_node, depth + 1, subnode)
  193. # Build a new tree with all nodes and properties starting from the
  194. # entry node
  195. fsw = libfdt.FdtSw()
  196. fsw.finish_reservemap()
  197. with fsw.add_node(''):
  198. _AddNode(self._node, 0, self._node)
  199. fdt = fsw.as_fdt()
  200. # Pack this new FDT and scan it so we can add the data later
  201. fdt.pack()
  202. self._fdt = Fdt.FromData(fdt.as_bytearray())
  203. self._fdt.Scan()
  204. def ObtainContents(self):
  205. """Obtain the contents of the FIT
  206. This adds the 'data' properties to the input ITB (Image-tree Binary)
  207. then runs mkimage to process it.
  208. """
  209. # self._BuildInput() either returns bytes or raises an exception.
  210. data = self._BuildInput(self._fdt)
  211. uniq = self.GetUniqueName()
  212. input_fname = tools.GetOutputFilename('%s.itb' % uniq)
  213. output_fname = tools.GetOutputFilename('%s.fit' % uniq)
  214. tools.WriteFile(input_fname, data)
  215. tools.WriteFile(output_fname, data)
  216. args = []
  217. ext_offset = self._fit_props.get('fit,external-offset')
  218. if ext_offset is not None:
  219. args += ['-E', '-p', '%x' % fdt_util.fdt32_to_cpu(ext_offset.value)]
  220. tools.Run('mkimage', '-t', '-F', output_fname, *args)
  221. self.SetContents(tools.ReadFile(output_fname))
  222. return True
  223. def _BuildInput(self, fdt):
  224. """Finish the FIT by adding the 'data' properties to it
  225. Arguments:
  226. fdt: FIT to update
  227. Returns:
  228. New fdt contents (bytes)
  229. """
  230. for path, section in self._fit_sections.items():
  231. node = fdt.GetNode(path)
  232. # Entry_section.ObtainContents() either returns True or
  233. # raises an exception.
  234. section.ObtainContents()
  235. section.Pack(0)
  236. data = section.GetData()
  237. node.AddData('data', data)
  238. fdt.Sync(auto_resize=True)
  239. data = fdt.GetContents()
  240. return data
  241. def CheckMissing(self, missing_list):
  242. """Check if any entries in this FIT have missing external blobs
  243. If there are missing blobs, the entries are added to the list
  244. Args:
  245. missing_list: List of Entry objects to be added to
  246. """
  247. for path, section in self._fit_sections.items():
  248. section.CheckMissing(missing_list)
  249. def SetAllowMissing(self, allow_missing):
  250. for section in self._fit_sections.values():
  251. section.SetAllowMissing(allow_missing)