fit.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. """Entry containing a 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 = "uboot";
  62. loadables = "atf";
  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 Sequence number of the generated fdt (1, 2, ...)
  70. NAME Name of the dtb as provided (i.e. without adding '.dtb')
  71. Note that if no devicetree files are provided (with '-a of-list' as above)
  72. then no nodes will be generated.
  73. The 'default' property, if present, will be automatically set to the name
  74. if of configuration whose devicetree matches the 'default-dt' entry
  75. argument, e.g. with '-a default-dt=sun50i-a64-pine64-lts'.
  76. Available substitutions for '@' property values are:
  77. DEFAULT-SEQ Sequence number of the default fdt,as provided by the
  78. 'default-dt' entry argument
  79. Properties (in the 'fit' node itself):
  80. fit,external-offset: Indicates that the contents of the FIT are external
  81. and provides the external offset. This is passsed to mkimage via
  82. the -E and -p flags.
  83. """
  84. def __init__(self, section, etype, node):
  85. """
  86. Members:
  87. _fit: FIT file being built
  88. _fit_sections: dict:
  89. key: relative path to entry Node (from the base of the FIT)
  90. value: Entry_section object comprising the contents of this
  91. node
  92. """
  93. super().__init__(section, etype, node)
  94. self._fit = None
  95. self._fit_sections = {}
  96. self._fit_props = {}
  97. for pname, prop in self._node.props.items():
  98. if pname.startswith('fit,'):
  99. self._fit_props[pname] = prop
  100. self._fdts = None
  101. self._fit_list_prop = self._fit_props.get('fit,fdt-list')
  102. if self._fit_list_prop:
  103. fdts, = self.GetEntryArgsOrProps(
  104. [EntryArg(self._fit_list_prop.value, str)])
  105. if fdts is not None:
  106. self._fdts = fdts.split()
  107. self._fit_default_dt = self.GetEntryArgsOrProps([EntryArg('default-dt',
  108. str)])[0]
  109. def ReadNode(self):
  110. self._ReadSubnodes()
  111. super().ReadNode()
  112. def _ReadSubnodes(self):
  113. def _AddNode(base_node, depth, node):
  114. """Add a node to the FIT
  115. Args:
  116. base_node: Base Node of the FIT (with 'description' property)
  117. depth: Current node depth (0 is the base node)
  118. node: Current node to process
  119. There are two cases to deal with:
  120. - hash and signature nodes which become part of the FIT
  121. - binman entries which are used to define the 'data' for each
  122. image
  123. """
  124. for pname, prop in node.props.items():
  125. if not pname.startswith('fit,'):
  126. if pname == 'default':
  127. val = prop.value
  128. # Handle the 'default' property
  129. if val.startswith('@'):
  130. if not self._fdts:
  131. continue
  132. if not self._fit_default_dt:
  133. self.Raise("Generated 'default' node requires default-dt entry argument")
  134. if self._fit_default_dt not in self._fdts:
  135. self.Raise("default-dt entry argument '%s' not found in fdt list: %s" %
  136. (self._fit_default_dt,
  137. ', '.join(self._fdts)))
  138. seq = self._fdts.index(self._fit_default_dt)
  139. val = val[1:].replace('DEFAULT-SEQ', str(seq + 1))
  140. fsw.property_string(pname, val)
  141. continue
  142. fsw.property(pname, prop.bytes)
  143. rel_path = node.path[len(base_node.path):]
  144. in_images = rel_path.startswith('/images')
  145. has_images = depth == 2 and in_images
  146. if has_images:
  147. # This node is a FIT subimage node (e.g. "/images/kernel")
  148. # containing content nodes. We collect the subimage nodes and
  149. # section entries for them here to merge the content subnodes
  150. # together and put the merged contents in the subimage node's
  151. # 'data' property later.
  152. entry = Entry.Create(self.section, node, etype='section')
  153. entry.ReadNode()
  154. self._fit_sections[rel_path] = entry
  155. for subnode in node.subnodes:
  156. if has_images and not (subnode.name.startswith('hash') or
  157. subnode.name.startswith('signature')):
  158. # This subnode is a content node not meant to appear in
  159. # the FIT (e.g. "/images/kernel/u-boot"), so don't call
  160. # fsw.add_node() or _AddNode() for it.
  161. pass
  162. elif subnode.name.startswith('@'):
  163. if self._fdts:
  164. # Generate notes for each FDT
  165. for seq, fdt_fname in enumerate(self._fdts):
  166. node_name = subnode.name[1:].replace('SEQ',
  167. str(seq + 1))
  168. fname = tools.GetInputFilename(fdt_fname + '.dtb')
  169. with fsw.add_node(node_name):
  170. for pname, prop in subnode.props.items():
  171. val = prop.bytes.replace(
  172. b'NAME', tools.ToBytes(fdt_fname))
  173. val = val.replace(
  174. b'SEQ', tools.ToBytes(str(seq + 1)))
  175. fsw.property(pname, val)
  176. # Add data for 'fdt' nodes (but not 'config')
  177. if depth == 1 and in_images:
  178. fsw.property('data',
  179. tools.ReadFile(fname))
  180. else:
  181. if self._fdts is None:
  182. if self._fit_list_prop:
  183. self.Raise("Generator node requires '%s' entry argument" %
  184. self._fit_list_prop.value)
  185. else:
  186. self.Raise("Generator node requires 'fit,fdt-list' property")
  187. else:
  188. with fsw.add_node(subnode.name):
  189. _AddNode(base_node, depth + 1, subnode)
  190. # Build a new tree with all nodes and properties starting from the
  191. # entry node
  192. fsw = libfdt.FdtSw()
  193. fsw.finish_reservemap()
  194. with fsw.add_node(''):
  195. _AddNode(self._node, 0, self._node)
  196. fdt = fsw.as_fdt()
  197. # Pack this new FDT and scan it so we can add the data later
  198. fdt.pack()
  199. self._fdt = Fdt.FromData(fdt.as_bytearray())
  200. self._fdt.Scan()
  201. def ObtainContents(self):
  202. """Obtain the contents of the FIT
  203. This adds the 'data' properties to the input ITB (Image-tree Binary)
  204. then runs mkimage to process it.
  205. """
  206. # self._BuildInput() either returns bytes or raises an exception.
  207. data = self._BuildInput(self._fdt)
  208. uniq = self.GetUniqueName()
  209. input_fname = tools.GetOutputFilename('%s.itb' % uniq)
  210. output_fname = tools.GetOutputFilename('%s.fit' % uniq)
  211. tools.WriteFile(input_fname, data)
  212. tools.WriteFile(output_fname, data)
  213. args = []
  214. ext_offset = self._fit_props.get('fit,external-offset')
  215. if ext_offset is not None:
  216. args += ['-E', '-p', '%x' % fdt_util.fdt32_to_cpu(ext_offset.value)]
  217. tools.Run('mkimage', '-t', '-F', output_fname, *args)
  218. self.SetContents(tools.ReadFile(output_fname))
  219. return True
  220. def _BuildInput(self, fdt):
  221. """Finish the FIT by adding the 'data' properties to it
  222. Arguments:
  223. fdt: FIT to update
  224. Returns:
  225. New fdt contents (bytes)
  226. """
  227. for path, section in self._fit_sections.items():
  228. node = fdt.GetNode(path)
  229. # Entry_section.ObtainContents() either returns True or
  230. # raises an exception.
  231. section.ObtainContents()
  232. section.Pack(0)
  233. data = section.GetData()
  234. node.AddData('data', data)
  235. fdt.Sync(auto_resize=True)
  236. data = fdt.GetContents()
  237. return data
  238. def CheckMissing(self, missing_list):
  239. """Check if any entries in this FIT have missing external blobs
  240. If there are missing blobs, the entries are added to the list
  241. Args:
  242. missing_list: List of Entry objects to be added to
  243. """
  244. for path, section in self._fit_sections.items():
  245. section.CheckMissing(missing_list)
  246. def SetAllowMissing(self, allow_missing):
  247. for section in self._fit_sections.values():
  248. section.SetAllowMissing(allow_missing)