dtlib.py 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981
  1. # Copyright (c) 2019, Nordic Semiconductor
  2. # SPDX-License-Identifier: BSD-3-Clause
  3. # Tip: You can view just the documentation with 'pydoc3 dtlib'
  4. # _init_tokens() builds names dynamically.
  5. #
  6. # pylint: disable=undefined-variable
  7. """
  8. A library for extracting information from .dts (devicetree) files. See the
  9. documentation for the DT and Node classes for more information.
  10. The top-level entry point of the library is the DT class. DT.__init__() takes a
  11. .dts file to parse and a list of directories to search for any /include/d
  12. files.
  13. """
  14. import collections
  15. import errno
  16. import os
  17. import re
  18. import sys
  19. import textwrap
  20. # NOTE: testdtlib.py is the test suite for this library. It can be run directly
  21. # as a script:
  22. #
  23. # ./testdtlib.py
  24. class DT:
  25. """
  26. Represents a devicetree parsed from a .dts file (or from many files, if the
  27. .dts file /include/s other files). Creating many instances of this class is
  28. fine. The library has no global state.
  29. These attributes are available on DT instances:
  30. root:
  31. A Node instance representing the root (/) node.
  32. alias2node:
  33. A dictionary that maps maps alias strings (from /aliases) to Node
  34. instances
  35. label2node:
  36. A dictionary that maps each node label (a string) to the Node instance
  37. for the node.
  38. label2prop:
  39. A dictionary that maps each property label (a string) to a Property
  40. instance.
  41. label2prop_offset:
  42. A dictionary that maps each label (a string) within a property value
  43. (e.g., 'x = label_1: < 1 label2: 2 >;') to a (prop, offset) tuple, where
  44. 'prop' is a Property instance and 'offset' the byte offset (0 for label_1
  45. and 4 for label_2 in the example).
  46. phandle2node:
  47. A dictionary that maps each phandle (a number) to a Node instance.
  48. memreserves:
  49. A list of (labels, address, length) tuples for the /memreserve/s in the
  50. .dts file, in the same order as they appear in the file.
  51. 'labels' is a possibly empty set with all labels preceding the memreserve
  52. (e.g., 'label1: label2: /memreserve/ ...'). 'address' and 'length' are
  53. numbers.
  54. filename:
  55. The filename passed to the DT constructor.
  56. """
  57. #
  58. # Public interface
  59. #
  60. def __init__(self, filename, include_path=()):
  61. """
  62. Parses a DTS file to create a DT instance. Raises OSError if 'filename'
  63. can't be opened, and DTError for any parse errors.
  64. filename:
  65. Path to the .dts file to parse.
  66. include_path:
  67. An iterable (e.g. list or tuple) containing paths to search for
  68. /include/d and /incbin/'d files. By default, files are only looked up
  69. relative to the .dts file that contains the /include/ or /incbin/.
  70. """
  71. self.filename = filename
  72. self._include_path = include_path
  73. with open(filename, encoding="utf-8") as f:
  74. self._file_contents = f.read()
  75. self._tok_i = self._tok_end_i = 0
  76. self._filestack = []
  77. self.alias2node = {}
  78. self._lexer_state = _DEFAULT
  79. self._saved_token = None
  80. self._lineno = 1
  81. self._parse_dt()
  82. self._register_phandles()
  83. self._fixup_props()
  84. self._register_aliases()
  85. self._remove_unreferenced()
  86. self._register_labels()
  87. def get_node(self, path):
  88. """
  89. Returns the Node instance for the node with path or alias 'path' (a
  90. string). Raises DTError if the path or alias doesn't exist.
  91. For example, both dt.get_node("/foo/bar") and dt.get_node("bar-alias")
  92. will return the 'bar' node below:
  93. /dts-v1/;
  94. / {
  95. foo {
  96. bar_label: bar {
  97. baz {
  98. };
  99. };
  100. };
  101. aliases {
  102. bar-alias = &bar-label;
  103. };
  104. };
  105. Fetching subnodes via aliases is supported:
  106. dt.get_node("bar-alias/baz") returns the 'baz' node.
  107. """
  108. if path.startswith("/"):
  109. return _root_and_path_to_node(self.root, path, path)
  110. # Path does not start with '/'. First component must be an alias.
  111. alias, _, rest = path.partition("/")
  112. if alias not in self.alias2node:
  113. _err("no alias '{}' found -- did you forget the leading '/' in "
  114. "the node path?".format(alias))
  115. return _root_and_path_to_node(self.alias2node[alias], rest, path)
  116. def has_node(self, path):
  117. """
  118. Returns True if the path or alias 'path' exists. See Node.get_node().
  119. """
  120. try:
  121. self.get_node(path)
  122. return True
  123. except DTError:
  124. return False
  125. def node_iter(self):
  126. """
  127. Returns a generator for iterating over all nodes in the devicetree.
  128. For example, this will print the name of each node that has a property
  129. called 'foo':
  130. for node in dt.node_iter():
  131. if "foo" in node.props:
  132. print(node.name)
  133. """
  134. yield from self.root.node_iter()
  135. def __str__(self):
  136. """
  137. Returns a DTS representation of the devicetree. Called automatically if
  138. the DT instance is print()ed.
  139. """
  140. s = "/dts-v1/;\n\n"
  141. if self.memreserves:
  142. for labels, address, offset in self.memreserves:
  143. # List the labels in a consistent order to help with testing
  144. for label in labels:
  145. s += label + ": "
  146. s += "/memreserve/ {:#018x} {:#018x};\n" \
  147. .format(address, offset)
  148. s += "\n"
  149. return s + str(self.root)
  150. def __repr__(self):
  151. """
  152. Returns some information about the DT instance. Called automatically if
  153. the DT instance is evaluated.
  154. """
  155. return "DT(filename='{}', include_path={})" \
  156. .format(self.filename, self._include_path)
  157. #
  158. # Parsing
  159. #
  160. def _parse_dt(self):
  161. # Top-level parsing loop
  162. self._parse_header()
  163. self._parse_memreserves()
  164. self.root = None
  165. while True:
  166. tok = self._next_token()
  167. if tok.val == "/":
  168. # '/ { ... };', the root node
  169. if not self.root:
  170. self.root = Node(name="/", parent=None, dt=self)
  171. self._parse_node(self.root)
  172. elif tok.id in (_T_LABEL, _T_REF):
  173. # '&foo { ... };' or 'label: &foo { ... };'. The C tools only
  174. # support a single label here too.
  175. if tok.id is _T_LABEL:
  176. label = tok.val
  177. tok = self._next_token()
  178. if tok.id is not _T_REF:
  179. self._parse_error("expected label reference (&foo)")
  180. else:
  181. label = None
  182. try:
  183. node = self._ref2node(tok.val)
  184. except DTError as e:
  185. self._parse_error(e)
  186. node = self._parse_node(node)
  187. if label:
  188. _append_no_dup(node.labels, label)
  189. elif tok.id is _T_DEL_NODE:
  190. self._next_ref2node()._del()
  191. self._expect_token(";")
  192. elif tok.id is _T_OMIT_IF_NO_REF:
  193. self._next_ref2node()._omit_if_no_ref = True
  194. self._expect_token(";")
  195. elif tok.id is _T_EOF:
  196. if not self.root:
  197. self._parse_error("no root node defined")
  198. return
  199. else:
  200. self._parse_error("expected '/' or label reference (&foo)")
  201. def _parse_header(self):
  202. # Parses /dts-v1/ (expected) and /plugin/ (unsupported) at the start of
  203. # files. There may be multiple /dts-v1/ at the start of a file.
  204. has_dts_v1 = False
  205. while self._peek_token().id is _T_DTS_V1:
  206. has_dts_v1 = True
  207. self._next_token()
  208. self._expect_token(";")
  209. # /plugin/ always comes after /dts-v1/
  210. if self._peek_token().id is _T_PLUGIN:
  211. self._parse_error("/plugin/ is not supported")
  212. if not has_dts_v1:
  213. self._parse_error("expected '/dts-v1/;' at start of file")
  214. def _parse_memreserves(self):
  215. # Parses /memreserve/, which appears after /dts-v1/
  216. self.memreserves = []
  217. while True:
  218. # Labels before /memreserve/
  219. labels = []
  220. while self._peek_token().id is _T_LABEL:
  221. _append_no_dup(labels, self._next_token().val)
  222. if self._peek_token().id is _T_MEMRESERVE:
  223. self._next_token()
  224. self.memreserves.append(
  225. (labels, self._eval_prim(), self._eval_prim()))
  226. self._expect_token(";")
  227. elif labels:
  228. self._parse_error("expected /memreserve/ after labels at "
  229. "beginning of file")
  230. else:
  231. return
  232. def _parse_node(self, node):
  233. # Parses the '{ ... };' part of 'node-name { ... };'. Returns the new
  234. # Node.
  235. self._expect_token("{")
  236. while True:
  237. labels, omit_if_no_ref = self._parse_propnode_labels()
  238. tok = self._next_token()
  239. if tok.id is _T_PROPNODENAME:
  240. if self._peek_token().val == "{":
  241. # '<tok> { ...', expect node
  242. if tok.val.count("@") > 1:
  243. self._parse_error("multiple '@' in node name")
  244. # Fetch the existing node if it already exists. This
  245. # happens when overriding nodes.
  246. child = node.nodes.get(tok.val) or \
  247. Node(name=tok.val, parent=node, dt=self)
  248. for label in labels:
  249. _append_no_dup(child.labels, label)
  250. if omit_if_no_ref:
  251. child._omit_if_no_ref = True
  252. node.nodes[child.name] = child
  253. self._parse_node(child)
  254. else:
  255. # Not '<tok> { ...', expect property assignment
  256. if omit_if_no_ref:
  257. self._parse_error(
  258. "/omit-if-no-ref/ can only be used on nodes")
  259. prop = node._get_prop(tok.val)
  260. if self._check_token("="):
  261. self._parse_assignment(prop)
  262. elif not self._check_token(";"):
  263. # ';' is for an empty property, like 'foo;'
  264. self._parse_error("expected '{', '=', or ';'")
  265. for label in labels:
  266. _append_no_dup(prop.labels, label)
  267. elif tok.id is _T_DEL_NODE:
  268. tok2 = self._next_token()
  269. if tok2.id is not _T_PROPNODENAME:
  270. self._parse_error("expected node name")
  271. if tok2.val in node.nodes:
  272. node.nodes[tok2.val]._del()
  273. self._expect_token(";")
  274. elif tok.id is _T_DEL_PROP:
  275. tok2 = self._next_token()
  276. if tok2.id is not _T_PROPNODENAME:
  277. self._parse_error("expected property name")
  278. node.props.pop(tok2.val, None)
  279. self._expect_token(";")
  280. elif tok.val == "}":
  281. self._expect_token(";")
  282. return node
  283. else:
  284. self._parse_error("expected node name, property name, or '}'")
  285. def _parse_propnode_labels(self):
  286. # _parse_node() helpers for parsing labels and /omit-if-no-ref/s before
  287. # nodes and properties. Returns a (<label list>, <omit-if-no-ref bool>)
  288. # tuple.
  289. labels = []
  290. omit_if_no_ref = False
  291. while True:
  292. tok = self._peek_token()
  293. if tok.id is _T_LABEL:
  294. _append_no_dup(labels, tok.val)
  295. elif tok.id is _T_OMIT_IF_NO_REF:
  296. omit_if_no_ref = True
  297. elif (labels or omit_if_no_ref) and tok.id is not _T_PROPNODENAME:
  298. # Got something like 'foo: bar: }'
  299. self._parse_error("expected node or property name")
  300. else:
  301. return labels, omit_if_no_ref
  302. self._next_token()
  303. def _parse_assignment(self, prop):
  304. # Parses the right-hand side of property assignment
  305. #
  306. # prop:
  307. # 'Property' instance being assigned
  308. # Remove any old value, path/phandle references, and in-value labels,
  309. # in case the property value is being overridden
  310. prop.value = b""
  311. prop._markers = []
  312. while True:
  313. # Parse labels before the value (e.g., '..., label: < 0 >')
  314. self._parse_value_labels(prop)
  315. tok = self._next_token()
  316. if tok.val == "<":
  317. self._parse_cells(prop, 4)
  318. elif tok.id is _T_BITS:
  319. n_bits = self._expect_num()
  320. if n_bits not in {8, 16, 32, 64}:
  321. self._parse_error("expected 8, 16, 32, or 64")
  322. self._expect_token("<")
  323. self._parse_cells(prop, n_bits//8)
  324. elif tok.val == "[":
  325. self._parse_bytes(prop)
  326. elif tok.id is _T_STRING:
  327. prop._add_marker(_TYPE_STRING)
  328. prop.value += self._unescape(tok.val.encode("utf-8")) + b"\0"
  329. elif tok.id is _T_REF:
  330. prop._add_marker(_REF_PATH, tok.val)
  331. elif tok.id is _T_INCBIN:
  332. self._parse_incbin(prop)
  333. else:
  334. self._parse_error("malformed value")
  335. # Parse labels after the value (e.g., '< 0 > label:, ...')
  336. self._parse_value_labels(prop)
  337. tok = self._next_token()
  338. if tok.val == ";":
  339. return
  340. if tok.val == ",":
  341. continue
  342. self._parse_error("expected ';' or ','")
  343. def _parse_cells(self, prop, n_bytes):
  344. # Parses '<...>'
  345. prop._add_marker(_N_BYTES_TO_TYPE[n_bytes])
  346. while True:
  347. tok = self._peek_token()
  348. if tok.id is _T_REF:
  349. self._next_token()
  350. if n_bytes != 4:
  351. self._parse_error("phandle references are only allowed in "
  352. "arrays with 32-bit elements")
  353. prop._add_marker(_REF_PHANDLE, tok.val)
  354. elif tok.id is _T_LABEL:
  355. prop._add_marker(_REF_LABEL, tok.val)
  356. self._next_token()
  357. elif self._check_token(">"):
  358. return
  359. else:
  360. # Literal value
  361. num = self._eval_prim()
  362. try:
  363. prop.value += num.to_bytes(n_bytes, "big")
  364. except OverflowError:
  365. try:
  366. # Try again as a signed number, in case it's negative
  367. prop.value += num.to_bytes(n_bytes, "big", signed=True)
  368. except OverflowError:
  369. self._parse_error("{} does not fit in {} bits"
  370. .format(num, 8*n_bytes))
  371. def _parse_bytes(self, prop):
  372. # Parses '[ ... ]'
  373. prop._add_marker(_TYPE_UINT8)
  374. while True:
  375. tok = self._next_token()
  376. if tok.id is _T_BYTE:
  377. prop.value += tok.val.to_bytes(1, "big")
  378. elif tok.id is _T_LABEL:
  379. prop._add_marker(_REF_LABEL, tok.val)
  380. elif tok.val == "]":
  381. return
  382. else:
  383. self._parse_error("expected two-digit byte or ']'")
  384. def _parse_incbin(self, prop):
  385. # Parses
  386. #
  387. # /incbin/ ("filename")
  388. #
  389. # and
  390. #
  391. # /incbin/ ("filename", <offset>, <size>)
  392. prop._add_marker(_TYPE_UINT8)
  393. self._expect_token("(")
  394. tok = self._next_token()
  395. if tok.id is not _T_STRING:
  396. self._parse_error("expected quoted filename")
  397. filename = tok.val
  398. tok = self._next_token()
  399. if tok.val == ",":
  400. offset = self._eval_prim()
  401. self._expect_token(",")
  402. size = self._eval_prim()
  403. self._expect_token(")")
  404. else:
  405. if tok.val != ")":
  406. self._parse_error("expected ',' or ')'")
  407. offset = None
  408. try:
  409. with self._open(filename, "rb") as f:
  410. if offset is None:
  411. prop.value += f.read()
  412. else:
  413. f.seek(offset)
  414. prop.value += f.read(size)
  415. except OSError as e:
  416. self._parse_error("could not read '{}': {}"
  417. .format(filename, e))
  418. def _parse_value_labels(self, prop):
  419. # _parse_assignment() helper for parsing labels before/after each
  420. # comma-separated value
  421. while True:
  422. tok = self._peek_token()
  423. if tok.id is not _T_LABEL:
  424. return
  425. prop._add_marker(_REF_LABEL, tok.val)
  426. self._next_token()
  427. def _node_phandle(self, node):
  428. # Returns the phandle for Node 'node', creating a new phandle if the
  429. # node has no phandle, and fixing up the value for existing
  430. # self-referential phandles (which get set to b'\0\0\0\0' initially).
  431. # Self-referential phandles must be rewritten instead of recreated, so
  432. # that labels are preserved.
  433. if "phandle" in node.props:
  434. phandle_prop = node.props["phandle"]
  435. else:
  436. phandle_prop = Property(node, "phandle")
  437. phandle_prop._add_marker(_TYPE_UINT32) # For displaying
  438. phandle_prop.value = b'\0\0\0\0'
  439. if phandle_prop.value == b'\0\0\0\0':
  440. phandle_i = 1
  441. while phandle_i in self.phandle2node:
  442. phandle_i += 1
  443. self.phandle2node[phandle_i] = node
  444. phandle_prop.value = phandle_i.to_bytes(4, "big")
  445. node.props["phandle"] = phandle_prop
  446. return phandle_prop.value
  447. # Expression evaluation
  448. def _eval_prim(self):
  449. tok = self._peek_token()
  450. if tok.id in (_T_NUM, _T_CHAR_LITERAL):
  451. return self._next_token().val
  452. tok = self._next_token()
  453. if tok.val != "(":
  454. self._parse_error("expected number or parenthesized expression")
  455. val = self._eval_ternary()
  456. self._expect_token(")")
  457. return val
  458. def _eval_ternary(self):
  459. val = self._eval_or()
  460. if self._check_token("?"):
  461. if_val = self._eval_ternary()
  462. self._expect_token(":")
  463. else_val = self._eval_ternary()
  464. return if_val if val else else_val
  465. return val
  466. def _eval_or(self):
  467. val = self._eval_and()
  468. while self._check_token("||"):
  469. val = 1 if self._eval_and() or val else 0
  470. return val
  471. def _eval_and(self):
  472. val = self._eval_bitor()
  473. while self._check_token("&&"):
  474. val = 1 if self._eval_bitor() and val else 0
  475. return val
  476. def _eval_bitor(self):
  477. val = self._eval_bitxor()
  478. while self._check_token("|"):
  479. val |= self._eval_bitxor()
  480. return val
  481. def _eval_bitxor(self):
  482. val = self._eval_bitand()
  483. while self._check_token("^"):
  484. val ^= self._eval_bitand()
  485. return val
  486. def _eval_bitand(self):
  487. val = self._eval_eq()
  488. while self._check_token("&"):
  489. val &= self._eval_eq()
  490. return val
  491. def _eval_eq(self):
  492. val = self._eval_rela()
  493. while True:
  494. if self._check_token("=="):
  495. val = 1 if val == self._eval_rela() else 0
  496. elif self._check_token("!="):
  497. val = 1 if val != self._eval_rela() else 0
  498. else:
  499. return val
  500. def _eval_rela(self):
  501. val = self._eval_shift()
  502. while True:
  503. if self._check_token("<"):
  504. val = 1 if val < self._eval_shift() else 0
  505. elif self._check_token(">"):
  506. val = 1 if val > self._eval_shift() else 0
  507. elif self._check_token("<="):
  508. val = 1 if val <= self._eval_shift() else 0
  509. elif self._check_token(">="):
  510. val = 1 if val >= self._eval_shift() else 0
  511. else:
  512. return val
  513. def _eval_shift(self):
  514. val = self._eval_add()
  515. while True:
  516. if self._check_token("<<"):
  517. val <<= self._eval_add()
  518. elif self._check_token(">>"):
  519. val >>= self._eval_add()
  520. else:
  521. return val
  522. def _eval_add(self):
  523. val = self._eval_mul()
  524. while True:
  525. if self._check_token("+"):
  526. val += self._eval_mul()
  527. elif self._check_token("-"):
  528. val -= self._eval_mul()
  529. else:
  530. return val
  531. def _eval_mul(self):
  532. val = self._eval_unary()
  533. while True:
  534. if self._check_token("*"):
  535. val *= self._eval_unary()
  536. elif self._check_token("/"):
  537. denom = self._eval_unary()
  538. if not denom:
  539. self._parse_error("division by zero")
  540. val //= denom
  541. elif self._check_token("%"):
  542. denom = self._eval_unary()
  543. if not denom:
  544. self._parse_error("division by zero")
  545. val %= denom
  546. else:
  547. return val
  548. def _eval_unary(self):
  549. if self._check_token("-"):
  550. return -self._eval_unary()
  551. if self._check_token("~"):
  552. return ~self._eval_unary()
  553. if self._check_token("!"):
  554. return 0 if self._eval_unary() else 1
  555. return self._eval_prim()
  556. #
  557. # Lexing
  558. #
  559. def _check_token(self, val):
  560. if self._peek_token().val == val:
  561. self._next_token()
  562. return True
  563. return False
  564. def _peek_token(self):
  565. if not self._saved_token:
  566. self._saved_token = self._next_token()
  567. return self._saved_token
  568. def _next_token(self):
  569. if self._saved_token:
  570. tmp = self._saved_token
  571. self._saved_token = None
  572. return tmp
  573. while True:
  574. tok_id = None
  575. match = _token_re.match(self._file_contents, self._tok_end_i)
  576. if match:
  577. tok_id = match.lastindex
  578. if tok_id is _T_CHAR_LITERAL:
  579. val = self._unescape(match.group(tok_id).encode("utf-8"))
  580. if len(val) != 1:
  581. self._parse_error("character literals must be length 1")
  582. tok_val = ord(val)
  583. else:
  584. tok_val = match.group(tok_id)
  585. elif self._lexer_state is _DEFAULT:
  586. match = _num_re.match(self._file_contents, self._tok_end_i)
  587. if match:
  588. tok_id = _T_NUM
  589. num_s = match.group(1)
  590. tok_val = int(num_s,
  591. 16 if num_s.startswith(("0x", "0X")) else
  592. 8 if num_s[0] == "0" else
  593. 10)
  594. elif self._lexer_state is _EXPECT_PROPNODENAME:
  595. match = _propnodename_re.match(self._file_contents,
  596. self._tok_end_i)
  597. if match:
  598. tok_id = _T_PROPNODENAME
  599. tok_val = match.group(1)
  600. self._lexer_state = _DEFAULT
  601. else: # self._lexer_state is _EXPECT_BYTE
  602. match = _byte_re.match(self._file_contents, self._tok_end_i)
  603. if match:
  604. tok_id = _T_BYTE
  605. tok_val = int(match.group(), 16)
  606. if not tok_id:
  607. match = _misc_re.match(self._file_contents, self._tok_end_i)
  608. if match:
  609. tok_id = _T_MISC
  610. tok_val = match.group()
  611. else:
  612. self._tok_i = self._tok_end_i
  613. # Could get here due to a node/property naming appearing in
  614. # an unexpected context as well as for bad characters in
  615. # files. Generate a token for it so that the error can
  616. # trickle up to some context where we can give a more
  617. # helpful error message.
  618. return _Token(_T_BAD, "<unknown token>")
  619. self._tok_i = match.start()
  620. self._tok_end_i = match.end()
  621. if tok_id is _T_SKIP:
  622. self._lineno += tok_val.count("\n")
  623. continue
  624. # /include/ is handled in the lexer in the C tools as well, and can
  625. # appear anywhere
  626. if tok_id is _T_INCLUDE:
  627. # Can have newlines between /include/ and the filename
  628. self._lineno += tok_val.count("\n")
  629. # Do this manual extraction instead of doing it in the regex so
  630. # that we can properly count newlines
  631. filename = tok_val[tok_val.find('"') + 1:-1]
  632. self._enter_file(filename)
  633. continue
  634. if tok_id is _T_LINE:
  635. # #line directive
  636. self._lineno = int(tok_val.split()[0]) - 1
  637. self.filename = tok_val[tok_val.find('"') + 1:-1]
  638. continue
  639. if tok_id is _T_EOF:
  640. if self._filestack:
  641. self._leave_file()
  642. continue
  643. return _Token(_T_EOF, "<EOF>")
  644. # State handling
  645. if tok_id in (_T_DEL_PROP, _T_DEL_NODE, _T_OMIT_IF_NO_REF) or \
  646. tok_val in ("{", ";"):
  647. self._lexer_state = _EXPECT_PROPNODENAME
  648. elif tok_val == "[":
  649. self._lexer_state = _EXPECT_BYTE
  650. elif tok_id in (_T_MEMRESERVE, _T_BITS) or tok_val == "]":
  651. self._lexer_state = _DEFAULT
  652. return _Token(tok_id, tok_val)
  653. def _expect_token(self, tok_val):
  654. # Raises an error if the next token does not have the string value
  655. # 'tok_val'. Returns the token.
  656. tok = self._next_token()
  657. if tok.val != tok_val:
  658. self._parse_error("expected '{}', not '{}'"
  659. .format(tok_val, tok.val))
  660. return tok
  661. def _expect_num(self):
  662. # Raises an error if the next token is not a number. Returns the token.
  663. tok = self._next_token()
  664. if tok.id is not _T_NUM:
  665. self._parse_error("expected number")
  666. return tok.val
  667. def _parse_error(self, s):
  668. _err("{}:{} (column {}): parse error: {}".format(
  669. self.filename, self._lineno,
  670. # This works out for the first line of the file too, where rfind()
  671. # returns -1
  672. self._tok_i - self._file_contents.rfind("\n", 0, self._tok_i + 1),
  673. s))
  674. def _enter_file(self, filename):
  675. # Enters the /include/d file 'filename', remembering the position in
  676. # the /include/ing file for later
  677. self._filestack.append((self.filename, self._lineno,
  678. self._file_contents, self._tok_end_i))
  679. # Handle escapes in filenames, just for completeness
  680. filename = self._unescape(filename.encode("utf-8"))
  681. try:
  682. filename = filename.decode("utf-8")
  683. except UnicodeDecodeError:
  684. self._parse_error("filename is not valid UTF-8")
  685. with self._open(filename, encoding="utf-8") as f:
  686. try:
  687. self._file_contents = f.read()
  688. except OSError as e:
  689. self._parse_error(e)
  690. # Check for recursive /include/
  691. for i, parent in enumerate(self._filestack):
  692. if filename == parent[0]:
  693. self._parse_error("recursive /include/:\n" + " ->\n".join(
  694. ["{}:{}".format(parent[0], parent[1])
  695. for parent in self._filestack[i:]] +
  696. [filename]))
  697. self.filename = f.name
  698. self._lineno = 1
  699. self._tok_end_i = 0
  700. def _leave_file(self):
  701. # Leaves an /include/d file, returning to the file that /include/d it
  702. self.filename, self._lineno, self._file_contents, self._tok_end_i = \
  703. self._filestack.pop()
  704. def _next_ref2node(self):
  705. # Checks that the next token is a label/path reference and returns the
  706. # Node it points to. Only used during parsing, so uses _parse_error()
  707. # on errors to save some code in callers.
  708. label = self._next_token()
  709. if label.id is not _T_REF:
  710. self._parse_error(
  711. "expected label (&foo) or path (&{/foo/bar}) reference")
  712. try:
  713. return self._ref2node(label.val)
  714. except DTError as e:
  715. self._parse_error(e)
  716. def _ref2node(self, s):
  717. # Returns the Node the label/path reference 's' points to
  718. if s[0] == "{":
  719. # Path reference (&{/foo/bar})
  720. path = s[1:-1]
  721. if not path.startswith("/"):
  722. _err("node path '{}' does not start with '/'".format(path))
  723. # Will raise DTError if the path doesn't exist
  724. return _root_and_path_to_node(self.root, path, path)
  725. # Label reference (&foo).
  726. # label2node hasn't been filled in yet, and using it would get messy
  727. # when nodes are deleted
  728. for node in self.node_iter():
  729. if s in node.labels:
  730. return node
  731. _err("undefined node label '{}'".format(s))
  732. #
  733. # Post-processing
  734. #
  735. def _register_phandles(self):
  736. # Registers any manually-inserted phandle properties in
  737. # self.phandle2node, so that we can avoid allocating any phandles from
  738. # that set. Also checks the format of the phandles and does misc.
  739. # sanity checking.
  740. self.phandle2node = {}
  741. for node in self.node_iter():
  742. phandle = node.props.get("phandle")
  743. if phandle:
  744. if len(phandle.value) != 4:
  745. _err("{}: bad phandle length ({}), expected 4 bytes"
  746. .format(node.path, len(phandle.value)))
  747. is_self_referential = False
  748. for marker in phandle._markers:
  749. _, marker_type, ref = marker
  750. if marker_type is _REF_PHANDLE:
  751. # The phandle's value is itself a phandle reference
  752. if self._ref2node(ref) is node:
  753. # Alright to set a node's phandle equal to its own
  754. # phandle. It'll force a new phandle to be
  755. # allocated even if the node is otherwise
  756. # unreferenced.
  757. is_self_referential = True
  758. break
  759. _err("{}: {} refers to another node"
  760. .format(node.path, phandle.name))
  761. # Could put on else on the 'for' above too, but keep it
  762. # somewhat readable
  763. if not is_self_referential:
  764. phandle_val = int.from_bytes(phandle.value, "big")
  765. if phandle_val in {0, 0xFFFFFFFF}:
  766. _err("{}: bad value {:#010x} for {}"
  767. .format(node.path, phandle_val, phandle.name))
  768. if phandle_val in self.phandle2node:
  769. _err("{}: duplicated phandle {:#x} (seen before at {})"
  770. .format(node.path, phandle_val,
  771. self.phandle2node[phandle_val].path))
  772. self.phandle2node[phandle_val] = node
  773. def _fixup_props(self):
  774. # Fills in node path and phandle references in property values, and
  775. # registers labels within values. This must be done after parsing,
  776. # since forwards references are allowed and nodes and properties might
  777. # be deleted.
  778. for node in self.node_iter():
  779. # The tuple() avoids a 'dictionary changed size during iteration'
  780. # error
  781. for prop in tuple(node.props.values()):
  782. # 'prev_pos' and 'pos' are indices in the unpatched
  783. # property value. The result is built up in 'res'.
  784. prev_pos = 0
  785. res = b""
  786. for marker in prop._markers:
  787. pos, marker_type, ref = marker
  788. # Add data before the marker, reading from the unpatched
  789. # property value
  790. res += prop.value[prev_pos:pos]
  791. # Fix the marker offset so that it's correct for the
  792. # patched property value, for later (not used in this
  793. # function). The offset might change due to path
  794. # references, which expand to something like "/foo/bar".
  795. marker[0] = len(res)
  796. if marker_type is _REF_LABEL:
  797. # This is a temporary format so that we can catch
  798. # duplicate references. prop.offset_labels is changed
  799. # to a dictionary that maps labels to offsets in
  800. # _register_labels().
  801. _append_no_dup(prop.offset_labels, (ref, len(res)))
  802. elif marker_type in (_REF_PATH, _REF_PHANDLE):
  803. # Path or phandle reference
  804. try:
  805. ref_node = self._ref2node(ref)
  806. except DTError as e:
  807. _err("{}: {}".format(prop.node.path, e))
  808. # For /omit-if-no-ref/
  809. ref_node._is_referenced = True
  810. if marker_type is _REF_PATH:
  811. res += ref_node.path.encode("utf-8") + b'\0'
  812. else: # marker_type is PHANDLE
  813. res += self._node_phandle(ref_node)
  814. # Skip over the dummy phandle placeholder
  815. pos += 4
  816. prev_pos = pos
  817. # Store the final fixed-up value. Add the data after the last
  818. # marker.
  819. prop.value = res + prop.value[prev_pos:]
  820. def _register_aliases(self):
  821. # Registers aliases from the /aliases node in self.alias2node. Also
  822. # checks the format of the alias properties.
  823. # We copy this to self.alias2node at the end to avoid get_node()
  824. # looking up paths via other aliases while verifying aliases
  825. alias2node = {}
  826. alias_re = re.compile("[0-9a-z-]+$")
  827. aliases = self.root.nodes.get("aliases")
  828. if aliases:
  829. for prop in aliases.props.values():
  830. if not alias_re.match(prop.name):
  831. _err("/aliases: alias property name '{}' should include "
  832. "only characters from [0-9a-z-]".format(prop.name))
  833. # Property.to_path() already checks that the node exists
  834. alias2node[prop.name] = prop.to_path()
  835. self.alias2node = alias2node
  836. def _remove_unreferenced(self):
  837. # Removes any unreferenced nodes marked with /omit-if-no-ref/ from the
  838. # tree
  839. # tuple() is to avoid 'RuntimeError: dictionary changed size during
  840. # iteration' errors
  841. for node in tuple(self.node_iter()):
  842. if node._omit_if_no_ref and not node._is_referenced:
  843. node._del()
  844. def _register_labels(self):
  845. # Checks for duplicate labels and registers labels in label2node,
  846. # label2prop, and label2prop_offset
  847. label2things = collections.defaultdict(set)
  848. self.label2node = {}
  849. self.label2prop = {}
  850. self.label2prop_offset = {}
  851. # Register all labels and the nodes/props they point to in label2things
  852. for node in self.node_iter():
  853. for label in node.labels:
  854. label2things[label].add(node)
  855. self.label2node[label] = node
  856. for prop in node.props.values():
  857. for label in prop.labels:
  858. label2things[label].add(prop)
  859. self.label2prop[label] = prop
  860. for label, offset in prop.offset_labels:
  861. label2things[label].add((prop, offset))
  862. self.label2prop_offset[label] = (prop, offset)
  863. # See _fixup_props()
  864. prop.offset_labels = {label: offset for label, offset in
  865. prop.offset_labels}
  866. for label, things in label2things.items():
  867. if len(things) > 1:
  868. strings = []
  869. for thing in things:
  870. if isinstance(thing, Node):
  871. strings.append("on " + thing.path)
  872. elif isinstance(thing, Property):
  873. strings.append("on property '{}' of node {}"
  874. .format(thing.name, thing.node.path))
  875. else:
  876. # Label within property value
  877. strings.append("in the value of property '{}' of node {}"
  878. .format(thing[0].name,
  879. thing[0].node.path))
  880. # Give consistent error messages to help with testing
  881. strings.sort()
  882. _err("Label '{}' appears ".format(label) +
  883. " and ".join(strings))
  884. #
  885. # Misc.
  886. #
  887. def _unescape(self, b):
  888. # Replaces backslash escapes in the 'bytes' array 'b'. We can't do this at
  889. # the string level, because the result might not be valid UTF-8 when
  890. # octal/hex escapes are involved.
  891. def sub(match):
  892. esc = match.group(1)
  893. if esc == b"a": return b"\a"
  894. if esc == b"b": return b"\b"
  895. if esc == b"t": return b"\t"
  896. if esc == b"n": return b"\n"
  897. if esc == b"v": return b"\v"
  898. if esc == b"f": return b"\f"
  899. if esc == b"r": return b"\r"
  900. if esc[0] in b"01234567":
  901. # Octal escape
  902. try:
  903. return int(esc, 8).to_bytes(1, "big")
  904. except OverflowError:
  905. self._parse_error("octal escape out of range (> 255)")
  906. if esc[0] == ord("x") and len(esc) > 1:
  907. # Hex escape
  908. return int(esc[1:], 16).to_bytes(1, "big")
  909. # Return <char> as-is for other \<char>
  910. return esc[0].to_bytes(1, "big")
  911. return _unescape_re.sub(sub, b)
  912. def _open(self, filename, mode="r", **kwargs):
  913. # Wrapper around standard Python open(), accepting the same params.
  914. # But searches for a 'filename' file in the directory of the current
  915. # file and the include path.
  916. # The C tools support specifying stdin with '-' too
  917. if filename == "-":
  918. return sys.stdin.buffer if "b" in mode else sys.stdin
  919. # Try the directory of the current file first
  920. dirname = os.path.dirname(self.filename)
  921. try:
  922. return open(os.path.join(dirname, filename), mode, **kwargs)
  923. except OSError as e:
  924. if e.errno != errno.ENOENT:
  925. self._parse_error(e)
  926. # Try each directory from the include path
  927. for path in self._include_path:
  928. try:
  929. return open(os.path.join(path, filename), mode, **kwargs)
  930. except OSError as e:
  931. if e.errno != errno.ENOENT:
  932. self._parse_error(e)
  933. continue
  934. self._parse_error("'{}' could not be found".format(filename))
  935. class Node:
  936. r"""
  937. Represents a node in the devicetree ('node-name { ... };').
  938. These attributes are available on Node instances:
  939. name:
  940. The name of the node (a string).
  941. unit_addr:
  942. The portion after the '@' in the node's name, or the empty string if the
  943. name has no '@' in it.
  944. Note that this is a string. Run int(node.unit_addr, 16) to get an
  945. integer.
  946. props:
  947. A collections.OrderedDict that maps the properties defined on the node to
  948. their values. 'props' is indexed by property name (a string), and values
  949. are represented as 'bytes' arrays.
  950. To convert property values to Python numbers or strings, use
  951. dtlib.to_num(), dtlib.to_nums(), or dtlib.to_string().
  952. Property values are represented as 'bytes' arrays to support the full
  953. generality of DTS, which allows assignments like
  954. x = "foo", < 0x12345678 >, [ 9A ];
  955. This gives x the value b"foo\0\x12\x34\x56\x78\x9A". Numbers in DTS are
  956. stored in big-endian format.
  957. nodes:
  958. A collections.OrderedDict containing the subnodes of the node, indexed by
  959. name.
  960. labels:
  961. A list with all labels pointing to the node, in the same order as the
  962. labels appear, but with duplicates removed.
  963. 'label_1: label_2: node { ... };' gives 'labels' the value
  964. ["label_1", "label_2"].
  965. parent:
  966. The parent Node of the node. 'None' for the root node.
  967. path:
  968. The path to the node as a string, e.g. "/foo/bar".
  969. dt:
  970. The DT instance this node belongs to.
  971. """
  972. #
  973. # Public interface
  974. #
  975. def __init__(self, name, parent, dt):
  976. """
  977. Node constructor. Not meant to be called directly by clients.
  978. """
  979. self.name = name
  980. self.parent = parent
  981. self.dt = dt
  982. self.props = collections.OrderedDict()
  983. self.nodes = collections.OrderedDict()
  984. self.labels = []
  985. self._omit_if_no_ref = False
  986. self._is_referenced = False
  987. @property
  988. def unit_addr(self):
  989. """
  990. See the class documentation.
  991. """
  992. return self.name.partition("@")[2]
  993. @property
  994. def path(self):
  995. """
  996. See the class documentation.
  997. """
  998. node_names = []
  999. cur = self
  1000. while cur.parent:
  1001. node_names.append(cur.name)
  1002. cur = cur.parent
  1003. return "/" + "/".join(reversed(node_names))
  1004. def node_iter(self):
  1005. """
  1006. Returns a generator for iterating over the node and its children,
  1007. recursively.
  1008. For example, this will iterate over all nodes in the tree (like
  1009. dt.node_iter()).
  1010. for node in dt.root.node_iter():
  1011. ...
  1012. """
  1013. yield self
  1014. for node in self.nodes.values():
  1015. yield from node.node_iter()
  1016. def _get_prop(self, name):
  1017. # Returns the property named 'name' on the node, creating it if it
  1018. # doesn't already exist
  1019. prop = self.props.get(name)
  1020. if not prop:
  1021. prop = Property(self, name)
  1022. self.props[name] = prop
  1023. return prop
  1024. def _del(self):
  1025. # Removes the node from the tree
  1026. self.parent.nodes.pop(self.name)
  1027. def __str__(self):
  1028. """
  1029. Returns a DTS representation of the node. Called automatically if the
  1030. node is print()ed.
  1031. """
  1032. s = "".join(label + ": " for label in self.labels)
  1033. s += "{} {{\n".format(self.name)
  1034. for prop in self.props.values():
  1035. s += "\t" + str(prop) + "\n"
  1036. for child in self.nodes.values():
  1037. s += textwrap.indent(child.__str__(), "\t") + "\n"
  1038. s += "};"
  1039. return s
  1040. def __repr__(self):
  1041. """
  1042. Returns some information about the Node instance. Called automatically
  1043. if the Node instance is evaluated.
  1044. """
  1045. return "<Node {} in '{}'>" \
  1046. .format(self.path, self.dt.filename)
  1047. class Property:
  1048. """
  1049. Represents a property ('x = ...').
  1050. These attributes are available on Property instances:
  1051. name:
  1052. The name of the property (a string).
  1053. value:
  1054. The value of the property, as a 'bytes' string. Numbers are stored in
  1055. big-endian format, and strings are null-terminated. Putting multiple
  1056. comma-separated values in an assignment (e.g., 'x = < 1 >, "foo"') will
  1057. concatenate the values.
  1058. See the to_*() methods for converting the value to other types.
  1059. type:
  1060. The type of the property, inferred from the syntax used in the
  1061. assignment. This is one of the following constants (with example
  1062. assignments):
  1063. Assignment | Property.type
  1064. ----------------------------+------------------------
  1065. foo; | dtlib.TYPE_EMPTY
  1066. foo = []; | dtlib.TYPE_BYTES
  1067. foo = [01 02]; | dtlib.TYPE_BYTES
  1068. foo = /bits/ 8 <1>; | dtlib.TYPE_BYTES
  1069. foo = <1>; | dtlib.TYPE_NUM
  1070. foo = <>; | dtlib.TYPE_NUMS
  1071. foo = <1 2 3>; | dtlib.TYPE_NUMS
  1072. foo = <1 2>, <3>; | dtlib.TYPE_NUMS
  1073. foo = "foo"; | dtlib.TYPE_STRING
  1074. foo = "foo", "bar"; | dtlib.TYPE_STRINGS
  1075. foo = <&l>; | dtlib.TYPE_PHANDLE
  1076. foo = <&l1 &l2 &l3>; | dtlib.TYPE_PHANDLES
  1077. foo = <&l1 &l2>, <&l3>; | dtlib.TYPE_PHANDLES
  1078. foo = <&l1 1 2 &l2 3 4>; | dtlib.TYPE_PHANDLES_AND_NUMS
  1079. foo = <&l1 1 2>, <&l2 3 4>; | dtlib.TYPE_PHANDLES_AND_NUMS
  1080. foo = &l; | dtlib.TYPE_PATH
  1081. *Anything else* | dtlib.TYPE_COMPOUND
  1082. *Anything else* includes properties mixing phandle (<&label>) and node
  1083. path (&label) references with other data.
  1084. Data labels in the property value do not influence the type.
  1085. labels:
  1086. A list with all labels pointing to the property, in the same order as the
  1087. labels appear, but with duplicates removed.
  1088. 'label_1: label2: x = ...' gives 'labels' the value
  1089. {"label_1", "label_2"}.
  1090. offset_labels:
  1091. A dictionary that maps any labels within the property's value to their
  1092. offset, in bytes. For example, 'x = < 0 label_1: 1 label_2: >' gives
  1093. 'offset_labels' the value {"label_1": 4, "label_2": 8}.
  1094. Iteration order will match the order of the labels on Python versions
  1095. that preserve dict insertion order.
  1096. node:
  1097. The Node the property is on.
  1098. """
  1099. #
  1100. # Public interface
  1101. #
  1102. def __init__(self, node, name):
  1103. if "@" in name:
  1104. node.dt._parse_error("'@' is only allowed in node names")
  1105. self.name = name
  1106. self.node = node
  1107. self.value = b""
  1108. self.labels = []
  1109. self.offset_labels = []
  1110. # A list of (offset, label, type) tuples (sorted by offset), giving the
  1111. # locations of references within the value. 'type' is either _REF_PATH,
  1112. # for a node path reference, _REF_PHANDLE, for a phandle reference, or
  1113. # _REF_LABEL, for a label on/within data. Node paths and phandles need
  1114. # to be patched in after parsing.
  1115. self._markers = []
  1116. def to_num(self, signed=False):
  1117. """
  1118. Returns the value of the property as a number.
  1119. Raises DTError if the property was not assigned with this syntax (has
  1120. Property.type TYPE_NUM):
  1121. foo = < 1 >;
  1122. signed (default: False):
  1123. If True, the value will be interpreted as signed rather than
  1124. unsigned.
  1125. """
  1126. if self.type is not TYPE_NUM:
  1127. _err("expected property '{0}' on {1} in {2} to be assigned with "
  1128. "'{0} = < (number) >;', not '{3}'"
  1129. .format(self.name, self.node.path, self.node.dt.filename,
  1130. self))
  1131. return int.from_bytes(self.value, "big", signed=signed)
  1132. def to_nums(self, signed=False):
  1133. """
  1134. Returns the value of the property as a list of numbers.
  1135. Raises DTError if the property was not assigned with this syntax (has
  1136. Property.type TYPE_NUM or TYPE_NUMS):
  1137. foo = < 1 2 ... >;
  1138. signed (default: False):
  1139. If True, the values will be interpreted as signed rather than
  1140. unsigned.
  1141. """
  1142. if self.type not in (TYPE_NUM, TYPE_NUMS):
  1143. _err("expected property '{0}' on {1} in {2} to be assigned with "
  1144. "'{0} = < (number) (number) ... >;', not '{3}'"
  1145. .format(self.name, self.node.path, self.node.dt.filename,
  1146. self))
  1147. return [int.from_bytes(self.value[i:i + 4], "big", signed=signed)
  1148. for i in range(0, len(self.value), 4)]
  1149. def to_bytes(self):
  1150. """
  1151. Returns the value of the property as a raw 'bytes', like
  1152. Property.value, except with added type checking.
  1153. Raises DTError if the property was not assigned with this syntax (has
  1154. Property.type TYPE_BYTES):
  1155. foo = [ 01 ... ];
  1156. """
  1157. if self.type is not TYPE_BYTES:
  1158. _err("expected property '{0}' on {1} in {2} to be assigned with "
  1159. "'{0} = [ (byte) (byte) ... ];', not '{3}'"
  1160. .format(self.name, self.node.path, self.node.dt.filename,
  1161. self))
  1162. return self.value
  1163. def to_string(self):
  1164. """
  1165. Returns the value of the property as a string.
  1166. Raises DTError if the property was not assigned with this syntax (has
  1167. Property.type TYPE_STRING):
  1168. foo = "string";
  1169. This function might also raise UnicodeDecodeError if the string is
  1170. not valid UTF-8.
  1171. """
  1172. if self.type is not TYPE_STRING:
  1173. _err("expected property '{0}' on {1} in {2} to be assigned with "
  1174. "'{0} = \"string\";', not '{3}'"
  1175. .format(self.name, self.node.path, self.node.dt.filename,
  1176. self))
  1177. try:
  1178. return self.value.decode("utf-8")[:-1] # Strip null
  1179. except UnicodeDecodeError:
  1180. _err("value of property '{}' ({}) on {} in {} is not valid UTF-8"
  1181. .format(self.name, self.value, self.node.path,
  1182. self.node.dt.filename))
  1183. def to_strings(self):
  1184. """
  1185. Returns the value of the property as a list of strings.
  1186. Raises DTError if the property was not assigned with this syntax (has
  1187. Property.type TYPE_STRING or TYPE_STRINGS):
  1188. foo = "string", "string", ... ;
  1189. Also raises DTError if any of the strings are not valid UTF-8.
  1190. """
  1191. if self.type not in (TYPE_STRING, TYPE_STRINGS):
  1192. _err("expected property '{0}' on {1} in {2} to be assigned with "
  1193. "'{0} = \"string\", \"string\", ... ;', not '{3}'"
  1194. .format(self.name, self.node.path, self.node.dt.filename,
  1195. self))
  1196. try:
  1197. return self.value.decode("utf-8").split("\0")[:-1]
  1198. except UnicodeDecodeError:
  1199. _err("value of property '{}' ({}) on {} in {} is not valid UTF-8"
  1200. .format(self.name, self.value, self.node.path,
  1201. self.node.dt.filename))
  1202. def to_node(self):
  1203. """
  1204. Returns the Node the phandle in the property points to.
  1205. Raises DTError if the property was not assigned with this syntax (has
  1206. Property.type TYPE_PHANDLE).
  1207. foo = < &bar >;
  1208. """
  1209. if self.type is not TYPE_PHANDLE:
  1210. _err("expected property '{0}' on {1} in {2} to be assigned with "
  1211. "'{0} = < &foo >;', not '{3}'"
  1212. .format(self.name, self.node.path, self.node.dt.filename,
  1213. self))
  1214. return self.node.dt.phandle2node[int.from_bytes(self.value, "big")]
  1215. def to_nodes(self):
  1216. """
  1217. Returns a list with the Nodes the phandles in the property point to.
  1218. Raises DTError if the property value contains anything other than
  1219. phandles. All of the following are accepted:
  1220. foo = < >
  1221. foo = < &bar >;
  1222. foo = < &bar &baz ... >;
  1223. foo = < &bar ... >, < &baz ... >;
  1224. """
  1225. def type_ok():
  1226. if self.type in (TYPE_PHANDLE, TYPE_PHANDLES):
  1227. return True
  1228. # Also accept 'foo = < >;'
  1229. return self.type is TYPE_NUMS and not self.value
  1230. if not type_ok():
  1231. _err("expected property '{0}' on {1} in {2} to be assigned with "
  1232. "'{0} = < &foo &bar ... >;', not '{3}'"
  1233. .format(self.name, self.node.path,
  1234. self.node.dt.filename, self))
  1235. return [self.node.dt.phandle2node[int.from_bytes(self.value[i:i + 4],
  1236. "big")]
  1237. for i in range(0, len(self.value), 4)]
  1238. def to_path(self):
  1239. """
  1240. Returns the Node referenced by the path stored in the property.
  1241. Raises DTError if the property was not assigned with either of these
  1242. syntaxes (has Property.type TYPE_PATH or TYPE_STRING):
  1243. foo = &bar;
  1244. foo = "/bar";
  1245. For the second case, DTError is raised if the path does not exist.
  1246. """
  1247. if self.type not in (TYPE_PATH, TYPE_STRING):
  1248. _err("expected property '{0}' on {1} in {2} to be assigned with "
  1249. "either '{0} = &foo' or '{0} = \"/path/to/node\"', not '{3}'"
  1250. .format(self.name, self.node.path, self.node.dt.filename,
  1251. self))
  1252. try:
  1253. path = self.value.decode("utf-8")[:-1]
  1254. except UnicodeDecodeError:
  1255. _err("value of property '{}' ({}) on {} in {} is not valid UTF-8"
  1256. .format(self.name, self.value, self.node.path,
  1257. self.node.dt.filename))
  1258. try:
  1259. return self.node.dt.get_node(path)
  1260. except DTError:
  1261. _err("property '{}' on {} in {} points to the non-existent node "
  1262. "\"{}\"".format(self.name, self.node.path,
  1263. self.node.dt.filename, path))
  1264. @property
  1265. def type(self):
  1266. """
  1267. See the class docstring.
  1268. """
  1269. # Data labels (e.g. 'foo = label: <3>') are irrelevant, so filter them
  1270. # out
  1271. types = [marker[1] for marker in self._markers
  1272. if marker[1] != _REF_LABEL]
  1273. if not types:
  1274. return TYPE_EMPTY
  1275. if types == [_TYPE_UINT8]:
  1276. return TYPE_BYTES
  1277. if types == [_TYPE_UINT32]:
  1278. return TYPE_NUM if len(self.value) == 4 else TYPE_NUMS
  1279. # Treat 'foo = <1 2 3>, <4 5>, ...' as TYPE_NUMS too
  1280. if set(types) == {_TYPE_UINT32}:
  1281. return TYPE_NUMS
  1282. if set(types) == {_TYPE_STRING}:
  1283. return TYPE_STRING if len(types) == 1 else TYPE_STRINGS
  1284. if types == [_REF_PATH]:
  1285. return TYPE_PATH
  1286. if types == [_TYPE_UINT32, _REF_PHANDLE] and len(self.value) == 4:
  1287. return TYPE_PHANDLE
  1288. if set(types) == {_TYPE_UINT32, _REF_PHANDLE}:
  1289. if len(self.value) == 4*types.count(_REF_PHANDLE):
  1290. # Array with just phandles in it
  1291. return TYPE_PHANDLES
  1292. # Array with both phandles and numbers
  1293. return TYPE_PHANDLES_AND_NUMS
  1294. return TYPE_COMPOUND
  1295. def __str__(self):
  1296. s = "".join(label + ": " for label in self.labels) + self.name
  1297. if not self.value:
  1298. return s + ";"
  1299. s += " ="
  1300. for i, (pos, marker_type, ref) in enumerate(self._markers):
  1301. if i < len(self._markers) - 1:
  1302. next_marker = self._markers[i + 1]
  1303. else:
  1304. next_marker = None
  1305. # End of current marker
  1306. end = next_marker[0] if next_marker else len(self.value)
  1307. if marker_type is _TYPE_STRING:
  1308. # end - 1 to strip off the null terminator
  1309. s += ' "{}"'.format(_decode_and_escape(
  1310. self.value[pos:end - 1]))
  1311. if end != len(self.value):
  1312. s += ","
  1313. elif marker_type is _REF_PATH:
  1314. s += " &" + ref
  1315. if end != len(self.value):
  1316. s += ","
  1317. else:
  1318. # <> or []
  1319. if marker_type is _REF_LABEL:
  1320. s += " {}:".format(ref)
  1321. elif marker_type is _REF_PHANDLE:
  1322. s += " &" + ref
  1323. pos += 4
  1324. # Subtle: There might be more data between the phandle and
  1325. # the next marker, so we can't 'continue' here
  1326. else: # marker_type is _TYPE_UINT*
  1327. elm_size = _TYPE_TO_N_BYTES[marker_type]
  1328. s += _N_BYTES_TO_START_STR[elm_size]
  1329. while pos != end:
  1330. num = int.from_bytes(self.value[pos:pos + elm_size],
  1331. "big")
  1332. if elm_size == 1:
  1333. s += " {:02X}".format(num)
  1334. else:
  1335. s += " " + hex(num)
  1336. pos += elm_size
  1337. if pos != 0 and \
  1338. (not next_marker or
  1339. next_marker[1] not in (_REF_PHANDLE, _REF_LABEL)):
  1340. s += _N_BYTES_TO_END_STR[elm_size]
  1341. if pos != len(self.value):
  1342. s += ","
  1343. return s + ";"
  1344. def __repr__(self):
  1345. return "<Property '{}' at '{}' in '{}'>" \
  1346. .format(self.name, self.node.path, self.node.dt.filename)
  1347. #
  1348. # Internal functions
  1349. #
  1350. def _add_marker(self, marker_type, data=None):
  1351. # Helper for registering markers in the value that are processed after
  1352. # parsing. See _fixup_props(). 'marker_type' identifies the type of
  1353. # marker, and 'data' has any optional data associated with the marker.
  1354. # len(self.value) gives the current offset. This function is called
  1355. # while the value is built. We use a list instead of a tuple to be able
  1356. # to fix up offsets later (they might increase if the value includes
  1357. # path references, e.g. 'foo = &bar, <3>;', which are expanded later).
  1358. self._markers.append([len(self.value), marker_type, data])
  1359. # For phandle references, add a dummy value with the same length as a
  1360. # phandle. This is handy for the length check in _register_phandles().
  1361. if marker_type is _REF_PHANDLE:
  1362. self.value += b"\0\0\0\0"
  1363. #
  1364. # Public functions
  1365. #
  1366. def to_num(data, length=None, signed=False):
  1367. """
  1368. Converts the 'bytes' array 'data' to a number. The value is expected to be
  1369. in big-endian format, which is standard in devicetree.
  1370. length (default: None):
  1371. The expected length of the value in bytes, as a simple type check. If
  1372. None, the length check is skipped.
  1373. signed (default: False):
  1374. If True, the value will be interpreted as signed rather than unsigned.
  1375. """
  1376. _check_is_bytes(data)
  1377. if length is not None:
  1378. _check_length_positive(length)
  1379. if len(data) != length:
  1380. _err("{} is {} bytes long, expected {}"
  1381. .format(data, len(data), length))
  1382. return int.from_bytes(data, "big", signed=signed)
  1383. def to_nums(data, length=4, signed=False):
  1384. """
  1385. Like Property.to_nums(), but takes an arbitrary 'bytes' array. The values
  1386. are assumed to be in big-endian format, which is standard in devicetree.
  1387. """
  1388. _check_is_bytes(data)
  1389. _check_length_positive(length)
  1390. if len(data) % length:
  1391. _err("{} is {} bytes long, expected a length that's a a multiple of {}"
  1392. .format(data, len(data), length))
  1393. return [int.from_bytes(data[i:i + length], "big", signed=signed)
  1394. for i in range(0, len(data), length)]
  1395. #
  1396. # Public constants
  1397. #
  1398. # See Property.type
  1399. TYPE_EMPTY = 0
  1400. TYPE_BYTES = 1
  1401. TYPE_NUM = 2
  1402. TYPE_NUMS = 3
  1403. TYPE_STRING = 4
  1404. TYPE_STRINGS = 5
  1405. TYPE_PATH = 6
  1406. TYPE_PHANDLE = 7
  1407. TYPE_PHANDLES = 8
  1408. TYPE_PHANDLES_AND_NUMS = 9
  1409. TYPE_COMPOUND = 10
  1410. def _check_is_bytes(data):
  1411. if not isinstance(data, bytes):
  1412. _err("'{}' has type '{}', expected 'bytes'"
  1413. .format(data, type(data).__name__))
  1414. def _check_length_positive(length):
  1415. if length < 1:
  1416. _err("'length' must be greater than zero, was " + str(length))
  1417. def _append_no_dup(lst, elm):
  1418. # Appends 'elm' to 'lst', but only if it isn't already in 'lst'. Lets us
  1419. # preserve order, which a set() doesn't.
  1420. if elm not in lst:
  1421. lst.append(elm)
  1422. def _decode_and_escape(b):
  1423. # Decodes the 'bytes' array 'b' as UTF-8 and backslash-escapes special
  1424. # characters
  1425. # Hacky but robust way to avoid double-escaping any '\' spit out by
  1426. # 'backslashreplace' bytes.translate() can't map to more than a single
  1427. # byte, but str.translate() can map to more than one character, so it's
  1428. # nice here. There's probably a nicer way to do this.
  1429. return b.decode("utf-8", "surrogateescape") \
  1430. .translate(_escape_table) \
  1431. .encode("utf-8", "surrogateescape") \
  1432. .decode("utf-8", "backslashreplace")
  1433. def _root_and_path_to_node(cur, path, fullpath):
  1434. # Returns the node pointed at by 'path', relative to the Node 'cur'. For
  1435. # example, if 'cur' has path /foo/bar, and 'path' is "baz/qaz", then the
  1436. # node with path /foo/bar/baz/qaz is returned. 'fullpath' is the path as
  1437. # given in the .dts file, for error messages.
  1438. for component in path.split("/"):
  1439. # Collapse multiple / in a row, and allow a / at the end
  1440. if not component:
  1441. continue
  1442. if component not in cur.nodes:
  1443. _err("component '{}' in path '{}' does not exist"
  1444. .format(component, fullpath))
  1445. cur = cur.nodes[component]
  1446. return cur
  1447. def _err(msg):
  1448. raise DTError(msg)
  1449. _escape_table = str.maketrans({
  1450. "\\": "\\\\",
  1451. '"': '\\"',
  1452. "\a": "\\a",
  1453. "\b": "\\b",
  1454. "\t": "\\t",
  1455. "\n": "\\n",
  1456. "\v": "\\v",
  1457. "\f": "\\f",
  1458. "\r": "\\r"})
  1459. class DTError(Exception):
  1460. "Exception raised for devicetree-related errors"
  1461. _Token = collections.namedtuple("Token", "id val")
  1462. # Lexer states
  1463. _DEFAULT = 0
  1464. _EXPECT_PROPNODENAME = 1
  1465. _EXPECT_BYTE = 2
  1466. _num_re = re.compile(r"(0[xX][0-9a-fA-F]+|[0-9]+)(?:ULL|UL|LL|U|L)?")
  1467. # A leading \ is allowed property and node names, probably to allow weird node
  1468. # names that would clash with other stuff
  1469. _propnodename_re = re.compile(r"\\?([a-zA-Z0-9,._+*#?@-]+)")
  1470. # Misc. tokens that are tried after a property/node name. This is important, as
  1471. # there's overlap with the allowed characters in names.
  1472. _misc_re = re.compile(
  1473. "|".join(re.escape(pat) for pat in (
  1474. "==", "!=", "!", "=", ",", ";", "+", "-", "*", "/", "%", "~", "?", ":",
  1475. "^", "(", ")", "{", "}", "[", "]", "<<", "<=", "<", ">>", ">=", ">",
  1476. "||", "|", "&&", "&")))
  1477. _byte_re = re.compile(r"[0-9a-fA-F]{2}")
  1478. # Matches a backslash escape within a 'bytes' array. Captures the 'c' part of
  1479. # '\c', where c might be a single character or an octal/hex escape.
  1480. _unescape_re = re.compile(br'\\([0-7]{1,3}|x[0-9A-Fa-f]{1,2}|.)')
  1481. # #line directive (this is the regex the C tools use)
  1482. _line_re = re.compile(
  1483. r'^#(?:line)?[ \t]+([0-9]+)[ \t]+"((?:[^\\"]|\\.)*)"(?:[ \t]+[0-9]+)?',
  1484. re.MULTILINE)
  1485. def _init_tokens():
  1486. # Builds a (<token 1>)|(<token 2>)|... regex and assigns the index of each
  1487. # capturing group to a corresponding _T_<TOKEN> variable. This makes the
  1488. # token type appear in match.lastindex after a match.
  1489. global _token_re
  1490. global _T_NUM
  1491. global _T_PROPNODENAME
  1492. global _T_MISC
  1493. global _T_BYTE
  1494. global _T_BAD
  1495. # Each pattern must have exactly one capturing group, which can capture any
  1496. # part of the pattern. This makes match.lastindex match the token type.
  1497. # _Token.val is based on the captured string.
  1498. token_spec = (("_T_INCLUDE", r'(/include/\s*"(?:[^\\"]|\\.)*")'),
  1499. ("_T_LINE", # #line directive
  1500. r'^#(?:line)?[ \t]+([0-9]+[ \t]+"(?:[^\\"]|\\.)*")(?:[ \t]+[0-9]+)?'),
  1501. ("_T_STRING", r'"((?:[^\\"]|\\.)*)"'),
  1502. ("_T_DTS_V1", r"(/dts-v1/)"),
  1503. ("_T_PLUGIN", r"(/plugin/)"),
  1504. ("_T_MEMRESERVE", r"(/memreserve/)"),
  1505. ("_T_BITS", r"(/bits/)"),
  1506. ("_T_DEL_PROP", r"(/delete-property/)"),
  1507. ("_T_DEL_NODE", r"(/delete-node/)"),
  1508. ("_T_OMIT_IF_NO_REF", r"(/omit-if-no-ref/)"),
  1509. ("_T_LABEL", r"([a-zA-Z_][a-zA-Z0-9_]*):"),
  1510. ("_T_CHAR_LITERAL", r"'((?:[^\\']|\\.)*)'"),
  1511. ("_T_REF",
  1512. r"&([a-zA-Z_][a-zA-Z0-9_]*|{[a-zA-Z0-9,._+*#?@/-]*})"),
  1513. ("_T_INCBIN", r"(/incbin/)"),
  1514. # Whitespace, C comments, and C++ comments
  1515. ("_T_SKIP", r"(\s+|(?:/\*(?:.|\n)*?\*/)|//.*$)"),
  1516. # Return a token for end-of-file so that the parsing code can
  1517. # always assume that there are more tokens when looking
  1518. # ahead. This simplifies things.
  1519. ("_T_EOF", r"(\Z)"))
  1520. # MULTILINE is needed for C++ comments and #line directives
  1521. _token_re = re.compile("|".join(spec[1] for spec in token_spec),
  1522. re.MULTILINE | re.ASCII)
  1523. for i, spec in enumerate(token_spec, 1):
  1524. globals()[spec[0]] = i
  1525. # pylint: disable=undefined-loop-variable
  1526. _T_NUM = i + 1
  1527. _T_PROPNODENAME = i + 2
  1528. _T_MISC = i + 3
  1529. _T_BYTE = i + 4
  1530. _T_BAD = i + 5
  1531. _init_tokens()
  1532. # Markers in property values
  1533. # References
  1534. _REF_PATH = 0 # &foo
  1535. _REF_PHANDLE = 1 # <&foo>
  1536. _REF_LABEL = 2 # foo: <1 2 3>
  1537. # Start of data blocks of specific type
  1538. _TYPE_UINT8 = 3 # [00 01 02] (and also used for /incbin/)
  1539. _TYPE_UINT16 = 4 # /bits/ 16 <1 2 3>
  1540. _TYPE_UINT32 = 5 # <1 2 3>
  1541. _TYPE_UINT64 = 6 # /bits/ 64 <1 2 3>
  1542. _TYPE_STRING = 7 # "foo"
  1543. _TYPE_TO_N_BYTES = {
  1544. _TYPE_UINT8: 1,
  1545. _TYPE_UINT16: 2,
  1546. _TYPE_UINT32: 4,
  1547. _TYPE_UINT64: 8,
  1548. }
  1549. _N_BYTES_TO_TYPE = {
  1550. 1: _TYPE_UINT8,
  1551. 2: _TYPE_UINT16,
  1552. 4: _TYPE_UINT32,
  1553. 8: _TYPE_UINT64,
  1554. }
  1555. _N_BYTES_TO_START_STR = {
  1556. 1: " [",
  1557. 2: " /bits/ 16 <",
  1558. 4: " <",
  1559. 8: " /bits/ 64 <",
  1560. }
  1561. _N_BYTES_TO_END_STR = {
  1562. 1: " ]",
  1563. 2: " >",
  1564. 4: " >",
  1565. 8: " >",
  1566. }