board_inspector: extend DSDT parser to allow parsing arbitrary trees

With AML templates for devices in the board XML, the parser now needs to be able
to parse a stream as an arbitrary object. This patch adds the `parse_tree`
method to the acpiparser.aml.parser module for this purpose.

Tracked-On: #6287
Signed-off-by: Junjie Mao <junjie.mao@intel.com>
This commit is contained in:
Junjie Mao 2021-07-28 10:44:52 +08:00 committed by wenlingz
parent 94d517b514
commit 5cf9ac714c
2 changed files with 24 additions and 11 deletions

View File

@ -167,15 +167,20 @@ class Context:
# Mode switches
self.__skip_external_on_lookup = False
def switch_stream(self, filepath):
if not filepath in self.streams.keys():
with open(filepath, "rb") as f:
stream = Stream(f.read())
self.streams[filepath] = stream
self.current_stream = stream
def switch_stream(self, val):
if isinstance(val, str):
if not val in self.streams.keys():
with open(val, "rb") as f:
stream = Stream(f.read())
self.streams[val] = stream
self.current_stream = stream
else:
self.current_stream = self.streams[val]
self.current_stream.reset()
elif isinstance(val, (bytes, bytearray)):
self.current_stream = Stream(val)
else:
self.current_stream = self.streams[filepath]
self.current_stream.reset()
raise NotImplementedError(f"Cannot use {val} as a stream.")
def get_scope(self):
return self.realpath(self.__current_scope, "")

View File

@ -7,7 +7,7 @@ import os
import logging
from acpiparser.aml.stream import Stream
from acpiparser.aml.parser import AMLCode, DeferredExpansion
from acpiparser.aml import parser
from acpiparser.aml.tree import Tree
from acpiparser.aml.context import Context
from acpiparser.aml.interpreter import ConcreteInterpreter
@ -26,8 +26,8 @@ def DSDT(val):
logging.info(f"Loading {t}")
context.switch_stream(t)
tree = Tree()
AMLCode.parse(context, tree)
tree = DeferredExpansion(context).transform(tree)
parser.AMLCode.parse(context, tree)
tree = parser.DeferredExpansion(context).transform(tree)
context.trees[os.path.basename(t)] = tree
except Exception as e:
context.current_stream.dump()
@ -38,3 +38,11 @@ def DSDT(val):
for tree in context.trees.values():
visitor.visit(tree)
return context
def parse_tree(label, data):
context = Context()
context.switch_stream(data)
tree = Tree()
getattr(parser, label).parse(context, tree)
tree = parser.DeferredExpansion(context).transform(tree)
return tree