dtb_platdata.py 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229
  1. #!/usr/bin/python
  2. # SPDX-License-Identifier: GPL-2.0+
  3. #
  4. # Copyright (C) 2017 Google, Inc
  5. # Written by Simon Glass <sjg@chromium.org>
  6. #
  7. """Device tree to platform data class
  8. This supports converting device tree data to C structures definitions and
  9. static data.
  10. See doc/driver-model/of-plat.rst for more informaiton
  11. """
  12. import collections
  13. import copy
  14. from enum import IntEnum
  15. import os
  16. import re
  17. import sys
  18. from dtoc import fdt
  19. from dtoc import fdt_util
  20. from dtoc import src_scan
  21. from dtoc.src_scan import conv_name_to_c
  22. # When we see these properties we ignore them - i.e. do not create a structure
  23. # member
  24. PROP_IGNORE_LIST = [
  25. '#address-cells',
  26. '#gpio-cells',
  27. '#size-cells',
  28. 'compatible',
  29. 'linux,phandle',
  30. "status",
  31. 'phandle',
  32. 'bootph-all',
  33. 'bootph-pre-sram',
  34. 'bootph-pre-ram',
  35. ]
  36. # C type declarations for the types we support
  37. TYPE_NAMES = {
  38. fdt.Type.INT: 'fdt32_t',
  39. fdt.Type.BYTE: 'unsigned char',
  40. fdt.Type.STRING: 'const char *',
  41. fdt.Type.BOOL: 'bool',
  42. fdt.Type.INT64: 'fdt64_t',
  43. }
  44. STRUCT_PREFIX = 'dtd_'
  45. VAL_PREFIX = 'dtv_'
  46. # Properties which are considered to be phandles
  47. # key: property name
  48. # value: name of associated #cells property in the target node
  49. #
  50. # New phandle properties must be added here; otherwise they will come through as
  51. # simple integers and finding devices by phandle will not work.
  52. # Any property that ends with one of these (e.g. 'cd-gpios') will be considered
  53. # a phandle property.
  54. PHANDLE_PROPS = {
  55. 'clocks': '#clock-cells',
  56. 'interrupts-extended': '#interrupt-cells',
  57. 'gpios': '#gpio-cells',
  58. 'sandbox,emul': '#emul-cells',
  59. }
  60. class Ftype(IntEnum):
  61. SOURCE, HEADER = range(2)
  62. # This holds information about each type of output file dtoc can create
  63. # ftype: Type of file (Ftype)
  64. # fname: Filename excluding directory, e.g. 'dt-plat.c'
  65. # hdr_comment: Comment explaining the purpose of the file
  66. OutputFile = collections.namedtuple('OutputFile',
  67. ['ftype', 'fname', 'method', 'hdr_comment'])
  68. # This holds information about a property which includes phandles.
  69. #
  70. # max_args: integer: Maximum number or arguments that any phandle uses (int).
  71. # args: Number of args for each phandle in the property. The total number of
  72. # phandles is len(args). This is a list of integers.
  73. PhandleInfo = collections.namedtuple('PhandleInfo', ['max_args', 'args'])
  74. # Holds a single phandle link, allowing a C struct value to be assigned to point
  75. # to a device
  76. #
  77. # var_node: C variable to assign (e.g. 'dtv_mmc.clocks[0].node')
  78. # dev_name: Name of device to assign to (e.g. 'clock')
  79. PhandleLink = collections.namedtuple('PhandleLink', ['var_node', 'dev_name'])
  80. def tab_to(num_tabs, line):
  81. """Append tabs to a line of text to reach a tab stop.
  82. Args:
  83. num_tabs (int): Tab stop to obtain (0 = column 0, 1 = column 8, etc.)
  84. line (str): Line of text to append to
  85. Returns:
  86. str: line with the correct number of tabs appeneded. If the line already
  87. extends past that tab stop then a single space is appended.
  88. """
  89. if len(line) >= num_tabs * 8:
  90. return line + ' '
  91. return line + '\t' * (num_tabs - len(line) // 8)
  92. def get_value(ftype, value):
  93. """Get a value as a C expression
  94. For integers this returns a byte-swapped (little-endian) hex string
  95. For bytes this returns a hex string, e.g. 0x12
  96. For strings this returns a literal string enclosed in quotes
  97. For booleans this return 'true'
  98. Args:
  99. ftype (fdt.Type): Data type (fdt_util)
  100. value (bytes): Data value, as a string of bytes
  101. Returns:
  102. str: String representation of the value
  103. """
  104. if ftype == fdt.Type.INT:
  105. val = '%#x' % fdt_util.fdt32_to_cpu(value)
  106. elif ftype == fdt.Type.BYTE:
  107. char = value[0]
  108. val = '%#x' % (ord(char) if isinstance(char, str) else char)
  109. elif ftype == fdt.Type.STRING:
  110. # Handle evil ACPI backslashes by adding another backslash before them.
  111. # So "\\_SB.GPO0" in the device tree effectively stays like that in C
  112. val = '"%s"' % value.replace('\\', '\\\\')
  113. elif ftype == fdt.Type.BOOL:
  114. val = 'true'
  115. else: # ftype == fdt.Type.INT64:
  116. val = '%#x' % value
  117. return val
  118. class DtbPlatdata():
  119. """Provide a means to convert device tree binary data to platform data
  120. The output of this process is C structures which can be used in space-
  121. constrained encvironments where the ~3KB code overhead of device tree
  122. code is not affordable.
  123. Properties:
  124. _scan: Scan object, for scanning and reporting on useful information
  125. from the U-Boot source code
  126. _fdt: Fdt object, referencing the device tree
  127. _dtb_fname: Filename of the input device tree binary file
  128. _valid_nodes_unsorted: A list of Node object with compatible strings,
  129. ordered by devicetree node order
  130. _valid_nodes: A list of Node object with compatible strings, ordered by
  131. conv_name_to_c(node.name)
  132. _include_disabled: true to include nodes marked status = "disabled"
  133. _outfile: The current output file (sys.stdout or a real file)
  134. _lines: Stashed list of output lines for outputting in the future
  135. _dirname: Directory to hold output files, or None for none (all files
  136. go to stdout)
  137. _struct_data (dict): OrderedDict of dtplat structures to output
  138. key (str): Node name, as a C identifier
  139. value: dict containing structure fields:
  140. key (str): Field name
  141. value: Prop object with field information
  142. _basedir (str): Base directory of source tree
  143. _valid_uclasses (list of src_scan.Uclass): List of uclasses needed for
  144. the selected devices (see _valid_node), in alphabetical order
  145. _instantiate: Instantiate devices so they don't need to be bound at
  146. run-time
  147. """
  148. def __init__(self, scan, dtb_fname, include_disabled, instantiate=False):
  149. self._scan = scan
  150. self._fdt = None
  151. self._dtb_fname = dtb_fname
  152. self._valid_nodes = None
  153. self._valid_nodes_unsorted = None
  154. self._include_disabled = include_disabled
  155. self._outfile = None
  156. self._lines = []
  157. self._dirnames = [None] * len(Ftype)
  158. self._struct_data = collections.OrderedDict()
  159. self._basedir = None
  160. self._valid_uclasses = None
  161. self._instantiate = instantiate
  162. def setup_output_dirs(self, output_dirs):
  163. """Set up the output directories
  164. This should be done before setup_output() is called
  165. Args:
  166. output_dirs (tuple of str):
  167. Directory to use for C output files.
  168. Use None to write files relative current directory
  169. Directory to use for H output files.
  170. Defaults to the C output dir
  171. """
  172. def process_dir(ftype, dirname):
  173. if dirname:
  174. os.makedirs(dirname, exist_ok=True)
  175. self._dirnames[ftype] = dirname
  176. if output_dirs:
  177. c_dirname = output_dirs[0]
  178. h_dirname = output_dirs[1] if len(output_dirs) > 1 else c_dirname
  179. process_dir(Ftype.SOURCE, c_dirname)
  180. process_dir(Ftype.HEADER, h_dirname)
  181. def setup_output(self, ftype, fname):
  182. """Set up the output destination
  183. Once this is done, future calls to self.out() will output to this
  184. file. The file used is as follows:
  185. self._dirnames[ftype] is None: output to fname, or stdout if None
  186. self._dirnames[ftype] is not None: output to fname in that directory
  187. Calling this function multiple times will close the old file and open
  188. the new one. If they are the same file, nothing happens and output will
  189. continue to the same file.
  190. Args:
  191. ftype (str): Type of file to create ('c' or 'h')
  192. fname (str): Filename to send output to. If there is a directory in
  193. self._dirnames for this file type, it will be put in that
  194. directory
  195. """
  196. dirname = self._dirnames[ftype]
  197. if dirname:
  198. pathname = os.path.join(dirname, fname)
  199. if self._outfile:
  200. self._outfile.close()
  201. self._outfile = open(pathname, 'w')
  202. elif fname:
  203. if not self._outfile:
  204. self._outfile = open(fname, 'w')
  205. else:
  206. self._outfile = sys.stdout
  207. def finish_output(self):
  208. """Finish outputing to a file
  209. This closes the output file, if one is in use
  210. """
  211. if self._outfile != sys.stdout:
  212. self._outfile.close()
  213. self._outfile = None
  214. def out(self, line):
  215. """Output a string to the output file
  216. Args:
  217. line (str): String to output
  218. """
  219. self._outfile.write(line)
  220. def buf(self, line):
  221. """Buffer up a string to send later
  222. Args:
  223. line (str): String to add to our 'buffer' list
  224. """
  225. self._lines.append(line)
  226. def get_buf(self):
  227. """Get the contents of the output buffer, and clear it
  228. Returns:
  229. list(str): The output buffer, which is then cleared for future use
  230. """
  231. lines = self._lines
  232. self._lines = []
  233. return lines
  234. def out_header(self, outfile):
  235. """Output a message indicating that this is an auto-generated file
  236. Args:
  237. outfile: OutputFile describing the file being generated
  238. """
  239. self.out('''/*
  240. * DO NOT MODIFY
  241. *
  242. * %s.
  243. * This was generated by dtoc from a .dtb (device tree binary) file.
  244. */
  245. ''' % outfile.hdr_comment)
  246. def get_phandle_argc(self, prop, node_name):
  247. """Check if a node contains phandles
  248. We have no reliable way of detecting whether a node uses a phandle
  249. or not. As an interim measure, use a list of known property names.
  250. Args:
  251. prop (fdt.Prop): Prop object to check
  252. node_name (str): Node name, only used for raising an error
  253. Returns:
  254. int or None: Number of argument cells is this is a phandle,
  255. else None
  256. Raises:
  257. ValueError: if the phandle cannot be parsed or the required property
  258. is not present
  259. """
  260. cells_prop = None
  261. for name, cprop in PHANDLE_PROPS.items():
  262. if prop.name.endswith(name):
  263. cells_prop = cprop
  264. if cells_prop:
  265. if not isinstance(prop.value, list):
  266. prop.value = [prop.value]
  267. val = prop.value
  268. i = 0
  269. max_args = 0
  270. args = []
  271. while i < len(val):
  272. phandle = fdt_util.fdt32_to_cpu(val[i])
  273. # If we get to the end of the list, stop. This can happen
  274. # since some nodes have more phandles in the list than others,
  275. # but we allocate enough space for the largest list. So those
  276. # nodes with shorter lists end up with zeroes at the end.
  277. if not phandle:
  278. break
  279. target = self._fdt.phandle_to_node.get(phandle)
  280. if not target:
  281. raise ValueError("Cannot parse '%s' in node '%s'" %
  282. (prop.name, node_name))
  283. cells = target.props.get(cells_prop)
  284. if not cells:
  285. raise ValueError("Node '%s' has no cells property" %
  286. target.name)
  287. num_args = fdt_util.fdt32_to_cpu(cells.value)
  288. max_args = max(max_args, num_args)
  289. args.append(num_args)
  290. i += 1 + num_args
  291. return PhandleInfo(max_args, args)
  292. return None
  293. def scan_dtb(self):
  294. """Scan the device tree to obtain a tree of nodes and properties
  295. Once this is done, self._fdt.GetRoot() can be called to obtain the
  296. device tree root node, and progress from there.
  297. """
  298. self._fdt = fdt.FdtScan(self._dtb_fname)
  299. def scan_node(self, node, valid_nodes):
  300. """Scan a node and subnodes to build a tree of node and phandle info
  301. This adds each subnode to self._valid_nodes if it is enabled and has a
  302. compatible string.
  303. Args:
  304. node (Node): Node for scan for subnodes
  305. valid_nodes (list of Node): List of Node objects to add to
  306. """
  307. for subnode in node.subnodes:
  308. if 'compatible' in subnode.props:
  309. status = subnode.props.get('status')
  310. if (not self._include_disabled and not status or
  311. status.value != 'disabled'):
  312. valid_nodes.append(subnode)
  313. # recurse to handle any subnodes
  314. self.scan_node(subnode, valid_nodes)
  315. def scan_tree(self, add_root):
  316. """Scan the device tree for useful information
  317. This fills in the following properties:
  318. _valid_nodes_unsorted: A list of nodes we wish to consider include
  319. in the platform data (in devicetree node order)
  320. _valid_nodes: Sorted version of _valid_nodes_unsorted
  321. Args:
  322. add_root: True to add the root node also (which wouldn't normally
  323. be added as it may not have a compatible string)
  324. """
  325. root = self._fdt.GetRoot()
  326. valid_nodes = []
  327. if add_root:
  328. valid_nodes.append(root)
  329. self.scan_node(root, valid_nodes)
  330. self._valid_nodes_unsorted = valid_nodes
  331. self._valid_nodes = sorted(valid_nodes,
  332. key=lambda x: conv_name_to_c(x.name))
  333. def prepare_nodes(self):
  334. """Add extra properties to the nodes we are using
  335. The following properties are added for use by dtoc:
  336. idx: Index number of this node (0=first, etc.)
  337. struct_name: Name of the struct dtd used by this node
  338. var_name: C name for this node
  339. child_devs: List of child devices for this node, each a None
  340. child_refs: Dict of references for each child:
  341. key: Position in child list (-1=head, 0=first, 1=second, ...
  342. n-1=last, n=head)
  343. seq: Sequence number of the device (unique within its uclass), or
  344. -1 not not known yet
  345. dev_ref: Reference to this device, e.g. 'DM_DEVICE_REF(serial)'
  346. driver: Driver record for this node, or None if not known
  347. uclass: Uclass record for this node, or None if not known
  348. uclass_seq: Position of this device within the uclass list (0=first,
  349. n-1=last)
  350. parent_seq: Position of this device within it siblings (0=first,
  351. n-1=last)
  352. parent_driver: Driver record of the node's parent, or None if none.
  353. We don't use node.parent.driver since node.parent may not be in
  354. the list of valid nodes
  355. """
  356. for idx, node in enumerate(self._valid_nodes):
  357. node.idx = idx
  358. node.struct_name, _ = self._scan.get_normalized_compat_name(node)
  359. node.var_name = conv_name_to_c(node.name)
  360. node.child_devs = []
  361. node.child_refs = {}
  362. node.seq = -1
  363. node.dev_ref = None
  364. node.driver = None
  365. node.uclass = None
  366. node.uclass_seq = None
  367. node.parent_seq = None
  368. node.parent_driver = None
  369. @staticmethod
  370. def get_num_cells(node):
  371. """Get the number of cells in addresses and sizes for this node
  372. Args:
  373. node (fdt.None): Node to check
  374. Returns:
  375. Tuple:
  376. Number of address cells for this node
  377. Number of size cells for this node
  378. """
  379. parent = node.parent
  380. if parent and not parent.props:
  381. raise ValueError("Parent node '%s' has no properties - do you need bootph-pre-ram or similar?" %
  382. parent.path)
  383. num_addr, num_size = 2, 2
  384. if parent:
  385. addr_prop = parent.props.get('#address-cells')
  386. size_prop = parent.props.get('#size-cells')
  387. if addr_prop:
  388. num_addr = fdt_util.fdt32_to_cpu(addr_prop.value)
  389. if size_prop:
  390. num_size = fdt_util.fdt32_to_cpu(size_prop.value)
  391. return num_addr, num_size
  392. def scan_reg_sizes(self):
  393. """Scan for 64-bit 'reg' properties and update the values
  394. This finds 'reg' properties with 64-bit data and converts the value to
  395. an array of 64-values. This allows it to be output in a way that the
  396. C code can read.
  397. """
  398. for node in self._valid_nodes:
  399. reg = node.props.get('reg')
  400. if not reg:
  401. continue
  402. num_addr, num_size = self.get_num_cells(node)
  403. total = num_addr + num_size
  404. if reg.type != fdt.Type.INT:
  405. raise ValueError("Node '%s' reg property is not an int" %
  406. node.name)
  407. if not isinstance(reg.value, list):
  408. reg.value = [reg.value]
  409. if len(reg.value) % total:
  410. raise ValueError(
  411. "Node '%s' (parent '%s') reg property has %d cells "
  412. 'which is not a multiple of na + ns = %d + %d)' %
  413. (node.name, node.parent.name, len(reg.value), num_addr,
  414. num_size))
  415. reg.num_addr = num_addr
  416. reg.num_size = num_size
  417. if num_addr > 1 or num_size > 1:
  418. reg.type = fdt.Type.INT64
  419. i = 0
  420. new_value = []
  421. val = reg.value
  422. while i < len(val):
  423. addr = fdt_util.fdt_cells_to_cpu(val[i:], reg.num_addr)
  424. i += num_addr
  425. size = fdt_util.fdt_cells_to_cpu(val[i:], reg.num_size)
  426. i += num_size
  427. new_value += [addr, size]
  428. reg.value = new_value
  429. def scan_structs(self):
  430. """Scan the device tree building up the C structures we will use.
  431. Build a dict keyed by C struct name containing a dict of Prop
  432. object for each struct field (keyed by property name). Where the
  433. same struct appears multiple times, try to use the 'widest'
  434. property, i.e. the one with a type which can express all others.
  435. Once the widest property is determined, all other properties are
  436. updated to match that width.
  437. The results are written to self._struct_data
  438. """
  439. structs = self._struct_data
  440. for node in self._valid_nodes:
  441. fields = {}
  442. # Get a list of all the valid properties in this node.
  443. for name, prop in node.props.items():
  444. if name not in PROP_IGNORE_LIST and name[0] != '#':
  445. fields[name] = copy.deepcopy(prop)
  446. # If we've seen this struct_name before, update the existing struct
  447. if node.struct_name in structs:
  448. struct = structs[node.struct_name]
  449. for name, prop in fields.items():
  450. oldprop = struct.get(name)
  451. if oldprop:
  452. oldprop.Widen(prop)
  453. else:
  454. struct[name] = prop
  455. # Otherwise store this as a new struct.
  456. else:
  457. structs[node.struct_name] = fields
  458. for node in self._valid_nodes:
  459. struct = structs[node.struct_name]
  460. for name, prop in node.props.items():
  461. if name not in PROP_IGNORE_LIST and name[0] != '#':
  462. prop.Widen(struct[name])
  463. def scan_phandles(self):
  464. """Figure out what phandles each node uses
  465. We need to be careful when outputing nodes that use phandles since
  466. they must come after the declaration of the phandles in the C file.
  467. Otherwise we get a compiler error since the phandle struct is not yet
  468. declared.
  469. This function adds to each node a list of phandle nodes that the node
  470. depends on. This allows us to output things in the right order.
  471. """
  472. for node in self._valid_nodes:
  473. node.phandles = set()
  474. for pname, prop in node.props.items():
  475. if pname in PROP_IGNORE_LIST or pname[0] == '#':
  476. continue
  477. info = self.get_phandle_argc(prop, node.name)
  478. if info:
  479. # Process the list as pairs of (phandle, id)
  480. pos = 0
  481. for args in info.args:
  482. phandle_cell = prop.value[pos]
  483. phandle = fdt_util.fdt32_to_cpu(phandle_cell)
  484. target_node = self._fdt.phandle_to_node[phandle]
  485. node.phandles.add(target_node)
  486. pos += 1 + args
  487. def generate_structs(self):
  488. """Generate struct defintions for the platform data
  489. This writes out the body of a header file consisting of structure
  490. definitions for node in self._valid_nodes. See the documentation in
  491. doc/driver-model/of-plat.rst for more information.
  492. """
  493. structs = self._struct_data
  494. self.out('#include <stdbool.h>\n')
  495. self.out('#include <linux/libfdt.h>\n')
  496. # Output the struct definition
  497. for name in sorted(structs):
  498. self.out('struct %s%s {\n' % (STRUCT_PREFIX, name))
  499. for pname in sorted(structs[name]):
  500. prop = structs[name][pname]
  501. info = self.get_phandle_argc(prop, structs[name])
  502. if info:
  503. # For phandles, include a reference to the target
  504. struct_name = 'struct phandle_%d_arg' % info.max_args
  505. self.out('\t%s%s[%d]' % (tab_to(2, struct_name),
  506. conv_name_to_c(prop.name),
  507. len(info.args)))
  508. else:
  509. ptype = TYPE_NAMES[prop.type]
  510. self.out('\t%s%s' % (tab_to(2, ptype),
  511. conv_name_to_c(prop.name)))
  512. if isinstance(prop.value, list):
  513. self.out('[%d]' % len(prop.value))
  514. self.out(';\n')
  515. self.out('};\n')
  516. def _output_list(self, node, prop):
  517. """Output the C code for a devicetree property that holds a list
  518. Args:
  519. node (fdt.Node): Node to output
  520. prop (fdt.Prop): Prop to output
  521. """
  522. self.buf('{')
  523. vals = []
  524. # For phandles, output a reference to the platform data
  525. # of the target node.
  526. info = self.get_phandle_argc(prop, node.name)
  527. if info:
  528. # Process the list as pairs of (phandle, id)
  529. pos = 0
  530. for args in info.args:
  531. phandle_cell = prop.value[pos]
  532. phandle = fdt_util.fdt32_to_cpu(phandle_cell)
  533. target_node = self._fdt.phandle_to_node[phandle]
  534. arg_values = []
  535. for i in range(args):
  536. arg_values.append(
  537. str(fdt_util.fdt32_to_cpu(prop.value[pos + 1 + i])))
  538. pos += 1 + args
  539. vals.append('\t{%d, {%s}}' % (target_node.idx,
  540. ', '.join(arg_values)))
  541. for val in vals:
  542. self.buf('\n\t\t%s,' % val)
  543. else:
  544. for val in prop.value:
  545. vals.append(get_value(prop.type, val))
  546. # Put 8 values per line to avoid very long lines.
  547. for i in range(0, len(vals), 8):
  548. if i:
  549. self.buf(',\n\t\t')
  550. self.buf(', '.join(vals[i:i + 8]))
  551. self.buf('}')
  552. def _declare_device(self, node):
  553. """Add a device declaration to the output
  554. This declares a U_BOOT_DRVINFO() for the device being processed
  555. Args:
  556. node: Node to process
  557. """
  558. self.buf('U_BOOT_DRVINFO(%s) = {\n' % node.var_name)
  559. self.buf('\t.name\t\t= "%s",\n' % node.struct_name)
  560. self.buf('\t.plat\t\t= &%s%s,\n' % (VAL_PREFIX, node.var_name))
  561. self.buf('\t.plat_size\t= sizeof(%s%s),\n' %
  562. (VAL_PREFIX, node.var_name))
  563. idx = -1
  564. if node.parent and node.parent in self._valid_nodes:
  565. idx = node.parent.idx
  566. self.buf('\t.parent_idx\t= %d,\n' % idx)
  567. self.buf('};\n')
  568. self.buf('\n')
  569. def prep_priv(self, struc, name, suffix, section='.priv_data'):
  570. if not struc:
  571. return None
  572. var_name = '_%s%s' % (name, suffix)
  573. hdr = self._scan._structs.get(struc)
  574. if hdr:
  575. self.buf('#include <%s>\n' % hdr.fname)
  576. else:
  577. print('Warning: Cannot find header file for struct %s' % struc)
  578. attr = '__attribute__ ((section ("%s")))' % section
  579. return var_name, struc, attr
  580. def alloc_priv(self, info, name, extra, suffix='_priv'):
  581. result = self.prep_priv(info, name, suffix)
  582. if not result:
  583. return None
  584. var_name, struc, section = result
  585. self.buf('u8 %s_%s[sizeof(struct %s)]\n\t%s;\n' %
  586. (var_name, extra, struc.strip(), section))
  587. return '%s_%s' % (var_name, extra)
  588. def alloc_plat(self, info, name, extra, node):
  589. result = self.prep_priv(info, name, '_plat')
  590. if not result:
  591. return None
  592. var_name, struc, section = result
  593. self.buf('struct %s %s\n\t%s_%s = {\n' %
  594. (struc.strip(), section, var_name, extra))
  595. self.buf('\t.dtplat = {\n')
  596. for pname in sorted(node.props):
  597. self._output_prop(node, node.props[pname], 2)
  598. self.buf('\t},\n')
  599. self.buf('};\n')
  600. return '&%s_%s' % (var_name, extra)
  601. def _declare_device_inst(self, node, parent_driver):
  602. """Add a device instance declaration to the output
  603. This declares a DM_DEVICE_INST() for the device being processed
  604. Args:
  605. node: Node to output
  606. """
  607. driver = node.driver
  608. uclass = node.uclass
  609. self.buf('\n')
  610. num_lines = len(self._lines)
  611. plat_name = self.alloc_plat(driver.plat, driver.name, node.var_name,
  612. node)
  613. priv_name = self.alloc_priv(driver.priv, driver.name, node.var_name)
  614. parent_plat_name = None
  615. parent_priv_name = None
  616. if parent_driver:
  617. # TODO: deal with uclass providing these values
  618. parent_plat_name = self.alloc_priv(
  619. parent_driver.child_plat, driver.name, node.var_name,
  620. '_parent_plat')
  621. parent_priv_name = self.alloc_priv(
  622. parent_driver.child_priv, driver.name, node.var_name,
  623. '_parent_priv')
  624. uclass_plat_name = self.alloc_priv(
  625. uclass.per_dev_plat, driver.name + '_uc', node.var_name, 'plat')
  626. uclass_priv_name = self.alloc_priv(uclass.per_dev_priv,
  627. driver.name + '_uc', node.var_name)
  628. for hdr in driver.headers:
  629. self.buf('#include %s\n' % hdr)
  630. # Add a blank line if we emitted any stuff above, for readability
  631. if num_lines != len(self._lines):
  632. self.buf('\n')
  633. self.buf('DM_DEVICE_INST(%s) = {\n' % node.var_name)
  634. self.buf('\t.driver\t\t= DM_DRIVER_REF(%s),\n' % node.struct_name)
  635. self.buf('\t.name\t\t= "%s",\n' % node.struct_name)
  636. if plat_name:
  637. self.buf('\t.plat_\t\t= %s,\n' % plat_name)
  638. else:
  639. self.buf('\t.plat_\t\t= &%s%s,\n' % (VAL_PREFIX, node.var_name))
  640. if parent_plat_name:
  641. self.buf('\t.parent_plat_\t= %s,\n' % parent_plat_name)
  642. if uclass_plat_name:
  643. self.buf('\t.uclass_plat_\t= %s,\n' % uclass_plat_name)
  644. driver_date = None
  645. if node != self._fdt.GetRoot():
  646. compat_list = node.props['compatible'].value
  647. if not isinstance(compat_list, list):
  648. compat_list = [compat_list]
  649. for compat in compat_list:
  650. driver_data = driver.compat.get(compat)
  651. if driver_data:
  652. self.buf('\t.driver_data\t= %s,\n' % driver_data)
  653. break
  654. if node.parent and node.parent.parent:
  655. if node.parent not in self._valid_nodes:
  656. # This might indicate that the parent node is not in the
  657. # SPL/TPL devicetree but the child is. For example if we are
  658. # dealing with of-platdata in TPL, the parent has a
  659. # bootph-pre-sram tag but the child has bootph-all. In
  660. # this case the child node exists in TPL but the parent does
  661. # not.
  662. raise ValueError("Node '%s' requires parent node '%s' but it is not in the valid list" %
  663. (node.path, node.parent.path))
  664. self.buf('\t.parent\t\t= DM_DEVICE_REF(%s),\n' %
  665. node.parent.var_name)
  666. if priv_name:
  667. self.buf('\t.priv_\t\t= %s,\n' % priv_name)
  668. self.buf('\t.uclass\t\t= DM_UCLASS_REF(%s),\n' % uclass.name)
  669. if uclass_priv_name:
  670. self.buf('\t.uclass_priv_ = %s,\n' % uclass_priv_name)
  671. if parent_priv_name:
  672. self.buf('\t.parent_priv_\t= %s,\n' % parent_priv_name)
  673. self.list_node('uclass_node', uclass.node_refs, node.uclass_seq)
  674. self.list_head('child_head', 'sibling_node', node.child_devs, node.var_name)
  675. if node.parent in self._valid_nodes:
  676. self.list_node('sibling_node', node.parent.child_refs,
  677. node.parent_seq)
  678. # flags is left as 0
  679. self.buf('\t.seq_ = %d,\n' % node.seq)
  680. self.buf('};\n')
  681. self.buf('\n')
  682. return parent_plat_name
  683. def _output_prop(self, node, prop, tabs=1):
  684. """Output a line containing the value of a struct member
  685. Args:
  686. node (Node): Node being output
  687. prop (Prop): Prop object to output
  688. """
  689. if prop.name in PROP_IGNORE_LIST or prop.name[0] == '#':
  690. return
  691. member_name = conv_name_to_c(prop.name)
  692. self.buf('%s%s= ' % ('\t' * tabs, tab_to(3, '.' + member_name)))
  693. # Special handling for lists
  694. if isinstance(prop.value, list):
  695. self._output_list(node, prop)
  696. else:
  697. self.buf(get_value(prop.type, prop.value))
  698. self.buf(',\n')
  699. def _output_values(self, node):
  700. """Output the definition of a device's struct values
  701. Args:
  702. node (Node): Node to output
  703. """
  704. self.buf('static struct %s%s %s%s = {\n' %
  705. (STRUCT_PREFIX, node.struct_name, VAL_PREFIX, node.var_name))
  706. for pname in sorted(node.props):
  707. self._output_prop(node, node.props[pname])
  708. self.buf('};\n')
  709. def list_head(self, head_member, node_member, node_refs, var_name):
  710. self.buf('\t.%s\t= {\n' % head_member)
  711. if node_refs:
  712. last = node_refs[-1].dev_ref
  713. first = node_refs[0].dev_ref
  714. member = node_member
  715. else:
  716. last = 'DM_DEVICE_REF(%s)' % var_name
  717. first = last
  718. member = head_member
  719. self.buf('\t\t.prev = &%s->%s,\n' % (last, member))
  720. self.buf('\t\t.next = &%s->%s,\n' % (first, member))
  721. self.buf('\t},\n')
  722. def list_node(self, member, node_refs, seq):
  723. self.buf('\t.%s\t= {\n' % member)
  724. self.buf('\t\t.prev = %s,\n' % node_refs[seq - 1])
  725. self.buf('\t\t.next = %s,\n' % node_refs[seq + 1])
  726. self.buf('\t},\n')
  727. def generate_uclasses(self):
  728. self.out('\n')
  729. self.out('#include <common.h>\n')
  730. self.out('#include <dm.h>\n')
  731. self.out('#include <dt-structs.h>\n')
  732. self.out('\n')
  733. self.buf('/*\n')
  734. self.buf(
  735. " * uclass declarations, ordered by 'struct uclass' linker_list idx:\n")
  736. uclass_list = self._valid_uclasses
  737. for seq, uclass in enumerate(uclass_list):
  738. self.buf(' * %3d: %s\n' % (seq, uclass.name))
  739. self.buf(' *\n')
  740. self.buf(' * Sequence numbers allocated in each uclass:\n')
  741. for uclass in uclass_list:
  742. if uclass.alias_num_to_node:
  743. self.buf(' * %s: %s\n' % (uclass.name, uclass.uclass_id))
  744. for seq, node in uclass.alias_num_to_node.items():
  745. self.buf(' * %d: %s\n' % (seq, node.path))
  746. self.buf(' */\n')
  747. uclass_node = {}
  748. for seq, uclass in enumerate(uclass_list):
  749. uclass_node[seq] = ('&DM_UCLASS_REF(%s)->sibling_node' %
  750. uclass.name)
  751. uclass_node[-1] = '&uclass_head'
  752. uclass_node[len(uclass_list)] = '&uclass_head'
  753. self.buf('\n')
  754. self.buf('struct list_head %s = {\n' % 'uclass_head')
  755. self.buf('\t.prev = %s,\n' % uclass_node[len(uclass_list) -1])
  756. self.buf('\t.next = %s,\n' % uclass_node[0])
  757. self.buf('};\n')
  758. self.buf('\n')
  759. for seq, uclass in enumerate(uclass_list):
  760. uc_drv = self._scan._uclass.get(uclass.uclass_id)
  761. priv_name = self.alloc_priv(uc_drv.priv, uc_drv.name, '')
  762. self.buf('DM_UCLASS_INST(%s) = {\n' % uclass.name)
  763. if priv_name:
  764. self.buf('\t.priv_\t\t= %s,\n' % priv_name)
  765. self.buf('\t.uc_drv\t\t= DM_UCLASS_DRIVER_REF(%s),\n' % uclass.name)
  766. self.list_node('sibling_node', uclass_node, seq)
  767. self.list_head('dev_head', 'uclass_node', uc_drv.devs, None)
  768. self.buf('};\n')
  769. self.buf('\n')
  770. self.out(''.join(self.get_buf()))
  771. def read_aliases(self):
  772. """Read the aliases and attach the information to self._alias
  773. Raises:
  774. ValueError: The alias path is not found
  775. """
  776. alias_node = self._fdt.GetNode('/aliases')
  777. if not alias_node:
  778. return
  779. re_num = re.compile('(^[a-z0-9-]+[a-z]+)([0-9]+)$')
  780. for prop in alias_node.props.values():
  781. m_alias = re_num.match(prop.name)
  782. if not m_alias:
  783. raise ValueError("Cannot decode alias '%s'" % prop.name)
  784. name, num = m_alias.groups()
  785. node = self._fdt.GetNode(prop.value)
  786. result = self._scan.add_uclass_alias(name, num, node)
  787. if result is None:
  788. raise ValueError("Alias '%s' path '%s' not found" %
  789. (prop.name, prop.value))
  790. elif result is False:
  791. print("Could not find uclass for alias '%s'" % prop.name)
  792. def generate_decl(self):
  793. nodes_to_output = list(self._valid_nodes)
  794. self.buf('#include <dm/device-internal.h>\n')
  795. self.buf('#include <dm/uclass-internal.h>\n')
  796. self.buf('\n')
  797. self.buf(
  798. '/* driver declarations - these allow DM_DRIVER_GET() to be used */\n')
  799. for node in nodes_to_output:
  800. self.buf('extern U_BOOT_DRIVER(%s);\n' % node.struct_name);
  801. self.buf('\n')
  802. if self._instantiate:
  803. self.buf(
  804. '/* device declarations - these allow DM_DEVICE_REF() to be used */\n')
  805. for node in nodes_to_output:
  806. self.buf('extern DM_DEVICE_INST(%s);\n' % node.var_name)
  807. self.buf('\n')
  808. uclass_list = self._valid_uclasses
  809. self.buf(
  810. '/* uclass driver declarations - needed for DM_UCLASS_DRIVER_REF() */\n')
  811. for uclass in uclass_list:
  812. self.buf('extern UCLASS_DRIVER(%s);\n' % uclass.name)
  813. if self._instantiate:
  814. self.buf('\n')
  815. self.buf('/* uclass declarations - needed for DM_UCLASS_REF() */\n')
  816. for uclass in uclass_list:
  817. self.buf('extern DM_UCLASS_INST(%s);\n' % uclass.name)
  818. self.out(''.join(self.get_buf()))
  819. def assign_seqs(self):
  820. """Assign a sequence number to each node"""
  821. for node in self._valid_nodes_unsorted:
  822. seq = self._scan.assign_seq(node)
  823. if seq is not None:
  824. node.seq = seq
  825. def process_nodes(self, need_drivers):
  826. nodes_to_output = list(self._valid_nodes)
  827. # Figure out which drivers we actually use
  828. self._scan.mark_used(nodes_to_output)
  829. for node in nodes_to_output:
  830. node.dev_ref = 'DM_DEVICE_REF(%s)' % node.var_name
  831. driver = self._scan.get_driver(node.struct_name)
  832. if not driver:
  833. if not need_drivers:
  834. continue
  835. raise ValueError("Cannot parse/find driver for '%s'" %
  836. node.struct_name)
  837. node.driver = driver
  838. uclass = self._scan._uclass.get(driver.uclass_id)
  839. if not uclass:
  840. raise ValueError("Cannot parse/find uclass '%s' for driver '%s'" %
  841. (driver.uclass_id, node.struct_name))
  842. node.uclass = uclass
  843. node.uclass_seq = len(node.uclass.devs)
  844. node.uclass.devs.append(node)
  845. uclass.node_refs[node.uclass_seq] = \
  846. '&%s->uclass_node' % node.dev_ref
  847. parent_driver = None
  848. if node.parent in self._valid_nodes:
  849. parent_driver = self._scan.get_driver(node.parent.struct_name)
  850. if not parent_driver:
  851. if not need_drivers:
  852. continue
  853. raise ValueError(
  854. "Cannot parse/find parent driver '%s' for '%s'" %
  855. (node.parent.struct_name, node.struct_name))
  856. node.parent_seq = len(node.parent.child_devs)
  857. node.parent.child_devs.append(node)
  858. node.parent.child_refs[node.parent_seq] = \
  859. '&%s->sibling_node' % node.dev_ref
  860. node.parent_driver = parent_driver
  861. for node in nodes_to_output:
  862. ref = '&%s->child_head' % node.dev_ref
  863. node.child_refs[-1] = ref
  864. node.child_refs[len(node.child_devs)] = ref
  865. uclass_set = set()
  866. for driver in self._scan._drivers.values():
  867. if driver.used and driver.uclass:
  868. uclass_set.add(driver.uclass)
  869. self._valid_uclasses = sorted(list(uclass_set),
  870. key=lambda uc: uc.uclass_id)
  871. for seq, uclass in enumerate(uclass_set):
  872. ref = '&DM_UCLASS_REF(%s)->dev_head' % uclass.name
  873. uclass.node_refs[-1] = ref
  874. uclass.node_refs[len(uclass.devs)] = ref
  875. def output_node_plat(self, node):
  876. """Output the C code for a node
  877. Args:
  878. node (fdt.Node): node to output
  879. """
  880. driver = node.driver
  881. parent_driver = node.parent_driver
  882. line1 = 'Node %s index %d' % (node.path, node.idx)
  883. if driver:
  884. self.buf('/*\n')
  885. self.buf(' * %s\n' % line1)
  886. self.buf(' * driver %s parent %s\n' % (driver.name,
  887. parent_driver.name if parent_driver else 'None'))
  888. self.buf(' */\n')
  889. else:
  890. self.buf('/* %s */\n' % line1)
  891. self._output_values(node)
  892. self._declare_device(node)
  893. self.out(''.join(self.get_buf()))
  894. def output_node_instance(self, node):
  895. """Output the C code for a node
  896. Args:
  897. node (fdt.Node): node to output
  898. """
  899. parent_driver = node.parent_driver
  900. self.buf('/*\n')
  901. self.buf(' * Node %s index %d\n' % (node.path, node.idx))
  902. self.buf(' * driver %s parent %s\n' % (node.driver.name,
  903. parent_driver.name if parent_driver else 'None'))
  904. self.buf('*/\n')
  905. if not node.driver.plat:
  906. self._output_values(node)
  907. self._declare_device_inst(node, parent_driver)
  908. self.out(''.join(self.get_buf()))
  909. def generate_plat(self):
  910. """Generate device defintions for the platform data
  911. This writes out C platform data initialisation data and
  912. U_BOOT_DRVINFO() declarations for each valid node. Where a node has
  913. multiple compatible strings, a #define is used to make them equivalent.
  914. See the documentation in doc/driver-model/of-plat.rst for more
  915. information.
  916. """
  917. self.out('/* Allow use of U_BOOT_DRVINFO() in this file */\n')
  918. self.out('#define DT_PLAT_C\n')
  919. self.out('\n')
  920. self.out('#include <common.h>\n')
  921. self.out('#include <dm.h>\n')
  922. self.out('#include <dt-structs.h>\n')
  923. self.out('\n')
  924. if self._valid_nodes:
  925. self.out('/*\n')
  926. self.out(
  927. " * driver_info declarations, ordered by 'struct driver_info' linker_list idx:\n")
  928. self.out(' *\n')
  929. self.out(' * idx %-20s %-s\n' % ('driver_info', 'driver'))
  930. self.out(' * --- %-20s %-s\n' % ('-' * 20, '-' * 20))
  931. for node in self._valid_nodes:
  932. self.out(' * %3d: %-20s %-s\n' %
  933. (node.idx, node.var_name, node.struct_name))
  934. self.out(' * --- %-20s %-s\n' % ('-' * 20, '-' * 20))
  935. self.out(' */\n')
  936. self.out('\n')
  937. for node in self._valid_nodes:
  938. self.output_node_plat(node)
  939. self.out(''.join(self.get_buf()))
  940. def generate_device(self):
  941. """Generate device instances
  942. This writes out DM_DEVICE_INST() records for each device in the
  943. build.
  944. See the documentation in doc/driver-model/of-plat.rst for more
  945. information.
  946. """
  947. self.out('#include <common.h>\n')
  948. self.out('#include <dm.h>\n')
  949. self.out('#include <dt-structs.h>\n')
  950. self.out('\n')
  951. if self._valid_nodes:
  952. self.out('/*\n')
  953. self.out(
  954. " * udevice declarations, ordered by 'struct udevice' linker_list position:\n")
  955. self.out(' *\n')
  956. self.out(' * idx %-20s %-s\n' % ('udevice', 'driver'))
  957. self.out(' * --- %-20s %-s\n' % ('-' * 20, '-' * 20))
  958. for node in self._valid_nodes:
  959. self.out(' * %3d: %-20s %-s\n' %
  960. (node.idx, node.var_name, node.struct_name))
  961. self.out(' * --- %-20s %-s\n' % ('-' * 20, '-' * 20))
  962. self.out(' */\n')
  963. self.out('\n')
  964. for node in self._valid_nodes:
  965. self.output_node_instance(node)
  966. self.out(''.join(self.get_buf()))
  967. # Types of output file we understand
  968. # key: Command used to generate this file
  969. # value: OutputFile for this command
  970. OUTPUT_FILES_COMMON = {
  971. 'decl':
  972. OutputFile(Ftype.HEADER, 'dt-decl.h', DtbPlatdata.generate_decl,
  973. 'Declares externs for all device/uclass instances'),
  974. 'struct':
  975. OutputFile(Ftype.HEADER, 'dt-structs-gen.h',
  976. DtbPlatdata.generate_structs,
  977. 'Defines the structs used to hold devicetree data'),
  978. }
  979. # File generated without instantiate
  980. OUTPUT_FILES_NOINST = {
  981. 'platdata':
  982. OutputFile(Ftype.SOURCE, 'dt-plat.c', DtbPlatdata.generate_plat,
  983. 'Declares the U_BOOT_DRIVER() records and platform data'),
  984. }
  985. # File generated with instantiate
  986. OUTPUT_FILES_INST = {
  987. 'device':
  988. OutputFile(Ftype.SOURCE, 'dt-device.c', DtbPlatdata.generate_device,
  989. 'Declares the DM_DEVICE_INST() records'),
  990. 'uclass':
  991. OutputFile(Ftype.SOURCE, 'dt-uclass.c', DtbPlatdata.generate_uclasses,
  992. 'Declares the uclass instances (struct uclass)'),
  993. }
  994. def run_steps(args, dtb_file, include_disabled, output, output_dirs, phase,
  995. instantiate, warning_disabled=False, drivers_additional=None,
  996. basedir=None, scan=None):
  997. """Run all the steps of the dtoc tool
  998. Args:
  999. args (list): List of non-option arguments provided to the problem
  1000. dtb_file (str): Filename of dtb file to process
  1001. include_disabled (bool): True to include disabled nodes
  1002. output (str): Name of output file (None for stdout)
  1003. output_dirs (tuple of str):
  1004. Directory to put C output files
  1005. Directory to put H output files
  1006. phase: The phase of U-Boot that we are generating data for, e.g. 'spl'
  1007. or 'tpl'. None if not known
  1008. instantiate: Instantiate devices so they don't need to be bound at
  1009. run-time
  1010. warning_disabled (bool): True to avoid showing warnings about missing
  1011. drivers
  1012. drivers_additional (list): List of additional drivers to use during
  1013. scanning
  1014. basedir (str): Base directory of U-Boot source code. Defaults to the
  1015. grandparent of this file's directory
  1016. scan (src_src.Scanner): Scanner from a previous run. This can help speed
  1017. up tests. Use None for normal operation
  1018. Returns:
  1019. DtbPlatdata object
  1020. Raises:
  1021. ValueError: if args has no command, or an unknown command
  1022. """
  1023. if not args:
  1024. raise ValueError('Please specify a command: struct, platdata, all')
  1025. if output and output_dirs and any(output_dirs):
  1026. raise ValueError('Must specify either output or output_dirs, not both')
  1027. if not scan:
  1028. scan = src_scan.Scanner(basedir, drivers_additional, phase)
  1029. scan.scan_drivers()
  1030. do_process = True
  1031. else:
  1032. do_process = False
  1033. plat = DtbPlatdata(scan, dtb_file, include_disabled, instantiate)
  1034. plat.scan_dtb()
  1035. plat.scan_tree(add_root=instantiate)
  1036. plat.prepare_nodes()
  1037. plat.scan_reg_sizes()
  1038. plat.setup_output_dirs(output_dirs)
  1039. plat.scan_structs()
  1040. plat.scan_phandles()
  1041. plat.process_nodes(instantiate)
  1042. plat.read_aliases()
  1043. plat.assign_seqs()
  1044. # Figure out what output files we plan to generate
  1045. output_files = dict(OUTPUT_FILES_COMMON)
  1046. if instantiate:
  1047. output_files.update(OUTPUT_FILES_INST)
  1048. else:
  1049. output_files.update(OUTPUT_FILES_NOINST)
  1050. cmds = args[0].split(',')
  1051. if 'all' in cmds:
  1052. cmds = sorted(output_files.keys())
  1053. for cmd in cmds:
  1054. outfile = output_files.get(cmd)
  1055. if not outfile:
  1056. raise ValueError("Unknown command '%s': (use: %s)" %
  1057. (cmd, ', '.join(sorted(output_files.keys()))))
  1058. plat.setup_output(outfile.ftype,
  1059. outfile.fname if output_dirs else output)
  1060. plat.out_header(outfile)
  1061. outfile.method(plat)
  1062. plat.finish_output()
  1063. if not warning_disabled:
  1064. scan.show_warnings()
  1065. return plat